blob: ec9f6ef6c5dd596889c8f48f7dd91d3ff8a734e5 [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;
1757 if (!(sp = nfs4_get_state_owner(server, cred))) {
1758 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1759 goto out_err;
1760 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001761 status = nfs4_recover_expired_lease(server);
1762 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001763 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001764 if (dentry->d_inode != NULL)
1765 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001766 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001767 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001768 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001769 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Al Viro82a2c1b2011-06-22 18:30:55 -04001771 if (dentry->d_inode != NULL)
1772 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001773
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001774 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001776 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001779 status = PTR_ERR(state);
1780 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001781 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001782 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001783 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001784
1785 if (opendata->o_arg.open_flags & O_EXCL) {
1786 nfs4_exclusive_attrset(opendata, sattr);
1787
1788 nfs_fattr_init(opendata->o_res.f_attr);
1789 status = nfs4_do_setattr(state->inode, cred,
1790 opendata->o_res.f_attr, sattr,
1791 state);
1792 if (status == 0)
1793 nfs_setattr_update_inode(state->inode, sattr);
1794 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1795 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001796 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 *res = state;
1799 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001800err_opendata_put:
1801 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001802err_put_state_owner:
1803 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *res = NULL;
1806 return status;
1807}
1808
1809
Al Viro82a2c1b2011-06-22 18:30:55 -04001810static 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 -07001811{
1812 struct nfs4_exception exception = { };
1813 struct nfs4_state *res;
1814 int status;
1815
1816 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001817 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 if (status == 0)
1819 break;
1820 /* NOTE: BAD_SEQID means the server and client disagree about the
1821 * book-keeping w.r.t. state-changing operations
1822 * (OPEN/CLOSE/LOCK/LOCKU...)
1823 * It is actually a sign of a bug on the client or on the server.
1824 *
1825 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001826 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * have unhashed the old state_owner for us, and that we can
1828 * therefore safely retry using a new one. We should still warn
1829 * the user though...
1830 */
1831 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001832 printk(KERN_WARNING "NFS: v4 server %s "
1833 " returned a bad sequence-id error!\n",
1834 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 exception.retry = 1;
1836 continue;
1837 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001838 /*
1839 * BAD_STATEID on OPEN means that the server cancelled our
1840 * state before it received the OPEN_CONFIRM.
1841 * Recover by retrying the request as per the discussion
1842 * on Page 181 of RFC3530.
1843 */
1844 if (status == -NFS4ERR_BAD_STATEID) {
1845 exception.retry = 1;
1846 continue;
1847 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848 if (status == -EAGAIN) {
1849 /* We must have found a delegation */
1850 exception.retry = 1;
1851 continue;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1854 status, &exception));
1855 } while (exception.retry);
1856 return res;
1857}
1858
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001859static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1860 struct nfs_fattr *fattr, struct iattr *sattr,
1861 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001863 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001865 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 .iap = sattr,
1867 .server = server,
1868 .bitmask = server->attr_bitmask,
1869 };
1870 struct nfs_setattrres res = {
1871 .fattr = fattr,
1872 .server = server,
1873 };
1874 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1876 .rpc_argp = &arg,
1877 .rpc_resp = &res,
1878 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001880 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001881 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Trond Myklebust0e574af2005-10-27 22:12:38 -04001883 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001885 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1886 /* Use that stateid */
1887 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001888 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001889 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1891
Bryan Schumaker7c513052011-03-24 17:12:24 +00001892 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001893 if (status == 0 && state != NULL)
1894 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001895 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001898static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1899 struct nfs_fattr *fattr, struct iattr *sattr,
1900 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001902 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct nfs4_exception exception = { };
1904 int err;
1905 do {
1906 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001907 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 &exception);
1909 } while (exception.retry);
1910 return err;
1911}
1912
1913struct nfs4_closedata {
1914 struct inode *inode;
1915 struct nfs4_state *state;
1916 struct nfs_closeargs arg;
1917 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001918 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001919 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001920 bool roc;
1921 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922};
1923
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001924static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001925{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001926 struct nfs4_closedata *calldata = data;
1927 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001928 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001929
Fred Isamanf7e89172011-01-06 11:36:32 +00001930 if (calldata->roc)
1931 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001932 nfs4_put_open_state(calldata->state);
1933 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001934 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001935 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001936 kfree(calldata);
1937}
1938
Trond Myklebust88069f72009-12-08 08:33:16 -05001939static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1940 fmode_t fmode)
1941{
1942 spin_lock(&state->owner->so_lock);
1943 if (!(fmode & FMODE_READ))
1944 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1945 if (!(fmode & FMODE_WRITE))
1946 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1947 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1948 spin_unlock(&state->owner->so_lock);
1949}
1950
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001951static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001953 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nfs_server *server = NFS_SERVER(calldata->inode);
1956
Trond Myklebust14516c32010-07-31 14:29:06 -04001957 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1958 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* hmm. we are done with the inode, and in the process of freeing
1960 * the state_owner. we keep this around to process errors
1961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 switch (task->tk_status) {
1963 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001964 if (calldata->roc)
1965 pnfs_roc_set_barrier(state->inode,
1966 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001967 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001968 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001969 nfs4_close_clear_stateid_flags(state,
1970 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001973 case -NFS4ERR_OLD_STATEID:
1974 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001976 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001979 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1980 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001982 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001983 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001986static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001988 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001989 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001990 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001991
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001992 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001993 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001994
Trond Myklebust88069f72009-12-08 08:33:16 -05001995 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1996 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001997 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001998 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001999 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002000 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002001 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2002 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2003 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002004 }
2005 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002006 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2007 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2008 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002009 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002010 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002011 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002012
2013 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002014 /* Note: exit _without_ calling nfs4_close_done */
2015 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002016 return;
2017 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002018
Fred Isamanf7e89172011-01-06 11:36:32 +00002019 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002020 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002021 if (calldata->roc &&
2022 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2023 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2024 task, NULL);
2025 return;
2026 }
2027 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002028
Trond Myklebust516a6af2005-10-27 22:12:41 -04002029 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002030 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002031 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002032 &calldata->arg.seq_args, &calldata->res.seq_res,
2033 1, task))
2034 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002035 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002038static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002039 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002040 .rpc_call_done = nfs4_close_done,
2041 .rpc_release = nfs4_free_closedata,
2042};
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044/*
2045 * It is possible for data to be read/written from a mem-mapped file
2046 * after the sys_close call (which hits the vfs layer as a flush).
2047 * This means that we can't safely call nfsv4 close on a file until
2048 * the inode is cleared. This in turn means that we are not good
2049 * NFSv4 citizens - we do not indicate to the server to update the file's
2050 * share state even when we are done with one of the three share
2051 * stateid's in the inode.
2052 *
2053 * NOTE: Caller must be holding the sp->so_owner semaphore!
2054 */
Al Viro643168c2011-06-22 18:20:23 -04002055int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002057 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002059 struct nfs4_state_owner *sp = state->owner;
2060 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002061 struct rpc_message msg = {
2062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2063 .rpc_cred = state->owner->so_cred,
2064 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002065 struct rpc_task_setup task_setup_data = {
2066 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002067 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002068 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002069 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002070 .flags = RPC_TASK_ASYNC,
2071 };
Trond Myklebust95121352005-10-18 14:20:12 -07002072 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002074 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002076 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002077 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002079 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002080 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002082 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002083 if (calldata->arg.seqid == NULL)
2084 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002085 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002086 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002087 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002088 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002089 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002090 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002091 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002092
Trond Myklebust1174dd12010-12-21 10:52:24 -05002093 msg.rpc_argp = &calldata->arg;
2094 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002095 task_setup_data.callback_data = calldata;
2096 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002097 if (IS_ERR(task))
2098 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002099 status = 0;
2100 if (wait)
2101 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002102 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002103 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002104out_free_calldata:
2105 kfree(calldata);
2106out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002107 if (roc)
2108 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002109 nfs4_put_open_state(state);
2110 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002111 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Trond Myklebust2b484292010-09-17 10:56:51 -04002114static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002115nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct nfs4_state *state;
2118
Trond Myklebust565277f2007-10-15 18:17:53 -04002119 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002120 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002121 if (IS_ERR(state))
2122 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002123 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002124 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Trond Myklebust1185a552009-12-03 15:54:02 -05002127static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002128{
2129 if (ctx->state == NULL)
2130 return;
2131 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002132 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002133 else
Al Viro643168c2011-06-22 18:20:23 -04002134 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2138{
Benny Halevy43652ad2009-04-01 09:21:54 -04002139 struct nfs4_server_caps_arg args = {
2140 .fhandle = fhandle,
2141 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 struct nfs4_server_caps_res res = {};
2143 struct rpc_message msg = {
2144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002145 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 .rpc_resp = &res,
2147 };
2148 int status;
2149
Bryan Schumaker7c513052011-03-24 17:12:24 +00002150 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (status == 0) {
2152 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002153 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2154 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2155 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2156 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2157 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2159 server->caps |= NFS_CAP_ACLS;
2160 if (res.has_links != 0)
2161 server->caps |= NFS_CAP_HARDLINKS;
2162 if (res.has_symlinks != 0)
2163 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002164 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2165 server->caps |= NFS_CAP_FILEID;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2167 server->caps |= NFS_CAP_MODE;
2168 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2169 server->caps |= NFS_CAP_NLINK;
2170 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2171 server->caps |= NFS_CAP_OWNER;
2172 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2173 server->caps |= NFS_CAP_OWNER_GROUP;
2174 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2175 server->caps |= NFS_CAP_ATIME;
2176 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2177 server->caps |= NFS_CAP_CTIME;
2178 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2179 server->caps |= NFS_CAP_MTIME;
2180
Trond Myklebusta65318b2009-03-11 14:10:28 -04002181 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2182 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2183 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 server->acl_bitmask = res.acl_bitmask;
2185 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return status;
2188}
2189
Trond Myklebust55a97592006-06-09 09:34:19 -04002190int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct nfs4_exception exception = { };
2193 int err;
2194 do {
2195 err = nfs4_handle_exception(server,
2196 _nfs4_server_capabilities(server, fhandle),
2197 &exception);
2198 } while (exception.retry);
2199 return err;
2200}
2201
2202static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2203 struct nfs_fsinfo *info)
2204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct nfs4_lookup_root_arg args = {
2206 .bitmask = nfs4_fattr_bitmap,
2207 };
2208 struct nfs4_lookup_res res = {
2209 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002210 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 .fh = fhandle,
2212 };
2213 struct rpc_message msg = {
2214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2215 .rpc_argp = &args,
2216 .rpc_resp = &res,
2217 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002218
Trond Myklebust0e574af2005-10-27 22:12:38 -04002219 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002220 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
2223static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2224 struct nfs_fsinfo *info)
2225{
2226 struct nfs4_exception exception = { };
2227 int err;
2228 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002229 err = _nfs4_lookup_root(server, fhandle, info);
2230 switch (err) {
2231 case 0:
2232 case -NFS4ERR_WRONGSEC:
2233 break;
2234 default:
2235 err = nfs4_handle_exception(server, err, &exception);
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 } while (exception.retry);
2238 return err;
2239}
2240
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002241static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2242 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2243{
2244 struct rpc_auth *auth;
2245 int ret;
2246
2247 auth = rpcauth_create(flavor, server->client);
2248 if (!auth) {
2249 ret = -EIO;
2250 goto out;
2251 }
2252 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002253out:
2254 return ret;
2255}
2256
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002257static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002258 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002260 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002261 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002263 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2264 flav_array[len] = RPC_AUTH_NULL;
2265 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002266
2267 for (i = 0; i < len; i++) {
2268 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002269 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002270 continue;
2271 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002272 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002273 /*
2274 * -EACCESS could mean that the user doesn't have correct permissions
2275 * to access the mount. It could also mean that we tried to mount
2276 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2277 * existing mount programs don't handle -EACCES very well so it should
2278 * be mapped to -EPERM instead.
2279 */
2280 if (status == -EACCES)
2281 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002282 return status;
2283}
2284
2285/*
2286 * get the file handle for the "/" directory on the server
2287 */
2288static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2289 struct nfs_fsinfo *info)
2290{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002291 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002292 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002293 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2294 /*
2295 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2296 * by nfs4_map_errors() as this function exits.
2297 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002298 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (status == 0)
2300 status = nfs4_server_capabilities(server, fhandle);
2301 if (status == 0)
2302 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002303 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Andy Adamson533eb462011-06-13 18:25:56 -04002306static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307/*
2308 * Get locations and (maybe) other attributes of a referral.
2309 * Note that we'll actually follow the referral later when
2310 * we detect fsid mismatch in inode revalidation
2311 */
Andy Adamson533eb462011-06-13 18:25:56 -04002312static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2313 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002314{
2315 int status = -ENOMEM;
2316 struct page *page = NULL;
2317 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002318
2319 page = alloc_page(GFP_KERNEL);
2320 if (page == NULL)
2321 goto out;
2322 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2323 if (locations == NULL)
2324 goto out;
2325
Trond Myklebustc228fd32007-01-13 02:28:11 -05002326 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327 if (status != 0)
2328 goto out;
2329 /* Make sure server returned a different fsid for the referral */
2330 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002331 dprintk("%s: server did not return a different fsid for"
2332 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002333 status = -EIO;
2334 goto out;
2335 }
Andy Adamson533eb462011-06-13 18:25:56 -04002336 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2337 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002338
Andy Adamson533eb462011-06-13 18:25:56 -04002339 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002340 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002341 memset(fhandle, 0, sizeof(struct nfs_fh));
2342out:
2343 if (page)
2344 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002345 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002346 return status;
2347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2350{
2351 struct nfs4_getattr_arg args = {
2352 .fh = fhandle,
2353 .bitmask = server->attr_bitmask,
2354 };
2355 struct nfs4_getattr_res res = {
2356 .fattr = fattr,
2357 .server = server,
2358 };
2359 struct rpc_message msg = {
2360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2361 .rpc_argp = &args,
2362 .rpc_resp = &res,
2363 };
2364
Trond Myklebust0e574af2005-10-27 22:12:38 -04002365 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002366 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2370{
2371 struct nfs4_exception exception = { };
2372 int err;
2373 do {
2374 err = nfs4_handle_exception(server,
2375 _nfs4_proc_getattr(server, fhandle, fattr),
2376 &exception);
2377 } while (exception.retry);
2378 return err;
2379}
2380
2381/*
2382 * The file is not closed if it is opened due to the a request to change
2383 * the size of the file. The open call will not be needed once the
2384 * VFS layer lookup-intents are implemented.
2385 *
2386 * Close is called when the inode is destroyed.
2387 * If we haven't opened the file for O_WRONLY, we
2388 * need to in the size_change case to obtain a stateid.
2389 *
2390 * Got race?
2391 * Because OPEN is always done by name in nfsv4, it is
2392 * possible that we opened a different file by the same
2393 * name. We can recognize this race condition, but we
2394 * can't do anything about it besides returning an error.
2395 *
2396 * This will be fixed with VFS changes (lookup-intent).
2397 */
2398static int
2399nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2400 struct iattr *sattr)
2401{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002402 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002403 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002404 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int status;
2406
Benny Halevy8a1636c2010-07-14 15:43:57 -04002407 if (pnfs_ld_layoutret_on_setattr(inode))
2408 pnfs_return_layout(inode);
2409
Trond Myklebust0e574af2005-10-27 22:12:38 -04002410 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Trond Myklebustd5308382005-11-04 15:33:38 -05002412 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002413 if (sattr->ia_valid & ATTR_FILE) {
2414 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002415
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002416 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002417 if (ctx) {
2418 cred = ctx->cred;
2419 state = ctx->state;
2420 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002421 }
2422
2423 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002424 if (status == 0)
2425 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 return status;
2427}
2428
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002429static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2430 const struct qstr *name, struct nfs_fh *fhandle,
2431 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002432{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002433 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002434 int status;
2435 struct nfs4_lookup_arg args = {
2436 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002437 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002438 .name = name,
2439 };
2440 struct nfs4_lookup_res res = {
2441 .server = server,
2442 .fattr = fattr,
2443 .fh = fhandle,
2444 };
2445 struct rpc_message msg = {
2446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2447 .rpc_argp = &args,
2448 .rpc_resp = &res,
2449 };
2450
2451 nfs_fattr_init(fattr);
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002454 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 dprintk("NFS reply lookup: %d\n", status);
2456 return status;
2457}
2458
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002459void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2460{
2461 memset(fh, 0, sizeof(struct nfs_fh));
2462 fattr->fsid.major = 1;
2463 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2464 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2465 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2466 fattr->nlink = 2;
2467}
2468
Bryan Schumaker7c513052011-03-24 17:12:24 +00002469static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2470 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 struct nfs4_exception exception = { };
2473 int err;
2474 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002475 int status;
2476
2477 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2478 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002479 case -NFS4ERR_BADNAME:
2480 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002481 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002482 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002483 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002484 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002485 }
2486 err = nfs4_handle_exception(NFS_SERVER(dir),
2487 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 } while (exception.retry);
2489 return err;
2490}
2491
2492static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2493{
Trond Myklebust76b32992007-08-10 17:45:11 -04002494 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 struct nfs4_accessargs args = {
2496 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002497 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002499 struct nfs4_accessres res = {
2500 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2504 .rpc_argp = &args,
2505 .rpc_resp = &res,
2506 .rpc_cred = entry->cred,
2507 };
2508 int mode = entry->mask;
2509 int status;
2510
2511 /*
2512 * Determine which access bits we want to ask for...
2513 */
2514 if (mode & MAY_READ)
2515 args.access |= NFS4_ACCESS_READ;
2516 if (S_ISDIR(inode->i_mode)) {
2517 if (mode & MAY_WRITE)
2518 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2519 if (mode & MAY_EXEC)
2520 args.access |= NFS4_ACCESS_LOOKUP;
2521 } else {
2522 if (mode & MAY_WRITE)
2523 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2524 if (mode & MAY_EXEC)
2525 args.access |= NFS4_ACCESS_EXECUTE;
2526 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002527
2528 res.fattr = nfs_alloc_fattr();
2529 if (res.fattr == NULL)
2530 return -ENOMEM;
2531
Bryan Schumaker7c513052011-03-24 17:12:24 +00002532 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (!status) {
2534 entry->mask = 0;
2535 if (res.access & NFS4_ACCESS_READ)
2536 entry->mask |= MAY_READ;
2537 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2538 entry->mask |= MAY_WRITE;
2539 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2540 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002541 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002543 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return status;
2545}
2546
2547static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2548{
2549 struct nfs4_exception exception = { };
2550 int err;
2551 do {
2552 err = nfs4_handle_exception(NFS_SERVER(inode),
2553 _nfs4_proc_access(inode, entry),
2554 &exception);
2555 } while (exception.retry);
2556 return err;
2557}
2558
2559/*
2560 * TODO: For the time being, we don't try to get any attributes
2561 * along with any of the zero-copy operations READ, READDIR,
2562 * READLINK, WRITE.
2563 *
2564 * In the case of the first three, we want to put the GETATTR
2565 * after the read-type operation -- this is because it is hard
2566 * to predict the length of a GETATTR response in v4, and thus
2567 * align the READ data correctly. This means that the GETATTR
2568 * may end up partially falling into the page cache, and we should
2569 * shift it into the 'tail' of the xdr_buf before processing.
2570 * To do this efficiently, we need to know the total length
2571 * of data received, which doesn't seem to be available outside
2572 * of the RPC layer.
2573 *
2574 * In the case of WRITE, we also want to put the GETATTR after
2575 * the operation -- in this case because we want to make sure
2576 * we get the post-operation mtime and size. This means that
2577 * we can't use xdr_encode_pages() as written: we need a variant
2578 * of it which would leave room in the 'tail' iovec.
2579 *
2580 * Both of these changes to the XDR layer would in fact be quite
2581 * minor, but I decided to leave them for a subsequent patch.
2582 */
2583static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2584 unsigned int pgbase, unsigned int pglen)
2585{
2586 struct nfs4_readlink args = {
2587 .fh = NFS_FH(inode),
2588 .pgbase = pgbase,
2589 .pglen = pglen,
2590 .pages = &page,
2591 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002592 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2595 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002596 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 };
2598
Bryan Schumaker7c513052011-03-24 17:12:24 +00002599 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 -07002600}
2601
2602static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2603 unsigned int pgbase, unsigned int pglen)
2604{
2605 struct nfs4_exception exception = { };
2606 int err;
2607 do {
2608 err = nfs4_handle_exception(NFS_SERVER(inode),
2609 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2610 &exception);
2611 } while (exception.retry);
2612 return err;
2613}
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615/*
2616 * Got race?
2617 * We will need to arrange for the VFS layer to provide an atomic open.
2618 * Until then, this create/open method is prone to inefficiency and race
2619 * conditions due to the lookup, create, and open VFS calls from sys_open()
2620 * placed on the wire.
2621 *
2622 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2623 * The file will be opened again in the subsequent VFS open call
2624 * (nfs4_proc_file_open).
2625 *
2626 * The open for read will just hang around to be used by any process that
2627 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2628 */
2629
2630static int
2631nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002632 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Al Viro82a2c1b2011-06-22 18:30:55 -04002634 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002636 struct rpc_cred *cred = NULL;
2637 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int status = 0;
2639
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002640 if (ctx != NULL) {
2641 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002642 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002643 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002645 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002646 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002647 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (IS_ERR(state)) {
2649 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002652 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002653 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002654 if (ctx != NULL)
2655 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002656 else
Al Viro643168c2011-06-22 18:20:23 -04002657 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658out:
2659 return status;
2660}
2661
2662static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2663{
Trond Myklebust16e42952005-10-27 22:12:44 -04002664 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002665 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002667 .name.len = name->len,
2668 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002669 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002671 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002672 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002673 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2676 .rpc_argp = &args,
2677 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002679 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Trond Myklebustd3468902010-04-16 16:22:50 -04002681 res.dir_attr = nfs_alloc_fattr();
2682 if (res.dir_attr == NULL)
2683 goto out;
2684
Bryan Schumaker7c513052011-03-24 17:12:24 +00002685 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002686 if (status == 0) {
2687 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002688 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002689 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002690 nfs_free_fattr(res.dir_attr);
2691out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return status;
2693}
2694
2695static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2696{
2697 struct nfs4_exception exception = { };
2698 int err;
2699 do {
2700 err = nfs4_handle_exception(NFS_SERVER(dir),
2701 _nfs4_proc_remove(dir, name),
2702 &exception);
2703 } while (exception.retry);
2704 return err;
2705}
2706
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002707static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002709 struct nfs_server *server = NFS_SERVER(dir);
2710 struct nfs_removeargs *args = msg->rpc_argp;
2711 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Trond Myklebusta65318b2009-03-11 14:10:28 -04002713 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002714 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002715 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002719static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002721 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2722
Trond Myklebust14516c32010-07-31 14:29:06 -04002723 if (!nfs4_sequence_done(task, &res->seq_res))
2724 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002725 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002726 return 0;
2727 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002728 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002729 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Jeff Laytond3d41522010-09-17 17:31:57 -04002732static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2733{
2734 struct nfs_server *server = NFS_SERVER(dir);
2735 struct nfs_renameargs *arg = msg->rpc_argp;
2736 struct nfs_renameres *res = msg->rpc_resp;
2737
2738 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2739 arg->bitmask = server->attr_bitmask;
2740 res->server = server;
2741}
2742
2743static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2744 struct inode *new_dir)
2745{
2746 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2747
2748 if (!nfs4_sequence_done(task, &res->seq_res))
2749 return 0;
2750 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2751 return 0;
2752
2753 update_changeattr(old_dir, &res->old_cinfo);
2754 nfs_post_op_update_inode(old_dir, res->old_fattr);
2755 update_changeattr(new_dir, &res->new_cinfo);
2756 nfs_post_op_update_inode(new_dir, res->new_fattr);
2757 return 1;
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2761 struct inode *new_dir, struct qstr *new_name)
2762{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002763 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002764 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 .old_dir = NFS_FH(old_dir),
2766 .new_dir = NFS_FH(new_dir),
2767 .old_name = old_name,
2768 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002769 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002771 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002772 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002773 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 struct rpc_message msg = {
2775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2776 .rpc_argp = &arg,
2777 .rpc_resp = &res,
2778 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002779 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Trond Myklebust011fff72010-04-16 16:22:49 -04002781 res.old_fattr = nfs_alloc_fattr();
2782 res.new_fattr = nfs_alloc_fattr();
2783 if (res.old_fattr == NULL || res.new_fattr == NULL)
2784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Bryan Schumaker7c513052011-03-24 17:12:24 +00002786 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (!status) {
2788 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002789 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002791 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002793out:
2794 nfs_free_fattr(res.new_fattr);
2795 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return status;
2797}
2798
2799static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2800 struct inode *new_dir, struct qstr *new_name)
2801{
2802 struct nfs4_exception exception = { };
2803 int err;
2804 do {
2805 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2806 _nfs4_proc_rename(old_dir, old_name,
2807 new_dir, new_name),
2808 &exception);
2809 } while (exception.retry);
2810 return err;
2811}
2812
2813static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2814{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002815 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct nfs4_link_arg arg = {
2817 .fh = NFS_FH(inode),
2818 .dir_fh = NFS_FH(dir),
2819 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002820 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002822 struct nfs4_link_res res = {
2823 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002824 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 struct rpc_message msg = {
2826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2827 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002828 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002830 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Trond Myklebust136f2622010-04-16 16:22:49 -04002832 res.fattr = nfs_alloc_fattr();
2833 res.dir_attr = nfs_alloc_fattr();
2834 if (res.fattr == NULL || res.dir_attr == NULL)
2835 goto out;
2836
Bryan Schumaker7c513052011-03-24 17:12:24 +00002837 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002838 if (!status) {
2839 update_changeattr(dir, &res.cinfo);
2840 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002841 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002842 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002843out:
2844 nfs_free_fattr(res.dir_attr);
2845 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return status;
2847}
2848
2849static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2850{
2851 struct nfs4_exception exception = { };
2852 int err;
2853 do {
2854 err = nfs4_handle_exception(NFS_SERVER(inode),
2855 _nfs4_proc_link(inode, dir, name),
2856 &exception);
2857 } while (exception.retry);
2858 return err;
2859}
2860
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002861struct nfs4_createdata {
2862 struct rpc_message msg;
2863 struct nfs4_create_arg arg;
2864 struct nfs4_create_res res;
2865 struct nfs_fh fh;
2866 struct nfs_fattr fattr;
2867 struct nfs_fattr dir_fattr;
2868};
2869
2870static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2871 struct qstr *name, struct iattr *sattr, u32 ftype)
2872{
2873 struct nfs4_createdata *data;
2874
2875 data = kzalloc(sizeof(*data), GFP_KERNEL);
2876 if (data != NULL) {
2877 struct nfs_server *server = NFS_SERVER(dir);
2878
2879 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2880 data->msg.rpc_argp = &data->arg;
2881 data->msg.rpc_resp = &data->res;
2882 data->arg.dir_fh = NFS_FH(dir);
2883 data->arg.server = server;
2884 data->arg.name = name;
2885 data->arg.attrs = sattr;
2886 data->arg.ftype = ftype;
2887 data->arg.bitmask = server->attr_bitmask;
2888 data->res.server = server;
2889 data->res.fh = &data->fh;
2890 data->res.fattr = &data->fattr;
2891 data->res.dir_fattr = &data->dir_fattr;
2892 nfs_fattr_init(data->res.fattr);
2893 nfs_fattr_init(data->res.dir_fattr);
2894 }
2895 return data;
2896}
2897
2898static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2899{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002900 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002901 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002902 if (status == 0) {
2903 update_changeattr(dir, &data->res.dir_cinfo);
2904 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2905 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2906 }
2907 return status;
2908}
2909
2910static void nfs4_free_createdata(struct nfs4_createdata *data)
2911{
2912 kfree(data);
2913}
2914
Chuck Lever4f390c12006-08-22 20:06:22 -04002915static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002916 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002918 struct nfs4_createdata *data;
2919 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Chuck Lever94a6d752006-08-22 20:06:23 -04002921 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002923
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002924 status = -ENOMEM;
2925 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2926 if (data == NULL)
2927 goto out;
2928
2929 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2930 data->arg.u.symlink.pages = &page;
2931 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002933 status = nfs4_do_create(dir, dentry, data);
2934
2935 nfs4_free_createdata(data);
2936out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return status;
2938}
2939
Chuck Lever4f390c12006-08-22 20:06:22 -04002940static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002941 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct nfs4_exception exception = { };
2944 int err;
2945 do {
2946 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002947 _nfs4_proc_symlink(dir, dentry, page,
2948 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
2954static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2955 struct iattr *sattr)
2956{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002957 struct nfs4_createdata *data;
2958 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002960 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2961 if (data == NULL)
2962 goto out;
2963
2964 status = nfs4_do_create(dir, dentry, data);
2965
2966 nfs4_free_createdata(data);
2967out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return status;
2969}
2970
2971static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2972 struct iattr *sattr)
2973{
2974 struct nfs4_exception exception = { };
2975 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002976
2977 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 do {
2979 err = nfs4_handle_exception(NFS_SERVER(dir),
2980 _nfs4_proc_mkdir(dir, dentry, sattr),
2981 &exception);
2982 } while (exception.retry);
2983 return err;
2984}
2985
2986static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002987 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 struct inode *dir = dentry->d_inode;
2990 struct nfs4_readdir_arg args = {
2991 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002992 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 .pgbase = 0,
2994 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002995 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002996 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
2998 struct nfs4_readdir_res res;
2999 struct rpc_message msg = {
3000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3001 .rpc_argp = &args,
3002 .rpc_resp = &res,
3003 .rpc_cred = cred,
3004 };
3005 int status;
3006
Harvey Harrison3110ff82008-05-02 13:42:44 -07003007 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003008 dentry->d_parent->d_name.name,
3009 dentry->d_name.name,
3010 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3012 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003013 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 -05003014 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003016 status += args.pgbase;
3017 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003018
3019 nfs_invalidate_atime(dir);
3020
Harvey Harrison3110ff82008-05-02 13:42:44 -07003021 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return status;
3023}
3024
3025static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003026 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
3028 struct nfs4_exception exception = { };
3029 int err;
3030 do {
3031 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3032 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003033 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 &exception);
3035 } while (exception.retry);
3036 return err;
3037}
3038
3039static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3040 struct iattr *sattr, dev_t rdev)
3041{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042 struct nfs4_createdata *data;
3043 int mode = sattr->ia_mode;
3044 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3047 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048
3049 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3050 if (data == NULL)
3051 goto out;
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003054 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003056 data->arg.ftype = NF4BLK;
3057 data->arg.u.device.specdata1 = MAJOR(rdev);
3058 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061 data->arg.ftype = NF4CHR;
3062 data->arg.u.device.specdata1 = MAJOR(rdev);
3063 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003066 status = nfs4_do_create(dir, dentry, data);
3067
3068 nfs4_free_createdata(data);
3069out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return status;
3071}
3072
3073static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3074 struct iattr *sattr, dev_t rdev)
3075{
3076 struct nfs4_exception exception = { };
3077 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003078
3079 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 do {
3081 err = nfs4_handle_exception(NFS_SERVER(dir),
3082 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3083 &exception);
3084 } while (exception.retry);
3085 return err;
3086}
3087
3088static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3089 struct nfs_fsstat *fsstat)
3090{
3091 struct nfs4_statfs_arg args = {
3092 .fh = fhandle,
3093 .bitmask = server->attr_bitmask,
3094 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003095 struct nfs4_statfs_res res = {
3096 .fsstat = fsstat,
3097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct rpc_message msg = {
3099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3100 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003101 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 };
3103
Trond Myklebust0e574af2005-10-27 22:12:38 -04003104 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003105 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
3108static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3109{
3110 struct nfs4_exception exception = { };
3111 int err;
3112 do {
3113 err = nfs4_handle_exception(server,
3114 _nfs4_proc_statfs(server, fhandle, fsstat),
3115 &exception);
3116 } while (exception.retry);
3117 return err;
3118}
3119
3120static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3121 struct nfs_fsinfo *fsinfo)
3122{
3123 struct nfs4_fsinfo_arg args = {
3124 .fh = fhandle,
3125 .bitmask = server->attr_bitmask,
3126 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003127 struct nfs4_fsinfo_res res = {
3128 .fsinfo = fsinfo,
3129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct rpc_message msg = {
3131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3132 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003133 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 };
3135
Bryan Schumaker7c513052011-03-24 17:12:24 +00003136 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
3139static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3140{
3141 struct nfs4_exception exception = { };
3142 int err;
3143
3144 do {
3145 err = nfs4_handle_exception(server,
3146 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3147 &exception);
3148 } while (exception.retry);
3149 return err;
3150}
3151
3152static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3153{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003154 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3156}
3157
3158static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3159 struct nfs_pathconf *pathconf)
3160{
3161 struct nfs4_pathconf_arg args = {
3162 .fh = fhandle,
3163 .bitmask = server->attr_bitmask,
3164 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003165 struct nfs4_pathconf_res res = {
3166 .pathconf = pathconf,
3167 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct rpc_message msg = {
3169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3170 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003171 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 };
3173
3174 /* None of the pathconf attributes are mandatory to implement */
3175 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3176 memset(pathconf, 0, sizeof(*pathconf));
3177 return 0;
3178 }
3179
Trond Myklebust0e574af2005-10-27 22:12:38 -04003180 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003181 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
3184static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3185 struct nfs_pathconf *pathconf)
3186{
3187 struct nfs4_exception exception = { };
3188 int err;
3189
3190 do {
3191 err = nfs4_handle_exception(server,
3192 _nfs4_proc_pathconf(server, fhandle, pathconf),
3193 &exception);
3194 } while (exception.retry);
3195 return err;
3196}
3197
Benny Halevyd20581a2011-05-22 19:52:03 +03003198void __nfs4_read_done_cb(struct nfs_read_data *data)
3199{
3200 nfs_invalidate_atime(data->inode);
3201}
3202
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003203static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
Trond Myklebustec06c092006-03-20 13:44:27 -05003205 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003207 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003208 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003209 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003211
Benny Halevyd20581a2011-05-22 19:52:03 +03003212 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003214 renew_lease(server, data->timestamp);
3215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216}
3217
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003218static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3219{
3220
3221 dprintk("--> %s\n", __func__);
3222
3223 if (!nfs4_sequence_done(task, &data->res.seq_res))
3224 return -EAGAIN;
3225
Benny Halevyd20581a2011-05-22 19:52:03 +03003226 return data->read_done_cb ? data->read_done_cb(task, data) :
3227 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003228}
3229
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003230static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003233 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003234 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003237/* Reset the the nfs_read_data to send the read to the MDS. */
3238void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3239{
3240 dprintk("%s Reset task for i/o through\n", __func__);
3241 put_lseg(data->lseg);
3242 data->lseg = NULL;
3243 /* offsets will differ in the dense stripe case */
3244 data->args.offset = data->mds_offset;
3245 data->ds_clp = NULL;
3246 data->args.fh = NFS_FH(data->inode);
3247 data->read_done_cb = nfs4_read_done_cb;
3248 task->tk_ops = data->mds_ops;
3249 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3250}
3251EXPORT_SYMBOL_GPL(nfs4_reset_read);
3252
Fred Isamanb029bc92011-03-03 15:13:42 +00003253static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 struct inode *inode = data->inode;
3256
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003257 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003258 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003259 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003261 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003263 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003264 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Fred Isamanb029bc92011-03-03 15:13:42 +00003268static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3269{
3270 if (!nfs4_sequence_done(task, &data->res.seq_res))
3271 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003272 return data->write_done_cb ? data->write_done_cb(task, data) :
3273 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003274}
3275
Fred Isamana69aef12011-03-03 15:13:47 +00003276/* Reset the the nfs_write_data to send the write to the MDS. */
3277void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3278{
3279 dprintk("%s Reset task for i/o through\n", __func__);
3280 put_lseg(data->lseg);
3281 data->lseg = NULL;
3282 data->ds_clp = NULL;
3283 data->write_done_cb = nfs4_write_done_cb;
3284 data->args.fh = NFS_FH(data->inode);
3285 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3286 data->args.offset = data->mds_offset;
3287 data->res.fattr = &data->fattr;
3288 task->tk_ops = data->mds_ops;
3289 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3290}
3291EXPORT_SYMBOL_GPL(nfs4_reset_write);
3292
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003293static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003295 struct nfs_server *server = NFS_SERVER(data->inode);
3296
Fred Isaman7ffd1062011-03-03 15:13:46 +00003297 if (data->lseg) {
3298 data->args.bitmask = NULL;
3299 data->res.fattr = NULL;
3300 } else
3301 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003302 if (!data->write_done_cb)
3303 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003304 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 data->timestamp = jiffies;
3306
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003307 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Fred Isaman5f452432011-03-23 13:27:46 +00003310static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003313
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003314 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003315 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003316 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003318 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Fred Isaman5f452432011-03-23 13:27:46 +00003322static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3323{
3324 if (!nfs4_sequence_done(task, &data->res.seq_res))
3325 return -EAGAIN;
3326 return data->write_done_cb(task, data);
3327}
3328
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003329static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003331 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003332
3333 if (data->lseg) {
3334 data->args.bitmask = NULL;
3335 data->res.fattr = NULL;
3336 } else
3337 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003338 if (!data->write_done_cb)
3339 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003340 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344struct nfs4_renewdata {
3345 struct nfs_client *client;
3346 unsigned long timestamp;
3347};
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349/*
3350 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3351 * standalone procedure for queueing an asynchronous RENEW.
3352 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003353static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003354{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003355 struct nfs4_renewdata *data = calldata;
3356 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003357
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003358 if (atomic_read(&clp->cl_count) > 1)
3359 nfs4_schedule_state_renewal(clp);
3360 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003361 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003362}
3363
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003364static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003366 struct nfs4_renewdata *data = calldata;
3367 struct nfs_client *clp = data->client;
3368 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003371 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003372 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3373 return;
3374 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003375 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003376 return;
3377 }
3378 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003380 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003383static const struct rpc_call_ops nfs4_renew_ops = {
3384 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003385 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003386};
3387
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003388static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 struct rpc_message msg = {
3391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3392 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003393 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003395 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003397 if (renew_flags == 0)
3398 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003399 if (!atomic_inc_not_zero(&clp->cl_count))
3400 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003401 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003402 if (data == NULL)
3403 return -ENOMEM;
3404 data->client = clp;
3405 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003407 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003410static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 struct rpc_message msg = {
3413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3414 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003415 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
3417 unsigned long now = jiffies;
3418 int status;
3419
3420 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3421 if (status < 0)
3422 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003423 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 0;
3425}
3426
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003427static inline int nfs4_server_supports_acls(struct nfs_server *server)
3428{
3429 return (server->caps & NFS_CAP_ACLS)
3430 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3431 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3432}
3433
3434/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3435 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3436 * the stack.
3437 */
3438#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3439
Neil Hormane9e3d722011-03-04 19:26:03 -05003440static int buf_to_pages_noslab(const void *buf, size_t buflen,
3441 struct page **pages, unsigned int *pgbase)
3442{
3443 struct page *newpage, **spages;
3444 int rc = 0;
3445 size_t len;
3446 spages = pages;
3447
3448 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003449 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003450 newpage = alloc_page(GFP_KERNEL);
3451
3452 if (newpage == NULL)
3453 goto unwind;
3454 memcpy(page_address(newpage), buf, len);
3455 buf += len;
3456 buflen -= len;
3457 *pages++ = newpage;
3458 rc++;
3459 } while (buflen != 0);
3460
3461 return rc;
3462
3463unwind:
3464 for(; rc > 0; rc--)
3465 __free_page(spages[rc-1]);
3466 return -ENOMEM;
3467}
3468
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003469struct nfs4_cached_acl {
3470 int cached;
3471 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003472 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003473};
3474
3475static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003476{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003477 struct nfs_inode *nfsi = NFS_I(inode);
3478
3479 spin_lock(&inode->i_lock);
3480 kfree(nfsi->nfs4_acl);
3481 nfsi->nfs4_acl = acl;
3482 spin_unlock(&inode->i_lock);
3483}
3484
3485static void nfs4_zap_acl_attr(struct inode *inode)
3486{
3487 nfs4_set_cached_acl(inode, NULL);
3488}
3489
3490static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3491{
3492 struct nfs_inode *nfsi = NFS_I(inode);
3493 struct nfs4_cached_acl *acl;
3494 int ret = -ENOENT;
3495
3496 spin_lock(&inode->i_lock);
3497 acl = nfsi->nfs4_acl;
3498 if (acl == NULL)
3499 goto out;
3500 if (buf == NULL) /* user is just asking for length */
3501 goto out_len;
3502 if (acl->cached == 0)
3503 goto out;
3504 ret = -ERANGE; /* see getxattr(2) man page */
3505 if (acl->len > buflen)
3506 goto out;
3507 memcpy(buf, acl->data, acl->len);
3508out_len:
3509 ret = acl->len;
3510out:
3511 spin_unlock(&inode->i_lock);
3512 return ret;
3513}
3514
3515static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3516{
3517 struct nfs4_cached_acl *acl;
3518
3519 if (buf && acl_len <= PAGE_SIZE) {
3520 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3521 if (acl == NULL)
3522 goto out;
3523 acl->cached = 1;
3524 memcpy(acl->data, buf, acl_len);
3525 } else {
3526 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3527 if (acl == NULL)
3528 goto out;
3529 acl->cached = 0;
3530 }
3531 acl->len = acl_len;
3532out:
3533 nfs4_set_cached_acl(inode, acl);
3534}
3535
Andy Adamsonbf118a32011-12-07 11:55:27 -05003536/*
3537 * The getxattr API returns the required buffer length when called with a
3538 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3539 * the required buf. On a NULL buf, we send a page of data to the server
3540 * guessing that the ACL request can be serviced by a page. If so, we cache
3541 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3542 * the cache. If not so, we throw away the page, and cache the required
3543 * length. The next getxattr call will then produce another round trip to
3544 * the server, this time with the input buf of the required size.
3545 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003546static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003547{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003548 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003549 struct nfs_getaclargs args = {
3550 .fh = NFS_FH(inode),
3551 .acl_pages = pages,
3552 .acl_len = buflen,
3553 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003554 struct nfs_getaclres res = {
3555 .acl_len = buflen,
3556 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003557 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3560 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003561 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003562 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003563 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003564
Andy Adamsonbf118a32011-12-07 11:55:27 -05003565 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3566 /* As long as we're doing a round trip to the server anyway,
3567 * let's be prepared for a page of acl data. */
3568 if (npages == 0)
3569 npages = 1;
3570
3571 for (i = 0; i < npages; i++) {
3572 pages[i] = alloc_page(GFP_KERNEL);
3573 if (!pages[i])
3574 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003575 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003576 if (npages > 1) {
3577 /* for decoding across pages */
Trond Myklebust331818f2012-02-03 18:30:53 -05003578 res.acl_scratch = alloc_page(GFP_KERNEL);
3579 if (!res.acl_scratch)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003580 goto out_free;
3581 }
3582 args.acl_len = npages * PAGE_SIZE;
3583 args.acl_pgbase = 0;
3584 /* Let decode_getfacl know not to fail if the ACL data is larger than
3585 * the page we send as a guess */
3586 if (buf == NULL)
3587 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3588 resp_buf = page_address(pages[0]);
3589
Peng Taode040be2012-01-10 22:42:47 +08003590 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003591 __func__, buf, buflen, npages, args.acl_len);
3592 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3593 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003594 if (ret)
3595 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003596
3597 acl_len = res.acl_len - res.acl_data_offset;
3598 if (acl_len > args.acl_len)
3599 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003600 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003601 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3602 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003603 if (buf) {
3604 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003605 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003606 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003607 _copy_from_pages(buf, pages, res.acl_data_offset,
3608 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003609 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003610 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003611out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003612 for (i = 0; i < npages; i++)
3613 if (pages[i])
3614 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003615 if (res.acl_scratch)
3616 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003617 return ret;
3618}
3619
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003620static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3621{
3622 struct nfs4_exception exception = { };
3623 ssize_t ret;
3624 do {
3625 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3626 if (ret >= 0)
3627 break;
3628 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3629 } while (exception.retry);
3630 return ret;
3631}
3632
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003633static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3634{
3635 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003636 int ret;
3637
3638 if (!nfs4_server_supports_acls(server))
3639 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003640 ret = nfs_revalidate_inode(server, inode);
3641 if (ret < 0)
3642 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003643 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3644 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645 ret = nfs4_read_cached_acl(inode, buf, buflen);
3646 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003647 /* -ENOENT is returned if there is no ACL or if there is an ACL
3648 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003649 return ret;
3650 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003651}
3652
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003653static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003654{
3655 struct nfs_server *server = NFS_SERVER(inode);
3656 struct page *pages[NFS4ACL_MAXPAGES];
3657 struct nfs_setaclargs arg = {
3658 .fh = NFS_FH(inode),
3659 .acl_pages = pages,
3660 .acl_len = buflen,
3661 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003662 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3665 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003666 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003667 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003668 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003669
3670 if (!nfs4_server_supports_acls(server))
3671 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003672 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3673 if (i < 0)
3674 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003675 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003676 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003677
3678 /*
3679 * Free each page after tx, so the only ref left is
3680 * held by the network stack
3681 */
3682 for (; i > 0; i--)
3683 put_page(pages[i-1]);
3684
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003685 /*
3686 * Acl update can result in inode attribute update.
3687 * so mark the attribute cache invalid.
3688 */
3689 spin_lock(&inode->i_lock);
3690 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3691 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003692 nfs_access_zap_cache(inode);
3693 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003694 return ret;
3695}
3696
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003697static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3698{
3699 struct nfs4_exception exception = { };
3700 int err;
3701 do {
3702 err = nfs4_handle_exception(NFS_SERVER(inode),
3703 __nfs4_proc_set_acl(inode, buf, buflen),
3704 &exception);
3705 } while (exception.retry);
3706 return err;
3707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003710nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003712 struct nfs_client *clp = server->nfs_client;
3713
3714 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return 0;
3716 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003717 case -NFS4ERR_ADMIN_REVOKED:
3718 case -NFS4ERR_BAD_STATEID:
3719 case -NFS4ERR_OPENMODE:
3720 if (state == NULL)
3721 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003722 nfs4_schedule_stateid_recovery(server, state);
3723 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003724 case -NFS4ERR_EXPIRED:
3725 if (state != NULL)
3726 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003728 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003729 nfs4_schedule_lease_recovery(clp);
3730 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003731#if defined(CONFIG_NFS_V4_1)
3732 case -NFS4ERR_BADSESSION:
3733 case -NFS4ERR_BADSLOT:
3734 case -NFS4ERR_BAD_HIGH_SLOT:
3735 case -NFS4ERR_DEADSESSION:
3736 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3737 case -NFS4ERR_SEQ_FALSE_RETRY:
3738 case -NFS4ERR_SEQ_MISORDERED:
3739 dprintk("%s ERROR %d, Reset session\n", __func__,
3740 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003741 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003742 task->tk_status = 0;
3743 return -EAGAIN;
3744#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003746 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003747 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003748 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3750 task->tk_status = 0;
3751 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003752 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 case -NFS4ERR_OLD_STATEID:
3754 task->tk_status = 0;
3755 return -EAGAIN;
3756 }
3757 task->tk_status = nfs4_map_errors(task->tk_status);
3758 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003759wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003760 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003761 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3762 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3763 task->tk_status = 0;
3764 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003767int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3768 unsigned short port, struct rpc_cred *cred,
3769 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
3771 nfs4_verifier sc_verifier;
3772 struct nfs4_setclientid setclientid = {
3773 .sc_verifier = &sc_verifier,
3774 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003775 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 };
3777 struct rpc_message msg = {
3778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3779 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003780 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003781 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 };
Al Virobc4785c2006-10-19 23:28:51 -07003783 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 int loop = 0;
3785 int status;
3786
Al Virobc4785c2006-10-19 23:28:51 -07003787 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3789 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3790
3791 for(;;) {
3792 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003793 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003794 clp->cl_ipaddr,
3795 rpc_peeraddr2str(clp->cl_rpcclient,
3796 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003797 rpc_peeraddr2str(clp->cl_rpcclient,
3798 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003799 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 clp->cl_id_uniquifier);
3801 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003802 sizeof(setclientid.sc_netid),
3803 rpc_peeraddr2str(clp->cl_rpcclient,
3804 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003806 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 clp->cl_ipaddr, port >> 8, port & 255);
3808
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003809 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 if (status != -NFS4ERR_CLID_INUSE)
3811 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003812 if (loop != 0) {
3813 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003815 }
3816 ++loop;
3817 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819 return status;
3820}
3821
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003822int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003823 struct nfs4_setclientid_res *arg,
3824 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 struct nfs_fsinfo fsinfo;
3827 struct rpc_message msg = {
3828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003829 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003831 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 };
3833 unsigned long now;
3834 int status;
3835
3836 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003837 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (status == 0) {
3839 spin_lock(&clp->cl_lock);
3840 clp->cl_lease_time = fsinfo.lease_time * HZ;
3841 clp->cl_last_renewal = now;
3842 spin_unlock(&clp->cl_lock);
3843 }
3844 return status;
3845}
3846
Trond Myklebustfe650402006-01-03 09:55:18 +01003847struct nfs4_delegreturndata {
3848 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003849 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003850 struct nfs_fh fh;
3851 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003852 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003853 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003854 int rpc_status;
3855};
3856
Trond Myklebustfe650402006-01-03 09:55:18 +01003857static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3858{
3859 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003860
Trond Myklebust14516c32010-07-31 14:29:06 -04003861 if (!nfs4_sequence_done(task, &data->res.seq_res))
3862 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003863
Ricardo Labiaga79708862009-12-07 09:23:21 -05003864 switch (task->tk_status) {
3865 case -NFS4ERR_STALE_STATEID:
3866 case -NFS4ERR_EXPIRED:
3867 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003868 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003869 break;
3870 default:
3871 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3872 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003873 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003874 return;
3875 }
3876 }
3877 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003878}
3879
3880static void nfs4_delegreturn_release(void *calldata)
3881{
Trond Myklebustfe650402006-01-03 09:55:18 +01003882 kfree(calldata);
3883}
3884
Andy Adamson938e1012009-04-01 09:22:28 -04003885#if defined(CONFIG_NFS_V4_1)
3886static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3887{
3888 struct nfs4_delegreturndata *d_data;
3889
3890 d_data = (struct nfs4_delegreturndata *)data;
3891
Trond Myklebust035168a2010-06-16 09:52:26 -04003892 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003893 &d_data->args.seq_args,
3894 &d_data->res.seq_res, 1, task))
3895 return;
3896 rpc_call_start(task);
3897}
3898#endif /* CONFIG_NFS_V4_1 */
3899
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003900static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003901#if defined(CONFIG_NFS_V4_1)
3902 .rpc_call_prepare = nfs4_delegreturn_prepare,
3903#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003904 .rpc_call_done = nfs4_delegreturn_done,
3905 .rpc_release = nfs4_delegreturn_release,
3906};
3907
Trond Myklebuste6f81072008-01-24 18:14:34 -05003908static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003909{
3910 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003911 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003912 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003913 struct rpc_message msg = {
3914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3915 .rpc_cred = cred,
3916 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003917 struct rpc_task_setup task_setup_data = {
3918 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003919 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003920 .callback_ops = &nfs4_delegreturn_ops,
3921 .flags = RPC_TASK_ASYNC,
3922 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003923 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003924
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003925 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003926 if (data == NULL)
3927 return -ENOMEM;
3928 data->args.fhandle = &data->fh;
3929 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003930 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003931 nfs_copy_fh(&data->fh, NFS_FH(inode));
3932 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003933 data->res.fattr = &data->fattr;
3934 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003935 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003936 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003937 data->rpc_status = 0;
3938
Trond Myklebustc970aa82007-07-14 15:39:59 -04003939 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003940 msg.rpc_argp = &data->args;
3941 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003942 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003943 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003944 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003945 if (!issync)
3946 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003947 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003948 if (status != 0)
3949 goto out;
3950 status = data->rpc_status;
3951 if (status != 0)
3952 goto out;
3953 nfs_refresh_inode(inode, &data->fattr);
3954out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003955 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003956 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
Trond Myklebuste6f81072008-01-24 18:14:34 -05003959int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
3961 struct nfs_server *server = NFS_SERVER(inode);
3962 struct nfs4_exception exception = { };
3963 int err;
3964 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003965 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 switch (err) {
3967 case -NFS4ERR_STALE_STATEID:
3968 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 case 0:
3970 return 0;
3971 }
3972 err = nfs4_handle_exception(server, err, &exception);
3973 } while (exception.retry);
3974 return err;
3975}
3976
3977#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3978#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3979
3980/*
3981 * sleep, with exponential backoff, and retry the LOCK operation.
3982 */
3983static unsigned long
3984nfs4_set_lock_task_retry(unsigned long timeout)
3985{
Jeff Laytond3103102011-12-01 22:44:39 +01003986 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 timeout <<= 1;
3988 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3989 return NFS4_LOCK_MAXTIMEOUT;
3990 return timeout;
3991}
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3994{
3995 struct inode *inode = state->inode;
3996 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003997 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003998 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004000 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004002 struct nfs_lockt_res res = {
4003 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 };
4005 struct rpc_message msg = {
4006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4007 .rpc_argp = &arg,
4008 .rpc_resp = &res,
4009 .rpc_cred = state->owner->so_cred,
4010 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 struct nfs4_lock_state *lsp;
4012 int status;
4013
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004014 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004015 status = nfs4_set_lock_state(state, request);
4016 if (status != 0)
4017 goto out;
4018 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004019 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004020 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004021 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004022 switch (status) {
4023 case 0:
4024 request->fl_type = F_UNLCK;
4025 break;
4026 case -NFS4ERR_DENIED:
4027 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004029 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004030out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return status;
4032}
4033
4034static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4035{
4036 struct nfs4_exception exception = { };
4037 int err;
4038
4039 do {
4040 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4041 _nfs4_proc_getlk(state, cmd, request),
4042 &exception);
4043 } while (exception.retry);
4044 return err;
4045}
4046
4047static int do_vfs_lock(struct file *file, struct file_lock *fl)
4048{
4049 int res = 0;
4050 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4051 case FL_POSIX:
4052 res = posix_lock_file_wait(file, fl);
4053 break;
4054 case FL_FLOCK:
4055 res = flock_lock_file_wait(file, fl);
4056 break;
4057 default:
4058 BUG();
4059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 return res;
4061}
4062
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004063struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004064 struct nfs_locku_args arg;
4065 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004066 struct nfs4_lock_state *lsp;
4067 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004068 struct file_lock fl;
4069 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004070 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004071};
4072
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004073static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4074 struct nfs_open_context *ctx,
4075 struct nfs4_lock_state *lsp,
4076 struct nfs_seqid *seqid)
4077{
4078 struct nfs4_unlockdata *p;
4079 struct inode *inode = lsp->ls_state->inode;
4080
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004081 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004082 if (p == NULL)
4083 return NULL;
4084 p->arg.fh = NFS_FH(inode);
4085 p->arg.fl = &p->fl;
4086 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004087 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004088 p->arg.stateid = &lsp->ls_stateid;
4089 p->lsp = lsp;
4090 atomic_inc(&lsp->ls_count);
4091 /* Ensure we don't close file until we're done freeing locks! */
4092 p->ctx = get_nfs_open_context(ctx);
4093 memcpy(&p->fl, fl, sizeof(p->fl));
4094 p->server = NFS_SERVER(inode);
4095 return p;
4096}
4097
Trond Myklebust06f814a2006-01-03 09:55:07 +01004098static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004100 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004101 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004102 nfs4_put_lock_state(calldata->lsp);
4103 put_nfs_open_context(calldata->ctx);
4104 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004105}
4106
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004107static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004108{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004109 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004110
Trond Myklebust14516c32010-07-31 14:29:06 -04004111 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4112 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004113 switch (task->tk_status) {
4114 case 0:
4115 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004116 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004117 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004118 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004119 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004120 case -NFS4ERR_BAD_STATEID:
4121 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004122 case -NFS4ERR_STALE_STATEID:
4123 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004124 break;
4125 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004126 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004127 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004128 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004129}
4130
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004131static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004132{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004133 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004135 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004136 return;
4137 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004138 /* Note: exit _without_ running nfs4_locku_done */
4139 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004140 return;
4141 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004142 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004143 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004144 &calldata->arg.seq_args,
4145 &calldata->res.seq_res, 1, task))
4146 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004147 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148}
4149
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004150static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004151 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004152 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004153 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004154};
4155
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004156static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4157 struct nfs_open_context *ctx,
4158 struct nfs4_lock_state *lsp,
4159 struct nfs_seqid *seqid)
4160{
4161 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004162 struct rpc_message msg = {
4163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4164 .rpc_cred = ctx->cred,
4165 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004166 struct rpc_task_setup task_setup_data = {
4167 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004168 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004169 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004170 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004171 .flags = RPC_TASK_ASYNC,
4172 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004173
Frank Filz137d6ac2007-07-09 15:32:29 -07004174 /* Ensure this is an unlock - when canceling a lock, the
4175 * canceled lock is passed in, and it won't be an unlock.
4176 */
4177 fl->fl_type = F_UNLCK;
4178
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004179 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4180 if (data == NULL) {
4181 nfs_free_seqid(seqid);
4182 return ERR_PTR(-ENOMEM);
4183 }
4184
Trond Myklebust1174dd12010-12-21 10:52:24 -05004185 msg.rpc_argp = &data->arg;
4186 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004187 task_setup_data.callback_data = data;
4188 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004189}
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4192{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004193 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004194 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004195 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004196 struct rpc_task *task;
4197 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004198 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Trond Myklebust9b073572006-06-29 16:38:34 -04004200 status = nfs4_set_lock_state(state, request);
4201 /* Unlock _before_ we do the RPC call */
4202 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004203 down_read(&nfsi->rwsem);
4204 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4205 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004206 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004207 }
4208 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004209 if (status != 0)
4210 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004211 /* Is this a delegated lock? */
4212 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004213 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004214 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004215 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004216 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004217 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004218 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004219 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004220 status = PTR_ERR(task);
4221 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004222 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004224 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004225out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004226 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004227 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230struct nfs4_lockdata {
4231 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004232 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004233 struct nfs4_lock_state *lsp;
4234 struct nfs_open_context *ctx;
4235 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004236 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 int rpc_status;
4238 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004239 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240};
4241
4242static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004243 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4244 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004245{
4246 struct nfs4_lockdata *p;
4247 struct inode *inode = lsp->ls_state->inode;
4248 struct nfs_server *server = NFS_SERVER(inode);
4249
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004250 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251 if (p == NULL)
4252 return NULL;
4253
4254 p->arg.fh = NFS_FH(inode);
4255 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004256 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004257 if (p->arg.open_seqid == NULL)
4258 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004259 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004260 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004261 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004262 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004263 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004264 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004265 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004266 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004267 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004268 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 atomic_inc(&lsp->ls_count);
4270 p->ctx = get_nfs_open_context(ctx);
4271 memcpy(&p->fl, fl, sizeof(p->fl));
4272 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004273out_free_seqid:
4274 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004275out_free:
4276 kfree(p);
4277 return NULL;
4278}
4279
4280static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4281{
4282 struct nfs4_lockdata *data = calldata;
4283 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Harvey Harrison3110ff82008-05-02 13:42:44 -07004285 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004286 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4287 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288 /* Do we need to do an open_to_lock_owner? */
4289 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004290 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4291 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004292 data->arg.open_stateid = &state->stateid;
4293 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004294 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004295 } else
4296 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004297 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004298 if (nfs4_setup_sequence(data->server,
4299 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004300 &data->res.seq_res, 1, task))
4301 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004302 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004303 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304}
4305
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004306static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4307{
4308 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4309 nfs4_lock_prepare(task, calldata);
4310}
4311
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4313{
4314 struct nfs4_lockdata *data = calldata;
4315
Harvey Harrison3110ff82008-05-02 13:42:44 -07004316 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004317
Trond Myklebust14516c32010-07-31 14:29:06 -04004318 if (!nfs4_sequence_done(task, &data->res.seq_res))
4319 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004320
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004323 if (data->rpc_status == 0)
4324 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4325 else
4326 goto out;
4327 }
4328 if (data->rpc_status == 0) {
4329 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4330 sizeof(data->lsp->ls_stateid.data));
4331 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004332 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004335 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004336}
4337
4338static void nfs4_lock_release(void *calldata)
4339{
4340 struct nfs4_lockdata *data = calldata;
4341
Harvey Harrison3110ff82008-05-02 13:42:44 -07004342 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004343 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004344 if (data->cancelled != 0) {
4345 struct rpc_task *task;
4346 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4347 data->arg.lock_seqid);
4348 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004349 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004350 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004351 } else
4352 nfs_free_seqid(data->arg.lock_seqid);
4353 nfs4_put_lock_state(data->lsp);
4354 put_nfs_open_context(data->ctx);
4355 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004356 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357}
4358
4359static const struct rpc_call_ops nfs4_lock_ops = {
4360 .rpc_call_prepare = nfs4_lock_prepare,
4361 .rpc_call_done = nfs4_lock_done,
4362 .rpc_release = nfs4_lock_release,
4363};
4364
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004365static const struct rpc_call_ops nfs4_recover_lock_ops = {
4366 .rpc_call_prepare = nfs4_recover_lock_prepare,
4367 .rpc_call_done = nfs4_lock_done,
4368 .rpc_release = nfs4_lock_release,
4369};
4370
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004371static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4372{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004373 switch (error) {
4374 case -NFS4ERR_ADMIN_REVOKED:
4375 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004376 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004377 if (new_lock_owner != 0 ||
4378 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004379 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004380 break;
4381 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004382 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004383 case -NFS4ERR_EXPIRED:
4384 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004385 };
4386}
4387
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004388static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004389{
4390 struct nfs4_lockdata *data;
4391 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4394 .rpc_cred = state->owner->so_cred,
4395 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004396 struct rpc_task_setup task_setup_data = {
4397 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004398 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004399 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004400 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004401 .flags = RPC_TASK_ASYNC,
4402 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004403 int ret;
4404
Harvey Harrison3110ff82008-05-02 13:42:44 -07004405 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004406 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004407 fl->fl_u.nfs4_fl.owner,
4408 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004409 if (data == NULL)
4410 return -ENOMEM;
4411 if (IS_SETLKW(cmd))
4412 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004413 if (recovery_type > NFS_LOCK_NEW) {
4414 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004415 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004416 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4417 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004418 msg.rpc_argp = &data->arg;
4419 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004420 task_setup_data.callback_data = data;
4421 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004422 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 ret = nfs4_wait_for_completion_rpc_task(task);
4425 if (ret == 0) {
4426 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004427 if (ret)
4428 nfs4_handle_setlk_error(data->server, data->lsp,
4429 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430 } else
4431 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004432 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004433 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
4437static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4438{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004439 struct nfs_server *server = NFS_SERVER(state->inode);
4440 struct nfs4_exception exception = { };
4441 int err;
4442
4443 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004444 /* Cache the lock if possible... */
4445 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4446 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004447 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004448 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004449 break;
4450 nfs4_handle_exception(server, err, &exception);
4451 } while (exception.retry);
4452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
4455static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4456{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004457 struct nfs_server *server = NFS_SERVER(state->inode);
4458 struct nfs4_exception exception = { };
4459 int err;
4460
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004461 err = nfs4_set_lock_state(state, request);
4462 if (err != 0)
4463 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004464 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004465 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4466 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004467 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004468 switch (err) {
4469 default:
4470 goto out;
4471 case -NFS4ERR_GRACE:
4472 case -NFS4ERR_DELAY:
4473 nfs4_handle_exception(server, err, &exception);
4474 err = 0;
4475 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004476 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004477out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004481#if defined(CONFIG_NFS_V4_1)
4482static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4483{
4484 int status;
4485 struct nfs_server *server = NFS_SERVER(state->inode);
4486
4487 status = nfs41_test_stateid(server, state);
4488 if (status == NFS_OK)
4489 return 0;
4490 nfs41_free_stateid(server, state);
4491 return nfs4_lock_expired(state, request);
4492}
4493#endif
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4496{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004497 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004498 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004499 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004501 if ((fl_flags & FL_POSIX) &&
4502 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4503 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004504 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004505 status = nfs4_set_lock_state(state, request);
4506 if (status != 0)
4507 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004508 request->fl_flags |= FL_ACCESS;
4509 status = do_vfs_lock(request->fl_file, request);
4510 if (status < 0)
4511 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004512 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004513 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004514 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004515 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004516 request->fl_flags = fl_flags & ~FL_SLEEP;
4517 status = do_vfs_lock(request->fl_file, request);
4518 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004519 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004520 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004521 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004522 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004523 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004524 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004525 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004526 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004527out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004528 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004529out:
4530 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return status;
4532}
4533
4534static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4535{
4536 struct nfs4_exception exception = { };
4537 int err;
4538
4539 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004540 err = _nfs4_proc_setlk(state, cmd, request);
4541 if (err == -NFS4ERR_DENIED)
4542 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004544 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 } while (exception.retry);
4546 return err;
4547}
4548
4549static int
4550nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4551{
4552 struct nfs_open_context *ctx;
4553 struct nfs4_state *state;
4554 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4555 int status;
4556
4557 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004558 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 state = ctx->state;
4560
4561 if (request->fl_start < 0 || request->fl_end < 0)
4562 return -EINVAL;
4563
Trond Myklebustd9531262009-07-21 19:22:38 -04004564 if (IS_GETLK(cmd)) {
4565 if (state != NULL)
4566 return nfs4_proc_getlk(state, F_GETLK, request);
4567 return 0;
4568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4571 return -EINVAL;
4572
Trond Myklebustd9531262009-07-21 19:22:38 -04004573 if (request->fl_type == F_UNLCK) {
4574 if (state != NULL)
4575 return nfs4_proc_unlck(state, cmd, request);
4576 return 0;
4577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Trond Myklebustd9531262009-07-21 19:22:38 -04004579 if (state == NULL)
4580 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 do {
4582 status = nfs4_proc_setlk(state, cmd, request);
4583 if ((status != -EAGAIN) || IS_SETLK(cmd))
4584 break;
4585 timeout = nfs4_set_lock_task_retry(timeout);
4586 status = -ERESTARTSYS;
4587 if (signalled())
4588 break;
4589 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return status;
4591}
4592
Trond Myklebust888e6942005-11-04 15:38:11 -05004593int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4594{
4595 struct nfs_server *server = NFS_SERVER(state->inode);
4596 struct nfs4_exception exception = { };
4597 int err;
4598
4599 err = nfs4_set_lock_state(state, fl);
4600 if (err != 0)
4601 goto out;
4602 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004603 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004604 switch (err) {
4605 default:
4606 printk(KERN_ERR "%s: unhandled error %d.\n",
4607 __func__, err);
4608 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004609 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004610 goto out;
4611 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004612 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004613 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004614 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004615 nfs4_schedule_lease_recovery(server->nfs_client);
4616 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004617 case -NFS4ERR_BADSESSION:
4618 case -NFS4ERR_BADSLOT:
4619 case -NFS4ERR_BAD_HIGH_SLOT:
4620 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4621 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004622 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004623 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004624 case -ERESTARTSYS:
4625 /*
4626 * The show must go on: exit, but mark the
4627 * stateid as needing recovery.
4628 */
4629 case -NFS4ERR_ADMIN_REVOKED:
4630 case -NFS4ERR_BAD_STATEID:
4631 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004632 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004633 err = 0;
4634 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004635 case -EKEYEXPIRED:
4636 /*
4637 * User RPCSEC_GSS context has expired.
4638 * We cannot recover this stateid now, so
4639 * skip it and allow recovery thread to
4640 * proceed.
4641 */
4642 err = 0;
4643 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004644 case -ENOMEM:
4645 case -NFS4ERR_DENIED:
4646 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4647 err = 0;
4648 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004649 case -NFS4ERR_DELAY:
4650 break;
4651 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004652 err = nfs4_handle_exception(server, err, &exception);
4653 } while (exception.retry);
4654out:
4655 return err;
4656}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004657
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004658static void nfs4_release_lockowner_release(void *calldata)
4659{
4660 kfree(calldata);
4661}
4662
4663const struct rpc_call_ops nfs4_release_lockowner_ops = {
4664 .rpc_release = nfs4_release_lockowner_release,
4665};
4666
4667void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4668{
4669 struct nfs_server *server = lsp->ls_state->owner->so_server;
4670 struct nfs_release_lockowner_args *args;
4671 struct rpc_message msg = {
4672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4673 };
4674
4675 if (server->nfs_client->cl_mvops->minor_version != 0)
4676 return;
4677 args = kmalloc(sizeof(*args), GFP_NOFS);
4678 if (!args)
4679 return;
4680 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4681 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004682 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004683 msg.rpc_argp = args;
4684 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4685}
4686
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004687#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4688
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004689static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4690 const void *buf, size_t buflen,
4691 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004692{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004693 if (strcmp(key, "") != 0)
4694 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004695
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004696 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004697}
4698
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004699static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4700 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004701{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004702 if (strcmp(key, "") != 0)
4703 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004704
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004705 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004706}
4707
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004708static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4709 size_t list_len, const char *name,
4710 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004711{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004712 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004713
J. Bruce Fields096455a2006-03-20 23:23:42 -05004714 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4715 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004716
4717 if (list && len <= list_len)
4718 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004719 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004720}
4721
Andy Adamson533eb462011-06-13 18:25:56 -04004722/*
4723 * nfs_fhget will use either the mounted_on_fileid or the fileid
4724 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004725static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4726{
Andy Adamson533eb462011-06-13 18:25:56 -04004727 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4728 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4729 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4730 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004731 return;
4732
4733 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4734 NFS_ATTR_FATTR_NLINK;
4735 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4736 fattr->nlink = 2;
4737}
4738
Trond Myklebust56659e92007-07-17 21:52:39 -04004739int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004740 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004741{
4742 struct nfs_server *server = NFS_SERVER(dir);
4743 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004744 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004745 };
4746 struct nfs4_fs_locations_arg args = {
4747 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004748 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004749 .page = page,
4750 .bitmask = bitmask,
4751 };
Benny Halevy22958462009-04-01 09:22:02 -04004752 struct nfs4_fs_locations_res res = {
4753 .fs_locations = fs_locations,
4754 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4757 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004758 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004759 };
4760 int status;
4761
Harvey Harrison3110ff82008-05-02 13:42:44 -07004762 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004763
4764 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4765 * is not supported */
4766 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4767 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4768 else
4769 bitmask[0] |= FATTR4_WORD0_FILEID;
4770
Trond Myklebustc228fd32007-01-13 02:28:11 -05004771 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004772 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004773 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004774 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004775 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004776 return status;
4777}
4778
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004779static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4780{
4781 int status;
4782 struct nfs4_secinfo_arg args = {
4783 .dir_fh = NFS_FH(dir),
4784 .name = name,
4785 };
4786 struct nfs4_secinfo_res res = {
4787 .flavors = flavors,
4788 };
4789 struct rpc_message msg = {
4790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4791 .rpc_argp = &args,
4792 .rpc_resp = &res,
4793 };
4794
4795 dprintk("NFS call secinfo %s\n", name->name);
4796 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4797 dprintk("NFS reply secinfo: %d\n", status);
4798 return status;
4799}
4800
4801int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4802{
4803 struct nfs4_exception exception = { };
4804 int err;
4805 do {
4806 err = nfs4_handle_exception(NFS_SERVER(dir),
4807 _nfs4_proc_secinfo(dir, name, flavors),
4808 &exception);
4809 } while (exception.retry);
4810 return err;
4811}
4812
Andy Adamson557134a2009-04-01 09:21:53 -04004813#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004814/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004815 * Check the exchange flags returned by the server for invalid flags, having
4816 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4817 * DS flags set.
4818 */
4819static int nfs4_check_cl_exchange_flags(u32 flags)
4820{
4821 if (flags & ~EXCHGID4_FLAG_MASK_R)
4822 goto out_inval;
4823 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4824 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4825 goto out_inval;
4826 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4827 goto out_inval;
4828 return NFS_OK;
4829out_inval:
4830 return -NFS4ERR_INVAL;
4831}
4832
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004833static bool
4834nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4835{
4836 if (a->server_scope_sz == b->server_scope_sz &&
4837 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4838 return true;
4839
4840 return false;
4841}
4842
Andy Adamson357f54d2010-12-14 10:11:57 -05004843/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004844 * nfs4_proc_exchange_id()
4845 *
4846 * Since the clientid has expired, all compounds using sessions
4847 * associated with the stale clientid will be returning
4848 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4849 * be in some phase of session reset.
4850 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004851int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004852{
4853 nfs4_verifier verifier;
4854 struct nfs41_exchange_id_args args = {
4855 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004856 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004857 };
4858 struct nfs41_exchange_id_res res = {
4859 .client = clp,
4860 };
4861 int status;
4862 struct rpc_message msg = {
4863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4864 .rpc_argp = &args,
4865 .rpc_resp = &res,
4866 .rpc_cred = cred,
4867 };
4868 __be32 *p;
4869
4870 dprintk("--> %s\n", __func__);
4871 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004872
Benny Halevy99fe60d2009-04-01 09:22:29 -04004873 p = (u32 *)verifier.data;
4874 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4875 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4876 args.verifier = &verifier;
4877
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004878 args.id_len = scnprintf(args.id, sizeof(args.id),
4879 "%s/%s.%s/%u",
4880 clp->cl_ipaddr,
4881 init_utsname()->nodename,
4882 init_utsname()->domainname,
4883 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004884
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004885 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05004886 if (unlikely(!res.server_scope)) {
4887 status = -ENOMEM;
4888 goto out;
4889 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004890
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004891 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004892 if (!status)
4893 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004894
4895 if (!status) {
4896 if (clp->server_scope &&
4897 !nfs41_same_server_scope(clp->server_scope,
4898 res.server_scope)) {
4899 dprintk("%s: server_scope mismatch detected\n",
4900 __func__);
4901 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4902 kfree(clp->server_scope);
4903 clp->server_scope = NULL;
4904 }
4905
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05004906 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004907 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05004908 goto out;
4909 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004910 }
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05004911 kfree(res.server_scope);
4912out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04004913 dprintk("<-- %s status= %d\n", __func__, status);
4914 return status;
4915}
4916
Andy Adamson2050f0c2009-04-01 09:22:30 -04004917struct nfs4_get_lease_time_data {
4918 struct nfs4_get_lease_time_args *args;
4919 struct nfs4_get_lease_time_res *res;
4920 struct nfs_client *clp;
4921};
4922
4923static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4924 void *calldata)
4925{
4926 int ret;
4927 struct nfs4_get_lease_time_data *data =
4928 (struct nfs4_get_lease_time_data *)calldata;
4929
4930 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004931 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004932 /* just setup sequence, do not trigger session recovery
4933 since we're invoked within one */
4934 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004935 &data->args->la_seq_args,
4936 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004937
4938 BUG_ON(ret == -EAGAIN);
4939 rpc_call_start(task);
4940 dprintk("<-- %s\n", __func__);
4941}
4942
4943/*
4944 * Called from nfs4_state_manager thread for session setup, so don't recover
4945 * from sequence operation or clientid errors.
4946 */
4947static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4948{
4949 struct nfs4_get_lease_time_data *data =
4950 (struct nfs4_get_lease_time_data *)calldata;
4951
4952 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004953 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4954 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004955 switch (task->tk_status) {
4956 case -NFS4ERR_DELAY:
4957 case -NFS4ERR_GRACE:
4958 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4959 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4960 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004961 /* fall through */
4962 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004963 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004964 return;
4965 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004966 dprintk("<-- %s\n", __func__);
4967}
4968
4969struct rpc_call_ops nfs4_get_lease_time_ops = {
4970 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4971 .rpc_call_done = nfs4_get_lease_time_done,
4972};
4973
4974int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4975{
4976 struct rpc_task *task;
4977 struct nfs4_get_lease_time_args args;
4978 struct nfs4_get_lease_time_res res = {
4979 .lr_fsinfo = fsinfo,
4980 };
4981 struct nfs4_get_lease_time_data data = {
4982 .args = &args,
4983 .res = &res,
4984 .clp = clp,
4985 };
4986 struct rpc_message msg = {
4987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4988 .rpc_argp = &args,
4989 .rpc_resp = &res,
4990 };
4991 struct rpc_task_setup task_setup = {
4992 .rpc_client = clp->cl_rpcclient,
4993 .rpc_message = &msg,
4994 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004995 .callback_data = &data,
4996 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004997 };
4998 int status;
4999
Andy Adamson2050f0c2009-04-01 09:22:30 -04005000 dprintk("--> %s\n", __func__);
5001 task = rpc_run_task(&task_setup);
5002
5003 if (IS_ERR(task))
5004 status = PTR_ERR(task);
5005 else {
5006 status = task->tk_status;
5007 rpc_put_task(task);
5008 }
5009 dprintk("<-- %s return %d\n", __func__, status);
5010
5011 return status;
5012}
5013
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005014static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5015{
5016 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5017}
5018
5019static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5020 struct nfs4_slot *new,
5021 u32 max_slots,
5022 u32 ivalue)
5023{
5024 struct nfs4_slot *old = NULL;
5025 u32 i;
5026
5027 spin_lock(&tbl->slot_tbl_lock);
5028 if (new) {
5029 old = tbl->slots;
5030 tbl->slots = new;
5031 tbl->max_slots = max_slots;
5032 }
5033 tbl->highest_used_slotid = -1; /* no slot is currently used */
5034 for (i = 0; i < tbl->max_slots; i++)
5035 tbl->slots[i].seq_nr = ivalue;
5036 spin_unlock(&tbl->slot_tbl_lock);
5037 kfree(old);
5038}
5039
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005040/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005041 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005042 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005043static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5044 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005045{
Andy Adamson104aeba2010-01-14 17:45:10 -05005046 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005047 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005048
Andy Adamson104aeba2010-01-14 17:45:10 -05005049 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5050 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005051
Andy Adamson104aeba2010-01-14 17:45:10 -05005052 /* Does the newly negotiated max_reqs match the existing slot table? */
5053 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005054 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005055 if (!new)
5056 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005057 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005058 ret = 0;
5059
5060 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005061 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5062 tbl, tbl->slots, tbl->max_slots);
5063out:
5064 dprintk("<-- %s: return %d\n", __func__, ret);
5065 return ret;
5066}
5067
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005068/* Destroy the slot table */
5069static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5070{
5071 if (session->fc_slot_table.slots != NULL) {
5072 kfree(session->fc_slot_table.slots);
5073 session->fc_slot_table.slots = NULL;
5074 }
5075 if (session->bc_slot_table.slots != NULL) {
5076 kfree(session->bc_slot_table.slots);
5077 session->bc_slot_table.slots = NULL;
5078 }
5079 return;
5080}
5081
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005082/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005083 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005084 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005085static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005086{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005087 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005088 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005089
Andy Adamsonaacd5532011-11-09 13:58:21 -05005090 dprintk("--> %s\n", __func__);
5091 /* Fore channel */
5092 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005093 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5094 if (status) /* -ENOMEM */
5095 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005096 /* Back channel */
5097 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005098 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5099 if (status && tbl->slots == NULL)
5100 /* Fore and back channel share a connection so get
5101 * both slot tables or neither */
5102 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005103 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005104}
5105
5106struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5107{
5108 struct nfs4_session *session;
5109 struct nfs4_slot_table *tbl;
5110
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005111 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005112 if (!session)
5113 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005114
Andy Adamson557134a2009-04-01 09:21:53 -04005115 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005116 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005117 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005118 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005119 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005120
5121 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005122 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005123 spin_lock_init(&tbl->slot_tbl_lock);
5124 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005125 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005126
Trond Myklebust1055d762010-06-16 09:52:27 -04005127 session->session_state = 1<<NFS4_SESSION_INITING;
5128
Andy Adamson557134a2009-04-01 09:21:53 -04005129 session->clp = clp;
5130 return session;
5131}
5132
5133void nfs4_destroy_session(struct nfs4_session *session)
5134{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005135 nfs4_proc_destroy_session(session);
5136 dprintk("%s Destroy backchannel for xprt %p\n",
5137 __func__, session->clp->cl_rpcclient->cl_xprt);
5138 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5139 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005140 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005141 kfree(session);
5142}
5143
Andy Adamsonfc931582009-04-01 09:22:31 -04005144/*
5145 * Initialize the values to be used by the client in CREATE_SESSION
5146 * If nfs4_init_session set the fore channel request and response sizes,
5147 * use them.
5148 *
5149 * Set the back channel max_resp_sz_cached to zero to force the client to
5150 * always set csa_cachethis to FALSE because the current implementation
5151 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5152 */
5153static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5154{
5155 struct nfs4_session *session = args->client->cl_session;
5156 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5157 mxresp_sz = session->fc_attrs.max_resp_sz;
5158
5159 if (mxrqst_sz == 0)
5160 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5161 if (mxresp_sz == 0)
5162 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5163 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005164 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5165 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005166 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5167 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5168
5169 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005170 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005171 __func__,
5172 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005173 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005174
5175 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005176 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5177 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5178 args->bc_attrs.max_resp_sz_cached = 0;
5179 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5180 args->bc_attrs.max_reqs = 1;
5181
5182 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5183 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5184 __func__,
5185 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5186 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5187 args->bc_attrs.max_reqs);
5188}
5189
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005190static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005191{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005192 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5193 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5194
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005195 if (rcvd->max_resp_sz > sent->max_resp_sz)
5196 return -EINVAL;
5197 /*
5198 * Our requested max_ops is the minimum we need; we're not
5199 * prepared to break up compounds into smaller pieces than that.
5200 * So, no point even trying to continue if the server won't
5201 * cooperate:
5202 */
5203 if (rcvd->max_ops < sent->max_ops)
5204 return -EINVAL;
5205 if (rcvd->max_reqs == 0)
5206 return -EINVAL;
5207 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005208}
5209
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005210static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5211{
5212 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5213 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005214
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005215 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5216 return -EINVAL;
5217 if (rcvd->max_resp_sz < sent->max_resp_sz)
5218 return -EINVAL;
5219 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5220 return -EINVAL;
5221 /* These would render the backchannel useless: */
5222 if (rcvd->max_ops == 0)
5223 return -EINVAL;
5224 if (rcvd->max_reqs == 0)
5225 return -EINVAL;
5226 return 0;
5227}
Andy Adamson8d353012009-04-01 09:22:32 -04005228
Andy Adamson8d353012009-04-01 09:22:32 -04005229static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5230 struct nfs4_session *session)
5231{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005232 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005233
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005234 ret = nfs4_verify_fore_channel_attrs(args, session);
5235 if (ret)
5236 return ret;
5237 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005238}
5239
Andy Adamsonfc931582009-04-01 09:22:31 -04005240static int _nfs4_proc_create_session(struct nfs_client *clp)
5241{
5242 struct nfs4_session *session = clp->cl_session;
5243 struct nfs41_create_session_args args = {
5244 .client = clp,
5245 .cb_program = NFS4_CALLBACK,
5246 };
5247 struct nfs41_create_session_res res = {
5248 .client = clp,
5249 };
5250 struct rpc_message msg = {
5251 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5252 .rpc_argp = &args,
5253 .rpc_resp = &res,
5254 };
5255 int status;
5256
5257 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005258 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005259
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005260 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005261
Andy Adamson8d353012009-04-01 09:22:32 -04005262 if (!status)
5263 /* Verify the session's negotiated channel_attrs values */
5264 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005265 if (!status) {
5266 /* Increment the clientid slot sequence id */
5267 clp->cl_seqid++;
5268 }
5269
5270 return status;
5271}
5272
5273/*
5274 * Issues a CREATE_SESSION operation to the server.
5275 * It is the responsibility of the caller to verify the session is
5276 * expired before calling this routine.
5277 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005278int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005279{
5280 int status;
5281 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005282 struct nfs4_session *session = clp->cl_session;
5283
5284 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5285
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005286 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005287 if (status)
5288 goto out;
5289
Andy Adamsonaacd5532011-11-09 13:58:21 -05005290 /* Init or reset the session slot tables */
5291 status = nfs4_setup_session_slot_tables(session);
5292 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005293 if (status)
5294 goto out;
5295
5296 ptr = (unsigned *)&session->sess_id.data[0];
5297 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5298 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005299out:
5300 dprintk("<-- %s\n", __func__);
5301 return status;
5302}
5303
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005304/*
5305 * Issue the over-the-wire RPC DESTROY_SESSION.
5306 * The caller must serialize access to this routine.
5307 */
5308int nfs4_proc_destroy_session(struct nfs4_session *session)
5309{
5310 int status = 0;
5311 struct rpc_message msg;
5312
5313 dprintk("--> nfs4_proc_destroy_session\n");
5314
5315 /* session is still being setup */
5316 if (session->clp->cl_cons_state != NFS_CS_READY)
5317 return status;
5318
5319 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5320 msg.rpc_argp = session;
5321 msg.rpc_resp = NULL;
5322 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005323 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005324
5325 if (status)
5326 printk(KERN_WARNING
5327 "Got error %d from the server on DESTROY_SESSION. "
5328 "Session has been destroyed regardless...\n", status);
5329
5330 dprintk("<-- nfs4_proc_destroy_session\n");
5331 return status;
5332}
5333
Trond Myklebustfccba802009-07-21 16:48:07 -04005334int nfs4_init_session(struct nfs_server *server)
5335{
5336 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005337 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005338 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005339 int ret;
5340
5341 if (!nfs4_has_session(clp))
5342 return 0;
5343
Trond Myklebust1055d762010-06-16 09:52:27 -04005344 session = clp->cl_session;
5345 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5346 return 0;
5347
Andy Adamson68bf05e2009-12-15 12:55:02 -05005348 rsize = server->rsize;
5349 if (rsize == 0)
5350 rsize = NFS_MAX_FILE_IO_SIZE;
5351 wsize = server->wsize;
5352 if (wsize == 0)
5353 wsize = NFS_MAX_FILE_IO_SIZE;
5354
Andy Adamson68bf05e2009-12-15 12:55:02 -05005355 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5356 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005357
Trond Myklebustfccba802009-07-21 16:48:07 -04005358 ret = nfs4_recover_expired_lease(server);
5359 if (!ret)
5360 ret = nfs4_check_client_ready(clp);
5361 return ret;
5362}
5363
Andy Adamsond83217c2011-03-01 01:34:17 +00005364int nfs4_init_ds_session(struct nfs_client *clp)
5365{
5366 struct nfs4_session *session = clp->cl_session;
5367 int ret;
5368
5369 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5370 return 0;
5371
5372 ret = nfs4_client_recover_expired_lease(clp);
5373 if (!ret)
5374 /* Test for the DS role */
5375 if (!is_ds_client(clp))
5376 ret = -ENODEV;
5377 if (!ret)
5378 ret = nfs4_check_client_ready(clp);
5379 return ret;
5380
5381}
5382EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5383
5384
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005385/*
5386 * Renew the cl_session lease.
5387 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005388struct nfs4_sequence_data {
5389 struct nfs_client *clp;
5390 struct nfs4_sequence_args args;
5391 struct nfs4_sequence_res res;
5392};
5393
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005394static void nfs41_sequence_release(void *data)
5395{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005396 struct nfs4_sequence_data *calldata = data;
5397 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005398
Alexandros Batsakis71358402010-02-05 03:45:05 -08005399 if (atomic_read(&clp->cl_count) > 1)
5400 nfs4_schedule_state_renewal(clp);
5401 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005402 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005403}
5404
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005405static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5406{
5407 switch(task->tk_status) {
5408 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005409 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5410 return -EAGAIN;
5411 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005412 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005413 }
5414 return 0;
5415}
5416
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005417static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005418{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005419 struct nfs4_sequence_data *calldata = data;
5420 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005421
Trond Myklebust14516c32010-07-31 14:29:06 -04005422 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5423 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005424
5425 if (task->tk_status < 0) {
5426 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005427 if (atomic_read(&clp->cl_count) == 1)
5428 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005429
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005430 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5431 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005432 return;
5433 }
5434 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005435 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005436out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005437 dprintk("<-- %s\n", __func__);
5438}
5439
5440static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5441{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005442 struct nfs4_sequence_data *calldata = data;
5443 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005444 struct nfs4_sequence_args *args;
5445 struct nfs4_sequence_res *res;
5446
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005447 args = task->tk_msg.rpc_argp;
5448 res = task->tk_msg.rpc_resp;
5449
Trond Myklebust035168a2010-06-16 09:52:26 -04005450 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005451 return;
5452 rpc_call_start(task);
5453}
5454
5455static const struct rpc_call_ops nfs41_sequence_ops = {
5456 .rpc_call_done = nfs41_sequence_call_done,
5457 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005458 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005459};
5460
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005461static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005462{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005463 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005464 struct rpc_message msg = {
5465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5466 .rpc_cred = cred,
5467 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005468 struct rpc_task_setup task_setup_data = {
5469 .rpc_client = clp->cl_rpcclient,
5470 .rpc_message = &msg,
5471 .callback_ops = &nfs41_sequence_ops,
5472 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5473 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005474
Alexandros Batsakis71358402010-02-05 03:45:05 -08005475 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005476 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005477 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005478 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005479 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005480 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005481 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005482 msg.rpc_argp = &calldata->args;
5483 msg.rpc_resp = &calldata->res;
5484 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005485 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005486
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005487 return rpc_run_task(&task_setup_data);
5488}
5489
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005490static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005491{
5492 struct rpc_task *task;
5493 int ret = 0;
5494
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005495 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5496 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005497 task = _nfs41_proc_sequence(clp, cred);
5498 if (IS_ERR(task))
5499 ret = PTR_ERR(task);
5500 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005501 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005502 dprintk("<-- %s status=%d\n", __func__, ret);
5503 return ret;
5504}
5505
5506static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5507{
5508 struct rpc_task *task;
5509 int ret;
5510
5511 task = _nfs41_proc_sequence(clp, cred);
5512 if (IS_ERR(task)) {
5513 ret = PTR_ERR(task);
5514 goto out;
5515 }
5516 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005517 if (!ret) {
5518 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5519
5520 if (task->tk_status == 0)
5521 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005522 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005523 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005524 rpc_put_task(task);
5525out:
5526 dprintk("<-- %s status=%d\n", __func__, ret);
5527 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005528}
5529
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005530struct nfs4_reclaim_complete_data {
5531 struct nfs_client *clp;
5532 struct nfs41_reclaim_complete_args arg;
5533 struct nfs41_reclaim_complete_res res;
5534};
5535
5536static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5537{
5538 struct nfs4_reclaim_complete_data *calldata = data;
5539
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005540 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005541 if (nfs41_setup_sequence(calldata->clp->cl_session,
5542 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005543 &calldata->res.seq_res, 0, task))
5544 return;
5545
5546 rpc_call_start(task);
5547}
5548
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005549static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5550{
5551 switch(task->tk_status) {
5552 case 0:
5553 case -NFS4ERR_COMPLETE_ALREADY:
5554 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5555 break;
5556 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005557 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005558 /* fall through */
5559 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005560 return -EAGAIN;
5561 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005562 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005563 }
5564 return 0;
5565}
5566
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005567static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5568{
5569 struct nfs4_reclaim_complete_data *calldata = data;
5570 struct nfs_client *clp = calldata->clp;
5571 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5572
5573 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005574 if (!nfs41_sequence_done(task, res))
5575 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005576
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005577 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5578 rpc_restart_call_prepare(task);
5579 return;
5580 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005581 dprintk("<-- %s\n", __func__);
5582}
5583
5584static void nfs4_free_reclaim_complete_data(void *data)
5585{
5586 struct nfs4_reclaim_complete_data *calldata = data;
5587
5588 kfree(calldata);
5589}
5590
5591static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5592 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5593 .rpc_call_done = nfs4_reclaim_complete_done,
5594 .rpc_release = nfs4_free_reclaim_complete_data,
5595};
5596
5597/*
5598 * Issue a global reclaim complete.
5599 */
5600static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5601{
5602 struct nfs4_reclaim_complete_data *calldata;
5603 struct rpc_task *task;
5604 struct rpc_message msg = {
5605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5606 };
5607 struct rpc_task_setup task_setup_data = {
5608 .rpc_client = clp->cl_rpcclient,
5609 .rpc_message = &msg,
5610 .callback_ops = &nfs4_reclaim_complete_call_ops,
5611 .flags = RPC_TASK_ASYNC,
5612 };
5613 int status = -ENOMEM;
5614
5615 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005616 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005617 if (calldata == NULL)
5618 goto out;
5619 calldata->clp = clp;
5620 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005621
5622 msg.rpc_argp = &calldata->arg;
5623 msg.rpc_resp = &calldata->res;
5624 task_setup_data.callback_data = calldata;
5625 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005626 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005627 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005628 goto out;
5629 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005630 status = nfs4_wait_for_completion_rpc_task(task);
5631 if (status == 0)
5632 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005633 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005634 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005635out:
5636 dprintk("<-- %s status=%d\n", __func__, status);
5637 return status;
5638}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005639
5640static void
5641nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5642{
5643 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005644 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005645
5646 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005647 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5648 * right now covering the LAYOUTGET we are about to send.
5649 * However, that is not so catastrophic, and there seems
5650 * to be no way to prevent it completely.
5651 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005652 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5653 &lgp->res.seq_res, 0, task))
5654 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005655 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5656 NFS_I(lgp->args.inode)->layout,
5657 lgp->args.ctx->state)) {
5658 rpc_exit(task, NFS4_OK);
5659 return;
5660 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005661 rpc_call_start(task);
5662}
5663
5664static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5665{
5666 struct nfs4_layoutget *lgp = calldata;
5667 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5668
5669 dprintk("--> %s\n", __func__);
5670
5671 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5672 return;
5673
5674 switch (task->tk_status) {
5675 case 0:
5676 break;
5677 case -NFS4ERR_LAYOUTTRYLATER:
5678 case -NFS4ERR_RECALLCONFLICT:
5679 task->tk_status = -NFS4ERR_DELAY;
5680 /* Fall through */
5681 default:
5682 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5683 rpc_restart_call_prepare(task);
5684 return;
5685 }
5686 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005687 dprintk("<-- %s\n", __func__);
5688}
5689
5690static void nfs4_layoutget_release(void *calldata)
5691{
5692 struct nfs4_layoutget *lgp = calldata;
5693
5694 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005695 put_nfs_open_context(lgp->args.ctx);
5696 kfree(calldata);
5697 dprintk("<-- %s\n", __func__);
5698}
5699
5700static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5701 .rpc_call_prepare = nfs4_layoutget_prepare,
5702 .rpc_call_done = nfs4_layoutget_done,
5703 .rpc_release = nfs4_layoutget_release,
5704};
5705
5706int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5707{
5708 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5709 struct rpc_task *task;
5710 struct rpc_message msg = {
5711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5712 .rpc_argp = &lgp->args,
5713 .rpc_resp = &lgp->res,
5714 };
5715 struct rpc_task_setup task_setup_data = {
5716 .rpc_client = server->client,
5717 .rpc_message = &msg,
5718 .callback_ops = &nfs4_layoutget_call_ops,
5719 .callback_data = lgp,
5720 .flags = RPC_TASK_ASYNC,
5721 };
5722 int status = 0;
5723
5724 dprintk("--> %s\n", __func__);
5725
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005726 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005727 lgp->res.seq_res.sr_slot = NULL;
5728 task = rpc_run_task(&task_setup_data);
5729 if (IS_ERR(task))
5730 return PTR_ERR(task);
5731 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005732 if (status == 0)
5733 status = task->tk_status;
5734 if (status == 0)
5735 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005736 rpc_put_task(task);
5737 dprintk("<-- %s status=%d\n", __func__, status);
5738 return status;
5739}
5740
Benny Halevycbe82602011-05-22 19:52:37 +03005741static void
5742nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5743{
5744 struct nfs4_layoutreturn *lrp = calldata;
5745
5746 dprintk("--> %s\n", __func__);
5747 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5748 &lrp->res.seq_res, 0, task))
5749 return;
5750 rpc_call_start(task);
5751}
5752
5753static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5754{
5755 struct nfs4_layoutreturn *lrp = calldata;
5756 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005757 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005758
5759 dprintk("--> %s\n", __func__);
5760
5761 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5762 return;
5763
5764 server = NFS_SERVER(lrp->args.inode);
5765 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005766 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005767 return;
5768 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005769 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005770 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005771 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005772 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005773 } else
5774 BUG_ON(!list_empty(&lo->plh_segs));
5775 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005776 lo->plh_block_lgets--;
5777 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005778 dprintk("<-- %s\n", __func__);
5779}
5780
5781static void nfs4_layoutreturn_release(void *calldata)
5782{
5783 struct nfs4_layoutreturn *lrp = calldata;
5784
5785 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005786 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005787 kfree(calldata);
5788 dprintk("<-- %s\n", __func__);
5789}
5790
5791static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5792 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5793 .rpc_call_done = nfs4_layoutreturn_done,
5794 .rpc_release = nfs4_layoutreturn_release,
5795};
5796
5797int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5798{
5799 struct rpc_task *task;
5800 struct rpc_message msg = {
5801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5802 .rpc_argp = &lrp->args,
5803 .rpc_resp = &lrp->res,
5804 };
5805 struct rpc_task_setup task_setup_data = {
5806 .rpc_client = lrp->clp->cl_rpcclient,
5807 .rpc_message = &msg,
5808 .callback_ops = &nfs4_layoutreturn_call_ops,
5809 .callback_data = lrp,
5810 };
5811 int status;
5812
5813 dprintk("--> %s\n", __func__);
5814 task = rpc_run_task(&task_setup_data);
5815 if (IS_ERR(task))
5816 return PTR_ERR(task);
5817 status = task->tk_status;
5818 dprintk("<-- %s status=%d\n", __func__, status);
5819 rpc_put_task(task);
5820 return status;
5821}
5822
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005823/*
5824 * Retrieve the list of Data Server devices from the MDS.
5825 */
5826static int _nfs4_getdevicelist(struct nfs_server *server,
5827 const struct nfs_fh *fh,
5828 struct pnfs_devicelist *devlist)
5829{
5830 struct nfs4_getdevicelist_args args = {
5831 .fh = fh,
5832 .layoutclass = server->pnfs_curr_ld->id,
5833 };
5834 struct nfs4_getdevicelist_res res = {
5835 .devlist = devlist,
5836 };
5837 struct rpc_message msg = {
5838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5839 .rpc_argp = &args,
5840 .rpc_resp = &res,
5841 };
5842 int status;
5843
5844 dprintk("--> %s\n", __func__);
5845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5846 &res.seq_res, 0);
5847 dprintk("<-- %s status=%d\n", __func__, status);
5848 return status;
5849}
5850
5851int nfs4_proc_getdevicelist(struct nfs_server *server,
5852 const struct nfs_fh *fh,
5853 struct pnfs_devicelist *devlist)
5854{
5855 struct nfs4_exception exception = { };
5856 int err;
5857
5858 do {
5859 err = nfs4_handle_exception(server,
5860 _nfs4_getdevicelist(server, fh, devlist),
5861 &exception);
5862 } while (exception.retry);
5863
5864 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5865 err, devlist->num_devs);
5866
5867 return err;
5868}
5869EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5870
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005871static int
5872_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5873{
5874 struct nfs4_getdeviceinfo_args args = {
5875 .pdev = pdev,
5876 };
5877 struct nfs4_getdeviceinfo_res res = {
5878 .pdev = pdev,
5879 };
5880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5882 .rpc_argp = &args,
5883 .rpc_resp = &res,
5884 };
5885 int status;
5886
5887 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005889 dprintk("<-- %s status=%d\n", __func__, status);
5890
5891 return status;
5892}
5893
5894int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5895{
5896 struct nfs4_exception exception = { };
5897 int err;
5898
5899 do {
5900 err = nfs4_handle_exception(server,
5901 _nfs4_proc_getdeviceinfo(server, pdev),
5902 &exception);
5903 } while (exception.retry);
5904 return err;
5905}
5906EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5907
Andy Adamson863a3c62011-03-23 13:27:54 +00005908static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5909{
5910 struct nfs4_layoutcommit_data *data = calldata;
5911 struct nfs_server *server = NFS_SERVER(data->args.inode);
5912
5913 if (nfs4_setup_sequence(server, &data->args.seq_args,
5914 &data->res.seq_res, 1, task))
5915 return;
5916 rpc_call_start(task);
5917}
5918
5919static void
5920nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5921{
5922 struct nfs4_layoutcommit_data *data = calldata;
5923 struct nfs_server *server = NFS_SERVER(data->args.inode);
5924
5925 if (!nfs4_sequence_done(task, &data->res.seq_res))
5926 return;
5927
5928 switch (task->tk_status) { /* Just ignore these failures */
5929 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5930 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5931 case NFS4ERR_BADLAYOUT: /* no layout */
5932 case NFS4ERR_GRACE: /* loca_recalim always false */
5933 task->tk_status = 0;
5934 }
5935
5936 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005937 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005938 return;
5939 }
5940
5941 if (task->tk_status == 0)
5942 nfs_post_op_update_inode_force_wcc(data->args.inode,
5943 data->res.fattr);
5944}
5945
5946static void nfs4_layoutcommit_release(void *calldata)
5947{
5948 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005949 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005950 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005951
Andy Adamsondb29c082011-07-30 20:52:38 -04005952 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005953 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005954 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5955 list_del_init(&lseg->pls_lc_list);
5956 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5957 &lseg->pls_flags))
5958 put_lseg(lseg);
5959 }
Peng Tao92407e72011-10-23 20:21:17 -07005960
5961 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5962 smp_mb__after_clear_bit();
5963 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5964
Andy Adamson863a3c62011-03-23 13:27:54 +00005965 put_rpccred(data->cred);
5966 kfree(data);
5967}
5968
5969static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5970 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5971 .rpc_call_done = nfs4_layoutcommit_done,
5972 .rpc_release = nfs4_layoutcommit_release,
5973};
5974
5975int
Andy Adamsonef311532011-03-12 02:58:10 -05005976nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005977{
5978 struct rpc_message msg = {
5979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5980 .rpc_argp = &data->args,
5981 .rpc_resp = &data->res,
5982 .rpc_cred = data->cred,
5983 };
5984 struct rpc_task_setup task_setup_data = {
5985 .task = &data->task,
5986 .rpc_client = NFS_CLIENT(data->args.inode),
5987 .rpc_message = &msg,
5988 .callback_ops = &nfs4_layoutcommit_ops,
5989 .callback_data = data,
5990 .flags = RPC_TASK_ASYNC,
5991 };
5992 struct rpc_task *task;
5993 int status = 0;
5994
5995 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5996 "lbw: %llu inode %lu\n",
5997 data->task.tk_pid, sync,
5998 data->args.lastbytewritten,
5999 data->args.inode->i_ino);
6000
6001 task = rpc_run_task(&task_setup_data);
6002 if (IS_ERR(task))
6003 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006004 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006005 goto out;
6006 status = nfs4_wait_for_completion_rpc_task(task);
6007 if (status != 0)
6008 goto out;
6009 status = task->tk_status;
6010out:
6011 dprintk("%s: status %d\n", __func__, status);
6012 rpc_put_task(task);
6013 return status;
6014}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006015
6016static int
6017_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6018 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6019{
6020 struct nfs41_secinfo_no_name_args args = {
6021 .style = SECINFO_STYLE_CURRENT_FH,
6022 };
6023 struct nfs4_secinfo_res res = {
6024 .flavors = flavors,
6025 };
6026 struct rpc_message msg = {
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6028 .rpc_argp = &args,
6029 .rpc_resp = &res,
6030 };
6031 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6032}
6033
6034static int
6035nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6036 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6037{
6038 struct nfs4_exception exception = { };
6039 int err;
6040 do {
6041 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6042 switch (err) {
6043 case 0:
6044 case -NFS4ERR_WRONGSEC:
6045 case -NFS4ERR_NOTSUPP:
6046 break;
6047 default:
6048 err = nfs4_handle_exception(server, err, &exception);
6049 }
6050 } while (exception.retry);
6051 return err;
6052}
6053
6054static int
6055nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6056 struct nfs_fsinfo *info)
6057{
6058 int err;
6059 struct page *page;
6060 rpc_authflavor_t flavor;
6061 struct nfs4_secinfo_flavors *flavors;
6062
6063 page = alloc_page(GFP_KERNEL);
6064 if (!page) {
6065 err = -ENOMEM;
6066 goto out;
6067 }
6068
6069 flavors = page_address(page);
6070 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6071
6072 /*
6073 * Fall back on "guess and check" method if
6074 * the server doesn't support SECINFO_NO_NAME
6075 */
6076 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6077 err = nfs4_find_root_sec(server, fhandle, info);
6078 goto out_freepage;
6079 }
6080 if (err)
6081 goto out_freepage;
6082
6083 flavor = nfs_find_best_sec(flavors);
6084 if (err == 0)
6085 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6086
6087out_freepage:
6088 put_page(page);
6089 if (err == -EACCES)
6090 return -EPERM;
6091out:
6092 return err;
6093}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006094static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6095{
6096 int status;
6097 struct nfs41_test_stateid_args args = {
6098 .stateid = &state->stateid,
6099 };
6100 struct nfs41_test_stateid_res res;
6101 struct rpc_message msg = {
6102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6103 .rpc_argp = &args,
6104 .rpc_resp = &res,
6105 };
6106 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6107 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6108 return status;
6109}
6110
6111static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6112{
6113 struct nfs4_exception exception = { };
6114 int err;
6115 do {
6116 err = nfs4_handle_exception(server,
6117 _nfs41_test_stateid(server, state),
6118 &exception);
6119 } while (exception.retry);
6120 return err;
6121}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006122
6123static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6124{
6125 int status;
6126 struct nfs41_free_stateid_args args = {
6127 .stateid = &state->stateid,
6128 };
6129 struct nfs41_free_stateid_res res;
6130 struct rpc_message msg = {
6131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6132 .rpc_argp = &args,
6133 .rpc_resp = &res,
6134 };
6135
6136 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6137 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6138 return status;
6139}
6140
6141static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6142{
6143 struct nfs4_exception exception = { };
6144 int err;
6145 do {
6146 err = nfs4_handle_exception(server,
6147 _nfs4_free_stateid(server, state),
6148 &exception);
6149 } while (exception.retry);
6150 return err;
6151}
Andy Adamson557134a2009-04-01 09:21:53 -04006152#endif /* CONFIG_NFS_V4_1 */
6153
Andy Adamson591d71c2009-04-01 09:22:47 -04006154struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006155 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006156 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 .recover_open = nfs4_open_reclaim,
6158 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006159 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006160 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161};
6162
Andy Adamson591d71c2009-04-01 09:22:47 -04006163#if defined(CONFIG_NFS_V4_1)
6164struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6165 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6166 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6167 .recover_open = nfs4_open_reclaim,
6168 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006169 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006170 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006171 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006172};
6173#endif /* CONFIG_NFS_V4_1 */
6174
6175struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006176 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006177 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 .recover_open = nfs4_open_expired,
6179 .recover_lock = nfs4_lock_expired,
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_nograce_recovery_ops = {
6186 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6187 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006188 .recover_open = nfs41_open_expired,
6189 .recover_lock = nfs41_lock_expired,
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,
Andy Adamson591d71c2009-04-01 09:22:47 -04006192};
6193#endif /* CONFIG_NFS_V4_1 */
6194
Benny Halevy29fba382009-04-01 09:22:44 -04006195struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6196 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006197 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006198 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006199};
6200
6201#if defined(CONFIG_NFS_V4_1)
6202struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6203 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006204 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006205 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006206};
6207#endif
6208
Trond Myklebust97dc1352010-06-16 09:52:26 -04006209static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6210 .minor_version = 0,
6211 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006212 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006213 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006214 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6215 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6216 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006217};
6218
6219#if defined(CONFIG_NFS_V4_1)
6220static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6221 .minor_version = 1,
6222 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006223 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006224 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006225 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6226 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6227 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006228};
6229#endif
6230
Trond Myklebust97dc1352010-06-16 09:52:26 -04006231const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6232 [0] = &nfs_v4_0_minor_ops,
6233#if defined(CONFIG_NFS_V4_1)
6234 [1] = &nfs_v4_1_minor_ops,
6235#endif
6236};
6237
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006238static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006239 .permission = nfs_permission,
6240 .getattr = nfs_getattr,
6241 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006242 .getxattr = generic_getxattr,
6243 .setxattr = generic_setxattr,
6244 .listxattr = generic_listxattr,
6245 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006246};
6247
David Howells509de812006-08-22 20:06:11 -04006248const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 .version = 4, /* protocol version */
6250 .dentry_ops = &nfs4_dentry_operations,
6251 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006252 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006253 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 .getroot = nfs4_proc_get_root,
6255 .getattr = nfs4_proc_getattr,
6256 .setattr = nfs4_proc_setattr,
6257 .lookup = nfs4_proc_lookup,
6258 .access = nfs4_proc_access,
6259 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 .create = nfs4_proc_create,
6261 .remove = nfs4_proc_remove,
6262 .unlink_setup = nfs4_proc_unlink_setup,
6263 .unlink_done = nfs4_proc_unlink_done,
6264 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006265 .rename_setup = nfs4_proc_rename_setup,
6266 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 .link = nfs4_proc_link,
6268 .symlink = nfs4_proc_symlink,
6269 .mkdir = nfs4_proc_mkdir,
6270 .rmdir = nfs4_proc_remove,
6271 .readdir = nfs4_proc_readdir,
6272 .mknod = nfs4_proc_mknod,
6273 .statfs = nfs4_proc_statfs,
6274 .fsinfo = nfs4_proc_fsinfo,
6275 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006276 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 .decode_dirent = nfs4_decode_dirent,
6278 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006279 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006281 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006283 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006285 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006286 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006287 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006288 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006289 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290};
6291
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006292static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6293 .prefix = XATTR_NAME_NFSV4_ACL,
6294 .list = nfs4_xattr_list_nfs4_acl,
6295 .get = nfs4_xattr_get_nfs4_acl,
6296 .set = nfs4_xattr_set_nfs4_acl,
6297};
6298
6299const struct xattr_handler *nfs4_xattr_handlers[] = {
6300 &nfs4_xattr_nfs4_acl_handler,
6301 NULL
6302};
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304/*
6305 * Local variables:
6306 * c-basic-offset: 8
6307 * End:
6308 */