blob: 9c247fa7915a40dbdb71f5f95b28ea9df37d3394 [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 Myklebust52567b02009-10-23 14:46:42 -0400104 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700106 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400107 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * This is our standard bitmap for GETATTR requests.
114 */
115const u32 nfs4_fattr_bitmap[2] = {
116 FATTR4_WORD0_TYPE
117 | FATTR4_WORD0_CHANGE
118 | FATTR4_WORD0_SIZE
119 | FATTR4_WORD0_FSID
120 | FATTR4_WORD0_FILEID,
121 FATTR4_WORD1_MODE
122 | FATTR4_WORD1_NUMLINKS
123 | FATTR4_WORD1_OWNER
124 | FATTR4_WORD1_OWNER_GROUP
125 | FATTR4_WORD1_RAWDEV
126 | FATTR4_WORD1_SPACE_USED
127 | FATTR4_WORD1_TIME_ACCESS
128 | FATTR4_WORD1_TIME_METADATA
129 | FATTR4_WORD1_TIME_MODIFY
130};
131
132const u32 nfs4_statfs_bitmap[2] = {
133 FATTR4_WORD0_FILES_AVAIL
134 | FATTR4_WORD0_FILES_FREE
135 | FATTR4_WORD0_FILES_TOTAL,
136 FATTR4_WORD1_SPACE_AVAIL
137 | FATTR4_WORD1_SPACE_FREE
138 | FATTR4_WORD1_SPACE_TOTAL
139};
140
Trond Myklebust4ce79712005-06-22 17:16:21 +0000141const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 FATTR4_WORD0_MAXLINK
143 | FATTR4_WORD0_MAXNAME,
144 0
145};
146
Fred Isamandae100c2011-07-30 20:52:37 -0400147const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 | FATTR4_WORD0_MAXREAD
149 | FATTR4_WORD0_MAXWRITE
150 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700151 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400152 | FATTR4_WORD1_FS_LAYOUT_TYPES,
153 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Manoj Naik830b8e32006-06-09 09:34:25 -0400156const u32 nfs4_fs_locations_bitmap[2] = {
157 FATTR4_WORD0_TYPE
158 | FATTR4_WORD0_CHANGE
159 | FATTR4_WORD0_SIZE
160 | FATTR4_WORD0_FSID
161 | FATTR4_WORD0_FILEID
162 | FATTR4_WORD0_FS_LOCATIONS,
163 FATTR4_WORD1_MODE
164 | FATTR4_WORD1_NUMLINKS
165 | FATTR4_WORD1_OWNER
166 | FATTR4_WORD1_OWNER_GROUP
167 | FATTR4_WORD1_RAWDEV
168 | FATTR4_WORD1_SPACE_USED
169 | FATTR4_WORD1_TIME_ACCESS
170 | FATTR4_WORD1_TIME_METADATA
171 | FATTR4_WORD1_TIME_MODIFY
172 | FATTR4_WORD1_MOUNTED_ON_FILEID
173};
174
Al Virobc4785c2006-10-19 23:28:51 -0700175static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct nfs4_readdir_arg *readdir)
177{
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 BUG_ON(readdir->count < 80);
181 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000182 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
184 return;
185 }
186
187 readdir->cookie = 0;
188 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
189 if (cookie == 2)
190 return;
191
192 /*
193 * NFSv4 servers do not return entries for '.' and '..'
194 * Therefore, we fake these entries here. We let '.'
195 * have cookie 0 and '..' have cookie 1. Note that
196 * when talking to the server, we always send cookie 0
197 * instead of 1 or 2.
198 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700199 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (cookie == 0) {
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_one; /* cookie, second word */
205 *p++ = xdr_one; /* entry len */
206 memcpy(p, ".\0\0\0", 4); /* entry */
207 p++;
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 *p++ = xdr_one; /* next */
215 *p++ = xdr_zero; /* cookie, first word */
216 *p++ = xdr_two; /* cookie, second word */
217 *p++ = xdr_two; /* entry len */
218 memcpy(p, "..\0\0", 4); /* entry */
219 p++;
220 *p++ = xdr_one; /* bitmap length */
221 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
222 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400223 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 readdir->pgbase = (char *)p - (char *)start;
226 readdir->count -= readdir->pgbase;
227 kunmap_atomic(start, KM_USER0);
228}
229
Trond Myklebust65de8722008-12-23 15:21:44 -0500230static int nfs4_wait_clnt_recover(struct nfs_client *clp)
231{
232 int res;
233
234 might_sleep();
235
Trond Myklebuste005e802008-12-23 15:21:48 -0500236 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400237 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500238 return res;
239}
240
241static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
242{
243 int res = 0;
244
245 might_sleep();
246
247 if (*timeout <= 0)
248 *timeout = NFS4_POLL_RETRY_MIN;
249 if (*timeout > NFS4_POLL_RETRY_MAX)
250 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100251 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500252 if (fatal_signal_pending(current))
253 res = -ERESTARTSYS;
254 *timeout <<= 1;
255 return res;
256}
257
258/* This is the error handling routine for processes that are allowed
259 * to sleep.
260 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800261static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500262{
263 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500264 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500265 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500266 int ret = errorcode;
267
268 exception->retry = 0;
269 switch(errorcode) {
270 case 0:
271 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500272 case -NFS4ERR_OPENMODE:
273 if (nfs_have_delegation(inode, FMODE_READ)) {
274 nfs_inode_return_delegation(inode);
275 exception->retry = 1;
276 return 0;
277 }
278 if (state == NULL)
279 break;
280 nfs4_schedule_stateid_recovery(server, state);
281 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500282 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500283 case -NFS4ERR_ADMIN_REVOKED:
284 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500285 if (state != NULL)
286 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500289 nfs4_schedule_stateid_recovery(server, state);
290 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400291 case -NFS4ERR_EXPIRED:
292 if (state != NULL)
293 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500295 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500296 nfs4_schedule_lease_recovery(clp);
297 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500298#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400299 case -NFS4ERR_BADSESSION:
300 case -NFS4ERR_BADSLOT:
301 case -NFS4ERR_BAD_HIGH_SLOT:
302 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
303 case -NFS4ERR_DEADSESSION:
304 case -NFS4ERR_SEQ_FALSE_RETRY:
305 case -NFS4ERR_SEQ_MISORDERED:
306 dprintk("%s ERROR: %d Reset session\n", __func__,
307 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500308 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400309 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500310 break;
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;
406}
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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Myklebust48c22eb2012-01-17 22:04:25 -0500842 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400843 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844 p->o_arg.server = server;
845 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500846 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500848 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500849 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400850
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 p->o_arg.u.attrs = &p->attrs;
852 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500853
854 verf[0] = jiffies;
855 verf[1] = current->pid;
856 memcpy(p->o_arg.u.verifier.data, verf,
857 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100859 p->c_arg.fh = &p->o_res.fh;
860 p->c_arg.stateid = &p->o_res.stateid;
861 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400862 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400863 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864 return p;
865err_free:
866 kfree(p);
867err:
868 dput(parent);
869 return NULL;
870}
871
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100873{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874 struct nfs4_opendata *p = container_of(kref,
875 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400876 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400877
878 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 if (p->state != NULL)
880 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400881 nfs4_put_state_owner(p->owner);
882 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 dput(p->dentry);
884 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500885 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400886 kfree(p);
887}
888
889static void nfs4_opendata_put(struct nfs4_opendata *p)
890{
891 if (p != NULL)
892 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893}
894
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 int ret;
898
Trond Myklebust06f814a2006-01-03 09:55:07 +0100899 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100900 return ret;
901}
902
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904{
905 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906
Trond Myklebust536e43d2012-01-17 22:04:26 -0500907 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 goto out;
909 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500911 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400913 break;
914 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500915 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400917 break;
918 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500919 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400923 return ret;
924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400927{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500928 if (delegation == NULL)
929 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500932 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500934 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400935 return 1;
936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100939{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100941 case FMODE_WRITE:
942 state->n_wronly++;
943 break;
944 case FMODE_READ:
945 state->n_rdonly++;
946 break;
947 case FMODE_READ|FMODE_WRITE:
948 state->n_rdwr++;
949 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400954{
955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500956 nfs4_stateid_copy(&state->stateid, stateid);
957 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400959 case FMODE_READ:
960 set_bit(NFS_O_RDONLY_STATE, &state->flags);
961 break;
962 case FMODE_WRITE:
963 set_bit(NFS_O_WRONLY_STATE, &state->flags);
964 break;
965 case FMODE_READ|FMODE_WRITE:
966 set_bit(NFS_O_RDWR_STATE, &state->flags);
967 }
968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400971{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400974 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static 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 -0700978{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400979 /*
980 * Protect the call to nfs4_state_set_mode_locked and
981 * serialise the stateid update
982 */
983 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500985 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400986 set_bit(NFS_DELEGATED_STATE, &state->flags);
987 }
988 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400990 write_sequnlock(&state->seqlock);
991 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700993 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static 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 -0500997{
998 struct nfs_inode *nfsi = NFS_I(state->inode);
999 struct nfs_delegation *deleg_cur;
1000 int ret = 0;
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001003
1004 rcu_read_lock();
1005 deleg_cur = rcu_dereference(nfsi->delegation);
1006 if (deleg_cur == NULL)
1007 goto no_delegation;
1008
1009 spin_lock(&deleg_cur->lock);
1010 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001012 goto no_delegation_unlock;
1013
1014 if (delegation == NULL)
1015 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001016 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001017 goto no_delegation_unlock;
1018
Trond Myklebustb7391f42008-12-23 15:21:52 -05001019 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001021 ret = 1;
1022no_delegation_unlock:
1023 spin_unlock(&deleg_cur->lock);
1024no_delegation:
1025 rcu_read_unlock();
1026
1027 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001029 ret = 1;
1030 }
1031
1032 return ret;
1033}
1034
1035
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037{
1038 struct nfs_delegation *delegation;
1039
1040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 rcu_read_unlock();
1044 return;
1045 }
1046 rcu_read_unlock();
1047 nfs_inode_return_delegation(inode);
1048}
1049
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051{
1052 struct nfs4_state *state = opendata->state;
1053 struct nfs_inode *nfsi = NFS_I(state->inode);
1054 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001055 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 nfs4_stateid stateid;
1058 int ret = -EAGAIN;
1059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001063 if (can_open_cached(state, fmode, open_mode)) {
1064 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 goto out_return_state;
1067 }
1068 spin_unlock(&state->owner->so_lock);
1069 }
Trond Myklebust34310432008-12-23 15:21:38 -05001070 rcu_read_lock();
1071 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001072 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001073 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001075 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001077 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001079 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001080 if (ret != 0)
1081 goto out;
1082 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001083
1084 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001086 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001088out:
1089 return ERR_PTR(ret);
1090out_return_state:
1091 atomic_inc(&state->count);
1092 return state;
1093}
1094
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1096{
1097 struct inode *inode;
1098 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001099 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001100 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001103 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104 goto out;
1105 }
1106
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001112 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113 goto err;
1114 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 state = nfs4_get_open_state(inode, data->owner);
1116 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001119 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001120 int delegation_flags = 0;
1121
Trond Myklebust003707c2007-07-05 18:07:55 -04001122 rcu_read_lock();
1123 delegation = rcu_dereference(NFS_I(inode)->delegation);
1124 if (delegation)
1125 delegation_flags = delegation->flags;
1126 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001131 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1135 &data->o_res);
1136 else
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1139 &data->o_res);
1140 }
Trond Myklebust34310432008-12-23 15:21:38 -05001141
1142 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001147err_put_inode:
1148 iput(inode);
1149err:
1150 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001151}
1152
Trond Myklebust864472e2006-01-03 09:55:15 +01001153static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1154{
1155 struct nfs_inode *nfsi = NFS_I(state->inode);
1156 struct nfs_open_context *ctx;
1157
1158 spin_lock(&state->inode->i_lock);
1159 list_for_each_entry(ctx, &nfsi->open_files, list) {
1160 if (ctx->state != state)
1161 continue;
1162 get_nfs_open_context(ctx);
1163 spin_unlock(&state->inode->i_lock);
1164 return ctx;
1165 }
1166 spin_unlock(&state->inode->i_lock);
1167 return ERR_PTR(-ENOENT);
1168}
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1171{
1172 struct nfs4_opendata *opendata;
1173
Al Viro3d4ff432011-06-22 18:40:12 -04001174 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001175 if (opendata == NULL)
1176 return ERR_PTR(-ENOMEM);
1177 opendata->state = state;
1178 atomic_inc(&state->count);
1179 return opendata;
1180}
1181
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001182static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001183{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 int ret;
1186
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001187 opendata->o_arg.open_flags = 0;
1188 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001192 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001196 if (IS_ERR(newstate))
1197 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001198 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return 0;
1201}
1202
1203static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1204{
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 int ret;
1207
1208 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 smp_rmb();
1211 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 if (ret != 0)
1215 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001216 if (newstate != state)
1217 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 }
1219 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 if (ret != 0)
1223 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001224 if (newstate != state)
1225 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 }
1227 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001230 if (ret != 0)
1231 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001232 if (newstate != state)
1233 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 /*
1236 * We may have performed cached opens for all three recoveries.
1237 * Check if we need to update the current stateid.
1238 */
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001241 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001244 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001246 return 0;
1247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/*
1250 * OPEN_RECLAIM:
1251 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001253static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001255 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int status;
1259
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001265 rcu_read_lock();
1266 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001267 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001268 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001269 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 opendata->o_arg.u.delegation_type = delegation_type;
1271 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return status;
1274}
1275
Trond Myklebust539cd032007-06-05 11:46:42 -04001276static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct nfs_server *server = NFS_SERVER(state->inode);
1279 struct nfs4_exception exception = { };
1280 int err;
1281 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001282 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001283 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001284 break;
1285 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 } while (exception.retry);
1287 return err;
1288}
1289
Trond Myklebust864472e2006-01-03 09:55:15 +01001290static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1291{
1292 struct nfs_open_context *ctx;
1293 int ret;
1294
1295 ctx = nfs4_state_find_open_context(state);
1296 if (IS_ERR(ctx))
1297 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001298 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 put_nfs_open_context(ctx);
1300 return ret;
1301}
1302
Trond Myklebust13437e12007-07-06 15:10:43 -04001303static 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 -07001304{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001306 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001308 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Trond Myklebust13437e12007-07-06 15:10:43 -04001318int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001321 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int err;
1323 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001324 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 switch (err) {
1326 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327 case -ENOENT:
1328 case -ESTALE:
1329 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001330 case -NFS4ERR_BADSESSION:
1331 case -NFS4ERR_BADSLOT:
1332 case -NFS4ERR_BAD_HIGH_SLOT:
1333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001335 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 case -NFS4ERR_STALE_CLIENTID:
1338 case -NFS4ERR_STALE_STATEID:
1339 case -NFS4ERR_EXPIRED:
1340 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001341 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001342 goto out;
1343 case -ERESTARTSYS:
1344 /*
1345 * The show must go on: exit, but mark the
1346 * stateid as needing recovery.
1347 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001348 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001349 case -NFS4ERR_ADMIN_REVOKED:
1350 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001351 nfs_inode_find_state_and_recover(state->inode,
1352 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001353 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001354 case -EKEYEXPIRED:
1355 /*
1356 * User RPCSEC_GSS context has expired.
1357 * We cannot recover this stateid now, so
1358 * skip it and allow recovery thread to
1359 * proceed.
1360 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001361 case -ENOMEM:
1362 err = 0;
1363 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 err = nfs4_handle_exception(server, err, &exception);
1366 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return err;
1369}
1370
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001371static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1372{
1373 struct nfs4_opendata *data = calldata;
1374
1375 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001376 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001377 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001380 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382}
1383
1384static void nfs4_open_confirm_release(void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387 struct nfs4_state *state = NULL;
1388
1389 /* If this request hasn't been cancelled, do nothing */
1390 if (data->cancelled == 0)
1391 goto out_free;
1392 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001396 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001397 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400}
1401
1402static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403 .rpc_call_done = nfs4_open_confirm_done,
1404 .rpc_release = nfs4_open_confirm_release,
1405};
1406
1407/*
1408 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1409 */
1410static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1411{
1412 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1413 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1416 .rpc_argp = &data->c_arg,
1417 .rpc_resp = &data->c_res,
1418 .rpc_cred = data->owner->so_cred,
1419 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001420 struct rpc_task_setup task_setup_data = {
1421 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001422 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001423 .callback_ops = &nfs4_open_confirm_ops,
1424 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001425 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 .flags = RPC_TASK_ASYNC,
1427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 int status;
1429
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001431 data->rpc_done = 0;
1432 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001433 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001434 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001435 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001436 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001437 status = nfs4_wait_for_completion_rpc_task(task);
1438 if (status != 0) {
1439 data->cancelled = 1;
1440 smp_wmb();
1441 } else
1442 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001443 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return status;
1445}
1446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1453 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001454 /*
1455 * Check if we still need to send an OPEN call, or if we can use
1456 * a delegation instead.
1457 */
1458 if (data->state != NULL) {
1459 struct nfs_delegation *delegation;
1460
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001461 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001462 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001463 rcu_read_lock();
1464 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001465 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1466 can_open_delegated(delegation, data->o_arg.fmode))
1467 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001468 rcu_read_unlock();
1469 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001471 data->o_arg.id = sp->so_seqid.owner_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 Myklebust9d12b212012-01-17 22:04:25 -05001480 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001481 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001482 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001483 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001484unlock_no_action:
1485 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001486out_no_action:
1487 task->tk_action = NULL;
1488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001491static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1492{
1493 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1494 nfs4_open_prepare(task, calldata);
1495}
1496
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497static void nfs4_open_done(struct rpc_task *task, void *calldata)
1498{
1499 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001502
Trond Myklebust14516c32010-07-31 14:29:06 -04001503 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1504 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001505
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506 if (task->tk_status == 0) {
1507 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 case S_IFREG:
1509 break;
1510 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 break;
1513 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 break;
1516 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001518 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001519 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001520 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1521 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001523 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001525
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001526static void nfs4_open_release(void *calldata)
1527{
1528 struct nfs4_opendata *data = calldata;
1529 struct nfs4_state *state = NULL;
1530
1531 /* If this request hasn't been cancelled, do nothing */
1532 if (data->cancelled == 0)
1533 goto out_free;
1534 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001535 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 goto out_free;
1537 /* In case we need an open_confirm, no cleanup! */
1538 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1539 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001541 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001542 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001544 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545}
1546
1547static const struct rpc_call_ops nfs4_open_ops = {
1548 .rpc_call_prepare = nfs4_open_prepare,
1549 .rpc_call_done = nfs4_open_done,
1550 .rpc_release = nfs4_open_release,
1551};
1552
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001553static const struct rpc_call_ops nfs4_recover_open_ops = {
1554 .rpc_call_prepare = nfs4_recover_open_prepare,
1555 .rpc_call_done = nfs4_open_done,
1556 .rpc_release = nfs4_open_release,
1557};
1558
1559static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560{
1561 struct inode *dir = data->dir->d_inode;
1562 struct nfs_server *server = NFS_SERVER(dir);
1563 struct nfs_openargs *o_arg = &data->o_arg;
1564 struct nfs_openres *o_res = &data->o_res;
1565 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001566 struct rpc_message msg = {
1567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1568 .rpc_argp = o_arg,
1569 .rpc_resp = o_res,
1570 .rpc_cred = data->owner->so_cred,
1571 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 struct rpc_task_setup task_setup_data = {
1573 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001574 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 .callback_ops = &nfs4_open_ops,
1576 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001577 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001578 .flags = RPC_TASK_ASYNC,
1579 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580 int status;
1581
Trond Myklebust9d12b212012-01-17 22:04:25 -05001582 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001583 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001584 data->rpc_done = 0;
1585 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001586 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001587 if (isrecover)
1588 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001589 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001590 if (IS_ERR(task))
1591 return PTR_ERR(task);
1592 status = nfs4_wait_for_completion_rpc_task(task);
1593 if (status != 0) {
1594 data->cancelled = 1;
1595 smp_wmb();
1596 } else
1597 status = data->rpc_status;
1598 rpc_put_task(task);
1599
1600 return status;
1601}
1602
1603static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1604{
1605 struct inode *dir = data->dir->d_inode;
1606 struct nfs_openres *o_res = &data->o_res;
1607 int status;
1608
1609 status = nfs4_run_open_task(data, 1);
1610 if (status != 0 || !data->rpc_done)
1611 return status;
1612
Trond Myklebust6926afd2012-01-07 13:22:46 -05001613 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1614
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001615 nfs_refresh_inode(dir, o_res->dir_attr);
1616
1617 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618 status = _nfs4_proc_open_confirm(data);
1619 if (status != 0)
1620 return status;
1621 }
1622
1623 return status;
1624}
1625
1626/*
1627 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1628 */
1629static int _nfs4_proc_open(struct nfs4_opendata *data)
1630{
1631 struct inode *dir = data->dir->d_inode;
1632 struct nfs_server *server = NFS_SERVER(dir);
1633 struct nfs_openargs *o_arg = &data->o_arg;
1634 struct nfs_openres *o_res = &data->o_res;
1635 int status;
1636
1637 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001638 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001639 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001640 if (status != 0) {
1641 if (status == -NFS4ERR_BADNAME &&
1642 !(o_arg->open_flags & O_CREAT))
1643 return -ENOENT;
1644 return status;
1645 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001646
Trond Myklebust6926afd2012-01-07 13:22:46 -05001647 nfs_fattr_map_and_free_names(server, &data->f_attr);
1648
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001649 if (o_arg->open_flags & O_CREAT) {
1650 update_changeattr(dir, &o_res->cinfo);
1651 nfs_post_op_update_inode(dir, o_res->dir_attr);
1652 } else
1653 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001654 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1655 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001657 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001659 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001662 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Andy Adamsond83217c2011-03-01 01:34:17 +00001666static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001667{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001668 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001669 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001670
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001672 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001673 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001674 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001675 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1676 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001677 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001678 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001679 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001680 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001681 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001682}
1683
Andy Adamsond83217c2011-03-01 01:34:17 +00001684static int nfs4_recover_expired_lease(struct nfs_server *server)
1685{
1686 return nfs4_client_recover_expired_lease(server->nfs_client);
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/*
1690 * OPEN_EXPIRED:
1691 * reclaim state on the server after a network partition.
1692 * Assumes caller holds the appropriate lock
1693 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001694static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001696 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001697 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001699 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1700 if (IS_ERR(opendata))
1701 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001702 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001703 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001704 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001705 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001710{
Trond Myklebust539cd032007-06-05 11:46:42 -04001711 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001712 struct nfs4_exception exception = { };
1713 int err;
1714
1715 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001716 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001717 switch (err) {
1718 default:
1719 goto out;
1720 case -NFS4ERR_GRACE:
1721 case -NFS4ERR_DELAY:
1722 nfs4_handle_exception(server, err, &exception);
1723 err = 0;
1724 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001725 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001726out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001727 return err;
1728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001733 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Trond Myklebust864472e2006-01-03 09:55:15 +01001735 ctx = nfs4_state_find_open_context(state);
1736 if (IS_ERR(ctx))
1737 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001738 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001739 put_nfs_open_context(ctx);
1740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001743#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001744static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001745{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001746 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001747 struct nfs_server *server = NFS_SERVER(state->inode);
1748
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001749 if (state->flags & flags) {
1750 status = nfs41_test_stateid(server, stateid);
1751 if (status != NFS_OK) {
1752 nfs41_free_stateid(server, stateid);
1753 state->flags &= ~flags;
1754 }
1755 }
1756 return status;
1757}
1758
1759static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1760{
1761 int deleg_status, open_status;
1762 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1763 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1764
1765 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1766 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1767
1768 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1769 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001770 return nfs4_open_expired(sp, state);
1771}
1772#endif
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001775 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1776 * fields corresponding to attributes that were used to store the verifier.
1777 * Make sure we clobber those fields in the later setattr call
1778 */
1779static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1780{
1781 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1782 !(sattr->ia_valid & ATTR_ATIME_SET))
1783 sattr->ia_valid |= ATTR_ATIME;
1784
1785 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1786 !(sattr->ia_valid & ATTR_MTIME_SET))
1787 sattr->ia_valid |= ATTR_MTIME;
1788}
1789
1790/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001791 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001793static 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 -07001794{
1795 struct nfs4_state_owner *sp;
1796 struct nfs4_state *state = NULL;
1797 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001798 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001799 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001803 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1804 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1806 goto out_err;
1807 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001808 status = nfs4_recover_expired_lease(server);
1809 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001810 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001811 if (dentry->d_inode != NULL)
1812 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001813 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001814 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001815 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001816 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Al Viro82a2c1b2011-06-22 18:30:55 -04001818 if (dentry->d_inode != NULL)
1819 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001823 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001825 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001826 status = PTR_ERR(state);
1827 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001828 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001829 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001830 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001831
1832 if (opendata->o_arg.open_flags & O_EXCL) {
1833 nfs4_exclusive_attrset(opendata, sattr);
1834
1835 nfs_fattr_init(opendata->o_res.f_attr);
1836 status = nfs4_do_setattr(state->inode, cred,
1837 opendata->o_res.f_attr, sattr,
1838 state);
1839 if (status == 0)
1840 nfs_setattr_update_inode(state->inode, sattr);
1841 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1842 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001843 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 *res = state;
1846 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001847err_opendata_put:
1848 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001849err_put_state_owner:
1850 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 *res = NULL;
1853 return status;
1854}
1855
1856
Al Viro82a2c1b2011-06-22 18:30:55 -04001857static 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 -07001858{
1859 struct nfs4_exception exception = { };
1860 struct nfs4_state *res;
1861 int status;
1862
1863 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001864 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (status == 0)
1866 break;
1867 /* NOTE: BAD_SEQID means the server and client disagree about the
1868 * book-keeping w.r.t. state-changing operations
1869 * (OPEN/CLOSE/LOCK/LOCKU...)
1870 * It is actually a sign of a bug on the client or on the server.
1871 *
1872 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001873 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 * have unhashed the old state_owner for us, and that we can
1875 * therefore safely retry using a new one. We should still warn
1876 * the user though...
1877 */
1878 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001879 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001880 " returned a bad sequence-id error!\n",
1881 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 exception.retry = 1;
1883 continue;
1884 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001885 /*
1886 * BAD_STATEID on OPEN means that the server cancelled our
1887 * state before it received the OPEN_CONFIRM.
1888 * Recover by retrying the request as per the discussion
1889 * on Page 181 of RFC3530.
1890 */
1891 if (status == -NFS4ERR_BAD_STATEID) {
1892 exception.retry = 1;
1893 continue;
1894 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001895 if (status == -EAGAIN) {
1896 /* We must have found a delegation */
1897 exception.retry = 1;
1898 continue;
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1901 status, &exception));
1902 } while (exception.retry);
1903 return res;
1904}
1905
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001906static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1907 struct nfs_fattr *fattr, struct iattr *sattr,
1908 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001910 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001912 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 .iap = sattr,
1914 .server = server,
1915 .bitmask = server->attr_bitmask,
1916 };
1917 struct nfs_setattrres res = {
1918 .fattr = fattr,
1919 .server = server,
1920 };
1921 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1923 .rpc_argp = &arg,
1924 .rpc_resp = &res,
1925 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001927 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001928 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Trond Myklebust0e574af2005-10-27 22:12:38 -04001930 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Trond Myklebust4fc87962012-03-08 17:42:01 -05001932 if (state != NULL) {
1933 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1934 current->files, current->tgid);
1935 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1936 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001937 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001938 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001939 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Bryan Schumaker7c513052011-03-24 17:12:24 +00001941 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001942 if (status == 0 && state != NULL)
1943 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001944 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001947static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1948 struct nfs_fattr *fattr, struct iattr *sattr,
1949 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001951 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001952 struct nfs4_exception exception = {
1953 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001954 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int err;
1957 do {
1958 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001959 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 &exception);
1961 } while (exception.retry);
1962 return err;
1963}
1964
1965struct nfs4_closedata {
1966 struct inode *inode;
1967 struct nfs4_state *state;
1968 struct nfs_closeargs arg;
1969 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001970 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001972 bool roc;
1973 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974};
1975
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001976static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001977{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001978 struct nfs4_closedata *calldata = data;
1979 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001980 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001981
Fred Isamanf7e89172011-01-06 11:36:32 +00001982 if (calldata->roc)
1983 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001984 nfs4_put_open_state(calldata->state);
1985 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001986 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001987 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001988 kfree(calldata);
1989}
1990
Trond Myklebust88069f72009-12-08 08:33:16 -05001991static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1992 fmode_t fmode)
1993{
1994 spin_lock(&state->owner->so_lock);
1995 if (!(fmode & FMODE_READ))
1996 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1997 if (!(fmode & FMODE_WRITE))
1998 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1999 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2000 spin_unlock(&state->owner->so_lock);
2001}
2002
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002003static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002005 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs_server *server = NFS_SERVER(calldata->inode);
2008
Chuck Levera3ca5652012-03-01 17:00:40 -05002009 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002010 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* hmm. we are done with the inode, and in the process of freeing
2013 * the state_owner. we keep this around to process errors
2014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 switch (task->tk_status) {
2016 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002017 if (calldata->roc)
2018 pnfs_roc_set_barrier(state->inode,
2019 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002020 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002022 nfs4_close_clear_stateid_flags(state,
2023 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002026 case -NFS4ERR_OLD_STATEID:
2027 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002029 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002032 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2033 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002035 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002036 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002037 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002040static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002042 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002043 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002044 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002045
Chuck Levera3ca5652012-03-01 17:00:40 -05002046 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002047 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002048 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002049
Trond Myklebust88069f72009-12-08 08:33:16 -05002050 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2051 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002052 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002053 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002054 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002055 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002056 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2057 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2058 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002059 }
2060 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002061 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2062 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2063 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002064 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002065 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002066 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002067
2068 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002069 /* Note: exit _without_ calling nfs4_close_done */
2070 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002071 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002072 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002073
Fred Isamanf7e89172011-01-06 11:36:32 +00002074 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002075 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 if (calldata->roc &&
2077 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2078 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2079 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002080 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002081 }
2082 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002083
Trond Myklebust516a6af2005-10-27 22:12:41 -04002084 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002085 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002086 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002087 &calldata->arg.seq_args,
2088 &calldata->res.seq_res,
2089 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002090 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002091 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002092out:
2093 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002096static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002097 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002098 .rpc_call_done = nfs4_close_done,
2099 .rpc_release = nfs4_free_closedata,
2100};
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/*
2103 * It is possible for data to be read/written from a mem-mapped file
2104 * after the sys_close call (which hits the vfs layer as a flush).
2105 * This means that we can't safely call nfsv4 close on a file until
2106 * the inode is cleared. This in turn means that we are not good
2107 * NFSv4 citizens - we do not indicate to the server to update the file's
2108 * share state even when we are done with one of the three share
2109 * stateid's in the inode.
2110 *
2111 * NOTE: Caller must be holding the sp->so_owner semaphore!
2112 */
Al Viro643168c2011-06-22 18:20:23 -04002113int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002115 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002117 struct nfs4_state_owner *sp = state->owner;
2118 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2121 .rpc_cred = state->owner->so_cred,
2122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002123 struct rpc_task_setup task_setup_data = {
2124 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002125 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002126 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002127 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002128 .flags = RPC_TASK_ASYNC,
2129 };
Trond Myklebust95121352005-10-18 14:20:12 -07002130 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002132 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002134 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002135 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002136 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002138 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002139 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002141 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002142 if (calldata->arg.seqid == NULL)
2143 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002144 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002145 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002146 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002147 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002148 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002149 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002150 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002151
Trond Myklebust1174dd12010-12-21 10:52:24 -05002152 msg.rpc_argp = &calldata->arg;
2153 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002154 task_setup_data.callback_data = calldata;
2155 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002156 if (IS_ERR(task))
2157 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002158 status = 0;
2159 if (wait)
2160 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002161 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002162 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002163out_free_calldata:
2164 kfree(calldata);
2165out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (roc)
2167 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002168 nfs4_put_open_state(state);
2169 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002170 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust2b484292010-09-17 10:56:51 -04002173static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002174nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 struct nfs4_state *state;
2177
Trond Myklebust565277f2007-10-15 18:17:53 -04002178 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002179 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002180 if (IS_ERR(state))
2181 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002182 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002183 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Trond Myklebust1185a552009-12-03 15:54:02 -05002186static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002187{
2188 if (ctx->state == NULL)
2189 return;
2190 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002191 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002192 else
Al Viro643168c2011-06-22 18:20:23 -04002193 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2197{
Benny Halevy43652ad2009-04-01 09:21:54 -04002198 struct nfs4_server_caps_arg args = {
2199 .fhandle = fhandle,
2200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct nfs4_server_caps_res res = {};
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002204 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 .rpc_resp = &res,
2206 };
2207 int status;
2208
Bryan Schumaker7c513052011-03-24 17:12:24 +00002209 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (status == 0) {
2211 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002212 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2213 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2214 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2215 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2216 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2218 server->caps |= NFS_CAP_ACLS;
2219 if (res.has_links != 0)
2220 server->caps |= NFS_CAP_HARDLINKS;
2221 if (res.has_symlinks != 0)
2222 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002223 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2224 server->caps |= NFS_CAP_FILEID;
2225 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2226 server->caps |= NFS_CAP_MODE;
2227 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2228 server->caps |= NFS_CAP_NLINK;
2229 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2230 server->caps |= NFS_CAP_OWNER;
2231 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2232 server->caps |= NFS_CAP_OWNER_GROUP;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2234 server->caps |= NFS_CAP_ATIME;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2236 server->caps |= NFS_CAP_CTIME;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2238 server->caps |= NFS_CAP_MTIME;
2239
Trond Myklebusta65318b2009-03-11 14:10:28 -04002240 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2241 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2242 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002244 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return status;
2248}
2249
Trond Myklebust55a97592006-06-09 09:34:19 -04002250int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct nfs4_exception exception = { };
2253 int err;
2254 do {
2255 err = nfs4_handle_exception(server,
2256 _nfs4_server_capabilities(server, fhandle),
2257 &exception);
2258 } while (exception.retry);
2259 return err;
2260}
2261
2262static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2263 struct nfs_fsinfo *info)
2264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct nfs4_lookup_root_arg args = {
2266 .bitmask = nfs4_fattr_bitmap,
2267 };
2268 struct nfs4_lookup_res res = {
2269 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002270 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .fh = fhandle,
2272 };
2273 struct rpc_message msg = {
2274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2275 .rpc_argp = &args,
2276 .rpc_resp = &res,
2277 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002278
Trond Myklebust0e574af2005-10-27 22:12:38 -04002279 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002280 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2284 struct nfs_fsinfo *info)
2285{
2286 struct nfs4_exception exception = { };
2287 int err;
2288 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002289 err = _nfs4_lookup_root(server, fhandle, info);
2290 switch (err) {
2291 case 0:
2292 case -NFS4ERR_WRONGSEC:
2293 break;
2294 default:
2295 err = nfs4_handle_exception(server, err, &exception);
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 } while (exception.retry);
2298 return err;
2299}
2300
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002301static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2303{
2304 struct rpc_auth *auth;
2305 int ret;
2306
2307 auth = rpcauth_create(flavor, server->client);
2308 if (!auth) {
2309 ret = -EIO;
2310 goto out;
2311 }
2312 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002313out:
2314 return ret;
2315}
2316
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002317static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002318 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002320 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002321 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002323 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2324 flav_array[len] = RPC_AUTH_NULL;
2325 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002326
2327 for (i = 0; i < len; i++) {
2328 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002329 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002330 continue;
2331 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002332 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002333 /*
2334 * -EACCESS could mean that the user doesn't have correct permissions
2335 * to access the mount. It could also mean that we tried to mount
2336 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2337 * existing mount programs don't handle -EACCES very well so it should
2338 * be mapped to -EPERM instead.
2339 */
2340 if (status == -EACCES)
2341 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002342 return status;
2343}
2344
2345/*
2346 * get the file handle for the "/" directory on the server
2347 */
2348static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2350{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002351 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002352 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002353 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2354 /*
2355 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2356 * by nfs4_map_errors() as this function exits.
2357 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002358 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (status == 0)
2360 status = nfs4_server_capabilities(server, fhandle);
2361 if (status == 0)
2362 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002363 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Manoj Naik6b97fd32006-06-09 09:34:29 -04002366/*
2367 * Get locations and (maybe) other attributes of a referral.
2368 * Note that we'll actually follow the referral later when
2369 * we detect fsid mismatch in inode revalidation
2370 */
Andy Adamson533eb462011-06-13 18:25:56 -04002371static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2372 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002373{
2374 int status = -ENOMEM;
2375 struct page *page = NULL;
2376 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002377
2378 page = alloc_page(GFP_KERNEL);
2379 if (page == NULL)
2380 goto out;
2381 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2382 if (locations == NULL)
2383 goto out;
2384
Trond Myklebustc228fd32007-01-13 02:28:11 -05002385 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002386 if (status != 0)
2387 goto out;
2388 /* Make sure server returned a different fsid for the referral */
2389 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002390 dprintk("%s: server did not return a different fsid for"
2391 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392 status = -EIO;
2393 goto out;
2394 }
Andy Adamson533eb462011-06-13 18:25:56 -04002395 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2396 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002397
Andy Adamson533eb462011-06-13 18:25:56 -04002398 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002399 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002400 memset(fhandle, 0, sizeof(struct nfs_fh));
2401out:
2402 if (page)
2403 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002404 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002405 return status;
2406}
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2409{
2410 struct nfs4_getattr_arg args = {
2411 .fh = fhandle,
2412 .bitmask = server->attr_bitmask,
2413 };
2414 struct nfs4_getattr_res res = {
2415 .fattr = fattr,
2416 .server = server,
2417 };
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2420 .rpc_argp = &args,
2421 .rpc_resp = &res,
2422 };
2423
Trond Myklebust0e574af2005-10-27 22:12:38 -04002424 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2429{
2430 struct nfs4_exception exception = { };
2431 int err;
2432 do {
2433 err = nfs4_handle_exception(server,
2434 _nfs4_proc_getattr(server, fhandle, fattr),
2435 &exception);
2436 } while (exception.retry);
2437 return err;
2438}
2439
2440/*
2441 * The file is not closed if it is opened due to the a request to change
2442 * the size of the file. The open call will not be needed once the
2443 * VFS layer lookup-intents are implemented.
2444 *
2445 * Close is called when the inode is destroyed.
2446 * If we haven't opened the file for O_WRONLY, we
2447 * need to in the size_change case to obtain a stateid.
2448 *
2449 * Got race?
2450 * Because OPEN is always done by name in nfsv4, it is
2451 * possible that we opened a different file by the same
2452 * name. We can recognize this race condition, but we
2453 * can't do anything about it besides returning an error.
2454 *
2455 * This will be fixed with VFS changes (lookup-intent).
2456 */
2457static int
2458nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2459 struct iattr *sattr)
2460{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002461 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002462 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002463 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 int status;
2465
Benny Halevy8a1636c2010-07-14 15:43:57 -04002466 if (pnfs_ld_layoutret_on_setattr(inode))
2467 pnfs_return_layout(inode);
2468
Trond Myklebust0e574af2005-10-27 22:12:38 -04002469 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Trond Myklebustd5308382005-11-04 15:33:38 -05002471 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002472 if (sattr->ia_valid & ATTR_FILE) {
2473 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002474
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002475 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002476 if (ctx) {
2477 cred = ctx->cred;
2478 state = ctx->state;
2479 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002480 }
2481
Trond Myklebust536e43d2012-01-17 22:04:26 -05002482 /* Deal with open(O_TRUNC) */
2483 if (sattr->ia_valid & ATTR_OPEN)
2484 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2485
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002486 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002487 if (status == 0)
2488 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return status;
2490}
2491
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002492static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2493 const struct qstr *name, struct nfs_fh *fhandle,
2494 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002495{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002496 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002497 int status;
2498 struct nfs4_lookup_arg args = {
2499 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002500 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002501 .name = name,
2502 };
2503 struct nfs4_lookup_res res = {
2504 .server = server,
2505 .fattr = fattr,
2506 .fh = fhandle,
2507 };
2508 struct rpc_message msg = {
2509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2510 .rpc_argp = &args,
2511 .rpc_resp = &res,
2512 };
2513
2514 nfs_fattr_init(fattr);
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002517 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 dprintk("NFS reply lookup: %d\n", status);
2519 return status;
2520}
2521
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002522void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2523{
2524 memset(fh, 0, sizeof(struct nfs_fh));
2525 fattr->fsid.major = 1;
2526 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2527 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2528 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2529 fattr->nlink = 2;
2530}
2531
Bryan Schumaker7c513052011-03-24 17:12:24 +00002532static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2533 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 struct nfs4_exception exception = { };
2536 int err;
2537 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002538 int status;
2539
2540 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2541 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002542 case -NFS4ERR_BADNAME:
2543 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002544 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002545 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002546 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002547 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002548 }
2549 err = nfs4_handle_exception(NFS_SERVER(dir),
2550 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 } while (exception.retry);
2552 return err;
2553}
2554
2555static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2556{
Trond Myklebust76b32992007-08-10 17:45:11 -04002557 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct nfs4_accessargs args = {
2559 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002560 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002562 struct nfs4_accessres res = {
2563 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002564 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct rpc_message msg = {
2566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2567 .rpc_argp = &args,
2568 .rpc_resp = &res,
2569 .rpc_cred = entry->cred,
2570 };
2571 int mode = entry->mask;
2572 int status;
2573
2574 /*
2575 * Determine which access bits we want to ask for...
2576 */
2577 if (mode & MAY_READ)
2578 args.access |= NFS4_ACCESS_READ;
2579 if (S_ISDIR(inode->i_mode)) {
2580 if (mode & MAY_WRITE)
2581 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2582 if (mode & MAY_EXEC)
2583 args.access |= NFS4_ACCESS_LOOKUP;
2584 } else {
2585 if (mode & MAY_WRITE)
2586 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2587 if (mode & MAY_EXEC)
2588 args.access |= NFS4_ACCESS_EXECUTE;
2589 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002590
2591 res.fattr = nfs_alloc_fattr();
2592 if (res.fattr == NULL)
2593 return -ENOMEM;
2594
Bryan Schumaker7c513052011-03-24 17:12:24 +00002595 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (!status) {
2597 entry->mask = 0;
2598 if (res.access & NFS4_ACCESS_READ)
2599 entry->mask |= MAY_READ;
2600 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2601 entry->mask |= MAY_WRITE;
2602 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2603 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002604 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002606 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return status;
2608}
2609
2610static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2611{
2612 struct nfs4_exception exception = { };
2613 int err;
2614 do {
2615 err = nfs4_handle_exception(NFS_SERVER(inode),
2616 _nfs4_proc_access(inode, entry),
2617 &exception);
2618 } while (exception.retry);
2619 return err;
2620}
2621
2622/*
2623 * TODO: For the time being, we don't try to get any attributes
2624 * along with any of the zero-copy operations READ, READDIR,
2625 * READLINK, WRITE.
2626 *
2627 * In the case of the first three, we want to put the GETATTR
2628 * after the read-type operation -- this is because it is hard
2629 * to predict the length of a GETATTR response in v4, and thus
2630 * align the READ data correctly. This means that the GETATTR
2631 * may end up partially falling into the page cache, and we should
2632 * shift it into the 'tail' of the xdr_buf before processing.
2633 * To do this efficiently, we need to know the total length
2634 * of data received, which doesn't seem to be available outside
2635 * of the RPC layer.
2636 *
2637 * In the case of WRITE, we also want to put the GETATTR after
2638 * the operation -- in this case because we want to make sure
2639 * we get the post-operation mtime and size. This means that
2640 * we can't use xdr_encode_pages() as written: we need a variant
2641 * of it which would leave room in the 'tail' iovec.
2642 *
2643 * Both of these changes to the XDR layer would in fact be quite
2644 * minor, but I decided to leave them for a subsequent patch.
2645 */
2646static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2647 unsigned int pgbase, unsigned int pglen)
2648{
2649 struct nfs4_readlink args = {
2650 .fh = NFS_FH(inode),
2651 .pgbase = pgbase,
2652 .pglen = pglen,
2653 .pages = &page,
2654 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002655 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2658 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002659 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 };
2661
Bryan Schumaker7c513052011-03-24 17:12:24 +00002662 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 -07002663}
2664
2665static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2666 unsigned int pgbase, unsigned int pglen)
2667{
2668 struct nfs4_exception exception = { };
2669 int err;
2670 do {
2671 err = nfs4_handle_exception(NFS_SERVER(inode),
2672 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2673 &exception);
2674 } while (exception.retry);
2675 return err;
2676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678/*
2679 * Got race?
2680 * We will need to arrange for the VFS layer to provide an atomic open.
2681 * Until then, this create/open method is prone to inefficiency and race
2682 * conditions due to the lookup, create, and open VFS calls from sys_open()
2683 * placed on the wire.
2684 *
2685 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2686 * The file will be opened again in the subsequent VFS open call
2687 * (nfs4_proc_file_open).
2688 *
2689 * The open for read will just hang around to be used by any process that
2690 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2691 */
2692
2693static int
2694nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002695 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Al Viro82a2c1b2011-06-22 18:30:55 -04002697 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002699 struct rpc_cred *cred = NULL;
2700 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int status = 0;
2702
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002703 if (ctx != NULL) {
2704 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002705 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002706 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002708 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002709 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002710 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (IS_ERR(state)) {
2712 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002715 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002716 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002717 if (ctx != NULL)
2718 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002719 else
Al Viro643168c2011-06-22 18:20:23 -04002720 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721out:
2722 return status;
2723}
2724
2725static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2726{
Trond Myklebust16e42952005-10-27 22:12:44 -04002727 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002728 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002730 .name.len = name->len,
2731 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002732 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002734 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002735 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2739 .rpc_argp = &args,
2740 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002742 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Trond Myklebustd3468902010-04-16 16:22:50 -04002744 res.dir_attr = nfs_alloc_fattr();
2745 if (res.dir_attr == NULL)
2746 goto out;
2747
Bryan Schumaker7c513052011-03-24 17:12:24 +00002748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002749 if (status == 0) {
2750 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002751 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002752 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002753 nfs_free_fattr(res.dir_attr);
2754out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 return status;
2756}
2757
2758static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2759{
2760 struct nfs4_exception exception = { };
2761 int err;
2762 do {
2763 err = nfs4_handle_exception(NFS_SERVER(dir),
2764 _nfs4_proc_remove(dir, name),
2765 &exception);
2766 } while (exception.retry);
2767 return err;
2768}
2769
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002770static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002772 struct nfs_server *server = NFS_SERVER(dir);
2773 struct nfs_removeargs *args = msg->rpc_argp;
2774 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Trond Myklebusta65318b2009-03-11 14:10:28 -04002776 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002777 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002779 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002782static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2783{
2784 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2785 &data->args.seq_args,
2786 &data->res.seq_res,
2787 task))
2788 return;
2789 rpc_call_start(task);
2790}
2791
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002792static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002794 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2795
Trond Myklebust14516c32010-07-31 14:29:06 -04002796 if (!nfs4_sequence_done(task, &res->seq_res))
2797 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002798 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002799 return 0;
2800 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002801 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002802 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
Jeff Laytond3d41522010-09-17 17:31:57 -04002805static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2806{
2807 struct nfs_server *server = NFS_SERVER(dir);
2808 struct nfs_renameargs *arg = msg->rpc_argp;
2809 struct nfs_renameres *res = msg->rpc_resp;
2810
2811 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2812 arg->bitmask = server->attr_bitmask;
2813 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002814 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002815}
2816
2817static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2818 struct inode *new_dir)
2819{
2820 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2821
2822 if (!nfs4_sequence_done(task, &res->seq_res))
2823 return 0;
2824 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2825 return 0;
2826
2827 update_changeattr(old_dir, &res->old_cinfo);
2828 nfs_post_op_update_inode(old_dir, res->old_fattr);
2829 update_changeattr(new_dir, &res->new_cinfo);
2830 nfs_post_op_update_inode(new_dir, res->new_fattr);
2831 return 1;
2832}
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2835 struct inode *new_dir, struct qstr *new_name)
2836{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002837 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002838 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 .old_dir = NFS_FH(old_dir),
2840 .new_dir = NFS_FH(new_dir),
2841 .old_name = old_name,
2842 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002843 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002845 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002846 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct rpc_message msg = {
2849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2850 .rpc_argp = &arg,
2851 .rpc_resp = &res,
2852 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002853 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Trond Myklebust011fff72010-04-16 16:22:49 -04002855 res.old_fattr = nfs_alloc_fattr();
2856 res.new_fattr = nfs_alloc_fattr();
2857 if (res.old_fattr == NULL || res.new_fattr == NULL)
2858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Bryan Schumaker7c513052011-03-24 17:12:24 +00002860 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (!status) {
2862 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002863 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002865 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002867out:
2868 nfs_free_fattr(res.new_fattr);
2869 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return status;
2871}
2872
2873static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2874 struct inode *new_dir, struct qstr *new_name)
2875{
2876 struct nfs4_exception exception = { };
2877 int err;
2878 do {
2879 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2880 _nfs4_proc_rename(old_dir, old_name,
2881 new_dir, new_name),
2882 &exception);
2883 } while (exception.retry);
2884 return err;
2885}
2886
2887static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2888{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002889 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 struct nfs4_link_arg arg = {
2891 .fh = NFS_FH(inode),
2892 .dir_fh = NFS_FH(dir),
2893 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002894 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002896 struct nfs4_link_res res = {
2897 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002898 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 struct rpc_message msg = {
2900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2901 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002902 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002904 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Trond Myklebust136f2622010-04-16 16:22:49 -04002906 res.fattr = nfs_alloc_fattr();
2907 res.dir_attr = nfs_alloc_fattr();
2908 if (res.fattr == NULL || res.dir_attr == NULL)
2909 goto out;
2910
Bryan Schumaker7c513052011-03-24 17:12:24 +00002911 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002912 if (!status) {
2913 update_changeattr(dir, &res.cinfo);
2914 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002915 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002916 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002917out:
2918 nfs_free_fattr(res.dir_attr);
2919 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return status;
2921}
2922
2923static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2924{
2925 struct nfs4_exception exception = { };
2926 int err;
2927 do {
2928 err = nfs4_handle_exception(NFS_SERVER(inode),
2929 _nfs4_proc_link(inode, dir, name),
2930 &exception);
2931 } while (exception.retry);
2932 return err;
2933}
2934
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935struct nfs4_createdata {
2936 struct rpc_message msg;
2937 struct nfs4_create_arg arg;
2938 struct nfs4_create_res res;
2939 struct nfs_fh fh;
2940 struct nfs_fattr fattr;
2941 struct nfs_fattr dir_fattr;
2942};
2943
2944static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2945 struct qstr *name, struct iattr *sattr, u32 ftype)
2946{
2947 struct nfs4_createdata *data;
2948
2949 data = kzalloc(sizeof(*data), GFP_KERNEL);
2950 if (data != NULL) {
2951 struct nfs_server *server = NFS_SERVER(dir);
2952
2953 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2954 data->msg.rpc_argp = &data->arg;
2955 data->msg.rpc_resp = &data->res;
2956 data->arg.dir_fh = NFS_FH(dir);
2957 data->arg.server = server;
2958 data->arg.name = name;
2959 data->arg.attrs = sattr;
2960 data->arg.ftype = ftype;
2961 data->arg.bitmask = server->attr_bitmask;
2962 data->res.server = server;
2963 data->res.fh = &data->fh;
2964 data->res.fattr = &data->fattr;
2965 data->res.dir_fattr = &data->dir_fattr;
2966 nfs_fattr_init(data->res.fattr);
2967 nfs_fattr_init(data->res.dir_fattr);
2968 }
2969 return data;
2970}
2971
2972static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2973{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002974 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002975 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 if (status == 0) {
2977 update_changeattr(dir, &data->res.dir_cinfo);
2978 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2979 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2980 }
2981 return status;
2982}
2983
2984static void nfs4_free_createdata(struct nfs4_createdata *data)
2985{
2986 kfree(data);
2987}
2988
Chuck Lever4f390c12006-08-22 20:06:22 -04002989static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002990 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002992 struct nfs4_createdata *data;
2993 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Chuck Lever94a6d752006-08-22 20:06:23 -04002995 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002996 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002997
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002998 status = -ENOMEM;
2999 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3000 if (data == NULL)
3001 goto out;
3002
3003 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3004 data->arg.u.symlink.pages = &page;
3005 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003007 status = nfs4_do_create(dir, dentry, data);
3008
3009 nfs4_free_createdata(data);
3010out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return status;
3012}
3013
Chuck Lever4f390c12006-08-22 20:06:22 -04003014static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003015 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 struct nfs4_exception exception = { };
3018 int err;
3019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003021 _nfs4_proc_symlink(dir, dentry, page,
3022 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3029 struct iattr *sattr)
3030{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003031 struct nfs4_createdata *data;
3032 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3035 if (data == NULL)
3036 goto out;
3037
3038 status = nfs4_do_create(dir, dentry, data);
3039
3040 nfs4_free_createdata(data);
3041out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return status;
3043}
3044
3045static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3046 struct iattr *sattr)
3047{
3048 struct nfs4_exception exception = { };
3049 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003050
3051 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 do {
3053 err = nfs4_handle_exception(NFS_SERVER(dir),
3054 _nfs4_proc_mkdir(dir, dentry, sattr),
3055 &exception);
3056 } while (exception.retry);
3057 return err;
3058}
3059
3060static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003061 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 struct inode *dir = dentry->d_inode;
3064 struct nfs4_readdir_arg args = {
3065 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003066 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .pgbase = 0,
3068 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003069 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003070 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 };
3072 struct nfs4_readdir_res res;
3073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3075 .rpc_argp = &args,
3076 .rpc_resp = &res,
3077 .rpc_cred = cred,
3078 };
3079 int status;
3080
Harvey Harrison3110ff82008-05-02 13:42:44 -07003081 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003082 dentry->d_parent->d_name.name,
3083 dentry->d_name.name,
3084 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3086 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003087 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 -05003088 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003090 status += args.pgbase;
3091 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003092
3093 nfs_invalidate_atime(dir);
3094
Harvey Harrison3110ff82008-05-02 13:42:44 -07003095 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return status;
3097}
3098
3099static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003100 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104 do {
3105 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3106 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003107 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 &exception);
3109 } while (exception.retry);
3110 return err;
3111}
3112
3113static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3114 struct iattr *sattr, dev_t rdev)
3115{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003116 struct nfs4_createdata *data;
3117 int mode = sattr->ia_mode;
3118 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3121 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003122
3123 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3124 if (data == NULL)
3125 goto out;
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003128 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003130 data->arg.ftype = NF4BLK;
3131 data->arg.u.device.specdata1 = MAJOR(rdev);
3132 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003135 data->arg.ftype = NF4CHR;
3136 data->arg.u.device.specdata1 = MAJOR(rdev);
3137 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003140 status = nfs4_do_create(dir, dentry, data);
3141
3142 nfs4_free_createdata(data);
3143out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return status;
3145}
3146
3147static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3148 struct iattr *sattr, dev_t rdev)
3149{
3150 struct nfs4_exception exception = { };
3151 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003152
3153 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dir),
3156 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3157 &exception);
3158 } while (exception.retry);
3159 return err;
3160}
3161
3162static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_fsstat *fsstat)
3164{
3165 struct nfs4_statfs_arg args = {
3166 .fh = fhandle,
3167 .bitmask = server->attr_bitmask,
3168 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003169 struct nfs4_statfs_res res = {
3170 .fsstat = fsstat,
3171 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3174 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003175 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 };
3177
Trond Myklebust0e574af2005-10-27 22:12:38 -04003178 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003179 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
3182static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3183{
3184 struct nfs4_exception exception = { };
3185 int err;
3186 do {
3187 err = nfs4_handle_exception(server,
3188 _nfs4_proc_statfs(server, fhandle, fsstat),
3189 &exception);
3190 } while (exception.retry);
3191 return err;
3192}
3193
3194static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3195 struct nfs_fsinfo *fsinfo)
3196{
3197 struct nfs4_fsinfo_arg args = {
3198 .fh = fhandle,
3199 .bitmask = server->attr_bitmask,
3200 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003201 struct nfs4_fsinfo_res res = {
3202 .fsinfo = fsinfo,
3203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct rpc_message msg = {
3205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3206 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003207 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 };
3209
Bryan Schumaker7c513052011-03-24 17:12:24 +00003210 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
3213static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3214{
3215 struct nfs4_exception exception = { };
3216 int err;
3217
3218 do {
3219 err = nfs4_handle_exception(server,
3220 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3221 &exception);
3222 } while (exception.retry);
3223 return err;
3224}
3225
3226static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3227{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003228 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3230}
3231
3232static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3233 struct nfs_pathconf *pathconf)
3234{
3235 struct nfs4_pathconf_arg args = {
3236 .fh = fhandle,
3237 .bitmask = server->attr_bitmask,
3238 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003239 struct nfs4_pathconf_res res = {
3240 .pathconf = pathconf,
3241 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 struct rpc_message msg = {
3243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3244 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003245 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 };
3247
3248 /* None of the pathconf attributes are mandatory to implement */
3249 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3250 memset(pathconf, 0, sizeof(*pathconf));
3251 return 0;
3252 }
3253
Trond Myklebust0e574af2005-10-27 22:12:38 -04003254 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003255 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256}
3257
3258static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3259 struct nfs_pathconf *pathconf)
3260{
3261 struct nfs4_exception exception = { };
3262 int err;
3263
3264 do {
3265 err = nfs4_handle_exception(server,
3266 _nfs4_proc_pathconf(server, fhandle, pathconf),
3267 &exception);
3268 } while (exception.retry);
3269 return err;
3270}
3271
Benny Halevyd20581a2011-05-22 19:52:03 +03003272void __nfs4_read_done_cb(struct nfs_read_data *data)
3273{
3274 nfs_invalidate_atime(data->inode);
3275}
3276
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003277static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Trond Myklebustec06c092006-03-20 13:44:27 -05003279 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003281 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003282 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003283 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003285
Benny Halevyd20581a2011-05-22 19:52:03 +03003286 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003288 renew_lease(server, data->timestamp);
3289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003292static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3293{
3294
3295 dprintk("--> %s\n", __func__);
3296
3297 if (!nfs4_sequence_done(task, &data->res.seq_res))
3298 return -EAGAIN;
3299
Benny Halevyd20581a2011-05-22 19:52:03 +03003300 return data->read_done_cb ? data->read_done_cb(task, data) :
3301 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003302}
3303
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003304static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003307 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003308 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003309 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003312static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3313{
3314 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3315 &data->args.seq_args,
3316 &data->res.seq_res,
3317 task))
3318 return;
3319 rpc_call_start(task);
3320}
3321
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003322/* Reset the the nfs_read_data to send the read to the MDS. */
3323void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3324{
3325 dprintk("%s Reset task for i/o through\n", __func__);
3326 put_lseg(data->lseg);
3327 data->lseg = NULL;
3328 /* offsets will differ in the dense stripe case */
3329 data->args.offset = data->mds_offset;
3330 data->ds_clp = NULL;
3331 data->args.fh = NFS_FH(data->inode);
3332 data->read_done_cb = nfs4_read_done_cb;
3333 task->tk_ops = data->mds_ops;
3334 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3335}
3336EXPORT_SYMBOL_GPL(nfs4_reset_read);
3337
Fred Isamanb029bc92011-03-03 15:13:42 +00003338static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct inode *inode = data->inode;
3341
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003342 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003343 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003344 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003346 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003348 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003349 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
Fred Isamanb029bc92011-03-03 15:13:42 +00003353static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3354{
3355 if (!nfs4_sequence_done(task, &data->res.seq_res))
3356 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003357 return data->write_done_cb ? data->write_done_cb(task, data) :
3358 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003359}
3360
Fred Isamana69aef12011-03-03 15:13:47 +00003361/* Reset the the nfs_write_data to send the write to the MDS. */
3362void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3363{
3364 dprintk("%s Reset task for i/o through\n", __func__);
3365 put_lseg(data->lseg);
3366 data->lseg = NULL;
3367 data->ds_clp = NULL;
3368 data->write_done_cb = nfs4_write_done_cb;
3369 data->args.fh = NFS_FH(data->inode);
3370 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3371 data->args.offset = data->mds_offset;
3372 data->res.fattr = &data->fattr;
3373 task->tk_ops = data->mds_ops;
3374 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3375}
3376EXPORT_SYMBOL_GPL(nfs4_reset_write);
3377
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003378static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003380 struct nfs_server *server = NFS_SERVER(data->inode);
3381
Fred Isaman7ffd1062011-03-03 15:13:46 +00003382 if (data->lseg) {
3383 data->args.bitmask = NULL;
3384 data->res.fattr = NULL;
3385 } else
3386 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003387 if (!data->write_done_cb)
3388 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003389 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 data->timestamp = jiffies;
3391
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003392 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003393 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003396static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3397{
3398 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3399 &data->args.seq_args,
3400 &data->res.seq_res,
3401 task))
3402 return;
3403 rpc_call_start(task);
3404}
3405
Fred Isaman5f452432011-03-23 13:27:46 +00003406static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003409
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003410 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003411 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003412 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003414 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Fred Isaman5f452432011-03-23 13:27:46 +00003418static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3419{
3420 if (!nfs4_sequence_done(task, &data->res.seq_res))
3421 return -EAGAIN;
3422 return data->write_done_cb(task, data);
3423}
3424
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003425static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003427 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003428
3429 if (data->lseg) {
3430 data->args.bitmask = NULL;
3431 data->res.fattr = NULL;
3432 } else
3433 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003434 if (!data->write_done_cb)
3435 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003436 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003437 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003438 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003441struct nfs4_renewdata {
3442 struct nfs_client *client;
3443 unsigned long timestamp;
3444};
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446/*
3447 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3448 * standalone procedure for queueing an asynchronous RENEW.
3449 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003450static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003451{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003452 struct nfs4_renewdata *data = calldata;
3453 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003454
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003455 if (atomic_read(&clp->cl_count) > 1)
3456 nfs4_schedule_state_renewal(clp);
3457 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003458 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003459}
3460
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003461static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003463 struct nfs4_renewdata *data = calldata;
3464 struct nfs_client *clp = data->client;
3465 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003468 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003469 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3470 return;
3471 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003472 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003473 return;
3474 }
3475 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003477 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003480static const struct rpc_call_ops nfs4_renew_ops = {
3481 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003482 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003483};
3484
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003485static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
3487 struct rpc_message msg = {
3488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3489 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003490 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003492 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003494 if (renew_flags == 0)
3495 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003496 if (!atomic_inc_not_zero(&clp->cl_count))
3497 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003498 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003499 if (data == NULL)
3500 return -ENOMEM;
3501 data->client = clp;
3502 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003504 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505}
3506
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003507static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
3509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3511 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003512 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 };
3514 unsigned long now = jiffies;
3515 int status;
3516
3517 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3518 if (status < 0)
3519 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003520 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 return 0;
3522}
3523
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003524static inline int nfs4_server_supports_acls(struct nfs_server *server)
3525{
3526 return (server->caps & NFS_CAP_ACLS)
3527 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3528 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3529}
3530
3531/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3532 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3533 * the stack.
3534 */
3535#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3536
Neil Hormane9e3d722011-03-04 19:26:03 -05003537static int buf_to_pages_noslab(const void *buf, size_t buflen,
3538 struct page **pages, unsigned int *pgbase)
3539{
3540 struct page *newpage, **spages;
3541 int rc = 0;
3542 size_t len;
3543 spages = pages;
3544
3545 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003546 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003547 newpage = alloc_page(GFP_KERNEL);
3548
3549 if (newpage == NULL)
3550 goto unwind;
3551 memcpy(page_address(newpage), buf, len);
3552 buf += len;
3553 buflen -= len;
3554 *pages++ = newpage;
3555 rc++;
3556 } while (buflen != 0);
3557
3558 return rc;
3559
3560unwind:
3561 for(; rc > 0; rc--)
3562 __free_page(spages[rc-1]);
3563 return -ENOMEM;
3564}
3565
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003566struct nfs4_cached_acl {
3567 int cached;
3568 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003569 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003570};
3571
3572static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003573{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003574 struct nfs_inode *nfsi = NFS_I(inode);
3575
3576 spin_lock(&inode->i_lock);
3577 kfree(nfsi->nfs4_acl);
3578 nfsi->nfs4_acl = acl;
3579 spin_unlock(&inode->i_lock);
3580}
3581
3582static void nfs4_zap_acl_attr(struct inode *inode)
3583{
3584 nfs4_set_cached_acl(inode, NULL);
3585}
3586
3587static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3588{
3589 struct nfs_inode *nfsi = NFS_I(inode);
3590 struct nfs4_cached_acl *acl;
3591 int ret = -ENOENT;
3592
3593 spin_lock(&inode->i_lock);
3594 acl = nfsi->nfs4_acl;
3595 if (acl == NULL)
3596 goto out;
3597 if (buf == NULL) /* user is just asking for length */
3598 goto out_len;
3599 if (acl->cached == 0)
3600 goto out;
3601 ret = -ERANGE; /* see getxattr(2) man page */
3602 if (acl->len > buflen)
3603 goto out;
3604 memcpy(buf, acl->data, acl->len);
3605out_len:
3606 ret = acl->len;
3607out:
3608 spin_unlock(&inode->i_lock);
3609 return ret;
3610}
3611
3612static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3613{
3614 struct nfs4_cached_acl *acl;
3615
3616 if (buf && acl_len <= PAGE_SIZE) {
3617 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3618 if (acl == NULL)
3619 goto out;
3620 acl->cached = 1;
3621 memcpy(acl->data, buf, acl_len);
3622 } else {
3623 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3624 if (acl == NULL)
3625 goto out;
3626 acl->cached = 0;
3627 }
3628 acl->len = acl_len;
3629out:
3630 nfs4_set_cached_acl(inode, acl);
3631}
3632
Andy Adamsonbf118a32011-12-07 11:55:27 -05003633/*
3634 * The getxattr API returns the required buffer length when called with a
3635 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3636 * the required buf. On a NULL buf, we send a page of data to the server
3637 * guessing that the ACL request can be serviced by a page. If so, we cache
3638 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3639 * the cache. If not so, we throw away the page, and cache the required
3640 * length. The next getxattr call will then produce another round trip to
3641 * the server, this time with the input buf of the required size.
3642 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003643static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003644{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003645 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003646 struct nfs_getaclargs args = {
3647 .fh = NFS_FH(inode),
3648 .acl_pages = pages,
3649 .acl_len = buflen,
3650 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003651 struct nfs_getaclres res = {
3652 .acl_len = buflen,
3653 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003654 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003655 struct rpc_message msg = {
3656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3657 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003658 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003659 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003660 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003661
Andy Adamsonbf118a32011-12-07 11:55:27 -05003662 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3663 /* As long as we're doing a round trip to the server anyway,
3664 * let's be prepared for a page of acl data. */
3665 if (npages == 0)
3666 npages = 1;
3667
3668 for (i = 0; i < npages; i++) {
3669 pages[i] = alloc_page(GFP_KERNEL);
3670 if (!pages[i])
3671 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003672 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003673 if (npages > 1) {
3674 /* for decoding across pages */
Trond Myklebust331818f2012-02-03 18:30:53 -05003675 res.acl_scratch = alloc_page(GFP_KERNEL);
3676 if (!res.acl_scratch)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003677 goto out_free;
3678 }
3679 args.acl_len = npages * PAGE_SIZE;
3680 args.acl_pgbase = 0;
3681 /* Let decode_getfacl know not to fail if the ACL data is larger than
3682 * the page we send as a guess */
3683 if (buf == NULL)
3684 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3685 resp_buf = page_address(pages[0]);
3686
Peng Taode040be2012-01-10 22:42:47 +08003687 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003688 __func__, buf, buflen, npages, args.acl_len);
3689 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3690 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691 if (ret)
3692 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003693
3694 acl_len = res.acl_len - res.acl_data_offset;
3695 if (acl_len > args.acl_len)
3696 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003697 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003698 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3699 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003700 if (buf) {
3701 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003702 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003703 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003704 _copy_from_pages(buf, pages, res.acl_data_offset,
3705 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003706 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003707 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003708out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003709 for (i = 0; i < npages; i++)
3710 if (pages[i])
3711 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003712 if (res.acl_scratch)
3713 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003714 return ret;
3715}
3716
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003717static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3718{
3719 struct nfs4_exception exception = { };
3720 ssize_t ret;
3721 do {
3722 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3723 if (ret >= 0)
3724 break;
3725 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3726 } while (exception.retry);
3727 return ret;
3728}
3729
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003730static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3731{
3732 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003733 int ret;
3734
3735 if (!nfs4_server_supports_acls(server))
3736 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003737 ret = nfs_revalidate_inode(server, inode);
3738 if (ret < 0)
3739 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003740 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3741 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003742 ret = nfs4_read_cached_acl(inode, buf, buflen);
3743 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003744 /* -ENOENT is returned if there is no ACL or if there is an ACL
3745 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003746 return ret;
3747 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003748}
3749
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003750static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003751{
3752 struct nfs_server *server = NFS_SERVER(inode);
3753 struct page *pages[NFS4ACL_MAXPAGES];
3754 struct nfs_setaclargs arg = {
3755 .fh = NFS_FH(inode),
3756 .acl_pages = pages,
3757 .acl_len = buflen,
3758 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003759 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003760 struct rpc_message msg = {
3761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3762 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003763 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003764 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003765 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003766
3767 if (!nfs4_server_supports_acls(server))
3768 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003769 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3770 if (i < 0)
3771 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003772 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003773 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003774
3775 /*
3776 * Free each page after tx, so the only ref left is
3777 * held by the network stack
3778 */
3779 for (; i > 0; i--)
3780 put_page(pages[i-1]);
3781
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003782 /*
3783 * Acl update can result in inode attribute update.
3784 * so mark the attribute cache invalid.
3785 */
3786 spin_lock(&inode->i_lock);
3787 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3788 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003789 nfs_access_zap_cache(inode);
3790 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003791 return ret;
3792}
3793
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003794static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3795{
3796 struct nfs4_exception exception = { };
3797 int err;
3798 do {
3799 err = nfs4_handle_exception(NFS_SERVER(inode),
3800 __nfs4_proc_set_acl(inode, buf, buflen),
3801 &exception);
3802 } while (exception.retry);
3803 return err;
3804}
3805
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003807nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003809 struct nfs_client *clp = server->nfs_client;
3810
3811 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 return 0;
3813 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003814 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003815 case -NFS4ERR_ADMIN_REVOKED:
3816 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003817 if (state != NULL)
3818 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003819 case -NFS4ERR_OPENMODE:
3820 if (state == NULL)
3821 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003822 nfs4_schedule_stateid_recovery(server, state);
3823 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003824 case -NFS4ERR_EXPIRED:
3825 if (state != NULL)
3826 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003828 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003829 nfs4_schedule_lease_recovery(clp);
3830 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003831#if defined(CONFIG_NFS_V4_1)
3832 case -NFS4ERR_BADSESSION:
3833 case -NFS4ERR_BADSLOT:
3834 case -NFS4ERR_BAD_HIGH_SLOT:
3835 case -NFS4ERR_DEADSESSION:
3836 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3837 case -NFS4ERR_SEQ_FALSE_RETRY:
3838 case -NFS4ERR_SEQ_MISORDERED:
3839 dprintk("%s ERROR %d, Reset session\n", __func__,
3840 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003841 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003842 task->tk_status = 0;
3843 return -EAGAIN;
3844#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003846 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003847 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003848 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3850 task->tk_status = 0;
3851 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003852 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 case -NFS4ERR_OLD_STATEID:
3854 task->tk_status = 0;
3855 return -EAGAIN;
3856 }
3857 task->tk_status = nfs4_map_errors(task->tk_status);
3858 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003859wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003860 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003861 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3862 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3863 task->tk_status = 0;
3864 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
Chuck Levercd937102012-03-02 17:14:31 -05003867static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3868 nfs4_verifier *bootverf)
3869{
3870 __be32 verf[2];
3871
3872 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3873 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3874 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3875}
3876
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003877int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3878 unsigned short port, struct rpc_cred *cred,
3879 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 nfs4_verifier sc_verifier;
3882 struct nfs4_setclientid setclientid = {
3883 .sc_verifier = &sc_verifier,
3884 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003885 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 };
3887 struct rpc_message msg = {
3888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3889 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003890 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003891 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 int loop = 0;
3894 int status;
3895
Chuck Levercd937102012-03-02 17:14:31 -05003896 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003899 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003901 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003902 clp->cl_ipaddr,
3903 rpc_peeraddr2str(clp->cl_rpcclient,
3904 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003905 rpc_peeraddr2str(clp->cl_rpcclient,
3906 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003907 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 clp->cl_id_uniquifier);
3909 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003910 sizeof(setclientid.sc_netid),
3911 rpc_peeraddr2str(clp->cl_rpcclient,
3912 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003914 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003916 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003918 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 if (status != -NFS4ERR_CLID_INUSE)
3920 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003921 if (loop != 0) {
3922 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003924 }
3925 ++loop;
3926 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
3928 return status;
3929}
3930
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003931int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003932 struct nfs4_setclientid_res *arg,
3933 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 struct nfs_fsinfo fsinfo;
3936 struct rpc_message msg = {
3937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003938 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003940 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 };
3942 unsigned long now;
3943 int status;
3944
3945 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003946 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 if (status == 0) {
3948 spin_lock(&clp->cl_lock);
3949 clp->cl_lease_time = fsinfo.lease_time * HZ;
3950 clp->cl_last_renewal = now;
3951 spin_unlock(&clp->cl_lock);
3952 }
3953 return status;
3954}
3955
Trond Myklebustfe650402006-01-03 09:55:18 +01003956struct nfs4_delegreturndata {
3957 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003958 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003959 struct nfs_fh fh;
3960 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003961 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003962 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003963 int rpc_status;
3964};
3965
Trond Myklebustfe650402006-01-03 09:55:18 +01003966static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3967{
3968 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003969
Trond Myklebust14516c32010-07-31 14:29:06 -04003970 if (!nfs4_sequence_done(task, &data->res.seq_res))
3971 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003972
Ricardo Labiaga79708862009-12-07 09:23:21 -05003973 switch (task->tk_status) {
3974 case -NFS4ERR_STALE_STATEID:
3975 case -NFS4ERR_EXPIRED:
3976 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003977 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003978 break;
3979 default:
3980 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3981 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003982 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003983 return;
3984 }
3985 }
3986 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003987}
3988
3989static void nfs4_delegreturn_release(void *calldata)
3990{
Trond Myklebustfe650402006-01-03 09:55:18 +01003991 kfree(calldata);
3992}
3993
Andy Adamson938e1012009-04-01 09:22:28 -04003994#if defined(CONFIG_NFS_V4_1)
3995static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3996{
3997 struct nfs4_delegreturndata *d_data;
3998
3999 d_data = (struct nfs4_delegreturndata *)data;
4000
Trond Myklebust035168a2010-06-16 09:52:26 -04004001 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004002 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004003 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004004 return;
4005 rpc_call_start(task);
4006}
4007#endif /* CONFIG_NFS_V4_1 */
4008
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004009static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004010#if defined(CONFIG_NFS_V4_1)
4011 .rpc_call_prepare = nfs4_delegreturn_prepare,
4012#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004013 .rpc_call_done = nfs4_delegreturn_done,
4014 .rpc_release = nfs4_delegreturn_release,
4015};
4016
Trond Myklebuste6f81072008-01-24 18:14:34 -05004017static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004018{
4019 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004020 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004021 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004022 struct rpc_message msg = {
4023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4024 .rpc_cred = cred,
4025 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004026 struct rpc_task_setup task_setup_data = {
4027 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004028 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004029 .callback_ops = &nfs4_delegreturn_ops,
4030 .flags = RPC_TASK_ASYNC,
4031 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004032 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004033
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004034 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004035 if (data == NULL)
4036 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004037 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004038 data->args.fhandle = &data->fh;
4039 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004040 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004041 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004042 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004043 data->res.fattr = &data->fattr;
4044 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004045 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004046 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004047 data->rpc_status = 0;
4048
Trond Myklebustc970aa82007-07-14 15:39:59 -04004049 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004050 msg.rpc_argp = &data->args;
4051 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004052 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004053 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004054 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004055 if (!issync)
4056 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004057 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004058 if (status != 0)
4059 goto out;
4060 status = data->rpc_status;
4061 if (status != 0)
4062 goto out;
4063 nfs_refresh_inode(inode, &data->fattr);
4064out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004065 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004066 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
Trond Myklebuste6f81072008-01-24 18:14:34 -05004069int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 struct nfs_server *server = NFS_SERVER(inode);
4072 struct nfs4_exception exception = { };
4073 int err;
4074 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004075 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 switch (err) {
4077 case -NFS4ERR_STALE_STATEID:
4078 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 case 0:
4080 return 0;
4081 }
4082 err = nfs4_handle_exception(server, err, &exception);
4083 } while (exception.retry);
4084 return err;
4085}
4086
4087#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4088#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4089
4090/*
4091 * sleep, with exponential backoff, and retry the LOCK operation.
4092 */
4093static unsigned long
4094nfs4_set_lock_task_retry(unsigned long timeout)
4095{
Jeff Laytond3103102011-12-01 22:44:39 +01004096 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 timeout <<= 1;
4098 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4099 return NFS4_LOCK_MAXTIMEOUT;
4100 return timeout;
4101}
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4104{
4105 struct inode *inode = state->inode;
4106 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004107 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004108 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004110 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004112 struct nfs_lockt_res res = {
4113 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 };
4115 struct rpc_message msg = {
4116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4117 .rpc_argp = &arg,
4118 .rpc_resp = &res,
4119 .rpc_cred = state->owner->so_cred,
4120 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 struct nfs4_lock_state *lsp;
4122 int status;
4123
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004124 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004125 status = nfs4_set_lock_state(state, request);
4126 if (status != 0)
4127 goto out;
4128 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004129 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004130 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004131 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004132 switch (status) {
4133 case 0:
4134 request->fl_type = F_UNLCK;
4135 break;
4136 case -NFS4ERR_DENIED:
4137 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004139 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004140out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return status;
4142}
4143
4144static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4145{
4146 struct nfs4_exception exception = { };
4147 int err;
4148
4149 do {
4150 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4151 _nfs4_proc_getlk(state, cmd, request),
4152 &exception);
4153 } while (exception.retry);
4154 return err;
4155}
4156
4157static int do_vfs_lock(struct file *file, struct file_lock *fl)
4158{
4159 int res = 0;
4160 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4161 case FL_POSIX:
4162 res = posix_lock_file_wait(file, fl);
4163 break;
4164 case FL_FLOCK:
4165 res = flock_lock_file_wait(file, fl);
4166 break;
4167 default:
4168 BUG();
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return res;
4171}
4172
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004173struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004174 struct nfs_locku_args arg;
4175 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004176 struct nfs4_lock_state *lsp;
4177 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004178 struct file_lock fl;
4179 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004180 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004181};
4182
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004183static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4184 struct nfs_open_context *ctx,
4185 struct nfs4_lock_state *lsp,
4186 struct nfs_seqid *seqid)
4187{
4188 struct nfs4_unlockdata *p;
4189 struct inode *inode = lsp->ls_state->inode;
4190
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004191 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004192 if (p == NULL)
4193 return NULL;
4194 p->arg.fh = NFS_FH(inode);
4195 p->arg.fl = &p->fl;
4196 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004197 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004198 p->arg.stateid = &lsp->ls_stateid;
4199 p->lsp = lsp;
4200 atomic_inc(&lsp->ls_count);
4201 /* Ensure we don't close file until we're done freeing locks! */
4202 p->ctx = get_nfs_open_context(ctx);
4203 memcpy(&p->fl, fl, sizeof(p->fl));
4204 p->server = NFS_SERVER(inode);
4205 return p;
4206}
4207
Trond Myklebust06f814a2006-01-03 09:55:07 +01004208static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004209{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004210 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004212 nfs4_put_lock_state(calldata->lsp);
4213 put_nfs_open_context(calldata->ctx);
4214 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004215}
4216
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004217static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004218{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004219 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004220
Trond Myklebust14516c32010-07-31 14:29:06 -04004221 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4222 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004223 switch (task->tk_status) {
4224 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004225 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4226 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004227 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004228 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004229 case -NFS4ERR_BAD_STATEID:
4230 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004231 case -NFS4ERR_STALE_STATEID:
4232 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004233 break;
4234 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004235 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004236 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004237 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004238}
4239
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004240static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004241{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004242 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004244 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004245 return;
4246 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004247 /* Note: exit _without_ running nfs4_locku_done */
4248 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004249 return;
4250 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004251 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004252 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004253 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004254 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004255 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004256 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004259static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004260 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004261 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004262 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004263};
4264
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004265static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4266 struct nfs_open_context *ctx,
4267 struct nfs4_lock_state *lsp,
4268 struct nfs_seqid *seqid)
4269{
4270 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4273 .rpc_cred = ctx->cred,
4274 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004275 struct rpc_task_setup task_setup_data = {
4276 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004277 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004278 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004279 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004280 .flags = RPC_TASK_ASYNC,
4281 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282
Frank Filz137d6ac2007-07-09 15:32:29 -07004283 /* Ensure this is an unlock - when canceling a lock, the
4284 * canceled lock is passed in, and it won't be an unlock.
4285 */
4286 fl->fl_type = F_UNLCK;
4287
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4289 if (data == NULL) {
4290 nfs_free_seqid(seqid);
4291 return ERR_PTR(-ENOMEM);
4292 }
4293
Trond Myklebust9d12b212012-01-17 22:04:25 -05004294 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004295 msg.rpc_argp = &data->arg;
4296 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004297 task_setup_data.callback_data = data;
4298 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299}
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4302{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004303 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004304 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004305 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004306 struct rpc_task *task;
4307 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004308 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Trond Myklebust9b073572006-06-29 16:38:34 -04004310 status = nfs4_set_lock_state(state, request);
4311 /* Unlock _before_ we do the RPC call */
4312 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004313 down_read(&nfsi->rwsem);
4314 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4315 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004316 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004317 }
4318 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004319 if (status != 0)
4320 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004321 /* Is this a delegated lock? */
4322 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004323 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004325 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004326 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004327 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004328 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004329 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004330 status = PTR_ERR(task);
4331 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004332 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004334 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004335out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004336 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004337 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004340struct nfs4_lockdata {
4341 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004342 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004343 struct nfs4_lock_state *lsp;
4344 struct nfs_open_context *ctx;
4345 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004346 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004347 int rpc_status;
4348 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004349 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004350};
4351
4352static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004353 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4354 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004355{
4356 struct nfs4_lockdata *p;
4357 struct inode *inode = lsp->ls_state->inode;
4358 struct nfs_server *server = NFS_SERVER(inode);
4359
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004360 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361 if (p == NULL)
4362 return NULL;
4363
4364 p->arg.fh = NFS_FH(inode);
4365 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004366 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004367 if (p->arg.open_seqid == NULL)
4368 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004369 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004370 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004371 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004373 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004374 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004375 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004376 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004377 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004378 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 atomic_inc(&lsp->ls_count);
4380 p->ctx = get_nfs_open_context(ctx);
4381 memcpy(&p->fl, fl, sizeof(p->fl));
4382 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004383out_free_seqid:
4384 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385out_free:
4386 kfree(p);
4387 return NULL;
4388}
4389
4390static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4391{
4392 struct nfs4_lockdata *data = calldata;
4393 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Harvey Harrison3110ff82008-05-02 13:42:44 -07004395 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004396 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4397 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 /* Do we need to do an open_to_lock_owner? */
4399 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004400 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4401 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004402 data->arg.open_stateid = &state->stateid;
4403 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004404 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004405 } else
4406 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004407 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004408 if (nfs4_setup_sequence(data->server,
4409 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004410 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004411 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004412 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004413 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004414}
4415
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004416static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4417{
4418 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4419 nfs4_lock_prepare(task, calldata);
4420}
4421
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4423{
4424 struct nfs4_lockdata *data = calldata;
4425
Harvey Harrison3110ff82008-05-02 13:42:44 -07004426 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427
Trond Myklebust14516c32010-07-31 14:29:06 -04004428 if (!nfs4_sequence_done(task, &data->res.seq_res))
4429 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004430
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004432 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433 if (data->rpc_status == 0)
4434 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4435 else
4436 goto out;
4437 }
4438 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004439 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004440 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004441 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004443out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004444 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004445}
4446
4447static void nfs4_lock_release(void *calldata)
4448{
4449 struct nfs4_lockdata *data = calldata;
4450
Harvey Harrison3110ff82008-05-02 13:42:44 -07004451 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004452 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453 if (data->cancelled != 0) {
4454 struct rpc_task *task;
4455 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4456 data->arg.lock_seqid);
4457 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004458 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004459 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004460 } else
4461 nfs_free_seqid(data->arg.lock_seqid);
4462 nfs4_put_lock_state(data->lsp);
4463 put_nfs_open_context(data->ctx);
4464 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004465 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004466}
4467
4468static const struct rpc_call_ops nfs4_lock_ops = {
4469 .rpc_call_prepare = nfs4_lock_prepare,
4470 .rpc_call_done = nfs4_lock_done,
4471 .rpc_release = nfs4_lock_release,
4472};
4473
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004474static const struct rpc_call_ops nfs4_recover_lock_ops = {
4475 .rpc_call_prepare = nfs4_recover_lock_prepare,
4476 .rpc_call_done = nfs4_lock_done,
4477 .rpc_release = nfs4_lock_release,
4478};
4479
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004480static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4481{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004482 switch (error) {
4483 case -NFS4ERR_ADMIN_REVOKED:
4484 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004485 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004486 if (new_lock_owner != 0 ||
4487 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004488 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004489 break;
4490 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004491 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004492 case -NFS4ERR_EXPIRED:
4493 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004494 };
4495}
4496
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004497static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004498{
4499 struct nfs4_lockdata *data;
4500 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004501 struct rpc_message msg = {
4502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4503 .rpc_cred = state->owner->so_cred,
4504 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004505 struct rpc_task_setup task_setup_data = {
4506 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004507 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004508 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004509 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004510 .flags = RPC_TASK_ASYNC,
4511 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004512 int ret;
4513
Harvey Harrison3110ff82008-05-02 13:42:44 -07004514 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004515 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004516 fl->fl_u.nfs4_fl.owner,
4517 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518 if (data == NULL)
4519 return -ENOMEM;
4520 if (IS_SETLKW(cmd))
4521 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004522 if (recovery_type > NFS_LOCK_NEW) {
4523 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004524 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004525 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4526 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004527 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004528 msg.rpc_argp = &data->arg;
4529 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004530 task_setup_data.callback_data = data;
4531 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004532 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004533 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004534 ret = nfs4_wait_for_completion_rpc_task(task);
4535 if (ret == 0) {
4536 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004537 if (ret)
4538 nfs4_handle_setlk_error(data->server, data->lsp,
4539 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004540 } else
4541 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004542 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004543 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
4547static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4548{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004549 struct nfs_server *server = NFS_SERVER(state->inode);
4550 struct nfs4_exception exception = { };
4551 int err;
4552
4553 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004554 /* Cache the lock if possible... */
4555 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4556 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004557 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004558 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004559 break;
4560 nfs4_handle_exception(server, err, &exception);
4561 } while (exception.retry);
4562 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
4565static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4566{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004567 struct nfs_server *server = NFS_SERVER(state->inode);
4568 struct nfs4_exception exception = { };
4569 int err;
4570
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004571 err = nfs4_set_lock_state(state, request);
4572 if (err != 0)
4573 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004574 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004575 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4576 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004577 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004578 switch (err) {
4579 default:
4580 goto out;
4581 case -NFS4ERR_GRACE:
4582 case -NFS4ERR_DELAY:
4583 nfs4_handle_exception(server, err, &exception);
4584 err = 0;
4585 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004586 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004587out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004588 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589}
4590
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004591#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004592static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004593{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004594 int status, ret = NFS_OK;
4595 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004596 struct nfs_server *server = NFS_SERVER(state->inode);
4597
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004598 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4599 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4600 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4601 if (status != NFS_OK) {
4602 nfs41_free_stateid(server, &lsp->ls_stateid);
4603 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4604 ret = status;
4605 }
4606 }
4607 };
4608
4609 return ret;
4610}
4611
4612static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4613{
4614 int status = NFS_OK;
4615
4616 if (test_bit(LK_STATE_IN_USE, &state->flags))
4617 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004618 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004619 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004620 return nfs4_lock_expired(state, request);
4621}
4622#endif
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4625{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004626 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004627 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004628 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004630 if ((fl_flags & FL_POSIX) &&
4631 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4632 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004633 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004634 status = nfs4_set_lock_state(state, request);
4635 if (status != 0)
4636 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004637 request->fl_flags |= FL_ACCESS;
4638 status = do_vfs_lock(request->fl_file, request);
4639 if (status < 0)
4640 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004641 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004642 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004643 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004644 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004645 request->fl_flags = fl_flags & ~FL_SLEEP;
4646 status = do_vfs_lock(request->fl_file, request);
4647 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004648 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004649 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004650 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004651 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004652 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004653 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004654 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004655 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4656 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004657out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004658 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004659out:
4660 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return status;
4662}
4663
4664static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4665{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004666 struct nfs4_exception exception = {
4667 .state = state,
4668 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 int err;
4670
4671 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004672 err = _nfs4_proc_setlk(state, cmd, request);
4673 if (err == -NFS4ERR_DENIED)
4674 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004676 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } while (exception.retry);
4678 return err;
4679}
4680
4681static int
4682nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4683{
4684 struct nfs_open_context *ctx;
4685 struct nfs4_state *state;
4686 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4687 int status;
4688
4689 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004690 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 state = ctx->state;
4692
4693 if (request->fl_start < 0 || request->fl_end < 0)
4694 return -EINVAL;
4695
Trond Myklebustd9531262009-07-21 19:22:38 -04004696 if (IS_GETLK(cmd)) {
4697 if (state != NULL)
4698 return nfs4_proc_getlk(state, F_GETLK, request);
4699 return 0;
4700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4703 return -EINVAL;
4704
Trond Myklebustd9531262009-07-21 19:22:38 -04004705 if (request->fl_type == F_UNLCK) {
4706 if (state != NULL)
4707 return nfs4_proc_unlck(state, cmd, request);
4708 return 0;
4709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Trond Myklebustd9531262009-07-21 19:22:38 -04004711 if (state == NULL)
4712 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 do {
4714 status = nfs4_proc_setlk(state, cmd, request);
4715 if ((status != -EAGAIN) || IS_SETLK(cmd))
4716 break;
4717 timeout = nfs4_set_lock_task_retry(timeout);
4718 status = -ERESTARTSYS;
4719 if (signalled())
4720 break;
4721 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 return status;
4723}
4724
Trond Myklebust888e6942005-11-04 15:38:11 -05004725int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4726{
4727 struct nfs_server *server = NFS_SERVER(state->inode);
4728 struct nfs4_exception exception = { };
4729 int err;
4730
4731 err = nfs4_set_lock_state(state, fl);
4732 if (err != 0)
4733 goto out;
4734 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004735 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004736 switch (err) {
4737 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004738 printk(KERN_ERR "NFS: %s: unhandled error "
4739 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004740 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004741 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004742 goto out;
4743 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004744 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004745 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004746 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004747 nfs4_schedule_lease_recovery(server->nfs_client);
4748 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004749 case -NFS4ERR_BADSESSION:
4750 case -NFS4ERR_BADSLOT:
4751 case -NFS4ERR_BAD_HIGH_SLOT:
4752 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4753 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004754 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004755 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004756 case -ERESTARTSYS:
4757 /*
4758 * The show must go on: exit, but mark the
4759 * stateid as needing recovery.
4760 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004761 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004762 case -NFS4ERR_ADMIN_REVOKED:
4763 case -NFS4ERR_BAD_STATEID:
4764 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004765 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004766 err = 0;
4767 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004768 case -EKEYEXPIRED:
4769 /*
4770 * User RPCSEC_GSS context has expired.
4771 * We cannot recover this stateid now, so
4772 * skip it and allow recovery thread to
4773 * proceed.
4774 */
4775 err = 0;
4776 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004777 case -ENOMEM:
4778 case -NFS4ERR_DENIED:
4779 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4780 err = 0;
4781 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004782 case -NFS4ERR_DELAY:
4783 break;
4784 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004785 err = nfs4_handle_exception(server, err, &exception);
4786 } while (exception.retry);
4787out:
4788 return err;
4789}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004790
Trond Myklebustcf470c32012-03-07 13:49:12 -05004791struct nfs_release_lockowner_data {
4792 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004793 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004794 struct nfs_release_lockowner_args args;
4795};
4796
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004797static void nfs4_release_lockowner_release(void *calldata)
4798{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004799 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004800 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004801 kfree(calldata);
4802}
4803
Trond Myklebust17280172012-03-11 13:11:00 -04004804static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004805 .rpc_release = nfs4_release_lockowner_release,
4806};
4807
Trond Myklebustcf470c32012-03-07 13:49:12 -05004808int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004809{
4810 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004811 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004812 struct rpc_message msg = {
4813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4814 };
4815
4816 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004817 return -EINVAL;
4818 data = kmalloc(sizeof(*data), GFP_NOFS);
4819 if (!data)
4820 return -ENOMEM;
4821 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004822 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004823 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4824 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4825 data->args.lock_owner.s_dev = server->s_dev;
4826 msg.rpc_argp = &data->args;
4827 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4828 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004829}
4830
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004831#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4832
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004833static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4834 const void *buf, size_t buflen,
4835 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004836{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004837 if (strcmp(key, "") != 0)
4838 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004839
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004840 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004841}
4842
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004843static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4844 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004845{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004846 if (strcmp(key, "") != 0)
4847 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004848
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004849 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004850}
4851
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004852static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4853 size_t list_len, const char *name,
4854 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004855{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004856 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004857
J. Bruce Fields096455a2006-03-20 23:23:42 -05004858 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4859 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004860
4861 if (list && len <= list_len)
4862 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004863 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004864}
4865
Andy Adamson533eb462011-06-13 18:25:56 -04004866/*
4867 * nfs_fhget will use either the mounted_on_fileid or the fileid
4868 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004869static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4870{
Andy Adamson533eb462011-06-13 18:25:56 -04004871 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4872 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4873 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004874 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004875 return;
4876
4877 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004878 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004879 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4880 fattr->nlink = 2;
4881}
4882
Trond Myklebust56659e92007-07-17 21:52:39 -04004883int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004884 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004885{
4886 struct nfs_server *server = NFS_SERVER(dir);
4887 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004888 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004889 };
4890 struct nfs4_fs_locations_arg args = {
4891 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004892 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004893 .page = page,
4894 .bitmask = bitmask,
4895 };
Benny Halevy22958462009-04-01 09:22:02 -04004896 struct nfs4_fs_locations_res res = {
4897 .fs_locations = fs_locations,
4898 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004899 struct rpc_message msg = {
4900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4901 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004902 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004903 };
4904 int status;
4905
Harvey Harrison3110ff82008-05-02 13:42:44 -07004906 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004907
4908 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4909 * is not supported */
4910 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4911 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4912 else
4913 bitmask[0] |= FATTR4_WORD0_FILEID;
4914
Trond Myklebustc228fd32007-01-13 02:28:11 -05004915 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004916 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004917 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004918 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004919 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004920 return status;
4921}
4922
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004923static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4924{
4925 int status;
4926 struct nfs4_secinfo_arg args = {
4927 .dir_fh = NFS_FH(dir),
4928 .name = name,
4929 };
4930 struct nfs4_secinfo_res res = {
4931 .flavors = flavors,
4932 };
4933 struct rpc_message msg = {
4934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4935 .rpc_argp = &args,
4936 .rpc_resp = &res,
4937 };
4938
4939 dprintk("NFS call secinfo %s\n", name->name);
4940 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4941 dprintk("NFS reply secinfo: %d\n", status);
4942 return status;
4943}
4944
Trond Myklebust17280172012-03-11 13:11:00 -04004945static int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
4946 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004947{
4948 struct nfs4_exception exception = { };
4949 int err;
4950 do {
4951 err = nfs4_handle_exception(NFS_SERVER(dir),
4952 _nfs4_proc_secinfo(dir, name, flavors),
4953 &exception);
4954 } while (exception.retry);
4955 return err;
4956}
4957
Andy Adamson557134a2009-04-01 09:21:53 -04004958#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004959/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004960 * Check the exchange flags returned by the server for invalid flags, having
4961 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4962 * DS flags set.
4963 */
4964static int nfs4_check_cl_exchange_flags(u32 flags)
4965{
4966 if (flags & ~EXCHGID4_FLAG_MASK_R)
4967 goto out_inval;
4968 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4969 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4970 goto out_inval;
4971 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4972 goto out_inval;
4973 return NFS_OK;
4974out_inval:
4975 return -NFS4ERR_INVAL;
4976}
4977
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004978static bool
4979nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4980{
4981 if (a->server_scope_sz == b->server_scope_sz &&
4982 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4983 return true;
4984
4985 return false;
4986}
4987
Andy Adamson357f54d2010-12-14 10:11:57 -05004988/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004989 * nfs4_proc_exchange_id()
4990 *
4991 * Since the clientid has expired, all compounds using sessions
4992 * associated with the stale clientid will be returning
4993 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4994 * be in some phase of session reset.
4995 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004996int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004997{
4998 nfs4_verifier verifier;
4999 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005000 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005001 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005002 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005003 };
5004 struct nfs41_exchange_id_res res = {
5005 .client = clp,
5006 };
5007 int status;
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5010 .rpc_argp = &args,
5011 .rpc_resp = &res,
5012 .rpc_cred = cred,
5013 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005014
5015 dprintk("--> %s\n", __func__);
5016 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005017
Chuck Levercd937102012-03-02 17:14:31 -05005018 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005019
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005020 args.id_len = scnprintf(args.id, sizeof(args.id),
5021 "%s/%s.%s/%u",
5022 clp->cl_ipaddr,
5023 init_utsname()->nodename,
5024 init_utsname()->domainname,
5025 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005026
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005027 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005028 if (unlikely(!res.server_scope)) {
5029 status = -ENOMEM;
5030 goto out;
5031 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005032
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005033 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5034 if (unlikely(!res.impl_id)) {
5035 status = -ENOMEM;
5036 goto out_server_scope;
5037 }
5038
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005039 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005040 if (!status)
5041 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005042
5043 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005044 /* use the most recent implementation id */
5045 kfree(clp->impl_id);
5046 clp->impl_id = res.impl_id;
5047 } else
5048 kfree(res.impl_id);
5049
5050 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005051 if (clp->server_scope &&
5052 !nfs41_same_server_scope(clp->server_scope,
5053 res.server_scope)) {
5054 dprintk("%s: server_scope mismatch detected\n",
5055 __func__);
5056 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5057 kfree(clp->server_scope);
5058 clp->server_scope = NULL;
5059 }
5060
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005061 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005062 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005063 goto out;
5064 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005065 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005066
5067out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05005068 kfree(res.server_scope);
5069out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005070 if (clp->impl_id)
5071 dprintk("%s: Server Implementation ID: "
5072 "domain: %s, name: %s, date: %llu,%u\n",
5073 __func__, clp->impl_id->domain, clp->impl_id->name,
5074 clp->impl_id->date.seconds,
5075 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005076 dprintk("<-- %s status= %d\n", __func__, status);
5077 return status;
5078}
5079
Andy Adamson2050f0c2009-04-01 09:22:30 -04005080struct nfs4_get_lease_time_data {
5081 struct nfs4_get_lease_time_args *args;
5082 struct nfs4_get_lease_time_res *res;
5083 struct nfs_client *clp;
5084};
5085
5086static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5087 void *calldata)
5088{
5089 int ret;
5090 struct nfs4_get_lease_time_data *data =
5091 (struct nfs4_get_lease_time_data *)calldata;
5092
5093 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005094 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005095 /* just setup sequence, do not trigger session recovery
5096 since we're invoked within one */
5097 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005098 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005099 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005100
5101 BUG_ON(ret == -EAGAIN);
5102 rpc_call_start(task);
5103 dprintk("<-- %s\n", __func__);
5104}
5105
5106/*
5107 * Called from nfs4_state_manager thread for session setup, so don't recover
5108 * from sequence operation or clientid errors.
5109 */
5110static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5111{
5112 struct nfs4_get_lease_time_data *data =
5113 (struct nfs4_get_lease_time_data *)calldata;
5114
5115 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005116 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5117 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005118 switch (task->tk_status) {
5119 case -NFS4ERR_DELAY:
5120 case -NFS4ERR_GRACE:
5121 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5122 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5123 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005124 /* fall through */
5125 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005126 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005127 return;
5128 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005129 dprintk("<-- %s\n", __func__);
5130}
5131
Trond Myklebust17280172012-03-11 13:11:00 -04005132static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005133 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5134 .rpc_call_done = nfs4_get_lease_time_done,
5135};
5136
5137int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5138{
5139 struct rpc_task *task;
5140 struct nfs4_get_lease_time_args args;
5141 struct nfs4_get_lease_time_res res = {
5142 .lr_fsinfo = fsinfo,
5143 };
5144 struct nfs4_get_lease_time_data data = {
5145 .args = &args,
5146 .res = &res,
5147 .clp = clp,
5148 };
5149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5151 .rpc_argp = &args,
5152 .rpc_resp = &res,
5153 };
5154 struct rpc_task_setup task_setup = {
5155 .rpc_client = clp->cl_rpcclient,
5156 .rpc_message = &msg,
5157 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005158 .callback_data = &data,
5159 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005160 };
5161 int status;
5162
Trond Myklebust9d12b212012-01-17 22:04:25 -05005163 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005164 dprintk("--> %s\n", __func__);
5165 task = rpc_run_task(&task_setup);
5166
5167 if (IS_ERR(task))
5168 status = PTR_ERR(task);
5169 else {
5170 status = task->tk_status;
5171 rpc_put_task(task);
5172 }
5173 dprintk("<-- %s return %d\n", __func__, status);
5174
5175 return status;
5176}
5177
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005178static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5179{
5180 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5181}
5182
5183static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5184 struct nfs4_slot *new,
5185 u32 max_slots,
5186 u32 ivalue)
5187{
5188 struct nfs4_slot *old = NULL;
5189 u32 i;
5190
5191 spin_lock(&tbl->slot_tbl_lock);
5192 if (new) {
5193 old = tbl->slots;
5194 tbl->slots = new;
5195 tbl->max_slots = max_slots;
5196 }
5197 tbl->highest_used_slotid = -1; /* no slot is currently used */
5198 for (i = 0; i < tbl->max_slots; i++)
5199 tbl->slots[i].seq_nr = ivalue;
5200 spin_unlock(&tbl->slot_tbl_lock);
5201 kfree(old);
5202}
5203
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005204/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005205 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005206 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005207static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5208 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005209{
Andy Adamson104aeba2010-01-14 17:45:10 -05005210 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005211 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005212
Andy Adamson104aeba2010-01-14 17:45:10 -05005213 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5214 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005215
Andy Adamson104aeba2010-01-14 17:45:10 -05005216 /* Does the newly negotiated max_reqs match the existing slot table? */
5217 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005218 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005219 if (!new)
5220 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005221 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005222 ret = 0;
5223
5224 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005225 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5226 tbl, tbl->slots, tbl->max_slots);
5227out:
5228 dprintk("<-- %s: return %d\n", __func__, ret);
5229 return ret;
5230}
5231
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005232/* Destroy the slot table */
5233static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5234{
5235 if (session->fc_slot_table.slots != NULL) {
5236 kfree(session->fc_slot_table.slots);
5237 session->fc_slot_table.slots = NULL;
5238 }
5239 if (session->bc_slot_table.slots != NULL) {
5240 kfree(session->bc_slot_table.slots);
5241 session->bc_slot_table.slots = NULL;
5242 }
5243 return;
5244}
5245
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005246/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005247 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005248 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005249static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005250{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005251 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005252 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005253
Andy Adamsonaacd5532011-11-09 13:58:21 -05005254 dprintk("--> %s\n", __func__);
5255 /* Fore channel */
5256 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005257 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5258 if (status) /* -ENOMEM */
5259 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005260 /* Back channel */
5261 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005262 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5263 if (status && tbl->slots == NULL)
5264 /* Fore and back channel share a connection so get
5265 * both slot tables or neither */
5266 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005267 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005268}
5269
5270struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5271{
5272 struct nfs4_session *session;
5273 struct nfs4_slot_table *tbl;
5274
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005275 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005276 if (!session)
5277 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005278
Andy Adamson557134a2009-04-01 09:21:53 -04005279 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005280 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005281 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005282 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005283 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005284
5285 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005286 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005287 spin_lock_init(&tbl->slot_tbl_lock);
5288 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005289 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005290
Trond Myklebust1055d762010-06-16 09:52:27 -04005291 session->session_state = 1<<NFS4_SESSION_INITING;
5292
Andy Adamson557134a2009-04-01 09:21:53 -04005293 session->clp = clp;
5294 return session;
5295}
5296
5297void nfs4_destroy_session(struct nfs4_session *session)
5298{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005299 struct rpc_xprt *xprt;
5300
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005301 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005302
5303 rcu_read_lock();
5304 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5305 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005306 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005307 __func__, xprt);
5308 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005309 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005310 kfree(session);
5311}
5312
Andy Adamsonfc931582009-04-01 09:22:31 -04005313/*
5314 * Initialize the values to be used by the client in CREATE_SESSION
5315 * If nfs4_init_session set the fore channel request and response sizes,
5316 * use them.
5317 *
5318 * Set the back channel max_resp_sz_cached to zero to force the client to
5319 * always set csa_cachethis to FALSE because the current implementation
5320 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5321 */
5322static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5323{
5324 struct nfs4_session *session = args->client->cl_session;
5325 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5326 mxresp_sz = session->fc_attrs.max_resp_sz;
5327
5328 if (mxrqst_sz == 0)
5329 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5330 if (mxresp_sz == 0)
5331 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5332 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005333 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5334 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005335 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005336 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005337
5338 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005339 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005340 __func__,
5341 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005342 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005343
5344 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005345 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5346 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5347 args->bc_attrs.max_resp_sz_cached = 0;
5348 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5349 args->bc_attrs.max_reqs = 1;
5350
5351 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5352 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5353 __func__,
5354 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5355 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5356 args->bc_attrs.max_reqs);
5357}
5358
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005359static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005360{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005361 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5362 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5363
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005364 if (rcvd->max_resp_sz > sent->max_resp_sz)
5365 return -EINVAL;
5366 /*
5367 * Our requested max_ops is the minimum we need; we're not
5368 * prepared to break up compounds into smaller pieces than that.
5369 * So, no point even trying to continue if the server won't
5370 * cooperate:
5371 */
5372 if (rcvd->max_ops < sent->max_ops)
5373 return -EINVAL;
5374 if (rcvd->max_reqs == 0)
5375 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005376 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5377 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005378 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005379}
5380
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005381static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5382{
5383 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5384 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005385
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005386 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5387 return -EINVAL;
5388 if (rcvd->max_resp_sz < sent->max_resp_sz)
5389 return -EINVAL;
5390 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5391 return -EINVAL;
5392 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005393 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005394 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005395 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005396 return -EINVAL;
5397 return 0;
5398}
Andy Adamson8d353012009-04-01 09:22:32 -04005399
Andy Adamson8d353012009-04-01 09:22:32 -04005400static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5401 struct nfs4_session *session)
5402{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005403 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005404
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005405 ret = nfs4_verify_fore_channel_attrs(args, session);
5406 if (ret)
5407 return ret;
5408 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005409}
5410
Andy Adamsonfc931582009-04-01 09:22:31 -04005411static int _nfs4_proc_create_session(struct nfs_client *clp)
5412{
5413 struct nfs4_session *session = clp->cl_session;
5414 struct nfs41_create_session_args args = {
5415 .client = clp,
5416 .cb_program = NFS4_CALLBACK,
5417 };
5418 struct nfs41_create_session_res res = {
5419 .client = clp,
5420 };
5421 struct rpc_message msg = {
5422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5423 .rpc_argp = &args,
5424 .rpc_resp = &res,
5425 };
5426 int status;
5427
5428 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005429 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005430
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005431 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005432
Andy Adamson8d353012009-04-01 09:22:32 -04005433 if (!status)
5434 /* Verify the session's negotiated channel_attrs values */
5435 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005436 if (!status) {
5437 /* Increment the clientid slot sequence id */
5438 clp->cl_seqid++;
5439 }
5440
5441 return status;
5442}
5443
5444/*
5445 * Issues a CREATE_SESSION operation to the server.
5446 * It is the responsibility of the caller to verify the session is
5447 * expired before calling this routine.
5448 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005449int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005450{
5451 int status;
5452 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005453 struct nfs4_session *session = clp->cl_session;
5454
5455 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5456
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005457 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005458 if (status)
5459 goto out;
5460
Andy Adamsonaacd5532011-11-09 13:58:21 -05005461 /* Init or reset the session slot tables */
5462 status = nfs4_setup_session_slot_tables(session);
5463 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005464 if (status)
5465 goto out;
5466
5467 ptr = (unsigned *)&session->sess_id.data[0];
5468 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5469 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005470out:
5471 dprintk("<-- %s\n", __func__);
5472 return status;
5473}
5474
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005475/*
5476 * Issue the over-the-wire RPC DESTROY_SESSION.
5477 * The caller must serialize access to this routine.
5478 */
5479int nfs4_proc_destroy_session(struct nfs4_session *session)
5480{
5481 int status = 0;
5482 struct rpc_message msg;
5483
5484 dprintk("--> nfs4_proc_destroy_session\n");
5485
5486 /* session is still being setup */
5487 if (session->clp->cl_cons_state != NFS_CS_READY)
5488 return status;
5489
5490 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5491 msg.rpc_argp = session;
5492 msg.rpc_resp = NULL;
5493 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005494 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005495
5496 if (status)
5497 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005498 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005499 "Session has been destroyed regardless...\n", status);
5500
5501 dprintk("<-- nfs4_proc_destroy_session\n");
5502 return status;
5503}
5504
Trond Myklebustfccba802009-07-21 16:48:07 -04005505int nfs4_init_session(struct nfs_server *server)
5506{
5507 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005508 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005509 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005510 int ret;
5511
5512 if (!nfs4_has_session(clp))
5513 return 0;
5514
Trond Myklebust1055d762010-06-16 09:52:27 -04005515 session = clp->cl_session;
5516 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5517 return 0;
5518
Andy Adamson68bf05e2009-12-15 12:55:02 -05005519 rsize = server->rsize;
5520 if (rsize == 0)
5521 rsize = NFS_MAX_FILE_IO_SIZE;
5522 wsize = server->wsize;
5523 if (wsize == 0)
5524 wsize = NFS_MAX_FILE_IO_SIZE;
5525
Andy Adamson68bf05e2009-12-15 12:55:02 -05005526 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5527 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005528
Trond Myklebustfccba802009-07-21 16:48:07 -04005529 ret = nfs4_recover_expired_lease(server);
5530 if (!ret)
5531 ret = nfs4_check_client_ready(clp);
5532 return ret;
5533}
5534
Andy Adamsond83217c2011-03-01 01:34:17 +00005535int nfs4_init_ds_session(struct nfs_client *clp)
5536{
5537 struct nfs4_session *session = clp->cl_session;
5538 int ret;
5539
5540 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5541 return 0;
5542
5543 ret = nfs4_client_recover_expired_lease(clp);
5544 if (!ret)
5545 /* Test for the DS role */
5546 if (!is_ds_client(clp))
5547 ret = -ENODEV;
5548 if (!ret)
5549 ret = nfs4_check_client_ready(clp);
5550 return ret;
5551
5552}
5553EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5554
5555
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005556/*
5557 * Renew the cl_session lease.
5558 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005559struct nfs4_sequence_data {
5560 struct nfs_client *clp;
5561 struct nfs4_sequence_args args;
5562 struct nfs4_sequence_res res;
5563};
5564
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005565static void nfs41_sequence_release(void *data)
5566{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005567 struct nfs4_sequence_data *calldata = data;
5568 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005569
Alexandros Batsakis71358402010-02-05 03:45:05 -08005570 if (atomic_read(&clp->cl_count) > 1)
5571 nfs4_schedule_state_renewal(clp);
5572 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005573 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005574}
5575
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005576static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5577{
5578 switch(task->tk_status) {
5579 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005580 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5581 return -EAGAIN;
5582 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005583 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005584 }
5585 return 0;
5586}
5587
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005588static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005589{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005590 struct nfs4_sequence_data *calldata = data;
5591 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005592
Trond Myklebust14516c32010-07-31 14:29:06 -04005593 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5594 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005595
5596 if (task->tk_status < 0) {
5597 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005598 if (atomic_read(&clp->cl_count) == 1)
5599 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005600
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005601 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5602 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005603 return;
5604 }
5605 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005606 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005607out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005608 dprintk("<-- %s\n", __func__);
5609}
5610
5611static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5612{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005613 struct nfs4_sequence_data *calldata = data;
5614 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005615 struct nfs4_sequence_args *args;
5616 struct nfs4_sequence_res *res;
5617
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005618 args = task->tk_msg.rpc_argp;
5619 res = task->tk_msg.rpc_resp;
5620
Trond Myklebust9d12b212012-01-17 22:04:25 -05005621 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005622 return;
5623 rpc_call_start(task);
5624}
5625
5626static const struct rpc_call_ops nfs41_sequence_ops = {
5627 .rpc_call_done = nfs41_sequence_call_done,
5628 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005629 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005630};
5631
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005632static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005633{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005634 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005635 struct rpc_message msg = {
5636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5637 .rpc_cred = cred,
5638 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005639 struct rpc_task_setup task_setup_data = {
5640 .rpc_client = clp->cl_rpcclient,
5641 .rpc_message = &msg,
5642 .callback_ops = &nfs41_sequence_ops,
5643 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5644 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005645
Alexandros Batsakis71358402010-02-05 03:45:05 -08005646 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005647 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005648 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005649 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005650 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005651 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005652 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005653 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005654 msg.rpc_argp = &calldata->args;
5655 msg.rpc_resp = &calldata->res;
5656 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005657 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005658
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005659 return rpc_run_task(&task_setup_data);
5660}
5661
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005662static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005663{
5664 struct rpc_task *task;
5665 int ret = 0;
5666
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005667 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5668 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005669 task = _nfs41_proc_sequence(clp, cred);
5670 if (IS_ERR(task))
5671 ret = PTR_ERR(task);
5672 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005673 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005674 dprintk("<-- %s status=%d\n", __func__, ret);
5675 return ret;
5676}
5677
5678static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5679{
5680 struct rpc_task *task;
5681 int ret;
5682
5683 task = _nfs41_proc_sequence(clp, cred);
5684 if (IS_ERR(task)) {
5685 ret = PTR_ERR(task);
5686 goto out;
5687 }
5688 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005689 if (!ret) {
5690 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5691
5692 if (task->tk_status == 0)
5693 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005694 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005695 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005696 rpc_put_task(task);
5697out:
5698 dprintk("<-- %s status=%d\n", __func__, ret);
5699 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005700}
5701
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005702struct nfs4_reclaim_complete_data {
5703 struct nfs_client *clp;
5704 struct nfs41_reclaim_complete_args arg;
5705 struct nfs41_reclaim_complete_res res;
5706};
5707
5708static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5709{
5710 struct nfs4_reclaim_complete_data *calldata = data;
5711
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005712 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005713 if (nfs41_setup_sequence(calldata->clp->cl_session,
5714 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005715 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005716 return;
5717
5718 rpc_call_start(task);
5719}
5720
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005721static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5722{
5723 switch(task->tk_status) {
5724 case 0:
5725 case -NFS4ERR_COMPLETE_ALREADY:
5726 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5727 break;
5728 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005729 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005730 /* fall through */
5731 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005732 return -EAGAIN;
5733 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005734 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005735 }
5736 return 0;
5737}
5738
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005739static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5740{
5741 struct nfs4_reclaim_complete_data *calldata = data;
5742 struct nfs_client *clp = calldata->clp;
5743 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5744
5745 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005746 if (!nfs41_sequence_done(task, res))
5747 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005748
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005749 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5750 rpc_restart_call_prepare(task);
5751 return;
5752 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005753 dprintk("<-- %s\n", __func__);
5754}
5755
5756static void nfs4_free_reclaim_complete_data(void *data)
5757{
5758 struct nfs4_reclaim_complete_data *calldata = data;
5759
5760 kfree(calldata);
5761}
5762
5763static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5764 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5765 .rpc_call_done = nfs4_reclaim_complete_done,
5766 .rpc_release = nfs4_free_reclaim_complete_data,
5767};
5768
5769/*
5770 * Issue a global reclaim complete.
5771 */
5772static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5773{
5774 struct nfs4_reclaim_complete_data *calldata;
5775 struct rpc_task *task;
5776 struct rpc_message msg = {
5777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5778 };
5779 struct rpc_task_setup task_setup_data = {
5780 .rpc_client = clp->cl_rpcclient,
5781 .rpc_message = &msg,
5782 .callback_ops = &nfs4_reclaim_complete_call_ops,
5783 .flags = RPC_TASK_ASYNC,
5784 };
5785 int status = -ENOMEM;
5786
5787 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005788 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005789 if (calldata == NULL)
5790 goto out;
5791 calldata->clp = clp;
5792 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005793
Trond Myklebust9d12b212012-01-17 22:04:25 -05005794 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005795 msg.rpc_argp = &calldata->arg;
5796 msg.rpc_resp = &calldata->res;
5797 task_setup_data.callback_data = calldata;
5798 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005799 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005800 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005801 goto out;
5802 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005803 status = nfs4_wait_for_completion_rpc_task(task);
5804 if (status == 0)
5805 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005806 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005807 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005808out:
5809 dprintk("<-- %s status=%d\n", __func__, status);
5810 return status;
5811}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005812
5813static void
5814nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5815{
5816 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005817 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005818
5819 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005820 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5821 * right now covering the LAYOUTGET we are about to send.
5822 * However, that is not so catastrophic, and there seems
5823 * to be no way to prevent it completely.
5824 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005825 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005826 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005827 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005828 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5829 NFS_I(lgp->args.inode)->layout,
5830 lgp->args.ctx->state)) {
5831 rpc_exit(task, NFS4_OK);
5832 return;
5833 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005834 rpc_call_start(task);
5835}
5836
5837static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5838{
5839 struct nfs4_layoutget *lgp = calldata;
5840 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5841
5842 dprintk("--> %s\n", __func__);
5843
5844 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5845 return;
5846
5847 switch (task->tk_status) {
5848 case 0:
5849 break;
5850 case -NFS4ERR_LAYOUTTRYLATER:
5851 case -NFS4ERR_RECALLCONFLICT:
5852 task->tk_status = -NFS4ERR_DELAY;
5853 /* Fall through */
5854 default:
5855 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5856 rpc_restart_call_prepare(task);
5857 return;
5858 }
5859 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005860 dprintk("<-- %s\n", __func__);
5861}
5862
5863static void nfs4_layoutget_release(void *calldata)
5864{
5865 struct nfs4_layoutget *lgp = calldata;
5866
5867 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005868 put_nfs_open_context(lgp->args.ctx);
5869 kfree(calldata);
5870 dprintk("<-- %s\n", __func__);
5871}
5872
5873static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5874 .rpc_call_prepare = nfs4_layoutget_prepare,
5875 .rpc_call_done = nfs4_layoutget_done,
5876 .rpc_release = nfs4_layoutget_release,
5877};
5878
5879int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5880{
5881 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5882 struct rpc_task *task;
5883 struct rpc_message msg = {
5884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5885 .rpc_argp = &lgp->args,
5886 .rpc_resp = &lgp->res,
5887 };
5888 struct rpc_task_setup task_setup_data = {
5889 .rpc_client = server->client,
5890 .rpc_message = &msg,
5891 .callback_ops = &nfs4_layoutget_call_ops,
5892 .callback_data = lgp,
5893 .flags = RPC_TASK_ASYNC,
5894 };
5895 int status = 0;
5896
5897 dprintk("--> %s\n", __func__);
5898
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005899 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005900 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005901 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005902 task = rpc_run_task(&task_setup_data);
5903 if (IS_ERR(task))
5904 return PTR_ERR(task);
5905 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005906 if (status == 0)
5907 status = task->tk_status;
5908 if (status == 0)
5909 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005910 rpc_put_task(task);
5911 dprintk("<-- %s status=%d\n", __func__, status);
5912 return status;
5913}
5914
Benny Halevycbe82602011-05-22 19:52:37 +03005915static void
5916nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5917{
5918 struct nfs4_layoutreturn *lrp = calldata;
5919
5920 dprintk("--> %s\n", __func__);
5921 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005922 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005923 return;
5924 rpc_call_start(task);
5925}
5926
5927static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5928{
5929 struct nfs4_layoutreturn *lrp = calldata;
5930 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005931 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005932
5933 dprintk("--> %s\n", __func__);
5934
5935 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5936 return;
5937
5938 server = NFS_SERVER(lrp->args.inode);
5939 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005940 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005941 return;
5942 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005943 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005944 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005945 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005946 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005947 } else
5948 BUG_ON(!list_empty(&lo->plh_segs));
5949 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005950 lo->plh_block_lgets--;
5951 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005952 dprintk("<-- %s\n", __func__);
5953}
5954
5955static void nfs4_layoutreturn_release(void *calldata)
5956{
5957 struct nfs4_layoutreturn *lrp = calldata;
5958
5959 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005960 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005961 kfree(calldata);
5962 dprintk("<-- %s\n", __func__);
5963}
5964
5965static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5966 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5967 .rpc_call_done = nfs4_layoutreturn_done,
5968 .rpc_release = nfs4_layoutreturn_release,
5969};
5970
5971int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5972{
5973 struct rpc_task *task;
5974 struct rpc_message msg = {
5975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5976 .rpc_argp = &lrp->args,
5977 .rpc_resp = &lrp->res,
5978 };
5979 struct rpc_task_setup task_setup_data = {
5980 .rpc_client = lrp->clp->cl_rpcclient,
5981 .rpc_message = &msg,
5982 .callback_ops = &nfs4_layoutreturn_call_ops,
5983 .callback_data = lrp,
5984 };
5985 int status;
5986
5987 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005988 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005989 task = rpc_run_task(&task_setup_data);
5990 if (IS_ERR(task))
5991 return PTR_ERR(task);
5992 status = task->tk_status;
5993 dprintk("<-- %s status=%d\n", __func__, status);
5994 rpc_put_task(task);
5995 return status;
5996}
5997
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005998/*
5999 * Retrieve the list of Data Server devices from the MDS.
6000 */
6001static int _nfs4_getdevicelist(struct nfs_server *server,
6002 const struct nfs_fh *fh,
6003 struct pnfs_devicelist *devlist)
6004{
6005 struct nfs4_getdevicelist_args args = {
6006 .fh = fh,
6007 .layoutclass = server->pnfs_curr_ld->id,
6008 };
6009 struct nfs4_getdevicelist_res res = {
6010 .devlist = devlist,
6011 };
6012 struct rpc_message msg = {
6013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6014 .rpc_argp = &args,
6015 .rpc_resp = &res,
6016 };
6017 int status;
6018
6019 dprintk("--> %s\n", __func__);
6020 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6021 &res.seq_res, 0);
6022 dprintk("<-- %s status=%d\n", __func__, status);
6023 return status;
6024}
6025
6026int nfs4_proc_getdevicelist(struct nfs_server *server,
6027 const struct nfs_fh *fh,
6028 struct pnfs_devicelist *devlist)
6029{
6030 struct nfs4_exception exception = { };
6031 int err;
6032
6033 do {
6034 err = nfs4_handle_exception(server,
6035 _nfs4_getdevicelist(server, fh, devlist),
6036 &exception);
6037 } while (exception.retry);
6038
6039 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6040 err, devlist->num_devs);
6041
6042 return err;
6043}
6044EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6045
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006046static int
6047_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6048{
6049 struct nfs4_getdeviceinfo_args args = {
6050 .pdev = pdev,
6051 };
6052 struct nfs4_getdeviceinfo_res res = {
6053 .pdev = pdev,
6054 };
6055 struct rpc_message msg = {
6056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6057 .rpc_argp = &args,
6058 .rpc_resp = &res,
6059 };
6060 int status;
6061
6062 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006063 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006064 dprintk("<-- %s status=%d\n", __func__, status);
6065
6066 return status;
6067}
6068
6069int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6070{
6071 struct nfs4_exception exception = { };
6072 int err;
6073
6074 do {
6075 err = nfs4_handle_exception(server,
6076 _nfs4_proc_getdeviceinfo(server, pdev),
6077 &exception);
6078 } while (exception.retry);
6079 return err;
6080}
6081EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6082
Andy Adamson863a3c62011-03-23 13:27:54 +00006083static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6084{
6085 struct nfs4_layoutcommit_data *data = calldata;
6086 struct nfs_server *server = NFS_SERVER(data->args.inode);
6087
6088 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006089 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006090 return;
6091 rpc_call_start(task);
6092}
6093
6094static void
6095nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6096{
6097 struct nfs4_layoutcommit_data *data = calldata;
6098 struct nfs_server *server = NFS_SERVER(data->args.inode);
6099
6100 if (!nfs4_sequence_done(task, &data->res.seq_res))
6101 return;
6102
6103 switch (task->tk_status) { /* Just ignore these failures */
6104 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6105 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6106 case NFS4ERR_BADLAYOUT: /* no layout */
6107 case NFS4ERR_GRACE: /* loca_recalim always false */
6108 task->tk_status = 0;
6109 }
6110
6111 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006112 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006113 return;
6114 }
6115
6116 if (task->tk_status == 0)
6117 nfs_post_op_update_inode_force_wcc(data->args.inode,
6118 data->res.fattr);
6119}
6120
6121static void nfs4_layoutcommit_release(void *calldata)
6122{
6123 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006124 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006125 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006126
Andy Adamsondb29c082011-07-30 20:52:38 -04006127 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006128 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006129 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6130 list_del_init(&lseg->pls_lc_list);
6131 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6132 &lseg->pls_flags))
6133 put_lseg(lseg);
6134 }
Peng Tao92407e72011-10-23 20:21:17 -07006135
6136 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6137 smp_mb__after_clear_bit();
6138 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6139
Andy Adamson863a3c62011-03-23 13:27:54 +00006140 put_rpccred(data->cred);
6141 kfree(data);
6142}
6143
6144static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6145 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6146 .rpc_call_done = nfs4_layoutcommit_done,
6147 .rpc_release = nfs4_layoutcommit_release,
6148};
6149
6150int
Andy Adamsonef311532011-03-12 02:58:10 -05006151nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006152{
6153 struct rpc_message msg = {
6154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6155 .rpc_argp = &data->args,
6156 .rpc_resp = &data->res,
6157 .rpc_cred = data->cred,
6158 };
6159 struct rpc_task_setup task_setup_data = {
6160 .task = &data->task,
6161 .rpc_client = NFS_CLIENT(data->args.inode),
6162 .rpc_message = &msg,
6163 .callback_ops = &nfs4_layoutcommit_ops,
6164 .callback_data = data,
6165 .flags = RPC_TASK_ASYNC,
6166 };
6167 struct rpc_task *task;
6168 int status = 0;
6169
6170 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6171 "lbw: %llu inode %lu\n",
6172 data->task.tk_pid, sync,
6173 data->args.lastbytewritten,
6174 data->args.inode->i_ino);
6175
Trond Myklebust9d12b212012-01-17 22:04:25 -05006176 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006177 task = rpc_run_task(&task_setup_data);
6178 if (IS_ERR(task))
6179 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006180 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006181 goto out;
6182 status = nfs4_wait_for_completion_rpc_task(task);
6183 if (status != 0)
6184 goto out;
6185 status = task->tk_status;
6186out:
6187 dprintk("%s: status %d\n", __func__, status);
6188 rpc_put_task(task);
6189 return status;
6190}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006191
6192static int
6193_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6194 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6195{
6196 struct nfs41_secinfo_no_name_args args = {
6197 .style = SECINFO_STYLE_CURRENT_FH,
6198 };
6199 struct nfs4_secinfo_res res = {
6200 .flavors = flavors,
6201 };
6202 struct rpc_message msg = {
6203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6204 .rpc_argp = &args,
6205 .rpc_resp = &res,
6206 };
6207 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6208}
6209
6210static int
6211nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6212 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6213{
6214 struct nfs4_exception exception = { };
6215 int err;
6216 do {
6217 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6218 switch (err) {
6219 case 0:
6220 case -NFS4ERR_WRONGSEC:
6221 case -NFS4ERR_NOTSUPP:
6222 break;
6223 default:
6224 err = nfs4_handle_exception(server, err, &exception);
6225 }
6226 } while (exception.retry);
6227 return err;
6228}
6229
6230static int
6231nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6232 struct nfs_fsinfo *info)
6233{
6234 int err;
6235 struct page *page;
6236 rpc_authflavor_t flavor;
6237 struct nfs4_secinfo_flavors *flavors;
6238
6239 page = alloc_page(GFP_KERNEL);
6240 if (!page) {
6241 err = -ENOMEM;
6242 goto out;
6243 }
6244
6245 flavors = page_address(page);
6246 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6247
6248 /*
6249 * Fall back on "guess and check" method if
6250 * the server doesn't support SECINFO_NO_NAME
6251 */
6252 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6253 err = nfs4_find_root_sec(server, fhandle, info);
6254 goto out_freepage;
6255 }
6256 if (err)
6257 goto out_freepage;
6258
6259 flavor = nfs_find_best_sec(flavors);
6260 if (err == 0)
6261 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6262
6263out_freepage:
6264 put_page(page);
6265 if (err == -EACCES)
6266 return -EPERM;
6267out:
6268 return err;
6269}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006270
6271static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006272{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006273 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006274 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006275 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006276 };
6277 struct nfs41_test_stateid_res res;
6278 struct rpc_message msg = {
6279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6280 .rpc_argp = &args,
6281 .rpc_resp = &res,
6282 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006283
Trond Myklebust9d12b212012-01-17 22:04:25 -05006284 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006285 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6286
6287 if (status == NFS_OK)
6288 return res.status;
6289 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006290}
6291
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006292static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006293{
6294 struct nfs4_exception exception = { };
6295 int err;
6296 do {
6297 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006298 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006299 &exception);
6300 } while (exception.retry);
6301 return err;
6302}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006303
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006304static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006305{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006306 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006307 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006308 };
6309 struct nfs41_free_stateid_res res;
6310 struct rpc_message msg = {
6311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6312 .rpc_argp = &args,
6313 .rpc_resp = &res,
6314 };
6315
Trond Myklebust9d12b212012-01-17 22:04:25 -05006316 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6317 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006318}
6319
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006320static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006321{
6322 struct nfs4_exception exception = { };
6323 int err;
6324 do {
6325 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006326 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006327 &exception);
6328 } while (exception.retry);
6329 return err;
6330}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006331
6332static bool nfs41_match_stateid(const nfs4_stateid *s1,
6333 const nfs4_stateid *s2)
6334{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006335 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006336 return false;
6337
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006338 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006339 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006340 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006341 return true;
6342
6343 return false;
6344}
6345
Andy Adamson557134a2009-04-01 09:21:53 -04006346#endif /* CONFIG_NFS_V4_1 */
6347
Trond Myklebust36281ca2012-03-04 18:13:56 -05006348static bool nfs4_match_stateid(const nfs4_stateid *s1,
6349 const nfs4_stateid *s2)
6350{
Trond Myklebustf597c532012-03-04 18:13:56 -05006351 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006352}
6353
6354
Trond Myklebust17280172012-03-11 13:11:00 -04006355static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006356 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006357 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 .recover_open = nfs4_open_reclaim,
6359 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006360 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006361 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362};
6363
Andy Adamson591d71c2009-04-01 09:22:47 -04006364#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006365static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006366 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6367 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6368 .recover_open = nfs4_open_reclaim,
6369 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006370 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006371 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006372 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006373};
6374#endif /* CONFIG_NFS_V4_1 */
6375
Trond Myklebust17280172012-03-11 13:11:00 -04006376static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006377 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006378 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 .recover_open = nfs4_open_expired,
6380 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006381 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006382 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383};
6384
Andy Adamson591d71c2009-04-01 09:22:47 -04006385#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006386static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006387 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6388 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006389 .recover_open = nfs41_open_expired,
6390 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006391 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006392 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006393};
6394#endif /* CONFIG_NFS_V4_1 */
6395
Trond Myklebust17280172012-03-11 13:11:00 -04006396static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006397 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006398 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006399 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006400};
6401
6402#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006403static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006404 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006405 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006406 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006407};
6408#endif
6409
Trond Myklebust97dc1352010-06-16 09:52:26 -04006410static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6411 .minor_version = 0,
6412 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006413 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006414 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006415 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6416 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6417 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006418};
6419
6420#if defined(CONFIG_NFS_V4_1)
6421static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6422 .minor_version = 1,
6423 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006424 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006425 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006426 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6427 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6428 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006429};
6430#endif
6431
Trond Myklebust97dc1352010-06-16 09:52:26 -04006432const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6433 [0] = &nfs_v4_0_minor_ops,
6434#if defined(CONFIG_NFS_V4_1)
6435 [1] = &nfs_v4_1_minor_ops,
6436#endif
6437};
6438
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006439static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006440 .permission = nfs_permission,
6441 .getattr = nfs_getattr,
6442 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006443 .getxattr = generic_getxattr,
6444 .setxattr = generic_setxattr,
6445 .listxattr = generic_listxattr,
6446 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006447};
6448
David Howells509de812006-08-22 20:06:11 -04006449const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 .version = 4, /* protocol version */
6451 .dentry_ops = &nfs4_dentry_operations,
6452 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006453 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006454 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 .getroot = nfs4_proc_get_root,
6456 .getattr = nfs4_proc_getattr,
6457 .setattr = nfs4_proc_setattr,
6458 .lookup = nfs4_proc_lookup,
6459 .access = nfs4_proc_access,
6460 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 .create = nfs4_proc_create,
6462 .remove = nfs4_proc_remove,
6463 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006464 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 .unlink_done = nfs4_proc_unlink_done,
6466 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006467 .rename_setup = nfs4_proc_rename_setup,
6468 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 .link = nfs4_proc_link,
6470 .symlink = nfs4_proc_symlink,
6471 .mkdir = nfs4_proc_mkdir,
6472 .rmdir = nfs4_proc_remove,
6473 .readdir = nfs4_proc_readdir,
6474 .mknod = nfs4_proc_mknod,
6475 .statfs = nfs4_proc_statfs,
6476 .fsinfo = nfs4_proc_fsinfo,
6477 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006478 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 .decode_dirent = nfs4_decode_dirent,
6480 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006481 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006482 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006484 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006485 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006487 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006489 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006490 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006491 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006492 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006493 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494};
6495
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006496static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6497 .prefix = XATTR_NAME_NFSV4_ACL,
6498 .list = nfs4_xattr_list_nfs4_acl,
6499 .get = nfs4_xattr_get_nfs4_acl,
6500 .set = nfs4_xattr_set_nfs4_acl,
6501};
6502
6503const struct xattr_handler *nfs4_xattr_handlers[] = {
6504 &nfs4_xattr_nfs4_acl_handler,
6505 NULL
6506};
6507
Trond Myklebustef159e92012-02-06 19:50:40 -05006508module_param(max_session_slots, ushort, 0644);
6509MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6510 "requests the client will negotiate");
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512/*
6513 * Local variables:
6514 * c-basic-offset: 8
6515 * End:
6516 */