blob: 87b9b91f76cfd687b0e5eab06e7365121d64f67d [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 Myklebust65de8722008-12-23 15:21:44 -0500265 int ret = errorcode;
266
267 exception->retry = 0;
268 switch(errorcode) {
269 case 0:
270 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500271 case -NFS4ERR_ADMIN_REVOKED:
272 case -NFS4ERR_BAD_STATEID:
273 case -NFS4ERR_OPENMODE:
274 if (state == NULL)
275 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500276 nfs4_schedule_stateid_recovery(server, state);
277 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400278 case -NFS4ERR_EXPIRED:
279 if (state != NULL)
280 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500281 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500282 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500283 nfs4_schedule_lease_recovery(clp);
284 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500285#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400286 case -NFS4ERR_BADSESSION:
287 case -NFS4ERR_BADSLOT:
288 case -NFS4ERR_BAD_HIGH_SLOT:
289 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
290 case -NFS4ERR_DEADSESSION:
291 case -NFS4ERR_SEQ_FALSE_RETRY:
292 case -NFS4ERR_SEQ_MISORDERED:
293 dprintk("%s ERROR: %d Reset session\n", __func__,
294 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500295 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400296 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500297 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500298#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500300 if (exception->timeout > HZ) {
301 /* We have retried a decent amount, time to
302 * fail
303 */
304 ret = -EBUSY;
305 break;
306 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500307 case -NFS4ERR_GRACE:
308 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500309 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500310 ret = nfs4_delay(server->client, &exception->timeout);
311 if (ret != 0)
312 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400313 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500314 case -NFS4ERR_OLD_STATEID:
315 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800316 break;
317 case -NFS4ERR_BADOWNER:
318 /* The following works around a Linux server bug! */
319 case -NFS4ERR_BADNAME:
320 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
321 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
322 exception->retry = 1;
323 printk(KERN_WARNING "NFS: v4 server %s "
324 "does not accept raw "
325 "uid/gids. "
326 "Reenabling the idmapper.\n",
327 server->nfs_client->cl_hostname);
328 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500329 }
330 /* We failed to handle the error */
331 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500332wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500333 ret = nfs4_wait_clnt_recover(clp);
334 if (ret == 0)
335 exception->retry = 1;
336 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500337}
338
339
Trond Myklebust452e9352010-07-31 14:29:06 -0400340static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_lock(&clp->cl_lock);
343 if (time_before(clp->cl_last_renewal,timestamp))
344 clp->cl_last_renewal = timestamp;
345 spin_unlock(&clp->cl_lock);
346}
347
Trond Myklebust452e9352010-07-31 14:29:06 -0400348static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
349{
350 do_renew_lease(server->nfs_client, timestamp);
351}
352
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400353#if defined(CONFIG_NFS_V4_1)
354
Benny Halevy510b8172009-04-01 09:22:14 -0400355/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400356 * nfs4_free_slot - free a slot and efficiently update slot table.
357 *
358 * freeing a slot is trivially done by clearing its respective bit
359 * in the bitmap.
360 * If the freed slotid equals highest_used_slotid we want to update it
361 * so that the server would be able to size down the slot table if needed,
362 * otherwise we know that the highest_used_slotid is still in use.
363 * When updating highest_used_slotid there may be "holes" in the bitmap
364 * so we need to scan down from highest_used_slotid to 0 looking for the now
365 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500366 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500367 *
368 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 */
370static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500371nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500373 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400374 /* clear used bit in bitmap */
375 __clear_bit(slotid, tbl->used_slots);
376
377 /* update highest_used_slotid when it is freed */
378 if (slotid == tbl->highest_used_slotid) {
379 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500380 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400381 tbl->highest_used_slotid = slotid;
382 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500383 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500385 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
386 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400387}
388
Trond Myklebust961a8282012-01-17 22:57:37 -0500389bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
390{
391 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
392 return true;
393}
394
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800395/*
Andy Adamson42acd022011-01-06 02:04:34 +0000396 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800397 */
Andy Adamson42acd022011-01-06 02:04:34 +0000398static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800399{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400400 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500401 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
402 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800403 return;
404 }
405
Trond Myklebust45d43c22012-02-06 19:38:51 -0500406 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800407 return;
408
Andy Adamson42acd022011-01-06 02:04:34 +0000409 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
410 complete(&ses->fc_slot_table.complete);
411}
412
413/*
414 * Signal state manager thread if session back channel is drained
415 */
416void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
417{
418 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000420 return;
421 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
422 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800423}
424
Trond Myklebustd185a332010-06-16 09:52:25 -0400425static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400426{
427 struct nfs4_slot_table *tbl;
428
Trond Myklebustd185a332010-06-16 09:52:25 -0400429 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400430 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400431 /* just wake up the next guy waiting since
432 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500433 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500434 return;
Andy Adamson13615872009-04-01 09:22:17 -0400435 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500436
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500437 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500438 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000439 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500440 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400441 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400442}
443
Trond Myklebust14516c32010-07-31 14:29:06 -0400444static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445{
446 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400447 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400448
449 /*
450 * sr_status remains 1 if an RPC level error occurred. The server
451 * may or may not have processed the sequence operation..
452 * Proceed as if the server received and processed the sequence
453 * operation.
454 */
455 if (res->sr_status == 1)
456 res->sr_status = NFS_OK;
457
Bryan Schumaker468f8612011-04-18 15:57:32 -0400458 /* don't increment the sequence number if the task wasn't sent */
459 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 goto out;
461
Andy Adamson691daf32009-12-04 15:55:39 -0500462 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 switch (res->sr_status) {
464 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400465 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400466 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400467 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400469 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500470 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500471 if (res->sr_status_flags != 0)
472 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400473 break;
474 case -NFS4ERR_DELAY:
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
477 * of RFC5661.
478 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200479 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 __func__,
481 res->sr_slot - res->sr_session->fc_slot_table.slots,
482 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 goto out_retry;
484 default:
485 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400487 }
488out:
489 /* The session may be reset by one of the error handlers. */
490 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400491 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400492 return 1;
493out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400494 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 goto out;
496 rpc_delay(task, NFS4_POLL_RETRY_MAX);
497 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498}
499
Trond Myklebust14516c32010-07-31 14:29:06 -0400500static int nfs4_sequence_done(struct rpc_task *task,
501 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400502{
Trond Myklebust14516c32010-07-31 14:29:06 -0400503 if (res->sr_session == NULL)
504 return 1;
505 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400506}
507
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400508/*
Benny Halevy510b8172009-04-01 09:22:14 -0400509 * nfs4_find_slot - efficiently look for a free slot
510 *
511 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
512 * If found, we mark the slot as used, update the highest_used_slotid,
513 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500514 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400515 *
516 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400517 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500518static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800519nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400520{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500521 u32 slotid;
522 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400523
Trond Myklebust45d43c22012-02-06 19:38:51 -0500524 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400525 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
526 tbl->max_slots);
527 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
528 if (slotid >= tbl->max_slots)
529 goto out;
530 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500531 if (slotid > tbl->highest_used_slotid ||
532 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400533 tbl->highest_used_slotid = slotid;
534 ret_id = slotid;
535out:
536 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
537 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400538 return ret_id;
539}
540
Trond Myklebust9d12b212012-01-17 22:04:25 -0500541static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
543{
544 args->sa_session = NULL;
545 args->sa_cache_this = 0;
546 if (cache_reply)
547 args->sa_cache_this = 1;
548 res->sr_session = NULL;
549 res->sr_slot = NULL;
550}
551
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000552int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400553 struct nfs4_sequence_args *args,
554 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400555 struct rpc_task *task)
556{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557 struct nfs4_slot *slot;
558 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500559 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400560
561 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400563 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400564 return 0;
565
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400566 tbl = &session->fc_slot_table;
567
568 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400569 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800570 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500571 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500572 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400573 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500574 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500575 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400576 }
577
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800578 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
579 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
580 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
581 spin_unlock(&tbl->slot_tbl_lock);
582 dprintk("%s enforce FIFO order\n", __func__);
583 return -EAGAIN;
584 }
585
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800586 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500587 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400588 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
589 spin_unlock(&tbl->slot_tbl_lock);
590 dprintk("<-- %s: no free slots\n", __func__);
591 return -EAGAIN;
592 }
593 spin_unlock(&tbl->slot_tbl_lock);
594
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800595 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400597 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400598 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400599
600 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
601
Benny Halevy99fe60d2009-04-01 09:22:29 -0400602 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400603 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400604 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400605 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400606 /*
607 * sr_status is only set in decode_sequence, and so will remain
608 * set to 1 if an rpc level failure occurs.
609 */
610 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 return 0;
612}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000613EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614
Trond Myklebust035168a2010-06-16 09:52:26 -0400615int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 struct rpc_task *task)
619{
Trond Myklebust035168a2010-06-16 09:52:26 -0400620 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 int ret = 0;
622
Trond Myklebust9d12b212012-01-17 22:04:25 -0500623 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400625
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200626 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400627 __func__, session->clp, session, res->sr_slot ?
628 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400629
Trond Myklebust9d12b212012-01-17 22:04:25 -0500630 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631out:
632 dprintk("<-- %s status=%d\n", __func__, ret);
633 return ret;
634}
635
636struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400637 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 struct nfs4_sequence_args *seq_args;
639 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640};
641
642static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
643{
644 struct nfs41_call_sync_data *data = calldata;
645
Trond Myklebust035168a2010-06-16 09:52:26 -0400646 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
647
648 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500649 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 return;
651 rpc_call_start(task);
652}
653
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800654static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
655{
656 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
657 nfs41_call_sync_prepare(task, calldata);
658}
659
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
661{
662 struct nfs41_call_sync_data *data = calldata;
663
Trond Myklebust14516c32010-07-31 14:29:06 -0400664 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665}
666
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667struct rpc_call_ops nfs41_call_sync_ops = {
668 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400669 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670};
671
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800672struct rpc_call_ops nfs41_call_priv_sync_ops = {
673 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
674 .rpc_call_done = nfs41_call_sync_done,
675};
676
Bryan Schumaker7c513052011-03-24 17:12:24 +0000677static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
678 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800682 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683{
684 int ret;
685 struct rpc_task *task;
686 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400687 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 .seq_args = args,
689 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 };
691 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000692 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 .rpc_message = msg,
694 .callback_ops = &nfs41_call_sync_ops,
695 .callback_data = &data
696 };
697
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800698 if (privileged)
699 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400700 task = rpc_run_task(&task_setup);
701 if (IS_ERR(task))
702 ret = PTR_ERR(task);
703 else {
704 ret = task->tk_status;
705 rpc_put_task(task);
706 }
707 return ret;
708}
709
Bryan Schumaker7c513052011-03-24 17:12:24 +0000710int _nfs4_call_sync_session(struct rpc_clnt *clnt,
711 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400712 struct rpc_message *msg,
713 struct nfs4_sequence_args *args,
714 struct nfs4_sequence_res *res,
715 int cache_reply)
716{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500717 nfs41_init_sequence(args, res, cache_reply);
718 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719}
720
Trond Myklebustdf896452010-06-16 09:52:26 -0400721#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500722static inline
723void nfs41_init_sequence(struct nfs4_sequence_args *args,
724 struct nfs4_sequence_res *res, int cache_reply)
725{
726}
727
Trond Myklebust14516c32010-07-31 14:29:06 -0400728static int nfs4_sequence_done(struct rpc_task *task,
729 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400730{
Trond Myklebust14516c32010-07-31 14:29:06 -0400731 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400732}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733#endif /* CONFIG_NFS_V4_1 */
734
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735int _nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
740 int cache_reply)
741{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500742 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400744}
745
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000746static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000747int nfs4_call_sync(struct rpc_clnt *clnt,
748 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000749 struct rpc_message *msg,
750 struct nfs4_sequence_args *args,
751 struct nfs4_sequence_res *res,
752 int cache_reply)
753{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000754 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
755 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000756}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757
Trond Myklebust38478b22006-05-25 01:40:57 -0400758static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Trond Myklebust38478b22006-05-25 01:40:57 -0400760 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Trond Myklebust38478b22006-05-25 01:40:57 -0400762 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400763 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700764 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400765 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700766 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400767 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct nfs_openargs o_arg;
773 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100774 struct nfs_open_confirmargs c_arg;
775 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500776 struct nfs4_string owner_name;
777 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778 struct nfs_fattr f_attr;
779 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400781 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400783 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100785 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400786 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100787 int rpc_status;
788 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789};
790
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400791
792static void nfs4_init_opendata_res(struct nfs4_opendata *p)
793{
794 p->o_res.f_attr = &p->f_attr;
795 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400796 p->o_res.seqid = p->o_arg.seqid;
797 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400798 p->o_res.server = p->o_arg.server;
799 nfs_fattr_init(&p->f_attr);
800 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500801 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400802}
803
Al Viro82a2c1b2011-06-22 18:30:55 -0400804static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400806 const struct iattr *attrs,
807 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808{
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 struct inode *dir = parent->d_inode;
811 struct nfs_server *server = NFS_SERVER(dir);
812 struct nfs4_opendata *p;
813
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400814 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 if (p == NULL)
816 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 if (p->o_arg.seqid == NULL)
819 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 nfs_sb_active(dentry->d_sb);
821 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 p->dir = parent;
823 p->owner = sp;
824 atomic_inc(&sp->so_count);
825 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826 p->o_arg.open_flags = flags;
827 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400828 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500829 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 p->o_arg.server = server;
832 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500833 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500835 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400836 u32 *s;
837
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838 p->o_arg.u.attrs = &p->attrs;
839 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400840 s = (u32 *) p->o_arg.u.verifier.data;
841 s[0] = jiffies;
842 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100844 p->c_arg.fh = &p->o_res.fh;
845 p->c_arg.stateid = &p->o_res.stateid;
846 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400847 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 return p;
850err_free:
851 kfree(p);
852err:
853 dput(parent);
854 return NULL;
855}
856
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400857static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100858{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400859 struct nfs4_opendata *p = container_of(kref,
860 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400861 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862
863 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400864 if (p->state != NULL)
865 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400866 nfs4_put_state_owner(p->owner);
867 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400868 dput(p->dentry);
869 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500870 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400871 kfree(p);
872}
873
874static void nfs4_opendata_put(struct nfs4_opendata *p)
875{
876 if (p != NULL)
877 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100878}
879
Trond Myklebust06f814a2006-01-03 09:55:07 +0100880static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
881{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100882 int ret;
883
Trond Myklebust06f814a2006-01-03 09:55:07 +0100884 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100885 return ret;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400889{
890 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891
Trond Myklebust536e43d2012-01-17 22:04:26 -0500892 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 goto out;
894 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500896 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
897 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400898 break;
899 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500900 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
901 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400902 break;
903 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500904 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
905 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400906 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400908 return ret;
909}
910
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400912{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500913 if (delegation == NULL)
914 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400916 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500917 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400918 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500919 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920 return 1;
921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100924{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100926 case FMODE_WRITE:
927 state->n_wronly++;
928 break;
929 case FMODE_READ:
930 state->n_rdonly++;
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 state->n_rdwr++;
934 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400939{
940 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
941 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
942 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400944 case FMODE_READ:
945 set_bit(NFS_O_RDONLY_STATE, &state->flags);
946 break;
947 case FMODE_WRITE:
948 set_bit(NFS_O_WRONLY_STATE, &state->flags);
949 break;
950 case FMODE_READ|FMODE_WRITE:
951 set_bit(NFS_O_RDWR_STATE, &state->flags);
952 }
953}
954
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400956{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400959 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400960}
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static 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 -0700963{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400964 /*
965 * Protect the call to nfs4_state_set_mode_locked and
966 * serialise the stateid update
967 */
968 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400969 if (deleg_stateid != NULL) {
970 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
971 set_bit(NFS_DELEGATED_STATE, &state->flags);
972 }
973 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400975 write_sequnlock(&state->seqlock);
976 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700978 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981static 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 -0500982{
983 struct nfs_inode *nfsi = NFS_I(state->inode);
984 struct nfs_delegation *deleg_cur;
985 int ret = 0;
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500988
989 rcu_read_lock();
990 deleg_cur = rcu_dereference(nfsi->delegation);
991 if (deleg_cur == NULL)
992 goto no_delegation;
993
994 spin_lock(&deleg_cur->lock);
995 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500997 goto no_delegation_unlock;
998
999 if (delegation == NULL)
1000 delegation = &deleg_cur->stateid;
1001 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1002 goto no_delegation_unlock;
1003
Trond Myklebustb7391f42008-12-23 15:21:52 -05001004 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001006 ret = 1;
1007no_delegation_unlock:
1008 spin_unlock(&deleg_cur->lock);
1009no_delegation:
1010 rcu_read_unlock();
1011
1012 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001014 ret = 1;
1015 }
1016
1017 return ret;
1018}
1019
1020
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022{
1023 struct nfs_delegation *delegation;
1024
1025 rcu_read_lock();
1026 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 rcu_read_unlock();
1029 return;
1030 }
1031 rcu_read_unlock();
1032 nfs_inode_return_delegation(inode);
1033}
1034
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036{
1037 struct nfs4_state *state = opendata->state;
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001040 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001041 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 nfs4_stateid stateid;
1043 int ret = -EAGAIN;
1044
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 if (can_open_cached(state, fmode, open_mode)) {
1049 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001050 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001051 goto out_return_state;
1052 }
1053 spin_unlock(&state->owner->so_lock);
1054 }
Trond Myklebust34310432008-12-23 15:21:38 -05001055 rcu_read_lock();
1056 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001057 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001058 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001059 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001060 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 /* Save the delegation */
1062 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1063 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001064 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065 if (ret != 0)
1066 goto out;
1067 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001068
1069 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001071 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073out:
1074 return ERR_PTR(ret);
1075out_return_state:
1076 atomic_inc(&state->count);
1077 return state;
1078}
1079
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1081{
1082 struct inode *inode;
1083 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001086
Trond Myklebustaac00a82007-07-05 19:02:21 -04001087 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001088 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001089 goto out;
1090 }
1091
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001092 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001093 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001094 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001095 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001096 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001097 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001098 goto err;
1099 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001100 state = nfs4_get_open_state(inode, data->owner);
1101 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001103 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001104 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001105 int delegation_flags = 0;
1106
Trond Myklebust003707c2007-07-05 18:07:55 -04001107 rcu_read_lock();
1108 delegation = rcu_dereference(NFS_I(inode)->delegation);
1109 if (delegation)
1110 delegation_flags = delegation->flags;
1111 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001112 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1113 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1114 "returning a delegation for "
1115 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001116 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001117 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 nfs_inode_set_delegation(state->inode,
1119 data->owner->so_cred,
1120 &data->o_res);
1121 else
1122 nfs_inode_reclaim_delegation(state->inode,
1123 data->owner->so_cred,
1124 &data->o_res);
1125 }
Trond Myklebust34310432008-12-23 15:21:38 -05001126
1127 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001128 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001129 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001130out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001132err_put_inode:
1133 iput(inode);
1134err:
1135 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001136}
1137
Trond Myklebust864472e2006-01-03 09:55:15 +01001138static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1139{
1140 struct nfs_inode *nfsi = NFS_I(state->inode);
1141 struct nfs_open_context *ctx;
1142
1143 spin_lock(&state->inode->i_lock);
1144 list_for_each_entry(ctx, &nfsi->open_files, list) {
1145 if (ctx->state != state)
1146 continue;
1147 get_nfs_open_context(ctx);
1148 spin_unlock(&state->inode->i_lock);
1149 return ctx;
1150 }
1151 spin_unlock(&state->inode->i_lock);
1152 return ERR_PTR(-ENOENT);
1153}
1154
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001155static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1156{
1157 struct nfs4_opendata *opendata;
1158
Al Viro3d4ff432011-06-22 18:40:12 -04001159 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160 if (opendata == NULL)
1161 return ERR_PTR(-ENOMEM);
1162 opendata->state = state;
1163 atomic_inc(&state->count);
1164 return opendata;
1165}
1166
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001168{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 int ret;
1171
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001172 opendata->o_arg.open_flags = 0;
1173 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1175 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1176 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001177 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 if (ret != 0)
1179 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001181 if (IS_ERR(newstate))
1182 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001183 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 return 0;
1186}
1187
1188static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1189{
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 int ret;
1192
1193 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 smp_rmb();
1196 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 if (ret != 0)
1200 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 if (newstate != state)
1202 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 }
1204 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001205 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 if (ret != 0)
1208 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 if (newstate != state)
1210 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 }
1212 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001213 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 if (ret != 0)
1216 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001217 if (newstate != state)
1218 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001220 /*
1221 * We may have performed cached opens for all three recoveries.
1222 * Check if we need to update the current stateid.
1223 */
1224 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1225 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001226 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1228 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001229 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001230 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 return 0;
1232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234/*
1235 * OPEN_RECLAIM:
1236 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001238static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001242 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int status;
1244
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001245 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1246 if (IS_ERR(opendata))
1247 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001248 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1249 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001250 rcu_read_lock();
1251 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001252 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001253 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001254 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 opendata->o_arg.u.delegation_type = delegation_type;
1256 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001257 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return status;
1259}
1260
Trond Myklebust539cd032007-06-05 11:46:42 -04001261static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct nfs_server *server = NFS_SERVER(state->inode);
1264 struct nfs4_exception exception = { };
1265 int err;
1266 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001267 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001268 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001269 break;
1270 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } while (exception.retry);
1272 return err;
1273}
1274
Trond Myklebust864472e2006-01-03 09:55:15 +01001275static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1276{
1277 struct nfs_open_context *ctx;
1278 int ret;
1279
1280 ctx = nfs4_state_find_open_context(state);
1281 if (IS_ERR(ctx))
1282 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001283 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 put_nfs_open_context(ctx);
1285 return ret;
1286}
1287
Trond Myklebust13437e12007-07-06 15:10:43 -04001288static 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 -07001289{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001291 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001293 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1294 if (IS_ERR(opendata))
1295 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001297 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001299 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Trond Myklebust13437e12007-07-06 15:10:43 -04001304int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001307 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 int err;
1309 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001310 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 switch (err) {
1312 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001313 case -ENOENT:
1314 case -ESTALE:
1315 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001316 case -NFS4ERR_BADSESSION:
1317 case -NFS4ERR_BADSLOT:
1318 case -NFS4ERR_BAD_HIGH_SLOT:
1319 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1320 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001321 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 case -NFS4ERR_STALE_CLIENTID:
1324 case -NFS4ERR_STALE_STATEID:
1325 case -NFS4ERR_EXPIRED:
1326 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001327 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001328 goto out;
1329 case -ERESTARTSYS:
1330 /*
1331 * The show must go on: exit, but mark the
1332 * stateid as needing recovery.
1333 */
1334 case -NFS4ERR_ADMIN_REVOKED:
1335 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001336 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001337 case -EKEYEXPIRED:
1338 /*
1339 * User RPCSEC_GSS context has expired.
1340 * We cannot recover this stateid now, so
1341 * skip it and allow recovery thread to
1342 * proceed.
1343 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001344 case -ENOMEM:
1345 err = 0;
1346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 err = nfs4_handle_exception(server, err, &exception);
1349 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001350out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return err;
1352}
1353
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1355{
1356 struct nfs4_opendata *data = calldata;
1357
1358 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001359 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1361 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001362 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001363 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001364 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001365 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366}
1367
1368static void nfs4_open_confirm_release(void *calldata)
1369{
1370 struct nfs4_opendata *data = calldata;
1371 struct nfs4_state *state = NULL;
1372
1373 /* If this request hasn't been cancelled, do nothing */
1374 if (data->cancelled == 0)
1375 goto out_free;
1376 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001377 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001378 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001380 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001381 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001383 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001384}
1385
1386static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387 .rpc_call_done = nfs4_open_confirm_done,
1388 .rpc_release = nfs4_open_confirm_release,
1389};
1390
1391/*
1392 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1393 */
1394static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1395{
1396 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1397 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001398 struct rpc_message msg = {
1399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1400 .rpc_argp = &data->c_arg,
1401 .rpc_resp = &data->c_res,
1402 .rpc_cred = data->owner->so_cred,
1403 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001404 struct rpc_task_setup task_setup_data = {
1405 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001407 .callback_ops = &nfs4_open_confirm_ops,
1408 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001409 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001410 .flags = RPC_TASK_ASYNC,
1411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 int status;
1413
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001414 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001415 data->rpc_done = 0;
1416 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001417 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001419 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001420 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001421 status = nfs4_wait_for_completion_rpc_task(task);
1422 if (status != 0) {
1423 data->cancelled = 1;
1424 smp_wmb();
1425 } else
1426 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001427 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return status;
1429}
1430
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 struct nfs4_opendata *data = calldata;
1434 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1437 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438 /*
1439 * Check if we still need to send an OPEN call, or if we can use
1440 * a delegation instead.
1441 */
1442 if (data->state != NULL) {
1443 struct nfs_delegation *delegation;
1444
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001446 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001447 rcu_read_lock();
1448 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001449 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1450 can_open_delegated(delegation, data->o_arg.fmode))
1451 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 rcu_read_unlock();
1453 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001455 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001456 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001457 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001458 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001459 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1460 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001461 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001462 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001463 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001464 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001465 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001466 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001467 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001468unlock_no_action:
1469 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001470out_no_action:
1471 task->tk_action = NULL;
1472
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001475static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1476{
1477 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1478 nfs4_open_prepare(task, calldata);
1479}
1480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481static void nfs4_open_done(struct rpc_task *task, void *calldata)
1482{
1483 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001486
Trond Myklebust14516c32010-07-31 14:29:06 -04001487 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1488 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 if (task->tk_status == 0) {
1491 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001492 case S_IFREG:
1493 break;
1494 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001496 break;
1497 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001499 break;
1500 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001502 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001503 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001504 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1505 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001506 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001507 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001509
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510static void nfs4_open_release(void *calldata)
1511{
1512 struct nfs4_opendata *data = calldata;
1513 struct nfs4_state *state = NULL;
1514
1515 /* If this request hasn't been cancelled, do nothing */
1516 if (data->cancelled == 0)
1517 goto out_free;
1518 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001519 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520 goto out_free;
1521 /* In case we need an open_confirm, no cleanup! */
1522 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1523 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001525 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001526 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001527out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001528 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529}
1530
1531static const struct rpc_call_ops nfs4_open_ops = {
1532 .rpc_call_prepare = nfs4_open_prepare,
1533 .rpc_call_done = nfs4_open_done,
1534 .rpc_release = nfs4_open_release,
1535};
1536
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001537static const struct rpc_call_ops nfs4_recover_open_ops = {
1538 .rpc_call_prepare = nfs4_recover_open_prepare,
1539 .rpc_call_done = nfs4_open_done,
1540 .rpc_release = nfs4_open_release,
1541};
1542
1543static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544{
1545 struct inode *dir = data->dir->d_inode;
1546 struct nfs_server *server = NFS_SERVER(dir);
1547 struct nfs_openargs *o_arg = &data->o_arg;
1548 struct nfs_openres *o_res = &data->o_res;
1549 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001550 struct rpc_message msg = {
1551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1552 .rpc_argp = o_arg,
1553 .rpc_resp = o_res,
1554 .rpc_cred = data->owner->so_cred,
1555 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001556 struct rpc_task_setup task_setup_data = {
1557 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001558 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001559 .callback_ops = &nfs4_open_ops,
1560 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001561 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001562 .flags = RPC_TASK_ASYNC,
1563 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001564 int status;
1565
Trond Myklebust9d12b212012-01-17 22:04:25 -05001566 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001567 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001568 data->rpc_done = 0;
1569 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001570 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001571 if (isrecover)
1572 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001573 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001574 if (IS_ERR(task))
1575 return PTR_ERR(task);
1576 status = nfs4_wait_for_completion_rpc_task(task);
1577 if (status != 0) {
1578 data->cancelled = 1;
1579 smp_wmb();
1580 } else
1581 status = data->rpc_status;
1582 rpc_put_task(task);
1583
1584 return status;
1585}
1586
1587static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1588{
1589 struct inode *dir = data->dir->d_inode;
1590 struct nfs_openres *o_res = &data->o_res;
1591 int status;
1592
1593 status = nfs4_run_open_task(data, 1);
1594 if (status != 0 || !data->rpc_done)
1595 return status;
1596
Trond Myklebust6926afd2012-01-07 13:22:46 -05001597 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1598
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001599 nfs_refresh_inode(dir, o_res->dir_attr);
1600
1601 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1602 status = _nfs4_proc_open_confirm(data);
1603 if (status != 0)
1604 return status;
1605 }
1606
1607 return status;
1608}
1609
1610/*
1611 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1612 */
1613static int _nfs4_proc_open(struct nfs4_opendata *data)
1614{
1615 struct inode *dir = data->dir->d_inode;
1616 struct nfs_server *server = NFS_SERVER(dir);
1617 struct nfs_openargs *o_arg = &data->o_arg;
1618 struct nfs_openres *o_res = &data->o_res;
1619 int status;
1620
1621 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001622 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001624 if (status != 0) {
1625 if (status == -NFS4ERR_BADNAME &&
1626 !(o_arg->open_flags & O_CREAT))
1627 return -ENOENT;
1628 return status;
1629 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630
Trond Myklebust6926afd2012-01-07 13:22:46 -05001631 nfs_fattr_map_and_free_names(server, &data->f_attr);
1632
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001633 if (o_arg->open_flags & O_CREAT) {
1634 update_changeattr(dir, &o_res->cinfo);
1635 nfs_post_op_update_inode(dir, o_res->dir_attr);
1636 } else
1637 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001638 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1639 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001641 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001646 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Andy Adamsond83217c2011-03-01 01:34:17 +00001650static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001651{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001652 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001653 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001654
Trond Myklebusta78cb572009-08-09 15:06:19 -04001655 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001656 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001657 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001658 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001659 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1660 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001661 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001662 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001663 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001664 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001665 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001666}
1667
Andy Adamsond83217c2011-03-01 01:34:17 +00001668static int nfs4_recover_expired_lease(struct nfs_server *server)
1669{
1670 return nfs4_client_recover_expired_lease(server->nfs_client);
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/*
1674 * OPEN_EXPIRED:
1675 * reclaim state on the server after a network partition.
1676 * Assumes caller holds the appropriate lock
1677 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001678static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001680 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001683 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1684 if (IS_ERR(opendata))
1685 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001686 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001687 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001688 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001689 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001693static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001694{
Trond Myklebust539cd032007-06-05 11:46:42 -04001695 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001696 struct nfs4_exception exception = { };
1697 int err;
1698
1699 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001700 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001701 switch (err) {
1702 default:
1703 goto out;
1704 case -NFS4ERR_GRACE:
1705 case -NFS4ERR_DELAY:
1706 nfs4_handle_exception(server, err, &exception);
1707 err = 0;
1708 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001709 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001710out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001711 return err;
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001717 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 ctx = nfs4_state_find_open_context(state);
1720 if (IS_ERR(ctx))
1721 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001722 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 put_nfs_open_context(ctx);
1724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001727#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001728static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001729{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001730 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001731 struct nfs_server *server = NFS_SERVER(state->inode);
1732
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001733 if (state->flags & flags) {
1734 status = nfs41_test_stateid(server, stateid);
1735 if (status != NFS_OK) {
1736 nfs41_free_stateid(server, stateid);
1737 state->flags &= ~flags;
1738 }
1739 }
1740 return status;
1741}
1742
1743static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1744{
1745 int deleg_status, open_status;
1746 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1747 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1748
1749 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1750 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1751
1752 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1753 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001754 return nfs4_open_expired(sp, state);
1755}
1756#endif
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001759 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1760 * fields corresponding to attributes that were used to store the verifier.
1761 * Make sure we clobber those fields in the later setattr call
1762 */
1763static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1764{
1765 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1766 !(sattr->ia_valid & ATTR_ATIME_SET))
1767 sattr->ia_valid |= ATTR_ATIME;
1768
1769 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1770 !(sattr->ia_valid & ATTR_MTIME_SET))
1771 sattr->ia_valid |= ATTR_MTIME;
1772}
1773
1774/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001777static 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 -07001778{
1779 struct nfs4_state_owner *sp;
1780 struct nfs4_state *state = NULL;
1781 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001782 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001783 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001787 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1788 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1790 goto out_err;
1791 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001792 status = nfs4_recover_expired_lease(server);
1793 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001794 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001795 if (dentry->d_inode != NULL)
1796 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001797 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001798 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001799 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001800 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Al Viro82a2c1b2011-06-22 18:30:55 -04001802 if (dentry->d_inode != NULL)
1803 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001804
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001805 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001807 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001809 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001810 status = PTR_ERR(state);
1811 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001812 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001813 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001814 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001815
1816 if (opendata->o_arg.open_flags & O_EXCL) {
1817 nfs4_exclusive_attrset(opendata, sattr);
1818
1819 nfs_fattr_init(opendata->o_res.f_attr);
1820 status = nfs4_do_setattr(state->inode, cred,
1821 opendata->o_res.f_attr, sattr,
1822 state);
1823 if (status == 0)
1824 nfs_setattr_update_inode(state->inode, sattr);
1825 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1826 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001827 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *res = state;
1830 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001831err_opendata_put:
1832 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001833err_put_state_owner:
1834 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 *res = NULL;
1837 return status;
1838}
1839
1840
Al Viro82a2c1b2011-06-22 18:30:55 -04001841static 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 -07001842{
1843 struct nfs4_exception exception = { };
1844 struct nfs4_state *res;
1845 int status;
1846
1847 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001848 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (status == 0)
1850 break;
1851 /* NOTE: BAD_SEQID means the server and client disagree about the
1852 * book-keeping w.r.t. state-changing operations
1853 * (OPEN/CLOSE/LOCK/LOCKU...)
1854 * It is actually a sign of a bug on the client or on the server.
1855 *
1856 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001857 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 * have unhashed the old state_owner for us, and that we can
1859 * therefore safely retry using a new one. We should still warn
1860 * the user though...
1861 */
1862 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001863 printk(KERN_WARNING "NFS: v4 server %s "
1864 " returned a bad sequence-id error!\n",
1865 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 exception.retry = 1;
1867 continue;
1868 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001869 /*
1870 * BAD_STATEID on OPEN means that the server cancelled our
1871 * state before it received the OPEN_CONFIRM.
1872 * Recover by retrying the request as per the discussion
1873 * on Page 181 of RFC3530.
1874 */
1875 if (status == -NFS4ERR_BAD_STATEID) {
1876 exception.retry = 1;
1877 continue;
1878 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001879 if (status == -EAGAIN) {
1880 /* We must have found a delegation */
1881 exception.retry = 1;
1882 continue;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1885 status, &exception));
1886 } while (exception.retry);
1887 return res;
1888}
1889
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001890static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1891 struct nfs_fattr *fattr, struct iattr *sattr,
1892 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001894 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001896 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 .iap = sattr,
1898 .server = server,
1899 .bitmask = server->attr_bitmask,
1900 };
1901 struct nfs_setattrres res = {
1902 .fattr = fattr,
1903 .server = server,
1904 };
1905 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1907 .rpc_argp = &arg,
1908 .rpc_resp = &res,
1909 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001911 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001912 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Trond Myklebust0e574af2005-10-27 22:12:38 -04001914 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001916 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1917 /* Use that stateid */
1918 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001919 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001920 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1922
Bryan Schumaker7c513052011-03-24 17:12:24 +00001923 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001924 if (status == 0 && state != NULL)
1925 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001926 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001929static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1930 struct nfs_fattr *fattr, struct iattr *sattr,
1931 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001933 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct nfs4_exception exception = { };
1935 int err;
1936 do {
1937 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001938 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 &exception);
1940 } while (exception.retry);
1941 return err;
1942}
1943
1944struct nfs4_closedata {
1945 struct inode *inode;
1946 struct nfs4_state *state;
1947 struct nfs_closeargs arg;
1948 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001949 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001950 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001951 bool roc;
1952 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953};
1954
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001955static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001956{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001957 struct nfs4_closedata *calldata = data;
1958 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001959 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001960
Fred Isamanf7e89172011-01-06 11:36:32 +00001961 if (calldata->roc)
1962 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001963 nfs4_put_open_state(calldata->state);
1964 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001965 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001966 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001967 kfree(calldata);
1968}
1969
Trond Myklebust88069f72009-12-08 08:33:16 -05001970static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1971 fmode_t fmode)
1972{
1973 spin_lock(&state->owner->so_lock);
1974 if (!(fmode & FMODE_READ))
1975 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1976 if (!(fmode & FMODE_WRITE))
1977 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1978 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1979 spin_unlock(&state->owner->so_lock);
1980}
1981
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001982static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001984 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct nfs_server *server = NFS_SERVER(calldata->inode);
1987
Chuck Levera3ca5652012-03-01 17:00:40 -05001988 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04001989 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1990 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* hmm. we are done with the inode, and in the process of freeing
1992 * the state_owner. we keep this around to process errors
1993 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 switch (task->tk_status) {
1995 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001996 if (calldata->roc)
1997 pnfs_roc_set_barrier(state->inode,
1998 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001999 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002000 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002001 nfs4_close_clear_stateid_flags(state,
2002 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002005 case -NFS4ERR_OLD_STATEID:
2006 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002008 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002011 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2012 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002014 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002015 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002016 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002019static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002021 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002022 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002023 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002024
Chuck Levera3ca5652012-03-01 17:00:40 -05002025 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002026 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002027 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002028
Trond Myklebust88069f72009-12-08 08:33:16 -05002029 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2030 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002031 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002032 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002033 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002034 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002035 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2036 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2037 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002038 }
2039 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002040 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2041 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2042 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002043 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002044 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002045 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002046
2047 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002048 /* Note: exit _without_ calling nfs4_close_done */
2049 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002050 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002051 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002052
Fred Isamanf7e89172011-01-06 11:36:32 +00002053 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002054 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002055 if (calldata->roc &&
2056 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2057 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2058 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002059 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002060 }
2061 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002062
Trond Myklebust516a6af2005-10-27 22:12:41 -04002063 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002064 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002065 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002066 &calldata->arg.seq_args,
2067 &calldata->res.seq_res,
2068 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002069 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002070 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002071out:
2072 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002075static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002076 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002077 .rpc_call_done = nfs4_close_done,
2078 .rpc_release = nfs4_free_closedata,
2079};
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/*
2082 * It is possible for data to be read/written from a mem-mapped file
2083 * after the sys_close call (which hits the vfs layer as a flush).
2084 * This means that we can't safely call nfsv4 close on a file until
2085 * the inode is cleared. This in turn means that we are not good
2086 * NFSv4 citizens - we do not indicate to the server to update the file's
2087 * share state even when we are done with one of the three share
2088 * stateid's in the inode.
2089 *
2090 * NOTE: Caller must be holding the sp->so_owner semaphore!
2091 */
Al Viro643168c2011-06-22 18:20:23 -04002092int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002094 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002096 struct nfs4_state_owner *sp = state->owner;
2097 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002098 struct rpc_message msg = {
2099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2100 .rpc_cred = state->owner->so_cred,
2101 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002102 struct rpc_task_setup task_setup_data = {
2103 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002104 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002105 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002106 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002107 .flags = RPC_TASK_ASYNC,
2108 };
Trond Myklebust95121352005-10-18 14:20:12 -07002109 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002111 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002113 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002114 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002115 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002117 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002118 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002120 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002121 if (calldata->arg.seqid == NULL)
2122 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002123 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002124 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002125 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002126 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002127 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002128 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002129 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002130
Trond Myklebust1174dd12010-12-21 10:52:24 -05002131 msg.rpc_argp = &calldata->arg;
2132 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002133 task_setup_data.callback_data = calldata;
2134 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002135 if (IS_ERR(task))
2136 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002137 status = 0;
2138 if (wait)
2139 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002140 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002141 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002142out_free_calldata:
2143 kfree(calldata);
2144out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002145 if (roc)
2146 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002147 nfs4_put_open_state(state);
2148 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002149 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Trond Myklebust2b484292010-09-17 10:56:51 -04002152static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002153nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct nfs4_state *state;
2156
Trond Myklebust565277f2007-10-15 18:17:53 -04002157 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002158 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002159 if (IS_ERR(state))
2160 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002161 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002162 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Trond Myklebust1185a552009-12-03 15:54:02 -05002165static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002166{
2167 if (ctx->state == NULL)
2168 return;
2169 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002170 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002171 else
Al Viro643168c2011-06-22 18:20:23 -04002172 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2176{
Benny Halevy43652ad2009-04-01 09:21:54 -04002177 struct nfs4_server_caps_arg args = {
2178 .fhandle = fhandle,
2179 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 struct nfs4_server_caps_res res = {};
2181 struct rpc_message msg = {
2182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002183 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 .rpc_resp = &res,
2185 };
2186 int status;
2187
Bryan Schumaker7c513052011-03-24 17:12:24 +00002188 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (status == 0) {
2190 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002191 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2192 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2193 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2194 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2195 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2197 server->caps |= NFS_CAP_ACLS;
2198 if (res.has_links != 0)
2199 server->caps |= NFS_CAP_HARDLINKS;
2200 if (res.has_symlinks != 0)
2201 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002202 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2203 server->caps |= NFS_CAP_FILEID;
2204 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2205 server->caps |= NFS_CAP_MODE;
2206 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2207 server->caps |= NFS_CAP_NLINK;
2208 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2209 server->caps |= NFS_CAP_OWNER;
2210 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2211 server->caps |= NFS_CAP_OWNER_GROUP;
2212 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2213 server->caps |= NFS_CAP_ATIME;
2214 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2215 server->caps |= NFS_CAP_CTIME;
2216 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2217 server->caps |= NFS_CAP_MTIME;
2218
Trond Myklebusta65318b2009-03-11 14:10:28 -04002219 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2220 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2221 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002223 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return status;
2227}
2228
Trond Myklebust55a97592006-06-09 09:34:19 -04002229int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
2231 struct nfs4_exception exception = { };
2232 int err;
2233 do {
2234 err = nfs4_handle_exception(server,
2235 _nfs4_server_capabilities(server, fhandle),
2236 &exception);
2237 } while (exception.retry);
2238 return err;
2239}
2240
2241static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2242 struct nfs_fsinfo *info)
2243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 struct nfs4_lookup_root_arg args = {
2245 .bitmask = nfs4_fattr_bitmap,
2246 };
2247 struct nfs4_lookup_res res = {
2248 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002249 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 .fh = fhandle,
2251 };
2252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2254 .rpc_argp = &args,
2255 .rpc_resp = &res,
2256 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002257
Trond Myklebust0e574af2005-10-27 22:12:38 -04002258 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002259 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2263 struct nfs_fsinfo *info)
2264{
2265 struct nfs4_exception exception = { };
2266 int err;
2267 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002268 err = _nfs4_lookup_root(server, fhandle, info);
2269 switch (err) {
2270 case 0:
2271 case -NFS4ERR_WRONGSEC:
2272 break;
2273 default:
2274 err = nfs4_handle_exception(server, err, &exception);
2275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 } while (exception.retry);
2277 return err;
2278}
2279
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002280static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2281 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2282{
2283 struct rpc_auth *auth;
2284 int ret;
2285
2286 auth = rpcauth_create(flavor, server->client);
2287 if (!auth) {
2288 ret = -EIO;
2289 goto out;
2290 }
2291 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002292out:
2293 return ret;
2294}
2295
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002296static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002297 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002299 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002300 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002302 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2303 flav_array[len] = RPC_AUTH_NULL;
2304 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002305
2306 for (i = 0; i < len; i++) {
2307 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002308 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002309 continue;
2310 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002311 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002312 /*
2313 * -EACCESS could mean that the user doesn't have correct permissions
2314 * to access the mount. It could also mean that we tried to mount
2315 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2316 * existing mount programs don't handle -EACCES very well so it should
2317 * be mapped to -EPERM instead.
2318 */
2319 if (status == -EACCES)
2320 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002321 return status;
2322}
2323
2324/*
2325 * get the file handle for the "/" directory on the server
2326 */
2327static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2328 struct nfs_fsinfo *info)
2329{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002330 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002331 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002332 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2333 /*
2334 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2335 * by nfs4_map_errors() as this function exits.
2336 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002337 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (status == 0)
2339 status = nfs4_server_capabilities(server, fhandle);
2340 if (status == 0)
2341 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002342 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Manoj Naik6b97fd32006-06-09 09:34:29 -04002345/*
2346 * Get locations and (maybe) other attributes of a referral.
2347 * Note that we'll actually follow the referral later when
2348 * we detect fsid mismatch in inode revalidation
2349 */
Andy Adamson533eb462011-06-13 18:25:56 -04002350static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2351 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002352{
2353 int status = -ENOMEM;
2354 struct page *page = NULL;
2355 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002356
2357 page = alloc_page(GFP_KERNEL);
2358 if (page == NULL)
2359 goto out;
2360 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2361 if (locations == NULL)
2362 goto out;
2363
Trond Myklebustc228fd32007-01-13 02:28:11 -05002364 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002365 if (status != 0)
2366 goto out;
2367 /* Make sure server returned a different fsid for the referral */
2368 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002369 dprintk("%s: server did not return a different fsid for"
2370 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002371 status = -EIO;
2372 goto out;
2373 }
Andy Adamson533eb462011-06-13 18:25:56 -04002374 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2375 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002376
Andy Adamson533eb462011-06-13 18:25:56 -04002377 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002378 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002379 memset(fhandle, 0, sizeof(struct nfs_fh));
2380out:
2381 if (page)
2382 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002383 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002384 return status;
2385}
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2388{
2389 struct nfs4_getattr_arg args = {
2390 .fh = fhandle,
2391 .bitmask = server->attr_bitmask,
2392 };
2393 struct nfs4_getattr_res res = {
2394 .fattr = fattr,
2395 .server = server,
2396 };
2397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2399 .rpc_argp = &args,
2400 .rpc_resp = &res,
2401 };
2402
Trond Myklebust0e574af2005-10-27 22:12:38 -04002403 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002404 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
2407static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2408{
2409 struct nfs4_exception exception = { };
2410 int err;
2411 do {
2412 err = nfs4_handle_exception(server,
2413 _nfs4_proc_getattr(server, fhandle, fattr),
2414 &exception);
2415 } while (exception.retry);
2416 return err;
2417}
2418
2419/*
2420 * The file is not closed if it is opened due to the a request to change
2421 * the size of the file. The open call will not be needed once the
2422 * VFS layer lookup-intents are implemented.
2423 *
2424 * Close is called when the inode is destroyed.
2425 * If we haven't opened the file for O_WRONLY, we
2426 * need to in the size_change case to obtain a stateid.
2427 *
2428 * Got race?
2429 * Because OPEN is always done by name in nfsv4, it is
2430 * possible that we opened a different file by the same
2431 * name. We can recognize this race condition, but we
2432 * can't do anything about it besides returning an error.
2433 *
2434 * This will be fixed with VFS changes (lookup-intent).
2435 */
2436static int
2437nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2438 struct iattr *sattr)
2439{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002440 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002441 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002442 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int status;
2444
Benny Halevy8a1636c2010-07-14 15:43:57 -04002445 if (pnfs_ld_layoutret_on_setattr(inode))
2446 pnfs_return_layout(inode);
2447
Trond Myklebust0e574af2005-10-27 22:12:38 -04002448 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Trond Myklebustd5308382005-11-04 15:33:38 -05002450 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002451 if (sattr->ia_valid & ATTR_FILE) {
2452 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002453
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002454 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002455 if (ctx) {
2456 cred = ctx->cred;
2457 state = ctx->state;
2458 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002459 }
2460
Trond Myklebust536e43d2012-01-17 22:04:26 -05002461 /* Deal with open(O_TRUNC) */
2462 if (sattr->ia_valid & ATTR_OPEN)
2463 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2464
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002465 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002466 if (status == 0)
2467 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return status;
2469}
2470
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002471static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2472 const struct qstr *name, struct nfs_fh *fhandle,
2473 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002474{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002475 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002476 int status;
2477 struct nfs4_lookup_arg args = {
2478 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002479 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002480 .name = name,
2481 };
2482 struct nfs4_lookup_res res = {
2483 .server = server,
2484 .fattr = fattr,
2485 .fh = fhandle,
2486 };
2487 struct rpc_message msg = {
2488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2489 .rpc_argp = &args,
2490 .rpc_resp = &res,
2491 };
2492
2493 nfs_fattr_init(fattr);
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002496 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 dprintk("NFS reply lookup: %d\n", status);
2498 return status;
2499}
2500
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002501void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2502{
2503 memset(fh, 0, sizeof(struct nfs_fh));
2504 fattr->fsid.major = 1;
2505 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2506 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2507 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2508 fattr->nlink = 2;
2509}
2510
Bryan Schumaker7c513052011-03-24 17:12:24 +00002511static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2512 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct nfs4_exception exception = { };
2515 int err;
2516 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002517 int status;
2518
2519 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2520 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002521 case -NFS4ERR_BADNAME:
2522 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002523 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002524 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002525 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002526 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002527 }
2528 err = nfs4_handle_exception(NFS_SERVER(dir),
2529 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 } while (exception.retry);
2531 return err;
2532}
2533
2534static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2535{
Trond Myklebust76b32992007-08-10 17:45:11 -04002536 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 struct nfs4_accessargs args = {
2538 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002539 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002541 struct nfs4_accessres res = {
2542 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002543 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 struct rpc_message msg = {
2545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2546 .rpc_argp = &args,
2547 .rpc_resp = &res,
2548 .rpc_cred = entry->cred,
2549 };
2550 int mode = entry->mask;
2551 int status;
2552
2553 /*
2554 * Determine which access bits we want to ask for...
2555 */
2556 if (mode & MAY_READ)
2557 args.access |= NFS4_ACCESS_READ;
2558 if (S_ISDIR(inode->i_mode)) {
2559 if (mode & MAY_WRITE)
2560 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2561 if (mode & MAY_EXEC)
2562 args.access |= NFS4_ACCESS_LOOKUP;
2563 } else {
2564 if (mode & MAY_WRITE)
2565 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2566 if (mode & MAY_EXEC)
2567 args.access |= NFS4_ACCESS_EXECUTE;
2568 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002569
2570 res.fattr = nfs_alloc_fattr();
2571 if (res.fattr == NULL)
2572 return -ENOMEM;
2573
Bryan Schumaker7c513052011-03-24 17:12:24 +00002574 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (!status) {
2576 entry->mask = 0;
2577 if (res.access & NFS4_ACCESS_READ)
2578 entry->mask |= MAY_READ;
2579 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2580 entry->mask |= MAY_WRITE;
2581 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2582 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002583 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002585 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return status;
2587}
2588
2589static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2590{
2591 struct nfs4_exception exception = { };
2592 int err;
2593 do {
2594 err = nfs4_handle_exception(NFS_SERVER(inode),
2595 _nfs4_proc_access(inode, entry),
2596 &exception);
2597 } while (exception.retry);
2598 return err;
2599}
2600
2601/*
2602 * TODO: For the time being, we don't try to get any attributes
2603 * along with any of the zero-copy operations READ, READDIR,
2604 * READLINK, WRITE.
2605 *
2606 * In the case of the first three, we want to put the GETATTR
2607 * after the read-type operation -- this is because it is hard
2608 * to predict the length of a GETATTR response in v4, and thus
2609 * align the READ data correctly. This means that the GETATTR
2610 * may end up partially falling into the page cache, and we should
2611 * shift it into the 'tail' of the xdr_buf before processing.
2612 * To do this efficiently, we need to know the total length
2613 * of data received, which doesn't seem to be available outside
2614 * of the RPC layer.
2615 *
2616 * In the case of WRITE, we also want to put the GETATTR after
2617 * the operation -- in this case because we want to make sure
2618 * we get the post-operation mtime and size. This means that
2619 * we can't use xdr_encode_pages() as written: we need a variant
2620 * of it which would leave room in the 'tail' iovec.
2621 *
2622 * Both of these changes to the XDR layer would in fact be quite
2623 * minor, but I decided to leave them for a subsequent patch.
2624 */
2625static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2626 unsigned int pgbase, unsigned int pglen)
2627{
2628 struct nfs4_readlink args = {
2629 .fh = NFS_FH(inode),
2630 .pgbase = pgbase,
2631 .pglen = pglen,
2632 .pages = &page,
2633 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002634 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 struct rpc_message msg = {
2636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2637 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002638 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 };
2640
Bryan Schumaker7c513052011-03-24 17:12:24 +00002641 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 -07002642}
2643
2644static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2645 unsigned int pgbase, unsigned int pglen)
2646{
2647 struct nfs4_exception exception = { };
2648 int err;
2649 do {
2650 err = nfs4_handle_exception(NFS_SERVER(inode),
2651 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2652 &exception);
2653 } while (exception.retry);
2654 return err;
2655}
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657/*
2658 * Got race?
2659 * We will need to arrange for the VFS layer to provide an atomic open.
2660 * Until then, this create/open method is prone to inefficiency and race
2661 * conditions due to the lookup, create, and open VFS calls from sys_open()
2662 * placed on the wire.
2663 *
2664 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2665 * The file will be opened again in the subsequent VFS open call
2666 * (nfs4_proc_file_open).
2667 *
2668 * The open for read will just hang around to be used by any process that
2669 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2670 */
2671
2672static int
2673nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002674 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Al Viro82a2c1b2011-06-22 18:30:55 -04002676 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002678 struct rpc_cred *cred = NULL;
2679 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int status = 0;
2681
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002682 if (ctx != NULL) {
2683 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002684 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002685 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002687 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002688 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002689 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (IS_ERR(state)) {
2691 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002692 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002694 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002695 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002696 if (ctx != NULL)
2697 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002698 else
Al Viro643168c2011-06-22 18:20:23 -04002699 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700out:
2701 return status;
2702}
2703
2704static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2705{
Trond Myklebust16e42952005-10-27 22:12:44 -04002706 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002707 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002709 .name.len = name->len,
2710 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002711 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002713 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002714 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2718 .rpc_argp = &args,
2719 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002721 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Trond Myklebustd3468902010-04-16 16:22:50 -04002723 res.dir_attr = nfs_alloc_fattr();
2724 if (res.dir_attr == NULL)
2725 goto out;
2726
Bryan Schumaker7c513052011-03-24 17:12:24 +00002727 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002728 if (status == 0) {
2729 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002730 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002731 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002732 nfs_free_fattr(res.dir_attr);
2733out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return status;
2735}
2736
2737static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2738{
2739 struct nfs4_exception exception = { };
2740 int err;
2741 do {
2742 err = nfs4_handle_exception(NFS_SERVER(dir),
2743 _nfs4_proc_remove(dir, name),
2744 &exception);
2745 } while (exception.retry);
2746 return err;
2747}
2748
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002749static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002751 struct nfs_server *server = NFS_SERVER(dir);
2752 struct nfs_removeargs *args = msg->rpc_argp;
2753 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Trond Myklebusta65318b2009-03-11 14:10:28 -04002755 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002756 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002758 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002761static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002763 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2764
Trond Myklebust14516c32010-07-31 14:29:06 -04002765 if (!nfs4_sequence_done(task, &res->seq_res))
2766 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002767 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002768 return 0;
2769 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002770 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002771 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
Jeff Laytond3d41522010-09-17 17:31:57 -04002774static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2775{
2776 struct nfs_server *server = NFS_SERVER(dir);
2777 struct nfs_renameargs *arg = msg->rpc_argp;
2778 struct nfs_renameres *res = msg->rpc_resp;
2779
2780 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2781 arg->bitmask = server->attr_bitmask;
2782 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002783 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002784}
2785
2786static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2787 struct inode *new_dir)
2788{
2789 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2790
2791 if (!nfs4_sequence_done(task, &res->seq_res))
2792 return 0;
2793 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2794 return 0;
2795
2796 update_changeattr(old_dir, &res->old_cinfo);
2797 nfs_post_op_update_inode(old_dir, res->old_fattr);
2798 update_changeattr(new_dir, &res->new_cinfo);
2799 nfs_post_op_update_inode(new_dir, res->new_fattr);
2800 return 1;
2801}
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2804 struct inode *new_dir, struct qstr *new_name)
2805{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002806 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002807 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 .old_dir = NFS_FH(old_dir),
2809 .new_dir = NFS_FH(new_dir),
2810 .old_name = old_name,
2811 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002812 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002814 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002815 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002816 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct rpc_message msg = {
2818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2819 .rpc_argp = &arg,
2820 .rpc_resp = &res,
2821 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002822 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Trond Myklebust011fff72010-04-16 16:22:49 -04002824 res.old_fattr = nfs_alloc_fattr();
2825 res.new_fattr = nfs_alloc_fattr();
2826 if (res.old_fattr == NULL || res.new_fattr == NULL)
2827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Bryan Schumaker7c513052011-03-24 17:12:24 +00002829 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (!status) {
2831 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002832 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002834 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002836out:
2837 nfs_free_fattr(res.new_fattr);
2838 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return status;
2840}
2841
2842static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2843 struct inode *new_dir, struct qstr *new_name)
2844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2849 _nfs4_proc_rename(old_dir, old_name,
2850 new_dir, new_name),
2851 &exception);
2852 } while (exception.retry);
2853 return err;
2854}
2855
2856static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2857{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002858 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct nfs4_link_arg arg = {
2860 .fh = NFS_FH(inode),
2861 .dir_fh = NFS_FH(dir),
2862 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002863 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002865 struct nfs4_link_res res = {
2866 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002867 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 struct rpc_message msg = {
2869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2870 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002871 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002873 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Trond Myklebust136f2622010-04-16 16:22:49 -04002875 res.fattr = nfs_alloc_fattr();
2876 res.dir_attr = nfs_alloc_fattr();
2877 if (res.fattr == NULL || res.dir_attr == NULL)
2878 goto out;
2879
Bryan Schumaker7c513052011-03-24 17:12:24 +00002880 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002881 if (!status) {
2882 update_changeattr(dir, &res.cinfo);
2883 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002884 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002885 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002886out:
2887 nfs_free_fattr(res.dir_attr);
2888 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return status;
2890}
2891
2892static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2893{
2894 struct nfs4_exception exception = { };
2895 int err;
2896 do {
2897 err = nfs4_handle_exception(NFS_SERVER(inode),
2898 _nfs4_proc_link(inode, dir, name),
2899 &exception);
2900 } while (exception.retry);
2901 return err;
2902}
2903
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002904struct nfs4_createdata {
2905 struct rpc_message msg;
2906 struct nfs4_create_arg arg;
2907 struct nfs4_create_res res;
2908 struct nfs_fh fh;
2909 struct nfs_fattr fattr;
2910 struct nfs_fattr dir_fattr;
2911};
2912
2913static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2914 struct qstr *name, struct iattr *sattr, u32 ftype)
2915{
2916 struct nfs4_createdata *data;
2917
2918 data = kzalloc(sizeof(*data), GFP_KERNEL);
2919 if (data != NULL) {
2920 struct nfs_server *server = NFS_SERVER(dir);
2921
2922 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2923 data->msg.rpc_argp = &data->arg;
2924 data->msg.rpc_resp = &data->res;
2925 data->arg.dir_fh = NFS_FH(dir);
2926 data->arg.server = server;
2927 data->arg.name = name;
2928 data->arg.attrs = sattr;
2929 data->arg.ftype = ftype;
2930 data->arg.bitmask = server->attr_bitmask;
2931 data->res.server = server;
2932 data->res.fh = &data->fh;
2933 data->res.fattr = &data->fattr;
2934 data->res.dir_fattr = &data->dir_fattr;
2935 nfs_fattr_init(data->res.fattr);
2936 nfs_fattr_init(data->res.dir_fattr);
2937 }
2938 return data;
2939}
2940
2941static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2942{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002943 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002944 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002945 if (status == 0) {
2946 update_changeattr(dir, &data->res.dir_cinfo);
2947 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2948 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2949 }
2950 return status;
2951}
2952
2953static void nfs4_free_createdata(struct nfs4_createdata *data)
2954{
2955 kfree(data);
2956}
2957
Chuck Lever4f390c12006-08-22 20:06:22 -04002958static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002959 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002961 struct nfs4_createdata *data;
2962 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Chuck Lever94a6d752006-08-22 20:06:23 -04002964 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002965 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002966
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002967 status = -ENOMEM;
2968 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2969 if (data == NULL)
2970 goto out;
2971
2972 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2973 data->arg.u.symlink.pages = &page;
2974 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 status = nfs4_do_create(dir, dentry, data);
2977
2978 nfs4_free_createdata(data);
2979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
Chuck Lever4f390c12006-08-22 20:06:22 -04002983static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002984 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct nfs4_exception exception = { };
2987 int err;
2988 do {
2989 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002990 _nfs4_proc_symlink(dir, dentry, page,
2991 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 &exception);
2993 } while (exception.retry);
2994 return err;
2995}
2996
2997static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2998 struct iattr *sattr)
2999{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003000 struct nfs4_createdata *data;
3001 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003003 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3004 if (data == NULL)
3005 goto out;
3006
3007 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
3014static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3015 struct iattr *sattr)
3016{
3017 struct nfs4_exception exception = { };
3018 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003019
3020 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 do {
3022 err = nfs4_handle_exception(NFS_SERVER(dir),
3023 _nfs4_proc_mkdir(dir, dentry, sattr),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
3029static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003030 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 struct inode *dir = dentry->d_inode;
3033 struct nfs4_readdir_arg args = {
3034 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003035 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .pgbase = 0,
3037 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003038 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003039 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 };
3041 struct nfs4_readdir_res res;
3042 struct rpc_message msg = {
3043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3044 .rpc_argp = &args,
3045 .rpc_resp = &res,
3046 .rpc_cred = cred,
3047 };
3048 int status;
3049
Harvey Harrison3110ff82008-05-02 13:42:44 -07003050 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003051 dentry->d_parent->d_name.name,
3052 dentry->d_name.name,
3053 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3055 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003056 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 -05003057 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003059 status += args.pgbase;
3060 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003061
3062 nfs_invalidate_atime(dir);
3063
Harvey Harrison3110ff82008-05-02 13:42:44 -07003064 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return status;
3066}
3067
3068static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003069 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
3071 struct nfs4_exception exception = { };
3072 int err;
3073 do {
3074 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3075 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003076 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 &exception);
3078 } while (exception.retry);
3079 return err;
3080}
3081
3082static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3083 struct iattr *sattr, dev_t rdev)
3084{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003085 struct nfs4_createdata *data;
3086 int mode = sattr->ia_mode;
3087 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3090 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003091
3092 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3093 if (data == NULL)
3094 goto out;
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003097 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003099 data->arg.ftype = NF4BLK;
3100 data->arg.u.device.specdata1 = MAJOR(rdev);
3101 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 }
3103 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003104 data->arg.ftype = NF4CHR;
3105 data->arg.u.device.specdata1 = MAJOR(rdev);
3106 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003109 status = nfs4_do_create(dir, dentry, data);
3110
3111 nfs4_free_createdata(data);
3112out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return status;
3114}
3115
3116static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3117 struct iattr *sattr, dev_t rdev)
3118{
3119 struct nfs4_exception exception = { };
3120 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003121
3122 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 do {
3124 err = nfs4_handle_exception(NFS_SERVER(dir),
3125 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3126 &exception);
3127 } while (exception.retry);
3128 return err;
3129}
3130
3131static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3132 struct nfs_fsstat *fsstat)
3133{
3134 struct nfs4_statfs_arg args = {
3135 .fh = fhandle,
3136 .bitmask = server->attr_bitmask,
3137 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003138 struct nfs4_statfs_res res = {
3139 .fsstat = fsstat,
3140 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 struct rpc_message msg = {
3142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3143 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003144 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 };
3146
Trond Myklebust0e574af2005-10-27 22:12:38 -04003147 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003148 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3152{
3153 struct nfs4_exception exception = { };
3154 int err;
3155 do {
3156 err = nfs4_handle_exception(server,
3157 _nfs4_proc_statfs(server, fhandle, fsstat),
3158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3164 struct nfs_fsinfo *fsinfo)
3165{
3166 struct nfs4_fsinfo_arg args = {
3167 .fh = fhandle,
3168 .bitmask = server->attr_bitmask,
3169 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003170 struct nfs4_fsinfo_res res = {
3171 .fsinfo = fsinfo,
3172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3175 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003176 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 };
3178
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_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3183{
3184 struct nfs4_exception exception = { };
3185 int err;
3186
3187 do {
3188 err = nfs4_handle_exception(server,
3189 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3190 &exception);
3191 } while (exception.retry);
3192 return err;
3193}
3194
3195static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3196{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003197 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3199}
3200
3201static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3202 struct nfs_pathconf *pathconf)
3203{
3204 struct nfs4_pathconf_arg args = {
3205 .fh = fhandle,
3206 .bitmask = server->attr_bitmask,
3207 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003208 struct nfs4_pathconf_res res = {
3209 .pathconf = pathconf,
3210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3213 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003214 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 };
3216
3217 /* None of the pathconf attributes are mandatory to implement */
3218 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3219 memset(pathconf, 0, sizeof(*pathconf));
3220 return 0;
3221 }
3222
Trond Myklebust0e574af2005-10-27 22:12:38 -04003223 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003224 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
3227static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3228 struct nfs_pathconf *pathconf)
3229{
3230 struct nfs4_exception exception = { };
3231 int err;
3232
3233 do {
3234 err = nfs4_handle_exception(server,
3235 _nfs4_proc_pathconf(server, fhandle, pathconf),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
Benny Halevyd20581a2011-05-22 19:52:03 +03003241void __nfs4_read_done_cb(struct nfs_read_data *data)
3242{
3243 nfs_invalidate_atime(data->inode);
3244}
3245
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003246static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Trond Myklebustec06c092006-03-20 13:44:27 -05003248 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003250 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003251 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003252 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003254
Benny Halevyd20581a2011-05-22 19:52:03 +03003255 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003257 renew_lease(server, data->timestamp);
3258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003261static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3262{
3263
3264 dprintk("--> %s\n", __func__);
3265
3266 if (!nfs4_sequence_done(task, &data->res.seq_res))
3267 return -EAGAIN;
3268
Benny Halevyd20581a2011-05-22 19:52:03 +03003269 return data->read_done_cb ? data->read_done_cb(task, data) :
3270 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003271}
3272
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003273static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003276 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003277 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003278 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003281/* Reset the the nfs_read_data to send the read to the MDS. */
3282void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3283{
3284 dprintk("%s Reset task for i/o through\n", __func__);
3285 put_lseg(data->lseg);
3286 data->lseg = NULL;
3287 /* offsets will differ in the dense stripe case */
3288 data->args.offset = data->mds_offset;
3289 data->ds_clp = NULL;
3290 data->args.fh = NFS_FH(data->inode);
3291 data->read_done_cb = nfs4_read_done_cb;
3292 task->tk_ops = data->mds_ops;
3293 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3294}
3295EXPORT_SYMBOL_GPL(nfs4_reset_read);
3296
Fred Isamanb029bc92011-03-03 15:13:42 +00003297static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct inode *inode = data->inode;
3300
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003301 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003302 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003303 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003305 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003307 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003308 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
Fred Isamanb029bc92011-03-03 15:13:42 +00003312static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3313{
3314 if (!nfs4_sequence_done(task, &data->res.seq_res))
3315 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003316 return data->write_done_cb ? data->write_done_cb(task, data) :
3317 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003318}
3319
Fred Isamana69aef12011-03-03 15:13:47 +00003320/* Reset the the nfs_write_data to send the write to the MDS. */
3321void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3322{
3323 dprintk("%s Reset task for i/o through\n", __func__);
3324 put_lseg(data->lseg);
3325 data->lseg = NULL;
3326 data->ds_clp = NULL;
3327 data->write_done_cb = nfs4_write_done_cb;
3328 data->args.fh = NFS_FH(data->inode);
3329 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3330 data->args.offset = data->mds_offset;
3331 data->res.fattr = &data->fattr;
3332 task->tk_ops = data->mds_ops;
3333 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3334}
3335EXPORT_SYMBOL_GPL(nfs4_reset_write);
3336
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003337static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003339 struct nfs_server *server = NFS_SERVER(data->inode);
3340
Fred Isaman7ffd1062011-03-03 15:13:46 +00003341 if (data->lseg) {
3342 data->args.bitmask = NULL;
3343 data->res.fattr = NULL;
3344 } else
3345 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003346 if (!data->write_done_cb)
3347 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003348 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 data->timestamp = jiffies;
3350
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003352 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Fred Isaman5f452432011-03-23 13:27:46 +00003355static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003358
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003359 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003360 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003361 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003363 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
Fred Isaman5f452432011-03-23 13:27:46 +00003367static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3368{
3369 if (!nfs4_sequence_done(task, &data->res.seq_res))
3370 return -EAGAIN;
3371 return data->write_done_cb(task, data);
3372}
3373
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003374static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003376 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003377
3378 if (data->lseg) {
3379 data->args.bitmask = NULL;
3380 data->res.fattr = NULL;
3381 } else
3382 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003383 if (!data->write_done_cb)
3384 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003385 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003386 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003387 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003390struct nfs4_renewdata {
3391 struct nfs_client *client;
3392 unsigned long timestamp;
3393};
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395/*
3396 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3397 * standalone procedure for queueing an asynchronous RENEW.
3398 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003399static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003400{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003401 struct nfs4_renewdata *data = calldata;
3402 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003403
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003404 if (atomic_read(&clp->cl_count) > 1)
3405 nfs4_schedule_state_renewal(clp);
3406 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003407 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003408}
3409
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003410static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003412 struct nfs4_renewdata *data = calldata;
3413 struct nfs_client *clp = data->client;
3414 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003417 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003418 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3419 return;
3420 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003421 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003422 return;
3423 }
3424 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003426 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427}
3428
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003429static const struct rpc_call_ops nfs4_renew_ops = {
3430 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003431 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003432};
3433
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003434static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
3436 struct rpc_message msg = {
3437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3438 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003439 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003441 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003443 if (renew_flags == 0)
3444 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003445 if (!atomic_inc_not_zero(&clp->cl_count))
3446 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003447 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003448 if (data == NULL)
3449 return -ENOMEM;
3450 data->client = clp;
3451 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003453 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003456static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 struct rpc_message msg = {
3459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3460 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003461 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 };
3463 unsigned long now = jiffies;
3464 int status;
3465
3466 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3467 if (status < 0)
3468 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003469 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return 0;
3471}
3472
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003473static inline int nfs4_server_supports_acls(struct nfs_server *server)
3474{
3475 return (server->caps & NFS_CAP_ACLS)
3476 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3477 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3478}
3479
3480/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3481 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3482 * the stack.
3483 */
3484#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3485
Neil Hormane9e3d722011-03-04 19:26:03 -05003486static int buf_to_pages_noslab(const void *buf, size_t buflen,
3487 struct page **pages, unsigned int *pgbase)
3488{
3489 struct page *newpage, **spages;
3490 int rc = 0;
3491 size_t len;
3492 spages = pages;
3493
3494 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003495 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003496 newpage = alloc_page(GFP_KERNEL);
3497
3498 if (newpage == NULL)
3499 goto unwind;
3500 memcpy(page_address(newpage), buf, len);
3501 buf += len;
3502 buflen -= len;
3503 *pages++ = newpage;
3504 rc++;
3505 } while (buflen != 0);
3506
3507 return rc;
3508
3509unwind:
3510 for(; rc > 0; rc--)
3511 __free_page(spages[rc-1]);
3512 return -ENOMEM;
3513}
3514
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003515struct nfs4_cached_acl {
3516 int cached;
3517 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003518 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003519};
3520
3521static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003522{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003523 struct nfs_inode *nfsi = NFS_I(inode);
3524
3525 spin_lock(&inode->i_lock);
3526 kfree(nfsi->nfs4_acl);
3527 nfsi->nfs4_acl = acl;
3528 spin_unlock(&inode->i_lock);
3529}
3530
3531static void nfs4_zap_acl_attr(struct inode *inode)
3532{
3533 nfs4_set_cached_acl(inode, NULL);
3534}
3535
3536static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3537{
3538 struct nfs_inode *nfsi = NFS_I(inode);
3539 struct nfs4_cached_acl *acl;
3540 int ret = -ENOENT;
3541
3542 spin_lock(&inode->i_lock);
3543 acl = nfsi->nfs4_acl;
3544 if (acl == NULL)
3545 goto out;
3546 if (buf == NULL) /* user is just asking for length */
3547 goto out_len;
3548 if (acl->cached == 0)
3549 goto out;
3550 ret = -ERANGE; /* see getxattr(2) man page */
3551 if (acl->len > buflen)
3552 goto out;
3553 memcpy(buf, acl->data, acl->len);
3554out_len:
3555 ret = acl->len;
3556out:
3557 spin_unlock(&inode->i_lock);
3558 return ret;
3559}
3560
3561static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3562{
3563 struct nfs4_cached_acl *acl;
3564
3565 if (buf && acl_len <= PAGE_SIZE) {
3566 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3567 if (acl == NULL)
3568 goto out;
3569 acl->cached = 1;
3570 memcpy(acl->data, buf, acl_len);
3571 } else {
3572 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3573 if (acl == NULL)
3574 goto out;
3575 acl->cached = 0;
3576 }
3577 acl->len = acl_len;
3578out:
3579 nfs4_set_cached_acl(inode, acl);
3580}
3581
Andy Adamsonbf118a32011-12-07 11:55:27 -05003582/*
3583 * The getxattr API returns the required buffer length when called with a
3584 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3585 * the required buf. On a NULL buf, we send a page of data to the server
3586 * guessing that the ACL request can be serviced by a page. If so, we cache
3587 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3588 * the cache. If not so, we throw away the page, and cache the required
3589 * length. The next getxattr call will then produce another round trip to
3590 * the server, this time with the input buf of the required size.
3591 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003592static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003593{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003594 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003595 struct nfs_getaclargs args = {
3596 .fh = NFS_FH(inode),
3597 .acl_pages = pages,
3598 .acl_len = buflen,
3599 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003600 struct nfs_getaclres res = {
3601 .acl_len = buflen,
3602 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003603 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003604 struct rpc_message msg = {
3605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3606 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003607 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003608 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003609 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610
Andy Adamsonbf118a32011-12-07 11:55:27 -05003611 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3612 /* As long as we're doing a round trip to the server anyway,
3613 * let's be prepared for a page of acl data. */
3614 if (npages == 0)
3615 npages = 1;
3616
3617 for (i = 0; i < npages; i++) {
3618 pages[i] = alloc_page(GFP_KERNEL);
3619 if (!pages[i])
3620 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003621 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003622 if (npages > 1) {
3623 /* for decoding across pages */
3624 args.acl_scratch = alloc_page(GFP_KERNEL);
3625 if (!args.acl_scratch)
3626 goto out_free;
3627 }
3628 args.acl_len = npages * PAGE_SIZE;
3629 args.acl_pgbase = 0;
3630 /* Let decode_getfacl know not to fail if the ACL data is larger than
3631 * the page we send as a guess */
3632 if (buf == NULL)
3633 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3634 resp_buf = page_address(pages[0]);
3635
Peng Taode040be2012-01-10 22:42:47 +08003636 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003637 __func__, buf, buflen, npages, args.acl_len);
3638 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3639 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003640 if (ret)
3641 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003642
3643 acl_len = res.acl_len - res.acl_data_offset;
3644 if (acl_len > args.acl_len)
3645 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003646 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003647 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3648 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003649 if (buf) {
3650 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003651 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003652 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003653 _copy_from_pages(buf, pages, res.acl_data_offset,
3654 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003656 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003657out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003658 for (i = 0; i < npages; i++)
3659 if (pages[i])
3660 __free_page(pages[i]);
3661 if (args.acl_scratch)
3662 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003663 return ret;
3664}
3665
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003666static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3667{
3668 struct nfs4_exception exception = { };
3669 ssize_t ret;
3670 do {
3671 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3672 if (ret >= 0)
3673 break;
3674 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3675 } while (exception.retry);
3676 return ret;
3677}
3678
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003679static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3680{
3681 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003682 int ret;
3683
3684 if (!nfs4_server_supports_acls(server))
3685 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003686 ret = nfs_revalidate_inode(server, inode);
3687 if (ret < 0)
3688 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003689 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3690 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003691 ret = nfs4_read_cached_acl(inode, buf, buflen);
3692 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003693 /* -ENOENT is returned if there is no ACL or if there is an ACL
3694 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003695 return ret;
3696 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003697}
3698
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003699static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003700{
3701 struct nfs_server *server = NFS_SERVER(inode);
3702 struct page *pages[NFS4ACL_MAXPAGES];
3703 struct nfs_setaclargs arg = {
3704 .fh = NFS_FH(inode),
3705 .acl_pages = pages,
3706 .acl_len = buflen,
3707 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003708 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3711 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003712 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003713 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003714 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003715
3716 if (!nfs4_server_supports_acls(server))
3717 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003718 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3719 if (i < 0)
3720 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003721 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003722 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003723
3724 /*
3725 * Free each page after tx, so the only ref left is
3726 * held by the network stack
3727 */
3728 for (; i > 0; i--)
3729 put_page(pages[i-1]);
3730
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003731 /*
3732 * Acl update can result in inode attribute update.
3733 * so mark the attribute cache invalid.
3734 */
3735 spin_lock(&inode->i_lock);
3736 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3737 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003738 nfs_access_zap_cache(inode);
3739 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003740 return ret;
3741}
3742
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003743static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3744{
3745 struct nfs4_exception exception = { };
3746 int err;
3747 do {
3748 err = nfs4_handle_exception(NFS_SERVER(inode),
3749 __nfs4_proc_set_acl(inode, buf, buflen),
3750 &exception);
3751 } while (exception.retry);
3752 return err;
3753}
3754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003756nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003758 struct nfs_client *clp = server->nfs_client;
3759
3760 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return 0;
3762 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003763 case -NFS4ERR_ADMIN_REVOKED:
3764 case -NFS4ERR_BAD_STATEID:
3765 case -NFS4ERR_OPENMODE:
3766 if (state == NULL)
3767 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003768 nfs4_schedule_stateid_recovery(server, state);
3769 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003770 case -NFS4ERR_EXPIRED:
3771 if (state != NULL)
3772 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003774 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003775 nfs4_schedule_lease_recovery(clp);
3776 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003777#if defined(CONFIG_NFS_V4_1)
3778 case -NFS4ERR_BADSESSION:
3779 case -NFS4ERR_BADSLOT:
3780 case -NFS4ERR_BAD_HIGH_SLOT:
3781 case -NFS4ERR_DEADSESSION:
3782 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3783 case -NFS4ERR_SEQ_FALSE_RETRY:
3784 case -NFS4ERR_SEQ_MISORDERED:
3785 dprintk("%s ERROR %d, Reset session\n", __func__,
3786 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003787 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003788 task->tk_status = 0;
3789 return -EAGAIN;
3790#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003792 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003793 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003794 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3796 task->tk_status = 0;
3797 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003798 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 case -NFS4ERR_OLD_STATEID:
3800 task->tk_status = 0;
3801 return -EAGAIN;
3802 }
3803 task->tk_status = nfs4_map_errors(task->tk_status);
3804 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003805wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003806 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003807 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3808 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3809 task->tk_status = 0;
3810 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003813int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3814 unsigned short port, struct rpc_cred *cred,
3815 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 nfs4_verifier sc_verifier;
3818 struct nfs4_setclientid setclientid = {
3819 .sc_verifier = &sc_verifier,
3820 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003821 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 };
3823 struct rpc_message msg = {
3824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3825 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003826 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003827 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 };
Al Virobc4785c2006-10-19 23:28:51 -07003829 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 int loop = 0;
3831 int status;
3832
Al Virobc4785c2006-10-19 23:28:51 -07003833 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3835 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3836
3837 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003838 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003840 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003841 clp->cl_ipaddr,
3842 rpc_peeraddr2str(clp->cl_rpcclient,
3843 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003844 rpc_peeraddr2str(clp->cl_rpcclient,
3845 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003846 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 clp->cl_id_uniquifier);
3848 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003849 sizeof(setclientid.sc_netid),
3850 rpc_peeraddr2str(clp->cl_rpcclient,
3851 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003853 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003855 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003857 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 if (status != -NFS4ERR_CLID_INUSE)
3859 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003860 if (loop != 0) {
3861 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003863 }
3864 ++loop;
3865 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 }
3867 return status;
3868}
3869
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003870int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003871 struct nfs4_setclientid_res *arg,
3872 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
3874 struct nfs_fsinfo fsinfo;
3875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003877 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003879 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 };
3881 unsigned long now;
3882 int status;
3883
3884 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003885 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (status == 0) {
3887 spin_lock(&clp->cl_lock);
3888 clp->cl_lease_time = fsinfo.lease_time * HZ;
3889 clp->cl_last_renewal = now;
3890 spin_unlock(&clp->cl_lock);
3891 }
3892 return status;
3893}
3894
Trond Myklebustfe650402006-01-03 09:55:18 +01003895struct nfs4_delegreturndata {
3896 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003897 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003898 struct nfs_fh fh;
3899 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003900 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003901 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003902 int rpc_status;
3903};
3904
Trond Myklebustfe650402006-01-03 09:55:18 +01003905static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3906{
3907 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003908
Trond Myklebust14516c32010-07-31 14:29:06 -04003909 if (!nfs4_sequence_done(task, &data->res.seq_res))
3910 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003911
Ricardo Labiaga79708862009-12-07 09:23:21 -05003912 switch (task->tk_status) {
3913 case -NFS4ERR_STALE_STATEID:
3914 case -NFS4ERR_EXPIRED:
3915 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003916 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003917 break;
3918 default:
3919 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3920 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003921 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003922 return;
3923 }
3924 }
3925 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003926}
3927
3928static void nfs4_delegreturn_release(void *calldata)
3929{
Trond Myklebustfe650402006-01-03 09:55:18 +01003930 kfree(calldata);
3931}
3932
Andy Adamson938e1012009-04-01 09:22:28 -04003933#if defined(CONFIG_NFS_V4_1)
3934static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3935{
3936 struct nfs4_delegreturndata *d_data;
3937
3938 d_data = (struct nfs4_delegreturndata *)data;
3939
Trond Myklebust035168a2010-06-16 09:52:26 -04003940 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003941 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003942 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003943 return;
3944 rpc_call_start(task);
3945}
3946#endif /* CONFIG_NFS_V4_1 */
3947
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003948static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003949#if defined(CONFIG_NFS_V4_1)
3950 .rpc_call_prepare = nfs4_delegreturn_prepare,
3951#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003952 .rpc_call_done = nfs4_delegreturn_done,
3953 .rpc_release = nfs4_delegreturn_release,
3954};
3955
Trond Myklebuste6f81072008-01-24 18:14:34 -05003956static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003957{
3958 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003959 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003960 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3963 .rpc_cred = cred,
3964 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003965 struct rpc_task_setup task_setup_data = {
3966 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003967 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003968 .callback_ops = &nfs4_delegreturn_ops,
3969 .flags = RPC_TASK_ASYNC,
3970 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003971 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003972
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003973 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003974 if (data == NULL)
3975 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003976 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003977 data->args.fhandle = &data->fh;
3978 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003979 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003980 nfs_copy_fh(&data->fh, NFS_FH(inode));
3981 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003982 data->res.fattr = &data->fattr;
3983 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003984 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003985 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003986 data->rpc_status = 0;
3987
Trond Myklebustc970aa82007-07-14 15:39:59 -04003988 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003989 msg.rpc_argp = &data->args;
3990 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003991 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003992 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003993 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003994 if (!issync)
3995 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003996 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003997 if (status != 0)
3998 goto out;
3999 status = data->rpc_status;
4000 if (status != 0)
4001 goto out;
4002 nfs_refresh_inode(inode, &data->fattr);
4003out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004004 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Trond Myklebuste6f81072008-01-24 18:14:34 -05004008int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
4010 struct nfs_server *server = NFS_SERVER(inode);
4011 struct nfs4_exception exception = { };
4012 int err;
4013 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004014 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 switch (err) {
4016 case -NFS4ERR_STALE_STATEID:
4017 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 case 0:
4019 return 0;
4020 }
4021 err = nfs4_handle_exception(server, err, &exception);
4022 } while (exception.retry);
4023 return err;
4024}
4025
4026#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4027#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4028
4029/*
4030 * sleep, with exponential backoff, and retry the LOCK operation.
4031 */
4032static unsigned long
4033nfs4_set_lock_task_retry(unsigned long timeout)
4034{
Jeff Laytond3103102011-12-01 22:44:39 +01004035 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 timeout <<= 1;
4037 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4038 return NFS4_LOCK_MAXTIMEOUT;
4039 return timeout;
4040}
4041
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4043{
4044 struct inode *inode = state->inode;
4045 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004046 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004047 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004049 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004051 struct nfs_lockt_res res = {
4052 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
4054 struct rpc_message msg = {
4055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4056 .rpc_argp = &arg,
4057 .rpc_resp = &res,
4058 .rpc_cred = state->owner->so_cred,
4059 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 struct nfs4_lock_state *lsp;
4061 int status;
4062
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004064 status = nfs4_set_lock_state(state, request);
4065 if (status != 0)
4066 goto out;
4067 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004068 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004069 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004070 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004071 switch (status) {
4072 case 0:
4073 request->fl_type = F_UNLCK;
4074 break;
4075 case -NFS4ERR_DENIED:
4076 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004078 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004079out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return status;
4081}
4082
4083static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4084{
4085 struct nfs4_exception exception = { };
4086 int err;
4087
4088 do {
4089 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4090 _nfs4_proc_getlk(state, cmd, request),
4091 &exception);
4092 } while (exception.retry);
4093 return err;
4094}
4095
4096static int do_vfs_lock(struct file *file, struct file_lock *fl)
4097{
4098 int res = 0;
4099 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4100 case FL_POSIX:
4101 res = posix_lock_file_wait(file, fl);
4102 break;
4103 case FL_FLOCK:
4104 res = flock_lock_file_wait(file, fl);
4105 break;
4106 default:
4107 BUG();
4108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return res;
4110}
4111
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004112struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004113 struct nfs_locku_args arg;
4114 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004115 struct nfs4_lock_state *lsp;
4116 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004117 struct file_lock fl;
4118 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004119 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004120};
4121
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004122static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4123 struct nfs_open_context *ctx,
4124 struct nfs4_lock_state *lsp,
4125 struct nfs_seqid *seqid)
4126{
4127 struct nfs4_unlockdata *p;
4128 struct inode *inode = lsp->ls_state->inode;
4129
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004130 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004131 if (p == NULL)
4132 return NULL;
4133 p->arg.fh = NFS_FH(inode);
4134 p->arg.fl = &p->fl;
4135 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004136 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004137 p->arg.stateid = &lsp->ls_stateid;
4138 p->lsp = lsp;
4139 atomic_inc(&lsp->ls_count);
4140 /* Ensure we don't close file until we're done freeing locks! */
4141 p->ctx = get_nfs_open_context(ctx);
4142 memcpy(&p->fl, fl, sizeof(p->fl));
4143 p->server = NFS_SERVER(inode);
4144 return p;
4145}
4146
Trond Myklebust06f814a2006-01-03 09:55:07 +01004147static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004148{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004149 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004150 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004151 nfs4_put_lock_state(calldata->lsp);
4152 put_nfs_open_context(calldata->ctx);
4153 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004154}
4155
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004156static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004157{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004158 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004159
Trond Myklebust14516c32010-07-31 14:29:06 -04004160 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4161 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004162 switch (task->tk_status) {
4163 case 0:
4164 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004165 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004166 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004167 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004168 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004169 case -NFS4ERR_BAD_STATEID:
4170 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004171 case -NFS4ERR_STALE_STATEID:
4172 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004173 break;
4174 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004175 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004176 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004178}
4179
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004180static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004181{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004182 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004184 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004185 return;
4186 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004187 /* Note: exit _without_ running nfs4_locku_done */
4188 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004189 return;
4190 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004191 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004192 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004193 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004194 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004195 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004196 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004199static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004200 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004201 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004202 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004203};
4204
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4206 struct nfs_open_context *ctx,
4207 struct nfs4_lock_state *lsp,
4208 struct nfs_seqid *seqid)
4209{
4210 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004211 struct rpc_message msg = {
4212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4213 .rpc_cred = ctx->cred,
4214 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004215 struct rpc_task_setup task_setup_data = {
4216 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004217 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004218 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004219 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004220 .flags = RPC_TASK_ASYNC,
4221 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222
Frank Filz137d6ac2007-07-09 15:32:29 -07004223 /* Ensure this is an unlock - when canceling a lock, the
4224 * canceled lock is passed in, and it won't be an unlock.
4225 */
4226 fl->fl_type = F_UNLCK;
4227
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4229 if (data == NULL) {
4230 nfs_free_seqid(seqid);
4231 return ERR_PTR(-ENOMEM);
4232 }
4233
Trond Myklebust9d12b212012-01-17 22:04:25 -05004234 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004235 msg.rpc_argp = &data->arg;
4236 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004237 task_setup_data.callback_data = data;
4238 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239}
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4242{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004243 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004244 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004245 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004246 struct rpc_task *task;
4247 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004248 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Trond Myklebust9b073572006-06-29 16:38:34 -04004250 status = nfs4_set_lock_state(state, request);
4251 /* Unlock _before_ we do the RPC call */
4252 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004253 down_read(&nfsi->rwsem);
4254 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4255 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004256 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004257 }
4258 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004259 if (status != 0)
4260 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004261 /* Is this a delegated lock? */
4262 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004263 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004264 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004265 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004266 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004267 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004268 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004269 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 status = PTR_ERR(task);
4271 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004272 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004273 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004274 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004275out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004276 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004277 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278}
4279
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004280struct nfs4_lockdata {
4281 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004282 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 struct nfs4_lock_state *lsp;
4284 struct nfs_open_context *ctx;
4285 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004286 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287 int rpc_status;
4288 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004289 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290};
4291
4292static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004293 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4294 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004295{
4296 struct nfs4_lockdata *p;
4297 struct inode *inode = lsp->ls_state->inode;
4298 struct nfs_server *server = NFS_SERVER(inode);
4299
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004300 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004301 if (p == NULL)
4302 return NULL;
4303
4304 p->arg.fh = NFS_FH(inode);
4305 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004306 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004307 if (p->arg.open_seqid == NULL)
4308 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004309 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004311 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004313 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004314 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004315 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004316 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004317 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004318 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004319 atomic_inc(&lsp->ls_count);
4320 p->ctx = get_nfs_open_context(ctx);
4321 memcpy(&p->fl, fl, sizeof(p->fl));
4322 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004323out_free_seqid:
4324 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325out_free:
4326 kfree(p);
4327 return NULL;
4328}
4329
4330static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4331{
4332 struct nfs4_lockdata *data = calldata;
4333 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Harvey Harrison3110ff82008-05-02 13:42:44 -07004335 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004336 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4337 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338 /* Do we need to do an open_to_lock_owner? */
4339 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004340 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4341 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004342 data->arg.open_stateid = &state->stateid;
4343 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004344 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004345 } else
4346 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004347 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004348 if (nfs4_setup_sequence(data->server,
4349 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004350 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004351 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004352 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004353 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004354}
4355
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004356static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4357{
4358 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4359 nfs4_lock_prepare(task, calldata);
4360}
4361
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004362static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4363{
4364 struct nfs4_lockdata *data = calldata;
4365
Harvey Harrison3110ff82008-05-02 13:42:44 -07004366 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004367
Trond Myklebust14516c32010-07-31 14:29:06 -04004368 if (!nfs4_sequence_done(task, &data->res.seq_res))
4369 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004370
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373 if (data->rpc_status == 0)
4374 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4375 else
4376 goto out;
4377 }
4378 if (data->rpc_status == 0) {
4379 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4380 sizeof(data->lsp->ls_stateid.data));
4381 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004382 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004385 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004386}
4387
4388static void nfs4_lock_release(void *calldata)
4389{
4390 struct nfs4_lockdata *data = calldata;
4391
Harvey Harrison3110ff82008-05-02 13:42:44 -07004392 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004393 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004394 if (data->cancelled != 0) {
4395 struct rpc_task *task;
4396 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4397 data->arg.lock_seqid);
4398 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004399 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004400 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004401 } else
4402 nfs_free_seqid(data->arg.lock_seqid);
4403 nfs4_put_lock_state(data->lsp);
4404 put_nfs_open_context(data->ctx);
4405 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004406 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407}
4408
4409static const struct rpc_call_ops nfs4_lock_ops = {
4410 .rpc_call_prepare = nfs4_lock_prepare,
4411 .rpc_call_done = nfs4_lock_done,
4412 .rpc_release = nfs4_lock_release,
4413};
4414
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004415static const struct rpc_call_ops nfs4_recover_lock_ops = {
4416 .rpc_call_prepare = nfs4_recover_lock_prepare,
4417 .rpc_call_done = nfs4_lock_done,
4418 .rpc_release = nfs4_lock_release,
4419};
4420
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004421static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4422{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004423 switch (error) {
4424 case -NFS4ERR_ADMIN_REVOKED:
4425 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004426 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004427 if (new_lock_owner != 0 ||
4428 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004429 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004430 break;
4431 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004432 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004433 case -NFS4ERR_EXPIRED:
4434 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004435 };
4436}
4437
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004438static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004439{
4440 struct nfs4_lockdata *data;
4441 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004442 struct rpc_message msg = {
4443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4444 .rpc_cred = state->owner->so_cred,
4445 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004446 struct rpc_task_setup task_setup_data = {
4447 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004448 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004449 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004450 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004451 .flags = RPC_TASK_ASYNC,
4452 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004453 int ret;
4454
Harvey Harrison3110ff82008-05-02 13:42:44 -07004455 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004456 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004457 fl->fl_u.nfs4_fl.owner,
4458 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004459 if (data == NULL)
4460 return -ENOMEM;
4461 if (IS_SETLKW(cmd))
4462 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004463 if (recovery_type > NFS_LOCK_NEW) {
4464 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004465 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004466 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4467 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004468 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004469 msg.rpc_argp = &data->arg;
4470 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004471 task_setup_data.callback_data = data;
4472 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004473 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004474 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 ret = nfs4_wait_for_completion_rpc_task(task);
4476 if (ret == 0) {
4477 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004478 if (ret)
4479 nfs4_handle_setlk_error(data->server, data->lsp,
4480 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004481 } else
4482 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004483 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004484 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004485 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
4488static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4489{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004490 struct nfs_server *server = NFS_SERVER(state->inode);
4491 struct nfs4_exception exception = { };
4492 int err;
4493
4494 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004495 /* Cache the lock if possible... */
4496 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4497 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004498 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004499 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004500 break;
4501 nfs4_handle_exception(server, err, &exception);
4502 } while (exception.retry);
4503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
4506static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4507{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004508 struct nfs_server *server = NFS_SERVER(state->inode);
4509 struct nfs4_exception exception = { };
4510 int err;
4511
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004512 err = nfs4_set_lock_state(state, request);
4513 if (err != 0)
4514 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004515 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004516 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4517 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004518 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004519 switch (err) {
4520 default:
4521 goto out;
4522 case -NFS4ERR_GRACE:
4523 case -NFS4ERR_DELAY:
4524 nfs4_handle_exception(server, err, &exception);
4525 err = 0;
4526 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004527 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004528out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004529 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004532#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004533static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004534{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004535 int status, ret = NFS_OK;
4536 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004537 struct nfs_server *server = NFS_SERVER(state->inode);
4538
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004539 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4540 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4541 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4542 if (status != NFS_OK) {
4543 nfs41_free_stateid(server, &lsp->ls_stateid);
4544 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4545 ret = status;
4546 }
4547 }
4548 };
4549
4550 return ret;
4551}
4552
4553static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4554{
4555 int status = NFS_OK;
4556
4557 if (test_bit(LK_STATE_IN_USE, &state->flags))
4558 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004559 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004560 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004561 return nfs4_lock_expired(state, request);
4562}
4563#endif
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4566{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004567 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004568 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004569 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004571 if ((fl_flags & FL_POSIX) &&
4572 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4573 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004574 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004575 status = nfs4_set_lock_state(state, request);
4576 if (status != 0)
4577 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004578 request->fl_flags |= FL_ACCESS;
4579 status = do_vfs_lock(request->fl_file, request);
4580 if (status < 0)
4581 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004582 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004583 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004584 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004585 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004586 request->fl_flags = fl_flags & ~FL_SLEEP;
4587 status = do_vfs_lock(request->fl_file, request);
4588 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004589 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004590 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004591 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004592 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004593 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004594 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004595 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004596 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4597 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004598out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004599 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004600out:
4601 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 return status;
4603}
4604
4605static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4606{
4607 struct nfs4_exception exception = { };
4608 int err;
4609
4610 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004611 err = _nfs4_proc_setlk(state, cmd, request);
4612 if (err == -NFS4ERR_DENIED)
4613 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004615 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 } while (exception.retry);
4617 return err;
4618}
4619
4620static int
4621nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4622{
4623 struct nfs_open_context *ctx;
4624 struct nfs4_state *state;
4625 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4626 int status;
4627
4628 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004629 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 state = ctx->state;
4631
4632 if (request->fl_start < 0 || request->fl_end < 0)
4633 return -EINVAL;
4634
Trond Myklebustd9531262009-07-21 19:22:38 -04004635 if (IS_GETLK(cmd)) {
4636 if (state != NULL)
4637 return nfs4_proc_getlk(state, F_GETLK, request);
4638 return 0;
4639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4642 return -EINVAL;
4643
Trond Myklebustd9531262009-07-21 19:22:38 -04004644 if (request->fl_type == F_UNLCK) {
4645 if (state != NULL)
4646 return nfs4_proc_unlck(state, cmd, request);
4647 return 0;
4648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Trond Myklebustd9531262009-07-21 19:22:38 -04004650 if (state == NULL)
4651 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 do {
4653 status = nfs4_proc_setlk(state, cmd, request);
4654 if ((status != -EAGAIN) || IS_SETLK(cmd))
4655 break;
4656 timeout = nfs4_set_lock_task_retry(timeout);
4657 status = -ERESTARTSYS;
4658 if (signalled())
4659 break;
4660 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return status;
4662}
4663
Trond Myklebust888e6942005-11-04 15:38:11 -05004664int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4665{
4666 struct nfs_server *server = NFS_SERVER(state->inode);
4667 struct nfs4_exception exception = { };
4668 int err;
4669
4670 err = nfs4_set_lock_state(state, fl);
4671 if (err != 0)
4672 goto out;
4673 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004674 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004675 switch (err) {
4676 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004677 printk(KERN_ERR "NFS: %s: unhandled error "
4678 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004679 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004680 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004681 goto out;
4682 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004683 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004684 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004685 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004686 nfs4_schedule_lease_recovery(server->nfs_client);
4687 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004688 case -NFS4ERR_BADSESSION:
4689 case -NFS4ERR_BADSLOT:
4690 case -NFS4ERR_BAD_HIGH_SLOT:
4691 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4692 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004693 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004694 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004695 case -ERESTARTSYS:
4696 /*
4697 * The show must go on: exit, but mark the
4698 * stateid as needing recovery.
4699 */
4700 case -NFS4ERR_ADMIN_REVOKED:
4701 case -NFS4ERR_BAD_STATEID:
4702 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004703 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004704 err = 0;
4705 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004706 case -EKEYEXPIRED:
4707 /*
4708 * User RPCSEC_GSS context has expired.
4709 * We cannot recover this stateid now, so
4710 * skip it and allow recovery thread to
4711 * proceed.
4712 */
4713 err = 0;
4714 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004715 case -ENOMEM:
4716 case -NFS4ERR_DENIED:
4717 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4718 err = 0;
4719 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004720 case -NFS4ERR_DELAY:
4721 break;
4722 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004723 err = nfs4_handle_exception(server, err, &exception);
4724 } while (exception.retry);
4725out:
4726 return err;
4727}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004728
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004729static void nfs4_release_lockowner_release(void *calldata)
4730{
4731 kfree(calldata);
4732}
4733
4734const struct rpc_call_ops nfs4_release_lockowner_ops = {
4735 .rpc_release = nfs4_release_lockowner_release,
4736};
4737
4738void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4739{
4740 struct nfs_server *server = lsp->ls_state->owner->so_server;
4741 struct nfs_release_lockowner_args *args;
4742 struct rpc_message msg = {
4743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4744 };
4745
4746 if (server->nfs_client->cl_mvops->minor_version != 0)
4747 return;
4748 args = kmalloc(sizeof(*args), GFP_NOFS);
4749 if (!args)
4750 return;
4751 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004752 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004753 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004754 msg.rpc_argp = args;
4755 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4756}
4757
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004758#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4759
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004760static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4761 const void *buf, size_t buflen,
4762 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004763{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004764 if (strcmp(key, "") != 0)
4765 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004766
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004767 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004768}
4769
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004770static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4771 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004772{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004773 if (strcmp(key, "") != 0)
4774 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004775
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004776 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004777}
4778
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004779static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4780 size_t list_len, const char *name,
4781 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004782{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004783 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004784
J. Bruce Fields096455a2006-03-20 23:23:42 -05004785 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4786 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004787
4788 if (list && len <= list_len)
4789 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004790 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004791}
4792
Andy Adamson533eb462011-06-13 18:25:56 -04004793/*
4794 * nfs_fhget will use either the mounted_on_fileid or the fileid
4795 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004796static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4797{
Andy Adamson533eb462011-06-13 18:25:56 -04004798 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4799 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4800 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004801 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004802 return;
4803
4804 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004805 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004806 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4807 fattr->nlink = 2;
4808}
4809
Trond Myklebust56659e92007-07-17 21:52:39 -04004810int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004811 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004812{
4813 struct nfs_server *server = NFS_SERVER(dir);
4814 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004815 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004816 };
4817 struct nfs4_fs_locations_arg args = {
4818 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004819 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004820 .page = page,
4821 .bitmask = bitmask,
4822 };
Benny Halevy22958462009-04-01 09:22:02 -04004823 struct nfs4_fs_locations_res res = {
4824 .fs_locations = fs_locations,
4825 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004826 struct rpc_message msg = {
4827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4828 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004829 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004830 };
4831 int status;
4832
Harvey Harrison3110ff82008-05-02 13:42:44 -07004833 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004834
4835 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4836 * is not supported */
4837 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4838 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4839 else
4840 bitmask[0] |= FATTR4_WORD0_FILEID;
4841
Trond Myklebustc228fd32007-01-13 02:28:11 -05004842 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004843 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004844 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004846 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004847 return status;
4848}
4849
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004850static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4851{
4852 int status;
4853 struct nfs4_secinfo_arg args = {
4854 .dir_fh = NFS_FH(dir),
4855 .name = name,
4856 };
4857 struct nfs4_secinfo_res res = {
4858 .flavors = flavors,
4859 };
4860 struct rpc_message msg = {
4861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4862 .rpc_argp = &args,
4863 .rpc_resp = &res,
4864 };
4865
4866 dprintk("NFS call secinfo %s\n", name->name);
4867 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4868 dprintk("NFS reply secinfo: %d\n", status);
4869 return status;
4870}
4871
4872int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4873{
4874 struct nfs4_exception exception = { };
4875 int err;
4876 do {
4877 err = nfs4_handle_exception(NFS_SERVER(dir),
4878 _nfs4_proc_secinfo(dir, name, flavors),
4879 &exception);
4880 } while (exception.retry);
4881 return err;
4882}
4883
Andy Adamson557134a2009-04-01 09:21:53 -04004884#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004885/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004886 * Check the exchange flags returned by the server for invalid flags, having
4887 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4888 * DS flags set.
4889 */
4890static int nfs4_check_cl_exchange_flags(u32 flags)
4891{
4892 if (flags & ~EXCHGID4_FLAG_MASK_R)
4893 goto out_inval;
4894 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4895 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4896 goto out_inval;
4897 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4898 goto out_inval;
4899 return NFS_OK;
4900out_inval:
4901 return -NFS4ERR_INVAL;
4902}
4903
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004904static bool
4905nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4906{
4907 if (a->server_scope_sz == b->server_scope_sz &&
4908 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4909 return true;
4910
4911 return false;
4912}
4913
Andy Adamson357f54d2010-12-14 10:11:57 -05004914/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004915 * nfs4_proc_exchange_id()
4916 *
4917 * Since the clientid has expired, all compounds using sessions
4918 * associated with the stale clientid will be returning
4919 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4920 * be in some phase of session reset.
4921 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004922int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004923{
4924 nfs4_verifier verifier;
4925 struct nfs41_exchange_id_args args = {
4926 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004927 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004928 };
4929 struct nfs41_exchange_id_res res = {
4930 .client = clp,
4931 };
4932 int status;
4933 struct rpc_message msg = {
4934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4935 .rpc_argp = &args,
4936 .rpc_resp = &res,
4937 .rpc_cred = cred,
4938 };
4939 __be32 *p;
4940
4941 dprintk("--> %s\n", __func__);
4942 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004943
Benny Halevy99fe60d2009-04-01 09:22:29 -04004944 p = (u32 *)verifier.data;
4945 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4946 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4947 args.verifier = &verifier;
4948
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004949 args.id_len = scnprintf(args.id, sizeof(args.id),
4950 "%s/%s.%s/%u",
4951 clp->cl_ipaddr,
4952 init_utsname()->nodename,
4953 init_utsname()->domainname,
4954 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004955
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004956 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004957 if (unlikely(!res.server_scope)) {
4958 status = -ENOMEM;
4959 goto out;
4960 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004961
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004962 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4963 if (unlikely(!res.impl_id)) {
4964 status = -ENOMEM;
4965 goto out_server_scope;
4966 }
4967
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004968 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004969 if (!status)
4970 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004971
4972 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004973 /* use the most recent implementation id */
4974 kfree(clp->impl_id);
4975 clp->impl_id = res.impl_id;
4976 } else
4977 kfree(res.impl_id);
4978
4979 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004980 if (clp->server_scope &&
4981 !nfs41_same_server_scope(clp->server_scope,
4982 res.server_scope)) {
4983 dprintk("%s: server_scope mismatch detected\n",
4984 __func__);
4985 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4986 kfree(clp->server_scope);
4987 clp->server_scope = NULL;
4988 }
4989
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004990 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004991 clp->server_scope = res.server_scope;
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004992 goto out;
4993 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004994 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004995
4996out_server_scope:
Weston Andros Adamson9edbd952012-02-17 15:20:25 -05004997 kfree(res.server_scope);
4998out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05004999 if (clp->impl_id)
5000 dprintk("%s: Server Implementation ID: "
5001 "domain: %s, name: %s, date: %llu,%u\n",
5002 __func__, clp->impl_id->domain, clp->impl_id->name,
5003 clp->impl_id->date.seconds,
5004 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005005 dprintk("<-- %s status= %d\n", __func__, status);
5006 return status;
5007}
5008
Andy Adamson2050f0c2009-04-01 09:22:30 -04005009struct nfs4_get_lease_time_data {
5010 struct nfs4_get_lease_time_args *args;
5011 struct nfs4_get_lease_time_res *res;
5012 struct nfs_client *clp;
5013};
5014
5015static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5016 void *calldata)
5017{
5018 int ret;
5019 struct nfs4_get_lease_time_data *data =
5020 (struct nfs4_get_lease_time_data *)calldata;
5021
5022 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005023 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005024 /* just setup sequence, do not trigger session recovery
5025 since we're invoked within one */
5026 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005027 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005028 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005029
5030 BUG_ON(ret == -EAGAIN);
5031 rpc_call_start(task);
5032 dprintk("<-- %s\n", __func__);
5033}
5034
5035/*
5036 * Called from nfs4_state_manager thread for session setup, so don't recover
5037 * from sequence operation or clientid errors.
5038 */
5039static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5040{
5041 struct nfs4_get_lease_time_data *data =
5042 (struct nfs4_get_lease_time_data *)calldata;
5043
5044 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005045 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5046 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005047 switch (task->tk_status) {
5048 case -NFS4ERR_DELAY:
5049 case -NFS4ERR_GRACE:
5050 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5051 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5052 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005053 /* fall through */
5054 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005055 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005056 return;
5057 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005058 dprintk("<-- %s\n", __func__);
5059}
5060
5061struct rpc_call_ops nfs4_get_lease_time_ops = {
5062 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5063 .rpc_call_done = nfs4_get_lease_time_done,
5064};
5065
5066int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5067{
5068 struct rpc_task *task;
5069 struct nfs4_get_lease_time_args args;
5070 struct nfs4_get_lease_time_res res = {
5071 .lr_fsinfo = fsinfo,
5072 };
5073 struct nfs4_get_lease_time_data data = {
5074 .args = &args,
5075 .res = &res,
5076 .clp = clp,
5077 };
5078 struct rpc_message msg = {
5079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5080 .rpc_argp = &args,
5081 .rpc_resp = &res,
5082 };
5083 struct rpc_task_setup task_setup = {
5084 .rpc_client = clp->cl_rpcclient,
5085 .rpc_message = &msg,
5086 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005087 .callback_data = &data,
5088 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005089 };
5090 int status;
5091
Trond Myklebust9d12b212012-01-17 22:04:25 -05005092 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005093 dprintk("--> %s\n", __func__);
5094 task = rpc_run_task(&task_setup);
5095
5096 if (IS_ERR(task))
5097 status = PTR_ERR(task);
5098 else {
5099 status = task->tk_status;
5100 rpc_put_task(task);
5101 }
5102 dprintk("<-- %s return %d\n", __func__, status);
5103
5104 return status;
5105}
5106
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005107/*
5108 * Reset a slot table
5109 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005110static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5111 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005112{
Andy Adamson104aeba2010-01-14 17:45:10 -05005113 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005114 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005115 int ret = 0;
5116
Andy Adamson104aeba2010-01-14 17:45:10 -05005117 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5118 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005119
Andy Adamson104aeba2010-01-14 17:45:10 -05005120 /* Does the newly negotiated max_reqs match the existing slot table? */
5121 if (max_reqs != tbl->max_slots) {
5122 ret = -ENOMEM;
5123 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005124 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005125 if (!new)
5126 goto out;
5127 ret = 0;
5128 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005129 }
5130 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005131 if (new) {
5132 tbl->slots = new;
5133 tbl->max_slots = max_reqs;
5134 }
5135 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005136 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005137 spin_unlock(&tbl->slot_tbl_lock);
5138 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5139 tbl, tbl->slots, tbl->max_slots);
5140out:
5141 dprintk("<-- %s: return %d\n", __func__, ret);
5142 return ret;
5143}
5144
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005145/* Destroy the slot table */
5146static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5147{
5148 if (session->fc_slot_table.slots != NULL) {
5149 kfree(session->fc_slot_table.slots);
5150 session->fc_slot_table.slots = NULL;
5151 }
5152 if (session->bc_slot_table.slots != NULL) {
5153 kfree(session->bc_slot_table.slots);
5154 session->bc_slot_table.slots = NULL;
5155 }
5156 return;
5157}
5158
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005159/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005160 * Initialize slot table
5161 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005162static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5163 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005164{
Andy Adamsonfc931582009-04-01 09:22:31 -04005165 struct nfs4_slot *slot;
5166 int ret = -ENOMEM;
5167
5168 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5169
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005170 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005171
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005172 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005173 if (!slot)
5174 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005175 ret = 0;
5176
5177 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005178 tbl->max_slots = max_slots;
5179 tbl->slots = slot;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005180 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
Andy Adamsonfc931582009-04-01 09:22:31 -04005181 spin_unlock(&tbl->slot_tbl_lock);
5182 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5183 tbl, tbl->slots, tbl->max_slots);
5184out:
5185 dprintk("<-- %s: return %d\n", __func__, ret);
5186 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005187}
5188
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005189/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005190 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005191 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005192static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005193{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005194 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005195 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005196
Andy Adamsonaacd5532011-11-09 13:58:21 -05005197 dprintk("--> %s\n", __func__);
5198 /* Fore channel */
5199 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005200 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005201 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5202 if (status) /* -ENOMEM */
5203 return status;
5204 } else {
5205 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005206 if (status)
5207 return status;
5208 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005209 /* Back channel */
5210 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005211 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005212 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005213 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005214 /* Fore and back channel share a connection so get
5215 * both slot tables or neither */
5216 nfs4_destroy_slot_tables(ses);
5217 } else
5218 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005219 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005220}
5221
5222struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5223{
5224 struct nfs4_session *session;
5225 struct nfs4_slot_table *tbl;
5226
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005227 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005228 if (!session)
5229 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005230
Andy Adamson557134a2009-04-01 09:21:53 -04005231 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005232 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005233 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005234 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005235 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005236
5237 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005238 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005239 spin_lock_init(&tbl->slot_tbl_lock);
5240 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005241 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005242
Trond Myklebust1055d762010-06-16 09:52:27 -04005243 session->session_state = 1<<NFS4_SESSION_INITING;
5244
Andy Adamson557134a2009-04-01 09:21:53 -04005245 session->clp = clp;
5246 return session;
5247}
5248
5249void nfs4_destroy_session(struct nfs4_session *session)
5250{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005251 struct rpc_xprt *xprt;
5252
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005253 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005254
5255 rcu_read_lock();
5256 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5257 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005258 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005259 __func__, xprt);
5260 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005261 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005262 kfree(session);
5263}
5264
Andy Adamsonfc931582009-04-01 09:22:31 -04005265/*
5266 * Initialize the values to be used by the client in CREATE_SESSION
5267 * If nfs4_init_session set the fore channel request and response sizes,
5268 * use them.
5269 *
5270 * Set the back channel max_resp_sz_cached to zero to force the client to
5271 * always set csa_cachethis to FALSE because the current implementation
5272 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5273 */
5274static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5275{
5276 struct nfs4_session *session = args->client->cl_session;
5277 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5278 mxresp_sz = session->fc_attrs.max_resp_sz;
5279
5280 if (mxrqst_sz == 0)
5281 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5282 if (mxresp_sz == 0)
5283 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5284 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005285 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5286 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005287 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005288 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005289
5290 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005291 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005292 __func__,
5293 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005294 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005295
5296 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005297 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5298 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5299 args->bc_attrs.max_resp_sz_cached = 0;
5300 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5301 args->bc_attrs.max_reqs = 1;
5302
5303 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5304 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5305 __func__,
5306 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5307 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5308 args->bc_attrs.max_reqs);
5309}
5310
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005311static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005312{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005313 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5314 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5315
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005316 if (rcvd->max_resp_sz > sent->max_resp_sz)
5317 return -EINVAL;
5318 /*
5319 * Our requested max_ops is the minimum we need; we're not
5320 * prepared to break up compounds into smaller pieces than that.
5321 * So, no point even trying to continue if the server won't
5322 * cooperate:
5323 */
5324 if (rcvd->max_ops < sent->max_ops)
5325 return -EINVAL;
5326 if (rcvd->max_reqs == 0)
5327 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005328 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5329 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005330 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005331}
5332
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005333static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5334{
5335 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5336 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005337
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005338 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5339 return -EINVAL;
5340 if (rcvd->max_resp_sz < sent->max_resp_sz)
5341 return -EINVAL;
5342 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5343 return -EINVAL;
5344 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005345 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005346 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005347 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005348 return -EINVAL;
5349 return 0;
5350}
Andy Adamson8d353012009-04-01 09:22:32 -04005351
Andy Adamson8d353012009-04-01 09:22:32 -04005352static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5353 struct nfs4_session *session)
5354{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005355 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005356
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005357 ret = nfs4_verify_fore_channel_attrs(args, session);
5358 if (ret)
5359 return ret;
5360 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005361}
5362
Andy Adamsonfc931582009-04-01 09:22:31 -04005363static int _nfs4_proc_create_session(struct nfs_client *clp)
5364{
5365 struct nfs4_session *session = clp->cl_session;
5366 struct nfs41_create_session_args args = {
5367 .client = clp,
5368 .cb_program = NFS4_CALLBACK,
5369 };
5370 struct nfs41_create_session_res res = {
5371 .client = clp,
5372 };
5373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5375 .rpc_argp = &args,
5376 .rpc_resp = &res,
5377 };
5378 int status;
5379
5380 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005381 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005382
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005383 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005384
Andy Adamson8d353012009-04-01 09:22:32 -04005385 if (!status)
5386 /* Verify the session's negotiated channel_attrs values */
5387 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005388 if (!status) {
5389 /* Increment the clientid slot sequence id */
5390 clp->cl_seqid++;
5391 }
5392
5393 return status;
5394}
5395
5396/*
5397 * Issues a CREATE_SESSION operation to the server.
5398 * It is the responsibility of the caller to verify the session is
5399 * expired before calling this routine.
5400 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005401int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005402{
5403 int status;
5404 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005405 struct nfs4_session *session = clp->cl_session;
5406
5407 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5408
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005409 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005410 if (status)
5411 goto out;
5412
Andy Adamsonaacd5532011-11-09 13:58:21 -05005413 /* Init or reset the session slot tables */
5414 status = nfs4_setup_session_slot_tables(session);
5415 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005416 if (status)
5417 goto out;
5418
5419 ptr = (unsigned *)&session->sess_id.data[0];
5420 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5421 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005422out:
5423 dprintk("<-- %s\n", __func__);
5424 return status;
5425}
5426
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005427/*
5428 * Issue the over-the-wire RPC DESTROY_SESSION.
5429 * The caller must serialize access to this routine.
5430 */
5431int nfs4_proc_destroy_session(struct nfs4_session *session)
5432{
5433 int status = 0;
5434 struct rpc_message msg;
5435
5436 dprintk("--> nfs4_proc_destroy_session\n");
5437
5438 /* session is still being setup */
5439 if (session->clp->cl_cons_state != NFS_CS_READY)
5440 return status;
5441
5442 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5443 msg.rpc_argp = session;
5444 msg.rpc_resp = NULL;
5445 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005446 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005447
5448 if (status)
5449 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005450 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005451 "Session has been destroyed regardless...\n", status);
5452
5453 dprintk("<-- nfs4_proc_destroy_session\n");
5454 return status;
5455}
5456
Trond Myklebustfccba802009-07-21 16:48:07 -04005457int nfs4_init_session(struct nfs_server *server)
5458{
5459 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005460 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005461 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005462 int ret;
5463
5464 if (!nfs4_has_session(clp))
5465 return 0;
5466
Trond Myklebust1055d762010-06-16 09:52:27 -04005467 session = clp->cl_session;
5468 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5469 return 0;
5470
Andy Adamson68bf05e2009-12-15 12:55:02 -05005471 rsize = server->rsize;
5472 if (rsize == 0)
5473 rsize = NFS_MAX_FILE_IO_SIZE;
5474 wsize = server->wsize;
5475 if (wsize == 0)
5476 wsize = NFS_MAX_FILE_IO_SIZE;
5477
Andy Adamson68bf05e2009-12-15 12:55:02 -05005478 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5479 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005480
Trond Myklebustfccba802009-07-21 16:48:07 -04005481 ret = nfs4_recover_expired_lease(server);
5482 if (!ret)
5483 ret = nfs4_check_client_ready(clp);
5484 return ret;
5485}
5486
Andy Adamsond83217c2011-03-01 01:34:17 +00005487int nfs4_init_ds_session(struct nfs_client *clp)
5488{
5489 struct nfs4_session *session = clp->cl_session;
5490 int ret;
5491
5492 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5493 return 0;
5494
5495 ret = nfs4_client_recover_expired_lease(clp);
5496 if (!ret)
5497 /* Test for the DS role */
5498 if (!is_ds_client(clp))
5499 ret = -ENODEV;
5500 if (!ret)
5501 ret = nfs4_check_client_ready(clp);
5502 return ret;
5503
5504}
5505EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5506
5507
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005508/*
5509 * Renew the cl_session lease.
5510 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005511struct nfs4_sequence_data {
5512 struct nfs_client *clp;
5513 struct nfs4_sequence_args args;
5514 struct nfs4_sequence_res res;
5515};
5516
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005517static void nfs41_sequence_release(void *data)
5518{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005519 struct nfs4_sequence_data *calldata = data;
5520 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005521
Alexandros Batsakis71358402010-02-05 03:45:05 -08005522 if (atomic_read(&clp->cl_count) > 1)
5523 nfs4_schedule_state_renewal(clp);
5524 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005525 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005526}
5527
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005528static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5529{
5530 switch(task->tk_status) {
5531 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005532 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5533 return -EAGAIN;
5534 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005535 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005536 }
5537 return 0;
5538}
5539
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005540static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005541{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005542 struct nfs4_sequence_data *calldata = data;
5543 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005544
Trond Myklebust14516c32010-07-31 14:29:06 -04005545 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5546 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005547
5548 if (task->tk_status < 0) {
5549 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005550 if (atomic_read(&clp->cl_count) == 1)
5551 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005552
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005553 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5554 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005555 return;
5556 }
5557 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005558 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005559out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005560 dprintk("<-- %s\n", __func__);
5561}
5562
5563static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5564{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005565 struct nfs4_sequence_data *calldata = data;
5566 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005567 struct nfs4_sequence_args *args;
5568 struct nfs4_sequence_res *res;
5569
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005570 args = task->tk_msg.rpc_argp;
5571 res = task->tk_msg.rpc_resp;
5572
Trond Myklebust9d12b212012-01-17 22:04:25 -05005573 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005574 return;
5575 rpc_call_start(task);
5576}
5577
5578static const struct rpc_call_ops nfs41_sequence_ops = {
5579 .rpc_call_done = nfs41_sequence_call_done,
5580 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005581 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005582};
5583
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005584static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005585{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005586 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005587 struct rpc_message msg = {
5588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5589 .rpc_cred = cred,
5590 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005591 struct rpc_task_setup task_setup_data = {
5592 .rpc_client = clp->cl_rpcclient,
5593 .rpc_message = &msg,
5594 .callback_ops = &nfs41_sequence_ops,
5595 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5596 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005597
Alexandros Batsakis71358402010-02-05 03:45:05 -08005598 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005599 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005600 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005601 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005602 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005603 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005604 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005605 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005606 msg.rpc_argp = &calldata->args;
5607 msg.rpc_resp = &calldata->res;
5608 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005609 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005610
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005611 return rpc_run_task(&task_setup_data);
5612}
5613
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005614static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005615{
5616 struct rpc_task *task;
5617 int ret = 0;
5618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005619 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5620 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005621 task = _nfs41_proc_sequence(clp, cred);
5622 if (IS_ERR(task))
5623 ret = PTR_ERR(task);
5624 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005625 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005626 dprintk("<-- %s status=%d\n", __func__, ret);
5627 return ret;
5628}
5629
5630static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5631{
5632 struct rpc_task *task;
5633 int ret;
5634
5635 task = _nfs41_proc_sequence(clp, cred);
5636 if (IS_ERR(task)) {
5637 ret = PTR_ERR(task);
5638 goto out;
5639 }
5640 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005641 if (!ret) {
5642 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5643
5644 if (task->tk_status == 0)
5645 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005646 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005647 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005648 rpc_put_task(task);
5649out:
5650 dprintk("<-- %s status=%d\n", __func__, ret);
5651 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005652}
5653
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005654struct nfs4_reclaim_complete_data {
5655 struct nfs_client *clp;
5656 struct nfs41_reclaim_complete_args arg;
5657 struct nfs41_reclaim_complete_res res;
5658};
5659
5660static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5661{
5662 struct nfs4_reclaim_complete_data *calldata = data;
5663
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005664 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005665 if (nfs41_setup_sequence(calldata->clp->cl_session,
5666 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005667 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005668 return;
5669
5670 rpc_call_start(task);
5671}
5672
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005673static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5674{
5675 switch(task->tk_status) {
5676 case 0:
5677 case -NFS4ERR_COMPLETE_ALREADY:
5678 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5679 break;
5680 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005681 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005682 /* fall through */
5683 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005684 return -EAGAIN;
5685 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005686 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005687 }
5688 return 0;
5689}
5690
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005691static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5692{
5693 struct nfs4_reclaim_complete_data *calldata = data;
5694 struct nfs_client *clp = calldata->clp;
5695 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5696
5697 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005698 if (!nfs41_sequence_done(task, res))
5699 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005700
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005701 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5702 rpc_restart_call_prepare(task);
5703 return;
5704 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005705 dprintk("<-- %s\n", __func__);
5706}
5707
5708static void nfs4_free_reclaim_complete_data(void *data)
5709{
5710 struct nfs4_reclaim_complete_data *calldata = data;
5711
5712 kfree(calldata);
5713}
5714
5715static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5716 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5717 .rpc_call_done = nfs4_reclaim_complete_done,
5718 .rpc_release = nfs4_free_reclaim_complete_data,
5719};
5720
5721/*
5722 * Issue a global reclaim complete.
5723 */
5724static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5725{
5726 struct nfs4_reclaim_complete_data *calldata;
5727 struct rpc_task *task;
5728 struct rpc_message msg = {
5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5730 };
5731 struct rpc_task_setup task_setup_data = {
5732 .rpc_client = clp->cl_rpcclient,
5733 .rpc_message = &msg,
5734 .callback_ops = &nfs4_reclaim_complete_call_ops,
5735 .flags = RPC_TASK_ASYNC,
5736 };
5737 int status = -ENOMEM;
5738
5739 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005740 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005741 if (calldata == NULL)
5742 goto out;
5743 calldata->clp = clp;
5744 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005745
Trond Myklebust9d12b212012-01-17 22:04:25 -05005746 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005747 msg.rpc_argp = &calldata->arg;
5748 msg.rpc_resp = &calldata->res;
5749 task_setup_data.callback_data = calldata;
5750 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005751 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005752 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005753 goto out;
5754 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005755 status = nfs4_wait_for_completion_rpc_task(task);
5756 if (status == 0)
5757 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005758 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005759 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005760out:
5761 dprintk("<-- %s status=%d\n", __func__, status);
5762 return status;
5763}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005764
5765static void
5766nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5767{
5768 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005769 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005770
5771 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005772 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5773 * right now covering the LAYOUTGET we are about to send.
5774 * However, that is not so catastrophic, and there seems
5775 * to be no way to prevent it completely.
5776 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005777 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005778 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005779 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005780 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5781 NFS_I(lgp->args.inode)->layout,
5782 lgp->args.ctx->state)) {
5783 rpc_exit(task, NFS4_OK);
5784 return;
5785 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005786 rpc_call_start(task);
5787}
5788
5789static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5790{
5791 struct nfs4_layoutget *lgp = calldata;
5792 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5793
5794 dprintk("--> %s\n", __func__);
5795
5796 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5797 return;
5798
5799 switch (task->tk_status) {
5800 case 0:
5801 break;
5802 case -NFS4ERR_LAYOUTTRYLATER:
5803 case -NFS4ERR_RECALLCONFLICT:
5804 task->tk_status = -NFS4ERR_DELAY;
5805 /* Fall through */
5806 default:
5807 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5808 rpc_restart_call_prepare(task);
5809 return;
5810 }
5811 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005812 dprintk("<-- %s\n", __func__);
5813}
5814
5815static void nfs4_layoutget_release(void *calldata)
5816{
5817 struct nfs4_layoutget *lgp = calldata;
5818
5819 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005820 put_nfs_open_context(lgp->args.ctx);
5821 kfree(calldata);
5822 dprintk("<-- %s\n", __func__);
5823}
5824
5825static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5826 .rpc_call_prepare = nfs4_layoutget_prepare,
5827 .rpc_call_done = nfs4_layoutget_done,
5828 .rpc_release = nfs4_layoutget_release,
5829};
5830
5831int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5832{
5833 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5834 struct rpc_task *task;
5835 struct rpc_message msg = {
5836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5837 .rpc_argp = &lgp->args,
5838 .rpc_resp = &lgp->res,
5839 };
5840 struct rpc_task_setup task_setup_data = {
5841 .rpc_client = server->client,
5842 .rpc_message = &msg,
5843 .callback_ops = &nfs4_layoutget_call_ops,
5844 .callback_data = lgp,
5845 .flags = RPC_TASK_ASYNC,
5846 };
5847 int status = 0;
5848
5849 dprintk("--> %s\n", __func__);
5850
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005851 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005852 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005853 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005854 task = rpc_run_task(&task_setup_data);
5855 if (IS_ERR(task))
5856 return PTR_ERR(task);
5857 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005858 if (status == 0)
5859 status = task->tk_status;
5860 if (status == 0)
5861 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005862 rpc_put_task(task);
5863 dprintk("<-- %s status=%d\n", __func__, status);
5864 return status;
5865}
5866
Benny Halevycbe82602011-05-22 19:52:37 +03005867static void
5868nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5869{
5870 struct nfs4_layoutreturn *lrp = calldata;
5871
5872 dprintk("--> %s\n", __func__);
5873 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005874 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005875 return;
5876 rpc_call_start(task);
5877}
5878
5879static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5880{
5881 struct nfs4_layoutreturn *lrp = calldata;
5882 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005883 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005884
5885 dprintk("--> %s\n", __func__);
5886
5887 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5888 return;
5889
5890 server = NFS_SERVER(lrp->args.inode);
5891 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005892 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005893 return;
5894 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005895 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005896 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005897 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005898 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005899 } else
5900 BUG_ON(!list_empty(&lo->plh_segs));
5901 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005902 lo->plh_block_lgets--;
5903 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005904 dprintk("<-- %s\n", __func__);
5905}
5906
5907static void nfs4_layoutreturn_release(void *calldata)
5908{
5909 struct nfs4_layoutreturn *lrp = calldata;
5910
5911 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005912 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005913 kfree(calldata);
5914 dprintk("<-- %s\n", __func__);
5915}
5916
5917static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5918 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5919 .rpc_call_done = nfs4_layoutreturn_done,
5920 .rpc_release = nfs4_layoutreturn_release,
5921};
5922
5923int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5924{
5925 struct rpc_task *task;
5926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5928 .rpc_argp = &lrp->args,
5929 .rpc_resp = &lrp->res,
5930 };
5931 struct rpc_task_setup task_setup_data = {
5932 .rpc_client = lrp->clp->cl_rpcclient,
5933 .rpc_message = &msg,
5934 .callback_ops = &nfs4_layoutreturn_call_ops,
5935 .callback_data = lrp,
5936 };
5937 int status;
5938
5939 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005940 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005941 task = rpc_run_task(&task_setup_data);
5942 if (IS_ERR(task))
5943 return PTR_ERR(task);
5944 status = task->tk_status;
5945 dprintk("<-- %s status=%d\n", __func__, status);
5946 rpc_put_task(task);
5947 return status;
5948}
5949
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005950/*
5951 * Retrieve the list of Data Server devices from the MDS.
5952 */
5953static int _nfs4_getdevicelist(struct nfs_server *server,
5954 const struct nfs_fh *fh,
5955 struct pnfs_devicelist *devlist)
5956{
5957 struct nfs4_getdevicelist_args args = {
5958 .fh = fh,
5959 .layoutclass = server->pnfs_curr_ld->id,
5960 };
5961 struct nfs4_getdevicelist_res res = {
5962 .devlist = devlist,
5963 };
5964 struct rpc_message msg = {
5965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5966 .rpc_argp = &args,
5967 .rpc_resp = &res,
5968 };
5969 int status;
5970
5971 dprintk("--> %s\n", __func__);
5972 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5973 &res.seq_res, 0);
5974 dprintk("<-- %s status=%d\n", __func__, status);
5975 return status;
5976}
5977
5978int nfs4_proc_getdevicelist(struct nfs_server *server,
5979 const struct nfs_fh *fh,
5980 struct pnfs_devicelist *devlist)
5981{
5982 struct nfs4_exception exception = { };
5983 int err;
5984
5985 do {
5986 err = nfs4_handle_exception(server,
5987 _nfs4_getdevicelist(server, fh, devlist),
5988 &exception);
5989 } while (exception.retry);
5990
5991 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5992 err, devlist->num_devs);
5993
5994 return err;
5995}
5996EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5997
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005998static int
5999_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6000{
6001 struct nfs4_getdeviceinfo_args args = {
6002 .pdev = pdev,
6003 };
6004 struct nfs4_getdeviceinfo_res res = {
6005 .pdev = pdev,
6006 };
6007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6009 .rpc_argp = &args,
6010 .rpc_resp = &res,
6011 };
6012 int status;
6013
6014 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006015 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006016 dprintk("<-- %s status=%d\n", __func__, status);
6017
6018 return status;
6019}
6020
6021int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6022{
6023 struct nfs4_exception exception = { };
6024 int err;
6025
6026 do {
6027 err = nfs4_handle_exception(server,
6028 _nfs4_proc_getdeviceinfo(server, pdev),
6029 &exception);
6030 } while (exception.retry);
6031 return err;
6032}
6033EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6034
Andy Adamson863a3c62011-03-23 13:27:54 +00006035static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6036{
6037 struct nfs4_layoutcommit_data *data = calldata;
6038 struct nfs_server *server = NFS_SERVER(data->args.inode);
6039
6040 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006041 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006042 return;
6043 rpc_call_start(task);
6044}
6045
6046static void
6047nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6048{
6049 struct nfs4_layoutcommit_data *data = calldata;
6050 struct nfs_server *server = NFS_SERVER(data->args.inode);
6051
6052 if (!nfs4_sequence_done(task, &data->res.seq_res))
6053 return;
6054
6055 switch (task->tk_status) { /* Just ignore these failures */
6056 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6057 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6058 case NFS4ERR_BADLAYOUT: /* no layout */
6059 case NFS4ERR_GRACE: /* loca_recalim always false */
6060 task->tk_status = 0;
6061 }
6062
6063 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006064 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006065 return;
6066 }
6067
6068 if (task->tk_status == 0)
6069 nfs_post_op_update_inode_force_wcc(data->args.inode,
6070 data->res.fattr);
6071}
6072
6073static void nfs4_layoutcommit_release(void *calldata)
6074{
6075 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006076 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006077 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006078
Andy Adamsondb29c082011-07-30 20:52:38 -04006079 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006080 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006081 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6082 list_del_init(&lseg->pls_lc_list);
6083 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6084 &lseg->pls_flags))
6085 put_lseg(lseg);
6086 }
Peng Tao92407e72011-10-23 20:21:17 -07006087
6088 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6089 smp_mb__after_clear_bit();
6090 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6091
Andy Adamson863a3c62011-03-23 13:27:54 +00006092 put_rpccred(data->cred);
6093 kfree(data);
6094}
6095
6096static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6097 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6098 .rpc_call_done = nfs4_layoutcommit_done,
6099 .rpc_release = nfs4_layoutcommit_release,
6100};
6101
6102int
Andy Adamsonef311532011-03-12 02:58:10 -05006103nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006104{
6105 struct rpc_message msg = {
6106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6107 .rpc_argp = &data->args,
6108 .rpc_resp = &data->res,
6109 .rpc_cred = data->cred,
6110 };
6111 struct rpc_task_setup task_setup_data = {
6112 .task = &data->task,
6113 .rpc_client = NFS_CLIENT(data->args.inode),
6114 .rpc_message = &msg,
6115 .callback_ops = &nfs4_layoutcommit_ops,
6116 .callback_data = data,
6117 .flags = RPC_TASK_ASYNC,
6118 };
6119 struct rpc_task *task;
6120 int status = 0;
6121
6122 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6123 "lbw: %llu inode %lu\n",
6124 data->task.tk_pid, sync,
6125 data->args.lastbytewritten,
6126 data->args.inode->i_ino);
6127
Trond Myklebust9d12b212012-01-17 22:04:25 -05006128 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006129 task = rpc_run_task(&task_setup_data);
6130 if (IS_ERR(task))
6131 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006132 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006133 goto out;
6134 status = nfs4_wait_for_completion_rpc_task(task);
6135 if (status != 0)
6136 goto out;
6137 status = task->tk_status;
6138out:
6139 dprintk("%s: status %d\n", __func__, status);
6140 rpc_put_task(task);
6141 return status;
6142}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006143
6144static int
6145_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6146 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6147{
6148 struct nfs41_secinfo_no_name_args args = {
6149 .style = SECINFO_STYLE_CURRENT_FH,
6150 };
6151 struct nfs4_secinfo_res res = {
6152 .flavors = flavors,
6153 };
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6156 .rpc_argp = &args,
6157 .rpc_resp = &res,
6158 };
6159 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6160}
6161
6162static int
6163nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6164 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6165{
6166 struct nfs4_exception exception = { };
6167 int err;
6168 do {
6169 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6170 switch (err) {
6171 case 0:
6172 case -NFS4ERR_WRONGSEC:
6173 case -NFS4ERR_NOTSUPP:
6174 break;
6175 default:
6176 err = nfs4_handle_exception(server, err, &exception);
6177 }
6178 } while (exception.retry);
6179 return err;
6180}
6181
6182static int
6183nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6184 struct nfs_fsinfo *info)
6185{
6186 int err;
6187 struct page *page;
6188 rpc_authflavor_t flavor;
6189 struct nfs4_secinfo_flavors *flavors;
6190
6191 page = alloc_page(GFP_KERNEL);
6192 if (!page) {
6193 err = -ENOMEM;
6194 goto out;
6195 }
6196
6197 flavors = page_address(page);
6198 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6199
6200 /*
6201 * Fall back on "guess and check" method if
6202 * the server doesn't support SECINFO_NO_NAME
6203 */
6204 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6205 err = nfs4_find_root_sec(server, fhandle, info);
6206 goto out_freepage;
6207 }
6208 if (err)
6209 goto out_freepage;
6210
6211 flavor = nfs_find_best_sec(flavors);
6212 if (err == 0)
6213 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6214
6215out_freepage:
6216 put_page(page);
6217 if (err == -EACCES)
6218 return -EPERM;
6219out:
6220 return err;
6221}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006222
6223static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006224{
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006225 int status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006226 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006227 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006228 };
6229 struct nfs41_test_stateid_res res;
6230 struct rpc_message msg = {
6231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6232 .rpc_argp = &args,
6233 .rpc_resp = &res,
6234 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006235
Trond Myklebust9d12b212012-01-17 22:04:25 -05006236 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006237 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6238
6239 if (status == NFS_OK)
6240 return res.status;
6241 return status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006242}
6243
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006244static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006245{
6246 struct nfs4_exception exception = { };
6247 int err;
6248 do {
6249 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006250 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006251 &exception);
6252 } while (exception.retry);
6253 return err;
6254}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006255
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006256static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006257{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006258 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006259 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006260 };
6261 struct nfs41_free_stateid_res res;
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6264 .rpc_argp = &args,
6265 .rpc_resp = &res,
6266 };
6267
Trond Myklebust9d12b212012-01-17 22:04:25 -05006268 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6269 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006270}
6271
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006272static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006273{
6274 struct nfs4_exception exception = { };
6275 int err;
6276 do {
6277 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006278 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006279 &exception);
6280 } while (exception.retry);
6281 return err;
6282}
Andy Adamson557134a2009-04-01 09:21:53 -04006283#endif /* CONFIG_NFS_V4_1 */
6284
Andy Adamson591d71c2009-04-01 09:22:47 -04006285struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006286 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006287 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 .recover_open = nfs4_open_reclaim,
6289 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006290 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006291 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292};
6293
Andy Adamson591d71c2009-04-01 09:22:47 -04006294#if defined(CONFIG_NFS_V4_1)
6295struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6296 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6297 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6298 .recover_open = nfs4_open_reclaim,
6299 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006300 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006301 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006302 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006303};
6304#endif /* CONFIG_NFS_V4_1 */
6305
6306struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006307 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006308 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 .recover_open = nfs4_open_expired,
6310 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006311 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006312 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313};
6314
Andy Adamson591d71c2009-04-01 09:22:47 -04006315#if defined(CONFIG_NFS_V4_1)
6316struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6317 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6318 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006319 .recover_open = nfs41_open_expired,
6320 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006321 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006322 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006323};
6324#endif /* CONFIG_NFS_V4_1 */
6325
Benny Halevy29fba382009-04-01 09:22:44 -04006326struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6327 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006328 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006329 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006330};
6331
6332#if defined(CONFIG_NFS_V4_1)
6333struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6334 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006335 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006336 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006337};
6338#endif
6339
Trond Myklebust97dc1352010-06-16 09:52:26 -04006340static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6341 .minor_version = 0,
6342 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006343 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006344 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006345 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6346 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6347 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006348};
6349
6350#if defined(CONFIG_NFS_V4_1)
6351static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6352 .minor_version = 1,
6353 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006354 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006355 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006356 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6357 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6358 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006359};
6360#endif
6361
Trond Myklebust97dc1352010-06-16 09:52:26 -04006362const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6363 [0] = &nfs_v4_0_minor_ops,
6364#if defined(CONFIG_NFS_V4_1)
6365 [1] = &nfs_v4_1_minor_ops,
6366#endif
6367};
6368
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006369static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006370 .permission = nfs_permission,
6371 .getattr = nfs_getattr,
6372 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006373 .getxattr = generic_getxattr,
6374 .setxattr = generic_setxattr,
6375 .listxattr = generic_listxattr,
6376 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006377};
6378
David Howells509de812006-08-22 20:06:11 -04006379const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 .version = 4, /* protocol version */
6381 .dentry_ops = &nfs4_dentry_operations,
6382 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006383 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006384 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 .getroot = nfs4_proc_get_root,
6386 .getattr = nfs4_proc_getattr,
6387 .setattr = nfs4_proc_setattr,
6388 .lookup = nfs4_proc_lookup,
6389 .access = nfs4_proc_access,
6390 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 .create = nfs4_proc_create,
6392 .remove = nfs4_proc_remove,
6393 .unlink_setup = nfs4_proc_unlink_setup,
6394 .unlink_done = nfs4_proc_unlink_done,
6395 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006396 .rename_setup = nfs4_proc_rename_setup,
6397 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 .link = nfs4_proc_link,
6399 .symlink = nfs4_proc_symlink,
6400 .mkdir = nfs4_proc_mkdir,
6401 .rmdir = nfs4_proc_remove,
6402 .readdir = nfs4_proc_readdir,
6403 .mknod = nfs4_proc_mknod,
6404 .statfs = nfs4_proc_statfs,
6405 .fsinfo = nfs4_proc_fsinfo,
6406 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006407 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 .decode_dirent = nfs4_decode_dirent,
6409 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006410 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006412 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006414 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006416 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006417 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006418 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006419 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006420 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421};
6422
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006423static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6424 .prefix = XATTR_NAME_NFSV4_ACL,
6425 .list = nfs4_xattr_list_nfs4_acl,
6426 .get = nfs4_xattr_get_nfs4_acl,
6427 .set = nfs4_xattr_set_nfs4_acl,
6428};
6429
6430const struct xattr_handler *nfs4_xattr_handlers[] = {
6431 &nfs4_xattr_nfs4_acl_handler,
6432 NULL
6433};
6434
Trond Myklebustef159e92012-02-06 19:50:40 -05006435module_param(max_session_slots, ushort, 0644);
6436MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6437 "requests the client will negotiate");
6438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439/*
6440 * Local variables:
6441 * c-basic-offset: 8
6442 * End:
6443 */