blob: 30351877aee218c44044f2edcd328e8e9ed3841b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040083static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040087#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050088static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050092static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Trond Myklebust52567b02009-10-23 14:46:42 -040094 if (err >= -1000)
95 return err;
96 switch (err) {
97 case -NFS4ERR_RESOURCE:
98 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000099 case -NFS4ERR_WRONGSEC:
100 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
103 return -EINVAL;
Trond Myklebust27b0a532012-05-28 11:36:28 -0400104 case -NFS4ERR_SHARE_DENIED:
105 return -EACCES;
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700108 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/*
115 * This is our standard bitmap for GETATTR requests.
116 */
117const u32 nfs4_fattr_bitmap[2] = {
118 FATTR4_WORD0_TYPE
119 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_SIZE
121 | FATTR4_WORD0_FSID
122 | FATTR4_WORD0_FILEID,
123 FATTR4_WORD1_MODE
124 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER
126 | FATTR4_WORD1_OWNER_GROUP
127 | FATTR4_WORD1_RAWDEV
128 | FATTR4_WORD1_SPACE_USED
129 | FATTR4_WORD1_TIME_ACCESS
130 | FATTR4_WORD1_TIME_METADATA
131 | FATTR4_WORD1_TIME_MODIFY
132};
133
134const u32 nfs4_statfs_bitmap[2] = {
135 FATTR4_WORD0_FILES_AVAIL
136 | FATTR4_WORD0_FILES_FREE
137 | FATTR4_WORD0_FILES_TOTAL,
138 FATTR4_WORD1_SPACE_AVAIL
139 | FATTR4_WORD1_SPACE_FREE
140 | FATTR4_WORD1_SPACE_TOTAL
141};
142
Trond Myklebust4ce79712005-06-22 17:16:21 +0000143const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 FATTR4_WORD0_MAXLINK
145 | FATTR4_WORD0_MAXNAME,
146 0
147};
148
Fred Isamandae100c2011-07-30 20:52:37 -0400149const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | FATTR4_WORD0_MAXREAD
151 | FATTR4_WORD0_MAXWRITE
152 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700153 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400154 | FATTR4_WORD1_FS_LAYOUT_TYPES,
155 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Manoj Naik830b8e32006-06-09 09:34:25 -0400158const u32 nfs4_fs_locations_bitmap[2] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_SIZE
162 | FATTR4_WORD0_FSID
163 | FATTR4_WORD0_FILEID
164 | FATTR4_WORD0_FS_LOCATIONS,
165 FATTR4_WORD1_MODE
166 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER
168 | FATTR4_WORD1_OWNER_GROUP
169 | FATTR4_WORD1_RAWDEV
170 | FATTR4_WORD1_SPACE_USED
171 | FATTR4_WORD1_TIME_ACCESS
172 | FATTR4_WORD1_TIME_METADATA
173 | FATTR4_WORD1_TIME_MODIFY
174 | FATTR4_WORD1_MOUNTED_ON_FILEID
175};
176
Al Virobc4785c2006-10-19 23:28:51 -0700177static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs4_readdir_arg *readdir)
179{
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 BUG_ON(readdir->count < 80);
183 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000184 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186 return;
187 }
188
189 readdir->cookie = 0;
190 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191 if (cookie == 2)
192 return;
193
194 /*
195 * NFSv4 servers do not return entries for '.' and '..'
196 * Therefore, we fake these entries here. We let '.'
197 * have cookie 0 and '..' have cookie 1. Note that
198 * when talking to the server, we always send cookie 0
199 * instead of 1 or 2.
200 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cookie == 0) {
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_one; /* cookie, second word */
207 *p++ = xdr_one; /* entry len */
208 memcpy(p, ".\0\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *p++ = xdr_one; /* next */
217 *p++ = xdr_zero; /* cookie, first word */
218 *p++ = xdr_two; /* cookie, second word */
219 *p++ = xdr_two; /* entry len */
220 memcpy(p, "..\0\0", 4); /* entry */
221 p++;
222 *p++ = xdr_one; /* bitmap length */
223 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
224 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400225 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 readdir->pgbase = (char *)p - (char *)start;
228 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800229 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Trond Myklebust65de8722008-12-23 15:21:44 -0500232static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233{
234 int res;
235
236 might_sleep();
237
Trond Myklebuste005e802008-12-23 15:21:48 -0500238 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400239 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 return res;
241}
242
243static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244{
245 int res = 0;
246
247 might_sleep();
248
249 if (*timeout <= 0)
250 *timeout = NFS4_POLL_RETRY_MIN;
251 if (*timeout > NFS4_POLL_RETRY_MAX)
252 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100253 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 if (fatal_signal_pending(current))
255 res = -ERESTARTSYS;
256 *timeout <<= 1;
257 return res;
258}
259
260/* This is the error handling routine for processes that are allowed
261 * to sleep.
262 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800263static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500264{
265 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500267 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 int ret = errorcode;
269
270 exception->retry = 0;
271 switch(errorcode) {
272 case 0:
273 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400275 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 nfs_inode_return_delegation(inode);
277 exception->retry = 1;
278 return 0;
279 }
280 if (state == NULL)
281 break;
282 nfs4_schedule_stateid_recovery(server, state);
283 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500284 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 case -NFS4ERR_ADMIN_REVOKED:
286 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400289 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_stateid_recovery(server, state);
291 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400292 case -NFS4ERR_EXPIRED:
293 if (state != NULL)
294 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500297 nfs4_schedule_lease_recovery(clp);
298 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500299#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400300 case -NFS4ERR_BADSESSION:
301 case -NFS4ERR_BADSLOT:
302 case -NFS4ERR_BAD_HIGH_SLOT:
303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304 case -NFS4ERR_DEADSESSION:
305 case -NFS4ERR_SEQ_FALSE_RETRY:
306 case -NFS4ERR_SEQ_MISORDERED:
307 dprintk("%s ERROR: %d Reset session\n", __func__,
308 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500309 nfs4_schedule_session_recovery(clp->cl_session);
Bryan Schumaker824904c2012-10-30 16:06:35 -0400310 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500311#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500312 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500313 if (exception->timeout > HZ) {
314 /* We have retried a decent amount, time to
315 * fail
316 */
317 ret = -EBUSY;
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_GRACE:
321 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500322 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500323 ret = nfs4_delay(server->client, &exception->timeout);
324 if (ret != 0)
325 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400326 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500327 case -NFS4ERR_OLD_STATEID:
328 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800329 break;
330 case -NFS4ERR_BADOWNER:
331 /* The following works around a Linux server bug! */
332 case -NFS4ERR_BADNAME:
333 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
334 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
335 exception->retry = 1;
336 printk(KERN_WARNING "NFS: v4 server %s "
337 "does not accept raw "
338 "uid/gids. "
339 "Reenabling the idmapper.\n",
340 server->nfs_client->cl_hostname);
341 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 }
343 /* We failed to handle the error */
344 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500345wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500346 ret = nfs4_wait_clnt_recover(clp);
347 if (ret == 0)
348 exception->retry = 1;
349 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500350}
351
352
Trond Myklebust452e9352010-07-31 14:29:06 -0400353static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_lock(&clp->cl_lock);
356 if (time_before(clp->cl_last_renewal,timestamp))
357 clp->cl_last_renewal = timestamp;
358 spin_unlock(&clp->cl_lock);
359}
360
Trond Myklebust452e9352010-07-31 14:29:06 -0400361static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
362{
363 do_renew_lease(server->nfs_client, timestamp);
364}
365
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400366#if defined(CONFIG_NFS_V4_1)
367
Benny Halevy510b8172009-04-01 09:22:14 -0400368/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 * nfs4_free_slot - free a slot and efficiently update slot table.
370 *
371 * freeing a slot is trivially done by clearing its respective bit
372 * in the bitmap.
373 * If the freed slotid equals highest_used_slotid we want to update it
374 * so that the server would be able to size down the slot table if needed,
375 * otherwise we know that the highest_used_slotid is still in use.
376 * When updating highest_used_slotid there may be "holes" in the bitmap
377 * so we need to scan down from highest_used_slotid to 0 looking for the now
378 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500379 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500380 *
381 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400382 */
383static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500384nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400385{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500386 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400387 /* clear used bit in bitmap */
388 __clear_bit(slotid, tbl->used_slots);
389
390 /* update highest_used_slotid when it is freed */
391 if (slotid == tbl->highest_used_slotid) {
392 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500393 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 tbl->highest_used_slotid = slotid;
395 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500396 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400397 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500398 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
399 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400}
401
Trond Myklebust961a8282012-01-17 22:57:37 -0500402bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
403{
404 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
405 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406}
407
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800408/*
Andy Adamson42acd022011-01-06 02:04:34 +0000409 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800410 */
Andy Adamson42acd022011-01-06 02:04:34 +0000411static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800412{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500414 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
415 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800416 return;
417 }
418
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800420 return;
421
Andy Adamson42acd022011-01-06 02:04:34 +0000422 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
423 complete(&ses->fc_slot_table.complete);
424}
425
426/*
427 * Signal state manager thread if session back channel is drained
428 */
429void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
430{
431 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500432 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000433 return;
434 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
435 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800436}
437
Trond Myklebustd185a332010-06-16 09:52:25 -0400438static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400439{
440 struct nfs4_slot_table *tbl;
441
Trond Myklebustd185a332010-06-16 09:52:25 -0400442 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400443 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400444 /* just wake up the next guy waiting since
445 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500446 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500447 return;
Andy Adamson13615872009-04-01 09:22:17 -0400448 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500449
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500450 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500451 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000452 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500453 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400454 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400455}
456
Trond Myklebust14516c32010-07-31 14:29:06 -0400457static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400458{
459 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461
462 /*
463 * sr_status remains 1 if an RPC level error occurred. The server
464 * may or may not have processed the sequence operation..
465 * Proceed as if the server received and processed the sequence
466 * operation.
467 */
468 if (res->sr_status == 1)
469 res->sr_status = NFS_OK;
470
Bryan Schumaker468f8612011-04-18 15:57:32 -0400471 /* don't increment the sequence number if the task wasn't sent */
472 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400473 goto out;
474
Andy Adamson691daf32009-12-04 15:55:39 -0500475 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 switch (res->sr_status) {
477 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400478 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400479 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400480 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400481 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400482 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500483 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500484 if (res->sr_status_flags != 0)
485 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 break;
487 case -NFS4ERR_DELAY:
488 /* The server detected a resend of the RPC call and
489 * returned NFS4ERR_DELAY as per Section 2.10.6.2
490 * of RFC5661.
491 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200492 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400493 __func__,
494 res->sr_slot - res->sr_session->fc_slot_table.slots,
495 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 goto out_retry;
497 default:
498 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400499 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 }
501out:
502 /* The session may be reset by one of the error handlers. */
503 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400504 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400505 return 1;
506out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400507 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 goto out;
509 rpc_delay(task, NFS4_POLL_RETRY_MAX);
510 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400511}
512
Trond Myklebust14516c32010-07-31 14:29:06 -0400513static int nfs4_sequence_done(struct rpc_task *task,
514 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400515{
Trond Myklebust14516c32010-07-31 14:29:06 -0400516 if (res->sr_session == NULL)
517 return 1;
518 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400519}
520
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400521/*
Benny Halevy510b8172009-04-01 09:22:14 -0400522 * nfs4_find_slot - efficiently look for a free slot
523 *
524 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
525 * If found, we mark the slot as used, update the highest_used_slotid,
526 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500527 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400528 *
529 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400530 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500531static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800532nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400533{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500534 u32 slotid;
535 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400536
Trond Myklebust45d43c22012-02-06 19:38:51 -0500537 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400538 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
539 tbl->max_slots);
540 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
541 if (slotid >= tbl->max_slots)
542 goto out;
543 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500544 if (slotid > tbl->highest_used_slotid ||
545 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400546 tbl->highest_used_slotid = slotid;
547 ret_id = slotid;
548out:
549 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
550 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400551 return ret_id;
552}
553
Trond Myklebust9d12b212012-01-17 22:04:25 -0500554static void nfs41_init_sequence(struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res, int cache_reply)
556{
557 args->sa_session = NULL;
558 args->sa_cache_this = 0;
559 if (cache_reply)
560 args->sa_cache_this = 1;
561 res->sr_session = NULL;
562 res->sr_slot = NULL;
563}
564
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000565int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct rpc_task *task)
569{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 struct nfs4_slot *slot;
571 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500572 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573
574 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400576 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 return 0;
578
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400579 tbl = &session->fc_slot_table;
580
581 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400582 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800583 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500584 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400586 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500587 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500588 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400589 }
590
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800591 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
592 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
593 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
594 spin_unlock(&tbl->slot_tbl_lock);
595 dprintk("%s enforce FIFO order\n", __func__);
596 return -EAGAIN;
597 }
598
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800599 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500600 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
602 spin_unlock(&tbl->slot_tbl_lock);
603 dprintk("<-- %s: no free slots\n", __func__);
604 return -EAGAIN;
605 }
606 spin_unlock(&tbl->slot_tbl_lock);
607
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800608 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400610 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400611 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612
613 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
614
Benny Halevy99fe60d2009-04-01 09:22:29 -0400615 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400616 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400617 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400618 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400619 /*
620 * sr_status is only set in decode_sequence, and so will remain
621 * set to 1 if an rpc level failure occurs.
622 */
623 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 return 0;
625}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000626EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627
Trond Myklebust035168a2010-06-16 09:52:26 -0400628int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_task *task)
632{
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 int ret = 0;
635
Trond Myklebust9d12b212012-01-17 22:04:25 -0500636 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400638
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200639 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400640 __func__, session->clp, session, res->sr_slot ?
641 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400642
Trond Myklebust9d12b212012-01-17 22:04:25 -0500643 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644out:
645 dprintk("<-- %s status=%d\n", __func__, ret);
646 return ret;
647}
648
649struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 struct nfs4_sequence_args *seq_args;
652 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653};
654
655static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust035168a2010-06-16 09:52:26 -0400659 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
660
661 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500662 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 return;
664 rpc_call_start(task);
665}
666
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
668{
669 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
670 nfs41_call_sync_prepare(task, calldata);
671}
672
Andy Adamson69ab40c2009-04-01 09:22:19 -0400673static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
674{
675 struct nfs41_call_sync_data *data = calldata;
676
Trond Myklebust14516c32010-07-31 14:29:06 -0400677 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400678}
679
Trond Myklebust17280172012-03-11 13:11:00 -0400680static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400682 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683};
684
Trond Myklebust17280172012-03-11 13:11:00 -0400685static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800686 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
687 .rpc_call_done = nfs41_call_sync_done,
688};
689
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
691 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696{
697 int ret;
698 struct rpc_task *task;
699 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400700 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 .seq_args = args,
702 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 };
704 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_message = msg,
707 .callback_ops = &nfs41_call_sync_ops,
708 .callback_data = &data
709 };
710
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 if (privileged)
712 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 task = rpc_run_task(&task_setup);
714 if (IS_ERR(task))
715 ret = PTR_ERR(task);
716 else {
717 ret = task->tk_status;
718 rpc_put_task(task);
719 }
720 return ret;
721}
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync_session(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
728 int cache_reply)
729{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500730 nfs41_init_sequence(args, res, cache_reply);
731 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732}
733
Trond Myklebustdf896452010-06-16 09:52:26 -0400734#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500735static inline
736void nfs41_init_sequence(struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res, int cache_reply)
738{
739}
740
Trond Myklebust14516c32010-07-31 14:29:06 -0400741static int nfs4_sequence_done(struct rpc_task *task,
742 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400743{
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400745}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746#endif /* CONFIG_NFS_V4_1 */
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000756 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000759static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000760int nfs4_call_sync(struct rpc_clnt *clnt,
761 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000762 struct rpc_message *msg,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 int cache_reply)
766{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000767 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
768 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000769}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400770
Trond Myklebust38478b22006-05-25 01:40:57 -0400771static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Trond Myklebust38478b22006-05-25 01:40:57 -0400773 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Trond Myklebust38478b22006-05-25 01:40:57 -0400775 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400776 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700777 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400778 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700779 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400780 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400784 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 struct nfs_openargs o_arg;
786 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100787 struct nfs_open_confirmargs c_arg;
788 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500789 struct nfs4_string owner_name;
790 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 struct nfs_fattr f_attr;
792 struct nfs_fattr dir_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;
808 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 p->o_res.server = p->o_arg.server;
812 nfs_fattr_init(&p->f_attr);
813 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400815}
816
Al Viro82a2c1b2011-06-22 18:30:55 -0400817static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400819 const struct iattr *attrs,
820 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821{
Al Viro82a2c1b2011-06-22 18:30:55 -0400822 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 struct inode *dir = parent->d_inode;
824 struct nfs_server *server = NFS_SERVER(dir);
825 struct nfs4_opendata *p;
826
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p == NULL)
829 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400830 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 if (p->o_arg.seqid == NULL)
832 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400833 nfs_sb_active(dentry->d_sb);
834 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 p->dir = parent;
836 p->owner = sp;
837 atomic_inc(&sp->so_count);
838 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400841 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400842 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
843 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 p->o_arg.server = server;
846 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500847 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500849 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500850 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400851
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 p->o_arg.u.attrs = &p->attrs;
853 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500854
855 verf[0] = jiffies;
856 verf[1] = current->pid;
857 memcpy(p->o_arg.u.verifier.data, verf,
858 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100860 p->c_arg.fh = &p->o_res.fh;
861 p->c_arg.stateid = &p->o_res.stateid;
862 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400863 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400864 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 return p;
866err_free:
867 kfree(p);
868err:
869 dput(parent);
870 return NULL;
871}
872
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400873static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400875 struct nfs4_opendata *p = container_of(kref,
876 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400877 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400878
879 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880 if (p->state != NULL)
881 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400882 nfs4_put_state_owner(p->owner);
883 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400884 dput(p->dentry);
885 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500886 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400887 kfree(p);
888}
889
890static void nfs4_opendata_put(struct nfs4_opendata *p)
891{
892 if (p != NULL)
893 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100894}
895
Trond Myklebust06f814a2006-01-03 09:55:07 +0100896static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
897{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100898 int ret;
899
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100901 return ret;
902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400905{
906 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907
Trond Myklebust536e43d2012-01-17 22:04:26 -0500908 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 goto out;
910 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500912 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
913 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400914 break;
915 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500916 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
917 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400918 break;
919 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500920 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
921 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400922 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400924 return ret;
925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500929 if (delegation == NULL)
930 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500933 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500935 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400936 return 1;
937}
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100940{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100942 case FMODE_WRITE:
943 state->n_wronly++;
944 break;
945 case FMODE_READ:
946 state->n_rdonly++;
947 break;
948 case FMODE_READ|FMODE_WRITE:
949 state->n_rdwr++;
950 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100952}
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400955{
956 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500957 nfs4_stateid_copy(&state->stateid, stateid);
958 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400960 case FMODE_READ:
961 set_bit(NFS_O_RDONLY_STATE, &state->flags);
962 break;
963 case FMODE_WRITE:
964 set_bit(NFS_O_WRONLY_STATE, &state->flags);
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 set_bit(NFS_O_RDWR_STATE, &state->flags);
968 }
969}
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400972{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400973 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400975 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400976}
977
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978static 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 -0700979{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400980 /*
981 * Protect the call to nfs4_state_set_mode_locked and
982 * serialise the stateid update
983 */
984 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400985 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500986 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400987 set_bit(NFS_DELEGATED_STATE, &state->flags);
988 }
989 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400991 write_sequnlock(&state->seqlock);
992 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700994 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997static 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 -0500998{
999 struct nfs_inode *nfsi = NFS_I(state->inode);
1000 struct nfs_delegation *deleg_cur;
1001 int ret = 0;
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001004
1005 rcu_read_lock();
1006 deleg_cur = rcu_dereference(nfsi->delegation);
1007 if (deleg_cur == NULL)
1008 goto no_delegation;
1009
1010 spin_lock(&deleg_cur->lock);
1011 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001013 goto no_delegation_unlock;
1014
1015 if (delegation == NULL)
1016 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001017 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001018 goto no_delegation_unlock;
1019
Trond Myklebustb7391f42008-12-23 15:21:52 -05001020 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001022 ret = 1;
1023no_delegation_unlock:
1024 spin_unlock(&deleg_cur->lock);
1025no_delegation:
1026 rcu_read_unlock();
1027
1028 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001030 ret = 1;
1031 }
1032
1033 return ret;
1034}
1035
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038{
1039 struct nfs_delegation *delegation;
1040
1041 rcu_read_lock();
1042 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 rcu_read_unlock();
1045 return;
1046 }
1047 rcu_read_unlock();
1048 nfs_inode_return_delegation(inode);
1049}
1050
Trond Myklebust6ee41262007-07-08 14:11:36 -04001051static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052{
1053 struct nfs4_state *state = opendata->state;
1054 struct nfs_inode *nfsi = NFS_I(state->inode);
1055 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001056 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 nfs4_stateid stateid;
1059 int ret = -EAGAIN;
1060
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001064 if (can_open_cached(state, fmode, open_mode)) {
1065 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001067 goto out_return_state;
1068 }
1069 spin_unlock(&state->owner->so_lock);
1070 }
Trond Myklebust34310432008-12-23 15:21:38 -05001071 rcu_read_lock();
1072 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001073 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001074 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001076 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001078 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001079 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001080 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001081 if (ret != 0)
1082 goto out;
1083 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001084
1085 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001086 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001087 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001089out:
1090 return ERR_PTR(ret);
1091out_return_state:
1092 atomic_inc(&state->count);
1093 return state;
1094}
1095
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1097{
1098 struct inode *inode;
1099 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001100 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001102
Trond Myklebustaac00a82007-07-05 19:02:21 -04001103 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001104 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001105 goto out;
1106 }
1107
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001110 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001111 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001113 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001114 goto err;
1115 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 state = nfs4_get_open_state(inode, data->owner);
1117 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001118 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001119 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001120 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001121 int delegation_flags = 0;
1122
Trond Myklebust003707c2007-07-05 18:07:55 -04001123 rcu_read_lock();
1124 delegation = rcu_dereference(NFS_I(inode)->delegation);
1125 if (delegation)
1126 delegation_flags = delegation->flags;
1127 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001128 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1129 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1130 "returning a delegation for "
1131 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001132 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001133 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001134 nfs_inode_set_delegation(state->inode,
1135 data->owner->so_cred,
1136 &data->o_res);
1137 else
1138 nfs_inode_reclaim_delegation(state->inode,
1139 data->owner->so_cred,
1140 &data->o_res);
1141 }
Trond Myklebust34310432008-12-23 15:21:38 -05001142
1143 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001145 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001146out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001147 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148err_put_inode:
1149 iput(inode);
1150err:
1151 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001152}
1153
Trond Myklebust864472e2006-01-03 09:55:15 +01001154static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1155{
1156 struct nfs_inode *nfsi = NFS_I(state->inode);
1157 struct nfs_open_context *ctx;
1158
1159 spin_lock(&state->inode->i_lock);
1160 list_for_each_entry(ctx, &nfsi->open_files, list) {
1161 if (ctx->state != state)
1162 continue;
1163 get_nfs_open_context(ctx);
1164 spin_unlock(&state->inode->i_lock);
1165 return ctx;
1166 }
1167 spin_unlock(&state->inode->i_lock);
1168 return ERR_PTR(-ENOENT);
1169}
1170
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001171static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1172{
1173 struct nfs4_opendata *opendata;
1174
Al Viro3d4ff432011-06-22 18:40:12 -04001175 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001176 if (opendata == NULL)
1177 return ERR_PTR(-ENOMEM);
1178 opendata->state = state;
1179 atomic_inc(&state->count);
1180 return opendata;
1181}
1182
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001184{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 int ret;
1187
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001188 opendata->o_arg.open_flags = 0;
1189 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1191 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1192 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001193 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001197 if (IS_ERR(newstate))
1198 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001199 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 return 0;
1202}
1203
1204static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1205{
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 int ret;
1208
1209 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 smp_rmb();
1212 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001213 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 if (ret != 0)
1216 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 if (newstate != state)
1218 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 }
1220 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001221 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 if (ret != 0)
1224 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001225 if (newstate != state)
1226 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 }
1228 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001229 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 if (ret != 0)
1232 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001233 if (newstate != state)
1234 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 /*
1237 * We may have performed cached opens for all three recoveries.
1238 * Check if we need to update the current stateid.
1239 */
1240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001241 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001242 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001243 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001244 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001245 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001246 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001247 return 0;
1248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/*
1251 * OPEN_RECLAIM:
1252 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001254static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001256 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001257 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001258 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int status;
1260
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001261 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1262 if (IS_ERR(opendata))
1263 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001264 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1265 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001266 rcu_read_lock();
1267 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001268 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001269 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001270 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 opendata->o_arg.u.delegation_type = delegation_type;
1272 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return status;
1275}
1276
Trond Myklebust539cd032007-06-05 11:46:42 -04001277static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct nfs_server *server = NFS_SERVER(state->inode);
1280 struct nfs4_exception exception = { };
1281 int err;
1282 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001283 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001284 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001285 break;
1286 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 } while (exception.retry);
1288 return err;
1289}
1290
Trond Myklebust864472e2006-01-03 09:55:15 +01001291static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1292{
1293 struct nfs_open_context *ctx;
1294 int ret;
1295
1296 ctx = nfs4_state_find_open_context(state);
1297 if (IS_ERR(ctx))
1298 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001299 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001300 put_nfs_open_context(ctx);
1301 return ret;
1302}
1303
Trond Myklebust13437e12007-07-06 15:10:43 -04001304static 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 -07001305{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001306 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001307 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001309 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1310 if (IS_ERR(opendata))
1311 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001312 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001313 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001314 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001315 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Trond Myklebust13437e12007-07-06 15:10:43 -04001319int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001322 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int err;
1324 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001325 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 switch (err) {
1327 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001328 case -ENOENT:
1329 case -ESTALE:
1330 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001331 case -NFS4ERR_BADSESSION:
1332 case -NFS4ERR_BADSLOT:
1333 case -NFS4ERR_BAD_HIGH_SLOT:
1334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1335 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001336 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case -NFS4ERR_STALE_CLIENTID:
1339 case -NFS4ERR_STALE_STATEID:
1340 case -NFS4ERR_EXPIRED:
1341 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001342 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001343 goto out;
1344 case -ERESTARTSYS:
1345 /*
1346 * The show must go on: exit, but mark the
1347 * stateid as needing recovery.
1348 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001349 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350 case -NFS4ERR_ADMIN_REVOKED:
1351 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001352 nfs_inode_find_state_and_recover(state->inode,
1353 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001354 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001355 case -EKEYEXPIRED:
1356 /*
1357 * User RPCSEC_GSS context has expired.
1358 * We cannot recover this stateid now, so
1359 * skip it and allow recovery thread to
1360 * proceed.
1361 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001362 case -ENOMEM:
1363 err = 0;
1364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366 err = nfs4_handle_exception(server, err, &exception);
1367 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001368out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return err;
1370}
1371
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1373{
1374 struct nfs4_opendata *data = calldata;
1375
1376 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001378 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001379 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001380 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001381 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001382 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001383}
1384
1385static void nfs4_open_confirm_release(void *calldata)
1386{
1387 struct nfs4_opendata *data = calldata;
1388 struct nfs4_state *state = NULL;
1389
1390 /* If this request hasn't been cancelled, do nothing */
1391 if (data->cancelled == 0)
1392 goto out_free;
1393 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001394 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001397 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001398 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001400 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401}
1402
1403static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001404 .rpc_call_done = nfs4_open_confirm_done,
1405 .rpc_release = nfs4_open_confirm_release,
1406};
1407
1408/*
1409 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1410 */
1411static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1412{
1413 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1414 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415 struct rpc_message msg = {
1416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1417 .rpc_argp = &data->c_arg,
1418 .rpc_resp = &data->c_res,
1419 .rpc_cred = data->owner->so_cred,
1420 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001421 struct rpc_task_setup task_setup_data = {
1422 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 .callback_ops = &nfs4_open_confirm_ops,
1425 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001426 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 .flags = RPC_TASK_ASYNC,
1428 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int status;
1430
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001432 data->rpc_done = 0;
1433 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001435 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001436 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001438 status = nfs4_wait_for_completion_rpc_task(task);
1439 if (status != 0) {
1440 data->cancelled = 1;
1441 smp_wmb();
1442 } else
1443 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001444 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return status;
1446}
1447
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 struct nfs4_opendata *data = calldata;
1451 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1454 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001455 /*
1456 * Check if we still need to send an OPEN call, or if we can use
1457 * a delegation instead.
1458 */
1459 if (data->state != NULL) {
1460 struct nfs_delegation *delegation;
1461
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001463 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001464 rcu_read_lock();
1465 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001466 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1467 can_open_delegated(delegation, data->o_arg.fmode))
1468 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001469 rcu_read_unlock();
1470 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001471 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001472 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001473 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001474 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001475 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1476 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001477 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001478 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001479 &data->o_arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04001480 &data->o_res.seq_res,
1481 task) != 0)
1482 nfs_release_seqid(data->o_arg.seqid);
1483 else
1484 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001486unlock_no_action:
1487 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488out_no_action:
1489 task->tk_action = NULL;
1490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001493static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1494{
1495 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1496 nfs4_open_prepare(task, calldata);
1497}
1498
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499static void nfs4_open_done(struct rpc_task *task, void *calldata)
1500{
1501 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001504
Trond Myklebust14516c32010-07-31 14:29:06 -04001505 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1506 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001507
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 if (task->tk_status == 0) {
1509 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001510 case S_IFREG:
1511 break;
1512 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 break;
1515 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001517 break;
1518 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001520 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001521 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001522 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1523 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001524 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001525 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001527
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528static void nfs4_open_release(void *calldata)
1529{
1530 struct nfs4_opendata *data = calldata;
1531 struct nfs4_state *state = NULL;
1532
1533 /* If this request hasn't been cancelled, do nothing */
1534 if (data->cancelled == 0)
1535 goto out_free;
1536 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001537 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 goto out_free;
1539 /* In case we need an open_confirm, no cleanup! */
1540 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1541 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001543 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001544 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001546 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547}
1548
1549static const struct rpc_call_ops nfs4_open_ops = {
1550 .rpc_call_prepare = nfs4_open_prepare,
1551 .rpc_call_done = nfs4_open_done,
1552 .rpc_release = nfs4_open_release,
1553};
1554
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001555static const struct rpc_call_ops nfs4_recover_open_ops = {
1556 .rpc_call_prepare = nfs4_recover_open_prepare,
1557 .rpc_call_done = nfs4_open_done,
1558 .rpc_release = nfs4_open_release,
1559};
1560
1561static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562{
1563 struct inode *dir = data->dir->d_inode;
1564 struct nfs_server *server = NFS_SERVER(dir);
1565 struct nfs_openargs *o_arg = &data->o_arg;
1566 struct nfs_openres *o_res = &data->o_res;
1567 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001568 struct rpc_message msg = {
1569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1570 .rpc_argp = o_arg,
1571 .rpc_resp = o_res,
1572 .rpc_cred = data->owner->so_cred,
1573 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 struct rpc_task_setup task_setup_data = {
1575 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001576 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001577 .callback_ops = &nfs4_open_ops,
1578 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001579 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001580 .flags = RPC_TASK_ASYNC,
1581 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001582 int status;
1583
Trond Myklebust9d12b212012-01-17 22:04:25 -05001584 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001585 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001586 data->rpc_done = 0;
1587 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001588 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001589 if (isrecover)
1590 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001591 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001592 if (IS_ERR(task))
1593 return PTR_ERR(task);
1594 status = nfs4_wait_for_completion_rpc_task(task);
1595 if (status != 0) {
1596 data->cancelled = 1;
1597 smp_wmb();
1598 } else
1599 status = data->rpc_status;
1600 rpc_put_task(task);
1601
1602 return status;
1603}
1604
1605static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1606{
1607 struct inode *dir = data->dir->d_inode;
1608 struct nfs_openres *o_res = &data->o_res;
1609 int status;
1610
1611 status = nfs4_run_open_task(data, 1);
1612 if (status != 0 || !data->rpc_done)
1613 return status;
1614
Trond Myklebust6926afd2012-01-07 13:22:46 -05001615 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1616
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001617 nfs_refresh_inode(dir, o_res->dir_attr);
1618
1619 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1620 status = _nfs4_proc_open_confirm(data);
1621 if (status != 0)
1622 return status;
1623 }
1624
1625 return status;
1626}
1627
1628/*
1629 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1630 */
1631static int _nfs4_proc_open(struct nfs4_opendata *data)
1632{
1633 struct inode *dir = data->dir->d_inode;
1634 struct nfs_server *server = NFS_SERVER(dir);
1635 struct nfs_openargs *o_arg = &data->o_arg;
1636 struct nfs_openres *o_res = &data->o_res;
1637 int status;
1638
1639 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001642 if (status != 0) {
1643 if (status == -NFS4ERR_BADNAME &&
1644 !(o_arg->open_flags & O_CREAT))
1645 return -ENOENT;
1646 return status;
1647 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648
Trond Myklebust6926afd2012-01-07 13:22:46 -05001649 nfs_fattr_map_and_free_names(server, &data->f_attr);
1650
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001651 if (o_arg->open_flags & O_CREAT) {
1652 update_changeattr(dir, &o_res->cinfo);
1653 nfs_post_op_update_inode(dir, o_res->dir_attr);
1654 } else
1655 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001656 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1657 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001659 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
1663 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001664 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Andy Adamsond83217c2011-03-01 01:34:17 +00001668static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001669{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001670 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001671 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001672
Trond Myklebusta78cb572009-08-09 15:06:19 -04001673 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001674 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001675 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001676 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001677 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1678 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001679 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001680 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001681 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001682 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001683 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001684}
1685
Andy Adamsond83217c2011-03-01 01:34:17 +00001686static int nfs4_recover_expired_lease(struct nfs_server *server)
1687{
1688 return nfs4_client_recover_expired_lease(server->nfs_client);
1689}
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691/*
1692 * OPEN_EXPIRED:
1693 * reclaim state on the server after a network partition.
1694 * Assumes caller holds the appropriate lock
1695 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001696static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001698 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001701 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1702 if (IS_ERR(opendata))
1703 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001705 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001706 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001707 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001711static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001712{
Trond Myklebust539cd032007-06-05 11:46:42 -04001713 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001714 struct nfs4_exception exception = { };
1715 int err;
1716
1717 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001718 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001719 switch (err) {
1720 default:
1721 goto out;
1722 case -NFS4ERR_GRACE:
1723 case -NFS4ERR_DELAY:
1724 nfs4_handle_exception(server, err, &exception);
1725 err = 0;
1726 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001728out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001729 return err;
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001735 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Trond Myklebust864472e2006-01-03 09:55:15 +01001737 ctx = nfs4_state_find_open_context(state);
1738 if (IS_ERR(ctx))
1739 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001740 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001741 put_nfs_open_context(ctx);
1742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001745#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001746static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001748 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001749 struct nfs_server *server = NFS_SERVER(state->inode);
1750
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001751 if (state->flags & flags) {
1752 status = nfs41_test_stateid(server, stateid);
1753 if (status != NFS_OK) {
1754 nfs41_free_stateid(server, stateid);
1755 state->flags &= ~flags;
1756 }
1757 }
1758 return status;
1759}
1760
1761static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1762{
1763 int deleg_status, open_status;
1764 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1765 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1766
1767 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1768 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1769
1770 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1771 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001772 return nfs4_open_expired(sp, state);
1773}
1774#endif
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001777 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1778 * fields corresponding to attributes that were used to store the verifier.
1779 * Make sure we clobber those fields in the later setattr call
1780 */
1781static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1782{
1783 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1784 !(sattr->ia_valid & ATTR_ATIME_SET))
1785 sattr->ia_valid |= ATTR_ATIME;
1786
1787 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1788 !(sattr->ia_valid & ATTR_MTIME_SET))
1789 sattr->ia_valid |= ATTR_MTIME;
1790}
1791
1792/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001793 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001795static 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 -07001796{
1797 struct nfs4_state_owner *sp;
1798 struct nfs4_state *state = NULL;
1799 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001800 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001801 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001805 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1806 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1808 goto out_err;
1809 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001810 status = nfs4_recover_expired_lease(server);
1811 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001812 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001813 if (dentry->d_inode != NULL)
1814 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001815 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001816 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001817 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001818 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Al Viro82a2c1b2011-06-22 18:30:55 -04001820 if (dentry->d_inode != NULL)
1821 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001822
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001823 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001825 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001827 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001828 status = PTR_ERR(state);
1829 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001830 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001831 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001832 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001833
1834 if (opendata->o_arg.open_flags & O_EXCL) {
1835 nfs4_exclusive_attrset(opendata, sattr);
1836
1837 nfs_fattr_init(opendata->o_res.f_attr);
1838 status = nfs4_do_setattr(state->inode, cred,
1839 opendata->o_res.f_attr, sattr,
1840 state);
1841 if (status == 0)
1842 nfs_setattr_update_inode(state->inode, sattr);
1843 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1844 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001845 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 *res = state;
1848 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001849err_opendata_put:
1850 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001851err_put_state_owner:
1852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *res = NULL;
1855 return status;
1856}
1857
1858
Al Viro82a2c1b2011-06-22 18:30:55 -04001859static 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 -07001860{
1861 struct nfs4_exception exception = { };
1862 struct nfs4_state *res;
1863 int status;
1864
Trond Myklebust5e74cfe2012-06-08 10:58:09 -04001865 fmode &= FMODE_READ|FMODE_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001867 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (status == 0)
1869 break;
1870 /* NOTE: BAD_SEQID means the server and client disagree about the
1871 * book-keeping w.r.t. state-changing operations
1872 * (OPEN/CLOSE/LOCK/LOCKU...)
1873 * It is actually a sign of a bug on the client or on the server.
1874 *
1875 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001876 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * have unhashed the old state_owner for us, and that we can
1878 * therefore safely retry using a new one. We should still warn
1879 * the user though...
1880 */
1881 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001882 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001883 " returned a bad sequence-id error!\n",
1884 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 exception.retry = 1;
1886 continue;
1887 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001888 /*
1889 * BAD_STATEID on OPEN means that the server cancelled our
1890 * state before it received the OPEN_CONFIRM.
1891 * Recover by retrying the request as per the discussion
1892 * on Page 181 of RFC3530.
1893 */
1894 if (status == -NFS4ERR_BAD_STATEID) {
1895 exception.retry = 1;
1896 continue;
1897 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001898 if (status == -EAGAIN) {
1899 /* We must have found a delegation */
1900 exception.retry = 1;
1901 continue;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1904 status, &exception));
1905 } while (exception.retry);
1906 return res;
1907}
1908
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001909static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1910 struct nfs_fattr *fattr, struct iattr *sattr,
1911 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001913 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001915 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 .iap = sattr,
1917 .server = server,
1918 .bitmask = server->attr_bitmask,
1919 };
1920 struct nfs_setattrres res = {
1921 .fattr = fattr,
1922 .server = server,
1923 };
1924 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1926 .rpc_argp = &arg,
1927 .rpc_resp = &res,
1928 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001930 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001931 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Trond Myklebust0e574af2005-10-27 22:12:38 -04001933 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Trond Myklebust4fc87962012-03-08 17:42:01 -05001935 if (state != NULL) {
1936 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1937 current->files, current->tgid);
1938 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1939 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001940 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001941 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001942 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Bryan Schumaker7c513052011-03-24 17:12:24 +00001944 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001945 if (status == 0 && state != NULL)
1946 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001947 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001950static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1951 struct nfs_fattr *fattr, struct iattr *sattr,
1952 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001954 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001955 struct nfs4_exception exception = {
1956 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001957 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int err;
1960 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001961 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1962 switch (err) {
1963 case -NFS4ERR_OPENMODE:
1964 if (state && !(state->state & FMODE_WRITE)) {
1965 err = -EBADF;
1966 if (sattr->ia_valid & ATTR_OPEN)
1967 err = -EACCES;
1968 goto out;
1969 }
1970 }
1971 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001973out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return err;
1975}
1976
1977struct nfs4_closedata {
1978 struct inode *inode;
1979 struct nfs4_state *state;
1980 struct nfs_closeargs arg;
1981 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001982 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001983 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001984 bool roc;
1985 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986};
1987
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001988static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001989{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001990 struct nfs4_closedata *calldata = data;
1991 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001992 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001993
Fred Isamanf7e89172011-01-06 11:36:32 +00001994 if (calldata->roc)
1995 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001996 nfs4_put_open_state(calldata->state);
1997 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001998 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001999 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002000 kfree(calldata);
2001}
2002
Trond Myklebust88069f72009-12-08 08:33:16 -05002003static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2004 fmode_t fmode)
2005{
2006 spin_lock(&state->owner->so_lock);
2007 if (!(fmode & FMODE_READ))
2008 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2009 if (!(fmode & FMODE_WRITE))
2010 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2011 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2012 spin_unlock(&state->owner->so_lock);
2013}
2014
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002015static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002017 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct nfs_server *server = NFS_SERVER(calldata->inode);
2020
Chuck Levera3ca5652012-03-01 17:00:40 -05002021 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002022 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2023 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /* hmm. we are done with the inode, and in the process of freeing
2025 * the state_owner. we keep this around to process errors
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 switch (task->tk_status) {
2028 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002029 if (calldata->roc)
2030 pnfs_roc_set_barrier(state->inode,
2031 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002032 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002033 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002034 nfs4_close_clear_stateid_flags(state,
2035 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 break;
2037 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002038 case -NFS4ERR_OLD_STATEID:
2039 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002041 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002044 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2045 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002047 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002048 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002049 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002052static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002054 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002055 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002056 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002057
Chuck Levera3ca5652012-03-01 17:00:40 -05002058 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002059 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002060 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002061
Trond Myklebust88069f72009-12-08 08:33:16 -05002062 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2063 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002064 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002065 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002066 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002067 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002068 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2069 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2070 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002071 }
2072 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002073 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2074 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2075 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002076 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002077 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002078 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002079
2080 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002081 /* Note: exit _without_ calling nfs4_close_done */
2082 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002083 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002084 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002085
Fred Isamanf7e89172011-01-06 11:36:32 +00002086 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002087 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002088 if (calldata->roc &&
2089 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2090 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2091 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002092 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002093 }
2094 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002095
Trond Myklebust516a6af2005-10-27 22:12:41 -04002096 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002097 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002098 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002099 &calldata->arg.seq_args,
2100 &calldata->res.seq_res,
Trond Myklebust0ada2102012-10-29 18:37:40 -04002101 task) != 0)
2102 nfs_release_seqid(calldata->arg.seqid);
2103 else
2104 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002105out:
2106 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002109static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002110 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002111 .rpc_call_done = nfs4_close_done,
2112 .rpc_release = nfs4_free_closedata,
2113};
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115/*
2116 * It is possible for data to be read/written from a mem-mapped file
2117 * after the sys_close call (which hits the vfs layer as a flush).
2118 * This means that we can't safely call nfsv4 close on a file until
2119 * the inode is cleared. This in turn means that we are not good
2120 * NFSv4 citizens - we do not indicate to the server to update the file's
2121 * share state even when we are done with one of the three share
2122 * stateid's in the inode.
2123 *
2124 * NOTE: Caller must be holding the sp->so_owner semaphore!
2125 */
Al Viro643168c2011-06-22 18:20:23 -04002126int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002128 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002130 struct nfs4_state_owner *sp = state->owner;
2131 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002132 struct rpc_message msg = {
2133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2134 .rpc_cred = state->owner->so_cred,
2135 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002136 struct rpc_task_setup task_setup_data = {
2137 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002138 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002139 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002140 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002141 .flags = RPC_TASK_ASYNC,
2142 };
Trond Myklebust95121352005-10-18 14:20:12 -07002143 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002145 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002147 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002148 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002149 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002151 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002152 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002154 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002155 if (calldata->arg.seqid == NULL)
2156 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002157 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002158 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002159 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002160 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002161 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002162 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002163 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002164
Trond Myklebust1174dd12010-12-21 10:52:24 -05002165 msg.rpc_argp = &calldata->arg;
2166 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002167 task_setup_data.callback_data = calldata;
2168 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002169 if (IS_ERR(task))
2170 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002171 status = 0;
2172 if (wait)
2173 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002174 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002175 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002176out_free_calldata:
2177 kfree(calldata);
2178out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002179 if (roc)
2180 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002181 nfs4_put_open_state(state);
2182 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002183 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Trond Myklebust2b484292010-09-17 10:56:51 -04002186static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002187nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct nfs4_state *state;
2190
Trond Myklebust565277f2007-10-15 18:17:53 -04002191 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002192 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002193 if (IS_ERR(state))
2194 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002195 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002196 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Trond Myklebust1185a552009-12-03 15:54:02 -05002199static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002200{
2201 if (ctx->state == NULL)
2202 return;
2203 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002204 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002205 else
Al Viro643168c2011-06-22 18:20:23 -04002206 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2210{
Benny Halevy43652ad2009-04-01 09:21:54 -04002211 struct nfs4_server_caps_arg args = {
2212 .fhandle = fhandle,
2213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct nfs4_server_caps_res res = {};
2215 struct rpc_message msg = {
2216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002217 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 .rpc_resp = &res,
2219 };
2220 int status;
2221
Bryan Schumaker7c513052011-03-24 17:12:24 +00002222 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (status == 0) {
2224 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002225 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2226 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2227 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2228 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2229 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2231 server->caps |= NFS_CAP_ACLS;
2232 if (res.has_links != 0)
2233 server->caps |= NFS_CAP_HARDLINKS;
2234 if (res.has_symlinks != 0)
2235 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002236 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2237 server->caps |= NFS_CAP_FILEID;
2238 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2239 server->caps |= NFS_CAP_MODE;
2240 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2241 server->caps |= NFS_CAP_NLINK;
2242 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2243 server->caps |= NFS_CAP_OWNER;
2244 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2245 server->caps |= NFS_CAP_OWNER_GROUP;
2246 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2247 server->caps |= NFS_CAP_ATIME;
2248 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2249 server->caps |= NFS_CAP_CTIME;
2250 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2251 server->caps |= NFS_CAP_MTIME;
2252
Trond Myklebusta65318b2009-03-11 14:10:28 -04002253 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2254 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2255 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002257 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return status;
2261}
2262
Trond Myklebust55a97592006-06-09 09:34:19 -04002263int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct nfs4_exception exception = { };
2266 int err;
2267 do {
2268 err = nfs4_handle_exception(server,
2269 _nfs4_server_capabilities(server, fhandle),
2270 &exception);
2271 } while (exception.retry);
2272 return err;
2273}
2274
2275static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2276 struct nfs_fsinfo *info)
2277{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct nfs4_lookup_root_arg args = {
2279 .bitmask = nfs4_fattr_bitmap,
2280 };
2281 struct nfs4_lookup_res res = {
2282 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002283 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 .fh = fhandle,
2285 };
2286 struct rpc_message msg = {
2287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2288 .rpc_argp = &args,
2289 .rpc_resp = &res,
2290 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002291
Trond Myklebust0e574af2005-10-27 22:12:38 -04002292 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002293 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2297 struct nfs_fsinfo *info)
2298{
2299 struct nfs4_exception exception = { };
2300 int err;
2301 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002302 err = _nfs4_lookup_root(server, fhandle, info);
2303 switch (err) {
2304 case 0:
2305 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002306 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002307 default:
2308 err = nfs4_handle_exception(server, err, &exception);
2309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002311out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return err;
2313}
2314
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002315static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2316 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2317{
2318 struct rpc_auth *auth;
2319 int ret;
2320
2321 auth = rpcauth_create(flavor, server->client);
2322 if (!auth) {
2323 ret = -EIO;
2324 goto out;
2325 }
2326 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002327out:
2328 return ret;
2329}
2330
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002331static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002332 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002334 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002335 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002337 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2338 flav_array[len] = RPC_AUTH_NULL;
2339 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002340
2341 for (i = 0; i < len; i++) {
2342 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002343 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002344 continue;
2345 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002346 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002347 /*
2348 * -EACCESS could mean that the user doesn't have correct permissions
2349 * to access the mount. It could also mean that we tried to mount
2350 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2351 * existing mount programs don't handle -EACCES very well so it should
2352 * be mapped to -EPERM instead.
2353 */
2354 if (status == -EACCES)
2355 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002356 return status;
2357}
2358
2359/*
2360 * get the file handle for the "/" directory on the server
2361 */
2362static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2363 struct nfs_fsinfo *info)
2364{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002365 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002366 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002367 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2368 /*
2369 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2370 * by nfs4_map_errors() as this function exits.
2371 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002372 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (status == 0)
2374 status = nfs4_server_capabilities(server, fhandle);
2375 if (status == 0)
2376 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002377 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Manoj Naik6b97fd32006-06-09 09:34:29 -04002380/*
2381 * Get locations and (maybe) other attributes of a referral.
2382 * Note that we'll actually follow the referral later when
2383 * we detect fsid mismatch in inode revalidation
2384 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002385static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2386 const struct qstr *name, struct nfs_fattr *fattr,
2387 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002388{
2389 int status = -ENOMEM;
2390 struct page *page = NULL;
2391 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392
2393 page = alloc_page(GFP_KERNEL);
2394 if (page == NULL)
2395 goto out;
2396 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2397 if (locations == NULL)
2398 goto out;
2399
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002400 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002401 if (status != 0)
2402 goto out;
2403 /* Make sure server returned a different fsid for the referral */
2404 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002405 dprintk("%s: server did not return a different fsid for"
2406 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002407 status = -EIO;
2408 goto out;
2409 }
Andy Adamson533eb462011-06-13 18:25:56 -04002410 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2411 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002412
Andy Adamson533eb462011-06-13 18:25:56 -04002413 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002414 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002415 memset(fhandle, 0, sizeof(struct nfs_fh));
2416out:
2417 if (page)
2418 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002419 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002420 return status;
2421}
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2424{
2425 struct nfs4_getattr_arg args = {
2426 .fh = fhandle,
2427 .bitmask = server->attr_bitmask,
2428 };
2429 struct nfs4_getattr_res res = {
2430 .fattr = fattr,
2431 .server = server,
2432 };
2433 struct rpc_message msg = {
2434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2435 .rpc_argp = &args,
2436 .rpc_resp = &res,
2437 };
2438
Trond Myklebust0e574af2005-10-27 22:12:38 -04002439 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002440 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
2443static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2444{
2445 struct nfs4_exception exception = { };
2446 int err;
2447 do {
2448 err = nfs4_handle_exception(server,
2449 _nfs4_proc_getattr(server, fhandle, fattr),
2450 &exception);
2451 } while (exception.retry);
2452 return err;
2453}
2454
2455/*
2456 * The file is not closed if it is opened due to the a request to change
2457 * the size of the file. The open call will not be needed once the
2458 * VFS layer lookup-intents are implemented.
2459 *
2460 * Close is called when the inode is destroyed.
2461 * If we haven't opened the file for O_WRONLY, we
2462 * need to in the size_change case to obtain a stateid.
2463 *
2464 * Got race?
2465 * Because OPEN is always done by name in nfsv4, it is
2466 * possible that we opened a different file by the same
2467 * name. We can recognize this race condition, but we
2468 * can't do anything about it besides returning an error.
2469 *
2470 * This will be fixed with VFS changes (lookup-intent).
2471 */
2472static int
2473nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2474 struct iattr *sattr)
2475{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002476 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002477 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002478 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int status;
2480
Benny Halevy8a1636c2010-07-14 15:43:57 -04002481 if (pnfs_ld_layoutret_on_setattr(inode))
2482 pnfs_return_layout(inode);
2483
Trond Myklebust0e574af2005-10-27 22:12:38 -04002484 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Trond Myklebustd5308382005-11-04 15:33:38 -05002486 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002487 if (sattr->ia_valid & ATTR_FILE) {
2488 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002489
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002490 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002491 if (ctx) {
2492 cred = ctx->cred;
2493 state = ctx->state;
2494 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002495 }
2496
Trond Myklebust536e43d2012-01-17 22:04:26 -05002497 /* Deal with open(O_TRUNC) */
2498 if (sattr->ia_valid & ATTR_OPEN)
2499 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2500
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002501 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002502 if (status == 0)
2503 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return status;
2505}
2506
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002507static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2508 const struct qstr *name, struct nfs_fh *fhandle,
2509 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002510{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002511 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002512 int status;
2513 struct nfs4_lookup_arg args = {
2514 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002515 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002516 .name = name,
2517 };
2518 struct nfs4_lookup_res res = {
2519 .server = server,
2520 .fattr = fattr,
2521 .fh = fhandle,
2522 };
2523 struct rpc_message msg = {
2524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2525 .rpc_argp = &args,
2526 .rpc_resp = &res,
2527 };
2528
2529 nfs_fattr_init(fattr);
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002532 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 dprintk("NFS reply lookup: %d\n", status);
2534 return status;
2535}
2536
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002537static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002538{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002539 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002540 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002541 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2542 fattr->nlink = 2;
2543}
2544
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002545static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2546 struct qstr *name, struct nfs_fh *fhandle,
2547 struct nfs_fattr *fattr)
2548{
2549 struct nfs4_exception exception = { };
2550 struct rpc_clnt *client = *clnt;
2551 int err;
2552 do {
2553 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2554 switch (err) {
2555 case -NFS4ERR_BADNAME:
2556 err = -ENOENT;
2557 goto out;
2558 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002559 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002560 goto out;
2561 case -NFS4ERR_WRONGSEC:
2562 err = -EPERM;
2563 if (client != *clnt)
2564 goto out;
2565
2566 client = nfs4_create_sec_client(client, dir, name);
2567 if (IS_ERR(client))
2568 return PTR_ERR(client);
2569
2570 exception.retry = 1;
2571 break;
2572 default:
2573 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2574 }
2575 } while (exception.retry);
2576
2577out:
2578 if (err == 0)
2579 *clnt = client;
2580 else if (client != *clnt)
2581 rpc_shutdown_client(client);
2582
2583 return err;
2584}
2585
Bryan Schumaker7c513052011-03-24 17:12:24 +00002586static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2587 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002589 int status;
2590 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002591
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002592 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2593 if (client != NFS_CLIENT(dir)) {
2594 rpc_shutdown_client(client);
2595 nfs_fixup_secinfo_attributes(fattr);
2596 }
2597 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002600struct rpc_clnt *
2601nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2602 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2603{
2604 int status;
2605 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2606
2607 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2608 if (status < 0) {
2609 rpc_shutdown_client(client);
2610 return ERR_PTR(status);
2611 }
2612 return client;
2613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2616{
Trond Myklebust76b32992007-08-10 17:45:11 -04002617 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 struct nfs4_accessargs args = {
2619 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002620 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002622 struct nfs4_accessres res = {
2623 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002624 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 struct rpc_message msg = {
2626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2627 .rpc_argp = &args,
2628 .rpc_resp = &res,
2629 .rpc_cred = entry->cred,
2630 };
2631 int mode = entry->mask;
2632 int status;
2633
2634 /*
2635 * Determine which access bits we want to ask for...
2636 */
2637 if (mode & MAY_READ)
2638 args.access |= NFS4_ACCESS_READ;
2639 if (S_ISDIR(inode->i_mode)) {
2640 if (mode & MAY_WRITE)
2641 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2642 if (mode & MAY_EXEC)
2643 args.access |= NFS4_ACCESS_LOOKUP;
2644 } else {
2645 if (mode & MAY_WRITE)
2646 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2647 if (mode & MAY_EXEC)
2648 args.access |= NFS4_ACCESS_EXECUTE;
2649 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002650
2651 res.fattr = nfs_alloc_fattr();
2652 if (res.fattr == NULL)
2653 return -ENOMEM;
2654
Bryan Schumaker7c513052011-03-24 17:12:24 +00002655 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (!status) {
2657 entry->mask = 0;
2658 if (res.access & NFS4_ACCESS_READ)
2659 entry->mask |= MAY_READ;
2660 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2661 entry->mask |= MAY_WRITE;
2662 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2663 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002664 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002666 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 return status;
2668}
2669
2670static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2671{
2672 struct nfs4_exception exception = { };
2673 int err;
2674 do {
2675 err = nfs4_handle_exception(NFS_SERVER(inode),
2676 _nfs4_proc_access(inode, entry),
2677 &exception);
2678 } while (exception.retry);
2679 return err;
2680}
2681
2682/*
2683 * TODO: For the time being, we don't try to get any attributes
2684 * along with any of the zero-copy operations READ, READDIR,
2685 * READLINK, WRITE.
2686 *
2687 * In the case of the first three, we want to put the GETATTR
2688 * after the read-type operation -- this is because it is hard
2689 * to predict the length of a GETATTR response in v4, and thus
2690 * align the READ data correctly. This means that the GETATTR
2691 * may end up partially falling into the page cache, and we should
2692 * shift it into the 'tail' of the xdr_buf before processing.
2693 * To do this efficiently, we need to know the total length
2694 * of data received, which doesn't seem to be available outside
2695 * of the RPC layer.
2696 *
2697 * In the case of WRITE, we also want to put the GETATTR after
2698 * the operation -- in this case because we want to make sure
2699 * we get the post-operation mtime and size. This means that
2700 * we can't use xdr_encode_pages() as written: we need a variant
2701 * of it which would leave room in the 'tail' iovec.
2702 *
2703 * Both of these changes to the XDR layer would in fact be quite
2704 * minor, but I decided to leave them for a subsequent patch.
2705 */
2706static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2707 unsigned int pgbase, unsigned int pglen)
2708{
2709 struct nfs4_readlink args = {
2710 .fh = NFS_FH(inode),
2711 .pgbase = pgbase,
2712 .pglen = pglen,
2713 .pages = &page,
2714 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002715 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct rpc_message msg = {
2717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2718 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002719 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 };
2721
Bryan Schumaker7c513052011-03-24 17:12:24 +00002722 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 -07002723}
2724
2725static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2726 unsigned int pgbase, unsigned int pglen)
2727{
2728 struct nfs4_exception exception = { };
2729 int err;
2730 do {
2731 err = nfs4_handle_exception(NFS_SERVER(inode),
2732 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2733 &exception);
2734 } while (exception.retry);
2735 return err;
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738/*
2739 * Got race?
2740 * We will need to arrange for the VFS layer to provide an atomic open.
2741 * Until then, this create/open method is prone to inefficiency and race
2742 * conditions due to the lookup, create, and open VFS calls from sys_open()
2743 * placed on the wire.
2744 *
2745 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2746 * The file will be opened again in the subsequent VFS open call
2747 * (nfs4_proc_file_open).
2748 *
2749 * The open for read will just hang around to be used by any process that
2750 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2751 */
2752
2753static int
2754nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002755 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
Al Viro82a2c1b2011-06-22 18:30:55 -04002757 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002759 struct rpc_cred *cred = NULL;
2760 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 int status = 0;
2762
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002763 if (ctx != NULL) {
2764 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002765 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002766 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002768 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002769 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002770 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (IS_ERR(state)) {
2772 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002775 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002776 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002777 if (ctx != NULL)
2778 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002779 else
Al Viro643168c2011-06-22 18:20:23 -04002780 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781out:
2782 return status;
2783}
2784
2785static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2786{
Trond Myklebust16e42952005-10-27 22:12:44 -04002787 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002788 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002790 .name.len = name->len,
2791 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002792 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002794 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002795 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002796 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2799 .rpc_argp = &args,
2800 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002802 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Trond Myklebustd3468902010-04-16 16:22:50 -04002804 res.dir_attr = nfs_alloc_fattr();
2805 if (res.dir_attr == NULL)
2806 goto out;
2807
Bryan Schumaker7c513052011-03-24 17:12:24 +00002808 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002809 if (status == 0) {
2810 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002811 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002812 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002813 nfs_free_fattr(res.dir_attr);
2814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2819{
2820 struct nfs4_exception exception = { };
2821 int err;
2822 do {
2823 err = nfs4_handle_exception(NFS_SERVER(dir),
2824 _nfs4_proc_remove(dir, name),
2825 &exception);
2826 } while (exception.retry);
2827 return err;
2828}
2829
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002830static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002832 struct nfs_server *server = NFS_SERVER(dir);
2833 struct nfs_removeargs *args = msg->rpc_argp;
2834 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Trond Myklebusta65318b2009-03-11 14:10:28 -04002836 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002837 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002839 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002842static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2843{
2844 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2845 &data->args.seq_args,
2846 &data->res.seq_res,
2847 task))
2848 return;
2849 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002852static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002854 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2855
Trond Myklebust14516c32010-07-31 14:29:06 -04002856 if (!nfs4_sequence_done(task, &res->seq_res))
2857 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002858 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002859 return 0;
2860 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002861 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002862 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
Jeff Laytond3d41522010-09-17 17:31:57 -04002865static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2866{
2867 struct nfs_server *server = NFS_SERVER(dir);
2868 struct nfs_renameargs *arg = msg->rpc_argp;
2869 struct nfs_renameres *res = msg->rpc_resp;
2870
2871 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2872 arg->bitmask = server->attr_bitmask;
2873 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002874 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002875}
2876
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002877static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2878{
2879 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2880 &data->args.seq_args,
2881 &data->res.seq_res,
2882 task))
2883 return;
2884 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002885}
2886
2887static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2888 struct inode *new_dir)
2889{
2890 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2891
2892 if (!nfs4_sequence_done(task, &res->seq_res))
2893 return 0;
2894 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2895 return 0;
2896
2897 update_changeattr(old_dir, &res->old_cinfo);
2898 nfs_post_op_update_inode(old_dir, res->old_fattr);
2899 update_changeattr(new_dir, &res->new_cinfo);
2900 nfs_post_op_update_inode(new_dir, res->new_fattr);
2901 return 1;
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2905 struct inode *new_dir, struct qstr *new_name)
2906{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002907 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002908 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 .old_dir = NFS_FH(old_dir),
2910 .new_dir = NFS_FH(new_dir),
2911 .old_name = old_name,
2912 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002913 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002915 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002916 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002917 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2920 .rpc_argp = &arg,
2921 .rpc_resp = &res,
2922 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002923 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebust011fff72010-04-16 16:22:49 -04002925 res.old_fattr = nfs_alloc_fattr();
2926 res.new_fattr = nfs_alloc_fattr();
2927 if (res.old_fattr == NULL || res.new_fattr == NULL)
2928 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Bryan Schumaker7c513052011-03-24 17:12:24 +00002930 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (!status) {
2932 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002933 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002935 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002937out:
2938 nfs_free_fattr(res.new_fattr);
2939 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return status;
2941}
2942
2943static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2944 struct inode *new_dir, struct qstr *new_name)
2945{
2946 struct nfs4_exception exception = { };
2947 int err;
2948 do {
2949 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2950 _nfs4_proc_rename(old_dir, old_name,
2951 new_dir, new_name),
2952 &exception);
2953 } while (exception.retry);
2954 return err;
2955}
2956
2957static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2958{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002959 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 struct nfs4_link_arg arg = {
2961 .fh = NFS_FH(inode),
2962 .dir_fh = NFS_FH(dir),
2963 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002964 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002966 struct nfs4_link_res res = {
2967 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002968 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct rpc_message msg = {
2970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2971 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002972 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002974 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Trond Myklebust136f2622010-04-16 16:22:49 -04002976 res.fattr = nfs_alloc_fattr();
2977 res.dir_attr = nfs_alloc_fattr();
2978 if (res.fattr == NULL || res.dir_attr == NULL)
2979 goto out;
2980
Bryan Schumaker7c513052011-03-24 17:12:24 +00002981 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002982 if (!status) {
2983 update_changeattr(dir, &res.cinfo);
2984 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002985 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002986 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002987out:
2988 nfs_free_fattr(res.dir_attr);
2989 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 return status;
2991}
2992
2993static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2994{
2995 struct nfs4_exception exception = { };
2996 int err;
2997 do {
2998 err = nfs4_handle_exception(NFS_SERVER(inode),
2999 _nfs4_proc_link(inode, dir, name),
3000 &exception);
3001 } while (exception.retry);
3002 return err;
3003}
3004
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003005struct nfs4_createdata {
3006 struct rpc_message msg;
3007 struct nfs4_create_arg arg;
3008 struct nfs4_create_res res;
3009 struct nfs_fh fh;
3010 struct nfs_fattr fattr;
3011 struct nfs_fattr dir_fattr;
3012};
3013
3014static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3015 struct qstr *name, struct iattr *sattr, u32 ftype)
3016{
3017 struct nfs4_createdata *data;
3018
3019 data = kzalloc(sizeof(*data), GFP_KERNEL);
3020 if (data != NULL) {
3021 struct nfs_server *server = NFS_SERVER(dir);
3022
3023 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3024 data->msg.rpc_argp = &data->arg;
3025 data->msg.rpc_resp = &data->res;
3026 data->arg.dir_fh = NFS_FH(dir);
3027 data->arg.server = server;
3028 data->arg.name = name;
3029 data->arg.attrs = sattr;
3030 data->arg.ftype = ftype;
3031 data->arg.bitmask = server->attr_bitmask;
3032 data->res.server = server;
3033 data->res.fh = &data->fh;
3034 data->res.fattr = &data->fattr;
3035 data->res.dir_fattr = &data->dir_fattr;
3036 nfs_fattr_init(data->res.fattr);
3037 nfs_fattr_init(data->res.dir_fattr);
3038 }
3039 return data;
3040}
3041
3042static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3043{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003044 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003045 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046 if (status == 0) {
3047 update_changeattr(dir, &data->res.dir_cinfo);
3048 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3049 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3050 }
3051 return status;
3052}
3053
3054static void nfs4_free_createdata(struct nfs4_createdata *data)
3055{
3056 kfree(data);
3057}
3058
Chuck Lever4f390c12006-08-22 20:06:22 -04003059static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003060 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003062 struct nfs4_createdata *data;
3063 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Chuck Lever94a6d752006-08-22 20:06:23 -04003065 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003066 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003067
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003068 status = -ENOMEM;
3069 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3070 if (data == NULL)
3071 goto out;
3072
3073 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3074 data->arg.u.symlink.pages = &page;
3075 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 status = nfs4_do_create(dir, dentry, data);
3078
3079 nfs4_free_createdata(data);
3080out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return status;
3082}
3083
Chuck Lever4f390c12006-08-22 20:06:22 -04003084static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003085 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
3087 struct nfs4_exception exception = { };
3088 int err;
3089 do {
3090 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003091 _nfs4_proc_symlink(dir, dentry, page,
3092 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 &exception);
3094 } while (exception.retry);
3095 return err;
3096}
3097
3098static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3099 struct iattr *sattr)
3100{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003101 struct nfs4_createdata *data;
3102 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3105 if (data == NULL)
3106 goto out;
3107
3108 status = nfs4_do_create(dir, dentry, data);
3109
3110 nfs4_free_createdata(data);
3111out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return status;
3113}
3114
3115static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3116 struct iattr *sattr)
3117{
3118 struct nfs4_exception exception = { };
3119 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003120
3121 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 do {
3123 err = nfs4_handle_exception(NFS_SERVER(dir),
3124 _nfs4_proc_mkdir(dir, dentry, sattr),
3125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003131 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
3133 struct inode *dir = dentry->d_inode;
3134 struct nfs4_readdir_arg args = {
3135 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003136 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 .pgbase = 0,
3138 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003139 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003140 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 };
3142 struct nfs4_readdir_res res;
3143 struct rpc_message msg = {
3144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3145 .rpc_argp = &args,
3146 .rpc_resp = &res,
3147 .rpc_cred = cred,
3148 };
3149 int status;
3150
Harvey Harrison3110ff82008-05-02 13:42:44 -07003151 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003152 dentry->d_parent->d_name.name,
3153 dentry->d_name.name,
3154 (unsigned long long)cookie);
Trond Myklebustf15e7242012-09-03 14:56:02 -04003155 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003157 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 -05003158 if (status >= 0) {
Trond Myklebustf15e7242012-09-03 14:56:02 -04003159 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003160 status += args.pgbase;
3161 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003162
3163 nfs_invalidate_atime(dir);
3164
Harvey Harrison3110ff82008-05-02 13:42:44 -07003165 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return status;
3167}
3168
3169static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003170 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
3172 struct nfs4_exception exception = { };
3173 int err;
3174 do {
3175 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3176 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003177 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 &exception);
3179 } while (exception.retry);
3180 return err;
3181}
3182
3183static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3184 struct iattr *sattr, dev_t rdev)
3185{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003186 struct nfs4_createdata *data;
3187 int mode = sattr->ia_mode;
3188 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3191 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003192
3193 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3194 if (data == NULL)
3195 goto out;
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003198 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003200 data->arg.ftype = NF4BLK;
3201 data->arg.u.device.specdata1 = MAJOR(rdev);
3202 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
3204 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003205 data->arg.ftype = NF4CHR;
3206 data->arg.u.device.specdata1 = MAJOR(rdev);
3207 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003210 status = nfs4_do_create(dir, dentry, data);
3211
3212 nfs4_free_createdata(data);
3213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return status;
3215}
3216
3217static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3218 struct iattr *sattr, dev_t rdev)
3219{
3220 struct nfs4_exception exception = { };
3221 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003222
3223 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 do {
3225 err = nfs4_handle_exception(NFS_SERVER(dir),
3226 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3227 &exception);
3228 } while (exception.retry);
3229 return err;
3230}
3231
3232static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3233 struct nfs_fsstat *fsstat)
3234{
3235 struct nfs4_statfs_arg args = {
3236 .fh = fhandle,
3237 .bitmask = server->attr_bitmask,
3238 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003239 struct nfs4_statfs_res res = {
3240 .fsstat = fsstat,
3241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 struct rpc_message msg = {
3243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3244 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003245 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 };
3247
Trond Myklebust0e574af2005-10-27 22:12:38 -04003248 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003249 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
3252static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3253{
3254 struct nfs4_exception exception = { };
3255 int err;
3256 do {
3257 err = nfs4_handle_exception(server,
3258 _nfs4_proc_statfs(server, fhandle, fsstat),
3259 &exception);
3260 } while (exception.retry);
3261 return err;
3262}
3263
3264static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3265 struct nfs_fsinfo *fsinfo)
3266{
3267 struct nfs4_fsinfo_arg args = {
3268 .fh = fhandle,
3269 .bitmask = server->attr_bitmask,
3270 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003271 struct nfs4_fsinfo_res res = {
3272 .fsinfo = fsinfo,
3273 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 struct rpc_message msg = {
3275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3276 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003277 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 };
3279
Bryan Schumaker7c513052011-03-24 17:12:24 +00003280 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
3283static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3284{
3285 struct nfs4_exception exception = { };
3286 int err;
3287
3288 do {
3289 err = nfs4_handle_exception(server,
3290 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3291 &exception);
3292 } while (exception.retry);
3293 return err;
3294}
3295
3296static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3297{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003298 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3300}
3301
3302static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3303 struct nfs_pathconf *pathconf)
3304{
3305 struct nfs4_pathconf_arg args = {
3306 .fh = fhandle,
3307 .bitmask = server->attr_bitmask,
3308 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003309 struct nfs4_pathconf_res res = {
3310 .pathconf = pathconf,
3311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 struct rpc_message msg = {
3313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3314 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003315 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 };
3317
3318 /* None of the pathconf attributes are mandatory to implement */
3319 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3320 memset(pathconf, 0, sizeof(*pathconf));
3321 return 0;
3322 }
3323
Trond Myklebust0e574af2005-10-27 22:12:38 -04003324 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003325 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
3328static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3329 struct nfs_pathconf *pathconf)
3330{
3331 struct nfs4_exception exception = { };
3332 int err;
3333
3334 do {
3335 err = nfs4_handle_exception(server,
3336 _nfs4_proc_pathconf(server, fhandle, pathconf),
3337 &exception);
3338 } while (exception.retry);
3339 return err;
3340}
3341
Benny Halevyd20581a2011-05-22 19:52:03 +03003342void __nfs4_read_done_cb(struct nfs_read_data *data)
3343{
3344 nfs_invalidate_atime(data->inode);
3345}
3346
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003347static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Trond Myklebustec06c092006-03-20 13:44:27 -05003349 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003351 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003352 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003353 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003355
Benny Halevyd20581a2011-05-22 19:52:03 +03003356 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003358 renew_lease(server, data->timestamp);
3359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003362static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3363{
3364
3365 dprintk("--> %s\n", __func__);
3366
3367 if (!nfs4_sequence_done(task, &data->res.seq_res))
3368 return -EAGAIN;
3369
Benny Halevyd20581a2011-05-22 19:52:03 +03003370 return data->read_done_cb ? data->read_done_cb(task, data) :
3371 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003372}
3373
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003374static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003377 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003378 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003379 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003382static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3383{
3384 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3385 &data->args.seq_args,
3386 &data->res.seq_res,
3387 task))
3388 return;
3389 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390}
3391
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003392/* Reset the the nfs_read_data to send the read to the MDS. */
3393void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3394{
3395 dprintk("%s Reset task for i/o through\n", __func__);
3396 put_lseg(data->lseg);
3397 data->lseg = NULL;
3398 /* offsets will differ in the dense stripe case */
3399 data->args.offset = data->mds_offset;
3400 data->ds_clp = NULL;
3401 data->args.fh = NFS_FH(data->inode);
3402 data->read_done_cb = nfs4_read_done_cb;
3403 task->tk_ops = data->mds_ops;
3404 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3405}
3406EXPORT_SYMBOL_GPL(nfs4_reset_read);
3407
Fred Isamanb029bc92011-03-03 15:13:42 +00003408static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct inode *inode = data->inode;
3411
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003412 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003413 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003414 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003416 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003418 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003419 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421}
3422
Fred Isamanb029bc92011-03-03 15:13:42 +00003423static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3424{
3425 if (!nfs4_sequence_done(task, &data->res.seq_res))
3426 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003427 return data->write_done_cb ? data->write_done_cb(task, data) :
3428 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003429}
3430
Fred Isamana69aef12011-03-03 15:13:47 +00003431/* Reset the the nfs_write_data to send the write to the MDS. */
3432void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3433{
3434 dprintk("%s Reset task for i/o through\n", __func__);
3435 put_lseg(data->lseg);
3436 data->lseg = NULL;
3437 data->ds_clp = NULL;
3438 data->write_done_cb = nfs4_write_done_cb;
3439 data->args.fh = NFS_FH(data->inode);
3440 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3441 data->args.offset = data->mds_offset;
3442 data->res.fattr = &data->fattr;
3443 task->tk_ops = data->mds_ops;
3444 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3445}
3446EXPORT_SYMBOL_GPL(nfs4_reset_write);
3447
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003448static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003450 struct nfs_server *server = NFS_SERVER(data->inode);
3451
Fred Isaman7ffd1062011-03-03 15:13:46 +00003452 if (data->lseg) {
3453 data->args.bitmask = NULL;
3454 data->res.fattr = NULL;
3455 } else
3456 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003457 if (!data->write_done_cb)
3458 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003459 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 data->timestamp = jiffies;
3461
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003462 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003463 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003466static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3467{
3468 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3469 &data->args.seq_args,
3470 &data->res.seq_res,
3471 task))
3472 return;
3473 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474}
3475
Fred Isaman5f452432011-03-23 13:27:46 +00003476static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003479
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003480 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003481 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003482 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003484 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Fred Isaman5f452432011-03-23 13:27:46 +00003488static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3489{
3490 if (!nfs4_sequence_done(task, &data->res.seq_res))
3491 return -EAGAIN;
3492 return data->write_done_cb(task, data);
3493}
3494
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003495static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003497 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003498
3499 if (data->lseg) {
3500 data->args.bitmask = NULL;
3501 data->res.fattr = NULL;
3502 } else
3503 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003504 if (!data->write_done_cb)
3505 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003506 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003507 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003508 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003511struct nfs4_renewdata {
3512 struct nfs_client *client;
3513 unsigned long timestamp;
3514};
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516/*
3517 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3518 * standalone procedure for queueing an asynchronous RENEW.
3519 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003520static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003521{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003522 struct nfs4_renewdata *data = calldata;
3523 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003524
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003525 if (atomic_read(&clp->cl_count) > 1)
3526 nfs4_schedule_state_renewal(clp);
3527 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003528 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003529}
3530
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003531static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003533 struct nfs4_renewdata *data = calldata;
3534 struct nfs_client *clp = data->client;
3535 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003538 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003539 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3540 return;
3541 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003542 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003543 return;
3544 }
3545 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003547 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548}
3549
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003550static const struct rpc_call_ops nfs4_renew_ops = {
3551 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003552 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003553};
3554
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003555static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3559 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003560 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003562 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003564 if (renew_flags == 0)
3565 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003566 if (!atomic_inc_not_zero(&clp->cl_count))
3567 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003568 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003569 if (data == NULL)
3570 return -ENOMEM;
3571 data->client = clp;
3572 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003574 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003577static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 struct rpc_message msg = {
3580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3581 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003582 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 };
3584 unsigned long now = jiffies;
3585 int status;
3586
3587 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3588 if (status < 0)
3589 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003590 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 return 0;
3592}
3593
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003594static inline int nfs4_server_supports_acls(struct nfs_server *server)
3595{
3596 return (server->caps & NFS_CAP_ACLS)
3597 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3598 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3599}
3600
3601/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3602 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3603 * the stack.
3604 */
3605#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3606
Neil Hormane9e3d722011-03-04 19:26:03 -05003607static int buf_to_pages_noslab(const void *buf, size_t buflen,
3608 struct page **pages, unsigned int *pgbase)
3609{
3610 struct page *newpage, **spages;
3611 int rc = 0;
3612 size_t len;
3613 spages = pages;
3614
3615 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003616 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003617 newpage = alloc_page(GFP_KERNEL);
3618
3619 if (newpage == NULL)
3620 goto unwind;
3621 memcpy(page_address(newpage), buf, len);
3622 buf += len;
3623 buflen -= len;
3624 *pages++ = newpage;
3625 rc++;
3626 } while (buflen != 0);
3627
3628 return rc;
3629
3630unwind:
3631 for(; rc > 0; rc--)
3632 __free_page(spages[rc-1]);
3633 return -ENOMEM;
3634}
3635
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003636struct nfs4_cached_acl {
3637 int cached;
3638 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003639 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003640};
3641
3642static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003643{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003644 struct nfs_inode *nfsi = NFS_I(inode);
3645
3646 spin_lock(&inode->i_lock);
3647 kfree(nfsi->nfs4_acl);
3648 nfsi->nfs4_acl = acl;
3649 spin_unlock(&inode->i_lock);
3650}
3651
3652static void nfs4_zap_acl_attr(struct inode *inode)
3653{
3654 nfs4_set_cached_acl(inode, NULL);
3655}
3656
3657static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3658{
3659 struct nfs_inode *nfsi = NFS_I(inode);
3660 struct nfs4_cached_acl *acl;
3661 int ret = -ENOENT;
3662
3663 spin_lock(&inode->i_lock);
3664 acl = nfsi->nfs4_acl;
3665 if (acl == NULL)
3666 goto out;
3667 if (buf == NULL) /* user is just asking for length */
3668 goto out_len;
3669 if (acl->cached == 0)
3670 goto out;
3671 ret = -ERANGE; /* see getxattr(2) man page */
3672 if (acl->len > buflen)
3673 goto out;
3674 memcpy(buf, acl->data, acl->len);
3675out_len:
3676 ret = acl->len;
3677out:
3678 spin_unlock(&inode->i_lock);
3679 return ret;
3680}
3681
Sachin Prabhu5794d212012-04-17 14:36:40 +01003682static 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 +00003683{
3684 struct nfs4_cached_acl *acl;
3685
Sachin Prabhu5794d212012-04-17 14:36:40 +01003686 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3688 if (acl == NULL)
3689 goto out;
3690 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003691 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003692 } else {
3693 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3694 if (acl == NULL)
3695 goto out;
3696 acl->cached = 0;
3697 }
3698 acl->len = acl_len;
3699out:
3700 nfs4_set_cached_acl(inode, acl);
3701}
3702
Andy Adamsonbf118a32011-12-07 11:55:27 -05003703/*
3704 * The getxattr API returns the required buffer length when called with a
3705 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3706 * the required buf. On a NULL buf, we send a page of data to the server
3707 * guessing that the ACL request can be serviced by a page. If so, we cache
3708 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3709 * the cache. If not so, we throw away the page, and cache the required
3710 * length. The next getxattr call will then produce another round trip to
3711 * the server, this time with the input buf of the required size.
3712 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003713static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003714{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003715 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003716 struct nfs_getaclargs args = {
3717 .fh = NFS_FH(inode),
3718 .acl_pages = pages,
3719 .acl_len = buflen,
3720 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003721 struct nfs_getaclres res = {
3722 .acl_len = buflen,
3723 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003724 struct rpc_message msg = {
3725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3726 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003727 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003728 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003729 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730
Andy Adamsonbf118a32011-12-07 11:55:27 -05003731 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3732 /* As long as we're doing a round trip to the server anyway,
3733 * let's be prepared for a page of acl data. */
3734 if (npages == 0)
3735 npages = 1;
3736
Sachin Prabhu5a006892012-04-17 14:35:39 +01003737 /* Add an extra page to handle the bitmap returned */
3738 npages++;
3739
Andy Adamsonbf118a32011-12-07 11:55:27 -05003740 for (i = 0; i < npages; i++) {
3741 pages[i] = alloc_page(GFP_KERNEL);
3742 if (!pages[i])
3743 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003744 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003745
3746 /* for decoding across pages */
3747 res.acl_scratch = alloc_page(GFP_KERNEL);
3748 if (!res.acl_scratch)
3749 goto out_free;
3750
Andy Adamsonbf118a32011-12-07 11:55:27 -05003751 args.acl_len = npages * PAGE_SIZE;
3752 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003753
Andy Adamsonbf118a32011-12-07 11:55:27 -05003754 /* Let decode_getfacl know not to fail if the ACL data is larger than
3755 * the page we send as a guess */
3756 if (buf == NULL)
3757 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003758
Peng Taode040be2012-01-10 22:42:47 +08003759 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760 __func__, buf, buflen, npages, args.acl_len);
3761 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3762 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003763 if (ret)
3764 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003765
3766 acl_len = res.acl_len - res.acl_data_offset;
3767 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003768 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003769 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003770 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003772 if (buf) {
3773 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003774 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003777 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003778 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003779 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003780out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003781 for (i = 0; i < npages; i++)
3782 if (pages[i])
3783 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003784 if (res.acl_scratch)
3785 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003786 return ret;
3787}
3788
Trond Myklebust16b42892006-08-24 12:27:15 -04003789static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3790{
3791 struct nfs4_exception exception = { };
3792 ssize_t ret;
3793 do {
3794 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3795 if (ret >= 0)
3796 break;
3797 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3798 } while (exception.retry);
3799 return ret;
3800}
3801
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003802static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3803{
3804 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003805 int ret;
3806
3807 if (!nfs4_server_supports_acls(server))
3808 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809 ret = nfs_revalidate_inode(server, inode);
3810 if (ret < 0)
3811 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003812 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3813 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003814 ret = nfs4_read_cached_acl(inode, buf, buflen);
3815 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003816 /* -ENOENT is returned if there is no ACL or if there is an ACL
3817 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003818 return ret;
3819 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003820}
3821
Trond Myklebust16b42892006-08-24 12:27:15 -04003822static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003823{
3824 struct nfs_server *server = NFS_SERVER(inode);
3825 struct page *pages[NFS4ACL_MAXPAGES];
3826 struct nfs_setaclargs arg = {
3827 .fh = NFS_FH(inode),
3828 .acl_pages = pages,
3829 .acl_len = buflen,
3830 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003831 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003832 struct rpc_message msg = {
3833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3834 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003835 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003836 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003837 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003838
3839 if (!nfs4_server_supports_acls(server))
3840 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003841 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3842 if (i < 0)
3843 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003844 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003845 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003846
3847 /*
3848 * Free each page after tx, so the only ref left is
3849 * held by the network stack
3850 */
3851 for (; i > 0; i--)
3852 put_page(pages[i-1]);
3853
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003854 /*
3855 * Acl update can result in inode attribute update.
3856 * so mark the attribute cache invalid.
3857 */
3858 spin_lock(&inode->i_lock);
3859 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3860 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003861 nfs_access_zap_cache(inode);
3862 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003863 return ret;
3864}
3865
Trond Myklebust16b42892006-08-24 12:27:15 -04003866static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3867{
3868 struct nfs4_exception exception = { };
3869 int err;
3870 do {
3871 err = nfs4_handle_exception(NFS_SERVER(inode),
3872 __nfs4_proc_set_acl(inode, buf, buflen),
3873 &exception);
3874 } while (exception.retry);
3875 return err;
3876}
3877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003879nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003881 struct nfs_client *clp = server->nfs_client;
3882
3883 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return 0;
3885 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003886 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003887 case -NFS4ERR_ADMIN_REVOKED:
3888 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003889 if (state == NULL)
3890 break;
3891 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003892 case -NFS4ERR_OPENMODE:
3893 if (state == NULL)
3894 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003895 nfs4_schedule_stateid_recovery(server, state);
3896 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003897 case -NFS4ERR_EXPIRED:
3898 if (state != NULL)
3899 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003901 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003902 nfs4_schedule_lease_recovery(clp);
3903 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003904#if defined(CONFIG_NFS_V4_1)
3905 case -NFS4ERR_BADSESSION:
3906 case -NFS4ERR_BADSLOT:
3907 case -NFS4ERR_BAD_HIGH_SLOT:
3908 case -NFS4ERR_DEADSESSION:
3909 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3910 case -NFS4ERR_SEQ_FALSE_RETRY:
3911 case -NFS4ERR_SEQ_MISORDERED:
3912 dprintk("%s ERROR %d, Reset session\n", __func__,
3913 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003914 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003915 task->tk_status = 0;
3916 return -EAGAIN;
3917#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003919 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003920 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003921 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3923 task->tk_status = 0;
3924 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003925 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 case -NFS4ERR_OLD_STATEID:
3927 task->tk_status = 0;
3928 return -EAGAIN;
3929 }
3930 task->tk_status = nfs4_map_errors(task->tk_status);
3931 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003932wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003933 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003934 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3935 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3936 task->tk_status = 0;
3937 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
Chuck Levercd937102012-03-02 17:14:31 -05003940static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3941 nfs4_verifier *bootverf)
3942{
3943 __be32 verf[2];
3944
3945 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3946 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3947 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3948}
3949
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003950int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3951 unsigned short port, struct rpc_cred *cred,
3952 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
3954 nfs4_verifier sc_verifier;
3955 struct nfs4_setclientid setclientid = {
3956 .sc_verifier = &sc_verifier,
3957 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003958 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 };
3960 struct rpc_message msg = {
3961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3962 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003963 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003964 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 int loop = 0;
3967 int status;
3968
Chuck Levercd937102012-03-02 17:14:31 -05003969 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003972 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003974 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003975 clp->cl_ipaddr,
3976 rpc_peeraddr2str(clp->cl_rpcclient,
3977 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003978 rpc_peeraddr2str(clp->cl_rpcclient,
3979 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003980 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 clp->cl_id_uniquifier);
3982 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003983 sizeof(setclientid.sc_netid),
3984 rpc_peeraddr2str(clp->cl_rpcclient,
3985 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003987 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003989 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003991 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 if (status != -NFS4ERR_CLID_INUSE)
3993 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003994 if (loop != 0) {
3995 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003997 }
3998 ++loop;
3999 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 }
4001 return status;
4002}
4003
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004004int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004005 struct nfs4_setclientid_res *arg,
4006 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
4008 struct nfs_fsinfo fsinfo;
4009 struct rpc_message msg = {
4010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004011 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004013 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 };
4015 unsigned long now;
4016 int status;
4017
4018 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004019 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (status == 0) {
4021 spin_lock(&clp->cl_lock);
4022 clp->cl_lease_time = fsinfo.lease_time * HZ;
4023 clp->cl_last_renewal = now;
4024 spin_unlock(&clp->cl_lock);
4025 }
4026 return status;
4027}
4028
Trond Myklebustfe650402006-01-03 09:55:18 +01004029struct nfs4_delegreturndata {
4030 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004031 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004032 struct nfs_fh fh;
4033 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004034 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004035 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004036 int rpc_status;
4037};
4038
Trond Myklebustfe650402006-01-03 09:55:18 +01004039static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4040{
4041 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004042
Trond Myklebust14516c32010-07-31 14:29:06 -04004043 if (!nfs4_sequence_done(task, &data->res.seq_res))
4044 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004045
Ricardo Labiaga79708862009-12-07 09:23:21 -05004046 switch (task->tk_status) {
4047 case -NFS4ERR_STALE_STATEID:
4048 case -NFS4ERR_EXPIRED:
4049 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004050 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004051 break;
4052 default:
4053 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4054 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004055 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004056 return;
4057 }
4058 }
4059 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004060}
4061
4062static void nfs4_delegreturn_release(void *calldata)
4063{
Trond Myklebustfe650402006-01-03 09:55:18 +01004064 kfree(calldata);
4065}
4066
Andy Adamson938e1012009-04-01 09:22:28 -04004067#if defined(CONFIG_NFS_V4_1)
4068static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4069{
4070 struct nfs4_delegreturndata *d_data;
4071
4072 d_data = (struct nfs4_delegreturndata *)data;
4073
Trond Myklebust035168a2010-06-16 09:52:26 -04004074 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004075 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004076 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004077 return;
4078 rpc_call_start(task);
4079}
4080#endif /* CONFIG_NFS_V4_1 */
4081
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004082static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004083#if defined(CONFIG_NFS_V4_1)
4084 .rpc_call_prepare = nfs4_delegreturn_prepare,
4085#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004086 .rpc_call_done = nfs4_delegreturn_done,
4087 .rpc_release = nfs4_delegreturn_release,
4088};
4089
Trond Myklebuste6f81072008-01-24 18:14:34 -05004090static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004091{
4092 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004093 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004094 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004095 struct rpc_message msg = {
4096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4097 .rpc_cred = cred,
4098 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004099 struct rpc_task_setup task_setup_data = {
4100 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004101 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004102 .callback_ops = &nfs4_delegreturn_ops,
4103 .flags = RPC_TASK_ASYNC,
4104 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004105 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004106
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004107 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004108 if (data == NULL)
4109 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004110 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004111 data->args.fhandle = &data->fh;
4112 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004113 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004114 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004115 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004116 data->res.fattr = &data->fattr;
4117 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004118 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004119 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004120 data->rpc_status = 0;
4121
Trond Myklebustc970aa82007-07-14 15:39:59 -04004122 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004123 msg.rpc_argp = &data->args;
4124 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004125 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004126 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004127 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004128 if (!issync)
4129 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004130 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004131 if (status != 0)
4132 goto out;
4133 status = data->rpc_status;
4134 if (status != 0)
4135 goto out;
4136 nfs_refresh_inode(inode, &data->fattr);
4137out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004138 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004139 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
Trond Myklebuste6f81072008-01-24 18:14:34 -05004142int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 struct nfs_server *server = NFS_SERVER(inode);
4145 struct nfs4_exception exception = { };
4146 int err;
4147 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004148 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 switch (err) {
4150 case -NFS4ERR_STALE_STATEID:
4151 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 case 0:
4153 return 0;
4154 }
4155 err = nfs4_handle_exception(server, err, &exception);
4156 } while (exception.retry);
4157 return err;
4158}
4159
4160#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4161#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4162
4163/*
4164 * sleep, with exponential backoff, and retry the LOCK operation.
4165 */
4166static unsigned long
4167nfs4_set_lock_task_retry(unsigned long timeout)
4168{
Jeff Laytond3103102011-12-01 22:44:39 +01004169 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 timeout <<= 1;
4171 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4172 return NFS4_LOCK_MAXTIMEOUT;
4173 return timeout;
4174}
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4177{
4178 struct inode *inode = state->inode;
4179 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004180 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004181 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004183 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004185 struct nfs_lockt_res res = {
4186 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 };
4188 struct rpc_message msg = {
4189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4190 .rpc_argp = &arg,
4191 .rpc_resp = &res,
4192 .rpc_cred = state->owner->so_cred,
4193 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 struct nfs4_lock_state *lsp;
4195 int status;
4196
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004197 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004198 status = nfs4_set_lock_state(state, request);
4199 if (status != 0)
4200 goto out;
4201 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004202 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004203 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004204 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004205 switch (status) {
4206 case 0:
4207 request->fl_type = F_UNLCK;
4208 break;
4209 case -NFS4ERR_DENIED:
4210 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004212 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return status;
4215}
4216
4217static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4218{
4219 struct nfs4_exception exception = { };
4220 int err;
4221
4222 do {
4223 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4224 _nfs4_proc_getlk(state, cmd, request),
4225 &exception);
4226 } while (exception.retry);
4227 return err;
4228}
4229
4230static int do_vfs_lock(struct file *file, struct file_lock *fl)
4231{
4232 int res = 0;
4233 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4234 case FL_POSIX:
4235 res = posix_lock_file_wait(file, fl);
4236 break;
4237 case FL_FLOCK:
4238 res = flock_lock_file_wait(file, fl);
4239 break;
4240 default:
4241 BUG();
4242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return res;
4244}
4245
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004246struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004247 struct nfs_locku_args arg;
4248 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004249 struct nfs4_lock_state *lsp;
4250 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004251 struct file_lock fl;
4252 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004253 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004254};
4255
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004256static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4257 struct nfs_open_context *ctx,
4258 struct nfs4_lock_state *lsp,
4259 struct nfs_seqid *seqid)
4260{
4261 struct nfs4_unlockdata *p;
4262 struct inode *inode = lsp->ls_state->inode;
4263
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004264 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004265 if (p == NULL)
4266 return NULL;
4267 p->arg.fh = NFS_FH(inode);
4268 p->arg.fl = &p->fl;
4269 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004270 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004271 p->arg.stateid = &lsp->ls_stateid;
4272 p->lsp = lsp;
4273 atomic_inc(&lsp->ls_count);
4274 /* Ensure we don't close file until we're done freeing locks! */
4275 p->ctx = get_nfs_open_context(ctx);
4276 memcpy(&p->fl, fl, sizeof(p->fl));
4277 p->server = NFS_SERVER(inode);
4278 return p;
4279}
4280
Trond Myklebust06f814a2006-01-03 09:55:07 +01004281static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004282{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004283 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004284 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004285 nfs4_put_lock_state(calldata->lsp);
4286 put_nfs_open_context(calldata->ctx);
4287 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004288}
4289
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004290static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004291{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004292 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004293
Trond Myklebust14516c32010-07-31 14:29:06 -04004294 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4295 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004296 switch (task->tk_status) {
4297 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004298 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4299 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004300 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004301 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004302 case -NFS4ERR_BAD_STATEID:
4303 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004304 case -NFS4ERR_STALE_STATEID:
4305 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004306 break;
4307 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004308 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004309 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310 }
Trond Myklebust8827f312012-10-29 18:53:23 -04004311 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004312}
4313
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004314static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004315{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004316 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004318 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004319 return;
4320 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004321 /* Note: exit _without_ running nfs4_locku_done */
4322 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004323 return;
4324 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004325 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004326 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004327 &calldata->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004328 &calldata->res.seq_res,
4329 task) != 0)
4330 nfs_release_seqid(calldata->arg.seqid);
4331 else
4332 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004335static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004336 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004337 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004338 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004339};
4340
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004341static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4342 struct nfs_open_context *ctx,
4343 struct nfs4_lock_state *lsp,
4344 struct nfs_seqid *seqid)
4345{
4346 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4349 .rpc_cred = ctx->cred,
4350 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004351 struct rpc_task_setup task_setup_data = {
4352 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004353 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004354 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004355 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004356 .flags = RPC_TASK_ASYNC,
4357 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358
Frank Filz137d6ac2007-07-09 15:32:29 -07004359 /* Ensure this is an unlock - when canceling a lock, the
4360 * canceled lock is passed in, and it won't be an unlock.
4361 */
4362 fl->fl_type = F_UNLCK;
4363
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004364 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4365 if (data == NULL) {
4366 nfs_free_seqid(seqid);
4367 return ERR_PTR(-ENOMEM);
4368 }
4369
Trond Myklebust9d12b212012-01-17 22:04:25 -05004370 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004371 msg.rpc_argp = &data->arg;
4372 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004373 task_setup_data.callback_data = data;
4374 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004375}
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4378{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004379 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004380 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004381 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004382 struct rpc_task *task;
4383 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004384 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Trond Myklebust9b073572006-06-29 16:38:34 -04004386 status = nfs4_set_lock_state(state, request);
4387 /* Unlock _before_ we do the RPC call */
4388 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004389 down_read(&nfsi->rwsem);
4390 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4391 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004392 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004393 }
4394 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004395 if (status != 0)
4396 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004397 /* Is this a delegated lock? */
4398 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004399 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004400 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004401 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004402 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004403 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004404 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004405 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406 status = PTR_ERR(task);
4407 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004408 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004409 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004410 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004411out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004412 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004413 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004416struct nfs4_lockdata {
4417 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004418 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419 struct nfs4_lock_state *lsp;
4420 struct nfs_open_context *ctx;
4421 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004422 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423 int rpc_status;
4424 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004425 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426};
4427
4428static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004429 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4430 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431{
4432 struct nfs4_lockdata *p;
4433 struct inode *inode = lsp->ls_state->inode;
4434 struct nfs_server *server = NFS_SERVER(inode);
4435
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004436 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004437 if (p == NULL)
4438 return NULL;
4439
4440 p->arg.fh = NFS_FH(inode);
4441 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004442 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004443 if (p->arg.open_seqid == NULL)
4444 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004445 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004446 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004447 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004449 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004450 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004451 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004452 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004454 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004455 atomic_inc(&lsp->ls_count);
4456 p->ctx = get_nfs_open_context(ctx);
4457 memcpy(&p->fl, fl, sizeof(p->fl));
4458 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004459out_free_seqid:
4460 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004461out_free:
4462 kfree(p);
4463 return NULL;
4464}
4465
4466static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4467{
4468 struct nfs4_lockdata *data = calldata;
4469 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Harvey Harrison3110ff82008-05-02 13:42:44 -07004471 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004472 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4473 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474 /* Do we need to do an open_to_lock_owner? */
4475 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004476 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
Trond Myklebust0ada2102012-10-29 18:37:40 -04004477 goto out_release_lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004478 data->arg.open_stateid = &state->stateid;
4479 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004480 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004481 } else
4482 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004483 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004484 if (nfs4_setup_sequence(data->server,
4485 &data->arg.seq_args,
Trond Myklebust0ada2102012-10-29 18:37:40 -04004486 &data->res.seq_res,
4487 task) == 0) {
4488 rpc_call_start(task);
Andy Adamson66179ef2009-04-01 09:22:22 -04004489 return;
Trond Myklebust0ada2102012-10-29 18:37:40 -04004490 }
4491 nfs_release_seqid(data->arg.open_seqid);
4492out_release_lock_seqid:
4493 nfs_release_seqid(data->arg.lock_seqid);
4494 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004495}
4496
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004497static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4498{
4499 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4500 nfs4_lock_prepare(task, calldata);
4501}
4502
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004503static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4504{
4505 struct nfs4_lockdata *data = calldata;
4506
Harvey Harrison3110ff82008-05-02 13:42:44 -07004507 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508
Trond Myklebust14516c32010-07-31 14:29:06 -04004509 if (!nfs4_sequence_done(task, &data->res.seq_res))
4510 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004511
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004513 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004514 if (data->rpc_status == 0)
4515 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4516 else
4517 goto out;
4518 }
4519 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004520 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004521 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004522 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004523 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004524out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004525 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004526}
4527
4528static void nfs4_lock_release(void *calldata)
4529{
4530 struct nfs4_lockdata *data = calldata;
4531
Harvey Harrison3110ff82008-05-02 13:42:44 -07004532 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004533 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 if (data->cancelled != 0) {
4535 struct rpc_task *task;
4536 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4537 data->arg.lock_seqid);
4538 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004539 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004540 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541 } else
4542 nfs_free_seqid(data->arg.lock_seqid);
4543 nfs4_put_lock_state(data->lsp);
4544 put_nfs_open_context(data->ctx);
4545 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004546 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004547}
4548
4549static const struct rpc_call_ops nfs4_lock_ops = {
4550 .rpc_call_prepare = nfs4_lock_prepare,
4551 .rpc_call_done = nfs4_lock_done,
4552 .rpc_release = nfs4_lock_release,
4553};
4554
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004555static const struct rpc_call_ops nfs4_recover_lock_ops = {
4556 .rpc_call_prepare = nfs4_recover_lock_prepare,
4557 .rpc_call_done = nfs4_lock_done,
4558 .rpc_release = nfs4_lock_release,
4559};
4560
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004561static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4562{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004563 switch (error) {
4564 case -NFS4ERR_ADMIN_REVOKED:
4565 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004566 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004567 if (new_lock_owner != 0 ||
4568 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004569 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004570 break;
4571 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004572 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004573 case -NFS4ERR_EXPIRED:
4574 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004575 };
4576}
4577
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004578static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004579{
4580 struct nfs4_lockdata *data;
4581 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004582 struct rpc_message msg = {
4583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4584 .rpc_cred = state->owner->so_cred,
4585 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004586 struct rpc_task_setup task_setup_data = {
4587 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004588 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004589 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004590 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004591 .flags = RPC_TASK_ASYNC,
4592 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 int ret;
4594
Harvey Harrison3110ff82008-05-02 13:42:44 -07004595 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004596 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004597 fl->fl_u.nfs4_fl.owner,
4598 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004599 if (data == NULL)
4600 return -ENOMEM;
4601 if (IS_SETLKW(cmd))
4602 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004603 if (recovery_type > NFS_LOCK_NEW) {
4604 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004605 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004606 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4607 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004608 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004609 msg.rpc_argp = &data->arg;
4610 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004611 task_setup_data.callback_data = data;
4612 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004613 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004614 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 ret = nfs4_wait_for_completion_rpc_task(task);
4616 if (ret == 0) {
4617 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004618 if (ret)
4619 nfs4_handle_setlk_error(data->server, data->lsp,
4620 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 } else
4622 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004623 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004624 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626}
4627
4628static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4629{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004630 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004631 struct nfs4_exception exception = {
4632 .inode = state->inode,
4633 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004634 int err;
4635
4636 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004637 /* Cache the lock if possible... */
4638 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4639 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004640 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004641 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004642 break;
4643 nfs4_handle_exception(server, err, &exception);
4644 } while (exception.retry);
4645 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646}
4647
4648static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4649{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004650 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004651 struct nfs4_exception exception = {
4652 .inode = state->inode,
4653 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004654 int err;
4655
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004656 err = nfs4_set_lock_state(state, request);
4657 if (err != 0)
4658 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004659 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004660 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4661 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004662 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004663 switch (err) {
4664 default:
4665 goto out;
4666 case -NFS4ERR_GRACE:
4667 case -NFS4ERR_DELAY:
4668 nfs4_handle_exception(server, err, &exception);
4669 err = 0;
4670 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004671 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004672out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004676#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004677static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004678{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004679 int status, ret = NFS_OK;
4680 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004681 struct nfs_server *server = NFS_SERVER(state->inode);
4682
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004683 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4684 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4685 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4686 if (status != NFS_OK) {
4687 nfs41_free_stateid(server, &lsp->ls_stateid);
4688 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4689 ret = status;
4690 }
4691 }
4692 };
4693
4694 return ret;
4695}
4696
4697static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4698{
4699 int status = NFS_OK;
4700
4701 if (test_bit(LK_STATE_IN_USE, &state->flags))
4702 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004703 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004704 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004705 return nfs4_lock_expired(state, request);
4706}
4707#endif
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4710{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004711 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004712 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004713 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004715 if ((fl_flags & FL_POSIX) &&
4716 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4717 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004718 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004719 status = nfs4_set_lock_state(state, request);
4720 if (status != 0)
4721 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004722 request->fl_flags |= FL_ACCESS;
4723 status = do_vfs_lock(request->fl_file, request);
4724 if (status < 0)
4725 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004726 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004728 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004730 request->fl_flags = fl_flags & ~FL_SLEEP;
4731 status = do_vfs_lock(request->fl_file, request);
4732 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004733 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004734 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004736 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004737 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004738 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004739 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004740 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4741 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004742out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004743 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004744out:
4745 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return status;
4747}
4748
4749static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4750{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004751 struct nfs4_exception exception = {
4752 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004753 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 int err;
4756
4757 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004758 err = _nfs4_proc_setlk(state, cmd, request);
4759 if (err == -NFS4ERR_DENIED)
4760 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004762 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 } while (exception.retry);
4764 return err;
4765}
4766
4767static int
4768nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4769{
4770 struct nfs_open_context *ctx;
4771 struct nfs4_state *state;
4772 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4773 int status;
4774
4775 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004776 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 state = ctx->state;
4778
4779 if (request->fl_start < 0 || request->fl_end < 0)
4780 return -EINVAL;
4781
Trond Myklebustd9531262009-07-21 19:22:38 -04004782 if (IS_GETLK(cmd)) {
4783 if (state != NULL)
4784 return nfs4_proc_getlk(state, F_GETLK, request);
4785 return 0;
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4789 return -EINVAL;
4790
Trond Myklebustd9531262009-07-21 19:22:38 -04004791 if (request->fl_type == F_UNLCK) {
4792 if (state != NULL)
4793 return nfs4_proc_unlck(state, cmd, request);
4794 return 0;
4795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Trond Myklebustd9531262009-07-21 19:22:38 -04004797 if (state == NULL)
4798 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004799 /*
4800 * Don't rely on the VFS having checked the file open mode,
4801 * since it won't do this for flock() locks.
4802 */
4803 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4804 case F_RDLCK:
4805 if (!(filp->f_mode & FMODE_READ))
4806 return -EBADF;
4807 break;
4808 case F_WRLCK:
4809 if (!(filp->f_mode & FMODE_WRITE))
4810 return -EBADF;
4811 }
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 do {
4814 status = nfs4_proc_setlk(state, cmd, request);
4815 if ((status != -EAGAIN) || IS_SETLK(cmd))
4816 break;
4817 timeout = nfs4_set_lock_task_retry(timeout);
4818 status = -ERESTARTSYS;
4819 if (signalled())
4820 break;
4821 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return status;
4823}
4824
Trond Myklebust888e6942005-11-04 15:38:11 -05004825int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4826{
4827 struct nfs_server *server = NFS_SERVER(state->inode);
4828 struct nfs4_exception exception = { };
4829 int err;
4830
4831 err = nfs4_set_lock_state(state, fl);
4832 if (err != 0)
4833 goto out;
4834 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004835 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004836 switch (err) {
4837 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004838 printk(KERN_ERR "NFS: %s: unhandled error "
4839 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004840 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004841 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004842 goto out;
4843 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004844 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004845 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004846 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004847 nfs4_schedule_lease_recovery(server->nfs_client);
4848 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004849 case -NFS4ERR_BADSESSION:
4850 case -NFS4ERR_BADSLOT:
4851 case -NFS4ERR_BAD_HIGH_SLOT:
4852 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4853 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004854 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004855 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004856 case -ERESTARTSYS:
4857 /*
4858 * The show must go on: exit, but mark the
4859 * stateid as needing recovery.
4860 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004861 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004862 case -NFS4ERR_ADMIN_REVOKED:
4863 case -NFS4ERR_BAD_STATEID:
4864 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004865 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004866 err = 0;
4867 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004868 case -EKEYEXPIRED:
4869 /*
4870 * User RPCSEC_GSS context has expired.
4871 * We cannot recover this stateid now, so
4872 * skip it and allow recovery thread to
4873 * proceed.
4874 */
4875 err = 0;
4876 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004877 case -ENOMEM:
4878 case -NFS4ERR_DENIED:
4879 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4880 err = 0;
4881 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004882 case -NFS4ERR_DELAY:
4883 break;
4884 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004885 err = nfs4_handle_exception(server, err, &exception);
4886 } while (exception.retry);
4887out:
4888 return err;
4889}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004890
Trond Myklebustcf470c32012-03-07 13:49:12 -05004891struct nfs_release_lockowner_data {
4892 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004893 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004894 struct nfs_release_lockowner_args args;
4895};
4896
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004897static void nfs4_release_lockowner_release(void *calldata)
4898{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004899 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004900 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004901 kfree(calldata);
4902}
4903
Trond Myklebust17280172012-03-11 13:11:00 -04004904static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004905 .rpc_release = nfs4_release_lockowner_release,
4906};
4907
Trond Myklebustcf470c32012-03-07 13:49:12 -05004908int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004909{
4910 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004911 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004912 struct rpc_message msg = {
4913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4914 };
4915
4916 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004917 return -EINVAL;
4918 data = kmalloc(sizeof(*data), GFP_NOFS);
4919 if (!data)
4920 return -ENOMEM;
4921 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004922 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004923 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4924 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4925 data->args.lock_owner.s_dev = server->s_dev;
4926 msg.rpc_argp = &data->args;
4927 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4928 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004929}
4930
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4932
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004933static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4934 const void *buf, size_t buflen,
4935 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004936{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004937 if (strcmp(key, "") != 0)
4938 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004939
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004940 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004941}
4942
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004943static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4944 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004945{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004946 if (strcmp(key, "") != 0)
4947 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004948
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004949 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004950}
4951
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004952static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4953 size_t list_len, const char *name,
4954 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004955{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004956 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004957
J. Bruce Fields096455a2006-03-20 23:23:42 -05004958 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4959 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004960
4961 if (list && len <= list_len)
4962 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004963 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004964}
4965
Andy Adamson533eb462011-06-13 18:25:56 -04004966/*
4967 * nfs_fhget will use either the mounted_on_fileid or the fileid
4968 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004969static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4970{
Andy Adamson533eb462011-06-13 18:25:56 -04004971 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4972 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4973 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004974 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004975 return;
4976
4977 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004978 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004979 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4980 fattr->nlink = 2;
4981}
4982
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004983static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4984 const struct qstr *name,
4985 struct nfs4_fs_locations *fs_locations,
4986 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004987{
4988 struct nfs_server *server = NFS_SERVER(dir);
4989 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004990 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004991 };
4992 struct nfs4_fs_locations_arg args = {
4993 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004994 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004995 .page = page,
4996 .bitmask = bitmask,
4997 };
Benny Halevy22958462009-04-01 09:22:02 -04004998 struct nfs4_fs_locations_res res = {
4999 .fs_locations = fs_locations,
5000 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5003 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005004 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005005 };
5006 int status;
5007
Harvey Harrison3110ff82008-05-02 13:42:44 -07005008 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005009
5010 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5011 * is not supported */
5012 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5013 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5014 else
5015 bitmask[0] |= FATTR4_WORD0_FILEID;
5016
Trond Myklebustc228fd32007-01-13 02:28:11 -05005017 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005018 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005019 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005020 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005021 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005022 return status;
5023}
5024
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005025int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5026 const struct qstr *name,
5027 struct nfs4_fs_locations *fs_locations,
5028 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005029{
5030 struct nfs4_exception exception = { };
5031 int err;
5032 do {
5033 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005034 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005035 &exception);
5036 } while (exception.retry);
5037 return err;
5038}
5039
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005040static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5041{
5042 int status;
5043 struct nfs4_secinfo_arg args = {
5044 .dir_fh = NFS_FH(dir),
5045 .name = name,
5046 };
5047 struct nfs4_secinfo_res res = {
5048 .flavors = flavors,
5049 };
5050 struct rpc_message msg = {
5051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5052 .rpc_argp = &args,
5053 .rpc_resp = &res,
5054 };
5055
5056 dprintk("NFS call secinfo %s\n", name->name);
5057 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5058 dprintk("NFS reply secinfo: %d\n", status);
5059 return status;
5060}
5061
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005062int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5063 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005064{
5065 struct nfs4_exception exception = { };
5066 int err;
5067 do {
5068 err = nfs4_handle_exception(NFS_SERVER(dir),
5069 _nfs4_proc_secinfo(dir, name, flavors),
5070 &exception);
5071 } while (exception.retry);
5072 return err;
5073}
5074
Andy Adamson557134a2009-04-01 09:21:53 -04005075#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005076/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005077 * Check the exchange flags returned by the server for invalid flags, having
5078 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5079 * DS flags set.
5080 */
5081static int nfs4_check_cl_exchange_flags(u32 flags)
5082{
5083 if (flags & ~EXCHGID4_FLAG_MASK_R)
5084 goto out_inval;
5085 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5086 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5087 goto out_inval;
5088 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5089 goto out_inval;
5090 return NFS_OK;
5091out_inval:
5092 return -NFS4ERR_INVAL;
5093}
5094
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005095static bool
5096nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5097{
5098 if (a->server_scope_sz == b->server_scope_sz &&
5099 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5100 return true;
5101
5102 return false;
5103}
5104
Andy Adamson357f54d2010-12-14 10:11:57 -05005105/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005106 * nfs4_proc_exchange_id()
5107 *
5108 * Since the clientid has expired, all compounds using sessions
5109 * associated with the stale clientid will be returning
5110 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5111 * be in some phase of session reset.
5112 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005113int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005114{
5115 nfs4_verifier verifier;
5116 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005117 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005118 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005119 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005120 };
5121 struct nfs41_exchange_id_res res = {
5122 .client = clp,
5123 };
5124 int status;
5125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5127 .rpc_argp = &args,
5128 .rpc_resp = &res,
5129 .rpc_cred = cred,
5130 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005131
5132 dprintk("--> %s\n", __func__);
5133 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005134
Chuck Levercd937102012-03-02 17:14:31 -05005135 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005136
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005137 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005138 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005139 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005140 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005141 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005142
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005143 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005144 if (unlikely(!res.server_scope)) {
5145 status = -ENOMEM;
5146 goto out;
5147 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005148
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005149 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5150 if (unlikely(!res.impl_id)) {
5151 status = -ENOMEM;
5152 goto out_server_scope;
5153 }
5154
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005155 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005156 if (!status)
5157 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005158
5159 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005160 /* use the most recent implementation id */
5161 kfree(clp->impl_id);
5162 clp->impl_id = res.impl_id;
5163 } else
5164 kfree(res.impl_id);
5165
5166 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005167 if (clp->server_scope &&
5168 !nfs41_same_server_scope(clp->server_scope,
5169 res.server_scope)) {
5170 dprintk("%s: server_scope mismatch detected\n",
5171 __func__);
5172 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5173 kfree(clp->server_scope);
5174 clp->server_scope = NULL;
5175 }
5176
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005177 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005178 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005179 goto out;
5180 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005181 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005182
5183out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005184 kfree(res.server_scope);
5185out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005186 if (clp->impl_id)
5187 dprintk("%s: Server Implementation ID: "
5188 "domain: %s, name: %s, date: %llu,%u\n",
5189 __func__, clp->impl_id->domain, clp->impl_id->name,
5190 clp->impl_id->date.seconds,
5191 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005192 dprintk("<-- %s status= %d\n", __func__, status);
5193 return status;
5194}
5195
Andy Adamson2050f0c2009-04-01 09:22:30 -04005196struct nfs4_get_lease_time_data {
5197 struct nfs4_get_lease_time_args *args;
5198 struct nfs4_get_lease_time_res *res;
5199 struct nfs_client *clp;
5200};
5201
5202static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5203 void *calldata)
5204{
5205 int ret;
5206 struct nfs4_get_lease_time_data *data =
5207 (struct nfs4_get_lease_time_data *)calldata;
5208
5209 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005210 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005211 /* just setup sequence, do not trigger session recovery
5212 since we're invoked within one */
5213 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005214 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005215 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005216
5217 BUG_ON(ret == -EAGAIN);
5218 rpc_call_start(task);
5219 dprintk("<-- %s\n", __func__);
5220}
5221
5222/*
5223 * Called from nfs4_state_manager thread for session setup, so don't recover
5224 * from sequence operation or clientid errors.
5225 */
5226static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5227{
5228 struct nfs4_get_lease_time_data *data =
5229 (struct nfs4_get_lease_time_data *)calldata;
5230
5231 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005232 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5233 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005234 switch (task->tk_status) {
5235 case -NFS4ERR_DELAY:
5236 case -NFS4ERR_GRACE:
5237 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5238 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5239 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005240 /* fall through */
5241 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005242 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005243 return;
5244 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005245 dprintk("<-- %s\n", __func__);
5246}
5247
Trond Myklebust17280172012-03-11 13:11:00 -04005248static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005249 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5250 .rpc_call_done = nfs4_get_lease_time_done,
5251};
5252
5253int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5254{
5255 struct rpc_task *task;
5256 struct nfs4_get_lease_time_args args;
5257 struct nfs4_get_lease_time_res res = {
5258 .lr_fsinfo = fsinfo,
5259 };
5260 struct nfs4_get_lease_time_data data = {
5261 .args = &args,
5262 .res = &res,
5263 .clp = clp,
5264 };
5265 struct rpc_message msg = {
5266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5267 .rpc_argp = &args,
5268 .rpc_resp = &res,
5269 };
5270 struct rpc_task_setup task_setup = {
5271 .rpc_client = clp->cl_rpcclient,
5272 .rpc_message = &msg,
5273 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005274 .callback_data = &data,
5275 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005276 };
5277 int status;
5278
Trond Myklebust9d12b212012-01-17 22:04:25 -05005279 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005280 dprintk("--> %s\n", __func__);
5281 task = rpc_run_task(&task_setup);
5282
5283 if (IS_ERR(task))
5284 status = PTR_ERR(task);
5285 else {
5286 status = task->tk_status;
5287 rpc_put_task(task);
5288 }
5289 dprintk("<-- %s return %d\n", __func__, status);
5290
5291 return status;
5292}
5293
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005294static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5295{
5296 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5297}
5298
5299static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5300 struct nfs4_slot *new,
5301 u32 max_slots,
5302 u32 ivalue)
5303{
5304 struct nfs4_slot *old = NULL;
5305 u32 i;
5306
5307 spin_lock(&tbl->slot_tbl_lock);
5308 if (new) {
5309 old = tbl->slots;
5310 tbl->slots = new;
5311 tbl->max_slots = max_slots;
5312 }
5313 tbl->highest_used_slotid = -1; /* no slot is currently used */
5314 for (i = 0; i < tbl->max_slots; i++)
5315 tbl->slots[i].seq_nr = ivalue;
5316 spin_unlock(&tbl->slot_tbl_lock);
5317 kfree(old);
5318}
5319
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005320/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005321 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005322 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005323static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5324 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005325{
Andy Adamson104aeba2010-01-14 17:45:10 -05005326 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005327 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005328
Andy Adamson104aeba2010-01-14 17:45:10 -05005329 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5330 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005331
Andy Adamson104aeba2010-01-14 17:45:10 -05005332 /* Does the newly negotiated max_reqs match the existing slot table? */
5333 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005334 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005335 if (!new)
5336 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005337 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005338 ret = 0;
5339
5340 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005341 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5342 tbl, tbl->slots, tbl->max_slots);
5343out:
5344 dprintk("<-- %s: return %d\n", __func__, ret);
5345 return ret;
5346}
5347
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005348/* Destroy the slot table */
5349static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5350{
5351 if (session->fc_slot_table.slots != NULL) {
5352 kfree(session->fc_slot_table.slots);
5353 session->fc_slot_table.slots = NULL;
5354 }
5355 if (session->bc_slot_table.slots != NULL) {
5356 kfree(session->bc_slot_table.slots);
5357 session->bc_slot_table.slots = NULL;
5358 }
5359 return;
5360}
5361
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005362/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005363 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005364 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005365static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005366{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005367 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005368 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005369
Andy Adamsonaacd5532011-11-09 13:58:21 -05005370 dprintk("--> %s\n", __func__);
5371 /* Fore channel */
5372 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005373 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5374 if (status) /* -ENOMEM */
5375 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005376 /* Back channel */
5377 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005378 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5379 if (status && tbl->slots == NULL)
5380 /* Fore and back channel share a connection so get
5381 * both slot tables or neither */
5382 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005383 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005384}
5385
5386struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5387{
5388 struct nfs4_session *session;
5389 struct nfs4_slot_table *tbl;
5390
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005391 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005392 if (!session)
5393 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005394
Andy Adamson557134a2009-04-01 09:21:53 -04005395 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005396 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005397 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005398 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005399 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005400
5401 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005402 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005403 spin_lock_init(&tbl->slot_tbl_lock);
5404 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005405 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005406
Trond Myklebust1055d762010-06-16 09:52:27 -04005407 session->session_state = 1<<NFS4_SESSION_INITING;
5408
Andy Adamson557134a2009-04-01 09:21:53 -04005409 session->clp = clp;
5410 return session;
5411}
5412
5413void nfs4_destroy_session(struct nfs4_session *session)
5414{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005415 struct rpc_xprt *xprt;
5416
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005417 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005418
5419 rcu_read_lock();
5420 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5421 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005422 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005423 __func__, xprt);
5424 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005425 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005426 kfree(session);
5427}
5428
Andy Adamsonfc931582009-04-01 09:22:31 -04005429/*
5430 * Initialize the values to be used by the client in CREATE_SESSION
5431 * If nfs4_init_session set the fore channel request and response sizes,
5432 * use them.
5433 *
5434 * Set the back channel max_resp_sz_cached to zero to force the client to
5435 * always set csa_cachethis to FALSE because the current implementation
5436 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5437 */
5438static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5439{
5440 struct nfs4_session *session = args->client->cl_session;
5441 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5442 mxresp_sz = session->fc_attrs.max_resp_sz;
5443
5444 if (mxrqst_sz == 0)
5445 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5446 if (mxresp_sz == 0)
5447 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5448 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005449 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5450 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005451 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005452 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005453
5454 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005455 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005456 __func__,
5457 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005458 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005459
5460 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005461 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5462 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5463 args->bc_attrs.max_resp_sz_cached = 0;
5464 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5465 args->bc_attrs.max_reqs = 1;
5466
5467 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5468 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5469 __func__,
5470 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5471 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5472 args->bc_attrs.max_reqs);
5473}
5474
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005475static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005476{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005477 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5478 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5479
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005480 if (rcvd->max_resp_sz > sent->max_resp_sz)
5481 return -EINVAL;
5482 /*
5483 * Our requested max_ops is the minimum we need; we're not
5484 * prepared to break up compounds into smaller pieces than that.
5485 * So, no point even trying to continue if the server won't
5486 * cooperate:
5487 */
5488 if (rcvd->max_ops < sent->max_ops)
5489 return -EINVAL;
5490 if (rcvd->max_reqs == 0)
5491 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005492 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5493 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005494 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005495}
5496
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005497static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5498{
5499 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5500 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005501
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005502 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5503 return -EINVAL;
5504 if (rcvd->max_resp_sz < sent->max_resp_sz)
5505 return -EINVAL;
5506 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5507 return -EINVAL;
5508 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005509 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005510 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005511 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005512 return -EINVAL;
5513 return 0;
5514}
Andy Adamson8d353012009-04-01 09:22:32 -04005515
Andy Adamson8d353012009-04-01 09:22:32 -04005516static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5517 struct nfs4_session *session)
5518{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005519 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005520
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005521 ret = nfs4_verify_fore_channel_attrs(args, session);
5522 if (ret)
5523 return ret;
5524 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005525}
5526
Andy Adamsonfc931582009-04-01 09:22:31 -04005527static int _nfs4_proc_create_session(struct nfs_client *clp)
5528{
5529 struct nfs4_session *session = clp->cl_session;
5530 struct nfs41_create_session_args args = {
5531 .client = clp,
5532 .cb_program = NFS4_CALLBACK,
5533 };
5534 struct nfs41_create_session_res res = {
5535 .client = clp,
5536 };
5537 struct rpc_message msg = {
5538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5539 .rpc_argp = &args,
5540 .rpc_resp = &res,
5541 };
5542 int status;
5543
5544 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005545 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005546
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005547 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005548
Andy Adamson8d353012009-04-01 09:22:32 -04005549 if (!status)
5550 /* Verify the session's negotiated channel_attrs values */
5551 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005552 if (!status) {
5553 /* Increment the clientid slot sequence id */
5554 clp->cl_seqid++;
5555 }
5556
5557 return status;
5558}
5559
5560/*
5561 * Issues a CREATE_SESSION operation to the server.
5562 * It is the responsibility of the caller to verify the session is
5563 * expired before calling this routine.
5564 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005565int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005566{
5567 int status;
5568 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005569 struct nfs4_session *session = clp->cl_session;
5570
5571 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5572
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005573 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005574 if (status)
5575 goto out;
5576
Andy Adamsonaacd5532011-11-09 13:58:21 -05005577 /* Init or reset the session slot tables */
5578 status = nfs4_setup_session_slot_tables(session);
5579 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005580 if (status)
5581 goto out;
5582
5583 ptr = (unsigned *)&session->sess_id.data[0];
5584 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5585 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005586out:
5587 dprintk("<-- %s\n", __func__);
5588 return status;
5589}
5590
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005591/*
5592 * Issue the over-the-wire RPC DESTROY_SESSION.
5593 * The caller must serialize access to this routine.
5594 */
5595int nfs4_proc_destroy_session(struct nfs4_session *session)
5596{
5597 int status = 0;
5598 struct rpc_message msg;
5599
5600 dprintk("--> nfs4_proc_destroy_session\n");
5601
5602 /* session is still being setup */
5603 if (session->clp->cl_cons_state != NFS_CS_READY)
5604 return status;
5605
5606 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5607 msg.rpc_argp = session;
5608 msg.rpc_resp = NULL;
5609 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005610 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005611
5612 if (status)
5613 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005614 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005615 "Session has been destroyed regardless...\n", status);
5616
5617 dprintk("<-- nfs4_proc_destroy_session\n");
5618 return status;
5619}
5620
Trond Myklebustfccba802009-07-21 16:48:07 -04005621int nfs4_init_session(struct nfs_server *server)
5622{
5623 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005624 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005625 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005626 int ret;
5627
5628 if (!nfs4_has_session(clp))
5629 return 0;
5630
Trond Myklebust1055d762010-06-16 09:52:27 -04005631 session = clp->cl_session;
5632 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5633 return 0;
5634
Andy Adamson68bf05e2009-12-15 12:55:02 -05005635 rsize = server->rsize;
5636 if (rsize == 0)
5637 rsize = NFS_MAX_FILE_IO_SIZE;
5638 wsize = server->wsize;
5639 if (wsize == 0)
5640 wsize = NFS_MAX_FILE_IO_SIZE;
5641
Andy Adamson68bf05e2009-12-15 12:55:02 -05005642 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5643 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005644
Trond Myklebustfccba802009-07-21 16:48:07 -04005645 ret = nfs4_recover_expired_lease(server);
5646 if (!ret)
5647 ret = nfs4_check_client_ready(clp);
5648 return ret;
5649}
5650
Andy Adamsond83217c2011-03-01 01:34:17 +00005651int nfs4_init_ds_session(struct nfs_client *clp)
5652{
5653 struct nfs4_session *session = clp->cl_session;
5654 int ret;
5655
5656 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5657 return 0;
5658
5659 ret = nfs4_client_recover_expired_lease(clp);
5660 if (!ret)
5661 /* Test for the DS role */
5662 if (!is_ds_client(clp))
5663 ret = -ENODEV;
5664 if (!ret)
5665 ret = nfs4_check_client_ready(clp);
5666 return ret;
5667
5668}
5669EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5670
5671
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005672/*
5673 * Renew the cl_session lease.
5674 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005675struct nfs4_sequence_data {
5676 struct nfs_client *clp;
5677 struct nfs4_sequence_args args;
5678 struct nfs4_sequence_res res;
5679};
5680
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005681static void nfs41_sequence_release(void *data)
5682{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005683 struct nfs4_sequence_data *calldata = data;
5684 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005685
Alexandros Batsakis71358402010-02-05 03:45:05 -08005686 if (atomic_read(&clp->cl_count) > 1)
5687 nfs4_schedule_state_renewal(clp);
5688 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005689 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005690}
5691
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005692static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5693{
5694 switch(task->tk_status) {
5695 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005696 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5697 return -EAGAIN;
5698 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005699 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005700 }
5701 return 0;
5702}
5703
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005704static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005705{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005706 struct nfs4_sequence_data *calldata = data;
5707 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005708
Trond Myklebust14516c32010-07-31 14:29:06 -04005709 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5710 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711
5712 if (task->tk_status < 0) {
5713 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005714 if (atomic_read(&clp->cl_count) == 1)
5715 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005716
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005717 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5718 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005719 return;
5720 }
5721 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005722 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005723out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005724 dprintk("<-- %s\n", __func__);
5725}
5726
5727static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5728{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005729 struct nfs4_sequence_data *calldata = data;
5730 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005731 struct nfs4_sequence_args *args;
5732 struct nfs4_sequence_res *res;
5733
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005734 args = task->tk_msg.rpc_argp;
5735 res = task->tk_msg.rpc_resp;
5736
Trond Myklebust9d12b212012-01-17 22:04:25 -05005737 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005738 return;
5739 rpc_call_start(task);
5740}
5741
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005742static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5743{
5744 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5745 nfs41_sequence_prepare(task, data);
5746}
5747
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005748static const struct rpc_call_ops nfs41_sequence_ops = {
5749 .rpc_call_done = nfs41_sequence_call_done,
5750 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005751 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005752};
5753
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005754static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5755 .rpc_call_done = nfs41_sequence_call_done,
5756 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5757 .rpc_release = nfs41_sequence_release,
5758};
5759
5760static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5761 const struct rpc_call_ops *seq_ops)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005762{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005763 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005764 struct rpc_message msg = {
5765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5766 .rpc_cred = cred,
5767 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005768 struct rpc_task_setup task_setup_data = {
5769 .rpc_client = clp->cl_rpcclient,
5770 .rpc_message = &msg,
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005771 .callback_ops = seq_ops,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005772 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5773 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005774
Alexandros Batsakis71358402010-02-05 03:45:05 -08005775 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005776 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005777 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005778 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005779 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005780 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005781 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005782 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005783 msg.rpc_argp = &calldata->args;
5784 msg.rpc_resp = &calldata->res;
5785 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005786 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005787
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005788 return rpc_run_task(&task_setup_data);
5789}
5790
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005791static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005792{
5793 struct rpc_task *task;
5794 int ret = 0;
5795
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005796 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5797 return 0;
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005798 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005799 if (IS_ERR(task))
5800 ret = PTR_ERR(task);
5801 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005802 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005803 dprintk("<-- %s status=%d\n", __func__, ret);
5804 return ret;
5805}
5806
5807static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5808{
5809 struct rpc_task *task;
5810 int ret;
5811
Bryan Schumakerd1967aa2012-11-12 16:55:38 -05005812 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005813 if (IS_ERR(task)) {
5814 ret = PTR_ERR(task);
5815 goto out;
5816 }
5817 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005818 if (!ret) {
5819 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5820
5821 if (task->tk_status == 0)
5822 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005823 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005824 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005825 rpc_put_task(task);
5826out:
5827 dprintk("<-- %s status=%d\n", __func__, ret);
5828 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005829}
5830
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005831struct nfs4_reclaim_complete_data {
5832 struct nfs_client *clp;
5833 struct nfs41_reclaim_complete_args arg;
5834 struct nfs41_reclaim_complete_res res;
5835};
5836
5837static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5838{
5839 struct nfs4_reclaim_complete_data *calldata = data;
5840
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005841 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005842 if (nfs41_setup_sequence(calldata->clp->cl_session,
5843 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005844 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005845 return;
5846
5847 rpc_call_start(task);
5848}
5849
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005850static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5851{
5852 switch(task->tk_status) {
5853 case 0:
5854 case -NFS4ERR_COMPLETE_ALREADY:
5855 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5856 break;
5857 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005858 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005859 /* fall through */
5860 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005861 return -EAGAIN;
5862 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005863 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005864 }
5865 return 0;
5866}
5867
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005868static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5869{
5870 struct nfs4_reclaim_complete_data *calldata = data;
5871 struct nfs_client *clp = calldata->clp;
5872 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5873
5874 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005875 if (!nfs41_sequence_done(task, res))
5876 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005877
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005878 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5879 rpc_restart_call_prepare(task);
5880 return;
5881 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005882 dprintk("<-- %s\n", __func__);
5883}
5884
5885static void nfs4_free_reclaim_complete_data(void *data)
5886{
5887 struct nfs4_reclaim_complete_data *calldata = data;
5888
5889 kfree(calldata);
5890}
5891
5892static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5893 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5894 .rpc_call_done = nfs4_reclaim_complete_done,
5895 .rpc_release = nfs4_free_reclaim_complete_data,
5896};
5897
5898/*
5899 * Issue a global reclaim complete.
5900 */
5901static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5902{
5903 struct nfs4_reclaim_complete_data *calldata;
5904 struct rpc_task *task;
5905 struct rpc_message msg = {
5906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5907 };
5908 struct rpc_task_setup task_setup_data = {
5909 .rpc_client = clp->cl_rpcclient,
5910 .rpc_message = &msg,
5911 .callback_ops = &nfs4_reclaim_complete_call_ops,
5912 .flags = RPC_TASK_ASYNC,
5913 };
5914 int status = -ENOMEM;
5915
5916 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005917 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005918 if (calldata == NULL)
5919 goto out;
5920 calldata->clp = clp;
5921 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005922
Trond Myklebust9d12b212012-01-17 22:04:25 -05005923 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 msg.rpc_argp = &calldata->arg;
5925 msg.rpc_resp = &calldata->res;
5926 task_setup_data.callback_data = calldata;
5927 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005928 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005929 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005930 goto out;
5931 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005932 status = nfs4_wait_for_completion_rpc_task(task);
5933 if (status == 0)
5934 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005935 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005936 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005937out:
5938 dprintk("<-- %s status=%d\n", __func__, status);
5939 return status;
5940}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005941
5942static void
5943nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5944{
5945 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005946 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005947
5948 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005949 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5950 * right now covering the LAYOUTGET we are about to send.
5951 * However, that is not so catastrophic, and there seems
5952 * to be no way to prevent it completely.
5953 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005954 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005955 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005956 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005957 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5958 NFS_I(lgp->args.inode)->layout,
5959 lgp->args.ctx->state)) {
5960 rpc_exit(task, NFS4_OK);
5961 return;
5962 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005963 rpc_call_start(task);
5964}
5965
5966static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5967{
5968 struct nfs4_layoutget *lgp = calldata;
5969 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5970
5971 dprintk("--> %s\n", __func__);
5972
5973 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5974 return;
5975
5976 switch (task->tk_status) {
5977 case 0:
5978 break;
5979 case -NFS4ERR_LAYOUTTRYLATER:
5980 case -NFS4ERR_RECALLCONFLICT:
5981 task->tk_status = -NFS4ERR_DELAY;
5982 /* Fall through */
5983 default:
5984 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5985 rpc_restart_call_prepare(task);
5986 return;
5987 }
5988 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005989 dprintk("<-- %s\n", __func__);
5990}
5991
Idan Kedarc2bf3222012-08-02 11:47:10 +03005992static size_t max_response_pages(struct nfs_server *server)
5993{
5994 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
5995 return nfs_page_array_len(0, max_resp_sz);
5996}
5997
5998static void nfs4_free_pages(struct page **pages, size_t size)
5999{
6000 int i;
6001
6002 if (!pages)
6003 return;
6004
6005 for (i = 0; i < size; i++) {
6006 if (!pages[i])
6007 break;
6008 __free_page(pages[i]);
6009 }
6010 kfree(pages);
6011}
6012
6013static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6014{
6015 struct page **pages;
6016 int i;
6017
6018 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6019 if (!pages) {
6020 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6021 return NULL;
6022 }
6023
6024 for (i = 0; i < size; i++) {
6025 pages[i] = alloc_page(gfp_flags);
6026 if (!pages[i]) {
6027 dprintk("%s: failed to allocate page\n", __func__);
6028 nfs4_free_pages(pages, size);
6029 return NULL;
6030 }
6031 }
6032
6033 return pages;
6034}
6035
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006036static void nfs4_layoutget_release(void *calldata)
6037{
6038 struct nfs4_layoutget *lgp = calldata;
Idan Kedarc2bf3222012-08-02 11:47:10 +03006039 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6040 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006041
6042 dprintk("--> %s\n", __func__);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006043 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006044 put_nfs_open_context(lgp->args.ctx);
6045 kfree(calldata);
6046 dprintk("<-- %s\n", __func__);
6047}
6048
6049static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6050 .rpc_call_prepare = nfs4_layoutget_prepare,
6051 .rpc_call_done = nfs4_layoutget_done,
6052 .rpc_release = nfs4_layoutget_release,
6053};
6054
Idan Kedarc2bf3222012-08-02 11:47:10 +03006055int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006056{
6057 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Idan Kedarc2bf3222012-08-02 11:47:10 +03006058 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006059 struct rpc_task *task;
6060 struct rpc_message msg = {
6061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6062 .rpc_argp = &lgp->args,
6063 .rpc_resp = &lgp->res,
6064 };
6065 struct rpc_task_setup task_setup_data = {
6066 .rpc_client = server->client,
6067 .rpc_message = &msg,
6068 .callback_ops = &nfs4_layoutget_call_ops,
6069 .callback_data = lgp,
6070 .flags = RPC_TASK_ASYNC,
6071 };
6072 int status = 0;
6073
6074 dprintk("--> %s\n", __func__);
6075
Idan Kedarc2bf3222012-08-02 11:47:10 +03006076 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6077 if (!lgp->args.layout.pages) {
6078 nfs4_layoutget_release(lgp);
6079 return -ENOMEM;
6080 }
6081 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6082
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006083 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006084 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006085 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006086 task = rpc_run_task(&task_setup_data);
6087 if (IS_ERR(task))
6088 return PTR_ERR(task);
6089 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006090 if (status == 0)
6091 status = task->tk_status;
6092 if (status == 0)
6093 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006094 rpc_put_task(task);
6095 dprintk("<-- %s status=%d\n", __func__, status);
6096 return status;
6097}
6098
Benny Halevycbe82602011-05-22 19:52:37 +03006099static void
6100nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6101{
6102 struct nfs4_layoutreturn *lrp = calldata;
6103
6104 dprintk("--> %s\n", __func__);
6105 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006106 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006107 return;
6108 rpc_call_start(task);
6109}
6110
6111static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6112{
6113 struct nfs4_layoutreturn *lrp = calldata;
6114 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006115 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006116
6117 dprintk("--> %s\n", __func__);
6118
6119 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6120 return;
6121
6122 server = NFS_SERVER(lrp->args.inode);
6123 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006124 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006125 return;
6126 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006127 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebustf401b102012-08-08 16:03:13 -04006128 if (task->tk_status == 0 && lrp->res.lrs_present)
6129 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006130 lo->plh_block_lgets--;
6131 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006132 dprintk("<-- %s\n", __func__);
6133}
6134
6135static void nfs4_layoutreturn_release(void *calldata)
6136{
6137 struct nfs4_layoutreturn *lrp = calldata;
6138
6139 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006140 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006141 kfree(calldata);
6142 dprintk("<-- %s\n", __func__);
6143}
6144
6145static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6146 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6147 .rpc_call_done = nfs4_layoutreturn_done,
6148 .rpc_release = nfs4_layoutreturn_release,
6149};
6150
6151int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6152{
6153 struct rpc_task *task;
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6156 .rpc_argp = &lrp->args,
6157 .rpc_resp = &lrp->res,
6158 };
6159 struct rpc_task_setup task_setup_data = {
6160 .rpc_client = lrp->clp->cl_rpcclient,
6161 .rpc_message = &msg,
6162 .callback_ops = &nfs4_layoutreturn_call_ops,
6163 .callback_data = lrp,
6164 };
6165 int status;
6166
6167 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006168 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006169 task = rpc_run_task(&task_setup_data);
6170 if (IS_ERR(task))
6171 return PTR_ERR(task);
6172 status = task->tk_status;
6173 dprintk("<-- %s status=%d\n", __func__, status);
6174 rpc_put_task(task);
6175 return status;
6176}
6177
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006178/*
6179 * Retrieve the list of Data Server devices from the MDS.
6180 */
6181static int _nfs4_getdevicelist(struct nfs_server *server,
6182 const struct nfs_fh *fh,
6183 struct pnfs_devicelist *devlist)
6184{
6185 struct nfs4_getdevicelist_args args = {
6186 .fh = fh,
6187 .layoutclass = server->pnfs_curr_ld->id,
6188 };
6189 struct nfs4_getdevicelist_res res = {
6190 .devlist = devlist,
6191 };
6192 struct rpc_message msg = {
6193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6194 .rpc_argp = &args,
6195 .rpc_resp = &res,
6196 };
6197 int status;
6198
6199 dprintk("--> %s\n", __func__);
6200 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6201 &res.seq_res, 0);
6202 dprintk("<-- %s status=%d\n", __func__, status);
6203 return status;
6204}
6205
6206int nfs4_proc_getdevicelist(struct nfs_server *server,
6207 const struct nfs_fh *fh,
6208 struct pnfs_devicelist *devlist)
6209{
6210 struct nfs4_exception exception = { };
6211 int err;
6212
6213 do {
6214 err = nfs4_handle_exception(server,
6215 _nfs4_getdevicelist(server, fh, devlist),
6216 &exception);
6217 } while (exception.retry);
6218
6219 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6220 err, devlist->num_devs);
6221
6222 return err;
6223}
6224EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6225
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006226static int
6227_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6228{
6229 struct nfs4_getdeviceinfo_args args = {
6230 .pdev = pdev,
6231 };
6232 struct nfs4_getdeviceinfo_res res = {
6233 .pdev = pdev,
6234 };
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6237 .rpc_argp = &args,
6238 .rpc_resp = &res,
6239 };
6240 int status;
6241
6242 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006243 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006244 dprintk("<-- %s status=%d\n", __func__, status);
6245
6246 return status;
6247}
6248
6249int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6250{
6251 struct nfs4_exception exception = { };
6252 int err;
6253
6254 do {
6255 err = nfs4_handle_exception(server,
6256 _nfs4_proc_getdeviceinfo(server, pdev),
6257 &exception);
6258 } while (exception.retry);
6259 return err;
6260}
6261EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6262
Andy Adamson863a3c62011-03-23 13:27:54 +00006263static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6264{
6265 struct nfs4_layoutcommit_data *data = calldata;
6266 struct nfs_server *server = NFS_SERVER(data->args.inode);
6267
6268 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006269 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006270 return;
6271 rpc_call_start(task);
6272}
6273
6274static void
6275nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6276{
6277 struct nfs4_layoutcommit_data *data = calldata;
6278 struct nfs_server *server = NFS_SERVER(data->args.inode);
6279
6280 if (!nfs4_sequence_done(task, &data->res.seq_res))
6281 return;
6282
6283 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006284 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6285 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6286 case -NFS4ERR_BADLAYOUT: /* no layout */
6287 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006288 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006289 break;
6290 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006291 nfs_post_op_update_inode_force_wcc(data->args.inode,
6292 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006293 break;
6294 default:
6295 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6296 rpc_restart_call_prepare(task);
6297 return;
6298 }
6299 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006300}
6301
6302static void nfs4_layoutcommit_release(void *calldata)
6303{
6304 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006305 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006306 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006307
Andy Adamsondb29c082011-07-30 20:52:38 -04006308 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006309 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006310 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6311 list_del_init(&lseg->pls_lc_list);
6312 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6313 &lseg->pls_flags))
6314 put_lseg(lseg);
6315 }
Peng Tao92407e72011-10-23 20:21:17 -07006316
6317 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6318 smp_mb__after_clear_bit();
6319 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6320
Andy Adamson863a3c62011-03-23 13:27:54 +00006321 put_rpccred(data->cred);
6322 kfree(data);
6323}
6324
6325static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6326 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6327 .rpc_call_done = nfs4_layoutcommit_done,
6328 .rpc_release = nfs4_layoutcommit_release,
6329};
6330
6331int
Andy Adamsonef311532011-03-12 02:58:10 -05006332nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006333{
6334 struct rpc_message msg = {
6335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6336 .rpc_argp = &data->args,
6337 .rpc_resp = &data->res,
6338 .rpc_cred = data->cred,
6339 };
6340 struct rpc_task_setup task_setup_data = {
6341 .task = &data->task,
6342 .rpc_client = NFS_CLIENT(data->args.inode),
6343 .rpc_message = &msg,
6344 .callback_ops = &nfs4_layoutcommit_ops,
6345 .callback_data = data,
6346 .flags = RPC_TASK_ASYNC,
6347 };
6348 struct rpc_task *task;
6349 int status = 0;
6350
6351 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6352 "lbw: %llu inode %lu\n",
6353 data->task.tk_pid, sync,
6354 data->args.lastbytewritten,
6355 data->args.inode->i_ino);
6356
Trond Myklebust9d12b212012-01-17 22:04:25 -05006357 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006358 task = rpc_run_task(&task_setup_data);
6359 if (IS_ERR(task))
6360 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006361 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006362 goto out;
6363 status = nfs4_wait_for_completion_rpc_task(task);
6364 if (status != 0)
6365 goto out;
6366 status = task->tk_status;
6367out:
6368 dprintk("%s: status %d\n", __func__, status);
6369 rpc_put_task(task);
6370 return status;
6371}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006372
6373static int
6374_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6375 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6376{
6377 struct nfs41_secinfo_no_name_args args = {
6378 .style = SECINFO_STYLE_CURRENT_FH,
6379 };
6380 struct nfs4_secinfo_res res = {
6381 .flavors = flavors,
6382 };
6383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6385 .rpc_argp = &args,
6386 .rpc_resp = &res,
6387 };
6388 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6389}
6390
6391static int
6392nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6393 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6394{
6395 struct nfs4_exception exception = { };
6396 int err;
6397 do {
6398 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6399 switch (err) {
6400 case 0:
6401 case -NFS4ERR_WRONGSEC:
6402 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006403 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006404 default:
6405 err = nfs4_handle_exception(server, err, &exception);
6406 }
6407 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006408out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006409 return err;
6410}
6411
6412static int
6413nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6414 struct nfs_fsinfo *info)
6415{
6416 int err;
6417 struct page *page;
6418 rpc_authflavor_t flavor;
6419 struct nfs4_secinfo_flavors *flavors;
6420
6421 page = alloc_page(GFP_KERNEL);
6422 if (!page) {
6423 err = -ENOMEM;
6424 goto out;
6425 }
6426
6427 flavors = page_address(page);
6428 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6429
6430 /*
6431 * Fall back on "guess and check" method if
6432 * the server doesn't support SECINFO_NO_NAME
6433 */
6434 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6435 err = nfs4_find_root_sec(server, fhandle, info);
6436 goto out_freepage;
6437 }
6438 if (err)
6439 goto out_freepage;
6440
6441 flavor = nfs_find_best_sec(flavors);
6442 if (err == 0)
6443 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6444
6445out_freepage:
6446 put_page(page);
6447 if (err == -EACCES)
6448 return -EPERM;
6449out:
6450 return err;
6451}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006452
6453static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006454{
6455 int status;
6456 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006457 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006458 };
6459 struct nfs41_test_stateid_res res;
6460 struct rpc_message msg = {
6461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6462 .rpc_argp = &args,
6463 .rpc_resp = &res,
6464 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006465
Trond Myklebust9d12b212012-01-17 22:04:25 -05006466 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006467 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6468
6469 if (status == NFS_OK)
6470 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006471 return status;
6472}
6473
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006474static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006475{
6476 struct nfs4_exception exception = { };
6477 int err;
6478 do {
6479 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006480 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006481 &exception);
6482 } while (exception.retry);
6483 return err;
6484}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006485
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006486static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006487{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006488 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006489 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006490 };
6491 struct nfs41_free_stateid_res res;
6492 struct rpc_message msg = {
6493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6494 .rpc_argp = &args,
6495 .rpc_resp = &res,
6496 };
6497
Trond Myklebust9d12b212012-01-17 22:04:25 -05006498 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6499 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006500}
6501
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006502static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006503{
6504 struct nfs4_exception exception = { };
6505 int err;
6506 do {
6507 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006508 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006509 &exception);
6510 } while (exception.retry);
6511 return err;
6512}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006513
6514static bool nfs41_match_stateid(const nfs4_stateid *s1,
6515 const nfs4_stateid *s2)
6516{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006517 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006518 return false;
6519
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006520 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006521 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006522 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006523 return true;
6524
6525 return false;
6526}
6527
Andy Adamson557134a2009-04-01 09:21:53 -04006528#endif /* CONFIG_NFS_V4_1 */
6529
Trond Myklebust36281ca2012-03-04 18:13:56 -05006530static bool nfs4_match_stateid(const nfs4_stateid *s1,
6531 const nfs4_stateid *s2)
6532{
Trond Myklebustf597c532012-03-04 18:13:56 -05006533 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006534}
6535
6536
Trond Myklebust17280172012-03-11 13:11:00 -04006537static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006538 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006539 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 .recover_open = nfs4_open_reclaim,
6541 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006542 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006543 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544};
6545
Andy Adamson591d71c2009-04-01 09:22:47 -04006546#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006547static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006548 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6549 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6550 .recover_open = nfs4_open_reclaim,
6551 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006552 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006553 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006554 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006555};
6556#endif /* CONFIG_NFS_V4_1 */
6557
Trond Myklebust17280172012-03-11 13:11:00 -04006558static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006559 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006560 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 .recover_open = nfs4_open_expired,
6562 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006563 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006564 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565};
6566
Andy Adamson591d71c2009-04-01 09:22:47 -04006567#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006568static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006569 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6570 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006571 .recover_open = nfs41_open_expired,
6572 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006573 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006574 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006575};
6576#endif /* CONFIG_NFS_V4_1 */
6577
Trond Myklebust17280172012-03-11 13:11:00 -04006578static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006579 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006580 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006581 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006582};
6583
6584#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006585static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006586 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006587 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006588 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006589};
6590#endif
6591
Trond Myklebust97dc1352010-06-16 09:52:26 -04006592static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6593 .minor_version = 0,
6594 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006595 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006596 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006597 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6598 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6599 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006600};
6601
6602#if defined(CONFIG_NFS_V4_1)
6603static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6604 .minor_version = 1,
6605 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006606 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006607 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006608 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6609 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6610 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006611};
6612#endif
6613
Trond Myklebust97dc1352010-06-16 09:52:26 -04006614const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6615 [0] = &nfs_v4_0_minor_ops,
6616#if defined(CONFIG_NFS_V4_1)
6617 [1] = &nfs_v4_1_minor_ops,
6618#endif
6619};
6620
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006621static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006622 .permission = nfs_permission,
6623 .getattr = nfs_getattr,
6624 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006625 .getxattr = generic_getxattr,
6626 .setxattr = generic_setxattr,
6627 .listxattr = generic_listxattr,
6628 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006629};
6630
David Howells509de812006-08-22 20:06:11 -04006631const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 .version = 4, /* protocol version */
6633 .dentry_ops = &nfs4_dentry_operations,
6634 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006635 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006636 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 .getroot = nfs4_proc_get_root,
6638 .getattr = nfs4_proc_getattr,
6639 .setattr = nfs4_proc_setattr,
6640 .lookup = nfs4_proc_lookup,
6641 .access = nfs4_proc_access,
6642 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 .create = nfs4_proc_create,
6644 .remove = nfs4_proc_remove,
6645 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006646 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 .unlink_done = nfs4_proc_unlink_done,
6648 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006649 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006650 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006651 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 .link = nfs4_proc_link,
6653 .symlink = nfs4_proc_symlink,
6654 .mkdir = nfs4_proc_mkdir,
6655 .rmdir = nfs4_proc_remove,
6656 .readdir = nfs4_proc_readdir,
6657 .mknod = nfs4_proc_mknod,
6658 .statfs = nfs4_proc_statfs,
6659 .fsinfo = nfs4_proc_fsinfo,
6660 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006661 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 .decode_dirent = nfs4_decode_dirent,
6663 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006664 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006665 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006667 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006668 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006670 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006672 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006673 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006674 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006675 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006676 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677};
6678
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006679static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6680 .prefix = XATTR_NAME_NFSV4_ACL,
6681 .list = nfs4_xattr_list_nfs4_acl,
6682 .get = nfs4_xattr_get_nfs4_acl,
6683 .set = nfs4_xattr_set_nfs4_acl,
6684};
6685
6686const struct xattr_handler *nfs4_xattr_handlers[] = {
6687 &nfs4_xattr_nfs4_acl_handler,
6688 NULL
6689};
6690
Trond Myklebustef159e92012-02-06 19:50:40 -05006691module_param(max_session_slots, ushort, 0644);
6692MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6693 "requests the client will negotiate");
6694
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695/*
6696 * Local variables:
6697 * c-basic-offset: 8
6698 * End:
6699 */