blob: 53ef365f4372872ff9f401ba66acbe4f65ddaf70 [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
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000537int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 struct nfs4_sequence_args *args,
539 struct nfs4_sequence_res *res,
540 int cache_reply,
541 struct rpc_task *task)
542{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 struct nfs4_slot *slot;
544 struct nfs4_slot_table *tbl;
545 u8 slotid;
546
547 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400549 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400550 return 0;
551
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 tbl = &session->fc_slot_table;
553
554 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400555 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800556 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500557 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400559 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500560 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500561 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400562 }
563
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800564 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
565 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
566 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
567 spin_unlock(&tbl->slot_tbl_lock);
568 dprintk("%s enforce FIFO order\n", __func__);
569 return -EAGAIN;
570 }
571
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800572 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573 if (slotid == NFS4_MAX_SLOT_TABLE) {
574 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
575 spin_unlock(&tbl->slot_tbl_lock);
576 dprintk("<-- %s: no free slots\n", __func__);
577 return -EAGAIN;
578 }
579 spin_unlock(&tbl->slot_tbl_lock);
580
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800581 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400582 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400583 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400584 args->sa_slotid = slotid;
585 args->sa_cache_this = cache_reply;
586
587 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
588
Benny Halevy99fe60d2009-04-01 09:22:29 -0400589 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400590 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400592 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 /*
594 * sr_status is only set in decode_sequence, and so will remain
595 * set to 1 if an rpc level failure occurs.
596 */
597 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 return 0;
599}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000600EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601
Trond Myklebust035168a2010-06-16 09:52:26 -0400602int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
605 int cache_reply,
606 struct rpc_task *task)
607{
Trond Myklebust035168a2010-06-16 09:52:26 -0400608 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 int ret = 0;
610
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 if (session == NULL) {
612 args->sa_session = NULL;
613 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400615 }
616
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200617 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400618 __func__, session->clp, session, res->sr_slot ?
619 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400620
621 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
626}
627
628struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
632 int cache_reply;
633};
634
635static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636{
637 struct nfs41_call_sync_data *data = calldata;
638
Trond Myklebust035168a2010-06-16 09:52:26 -0400639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640
641 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642 data->seq_res, data->cache_reply, task))
643 return;
644 rpc_call_start(task);
645}
646
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800647static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
648{
649 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
650 nfs41_call_sync_prepare(task, calldata);
651}
652
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
654{
655 struct nfs41_call_sync_data *data = calldata;
656
Trond Myklebust14516c32010-07-31 14:29:06 -0400657 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400658}
659
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660struct rpc_call_ops nfs41_call_sync_ops = {
661 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400662 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663};
664
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800665struct rpc_call_ops nfs41_call_priv_sync_ops = {
666 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
667 .rpc_call_done = nfs41_call_sync_done,
668};
669
Bryan Schumaker7c513052011-03-24 17:12:24 +0000670static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
671 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 struct rpc_message *msg,
673 struct nfs4_sequence_args *args,
674 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800675 int cache_reply,
676 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677{
678 int ret;
679 struct rpc_task *task;
680 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .seq_args = args,
683 .seq_res = res,
684 .cache_reply = cache_reply,
685 };
686 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000687 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 .rpc_message = msg,
689 .callback_ops = &nfs41_call_sync_ops,
690 .callback_data = &data
691 };
692
Benny Halevydfb4f3092010-09-24 09:17:01 -0400693 res->sr_slot = NULL;
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{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000713 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714}
715
Trond Myklebustdf896452010-06-16 09:52:26 -0400716#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400717static int nfs4_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400719{
Trond Myklebust14516c32010-07-31 14:29:06 -0400720 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400721}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722#endif /* CONFIG_NFS_V4_1 */
723
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724int _nfs4_call_sync(struct rpc_clnt *clnt,
725 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
729 int cache_reply)
730{
Benny Halevyf3752972009-04-01 09:22:04 -0400731 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733}
734
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000735static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000736int nfs4_call_sync(struct rpc_clnt *clnt,
737 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000738 struct rpc_message *msg,
739 struct nfs4_sequence_args *args,
740 struct nfs4_sequence_res *res,
741 int cache_reply)
742{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
744 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Trond Myklebust38478b22006-05-25 01:40:57 -0400751 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400752 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700753 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400754 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700755 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400756 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400760 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct nfs_openargs o_arg;
762 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100763 struct nfs_open_confirmargs c_arg;
764 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500765 struct nfs4_string owner_name;
766 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs_fattr f_attr;
768 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400770 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400772 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100774 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400775 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100776 int rpc_status;
777 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778};
779
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780
781static void nfs4_init_opendata_res(struct nfs4_opendata *p)
782{
783 p->o_res.f_attr = &p->f_attr;
784 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400785 p->o_res.seqid = p->o_arg.seqid;
786 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787 p->o_res.server = p->o_arg.server;
788 nfs_fattr_init(&p->f_attr);
789 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500790 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400791}
792
Al Viro82a2c1b2011-06-22 18:30:55 -0400793static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 const struct iattr *attrs,
796 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797{
Al Viro82a2c1b2011-06-22 18:30:55 -0400798 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 struct inode *dir = parent->d_inode;
800 struct nfs_server *server = NFS_SERVER(dir);
801 struct nfs4_opendata *p;
802
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400803 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804 if (p == NULL)
805 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400806 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 if (p->o_arg.seqid == NULL)
808 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 nfs_sb_active(dentry->d_sb);
810 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->dir = parent;
812 p->owner = sp;
813 atomic_inc(&sp->so_count);
814 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815 p->o_arg.open_flags = flags;
816 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400817 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400818 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 p->o_arg.server = server;
821 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400824 if (flags & O_CREAT) {
825 u32 *s;
826
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 p->o_arg.u.attrs = &p->attrs;
828 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400829 s = (u32 *) p->o_arg.u.verifier.data;
830 s[0] = jiffies;
831 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100833 p->c_arg.fh = &p->o_res.fh;
834 p->c_arg.stateid = &p->o_res.stateid;
835 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400836 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838 return p;
839err_free:
840 kfree(p);
841err:
842 dput(parent);
843 return NULL;
844}
845
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100847{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848 struct nfs4_opendata *p = container_of(kref,
849 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400850 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851
852 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853 if (p->state != NULL)
854 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400855 nfs4_put_state_owner(p->owner);
856 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400857 dput(p->dentry);
858 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500859 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400860 kfree(p);
861}
862
863static void nfs4_opendata_put(struct nfs4_opendata *p)
864{
865 if (p != NULL)
866 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100867}
868
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
870{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 int ret;
872
Trond Myklebust06f814a2006-01-03 09:55:07 +0100873 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100874 return ret;
875}
876
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878{
879 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880
881 if (open_mode & O_EXCL)
882 goto out;
883 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500885 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
886 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 break;
888 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500889 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
890 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 break;
892 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500893 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
894 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400897 return ret;
898}
899
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500902 if (delegation == NULL)
903 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400905 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500906 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500908 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400909 return 1;
910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100913{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100915 case FMODE_WRITE:
916 state->n_wronly++;
917 break;
918 case FMODE_READ:
919 state->n_rdonly++;
920 break;
921 case FMODE_READ|FMODE_WRITE:
922 state->n_rdwr++;
923 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400928{
929 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
930 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
931 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400933 case FMODE_READ:
934 set_bit(NFS_O_RDONLY_STATE, &state->flags);
935 break;
936 case FMODE_WRITE:
937 set_bit(NFS_O_WRONLY_STATE, &state->flags);
938 break;
939 case FMODE_READ|FMODE_WRITE:
940 set_bit(NFS_O_RDWR_STATE, &state->flags);
941 }
942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400948 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static 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 -0700952{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400953 /*
954 * Protect the call to nfs4_state_set_mode_locked and
955 * serialise the stateid update
956 */
957 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400958 if (deleg_stateid != NULL) {
959 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
960 set_bit(NFS_DELEGATED_STATE, &state->flags);
961 }
962 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400964 write_sequnlock(&state->seqlock);
965 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700967 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970static 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 -0500971{
972 struct nfs_inode *nfsi = NFS_I(state->inode);
973 struct nfs_delegation *deleg_cur;
974 int ret = 0;
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500977
978 rcu_read_lock();
979 deleg_cur = rcu_dereference(nfsi->delegation);
980 if (deleg_cur == NULL)
981 goto no_delegation;
982
983 spin_lock(&deleg_cur->lock);
984 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500986 goto no_delegation_unlock;
987
988 if (delegation == NULL)
989 delegation = &deleg_cur->stateid;
990 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
991 goto no_delegation_unlock;
992
Trond Myklebustb7391f42008-12-23 15:21:52 -0500993 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500995 ret = 1;
996no_delegation_unlock:
997 spin_unlock(&deleg_cur->lock);
998no_delegation:
999 rcu_read_unlock();
1000
1001 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001003 ret = 1;
1004 }
1005
1006 return ret;
1007}
1008
1009
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001010static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011{
1012 struct nfs_delegation *delegation;
1013
1014 rcu_read_lock();
1015 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 rcu_read_unlock();
1018 return;
1019 }
1020 rcu_read_unlock();
1021 nfs_inode_return_delegation(inode);
1022}
1023
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025{
1026 struct nfs4_state *state = opendata->state;
1027 struct nfs_inode *nfsi = NFS_I(state->inode);
1028 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1030 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 nfs4_stateid stateid;
1032 int ret = -EAGAIN;
1033
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 if (can_open_cached(state, fmode, open_mode)) {
1038 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001040 goto out_return_state;
1041 }
1042 spin_unlock(&state->owner->so_lock);
1043 }
Trond Myklebust34310432008-12-23 15:21:38 -05001044 rcu_read_lock();
1045 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001046 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001047 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001048 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001049 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 /* Save the delegation */
1051 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1052 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001053 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 if (ret != 0)
1055 goto out;
1056 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001057
1058 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001060 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062out:
1063 return ERR_PTR(ret);
1064out_return_state:
1065 atomic_inc(&state->count);
1066 return state;
1067}
1068
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001069static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1070{
1071 struct inode *inode;
1072 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001073 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001077 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 goto out;
1079 }
1080
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001084 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001086 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001087 goto err;
1088 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001089 state = nfs4_get_open_state(inode, data->owner);
1090 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001091 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001092 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001093 int delegation_flags = 0;
1094
Trond Myklebust003707c2007-07-05 18:07:55 -04001095 rcu_read_lock();
1096 delegation = rcu_dereference(NFS_I(inode)->delegation);
1097 if (delegation)
1098 delegation_flags = delegation->flags;
1099 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001100 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1101 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1102 "returning a delegation for "
1103 "OPEN(CLAIM_DELEGATE_CUR)\n",
1104 NFS_CLIENT(inode)->cl_server);
1105 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001106 nfs_inode_set_delegation(state->inode,
1107 data->owner->so_cred,
1108 &data->o_res);
1109 else
1110 nfs_inode_reclaim_delegation(state->inode,
1111 data->owner->so_cred,
1112 &data->o_res);
1113 }
Trond Myklebust34310432008-12-23 15:21:38 -05001114
1115 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001116 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001119 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001120err_put_inode:
1121 iput(inode);
1122err:
1123 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001124}
1125
Trond Myklebust864472e2006-01-03 09:55:15 +01001126static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1127{
1128 struct nfs_inode *nfsi = NFS_I(state->inode);
1129 struct nfs_open_context *ctx;
1130
1131 spin_lock(&state->inode->i_lock);
1132 list_for_each_entry(ctx, &nfsi->open_files, list) {
1133 if (ctx->state != state)
1134 continue;
1135 get_nfs_open_context(ctx);
1136 spin_unlock(&state->inode->i_lock);
1137 return ctx;
1138 }
1139 spin_unlock(&state->inode->i_lock);
1140 return ERR_PTR(-ENOENT);
1141}
1142
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001143static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1144{
1145 struct nfs4_opendata *opendata;
1146
Al Viro3d4ff432011-06-22 18:40:12 -04001147 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001148 if (opendata == NULL)
1149 return ERR_PTR(-ENOMEM);
1150 opendata->state = state;
1151 atomic_inc(&state->count);
1152 return opendata;
1153}
1154
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001156{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001157 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001158 int ret;
1159
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 opendata->o_arg.open_flags = 0;
1161 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001162 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1163 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1164 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001165 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 if (ret != 0)
1167 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001169 if (IS_ERR(newstate))
1170 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001171 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 return 0;
1174}
1175
1176static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1177{
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 int ret;
1180
1181 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 smp_rmb();
1184 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001185 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 if (ret != 0)
1188 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 if (newstate != state)
1190 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 }
1192 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001193 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 if (newstate != state)
1198 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 }
1200 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001201 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 if (ret != 0)
1204 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001205 if (newstate != state)
1206 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001208 /*
1209 * We may have performed cached opens for all three recoveries.
1210 * Check if we need to update the current stateid.
1211 */
1212 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1213 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001214 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1216 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001217 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001218 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 return 0;
1220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * OPEN_RECLAIM:
1224 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001226static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001228 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001230 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int status;
1232
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001233 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1234 if (IS_ERR(opendata))
1235 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1237 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 rcu_read_lock();
1239 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001240 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001241 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 opendata->o_arg.u.delegation_type = delegation_type;
1244 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001245 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return status;
1247}
1248
Trond Myklebust539cd032007-06-05 11:46:42 -04001249static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct nfs_server *server = NFS_SERVER(state->inode);
1252 struct nfs4_exception exception = { };
1253 int err;
1254 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001255 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001256 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001257 break;
1258 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 } while (exception.retry);
1260 return err;
1261}
1262
Trond Myklebust864472e2006-01-03 09:55:15 +01001263static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1264{
1265 struct nfs_open_context *ctx;
1266 int ret;
1267
1268 ctx = nfs4_state_find_open_context(state);
1269 if (IS_ERR(ctx))
1270 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001271 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 put_nfs_open_context(ctx);
1273 return ret;
1274}
1275
Trond Myklebust13437e12007-07-06 15:10:43 -04001276static 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 -07001277{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001281 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1282 if (IS_ERR(opendata))
1283 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001285 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001286 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001289 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Trond Myklebust13437e12007-07-06 15:10:43 -04001292int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
1294 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001295 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int err;
1297 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001298 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 switch (err) {
1300 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001301 case -ENOENT:
1302 case -ESTALE:
1303 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001304 case -NFS4ERR_BADSESSION:
1305 case -NFS4ERR_BADSLOT:
1306 case -NFS4ERR_BAD_HIGH_SLOT:
1307 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1308 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001309 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001310 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 case -NFS4ERR_STALE_CLIENTID:
1312 case -NFS4ERR_STALE_STATEID:
1313 case -NFS4ERR_EXPIRED:
1314 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001315 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001316 goto out;
1317 case -ERESTARTSYS:
1318 /*
1319 * The show must go on: exit, but mark the
1320 * stateid as needing recovery.
1321 */
1322 case -NFS4ERR_ADMIN_REVOKED:
1323 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001324 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001325 case -EKEYEXPIRED:
1326 /*
1327 * User RPCSEC_GSS context has expired.
1328 * We cannot recover this stateid now, so
1329 * skip it and allow recovery thread to
1330 * proceed.
1331 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001332 case -ENOMEM:
1333 err = 0;
1334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 err = nfs4_handle_exception(server, err, &exception);
1337 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001338out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return err;
1340}
1341
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1343{
1344 struct nfs4_opendata *data = calldata;
1345
1346 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001347 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001348 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1349 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001350 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001351 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001352 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001353 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354}
1355
1356static void nfs4_open_confirm_release(void *calldata)
1357{
1358 struct nfs4_opendata *data = calldata;
1359 struct nfs4_state *state = NULL;
1360
1361 /* If this request hasn't been cancelled, do nothing */
1362 if (data->cancelled == 0)
1363 goto out_free;
1364 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001365 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001367 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001368 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001369 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001370out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001371 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372}
1373
1374static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 .rpc_call_done = nfs4_open_confirm_done,
1376 .rpc_release = nfs4_open_confirm_release,
1377};
1378
1379/*
1380 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1381 */
1382static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1383{
1384 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1385 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001386 struct rpc_message msg = {
1387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1388 .rpc_argp = &data->c_arg,
1389 .rpc_resp = &data->c_res,
1390 .rpc_cred = data->owner->so_cred,
1391 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001392 struct rpc_task_setup task_setup_data = {
1393 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001394 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001395 .callback_ops = &nfs4_open_confirm_ops,
1396 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001397 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001398 .flags = RPC_TASK_ASYNC,
1399 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 int status;
1401
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001402 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001403 data->rpc_done = 0;
1404 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001407 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001409 status = nfs4_wait_for_completion_rpc_task(task);
1410 if (status != 0) {
1411 data->cancelled = 1;
1412 smp_wmb();
1413 } else
1414 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001415 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return status;
1417}
1418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 struct nfs4_opendata *data = calldata;
1422 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1425 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001426 /*
1427 * Check if we still need to send an OPEN call, or if we can use
1428 * a delegation instead.
1429 */
1430 if (data->state != NULL) {
1431 struct nfs_delegation *delegation;
1432
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001434 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001435 rcu_read_lock();
1436 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001437 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1438 can_open_delegated(delegation, data->o_arg.fmode))
1439 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001440 rcu_read_unlock();
1441 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001443 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001444 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001445 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001446 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001447 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1448 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001449 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001450 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001451 &data->o_arg.seq_args,
1452 &data->o_res.seq_res, 1, task))
1453 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001454 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001455 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001456unlock_no_action:
1457 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001458out_no_action:
1459 task->tk_action = NULL;
1460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001463static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1464{
1465 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1466 nfs4_open_prepare(task, calldata);
1467}
1468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469static void nfs4_open_done(struct rpc_task *task, void *calldata)
1470{
1471 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001474
Trond Myklebust14516c32010-07-31 14:29:06 -04001475 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1476 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001477
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 if (task->tk_status == 0) {
1479 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 case S_IFREG:
1481 break;
1482 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001484 break;
1485 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 break;
1488 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001491 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001492 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1493 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001495 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498static void nfs4_open_release(void *calldata)
1499{
1500 struct nfs4_opendata *data = calldata;
1501 struct nfs4_state *state = NULL;
1502
1503 /* If this request hasn't been cancelled, do nothing */
1504 if (data->cancelled == 0)
1505 goto out_free;
1506 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001507 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 goto out_free;
1509 /* In case we need an open_confirm, no cleanup! */
1510 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1511 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001513 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001514 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001516 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001517}
1518
1519static const struct rpc_call_ops nfs4_open_ops = {
1520 .rpc_call_prepare = nfs4_open_prepare,
1521 .rpc_call_done = nfs4_open_done,
1522 .rpc_release = nfs4_open_release,
1523};
1524
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001525static const struct rpc_call_ops nfs4_recover_open_ops = {
1526 .rpc_call_prepare = nfs4_recover_open_prepare,
1527 .rpc_call_done = nfs4_open_done,
1528 .rpc_release = nfs4_open_release,
1529};
1530
1531static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532{
1533 struct inode *dir = data->dir->d_inode;
1534 struct nfs_server *server = NFS_SERVER(dir);
1535 struct nfs_openargs *o_arg = &data->o_arg;
1536 struct nfs_openres *o_res = &data->o_res;
1537 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001538 struct rpc_message msg = {
1539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1540 .rpc_argp = o_arg,
1541 .rpc_resp = o_res,
1542 .rpc_cred = data->owner->so_cred,
1543 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001544 struct rpc_task_setup task_setup_data = {
1545 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001546 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001547 .callback_ops = &nfs4_open_ops,
1548 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001549 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001550 .flags = RPC_TASK_ASYNC,
1551 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001552 int status;
1553
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001554 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001555 data->rpc_done = 0;
1556 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001557 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558 if (isrecover)
1559 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001560 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001561 if (IS_ERR(task))
1562 return PTR_ERR(task);
1563 status = nfs4_wait_for_completion_rpc_task(task);
1564 if (status != 0) {
1565 data->cancelled = 1;
1566 smp_wmb();
1567 } else
1568 status = data->rpc_status;
1569 rpc_put_task(task);
1570
1571 return status;
1572}
1573
1574static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1575{
1576 struct inode *dir = data->dir->d_inode;
1577 struct nfs_openres *o_res = &data->o_res;
1578 int status;
1579
1580 status = nfs4_run_open_task(data, 1);
1581 if (status != 0 || !data->rpc_done)
1582 return status;
1583
Trond Myklebust6926afd2012-01-07 13:22:46 -05001584 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1585
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 nfs_refresh_inode(dir, o_res->dir_attr);
1587
1588 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1589 status = _nfs4_proc_open_confirm(data);
1590 if (status != 0)
1591 return status;
1592 }
1593
1594 return status;
1595}
1596
1597/*
1598 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1599 */
1600static int _nfs4_proc_open(struct nfs4_opendata *data)
1601{
1602 struct inode *dir = data->dir->d_inode;
1603 struct nfs_server *server = NFS_SERVER(dir);
1604 struct nfs_openargs *o_arg = &data->o_arg;
1605 struct nfs_openres *o_res = &data->o_res;
1606 int status;
1607
1608 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001609 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001611 if (status != 0) {
1612 if (status == -NFS4ERR_BADNAME &&
1613 !(o_arg->open_flags & O_CREAT))
1614 return -ENOENT;
1615 return status;
1616 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001617
Trond Myklebust6926afd2012-01-07 13:22:46 -05001618 nfs_fattr_map_and_free_names(server, &data->f_attr);
1619
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001620 if (o_arg->open_flags & O_CREAT) {
1621 update_changeattr(dir, &o_res->cinfo);
1622 nfs_post_op_update_inode(dir, o_res->dir_attr);
1623 } else
1624 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001625 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1626 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001628 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001633 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Andy Adamsond83217c2011-03-01 01:34:17 +00001637static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001638{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001639 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001640 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001641
Trond Myklebusta78cb572009-08-09 15:06:19 -04001642 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001643 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001644 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001645 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001646 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1647 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001648 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001649 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001650 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001651 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001652 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001653}
1654
Andy Adamsond83217c2011-03-01 01:34:17 +00001655static int nfs4_recover_expired_lease(struct nfs_server *server)
1656{
1657 return nfs4_client_recover_expired_lease(server->nfs_client);
1658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/*
1661 * OPEN_EXPIRED:
1662 * reclaim state on the server after a network partition.
1663 * Assumes caller holds the appropriate lock
1664 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001665static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001667 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001668 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001670 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1671 if (IS_ERR(opendata))
1672 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001673 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001674 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001675 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001676 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001677 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001680static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001681{
Trond Myklebust539cd032007-06-05 11:46:42 -04001682 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001683 struct nfs4_exception exception = { };
1684 int err;
1685
1686 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001687 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001688 switch (err) {
1689 default:
1690 goto out;
1691 case -NFS4ERR_GRACE:
1692 case -NFS4ERR_DELAY:
1693 nfs4_handle_exception(server, err, &exception);
1694 err = 0;
1695 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001696 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001697out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001698 return err;
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1702{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Trond Myklebust864472e2006-01-03 09:55:15 +01001706 ctx = nfs4_state_find_open_context(state);
1707 if (IS_ERR(ctx))
1708 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001709 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001710 put_nfs_open_context(ctx);
1711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001714#if defined(CONFIG_NFS_V4_1)
1715static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1716{
1717 int status;
1718 struct nfs_server *server = NFS_SERVER(state->inode);
1719
1720 status = nfs41_test_stateid(server, state);
1721 if (status == NFS_OK)
1722 return 0;
1723 nfs41_free_stateid(server, state);
1724 return nfs4_open_expired(sp, state);
1725}
1726#endif
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001729 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1730 * fields corresponding to attributes that were used to store the verifier.
1731 * Make sure we clobber those fields in the later setattr call
1732 */
1733static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1734{
1735 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1736 !(sattr->ia_valid & ATTR_ATIME_SET))
1737 sattr->ia_valid |= ATTR_ATIME;
1738
1739 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1740 !(sattr->ia_valid & ATTR_MTIME_SET))
1741 sattr->ia_valid |= ATTR_MTIME;
1742}
1743
1744/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001747static 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 -07001748{
1749 struct nfs4_state_owner *sp;
1750 struct nfs4_state *state = NULL;
1751 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001752 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001753 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001757 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1758 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1760 goto out_err;
1761 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001762 status = nfs4_recover_expired_lease(server);
1763 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001764 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001765 if (dentry->d_inode != NULL)
1766 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001767 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001768 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001769 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001770 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Al Viro82a2c1b2011-06-22 18:30:55 -04001772 if (dentry->d_inode != NULL)
1773 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001777 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001780 status = PTR_ERR(state);
1781 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001782 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001783 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001784 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001785
1786 if (opendata->o_arg.open_flags & O_EXCL) {
1787 nfs4_exclusive_attrset(opendata, sattr);
1788
1789 nfs_fattr_init(opendata->o_res.f_attr);
1790 status = nfs4_do_setattr(state->inode, cred,
1791 opendata->o_res.f_attr, sattr,
1792 state);
1793 if (status == 0)
1794 nfs_setattr_update_inode(state->inode, sattr);
1795 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1796 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001797 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 *res = state;
1800 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001801err_opendata_put:
1802 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001803err_put_state_owner:
1804 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 *res = NULL;
1807 return status;
1808}
1809
1810
Al Viro82a2c1b2011-06-22 18:30:55 -04001811static 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 -07001812{
1813 struct nfs4_exception exception = { };
1814 struct nfs4_state *res;
1815 int status;
1816
1817 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001818 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (status == 0)
1820 break;
1821 /* NOTE: BAD_SEQID means the server and client disagree about the
1822 * book-keeping w.r.t. state-changing operations
1823 * (OPEN/CLOSE/LOCK/LOCKU...)
1824 * It is actually a sign of a bug on the client or on the server.
1825 *
1826 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001827 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 * have unhashed the old state_owner for us, and that we can
1829 * therefore safely retry using a new one. We should still warn
1830 * the user though...
1831 */
1832 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001833 printk(KERN_WARNING "NFS: v4 server %s "
1834 " returned a bad sequence-id error!\n",
1835 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 exception.retry = 1;
1837 continue;
1838 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001839 /*
1840 * BAD_STATEID on OPEN means that the server cancelled our
1841 * state before it received the OPEN_CONFIRM.
1842 * Recover by retrying the request as per the discussion
1843 * on Page 181 of RFC3530.
1844 */
1845 if (status == -NFS4ERR_BAD_STATEID) {
1846 exception.retry = 1;
1847 continue;
1848 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849 if (status == -EAGAIN) {
1850 /* We must have found a delegation */
1851 exception.retry = 1;
1852 continue;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1855 status, &exception));
1856 } while (exception.retry);
1857 return res;
1858}
1859
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001860static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1861 struct nfs_fattr *fattr, struct iattr *sattr,
1862 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001864 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001866 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 .iap = sattr,
1868 .server = server,
1869 .bitmask = server->attr_bitmask,
1870 };
1871 struct nfs_setattrres res = {
1872 .fattr = fattr,
1873 .server = server,
1874 };
1875 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1877 .rpc_argp = &arg,
1878 .rpc_resp = &res,
1879 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001881 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001882 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust0e574af2005-10-27 22:12:38 -04001884 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001886 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1887 /* Use that stateid */
1888 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001889 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001890 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1892
Bryan Schumaker7c513052011-03-24 17:12:24 +00001893 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001894 if (status == 0 && state != NULL)
1895 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001896 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001899static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1900 struct nfs_fattr *fattr, struct iattr *sattr,
1901 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001903 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct nfs4_exception exception = { };
1905 int err;
1906 do {
1907 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001908 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 &exception);
1910 } while (exception.retry);
1911 return err;
1912}
1913
1914struct nfs4_closedata {
1915 struct inode *inode;
1916 struct nfs4_state *state;
1917 struct nfs_closeargs arg;
1918 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001919 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001920 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001921 bool roc;
1922 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923};
1924
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001925static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001926{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001927 struct nfs4_closedata *calldata = data;
1928 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001929 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001930
Fred Isamanf7e89172011-01-06 11:36:32 +00001931 if (calldata->roc)
1932 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001933 nfs4_put_open_state(calldata->state);
1934 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001935 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001936 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001937 kfree(calldata);
1938}
1939
Trond Myklebust88069f72009-12-08 08:33:16 -05001940static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1941 fmode_t fmode)
1942{
1943 spin_lock(&state->owner->so_lock);
1944 if (!(fmode & FMODE_READ))
1945 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1946 if (!(fmode & FMODE_WRITE))
1947 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1948 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1949 spin_unlock(&state->owner->so_lock);
1950}
1951
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001952static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001954 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct nfs_server *server = NFS_SERVER(calldata->inode);
1957
Trond Myklebust14516c32010-07-31 14:29:06 -04001958 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* hmm. we are done with the inode, and in the process of freeing
1961 * the state_owner. we keep this around to process errors
1962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 switch (task->tk_status) {
1964 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001965 if (calldata->roc)
1966 pnfs_roc_set_barrier(state->inode,
1967 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001968 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001969 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001970 nfs4_close_clear_stateid_flags(state,
1971 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001974 case -NFS4ERR_OLD_STATEID:
1975 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001977 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001980 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1981 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001983 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001984 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001987static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001989 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001990 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001991 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001992
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001993 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001994 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001995
Trond Myklebust88069f72009-12-08 08:33:16 -05001996 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1997 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001998 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001999 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002000 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002001 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002002 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2003 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2004 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002005 }
2006 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002007 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2008 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2009 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002010 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002011 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002012 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002013
2014 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002015 /* Note: exit _without_ calling nfs4_close_done */
2016 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002017 return;
2018 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002019
Fred Isamanf7e89172011-01-06 11:36:32 +00002020 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002021 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002022 if (calldata->roc &&
2023 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2024 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2025 task, NULL);
2026 return;
2027 }
2028 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002029
Trond Myklebust516a6af2005-10-27 22:12:41 -04002030 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002031 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002032 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002033 &calldata->arg.seq_args, &calldata->res.seq_res,
2034 1, task))
2035 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002036 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002039static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002040 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002041 .rpc_call_done = nfs4_close_done,
2042 .rpc_release = nfs4_free_closedata,
2043};
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/*
2046 * It is possible for data to be read/written from a mem-mapped file
2047 * after the sys_close call (which hits the vfs layer as a flush).
2048 * This means that we can't safely call nfsv4 close on a file until
2049 * the inode is cleared. This in turn means that we are not good
2050 * NFSv4 citizens - we do not indicate to the server to update the file's
2051 * share state even when we are done with one of the three share
2052 * stateid's in the inode.
2053 *
2054 * NOTE: Caller must be holding the sp->so_owner semaphore!
2055 */
Al Viro643168c2011-06-22 18:20:23 -04002056int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002058 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002060 struct nfs4_state_owner *sp = state->owner;
2061 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002062 struct rpc_message msg = {
2063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2064 .rpc_cred = state->owner->so_cred,
2065 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002066 struct rpc_task_setup task_setup_data = {
2067 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002068 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002069 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002070 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002071 .flags = RPC_TASK_ASYNC,
2072 };
Trond Myklebust95121352005-10-18 14:20:12 -07002073 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002075 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002077 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002078 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002080 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002081 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002083 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002084 if (calldata->arg.seqid == NULL)
2085 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002086 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002087 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002088 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002089 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002090 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002091 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002092 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002093
Trond Myklebust1174dd12010-12-21 10:52:24 -05002094 msg.rpc_argp = &calldata->arg;
2095 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002096 task_setup_data.callback_data = calldata;
2097 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002098 if (IS_ERR(task))
2099 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002100 status = 0;
2101 if (wait)
2102 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002103 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002104 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002105out_free_calldata:
2106 kfree(calldata);
2107out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002108 if (roc)
2109 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002110 nfs4_put_open_state(state);
2111 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002112 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Trond Myklebust2b484292010-09-17 10:56:51 -04002115static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002116nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 struct nfs4_state *state;
2119
Trond Myklebust565277f2007-10-15 18:17:53 -04002120 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002121 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002122 if (IS_ERR(state))
2123 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002124 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002125 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Trond Myklebust1185a552009-12-03 15:54:02 -05002128static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002129{
2130 if (ctx->state == NULL)
2131 return;
2132 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002133 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002134 else
Al Viro643168c2011-06-22 18:20:23 -04002135 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2139{
Benny Halevy43652ad2009-04-01 09:21:54 -04002140 struct nfs4_server_caps_arg args = {
2141 .fhandle = fhandle,
2142 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct nfs4_server_caps_res res = {};
2144 struct rpc_message msg = {
2145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002146 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 .rpc_resp = &res,
2148 };
2149 int status;
2150
Bryan Schumaker7c513052011-03-24 17:12:24 +00002151 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (status == 0) {
2153 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002154 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2155 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2156 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2157 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2158 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2160 server->caps |= NFS_CAP_ACLS;
2161 if (res.has_links != 0)
2162 server->caps |= NFS_CAP_HARDLINKS;
2163 if (res.has_symlinks != 0)
2164 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002165 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2166 server->caps |= NFS_CAP_FILEID;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2168 server->caps |= NFS_CAP_MODE;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2170 server->caps |= NFS_CAP_NLINK;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2172 server->caps |= NFS_CAP_OWNER;
2173 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2174 server->caps |= NFS_CAP_OWNER_GROUP;
2175 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2176 server->caps |= NFS_CAP_ATIME;
2177 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2178 server->caps |= NFS_CAP_CTIME;
2179 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2180 server->caps |= NFS_CAP_MTIME;
2181
Trond Myklebusta65318b2009-03-11 14:10:28 -04002182 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2183 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2184 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 server->acl_bitmask = res.acl_bitmask;
2186 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return status;
2189}
2190
Trond Myklebust55a97592006-06-09 09:34:19 -04002191int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 struct nfs4_exception exception = { };
2194 int err;
2195 do {
2196 err = nfs4_handle_exception(server,
2197 _nfs4_server_capabilities(server, fhandle),
2198 &exception);
2199 } while (exception.retry);
2200 return err;
2201}
2202
2203static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2204 struct nfs_fsinfo *info)
2205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 struct nfs4_lookup_root_arg args = {
2207 .bitmask = nfs4_fattr_bitmap,
2208 };
2209 struct nfs4_lookup_res res = {
2210 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002211 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 .fh = fhandle,
2213 };
2214 struct rpc_message msg = {
2215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2216 .rpc_argp = &args,
2217 .rpc_resp = &res,
2218 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002219
Trond Myklebust0e574af2005-10-27 22:12:38 -04002220 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002221 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2225 struct nfs_fsinfo *info)
2226{
2227 struct nfs4_exception exception = { };
2228 int err;
2229 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002230 err = _nfs4_lookup_root(server, fhandle, info);
2231 switch (err) {
2232 case 0:
2233 case -NFS4ERR_WRONGSEC:
2234 break;
2235 default:
2236 err = nfs4_handle_exception(server, err, &exception);
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 } while (exception.retry);
2239 return err;
2240}
2241
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002242static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2243 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2244{
2245 struct rpc_auth *auth;
2246 int ret;
2247
2248 auth = rpcauth_create(flavor, server->client);
2249 if (!auth) {
2250 ret = -EIO;
2251 goto out;
2252 }
2253 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002254out:
2255 return ret;
2256}
2257
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002258static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002259 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002261 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002262 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002264 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2265 flav_array[len] = RPC_AUTH_NULL;
2266 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002267
2268 for (i = 0; i < len; i++) {
2269 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002270 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002271 continue;
2272 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002273 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002274 /*
2275 * -EACCESS could mean that the user doesn't have correct permissions
2276 * to access the mount. It could also mean that we tried to mount
2277 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2278 * existing mount programs don't handle -EACCES very well so it should
2279 * be mapped to -EPERM instead.
2280 */
2281 if (status == -EACCES)
2282 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002283 return status;
2284}
2285
2286/*
2287 * get the file handle for the "/" directory on the server
2288 */
2289static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2290 struct nfs_fsinfo *info)
2291{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002292 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002293 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002294 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2295 /*
2296 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2297 * by nfs4_map_errors() as this function exits.
2298 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002299 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if (status == 0)
2301 status = nfs4_server_capabilities(server, fhandle);
2302 if (status == 0)
2303 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002304 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
Andy Adamson533eb462011-06-13 18:25:56 -04002307static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002308/*
2309 * Get locations and (maybe) other attributes of a referral.
2310 * Note that we'll actually follow the referral later when
2311 * we detect fsid mismatch in inode revalidation
2312 */
Andy Adamson533eb462011-06-13 18:25:56 -04002313static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2314 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002315{
2316 int status = -ENOMEM;
2317 struct page *page = NULL;
2318 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002319
2320 page = alloc_page(GFP_KERNEL);
2321 if (page == NULL)
2322 goto out;
2323 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2324 if (locations == NULL)
2325 goto out;
2326
Trond Myklebustc228fd32007-01-13 02:28:11 -05002327 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002328 if (status != 0)
2329 goto out;
2330 /* Make sure server returned a different fsid for the referral */
2331 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002332 dprintk("%s: server did not return a different fsid for"
2333 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002334 status = -EIO;
2335 goto out;
2336 }
Andy Adamson533eb462011-06-13 18:25:56 -04002337 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2338 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002339
Andy Adamson533eb462011-06-13 18:25:56 -04002340 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002341 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002342 memset(fhandle, 0, sizeof(struct nfs_fh));
2343out:
2344 if (page)
2345 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002346 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002347 return status;
2348}
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2351{
2352 struct nfs4_getattr_arg args = {
2353 .fh = fhandle,
2354 .bitmask = server->attr_bitmask,
2355 };
2356 struct nfs4_getattr_res res = {
2357 .fattr = fattr,
2358 .server = server,
2359 };
2360 struct rpc_message msg = {
2361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2362 .rpc_argp = &args,
2363 .rpc_resp = &res,
2364 };
2365
Trond Myklebust0e574af2005-10-27 22:12:38 -04002366 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002367 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368}
2369
2370static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2371{
2372 struct nfs4_exception exception = { };
2373 int err;
2374 do {
2375 err = nfs4_handle_exception(server,
2376 _nfs4_proc_getattr(server, fhandle, fattr),
2377 &exception);
2378 } while (exception.retry);
2379 return err;
2380}
2381
2382/*
2383 * The file is not closed if it is opened due to the a request to change
2384 * the size of the file. The open call will not be needed once the
2385 * VFS layer lookup-intents are implemented.
2386 *
2387 * Close is called when the inode is destroyed.
2388 * If we haven't opened the file for O_WRONLY, we
2389 * need to in the size_change case to obtain a stateid.
2390 *
2391 * Got race?
2392 * Because OPEN is always done by name in nfsv4, it is
2393 * possible that we opened a different file by the same
2394 * name. We can recognize this race condition, but we
2395 * can't do anything about it besides returning an error.
2396 *
2397 * This will be fixed with VFS changes (lookup-intent).
2398 */
2399static int
2400nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2401 struct iattr *sattr)
2402{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002403 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002404 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002405 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 int status;
2407
Benny Halevy8a1636c2010-07-14 15:43:57 -04002408 if (pnfs_ld_layoutret_on_setattr(inode))
2409 pnfs_return_layout(inode);
2410
Trond Myklebust0e574af2005-10-27 22:12:38 -04002411 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Trond Myklebustd5308382005-11-04 15:33:38 -05002413 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002414 if (sattr->ia_valid & ATTR_FILE) {
2415 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002416
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002417 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002418 if (ctx) {
2419 cred = ctx->cred;
2420 state = ctx->state;
2421 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002422 }
2423
2424 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002425 if (status == 0)
2426 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return status;
2428}
2429
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002430static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2431 const struct qstr *name, struct nfs_fh *fhandle,
2432 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002433{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002434 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002435 int status;
2436 struct nfs4_lookup_arg args = {
2437 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002438 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002439 .name = name,
2440 };
2441 struct nfs4_lookup_res res = {
2442 .server = server,
2443 .fattr = fattr,
2444 .fh = fhandle,
2445 };
2446 struct rpc_message msg = {
2447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2448 .rpc_argp = &args,
2449 .rpc_resp = &res,
2450 };
2451
2452 nfs_fattr_init(fattr);
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002455 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 dprintk("NFS reply lookup: %d\n", status);
2457 return status;
2458}
2459
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002460void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2461{
2462 memset(fh, 0, sizeof(struct nfs_fh));
2463 fattr->fsid.major = 1;
2464 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2465 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2466 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2467 fattr->nlink = 2;
2468}
2469
Bryan Schumaker7c513052011-03-24 17:12:24 +00002470static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2471 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
2473 struct nfs4_exception exception = { };
2474 int err;
2475 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002476 int status;
2477
2478 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2479 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002480 case -NFS4ERR_BADNAME:
2481 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002482 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002483 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002484 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002485 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002486 }
2487 err = nfs4_handle_exception(NFS_SERVER(dir),
2488 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 } while (exception.retry);
2490 return err;
2491}
2492
2493static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2494{
Trond Myklebust76b32992007-08-10 17:45:11 -04002495 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct nfs4_accessargs args = {
2497 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002498 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002500 struct nfs4_accessres res = {
2501 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002502 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct rpc_message msg = {
2504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2505 .rpc_argp = &args,
2506 .rpc_resp = &res,
2507 .rpc_cred = entry->cred,
2508 };
2509 int mode = entry->mask;
2510 int status;
2511
2512 /*
2513 * Determine which access bits we want to ask for...
2514 */
2515 if (mode & MAY_READ)
2516 args.access |= NFS4_ACCESS_READ;
2517 if (S_ISDIR(inode->i_mode)) {
2518 if (mode & MAY_WRITE)
2519 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2520 if (mode & MAY_EXEC)
2521 args.access |= NFS4_ACCESS_LOOKUP;
2522 } else {
2523 if (mode & MAY_WRITE)
2524 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2525 if (mode & MAY_EXEC)
2526 args.access |= NFS4_ACCESS_EXECUTE;
2527 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002528
2529 res.fattr = nfs_alloc_fattr();
2530 if (res.fattr == NULL)
2531 return -ENOMEM;
2532
Bryan Schumaker7c513052011-03-24 17:12:24 +00002533 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (!status) {
2535 entry->mask = 0;
2536 if (res.access & NFS4_ACCESS_READ)
2537 entry->mask |= MAY_READ;
2538 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2539 entry->mask |= MAY_WRITE;
2540 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2541 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002542 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002544 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return status;
2546}
2547
2548static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2549{
2550 struct nfs4_exception exception = { };
2551 int err;
2552 do {
2553 err = nfs4_handle_exception(NFS_SERVER(inode),
2554 _nfs4_proc_access(inode, entry),
2555 &exception);
2556 } while (exception.retry);
2557 return err;
2558}
2559
2560/*
2561 * TODO: For the time being, we don't try to get any attributes
2562 * along with any of the zero-copy operations READ, READDIR,
2563 * READLINK, WRITE.
2564 *
2565 * In the case of the first three, we want to put the GETATTR
2566 * after the read-type operation -- this is because it is hard
2567 * to predict the length of a GETATTR response in v4, and thus
2568 * align the READ data correctly. This means that the GETATTR
2569 * may end up partially falling into the page cache, and we should
2570 * shift it into the 'tail' of the xdr_buf before processing.
2571 * To do this efficiently, we need to know the total length
2572 * of data received, which doesn't seem to be available outside
2573 * of the RPC layer.
2574 *
2575 * In the case of WRITE, we also want to put the GETATTR after
2576 * the operation -- in this case because we want to make sure
2577 * we get the post-operation mtime and size. This means that
2578 * we can't use xdr_encode_pages() as written: we need a variant
2579 * of it which would leave room in the 'tail' iovec.
2580 *
2581 * Both of these changes to the XDR layer would in fact be quite
2582 * minor, but I decided to leave them for a subsequent patch.
2583 */
2584static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2585 unsigned int pgbase, unsigned int pglen)
2586{
2587 struct nfs4_readlink args = {
2588 .fh = NFS_FH(inode),
2589 .pgbase = pgbase,
2590 .pglen = pglen,
2591 .pages = &page,
2592 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002593 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 struct rpc_message msg = {
2595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2596 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002597 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 };
2599
Bryan Schumaker7c513052011-03-24 17:12:24 +00002600 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 -07002601}
2602
2603static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2604 unsigned int pgbase, unsigned int pglen)
2605{
2606 struct nfs4_exception exception = { };
2607 int err;
2608 do {
2609 err = nfs4_handle_exception(NFS_SERVER(inode),
2610 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2611 &exception);
2612 } while (exception.retry);
2613 return err;
2614}
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616/*
2617 * Got race?
2618 * We will need to arrange for the VFS layer to provide an atomic open.
2619 * Until then, this create/open method is prone to inefficiency and race
2620 * conditions due to the lookup, create, and open VFS calls from sys_open()
2621 * placed on the wire.
2622 *
2623 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2624 * The file will be opened again in the subsequent VFS open call
2625 * (nfs4_proc_file_open).
2626 *
2627 * The open for read will just hang around to be used by any process that
2628 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2629 */
2630
2631static int
2632nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002633 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634{
Al Viro82a2c1b2011-06-22 18:30:55 -04002635 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 struct rpc_cred *cred = NULL;
2638 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 int status = 0;
2640
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002641 if (ctx != NULL) {
2642 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002643 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002644 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002646 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002647 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002648 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (IS_ERR(state)) {
2650 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002653 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002654 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002655 if (ctx != NULL)
2656 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002657 else
Al Viro643168c2011-06-22 18:20:23 -04002658 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659out:
2660 return status;
2661}
2662
2663static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2664{
Trond Myklebust16e42952005-10-27 22:12:44 -04002665 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002666 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002668 .name.len = name->len,
2669 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002670 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002672 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002673 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002674 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2677 .rpc_argp = &args,
2678 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002680 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Trond Myklebustd3468902010-04-16 16:22:50 -04002682 res.dir_attr = nfs_alloc_fattr();
2683 if (res.dir_attr == NULL)
2684 goto out;
2685
Bryan Schumaker7c513052011-03-24 17:12:24 +00002686 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002687 if (status == 0) {
2688 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002689 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002690 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002691 nfs_free_fattr(res.dir_attr);
2692out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return status;
2694}
2695
2696static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2697{
2698 struct nfs4_exception exception = { };
2699 int err;
2700 do {
2701 err = nfs4_handle_exception(NFS_SERVER(dir),
2702 _nfs4_proc_remove(dir, name),
2703 &exception);
2704 } while (exception.retry);
2705 return err;
2706}
2707
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002710 struct nfs_server *server = NFS_SERVER(dir);
2711 struct nfs_removeargs *args = msg->rpc_argp;
2712 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Trond Myklebusta65318b2009-03-11 14:10:28 -04002714 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002716 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002720static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002722 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2723
Trond Myklebust14516c32010-07-31 14:29:06 -04002724 if (!nfs4_sequence_done(task, &res->seq_res))
2725 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002726 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002727 return 0;
2728 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002729 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002730 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Jeff Laytond3d41522010-09-17 17:31:57 -04002733static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2734{
2735 struct nfs_server *server = NFS_SERVER(dir);
2736 struct nfs_renameargs *arg = msg->rpc_argp;
2737 struct nfs_renameres *res = msg->rpc_resp;
2738
2739 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2740 arg->bitmask = server->attr_bitmask;
2741 res->server = server;
2742}
2743
2744static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2745 struct inode *new_dir)
2746{
2747 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2748
2749 if (!nfs4_sequence_done(task, &res->seq_res))
2750 return 0;
2751 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2752 return 0;
2753
2754 update_changeattr(old_dir, &res->old_cinfo);
2755 nfs_post_op_update_inode(old_dir, res->old_fattr);
2756 update_changeattr(new_dir, &res->new_cinfo);
2757 nfs_post_op_update_inode(new_dir, res->new_fattr);
2758 return 1;
2759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2762 struct inode *new_dir, struct qstr *new_name)
2763{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002764 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002765 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 .old_dir = NFS_FH(old_dir),
2767 .new_dir = NFS_FH(new_dir),
2768 .old_name = old_name,
2769 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002770 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002772 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002773 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002774 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct rpc_message msg = {
2776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2777 .rpc_argp = &arg,
2778 .rpc_resp = &res,
2779 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002780 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Trond Myklebust011fff72010-04-16 16:22:49 -04002782 res.old_fattr = nfs_alloc_fattr();
2783 res.new_fattr = nfs_alloc_fattr();
2784 if (res.old_fattr == NULL || res.new_fattr == NULL)
2785 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Bryan Schumaker7c513052011-03-24 17:12:24 +00002787 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (!status) {
2789 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002790 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002792 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002794out:
2795 nfs_free_fattr(res.new_fattr);
2796 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return status;
2798}
2799
2800static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2801 struct inode *new_dir, struct qstr *new_name)
2802{
2803 struct nfs4_exception exception = { };
2804 int err;
2805 do {
2806 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2807 _nfs4_proc_rename(old_dir, old_name,
2808 new_dir, new_name),
2809 &exception);
2810 } while (exception.retry);
2811 return err;
2812}
2813
2814static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2815{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002816 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct nfs4_link_arg arg = {
2818 .fh = NFS_FH(inode),
2819 .dir_fh = NFS_FH(dir),
2820 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002821 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002823 struct nfs4_link_res res = {
2824 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 struct rpc_message msg = {
2827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2828 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002829 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002831 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Trond Myklebust136f2622010-04-16 16:22:49 -04002833 res.fattr = nfs_alloc_fattr();
2834 res.dir_attr = nfs_alloc_fattr();
2835 if (res.fattr == NULL || res.dir_attr == NULL)
2836 goto out;
2837
Bryan Schumaker7c513052011-03-24 17:12:24 +00002838 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002839 if (!status) {
2840 update_changeattr(dir, &res.cinfo);
2841 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002842 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002843 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002844out:
2845 nfs_free_fattr(res.dir_attr);
2846 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2851{
2852 struct nfs4_exception exception = { };
2853 int err;
2854 do {
2855 err = nfs4_handle_exception(NFS_SERVER(inode),
2856 _nfs4_proc_link(inode, dir, name),
2857 &exception);
2858 } while (exception.retry);
2859 return err;
2860}
2861
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002862struct nfs4_createdata {
2863 struct rpc_message msg;
2864 struct nfs4_create_arg arg;
2865 struct nfs4_create_res res;
2866 struct nfs_fh fh;
2867 struct nfs_fattr fattr;
2868 struct nfs_fattr dir_fattr;
2869};
2870
2871static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2872 struct qstr *name, struct iattr *sattr, u32 ftype)
2873{
2874 struct nfs4_createdata *data;
2875
2876 data = kzalloc(sizeof(*data), GFP_KERNEL);
2877 if (data != NULL) {
2878 struct nfs_server *server = NFS_SERVER(dir);
2879
2880 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2881 data->msg.rpc_argp = &data->arg;
2882 data->msg.rpc_resp = &data->res;
2883 data->arg.dir_fh = NFS_FH(dir);
2884 data->arg.server = server;
2885 data->arg.name = name;
2886 data->arg.attrs = sattr;
2887 data->arg.ftype = ftype;
2888 data->arg.bitmask = server->attr_bitmask;
2889 data->res.server = server;
2890 data->res.fh = &data->fh;
2891 data->res.fattr = &data->fattr;
2892 data->res.dir_fattr = &data->dir_fattr;
2893 nfs_fattr_init(data->res.fattr);
2894 nfs_fattr_init(data->res.dir_fattr);
2895 }
2896 return data;
2897}
2898
2899static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2900{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002901 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002902 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002903 if (status == 0) {
2904 update_changeattr(dir, &data->res.dir_cinfo);
2905 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2906 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2907 }
2908 return status;
2909}
2910
2911static void nfs4_free_createdata(struct nfs4_createdata *data)
2912{
2913 kfree(data);
2914}
2915
Chuck Lever4f390c12006-08-22 20:06:22 -04002916static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002917 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002919 struct nfs4_createdata *data;
2920 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Chuck Lever94a6d752006-08-22 20:06:23 -04002922 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002924
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002925 status = -ENOMEM;
2926 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2927 if (data == NULL)
2928 goto out;
2929
2930 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2931 data->arg.u.symlink.pages = &page;
2932 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002934 status = nfs4_do_create(dir, dentry, data);
2935
2936 nfs4_free_createdata(data);
2937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return status;
2939}
2940
Chuck Lever4f390c12006-08-22 20:06:22 -04002941static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002942 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 struct nfs4_exception exception = { };
2945 int err;
2946 do {
2947 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002948 _nfs4_proc_symlink(dir, dentry, page,
2949 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 &exception);
2951 } while (exception.retry);
2952 return err;
2953}
2954
2955static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2956 struct iattr *sattr)
2957{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958 struct nfs4_createdata *data;
2959 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002961 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2962 if (data == NULL)
2963 goto out;
2964
2965 status = nfs4_do_create(dir, dentry, data);
2966
2967 nfs4_free_createdata(data);
2968out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return status;
2970}
2971
2972static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2973 struct iattr *sattr)
2974{
2975 struct nfs4_exception exception = { };
2976 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002977
2978 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 do {
2980 err = nfs4_handle_exception(NFS_SERVER(dir),
2981 _nfs4_proc_mkdir(dir, dentry, sattr),
2982 &exception);
2983 } while (exception.retry);
2984 return err;
2985}
2986
2987static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002988 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 struct inode *dir = dentry->d_inode;
2991 struct nfs4_readdir_arg args = {
2992 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002993 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .pgbase = 0,
2995 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002996 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002997 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
2999 struct nfs4_readdir_res res;
3000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3002 .rpc_argp = &args,
3003 .rpc_resp = &res,
3004 .rpc_cred = cred,
3005 };
3006 int status;
3007
Harvey Harrison3110ff82008-05-02 13:42:44 -07003008 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003009 dentry->d_parent->d_name.name,
3010 dentry->d_name.name,
3011 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3013 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003014 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 -05003015 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003017 status += args.pgbase;
3018 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003019
3020 nfs_invalidate_atime(dir);
3021
Harvey Harrison3110ff82008-05-02 13:42:44 -07003022 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return status;
3024}
3025
3026static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003027 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 struct nfs4_exception exception = { };
3030 int err;
3031 do {
3032 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3033 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003034 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 &exception);
3036 } while (exception.retry);
3037 return err;
3038}
3039
3040static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3041 struct iattr *sattr, dev_t rdev)
3042{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 struct nfs4_createdata *data;
3044 int mode = sattr->ia_mode;
3045 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3048 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003049
3050 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3051 if (data == NULL)
3052 goto out;
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003057 data->arg.ftype = NF4BLK;
3058 data->arg.u.device.specdata1 = MAJOR(rdev);
3059 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003062 data->arg.ftype = NF4CHR;
3063 data->arg.u.device.specdata1 = MAJOR(rdev);
3064 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067 status = nfs4_do_create(dir, dentry, data);
3068
3069 nfs4_free_createdata(data);
3070out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return status;
3072}
3073
3074static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3075 struct iattr *sattr, dev_t rdev)
3076{
3077 struct nfs4_exception exception = { };
3078 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003079
3080 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 do {
3082 err = nfs4_handle_exception(NFS_SERVER(dir),
3083 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3084 &exception);
3085 } while (exception.retry);
3086 return err;
3087}
3088
3089static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3090 struct nfs_fsstat *fsstat)
3091{
3092 struct nfs4_statfs_arg args = {
3093 .fh = fhandle,
3094 .bitmask = server->attr_bitmask,
3095 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003096 struct nfs4_statfs_res res = {
3097 .fsstat = fsstat,
3098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct rpc_message msg = {
3100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3101 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003102 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 };
3104
Trond Myklebust0e574af2005-10-27 22:12:38 -04003105 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003106 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
3109static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3110{
3111 struct nfs4_exception exception = { };
3112 int err;
3113 do {
3114 err = nfs4_handle_exception(server,
3115 _nfs4_proc_statfs(server, fhandle, fsstat),
3116 &exception);
3117 } while (exception.retry);
3118 return err;
3119}
3120
3121static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3122 struct nfs_fsinfo *fsinfo)
3123{
3124 struct nfs4_fsinfo_arg args = {
3125 .fh = fhandle,
3126 .bitmask = server->attr_bitmask,
3127 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003128 struct nfs4_fsinfo_res res = {
3129 .fsinfo = fsinfo,
3130 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct rpc_message msg = {
3132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3133 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003134 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 };
3136
Bryan Schumaker7c513052011-03-24 17:12:24 +00003137 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
3140static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3141{
3142 struct nfs4_exception exception = { };
3143 int err;
3144
3145 do {
3146 err = nfs4_handle_exception(server,
3147 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3148 &exception);
3149 } while (exception.retry);
3150 return err;
3151}
3152
3153static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3154{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003155 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3157}
3158
3159static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3160 struct nfs_pathconf *pathconf)
3161{
3162 struct nfs4_pathconf_arg args = {
3163 .fh = fhandle,
3164 .bitmask = server->attr_bitmask,
3165 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003166 struct nfs4_pathconf_res res = {
3167 .pathconf = pathconf,
3168 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3171 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003172 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 };
3174
3175 /* None of the pathconf attributes are mandatory to implement */
3176 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3177 memset(pathconf, 0, sizeof(*pathconf));
3178 return 0;
3179 }
3180
Trond Myklebust0e574af2005-10-27 22:12:38 -04003181 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003182 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
3185static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3186 struct nfs_pathconf *pathconf)
3187{
3188 struct nfs4_exception exception = { };
3189 int err;
3190
3191 do {
3192 err = nfs4_handle_exception(server,
3193 _nfs4_proc_pathconf(server, fhandle, pathconf),
3194 &exception);
3195 } while (exception.retry);
3196 return err;
3197}
3198
Benny Halevyd20581a2011-05-22 19:52:03 +03003199void __nfs4_read_done_cb(struct nfs_read_data *data)
3200{
3201 nfs_invalidate_atime(data->inode);
3202}
3203
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003204static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205{
Trond Myklebustec06c092006-03-20 13:44:27 -05003206 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003208 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003209 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003210 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003212
Benny Halevyd20581a2011-05-22 19:52:03 +03003213 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003215 renew_lease(server, data->timestamp);
3216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003219static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3220{
3221
3222 dprintk("--> %s\n", __func__);
3223
3224 if (!nfs4_sequence_done(task, &data->res.seq_res))
3225 return -EAGAIN;
3226
Benny Halevyd20581a2011-05-22 19:52:03 +03003227 return data->read_done_cb ? data->read_done_cb(task, data) :
3228 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003229}
3230
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003231static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003234 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003235 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003238/* Reset the the nfs_read_data to send the read to the MDS. */
3239void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3240{
3241 dprintk("%s Reset task for i/o through\n", __func__);
3242 put_lseg(data->lseg);
3243 data->lseg = NULL;
3244 /* offsets will differ in the dense stripe case */
3245 data->args.offset = data->mds_offset;
3246 data->ds_clp = NULL;
3247 data->args.fh = NFS_FH(data->inode);
3248 data->read_done_cb = nfs4_read_done_cb;
3249 task->tk_ops = data->mds_ops;
3250 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3251}
3252EXPORT_SYMBOL_GPL(nfs4_reset_read);
3253
Fred Isamanb029bc92011-03-03 15:13:42 +00003254static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct inode *inode = data->inode;
3257
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003258 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003259 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003260 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003262 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003264 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003265 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Fred Isamanb029bc92011-03-03 15:13:42 +00003269static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3270{
3271 if (!nfs4_sequence_done(task, &data->res.seq_res))
3272 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003273 return data->write_done_cb ? data->write_done_cb(task, data) :
3274 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003275}
3276
Fred Isamana69aef12011-03-03 15:13:47 +00003277/* Reset the the nfs_write_data to send the write to the MDS. */
3278void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3279{
3280 dprintk("%s Reset task for i/o through\n", __func__);
3281 put_lseg(data->lseg);
3282 data->lseg = NULL;
3283 data->ds_clp = NULL;
3284 data->write_done_cb = nfs4_write_done_cb;
3285 data->args.fh = NFS_FH(data->inode);
3286 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3287 data->args.offset = data->mds_offset;
3288 data->res.fattr = &data->fattr;
3289 task->tk_ops = data->mds_ops;
3290 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3291}
3292EXPORT_SYMBOL_GPL(nfs4_reset_write);
3293
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003294static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003296 struct nfs_server *server = NFS_SERVER(data->inode);
3297
Fred Isaman7ffd1062011-03-03 15:13:46 +00003298 if (data->lseg) {
3299 data->args.bitmask = NULL;
3300 data->res.fattr = NULL;
3301 } else
3302 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003303 if (!data->write_done_cb)
3304 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003305 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 data->timestamp = jiffies;
3307
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003308 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Fred Isaman5f452432011-03-23 13:27:46 +00003311static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003314
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003315 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003316 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003317 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003319 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Fred Isaman5f452432011-03-23 13:27:46 +00003323static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3324{
3325 if (!nfs4_sequence_done(task, &data->res.seq_res))
3326 return -EAGAIN;
3327 return data->write_done_cb(task, data);
3328}
3329
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003330static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003332 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003333
3334 if (data->lseg) {
3335 data->args.bitmask = NULL;
3336 data->res.fattr = NULL;
3337 } else
3338 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003339 if (!data->write_done_cb)
3340 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003341 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003342 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343}
3344
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003345struct nfs4_renewdata {
3346 struct nfs_client *client;
3347 unsigned long timestamp;
3348};
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350/*
3351 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3352 * standalone procedure for queueing an asynchronous RENEW.
3353 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003354static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003355{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003356 struct nfs4_renewdata *data = calldata;
3357 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003358
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003359 if (atomic_read(&clp->cl_count) > 1)
3360 nfs4_schedule_state_renewal(clp);
3361 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003362 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003363}
3364
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003365static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003367 struct nfs4_renewdata *data = calldata;
3368 struct nfs_client *clp = data->client;
3369 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003372 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003373 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3374 return;
3375 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003376 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003377 return;
3378 }
3379 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003381 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003384static const struct rpc_call_ops nfs4_renew_ops = {
3385 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003386 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003387};
3388
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003389static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 struct rpc_message msg = {
3392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3393 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003394 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003396 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003398 if (renew_flags == 0)
3399 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003400 if (!atomic_inc_not_zero(&clp->cl_count))
3401 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003402 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003403 if (data == NULL)
3404 return -ENOMEM;
3405 data->client = clp;
3406 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003408 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003411static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
3413 struct rpc_message msg = {
3414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3415 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003416 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 };
3418 unsigned long now = jiffies;
3419 int status;
3420
3421 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3422 if (status < 0)
3423 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003424 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return 0;
3426}
3427
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003428static inline int nfs4_server_supports_acls(struct nfs_server *server)
3429{
3430 return (server->caps & NFS_CAP_ACLS)
3431 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3432 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3433}
3434
3435/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3436 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3437 * the stack.
3438 */
3439#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3440
Neil Hormane9e3d722011-03-04 19:26:03 -05003441static int buf_to_pages_noslab(const void *buf, size_t buflen,
3442 struct page **pages, unsigned int *pgbase)
3443{
3444 struct page *newpage, **spages;
3445 int rc = 0;
3446 size_t len;
3447 spages = pages;
3448
3449 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003450 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003451 newpage = alloc_page(GFP_KERNEL);
3452
3453 if (newpage == NULL)
3454 goto unwind;
3455 memcpy(page_address(newpage), buf, len);
3456 buf += len;
3457 buflen -= len;
3458 *pages++ = newpage;
3459 rc++;
3460 } while (buflen != 0);
3461
3462 return rc;
3463
3464unwind:
3465 for(; rc > 0; rc--)
3466 __free_page(spages[rc-1]);
3467 return -ENOMEM;
3468}
3469
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003470struct nfs4_cached_acl {
3471 int cached;
3472 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003473 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003474};
3475
3476static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003477{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003478 struct nfs_inode *nfsi = NFS_I(inode);
3479
3480 spin_lock(&inode->i_lock);
3481 kfree(nfsi->nfs4_acl);
3482 nfsi->nfs4_acl = acl;
3483 spin_unlock(&inode->i_lock);
3484}
3485
3486static void nfs4_zap_acl_attr(struct inode *inode)
3487{
3488 nfs4_set_cached_acl(inode, NULL);
3489}
3490
3491static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3492{
3493 struct nfs_inode *nfsi = NFS_I(inode);
3494 struct nfs4_cached_acl *acl;
3495 int ret = -ENOENT;
3496
3497 spin_lock(&inode->i_lock);
3498 acl = nfsi->nfs4_acl;
3499 if (acl == NULL)
3500 goto out;
3501 if (buf == NULL) /* user is just asking for length */
3502 goto out_len;
3503 if (acl->cached == 0)
3504 goto out;
3505 ret = -ERANGE; /* see getxattr(2) man page */
3506 if (acl->len > buflen)
3507 goto out;
3508 memcpy(buf, acl->data, acl->len);
3509out_len:
3510 ret = acl->len;
3511out:
3512 spin_unlock(&inode->i_lock);
3513 return ret;
3514}
3515
3516static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3517{
3518 struct nfs4_cached_acl *acl;
3519
3520 if (buf && acl_len <= PAGE_SIZE) {
3521 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3522 if (acl == NULL)
3523 goto out;
3524 acl->cached = 1;
3525 memcpy(acl->data, buf, acl_len);
3526 } else {
3527 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3528 if (acl == NULL)
3529 goto out;
3530 acl->cached = 0;
3531 }
3532 acl->len = acl_len;
3533out:
3534 nfs4_set_cached_acl(inode, acl);
3535}
3536
Andy Adamsonbf118a32011-12-07 11:55:27 -05003537/*
3538 * The getxattr API returns the required buffer length when called with a
3539 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3540 * the required buf. On a NULL buf, we send a page of data to the server
3541 * guessing that the ACL request can be serviced by a page. If so, we cache
3542 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3543 * the cache. If not so, we throw away the page, and cache the required
3544 * length. The next getxattr call will then produce another round trip to
3545 * the server, this time with the input buf of the required size.
3546 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003547static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003548{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003549 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003550 struct nfs_getaclargs args = {
3551 .fh = NFS_FH(inode),
3552 .acl_pages = pages,
3553 .acl_len = buflen,
3554 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003555 struct nfs_getaclres res = {
3556 .acl_len = buflen,
3557 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003558 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003559 struct rpc_message msg = {
3560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3561 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003562 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003563 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003564 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003565
Andy Adamsonbf118a32011-12-07 11:55:27 -05003566 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3567 /* As long as we're doing a round trip to the server anyway,
3568 * let's be prepared for a page of acl data. */
3569 if (npages == 0)
3570 npages = 1;
3571
3572 for (i = 0; i < npages; i++) {
3573 pages[i] = alloc_page(GFP_KERNEL);
3574 if (!pages[i])
3575 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003577 if (npages > 1) {
3578 /* for decoding across pages */
3579 args.acl_scratch = alloc_page(GFP_KERNEL);
3580 if (!args.acl_scratch)
3581 goto out_free;
3582 }
3583 args.acl_len = npages * PAGE_SIZE;
3584 args.acl_pgbase = 0;
3585 /* Let decode_getfacl know not to fail if the ACL data is larger than
3586 * the page we send as a guess */
3587 if (buf == NULL)
3588 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3589 resp_buf = page_address(pages[0]);
3590
Peng Taode040be2012-01-10 22:42:47 +08003591 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003592 __func__, buf, buflen, npages, args.acl_len);
3593 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3594 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003595 if (ret)
3596 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003597
3598 acl_len = res.acl_len - res.acl_data_offset;
3599 if (acl_len > args.acl_len)
3600 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003601 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003602 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3603 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003604 if (buf) {
3605 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003606 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003607 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003608 _copy_from_pages(buf, pages, res.acl_data_offset,
3609 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003611 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003612out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003613 for (i = 0; i < npages; i++)
3614 if (pages[i])
3615 __free_page(pages[i]);
3616 if (args.acl_scratch)
3617 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003618 return ret;
3619}
3620
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003621static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3622{
3623 struct nfs4_exception exception = { };
3624 ssize_t ret;
3625 do {
3626 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3627 if (ret >= 0)
3628 break;
3629 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3630 } while (exception.retry);
3631 return ret;
3632}
3633
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003634static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3635{
3636 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003637 int ret;
3638
3639 if (!nfs4_server_supports_acls(server))
3640 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641 ret = nfs_revalidate_inode(server, inode);
3642 if (ret < 0)
3643 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003644 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3645 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003646 ret = nfs4_read_cached_acl(inode, buf, buflen);
3647 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003648 /* -ENOENT is returned if there is no ACL or if there is an ACL
3649 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003650 return ret;
3651 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003652}
3653
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003654static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003655{
3656 struct nfs_server *server = NFS_SERVER(inode);
3657 struct page *pages[NFS4ACL_MAXPAGES];
3658 struct nfs_setaclargs arg = {
3659 .fh = NFS_FH(inode),
3660 .acl_pages = pages,
3661 .acl_len = buflen,
3662 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003663 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003664 struct rpc_message msg = {
3665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3666 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003667 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003668 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003669 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003670
3671 if (!nfs4_server_supports_acls(server))
3672 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003673 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3674 if (i < 0)
3675 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003676 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003677 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003678
3679 /*
3680 * Free each page after tx, so the only ref left is
3681 * held by the network stack
3682 */
3683 for (; i > 0; i--)
3684 put_page(pages[i-1]);
3685
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003686 /*
3687 * Acl update can result in inode attribute update.
3688 * so mark the attribute cache invalid.
3689 */
3690 spin_lock(&inode->i_lock);
3691 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3692 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003693 nfs_access_zap_cache(inode);
3694 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003695 return ret;
3696}
3697
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003698static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3699{
3700 struct nfs4_exception exception = { };
3701 int err;
3702 do {
3703 err = nfs4_handle_exception(NFS_SERVER(inode),
3704 __nfs4_proc_set_acl(inode, buf, buflen),
3705 &exception);
3706 } while (exception.retry);
3707 return err;
3708}
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003711nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003713 struct nfs_client *clp = server->nfs_client;
3714
3715 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return 0;
3717 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003718 case -NFS4ERR_ADMIN_REVOKED:
3719 case -NFS4ERR_BAD_STATEID:
3720 case -NFS4ERR_OPENMODE:
3721 if (state == NULL)
3722 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003723 nfs4_schedule_stateid_recovery(server, state);
3724 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003725 case -NFS4ERR_EXPIRED:
3726 if (state != NULL)
3727 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003729 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003730 nfs4_schedule_lease_recovery(clp);
3731 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003732#if defined(CONFIG_NFS_V4_1)
3733 case -NFS4ERR_BADSESSION:
3734 case -NFS4ERR_BADSLOT:
3735 case -NFS4ERR_BAD_HIGH_SLOT:
3736 case -NFS4ERR_DEADSESSION:
3737 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3738 case -NFS4ERR_SEQ_FALSE_RETRY:
3739 case -NFS4ERR_SEQ_MISORDERED:
3740 dprintk("%s ERROR %d, Reset session\n", __func__,
3741 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003742 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003743 task->tk_status = 0;
3744 return -EAGAIN;
3745#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003747 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003748 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003749 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3751 task->tk_status = 0;
3752 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003753 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 case -NFS4ERR_OLD_STATEID:
3755 task->tk_status = 0;
3756 return -EAGAIN;
3757 }
3758 task->tk_status = nfs4_map_errors(task->tk_status);
3759 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003760wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003761 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003762 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3763 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3764 task->tk_status = 0;
3765 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003768int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3769 unsigned short port, struct rpc_cred *cred,
3770 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
3772 nfs4_verifier sc_verifier;
3773 struct nfs4_setclientid setclientid = {
3774 .sc_verifier = &sc_verifier,
3775 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003776 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 };
3778 struct rpc_message msg = {
3779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3780 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003781 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003782 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 };
Al Virobc4785c2006-10-19 23:28:51 -07003784 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 int loop = 0;
3786 int status;
3787
Al Virobc4785c2006-10-19 23:28:51 -07003788 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3790 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3791
3792 for(;;) {
3793 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003794 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003795 clp->cl_ipaddr,
3796 rpc_peeraddr2str(clp->cl_rpcclient,
3797 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003798 rpc_peeraddr2str(clp->cl_rpcclient,
3799 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003800 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 clp->cl_id_uniquifier);
3802 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003803 sizeof(setclientid.sc_netid),
3804 rpc_peeraddr2str(clp->cl_rpcclient,
3805 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003807 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 clp->cl_ipaddr, port >> 8, port & 255);
3809
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003810 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if (status != -NFS4ERR_CLID_INUSE)
3812 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003813 if (loop != 0) {
3814 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003816 }
3817 ++loop;
3818 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 }
3820 return status;
3821}
3822
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003823int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003824 struct nfs4_setclientid_res *arg,
3825 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
3827 struct nfs_fsinfo fsinfo;
3828 struct rpc_message msg = {
3829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003830 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003832 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 };
3834 unsigned long now;
3835 int status;
3836
3837 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003838 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 if (status == 0) {
3840 spin_lock(&clp->cl_lock);
3841 clp->cl_lease_time = fsinfo.lease_time * HZ;
3842 clp->cl_last_renewal = now;
3843 spin_unlock(&clp->cl_lock);
3844 }
3845 return status;
3846}
3847
Trond Myklebustfe650402006-01-03 09:55:18 +01003848struct nfs4_delegreturndata {
3849 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003850 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003851 struct nfs_fh fh;
3852 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003853 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003854 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003855 int rpc_status;
3856};
3857
Trond Myklebustfe650402006-01-03 09:55:18 +01003858static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3859{
3860 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003861
Trond Myklebust14516c32010-07-31 14:29:06 -04003862 if (!nfs4_sequence_done(task, &data->res.seq_res))
3863 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003864
Ricardo Labiaga79708862009-12-07 09:23:21 -05003865 switch (task->tk_status) {
3866 case -NFS4ERR_STALE_STATEID:
3867 case -NFS4ERR_EXPIRED:
3868 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003869 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003870 break;
3871 default:
3872 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3873 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003874 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003875 return;
3876 }
3877 }
3878 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003879}
3880
3881static void nfs4_delegreturn_release(void *calldata)
3882{
Trond Myklebustfe650402006-01-03 09:55:18 +01003883 kfree(calldata);
3884}
3885
Andy Adamson938e1012009-04-01 09:22:28 -04003886#if defined(CONFIG_NFS_V4_1)
3887static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3888{
3889 struct nfs4_delegreturndata *d_data;
3890
3891 d_data = (struct nfs4_delegreturndata *)data;
3892
Trond Myklebust035168a2010-06-16 09:52:26 -04003893 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003894 &d_data->args.seq_args,
3895 &d_data->res.seq_res, 1, task))
3896 return;
3897 rpc_call_start(task);
3898}
3899#endif /* CONFIG_NFS_V4_1 */
3900
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003901static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003902#if defined(CONFIG_NFS_V4_1)
3903 .rpc_call_prepare = nfs4_delegreturn_prepare,
3904#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003905 .rpc_call_done = nfs4_delegreturn_done,
3906 .rpc_release = nfs4_delegreturn_release,
3907};
3908
Trond Myklebuste6f81072008-01-24 18:14:34 -05003909static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003910{
3911 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003912 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003913 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003914 struct rpc_message msg = {
3915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3916 .rpc_cred = cred,
3917 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003918 struct rpc_task_setup task_setup_data = {
3919 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003920 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003921 .callback_ops = &nfs4_delegreturn_ops,
3922 .flags = RPC_TASK_ASYNC,
3923 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003924 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003925
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003926 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003927 if (data == NULL)
3928 return -ENOMEM;
3929 data->args.fhandle = &data->fh;
3930 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003931 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003932 nfs_copy_fh(&data->fh, NFS_FH(inode));
3933 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003934 data->res.fattr = &data->fattr;
3935 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003936 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003937 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003938 data->rpc_status = 0;
3939
Trond Myklebustc970aa82007-07-14 15:39:59 -04003940 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003941 msg.rpc_argp = &data->args;
3942 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003943 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003944 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003945 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003946 if (!issync)
3947 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003948 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003949 if (status != 0)
3950 goto out;
3951 status = data->rpc_status;
3952 if (status != 0)
3953 goto out;
3954 nfs_refresh_inode(inode, &data->fattr);
3955out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003956 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003957 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Trond Myklebuste6f81072008-01-24 18:14:34 -05003960int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 struct nfs_server *server = NFS_SERVER(inode);
3963 struct nfs4_exception exception = { };
3964 int err;
3965 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003966 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 switch (err) {
3968 case -NFS4ERR_STALE_STATEID:
3969 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 case 0:
3971 return 0;
3972 }
3973 err = nfs4_handle_exception(server, err, &exception);
3974 } while (exception.retry);
3975 return err;
3976}
3977
3978#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3979#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3980
3981/*
3982 * sleep, with exponential backoff, and retry the LOCK operation.
3983 */
3984static unsigned long
3985nfs4_set_lock_task_retry(unsigned long timeout)
3986{
Jeff Laytond3103102011-12-01 22:44:39 +01003987 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 timeout <<= 1;
3989 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3990 return NFS4_LOCK_MAXTIMEOUT;
3991 return timeout;
3992}
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3995{
3996 struct inode *inode = state->inode;
3997 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003998 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003999 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004001 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004003 struct nfs_lockt_res res = {
4004 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 };
4006 struct rpc_message msg = {
4007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4008 .rpc_argp = &arg,
4009 .rpc_resp = &res,
4010 .rpc_cred = state->owner->so_cred,
4011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 struct nfs4_lock_state *lsp;
4013 int status;
4014
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004015 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004016 status = nfs4_set_lock_state(state, request);
4017 if (status != 0)
4018 goto out;
4019 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004020 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004021 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004022 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004023 switch (status) {
4024 case 0:
4025 request->fl_type = F_UNLCK;
4026 break;
4027 case -NFS4ERR_DENIED:
4028 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004030 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004031out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return status;
4033}
4034
4035static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4036{
4037 struct nfs4_exception exception = { };
4038 int err;
4039
4040 do {
4041 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4042 _nfs4_proc_getlk(state, cmd, request),
4043 &exception);
4044 } while (exception.retry);
4045 return err;
4046}
4047
4048static int do_vfs_lock(struct file *file, struct file_lock *fl)
4049{
4050 int res = 0;
4051 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4052 case FL_POSIX:
4053 res = posix_lock_file_wait(file, fl);
4054 break;
4055 case FL_FLOCK:
4056 res = flock_lock_file_wait(file, fl);
4057 break;
4058 default:
4059 BUG();
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return res;
4062}
4063
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004064struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004065 struct nfs_locku_args arg;
4066 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067 struct nfs4_lock_state *lsp;
4068 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004069 struct file_lock fl;
4070 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004071 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004072};
4073
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004074static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4075 struct nfs_open_context *ctx,
4076 struct nfs4_lock_state *lsp,
4077 struct nfs_seqid *seqid)
4078{
4079 struct nfs4_unlockdata *p;
4080 struct inode *inode = lsp->ls_state->inode;
4081
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004082 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004083 if (p == NULL)
4084 return NULL;
4085 p->arg.fh = NFS_FH(inode);
4086 p->arg.fl = &p->fl;
4087 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004088 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004089 p->arg.stateid = &lsp->ls_stateid;
4090 p->lsp = lsp;
4091 atomic_inc(&lsp->ls_count);
4092 /* Ensure we don't close file until we're done freeing locks! */
4093 p->ctx = get_nfs_open_context(ctx);
4094 memcpy(&p->fl, fl, sizeof(p->fl));
4095 p->server = NFS_SERVER(inode);
4096 return p;
4097}
4098
Trond Myklebust06f814a2006-01-03 09:55:07 +01004099static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004101 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004102 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004103 nfs4_put_lock_state(calldata->lsp);
4104 put_nfs_open_context(calldata->ctx);
4105 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004106}
4107
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004108static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004109{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004110 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111
Trond Myklebust14516c32010-07-31 14:29:06 -04004112 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4113 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004114 switch (task->tk_status) {
4115 case 0:
4116 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004117 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004118 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004119 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004120 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004121 case -NFS4ERR_BAD_STATEID:
4122 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004123 case -NFS4ERR_STALE_STATEID:
4124 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004125 break;
4126 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004127 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004128 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004129 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004130}
4131
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004132static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004133{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004134 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004136 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004137 return;
4138 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004139 /* Note: exit _without_ running nfs4_locku_done */
4140 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004141 return;
4142 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004143 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004144 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004145 &calldata->arg.seq_args,
4146 &calldata->res.seq_res, 1, task))
4147 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004148 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004151static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004152 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004153 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004154 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004155};
4156
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4158 struct nfs_open_context *ctx,
4159 struct nfs4_lock_state *lsp,
4160 struct nfs_seqid *seqid)
4161{
4162 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004163 struct rpc_message msg = {
4164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4165 .rpc_cred = ctx->cred,
4166 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004167 struct rpc_task_setup task_setup_data = {
4168 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004169 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004170 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004171 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004172 .flags = RPC_TASK_ASYNC,
4173 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004174
Frank Filz137d6ac2007-07-09 15:32:29 -07004175 /* Ensure this is an unlock - when canceling a lock, the
4176 * canceled lock is passed in, and it won't be an unlock.
4177 */
4178 fl->fl_type = F_UNLCK;
4179
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004180 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4181 if (data == NULL) {
4182 nfs_free_seqid(seqid);
4183 return ERR_PTR(-ENOMEM);
4184 }
4185
Trond Myklebust1174dd12010-12-21 10:52:24 -05004186 msg.rpc_argp = &data->arg;
4187 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004188 task_setup_data.callback_data = data;
4189 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004190}
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4193{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004194 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004195 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004196 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004197 struct rpc_task *task;
4198 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004199 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Trond Myklebust9b073572006-06-29 16:38:34 -04004201 status = nfs4_set_lock_state(state, request);
4202 /* Unlock _before_ we do the RPC call */
4203 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004204 down_read(&nfsi->rwsem);
4205 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4206 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004207 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004208 }
4209 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004210 if (status != 0)
4211 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004212 /* Is this a delegated lock? */
4213 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004214 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004215 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004216 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004217 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004218 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004219 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004220 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221 status = PTR_ERR(task);
4222 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004223 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004225 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004226out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004227 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004228 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229}
4230
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231struct nfs4_lockdata {
4232 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004233 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004234 struct nfs4_lock_state *lsp;
4235 struct nfs_open_context *ctx;
4236 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004237 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238 int rpc_status;
4239 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004240 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241};
4242
4243static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004244 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4245 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004246{
4247 struct nfs4_lockdata *p;
4248 struct inode *inode = lsp->ls_state->inode;
4249 struct nfs_server *server = NFS_SERVER(inode);
4250
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004251 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004252 if (p == NULL)
4253 return NULL;
4254
4255 p->arg.fh = NFS_FH(inode);
4256 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004257 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004258 if (p->arg.open_seqid == NULL)
4259 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004260 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004261 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004262 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004263 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004264 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004265 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004266 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004267 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004269 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 atomic_inc(&lsp->ls_count);
4271 p->ctx = get_nfs_open_context(ctx);
4272 memcpy(&p->fl, fl, sizeof(p->fl));
4273 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004274out_free_seqid:
4275 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276out_free:
4277 kfree(p);
4278 return NULL;
4279}
4280
4281static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4282{
4283 struct nfs4_lockdata *data = calldata;
4284 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Harvey Harrison3110ff82008-05-02 13:42:44 -07004286 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004287 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4288 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004289 /* Do we need to do an open_to_lock_owner? */
4290 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004291 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4292 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293 data->arg.open_stateid = &state->stateid;
4294 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004295 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004296 } else
4297 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004298 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004299 if (nfs4_setup_sequence(data->server,
4300 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004301 &data->res.seq_res, 1, task))
4302 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004303 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004304 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305}
4306
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004307static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4308{
4309 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4310 nfs4_lock_prepare(task, calldata);
4311}
4312
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004313static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4314{
4315 struct nfs4_lockdata *data = calldata;
4316
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318
Trond Myklebust14516c32010-07-31 14:29:06 -04004319 if (!nfs4_sequence_done(task, &data->res.seq_res))
4320 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004321
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004323 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004324 if (data->rpc_status == 0)
4325 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4326 else
4327 goto out;
4328 }
4329 if (data->rpc_status == 0) {
4330 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4331 sizeof(data->lsp->ls_stateid.data));
4332 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004333 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004335out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004336 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004337}
4338
4339static void nfs4_lock_release(void *calldata)
4340{
4341 struct nfs4_lockdata *data = calldata;
4342
Harvey Harrison3110ff82008-05-02 13:42:44 -07004343 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004344 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004345 if (data->cancelled != 0) {
4346 struct rpc_task *task;
4347 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4348 data->arg.lock_seqid);
4349 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004350 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004351 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352 } else
4353 nfs_free_seqid(data->arg.lock_seqid);
4354 nfs4_put_lock_state(data->lsp);
4355 put_nfs_open_context(data->ctx);
4356 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004357 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358}
4359
4360static const struct rpc_call_ops nfs4_lock_ops = {
4361 .rpc_call_prepare = nfs4_lock_prepare,
4362 .rpc_call_done = nfs4_lock_done,
4363 .rpc_release = nfs4_lock_release,
4364};
4365
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004366static const struct rpc_call_ops nfs4_recover_lock_ops = {
4367 .rpc_call_prepare = nfs4_recover_lock_prepare,
4368 .rpc_call_done = nfs4_lock_done,
4369 .rpc_release = nfs4_lock_release,
4370};
4371
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004372static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4373{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004374 switch (error) {
4375 case -NFS4ERR_ADMIN_REVOKED:
4376 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004377 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004378 if (new_lock_owner != 0 ||
4379 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004380 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004381 break;
4382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004383 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004384 case -NFS4ERR_EXPIRED:
4385 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004386 };
4387}
4388
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004389static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390{
4391 struct nfs4_lockdata *data;
4392 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4395 .rpc_cred = state->owner->so_cred,
4396 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004397 struct rpc_task_setup task_setup_data = {
4398 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004399 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004400 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004401 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004402 .flags = RPC_TASK_ASYNC,
4403 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004404 int ret;
4405
Harvey Harrison3110ff82008-05-02 13:42:44 -07004406 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004407 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004408 fl->fl_u.nfs4_fl.owner,
4409 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 if (data == NULL)
4411 return -ENOMEM;
4412 if (IS_SETLKW(cmd))
4413 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004414 if (recovery_type > NFS_LOCK_NEW) {
4415 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004416 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004417 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4418 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004419 msg.rpc_argp = &data->arg;
4420 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004421 task_setup_data.callback_data = data;
4422 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004423 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004425 ret = nfs4_wait_for_completion_rpc_task(task);
4426 if (ret == 0) {
4427 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004428 if (ret)
4429 nfs4_handle_setlk_error(data->server, data->lsp,
4430 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004431 } else
4432 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004433 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004434 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
4438static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4439{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004440 struct nfs_server *server = NFS_SERVER(state->inode);
4441 struct nfs4_exception exception = { };
4442 int err;
4443
4444 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004445 /* Cache the lock if possible... */
4446 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4447 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004448 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004449 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004450 break;
4451 nfs4_handle_exception(server, err, &exception);
4452 } while (exception.retry);
4453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454}
4455
4456static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4457{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004458 struct nfs_server *server = NFS_SERVER(state->inode);
4459 struct nfs4_exception exception = { };
4460 int err;
4461
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004462 err = nfs4_set_lock_state(state, request);
4463 if (err != 0)
4464 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004465 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004466 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4467 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004468 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004469 switch (err) {
4470 default:
4471 goto out;
4472 case -NFS4ERR_GRACE:
4473 case -NFS4ERR_DELAY:
4474 nfs4_handle_exception(server, err, &exception);
4475 err = 0;
4476 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004477 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004478out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004482#if defined(CONFIG_NFS_V4_1)
4483static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4484{
4485 int status;
4486 struct nfs_server *server = NFS_SERVER(state->inode);
4487
4488 status = nfs41_test_stateid(server, state);
4489 if (status == NFS_OK)
4490 return 0;
4491 nfs41_free_stateid(server, state);
4492 return nfs4_lock_expired(state, request);
4493}
4494#endif
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4497{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004498 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004499 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004500 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004502 if ((fl_flags & FL_POSIX) &&
4503 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4504 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004505 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004506 status = nfs4_set_lock_state(state, request);
4507 if (status != 0)
4508 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004509 request->fl_flags |= FL_ACCESS;
4510 status = do_vfs_lock(request->fl_file, request);
4511 if (status < 0)
4512 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004513 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004514 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004515 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004516 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004517 request->fl_flags = fl_flags & ~FL_SLEEP;
4518 status = do_vfs_lock(request->fl_file, request);
4519 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004520 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004521 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004522 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004523 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004524 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004525 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004526 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004527 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004528out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004529 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004530out:
4531 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return status;
4533}
4534
4535static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4536{
4537 struct nfs4_exception exception = { };
4538 int err;
4539
4540 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004541 err = _nfs4_proc_setlk(state, cmd, request);
4542 if (err == -NFS4ERR_DENIED)
4543 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004545 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 } while (exception.retry);
4547 return err;
4548}
4549
4550static int
4551nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4552{
4553 struct nfs_open_context *ctx;
4554 struct nfs4_state *state;
4555 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4556 int status;
4557
4558 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004559 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 state = ctx->state;
4561
4562 if (request->fl_start < 0 || request->fl_end < 0)
4563 return -EINVAL;
4564
Trond Myklebustd9531262009-07-21 19:22:38 -04004565 if (IS_GETLK(cmd)) {
4566 if (state != NULL)
4567 return nfs4_proc_getlk(state, F_GETLK, request);
4568 return 0;
4569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
4571 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4572 return -EINVAL;
4573
Trond Myklebustd9531262009-07-21 19:22:38 -04004574 if (request->fl_type == F_UNLCK) {
4575 if (state != NULL)
4576 return nfs4_proc_unlck(state, cmd, request);
4577 return 0;
4578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Trond Myklebustd9531262009-07-21 19:22:38 -04004580 if (state == NULL)
4581 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 do {
4583 status = nfs4_proc_setlk(state, cmd, request);
4584 if ((status != -EAGAIN) || IS_SETLK(cmd))
4585 break;
4586 timeout = nfs4_set_lock_task_retry(timeout);
4587 status = -ERESTARTSYS;
4588 if (signalled())
4589 break;
4590 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return status;
4592}
4593
Trond Myklebust888e6942005-11-04 15:38:11 -05004594int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4595{
4596 struct nfs_server *server = NFS_SERVER(state->inode);
4597 struct nfs4_exception exception = { };
4598 int err;
4599
4600 err = nfs4_set_lock_state(state, fl);
4601 if (err != 0)
4602 goto out;
4603 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004604 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004605 switch (err) {
4606 default:
4607 printk(KERN_ERR "%s: unhandled error %d.\n",
4608 __func__, err);
4609 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004610 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004611 goto out;
4612 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004613 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004614 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004615 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004616 nfs4_schedule_lease_recovery(server->nfs_client);
4617 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004618 case -NFS4ERR_BADSESSION:
4619 case -NFS4ERR_BADSLOT:
4620 case -NFS4ERR_BAD_HIGH_SLOT:
4621 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4622 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004623 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004624 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004625 case -ERESTARTSYS:
4626 /*
4627 * The show must go on: exit, but mark the
4628 * stateid as needing recovery.
4629 */
4630 case -NFS4ERR_ADMIN_REVOKED:
4631 case -NFS4ERR_BAD_STATEID:
4632 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004633 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004634 err = 0;
4635 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004636 case -EKEYEXPIRED:
4637 /*
4638 * User RPCSEC_GSS context has expired.
4639 * We cannot recover this stateid now, so
4640 * skip it and allow recovery thread to
4641 * proceed.
4642 */
4643 err = 0;
4644 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004645 case -ENOMEM:
4646 case -NFS4ERR_DENIED:
4647 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4648 err = 0;
4649 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004650 case -NFS4ERR_DELAY:
4651 break;
4652 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004653 err = nfs4_handle_exception(server, err, &exception);
4654 } while (exception.retry);
4655out:
4656 return err;
4657}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004658
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004659static void nfs4_release_lockowner_release(void *calldata)
4660{
4661 kfree(calldata);
4662}
4663
4664const struct rpc_call_ops nfs4_release_lockowner_ops = {
4665 .rpc_release = nfs4_release_lockowner_release,
4666};
4667
4668void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4669{
4670 struct nfs_server *server = lsp->ls_state->owner->so_server;
4671 struct nfs_release_lockowner_args *args;
4672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4674 };
4675
4676 if (server->nfs_client->cl_mvops->minor_version != 0)
4677 return;
4678 args = kmalloc(sizeof(*args), GFP_NOFS);
4679 if (!args)
4680 return;
4681 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4682 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004683 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004684 msg.rpc_argp = args;
4685 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4686}
4687
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004688#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4689
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004690static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4691 const void *buf, size_t buflen,
4692 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004693{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004694 if (strcmp(key, "") != 0)
4695 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004696
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004697 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004698}
4699
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004700static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4701 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004702{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004703 if (strcmp(key, "") != 0)
4704 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004705
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004706 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004707}
4708
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004709static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4710 size_t list_len, const char *name,
4711 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004712{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004713 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004714
J. Bruce Fields096455a2006-03-20 23:23:42 -05004715 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4716 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004717
4718 if (list && len <= list_len)
4719 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004720 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004721}
4722
Andy Adamson533eb462011-06-13 18:25:56 -04004723/*
4724 * nfs_fhget will use either the mounted_on_fileid or the fileid
4725 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004726static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4727{
Andy Adamson533eb462011-06-13 18:25:56 -04004728 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4729 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4730 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4731 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004732 return;
4733
4734 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4735 NFS_ATTR_FATTR_NLINK;
4736 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4737 fattr->nlink = 2;
4738}
4739
Trond Myklebust56659e92007-07-17 21:52:39 -04004740int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004741 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004742{
4743 struct nfs_server *server = NFS_SERVER(dir);
4744 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004745 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004746 };
4747 struct nfs4_fs_locations_arg args = {
4748 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004749 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004750 .page = page,
4751 .bitmask = bitmask,
4752 };
Benny Halevy22958462009-04-01 09:22:02 -04004753 struct nfs4_fs_locations_res res = {
4754 .fs_locations = fs_locations,
4755 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004756 struct rpc_message msg = {
4757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4758 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004759 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004760 };
4761 int status;
4762
Harvey Harrison3110ff82008-05-02 13:42:44 -07004763 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004764
4765 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4766 * is not supported */
4767 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4768 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4769 else
4770 bitmask[0] |= FATTR4_WORD0_FILEID;
4771
Trond Myklebustc228fd32007-01-13 02:28:11 -05004772 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004773 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004774 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004775 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004776 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004777 return status;
4778}
4779
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004780static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4781{
4782 int status;
4783 struct nfs4_secinfo_arg args = {
4784 .dir_fh = NFS_FH(dir),
4785 .name = name,
4786 };
4787 struct nfs4_secinfo_res res = {
4788 .flavors = flavors,
4789 };
4790 struct rpc_message msg = {
4791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4792 .rpc_argp = &args,
4793 .rpc_resp = &res,
4794 };
4795
4796 dprintk("NFS call secinfo %s\n", name->name);
4797 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4798 dprintk("NFS reply secinfo: %d\n", status);
4799 return status;
4800}
4801
4802int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4803{
4804 struct nfs4_exception exception = { };
4805 int err;
4806 do {
4807 err = nfs4_handle_exception(NFS_SERVER(dir),
4808 _nfs4_proc_secinfo(dir, name, flavors),
4809 &exception);
4810 } while (exception.retry);
4811 return err;
4812}
4813
Andy Adamson557134a2009-04-01 09:21:53 -04004814#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004815/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004816 * Check the exchange flags returned by the server for invalid flags, having
4817 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4818 * DS flags set.
4819 */
4820static int nfs4_check_cl_exchange_flags(u32 flags)
4821{
4822 if (flags & ~EXCHGID4_FLAG_MASK_R)
4823 goto out_inval;
4824 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4825 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4826 goto out_inval;
4827 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4828 goto out_inval;
4829 return NFS_OK;
4830out_inval:
4831 return -NFS4ERR_INVAL;
4832}
4833
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004834static bool
4835nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4836{
4837 if (a->server_scope_sz == b->server_scope_sz &&
4838 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4839 return true;
4840
4841 return false;
4842}
4843
Andy Adamson357f54d2010-12-14 10:11:57 -05004844/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004845 * nfs4_proc_exchange_id()
4846 *
4847 * Since the clientid has expired, all compounds using sessions
4848 * associated with the stale clientid will be returning
4849 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4850 * be in some phase of session reset.
4851 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004852int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004853{
4854 nfs4_verifier verifier;
4855 struct nfs41_exchange_id_args args = {
4856 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004857 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004858 };
4859 struct nfs41_exchange_id_res res = {
4860 .client = clp,
4861 };
4862 int status;
4863 struct rpc_message msg = {
4864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4865 .rpc_argp = &args,
4866 .rpc_resp = &res,
4867 .rpc_cred = cred,
4868 };
4869 __be32 *p;
4870
4871 dprintk("--> %s\n", __func__);
4872 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004873
Benny Halevy99fe60d2009-04-01 09:22:29 -04004874 p = (u32 *)verifier.data;
4875 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4876 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4877 args.verifier = &verifier;
4878
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004879 args.id_len = scnprintf(args.id, sizeof(args.id),
4880 "%s/%s.%s/%u",
4881 clp->cl_ipaddr,
4882 init_utsname()->nodename,
4883 init_utsname()->domainname,
4884 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004885
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004886 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4887 if (unlikely(!res.server_scope))
4888 return -ENOMEM;
4889
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004890 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004891 if (!status)
4892 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004893
4894 if (!status) {
4895 if (clp->server_scope &&
4896 !nfs41_same_server_scope(clp->server_scope,
4897 res.server_scope)) {
4898 dprintk("%s: server_scope mismatch detected\n",
4899 __func__);
4900 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4901 kfree(clp->server_scope);
4902 clp->server_scope = NULL;
4903 }
4904
4905 if (!clp->server_scope)
4906 clp->server_scope = res.server_scope;
4907 else
4908 kfree(res.server_scope);
4909 }
4910
Benny Halevy99fe60d2009-04-01 09:22:29 -04004911 dprintk("<-- %s status= %d\n", __func__, status);
4912 return status;
4913}
4914
Andy Adamson2050f0c2009-04-01 09:22:30 -04004915struct nfs4_get_lease_time_data {
4916 struct nfs4_get_lease_time_args *args;
4917 struct nfs4_get_lease_time_res *res;
4918 struct nfs_client *clp;
4919};
4920
4921static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4922 void *calldata)
4923{
4924 int ret;
4925 struct nfs4_get_lease_time_data *data =
4926 (struct nfs4_get_lease_time_data *)calldata;
4927
4928 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004929 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004930 /* just setup sequence, do not trigger session recovery
4931 since we're invoked within one */
4932 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004933 &data->args->la_seq_args,
4934 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004935
4936 BUG_ON(ret == -EAGAIN);
4937 rpc_call_start(task);
4938 dprintk("<-- %s\n", __func__);
4939}
4940
4941/*
4942 * Called from nfs4_state_manager thread for session setup, so don't recover
4943 * from sequence operation or clientid errors.
4944 */
4945static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4946{
4947 struct nfs4_get_lease_time_data *data =
4948 (struct nfs4_get_lease_time_data *)calldata;
4949
4950 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004951 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4952 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004953 switch (task->tk_status) {
4954 case -NFS4ERR_DELAY:
4955 case -NFS4ERR_GRACE:
4956 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4957 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4958 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004959 /* fall through */
4960 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004961 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004962 return;
4963 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004964 dprintk("<-- %s\n", __func__);
4965}
4966
4967struct rpc_call_ops nfs4_get_lease_time_ops = {
4968 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4969 .rpc_call_done = nfs4_get_lease_time_done,
4970};
4971
4972int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4973{
4974 struct rpc_task *task;
4975 struct nfs4_get_lease_time_args args;
4976 struct nfs4_get_lease_time_res res = {
4977 .lr_fsinfo = fsinfo,
4978 };
4979 struct nfs4_get_lease_time_data data = {
4980 .args = &args,
4981 .res = &res,
4982 .clp = clp,
4983 };
4984 struct rpc_message msg = {
4985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4986 .rpc_argp = &args,
4987 .rpc_resp = &res,
4988 };
4989 struct rpc_task_setup task_setup = {
4990 .rpc_client = clp->cl_rpcclient,
4991 .rpc_message = &msg,
4992 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004993 .callback_data = &data,
4994 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004995 };
4996 int status;
4997
Andy Adamson2050f0c2009-04-01 09:22:30 -04004998 dprintk("--> %s\n", __func__);
4999 task = rpc_run_task(&task_setup);
5000
5001 if (IS_ERR(task))
5002 status = PTR_ERR(task);
5003 else {
5004 status = task->tk_status;
5005 rpc_put_task(task);
5006 }
5007 dprintk("<-- %s return %d\n", __func__, status);
5008
5009 return status;
5010}
5011
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005012/*
5013 * Reset a slot table
5014 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005015static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5016 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005017{
Andy Adamson104aeba2010-01-14 17:45:10 -05005018 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005019 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005020 int ret = 0;
5021
Andy Adamson104aeba2010-01-14 17:45:10 -05005022 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5023 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005024
Andy Adamson104aeba2010-01-14 17:45:10 -05005025 /* Does the newly negotiated max_reqs match the existing slot table? */
5026 if (max_reqs != tbl->max_slots) {
5027 ret = -ENOMEM;
5028 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005029 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005030 if (!new)
5031 goto out;
5032 ret = 0;
5033 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005034 }
5035 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005036 if (new) {
5037 tbl->slots = new;
5038 tbl->max_slots = max_reqs;
5039 }
5040 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005041 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005042 spin_unlock(&tbl->slot_tbl_lock);
5043 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5044 tbl, tbl->slots, tbl->max_slots);
5045out:
5046 dprintk("<-- %s: return %d\n", __func__, ret);
5047 return ret;
5048}
5049
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005050/* Destroy the slot table */
5051static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5052{
5053 if (session->fc_slot_table.slots != NULL) {
5054 kfree(session->fc_slot_table.slots);
5055 session->fc_slot_table.slots = NULL;
5056 }
5057 if (session->bc_slot_table.slots != NULL) {
5058 kfree(session->bc_slot_table.slots);
5059 session->bc_slot_table.slots = NULL;
5060 }
5061 return;
5062}
5063
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005064/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005065 * Initialize slot table
5066 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005067static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5068 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005069{
Andy Adamsonfc931582009-04-01 09:22:31 -04005070 struct nfs4_slot *slot;
5071 int ret = -ENOMEM;
5072
5073 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5074
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005075 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005076
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005077 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005078 if (!slot)
5079 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005080 ret = 0;
5081
5082 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005083 tbl->max_slots = max_slots;
5084 tbl->slots = slot;
5085 tbl->highest_used_slotid = -1; /* no slot is currently used */
5086 spin_unlock(&tbl->slot_tbl_lock);
5087 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5088 tbl, tbl->slots, tbl->max_slots);
5089out:
5090 dprintk("<-- %s: return %d\n", __func__, ret);
5091 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005092}
5093
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005094/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005095 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005096 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005097static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005098{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005099 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005100 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005101
Andy Adamsonaacd5532011-11-09 13:58:21 -05005102 dprintk("--> %s\n", __func__);
5103 /* Fore channel */
5104 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005105 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005106 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5107 if (status) /* -ENOMEM */
5108 return status;
5109 } else {
5110 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005111 if (status)
5112 return status;
5113 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005114 /* Back channel */
5115 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005116 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005117 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005118 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005119 /* Fore and back channel share a connection so get
5120 * both slot tables or neither */
5121 nfs4_destroy_slot_tables(ses);
5122 } else
5123 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005124 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005125}
5126
5127struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5128{
5129 struct nfs4_session *session;
5130 struct nfs4_slot_table *tbl;
5131
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005132 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005133 if (!session)
5134 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005135
Andy Adamson557134a2009-04-01 09:21:53 -04005136 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005137 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005138 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005139 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005140 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005141
5142 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005143 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005144 spin_lock_init(&tbl->slot_tbl_lock);
5145 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005146 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005147
Trond Myklebust1055d762010-06-16 09:52:27 -04005148 session->session_state = 1<<NFS4_SESSION_INITING;
5149
Andy Adamson557134a2009-04-01 09:21:53 -04005150 session->clp = clp;
5151 return session;
5152}
5153
5154void nfs4_destroy_session(struct nfs4_session *session)
5155{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005156 nfs4_proc_destroy_session(session);
5157 dprintk("%s Destroy backchannel for xprt %p\n",
5158 __func__, session->clp->cl_rpcclient->cl_xprt);
5159 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5160 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005161 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005162 kfree(session);
5163}
5164
Andy Adamsonfc931582009-04-01 09:22:31 -04005165/*
5166 * Initialize the values to be used by the client in CREATE_SESSION
5167 * If nfs4_init_session set the fore channel request and response sizes,
5168 * use them.
5169 *
5170 * Set the back channel max_resp_sz_cached to zero to force the client to
5171 * always set csa_cachethis to FALSE because the current implementation
5172 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5173 */
5174static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5175{
5176 struct nfs4_session *session = args->client->cl_session;
5177 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5178 mxresp_sz = session->fc_attrs.max_resp_sz;
5179
5180 if (mxrqst_sz == 0)
5181 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5182 if (mxresp_sz == 0)
5183 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5184 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005185 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5186 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005187 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5188 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5189
5190 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005191 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005192 __func__,
5193 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005194 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005195
5196 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005197 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5198 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5199 args->bc_attrs.max_resp_sz_cached = 0;
5200 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5201 args->bc_attrs.max_reqs = 1;
5202
5203 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5204 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5205 __func__,
5206 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5207 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5208 args->bc_attrs.max_reqs);
5209}
5210
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005211static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005212{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005213 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5214 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5215
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005216 if (rcvd->max_resp_sz > sent->max_resp_sz)
5217 return -EINVAL;
5218 /*
5219 * Our requested max_ops is the minimum we need; we're not
5220 * prepared to break up compounds into smaller pieces than that.
5221 * So, no point even trying to continue if the server won't
5222 * cooperate:
5223 */
5224 if (rcvd->max_ops < sent->max_ops)
5225 return -EINVAL;
5226 if (rcvd->max_reqs == 0)
5227 return -EINVAL;
5228 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005229}
5230
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005231static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5232{
5233 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5234 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005235
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005236 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5237 return -EINVAL;
5238 if (rcvd->max_resp_sz < sent->max_resp_sz)
5239 return -EINVAL;
5240 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5241 return -EINVAL;
5242 /* These would render the backchannel useless: */
5243 if (rcvd->max_ops == 0)
5244 return -EINVAL;
5245 if (rcvd->max_reqs == 0)
5246 return -EINVAL;
5247 return 0;
5248}
Andy Adamson8d353012009-04-01 09:22:32 -04005249
Andy Adamson8d353012009-04-01 09:22:32 -04005250static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5251 struct nfs4_session *session)
5252{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005253 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005254
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005255 ret = nfs4_verify_fore_channel_attrs(args, session);
5256 if (ret)
5257 return ret;
5258 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005259}
5260
Andy Adamsonfc931582009-04-01 09:22:31 -04005261static int _nfs4_proc_create_session(struct nfs_client *clp)
5262{
5263 struct nfs4_session *session = clp->cl_session;
5264 struct nfs41_create_session_args args = {
5265 .client = clp,
5266 .cb_program = NFS4_CALLBACK,
5267 };
5268 struct nfs41_create_session_res res = {
5269 .client = clp,
5270 };
5271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5273 .rpc_argp = &args,
5274 .rpc_resp = &res,
5275 };
5276 int status;
5277
5278 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005279 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005280
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005281 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005282
Andy Adamson8d353012009-04-01 09:22:32 -04005283 if (!status)
5284 /* Verify the session's negotiated channel_attrs values */
5285 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005286 if (!status) {
5287 /* Increment the clientid slot sequence id */
5288 clp->cl_seqid++;
5289 }
5290
5291 return status;
5292}
5293
5294/*
5295 * Issues a CREATE_SESSION operation to the server.
5296 * It is the responsibility of the caller to verify the session is
5297 * expired before calling this routine.
5298 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005299int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005300{
5301 int status;
5302 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005303 struct nfs4_session *session = clp->cl_session;
5304
5305 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5306
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005307 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005308 if (status)
5309 goto out;
5310
Andy Adamsonaacd5532011-11-09 13:58:21 -05005311 /* Init or reset the session slot tables */
5312 status = nfs4_setup_session_slot_tables(session);
5313 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005314 if (status)
5315 goto out;
5316
5317 ptr = (unsigned *)&session->sess_id.data[0];
5318 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5319 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005320out:
5321 dprintk("<-- %s\n", __func__);
5322 return status;
5323}
5324
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005325/*
5326 * Issue the over-the-wire RPC DESTROY_SESSION.
5327 * The caller must serialize access to this routine.
5328 */
5329int nfs4_proc_destroy_session(struct nfs4_session *session)
5330{
5331 int status = 0;
5332 struct rpc_message msg;
5333
5334 dprintk("--> nfs4_proc_destroy_session\n");
5335
5336 /* session is still being setup */
5337 if (session->clp->cl_cons_state != NFS_CS_READY)
5338 return status;
5339
5340 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5341 msg.rpc_argp = session;
5342 msg.rpc_resp = NULL;
5343 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005344 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005345
5346 if (status)
5347 printk(KERN_WARNING
5348 "Got error %d from the server on DESTROY_SESSION. "
5349 "Session has been destroyed regardless...\n", status);
5350
5351 dprintk("<-- nfs4_proc_destroy_session\n");
5352 return status;
5353}
5354
Trond Myklebustfccba802009-07-21 16:48:07 -04005355int nfs4_init_session(struct nfs_server *server)
5356{
5357 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005358 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005359 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005360 int ret;
5361
5362 if (!nfs4_has_session(clp))
5363 return 0;
5364
Trond Myklebust1055d762010-06-16 09:52:27 -04005365 session = clp->cl_session;
5366 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5367 return 0;
5368
Andy Adamson68bf05e2009-12-15 12:55:02 -05005369 rsize = server->rsize;
5370 if (rsize == 0)
5371 rsize = NFS_MAX_FILE_IO_SIZE;
5372 wsize = server->wsize;
5373 if (wsize == 0)
5374 wsize = NFS_MAX_FILE_IO_SIZE;
5375
Andy Adamson68bf05e2009-12-15 12:55:02 -05005376 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5377 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005378
Trond Myklebustfccba802009-07-21 16:48:07 -04005379 ret = nfs4_recover_expired_lease(server);
5380 if (!ret)
5381 ret = nfs4_check_client_ready(clp);
5382 return ret;
5383}
5384
Andy Adamsond83217c2011-03-01 01:34:17 +00005385int nfs4_init_ds_session(struct nfs_client *clp)
5386{
5387 struct nfs4_session *session = clp->cl_session;
5388 int ret;
5389
5390 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5391 return 0;
5392
5393 ret = nfs4_client_recover_expired_lease(clp);
5394 if (!ret)
5395 /* Test for the DS role */
5396 if (!is_ds_client(clp))
5397 ret = -ENODEV;
5398 if (!ret)
5399 ret = nfs4_check_client_ready(clp);
5400 return ret;
5401
5402}
5403EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5404
5405
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005406/*
5407 * Renew the cl_session lease.
5408 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005409struct nfs4_sequence_data {
5410 struct nfs_client *clp;
5411 struct nfs4_sequence_args args;
5412 struct nfs4_sequence_res res;
5413};
5414
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005415static void nfs41_sequence_release(void *data)
5416{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005417 struct nfs4_sequence_data *calldata = data;
5418 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005419
Alexandros Batsakis71358402010-02-05 03:45:05 -08005420 if (atomic_read(&clp->cl_count) > 1)
5421 nfs4_schedule_state_renewal(clp);
5422 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005423 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005424}
5425
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005426static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5427{
5428 switch(task->tk_status) {
5429 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005430 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5431 return -EAGAIN;
5432 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005433 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005434 }
5435 return 0;
5436}
5437
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005438static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005439{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005440 struct nfs4_sequence_data *calldata = data;
5441 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005442
Trond Myklebust14516c32010-07-31 14:29:06 -04005443 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5444 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005445
5446 if (task->tk_status < 0) {
5447 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005448 if (atomic_read(&clp->cl_count) == 1)
5449 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005450
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005451 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5452 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453 return;
5454 }
5455 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005456 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005457out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005458 dprintk("<-- %s\n", __func__);
5459}
5460
5461static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5462{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005463 struct nfs4_sequence_data *calldata = data;
5464 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005465 struct nfs4_sequence_args *args;
5466 struct nfs4_sequence_res *res;
5467
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005468 args = task->tk_msg.rpc_argp;
5469 res = task->tk_msg.rpc_resp;
5470
Trond Myklebust035168a2010-06-16 09:52:26 -04005471 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005472 return;
5473 rpc_call_start(task);
5474}
5475
5476static const struct rpc_call_ops nfs41_sequence_ops = {
5477 .rpc_call_done = nfs41_sequence_call_done,
5478 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005479 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005480};
5481
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005482static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005483{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005484 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005485 struct rpc_message msg = {
5486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5487 .rpc_cred = cred,
5488 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005489 struct rpc_task_setup task_setup_data = {
5490 .rpc_client = clp->cl_rpcclient,
5491 .rpc_message = &msg,
5492 .callback_ops = &nfs41_sequence_ops,
5493 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5494 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005495
Alexandros Batsakis71358402010-02-05 03:45:05 -08005496 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005497 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005498 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005499 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005500 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005501 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005502 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005503 msg.rpc_argp = &calldata->args;
5504 msg.rpc_resp = &calldata->res;
5505 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005506 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005507
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005508 return rpc_run_task(&task_setup_data);
5509}
5510
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005511static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005512{
5513 struct rpc_task *task;
5514 int ret = 0;
5515
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005516 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5517 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005518 task = _nfs41_proc_sequence(clp, cred);
5519 if (IS_ERR(task))
5520 ret = PTR_ERR(task);
5521 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005522 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005523 dprintk("<-- %s status=%d\n", __func__, ret);
5524 return ret;
5525}
5526
5527static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5528{
5529 struct rpc_task *task;
5530 int ret;
5531
5532 task = _nfs41_proc_sequence(clp, cred);
5533 if (IS_ERR(task)) {
5534 ret = PTR_ERR(task);
5535 goto out;
5536 }
5537 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005538 if (!ret) {
5539 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5540
5541 if (task->tk_status == 0)
5542 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005543 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005544 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005545 rpc_put_task(task);
5546out:
5547 dprintk("<-- %s status=%d\n", __func__, ret);
5548 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005549}
5550
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005551struct nfs4_reclaim_complete_data {
5552 struct nfs_client *clp;
5553 struct nfs41_reclaim_complete_args arg;
5554 struct nfs41_reclaim_complete_res res;
5555};
5556
5557static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5558{
5559 struct nfs4_reclaim_complete_data *calldata = data;
5560
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005561 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005562 if (nfs41_setup_sequence(calldata->clp->cl_session,
5563 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005564 &calldata->res.seq_res, 0, task))
5565 return;
5566
5567 rpc_call_start(task);
5568}
5569
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005570static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5571{
5572 switch(task->tk_status) {
5573 case 0:
5574 case -NFS4ERR_COMPLETE_ALREADY:
5575 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5576 break;
5577 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005578 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005579 /* fall through */
5580 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005581 return -EAGAIN;
5582 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005583 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005584 }
5585 return 0;
5586}
5587
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005588static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5589{
5590 struct nfs4_reclaim_complete_data *calldata = data;
5591 struct nfs_client *clp = calldata->clp;
5592 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5593
5594 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005595 if (!nfs41_sequence_done(task, res))
5596 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005597
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005598 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5599 rpc_restart_call_prepare(task);
5600 return;
5601 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005602 dprintk("<-- %s\n", __func__);
5603}
5604
5605static void nfs4_free_reclaim_complete_data(void *data)
5606{
5607 struct nfs4_reclaim_complete_data *calldata = data;
5608
5609 kfree(calldata);
5610}
5611
5612static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5613 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5614 .rpc_call_done = nfs4_reclaim_complete_done,
5615 .rpc_release = nfs4_free_reclaim_complete_data,
5616};
5617
5618/*
5619 * Issue a global reclaim complete.
5620 */
5621static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5622{
5623 struct nfs4_reclaim_complete_data *calldata;
5624 struct rpc_task *task;
5625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5627 };
5628 struct rpc_task_setup task_setup_data = {
5629 .rpc_client = clp->cl_rpcclient,
5630 .rpc_message = &msg,
5631 .callback_ops = &nfs4_reclaim_complete_call_ops,
5632 .flags = RPC_TASK_ASYNC,
5633 };
5634 int status = -ENOMEM;
5635
5636 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005637 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005638 if (calldata == NULL)
5639 goto out;
5640 calldata->clp = clp;
5641 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005642
5643 msg.rpc_argp = &calldata->arg;
5644 msg.rpc_resp = &calldata->res;
5645 task_setup_data.callback_data = calldata;
5646 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005647 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005648 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005649 goto out;
5650 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005651 status = nfs4_wait_for_completion_rpc_task(task);
5652 if (status == 0)
5653 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005654 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005655 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005656out:
5657 dprintk("<-- %s status=%d\n", __func__, status);
5658 return status;
5659}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005660
5661static void
5662nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5663{
5664 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005665 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005666
5667 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005668 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5669 * right now covering the LAYOUTGET we are about to send.
5670 * However, that is not so catastrophic, and there seems
5671 * to be no way to prevent it completely.
5672 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005673 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5674 &lgp->res.seq_res, 0, task))
5675 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005676 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5677 NFS_I(lgp->args.inode)->layout,
5678 lgp->args.ctx->state)) {
5679 rpc_exit(task, NFS4_OK);
5680 return;
5681 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005682 rpc_call_start(task);
5683}
5684
5685static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5686{
5687 struct nfs4_layoutget *lgp = calldata;
5688 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5689
5690 dprintk("--> %s\n", __func__);
5691
5692 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5693 return;
5694
5695 switch (task->tk_status) {
5696 case 0:
5697 break;
5698 case -NFS4ERR_LAYOUTTRYLATER:
5699 case -NFS4ERR_RECALLCONFLICT:
5700 task->tk_status = -NFS4ERR_DELAY;
5701 /* Fall through */
5702 default:
5703 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5704 rpc_restart_call_prepare(task);
5705 return;
5706 }
5707 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005708 dprintk("<-- %s\n", __func__);
5709}
5710
5711static void nfs4_layoutget_release(void *calldata)
5712{
5713 struct nfs4_layoutget *lgp = calldata;
5714
5715 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005716 put_nfs_open_context(lgp->args.ctx);
5717 kfree(calldata);
5718 dprintk("<-- %s\n", __func__);
5719}
5720
5721static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5722 .rpc_call_prepare = nfs4_layoutget_prepare,
5723 .rpc_call_done = nfs4_layoutget_done,
5724 .rpc_release = nfs4_layoutget_release,
5725};
5726
5727int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5728{
5729 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5730 struct rpc_task *task;
5731 struct rpc_message msg = {
5732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5733 .rpc_argp = &lgp->args,
5734 .rpc_resp = &lgp->res,
5735 };
5736 struct rpc_task_setup task_setup_data = {
5737 .rpc_client = server->client,
5738 .rpc_message = &msg,
5739 .callback_ops = &nfs4_layoutget_call_ops,
5740 .callback_data = lgp,
5741 .flags = RPC_TASK_ASYNC,
5742 };
5743 int status = 0;
5744
5745 dprintk("--> %s\n", __func__);
5746
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005747 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005748 lgp->res.seq_res.sr_slot = NULL;
5749 task = rpc_run_task(&task_setup_data);
5750 if (IS_ERR(task))
5751 return PTR_ERR(task);
5752 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005753 if (status == 0)
5754 status = task->tk_status;
5755 if (status == 0)
5756 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005757 rpc_put_task(task);
5758 dprintk("<-- %s status=%d\n", __func__, status);
5759 return status;
5760}
5761
Benny Halevycbe82602011-05-22 19:52:37 +03005762static void
5763nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5764{
5765 struct nfs4_layoutreturn *lrp = calldata;
5766
5767 dprintk("--> %s\n", __func__);
5768 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5769 &lrp->res.seq_res, 0, task))
5770 return;
5771 rpc_call_start(task);
5772}
5773
5774static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5775{
5776 struct nfs4_layoutreturn *lrp = calldata;
5777 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005778 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005779
5780 dprintk("--> %s\n", __func__);
5781
5782 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5783 return;
5784
5785 server = NFS_SERVER(lrp->args.inode);
5786 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005787 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005788 return;
5789 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005790 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005791 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005792 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005793 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005794 } else
5795 BUG_ON(!list_empty(&lo->plh_segs));
5796 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005797 lo->plh_block_lgets--;
5798 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005799 dprintk("<-- %s\n", __func__);
5800}
5801
5802static void nfs4_layoutreturn_release(void *calldata)
5803{
5804 struct nfs4_layoutreturn *lrp = calldata;
5805
5806 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005807 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005808 kfree(calldata);
5809 dprintk("<-- %s\n", __func__);
5810}
5811
5812static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5813 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5814 .rpc_call_done = nfs4_layoutreturn_done,
5815 .rpc_release = nfs4_layoutreturn_release,
5816};
5817
5818int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5819{
5820 struct rpc_task *task;
5821 struct rpc_message msg = {
5822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5823 .rpc_argp = &lrp->args,
5824 .rpc_resp = &lrp->res,
5825 };
5826 struct rpc_task_setup task_setup_data = {
5827 .rpc_client = lrp->clp->cl_rpcclient,
5828 .rpc_message = &msg,
5829 .callback_ops = &nfs4_layoutreturn_call_ops,
5830 .callback_data = lrp,
5831 };
5832 int status;
5833
5834 dprintk("--> %s\n", __func__);
5835 task = rpc_run_task(&task_setup_data);
5836 if (IS_ERR(task))
5837 return PTR_ERR(task);
5838 status = task->tk_status;
5839 dprintk("<-- %s status=%d\n", __func__, status);
5840 rpc_put_task(task);
5841 return status;
5842}
5843
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005844/*
5845 * Retrieve the list of Data Server devices from the MDS.
5846 */
5847static int _nfs4_getdevicelist(struct nfs_server *server,
5848 const struct nfs_fh *fh,
5849 struct pnfs_devicelist *devlist)
5850{
5851 struct nfs4_getdevicelist_args args = {
5852 .fh = fh,
5853 .layoutclass = server->pnfs_curr_ld->id,
5854 };
5855 struct nfs4_getdevicelist_res res = {
5856 .devlist = devlist,
5857 };
5858 struct rpc_message msg = {
5859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5860 .rpc_argp = &args,
5861 .rpc_resp = &res,
5862 };
5863 int status;
5864
5865 dprintk("--> %s\n", __func__);
5866 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5867 &res.seq_res, 0);
5868 dprintk("<-- %s status=%d\n", __func__, status);
5869 return status;
5870}
5871
5872int nfs4_proc_getdevicelist(struct nfs_server *server,
5873 const struct nfs_fh *fh,
5874 struct pnfs_devicelist *devlist)
5875{
5876 struct nfs4_exception exception = { };
5877 int err;
5878
5879 do {
5880 err = nfs4_handle_exception(server,
5881 _nfs4_getdevicelist(server, fh, devlist),
5882 &exception);
5883 } while (exception.retry);
5884
5885 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5886 err, devlist->num_devs);
5887
5888 return err;
5889}
5890EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5891
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005892static int
5893_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5894{
5895 struct nfs4_getdeviceinfo_args args = {
5896 .pdev = pdev,
5897 };
5898 struct nfs4_getdeviceinfo_res res = {
5899 .pdev = pdev,
5900 };
5901 struct rpc_message msg = {
5902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5903 .rpc_argp = &args,
5904 .rpc_resp = &res,
5905 };
5906 int status;
5907
5908 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005909 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005910 dprintk("<-- %s status=%d\n", __func__, status);
5911
5912 return status;
5913}
5914
5915int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5916{
5917 struct nfs4_exception exception = { };
5918 int err;
5919
5920 do {
5921 err = nfs4_handle_exception(server,
5922 _nfs4_proc_getdeviceinfo(server, pdev),
5923 &exception);
5924 } while (exception.retry);
5925 return err;
5926}
5927EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5928
Andy Adamson863a3c62011-03-23 13:27:54 +00005929static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5930{
5931 struct nfs4_layoutcommit_data *data = calldata;
5932 struct nfs_server *server = NFS_SERVER(data->args.inode);
5933
5934 if (nfs4_setup_sequence(server, &data->args.seq_args,
5935 &data->res.seq_res, 1, task))
5936 return;
5937 rpc_call_start(task);
5938}
5939
5940static void
5941nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5942{
5943 struct nfs4_layoutcommit_data *data = calldata;
5944 struct nfs_server *server = NFS_SERVER(data->args.inode);
5945
5946 if (!nfs4_sequence_done(task, &data->res.seq_res))
5947 return;
5948
5949 switch (task->tk_status) { /* Just ignore these failures */
5950 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5951 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5952 case NFS4ERR_BADLAYOUT: /* no layout */
5953 case NFS4ERR_GRACE: /* loca_recalim always false */
5954 task->tk_status = 0;
5955 }
5956
5957 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005958 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005959 return;
5960 }
5961
5962 if (task->tk_status == 0)
5963 nfs_post_op_update_inode_force_wcc(data->args.inode,
5964 data->res.fattr);
5965}
5966
5967static void nfs4_layoutcommit_release(void *calldata)
5968{
5969 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005970 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005971 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005972
Andy Adamsondb29c082011-07-30 20:52:38 -04005973 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005974 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005975 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5976 list_del_init(&lseg->pls_lc_list);
5977 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5978 &lseg->pls_flags))
5979 put_lseg(lseg);
5980 }
Peng Tao92407e72011-10-23 20:21:17 -07005981
5982 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5983 smp_mb__after_clear_bit();
5984 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5985
Andy Adamson863a3c62011-03-23 13:27:54 +00005986 put_rpccred(data->cred);
5987 kfree(data);
5988}
5989
5990static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5991 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5992 .rpc_call_done = nfs4_layoutcommit_done,
5993 .rpc_release = nfs4_layoutcommit_release,
5994};
5995
5996int
Andy Adamsonef311532011-03-12 02:58:10 -05005997nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005998{
5999 struct rpc_message msg = {
6000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6001 .rpc_argp = &data->args,
6002 .rpc_resp = &data->res,
6003 .rpc_cred = data->cred,
6004 };
6005 struct rpc_task_setup task_setup_data = {
6006 .task = &data->task,
6007 .rpc_client = NFS_CLIENT(data->args.inode),
6008 .rpc_message = &msg,
6009 .callback_ops = &nfs4_layoutcommit_ops,
6010 .callback_data = data,
6011 .flags = RPC_TASK_ASYNC,
6012 };
6013 struct rpc_task *task;
6014 int status = 0;
6015
6016 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6017 "lbw: %llu inode %lu\n",
6018 data->task.tk_pid, sync,
6019 data->args.lastbytewritten,
6020 data->args.inode->i_ino);
6021
6022 task = rpc_run_task(&task_setup_data);
6023 if (IS_ERR(task))
6024 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006025 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006026 goto out;
6027 status = nfs4_wait_for_completion_rpc_task(task);
6028 if (status != 0)
6029 goto out;
6030 status = task->tk_status;
6031out:
6032 dprintk("%s: status %d\n", __func__, status);
6033 rpc_put_task(task);
6034 return status;
6035}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006036
6037static int
6038_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6039 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6040{
6041 struct nfs41_secinfo_no_name_args args = {
6042 .style = SECINFO_STYLE_CURRENT_FH,
6043 };
6044 struct nfs4_secinfo_res res = {
6045 .flavors = flavors,
6046 };
6047 struct rpc_message msg = {
6048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6049 .rpc_argp = &args,
6050 .rpc_resp = &res,
6051 };
6052 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6053}
6054
6055static int
6056nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6057 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6058{
6059 struct nfs4_exception exception = { };
6060 int err;
6061 do {
6062 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6063 switch (err) {
6064 case 0:
6065 case -NFS4ERR_WRONGSEC:
6066 case -NFS4ERR_NOTSUPP:
6067 break;
6068 default:
6069 err = nfs4_handle_exception(server, err, &exception);
6070 }
6071 } while (exception.retry);
6072 return err;
6073}
6074
6075static int
6076nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6077 struct nfs_fsinfo *info)
6078{
6079 int err;
6080 struct page *page;
6081 rpc_authflavor_t flavor;
6082 struct nfs4_secinfo_flavors *flavors;
6083
6084 page = alloc_page(GFP_KERNEL);
6085 if (!page) {
6086 err = -ENOMEM;
6087 goto out;
6088 }
6089
6090 flavors = page_address(page);
6091 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6092
6093 /*
6094 * Fall back on "guess and check" method if
6095 * the server doesn't support SECINFO_NO_NAME
6096 */
6097 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6098 err = nfs4_find_root_sec(server, fhandle, info);
6099 goto out_freepage;
6100 }
6101 if (err)
6102 goto out_freepage;
6103
6104 flavor = nfs_find_best_sec(flavors);
6105 if (err == 0)
6106 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6107
6108out_freepage:
6109 put_page(page);
6110 if (err == -EACCES)
6111 return -EPERM;
6112out:
6113 return err;
6114}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006115static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6116{
6117 int status;
6118 struct nfs41_test_stateid_args args = {
6119 .stateid = &state->stateid,
6120 };
6121 struct nfs41_test_stateid_res res;
6122 struct rpc_message msg = {
6123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6124 .rpc_argp = &args,
6125 .rpc_resp = &res,
6126 };
6127 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6128 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6129 return status;
6130}
6131
6132static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6133{
6134 struct nfs4_exception exception = { };
6135 int err;
6136 do {
6137 err = nfs4_handle_exception(server,
6138 _nfs41_test_stateid(server, state),
6139 &exception);
6140 } while (exception.retry);
6141 return err;
6142}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006143
6144static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6145{
6146 int status;
6147 struct nfs41_free_stateid_args args = {
6148 .stateid = &state->stateid,
6149 };
6150 struct nfs41_free_stateid_res res;
6151 struct rpc_message msg = {
6152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6153 .rpc_argp = &args,
6154 .rpc_resp = &res,
6155 };
6156
6157 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6158 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6159 return status;
6160}
6161
6162static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6163{
6164 struct nfs4_exception exception = { };
6165 int err;
6166 do {
6167 err = nfs4_handle_exception(server,
6168 _nfs4_free_stateid(server, state),
6169 &exception);
6170 } while (exception.retry);
6171 return err;
6172}
Andy Adamson557134a2009-04-01 09:21:53 -04006173#endif /* CONFIG_NFS_V4_1 */
6174
Andy Adamson591d71c2009-04-01 09:22:47 -04006175struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006176 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006177 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 .recover_open = nfs4_open_reclaim,
6179 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006180 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006181 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182};
6183
Andy Adamson591d71c2009-04-01 09:22:47 -04006184#if defined(CONFIG_NFS_V4_1)
6185struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6186 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6187 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6188 .recover_open = nfs4_open_reclaim,
6189 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006190 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006191 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006192 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006193};
6194#endif /* CONFIG_NFS_V4_1 */
6195
6196struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006197 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006198 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 .recover_open = nfs4_open_expired,
6200 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006201 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006202 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203};
6204
Andy Adamson591d71c2009-04-01 09:22:47 -04006205#if defined(CONFIG_NFS_V4_1)
6206struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6207 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6208 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006209 .recover_open = nfs41_open_expired,
6210 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006211 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006212 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006213};
6214#endif /* CONFIG_NFS_V4_1 */
6215
Benny Halevy29fba382009-04-01 09:22:44 -04006216struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6217 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006218 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006219 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006220};
6221
6222#if defined(CONFIG_NFS_V4_1)
6223struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6224 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006225 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006226 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006227};
6228#endif
6229
Trond Myklebust97dc1352010-06-16 09:52:26 -04006230static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6231 .minor_version = 0,
6232 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006233 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006234 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006235 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6236 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6237 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006238};
6239
6240#if defined(CONFIG_NFS_V4_1)
6241static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6242 .minor_version = 1,
6243 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006244 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006245 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006246 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6247 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6248 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006249};
6250#endif
6251
Trond Myklebust97dc1352010-06-16 09:52:26 -04006252const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6253 [0] = &nfs_v4_0_minor_ops,
6254#if defined(CONFIG_NFS_V4_1)
6255 [1] = &nfs_v4_1_minor_ops,
6256#endif
6257};
6258
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006259static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006260 .permission = nfs_permission,
6261 .getattr = nfs_getattr,
6262 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006263 .getxattr = generic_getxattr,
6264 .setxattr = generic_setxattr,
6265 .listxattr = generic_listxattr,
6266 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006267};
6268
David Howells509de812006-08-22 20:06:11 -04006269const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 .version = 4, /* protocol version */
6271 .dentry_ops = &nfs4_dentry_operations,
6272 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006273 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006274 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 .getroot = nfs4_proc_get_root,
6276 .getattr = nfs4_proc_getattr,
6277 .setattr = nfs4_proc_setattr,
6278 .lookup = nfs4_proc_lookup,
6279 .access = nfs4_proc_access,
6280 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 .create = nfs4_proc_create,
6282 .remove = nfs4_proc_remove,
6283 .unlink_setup = nfs4_proc_unlink_setup,
6284 .unlink_done = nfs4_proc_unlink_done,
6285 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006286 .rename_setup = nfs4_proc_rename_setup,
6287 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 .link = nfs4_proc_link,
6289 .symlink = nfs4_proc_symlink,
6290 .mkdir = nfs4_proc_mkdir,
6291 .rmdir = nfs4_proc_remove,
6292 .readdir = nfs4_proc_readdir,
6293 .mknod = nfs4_proc_mknod,
6294 .statfs = nfs4_proc_statfs,
6295 .fsinfo = nfs4_proc_fsinfo,
6296 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006297 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 .decode_dirent = nfs4_decode_dirent,
6299 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006300 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006302 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006304 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006306 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006307 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006308 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006309 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006310 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311};
6312
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006313static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6314 .prefix = XATTR_NAME_NFSV4_ACL,
6315 .list = nfs4_xattr_list_nfs4_acl,
6316 .get = nfs4_xattr_get_nfs4_acl,
6317 .set = nfs4_xattr_set_nfs4_acl,
6318};
6319
6320const struct xattr_handler *nfs4_xattr_handlers[] = {
6321 &nfs4_xattr_nfs4_acl_handler,
6322 NULL
6323};
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325/*
6326 * Local variables:
6327 * c-basic-offset: 8
6328 * End:
6329 */