blob: 88a8b6999b4f4dd434cc5e8e8395bebb2c6d6cdb [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 Myklebustcdd4e682006-01-03 09:55:12 +010075struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010076static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080077static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050079static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040080static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040081static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82 struct nfs_fattr *fattr, struct iattr *sattr,
83 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040084#ifdef CONFIG_NFS_V4_1
85static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
86static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
87#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050089static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Trond Myklebust52567b02009-10-23 14:46:42 -040091 if (err >= -1000)
92 return err;
93 switch (err) {
94 case -NFS4ERR_RESOURCE:
95 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000096 case -NFS4ERR_WRONGSEC:
97 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080098 case -NFS4ERR_BADOWNER:
99 case -NFS4ERR_BADNAME:
100 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400101 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700103 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109/*
110 * This is our standard bitmap for GETATTR requests.
111 */
112const u32 nfs4_fattr_bitmap[2] = {
113 FATTR4_WORD0_TYPE
114 | FATTR4_WORD0_CHANGE
115 | FATTR4_WORD0_SIZE
116 | FATTR4_WORD0_FSID
117 | FATTR4_WORD0_FILEID,
118 FATTR4_WORD1_MODE
119 | FATTR4_WORD1_NUMLINKS
120 | FATTR4_WORD1_OWNER
121 | FATTR4_WORD1_OWNER_GROUP
122 | FATTR4_WORD1_RAWDEV
123 | FATTR4_WORD1_SPACE_USED
124 | FATTR4_WORD1_TIME_ACCESS
125 | FATTR4_WORD1_TIME_METADATA
126 | FATTR4_WORD1_TIME_MODIFY
127};
128
129const u32 nfs4_statfs_bitmap[2] = {
130 FATTR4_WORD0_FILES_AVAIL
131 | FATTR4_WORD0_FILES_FREE
132 | FATTR4_WORD0_FILES_TOTAL,
133 FATTR4_WORD1_SPACE_AVAIL
134 | FATTR4_WORD1_SPACE_FREE
135 | FATTR4_WORD1_SPACE_TOTAL
136};
137
Trond Myklebust4ce79712005-06-22 17:16:21 +0000138const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 FATTR4_WORD0_MAXLINK
140 | FATTR4_WORD0_MAXNAME,
141 0
142};
143
Fred Isamandae100c2011-07-30 20:52:37 -0400144const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 | FATTR4_WORD0_MAXREAD
146 | FATTR4_WORD0_MAXWRITE
147 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700148 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400149 | FATTR4_WORD1_FS_LAYOUT_TYPES,
150 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151};
152
Manoj Naik830b8e32006-06-09 09:34:25 -0400153const u32 nfs4_fs_locations_bitmap[2] = {
154 FATTR4_WORD0_TYPE
155 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_SIZE
157 | FATTR4_WORD0_FSID
158 | FATTR4_WORD0_FILEID
159 | FATTR4_WORD0_FS_LOCATIONS,
160 FATTR4_WORD1_MODE
161 | FATTR4_WORD1_NUMLINKS
162 | FATTR4_WORD1_OWNER
163 | FATTR4_WORD1_OWNER_GROUP
164 | FATTR4_WORD1_RAWDEV
165 | FATTR4_WORD1_SPACE_USED
166 | FATTR4_WORD1_TIME_ACCESS
167 | FATTR4_WORD1_TIME_METADATA
168 | FATTR4_WORD1_TIME_MODIFY
169 | FATTR4_WORD1_MOUNTED_ON_FILEID
170};
171
Al Virobc4785c2006-10-19 23:28:51 -0700172static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct nfs4_readdir_arg *readdir)
174{
Al Viro0dbb4c62006-10-19 23:28:49 -0700175 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 BUG_ON(readdir->count < 80);
178 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000179 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
181 return;
182 }
183
184 readdir->cookie = 0;
185 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
186 if (cookie == 2)
187 return;
188
189 /*
190 * NFSv4 servers do not return entries for '.' and '..'
191 * Therefore, we fake these entries here. We let '.'
192 * have cookie 0 and '..' have cookie 1. Note that
193 * when talking to the server, we always send cookie 0
194 * instead of 1 or 2.
195 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700196 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (cookie == 0) {
199 *p++ = xdr_one; /* next */
200 *p++ = xdr_zero; /* cookie, first word */
201 *p++ = xdr_one; /* cookie, second word */
202 *p++ = xdr_one; /* entry len */
203 memcpy(p, ".\0\0\0", 4); /* entry */
204 p++;
205 *p++ = xdr_one; /* bitmap length */
206 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
207 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400208 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 *p++ = xdr_one; /* next */
212 *p++ = xdr_zero; /* cookie, first word */
213 *p++ = xdr_two; /* cookie, second word */
214 *p++ = xdr_two; /* entry len */
215 memcpy(p, "..\0\0", 4); /* entry */
216 p++;
217 *p++ = xdr_one; /* bitmap length */
218 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
219 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400220 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 readdir->pgbase = (char *)p - (char *)start;
223 readdir->count -= readdir->pgbase;
224 kunmap_atomic(start, KM_USER0);
225}
226
Trond Myklebust65de8722008-12-23 15:21:44 -0500227static int nfs4_wait_clnt_recover(struct nfs_client *clp)
228{
229 int res;
230
231 might_sleep();
232
Trond Myklebuste005e802008-12-23 15:21:48 -0500233 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400234 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500235 return res;
236}
237
238static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
239{
240 int res = 0;
241
242 might_sleep();
243
244 if (*timeout <= 0)
245 *timeout = NFS4_POLL_RETRY_MIN;
246 if (*timeout > NFS4_POLL_RETRY_MAX)
247 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100248 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500249 if (fatal_signal_pending(current))
250 res = -ERESTARTSYS;
251 *timeout <<= 1;
252 return res;
253}
254
255/* This is the error handling routine for processes that are allowed
256 * to sleep.
257 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800258static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500259{
260 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500261 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 int ret = errorcode;
263
264 exception->retry = 0;
265 switch(errorcode) {
266 case 0:
267 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500268 case -NFS4ERR_ADMIN_REVOKED:
269 case -NFS4ERR_BAD_STATEID:
270 case -NFS4ERR_OPENMODE:
271 if (state == NULL)
272 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500273 nfs4_schedule_stateid_recovery(server, state);
274 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400275 case -NFS4ERR_EXPIRED:
276 if (state != NULL)
277 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500279 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500280 nfs4_schedule_lease_recovery(clp);
281 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500282#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400283 case -NFS4ERR_BADSESSION:
284 case -NFS4ERR_BADSLOT:
285 case -NFS4ERR_BAD_HIGH_SLOT:
286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
287 case -NFS4ERR_DEADSESSION:
288 case -NFS4ERR_SEQ_FALSE_RETRY:
289 case -NFS4ERR_SEQ_MISORDERED:
290 dprintk("%s ERROR: %d Reset session\n", __func__,
291 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500292 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400293 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500294 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500295#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500296 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500297 if (exception->timeout > HZ) {
298 /* We have retried a decent amount, time to
299 * fail
300 */
301 ret = -EBUSY;
302 break;
303 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500304 case -NFS4ERR_GRACE:
305 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500306 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500307 ret = nfs4_delay(server->client, &exception->timeout);
308 if (ret != 0)
309 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400310 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500311 case -NFS4ERR_OLD_STATEID:
312 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800313 break;
314 case -NFS4ERR_BADOWNER:
315 /* The following works around a Linux server bug! */
316 case -NFS4ERR_BADNAME:
317 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
318 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
319 exception->retry = 1;
320 printk(KERN_WARNING "NFS: v4 server %s "
321 "does not accept raw "
322 "uid/gids. "
323 "Reenabling the idmapper.\n",
324 server->nfs_client->cl_hostname);
325 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500326 }
327 /* We failed to handle the error */
328 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500329wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500330 ret = nfs4_wait_clnt_recover(clp);
331 if (ret == 0)
332 exception->retry = 1;
333 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500334}
335
336
Trond Myklebust452e9352010-07-31 14:29:06 -0400337static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_lock(&clp->cl_lock);
340 if (time_before(clp->cl_last_renewal,timestamp))
341 clp->cl_last_renewal = timestamp;
342 spin_unlock(&clp->cl_lock);
343}
344
Trond Myklebust452e9352010-07-31 14:29:06 -0400345static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
346{
347 do_renew_lease(server->nfs_client, timestamp);
348}
349
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400350#if defined(CONFIG_NFS_V4_1)
351
Benny Halevy510b8172009-04-01 09:22:14 -0400352/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400353 * nfs4_free_slot - free a slot and efficiently update slot table.
354 *
355 * freeing a slot is trivially done by clearing its respective bit
356 * in the bitmap.
357 * If the freed slotid equals highest_used_slotid we want to update it
358 * so that the server would be able to size down the slot table if needed,
359 * otherwise we know that the highest_used_slotid is still in use.
360 * When updating highest_used_slotid there may be "holes" in the bitmap
361 * so we need to scan down from highest_used_slotid to 0 looking for the now
362 * highest slotid in use.
363 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500364 *
365 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400366 */
367static void
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500368nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369{
370 int slotid = free_slotid;
371
Benny Halevydfb4f3092010-09-24 09:17:01 -0400372 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
375
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500379 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 tbl->highest_used_slotid = slotid;
381 else
382 tbl->highest_used_slotid = -1;
383 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
385 free_slotid, tbl->highest_used_slotid);
386}
387
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388/*
Andy Adamson42acd022011-01-06 02:04:34 +0000389 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390 */
Andy Adamson42acd022011-01-06 02:04:34 +0000391static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800393 struct rpc_task *task;
394
Trond Myklebusta2118c32010-06-16 09:52:26 -0400395 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800396 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
397 if (task)
398 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800399 return;
400 }
401
402 if (ses->fc_slot_table.highest_used_slotid != -1)
403 return;
404
Andy Adamson42acd022011-01-06 02:04:34 +0000405 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
406 complete(&ses->fc_slot_table.complete);
407}
408
409/*
410 * Signal state manager thread if session back channel is drained
411 */
412void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
413{
414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
415 ses->bc_slot_table.highest_used_slotid != -1)
416 return;
417 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
418 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419}
420
Trond Myklebustd185a332010-06-16 09:52:25 -0400421static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400422{
423 struct nfs4_slot_table *tbl;
424
Trond Myklebustd185a332010-06-16 09:52:25 -0400425 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400427 /* just wake up the next guy waiting since
428 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500429 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 return;
Andy Adamson13615872009-04-01 09:22:17 -0400431 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500432
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500434 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000435 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400437 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400438}
439
Trond Myklebust14516c32010-07-31 14:29:06 -0400440static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441{
442 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444
445 /*
446 * sr_status remains 1 if an RPC level error occurred. The server
447 * may or may not have processed the sequence operation..
448 * Proceed as if the server received and processed the sequence
449 * operation.
450 */
451 if (res->sr_status == 1)
452 res->sr_status = NFS_OK;
453
Bryan Schumaker468f8612011-04-18 15:57:32 -0400454 /* don't increment the sequence number if the task wasn't sent */
455 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 goto out;
457
Andy Adamson691daf32009-12-04 15:55:39 -0500458 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 switch (res->sr_status) {
460 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400462 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400463 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400465 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500466 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500467 if (res->sr_status_flags != 0)
468 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400469 break;
470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200475 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
477 res->sr_slot - res->sr_session->fc_slot_table.slots,
478 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
480 default:
481 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483 }
484out:
485 /* The session may be reset by one of the error handlers. */
486 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400487 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 return 1;
489out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400490 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 goto out;
492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
493 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494}
495
Trond Myklebust14516c32010-07-31 14:29:06 -0400496static int nfs4_sequence_done(struct rpc_task *task,
497 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400498{
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 if (res->sr_session == NULL)
500 return 1;
501 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400502}
503
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400504/*
Benny Halevy510b8172009-04-01 09:22:14 -0400505 * nfs4_find_slot - efficiently look for a free slot
506 *
507 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
508 * If found, we mark the slot as used, update the highest_used_slotid,
509 * and respectively set up the sequence operation args.
510 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511 *
512 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400513 */
514static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800515nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400516{
517 int slotid;
518 u8 ret_id = NFS4_MAX_SLOT_TABLE;
519 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
520
Benny Halevy510b8172009-04-01 09:22:14 -0400521 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
523 tbl->max_slots);
524 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
525 if (slotid >= tbl->max_slots)
526 goto out;
527 __set_bit(slotid, tbl->used_slots);
528 if (slotid > tbl->highest_used_slotid)
529 tbl->highest_used_slotid = slotid;
530 ret_id = slotid;
531out:
532 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
533 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400534 return ret_id;
535}
536
Trond Myklebust9d12b212012-01-17 22:04:25 -0500537static void nfs41_init_sequence(struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res, int cache_reply)
539{
540 args->sa_session = NULL;
541 args->sa_cache_this = 0;
542 if (cache_reply)
543 args->sa_cache_this = 1;
544 res->sr_session = NULL;
545 res->sr_slot = NULL;
546}
547
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000548int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 struct nfs4_sequence_args *args,
550 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400551 struct rpc_task *task)
552{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 struct nfs4_slot *slot;
554 struct nfs4_slot_table *tbl;
555 u8 slotid;
556
557 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400559 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 return 0;
561
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400562 tbl = &session->fc_slot_table;
563
564 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400565 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800566 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500567 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400569 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500570 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500571 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400572 }
573
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800574 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
575 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
576 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577 spin_unlock(&tbl->slot_tbl_lock);
578 dprintk("%s enforce FIFO order\n", __func__);
579 return -EAGAIN;
580 }
581
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800582 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 if (slotid == NFS4_MAX_SLOT_TABLE) {
584 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
585 spin_unlock(&tbl->slot_tbl_lock);
586 dprintk("<-- %s: no free slots\n", __func__);
587 return -EAGAIN;
588 }
589 spin_unlock(&tbl->slot_tbl_lock);
590
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800591 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400593 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595
596 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
597
Benny Halevy99fe60d2009-04-01 09:22:29 -0400598 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400599 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400600 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400601 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 /*
603 * sr_status is only set in decode_sequence, and so will remain
604 * set to 1 if an rpc level failure occurs.
605 */
606 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 return 0;
608}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000609EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610
Trond Myklebust035168a2010-06-16 09:52:26 -0400611int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 struct rpc_task *task)
615{
Trond Myklebust035168a2010-06-16 09:52:26 -0400616 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 int ret = 0;
618
Trond Myklebust9d12b212012-01-17 22:04:25 -0500619 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200622 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400623 __func__, session->clp, session, res->sr_slot ?
624 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400625
Trond Myklebust9d12b212012-01-17 22:04:25 -0500626 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627out:
628 dprintk("<-- %s status=%d\n", __func__, ret);
629 return ret;
630}
631
632struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct nfs4_sequence_args *seq_args;
635 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636};
637
638static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
639{
640 struct nfs41_call_sync_data *data = calldata;
641
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
643
644 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500645 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 return;
647 rpc_call_start(task);
648}
649
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800650static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
653 nfs41_call_sync_prepare(task, calldata);
654}
655
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661}
662
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663struct rpc_call_ops nfs41_call_sync_ops = {
664 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666};
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668struct rpc_call_ops nfs41_call_priv_sync_ops = {
669 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
670 .rpc_call_done = nfs41_call_sync_done,
671};
672
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
674 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct rpc_message *msg,
676 struct nfs4_sequence_args *args,
677 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800678 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679{
680 int ret;
681 struct rpc_task *task;
682 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .seq_args = args,
685 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 };
687 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000688 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 .rpc_message = msg,
690 .callback_ops = &nfs41_call_sync_ops,
691 .callback_data = &data
692 };
693
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694 if (privileged)
695 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 task = rpc_run_task(&task_setup);
697 if (IS_ERR(task))
698 ret = PTR_ERR(task);
699 else {
700 ret = task->tk_status;
701 rpc_put_task(task);
702 }
703 return ret;
704}
705
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync_session(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res,
711 int cache_reply)
712{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500713 nfs41_init_sequence(args, res, cache_reply);
714 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715}
716
Trond Myklebustdf896452010-06-16 09:52:26 -0400717#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500718static inline
719void nfs41_init_sequence(struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res, int cache_reply)
721{
722}
723
Trond Myklebust14516c32010-07-31 14:29:06 -0400724static int nfs4_sequence_done(struct rpc_task *task,
725 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400726{
Trond Myklebust14516c32010-07-31 14:29:06 -0400727 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400728}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729#endif /* CONFIG_NFS_V4_1 */
730
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int _nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500738 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400740}
741
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000742static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743int nfs4_call_sync(struct rpc_clnt *clnt,
744 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745 struct rpc_message *msg,
746 struct nfs4_sequence_args *args,
747 struct nfs4_sequence_res *res,
748 int cache_reply)
749{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000750 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
751 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000752}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400753
Trond Myklebust38478b22006-05-25 01:40:57 -0400754static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Trond Myklebust38478b22006-05-25 01:40:57 -0400756 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Trond Myklebust38478b22006-05-25 01:40:57 -0400758 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400759 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700760 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400761 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700762 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400763 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400767 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct nfs_openargs o_arg;
769 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100770 struct nfs_open_confirmargs c_arg;
771 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500772 struct nfs4_string owner_name;
773 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 struct nfs_fattr f_attr;
775 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400777 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400779 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100781 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400782 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100783 int rpc_status;
784 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785};
786
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787
788static void nfs4_init_opendata_res(struct nfs4_opendata *p)
789{
790 p->o_res.f_attr = &p->f_attr;
791 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400792 p->o_res.seqid = p->o_arg.seqid;
793 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400794 p->o_res.server = p->o_arg.server;
795 nfs_fattr_init(&p->f_attr);
796 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500797 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400798}
799
Al Viro82a2c1b2011-06-22 18:30:55 -0400800static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 const struct iattr *attrs,
803 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804{
Al Viro82a2c1b2011-06-22 18:30:55 -0400805 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 struct inode *dir = parent->d_inode;
807 struct nfs_server *server = NFS_SERVER(dir);
808 struct nfs4_opendata *p;
809
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400810 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 if (p == NULL)
812 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400813 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 if (p->o_arg.seqid == NULL)
815 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400816 nfs_sb_active(dentry->d_sb);
817 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->dir = parent;
819 p->owner = sp;
820 atomic_inc(&sp->so_count);
821 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822 p->o_arg.open_flags = flags;
823 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400824 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -0500825 p->o_arg.id = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400826 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 p->o_arg.server = server;
828 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500829 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400831 if (flags & O_CREAT) {
832 u32 *s;
833
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 p->o_arg.u.attrs = &p->attrs;
835 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400836 s = (u32 *) p->o_arg.u.verifier.data;
837 s[0] = jiffies;
838 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100840 p->c_arg.fh = &p->o_res.fh;
841 p->c_arg.stateid = &p->o_res.stateid;
842 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400843 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845 return p;
846err_free:
847 kfree(p);
848err:
849 dput(parent);
850 return NULL;
851}
852
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400855 struct nfs4_opendata *p = container_of(kref,
856 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858
859 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400860 if (p->state != NULL)
861 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400862 nfs4_put_state_owner(p->owner);
863 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400864 dput(p->dentry);
865 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500866 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400867 kfree(p);
868}
869
870static void nfs4_opendata_put(struct nfs4_opendata *p)
871{
872 if (p != NULL)
873 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100874}
875
Trond Myklebust06f814a2006-01-03 09:55:07 +0100876static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
877{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100878 int ret;
879
Trond Myklebust06f814a2006-01-03 09:55:07 +0100880 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100881 return ret;
882}
883
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885{
886 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887
888 if (open_mode & O_EXCL)
889 goto out;
890 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500892 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
893 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 break;
895 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500896 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
897 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400898 break;
899 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500900 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
901 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400902 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400904 return ret;
905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400908{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500909 if (delegation == NULL)
910 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400912 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500913 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400914 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500915 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400916 return 1;
917}
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100920{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100922 case FMODE_WRITE:
923 state->n_wronly++;
924 break;
925 case FMODE_READ:
926 state->n_rdonly++;
927 break;
928 case FMODE_READ|FMODE_WRITE:
929 state->n_rdwr++;
930 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400935{
936 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
937 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
938 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400940 case FMODE_READ:
941 set_bit(NFS_O_RDONLY_STATE, &state->flags);
942 break;
943 case FMODE_WRITE:
944 set_bit(NFS_O_WRONLY_STATE, &state->flags);
945 break;
946 case FMODE_READ|FMODE_WRITE:
947 set_bit(NFS_O_RDWR_STATE, &state->flags);
948 }
949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400952{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400953 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400955 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static 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 -0700959{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 /*
961 * Protect the call to nfs4_state_set_mode_locked and
962 * serialise the stateid update
963 */
964 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400965 if (deleg_stateid != NULL) {
966 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
967 set_bit(NFS_DELEGATED_STATE, &state->flags);
968 }
969 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400971 write_sequnlock(&state->seqlock);
972 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700974 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977static 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 -0500978{
979 struct nfs_inode *nfsi = NFS_I(state->inode);
980 struct nfs_delegation *deleg_cur;
981 int ret = 0;
982
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500984
985 rcu_read_lock();
986 deleg_cur = rcu_dereference(nfsi->delegation);
987 if (deleg_cur == NULL)
988 goto no_delegation;
989
990 spin_lock(&deleg_cur->lock);
991 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500993 goto no_delegation_unlock;
994
995 if (delegation == NULL)
996 delegation = &deleg_cur->stateid;
997 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
998 goto no_delegation_unlock;
999
Trond Myklebustb7391f42008-12-23 15:21:52 -05001000 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001002 ret = 1;
1003no_delegation_unlock:
1004 spin_unlock(&deleg_cur->lock);
1005no_delegation:
1006 rcu_read_unlock();
1007
1008 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001010 ret = 1;
1011 }
1012
1013 return ret;
1014}
1015
1016
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
1019 struct nfs_delegation *delegation;
1020
1021 rcu_read_lock();
1022 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 rcu_read_unlock();
1025 return;
1026 }
1027 rcu_read_unlock();
1028 nfs_inode_return_delegation(inode);
1029}
1030
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032{
1033 struct nfs4_state *state = opendata->state;
1034 struct nfs_inode *nfsi = NFS_I(state->inode);
1035 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1037 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 nfs4_stateid stateid;
1039 int ret = -EAGAIN;
1040
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001043 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 if (can_open_cached(state, fmode, open_mode)) {
1045 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047 goto out_return_state;
1048 }
1049 spin_unlock(&state->owner->so_lock);
1050 }
Trond Myklebust34310432008-12-23 15:21:38 -05001051 rcu_read_lock();
1052 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001053 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001054 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001055 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001056 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 /* Save the delegation */
1058 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1059 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001060 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 if (ret != 0)
1062 goto out;
1063 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001064
1065 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001067 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069out:
1070 return ERR_PTR(ret);
1071out_return_state:
1072 atomic_inc(&state->count);
1073 return state;
1074}
1075
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1077{
1078 struct inode *inode;
1079 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001080 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082
Trond Myklebustaac00a82007-07-05 19:02:21 -04001083 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001084 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 goto out;
1086 }
1087
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001090 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001091 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001092 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001093 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001094 goto err;
1095 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096 state = nfs4_get_open_state(inode, data->owner);
1097 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001098 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001099 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001100 int delegation_flags = 0;
1101
Trond Myklebust003707c2007-07-05 18:07:55 -04001102 rcu_read_lock();
1103 delegation = rcu_dereference(NFS_I(inode)->delegation);
1104 if (delegation)
1105 delegation_flags = delegation->flags;
1106 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001107 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1108 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1109 "returning a delegation for "
1110 "OPEN(CLAIM_DELEGATE_CUR)\n",
1111 NFS_CLIENT(inode)->cl_server);
1112 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001113 nfs_inode_set_delegation(state->inode,
1114 data->owner->so_cred,
1115 &data->o_res);
1116 else
1117 nfs_inode_reclaim_delegation(state->inode,
1118 data->owner->so_cred,
1119 &data->o_res);
1120 }
Trond Myklebust34310432008-12-23 15:21:38 -05001121
1122 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001123 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001125out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001126 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001127err_put_inode:
1128 iput(inode);
1129err:
1130 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001131}
1132
Trond Myklebust864472e2006-01-03 09:55:15 +01001133static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1134{
1135 struct nfs_inode *nfsi = NFS_I(state->inode);
1136 struct nfs_open_context *ctx;
1137
1138 spin_lock(&state->inode->i_lock);
1139 list_for_each_entry(ctx, &nfsi->open_files, list) {
1140 if (ctx->state != state)
1141 continue;
1142 get_nfs_open_context(ctx);
1143 spin_unlock(&state->inode->i_lock);
1144 return ctx;
1145 }
1146 spin_unlock(&state->inode->i_lock);
1147 return ERR_PTR(-ENOENT);
1148}
1149
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001150static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1151{
1152 struct nfs4_opendata *opendata;
1153
Al Viro3d4ff432011-06-22 18:40:12 -04001154 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001155 if (opendata == NULL)
1156 return ERR_PTR(-ENOMEM);
1157 opendata->state = state;
1158 atomic_inc(&state->count);
1159 return opendata;
1160}
1161
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001163{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 int ret;
1166
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 opendata->o_arg.open_flags = 0;
1168 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1170 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1171 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001172 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 if (ret != 0)
1174 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001176 if (IS_ERR(newstate))
1177 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001178 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 return 0;
1181}
1182
1183static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1184{
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 int ret;
1187
1188 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 smp_rmb();
1191 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001192 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 if (newstate != state)
1197 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 }
1199 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001200 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 if (ret != 0)
1203 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 if (newstate != state)
1205 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 }
1207 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001208 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001209 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 if (ret != 0)
1211 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001212 if (newstate != state)
1213 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 /*
1216 * We may have performed cached opens for all three recoveries.
1217 * Check if we need to update the current stateid.
1218 */
1219 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1220 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001221 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1223 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return 0;
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * OPEN_RECLAIM:
1231 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001233static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int status;
1239
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001240 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1241 if (IS_ERR(opendata))
1242 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1244 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001245 rcu_read_lock();
1246 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001247 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001248 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001249 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001250 opendata->o_arg.u.delegation_type = delegation_type;
1251 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001252 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return status;
1254}
1255
Trond Myklebust539cd032007-06-05 11:46:42 -04001256static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 struct nfs_server *server = NFS_SERVER(state->inode);
1259 struct nfs4_exception exception = { };
1260 int err;
1261 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001262 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001263 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001264 break;
1265 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 } while (exception.retry);
1267 return err;
1268}
1269
Trond Myklebust864472e2006-01-03 09:55:15 +01001270static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1271{
1272 struct nfs_open_context *ctx;
1273 int ret;
1274
1275 ctx = nfs4_state_find_open_context(state);
1276 if (IS_ERR(ctx))
1277 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001278 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 put_nfs_open_context(ctx);
1280 return ret;
1281}
1282
Trond Myklebust13437e12007-07-06 15:10:43 -04001283static 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 -07001284{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001292 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001294 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Trond Myklebust13437e12007-07-06 15:10:43 -04001299int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001302 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int err;
1304 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001305 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 switch (err) {
1307 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001308 case -ENOENT:
1309 case -ESTALE:
1310 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001311 case -NFS4ERR_BADSESSION:
1312 case -NFS4ERR_BADSLOT:
1313 case -NFS4ERR_BAD_HIGH_SLOT:
1314 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1315 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001316 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case -NFS4ERR_STALE_CLIENTID:
1319 case -NFS4ERR_STALE_STATEID:
1320 case -NFS4ERR_EXPIRED:
1321 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001322 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 goto out;
1324 case -ERESTARTSYS:
1325 /*
1326 * The show must go on: exit, but mark the
1327 * stateid as needing recovery.
1328 */
1329 case -NFS4ERR_ADMIN_REVOKED:
1330 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001331 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001332 case -EKEYEXPIRED:
1333 /*
1334 * User RPCSEC_GSS context has expired.
1335 * We cannot recover this stateid now, so
1336 * skip it and allow recovery thread to
1337 * proceed.
1338 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 case -ENOMEM:
1340 err = 0;
1341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 err = nfs4_handle_exception(server, err, &exception);
1344 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return err;
1347}
1348
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001349static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1350{
1351 struct nfs4_opendata *data = calldata;
1352
1353 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001354 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001355 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1356 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001357 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001358 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001359 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001360 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361}
1362
1363static void nfs4_open_confirm_release(void *calldata)
1364{
1365 struct nfs4_opendata *data = calldata;
1366 struct nfs4_state *state = NULL;
1367
1368 /* If this request hasn't been cancelled, do nothing */
1369 if (data->cancelled == 0)
1370 goto out_free;
1371 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001372 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001373 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001375 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001376 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001377out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001378 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379}
1380
1381static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382 .rpc_call_done = nfs4_open_confirm_done,
1383 .rpc_release = nfs4_open_confirm_release,
1384};
1385
1386/*
1387 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1388 */
1389static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1390{
1391 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1392 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001393 struct rpc_message msg = {
1394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1395 .rpc_argp = &data->c_arg,
1396 .rpc_resp = &data->c_res,
1397 .rpc_cred = data->owner->so_cred,
1398 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001399 struct rpc_task_setup task_setup_data = {
1400 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001401 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 .callback_ops = &nfs4_open_confirm_ops,
1403 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001404 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 .flags = RPC_TASK_ASYNC,
1406 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int status;
1408
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001409 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001410 data->rpc_done = 0;
1411 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001414 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001415 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001416 status = nfs4_wait_for_completion_rpc_task(task);
1417 if (status != 0) {
1418 data->cancelled = 1;
1419 smp_wmb();
1420 } else
1421 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001422 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return status;
1424}
1425
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 struct nfs4_opendata *data = calldata;
1429 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1432 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 /*
1434 * Check if we still need to send an OPEN call, or if we can use
1435 * a delegation instead.
1436 */
1437 if (data->state != NULL) {
1438 struct nfs_delegation *delegation;
1439
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001441 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001442 rcu_read_lock();
1443 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001444 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1445 can_open_delegated(delegation, data->o_arg.fmode))
1446 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001447 rcu_read_unlock();
1448 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 /* Update sequence id. */
Trond Myklebust48c22eb2012-01-17 22:04:25 -05001450 data->o_arg.id = sp->so_seqid.owner_id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001451 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001452 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001454 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1455 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001456 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001457 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001458 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001459 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001460 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001461 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001462 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001463unlock_no_action:
1464 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465out_no_action:
1466 task->tk_action = NULL;
1467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001470static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1471{
1472 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1473 nfs4_open_prepare(task, calldata);
1474}
1475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476static void nfs4_open_done(struct rpc_task *task, void *calldata)
1477{
1478 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001481
Trond Myklebust14516c32010-07-31 14:29:06 -04001482 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1483 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001484
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 if (task->tk_status == 0) {
1486 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 case S_IFREG:
1488 break;
1489 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 break;
1492 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494 break;
1495 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001498 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001499 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1500 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001501 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001502 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001504
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505static void nfs4_open_release(void *calldata)
1506{
1507 struct nfs4_opendata *data = calldata;
1508 struct nfs4_state *state = NULL;
1509
1510 /* If this request hasn't been cancelled, do nothing */
1511 if (data->cancelled == 0)
1512 goto out_free;
1513 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001514 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515 goto out_free;
1516 /* In case we need an open_confirm, no cleanup! */
1517 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1518 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001519 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001520 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001521 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001523 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524}
1525
1526static const struct rpc_call_ops nfs4_open_ops = {
1527 .rpc_call_prepare = nfs4_open_prepare,
1528 .rpc_call_done = nfs4_open_done,
1529 .rpc_release = nfs4_open_release,
1530};
1531
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001532static const struct rpc_call_ops nfs4_recover_open_ops = {
1533 .rpc_call_prepare = nfs4_recover_open_prepare,
1534 .rpc_call_done = nfs4_open_done,
1535 .rpc_release = nfs4_open_release,
1536};
1537
1538static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539{
1540 struct inode *dir = data->dir->d_inode;
1541 struct nfs_server *server = NFS_SERVER(dir);
1542 struct nfs_openargs *o_arg = &data->o_arg;
1543 struct nfs_openres *o_res = &data->o_res;
1544 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 struct rpc_message msg = {
1546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1547 .rpc_argp = o_arg,
1548 .rpc_resp = o_res,
1549 .rpc_cred = data->owner->so_cred,
1550 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001551 struct rpc_task_setup task_setup_data = {
1552 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001553 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001554 .callback_ops = &nfs4_open_ops,
1555 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001556 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 .flags = RPC_TASK_ASYNC,
1558 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 int status;
1560
Trond Myklebust9d12b212012-01-17 22:04:25 -05001561 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001562 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001563 data->rpc_done = 0;
1564 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001565 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001566 if (isrecover)
1567 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001568 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001569 if (IS_ERR(task))
1570 return PTR_ERR(task);
1571 status = nfs4_wait_for_completion_rpc_task(task);
1572 if (status != 0) {
1573 data->cancelled = 1;
1574 smp_wmb();
1575 } else
1576 status = data->rpc_status;
1577 rpc_put_task(task);
1578
1579 return status;
1580}
1581
1582static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1583{
1584 struct inode *dir = data->dir->d_inode;
1585 struct nfs_openres *o_res = &data->o_res;
1586 int status;
1587
1588 status = nfs4_run_open_task(data, 1);
1589 if (status != 0 || !data->rpc_done)
1590 return status;
1591
Trond Myklebust6926afd2012-01-07 13:22:46 -05001592 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1593
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001594 nfs_refresh_inode(dir, o_res->dir_attr);
1595
1596 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1597 status = _nfs4_proc_open_confirm(data);
1598 if (status != 0)
1599 return status;
1600 }
1601
1602 return status;
1603}
1604
1605/*
1606 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1607 */
1608static int _nfs4_proc_open(struct nfs4_opendata *data)
1609{
1610 struct inode *dir = data->dir->d_inode;
1611 struct nfs_server *server = NFS_SERVER(dir);
1612 struct nfs_openargs *o_arg = &data->o_arg;
1613 struct nfs_openres *o_res = &data->o_res;
1614 int status;
1615
1616 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001617 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001618 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001619 if (status != 0) {
1620 if (status == -NFS4ERR_BADNAME &&
1621 !(o_arg->open_flags & O_CREAT))
1622 return -ENOENT;
1623 return status;
1624 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001625
Trond Myklebust6926afd2012-01-07 13:22:46 -05001626 nfs_fattr_map_and_free_names(server, &data->f_attr);
1627
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001628 if (o_arg->open_flags & O_CREAT) {
1629 update_changeattr(dir, &o_res->cinfo);
1630 nfs_post_op_update_inode(dir, o_res->dir_attr);
1631 } else
1632 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001633 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1634 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001636 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001641 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Andy Adamsond83217c2011-03-01 01:34:17 +00001645static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001646{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001647 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001648 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001649
Trond Myklebusta78cb572009-08-09 15:06:19 -04001650 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001651 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001652 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001653 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001654 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1655 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001656 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001657 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001658 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001659 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001660 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001661}
1662
Andy Adamsond83217c2011-03-01 01:34:17 +00001663static int nfs4_recover_expired_lease(struct nfs_server *server)
1664{
1665 return nfs4_client_recover_expired_lease(server->nfs_client);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
1669 * OPEN_EXPIRED:
1670 * reclaim state on the server after a network partition.
1671 * Assumes caller holds the appropriate lock
1672 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001673static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001675 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001678 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1679 if (IS_ERR(opendata))
1680 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001682 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001683 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001688static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001689{
Trond Myklebust539cd032007-06-05 11:46:42 -04001690 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691 struct nfs4_exception exception = { };
1692 int err;
1693
1694 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001695 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001696 switch (err) {
1697 default:
1698 goto out;
1699 case -NFS4ERR_GRACE:
1700 case -NFS4ERR_DELAY:
1701 nfs4_handle_exception(server, err, &exception);
1702 err = 0;
1703 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001705out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001706 return err;
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 ctx = nfs4_state_find_open_context(state);
1715 if (IS_ERR(ctx))
1716 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001717 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001718 put_nfs_open_context(ctx);
1719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001722#if defined(CONFIG_NFS_V4_1)
1723static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1724{
1725 int status;
1726 struct nfs_server *server = NFS_SERVER(state->inode);
1727
1728 status = nfs41_test_stateid(server, state);
1729 if (status == NFS_OK)
1730 return 0;
1731 nfs41_free_stateid(server, state);
1732 return nfs4_open_expired(sp, state);
1733}
1734#endif
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001737 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1738 * fields corresponding to attributes that were used to store the verifier.
1739 * Make sure we clobber those fields in the later setattr call
1740 */
1741static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1742{
1743 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1744 !(sattr->ia_valid & ATTR_ATIME_SET))
1745 sattr->ia_valid |= ATTR_ATIME;
1746
1747 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1748 !(sattr->ia_valid & ATTR_MTIME_SET))
1749 sattr->ia_valid |= ATTR_MTIME;
1750}
1751
1752/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001755static 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 -07001756{
1757 struct nfs4_state_owner *sp;
1758 struct nfs4_state *state = NULL;
1759 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001760 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001761 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001765 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1766 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1768 goto out_err;
1769 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001770 status = nfs4_recover_expired_lease(server);
1771 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001772 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001773 if (dentry->d_inode != NULL)
1774 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001775 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001776 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001777 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001778 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Al Viro82a2c1b2011-06-22 18:30:55 -04001780 if (dentry->d_inode != NULL)
1781 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001782
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001785 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001788 status = PTR_ERR(state);
1789 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001790 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001791 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001792 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001793
1794 if (opendata->o_arg.open_flags & O_EXCL) {
1795 nfs4_exclusive_attrset(opendata, sattr);
1796
1797 nfs_fattr_init(opendata->o_res.f_attr);
1798 status = nfs4_do_setattr(state->inode, cred,
1799 opendata->o_res.f_attr, sattr,
1800 state);
1801 if (status == 0)
1802 nfs_setattr_update_inode(state->inode, sattr);
1803 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1804 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001805 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 *res = state;
1808 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001809err_opendata_put:
1810 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001811err_put_state_owner:
1812 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 *res = NULL;
1815 return status;
1816}
1817
1818
Al Viro82a2c1b2011-06-22 18:30:55 -04001819static 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 -07001820{
1821 struct nfs4_exception exception = { };
1822 struct nfs4_state *res;
1823 int status;
1824
1825 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001826 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (status == 0)
1828 break;
1829 /* NOTE: BAD_SEQID means the server and client disagree about the
1830 * book-keeping w.r.t. state-changing operations
1831 * (OPEN/CLOSE/LOCK/LOCKU...)
1832 * It is actually a sign of a bug on the client or on the server.
1833 *
1834 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001835 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 * have unhashed the old state_owner for us, and that we can
1837 * therefore safely retry using a new one. We should still warn
1838 * the user though...
1839 */
1840 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001841 printk(KERN_WARNING "NFS: v4 server %s "
1842 " returned a bad sequence-id error!\n",
1843 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 exception.retry = 1;
1845 continue;
1846 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001847 /*
1848 * BAD_STATEID on OPEN means that the server cancelled our
1849 * state before it received the OPEN_CONFIRM.
1850 * Recover by retrying the request as per the discussion
1851 * on Page 181 of RFC3530.
1852 */
1853 if (status == -NFS4ERR_BAD_STATEID) {
1854 exception.retry = 1;
1855 continue;
1856 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857 if (status == -EAGAIN) {
1858 /* We must have found a delegation */
1859 exception.retry = 1;
1860 continue;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1863 status, &exception));
1864 } while (exception.retry);
1865 return res;
1866}
1867
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001868static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1869 struct nfs_fattr *fattr, struct iattr *sattr,
1870 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001872 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001874 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .iap = sattr,
1876 .server = server,
1877 .bitmask = server->attr_bitmask,
1878 };
1879 struct nfs_setattrres res = {
1880 .fattr = fattr,
1881 .server = server,
1882 };
1883 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1885 .rpc_argp = &arg,
1886 .rpc_resp = &res,
1887 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001889 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001890 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Trond Myklebust0e574af2005-10-27 22:12:38 -04001892 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001894 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1895 /* Use that stateid */
1896 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001897 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001898 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1900
Bryan Schumaker7c513052011-03-24 17:12:24 +00001901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001902 if (status == 0 && state != NULL)
1903 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001907static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908 struct nfs_fattr *fattr, struct iattr *sattr,
1909 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001911 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs4_exception exception = { };
1913 int err;
1914 do {
1915 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001916 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 &exception);
1918 } while (exception.retry);
1919 return err;
1920}
1921
1922struct nfs4_closedata {
1923 struct inode *inode;
1924 struct nfs4_state *state;
1925 struct nfs_closeargs arg;
1926 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001927 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001929 bool roc;
1930 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931};
1932
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001934{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 struct nfs4_closedata *calldata = data;
1936 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001937 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001938
Fred Isamanf7e89172011-01-06 11:36:32 +00001939 if (calldata->roc)
1940 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001941 nfs4_put_open_state(calldata->state);
1942 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001943 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001944 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001945 kfree(calldata);
1946}
1947
Trond Myklebust88069f72009-12-08 08:33:16 -05001948static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1949 fmode_t fmode)
1950{
1951 spin_lock(&state->owner->so_lock);
1952 if (!(fmode & FMODE_READ))
1953 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1954 if (!(fmode & FMODE_WRITE))
1955 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1956 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1957 spin_unlock(&state->owner->so_lock);
1958}
1959
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001960static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001962 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct nfs_server *server = NFS_SERVER(calldata->inode);
1965
Trond Myklebust14516c32010-07-31 14:29:06 -04001966 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* hmm. we are done with the inode, and in the process of freeing
1969 * the state_owner. we keep this around to process errors
1970 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 switch (task->tk_status) {
1972 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001973 if (calldata->roc)
1974 pnfs_roc_set_barrier(state->inode,
1975 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001976 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001977 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001978 nfs4_close_clear_stateid_flags(state,
1979 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001982 case -NFS4ERR_OLD_STATEID:
1983 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001985 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001988 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1989 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001991 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001992 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001995static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001997 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001998 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001999 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002000
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002001 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002002 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002003
Trond Myklebust88069f72009-12-08 08:33:16 -05002004 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2005 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002006 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002007 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002008 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002009 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002010 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2011 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2012 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002013 }
2014 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002015 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2016 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2017 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002018 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002019 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002020 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002021
2022 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002023 /* Note: exit _without_ calling nfs4_close_done */
2024 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002025 return;
2026 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002027
Fred Isamanf7e89172011-01-06 11:36:32 +00002028 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002029 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002030 if (calldata->roc &&
2031 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2032 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2033 task, NULL);
2034 return;
2035 }
2036 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002037
Trond Myklebust516a6af2005-10-27 22:12:41 -04002038 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002039 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002040 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002041 &calldata->arg.seq_args,
2042 &calldata->res.seq_res,
2043 task))
Andy Adamson19ddab02009-04-01 09:22:20 -04002044 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002045 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002048static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002049 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002050 .rpc_call_done = nfs4_close_done,
2051 .rpc_release = nfs4_free_closedata,
2052};
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/*
2055 * It is possible for data to be read/written from a mem-mapped file
2056 * after the sys_close call (which hits the vfs layer as a flush).
2057 * This means that we can't safely call nfsv4 close on a file until
2058 * the inode is cleared. This in turn means that we are not good
2059 * NFSv4 citizens - we do not indicate to the server to update the file's
2060 * share state even when we are done with one of the three share
2061 * stateid's in the inode.
2062 *
2063 * NOTE: Caller must be holding the sp->so_owner semaphore!
2064 */
Al Viro643168c2011-06-22 18:20:23 -04002065int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002067 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002069 struct nfs4_state_owner *sp = state->owner;
2070 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002071 struct rpc_message msg = {
2072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2073 .rpc_cred = state->owner->so_cred,
2074 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002075 struct rpc_task_setup task_setup_data = {
2076 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002077 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002078 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002079 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002080 .flags = RPC_TASK_ASYNC,
2081 };
Trond Myklebust95121352005-10-18 14:20:12 -07002082 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002084 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002086 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002087 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002088 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002090 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002091 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002093 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002094 if (calldata->arg.seqid == NULL)
2095 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002096 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002097 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002098 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002099 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002100 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002101 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002102 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002103
Trond Myklebust1174dd12010-12-21 10:52:24 -05002104 msg.rpc_argp = &calldata->arg;
2105 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002106 task_setup_data.callback_data = calldata;
2107 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002108 if (IS_ERR(task))
2109 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002110 status = 0;
2111 if (wait)
2112 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002113 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002114 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002115out_free_calldata:
2116 kfree(calldata);
2117out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002118 if (roc)
2119 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002120 nfs4_put_open_state(state);
2121 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Trond Myklebust2b484292010-09-17 10:56:51 -04002125static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002126nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct nfs4_state *state;
2129
Trond Myklebust565277f2007-10-15 18:17:53 -04002130 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002131 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002132 if (IS_ERR(state))
2133 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002134 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002135 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
2137
Trond Myklebust1185a552009-12-03 15:54:02 -05002138static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002139{
2140 if (ctx->state == NULL)
2141 return;
2142 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002143 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002144 else
Al Viro643168c2011-06-22 18:20:23 -04002145 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2149{
Benny Halevy43652ad2009-04-01 09:21:54 -04002150 struct nfs4_server_caps_arg args = {
2151 .fhandle = fhandle,
2152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 struct nfs4_server_caps_res res = {};
2154 struct rpc_message msg = {
2155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002156 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .rpc_resp = &res,
2158 };
2159 int status;
2160
Bryan Schumaker7c513052011-03-24 17:12:24 +00002161 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (status == 0) {
2163 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002164 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2165 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2166 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2167 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2168 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2170 server->caps |= NFS_CAP_ACLS;
2171 if (res.has_links != 0)
2172 server->caps |= NFS_CAP_HARDLINKS;
2173 if (res.has_symlinks != 0)
2174 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002175 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2176 server->caps |= NFS_CAP_FILEID;
2177 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2178 server->caps |= NFS_CAP_MODE;
2179 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2180 server->caps |= NFS_CAP_NLINK;
2181 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2182 server->caps |= NFS_CAP_OWNER;
2183 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2184 server->caps |= NFS_CAP_OWNER_GROUP;
2185 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2186 server->caps |= NFS_CAP_ATIME;
2187 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2188 server->caps |= NFS_CAP_CTIME;
2189 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2190 server->caps |= NFS_CAP_MTIME;
2191
Trond Myklebusta65318b2009-03-11 14:10:28 -04002192 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2193 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2194 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 server->acl_bitmask = res.acl_bitmask;
2196 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return status;
2199}
2200
Trond Myklebust55a97592006-06-09 09:34:19 -04002201int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
2206 err = nfs4_handle_exception(server,
2207 _nfs4_server_capabilities(server, fhandle),
2208 &exception);
2209 } while (exception.retry);
2210 return err;
2211}
2212
2213static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2214 struct nfs_fsinfo *info)
2215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct nfs4_lookup_root_arg args = {
2217 .bitmask = nfs4_fattr_bitmap,
2218 };
2219 struct nfs4_lookup_res res = {
2220 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002221 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 .fh = fhandle,
2223 };
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2226 .rpc_argp = &args,
2227 .rpc_resp = &res,
2228 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002229
Trond Myklebust0e574af2005-10-27 22:12:38 -04002230 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002231 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info)
2236{
2237 struct nfs4_exception exception = { };
2238 int err;
2239 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002240 err = _nfs4_lookup_root(server, fhandle, info);
2241 switch (err) {
2242 case 0:
2243 case -NFS4ERR_WRONGSEC:
2244 break;
2245 default:
2246 err = nfs4_handle_exception(server, err, &exception);
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 } while (exception.retry);
2249 return err;
2250}
2251
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2253 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2254{
2255 struct rpc_auth *auth;
2256 int ret;
2257
2258 auth = rpcauth_create(flavor, server->client);
2259 if (!auth) {
2260 ret = -EIO;
2261 goto out;
2262 }
2263 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002264out:
2265 return ret;
2266}
2267
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002268static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002269 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002271 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002272 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002274 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2275 flav_array[len] = RPC_AUTH_NULL;
2276 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002277
2278 for (i = 0; i < len; i++) {
2279 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002280 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002281 continue;
2282 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002283 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002284 /*
2285 * -EACCESS could mean that the user doesn't have correct permissions
2286 * to access the mount. It could also mean that we tried to mount
2287 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2288 * existing mount programs don't handle -EACCES very well so it should
2289 * be mapped to -EPERM instead.
2290 */
2291 if (status == -EACCES)
2292 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002293 return status;
2294}
2295
2296/*
2297 * get the file handle for the "/" directory on the server
2298 */
2299static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2300 struct nfs_fsinfo *info)
2301{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002302 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002303 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002304 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2305 /*
2306 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2307 * by nfs4_map_errors() as this function exits.
2308 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002309 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (status == 0)
2311 status = nfs4_server_capabilities(server, fhandle);
2312 if (status == 0)
2313 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002314 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Andy Adamson533eb462011-06-13 18:25:56 -04002317static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002318/*
2319 * Get locations and (maybe) other attributes of a referral.
2320 * Note that we'll actually follow the referral later when
2321 * we detect fsid mismatch in inode revalidation
2322 */
Andy Adamson533eb462011-06-13 18:25:56 -04002323static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2324 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002325{
2326 int status = -ENOMEM;
2327 struct page *page = NULL;
2328 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002329
2330 page = alloc_page(GFP_KERNEL);
2331 if (page == NULL)
2332 goto out;
2333 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2334 if (locations == NULL)
2335 goto out;
2336
Trond Myklebustc228fd32007-01-13 02:28:11 -05002337 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002338 if (status != 0)
2339 goto out;
2340 /* Make sure server returned a different fsid for the referral */
2341 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002342 dprintk("%s: server did not return a different fsid for"
2343 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002344 status = -EIO;
2345 goto out;
2346 }
Andy Adamson533eb462011-06-13 18:25:56 -04002347 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2348 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002349
Andy Adamson533eb462011-06-13 18:25:56 -04002350 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002351 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002352 memset(fhandle, 0, sizeof(struct nfs_fh));
2353out:
2354 if (page)
2355 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002356 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002357 return status;
2358}
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2361{
2362 struct nfs4_getattr_arg args = {
2363 .fh = fhandle,
2364 .bitmask = server->attr_bitmask,
2365 };
2366 struct nfs4_getattr_res res = {
2367 .fattr = fattr,
2368 .server = server,
2369 };
2370 struct rpc_message msg = {
2371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2372 .rpc_argp = &args,
2373 .rpc_resp = &res,
2374 };
2375
Trond Myklebust0e574af2005-10-27 22:12:38 -04002376 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002377 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2381{
2382 struct nfs4_exception exception = { };
2383 int err;
2384 do {
2385 err = nfs4_handle_exception(server,
2386 _nfs4_proc_getattr(server, fhandle, fattr),
2387 &exception);
2388 } while (exception.retry);
2389 return err;
2390}
2391
2392/*
2393 * The file is not closed if it is opened due to the a request to change
2394 * the size of the file. The open call will not be needed once the
2395 * VFS layer lookup-intents are implemented.
2396 *
2397 * Close is called when the inode is destroyed.
2398 * If we haven't opened the file for O_WRONLY, we
2399 * need to in the size_change case to obtain a stateid.
2400 *
2401 * Got race?
2402 * Because OPEN is always done by name in nfsv4, it is
2403 * possible that we opened a different file by the same
2404 * name. We can recognize this race condition, but we
2405 * can't do anything about it besides returning an error.
2406 *
2407 * This will be fixed with VFS changes (lookup-intent).
2408 */
2409static int
2410nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2411 struct iattr *sattr)
2412{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002413 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002414 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002415 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 int status;
2417
Benny Halevy8a1636c2010-07-14 15:43:57 -04002418 if (pnfs_ld_layoutret_on_setattr(inode))
2419 pnfs_return_layout(inode);
2420
Trond Myklebust0e574af2005-10-27 22:12:38 -04002421 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Trond Myklebustd5308382005-11-04 15:33:38 -05002423 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002424 if (sattr->ia_valid & ATTR_FILE) {
2425 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002426
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002427 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002428 if (ctx) {
2429 cred = ctx->cred;
2430 state = ctx->state;
2431 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002432 }
2433
2434 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002435 if (status == 0)
2436 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return status;
2438}
2439
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002440static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2441 const struct qstr *name, struct nfs_fh *fhandle,
2442 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002443{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002444 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002445 int status;
2446 struct nfs4_lookup_arg args = {
2447 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002448 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002449 .name = name,
2450 };
2451 struct nfs4_lookup_res res = {
2452 .server = server,
2453 .fattr = fattr,
2454 .fh = fhandle,
2455 };
2456 struct rpc_message msg = {
2457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2458 .rpc_argp = &args,
2459 .rpc_resp = &res,
2460 };
2461
2462 nfs_fattr_init(fattr);
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002465 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 dprintk("NFS reply lookup: %d\n", status);
2467 return status;
2468}
2469
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002470void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2471{
2472 memset(fh, 0, sizeof(struct nfs_fh));
2473 fattr->fsid.major = 1;
2474 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2475 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2476 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2477 fattr->nlink = 2;
2478}
2479
Bryan Schumaker7c513052011-03-24 17:12:24 +00002480static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2481 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 struct nfs4_exception exception = { };
2484 int err;
2485 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002486 int status;
2487
2488 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2489 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002490 case -NFS4ERR_BADNAME:
2491 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002492 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002493 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002494 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002495 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002496 }
2497 err = nfs4_handle_exception(NFS_SERVER(dir),
2498 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } while (exception.retry);
2500 return err;
2501}
2502
2503static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2504{
Trond Myklebust76b32992007-08-10 17:45:11 -04002505 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct nfs4_accessargs args = {
2507 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002508 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002510 struct nfs4_accessres res = {
2511 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002512 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct rpc_message msg = {
2514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2515 .rpc_argp = &args,
2516 .rpc_resp = &res,
2517 .rpc_cred = entry->cred,
2518 };
2519 int mode = entry->mask;
2520 int status;
2521
2522 /*
2523 * Determine which access bits we want to ask for...
2524 */
2525 if (mode & MAY_READ)
2526 args.access |= NFS4_ACCESS_READ;
2527 if (S_ISDIR(inode->i_mode)) {
2528 if (mode & MAY_WRITE)
2529 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2530 if (mode & MAY_EXEC)
2531 args.access |= NFS4_ACCESS_LOOKUP;
2532 } else {
2533 if (mode & MAY_WRITE)
2534 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2535 if (mode & MAY_EXEC)
2536 args.access |= NFS4_ACCESS_EXECUTE;
2537 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002538
2539 res.fattr = nfs_alloc_fattr();
2540 if (res.fattr == NULL)
2541 return -ENOMEM;
2542
Bryan Schumaker7c513052011-03-24 17:12:24 +00002543 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (!status) {
2545 entry->mask = 0;
2546 if (res.access & NFS4_ACCESS_READ)
2547 entry->mask |= MAY_READ;
2548 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2549 entry->mask |= MAY_WRITE;
2550 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2551 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002552 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002554 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return status;
2556}
2557
2558static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2559{
2560 struct nfs4_exception exception = { };
2561 int err;
2562 do {
2563 err = nfs4_handle_exception(NFS_SERVER(inode),
2564 _nfs4_proc_access(inode, entry),
2565 &exception);
2566 } while (exception.retry);
2567 return err;
2568}
2569
2570/*
2571 * TODO: For the time being, we don't try to get any attributes
2572 * along with any of the zero-copy operations READ, READDIR,
2573 * READLINK, WRITE.
2574 *
2575 * In the case of the first three, we want to put the GETATTR
2576 * after the read-type operation -- this is because it is hard
2577 * to predict the length of a GETATTR response in v4, and thus
2578 * align the READ data correctly. This means that the GETATTR
2579 * may end up partially falling into the page cache, and we should
2580 * shift it into the 'tail' of the xdr_buf before processing.
2581 * To do this efficiently, we need to know the total length
2582 * of data received, which doesn't seem to be available outside
2583 * of the RPC layer.
2584 *
2585 * In the case of WRITE, we also want to put the GETATTR after
2586 * the operation -- in this case because we want to make sure
2587 * we get the post-operation mtime and size. This means that
2588 * we can't use xdr_encode_pages() as written: we need a variant
2589 * of it which would leave room in the 'tail' iovec.
2590 *
2591 * Both of these changes to the XDR layer would in fact be quite
2592 * minor, but I decided to leave them for a subsequent patch.
2593 */
2594static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2595 unsigned int pgbase, unsigned int pglen)
2596{
2597 struct nfs4_readlink args = {
2598 .fh = NFS_FH(inode),
2599 .pgbase = pgbase,
2600 .pglen = pglen,
2601 .pages = &page,
2602 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002603 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct rpc_message msg = {
2605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2606 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002607 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 };
2609
Bryan Schumaker7c513052011-03-24 17:12:24 +00002610 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 -07002611}
2612
2613static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2614 unsigned int pgbase, unsigned int pglen)
2615{
2616 struct nfs4_exception exception = { };
2617 int err;
2618 do {
2619 err = nfs4_handle_exception(NFS_SERVER(inode),
2620 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2621 &exception);
2622 } while (exception.retry);
2623 return err;
2624}
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626/*
2627 * Got race?
2628 * We will need to arrange for the VFS layer to provide an atomic open.
2629 * Until then, this create/open method is prone to inefficiency and race
2630 * conditions due to the lookup, create, and open VFS calls from sys_open()
2631 * placed on the wire.
2632 *
2633 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2634 * The file will be opened again in the subsequent VFS open call
2635 * (nfs4_proc_file_open).
2636 *
2637 * The open for read will just hang around to be used by any process that
2638 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2639 */
2640
2641static int
2642nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002643 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Al Viro82a2c1b2011-06-22 18:30:55 -04002645 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002647 struct rpc_cred *cred = NULL;
2648 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 int status = 0;
2650
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002651 if (ctx != NULL) {
2652 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002653 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002654 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002656 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002657 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002658 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (IS_ERR(state)) {
2660 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002663 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002664 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002665 if (ctx != NULL)
2666 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002667 else
Al Viro643168c2011-06-22 18:20:23 -04002668 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669out:
2670 return status;
2671}
2672
2673static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2674{
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002676 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002678 .name.len = name->len,
2679 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002680 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002682 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002683 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2687 .rpc_argp = &args,
2688 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002690 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Trond Myklebustd3468902010-04-16 16:22:50 -04002692 res.dir_attr = nfs_alloc_fattr();
2693 if (res.dir_attr == NULL)
2694 goto out;
2695
Bryan Schumaker7c513052011-03-24 17:12:24 +00002696 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002697 if (status == 0) {
2698 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002699 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002700 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002701 nfs_free_fattr(res.dir_attr);
2702out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 return status;
2704}
2705
2706static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2707{
2708 struct nfs4_exception exception = { };
2709 int err;
2710 do {
2711 err = nfs4_handle_exception(NFS_SERVER(dir),
2712 _nfs4_proc_remove(dir, name),
2713 &exception);
2714 } while (exception.retry);
2715 return err;
2716}
2717
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002720 struct nfs_server *server = NFS_SERVER(dir);
2721 struct nfs_removeargs *args = msg->rpc_argp;
2722 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Trond Myklebusta65318b2009-03-11 14:10:28 -04002724 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002725 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002727 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002730static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002732 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2733
Trond Myklebust14516c32010-07-31 14:29:06 -04002734 if (!nfs4_sequence_done(task, &res->seq_res))
2735 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002736 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002737 return 0;
2738 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002739 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002740 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Jeff Laytond3d41522010-09-17 17:31:57 -04002743static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2744{
2745 struct nfs_server *server = NFS_SERVER(dir);
2746 struct nfs_renameargs *arg = msg->rpc_argp;
2747 struct nfs_renameres *res = msg->rpc_resp;
2748
2749 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2750 arg->bitmask = server->attr_bitmask;
2751 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002752 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002753}
2754
2755static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2756 struct inode *new_dir)
2757{
2758 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2759
2760 if (!nfs4_sequence_done(task, &res->seq_res))
2761 return 0;
2762 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2763 return 0;
2764
2765 update_changeattr(old_dir, &res->old_cinfo);
2766 nfs_post_op_update_inode(old_dir, res->old_fattr);
2767 update_changeattr(new_dir, &res->new_cinfo);
2768 nfs_post_op_update_inode(new_dir, res->new_fattr);
2769 return 1;
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2773 struct inode *new_dir, struct qstr *new_name)
2774{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002775 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002776 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 .old_dir = NFS_FH(old_dir),
2778 .new_dir = NFS_FH(new_dir),
2779 .old_name = old_name,
2780 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002781 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002783 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002784 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2788 .rpc_argp = &arg,
2789 .rpc_resp = &res,
2790 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002791 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Trond Myklebust011fff72010-04-16 16:22:49 -04002793 res.old_fattr = nfs_alloc_fattr();
2794 res.new_fattr = nfs_alloc_fattr();
2795 if (res.old_fattr == NULL || res.new_fattr == NULL)
2796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Bryan Schumaker7c513052011-03-24 17:12:24 +00002798 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (!status) {
2800 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002801 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002803 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002805out:
2806 nfs_free_fattr(res.new_fattr);
2807 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return status;
2809}
2810
2811static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2812 struct inode *new_dir, struct qstr *new_name)
2813{
2814 struct nfs4_exception exception = { };
2815 int err;
2816 do {
2817 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2818 _nfs4_proc_rename(old_dir, old_name,
2819 new_dir, new_name),
2820 &exception);
2821 } while (exception.retry);
2822 return err;
2823}
2824
2825static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2826{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002827 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 struct nfs4_link_arg arg = {
2829 .fh = NFS_FH(inode),
2830 .dir_fh = NFS_FH(dir),
2831 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002832 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002834 struct nfs4_link_res res = {
2835 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002836 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct rpc_message msg = {
2838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2839 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002840 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002842 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Trond Myklebust136f2622010-04-16 16:22:49 -04002844 res.fattr = nfs_alloc_fattr();
2845 res.dir_attr = nfs_alloc_fattr();
2846 if (res.fattr == NULL || res.dir_attr == NULL)
2847 goto out;
2848
Bryan Schumaker7c513052011-03-24 17:12:24 +00002849 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002850 if (!status) {
2851 update_changeattr(dir, &res.cinfo);
2852 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002853 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002854 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002855out:
2856 nfs_free_fattr(res.dir_attr);
2857 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return status;
2859}
2860
2861static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2862{
2863 struct nfs4_exception exception = { };
2864 int err;
2865 do {
2866 err = nfs4_handle_exception(NFS_SERVER(inode),
2867 _nfs4_proc_link(inode, dir, name),
2868 &exception);
2869 } while (exception.retry);
2870 return err;
2871}
2872
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002873struct nfs4_createdata {
2874 struct rpc_message msg;
2875 struct nfs4_create_arg arg;
2876 struct nfs4_create_res res;
2877 struct nfs_fh fh;
2878 struct nfs_fattr fattr;
2879 struct nfs_fattr dir_fattr;
2880};
2881
2882static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2883 struct qstr *name, struct iattr *sattr, u32 ftype)
2884{
2885 struct nfs4_createdata *data;
2886
2887 data = kzalloc(sizeof(*data), GFP_KERNEL);
2888 if (data != NULL) {
2889 struct nfs_server *server = NFS_SERVER(dir);
2890
2891 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2892 data->msg.rpc_argp = &data->arg;
2893 data->msg.rpc_resp = &data->res;
2894 data->arg.dir_fh = NFS_FH(dir);
2895 data->arg.server = server;
2896 data->arg.name = name;
2897 data->arg.attrs = sattr;
2898 data->arg.ftype = ftype;
2899 data->arg.bitmask = server->attr_bitmask;
2900 data->res.server = server;
2901 data->res.fh = &data->fh;
2902 data->res.fattr = &data->fattr;
2903 data->res.dir_fattr = &data->dir_fattr;
2904 nfs_fattr_init(data->res.fattr);
2905 nfs_fattr_init(data->res.dir_fattr);
2906 }
2907 return data;
2908}
2909
2910static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2911{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002912 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002913 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002914 if (status == 0) {
2915 update_changeattr(dir, &data->res.dir_cinfo);
2916 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2917 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2918 }
2919 return status;
2920}
2921
2922static void nfs4_free_createdata(struct nfs4_createdata *data)
2923{
2924 kfree(data);
2925}
2926
Chuck Lever4f390c12006-08-22 20:06:22 -04002927static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002928 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 struct nfs4_createdata *data;
2931 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Chuck Lever94a6d752006-08-22 20:06:23 -04002933 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002934 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002935
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936 status = -ENOMEM;
2937 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2938 if (data == NULL)
2939 goto out;
2940
2941 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2942 data->arg.u.symlink.pages = &page;
2943 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002945 status = nfs4_do_create(dir, dentry, data);
2946
2947 nfs4_free_createdata(data);
2948out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
Chuck Lever4f390c12006-08-22 20:06:22 -04002952static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002953 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 struct nfs4_exception exception = { };
2956 int err;
2957 do {
2958 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002959 _nfs4_proc_symlink(dir, dentry, page,
2960 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2967 struct iattr *sattr)
2968{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002969 struct nfs4_createdata *data;
2970 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002972 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2973 if (data == NULL)
2974 goto out;
2975
2976 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
2983static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2984 struct iattr *sattr)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002988
2989 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 do {
2991 err = nfs4_handle_exception(NFS_SERVER(dir),
2992 _nfs4_proc_mkdir(dir, dentry, sattr),
2993 &exception);
2994 } while (exception.retry);
2995 return err;
2996}
2997
2998static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002999 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001 struct inode *dir = dentry->d_inode;
3002 struct nfs4_readdir_arg args = {
3003 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003004 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 .pgbase = 0,
3006 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003007 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003008 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 };
3010 struct nfs4_readdir_res res;
3011 struct rpc_message msg = {
3012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3013 .rpc_argp = &args,
3014 .rpc_resp = &res,
3015 .rpc_cred = cred,
3016 };
3017 int status;
3018
Harvey Harrison3110ff82008-05-02 13:42:44 -07003019 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003020 dentry->d_parent->d_name.name,
3021 dentry->d_name.name,
3022 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3024 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003025 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 -05003026 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003028 status += args.pgbase;
3029 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003030
3031 nfs_invalidate_atime(dir);
3032
Harvey Harrison3110ff82008-05-02 13:42:44 -07003033 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return status;
3035}
3036
3037static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003038 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 struct nfs4_exception exception = { };
3041 int err;
3042 do {
3043 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3044 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003045 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 &exception);
3047 } while (exception.retry);
3048 return err;
3049}
3050
3051static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3052 struct iattr *sattr, dev_t rdev)
3053{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003054 struct nfs4_createdata *data;
3055 int mode = sattr->ia_mode;
3056 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3059 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060
3061 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3062 if (data == NULL)
3063 goto out;
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003066 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003068 data->arg.ftype = NF4BLK;
3069 data->arg.u.device.specdata1 = MAJOR(rdev);
3070 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003073 data->arg.ftype = NF4CHR;
3074 data->arg.u.device.specdata1 = MAJOR(rdev);
3075 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003078 status = nfs4_do_create(dir, dentry, data);
3079
3080 nfs4_free_createdata(data);
3081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return status;
3083}
3084
3085static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3086 struct iattr *sattr, dev_t rdev)
3087{
3088 struct nfs4_exception exception = { };
3089 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003090
3091 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 do {
3093 err = nfs4_handle_exception(NFS_SERVER(dir),
3094 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3095 &exception);
3096 } while (exception.retry);
3097 return err;
3098}
3099
3100static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3101 struct nfs_fsstat *fsstat)
3102{
3103 struct nfs4_statfs_arg args = {
3104 .fh = fhandle,
3105 .bitmask = server->attr_bitmask,
3106 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003107 struct nfs4_statfs_res res = {
3108 .fsstat = fsstat,
3109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct rpc_message msg = {
3111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3112 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003113 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 };
3115
Trond Myklebust0e574af2005-10-27 22:12:38 -04003116 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003117 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
3120static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3121{
3122 struct nfs4_exception exception = { };
3123 int err;
3124 do {
3125 err = nfs4_handle_exception(server,
3126 _nfs4_proc_statfs(server, fhandle, fsstat),
3127 &exception);
3128 } while (exception.retry);
3129 return err;
3130}
3131
3132static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3133 struct nfs_fsinfo *fsinfo)
3134{
3135 struct nfs4_fsinfo_arg args = {
3136 .fh = fhandle,
3137 .bitmask = server->attr_bitmask,
3138 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003139 struct nfs4_fsinfo_res res = {
3140 .fsinfo = fsinfo,
3141 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 struct rpc_message msg = {
3143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3144 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003145 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 };
3147
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_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3152{
3153 struct nfs4_exception exception = { };
3154 int err;
3155
3156 do {
3157 err = nfs4_handle_exception(server,
3158 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3159 &exception);
3160 } while (exception.retry);
3161 return err;
3162}
3163
3164static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3165{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003166 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3168}
3169
3170static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3171 struct nfs_pathconf *pathconf)
3172{
3173 struct nfs4_pathconf_arg args = {
3174 .fh = fhandle,
3175 .bitmask = server->attr_bitmask,
3176 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003177 struct nfs4_pathconf_res res = {
3178 .pathconf = pathconf,
3179 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 struct rpc_message msg = {
3181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3182 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003183 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 };
3185
3186 /* None of the pathconf attributes are mandatory to implement */
3187 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3188 memset(pathconf, 0, sizeof(*pathconf));
3189 return 0;
3190 }
3191
Trond Myklebust0e574af2005-10-27 22:12:38 -04003192 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003193 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
3196static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3197 struct nfs_pathconf *pathconf)
3198{
3199 struct nfs4_exception exception = { };
3200 int err;
3201
3202 do {
3203 err = nfs4_handle_exception(server,
3204 _nfs4_proc_pathconf(server, fhandle, pathconf),
3205 &exception);
3206 } while (exception.retry);
3207 return err;
3208}
3209
Benny Halevyd20581a2011-05-22 19:52:03 +03003210void __nfs4_read_done_cb(struct nfs_read_data *data)
3211{
3212 nfs_invalidate_atime(data->inode);
3213}
3214
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003215static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
Trond Myklebustec06c092006-03-20 13:44:27 -05003217 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003219 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003220 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003221 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003223
Benny Halevyd20581a2011-05-22 19:52:03 +03003224 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003226 renew_lease(server, data->timestamp);
3227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003230static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3231{
3232
3233 dprintk("--> %s\n", __func__);
3234
3235 if (!nfs4_sequence_done(task, &data->res.seq_res))
3236 return -EAGAIN;
3237
Benny Halevyd20581a2011-05-22 19:52:03 +03003238 return data->read_done_cb ? data->read_done_cb(task, data) :
3239 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003240}
3241
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003242static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003245 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003246 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003247 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003250/* Reset the the nfs_read_data to send the read to the MDS. */
3251void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3252{
3253 dprintk("%s Reset task for i/o through\n", __func__);
3254 put_lseg(data->lseg);
3255 data->lseg = NULL;
3256 /* offsets will differ in the dense stripe case */
3257 data->args.offset = data->mds_offset;
3258 data->ds_clp = NULL;
3259 data->args.fh = NFS_FH(data->inode);
3260 data->read_done_cb = nfs4_read_done_cb;
3261 task->tk_ops = data->mds_ops;
3262 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3263}
3264EXPORT_SYMBOL_GPL(nfs4_reset_read);
3265
Fred Isamanb029bc92011-03-03 15:13:42 +00003266static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct inode *inode = data->inode;
3269
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003270 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003271 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003272 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003274 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003276 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003277 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Fred Isamanb029bc92011-03-03 15:13:42 +00003281static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3282{
3283 if (!nfs4_sequence_done(task, &data->res.seq_res))
3284 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003285 return data->write_done_cb ? data->write_done_cb(task, data) :
3286 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003287}
3288
Fred Isamana69aef12011-03-03 15:13:47 +00003289/* Reset the the nfs_write_data to send the write to the MDS. */
3290void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3291{
3292 dprintk("%s Reset task for i/o through\n", __func__);
3293 put_lseg(data->lseg);
3294 data->lseg = NULL;
3295 data->ds_clp = NULL;
3296 data->write_done_cb = nfs4_write_done_cb;
3297 data->args.fh = NFS_FH(data->inode);
3298 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3299 data->args.offset = data->mds_offset;
3300 data->res.fattr = &data->fattr;
3301 task->tk_ops = data->mds_ops;
3302 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3303}
3304EXPORT_SYMBOL_GPL(nfs4_reset_write);
3305
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003306static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003308 struct nfs_server *server = NFS_SERVER(data->inode);
3309
Fred Isaman7ffd1062011-03-03 15:13:46 +00003310 if (data->lseg) {
3311 data->args.bitmask = NULL;
3312 data->res.fattr = NULL;
3313 } else
3314 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003315 if (!data->write_done_cb)
3316 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003317 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 data->timestamp = jiffies;
3319
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003321 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
Fred Isaman5f452432011-03-23 13:27:46 +00003324static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003327
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003328 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003329 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003330 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003332 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Fred Isaman5f452432011-03-23 13:27:46 +00003336static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3337{
3338 if (!nfs4_sequence_done(task, &data->res.seq_res))
3339 return -EAGAIN;
3340 return data->write_done_cb(task, data);
3341}
3342
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003343static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003345 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003346
3347 if (data->lseg) {
3348 data->args.bitmask = NULL;
3349 data->res.fattr = NULL;
3350 } else
3351 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003352 if (!data->write_done_cb)
3353 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003354 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003355 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003356 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003359struct nfs4_renewdata {
3360 struct nfs_client *client;
3361 unsigned long timestamp;
3362};
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364/*
3365 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3366 * standalone procedure for queueing an asynchronous RENEW.
3367 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003368static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003369{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003370 struct nfs4_renewdata *data = calldata;
3371 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003372
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003373 if (atomic_read(&clp->cl_count) > 1)
3374 nfs4_schedule_state_renewal(clp);
3375 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003376 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003377}
3378
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003379static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003381 struct nfs4_renewdata *data = calldata;
3382 struct nfs_client *clp = data->client;
3383 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003386 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003387 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3388 return;
3389 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003390 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003391 return;
3392 }
3393 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003395 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
3397
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003398static const struct rpc_call_ops nfs4_renew_ops = {
3399 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003400 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003401};
3402
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003403static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3407 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003408 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003410 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003412 if (renew_flags == 0)
3413 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003414 if (!atomic_inc_not_zero(&clp->cl_count))
3415 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003416 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003417 if (data == NULL)
3418 return -ENOMEM;
3419 data->client = clp;
3420 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003422 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003425static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
3427 struct rpc_message msg = {
3428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3429 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003430 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 };
3432 unsigned long now = jiffies;
3433 int status;
3434
3435 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3436 if (status < 0)
3437 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003438 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return 0;
3440}
3441
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003442static inline int nfs4_server_supports_acls(struct nfs_server *server)
3443{
3444 return (server->caps & NFS_CAP_ACLS)
3445 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3446 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3447}
3448
3449/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3450 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3451 * the stack.
3452 */
3453#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3454
Neil Hormane9e3d722011-03-04 19:26:03 -05003455static int buf_to_pages_noslab(const void *buf, size_t buflen,
3456 struct page **pages, unsigned int *pgbase)
3457{
3458 struct page *newpage, **spages;
3459 int rc = 0;
3460 size_t len;
3461 spages = pages;
3462
3463 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003464 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003465 newpage = alloc_page(GFP_KERNEL);
3466
3467 if (newpage == NULL)
3468 goto unwind;
3469 memcpy(page_address(newpage), buf, len);
3470 buf += len;
3471 buflen -= len;
3472 *pages++ = newpage;
3473 rc++;
3474 } while (buflen != 0);
3475
3476 return rc;
3477
3478unwind:
3479 for(; rc > 0; rc--)
3480 __free_page(spages[rc-1]);
3481 return -ENOMEM;
3482}
3483
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003484struct nfs4_cached_acl {
3485 int cached;
3486 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003487 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003488};
3489
3490static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003491{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003492 struct nfs_inode *nfsi = NFS_I(inode);
3493
3494 spin_lock(&inode->i_lock);
3495 kfree(nfsi->nfs4_acl);
3496 nfsi->nfs4_acl = acl;
3497 spin_unlock(&inode->i_lock);
3498}
3499
3500static void nfs4_zap_acl_attr(struct inode *inode)
3501{
3502 nfs4_set_cached_acl(inode, NULL);
3503}
3504
3505static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3506{
3507 struct nfs_inode *nfsi = NFS_I(inode);
3508 struct nfs4_cached_acl *acl;
3509 int ret = -ENOENT;
3510
3511 spin_lock(&inode->i_lock);
3512 acl = nfsi->nfs4_acl;
3513 if (acl == NULL)
3514 goto out;
3515 if (buf == NULL) /* user is just asking for length */
3516 goto out_len;
3517 if (acl->cached == 0)
3518 goto out;
3519 ret = -ERANGE; /* see getxattr(2) man page */
3520 if (acl->len > buflen)
3521 goto out;
3522 memcpy(buf, acl->data, acl->len);
3523out_len:
3524 ret = acl->len;
3525out:
3526 spin_unlock(&inode->i_lock);
3527 return ret;
3528}
3529
3530static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3531{
3532 struct nfs4_cached_acl *acl;
3533
3534 if (buf && acl_len <= PAGE_SIZE) {
3535 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3536 if (acl == NULL)
3537 goto out;
3538 acl->cached = 1;
3539 memcpy(acl->data, buf, acl_len);
3540 } else {
3541 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3542 if (acl == NULL)
3543 goto out;
3544 acl->cached = 0;
3545 }
3546 acl->len = acl_len;
3547out:
3548 nfs4_set_cached_acl(inode, acl);
3549}
3550
Andy Adamsonbf118a32011-12-07 11:55:27 -05003551/*
3552 * The getxattr API returns the required buffer length when called with a
3553 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3554 * the required buf. On a NULL buf, we send a page of data to the server
3555 * guessing that the ACL request can be serviced by a page. If so, we cache
3556 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3557 * the cache. If not so, we throw away the page, and cache the required
3558 * length. The next getxattr call will then produce another round trip to
3559 * the server, this time with the input buf of the required size.
3560 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003561static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003562{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003563 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003564 struct nfs_getaclargs args = {
3565 .fh = NFS_FH(inode),
3566 .acl_pages = pages,
3567 .acl_len = buflen,
3568 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003569 struct nfs_getaclres res = {
3570 .acl_len = buflen,
3571 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003572 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003573 struct rpc_message msg = {
3574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3575 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003576 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003577 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003578 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579
Andy Adamsonbf118a32011-12-07 11:55:27 -05003580 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3581 /* As long as we're doing a round trip to the server anyway,
3582 * let's be prepared for a page of acl data. */
3583 if (npages == 0)
3584 npages = 1;
3585
3586 for (i = 0; i < npages; i++) {
3587 pages[i] = alloc_page(GFP_KERNEL);
3588 if (!pages[i])
3589 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003590 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003591 if (npages > 1) {
3592 /* for decoding across pages */
3593 args.acl_scratch = alloc_page(GFP_KERNEL);
3594 if (!args.acl_scratch)
3595 goto out_free;
3596 }
3597 args.acl_len = npages * PAGE_SIZE;
3598 args.acl_pgbase = 0;
3599 /* Let decode_getfacl know not to fail if the ACL data is larger than
3600 * the page we send as a guess */
3601 if (buf == NULL)
3602 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3603 resp_buf = page_address(pages[0]);
3604
Peng Taode040be2012-01-10 22:42:47 +08003605 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003606 __func__, buf, buflen, npages, args.acl_len);
3607 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3608 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003609 if (ret)
3610 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003611
3612 acl_len = res.acl_len - res.acl_data_offset;
3613 if (acl_len > args.acl_len)
3614 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003615 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003616 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3617 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003618 if (buf) {
3619 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003620 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003621 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003622 _copy_from_pages(buf, pages, res.acl_data_offset,
3623 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003624 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003625 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003626out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003627 for (i = 0; i < npages; i++)
3628 if (pages[i])
3629 __free_page(pages[i]);
3630 if (args.acl_scratch)
3631 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003632 return ret;
3633}
3634
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003635static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3636{
3637 struct nfs4_exception exception = { };
3638 ssize_t ret;
3639 do {
3640 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3641 if (ret >= 0)
3642 break;
3643 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3644 } while (exception.retry);
3645 return ret;
3646}
3647
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3649{
3650 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003651 int ret;
3652
3653 if (!nfs4_server_supports_acls(server))
3654 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003655 ret = nfs_revalidate_inode(server, inode);
3656 if (ret < 0)
3657 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003658 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3659 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003660 ret = nfs4_read_cached_acl(inode, buf, buflen);
3661 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003662 /* -ENOENT is returned if there is no ACL or if there is an ACL
3663 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003664 return ret;
3665 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003666}
3667
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003668static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003669{
3670 struct nfs_server *server = NFS_SERVER(inode);
3671 struct page *pages[NFS4ACL_MAXPAGES];
3672 struct nfs_setaclargs arg = {
3673 .fh = NFS_FH(inode),
3674 .acl_pages = pages,
3675 .acl_len = buflen,
3676 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003677 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003678 struct rpc_message msg = {
3679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3680 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003681 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003682 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003683 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003684
3685 if (!nfs4_server_supports_acls(server))
3686 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003687 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3688 if (i < 0)
3689 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003690 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003691 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003692
3693 /*
3694 * Free each page after tx, so the only ref left is
3695 * held by the network stack
3696 */
3697 for (; i > 0; i--)
3698 put_page(pages[i-1]);
3699
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003700 /*
3701 * Acl update can result in inode attribute update.
3702 * so mark the attribute cache invalid.
3703 */
3704 spin_lock(&inode->i_lock);
3705 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3706 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003707 nfs_access_zap_cache(inode);
3708 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003709 return ret;
3710}
3711
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003712static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3713{
3714 struct nfs4_exception exception = { };
3715 int err;
3716 do {
3717 err = nfs4_handle_exception(NFS_SERVER(inode),
3718 __nfs4_proc_set_acl(inode, buf, buflen),
3719 &exception);
3720 } while (exception.retry);
3721 return err;
3722}
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003725nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003727 struct nfs_client *clp = server->nfs_client;
3728
3729 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return 0;
3731 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003732 case -NFS4ERR_ADMIN_REVOKED:
3733 case -NFS4ERR_BAD_STATEID:
3734 case -NFS4ERR_OPENMODE:
3735 if (state == NULL)
3736 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003737 nfs4_schedule_stateid_recovery(server, state);
3738 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003739 case -NFS4ERR_EXPIRED:
3740 if (state != NULL)
3741 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003743 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003744 nfs4_schedule_lease_recovery(clp);
3745 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003746#if defined(CONFIG_NFS_V4_1)
3747 case -NFS4ERR_BADSESSION:
3748 case -NFS4ERR_BADSLOT:
3749 case -NFS4ERR_BAD_HIGH_SLOT:
3750 case -NFS4ERR_DEADSESSION:
3751 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3752 case -NFS4ERR_SEQ_FALSE_RETRY:
3753 case -NFS4ERR_SEQ_MISORDERED:
3754 dprintk("%s ERROR %d, Reset session\n", __func__,
3755 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003756 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003757 task->tk_status = 0;
3758 return -EAGAIN;
3759#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003761 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003762 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003763 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3765 task->tk_status = 0;
3766 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003767 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 case -NFS4ERR_OLD_STATEID:
3769 task->tk_status = 0;
3770 return -EAGAIN;
3771 }
3772 task->tk_status = nfs4_map_errors(task->tk_status);
3773 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003774wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003775 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003776 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3777 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3778 task->tk_status = 0;
3779 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003782int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3783 unsigned short port, struct rpc_cred *cred,
3784 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 nfs4_verifier sc_verifier;
3787 struct nfs4_setclientid setclientid = {
3788 .sc_verifier = &sc_verifier,
3789 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003790 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 };
3792 struct rpc_message msg = {
3793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3794 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003795 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003796 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 };
Al Virobc4785c2006-10-19 23:28:51 -07003798 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int loop = 0;
3800 int status;
3801
Al Virobc4785c2006-10-19 23:28:51 -07003802 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3804 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3805
3806 for(;;) {
3807 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003808 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003809 clp->cl_ipaddr,
3810 rpc_peeraddr2str(clp->cl_rpcclient,
3811 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003812 rpc_peeraddr2str(clp->cl_rpcclient,
3813 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003814 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 clp->cl_id_uniquifier);
3816 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003817 sizeof(setclientid.sc_netid),
3818 rpc_peeraddr2str(clp->cl_rpcclient,
3819 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003821 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 clp->cl_ipaddr, port >> 8, port & 255);
3823
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003824 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (status != -NFS4ERR_CLID_INUSE)
3826 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003827 if (loop != 0) {
3828 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003830 }
3831 ++loop;
3832 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
3834 return status;
3835}
3836
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003837int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003838 struct nfs4_setclientid_res *arg,
3839 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
3841 struct nfs_fsinfo fsinfo;
3842 struct rpc_message msg = {
3843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003844 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003846 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 };
3848 unsigned long now;
3849 int status;
3850
3851 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003852 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (status == 0) {
3854 spin_lock(&clp->cl_lock);
3855 clp->cl_lease_time = fsinfo.lease_time * HZ;
3856 clp->cl_last_renewal = now;
3857 spin_unlock(&clp->cl_lock);
3858 }
3859 return status;
3860}
3861
Trond Myklebustfe650402006-01-03 09:55:18 +01003862struct nfs4_delegreturndata {
3863 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003864 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003865 struct nfs_fh fh;
3866 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003867 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003868 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003869 int rpc_status;
3870};
3871
Trond Myklebustfe650402006-01-03 09:55:18 +01003872static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3873{
3874 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003875
Trond Myklebust14516c32010-07-31 14:29:06 -04003876 if (!nfs4_sequence_done(task, &data->res.seq_res))
3877 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003878
Ricardo Labiaga79708862009-12-07 09:23:21 -05003879 switch (task->tk_status) {
3880 case -NFS4ERR_STALE_STATEID:
3881 case -NFS4ERR_EXPIRED:
3882 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003883 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003884 break;
3885 default:
3886 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3887 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003888 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003889 return;
3890 }
3891 }
3892 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003893}
3894
3895static void nfs4_delegreturn_release(void *calldata)
3896{
Trond Myklebustfe650402006-01-03 09:55:18 +01003897 kfree(calldata);
3898}
3899
Andy Adamson938e1012009-04-01 09:22:28 -04003900#if defined(CONFIG_NFS_V4_1)
3901static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3902{
3903 struct nfs4_delegreturndata *d_data;
3904
3905 d_data = (struct nfs4_delegreturndata *)data;
3906
Trond Myklebust035168a2010-06-16 09:52:26 -04003907 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003908 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003909 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04003910 return;
3911 rpc_call_start(task);
3912}
3913#endif /* CONFIG_NFS_V4_1 */
3914
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003915static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003916#if defined(CONFIG_NFS_V4_1)
3917 .rpc_call_prepare = nfs4_delegreturn_prepare,
3918#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003919 .rpc_call_done = nfs4_delegreturn_done,
3920 .rpc_release = nfs4_delegreturn_release,
3921};
3922
Trond Myklebuste6f81072008-01-24 18:14:34 -05003923static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003924{
3925 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003926 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003927 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003928 struct rpc_message msg = {
3929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3930 .rpc_cred = cred,
3931 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003932 struct rpc_task_setup task_setup_data = {
3933 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003934 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003935 .callback_ops = &nfs4_delegreturn_ops,
3936 .flags = RPC_TASK_ASYNC,
3937 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003938 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003939
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003940 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003941 if (data == NULL)
3942 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003943 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01003944 data->args.fhandle = &data->fh;
3945 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003946 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003947 nfs_copy_fh(&data->fh, NFS_FH(inode));
3948 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003949 data->res.fattr = &data->fattr;
3950 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003951 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003952 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003953 data->rpc_status = 0;
3954
Trond Myklebustc970aa82007-07-14 15:39:59 -04003955 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003956 msg.rpc_argp = &data->args;
3957 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003958 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003959 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003960 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003961 if (!issync)
3962 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003963 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003964 if (status != 0)
3965 goto out;
3966 status = data->rpc_status;
3967 if (status != 0)
3968 goto out;
3969 nfs_refresh_inode(inode, &data->fattr);
3970out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003971 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003972 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Trond Myklebuste6f81072008-01-24 18:14:34 -05003975int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
3977 struct nfs_server *server = NFS_SERVER(inode);
3978 struct nfs4_exception exception = { };
3979 int err;
3980 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003981 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 switch (err) {
3983 case -NFS4ERR_STALE_STATEID:
3984 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 case 0:
3986 return 0;
3987 }
3988 err = nfs4_handle_exception(server, err, &exception);
3989 } while (exception.retry);
3990 return err;
3991}
3992
3993#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3994#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3995
3996/*
3997 * sleep, with exponential backoff, and retry the LOCK operation.
3998 */
3999static unsigned long
4000nfs4_set_lock_task_retry(unsigned long timeout)
4001{
Jeff Laytond3103102011-12-01 22:44:39 +01004002 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 timeout <<= 1;
4004 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4005 return NFS4_LOCK_MAXTIMEOUT;
4006 return timeout;
4007}
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4010{
4011 struct inode *inode = state->inode;
4012 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004013 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004014 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004016 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004018 struct nfs_lockt_res res = {
4019 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 };
4021 struct rpc_message msg = {
4022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4023 .rpc_argp = &arg,
4024 .rpc_resp = &res,
4025 .rpc_cred = state->owner->so_cred,
4026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 struct nfs4_lock_state *lsp;
4028 int status;
4029
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004030 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004031 status = nfs4_set_lock_state(state, request);
4032 if (status != 0)
4033 goto out;
4034 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004035 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004036 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004037 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004038 switch (status) {
4039 case 0:
4040 request->fl_type = F_UNLCK;
4041 break;
4042 case -NFS4ERR_DENIED:
4043 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004045 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004046out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return status;
4048}
4049
4050static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4051{
4052 struct nfs4_exception exception = { };
4053 int err;
4054
4055 do {
4056 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4057 _nfs4_proc_getlk(state, cmd, request),
4058 &exception);
4059 } while (exception.retry);
4060 return err;
4061}
4062
4063static int do_vfs_lock(struct file *file, struct file_lock *fl)
4064{
4065 int res = 0;
4066 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4067 case FL_POSIX:
4068 res = posix_lock_file_wait(file, fl);
4069 break;
4070 case FL_FLOCK:
4071 res = flock_lock_file_wait(file, fl);
4072 break;
4073 default:
4074 BUG();
4075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return res;
4077}
4078
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004080 struct nfs_locku_args arg;
4081 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004082 struct nfs4_lock_state *lsp;
4083 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004084 struct file_lock fl;
4085 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004086 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087};
4088
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004089static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4090 struct nfs_open_context *ctx,
4091 struct nfs4_lock_state *lsp,
4092 struct nfs_seqid *seqid)
4093{
4094 struct nfs4_unlockdata *p;
4095 struct inode *inode = lsp->ls_state->inode;
4096
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004097 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004098 if (p == NULL)
4099 return NULL;
4100 p->arg.fh = NFS_FH(inode);
4101 p->arg.fl = &p->fl;
4102 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004103 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004104 p->arg.stateid = &lsp->ls_stateid;
4105 p->lsp = lsp;
4106 atomic_inc(&lsp->ls_count);
4107 /* Ensure we don't close file until we're done freeing locks! */
4108 p->ctx = get_nfs_open_context(ctx);
4109 memcpy(&p->fl, fl, sizeof(p->fl));
4110 p->server = NFS_SERVER(inode);
4111 return p;
4112}
4113
Trond Myklebust06f814a2006-01-03 09:55:07 +01004114static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004115{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004116 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004117 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004118 nfs4_put_lock_state(calldata->lsp);
4119 put_nfs_open_context(calldata->ctx);
4120 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004121}
4122
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004123static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004124{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004125 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004126
Trond Myklebust14516c32010-07-31 14:29:06 -04004127 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4128 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004129 switch (task->tk_status) {
4130 case 0:
4131 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004132 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004133 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004134 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004135 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004136 case -NFS4ERR_BAD_STATEID:
4137 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004138 case -NFS4ERR_STALE_STATEID:
4139 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004140 break;
4141 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004142 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004143 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004144 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004145}
4146
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004147static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004148{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004149 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004151 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004152 return;
4153 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004154 /* Note: exit _without_ running nfs4_locku_done */
4155 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004156 return;
4157 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004158 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004159 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004160 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004161 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004162 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004163 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004166static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004167 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004168 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004169 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004170};
4171
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4173 struct nfs_open_context *ctx,
4174 struct nfs4_lock_state *lsp,
4175 struct nfs_seqid *seqid)
4176{
4177 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004178 struct rpc_message msg = {
4179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4180 .rpc_cred = ctx->cred,
4181 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004182 struct rpc_task_setup task_setup_data = {
4183 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004184 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004185 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004186 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004187 .flags = RPC_TASK_ASYNC,
4188 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004189
Frank Filz137d6ac2007-07-09 15:32:29 -07004190 /* Ensure this is an unlock - when canceling a lock, the
4191 * canceled lock is passed in, and it won't be an unlock.
4192 */
4193 fl->fl_type = F_UNLCK;
4194
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4196 if (data == NULL) {
4197 nfs_free_seqid(seqid);
4198 return ERR_PTR(-ENOMEM);
4199 }
4200
Trond Myklebust9d12b212012-01-17 22:04:25 -05004201 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004202 msg.rpc_argp = &data->arg;
4203 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004204 task_setup_data.callback_data = data;
4205 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206}
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4209{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004210 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004211 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004212 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004213 struct rpc_task *task;
4214 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004215 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Trond Myklebust9b073572006-06-29 16:38:34 -04004217 status = nfs4_set_lock_state(state, request);
4218 /* Unlock _before_ we do the RPC call */
4219 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004220 down_read(&nfsi->rwsem);
4221 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4222 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004223 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004224 }
4225 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004226 if (status != 0)
4227 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004228 /* Is this a delegated lock? */
4229 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004230 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004231 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004232 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004233 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004234 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004235 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004236 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 status = PTR_ERR(task);
4238 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004239 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004241 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004242out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004243 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004244 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004247struct nfs4_lockdata {
4248 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004249 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250 struct nfs4_lock_state *lsp;
4251 struct nfs_open_context *ctx;
4252 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004253 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004254 int rpc_status;
4255 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004256 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004257};
4258
4259static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004260 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4261 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004262{
4263 struct nfs4_lockdata *p;
4264 struct inode *inode = lsp->ls_state->inode;
4265 struct nfs_server *server = NFS_SERVER(inode);
4266
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004267 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268 if (p == NULL)
4269 return NULL;
4270
4271 p->arg.fh = NFS_FH(inode);
4272 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004273 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004274 if (p->arg.open_seqid == NULL)
4275 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004276 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004277 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004278 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004279 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004280 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004281 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004282 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004283 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004285 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286 atomic_inc(&lsp->ls_count);
4287 p->ctx = get_nfs_open_context(ctx);
4288 memcpy(&p->fl, fl, sizeof(p->fl));
4289 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004290out_free_seqid:
4291 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004292out_free:
4293 kfree(p);
4294 return NULL;
4295}
4296
4297static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4298{
4299 struct nfs4_lockdata *data = calldata;
4300 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Harvey Harrison3110ff82008-05-02 13:42:44 -07004302 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004303 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4304 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305 /* Do we need to do an open_to_lock_owner? */
4306 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004307 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4308 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309 data->arg.open_stateid = &state->stateid;
4310 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004311 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004312 } else
4313 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004314 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004315 if (nfs4_setup_sequence(data->server,
4316 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004317 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004318 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004319 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004320 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321}
4322
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004323static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4324{
4325 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4326 nfs4_lock_prepare(task, calldata);
4327}
4328
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004329static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4330{
4331 struct nfs4_lockdata *data = calldata;
4332
Harvey Harrison3110ff82008-05-02 13:42:44 -07004333 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334
Trond Myklebust14516c32010-07-31 14:29:06 -04004335 if (!nfs4_sequence_done(task, &data->res.seq_res))
4336 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004337
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004339 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004340 if (data->rpc_status == 0)
4341 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4342 else
4343 goto out;
4344 }
4345 if (data->rpc_status == 0) {
4346 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4347 sizeof(data->lsp->ls_stateid.data));
4348 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004349 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004350 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004351out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004352 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004353}
4354
4355static void nfs4_lock_release(void *calldata)
4356{
4357 struct nfs4_lockdata *data = calldata;
4358
Harvey Harrison3110ff82008-05-02 13:42:44 -07004359 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004360 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004361 if (data->cancelled != 0) {
4362 struct rpc_task *task;
4363 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4364 data->arg.lock_seqid);
4365 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004366 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004367 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004368 } else
4369 nfs_free_seqid(data->arg.lock_seqid);
4370 nfs4_put_lock_state(data->lsp);
4371 put_nfs_open_context(data->ctx);
4372 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004373 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004374}
4375
4376static const struct rpc_call_ops nfs4_lock_ops = {
4377 .rpc_call_prepare = nfs4_lock_prepare,
4378 .rpc_call_done = nfs4_lock_done,
4379 .rpc_release = nfs4_lock_release,
4380};
4381
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004382static const struct rpc_call_ops nfs4_recover_lock_ops = {
4383 .rpc_call_prepare = nfs4_recover_lock_prepare,
4384 .rpc_call_done = nfs4_lock_done,
4385 .rpc_release = nfs4_lock_release,
4386};
4387
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004388static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4389{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004390 switch (error) {
4391 case -NFS4ERR_ADMIN_REVOKED:
4392 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004393 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004394 if (new_lock_owner != 0 ||
4395 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004396 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004397 break;
4398 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004399 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004400 case -NFS4ERR_EXPIRED:
4401 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004402 };
4403}
4404
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004405static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406{
4407 struct nfs4_lockdata *data;
4408 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004409 struct rpc_message msg = {
4410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4411 .rpc_cred = state->owner->so_cred,
4412 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004413 struct rpc_task_setup task_setup_data = {
4414 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004415 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004416 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004417 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004418 .flags = RPC_TASK_ASYNC,
4419 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420 int ret;
4421
Harvey Harrison3110ff82008-05-02 13:42:44 -07004422 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004423 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004424 fl->fl_u.nfs4_fl.owner,
4425 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004426 if (data == NULL)
4427 return -ENOMEM;
4428 if (IS_SETLKW(cmd))
4429 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004430 if (recovery_type > NFS_LOCK_NEW) {
4431 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004432 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004433 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4434 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004435 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004436 msg.rpc_argp = &data->arg;
4437 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004438 task_setup_data.callback_data = data;
4439 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004440 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004441 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004442 ret = nfs4_wait_for_completion_rpc_task(task);
4443 if (ret == 0) {
4444 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004445 if (ret)
4446 nfs4_handle_setlk_error(data->server, data->lsp,
4447 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004448 } else
4449 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004450 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004451 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
4455static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4456{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004457 struct nfs_server *server = NFS_SERVER(state->inode);
4458 struct nfs4_exception exception = { };
4459 int err;
4460
4461 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004462 /* Cache the lock if possible... */
4463 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4464 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004465 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004466 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004467 break;
4468 nfs4_handle_exception(server, err, &exception);
4469 } while (exception.retry);
4470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
4472
4473static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4474{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004475 struct nfs_server *server = NFS_SERVER(state->inode);
4476 struct nfs4_exception exception = { };
4477 int err;
4478
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004479 err = nfs4_set_lock_state(state, request);
4480 if (err != 0)
4481 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004482 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4484 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004485 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004486 switch (err) {
4487 default:
4488 goto out;
4489 case -NFS4ERR_GRACE:
4490 case -NFS4ERR_DELAY:
4491 nfs4_handle_exception(server, err, &exception);
4492 err = 0;
4493 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004494 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004495out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004496 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497}
4498
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004499#if defined(CONFIG_NFS_V4_1)
4500static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4501{
4502 int status;
4503 struct nfs_server *server = NFS_SERVER(state->inode);
4504
4505 status = nfs41_test_stateid(server, state);
4506 if (status == NFS_OK)
4507 return 0;
4508 nfs41_free_stateid(server, state);
4509 return nfs4_lock_expired(state, request);
4510}
4511#endif
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4514{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004515 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004516 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004517 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004519 if ((fl_flags & FL_POSIX) &&
4520 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4521 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004522 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004523 status = nfs4_set_lock_state(state, request);
4524 if (status != 0)
4525 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004526 request->fl_flags |= FL_ACCESS;
4527 status = do_vfs_lock(request->fl_file, request);
4528 if (status < 0)
4529 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004530 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004531 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004532 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004533 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004534 request->fl_flags = fl_flags & ~FL_SLEEP;
4535 status = do_vfs_lock(request->fl_file, request);
4536 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004537 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004538 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004539 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004540 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004541 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004542 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004543 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004544 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004545out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004546 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004547out:
4548 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 return status;
4550}
4551
4552static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4553{
4554 struct nfs4_exception exception = { };
4555 int err;
4556
4557 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004558 err = _nfs4_proc_setlk(state, cmd, request);
4559 if (err == -NFS4ERR_DENIED)
4560 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004562 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 } while (exception.retry);
4564 return err;
4565}
4566
4567static int
4568nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4569{
4570 struct nfs_open_context *ctx;
4571 struct nfs4_state *state;
4572 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4573 int status;
4574
4575 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004576 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 state = ctx->state;
4578
4579 if (request->fl_start < 0 || request->fl_end < 0)
4580 return -EINVAL;
4581
Trond Myklebustd9531262009-07-21 19:22:38 -04004582 if (IS_GETLK(cmd)) {
4583 if (state != NULL)
4584 return nfs4_proc_getlk(state, F_GETLK, request);
4585 return 0;
4586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4589 return -EINVAL;
4590
Trond Myklebustd9531262009-07-21 19:22:38 -04004591 if (request->fl_type == F_UNLCK) {
4592 if (state != NULL)
4593 return nfs4_proc_unlck(state, cmd, request);
4594 return 0;
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Trond Myklebustd9531262009-07-21 19:22:38 -04004597 if (state == NULL)
4598 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 do {
4600 status = nfs4_proc_setlk(state, cmd, request);
4601 if ((status != -EAGAIN) || IS_SETLK(cmd))
4602 break;
4603 timeout = nfs4_set_lock_task_retry(timeout);
4604 status = -ERESTARTSYS;
4605 if (signalled())
4606 break;
4607 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return status;
4609}
4610
Trond Myklebust888e6942005-11-04 15:38:11 -05004611int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4612{
4613 struct nfs_server *server = NFS_SERVER(state->inode);
4614 struct nfs4_exception exception = { };
4615 int err;
4616
4617 err = nfs4_set_lock_state(state, fl);
4618 if (err != 0)
4619 goto out;
4620 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004621 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004622 switch (err) {
4623 default:
4624 printk(KERN_ERR "%s: unhandled error %d.\n",
4625 __func__, err);
4626 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004627 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004628 goto out;
4629 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004630 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004631 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004632 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004633 nfs4_schedule_lease_recovery(server->nfs_client);
4634 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004635 case -NFS4ERR_BADSESSION:
4636 case -NFS4ERR_BADSLOT:
4637 case -NFS4ERR_BAD_HIGH_SLOT:
4638 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4639 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004640 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004641 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004642 case -ERESTARTSYS:
4643 /*
4644 * The show must go on: exit, but mark the
4645 * stateid as needing recovery.
4646 */
4647 case -NFS4ERR_ADMIN_REVOKED:
4648 case -NFS4ERR_BAD_STATEID:
4649 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004650 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004651 err = 0;
4652 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004653 case -EKEYEXPIRED:
4654 /*
4655 * User RPCSEC_GSS context has expired.
4656 * We cannot recover this stateid now, so
4657 * skip it and allow recovery thread to
4658 * proceed.
4659 */
4660 err = 0;
4661 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004662 case -ENOMEM:
4663 case -NFS4ERR_DENIED:
4664 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4665 err = 0;
4666 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004667 case -NFS4ERR_DELAY:
4668 break;
4669 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004670 err = nfs4_handle_exception(server, err, &exception);
4671 } while (exception.retry);
4672out:
4673 return err;
4674}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004675
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004676static void nfs4_release_lockowner_release(void *calldata)
4677{
4678 kfree(calldata);
4679}
4680
4681const struct rpc_call_ops nfs4_release_lockowner_ops = {
4682 .rpc_release = nfs4_release_lockowner_release,
4683};
4684
4685void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4686{
4687 struct nfs_server *server = lsp->ls_state->owner->so_server;
4688 struct nfs_release_lockowner_args *args;
4689 struct rpc_message msg = {
4690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4691 };
4692
4693 if (server->nfs_client->cl_mvops->minor_version != 0)
4694 return;
4695 args = kmalloc(sizeof(*args), GFP_NOFS);
4696 if (!args)
4697 return;
4698 args->lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004699 args->lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004700 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004701 msg.rpc_argp = args;
4702 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4703}
4704
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004705#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4706
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004707static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4708 const void *buf, size_t buflen,
4709 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004710{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004711 if (strcmp(key, "") != 0)
4712 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004713
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004714 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004715}
4716
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004717static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4718 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004719{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004720 if (strcmp(key, "") != 0)
4721 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004722
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004723 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004724}
4725
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004726static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4727 size_t list_len, const char *name,
4728 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004729{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004730 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004731
J. Bruce Fields096455a2006-03-20 23:23:42 -05004732 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4733 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004734
4735 if (list && len <= list_len)
4736 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004737 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004738}
4739
Andy Adamson533eb462011-06-13 18:25:56 -04004740/*
4741 * nfs_fhget will use either the mounted_on_fileid or the fileid
4742 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004743static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4744{
Andy Adamson533eb462011-06-13 18:25:56 -04004745 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4746 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4747 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4748 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004749 return;
4750
4751 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4752 NFS_ATTR_FATTR_NLINK;
4753 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4754 fattr->nlink = 2;
4755}
4756
Trond Myklebust56659e92007-07-17 21:52:39 -04004757int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004758 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004759{
4760 struct nfs_server *server = NFS_SERVER(dir);
4761 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004762 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004763 };
4764 struct nfs4_fs_locations_arg args = {
4765 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004766 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004767 .page = page,
4768 .bitmask = bitmask,
4769 };
Benny Halevy22958462009-04-01 09:22:02 -04004770 struct nfs4_fs_locations_res res = {
4771 .fs_locations = fs_locations,
4772 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004773 struct rpc_message msg = {
4774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4775 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004776 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004777 };
4778 int status;
4779
Harvey Harrison3110ff82008-05-02 13:42:44 -07004780 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004781
4782 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4783 * is not supported */
4784 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4785 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4786 else
4787 bitmask[0] |= FATTR4_WORD0_FILEID;
4788
Trond Myklebustc228fd32007-01-13 02:28:11 -05004789 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004790 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004791 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004792 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004793 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004794 return status;
4795}
4796
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004797static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4798{
4799 int status;
4800 struct nfs4_secinfo_arg args = {
4801 .dir_fh = NFS_FH(dir),
4802 .name = name,
4803 };
4804 struct nfs4_secinfo_res res = {
4805 .flavors = flavors,
4806 };
4807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4809 .rpc_argp = &args,
4810 .rpc_resp = &res,
4811 };
4812
4813 dprintk("NFS call secinfo %s\n", name->name);
4814 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4815 dprintk("NFS reply secinfo: %d\n", status);
4816 return status;
4817}
4818
4819int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4820{
4821 struct nfs4_exception exception = { };
4822 int err;
4823 do {
4824 err = nfs4_handle_exception(NFS_SERVER(dir),
4825 _nfs4_proc_secinfo(dir, name, flavors),
4826 &exception);
4827 } while (exception.retry);
4828 return err;
4829}
4830
Andy Adamson557134a2009-04-01 09:21:53 -04004831#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004832/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004833 * Check the exchange flags returned by the server for invalid flags, having
4834 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4835 * DS flags set.
4836 */
4837static int nfs4_check_cl_exchange_flags(u32 flags)
4838{
4839 if (flags & ~EXCHGID4_FLAG_MASK_R)
4840 goto out_inval;
4841 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4842 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4843 goto out_inval;
4844 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4845 goto out_inval;
4846 return NFS_OK;
4847out_inval:
4848 return -NFS4ERR_INVAL;
4849}
4850
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004851static bool
4852nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4853{
4854 if (a->server_scope_sz == b->server_scope_sz &&
4855 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4856 return true;
4857
4858 return false;
4859}
4860
Andy Adamson357f54d2010-12-14 10:11:57 -05004861/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004862 * nfs4_proc_exchange_id()
4863 *
4864 * Since the clientid has expired, all compounds using sessions
4865 * associated with the stale clientid will be returning
4866 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4867 * be in some phase of session reset.
4868 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004869int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004870{
4871 nfs4_verifier verifier;
4872 struct nfs41_exchange_id_args args = {
4873 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004874 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004875 };
4876 struct nfs41_exchange_id_res res = {
4877 .client = clp,
4878 };
4879 int status;
4880 struct rpc_message msg = {
4881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4882 .rpc_argp = &args,
4883 .rpc_resp = &res,
4884 .rpc_cred = cred,
4885 };
4886 __be32 *p;
4887
4888 dprintk("--> %s\n", __func__);
4889 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004890
Benny Halevy99fe60d2009-04-01 09:22:29 -04004891 p = (u32 *)verifier.data;
4892 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4893 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4894 args.verifier = &verifier;
4895
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004896 args.id_len = scnprintf(args.id, sizeof(args.id),
4897 "%s/%s.%s/%u",
4898 clp->cl_ipaddr,
4899 init_utsname()->nodename,
4900 init_utsname()->domainname,
4901 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004902
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004903 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4904 if (unlikely(!res.server_scope))
4905 return -ENOMEM;
4906
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004907 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004908 if (!status)
4909 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004910
4911 if (!status) {
4912 if (clp->server_scope &&
4913 !nfs41_same_server_scope(clp->server_scope,
4914 res.server_scope)) {
4915 dprintk("%s: server_scope mismatch detected\n",
4916 __func__);
4917 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4918 kfree(clp->server_scope);
4919 clp->server_scope = NULL;
4920 }
4921
4922 if (!clp->server_scope)
4923 clp->server_scope = res.server_scope;
4924 else
4925 kfree(res.server_scope);
4926 }
4927
Benny Halevy99fe60d2009-04-01 09:22:29 -04004928 dprintk("<-- %s status= %d\n", __func__, status);
4929 return status;
4930}
4931
Andy Adamson2050f0c2009-04-01 09:22:30 -04004932struct nfs4_get_lease_time_data {
4933 struct nfs4_get_lease_time_args *args;
4934 struct nfs4_get_lease_time_res *res;
4935 struct nfs_client *clp;
4936};
4937
4938static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4939 void *calldata)
4940{
4941 int ret;
4942 struct nfs4_get_lease_time_data *data =
4943 (struct nfs4_get_lease_time_data *)calldata;
4944
4945 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004946 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004947 /* just setup sequence, do not trigger session recovery
4948 since we're invoked within one */
4949 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004950 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004951 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004952
4953 BUG_ON(ret == -EAGAIN);
4954 rpc_call_start(task);
4955 dprintk("<-- %s\n", __func__);
4956}
4957
4958/*
4959 * Called from nfs4_state_manager thread for session setup, so don't recover
4960 * from sequence operation or clientid errors.
4961 */
4962static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4963{
4964 struct nfs4_get_lease_time_data *data =
4965 (struct nfs4_get_lease_time_data *)calldata;
4966
4967 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004968 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4969 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004970 switch (task->tk_status) {
4971 case -NFS4ERR_DELAY:
4972 case -NFS4ERR_GRACE:
4973 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4974 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4975 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004976 /* fall through */
4977 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004978 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004979 return;
4980 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004981 dprintk("<-- %s\n", __func__);
4982}
4983
4984struct rpc_call_ops nfs4_get_lease_time_ops = {
4985 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4986 .rpc_call_done = nfs4_get_lease_time_done,
4987};
4988
4989int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4990{
4991 struct rpc_task *task;
4992 struct nfs4_get_lease_time_args args;
4993 struct nfs4_get_lease_time_res res = {
4994 .lr_fsinfo = fsinfo,
4995 };
4996 struct nfs4_get_lease_time_data data = {
4997 .args = &args,
4998 .res = &res,
4999 .clp = clp,
5000 };
5001 struct rpc_message msg = {
5002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5003 .rpc_argp = &args,
5004 .rpc_resp = &res,
5005 };
5006 struct rpc_task_setup task_setup = {
5007 .rpc_client = clp->cl_rpcclient,
5008 .rpc_message = &msg,
5009 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005010 .callback_data = &data,
5011 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005012 };
5013 int status;
5014
Trond Myklebust9d12b212012-01-17 22:04:25 -05005015 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005016 dprintk("--> %s\n", __func__);
5017 task = rpc_run_task(&task_setup);
5018
5019 if (IS_ERR(task))
5020 status = PTR_ERR(task);
5021 else {
5022 status = task->tk_status;
5023 rpc_put_task(task);
5024 }
5025 dprintk("<-- %s return %d\n", __func__, status);
5026
5027 return status;
5028}
5029
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005030/*
5031 * Reset a slot table
5032 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005033static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5034 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005035{
Andy Adamson104aeba2010-01-14 17:45:10 -05005036 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005037 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005038 int ret = 0;
5039
Andy Adamson104aeba2010-01-14 17:45:10 -05005040 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5041 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005042
Andy Adamson104aeba2010-01-14 17:45:10 -05005043 /* Does the newly negotiated max_reqs match the existing slot table? */
5044 if (max_reqs != tbl->max_slots) {
5045 ret = -ENOMEM;
5046 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005047 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005048 if (!new)
5049 goto out;
5050 ret = 0;
5051 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005052 }
5053 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005054 if (new) {
5055 tbl->slots = new;
5056 tbl->max_slots = max_reqs;
5057 }
5058 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005059 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005060 spin_unlock(&tbl->slot_tbl_lock);
5061 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5062 tbl, tbl->slots, tbl->max_slots);
5063out:
5064 dprintk("<-- %s: return %d\n", __func__, ret);
5065 return ret;
5066}
5067
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005068/* Destroy the slot table */
5069static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5070{
5071 if (session->fc_slot_table.slots != NULL) {
5072 kfree(session->fc_slot_table.slots);
5073 session->fc_slot_table.slots = NULL;
5074 }
5075 if (session->bc_slot_table.slots != NULL) {
5076 kfree(session->bc_slot_table.slots);
5077 session->bc_slot_table.slots = NULL;
5078 }
5079 return;
5080}
5081
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005082/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005083 * Initialize slot table
5084 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005085static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5086 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005087{
Andy Adamsonfc931582009-04-01 09:22:31 -04005088 struct nfs4_slot *slot;
5089 int ret = -ENOMEM;
5090
5091 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5092
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005093 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005094
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005095 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005096 if (!slot)
5097 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005098 ret = 0;
5099
5100 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005101 tbl->max_slots = max_slots;
5102 tbl->slots = slot;
5103 tbl->highest_used_slotid = -1; /* no slot is currently used */
5104 spin_unlock(&tbl->slot_tbl_lock);
5105 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5106 tbl, tbl->slots, tbl->max_slots);
5107out:
5108 dprintk("<-- %s: return %d\n", __func__, ret);
5109 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005110}
5111
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005112/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005113 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005114 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005115static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005116{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005117 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005118 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005119
Andy Adamsonaacd5532011-11-09 13:58:21 -05005120 dprintk("--> %s\n", __func__);
5121 /* Fore channel */
5122 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005123 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005124 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5125 if (status) /* -ENOMEM */
5126 return status;
5127 } else {
5128 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005129 if (status)
5130 return status;
5131 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005132 /* Back channel */
5133 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005134 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005135 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005136 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005137 /* Fore and back channel share a connection so get
5138 * both slot tables or neither */
5139 nfs4_destroy_slot_tables(ses);
5140 } else
5141 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005142 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005143}
5144
5145struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5146{
5147 struct nfs4_session *session;
5148 struct nfs4_slot_table *tbl;
5149
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005150 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005151 if (!session)
5152 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005153
Andy Adamson557134a2009-04-01 09:21:53 -04005154 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005155 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005156 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005157 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005158 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005159
5160 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005161 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005162 spin_lock_init(&tbl->slot_tbl_lock);
5163 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005164 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005165
Trond Myklebust1055d762010-06-16 09:52:27 -04005166 session->session_state = 1<<NFS4_SESSION_INITING;
5167
Andy Adamson557134a2009-04-01 09:21:53 -04005168 session->clp = clp;
5169 return session;
5170}
5171
5172void nfs4_destroy_session(struct nfs4_session *session)
5173{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005174 nfs4_proc_destroy_session(session);
5175 dprintk("%s Destroy backchannel for xprt %p\n",
5176 __func__, session->clp->cl_rpcclient->cl_xprt);
5177 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5178 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005179 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005180 kfree(session);
5181}
5182
Andy Adamsonfc931582009-04-01 09:22:31 -04005183/*
5184 * Initialize the values to be used by the client in CREATE_SESSION
5185 * If nfs4_init_session set the fore channel request and response sizes,
5186 * use them.
5187 *
5188 * Set the back channel max_resp_sz_cached to zero to force the client to
5189 * always set csa_cachethis to FALSE because the current implementation
5190 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5191 */
5192static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5193{
5194 struct nfs4_session *session = args->client->cl_session;
5195 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5196 mxresp_sz = session->fc_attrs.max_resp_sz;
5197
5198 if (mxrqst_sz == 0)
5199 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5200 if (mxresp_sz == 0)
5201 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5202 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005203 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5204 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005205 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5206 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5207
5208 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005209 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005210 __func__,
5211 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005212 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005213
5214 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005215 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5216 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5217 args->bc_attrs.max_resp_sz_cached = 0;
5218 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5219 args->bc_attrs.max_reqs = 1;
5220
5221 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5222 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5223 __func__,
5224 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5225 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5226 args->bc_attrs.max_reqs);
5227}
5228
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005229static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005230{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005231 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5232 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5233
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005234 if (rcvd->max_resp_sz > sent->max_resp_sz)
5235 return -EINVAL;
5236 /*
5237 * Our requested max_ops is the minimum we need; we're not
5238 * prepared to break up compounds into smaller pieces than that.
5239 * So, no point even trying to continue if the server won't
5240 * cooperate:
5241 */
5242 if (rcvd->max_ops < sent->max_ops)
5243 return -EINVAL;
5244 if (rcvd->max_reqs == 0)
5245 return -EINVAL;
5246 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005247}
5248
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005249static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5250{
5251 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5252 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005253
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005254 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5255 return -EINVAL;
5256 if (rcvd->max_resp_sz < sent->max_resp_sz)
5257 return -EINVAL;
5258 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5259 return -EINVAL;
5260 /* These would render the backchannel useless: */
5261 if (rcvd->max_ops == 0)
5262 return -EINVAL;
5263 if (rcvd->max_reqs == 0)
5264 return -EINVAL;
5265 return 0;
5266}
Andy Adamson8d353012009-04-01 09:22:32 -04005267
Andy Adamson8d353012009-04-01 09:22:32 -04005268static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5269 struct nfs4_session *session)
5270{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005271 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005272
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005273 ret = nfs4_verify_fore_channel_attrs(args, session);
5274 if (ret)
5275 return ret;
5276 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005277}
5278
Andy Adamsonfc931582009-04-01 09:22:31 -04005279static int _nfs4_proc_create_session(struct nfs_client *clp)
5280{
5281 struct nfs4_session *session = clp->cl_session;
5282 struct nfs41_create_session_args args = {
5283 .client = clp,
5284 .cb_program = NFS4_CALLBACK,
5285 };
5286 struct nfs41_create_session_res res = {
5287 .client = clp,
5288 };
5289 struct rpc_message msg = {
5290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5291 .rpc_argp = &args,
5292 .rpc_resp = &res,
5293 };
5294 int status;
5295
5296 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005297 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005298
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005299 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005300
Andy Adamson8d353012009-04-01 09:22:32 -04005301 if (!status)
5302 /* Verify the session's negotiated channel_attrs values */
5303 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005304 if (!status) {
5305 /* Increment the clientid slot sequence id */
5306 clp->cl_seqid++;
5307 }
5308
5309 return status;
5310}
5311
5312/*
5313 * Issues a CREATE_SESSION operation to the server.
5314 * It is the responsibility of the caller to verify the session is
5315 * expired before calling this routine.
5316 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005317int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005318{
5319 int status;
5320 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005321 struct nfs4_session *session = clp->cl_session;
5322
5323 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5324
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005325 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005326 if (status)
5327 goto out;
5328
Andy Adamsonaacd5532011-11-09 13:58:21 -05005329 /* Init or reset the session slot tables */
5330 status = nfs4_setup_session_slot_tables(session);
5331 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005332 if (status)
5333 goto out;
5334
5335 ptr = (unsigned *)&session->sess_id.data[0];
5336 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5337 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005338out:
5339 dprintk("<-- %s\n", __func__);
5340 return status;
5341}
5342
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005343/*
5344 * Issue the over-the-wire RPC DESTROY_SESSION.
5345 * The caller must serialize access to this routine.
5346 */
5347int nfs4_proc_destroy_session(struct nfs4_session *session)
5348{
5349 int status = 0;
5350 struct rpc_message msg;
5351
5352 dprintk("--> nfs4_proc_destroy_session\n");
5353
5354 /* session is still being setup */
5355 if (session->clp->cl_cons_state != NFS_CS_READY)
5356 return status;
5357
5358 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5359 msg.rpc_argp = session;
5360 msg.rpc_resp = NULL;
5361 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005362 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005363
5364 if (status)
5365 printk(KERN_WARNING
5366 "Got error %d from the server on DESTROY_SESSION. "
5367 "Session has been destroyed regardless...\n", status);
5368
5369 dprintk("<-- nfs4_proc_destroy_session\n");
5370 return status;
5371}
5372
Trond Myklebustfccba802009-07-21 16:48:07 -04005373int nfs4_init_session(struct nfs_server *server)
5374{
5375 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005376 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005377 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005378 int ret;
5379
5380 if (!nfs4_has_session(clp))
5381 return 0;
5382
Trond Myklebust1055d762010-06-16 09:52:27 -04005383 session = clp->cl_session;
5384 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5385 return 0;
5386
Andy Adamson68bf05e2009-12-15 12:55:02 -05005387 rsize = server->rsize;
5388 if (rsize == 0)
5389 rsize = NFS_MAX_FILE_IO_SIZE;
5390 wsize = server->wsize;
5391 if (wsize == 0)
5392 wsize = NFS_MAX_FILE_IO_SIZE;
5393
Andy Adamson68bf05e2009-12-15 12:55:02 -05005394 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5395 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005396
Trond Myklebustfccba802009-07-21 16:48:07 -04005397 ret = nfs4_recover_expired_lease(server);
5398 if (!ret)
5399 ret = nfs4_check_client_ready(clp);
5400 return ret;
5401}
5402
Andy Adamsond83217c2011-03-01 01:34:17 +00005403int nfs4_init_ds_session(struct nfs_client *clp)
5404{
5405 struct nfs4_session *session = clp->cl_session;
5406 int ret;
5407
5408 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5409 return 0;
5410
5411 ret = nfs4_client_recover_expired_lease(clp);
5412 if (!ret)
5413 /* Test for the DS role */
5414 if (!is_ds_client(clp))
5415 ret = -ENODEV;
5416 if (!ret)
5417 ret = nfs4_check_client_ready(clp);
5418 return ret;
5419
5420}
5421EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5422
5423
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005424/*
5425 * Renew the cl_session lease.
5426 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005427struct nfs4_sequence_data {
5428 struct nfs_client *clp;
5429 struct nfs4_sequence_args args;
5430 struct nfs4_sequence_res res;
5431};
5432
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005433static void nfs41_sequence_release(void *data)
5434{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005435 struct nfs4_sequence_data *calldata = data;
5436 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005437
Alexandros Batsakis71358402010-02-05 03:45:05 -08005438 if (atomic_read(&clp->cl_count) > 1)
5439 nfs4_schedule_state_renewal(clp);
5440 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005441 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005442}
5443
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005444static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5445{
5446 switch(task->tk_status) {
5447 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005448 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5449 return -EAGAIN;
5450 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005451 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005452 }
5453 return 0;
5454}
5455
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005456static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005457{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005458 struct nfs4_sequence_data *calldata = data;
5459 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005460
Trond Myklebust14516c32010-07-31 14:29:06 -04005461 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5462 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005463
5464 if (task->tk_status < 0) {
5465 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005466 if (atomic_read(&clp->cl_count) == 1)
5467 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005468
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005469 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5470 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005471 return;
5472 }
5473 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005474 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005475out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005476 dprintk("<-- %s\n", __func__);
5477}
5478
5479static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5480{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005481 struct nfs4_sequence_data *calldata = data;
5482 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005483 struct nfs4_sequence_args *args;
5484 struct nfs4_sequence_res *res;
5485
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005486 args = task->tk_msg.rpc_argp;
5487 res = task->tk_msg.rpc_resp;
5488
Trond Myklebust9d12b212012-01-17 22:04:25 -05005489 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005490 return;
5491 rpc_call_start(task);
5492}
5493
5494static const struct rpc_call_ops nfs41_sequence_ops = {
5495 .rpc_call_done = nfs41_sequence_call_done,
5496 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005497 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005498};
5499
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005500static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005501{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005502 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5505 .rpc_cred = cred,
5506 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005507 struct rpc_task_setup task_setup_data = {
5508 .rpc_client = clp->cl_rpcclient,
5509 .rpc_message = &msg,
5510 .callback_ops = &nfs41_sequence_ops,
5511 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5512 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005513
Alexandros Batsakis71358402010-02-05 03:45:05 -08005514 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005515 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005516 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005517 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005518 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005519 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005520 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005521 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005522 msg.rpc_argp = &calldata->args;
5523 msg.rpc_resp = &calldata->res;
5524 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005525 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005526
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005527 return rpc_run_task(&task_setup_data);
5528}
5529
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005530static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005531{
5532 struct rpc_task *task;
5533 int ret = 0;
5534
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005535 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5536 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005537 task = _nfs41_proc_sequence(clp, cred);
5538 if (IS_ERR(task))
5539 ret = PTR_ERR(task);
5540 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005541 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005542 dprintk("<-- %s status=%d\n", __func__, ret);
5543 return ret;
5544}
5545
5546static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5547{
5548 struct rpc_task *task;
5549 int ret;
5550
5551 task = _nfs41_proc_sequence(clp, cred);
5552 if (IS_ERR(task)) {
5553 ret = PTR_ERR(task);
5554 goto out;
5555 }
5556 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005557 if (!ret) {
5558 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5559
5560 if (task->tk_status == 0)
5561 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005562 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005563 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005564 rpc_put_task(task);
5565out:
5566 dprintk("<-- %s status=%d\n", __func__, ret);
5567 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005568}
5569
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005570struct nfs4_reclaim_complete_data {
5571 struct nfs_client *clp;
5572 struct nfs41_reclaim_complete_args arg;
5573 struct nfs41_reclaim_complete_res res;
5574};
5575
5576static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5577{
5578 struct nfs4_reclaim_complete_data *calldata = data;
5579
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005580 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005581 if (nfs41_setup_sequence(calldata->clp->cl_session,
5582 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005583 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005584 return;
5585
5586 rpc_call_start(task);
5587}
5588
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005589static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5590{
5591 switch(task->tk_status) {
5592 case 0:
5593 case -NFS4ERR_COMPLETE_ALREADY:
5594 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5595 break;
5596 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005597 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005598 /* fall through */
5599 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005600 return -EAGAIN;
5601 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005602 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005603 }
5604 return 0;
5605}
5606
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005607static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5608{
5609 struct nfs4_reclaim_complete_data *calldata = data;
5610 struct nfs_client *clp = calldata->clp;
5611 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5612
5613 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005614 if (!nfs41_sequence_done(task, res))
5615 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005616
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005617 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5618 rpc_restart_call_prepare(task);
5619 return;
5620 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005621 dprintk("<-- %s\n", __func__);
5622}
5623
5624static void nfs4_free_reclaim_complete_data(void *data)
5625{
5626 struct nfs4_reclaim_complete_data *calldata = data;
5627
5628 kfree(calldata);
5629}
5630
5631static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5632 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5633 .rpc_call_done = nfs4_reclaim_complete_done,
5634 .rpc_release = nfs4_free_reclaim_complete_data,
5635};
5636
5637/*
5638 * Issue a global reclaim complete.
5639 */
5640static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5641{
5642 struct nfs4_reclaim_complete_data *calldata;
5643 struct rpc_task *task;
5644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5646 };
5647 struct rpc_task_setup task_setup_data = {
5648 .rpc_client = clp->cl_rpcclient,
5649 .rpc_message = &msg,
5650 .callback_ops = &nfs4_reclaim_complete_call_ops,
5651 .flags = RPC_TASK_ASYNC,
5652 };
5653 int status = -ENOMEM;
5654
5655 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005656 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005657 if (calldata == NULL)
5658 goto out;
5659 calldata->clp = clp;
5660 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005661
Trond Myklebust9d12b212012-01-17 22:04:25 -05005662 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005663 msg.rpc_argp = &calldata->arg;
5664 msg.rpc_resp = &calldata->res;
5665 task_setup_data.callback_data = calldata;
5666 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005667 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005668 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005669 goto out;
5670 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005671 status = nfs4_wait_for_completion_rpc_task(task);
5672 if (status == 0)
5673 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005674 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005675 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005676out:
5677 dprintk("<-- %s status=%d\n", __func__, status);
5678 return status;
5679}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005680
5681static void
5682nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5683{
5684 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005685 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005686
5687 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005688 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5689 * right now covering the LAYOUTGET we are about to send.
5690 * However, that is not so catastrophic, and there seems
5691 * to be no way to prevent it completely.
5692 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005693 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005694 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005695 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005696 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5697 NFS_I(lgp->args.inode)->layout,
5698 lgp->args.ctx->state)) {
5699 rpc_exit(task, NFS4_OK);
5700 return;
5701 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005702 rpc_call_start(task);
5703}
5704
5705static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5706{
5707 struct nfs4_layoutget *lgp = calldata;
5708 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5709
5710 dprintk("--> %s\n", __func__);
5711
5712 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5713 return;
5714
5715 switch (task->tk_status) {
5716 case 0:
5717 break;
5718 case -NFS4ERR_LAYOUTTRYLATER:
5719 case -NFS4ERR_RECALLCONFLICT:
5720 task->tk_status = -NFS4ERR_DELAY;
5721 /* Fall through */
5722 default:
5723 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5724 rpc_restart_call_prepare(task);
5725 return;
5726 }
5727 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005728 dprintk("<-- %s\n", __func__);
5729}
5730
5731static void nfs4_layoutget_release(void *calldata)
5732{
5733 struct nfs4_layoutget *lgp = calldata;
5734
5735 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005736 put_nfs_open_context(lgp->args.ctx);
5737 kfree(calldata);
5738 dprintk("<-- %s\n", __func__);
5739}
5740
5741static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5742 .rpc_call_prepare = nfs4_layoutget_prepare,
5743 .rpc_call_done = nfs4_layoutget_done,
5744 .rpc_release = nfs4_layoutget_release,
5745};
5746
5747int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5748{
5749 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5750 struct rpc_task *task;
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5753 .rpc_argp = &lgp->args,
5754 .rpc_resp = &lgp->res,
5755 };
5756 struct rpc_task_setup task_setup_data = {
5757 .rpc_client = server->client,
5758 .rpc_message = &msg,
5759 .callback_ops = &nfs4_layoutget_call_ops,
5760 .callback_data = lgp,
5761 .flags = RPC_TASK_ASYNC,
5762 };
5763 int status = 0;
5764
5765 dprintk("--> %s\n", __func__);
5766
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005767 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005768 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005769 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005770 task = rpc_run_task(&task_setup_data);
5771 if (IS_ERR(task))
5772 return PTR_ERR(task);
5773 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005774 if (status == 0)
5775 status = task->tk_status;
5776 if (status == 0)
5777 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005778 rpc_put_task(task);
5779 dprintk("<-- %s status=%d\n", __func__, status);
5780 return status;
5781}
5782
Benny Halevycbe82602011-05-22 19:52:37 +03005783static void
5784nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5785{
5786 struct nfs4_layoutreturn *lrp = calldata;
5787
5788 dprintk("--> %s\n", __func__);
5789 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005790 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03005791 return;
5792 rpc_call_start(task);
5793}
5794
5795static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5796{
5797 struct nfs4_layoutreturn *lrp = calldata;
5798 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005799 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005800
5801 dprintk("--> %s\n", __func__);
5802
5803 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5804 return;
5805
5806 server = NFS_SERVER(lrp->args.inode);
5807 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005808 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005809 return;
5810 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005811 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005812 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005813 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005814 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005815 } else
5816 BUG_ON(!list_empty(&lo->plh_segs));
5817 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005818 lo->plh_block_lgets--;
5819 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005820 dprintk("<-- %s\n", __func__);
5821}
5822
5823static void nfs4_layoutreturn_release(void *calldata)
5824{
5825 struct nfs4_layoutreturn *lrp = calldata;
5826
5827 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005828 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005829 kfree(calldata);
5830 dprintk("<-- %s\n", __func__);
5831}
5832
5833static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5834 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5835 .rpc_call_done = nfs4_layoutreturn_done,
5836 .rpc_release = nfs4_layoutreturn_release,
5837};
5838
5839int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5840{
5841 struct rpc_task *task;
5842 struct rpc_message msg = {
5843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5844 .rpc_argp = &lrp->args,
5845 .rpc_resp = &lrp->res,
5846 };
5847 struct rpc_task_setup task_setup_data = {
5848 .rpc_client = lrp->clp->cl_rpcclient,
5849 .rpc_message = &msg,
5850 .callback_ops = &nfs4_layoutreturn_call_ops,
5851 .callback_data = lrp,
5852 };
5853 int status;
5854
5855 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05005856 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03005857 task = rpc_run_task(&task_setup_data);
5858 if (IS_ERR(task))
5859 return PTR_ERR(task);
5860 status = task->tk_status;
5861 dprintk("<-- %s status=%d\n", __func__, status);
5862 rpc_put_task(task);
5863 return status;
5864}
5865
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005866/*
5867 * Retrieve the list of Data Server devices from the MDS.
5868 */
5869static int _nfs4_getdevicelist(struct nfs_server *server,
5870 const struct nfs_fh *fh,
5871 struct pnfs_devicelist *devlist)
5872{
5873 struct nfs4_getdevicelist_args args = {
5874 .fh = fh,
5875 .layoutclass = server->pnfs_curr_ld->id,
5876 };
5877 struct nfs4_getdevicelist_res res = {
5878 .devlist = devlist,
5879 };
5880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5882 .rpc_argp = &args,
5883 .rpc_resp = &res,
5884 };
5885 int status;
5886
5887 dprintk("--> %s\n", __func__);
5888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5889 &res.seq_res, 0);
5890 dprintk("<-- %s status=%d\n", __func__, status);
5891 return status;
5892}
5893
5894int nfs4_proc_getdevicelist(struct nfs_server *server,
5895 const struct nfs_fh *fh,
5896 struct pnfs_devicelist *devlist)
5897{
5898 struct nfs4_exception exception = { };
5899 int err;
5900
5901 do {
5902 err = nfs4_handle_exception(server,
5903 _nfs4_getdevicelist(server, fh, devlist),
5904 &exception);
5905 } while (exception.retry);
5906
5907 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5908 err, devlist->num_devs);
5909
5910 return err;
5911}
5912EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5913
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005914static int
5915_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5916{
5917 struct nfs4_getdeviceinfo_args args = {
5918 .pdev = pdev,
5919 };
5920 struct nfs4_getdeviceinfo_res res = {
5921 .pdev = pdev,
5922 };
5923 struct rpc_message msg = {
5924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5925 .rpc_argp = &args,
5926 .rpc_resp = &res,
5927 };
5928 int status;
5929
5930 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005931 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005932 dprintk("<-- %s status=%d\n", __func__, status);
5933
5934 return status;
5935}
5936
5937int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5938{
5939 struct nfs4_exception exception = { };
5940 int err;
5941
5942 do {
5943 err = nfs4_handle_exception(server,
5944 _nfs4_proc_getdeviceinfo(server, pdev),
5945 &exception);
5946 } while (exception.retry);
5947 return err;
5948}
5949EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5950
Andy Adamson863a3c62011-03-23 13:27:54 +00005951static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5952{
5953 struct nfs4_layoutcommit_data *data = calldata;
5954 struct nfs_server *server = NFS_SERVER(data->args.inode);
5955
5956 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005957 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00005958 return;
5959 rpc_call_start(task);
5960}
5961
5962static void
5963nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5964{
5965 struct nfs4_layoutcommit_data *data = calldata;
5966 struct nfs_server *server = NFS_SERVER(data->args.inode);
5967
5968 if (!nfs4_sequence_done(task, &data->res.seq_res))
5969 return;
5970
5971 switch (task->tk_status) { /* Just ignore these failures */
5972 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5973 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5974 case NFS4ERR_BADLAYOUT: /* no layout */
5975 case NFS4ERR_GRACE: /* loca_recalim always false */
5976 task->tk_status = 0;
5977 }
5978
5979 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005980 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005981 return;
5982 }
5983
5984 if (task->tk_status == 0)
5985 nfs_post_op_update_inode_force_wcc(data->args.inode,
5986 data->res.fattr);
5987}
5988
5989static void nfs4_layoutcommit_release(void *calldata)
5990{
5991 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005992 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005993 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005994
Andy Adamsondb29c082011-07-30 20:52:38 -04005995 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005996 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005997 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5998 list_del_init(&lseg->pls_lc_list);
5999 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6000 &lseg->pls_flags))
6001 put_lseg(lseg);
6002 }
Peng Tao92407e72011-10-23 20:21:17 -07006003
6004 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6005 smp_mb__after_clear_bit();
6006 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6007
Andy Adamson863a3c62011-03-23 13:27:54 +00006008 put_rpccred(data->cred);
6009 kfree(data);
6010}
6011
6012static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6013 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6014 .rpc_call_done = nfs4_layoutcommit_done,
6015 .rpc_release = nfs4_layoutcommit_release,
6016};
6017
6018int
Andy Adamsonef311532011-03-12 02:58:10 -05006019nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006020{
6021 struct rpc_message msg = {
6022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6023 .rpc_argp = &data->args,
6024 .rpc_resp = &data->res,
6025 .rpc_cred = data->cred,
6026 };
6027 struct rpc_task_setup task_setup_data = {
6028 .task = &data->task,
6029 .rpc_client = NFS_CLIENT(data->args.inode),
6030 .rpc_message = &msg,
6031 .callback_ops = &nfs4_layoutcommit_ops,
6032 .callback_data = data,
6033 .flags = RPC_TASK_ASYNC,
6034 };
6035 struct rpc_task *task;
6036 int status = 0;
6037
6038 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6039 "lbw: %llu inode %lu\n",
6040 data->task.tk_pid, sync,
6041 data->args.lastbytewritten,
6042 data->args.inode->i_ino);
6043
Trond Myklebust9d12b212012-01-17 22:04:25 -05006044 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006045 task = rpc_run_task(&task_setup_data);
6046 if (IS_ERR(task))
6047 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006048 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006049 goto out;
6050 status = nfs4_wait_for_completion_rpc_task(task);
6051 if (status != 0)
6052 goto out;
6053 status = task->tk_status;
6054out:
6055 dprintk("%s: status %d\n", __func__, status);
6056 rpc_put_task(task);
6057 return status;
6058}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006059
6060static int
6061_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6062 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6063{
6064 struct nfs41_secinfo_no_name_args args = {
6065 .style = SECINFO_STYLE_CURRENT_FH,
6066 };
6067 struct nfs4_secinfo_res res = {
6068 .flavors = flavors,
6069 };
6070 struct rpc_message msg = {
6071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6072 .rpc_argp = &args,
6073 .rpc_resp = &res,
6074 };
6075 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6076}
6077
6078static int
6079nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6080 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6081{
6082 struct nfs4_exception exception = { };
6083 int err;
6084 do {
6085 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6086 switch (err) {
6087 case 0:
6088 case -NFS4ERR_WRONGSEC:
6089 case -NFS4ERR_NOTSUPP:
6090 break;
6091 default:
6092 err = nfs4_handle_exception(server, err, &exception);
6093 }
6094 } while (exception.retry);
6095 return err;
6096}
6097
6098static int
6099nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6100 struct nfs_fsinfo *info)
6101{
6102 int err;
6103 struct page *page;
6104 rpc_authflavor_t flavor;
6105 struct nfs4_secinfo_flavors *flavors;
6106
6107 page = alloc_page(GFP_KERNEL);
6108 if (!page) {
6109 err = -ENOMEM;
6110 goto out;
6111 }
6112
6113 flavors = page_address(page);
6114 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6115
6116 /*
6117 * Fall back on "guess and check" method if
6118 * the server doesn't support SECINFO_NO_NAME
6119 */
6120 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6121 err = nfs4_find_root_sec(server, fhandle, info);
6122 goto out_freepage;
6123 }
6124 if (err)
6125 goto out_freepage;
6126
6127 flavor = nfs_find_best_sec(flavors);
6128 if (err == 0)
6129 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6130
6131out_freepage:
6132 put_page(page);
6133 if (err == -EACCES)
6134 return -EPERM;
6135out:
6136 return err;
6137}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006138static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6139{
Bryan Schumaker7d974792011-06-02 14:59:08 -04006140 struct nfs41_test_stateid_args args = {
6141 .stateid = &state->stateid,
6142 };
6143 struct nfs41_test_stateid_res res;
6144 struct rpc_message msg = {
6145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6146 .rpc_argp = &args,
6147 .rpc_resp = &res,
6148 };
Trond Myklebust9d12b212012-01-17 22:04:25 -05006149 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6150 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker7d974792011-06-02 14:59:08 -04006151}
6152
6153static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6154{
6155 struct nfs4_exception exception = { };
6156 int err;
6157 do {
6158 err = nfs4_handle_exception(server,
6159 _nfs41_test_stateid(server, state),
6160 &exception);
6161 } while (exception.retry);
6162 return err;
6163}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006164
6165static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6166{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006167 struct nfs41_free_stateid_args args = {
6168 .stateid = &state->stateid,
6169 };
6170 struct nfs41_free_stateid_res res;
6171 struct rpc_message msg = {
6172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6173 .rpc_argp = &args,
6174 .rpc_resp = &res,
6175 };
6176
Trond Myklebust9d12b212012-01-17 22:04:25 -05006177 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6178 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006179}
6180
6181static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6182{
6183 struct nfs4_exception exception = { };
6184 int err;
6185 do {
6186 err = nfs4_handle_exception(server,
6187 _nfs4_free_stateid(server, state),
6188 &exception);
6189 } while (exception.retry);
6190 return err;
6191}
Andy Adamson557134a2009-04-01 09:21:53 -04006192#endif /* CONFIG_NFS_V4_1 */
6193
Andy Adamson591d71c2009-04-01 09:22:47 -04006194struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006195 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006196 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 .recover_open = nfs4_open_reclaim,
6198 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006199 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006200 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201};
6202
Andy Adamson591d71c2009-04-01 09:22:47 -04006203#if defined(CONFIG_NFS_V4_1)
6204struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6207 .recover_open = nfs4_open_reclaim,
6208 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006209 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006210 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006211 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006212};
6213#endif /* CONFIG_NFS_V4_1 */
6214
6215struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 .recover_open = nfs4_open_expired,
6219 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006220 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006221 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222};
6223
Andy Adamson591d71c2009-04-01 09:22:47 -04006224#if defined(CONFIG_NFS_V4_1)
6225struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6226 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6227 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006228 .recover_open = nfs41_open_expired,
6229 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006230 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006231 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006232};
6233#endif /* CONFIG_NFS_V4_1 */
6234
Benny Halevy29fba382009-04-01 09:22:44 -04006235struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6236 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006237 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006238 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006239};
6240
6241#if defined(CONFIG_NFS_V4_1)
6242struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6243 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006244 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006245 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006246};
6247#endif
6248
Trond Myklebust97dc1352010-06-16 09:52:26 -04006249static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6250 .minor_version = 0,
6251 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006252 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006253 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006254 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6255 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6256 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006257};
6258
6259#if defined(CONFIG_NFS_V4_1)
6260static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6261 .minor_version = 1,
6262 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006263 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006264 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006265 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6266 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6267 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006268};
6269#endif
6270
Trond Myklebust97dc1352010-06-16 09:52:26 -04006271const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6272 [0] = &nfs_v4_0_minor_ops,
6273#if defined(CONFIG_NFS_V4_1)
6274 [1] = &nfs_v4_1_minor_ops,
6275#endif
6276};
6277
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006278static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006279 .permission = nfs_permission,
6280 .getattr = nfs_getattr,
6281 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006282 .getxattr = generic_getxattr,
6283 .setxattr = generic_setxattr,
6284 .listxattr = generic_listxattr,
6285 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006286};
6287
David Howells509de812006-08-22 20:06:11 -04006288const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 .version = 4, /* protocol version */
6290 .dentry_ops = &nfs4_dentry_operations,
6291 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006292 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006293 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 .getroot = nfs4_proc_get_root,
6295 .getattr = nfs4_proc_getattr,
6296 .setattr = nfs4_proc_setattr,
6297 .lookup = nfs4_proc_lookup,
6298 .access = nfs4_proc_access,
6299 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 .create = nfs4_proc_create,
6301 .remove = nfs4_proc_remove,
6302 .unlink_setup = nfs4_proc_unlink_setup,
6303 .unlink_done = nfs4_proc_unlink_done,
6304 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006305 .rename_setup = nfs4_proc_rename_setup,
6306 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 .link = nfs4_proc_link,
6308 .symlink = nfs4_proc_symlink,
6309 .mkdir = nfs4_proc_mkdir,
6310 .rmdir = nfs4_proc_remove,
6311 .readdir = nfs4_proc_readdir,
6312 .mknod = nfs4_proc_mknod,
6313 .statfs = nfs4_proc_statfs,
6314 .fsinfo = nfs4_proc_fsinfo,
6315 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006316 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 .decode_dirent = nfs4_decode_dirent,
6318 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006319 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006321 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006323 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006325 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006326 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006327 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006328 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006329 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330};
6331
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006332static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6333 .prefix = XATTR_NAME_NFSV4_ACL,
6334 .list = nfs4_xattr_list_nfs4_acl,
6335 .get = nfs4_xattr_get_nfs4_acl,
6336 .set = nfs4_xattr_set_nfs4_acl,
6337};
6338
6339const struct xattr_handler *nfs4_xattr_handlers[] = {
6340 &nfs4_xattr_nfs4_acl_handler,
6341 NULL
6342};
6343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344/*
6345 * Local variables:
6346 * c-basic-offset: 8
6347 * End:
6348 */