blob: c856298def7d3a0592484c705fc3eb00abc62a35 [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"
Chuck Leverf0920752012-05-21 22:45:41 -040067#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebusta78cb572009-08-09 15:06:19 -040074#define NFS4_MAX_LOOP_ON_RECOVER (10)
75
Trond Myklebustef159e92012-02-06 19:50:40 -050076static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
77
Trond Myklebustcdd4e682006-01-03 09:55:12 +010078struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010079static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080080static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050082static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050083static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040084static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040085static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040086static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
87 struct nfs_fattr *fattr, struct iattr *sattr,
88 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050094static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust52567b02009-10-23 14:46:42 -040096 if (err >= -1000)
97 return err;
98 switch (err) {
99 case -NFS4ERR_RESOURCE:
100 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000101 case -NFS4ERR_WRONGSEC:
102 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
105 return -EINVAL;
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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400794 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400796 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100798 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400799 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100800 int rpc_status;
801 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802};
803
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400804
805static void nfs4_init_opendata_res(struct nfs4_opendata *p)
806{
807 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400808 p->o_res.seqid = p->o_arg.seqid;
809 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400810 p->o_res.server = p->o_arg.server;
811 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500812 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813}
814
Al Viro82a2c1b2011-06-22 18:30:55 -0400815static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 const struct iattr *attrs,
818 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819{
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct inode *dir = parent->d_inode;
822 struct nfs_server *server = NFS_SERVER(dir);
823 struct nfs4_opendata *p;
824
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400825 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 if (p == NULL)
827 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p->o_arg.seqid == NULL)
830 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400831 nfs_sb_active(dentry->d_sb);
832 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 p->dir = parent;
834 p->owner = sp;
835 atomic_inc(&sp->so_count);
836 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837 p->o_arg.open_flags = flags;
838 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400839 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400840 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
841 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400842 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 p->o_arg.server = server;
844 p->o_arg.bitmask = server->attr_bitmask;
845 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500846 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500847 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400848
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.u.attrs = &p->attrs;
850 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500851
852 verf[0] = jiffies;
853 verf[1] = current->pid;
854 memcpy(p->o_arg.u.verifier.data, verf,
855 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100857 p->c_arg.fh = &p->o_res.fh;
858 p->c_arg.stateid = &p->o_res.stateid;
859 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400860 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400861 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 return p;
863err_free:
864 kfree(p);
865err:
866 dput(parent);
867 return NULL;
868}
869
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400870static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100871{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872 struct nfs4_opendata *p = container_of(kref,
873 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400874 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400875
876 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877 if (p->state != NULL)
878 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400879 nfs4_put_state_owner(p->owner);
880 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400881 dput(p->dentry);
882 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500883 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kfree(p);
885}
886
887static void nfs4_opendata_put(struct nfs4_opendata *p)
888{
889 if (p != NULL)
890 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891}
892
Trond Myklebust06f814a2006-01-03 09:55:07 +0100893static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
894{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895 int ret;
896
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100898 return ret;
899}
900
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400902{
903 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904
Trond Myklebust536e43d2012-01-17 22:04:26 -0500905 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 goto out;
907 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400908 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500909 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
910 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 break;
912 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500913 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
914 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400915 break;
916 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500917 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
918 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400919 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400925{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500926 if (delegation == NULL)
927 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500930 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500932 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 1;
934}
935
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100937{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100939 case FMODE_WRITE:
940 state->n_wronly++;
941 break;
942 case FMODE_READ:
943 state->n_rdonly++;
944 break;
945 case FMODE_READ|FMODE_WRITE:
946 state->n_rdwr++;
947 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400952{
953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500954 nfs4_stateid_copy(&state->stateid, stateid);
955 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400957 case FMODE_READ:
958 set_bit(NFS_O_RDONLY_STATE, &state->flags);
959 break;
960 case FMODE_WRITE:
961 set_bit(NFS_O_WRONLY_STATE, &state->flags);
962 break;
963 case FMODE_READ|FMODE_WRITE:
964 set_bit(NFS_O_RDWR_STATE, &state->flags);
965 }
966}
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400969{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400970 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400973}
974
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975static 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 -0700976{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400977 /*
978 * Protect the call to nfs4_state_set_mode_locked and
979 * serialise the stateid update
980 */
981 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500983 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 set_bit(NFS_DELEGATED_STATE, &state->flags);
985 }
986 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400988 write_sequnlock(&state->seqlock);
989 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700991 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static 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 -0500995{
996 struct nfs_inode *nfsi = NFS_I(state->inode);
997 struct nfs_delegation *deleg_cur;
998 int ret = 0;
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001001
1002 rcu_read_lock();
1003 deleg_cur = rcu_dereference(nfsi->delegation);
1004 if (deleg_cur == NULL)
1005 goto no_delegation;
1006
1007 spin_lock(&deleg_cur->lock);
1008 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001010 goto no_delegation_unlock;
1011
1012 if (delegation == NULL)
1013 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001014 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001015 goto no_delegation_unlock;
1016
Trond Myklebustb7391f42008-12-23 15:21:52 -05001017 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001019 ret = 1;
1020no_delegation_unlock:
1021 spin_unlock(&deleg_cur->lock);
1022no_delegation:
1023 rcu_read_unlock();
1024
1025 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001027 ret = 1;
1028 }
1029
1030 return ret;
1031}
1032
1033
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035{
1036 struct nfs_delegation *delegation;
1037
1038 rcu_read_lock();
1039 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 rcu_read_unlock();
1042 return;
1043 }
1044 rcu_read_unlock();
1045 nfs_inode_return_delegation(inode);
1046}
1047
Trond Myklebust6ee41262007-07-08 14:11:36 -04001048static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049{
1050 struct nfs4_state *state = opendata->state;
1051 struct nfs_inode *nfsi = NFS_I(state->inode);
1052 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001053 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 nfs4_stateid stateid;
1056 int ret = -EAGAIN;
1057
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001060 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
1062 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 goto out_return_state;
1065 }
1066 spin_unlock(&state->owner->so_lock);
1067 }
Trond Myklebust34310432008-12-23 15:21:38 -05001068 rcu_read_lock();
1069 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001070 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001071 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001072 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001073 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001075 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001077 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 if (ret != 0)
1079 goto out;
1080 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001081
1082 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001084 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086out:
1087 return ERR_PTR(ret);
1088out_return_state:
1089 atomic_inc(&state->count);
1090 return state;
1091}
1092
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001093static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1094{
1095 struct inode *inode;
1096 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001097 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001098 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001099
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 goto out;
1103 }
1104
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001106 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001110 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 goto err;
1112 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001116 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001117 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 int delegation_flags = 0;
1119
Trond Myklebust003707c2007-07-05 18:07:55 -04001120 rcu_read_lock();
1121 delegation = rcu_dereference(NFS_I(inode)->delegation);
1122 if (delegation)
1123 delegation_flags = delegation->flags;
1124 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001125 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1126 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1127 "returning a delegation for "
1128 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001129 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001130 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001131 nfs_inode_set_delegation(state->inode,
1132 data->owner->so_cred,
1133 &data->o_res);
1134 else
1135 nfs_inode_reclaim_delegation(state->inode,
1136 data->owner->so_cred,
1137 &data->o_res);
1138 }
Trond Myklebust34310432008-12-23 15:21:38 -05001139
1140 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145err_put_inode:
1146 iput(inode);
1147err:
1148 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149}
1150
Trond Myklebust864472e2006-01-03 09:55:15 +01001151static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1152{
1153 struct nfs_inode *nfsi = NFS_I(state->inode);
1154 struct nfs_open_context *ctx;
1155
1156 spin_lock(&state->inode->i_lock);
1157 list_for_each_entry(ctx, &nfsi->open_files, list) {
1158 if (ctx->state != state)
1159 continue;
1160 get_nfs_open_context(ctx);
1161 spin_unlock(&state->inode->i_lock);
1162 return ctx;
1163 }
1164 spin_unlock(&state->inode->i_lock);
1165 return ERR_PTR(-ENOENT);
1166}
1167
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001168static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1169{
1170 struct nfs4_opendata *opendata;
1171
Al Viro3d4ff432011-06-22 18:40:12 -04001172 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001173 if (opendata == NULL)
1174 return ERR_PTR(-ENOMEM);
1175 opendata->state = state;
1176 atomic_inc(&state->count);
1177 return opendata;
1178}
1179
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001180static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001181{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 int ret;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 opendata->o_arg.open_flags = 0;
1186 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1188 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1189 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001190 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 if (ret != 0)
1192 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001194 if (IS_ERR(newstate))
1195 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001196 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 return 0;
1199}
1200
1201static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1202{
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 int ret;
1205
1206 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 smp_rmb();
1209 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
1225 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001226 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 if (ret != 0)
1229 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 if (newstate != state)
1231 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001233 /*
1234 * We may have performed cached opens for all three recoveries.
1235 * Check if we need to update the current stateid.
1236 */
1237 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001238 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001239 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001241 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001242 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001243 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 return 0;
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/*
1248 * OPEN_RECLAIM:
1249 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001251static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001253 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001255 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int status;
1257
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001258 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1259 if (IS_ERR(opendata))
1260 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1262 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001263 rcu_read_lock();
1264 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001265 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001266 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001267 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 opendata->o_arg.u.delegation_type = delegation_type;
1269 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return status;
1272}
1273
Trond Myklebust539cd032007-06-05 11:46:42 -04001274static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct nfs_server *server = NFS_SERVER(state->inode);
1277 struct nfs4_exception exception = { };
1278 int err;
1279 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001280 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001281 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001282 break;
1283 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } while (exception.retry);
1285 return err;
1286}
1287
Trond Myklebust864472e2006-01-03 09:55:15 +01001288static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1289{
1290 struct nfs_open_context *ctx;
1291 int ret;
1292
1293 ctx = nfs4_state_find_open_context(state);
1294 if (IS_ERR(ctx))
1295 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001296 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 put_nfs_open_context(ctx);
1298 return ret;
1299}
1300
Trond Myklebust13437e12007-07-06 15:10:43 -04001301static 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 -07001302{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001306 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1307 if (IS_ERR(opendata))
1308 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001309 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001310 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Trond Myklebust13437e12007-07-06 15:10:43 -04001316int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001319 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int err;
1321 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001322 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 switch (err) {
1324 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001325 case -ENOENT:
1326 case -ESTALE:
1327 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001328 case -NFS4ERR_BADSESSION:
1329 case -NFS4ERR_BADSLOT:
1330 case -NFS4ERR_BAD_HIGH_SLOT:
1331 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1332 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001333 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case -NFS4ERR_STALE_CLIENTID:
1336 case -NFS4ERR_STALE_STATEID:
1337 case -NFS4ERR_EXPIRED:
1338 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001339 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001340 goto out;
1341 case -ERESTARTSYS:
1342 /*
1343 * The show must go on: exit, but mark the
1344 * stateid as needing recovery.
1345 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001346 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 case -NFS4ERR_ADMIN_REVOKED:
1348 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001349 nfs_inode_find_state_and_recover(state->inode,
1350 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001351 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001352 case -EKEYEXPIRED:
1353 /*
1354 * User RPCSEC_GSS context has expired.
1355 * We cannot recover this stateid now, so
1356 * skip it and allow recovery thread to
1357 * proceed.
1358 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001359 case -ENOMEM:
1360 err = 0;
1361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 err = nfs4_handle_exception(server, err, &exception);
1364 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return err;
1367}
1368
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001369static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1370{
1371 struct nfs4_opendata *data = calldata;
1372
1373 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001374 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001375 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001376 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001378 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380}
1381
1382static void nfs4_open_confirm_release(void *calldata)
1383{
1384 struct nfs4_opendata *data = calldata;
1385 struct nfs4_state *state = NULL;
1386
1387 /* If this request hasn't been cancelled, do nothing */
1388 if (data->cancelled == 0)
1389 goto out_free;
1390 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001391 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001394 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001395 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001397 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398}
1399
1400static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 .rpc_call_done = nfs4_open_confirm_done,
1402 .rpc_release = nfs4_open_confirm_release,
1403};
1404
1405/*
1406 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1407 */
1408static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1409{
1410 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1411 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 struct rpc_message msg = {
1413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1414 .rpc_argp = &data->c_arg,
1415 .rpc_resp = &data->c_res,
1416 .rpc_cred = data->owner->so_cred,
1417 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 struct rpc_task_setup task_setup_data = {
1419 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001420 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001421 .callback_ops = &nfs4_open_confirm_ops,
1422 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001423 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 .flags = RPC_TASK_ASYNC,
1425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int status;
1427
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001429 data->rpc_done = 0;
1430 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001433 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001435 status = nfs4_wait_for_completion_rpc_task(task);
1436 if (status != 0) {
1437 data->cancelled = 1;
1438 smp_wmb();
1439 } else
1440 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001441 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return status;
1443}
1444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 struct nfs4_opendata *data = calldata;
1448 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1451 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 /*
1453 * Check if we still need to send an OPEN call, or if we can use
1454 * a delegation instead.
1455 */
1456 if (data->state != NULL) {
1457 struct nfs_delegation *delegation;
1458
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001460 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001461 rcu_read_lock();
1462 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001463 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1464 can_open_delegated(delegation, data->o_arg.fmode))
1465 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 rcu_read_unlock();
1467 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001468 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001469 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001470 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001472 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1473 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001474 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001475 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001476 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001477 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001478 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001479 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001480 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001481unlock_no_action:
1482 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001483out_no_action:
1484 task->tk_action = NULL;
1485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001488static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1489{
1490 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1491 nfs4_open_prepare(task, calldata);
1492}
1493
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494static void nfs4_open_done(struct rpc_task *task, void *calldata)
1495{
1496 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001499
Trond Myklebust14516c32010-07-31 14:29:06 -04001500 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1501 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503 if (task->tk_status == 0) {
1504 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 case S_IFREG:
1506 break;
1507 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001509 break;
1510 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 break;
1513 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001516 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001517 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1518 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001520 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523static void nfs4_open_release(void *calldata)
1524{
1525 struct nfs4_opendata *data = calldata;
1526 struct nfs4_state *state = NULL;
1527
1528 /* If this request hasn't been cancelled, do nothing */
1529 if (data->cancelled == 0)
1530 goto out_free;
1531 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 goto out_free;
1534 /* In case we need an open_confirm, no cleanup! */
1535 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1536 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001538 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001539 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001541 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542}
1543
1544static const struct rpc_call_ops nfs4_open_ops = {
1545 .rpc_call_prepare = nfs4_open_prepare,
1546 .rpc_call_done = nfs4_open_done,
1547 .rpc_release = nfs4_open_release,
1548};
1549
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001550static const struct rpc_call_ops nfs4_recover_open_ops = {
1551 .rpc_call_prepare = nfs4_recover_open_prepare,
1552 .rpc_call_done = nfs4_open_done,
1553 .rpc_release = nfs4_open_release,
1554};
1555
1556static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557{
1558 struct inode *dir = data->dir->d_inode;
1559 struct nfs_server *server = NFS_SERVER(dir);
1560 struct nfs_openargs *o_arg = &data->o_arg;
1561 struct nfs_openres *o_res = &data->o_res;
1562 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001563 struct rpc_message msg = {
1564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1565 .rpc_argp = o_arg,
1566 .rpc_resp = o_res,
1567 .rpc_cred = data->owner->so_cred,
1568 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001569 struct rpc_task_setup task_setup_data = {
1570 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001571 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 .callback_ops = &nfs4_open_ops,
1573 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001574 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 .flags = RPC_TASK_ASYNC,
1576 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001577 int status;
1578
Trond Myklebust9d12b212012-01-17 22:04:25 -05001579 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001580 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001581 data->rpc_done = 0;
1582 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001583 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584 if (isrecover)
1585 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001587 if (IS_ERR(task))
1588 return PTR_ERR(task);
1589 status = nfs4_wait_for_completion_rpc_task(task);
1590 if (status != 0) {
1591 data->cancelled = 1;
1592 smp_wmb();
1593 } else
1594 status = data->rpc_status;
1595 rpc_put_task(task);
1596
1597 return status;
1598}
1599
1600static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1601{
1602 struct inode *dir = data->dir->d_inode;
1603 struct nfs_openres *o_res = &data->o_res;
1604 int status;
1605
1606 status = nfs4_run_open_task(data, 1);
1607 if (status != 0 || !data->rpc_done)
1608 return status;
1609
Trond Myklebust6926afd2012-01-07 13:22:46 -05001610 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1611
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1613 status = _nfs4_proc_open_confirm(data);
1614 if (status != 0)
1615 return status;
1616 }
1617
1618 return status;
1619}
1620
1621/*
1622 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1623 */
1624static int _nfs4_proc_open(struct nfs4_opendata *data)
1625{
1626 struct inode *dir = data->dir->d_inode;
1627 struct nfs_server *server = NFS_SERVER(dir);
1628 struct nfs_openargs *o_arg = &data->o_arg;
1629 struct nfs_openres *o_res = &data->o_res;
1630 int status;
1631
1632 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001633 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001635 if (status != 0) {
1636 if (status == -NFS4ERR_BADNAME &&
1637 !(o_arg->open_flags & O_CREAT))
1638 return -ENOENT;
1639 return status;
1640 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641
Trond Myklebust6926afd2012-01-07 13:22:46 -05001642 nfs_fattr_map_and_free_names(server, &data->f_attr);
1643
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001644 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001645 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001646 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1647 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001649 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001654 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Andy Adamsond83217c2011-03-01 01:34:17 +00001658static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001659{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001660 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001661 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001662
Trond Myklebusta78cb572009-08-09 15:06:19 -04001663 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001664 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001665 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001666 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001667 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1668 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001669 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001670 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001672 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001673 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001674}
1675
Andy Adamsond83217c2011-03-01 01:34:17 +00001676static int nfs4_recover_expired_lease(struct nfs_server *server)
1677{
1678 return nfs4_client_recover_expired_lease(server->nfs_client);
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * OPEN_EXPIRED:
1683 * reclaim state on the server after a network partition.
1684 * Assumes caller holds the appropriate lock
1685 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001686static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001688 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001691 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1692 if (IS_ERR(opendata))
1693 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001694 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001695 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001696 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001697 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001701static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001702{
Trond Myklebust539cd032007-06-05 11:46:42 -04001703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 struct nfs4_exception exception = { };
1705 int err;
1706
1707 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001708 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709 switch (err) {
1710 default:
1711 goto out;
1712 case -NFS4ERR_GRACE:
1713 case -NFS4ERR_DELAY:
1714 nfs4_handle_exception(server, err, &exception);
1715 err = 0;
1716 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001717 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001718out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001719 return err;
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 ctx = nfs4_state_find_open_context(state);
1728 if (IS_ERR(ctx))
1729 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 put_nfs_open_context(ctx);
1732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001736static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001738 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001739 struct nfs_server *server = NFS_SERVER(state->inode);
1740
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001741 if (state->flags & flags) {
1742 status = nfs41_test_stateid(server, stateid);
1743 if (status != NFS_OK) {
1744 nfs41_free_stateid(server, stateid);
1745 state->flags &= ~flags;
1746 }
1747 }
1748 return status;
1749}
1750
1751static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1752{
1753 int deleg_status, open_status;
1754 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1755 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1756
1757 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1758 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1759
1760 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1761 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762 return nfs4_open_expired(sp, state);
1763}
1764#endif
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001767 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1768 * fields corresponding to attributes that were used to store the verifier.
1769 * Make sure we clobber those fields in the later setattr call
1770 */
1771static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1772{
1773 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1774 !(sattr->ia_valid & ATTR_ATIME_SET))
1775 sattr->ia_valid |= ATTR_ATIME;
1776
1777 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1778 !(sattr->ia_valid & ATTR_MTIME_SET))
1779 sattr->ia_valid |= ATTR_MTIME;
1780}
1781
1782/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001785static 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 -07001786{
1787 struct nfs4_state_owner *sp;
1788 struct nfs4_state *state = NULL;
1789 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001790 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001791 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001795 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1796 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1798 goto out_err;
1799 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001800 status = nfs4_recover_expired_lease(server);
1801 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001802 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001803 if (dentry->d_inode != NULL)
1804 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001805 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001806 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001807 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001808 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Al Viro82a2c1b2011-06-22 18:30:55 -04001810 if (dentry->d_inode != NULL)
1811 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001812
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001815 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001817 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001818 status = PTR_ERR(state);
1819 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001820 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001821 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001822 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001823
1824 if (opendata->o_arg.open_flags & O_EXCL) {
1825 nfs4_exclusive_attrset(opendata, sattr);
1826
1827 nfs_fattr_init(opendata->o_res.f_attr);
1828 status = nfs4_do_setattr(state->inode, cred,
1829 opendata->o_res.f_attr, sattr,
1830 state);
1831 if (status == 0)
1832 nfs_setattr_update_inode(state->inode, sattr);
1833 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1834 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001835 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 *res = state;
1838 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001839err_opendata_put:
1840 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001841err_put_state_owner:
1842 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 *res = NULL;
1845 return status;
1846}
1847
1848
Al Viro82a2c1b2011-06-22 18:30:55 -04001849static 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 -07001850{
1851 struct nfs4_exception exception = { };
1852 struct nfs4_state *res;
1853 int status;
1854
1855 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001856 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (status == 0)
1858 break;
1859 /* NOTE: BAD_SEQID means the server and client disagree about the
1860 * book-keeping w.r.t. state-changing operations
1861 * (OPEN/CLOSE/LOCK/LOCKU...)
1862 * It is actually a sign of a bug on the client or on the server.
1863 *
1864 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001865 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * have unhashed the old state_owner for us, and that we can
1867 * therefore safely retry using a new one. We should still warn
1868 * the user though...
1869 */
1870 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001871 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001872 " returned a bad sequence-id error!\n",
1873 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 exception.retry = 1;
1875 continue;
1876 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001877 /*
1878 * BAD_STATEID on OPEN means that the server cancelled our
1879 * state before it received the OPEN_CONFIRM.
1880 * Recover by retrying the request as per the discussion
1881 * on Page 181 of RFC3530.
1882 */
1883 if (status == -NFS4ERR_BAD_STATEID) {
1884 exception.retry = 1;
1885 continue;
1886 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001887 if (status == -EAGAIN) {
1888 /* We must have found a delegation */
1889 exception.retry = 1;
1890 continue;
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1893 status, &exception));
1894 } while (exception.retry);
1895 return res;
1896}
1897
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001898static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1899 struct nfs_fattr *fattr, struct iattr *sattr,
1900 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001902 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001904 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 .iap = sattr,
1906 .server = server,
1907 .bitmask = server->attr_bitmask,
1908 };
1909 struct nfs_setattrres res = {
1910 .fattr = fattr,
1911 .server = server,
1912 };
1913 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1915 .rpc_argp = &arg,
1916 .rpc_resp = &res,
1917 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001919 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001920 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Trond Myklebust0e574af2005-10-27 22:12:38 -04001922 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Trond Myklebust4fc87962012-03-08 17:42:01 -05001924 if (state != NULL) {
1925 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1926 current->files, current->tgid);
1927 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1928 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001929 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001930 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001931 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Bryan Schumaker7c513052011-03-24 17:12:24 +00001933 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001934 if (status == 0 && state != NULL)
1935 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001936 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001939static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1940 struct nfs_fattr *fattr, struct iattr *sattr,
1941 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001943 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001944 struct nfs4_exception exception = {
1945 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001946 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001947 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int err;
1949 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001950 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1951 switch (err) {
1952 case -NFS4ERR_OPENMODE:
1953 if (state && !(state->state & FMODE_WRITE)) {
1954 err = -EBADF;
1955 if (sattr->ia_valid & ATTR_OPEN)
1956 err = -EACCES;
1957 goto out;
1958 }
1959 }
1960 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return err;
1964}
1965
1966struct nfs4_closedata {
1967 struct inode *inode;
1968 struct nfs4_state *state;
1969 struct nfs_closeargs arg;
1970 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001971 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001972 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001973 bool roc;
1974 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975};
1976
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001977static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001978{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001979 struct nfs4_closedata *calldata = data;
1980 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001981 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001982
Fred Isamanf7e89172011-01-06 11:36:32 +00001983 if (calldata->roc)
1984 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001985 nfs4_put_open_state(calldata->state);
1986 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001987 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001988 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001989 kfree(calldata);
1990}
1991
Trond Myklebust88069f72009-12-08 08:33:16 -05001992static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1993 fmode_t fmode)
1994{
1995 spin_lock(&state->owner->so_lock);
1996 if (!(fmode & FMODE_READ))
1997 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1998 if (!(fmode & FMODE_WRITE))
1999 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2000 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2001 spin_unlock(&state->owner->so_lock);
2002}
2003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002004static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002006 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct nfs_server *server = NFS_SERVER(calldata->inode);
2009
Chuck Levera3ca5652012-03-01 17:00:40 -05002010 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002011 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* hmm. we are done with the inode, and in the process of freeing
2014 * the state_owner. we keep this around to process errors
2015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 switch (task->tk_status) {
2017 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002018 if (calldata->roc)
2019 pnfs_roc_set_barrier(state->inode,
2020 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002021 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002023 nfs4_close_clear_stateid_flags(state,
2024 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 break;
2026 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002027 case -NFS4ERR_OLD_STATEID:
2028 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002030 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002033 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2034 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002036 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002037 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002038 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002041static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002043 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002044 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002045 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002046
Chuck Levera3ca5652012-03-01 17:00:40 -05002047 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002048 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002049 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002050
Trond Myklebust88069f72009-12-08 08:33:16 -05002051 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2052 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002053 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002054 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002055 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002056 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002057 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2058 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2059 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002060 }
2061 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002062 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2063 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2064 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002065 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002066 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002067 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002068
2069 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002070 /* Note: exit _without_ calling nfs4_close_done */
2071 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002072 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002073 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002074
Fred Isamanf7e89172011-01-06 11:36:32 +00002075 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002076 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002077 if (calldata->roc &&
2078 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2079 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2080 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002081 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002082 }
2083 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002084
Trond Myklebust516a6af2005-10-27 22:12:41 -04002085 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002086 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002087 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002088 &calldata->arg.seq_args,
2089 &calldata->res.seq_res,
2090 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002091 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002092 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002093out:
2094 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002098 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002099 .rpc_call_done = nfs4_close_done,
2100 .rpc_release = nfs4_free_closedata,
2101};
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/*
2104 * It is possible for data to be read/written from a mem-mapped file
2105 * after the sys_close call (which hits the vfs layer as a flush).
2106 * This means that we can't safely call nfsv4 close on a file until
2107 * the inode is cleared. This in turn means that we are not good
2108 * NFSv4 citizens - we do not indicate to the server to update the file's
2109 * share state even when we are done with one of the three share
2110 * stateid's in the inode.
2111 *
2112 * NOTE: Caller must be holding the sp->so_owner semaphore!
2113 */
Al Viro643168c2011-06-22 18:20:23 -04002114int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002116 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002118 struct nfs4_state_owner *sp = state->owner;
2119 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002120 struct rpc_message msg = {
2121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2122 .rpc_cred = state->owner->so_cred,
2123 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002124 struct rpc_task_setup task_setup_data = {
2125 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002126 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002127 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002128 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002129 .flags = RPC_TASK_ASYNC,
2130 };
Trond Myklebust95121352005-10-18 14:20:12 -07002131 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002133 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002135 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002136 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002137 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002139 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002140 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002142 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002143 if (calldata->arg.seqid == NULL)
2144 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002145 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002146 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002147 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002148 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002149 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002150 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002151 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002152
Trond Myklebust1174dd12010-12-21 10:52:24 -05002153 msg.rpc_argp = &calldata->arg;
2154 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002155 task_setup_data.callback_data = calldata;
2156 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002157 if (IS_ERR(task))
2158 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002159 status = 0;
2160 if (wait)
2161 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002162 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002163 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002164out_free_calldata:
2165 kfree(calldata);
2166out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002167 if (roc)
2168 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002169 nfs4_put_open_state(state);
2170 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002171 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Trond Myklebust2b484292010-09-17 10:56:51 -04002174static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002175nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 struct nfs4_state *state;
2178
Trond Myklebust565277f2007-10-15 18:17:53 -04002179 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002180 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002181 if (IS_ERR(state))
2182 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002183 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002184 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Trond Myklebust1185a552009-12-03 15:54:02 -05002187static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002188{
2189 if (ctx->state == NULL)
2190 return;
2191 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002192 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002193 else
Al Viro643168c2011-06-22 18:20:23 -04002194 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2198{
Benny Halevy43652ad2009-04-01 09:21:54 -04002199 struct nfs4_server_caps_arg args = {
2200 .fhandle = fhandle,
2201 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct nfs4_server_caps_res res = {};
2203 struct rpc_message msg = {
2204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002205 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 .rpc_resp = &res,
2207 };
2208 int status;
2209
Bryan Schumaker7c513052011-03-24 17:12:24 +00002210 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (status == 0) {
2212 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002213 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2214 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2215 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2216 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2217 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2219 server->caps |= NFS_CAP_ACLS;
2220 if (res.has_links != 0)
2221 server->caps |= NFS_CAP_HARDLINKS;
2222 if (res.has_symlinks != 0)
2223 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002224 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2225 server->caps |= NFS_CAP_FILEID;
2226 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2227 server->caps |= NFS_CAP_MODE;
2228 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2229 server->caps |= NFS_CAP_NLINK;
2230 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2231 server->caps |= NFS_CAP_OWNER;
2232 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2233 server->caps |= NFS_CAP_OWNER_GROUP;
2234 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2235 server->caps |= NFS_CAP_ATIME;
2236 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2237 server->caps |= NFS_CAP_CTIME;
2238 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2239 server->caps |= NFS_CAP_MTIME;
2240
Trond Myklebusta65318b2009-03-11 14:10:28 -04002241 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2242 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2243 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002245 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return status;
2249}
2250
Trond Myklebust55a97592006-06-09 09:34:19 -04002251int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 struct nfs4_exception exception = { };
2254 int err;
2255 do {
2256 err = nfs4_handle_exception(server,
2257 _nfs4_server_capabilities(server, fhandle),
2258 &exception);
2259 } while (exception.retry);
2260 return err;
2261}
2262
2263static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2264 struct nfs_fsinfo *info)
2265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 struct nfs4_lookup_root_arg args = {
2267 .bitmask = nfs4_fattr_bitmap,
2268 };
2269 struct nfs4_lookup_res res = {
2270 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002271 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 .fh = fhandle,
2273 };
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2276 .rpc_argp = &args,
2277 .rpc_resp = &res,
2278 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002279
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002281 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2285 struct nfs_fsinfo *info)
2286{
2287 struct nfs4_exception exception = { };
2288 int err;
2289 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002290 err = _nfs4_lookup_root(server, fhandle, info);
2291 switch (err) {
2292 case 0:
2293 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002294 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002295 default:
2296 err = nfs4_handle_exception(server, err, &exception);
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002299out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return err;
2301}
2302
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002303static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2304 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2305{
2306 struct rpc_auth *auth;
2307 int ret;
2308
2309 auth = rpcauth_create(flavor, server->client);
2310 if (!auth) {
2311 ret = -EIO;
2312 goto out;
2313 }
2314 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002315out:
2316 return ret;
2317}
2318
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002319static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002320 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002322 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002323 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002325 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2326 flav_array[len] = RPC_AUTH_NULL;
2327 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002328
2329 for (i = 0; i < len; i++) {
2330 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002331 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002332 continue;
2333 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002334 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002335 /*
2336 * -EACCESS could mean that the user doesn't have correct permissions
2337 * to access the mount. It could also mean that we tried to mount
2338 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2339 * existing mount programs don't handle -EACCES very well so it should
2340 * be mapped to -EPERM instead.
2341 */
2342 if (status == -EACCES)
2343 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002344 return status;
2345}
2346
2347/*
2348 * get the file handle for the "/" directory on the server
2349 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002350int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2351 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002352{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002353 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002354 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002355 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2356 /*
2357 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2358 * by nfs4_map_errors() as this function exits.
2359 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002360 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (status == 0)
2362 status = nfs4_server_capabilities(server, fhandle);
2363 if (status == 0)
2364 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002365 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Bryan Schumakerbae36242012-05-10 15:07:31 -04002368static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2369 struct nfs_fsinfo *info)
2370{
2371 int error;
2372 struct nfs_fattr *fattr = info->fattr;
2373
2374 error = nfs4_server_capabilities(server, mntfh);
2375 if (error < 0) {
2376 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2377 return error;
2378 }
2379
2380 error = nfs4_proc_getattr(server, mntfh, fattr);
2381 if (error < 0) {
2382 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2383 return error;
2384 }
2385
2386 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2387 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2388 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2389
2390 return error;
2391}
2392
Manoj Naik6b97fd32006-06-09 09:34:29 -04002393/*
2394 * Get locations and (maybe) other attributes of a referral.
2395 * Note that we'll actually follow the referral later when
2396 * we detect fsid mismatch in inode revalidation
2397 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002398static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2399 const struct qstr *name, struct nfs_fattr *fattr,
2400 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002401{
2402 int status = -ENOMEM;
2403 struct page *page = NULL;
2404 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002405
2406 page = alloc_page(GFP_KERNEL);
2407 if (page == NULL)
2408 goto out;
2409 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2410 if (locations == NULL)
2411 goto out;
2412
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002413 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002414 if (status != 0)
2415 goto out;
2416 /* Make sure server returned a different fsid for the referral */
2417 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002418 dprintk("%s: server did not return a different fsid for"
2419 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002420 status = -EIO;
2421 goto out;
2422 }
Andy Adamson533eb462011-06-13 18:25:56 -04002423 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2424 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002425
Andy Adamson533eb462011-06-13 18:25:56 -04002426 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002427 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002428 memset(fhandle, 0, sizeof(struct nfs_fh));
2429out:
2430 if (page)
2431 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002432 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002433 return status;
2434}
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2437{
2438 struct nfs4_getattr_arg args = {
2439 .fh = fhandle,
2440 .bitmask = server->attr_bitmask,
2441 };
2442 struct nfs4_getattr_res res = {
2443 .fattr = fattr,
2444 .server = server,
2445 };
2446 struct rpc_message msg = {
2447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2448 .rpc_argp = &args,
2449 .rpc_resp = &res,
2450 };
2451
Trond Myklebust0e574af2005-10-27 22:12:38 -04002452 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002453 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2457{
2458 struct nfs4_exception exception = { };
2459 int err;
2460 do {
2461 err = nfs4_handle_exception(server,
2462 _nfs4_proc_getattr(server, fhandle, fattr),
2463 &exception);
2464 } while (exception.retry);
2465 return err;
2466}
2467
2468/*
2469 * The file is not closed if it is opened due to the a request to change
2470 * the size of the file. The open call will not be needed once the
2471 * VFS layer lookup-intents are implemented.
2472 *
2473 * Close is called when the inode is destroyed.
2474 * If we haven't opened the file for O_WRONLY, we
2475 * need to in the size_change case to obtain a stateid.
2476 *
2477 * Got race?
2478 * Because OPEN is always done by name in nfsv4, it is
2479 * possible that we opened a different file by the same
2480 * name. We can recognize this race condition, but we
2481 * can't do anything about it besides returning an error.
2482 *
2483 * This will be fixed with VFS changes (lookup-intent).
2484 */
2485static int
2486nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2487 struct iattr *sattr)
2488{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002489 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002490 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002491 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int status;
2493
Benny Halevy8a1636c2010-07-14 15:43:57 -04002494 if (pnfs_ld_layoutret_on_setattr(inode))
2495 pnfs_return_layout(inode);
2496
Trond Myklebust0e574af2005-10-27 22:12:38 -04002497 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Trond Myklebustd5308382005-11-04 15:33:38 -05002499 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002500 if (sattr->ia_valid & ATTR_FILE) {
2501 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002502
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002503 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002504 if (ctx) {
2505 cred = ctx->cred;
2506 state = ctx->state;
2507 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002508 }
2509
Trond Myklebust536e43d2012-01-17 22:04:26 -05002510 /* Deal with open(O_TRUNC) */
2511 if (sattr->ia_valid & ATTR_OPEN)
2512 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2513
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002514 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002515 if (status == 0)
2516 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return status;
2518}
2519
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002520static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2521 const struct qstr *name, struct nfs_fh *fhandle,
2522 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002523{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002524 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002525 int status;
2526 struct nfs4_lookup_arg args = {
2527 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002528 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002529 .name = name,
2530 };
2531 struct nfs4_lookup_res res = {
2532 .server = server,
2533 .fattr = fattr,
2534 .fh = fhandle,
2535 };
2536 struct rpc_message msg = {
2537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2538 .rpc_argp = &args,
2539 .rpc_resp = &res,
2540 };
2541
2542 nfs_fattr_init(fattr);
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002545 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 dprintk("NFS reply lookup: %d\n", status);
2547 return status;
2548}
2549
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002550static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002551{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002552 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002553 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002554 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2555 fattr->nlink = 2;
2556}
2557
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002558static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2559 struct qstr *name, struct nfs_fh *fhandle,
2560 struct nfs_fattr *fattr)
2561{
2562 struct nfs4_exception exception = { };
2563 struct rpc_clnt *client = *clnt;
2564 int err;
2565 do {
2566 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2567 switch (err) {
2568 case -NFS4ERR_BADNAME:
2569 err = -ENOENT;
2570 goto out;
2571 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002572 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002573 goto out;
2574 case -NFS4ERR_WRONGSEC:
2575 err = -EPERM;
2576 if (client != *clnt)
2577 goto out;
2578
2579 client = nfs4_create_sec_client(client, dir, name);
2580 if (IS_ERR(client))
2581 return PTR_ERR(client);
2582
2583 exception.retry = 1;
2584 break;
2585 default:
2586 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2587 }
2588 } while (exception.retry);
2589
2590out:
2591 if (err == 0)
2592 *clnt = client;
2593 else if (client != *clnt)
2594 rpc_shutdown_client(client);
2595
2596 return err;
2597}
2598
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002599static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002600 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002602 int status;
2603 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002604
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002605 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2606 if (client != NFS_CLIENT(dir)) {
2607 rpc_shutdown_client(client);
2608 nfs_fixup_secinfo_attributes(fattr);
2609 }
2610 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002613struct rpc_clnt *
2614nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2615 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2616{
2617 int status;
2618 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2619
2620 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2621 if (status < 0) {
2622 rpc_shutdown_client(client);
2623 return ERR_PTR(status);
2624 }
2625 return client;
2626}
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2629{
Trond Myklebust76b32992007-08-10 17:45:11 -04002630 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct nfs4_accessargs args = {
2632 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002633 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002635 struct nfs4_accessres res = {
2636 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002637 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 struct rpc_message msg = {
2639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2640 .rpc_argp = &args,
2641 .rpc_resp = &res,
2642 .rpc_cred = entry->cred,
2643 };
2644 int mode = entry->mask;
2645 int status;
2646
2647 /*
2648 * Determine which access bits we want to ask for...
2649 */
2650 if (mode & MAY_READ)
2651 args.access |= NFS4_ACCESS_READ;
2652 if (S_ISDIR(inode->i_mode)) {
2653 if (mode & MAY_WRITE)
2654 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2655 if (mode & MAY_EXEC)
2656 args.access |= NFS4_ACCESS_LOOKUP;
2657 } else {
2658 if (mode & MAY_WRITE)
2659 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2660 if (mode & MAY_EXEC)
2661 args.access |= NFS4_ACCESS_EXECUTE;
2662 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002663
2664 res.fattr = nfs_alloc_fattr();
2665 if (res.fattr == NULL)
2666 return -ENOMEM;
2667
Bryan Schumaker7c513052011-03-24 17:12:24 +00002668 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if (!status) {
2670 entry->mask = 0;
2671 if (res.access & NFS4_ACCESS_READ)
2672 entry->mask |= MAY_READ;
2673 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2674 entry->mask |= MAY_WRITE;
2675 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2676 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002677 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002679 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return status;
2681}
2682
2683static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2684{
2685 struct nfs4_exception exception = { };
2686 int err;
2687 do {
2688 err = nfs4_handle_exception(NFS_SERVER(inode),
2689 _nfs4_proc_access(inode, entry),
2690 &exception);
2691 } while (exception.retry);
2692 return err;
2693}
2694
2695/*
2696 * TODO: For the time being, we don't try to get any attributes
2697 * along with any of the zero-copy operations READ, READDIR,
2698 * READLINK, WRITE.
2699 *
2700 * In the case of the first three, we want to put the GETATTR
2701 * after the read-type operation -- this is because it is hard
2702 * to predict the length of a GETATTR response in v4, and thus
2703 * align the READ data correctly. This means that the GETATTR
2704 * may end up partially falling into the page cache, and we should
2705 * shift it into the 'tail' of the xdr_buf before processing.
2706 * To do this efficiently, we need to know the total length
2707 * of data received, which doesn't seem to be available outside
2708 * of the RPC layer.
2709 *
2710 * In the case of WRITE, we also want to put the GETATTR after
2711 * the operation -- in this case because we want to make sure
2712 * we get the post-operation mtime and size. This means that
2713 * we can't use xdr_encode_pages() as written: we need a variant
2714 * of it which would leave room in the 'tail' iovec.
2715 *
2716 * Both of these changes to the XDR layer would in fact be quite
2717 * minor, but I decided to leave them for a subsequent patch.
2718 */
2719static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2720 unsigned int pgbase, unsigned int pglen)
2721{
2722 struct nfs4_readlink args = {
2723 .fh = NFS_FH(inode),
2724 .pgbase = pgbase,
2725 .pglen = pglen,
2726 .pages = &page,
2727 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002728 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct rpc_message msg = {
2730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2731 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002732 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 };
2734
Bryan Schumaker7c513052011-03-24 17:12:24 +00002735 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 -07002736}
2737
2738static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2739 unsigned int pgbase, unsigned int pglen)
2740{
2741 struct nfs4_exception exception = { };
2742 int err;
2743 do {
2744 err = nfs4_handle_exception(NFS_SERVER(inode),
2745 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2746 &exception);
2747 } while (exception.retry);
2748 return err;
2749}
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751/*
2752 * Got race?
2753 * We will need to arrange for the VFS layer to provide an atomic open.
2754 * Until then, this create/open method is prone to inefficiency and race
2755 * conditions due to the lookup, create, and open VFS calls from sys_open()
2756 * placed on the wire.
2757 *
2758 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2759 * The file will be opened again in the subsequent VFS open call
2760 * (nfs4_proc_file_open).
2761 *
2762 * The open for read will just hang around to be used by any process that
2763 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2764 */
2765
2766static int
2767nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002768 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Al Viro82a2c1b2011-06-22 18:30:55 -04002770 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002772 struct rpc_cred *cred = NULL;
2773 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 int status = 0;
2775
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002776 if (ctx != NULL) {
2777 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002778 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002779 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002781 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002782 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002783 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (IS_ERR(state)) {
2785 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002788 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002789 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002790 if (ctx != NULL)
2791 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002792 else
Al Viro643168c2011-06-22 18:20:23 -04002793 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794out:
2795 return status;
2796}
2797
2798static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2799{
Trond Myklebust16e42952005-10-27 22:12:44 -04002800 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002801 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002803 .name.len = name->len,
2804 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002806 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002807 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2811 .rpc_argp = &args,
2812 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002814 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002815
Bryan Schumaker7c513052011-03-24 17:12:24 +00002816 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002817 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002818 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return status;
2820}
2821
2822static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2823{
2824 struct nfs4_exception exception = { };
2825 int err;
2826 do {
2827 err = nfs4_handle_exception(NFS_SERVER(dir),
2828 _nfs4_proc_remove(dir, name),
2829 &exception);
2830 } while (exception.retry);
2831 return err;
2832}
2833
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002834static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002836 struct nfs_server *server = NFS_SERVER(dir);
2837 struct nfs_removeargs *args = msg->rpc_argp;
2838 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002840 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002842 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002845static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2846{
2847 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2848 &data->args.seq_args,
2849 &data->res.seq_res,
2850 task))
2851 return;
2852 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002855static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002857 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2858
Trond Myklebust14516c32010-07-31 14:29:06 -04002859 if (!nfs4_sequence_done(task, &res->seq_res))
2860 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002861 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002862 return 0;
2863 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002864 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
Jeff Laytond3d41522010-09-17 17:31:57 -04002867static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2868{
2869 struct nfs_server *server = NFS_SERVER(dir);
2870 struct nfs_renameargs *arg = msg->rpc_argp;
2871 struct nfs_renameres *res = msg->rpc_resp;
2872
2873 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002874 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);
Jeff Laytond3d41522010-09-17 17:31:57 -04002899 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002900 return 1;
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2904 struct inode *new_dir, struct qstr *new_name)
2905{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002906 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002907 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 .old_dir = NFS_FH(old_dir),
2909 .new_dir = NFS_FH(new_dir),
2910 .old_name = old_name,
2911 .new_name = new_name,
2912 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002913 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002914 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002915 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 struct rpc_message msg = {
2917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2918 .rpc_argp = &arg,
2919 .rpc_resp = &res,
2920 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002921 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Bryan Schumaker7c513052011-03-24 17:12:24 +00002923 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (!status) {
2925 update_changeattr(old_dir, &res.old_cinfo);
2926 update_changeattr(new_dir, &res.new_cinfo);
2927 }
2928 return status;
2929}
2930
2931static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2932 struct inode *new_dir, struct qstr *new_name)
2933{
2934 struct nfs4_exception exception = { };
2935 int err;
2936 do {
2937 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2938 _nfs4_proc_rename(old_dir, old_name,
2939 new_dir, new_name),
2940 &exception);
2941 } while (exception.retry);
2942 return err;
2943}
2944
2945static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2946{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002947 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 struct nfs4_link_arg arg = {
2949 .fh = NFS_FH(inode),
2950 .dir_fh = NFS_FH(dir),
2951 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002952 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002954 struct nfs4_link_res res = {
2955 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct rpc_message msg = {
2958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2959 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002960 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002962 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Trond Myklebust136f2622010-04-16 16:22:49 -04002964 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04002965 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04002966 goto out;
2967
Bryan Schumaker7c513052011-03-24 17:12:24 +00002968 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002969 if (!status) {
2970 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002971 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002972 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002973out:
Trond Myklebust136f2622010-04-16 16:22:49 -04002974 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return status;
2976}
2977
2978static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2979{
2980 struct nfs4_exception exception = { };
2981 int err;
2982 do {
2983 err = nfs4_handle_exception(NFS_SERVER(inode),
2984 _nfs4_proc_link(inode, dir, name),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002990struct nfs4_createdata {
2991 struct rpc_message msg;
2992 struct nfs4_create_arg arg;
2993 struct nfs4_create_res res;
2994 struct nfs_fh fh;
2995 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002996};
2997
2998static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2999 struct qstr *name, struct iattr *sattr, u32 ftype)
3000{
3001 struct nfs4_createdata *data;
3002
3003 data = kzalloc(sizeof(*data), GFP_KERNEL);
3004 if (data != NULL) {
3005 struct nfs_server *server = NFS_SERVER(dir);
3006
3007 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3008 data->msg.rpc_argp = &data->arg;
3009 data->msg.rpc_resp = &data->res;
3010 data->arg.dir_fh = NFS_FH(dir);
3011 data->arg.server = server;
3012 data->arg.name = name;
3013 data->arg.attrs = sattr;
3014 data->arg.ftype = ftype;
3015 data->arg.bitmask = server->attr_bitmask;
3016 data->res.server = server;
3017 data->res.fh = &data->fh;
3018 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003019 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003020 }
3021 return data;
3022}
3023
3024static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3025{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003026 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003027 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003028 if (status == 0) {
3029 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003030 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3031 }
3032 return status;
3033}
3034
3035static void nfs4_free_createdata(struct nfs4_createdata *data)
3036{
3037 kfree(data);
3038}
3039
Chuck Lever4f390c12006-08-22 20:06:22 -04003040static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003041 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 struct nfs4_createdata *data;
3044 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Chuck Lever94a6d752006-08-22 20:06:23 -04003046 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003048
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003049 status = -ENOMEM;
3050 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3051 if (data == NULL)
3052 goto out;
3053
3054 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3055 data->arg.u.symlink.pages = &page;
3056 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 status = nfs4_do_create(dir, dentry, data);
3059
3060 nfs4_free_createdata(data);
3061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return status;
3063}
3064
Chuck Lever4f390c12006-08-22 20:06:22 -04003065static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003066 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
3068 struct nfs4_exception exception = { };
3069 int err;
3070 do {
3071 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003072 _nfs4_proc_symlink(dir, dentry, page,
3073 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 &exception);
3075 } while (exception.retry);
3076 return err;
3077}
3078
3079static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3080 struct iattr *sattr)
3081{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003082 struct nfs4_createdata *data;
3083 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003085 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3086 if (data == NULL)
3087 goto out;
3088
3089 status = nfs4_do_create(dir, dentry, data);
3090
3091 nfs4_free_createdata(data);
3092out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return status;
3094}
3095
3096static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3097 struct iattr *sattr)
3098{
3099 struct nfs4_exception exception = { };
3100 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003101
3102 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 do {
3104 err = nfs4_handle_exception(NFS_SERVER(dir),
3105 _nfs4_proc_mkdir(dir, dentry, sattr),
3106 &exception);
3107 } while (exception.retry);
3108 return err;
3109}
3110
3111static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003112 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct inode *dir = dentry->d_inode;
3115 struct nfs4_readdir_arg args = {
3116 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003117 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 .pgbase = 0,
3119 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003120 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003121 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 };
3123 struct nfs4_readdir_res res;
3124 struct rpc_message msg = {
3125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3126 .rpc_argp = &args,
3127 .rpc_resp = &res,
3128 .rpc_cred = cred,
3129 };
3130 int status;
3131
Harvey Harrison3110ff82008-05-02 13:42:44 -07003132 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003133 dentry->d_parent->d_name.name,
3134 dentry->d_name.name,
3135 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3137 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003138 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 -05003139 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003141 status += args.pgbase;
3142 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003143
3144 nfs_invalidate_atime(dir);
3145
Harvey Harrison3110ff82008-05-02 13:42:44 -07003146 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return status;
3148}
3149
3150static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003151 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct nfs4_exception exception = { };
3154 int err;
3155 do {
3156 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3157 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003158 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 &exception);
3160 } while (exception.retry);
3161 return err;
3162}
3163
3164static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3165 struct iattr *sattr, dev_t rdev)
3166{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003167 struct nfs4_createdata *data;
3168 int mode = sattr->ia_mode;
3169 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
3171 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3172 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003173
3174 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3175 if (data == NULL)
3176 goto out;
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003179 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003181 data->arg.ftype = NF4BLK;
3182 data->arg.u.device.specdata1 = MAJOR(rdev);
3183 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
3185 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003186 data->arg.ftype = NF4CHR;
3187 data->arg.u.device.specdata1 = MAJOR(rdev);
3188 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003191 status = nfs4_do_create(dir, dentry, data);
3192
3193 nfs4_free_createdata(data);
3194out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return status;
3196}
3197
3198static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3199 struct iattr *sattr, dev_t rdev)
3200{
3201 struct nfs4_exception exception = { };
3202 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003203
3204 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 do {
3206 err = nfs4_handle_exception(NFS_SERVER(dir),
3207 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3208 &exception);
3209 } while (exception.retry);
3210 return err;
3211}
3212
3213static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3214 struct nfs_fsstat *fsstat)
3215{
3216 struct nfs4_statfs_arg args = {
3217 .fh = fhandle,
3218 .bitmask = server->attr_bitmask,
3219 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003220 struct nfs4_statfs_res res = {
3221 .fsstat = fsstat,
3222 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct rpc_message msg = {
3224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3225 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003226 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 };
3228
Trond Myklebust0e574af2005-10-27 22:12:38 -04003229 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003230 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
3233static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3234{
3235 struct nfs4_exception exception = { };
3236 int err;
3237 do {
3238 err = nfs4_handle_exception(server,
3239 _nfs4_proc_statfs(server, fhandle, fsstat),
3240 &exception);
3241 } while (exception.retry);
3242 return err;
3243}
3244
3245static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3246 struct nfs_fsinfo *fsinfo)
3247{
3248 struct nfs4_fsinfo_arg args = {
3249 .fh = fhandle,
3250 .bitmask = server->attr_bitmask,
3251 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003252 struct nfs4_fsinfo_res res = {
3253 .fsinfo = fsinfo,
3254 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 struct rpc_message msg = {
3256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3257 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003258 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 };
3260
Bryan Schumaker7c513052011-03-24 17:12:24 +00003261 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
3264static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3265{
3266 struct nfs4_exception exception = { };
3267 int err;
3268
3269 do {
3270 err = nfs4_handle_exception(server,
3271 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3272 &exception);
3273 } while (exception.retry);
3274 return err;
3275}
3276
3277static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3278{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003279 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3281}
3282
3283static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3284 struct nfs_pathconf *pathconf)
3285{
3286 struct nfs4_pathconf_arg args = {
3287 .fh = fhandle,
3288 .bitmask = server->attr_bitmask,
3289 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003290 struct nfs4_pathconf_res res = {
3291 .pathconf = pathconf,
3292 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3295 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003296 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 };
3298
3299 /* None of the pathconf attributes are mandatory to implement */
3300 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3301 memset(pathconf, 0, sizeof(*pathconf));
3302 return 0;
3303 }
3304
Trond Myklebust0e574af2005-10-27 22:12:38 -04003305 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003306 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
3309static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3310 struct nfs_pathconf *pathconf)
3311{
3312 struct nfs4_exception exception = { };
3313 int err;
3314
3315 do {
3316 err = nfs4_handle_exception(server,
3317 _nfs4_proc_pathconf(server, fhandle, pathconf),
3318 &exception);
3319 } while (exception.retry);
3320 return err;
3321}
3322
Benny Halevyd20581a2011-05-22 19:52:03 +03003323void __nfs4_read_done_cb(struct nfs_read_data *data)
3324{
Fred Isamancd841602012-04-20 14:47:44 -04003325 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003326}
3327
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003328static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Fred Isamancd841602012-04-20 14:47:44 -04003330 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003332 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003333 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003334 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003336
Benny Halevyd20581a2011-05-22 19:52:03 +03003337 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003339 renew_lease(server, data->timestamp);
3340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003343static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3344{
3345
3346 dprintk("--> %s\n", __func__);
3347
3348 if (!nfs4_sequence_done(task, &data->res.seq_res))
3349 return -EAGAIN;
3350
Benny Halevyd20581a2011-05-22 19:52:03 +03003351 return data->read_done_cb ? data->read_done_cb(task, data) :
3352 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003353}
3354
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003355static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003358 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003359 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003360 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361}
3362
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003363static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3364{
Fred Isamancd841602012-04-20 14:47:44 -04003365 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003366 &data->args.seq_args,
3367 &data->res.seq_res,
3368 task))
3369 return;
3370 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
Fred Isamanb029bc92011-03-03 15:13:42 +00003373static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374{
Fred Isamancd841602012-04-20 14:47:44 -04003375 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003377 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003378 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003379 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003381 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003383 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003384 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
Fred Isamanb029bc92011-03-03 15:13:42 +00003388static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3389{
3390 if (!nfs4_sequence_done(task, &data->res.seq_res))
3391 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003392 return data->write_done_cb ? data->write_done_cb(task, data) :
3393 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003394}
3395
Trond Myklebust5a37f852012-04-28 14:55:16 -04003396static
3397bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3398{
3399 const struct nfs_pgio_header *hdr = data->header;
3400
3401 /* Don't request attributes for pNFS or O_DIRECT writes */
3402 if (data->ds_clp != NULL || hdr->dreq != NULL)
3403 return false;
3404 /* Otherwise, request attributes if and only if we don't hold
3405 * a delegation
3406 */
3407 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3408}
3409
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003410static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Fred Isamancd841602012-04-20 14:47:44 -04003412 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003413
Trond Myklebust5a37f852012-04-28 14:55:16 -04003414 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003415 data->args.bitmask = NULL;
3416 data->res.fattr = NULL;
3417 } else
3418 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003419
Fred Isamanb029bc92011-03-03 15:13:42 +00003420 if (!data->write_done_cb)
3421 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003422 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 data->timestamp = jiffies;
3424
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003425 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003426 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003429static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3430{
Fred Isamancd841602012-04-20 14:47:44 -04003431 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003432 &data->args.seq_args,
3433 &data->res.seq_res,
3434 task))
3435 return;
3436 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
Fred Isaman0b7c0152012-04-20 14:47:39 -04003439static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3440{
3441 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3442 &data->args.seq_args,
3443 &data->res.seq_res,
3444 task))
3445 return;
3446 rpc_call_start(task);
3447}
3448
3449static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003452
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003453 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003454 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003455 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Fred Isaman0b7c0152012-04-20 14:47:39 -04003460static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003461{
3462 if (!nfs4_sequence_done(task, &data->res.seq_res))
3463 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003464 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003465}
3466
Fred Isaman0b7c0152012-04-20 14:47:39 -04003467static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003469 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003470
Fred Isaman0b7c0152012-04-20 14:47:39 -04003471 if (data->commit_done_cb == NULL)
3472 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003473 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003474 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003475 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003478struct nfs4_renewdata {
3479 struct nfs_client *client;
3480 unsigned long timestamp;
3481};
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483/*
3484 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3485 * standalone procedure for queueing an asynchronous RENEW.
3486 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003487static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003488{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003489 struct nfs4_renewdata *data = calldata;
3490 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003491
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003492 if (atomic_read(&clp->cl_count) > 1)
3493 nfs4_schedule_state_renewal(clp);
3494 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003495 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003496}
3497
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003498static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003500 struct nfs4_renewdata *data = calldata;
3501 struct nfs_client *clp = data->client;
3502 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003505 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003506 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3507 return;
3508 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003509 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003510 return;
3511 }
3512 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003514 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515}
3516
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003517static const struct rpc_call_ops nfs4_renew_ops = {
3518 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003519 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003520};
3521
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003522static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523{
3524 struct rpc_message msg = {
3525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3526 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003527 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003531 if (renew_flags == 0)
3532 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003533 if (!atomic_inc_not_zero(&clp->cl_count))
3534 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003535 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003536 if (data == NULL)
3537 return -ENOMEM;
3538 data->client = clp;
3539 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003541 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003544static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 struct rpc_message msg = {
3547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3548 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003549 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 };
3551 unsigned long now = jiffies;
3552 int status;
3553
3554 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3555 if (status < 0)
3556 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003557 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return 0;
3559}
3560
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003561static inline int nfs4_server_supports_acls(struct nfs_server *server)
3562{
3563 return (server->caps & NFS_CAP_ACLS)
3564 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3565 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3566}
3567
3568/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3569 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3570 * the stack.
3571 */
3572#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3573
Neil Hormane9e3d722011-03-04 19:26:03 -05003574static int buf_to_pages_noslab(const void *buf, size_t buflen,
3575 struct page **pages, unsigned int *pgbase)
3576{
3577 struct page *newpage, **spages;
3578 int rc = 0;
3579 size_t len;
3580 spages = pages;
3581
3582 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003583 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003584 newpage = alloc_page(GFP_KERNEL);
3585
3586 if (newpage == NULL)
3587 goto unwind;
3588 memcpy(page_address(newpage), buf, len);
3589 buf += len;
3590 buflen -= len;
3591 *pages++ = newpage;
3592 rc++;
3593 } while (buflen != 0);
3594
3595 return rc;
3596
3597unwind:
3598 for(; rc > 0; rc--)
3599 __free_page(spages[rc-1]);
3600 return -ENOMEM;
3601}
3602
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003603struct nfs4_cached_acl {
3604 int cached;
3605 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003606 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003607};
3608
3609static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003610{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003611 struct nfs_inode *nfsi = NFS_I(inode);
3612
3613 spin_lock(&inode->i_lock);
3614 kfree(nfsi->nfs4_acl);
3615 nfsi->nfs4_acl = acl;
3616 spin_unlock(&inode->i_lock);
3617}
3618
3619static void nfs4_zap_acl_attr(struct inode *inode)
3620{
3621 nfs4_set_cached_acl(inode, NULL);
3622}
3623
3624static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3625{
3626 struct nfs_inode *nfsi = NFS_I(inode);
3627 struct nfs4_cached_acl *acl;
3628 int ret = -ENOENT;
3629
3630 spin_lock(&inode->i_lock);
3631 acl = nfsi->nfs4_acl;
3632 if (acl == NULL)
3633 goto out;
3634 if (buf == NULL) /* user is just asking for length */
3635 goto out_len;
3636 if (acl->cached == 0)
3637 goto out;
3638 ret = -ERANGE; /* see getxattr(2) man page */
3639 if (acl->len > buflen)
3640 goto out;
3641 memcpy(buf, acl->data, acl->len);
3642out_len:
3643 ret = acl->len;
3644out:
3645 spin_unlock(&inode->i_lock);
3646 return ret;
3647}
3648
Sachin Prabhu5794d212012-04-17 14:36:40 +01003649static 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 +00003650{
3651 struct nfs4_cached_acl *acl;
3652
Sachin Prabhu5794d212012-04-17 14:36:40 +01003653 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3655 if (acl == NULL)
3656 goto out;
3657 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003658 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003659 } else {
3660 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3661 if (acl == NULL)
3662 goto out;
3663 acl->cached = 0;
3664 }
3665 acl->len = acl_len;
3666out:
3667 nfs4_set_cached_acl(inode, acl);
3668}
3669
Andy Adamsonbf118a32011-12-07 11:55:27 -05003670/*
3671 * The getxattr API returns the required buffer length when called with a
3672 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3673 * the required buf. On a NULL buf, we send a page of data to the server
3674 * guessing that the ACL request can be serviced by a page. If so, we cache
3675 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3676 * the cache. If not so, we throw away the page, and cache the required
3677 * length. The next getxattr call will then produce another round trip to
3678 * the server, this time with the input buf of the required size.
3679 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003680static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003681{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003682 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003683 struct nfs_getaclargs args = {
3684 .fh = NFS_FH(inode),
3685 .acl_pages = pages,
3686 .acl_len = buflen,
3687 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003688 struct nfs_getaclres res = {
3689 .acl_len = buflen,
3690 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003691 struct rpc_message msg = {
3692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3693 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003694 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003695 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003696 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003697
Andy Adamsonbf118a32011-12-07 11:55:27 -05003698 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3699 /* As long as we're doing a round trip to the server anyway,
3700 * let's be prepared for a page of acl data. */
3701 if (npages == 0)
3702 npages = 1;
3703
Sachin Prabhu5a006892012-04-17 14:35:39 +01003704 /* Add an extra page to handle the bitmap returned */
3705 npages++;
3706
Andy Adamsonbf118a32011-12-07 11:55:27 -05003707 for (i = 0; i < npages; i++) {
3708 pages[i] = alloc_page(GFP_KERNEL);
3709 if (!pages[i])
3710 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003711 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003712
3713 /* for decoding across pages */
3714 res.acl_scratch = alloc_page(GFP_KERNEL);
3715 if (!res.acl_scratch)
3716 goto out_free;
3717
Andy Adamsonbf118a32011-12-07 11:55:27 -05003718 args.acl_len = npages * PAGE_SIZE;
3719 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003720
Andy Adamsonbf118a32011-12-07 11:55:27 -05003721 /* Let decode_getfacl know not to fail if the ACL data is larger than
3722 * the page we send as a guess */
3723 if (buf == NULL)
3724 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003725
Peng Taode040be2012-01-10 22:42:47 +08003726 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003727 __func__, buf, buflen, npages, args.acl_len);
3728 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3729 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730 if (ret)
3731 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003732
3733 acl_len = res.acl_len - res.acl_data_offset;
3734 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003735 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003736 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003737 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003738 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 if (buf) {
3740 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003741 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003743 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003744 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003745 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003746 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003747out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003748 for (i = 0; i < npages; i++)
3749 if (pages[i])
3750 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003751 if (res.acl_scratch)
3752 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003753 return ret;
3754}
3755
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003756static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3757{
3758 struct nfs4_exception exception = { };
3759 ssize_t ret;
3760 do {
3761 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3762 if (ret >= 0)
3763 break;
3764 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3765 } while (exception.retry);
3766 return ret;
3767}
3768
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3770{
3771 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003772 int ret;
3773
3774 if (!nfs4_server_supports_acls(server))
3775 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 ret = nfs_revalidate_inode(server, inode);
3777 if (ret < 0)
3778 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003779 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3780 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 ret = nfs4_read_cached_acl(inode, buf, buflen);
3782 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003783 /* -ENOENT is returned if there is no ACL or if there is an ACL
3784 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003785 return ret;
3786 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003787}
3788
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003789static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003790{
3791 struct nfs_server *server = NFS_SERVER(inode);
3792 struct page *pages[NFS4ACL_MAXPAGES];
3793 struct nfs_setaclargs arg = {
3794 .fh = NFS_FH(inode),
3795 .acl_pages = pages,
3796 .acl_len = buflen,
3797 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003798 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003799 struct rpc_message msg = {
3800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3801 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003802 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003803 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003804 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003805
3806 if (!nfs4_server_supports_acls(server))
3807 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003808 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3809 if (i < 0)
3810 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003811 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003812 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003813
3814 /*
3815 * Free each page after tx, so the only ref left is
3816 * held by the network stack
3817 */
3818 for (; i > 0; i--)
3819 put_page(pages[i-1]);
3820
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003821 /*
3822 * Acl update can result in inode attribute update.
3823 * so mark the attribute cache invalid.
3824 */
3825 spin_lock(&inode->i_lock);
3826 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3827 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003828 nfs_access_zap_cache(inode);
3829 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003830 return ret;
3831}
3832
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003833static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3834{
3835 struct nfs4_exception exception = { };
3836 int err;
3837 do {
3838 err = nfs4_handle_exception(NFS_SERVER(inode),
3839 __nfs4_proc_set_acl(inode, buf, buflen),
3840 &exception);
3841 } while (exception.retry);
3842 return err;
3843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003846nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003848 struct nfs_client *clp = server->nfs_client;
3849
3850 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 return 0;
3852 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003853 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003854 case -NFS4ERR_ADMIN_REVOKED:
3855 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003856 if (state == NULL)
3857 break;
3858 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003859 case -NFS4ERR_OPENMODE:
3860 if (state == NULL)
3861 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003862 nfs4_schedule_stateid_recovery(server, state);
3863 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003864 case -NFS4ERR_EXPIRED:
3865 if (state != NULL)
3866 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003868 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003869 nfs4_schedule_lease_recovery(clp);
3870 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003871#if defined(CONFIG_NFS_V4_1)
3872 case -NFS4ERR_BADSESSION:
3873 case -NFS4ERR_BADSLOT:
3874 case -NFS4ERR_BAD_HIGH_SLOT:
3875 case -NFS4ERR_DEADSESSION:
3876 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3877 case -NFS4ERR_SEQ_FALSE_RETRY:
3878 case -NFS4ERR_SEQ_MISORDERED:
3879 dprintk("%s ERROR %d, Reset session\n", __func__,
3880 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003881 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003882 task->tk_status = 0;
3883 return -EAGAIN;
3884#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003886 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003887 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003888 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3890 task->tk_status = 0;
3891 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003892 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 case -NFS4ERR_OLD_STATEID:
3894 task->tk_status = 0;
3895 return -EAGAIN;
3896 }
3897 task->tk_status = nfs4_map_errors(task->tk_status);
3898 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003899wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003900 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003901 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3902 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3903 task->tk_status = 0;
3904 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
Chuck Leverf0920752012-05-21 22:45:41 -04003907static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3908 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003909{
3910 __be32 verf[2];
3911
Chuck Lever2c820d92012-05-21 22:45:33 -04003912 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3913 /* An impossible timestamp guarantees this value
3914 * will never match a generated boot time. */
3915 verf[0] = 0;
3916 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3917 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003918 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3919 verf[0] = (__be32)nn->boot_time.tv_sec;
3920 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003921 }
Chuck Levercd937102012-03-02 17:14:31 -05003922 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3923}
3924
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003925int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3926 unsigned short port, struct rpc_cred *cred,
3927 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928{
3929 nfs4_verifier sc_verifier;
3930 struct nfs4_setclientid setclientid = {
3931 .sc_verifier = &sc_verifier,
3932 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003933 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 };
3935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3937 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003938 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003939 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 int loop = 0;
3942 int status;
3943
Chuck Leverf0920752012-05-21 22:45:41 -04003944 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003947 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003949 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003950 clp->cl_ipaddr,
3951 rpc_peeraddr2str(clp->cl_rpcclient,
3952 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003953 rpc_peeraddr2str(clp->cl_rpcclient,
3954 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003955 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 clp->cl_id_uniquifier);
3957 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003958 sizeof(setclientid.sc_netid),
3959 rpc_peeraddr2str(clp->cl_rpcclient,
3960 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003962 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003964 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003966 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 if (status != -NFS4ERR_CLID_INUSE)
3968 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003969 if (loop != 0) {
3970 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003972 }
3973 ++loop;
3974 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976 return status;
3977}
3978
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003979int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003980 struct nfs4_setclientid_res *arg,
3981 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 struct nfs_fsinfo fsinfo;
3984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003986 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003988 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 };
3990 unsigned long now;
3991 int status;
3992
3993 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003994 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (status == 0) {
3996 spin_lock(&clp->cl_lock);
3997 clp->cl_lease_time = fsinfo.lease_time * HZ;
3998 clp->cl_last_renewal = now;
3999 spin_unlock(&clp->cl_lock);
4000 }
4001 return status;
4002}
4003
Trond Myklebustfe650402006-01-03 09:55:18 +01004004struct nfs4_delegreturndata {
4005 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004006 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004007 struct nfs_fh fh;
4008 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004009 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004010 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004011 int rpc_status;
4012};
4013
Trond Myklebustfe650402006-01-03 09:55:18 +01004014static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4015{
4016 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004017
Trond Myklebust14516c32010-07-31 14:29:06 -04004018 if (!nfs4_sequence_done(task, &data->res.seq_res))
4019 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004020
Ricardo Labiaga79708862009-12-07 09:23:21 -05004021 switch (task->tk_status) {
4022 case -NFS4ERR_STALE_STATEID:
4023 case -NFS4ERR_EXPIRED:
4024 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004025 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004026 break;
4027 default:
4028 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4029 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004030 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004031 return;
4032 }
4033 }
4034 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004035}
4036
4037static void nfs4_delegreturn_release(void *calldata)
4038{
Trond Myklebustfe650402006-01-03 09:55:18 +01004039 kfree(calldata);
4040}
4041
Andy Adamson938e1012009-04-01 09:22:28 -04004042#if defined(CONFIG_NFS_V4_1)
4043static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4044{
4045 struct nfs4_delegreturndata *d_data;
4046
4047 d_data = (struct nfs4_delegreturndata *)data;
4048
Trond Myklebust035168a2010-06-16 09:52:26 -04004049 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004050 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004051 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004052 return;
4053 rpc_call_start(task);
4054}
4055#endif /* CONFIG_NFS_V4_1 */
4056
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004057static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004058#if defined(CONFIG_NFS_V4_1)
4059 .rpc_call_prepare = nfs4_delegreturn_prepare,
4060#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004061 .rpc_call_done = nfs4_delegreturn_done,
4062 .rpc_release = nfs4_delegreturn_release,
4063};
4064
Trond Myklebuste6f81072008-01-24 18:14:34 -05004065static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004066{
4067 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004068 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004069 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004070 struct rpc_message msg = {
4071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4072 .rpc_cred = cred,
4073 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004074 struct rpc_task_setup task_setup_data = {
4075 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004076 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004077 .callback_ops = &nfs4_delegreturn_ops,
4078 .flags = RPC_TASK_ASYNC,
4079 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004080 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004081
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004082 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004083 if (data == NULL)
4084 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004085 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004086 data->args.fhandle = &data->fh;
4087 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004088 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004089 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004090 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004091 data->res.fattr = &data->fattr;
4092 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004093 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004094 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004095 data->rpc_status = 0;
4096
Trond Myklebustc970aa82007-07-14 15:39:59 -04004097 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004098 msg.rpc_argp = &data->args;
4099 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004100 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004101 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004102 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004103 if (!issync)
4104 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004105 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004106 if (status != 0)
4107 goto out;
4108 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004109 if (status == 0)
4110 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4111 else
4112 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004113out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004114 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Trond Myklebuste6f81072008-01-24 18:14:34 -05004118int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 struct nfs_server *server = NFS_SERVER(inode);
4121 struct nfs4_exception exception = { };
4122 int err;
4123 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004124 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 switch (err) {
4126 case -NFS4ERR_STALE_STATEID:
4127 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 case 0:
4129 return 0;
4130 }
4131 err = nfs4_handle_exception(server, err, &exception);
4132 } while (exception.retry);
4133 return err;
4134}
4135
4136#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4137#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4138
4139/*
4140 * sleep, with exponential backoff, and retry the LOCK operation.
4141 */
4142static unsigned long
4143nfs4_set_lock_task_retry(unsigned long timeout)
4144{
Jeff Laytond3103102011-12-01 22:44:39 +01004145 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 timeout <<= 1;
4147 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4148 return NFS4_LOCK_MAXTIMEOUT;
4149 return timeout;
4150}
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4153{
4154 struct inode *inode = state->inode;
4155 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004156 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004157 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004159 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004161 struct nfs_lockt_res res = {
4162 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 };
4164 struct rpc_message msg = {
4165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4166 .rpc_argp = &arg,
4167 .rpc_resp = &res,
4168 .rpc_cred = state->owner->so_cred,
4169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct nfs4_lock_state *lsp;
4171 int status;
4172
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004173 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004174 status = nfs4_set_lock_state(state, request);
4175 if (status != 0)
4176 goto out;
4177 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004178 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004179 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004180 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004181 switch (status) {
4182 case 0:
4183 request->fl_type = F_UNLCK;
4184 break;
4185 case -NFS4ERR_DENIED:
4186 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004188 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004189out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return status;
4191}
4192
4193static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4194{
4195 struct nfs4_exception exception = { };
4196 int err;
4197
4198 do {
4199 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4200 _nfs4_proc_getlk(state, cmd, request),
4201 &exception);
4202 } while (exception.retry);
4203 return err;
4204}
4205
4206static int do_vfs_lock(struct file *file, struct file_lock *fl)
4207{
4208 int res = 0;
4209 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4210 case FL_POSIX:
4211 res = posix_lock_file_wait(file, fl);
4212 break;
4213 case FL_FLOCK:
4214 res = flock_lock_file_wait(file, fl);
4215 break;
4216 default:
4217 BUG();
4218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return res;
4220}
4221
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004222struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004223 struct nfs_locku_args arg;
4224 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004225 struct nfs4_lock_state *lsp;
4226 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004227 struct file_lock fl;
4228 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004229 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004230};
4231
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004232static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4233 struct nfs_open_context *ctx,
4234 struct nfs4_lock_state *lsp,
4235 struct nfs_seqid *seqid)
4236{
4237 struct nfs4_unlockdata *p;
4238 struct inode *inode = lsp->ls_state->inode;
4239
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004240 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004241 if (p == NULL)
4242 return NULL;
4243 p->arg.fh = NFS_FH(inode);
4244 p->arg.fl = &p->fl;
4245 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004246 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004247 p->arg.stateid = &lsp->ls_stateid;
4248 p->lsp = lsp;
4249 atomic_inc(&lsp->ls_count);
4250 /* Ensure we don't close file until we're done freeing locks! */
4251 p->ctx = get_nfs_open_context(ctx);
4252 memcpy(&p->fl, fl, sizeof(p->fl));
4253 p->server = NFS_SERVER(inode);
4254 return p;
4255}
4256
Trond Myklebust06f814a2006-01-03 09:55:07 +01004257static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004258{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004259 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004260 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004261 nfs4_put_lock_state(calldata->lsp);
4262 put_nfs_open_context(calldata->ctx);
4263 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004264}
4265
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004266static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004267{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004268 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004269
Trond Myklebust14516c32010-07-31 14:29:06 -04004270 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4271 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004272 switch (task->tk_status) {
4273 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004274 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4275 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004276 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004277 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004278 case -NFS4ERR_BAD_STATEID:
4279 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004280 case -NFS4ERR_STALE_STATEID:
4281 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004282 break;
4283 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004284 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004285 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004286 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004287}
4288
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004289static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004290{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004291 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004293 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004294 return;
4295 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004296 /* Note: exit _without_ running nfs4_locku_done */
4297 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004298 return;
4299 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004300 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004301 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004302 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004303 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004304 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004305 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004308static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004309 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004310 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004311 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004312};
4313
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004314static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4315 struct nfs_open_context *ctx,
4316 struct nfs4_lock_state *lsp,
4317 struct nfs_seqid *seqid)
4318{
4319 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004320 struct rpc_message msg = {
4321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4322 .rpc_cred = ctx->cred,
4323 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004324 struct rpc_task_setup task_setup_data = {
4325 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004326 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004327 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004328 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004329 .flags = RPC_TASK_ASYNC,
4330 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004331
Frank Filz137d6ac2007-07-09 15:32:29 -07004332 /* Ensure this is an unlock - when canceling a lock, the
4333 * canceled lock is passed in, and it won't be an unlock.
4334 */
4335 fl->fl_type = F_UNLCK;
4336
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004337 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4338 if (data == NULL) {
4339 nfs_free_seqid(seqid);
4340 return ERR_PTR(-ENOMEM);
4341 }
4342
Trond Myklebust9d12b212012-01-17 22:04:25 -05004343 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004344 msg.rpc_argp = &data->arg;
4345 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004346 task_setup_data.callback_data = data;
4347 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004348}
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4351{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004352 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004353 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004354 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004355 struct rpc_task *task;
4356 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004357 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Trond Myklebust9b073572006-06-29 16:38:34 -04004359 status = nfs4_set_lock_state(state, request);
4360 /* Unlock _before_ we do the RPC call */
4361 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004362 down_read(&nfsi->rwsem);
4363 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4364 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004365 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004366 }
4367 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004368 if (status != 0)
4369 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004370 /* Is this a delegated lock? */
4371 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004372 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004373 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004374 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004375 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004376 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004377 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004378 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 status = PTR_ERR(task);
4380 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004381 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004383 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004384out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004385 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004389struct nfs4_lockdata {
4390 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004391 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004392 struct nfs4_lock_state *lsp;
4393 struct nfs_open_context *ctx;
4394 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004395 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004396 int rpc_status;
4397 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004398 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399};
4400
4401static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004402 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4403 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004404{
4405 struct nfs4_lockdata *p;
4406 struct inode *inode = lsp->ls_state->inode;
4407 struct nfs_server *server = NFS_SERVER(inode);
4408
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004409 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 if (p == NULL)
4411 return NULL;
4412
4413 p->arg.fh = NFS_FH(inode);
4414 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004415 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004416 if (p->arg.open_seqid == NULL)
4417 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004418 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004420 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004421 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004422 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004423 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004424 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004425 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004427 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004428 atomic_inc(&lsp->ls_count);
4429 p->ctx = get_nfs_open_context(ctx);
4430 memcpy(&p->fl, fl, sizeof(p->fl));
4431 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004432out_free_seqid:
4433 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434out_free:
4435 kfree(p);
4436 return NULL;
4437}
4438
4439static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4440{
4441 struct nfs4_lockdata *data = calldata;
4442 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Harvey Harrison3110ff82008-05-02 13:42:44 -07004444 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004445 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4446 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 /* Do we need to do an open_to_lock_owner? */
4448 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004449 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4450 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004451 data->arg.open_stateid = &state->stateid;
4452 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004453 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004454 } else
4455 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004456 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004457 if (nfs4_setup_sequence(data->server,
4458 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004459 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004460 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004461 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004462 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004463}
4464
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004465static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4466{
4467 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4468 nfs4_lock_prepare(task, calldata);
4469}
4470
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004471static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4472{
4473 struct nfs4_lockdata *data = calldata;
4474
Harvey Harrison3110ff82008-05-02 13:42:44 -07004475 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004476
Trond Myklebust14516c32010-07-31 14:29:06 -04004477 if (!nfs4_sequence_done(task, &data->res.seq_res))
4478 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004479
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004480 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004482 if (data->rpc_status == 0)
4483 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4484 else
4485 goto out;
4486 }
4487 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004488 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004490 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004492out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004493 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004494}
4495
4496static void nfs4_lock_release(void *calldata)
4497{
4498 struct nfs4_lockdata *data = calldata;
4499
Harvey Harrison3110ff82008-05-02 13:42:44 -07004500 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004501 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502 if (data->cancelled != 0) {
4503 struct rpc_task *task;
4504 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4505 data->arg.lock_seqid);
4506 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004507 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004508 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 } else
4510 nfs_free_seqid(data->arg.lock_seqid);
4511 nfs4_put_lock_state(data->lsp);
4512 put_nfs_open_context(data->ctx);
4513 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004514 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515}
4516
4517static const struct rpc_call_ops nfs4_lock_ops = {
4518 .rpc_call_prepare = nfs4_lock_prepare,
4519 .rpc_call_done = nfs4_lock_done,
4520 .rpc_release = nfs4_lock_release,
4521};
4522
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004523static const struct rpc_call_ops nfs4_recover_lock_ops = {
4524 .rpc_call_prepare = nfs4_recover_lock_prepare,
4525 .rpc_call_done = nfs4_lock_done,
4526 .rpc_release = nfs4_lock_release,
4527};
4528
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004529static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4530{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004531 switch (error) {
4532 case -NFS4ERR_ADMIN_REVOKED:
4533 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004534 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004535 if (new_lock_owner != 0 ||
4536 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004537 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004538 break;
4539 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004540 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004541 case -NFS4ERR_EXPIRED:
4542 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004543 };
4544}
4545
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004546static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547{
4548 struct nfs4_lockdata *data;
4549 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004550 struct rpc_message msg = {
4551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4552 .rpc_cred = state->owner->so_cred,
4553 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004554 struct rpc_task_setup task_setup_data = {
4555 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004556 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004557 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004558 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004559 .flags = RPC_TASK_ASYNC,
4560 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004561 int ret;
4562
Harvey Harrison3110ff82008-05-02 13:42:44 -07004563 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004564 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004565 fl->fl_u.nfs4_fl.owner,
4566 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004567 if (data == NULL)
4568 return -ENOMEM;
4569 if (IS_SETLKW(cmd))
4570 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004571 if (recovery_type > NFS_LOCK_NEW) {
4572 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004573 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004574 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4575 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004576 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004577 msg.rpc_argp = &data->arg;
4578 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004579 task_setup_data.callback_data = data;
4580 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004581 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004582 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004583 ret = nfs4_wait_for_completion_rpc_task(task);
4584 if (ret == 0) {
4585 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004586 if (ret)
4587 nfs4_handle_setlk_error(data->server, data->lsp,
4588 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 } else
4590 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004591 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004592 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594}
4595
4596static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4597{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004598 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004599 struct nfs4_exception exception = {
4600 .inode = state->inode,
4601 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004602 int err;
4603
4604 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004605 /* Cache the lock if possible... */
4606 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4607 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004608 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004609 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004610 break;
4611 nfs4_handle_exception(server, err, &exception);
4612 } while (exception.retry);
4613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
4616static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4617{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004618 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004619 struct nfs4_exception exception = {
4620 .inode = state->inode,
4621 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004622 int err;
4623
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004624 err = nfs4_set_lock_state(state, request);
4625 if (err != 0)
4626 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004627 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004628 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4629 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004630 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004631 switch (err) {
4632 default:
4633 goto out;
4634 case -NFS4ERR_GRACE:
4635 case -NFS4ERR_DELAY:
4636 nfs4_handle_exception(server, err, &exception);
4637 err = 0;
4638 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004639 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004640out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004644#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004645static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004646{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004647 int status, ret = NFS_OK;
4648 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004649 struct nfs_server *server = NFS_SERVER(state->inode);
4650
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004651 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4652 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4653 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4654 if (status != NFS_OK) {
4655 nfs41_free_stateid(server, &lsp->ls_stateid);
4656 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4657 ret = status;
4658 }
4659 }
4660 };
4661
4662 return ret;
4663}
4664
4665static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4666{
4667 int status = NFS_OK;
4668
4669 if (test_bit(LK_STATE_IN_USE, &state->flags))
4670 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004671 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004672 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004673 return nfs4_lock_expired(state, request);
4674}
4675#endif
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4678{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004679 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004680 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004681 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004683 if ((fl_flags & FL_POSIX) &&
4684 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4685 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004686 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004687 status = nfs4_set_lock_state(state, request);
4688 if (status != 0)
4689 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004690 request->fl_flags |= FL_ACCESS;
4691 status = do_vfs_lock(request->fl_file, request);
4692 if (status < 0)
4693 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004694 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004695 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004696 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004697 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004698 request->fl_flags = fl_flags & ~FL_SLEEP;
4699 status = do_vfs_lock(request->fl_file, request);
4700 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004701 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004702 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004703 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004704 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004705 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004706 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004707 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004708 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4709 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004710out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004711 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004712out:
4713 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return status;
4715}
4716
4717static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4718{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004719 struct nfs4_exception exception = {
4720 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004721 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004722 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 int err;
4724
4725 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004726 err = _nfs4_proc_setlk(state, cmd, request);
4727 if (err == -NFS4ERR_DENIED)
4728 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004730 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 } while (exception.retry);
4732 return err;
4733}
4734
4735static int
4736nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4737{
4738 struct nfs_open_context *ctx;
4739 struct nfs4_state *state;
4740 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4741 int status;
4742
4743 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004744 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 state = ctx->state;
4746
4747 if (request->fl_start < 0 || request->fl_end < 0)
4748 return -EINVAL;
4749
Trond Myklebustd9531262009-07-21 19:22:38 -04004750 if (IS_GETLK(cmd)) {
4751 if (state != NULL)
4752 return nfs4_proc_getlk(state, F_GETLK, request);
4753 return 0;
4754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4757 return -EINVAL;
4758
Trond Myklebustd9531262009-07-21 19:22:38 -04004759 if (request->fl_type == F_UNLCK) {
4760 if (state != NULL)
4761 return nfs4_proc_unlck(state, cmd, request);
4762 return 0;
4763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Trond Myklebustd9531262009-07-21 19:22:38 -04004765 if (state == NULL)
4766 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004767 /*
4768 * Don't rely on the VFS having checked the file open mode,
4769 * since it won't do this for flock() locks.
4770 */
4771 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4772 case F_RDLCK:
4773 if (!(filp->f_mode & FMODE_READ))
4774 return -EBADF;
4775 break;
4776 case F_WRLCK:
4777 if (!(filp->f_mode & FMODE_WRITE))
4778 return -EBADF;
4779 }
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 do {
4782 status = nfs4_proc_setlk(state, cmd, request);
4783 if ((status != -EAGAIN) || IS_SETLK(cmd))
4784 break;
4785 timeout = nfs4_set_lock_task_retry(timeout);
4786 status = -ERESTARTSYS;
4787 if (signalled())
4788 break;
4789 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 return status;
4791}
4792
Trond Myklebust888e6942005-11-04 15:38:11 -05004793int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4794{
4795 struct nfs_server *server = NFS_SERVER(state->inode);
4796 struct nfs4_exception exception = { };
4797 int err;
4798
4799 err = nfs4_set_lock_state(state, fl);
4800 if (err != 0)
4801 goto out;
4802 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004803 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004804 switch (err) {
4805 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004806 printk(KERN_ERR "NFS: %s: unhandled error "
4807 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004808 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004809 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004810 goto out;
4811 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004812 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004813 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004814 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004815 nfs4_schedule_lease_recovery(server->nfs_client);
4816 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004817 case -NFS4ERR_BADSESSION:
4818 case -NFS4ERR_BADSLOT:
4819 case -NFS4ERR_BAD_HIGH_SLOT:
4820 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4821 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004822 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004823 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004824 case -ERESTARTSYS:
4825 /*
4826 * The show must go on: exit, but mark the
4827 * stateid as needing recovery.
4828 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004829 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004830 case -NFS4ERR_ADMIN_REVOKED:
4831 case -NFS4ERR_BAD_STATEID:
4832 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004833 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004834 err = 0;
4835 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004836 case -EKEYEXPIRED:
4837 /*
4838 * User RPCSEC_GSS context has expired.
4839 * We cannot recover this stateid now, so
4840 * skip it and allow recovery thread to
4841 * proceed.
4842 */
4843 err = 0;
4844 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004845 case -ENOMEM:
4846 case -NFS4ERR_DENIED:
4847 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4848 err = 0;
4849 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004850 case -NFS4ERR_DELAY:
4851 break;
4852 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004853 err = nfs4_handle_exception(server, err, &exception);
4854 } while (exception.retry);
4855out:
4856 return err;
4857}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004858
Trond Myklebustcf470c32012-03-07 13:49:12 -05004859struct nfs_release_lockowner_data {
4860 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004861 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004862 struct nfs_release_lockowner_args args;
4863};
4864
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004865static void nfs4_release_lockowner_release(void *calldata)
4866{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004867 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004868 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004869 kfree(calldata);
4870}
4871
Trond Myklebust17280172012-03-11 13:11:00 -04004872static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004873 .rpc_release = nfs4_release_lockowner_release,
4874};
4875
Trond Myklebustcf470c32012-03-07 13:49:12 -05004876int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004877{
4878 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004879 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4882 };
4883
4884 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004885 return -EINVAL;
4886 data = kmalloc(sizeof(*data), GFP_NOFS);
4887 if (!data)
4888 return -ENOMEM;
4889 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004890 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004891 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4892 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4893 data->args.lock_owner.s_dev = server->s_dev;
4894 msg.rpc_argp = &data->args;
4895 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4896 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004897}
4898
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004899#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4900
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004901static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4902 const void *buf, size_t buflen,
4903 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004904{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004905 if (strcmp(key, "") != 0)
4906 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004907
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004908 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004909}
4910
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004911static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4912 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004913{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004914 if (strcmp(key, "") != 0)
4915 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004916
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004917 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004918}
4919
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004920static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4921 size_t list_len, const char *name,
4922 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004923{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004924 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004925
J. Bruce Fields096455a2006-03-20 23:23:42 -05004926 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4927 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004928
4929 if (list && len <= list_len)
4930 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004932}
4933
Andy Adamson533eb462011-06-13 18:25:56 -04004934/*
4935 * nfs_fhget will use either the mounted_on_fileid or the fileid
4936 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004937static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4938{
Andy Adamson533eb462011-06-13 18:25:56 -04004939 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4940 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4941 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004942 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004943 return;
4944
4945 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004946 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004947 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4948 fattr->nlink = 2;
4949}
4950
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004951static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4952 const struct qstr *name,
4953 struct nfs4_fs_locations *fs_locations,
4954 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004955{
4956 struct nfs_server *server = NFS_SERVER(dir);
4957 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004958 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004959 };
4960 struct nfs4_fs_locations_arg args = {
4961 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004962 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004963 .page = page,
4964 .bitmask = bitmask,
4965 };
Benny Halevy22958462009-04-01 09:22:02 -04004966 struct nfs4_fs_locations_res res = {
4967 .fs_locations = fs_locations,
4968 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004969 struct rpc_message msg = {
4970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4971 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004972 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004973 };
4974 int status;
4975
Harvey Harrison3110ff82008-05-02 13:42:44 -07004976 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004977
4978 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4979 * is not supported */
4980 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4981 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4982 else
4983 bitmask[0] |= FATTR4_WORD0_FILEID;
4984
Trond Myklebustc228fd32007-01-13 02:28:11 -05004985 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004986 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004987 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004988 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004989 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004990 return status;
4991}
4992
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004993int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4994 const struct qstr *name,
4995 struct nfs4_fs_locations *fs_locations,
4996 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04004997{
4998 struct nfs4_exception exception = { };
4999 int err;
5000 do {
5001 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005002 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005003 &exception);
5004 } while (exception.retry);
5005 return err;
5006}
5007
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005008static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5009{
5010 int status;
5011 struct nfs4_secinfo_arg args = {
5012 .dir_fh = NFS_FH(dir),
5013 .name = name,
5014 };
5015 struct nfs4_secinfo_res res = {
5016 .flavors = flavors,
5017 };
5018 struct rpc_message msg = {
5019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5020 .rpc_argp = &args,
5021 .rpc_resp = &res,
5022 };
5023
5024 dprintk("NFS call secinfo %s\n", name->name);
5025 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5026 dprintk("NFS reply secinfo: %d\n", status);
5027 return status;
5028}
5029
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005030int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5031 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005032{
5033 struct nfs4_exception exception = { };
5034 int err;
5035 do {
5036 err = nfs4_handle_exception(NFS_SERVER(dir),
5037 _nfs4_proc_secinfo(dir, name, flavors),
5038 &exception);
5039 } while (exception.retry);
5040 return err;
5041}
5042
Andy Adamson557134a2009-04-01 09:21:53 -04005043#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005044/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005045 * Check the exchange flags returned by the server for invalid flags, having
5046 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5047 * DS flags set.
5048 */
5049static int nfs4_check_cl_exchange_flags(u32 flags)
5050{
5051 if (flags & ~EXCHGID4_FLAG_MASK_R)
5052 goto out_inval;
5053 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5054 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5055 goto out_inval;
5056 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5057 goto out_inval;
5058 return NFS_OK;
5059out_inval:
5060 return -NFS4ERR_INVAL;
5061}
5062
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005063static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005064nfs41_same_server_scope(struct nfs41_server_scope *a,
5065 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005066{
5067 if (a->server_scope_sz == b->server_scope_sz &&
5068 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5069 return true;
5070
5071 return false;
5072}
5073
Andy Adamson357f54d2010-12-14 10:11:57 -05005074/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005075 * nfs4_proc_exchange_id()
5076 *
5077 * Since the clientid has expired, all compounds using sessions
5078 * associated with the stale clientid will be returning
5079 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5080 * be in some phase of session reset.
5081 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005082int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005083{
5084 nfs4_verifier verifier;
5085 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005086 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005087 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005088 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005089 };
5090 struct nfs41_exchange_id_res res = {
5091 .client = clp,
5092 };
5093 int status;
5094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5096 .rpc_argp = &args,
5097 .rpc_resp = &res,
5098 .rpc_cred = cred,
5099 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005100
5101 dprintk("--> %s\n", __func__);
5102 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005103
Chuck Leverf0920752012-05-21 22:45:41 -04005104 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005105
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005106 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005107 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005108 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005109 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005110 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005111
Chuck Leveracdeb692012-05-21 22:46:16 -04005112 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5113 GFP_KERNEL);
5114 if (unlikely(res.server_owner == NULL)) {
5115 status = -ENOMEM;
5116 goto out;
5117 }
5118
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005119 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5120 GFP_KERNEL);
Chuck Lever177313f2012-05-21 22:44:58 -04005121 if (unlikely(res.server_scope == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005122 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005123 goto out_server_owner;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005124 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005125
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005126 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
Chuck Lever177313f2012-05-21 22:44:58 -04005127 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005128 status = -ENOMEM;
5129 goto out_server_scope;
5130 }
5131
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005132 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005133 if (status == 0)
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005134 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005135
Chuck Lever177313f2012-05-21 22:44:58 -04005136 if (status == 0) {
Chuck Leveracdeb692012-05-21 22:46:16 -04005137 kfree(clp->cl_serverowner);
5138 clp->cl_serverowner = res.server_owner;
5139 res.server_owner = NULL;
5140 }
5141
5142 if (status == 0) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005143 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005144 kfree(clp->cl_implid);
5145 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005146 } else
5147 kfree(res.impl_id);
5148
Chuck Lever177313f2012-05-21 22:44:58 -04005149 if (status == 0) {
5150 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005151 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005152 res.server_scope)) {
5153 dprintk("%s: server_scope mismatch detected\n",
5154 __func__);
5155 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005156 kfree(clp->cl_serverscope);
5157 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005158 }
5159
Chuck Lever177313f2012-05-21 22:44:58 -04005160 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005161 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005162 goto out;
5163 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005164 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005165
Chuck Leveracdeb692012-05-21 22:46:16 -04005166out_server_owner:
5167 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005168out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005169 kfree(res.server_scope);
5170out:
Chuck Lever177313f2012-05-21 22:44:58 -04005171 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005172 dprintk("%s: Server Implementation ID: "
5173 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005174 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5175 clp->cl_implid->date.seconds,
5176 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005177 dprintk("<-- %s status= %d\n", __func__, status);
5178 return status;
5179}
5180
Andy Adamson2050f0c2009-04-01 09:22:30 -04005181struct nfs4_get_lease_time_data {
5182 struct nfs4_get_lease_time_args *args;
5183 struct nfs4_get_lease_time_res *res;
5184 struct nfs_client *clp;
5185};
5186
5187static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5188 void *calldata)
5189{
5190 int ret;
5191 struct nfs4_get_lease_time_data *data =
5192 (struct nfs4_get_lease_time_data *)calldata;
5193
5194 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005195 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005196 /* just setup sequence, do not trigger session recovery
5197 since we're invoked within one */
5198 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005199 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005200 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005201
5202 BUG_ON(ret == -EAGAIN);
5203 rpc_call_start(task);
5204 dprintk("<-- %s\n", __func__);
5205}
5206
5207/*
5208 * Called from nfs4_state_manager thread for session setup, so don't recover
5209 * from sequence operation or clientid errors.
5210 */
5211static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5212{
5213 struct nfs4_get_lease_time_data *data =
5214 (struct nfs4_get_lease_time_data *)calldata;
5215
5216 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005217 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5218 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005219 switch (task->tk_status) {
5220 case -NFS4ERR_DELAY:
5221 case -NFS4ERR_GRACE:
5222 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5223 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5224 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005225 /* fall through */
5226 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005227 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005228 return;
5229 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005230 dprintk("<-- %s\n", __func__);
5231}
5232
Trond Myklebust17280172012-03-11 13:11:00 -04005233static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005234 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5235 .rpc_call_done = nfs4_get_lease_time_done,
5236};
5237
5238int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5239{
5240 struct rpc_task *task;
5241 struct nfs4_get_lease_time_args args;
5242 struct nfs4_get_lease_time_res res = {
5243 .lr_fsinfo = fsinfo,
5244 };
5245 struct nfs4_get_lease_time_data data = {
5246 .args = &args,
5247 .res = &res,
5248 .clp = clp,
5249 };
5250 struct rpc_message msg = {
5251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5252 .rpc_argp = &args,
5253 .rpc_resp = &res,
5254 };
5255 struct rpc_task_setup task_setup = {
5256 .rpc_client = clp->cl_rpcclient,
5257 .rpc_message = &msg,
5258 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005259 .callback_data = &data,
5260 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005261 };
5262 int status;
5263
Trond Myklebust9d12b212012-01-17 22:04:25 -05005264 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005265 dprintk("--> %s\n", __func__);
5266 task = rpc_run_task(&task_setup);
5267
5268 if (IS_ERR(task))
5269 status = PTR_ERR(task);
5270 else {
5271 status = task->tk_status;
5272 rpc_put_task(task);
5273 }
5274 dprintk("<-- %s return %d\n", __func__, status);
5275
5276 return status;
5277}
5278
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005279static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5280{
5281 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5282}
5283
5284static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5285 struct nfs4_slot *new,
5286 u32 max_slots,
5287 u32 ivalue)
5288{
5289 struct nfs4_slot *old = NULL;
5290 u32 i;
5291
5292 spin_lock(&tbl->slot_tbl_lock);
5293 if (new) {
5294 old = tbl->slots;
5295 tbl->slots = new;
5296 tbl->max_slots = max_slots;
5297 }
5298 tbl->highest_used_slotid = -1; /* no slot is currently used */
5299 for (i = 0; i < tbl->max_slots; i++)
5300 tbl->slots[i].seq_nr = ivalue;
5301 spin_unlock(&tbl->slot_tbl_lock);
5302 kfree(old);
5303}
5304
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005305/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005306 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005307 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005308static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5309 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005310{
Andy Adamson104aeba2010-01-14 17:45:10 -05005311 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005312 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005313
Andy Adamson104aeba2010-01-14 17:45:10 -05005314 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5315 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005316
Andy Adamson104aeba2010-01-14 17:45:10 -05005317 /* Does the newly negotiated max_reqs match the existing slot table? */
5318 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005319 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005320 if (!new)
5321 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005322 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005323 ret = 0;
5324
5325 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005326 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5327 tbl, tbl->slots, tbl->max_slots);
5328out:
5329 dprintk("<-- %s: return %d\n", __func__, ret);
5330 return ret;
5331}
5332
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005333/* Destroy the slot table */
5334static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5335{
5336 if (session->fc_slot_table.slots != NULL) {
5337 kfree(session->fc_slot_table.slots);
5338 session->fc_slot_table.slots = NULL;
5339 }
5340 if (session->bc_slot_table.slots != NULL) {
5341 kfree(session->bc_slot_table.slots);
5342 session->bc_slot_table.slots = NULL;
5343 }
5344 return;
5345}
5346
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005347/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005348 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005349 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005350static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005351{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005352 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005353 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005354
Andy Adamsonaacd5532011-11-09 13:58:21 -05005355 dprintk("--> %s\n", __func__);
5356 /* Fore channel */
5357 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005358 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5359 if (status) /* -ENOMEM */
5360 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005361 /* Back channel */
5362 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005363 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5364 if (status && tbl->slots == NULL)
5365 /* Fore and back channel share a connection so get
5366 * both slot tables or neither */
5367 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005368 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005369}
5370
5371struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5372{
5373 struct nfs4_session *session;
5374 struct nfs4_slot_table *tbl;
5375
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005376 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005377 if (!session)
5378 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005379
Andy Adamson557134a2009-04-01 09:21:53 -04005380 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005381 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005382 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005383 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005384 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005385
5386 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005387 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005388 spin_lock_init(&tbl->slot_tbl_lock);
5389 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005390 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005391
Trond Myklebust1055d762010-06-16 09:52:27 -04005392 session->session_state = 1<<NFS4_SESSION_INITING;
5393
Andy Adamson557134a2009-04-01 09:21:53 -04005394 session->clp = clp;
5395 return session;
5396}
5397
5398void nfs4_destroy_session(struct nfs4_session *session)
5399{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005400 struct rpc_xprt *xprt;
5401
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005402 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005403
5404 rcu_read_lock();
5405 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5406 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005407 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005408 __func__, xprt);
5409 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005410 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005411 kfree(session);
5412}
5413
Andy Adamsonfc931582009-04-01 09:22:31 -04005414/*
5415 * Initialize the values to be used by the client in CREATE_SESSION
5416 * If nfs4_init_session set the fore channel request and response sizes,
5417 * use them.
5418 *
5419 * Set the back channel max_resp_sz_cached to zero to force the client to
5420 * always set csa_cachethis to FALSE because the current implementation
5421 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5422 */
5423static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5424{
5425 struct nfs4_session *session = args->client->cl_session;
5426 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5427 mxresp_sz = session->fc_attrs.max_resp_sz;
5428
5429 if (mxrqst_sz == 0)
5430 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5431 if (mxresp_sz == 0)
5432 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5433 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005434 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5435 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005436 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005437 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005438
5439 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005440 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005441 __func__,
5442 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005443 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005444
5445 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005446 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5447 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5448 args->bc_attrs.max_resp_sz_cached = 0;
5449 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5450 args->bc_attrs.max_reqs = 1;
5451
5452 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5453 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5454 __func__,
5455 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5456 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5457 args->bc_attrs.max_reqs);
5458}
5459
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005460static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005461{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005462 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5463 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5464
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005465 if (rcvd->max_resp_sz > sent->max_resp_sz)
5466 return -EINVAL;
5467 /*
5468 * Our requested max_ops is the minimum we need; we're not
5469 * prepared to break up compounds into smaller pieces than that.
5470 * So, no point even trying to continue if the server won't
5471 * cooperate:
5472 */
5473 if (rcvd->max_ops < sent->max_ops)
5474 return -EINVAL;
5475 if (rcvd->max_reqs == 0)
5476 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005477 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5478 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005479 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005480}
5481
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005482static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5483{
5484 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5485 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005486
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005487 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5488 return -EINVAL;
5489 if (rcvd->max_resp_sz < sent->max_resp_sz)
5490 return -EINVAL;
5491 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5492 return -EINVAL;
5493 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005494 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005495 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005496 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005497 return -EINVAL;
5498 return 0;
5499}
Andy Adamson8d353012009-04-01 09:22:32 -04005500
Andy Adamson8d353012009-04-01 09:22:32 -04005501static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5502 struct nfs4_session *session)
5503{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005504 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005505
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005506 ret = nfs4_verify_fore_channel_attrs(args, session);
5507 if (ret)
5508 return ret;
5509 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005510}
5511
Andy Adamsonfc931582009-04-01 09:22:31 -04005512static int _nfs4_proc_create_session(struct nfs_client *clp)
5513{
5514 struct nfs4_session *session = clp->cl_session;
5515 struct nfs41_create_session_args args = {
5516 .client = clp,
5517 .cb_program = NFS4_CALLBACK,
5518 };
5519 struct nfs41_create_session_res res = {
5520 .client = clp,
5521 };
5522 struct rpc_message msg = {
5523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5524 .rpc_argp = &args,
5525 .rpc_resp = &res,
5526 };
5527 int status;
5528
5529 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005530 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005531
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005532 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005533
Andy Adamson8d353012009-04-01 09:22:32 -04005534 if (!status)
5535 /* Verify the session's negotiated channel_attrs values */
5536 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005537 if (!status) {
5538 /* Increment the clientid slot sequence id */
5539 clp->cl_seqid++;
5540 }
5541
5542 return status;
5543}
5544
5545/*
5546 * Issues a CREATE_SESSION operation to the server.
5547 * It is the responsibility of the caller to verify the session is
5548 * expired before calling this routine.
5549 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005550int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005551{
5552 int status;
5553 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005554 struct nfs4_session *session = clp->cl_session;
5555
5556 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5557
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005558 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005559 if (status)
5560 goto out;
5561
Andy Adamsonaacd5532011-11-09 13:58:21 -05005562 /* Init or reset the session slot tables */
5563 status = nfs4_setup_session_slot_tables(session);
5564 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005565 if (status)
5566 goto out;
5567
5568 ptr = (unsigned *)&session->sess_id.data[0];
5569 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5570 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005571out:
5572 dprintk("<-- %s\n", __func__);
5573 return status;
5574}
5575
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005576/*
5577 * Issue the over-the-wire RPC DESTROY_SESSION.
5578 * The caller must serialize access to this routine.
5579 */
5580int nfs4_proc_destroy_session(struct nfs4_session *session)
5581{
5582 int status = 0;
5583 struct rpc_message msg;
5584
5585 dprintk("--> nfs4_proc_destroy_session\n");
5586
5587 /* session is still being setup */
5588 if (session->clp->cl_cons_state != NFS_CS_READY)
5589 return status;
5590
5591 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5592 msg.rpc_argp = session;
5593 msg.rpc_resp = NULL;
5594 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005595 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005596
5597 if (status)
5598 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005599 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005600 "Session has been destroyed regardless...\n", status);
5601
5602 dprintk("<-- nfs4_proc_destroy_session\n");
5603 return status;
5604}
5605
Trond Myklebust7b38c362012-05-23 13:23:31 -04005606/*
5607 * With sessions, the client is not marked ready until after a
5608 * successful EXCHANGE_ID and CREATE_SESSION.
5609 *
5610 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5611 * other versions of NFS can be tried.
5612 */
5613static int nfs41_check_session_ready(struct nfs_client *clp)
5614{
5615 int ret;
5616
5617 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5618 ret = nfs4_client_recover_expired_lease(clp);
5619 if (ret)
5620 return ret;
5621 }
5622 if (clp->cl_cons_state < NFS_CS_READY)
5623 return -EPROTONOSUPPORT;
5624 return 0;
5625}
5626
Trond Myklebustfccba802009-07-21 16:48:07 -04005627int nfs4_init_session(struct nfs_server *server)
5628{
5629 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005630 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005631 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005632
5633 if (!nfs4_has_session(clp))
5634 return 0;
5635
Trond Myklebust1055d762010-06-16 09:52:27 -04005636 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005637 spin_lock(&clp->cl_lock);
5638 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005639
Trond Myklebust7b38c362012-05-23 13:23:31 -04005640 rsize = server->rsize;
5641 if (rsize == 0)
5642 rsize = NFS_MAX_FILE_IO_SIZE;
5643 wsize = server->wsize;
5644 if (wsize == 0)
5645 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005646
Trond Myklebust7b38c362012-05-23 13:23:31 -04005647 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5648 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5649 }
5650 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005651
Trond Myklebust7b38c362012-05-23 13:23:31 -04005652 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005653}
5654
Trond Myklebust7b38c362012-05-23 13:23:31 -04005655int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005656{
5657 struct nfs4_session *session = clp->cl_session;
5658 int ret;
5659
Trond Myklebust7b38c362012-05-23 13:23:31 -04005660 spin_lock(&clp->cl_lock);
5661 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5662 /*
5663 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5664 * DS lease to be equal to the MDS lease.
5665 */
5666 clp->cl_lease_time = lease_time;
5667 clp->cl_last_renewal = jiffies;
5668 }
5669 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005670
Trond Myklebust7b38c362012-05-23 13:23:31 -04005671 ret = nfs41_check_session_ready(clp);
5672 if (ret)
5673 return ret;
5674 /* Test for the DS role */
5675 if (!is_ds_client(clp))
5676 return -ENODEV;
5677 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005678}
5679EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5680
5681
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005682/*
5683 * Renew the cl_session lease.
5684 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005685struct nfs4_sequence_data {
5686 struct nfs_client *clp;
5687 struct nfs4_sequence_args args;
5688 struct nfs4_sequence_res res;
5689};
5690
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005691static void nfs41_sequence_release(void *data)
5692{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005693 struct nfs4_sequence_data *calldata = data;
5694 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005695
Alexandros Batsakis71358402010-02-05 03:45:05 -08005696 if (atomic_read(&clp->cl_count) > 1)
5697 nfs4_schedule_state_renewal(clp);
5698 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005699 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005700}
5701
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005702static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5703{
5704 switch(task->tk_status) {
5705 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005706 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5707 return -EAGAIN;
5708 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005709 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005710 }
5711 return 0;
5712}
5713
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005714static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005715{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005716 struct nfs4_sequence_data *calldata = data;
5717 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005718
Trond Myklebust14516c32010-07-31 14:29:06 -04005719 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5720 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005721
5722 if (task->tk_status < 0) {
5723 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005724 if (atomic_read(&clp->cl_count) == 1)
5725 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005727 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5728 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005729 return;
5730 }
5731 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005732 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005733out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005734 dprintk("<-- %s\n", __func__);
5735}
5736
5737static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5738{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005739 struct nfs4_sequence_data *calldata = data;
5740 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005741 struct nfs4_sequence_args *args;
5742 struct nfs4_sequence_res *res;
5743
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005744 args = task->tk_msg.rpc_argp;
5745 res = task->tk_msg.rpc_resp;
5746
Trond Myklebust9d12b212012-01-17 22:04:25 -05005747 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748 return;
5749 rpc_call_start(task);
5750}
5751
5752static const struct rpc_call_ops nfs41_sequence_ops = {
5753 .rpc_call_done = nfs41_sequence_call_done,
5754 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005755 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756};
5757
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005758static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005759{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005760 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005761 struct rpc_message msg = {
5762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5763 .rpc_cred = cred,
5764 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005765 struct rpc_task_setup task_setup_data = {
5766 .rpc_client = clp->cl_rpcclient,
5767 .rpc_message = &msg,
5768 .callback_ops = &nfs41_sequence_ops,
5769 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5770 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005771
Alexandros Batsakis71358402010-02-05 03:45:05 -08005772 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005773 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005774 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005775 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005776 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005777 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005778 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005779 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005780 msg.rpc_argp = &calldata->args;
5781 msg.rpc_resp = &calldata->res;
5782 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005783 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005784
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005785 return rpc_run_task(&task_setup_data);
5786}
5787
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005788static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005789{
5790 struct rpc_task *task;
5791 int ret = 0;
5792
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005793 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5794 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005795 task = _nfs41_proc_sequence(clp, cred);
5796 if (IS_ERR(task))
5797 ret = PTR_ERR(task);
5798 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005799 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005800 dprintk("<-- %s status=%d\n", __func__, ret);
5801 return ret;
5802}
5803
5804static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5805{
5806 struct rpc_task *task;
5807 int ret;
5808
5809 task = _nfs41_proc_sequence(clp, cred);
5810 if (IS_ERR(task)) {
5811 ret = PTR_ERR(task);
5812 goto out;
5813 }
5814 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005815 if (!ret) {
5816 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5817
5818 if (task->tk_status == 0)
5819 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005820 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005821 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005822 rpc_put_task(task);
5823out:
5824 dprintk("<-- %s status=%d\n", __func__, ret);
5825 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005826}
5827
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005828struct nfs4_reclaim_complete_data {
5829 struct nfs_client *clp;
5830 struct nfs41_reclaim_complete_args arg;
5831 struct nfs41_reclaim_complete_res res;
5832};
5833
5834static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5835{
5836 struct nfs4_reclaim_complete_data *calldata = data;
5837
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005838 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005839 if (nfs41_setup_sequence(calldata->clp->cl_session,
5840 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005841 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005842 return;
5843
5844 rpc_call_start(task);
5845}
5846
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005847static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5848{
5849 switch(task->tk_status) {
5850 case 0:
5851 case -NFS4ERR_COMPLETE_ALREADY:
5852 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5853 break;
5854 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005855 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005856 /* fall through */
5857 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005858 return -EAGAIN;
5859 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005860 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005861 }
5862 return 0;
5863}
5864
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005865static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5866{
5867 struct nfs4_reclaim_complete_data *calldata = data;
5868 struct nfs_client *clp = calldata->clp;
5869 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5870
5871 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005872 if (!nfs41_sequence_done(task, res))
5873 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005874
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005875 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5876 rpc_restart_call_prepare(task);
5877 return;
5878 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005879 dprintk("<-- %s\n", __func__);
5880}
5881
5882static void nfs4_free_reclaim_complete_data(void *data)
5883{
5884 struct nfs4_reclaim_complete_data *calldata = data;
5885
5886 kfree(calldata);
5887}
5888
5889static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5890 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5891 .rpc_call_done = nfs4_reclaim_complete_done,
5892 .rpc_release = nfs4_free_reclaim_complete_data,
5893};
5894
5895/*
5896 * Issue a global reclaim complete.
5897 */
5898static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5899{
5900 struct nfs4_reclaim_complete_data *calldata;
5901 struct rpc_task *task;
5902 struct rpc_message msg = {
5903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5904 };
5905 struct rpc_task_setup task_setup_data = {
5906 .rpc_client = clp->cl_rpcclient,
5907 .rpc_message = &msg,
5908 .callback_ops = &nfs4_reclaim_complete_call_ops,
5909 .flags = RPC_TASK_ASYNC,
5910 };
5911 int status = -ENOMEM;
5912
5913 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005914 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005915 if (calldata == NULL)
5916 goto out;
5917 calldata->clp = clp;
5918 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005919
Trond Myklebust9d12b212012-01-17 22:04:25 -05005920 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005921 msg.rpc_argp = &calldata->arg;
5922 msg.rpc_resp = &calldata->res;
5923 task_setup_data.callback_data = calldata;
5924 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005925 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005926 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005927 goto out;
5928 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005929 status = nfs4_wait_for_completion_rpc_task(task);
5930 if (status == 0)
5931 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005932 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005933 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005934out:
5935 dprintk("<-- %s status=%d\n", __func__, status);
5936 return status;
5937}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005938
5939static void
5940nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5941{
5942 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005943 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005944
5945 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005946 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5947 * right now covering the LAYOUTGET we are about to send.
5948 * However, that is not so catastrophic, and there seems
5949 * to be no way to prevent it completely.
5950 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005951 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005952 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005953 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005954 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5955 NFS_I(lgp->args.inode)->layout,
5956 lgp->args.ctx->state)) {
5957 rpc_exit(task, NFS4_OK);
5958 return;
5959 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005960 rpc_call_start(task);
5961}
5962
5963static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5964{
5965 struct nfs4_layoutget *lgp = calldata;
5966 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5967
5968 dprintk("--> %s\n", __func__);
5969
5970 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5971 return;
5972
5973 switch (task->tk_status) {
5974 case 0:
5975 break;
5976 case -NFS4ERR_LAYOUTTRYLATER:
5977 case -NFS4ERR_RECALLCONFLICT:
5978 task->tk_status = -NFS4ERR_DELAY;
5979 /* Fall through */
5980 default:
5981 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5982 rpc_restart_call_prepare(task);
5983 return;
5984 }
5985 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005986 dprintk("<-- %s\n", __func__);
5987}
5988
5989static void nfs4_layoutget_release(void *calldata)
5990{
5991 struct nfs4_layoutget *lgp = calldata;
5992
5993 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005994 put_nfs_open_context(lgp->args.ctx);
5995 kfree(calldata);
5996 dprintk("<-- %s\n", __func__);
5997}
5998
5999static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6000 .rpc_call_prepare = nfs4_layoutget_prepare,
6001 .rpc_call_done = nfs4_layoutget_done,
6002 .rpc_release = nfs4_layoutget_release,
6003};
6004
6005int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6006{
6007 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6008 struct rpc_task *task;
6009 struct rpc_message msg = {
6010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6011 .rpc_argp = &lgp->args,
6012 .rpc_resp = &lgp->res,
6013 };
6014 struct rpc_task_setup task_setup_data = {
6015 .rpc_client = server->client,
6016 .rpc_message = &msg,
6017 .callback_ops = &nfs4_layoutget_call_ops,
6018 .callback_data = lgp,
6019 .flags = RPC_TASK_ASYNC,
6020 };
6021 int status = 0;
6022
6023 dprintk("--> %s\n", __func__);
6024
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006025 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006026 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006027 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006028 task = rpc_run_task(&task_setup_data);
6029 if (IS_ERR(task))
6030 return PTR_ERR(task);
6031 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006032 if (status == 0)
6033 status = task->tk_status;
6034 if (status == 0)
6035 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006036 rpc_put_task(task);
6037 dprintk("<-- %s status=%d\n", __func__, status);
6038 return status;
6039}
6040
Benny Halevycbe82602011-05-22 19:52:37 +03006041static void
6042nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6043{
6044 struct nfs4_layoutreturn *lrp = calldata;
6045
6046 dprintk("--> %s\n", __func__);
6047 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006048 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006049 return;
6050 rpc_call_start(task);
6051}
6052
6053static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6054{
6055 struct nfs4_layoutreturn *lrp = calldata;
6056 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006057 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006058
6059 dprintk("--> %s\n", __func__);
6060
6061 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6062 return;
6063
6064 server = NFS_SERVER(lrp->args.inode);
6065 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006066 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006067 return;
6068 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006069 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006070 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006071 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006072 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006073 } else
6074 BUG_ON(!list_empty(&lo->plh_segs));
6075 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006076 lo->plh_block_lgets--;
6077 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006078 dprintk("<-- %s\n", __func__);
6079}
6080
6081static void nfs4_layoutreturn_release(void *calldata)
6082{
6083 struct nfs4_layoutreturn *lrp = calldata;
6084
6085 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006086 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006087 kfree(calldata);
6088 dprintk("<-- %s\n", __func__);
6089}
6090
6091static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6092 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6093 .rpc_call_done = nfs4_layoutreturn_done,
6094 .rpc_release = nfs4_layoutreturn_release,
6095};
6096
6097int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6098{
6099 struct rpc_task *task;
6100 struct rpc_message msg = {
6101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6102 .rpc_argp = &lrp->args,
6103 .rpc_resp = &lrp->res,
6104 };
6105 struct rpc_task_setup task_setup_data = {
6106 .rpc_client = lrp->clp->cl_rpcclient,
6107 .rpc_message = &msg,
6108 .callback_ops = &nfs4_layoutreturn_call_ops,
6109 .callback_data = lrp,
6110 };
6111 int status;
6112
6113 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006114 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006115 task = rpc_run_task(&task_setup_data);
6116 if (IS_ERR(task))
6117 return PTR_ERR(task);
6118 status = task->tk_status;
6119 dprintk("<-- %s status=%d\n", __func__, status);
6120 rpc_put_task(task);
6121 return status;
6122}
6123
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006124/*
6125 * Retrieve the list of Data Server devices from the MDS.
6126 */
6127static int _nfs4_getdevicelist(struct nfs_server *server,
6128 const struct nfs_fh *fh,
6129 struct pnfs_devicelist *devlist)
6130{
6131 struct nfs4_getdevicelist_args args = {
6132 .fh = fh,
6133 .layoutclass = server->pnfs_curr_ld->id,
6134 };
6135 struct nfs4_getdevicelist_res res = {
6136 .devlist = devlist,
6137 };
6138 struct rpc_message msg = {
6139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6140 .rpc_argp = &args,
6141 .rpc_resp = &res,
6142 };
6143 int status;
6144
6145 dprintk("--> %s\n", __func__);
6146 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6147 &res.seq_res, 0);
6148 dprintk("<-- %s status=%d\n", __func__, status);
6149 return status;
6150}
6151
6152int nfs4_proc_getdevicelist(struct nfs_server *server,
6153 const struct nfs_fh *fh,
6154 struct pnfs_devicelist *devlist)
6155{
6156 struct nfs4_exception exception = { };
6157 int err;
6158
6159 do {
6160 err = nfs4_handle_exception(server,
6161 _nfs4_getdevicelist(server, fh, devlist),
6162 &exception);
6163 } while (exception.retry);
6164
6165 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6166 err, devlist->num_devs);
6167
6168 return err;
6169}
6170EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6171
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006172static int
6173_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6174{
6175 struct nfs4_getdeviceinfo_args args = {
6176 .pdev = pdev,
6177 };
6178 struct nfs4_getdeviceinfo_res res = {
6179 .pdev = pdev,
6180 };
6181 struct rpc_message msg = {
6182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6183 .rpc_argp = &args,
6184 .rpc_resp = &res,
6185 };
6186 int status;
6187
6188 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006189 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006190 dprintk("<-- %s status=%d\n", __func__, status);
6191
6192 return status;
6193}
6194
6195int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6196{
6197 struct nfs4_exception exception = { };
6198 int err;
6199
6200 do {
6201 err = nfs4_handle_exception(server,
6202 _nfs4_proc_getdeviceinfo(server, pdev),
6203 &exception);
6204 } while (exception.retry);
6205 return err;
6206}
6207EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6208
Andy Adamson863a3c62011-03-23 13:27:54 +00006209static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6210{
6211 struct nfs4_layoutcommit_data *data = calldata;
6212 struct nfs_server *server = NFS_SERVER(data->args.inode);
6213
6214 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006215 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006216 return;
6217 rpc_call_start(task);
6218}
6219
6220static void
6221nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6222{
6223 struct nfs4_layoutcommit_data *data = calldata;
6224 struct nfs_server *server = NFS_SERVER(data->args.inode);
6225
6226 if (!nfs4_sequence_done(task, &data->res.seq_res))
6227 return;
6228
6229 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006230 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6231 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6232 case -NFS4ERR_BADLAYOUT: /* no layout */
6233 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006234 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006235 break;
6236 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006237 nfs_post_op_update_inode_force_wcc(data->args.inode,
6238 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006239 break;
6240 default:
6241 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6242 rpc_restart_call_prepare(task);
6243 return;
6244 }
6245 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006246}
6247
6248static void nfs4_layoutcommit_release(void *calldata)
6249{
6250 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006251 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006252 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006253
Andy Adamsondb29c082011-07-30 20:52:38 -04006254 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006255 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006256 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6257 list_del_init(&lseg->pls_lc_list);
6258 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6259 &lseg->pls_flags))
6260 put_lseg(lseg);
6261 }
Peng Tao92407e72011-10-23 20:21:17 -07006262
6263 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6264 smp_mb__after_clear_bit();
6265 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6266
Andy Adamson863a3c62011-03-23 13:27:54 +00006267 put_rpccred(data->cred);
6268 kfree(data);
6269}
6270
6271static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6272 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6273 .rpc_call_done = nfs4_layoutcommit_done,
6274 .rpc_release = nfs4_layoutcommit_release,
6275};
6276
6277int
Andy Adamsonef311532011-03-12 02:58:10 -05006278nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006279{
6280 struct rpc_message msg = {
6281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6282 .rpc_argp = &data->args,
6283 .rpc_resp = &data->res,
6284 .rpc_cred = data->cred,
6285 };
6286 struct rpc_task_setup task_setup_data = {
6287 .task = &data->task,
6288 .rpc_client = NFS_CLIENT(data->args.inode),
6289 .rpc_message = &msg,
6290 .callback_ops = &nfs4_layoutcommit_ops,
6291 .callback_data = data,
6292 .flags = RPC_TASK_ASYNC,
6293 };
6294 struct rpc_task *task;
6295 int status = 0;
6296
6297 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6298 "lbw: %llu inode %lu\n",
6299 data->task.tk_pid, sync,
6300 data->args.lastbytewritten,
6301 data->args.inode->i_ino);
6302
Trond Myklebust9d12b212012-01-17 22:04:25 -05006303 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006304 task = rpc_run_task(&task_setup_data);
6305 if (IS_ERR(task))
6306 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006307 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006308 goto out;
6309 status = nfs4_wait_for_completion_rpc_task(task);
6310 if (status != 0)
6311 goto out;
6312 status = task->tk_status;
6313out:
6314 dprintk("%s: status %d\n", __func__, status);
6315 rpc_put_task(task);
6316 return status;
6317}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006318
6319static int
6320_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6321 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6322{
6323 struct nfs41_secinfo_no_name_args args = {
6324 .style = SECINFO_STYLE_CURRENT_FH,
6325 };
6326 struct nfs4_secinfo_res res = {
6327 .flavors = flavors,
6328 };
6329 struct rpc_message msg = {
6330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6331 .rpc_argp = &args,
6332 .rpc_resp = &res,
6333 };
6334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6335}
6336
6337static int
6338nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6339 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6340{
6341 struct nfs4_exception exception = { };
6342 int err;
6343 do {
6344 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6345 switch (err) {
6346 case 0:
6347 case -NFS4ERR_WRONGSEC:
6348 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006349 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006350 default:
6351 err = nfs4_handle_exception(server, err, &exception);
6352 }
6353 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006354out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006355 return err;
6356}
6357
6358static int
6359nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6360 struct nfs_fsinfo *info)
6361{
6362 int err;
6363 struct page *page;
6364 rpc_authflavor_t flavor;
6365 struct nfs4_secinfo_flavors *flavors;
6366
6367 page = alloc_page(GFP_KERNEL);
6368 if (!page) {
6369 err = -ENOMEM;
6370 goto out;
6371 }
6372
6373 flavors = page_address(page);
6374 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6375
6376 /*
6377 * Fall back on "guess and check" method if
6378 * the server doesn't support SECINFO_NO_NAME
6379 */
6380 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6381 err = nfs4_find_root_sec(server, fhandle, info);
6382 goto out_freepage;
6383 }
6384 if (err)
6385 goto out_freepage;
6386
6387 flavor = nfs_find_best_sec(flavors);
6388 if (err == 0)
6389 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6390
6391out_freepage:
6392 put_page(page);
6393 if (err == -EACCES)
6394 return -EPERM;
6395out:
6396 return err;
6397}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006398
6399static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006400{
6401 int status;
6402 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006403 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006404 };
6405 struct nfs41_test_stateid_res res;
6406 struct rpc_message msg = {
6407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6408 .rpc_argp = &args,
6409 .rpc_resp = &res,
6410 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006411
Trond Myklebust9d12b212012-01-17 22:04:25 -05006412 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006413 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6414
6415 if (status == NFS_OK)
6416 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006417 return status;
6418}
6419
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006420static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006421{
6422 struct nfs4_exception exception = { };
6423 int err;
6424 do {
6425 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006426 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006427 &exception);
6428 } while (exception.retry);
6429 return err;
6430}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006431
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006432static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006433{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006434 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006435 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006436 };
6437 struct nfs41_free_stateid_res res;
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6440 .rpc_argp = &args,
6441 .rpc_resp = &res,
6442 };
6443
Trond Myklebust9d12b212012-01-17 22:04:25 -05006444 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6445 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006446}
6447
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006448static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006449{
6450 struct nfs4_exception exception = { };
6451 int err;
6452 do {
6453 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006454 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006455 &exception);
6456 } while (exception.retry);
6457 return err;
6458}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006459
6460static bool nfs41_match_stateid(const nfs4_stateid *s1,
6461 const nfs4_stateid *s2)
6462{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006463 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006464 return false;
6465
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006466 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006467 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006468 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006469 return true;
6470
6471 return false;
6472}
6473
Andy Adamson557134a2009-04-01 09:21:53 -04006474#endif /* CONFIG_NFS_V4_1 */
6475
Trond Myklebust36281ca2012-03-04 18:13:56 -05006476static bool nfs4_match_stateid(const nfs4_stateid *s1,
6477 const nfs4_stateid *s2)
6478{
Trond Myklebustf597c532012-03-04 18:13:56 -05006479 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006480}
6481
6482
Trond Myklebust17280172012-03-11 13:11:00 -04006483static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006484 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006485 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 .recover_open = nfs4_open_reclaim,
6487 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006488 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006489 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490};
6491
Andy Adamson591d71c2009-04-01 09:22:47 -04006492#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006493static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006494 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6495 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6496 .recover_open = nfs4_open_reclaim,
6497 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006498 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006499 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006500 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006501};
6502#endif /* CONFIG_NFS_V4_1 */
6503
Trond Myklebust17280172012-03-11 13:11:00 -04006504static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006505 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006506 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 .recover_open = nfs4_open_expired,
6508 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006509 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006510 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511};
6512
Andy Adamson591d71c2009-04-01 09:22:47 -04006513#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006514static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006515 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6516 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006517 .recover_open = nfs41_open_expired,
6518 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006519 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006520 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006521};
6522#endif /* CONFIG_NFS_V4_1 */
6523
Trond Myklebust17280172012-03-11 13:11:00 -04006524static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006525 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006526 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006527 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006528};
6529
6530#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006531static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006532 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006533 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006534 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006535};
6536#endif
6537
Trond Myklebust97dc1352010-06-16 09:52:26 -04006538static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6539 .minor_version = 0,
6540 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006541 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006542 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006543 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6544 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6545 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006546};
6547
6548#if defined(CONFIG_NFS_V4_1)
6549static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6550 .minor_version = 1,
6551 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006552 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006553 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006554 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6555 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6556 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006557};
6558#endif
6559
Trond Myklebust97dc1352010-06-16 09:52:26 -04006560const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6561 [0] = &nfs_v4_0_minor_ops,
6562#if defined(CONFIG_NFS_V4_1)
6563 [1] = &nfs_v4_1_minor_ops,
6564#endif
6565};
6566
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006567static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006568 .permission = nfs_permission,
6569 .getattr = nfs_getattr,
6570 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006571 .getxattr = generic_getxattr,
6572 .setxattr = generic_setxattr,
6573 .listxattr = generic_listxattr,
6574 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006575};
6576
David Howells509de812006-08-22 20:06:11 -04006577const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 .version = 4, /* protocol version */
6579 .dentry_ops = &nfs4_dentry_operations,
6580 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006581 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006582 .file_ops = &nfs4_file_operations,
Bryan Schumakerbae36242012-05-10 15:07:31 -04006583 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006584 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 .getattr = nfs4_proc_getattr,
6586 .setattr = nfs4_proc_setattr,
6587 .lookup = nfs4_proc_lookup,
6588 .access = nfs4_proc_access,
6589 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 .create = nfs4_proc_create,
6591 .remove = nfs4_proc_remove,
6592 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006593 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 .unlink_done = nfs4_proc_unlink_done,
6595 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006596 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006597 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006598 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 .link = nfs4_proc_link,
6600 .symlink = nfs4_proc_symlink,
6601 .mkdir = nfs4_proc_mkdir,
6602 .rmdir = nfs4_proc_remove,
6603 .readdir = nfs4_proc_readdir,
6604 .mknod = nfs4_proc_mknod,
6605 .statfs = nfs4_proc_statfs,
6606 .fsinfo = nfs4_proc_fsinfo,
6607 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006608 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 .decode_dirent = nfs4_decode_dirent,
6610 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006611 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006612 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006614 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006615 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006617 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006618 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006620 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006621 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006622 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006623 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624};
6625
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006626static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6627 .prefix = XATTR_NAME_NFSV4_ACL,
6628 .list = nfs4_xattr_list_nfs4_acl,
6629 .get = nfs4_xattr_get_nfs4_acl,
6630 .set = nfs4_xattr_set_nfs4_acl,
6631};
6632
6633const struct xattr_handler *nfs4_xattr_handlers[] = {
6634 &nfs4_xattr_nfs4_acl_handler,
6635 NULL
6636};
6637
Trond Myklebustef159e92012-02-06 19:50:40 -05006638module_param(max_session_slots, ushort, 0644);
6639MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6640 "requests the client will negotiate");
6641
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642/*
6643 * Local variables:
6644 * c-basic-offset: 8
6645 * End:
6646 */