blob: df3d3068242e7b829dd3ea341e95e483700ceb83 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebust4ce79712005-06-22 17:16:21 +000060#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050062#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050063#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040064#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040065#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#define NFSDBG_FACILITY NFSDBG_PROC
68
Trond Myklebust2066fe82006-09-15 08:30:46 -040069#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define NFS4_POLL_RETRY_MAX (15*HZ)
71
Trond Myklebusta78cb572009-08-09 15:06:19 -040072#define NFS4_MAX_LOOP_ON_RECOVER (10)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040079static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81 struct nfs_fattr *fattr, struct iattr *sattr,
82 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040083#ifdef CONFIG_NFS_V4_1
84static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
85static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050088static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Trond Myklebust52567b02009-10-23 14:46:42 -040090 if (err >= -1000)
91 return err;
92 switch (err) {
93 case -NFS4ERR_RESOURCE:
94 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000095 case -NFS4ERR_WRONGSEC:
96 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080097 case -NFS4ERR_BADOWNER:
98 case -NFS4ERR_BADNAME:
99 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700102 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * This is our standard bitmap for GETATTR requests.
110 */
111const u32 nfs4_fattr_bitmap[2] = {
112 FATTR4_WORD0_TYPE
113 | FATTR4_WORD0_CHANGE
114 | FATTR4_WORD0_SIZE
115 | FATTR4_WORD0_FSID
116 | FATTR4_WORD0_FILEID,
117 FATTR4_WORD1_MODE
118 | FATTR4_WORD1_NUMLINKS
119 | FATTR4_WORD1_OWNER
120 | FATTR4_WORD1_OWNER_GROUP
121 | FATTR4_WORD1_RAWDEV
122 | FATTR4_WORD1_SPACE_USED
123 | FATTR4_WORD1_TIME_ACCESS
124 | FATTR4_WORD1_TIME_METADATA
125 | FATTR4_WORD1_TIME_MODIFY
126};
127
128const u32 nfs4_statfs_bitmap[2] = {
129 FATTR4_WORD0_FILES_AVAIL
130 | FATTR4_WORD0_FILES_FREE
131 | FATTR4_WORD0_FILES_TOTAL,
132 FATTR4_WORD1_SPACE_AVAIL
133 | FATTR4_WORD1_SPACE_FREE
134 | FATTR4_WORD1_SPACE_TOTAL
135};
136
Trond Myklebust4ce79712005-06-22 17:16:21 +0000137const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 FATTR4_WORD0_MAXLINK
139 | FATTR4_WORD0_MAXNAME,
140 0
141};
142
Fred Isamandae100c2011-07-30 20:52:37 -0400143const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 | FATTR4_WORD0_MAXREAD
145 | FATTR4_WORD0_MAXWRITE
146 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700147 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400148 | FATTR4_WORD1_FS_LAYOUT_TYPES,
149 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Manoj Naik830b8e32006-06-09 09:34:25 -0400152const u32 nfs4_fs_locations_bitmap[2] = {
153 FATTR4_WORD0_TYPE
154 | FATTR4_WORD0_CHANGE
155 | FATTR4_WORD0_SIZE
156 | FATTR4_WORD0_FSID
157 | FATTR4_WORD0_FILEID
158 | FATTR4_WORD0_FS_LOCATIONS,
159 FATTR4_WORD1_MODE
160 | FATTR4_WORD1_NUMLINKS
161 | FATTR4_WORD1_OWNER
162 | FATTR4_WORD1_OWNER_GROUP
163 | FATTR4_WORD1_RAWDEV
164 | FATTR4_WORD1_SPACE_USED
165 | FATTR4_WORD1_TIME_ACCESS
166 | FATTR4_WORD1_TIME_METADATA
167 | FATTR4_WORD1_TIME_MODIFY
168 | FATTR4_WORD1_MOUNTED_ON_FILEID
169};
170
Al Virobc4785c2006-10-19 23:28:51 -0700171static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct nfs4_readdir_arg *readdir)
173{
Al Viro0dbb4c62006-10-19 23:28:49 -0700174 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 BUG_ON(readdir->count < 80);
177 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000178 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
180 return;
181 }
182
183 readdir->cookie = 0;
184 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
185 if (cookie == 2)
186 return;
187
188 /*
189 * NFSv4 servers do not return entries for '.' and '..'
190 * Therefore, we fake these entries here. We let '.'
191 * have cookie 0 and '..' have cookie 1. Note that
192 * when talking to the server, we always send cookie 0
193 * instead of 1 or 2.
194 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700195 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (cookie == 0) {
198 *p++ = xdr_one; /* next */
199 *p++ = xdr_zero; /* cookie, first word */
200 *p++ = xdr_one; /* cookie, second word */
201 *p++ = xdr_one; /* entry len */
202 memcpy(p, ".\0\0\0", 4); /* entry */
203 p++;
204 *p++ = xdr_one; /* bitmap length */
205 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
206 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400207 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 *p++ = xdr_one; /* next */
211 *p++ = xdr_zero; /* cookie, first word */
212 *p++ = xdr_two; /* cookie, second word */
213 *p++ = xdr_two; /* entry len */
214 memcpy(p, "..\0\0", 4); /* entry */
215 p++;
216 *p++ = xdr_one; /* bitmap length */
217 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
218 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400219 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 readdir->pgbase = (char *)p - (char *)start;
222 readdir->count -= readdir->pgbase;
223 kunmap_atomic(start, KM_USER0);
224}
225
Trond Myklebust65de8722008-12-23 15:21:44 -0500226static int nfs4_wait_clnt_recover(struct nfs_client *clp)
227{
228 int res;
229
230 might_sleep();
231
Trond Myklebuste005e802008-12-23 15:21:48 -0500232 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400233 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500234 return res;
235}
236
237static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
238{
239 int res = 0;
240
241 might_sleep();
242
243 if (*timeout <= 0)
244 *timeout = NFS4_POLL_RETRY_MIN;
245 if (*timeout > NFS4_POLL_RETRY_MAX)
246 *timeout = NFS4_POLL_RETRY_MAX;
247 schedule_timeout_killable(*timeout);
248 if (fatal_signal_pending(current))
249 res = -ERESTARTSYS;
250 *timeout <<= 1;
251 return res;
252}
253
254/* This is the error handling routine for processes that are allowed
255 * to sleep.
256 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800257static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500258{
259 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500260 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500261 int ret = errorcode;
262
263 exception->retry = 0;
264 switch(errorcode) {
265 case 0:
266 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500267 case -NFS4ERR_ADMIN_REVOKED:
268 case -NFS4ERR_BAD_STATEID:
269 case -NFS4ERR_OPENMODE:
270 if (state == NULL)
271 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500272 nfs4_schedule_stateid_recovery(server, state);
273 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400274 case -NFS4ERR_EXPIRED:
275 if (state != NULL)
276 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500278 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500279 nfs4_schedule_lease_recovery(clp);
280 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500281#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400282 case -NFS4ERR_BADSESSION:
283 case -NFS4ERR_BADSLOT:
284 case -NFS4ERR_BAD_HIGH_SLOT:
285 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
286 case -NFS4ERR_DEADSESSION:
287 case -NFS4ERR_SEQ_FALSE_RETRY:
288 case -NFS4ERR_SEQ_MISORDERED:
289 dprintk("%s ERROR: %d Reset session\n", __func__,
290 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500291 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400292 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500293 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500294#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500296 if (exception->timeout > HZ) {
297 /* We have retried a decent amount, time to
298 * fail
299 */
300 ret = -EBUSY;
301 break;
302 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500303 case -NFS4ERR_GRACE:
304 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500305 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500306 ret = nfs4_delay(server->client, &exception->timeout);
307 if (ret != 0)
308 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400309 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500310 case -NFS4ERR_OLD_STATEID:
311 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800312 break;
313 case -NFS4ERR_BADOWNER:
314 /* The following works around a Linux server bug! */
315 case -NFS4ERR_BADNAME:
316 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
317 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
318 exception->retry = 1;
319 printk(KERN_WARNING "NFS: v4 server %s "
320 "does not accept raw "
321 "uid/gids. "
322 "Reenabling the idmapper.\n",
323 server->nfs_client->cl_hostname);
324 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500325 }
326 /* We failed to handle the error */
327 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500329 ret = nfs4_wait_clnt_recover(clp);
330 if (ret == 0)
331 exception->retry = 1;
332 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500333}
334
335
Trond Myklebust452e9352010-07-31 14:29:06 -0400336static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_lock(&clp->cl_lock);
339 if (time_before(clp->cl_last_renewal,timestamp))
340 clp->cl_last_renewal = timestamp;
341 spin_unlock(&clp->cl_lock);
342}
343
Trond Myklebust452e9352010-07-31 14:29:06 -0400344static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
345{
346 do_renew_lease(server->nfs_client, timestamp);
347}
348
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400349#if defined(CONFIG_NFS_V4_1)
350
Benny Halevy510b8172009-04-01 09:22:14 -0400351/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 * nfs4_free_slot - free a slot and efficiently update slot table.
353 *
354 * freeing a slot is trivially done by clearing its respective bit
355 * in the bitmap.
356 * If the freed slotid equals highest_used_slotid we want to update it
357 * so that the server would be able to size down the slot table if needed,
358 * otherwise we know that the highest_used_slotid is still in use.
359 * When updating highest_used_slotid there may be "holes" in the bitmap
360 * so we need to scan down from highest_used_slotid to 0 looking for the now
361 * highest slotid in use.
362 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500363 *
364 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400365 */
366static void
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500367nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368{
369 int slotid = free_slotid;
370
Benny Halevydfb4f302010-09-24 09:17:01 -0400371 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 /* clear used bit in bitmap */
373 __clear_bit(slotid, tbl->used_slots);
374
375 /* update highest_used_slotid when it is freed */
376 if (slotid == tbl->highest_used_slotid) {
377 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500378 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400379 tbl->highest_used_slotid = slotid;
380 else
381 tbl->highest_used_slotid = -1;
382 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384 free_slotid, tbl->highest_used_slotid);
385}
386
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800387/*
Andy Adamson42acd022011-01-06 02:04:34 +0000388 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800389 */
Andy Adamson42acd022011-01-06 02:04:34 +0000390static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800391{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800392 struct rpc_task *task;
393
Trond Myklebusta2118c32010-06-16 09:52:26 -0400394 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800395 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396 if (task)
397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398 return;
399 }
400
401 if (ses->fc_slot_table.highest_used_slotid != -1)
402 return;
403
Andy Adamson42acd022011-01-06 02:04:34 +0000404 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405 complete(&ses->fc_slot_table.complete);
406}
407
408/*
409 * Signal state manager thread if session back channel is drained
410 */
411void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
412{
413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414 ses->bc_slot_table.highest_used_slotid != -1)
415 return;
416 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800418}
419
Trond Myklebustd185a332010-06-16 09:52:25 -0400420static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400421{
422 struct nfs4_slot_table *tbl;
423
Trond Myklebustd185a332010-06-16 09:52:25 -0400424 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400425 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400426 /* just wake up the next guy waiting since
427 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500428 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 return;
Andy Adamson13615872009-04-01 09:22:17 -0400430 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500431
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500432 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500433 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000434 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500435 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400436 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400437}
438
Trond Myklebust14516c32010-07-31 14:29:06 -0400439static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440{
441 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400442 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443
444 /*
445 * sr_status remains 1 if an RPC level error occurred. The server
446 * may or may not have processed the sequence operation..
447 * Proceed as if the server received and processed the sequence
448 * operation.
449 */
450 if (res->sr_status == 1)
451 res->sr_status = NFS_OK;
452
Bryan Schumaker468f8612011-04-18 15:57:32 -0400453 /* don't increment the sequence number if the task wasn't sent */
454 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 goto out;
456
Andy Adamson691daf32009-12-04 15:55:39 -0500457 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 switch (res->sr_status) {
459 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400461 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400464 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500465 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500466 if (res->sr_status_flags != 0)
467 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 break;
469 case -NFS4ERR_DELAY:
470 /* The server detected a resend of the RPC call and
471 * returned NFS4ERR_DELAY as per Section 2.10.6.2
472 * of RFC5661.
473 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200474 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400475 __func__,
476 res->sr_slot - res->sr_session->fc_slot_table.slots,
477 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400478 goto out_retry;
479 default:
480 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400481 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400482 }
483out:
484 /* The session may be reset by one of the error handlers. */
485 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400486 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 return 1;
488out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400489 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400490 goto out;
491 rpc_delay(task, NFS4_POLL_RETRY_MAX);
492 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400493}
494
Trond Myklebust14516c32010-07-31 14:29:06 -0400495static int nfs4_sequence_done(struct rpc_task *task,
496 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400497{
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 if (res->sr_session == NULL)
499 return 1;
500 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400501}
502
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400503/*
Benny Halevy510b8172009-04-01 09:22:14 -0400504 * nfs4_find_slot - efficiently look for a free slot
505 *
506 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507 * If found, we mark the slot as used, update the highest_used_slotid,
508 * and respectively set up the sequence operation args.
509 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 *
511 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400512 */
513static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800514nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400515{
516 int slotid;
517 u8 ret_id = NFS4_MAX_SLOT_TABLE;
518 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
519
Benny Halevy510b8172009-04-01 09:22:14 -0400520 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522 tbl->max_slots);
523 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524 if (slotid >= tbl->max_slots)
525 goto out;
526 __set_bit(slotid, tbl->used_slots);
527 if (slotid > tbl->highest_used_slotid)
528 tbl->highest_used_slotid = slotid;
529 ret_id = slotid;
530out:
531 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400533 return ret_id;
534}
535
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000536int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400537 struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res,
539 int cache_reply,
540 struct rpc_task *task)
541{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 struct nfs4_slot *slot;
543 struct nfs4_slot_table *tbl;
544 u8 slotid;
545
546 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400548 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 return 0;
550
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 tbl = &session->fc_slot_table;
552
553 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400554 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500556 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500557 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400558 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500559 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500560 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400561 }
562
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800563 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
564 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
565 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
566 spin_unlock(&tbl->slot_tbl_lock);
567 dprintk("%s enforce FIFO order\n", __func__);
568 return -EAGAIN;
569 }
570
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800571 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 if (slotid == NFS4_MAX_SLOT_TABLE) {
573 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
574 spin_unlock(&tbl->slot_tbl_lock);
575 dprintk("<-- %s: no free slots\n", __func__);
576 return -EAGAIN;
577 }
578 spin_unlock(&tbl->slot_tbl_lock);
579
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800580 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400582 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 args->sa_slotid = slotid;
584 args->sa_cache_this = cache_reply;
585
586 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
587
Benny Halevy99fe60d2009-04-01 09:22:29 -0400588 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400589 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400591 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 /*
593 * sr_status is only set in decode_sequence, and so will remain
594 * set to 1 if an rpc level failure occurs.
595 */
596 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 return 0;
598}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000599EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply,
605 struct rpc_task *task)
606{
Trond Myklebust035168a2010-06-16 09:52:26 -0400607 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 int ret = 0;
609
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 if (session == NULL) {
611 args->sa_session = NULL;
612 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400614 }
615
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200616 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400617 __func__, session->clp, session, res->sr_slot ?
618 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400619
620 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622out:
623 dprintk("<-- %s status=%d\n", __func__, ret);
624 return ret;
625}
626
627struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400628 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *seq_args;
630 struct nfs4_sequence_res *seq_res;
631 int cache_reply;
632};
633
634static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635{
636 struct nfs41_call_sync_data *data = calldata;
637
Trond Myklebust035168a2010-06-16 09:52:26 -0400638 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
639
640 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 data->seq_res, data->cache_reply, task))
642 return;
643 rpc_call_start(task);
644}
645
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800646static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
647{
648 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
649 nfs41_call_sync_prepare(task, calldata);
650}
651
Andy Adamson69ab40c2009-04-01 09:22:19 -0400652static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
653{
654 struct nfs41_call_sync_data *data = calldata;
655
Trond Myklebust14516c32010-07-31 14:29:06 -0400656 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400657}
658
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659struct rpc_call_ops nfs41_call_sync_ops = {
660 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662};
663
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800664struct rpc_call_ops nfs41_call_priv_sync_ops = {
665 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
666 .rpc_call_done = nfs41_call_sync_done,
667};
668
Bryan Schumaker7c513052011-03-24 17:12:24 +0000669static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
670 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 struct rpc_message *msg,
672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800674 int cache_reply,
675 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
683 .cache_reply = cache_reply,
684 };
685 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .rpc_message = msg,
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
690 };
691
Benny Halevydfb4f302010-09-24 09:17:01 -0400692 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800693 if (privileged)
694 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695 task = rpc_run_task(&task_setup);
696 if (IS_ERR(task))
697 ret = PTR_ERR(task);
698 else {
699 ret = task->tk_status;
700 rpc_put_task(task);
701 }
702 return ret;
703}
704
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705int _nfs4_call_sync_session(struct rpc_clnt *clnt,
706 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400707 struct rpc_message *msg,
708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
710 int cache_reply)
711{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713}
714
Trond Myklebustdf896452010-06-16 09:52:26 -0400715#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400716static int nfs4_sequence_done(struct rpc_task *task,
717 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400718{
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400720}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400721#endif /* CONFIG_NFS_V4_1 */
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
728 int cache_reply)
729{
Benny Halevyf3752972009-04-01 09:22:04 -0400730 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732}
733
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000734static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735int nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
740 int cache_reply)
741{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000742 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
743 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000744}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400745
Trond Myklebust38478b22006-05-25 01:40:57 -0400746static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Trond Myklebust38478b22006-05-25 01:40:57 -0400750 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400751 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700752 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400753 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700754 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400755 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_openargs o_arg;
761 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100762 struct nfs_open_confirmargs c_arg;
763 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500764 struct nfs4_string owner_name;
765 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct nfs_fattr f_attr;
767 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400769 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400771 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100773 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400774 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400779
780static void nfs4_init_opendata_res(struct nfs4_opendata *p)
781{
782 p->o_res.f_attr = &p->f_attr;
783 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400784 p->o_res.seqid = p->o_arg.seqid;
785 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400786 p->o_res.server = p->o_arg.server;
787 nfs_fattr_init(&p->f_attr);
788 nfs_fattr_init(&p->dir_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500789 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400790}
791
Al Viro82a2c1b2011-06-22 18:30:55 -0400792static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500793 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400794 const struct iattr *attrs,
795 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796{
Al Viro82a2c1b2011-06-22 18:30:55 -0400797 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 struct inode *dir = parent->d_inode;
799 struct nfs_server *server = NFS_SERVER(dir);
800 struct nfs4_opendata *p;
801
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 if (p == NULL)
804 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400805 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 if (p->o_arg.seqid == NULL)
807 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 nfs_sb_active(dentry->d_sb);
809 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 p->dir = parent;
811 p->owner = sp;
812 atomic_inc(&sp->so_count);
813 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 p->o_arg.open_flags = flags;
815 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400816 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400817 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 p->o_arg.server = server;
820 p->o_arg.bitmask = server->attr_bitmask;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500821 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400823 if (flags & O_CREAT) {
824 u32 *s;
825
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 p->o_arg.u.attrs = &p->attrs;
827 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400828 s = (u32 *) p->o_arg.u.verifier.data;
829 s[0] = jiffies;
830 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100832 p->c_arg.fh = &p->o_res.fh;
833 p->c_arg.stateid = &p->o_res.stateid;
834 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400835 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 return p;
838err_free:
839 kfree(p);
840err:
841 dput(parent);
842 return NULL;
843}
844
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 struct nfs4_opendata *p = container_of(kref,
848 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400849 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400850
851 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400852 if (p->state != NULL)
853 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400854 nfs4_put_state_owner(p->owner);
855 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400856 dput(p->dentry);
857 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500858 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400859 kfree(p);
860}
861
862static void nfs4_opendata_put(struct nfs4_opendata *p)
863{
864 if (p != NULL)
865 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100866}
867
Trond Myklebust06f814a2006-01-03 09:55:07 +0100868static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
869{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100870 int ret;
871
Trond Myklebust06f814a2006-01-03 09:55:07 +0100872 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100873 return ret;
874}
875
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877{
878 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879
880 if (open_mode & O_EXCL)
881 goto out;
882 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500884 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
885 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 break;
887 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500888 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
889 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890 break;
891 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500892 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
893 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400896 return ret;
897}
898
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500901 if (delegation == NULL)
902 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500905 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400906 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500907 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400908 return 1;
909}
910
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100912{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100914 case FMODE_WRITE:
915 state->n_wronly++;
916 break;
917 case FMODE_READ:
918 state->n_rdonly++;
919 break;
920 case FMODE_READ|FMODE_WRITE:
921 state->n_rdwr++;
922 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100924}
925
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500926static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400927{
928 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
929 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
930 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400932 case FMODE_READ:
933 set_bit(NFS_O_RDONLY_STATE, &state->flags);
934 break;
935 case FMODE_WRITE:
936 set_bit(NFS_O_WRONLY_STATE, &state->flags);
937 break;
938 case FMODE_READ|FMODE_WRITE:
939 set_bit(NFS_O_RDWR_STATE, &state->flags);
940 }
941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400944{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400945 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400947 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400948}
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950static 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 -0700951{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400952 /*
953 * Protect the call to nfs4_state_set_mode_locked and
954 * serialise the stateid update
955 */
956 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400957 if (deleg_stateid != NULL) {
958 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
959 set_bit(NFS_DELEGATED_STATE, &state->flags);
960 }
961 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400963 write_sequnlock(&state->seqlock);
964 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700966 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969static 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 -0500970{
971 struct nfs_inode *nfsi = NFS_I(state->inode);
972 struct nfs_delegation *deleg_cur;
973 int ret = 0;
974
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500976
977 rcu_read_lock();
978 deleg_cur = rcu_dereference(nfsi->delegation);
979 if (deleg_cur == NULL)
980 goto no_delegation;
981
982 spin_lock(&deleg_cur->lock);
983 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500985 goto no_delegation_unlock;
986
987 if (delegation == NULL)
988 delegation = &deleg_cur->stateid;
989 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
990 goto no_delegation_unlock;
991
Trond Myklebustb7391f42008-12-23 15:21:52 -0500992 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500994 ret = 1;
995no_delegation_unlock:
996 spin_unlock(&deleg_cur->lock);
997no_delegation:
998 rcu_read_unlock();
999
1000 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001002 ret = 1;
1003 }
1004
1005 return ret;
1006}
1007
1008
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010{
1011 struct nfs_delegation *delegation;
1012
1013 rcu_read_lock();
1014 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 rcu_read_unlock();
1017 return;
1018 }
1019 rcu_read_unlock();
1020 nfs_inode_return_delegation(inode);
1021}
1022
Trond Myklebust6ee41262007-07-08 14:11:36 -04001023static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024{
1025 struct nfs4_state *state = opendata->state;
1026 struct nfs_inode *nfsi = NFS_I(state->inode);
1027 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1029 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 nfs4_stateid stateid;
1031 int ret = -EAGAIN;
1032
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001036 if (can_open_cached(state, fmode, open_mode)) {
1037 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001038 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 goto out_return_state;
1040 }
1041 spin_unlock(&state->owner->so_lock);
1042 }
Trond Myklebust34310432008-12-23 15:21:38 -05001043 rcu_read_lock();
1044 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001045 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001046 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001048 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 /* Save the delegation */
1050 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1051 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001052 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053 if (ret != 0)
1054 goto out;
1055 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001056
1057 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001059 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061out:
1062 return ERR_PTR(ret);
1063out_return_state:
1064 atomic_inc(&state->count);
1065 return state;
1066}
1067
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1069{
1070 struct inode *inode;
1071 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001072 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001073 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001074
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001076 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 goto out;
1078 }
1079
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001081 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001083 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001085 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001086 goto err;
1087 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001088 state = nfs4_get_open_state(inode, data->owner);
1089 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001090 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001091 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001092 int delegation_flags = 0;
1093
Trond Myklebust003707c2007-07-05 18:07:55 -04001094 rcu_read_lock();
1095 delegation = rcu_dereference(NFS_I(inode)->delegation);
1096 if (delegation)
1097 delegation_flags = delegation->flags;
1098 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001099 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1100 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1101 "returning a delegation for "
1102 "OPEN(CLAIM_DELEGATE_CUR)\n",
1103 NFS_CLIENT(inode)->cl_server);
1104 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001105 nfs_inode_set_delegation(state->inode,
1106 data->owner->so_cred,
1107 &data->o_res);
1108 else
1109 nfs_inode_reclaim_delegation(state->inode,
1110 data->owner->so_cred,
1111 &data->o_res);
1112 }
Trond Myklebust34310432008-12-23 15:21:38 -05001113
1114 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001117out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001118 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119err_put_inode:
1120 iput(inode);
1121err:
1122 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001123}
1124
Trond Myklebust864472e2006-01-03 09:55:15 +01001125static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1126{
1127 struct nfs_inode *nfsi = NFS_I(state->inode);
1128 struct nfs_open_context *ctx;
1129
1130 spin_lock(&state->inode->i_lock);
1131 list_for_each_entry(ctx, &nfsi->open_files, list) {
1132 if (ctx->state != state)
1133 continue;
1134 get_nfs_open_context(ctx);
1135 spin_unlock(&state->inode->i_lock);
1136 return ctx;
1137 }
1138 spin_unlock(&state->inode->i_lock);
1139 return ERR_PTR(-ENOENT);
1140}
1141
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001142static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1143{
1144 struct nfs4_opendata *opendata;
1145
Al Viro3d4ff432011-06-22 18:40:12 -04001146 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001147 if (opendata == NULL)
1148 return ERR_PTR(-ENOMEM);
1149 opendata->state = state;
1150 atomic_inc(&state->count);
1151 return opendata;
1152}
1153
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001155{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001156 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 int ret;
1158
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001159 opendata->o_arg.open_flags = 0;
1160 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001161 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1162 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1163 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001164 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 if (ret != 0)
1166 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001168 if (IS_ERR(newstate))
1169 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001170 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001171 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 return 0;
1173}
1174
1175static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1176{
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 int ret;
1179
1180 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 smp_rmb();
1183 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001184 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 if (ret != 0)
1187 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 if (newstate != state)
1189 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 }
1191 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001192 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 if (newstate != state)
1197 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 }
1199 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001200 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 if (ret != 0)
1203 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001204 if (newstate != state)
1205 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001206 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001207 /*
1208 * We may have performed cached opens for all three recoveries.
1209 * Check if we need to update the current stateid.
1210 */
1211 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1212 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001213 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001214 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1215 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001216 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001217 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 return 0;
1219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
1222 * OPEN_RECLAIM:
1223 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001225static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001227 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001229 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int status;
1231
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001232 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1233 if (IS_ERR(opendata))
1234 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1236 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001237 rcu_read_lock();
1238 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001239 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001240 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001241 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 opendata->o_arg.u.delegation_type = delegation_type;
1243 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001244 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return status;
1246}
1247
Trond Myklebust539cd032007-06-05 11:46:42 -04001248static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct nfs_server *server = NFS_SERVER(state->inode);
1251 struct nfs4_exception exception = { };
1252 int err;
1253 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001254 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001255 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001256 break;
1257 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } while (exception.retry);
1259 return err;
1260}
1261
Trond Myklebust864472e2006-01-03 09:55:15 +01001262static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1263{
1264 struct nfs_open_context *ctx;
1265 int ret;
1266
1267 ctx = nfs4_state_find_open_context(state);
1268 if (IS_ERR(ctx))
1269 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001270 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 put_nfs_open_context(ctx);
1272 return ret;
1273}
1274
Trond Myklebust13437e12007-07-06 15:10:43 -04001275static 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 -07001276{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001280 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1281 if (IS_ERR(opendata))
1282 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001284 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001288 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Trond Myklebust13437e12007-07-06 15:10:43 -04001291int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001294 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int err;
1296 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001297 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 switch (err) {
1299 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001300 case -ENOENT:
1301 case -ESTALE:
1302 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001303 case -NFS4ERR_BADSESSION:
1304 case -NFS4ERR_BADSLOT:
1305 case -NFS4ERR_BAD_HIGH_SLOT:
1306 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1307 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001308 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 case -NFS4ERR_STALE_CLIENTID:
1311 case -NFS4ERR_STALE_STATEID:
1312 case -NFS4ERR_EXPIRED:
1313 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001314 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001315 goto out;
1316 case -ERESTARTSYS:
1317 /*
1318 * The show must go on: exit, but mark the
1319 * stateid as needing recovery.
1320 */
1321 case -NFS4ERR_ADMIN_REVOKED:
1322 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001323 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001324 case -EKEYEXPIRED:
1325 /*
1326 * User RPCSEC_GSS context has expired.
1327 * We cannot recover this stateid now, so
1328 * skip it and allow recovery thread to
1329 * proceed.
1330 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001331 case -ENOMEM:
1332 err = 0;
1333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 err = nfs4_handle_exception(server, err, &exception);
1336 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001337out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return err;
1339}
1340
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1342{
1343 struct nfs4_opendata *data = calldata;
1344
1345 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001346 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001347 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1348 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001349 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001350 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001351 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001352 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001353}
1354
1355static void nfs4_open_confirm_release(void *calldata)
1356{
1357 struct nfs4_opendata *data = calldata;
1358 struct nfs4_state *state = NULL;
1359
1360 /* If this request hasn't been cancelled, do nothing */
1361 if (data->cancelled == 0)
1362 goto out_free;
1363 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001364 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001367 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001368 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001369out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001370 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001371}
1372
1373static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 .rpc_call_done = nfs4_open_confirm_done,
1375 .rpc_release = nfs4_open_confirm_release,
1376};
1377
1378/*
1379 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1380 */
1381static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1382{
1383 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1384 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001385 struct rpc_message msg = {
1386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1387 .rpc_argp = &data->c_arg,
1388 .rpc_resp = &data->c_res,
1389 .rpc_cred = data->owner->so_cred,
1390 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001391 struct rpc_task_setup task_setup_data = {
1392 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001393 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001394 .callback_ops = &nfs4_open_confirm_ops,
1395 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001396 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001397 .flags = RPC_TASK_ASYNC,
1398 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int status;
1400
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001401 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001402 data->rpc_done = 0;
1403 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001405 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001406 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001408 status = nfs4_wait_for_completion_rpc_task(task);
1409 if (status != 0) {
1410 data->cancelled = 1;
1411 smp_wmb();
1412 } else
1413 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001414 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return status;
1416}
1417
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 struct nfs4_opendata *data = calldata;
1421 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001422
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1424 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425 /*
1426 * Check if we still need to send an OPEN call, or if we can use
1427 * a delegation instead.
1428 */
1429 if (data->state != NULL) {
1430 struct nfs_delegation *delegation;
1431
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001433 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001434 rcu_read_lock();
1435 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001436 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1437 can_open_delegated(delegation, data->o_arg.fmode))
1438 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001439 rcu_read_unlock();
1440 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001442 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001443 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001444 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001446 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1447 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001448 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001449 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001450 &data->o_arg.seq_args,
1451 &data->o_res.seq_res, 1, task))
1452 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001453 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001454 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001455unlock_no_action:
1456 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001457out_no_action:
1458 task->tk_action = NULL;
1459
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001462static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1463{
1464 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1465 nfs4_open_prepare(task, calldata);
1466}
1467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468static void nfs4_open_done(struct rpc_task *task, void *calldata)
1469{
1470 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001473
Trond Myklebust14516c32010-07-31 14:29:06 -04001474 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1475 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 if (task->tk_status == 0) {
1478 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001479 case S_IFREG:
1480 break;
1481 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483 break;
1484 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001486 break;
1487 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001489 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001490 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001491 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1492 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001493 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001494 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001496
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497static void nfs4_open_release(void *calldata)
1498{
1499 struct nfs4_opendata *data = calldata;
1500 struct nfs4_state *state = NULL;
1501
1502 /* If this request hasn't been cancelled, do nothing */
1503 if (data->cancelled == 0)
1504 goto out_free;
1505 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001506 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 goto out_free;
1508 /* In case we need an open_confirm, no cleanup! */
1509 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1510 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001512 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001513 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001515 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516}
1517
1518static const struct rpc_call_ops nfs4_open_ops = {
1519 .rpc_call_prepare = nfs4_open_prepare,
1520 .rpc_call_done = nfs4_open_done,
1521 .rpc_release = nfs4_open_release,
1522};
1523
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001524static const struct rpc_call_ops nfs4_recover_open_ops = {
1525 .rpc_call_prepare = nfs4_recover_open_prepare,
1526 .rpc_call_done = nfs4_open_done,
1527 .rpc_release = nfs4_open_release,
1528};
1529
1530static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531{
1532 struct inode *dir = data->dir->d_inode;
1533 struct nfs_server *server = NFS_SERVER(dir);
1534 struct nfs_openargs *o_arg = &data->o_arg;
1535 struct nfs_openres *o_res = &data->o_res;
1536 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537 struct rpc_message msg = {
1538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1539 .rpc_argp = o_arg,
1540 .rpc_resp = o_res,
1541 .rpc_cred = data->owner->so_cred,
1542 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 struct rpc_task_setup task_setup_data = {
1544 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001545 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001546 .callback_ops = &nfs4_open_ops,
1547 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001548 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001549 .flags = RPC_TASK_ASYNC,
1550 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 int status;
1552
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001553 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001554 data->rpc_done = 0;
1555 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001556 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001557 if (isrecover)
1558 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001559 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001560 if (IS_ERR(task))
1561 return PTR_ERR(task);
1562 status = nfs4_wait_for_completion_rpc_task(task);
1563 if (status != 0) {
1564 data->cancelled = 1;
1565 smp_wmb();
1566 } else
1567 status = data->rpc_status;
1568 rpc_put_task(task);
1569
1570 return status;
1571}
1572
1573static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1574{
1575 struct inode *dir = data->dir->d_inode;
1576 struct nfs_openres *o_res = &data->o_res;
1577 int status;
1578
1579 status = nfs4_run_open_task(data, 1);
1580 if (status != 0 || !data->rpc_done)
1581 return status;
1582
Trond Myklebust6926afd2012-01-07 13:22:46 -05001583 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1584
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001585 nfs_refresh_inode(dir, o_res->dir_attr);
1586
1587 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1588 status = _nfs4_proc_open_confirm(data);
1589 if (status != 0)
1590 return status;
1591 }
1592
1593 return status;
1594}
1595
1596/*
1597 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1598 */
1599static int _nfs4_proc_open(struct nfs4_opendata *data)
1600{
1601 struct inode *dir = data->dir->d_inode;
1602 struct nfs_server *server = NFS_SERVER(dir);
1603 struct nfs_openargs *o_arg = &data->o_arg;
1604 struct nfs_openres *o_res = &data->o_res;
1605 int status;
1606
1607 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001608 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001610 if (status != 0) {
1611 if (status == -NFS4ERR_BADNAME &&
1612 !(o_arg->open_flags & O_CREAT))
1613 return -ENOENT;
1614 return status;
1615 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616
Trond Myklebust6926afd2012-01-07 13:22:46 -05001617 nfs_fattr_map_and_free_names(server, &data->f_attr);
1618
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001619 if (o_arg->open_flags & O_CREAT) {
1620 update_changeattr(dir, &o_res->cinfo);
1621 nfs_post_op_update_inode(dir, o_res->dir_attr);
1622 } else
1623 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001624 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1625 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001627 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001632 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Andy Adamsond83217c2011-03-01 01:34:17 +00001636static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001637{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001638 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001639 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001640
Trond Myklebusta78cb572009-08-09 15:06:19 -04001641 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001642 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001643 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001644 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001645 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1646 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001647 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001648 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001649 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001650 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001651 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001652}
1653
Andy Adamsond83217c2011-03-01 01:34:17 +00001654static int nfs4_recover_expired_lease(struct nfs_server *server)
1655{
1656 return nfs4_client_recover_expired_lease(server->nfs_client);
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
1660 * OPEN_EXPIRED:
1661 * reclaim state on the server after a network partition.
1662 * Assumes caller holds the appropriate lock
1663 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001664static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001666 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001669 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1670 if (IS_ERR(opendata))
1671 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001672 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001673 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001674 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001675 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001679static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001680{
Trond Myklebust539cd032007-06-05 11:46:42 -04001681 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001682 struct nfs4_exception exception = { };
1683 int err;
1684
1685 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001686 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001687 switch (err) {
1688 default:
1689 goto out;
1690 case -NFS4ERR_GRACE:
1691 case -NFS4ERR_DELAY:
1692 nfs4_handle_exception(server, err, &exception);
1693 err = 0;
1694 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001695 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001696out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001697 return err;
1698}
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001703 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Trond Myklebust864472e2006-01-03 09:55:15 +01001705 ctx = nfs4_state_find_open_context(state);
1706 if (IS_ERR(ctx))
1707 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001708 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001709 put_nfs_open_context(ctx);
1710 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001713#if defined(CONFIG_NFS_V4_1)
1714static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1715{
1716 int status;
1717 struct nfs_server *server = NFS_SERVER(state->inode);
1718
1719 status = nfs41_test_stateid(server, state);
1720 if (status == NFS_OK)
1721 return 0;
1722 nfs41_free_stateid(server, state);
1723 return nfs4_open_expired(sp, state);
1724}
1725#endif
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001728 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1729 * fields corresponding to attributes that were used to store the verifier.
1730 * Make sure we clobber those fields in the later setattr call
1731 */
1732static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1733{
1734 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1735 !(sattr->ia_valid & ATTR_ATIME_SET))
1736 sattr->ia_valid |= ATTR_ATIME;
1737
1738 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1739 !(sattr->ia_valid & ATTR_MTIME_SET))
1740 sattr->ia_valid |= ATTR_MTIME;
1741}
1742
1743/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001744 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001746static 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 -07001747{
1748 struct nfs4_state_owner *sp;
1749 struct nfs4_state *state = NULL;
1750 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001751 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 status = -ENOMEM;
1756 if (!(sp = nfs4_get_state_owner(server, cred))) {
1757 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1758 goto out_err;
1759 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001760 status = nfs4_recover_expired_lease(server);
1761 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001762 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001763 if (dentry->d_inode != NULL)
1764 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001765 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001766 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001767 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001768 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Al Viro82a2c1b2011-06-22 18:30:55 -04001770 if (dentry->d_inode != NULL)
1771 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001775 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001778 status = PTR_ERR(state);
1779 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001780 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001781 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001782 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001783
1784 if (opendata->o_arg.open_flags & O_EXCL) {
1785 nfs4_exclusive_attrset(opendata, sattr);
1786
1787 nfs_fattr_init(opendata->o_res.f_attr);
1788 status = nfs4_do_setattr(state->inode, cred,
1789 opendata->o_res.f_attr, sattr,
1790 state);
1791 if (status == 0)
1792 nfs_setattr_update_inode(state->inode, sattr);
1793 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1794 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001795 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 *res = state;
1798 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001799err_opendata_put:
1800 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001801err_put_state_owner:
1802 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *res = NULL;
1805 return status;
1806}
1807
1808
Al Viro82a2c1b2011-06-22 18:30:55 -04001809static 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 -07001810{
1811 struct nfs4_exception exception = { };
1812 struct nfs4_state *res;
1813 int status;
1814
1815 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001816 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (status == 0)
1818 break;
1819 /* NOTE: BAD_SEQID means the server and client disagree about the
1820 * book-keeping w.r.t. state-changing operations
1821 * (OPEN/CLOSE/LOCK/LOCKU...)
1822 * It is actually a sign of a bug on the client or on the server.
1823 *
1824 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001825 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 * have unhashed the old state_owner for us, and that we can
1827 * therefore safely retry using a new one. We should still warn
1828 * the user though...
1829 */
1830 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001831 printk(KERN_WARNING "NFS: v4 server %s "
1832 " returned a bad sequence-id error!\n",
1833 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 exception.retry = 1;
1835 continue;
1836 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001837 /*
1838 * BAD_STATEID on OPEN means that the server cancelled our
1839 * state before it received the OPEN_CONFIRM.
1840 * Recover by retrying the request as per the discussion
1841 * on Page 181 of RFC3530.
1842 */
1843 if (status == -NFS4ERR_BAD_STATEID) {
1844 exception.retry = 1;
1845 continue;
1846 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001847 if (status == -EAGAIN) {
1848 /* We must have found a delegation */
1849 exception.retry = 1;
1850 continue;
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1853 status, &exception));
1854 } while (exception.retry);
1855 return res;
1856}
1857
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001858static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1859 struct nfs_fattr *fattr, struct iattr *sattr,
1860 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001862 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001864 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 .iap = sattr,
1866 .server = server,
1867 .bitmask = server->attr_bitmask,
1868 };
1869 struct nfs_setattrres res = {
1870 .fattr = fattr,
1871 .server = server,
1872 };
1873 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1875 .rpc_argp = &arg,
1876 .rpc_resp = &res,
1877 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001879 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001880 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Trond Myklebust0e574af2005-10-27 22:12:38 -04001882 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001884 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1885 /* Use that stateid */
1886 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001887 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1890
Bryan Schumaker7c513052011-03-24 17:12:24 +00001891 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001892 if (status == 0 && state != NULL)
1893 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001894 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001897static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1898 struct nfs_fattr *fattr, struct iattr *sattr,
1899 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001901 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct nfs4_exception exception = { };
1903 int err;
1904 do {
1905 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001906 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 &exception);
1908 } while (exception.retry);
1909 return err;
1910}
1911
1912struct nfs4_closedata {
1913 struct inode *inode;
1914 struct nfs4_state *state;
1915 struct nfs_closeargs arg;
1916 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001917 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001918 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001919 bool roc;
1920 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921};
1922
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001923static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001924{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001925 struct nfs4_closedata *calldata = data;
1926 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001927 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001928
Fred Isamanf7e89172011-01-06 11:36:32 +00001929 if (calldata->roc)
1930 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001931 nfs4_put_open_state(calldata->state);
1932 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001933 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001934 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001935 kfree(calldata);
1936}
1937
Trond Myklebust88069f72009-12-08 08:33:16 -05001938static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1939 fmode_t fmode)
1940{
1941 spin_lock(&state->owner->so_lock);
1942 if (!(fmode & FMODE_READ))
1943 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1944 if (!(fmode & FMODE_WRITE))
1945 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1946 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1947 spin_unlock(&state->owner->so_lock);
1948}
1949
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001950static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001952 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct nfs_server *server = NFS_SERVER(calldata->inode);
1955
Trond Myklebust14516c32010-07-31 14:29:06 -04001956 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* hmm. we are done with the inode, and in the process of freeing
1959 * the state_owner. we keep this around to process errors
1960 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 switch (task->tk_status) {
1962 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001963 if (calldata->roc)
1964 pnfs_roc_set_barrier(state->inode,
1965 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001966 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001967 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001968 nfs4_close_clear_stateid_flags(state,
1969 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 break;
1971 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001972 case -NFS4ERR_OLD_STATEID:
1973 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001978 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1979 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001981 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001982 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001985static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001987 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001988 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001989 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001990
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001991 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001992 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001993
Trond Myklebust88069f72009-12-08 08:33:16 -05001994 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1995 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001996 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001997 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001998 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001999 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002000 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2001 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2002 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002003 }
2004 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002005 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2006 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2007 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002008 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002009 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002010 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002011
2012 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002013 /* Note: exit _without_ calling nfs4_close_done */
2014 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002015 return;
2016 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002017
Fred Isamanf7e89172011-01-06 11:36:32 +00002018 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002019 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002020 if (calldata->roc &&
2021 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2022 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2023 task, NULL);
2024 return;
2025 }
2026 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002027
Trond Myklebust516a6af2005-10-27 22:12:41 -04002028 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002029 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002030 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002031 &calldata->arg.seq_args, &calldata->res.seq_res,
2032 1, task))
2033 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002034 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002037static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002038 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002039 .rpc_call_done = nfs4_close_done,
2040 .rpc_release = nfs4_free_closedata,
2041};
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043/*
2044 * It is possible for data to be read/written from a mem-mapped file
2045 * after the sys_close call (which hits the vfs layer as a flush).
2046 * This means that we can't safely call nfsv4 close on a file until
2047 * the inode is cleared. This in turn means that we are not good
2048 * NFSv4 citizens - we do not indicate to the server to update the file's
2049 * share state even when we are done with one of the three share
2050 * stateid's in the inode.
2051 *
2052 * NOTE: Caller must be holding the sp->so_owner semaphore!
2053 */
Al Viro643168c2011-06-22 18:20:23 -04002054int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002056 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002058 struct nfs4_state_owner *sp = state->owner;
2059 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002060 struct rpc_message msg = {
2061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2062 .rpc_cred = state->owner->so_cred,
2063 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002064 struct rpc_task_setup task_setup_data = {
2065 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002066 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002067 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002068 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002069 .flags = RPC_TASK_ASYNC,
2070 };
Trond Myklebust95121352005-10-18 14:20:12 -07002071 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002073 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002075 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002076 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002078 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002079 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002081 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002082 if (calldata->arg.seqid == NULL)
2083 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002084 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002085 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002086 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002087 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002088 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002090 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002091
Trond Myklebust1174dd12010-12-21 10:52:24 -05002092 msg.rpc_argp = &calldata->arg;
2093 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002094 task_setup_data.callback_data = calldata;
2095 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002096 if (IS_ERR(task))
2097 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002098 status = 0;
2099 if (wait)
2100 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002101 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002102 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002103out_free_calldata:
2104 kfree(calldata);
2105out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002106 if (roc)
2107 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002108 nfs4_put_open_state(state);
2109 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002110 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Trond Myklebust2b484292010-09-17 10:56:51 -04002113static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002114nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct nfs4_state *state;
2117
Trond Myklebust565277f2007-10-15 18:17:53 -04002118 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002119 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002120 if (IS_ERR(state))
2121 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002122 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002123 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
Trond Myklebust1185a552009-12-03 15:54:02 -05002126static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002127{
2128 if (ctx->state == NULL)
2129 return;
2130 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002131 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002132 else
Al Viro643168c2011-06-22 18:20:23 -04002133 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2137{
Benny Halevy43652ad2009-04-01 09:21:54 -04002138 struct nfs4_server_caps_arg args = {
2139 .fhandle = fhandle,
2140 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct nfs4_server_caps_res res = {};
2142 struct rpc_message msg = {
2143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002144 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 .rpc_resp = &res,
2146 };
2147 int status;
2148
Bryan Schumaker7c513052011-03-24 17:12:24 +00002149 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (status == 0) {
2151 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002152 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2153 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2154 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2155 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2156 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2158 server->caps |= NFS_CAP_ACLS;
2159 if (res.has_links != 0)
2160 server->caps |= NFS_CAP_HARDLINKS;
2161 if (res.has_symlinks != 0)
2162 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002163 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2164 server->caps |= NFS_CAP_FILEID;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2166 server->caps |= NFS_CAP_MODE;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2168 server->caps |= NFS_CAP_NLINK;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2170 server->caps |= NFS_CAP_OWNER;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2172 server->caps |= NFS_CAP_OWNER_GROUP;
2173 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2174 server->caps |= NFS_CAP_ATIME;
2175 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2176 server->caps |= NFS_CAP_CTIME;
2177 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2178 server->caps |= NFS_CAP_MTIME;
2179
Trond Myklebusta65318b2009-03-11 14:10:28 -04002180 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2181 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2182 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 server->acl_bitmask = res.acl_bitmask;
2184 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return status;
2187}
2188
Trond Myklebust55a97592006-06-09 09:34:19 -04002189int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct nfs4_exception exception = { };
2192 int err;
2193 do {
2194 err = nfs4_handle_exception(server,
2195 _nfs4_server_capabilities(server, fhandle),
2196 &exception);
2197 } while (exception.retry);
2198 return err;
2199}
2200
2201static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2202 struct nfs_fsinfo *info)
2203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 struct nfs4_lookup_root_arg args = {
2205 .bitmask = nfs4_fattr_bitmap,
2206 };
2207 struct nfs4_lookup_res res = {
2208 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002209 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 .fh = fhandle,
2211 };
2212 struct rpc_message msg = {
2213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2214 .rpc_argp = &args,
2215 .rpc_resp = &res,
2216 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002217
Trond Myklebust0e574af2005-10-27 22:12:38 -04002218 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002219 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
2222static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2223 struct nfs_fsinfo *info)
2224{
2225 struct nfs4_exception exception = { };
2226 int err;
2227 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002228 err = _nfs4_lookup_root(server, fhandle, info);
2229 switch (err) {
2230 case 0:
2231 case -NFS4ERR_WRONGSEC:
2232 break;
2233 default:
2234 err = nfs4_handle_exception(server, err, &exception);
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 } while (exception.retry);
2237 return err;
2238}
2239
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002240static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2241 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2242{
2243 struct rpc_auth *auth;
2244 int ret;
2245
2246 auth = rpcauth_create(flavor, server->client);
2247 if (!auth) {
2248 ret = -EIO;
2249 goto out;
2250 }
2251 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252out:
2253 return ret;
2254}
2255
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002256static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002257 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002259 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002260 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002262 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2263 flav_array[len] = RPC_AUTH_NULL;
2264 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002265
2266 for (i = 0; i < len; i++) {
2267 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002268 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002269 continue;
2270 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002271 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002272 /*
2273 * -EACCESS could mean that the user doesn't have correct permissions
2274 * to access the mount. It could also mean that we tried to mount
2275 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2276 * existing mount programs don't handle -EACCES very well so it should
2277 * be mapped to -EPERM instead.
2278 */
2279 if (status == -EACCES)
2280 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002281 return status;
2282}
2283
2284/*
2285 * get the file handle for the "/" directory on the server
2286 */
2287static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2288 struct nfs_fsinfo *info)
2289{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002290 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002291 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002292 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2293 /*
2294 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2295 * by nfs4_map_errors() as this function exits.
2296 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002297 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (status == 0)
2299 status = nfs4_server_capabilities(server, fhandle);
2300 if (status == 0)
2301 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002302 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Andy Adamson533eb462011-06-13 18:25:56 -04002305static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002306/*
2307 * Get locations and (maybe) other attributes of a referral.
2308 * Note that we'll actually follow the referral later when
2309 * we detect fsid mismatch in inode revalidation
2310 */
Andy Adamson533eb462011-06-13 18:25:56 -04002311static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2312 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002313{
2314 int status = -ENOMEM;
2315 struct page *page = NULL;
2316 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002317
2318 page = alloc_page(GFP_KERNEL);
2319 if (page == NULL)
2320 goto out;
2321 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2322 if (locations == NULL)
2323 goto out;
2324
Trond Myklebustc228fd32007-01-13 02:28:11 -05002325 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002326 if (status != 0)
2327 goto out;
2328 /* Make sure server returned a different fsid for the referral */
2329 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002330 dprintk("%s: server did not return a different fsid for"
2331 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002332 status = -EIO;
2333 goto out;
2334 }
Andy Adamson533eb462011-06-13 18:25:56 -04002335 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2336 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002337
Andy Adamson533eb462011-06-13 18:25:56 -04002338 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002339 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002340 memset(fhandle, 0, sizeof(struct nfs_fh));
2341out:
2342 if (page)
2343 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002344 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002345 return status;
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2349{
2350 struct nfs4_getattr_arg args = {
2351 .fh = fhandle,
2352 .bitmask = server->attr_bitmask,
2353 };
2354 struct nfs4_getattr_res res = {
2355 .fattr = fattr,
2356 .server = server,
2357 };
2358 struct rpc_message msg = {
2359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2360 .rpc_argp = &args,
2361 .rpc_resp = &res,
2362 };
2363
Trond Myklebust0e574af2005-10-27 22:12:38 -04002364 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002365 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
2368static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2369{
2370 struct nfs4_exception exception = { };
2371 int err;
2372 do {
2373 err = nfs4_handle_exception(server,
2374 _nfs4_proc_getattr(server, fhandle, fattr),
2375 &exception);
2376 } while (exception.retry);
2377 return err;
2378}
2379
2380/*
2381 * The file is not closed if it is opened due to the a request to change
2382 * the size of the file. The open call will not be needed once the
2383 * VFS layer lookup-intents are implemented.
2384 *
2385 * Close is called when the inode is destroyed.
2386 * If we haven't opened the file for O_WRONLY, we
2387 * need to in the size_change case to obtain a stateid.
2388 *
2389 * Got race?
2390 * Because OPEN is always done by name in nfsv4, it is
2391 * possible that we opened a different file by the same
2392 * name. We can recognize this race condition, but we
2393 * can't do anything about it besides returning an error.
2394 *
2395 * This will be fixed with VFS changes (lookup-intent).
2396 */
2397static int
2398nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2399 struct iattr *sattr)
2400{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002401 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002403 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 int status;
2405
Benny Halevy8a1636c2010-07-14 15:43:57 -04002406 if (pnfs_ld_layoutret_on_setattr(inode))
2407 pnfs_return_layout(inode);
2408
Trond Myklebust0e574af2005-10-27 22:12:38 -04002409 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Trond Myklebustd5308382005-11-04 15:33:38 -05002411 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002412 if (sattr->ia_valid & ATTR_FILE) {
2413 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002414
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002415 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002416 if (ctx) {
2417 cred = ctx->cred;
2418 state = ctx->state;
2419 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002420 }
2421
2422 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002423 if (status == 0)
2424 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return status;
2426}
2427
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002428static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2429 const struct qstr *name, struct nfs_fh *fhandle,
2430 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002431{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002432 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002433 int status;
2434 struct nfs4_lookup_arg args = {
2435 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002436 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002437 .name = name,
2438 };
2439 struct nfs4_lookup_res res = {
2440 .server = server,
2441 .fattr = fattr,
2442 .fh = fhandle,
2443 };
2444 struct rpc_message msg = {
2445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2446 .rpc_argp = &args,
2447 .rpc_resp = &res,
2448 };
2449
2450 nfs_fattr_init(fattr);
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002453 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 dprintk("NFS reply lookup: %d\n", status);
2455 return status;
2456}
2457
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002458void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2459{
2460 memset(fh, 0, sizeof(struct nfs_fh));
2461 fattr->fsid.major = 1;
2462 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2463 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2464 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2465 fattr->nlink = 2;
2466}
2467
Bryan Schumaker7c513052011-03-24 17:12:24 +00002468static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2469 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 struct nfs4_exception exception = { };
2472 int err;
2473 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002474 int status;
2475
2476 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2477 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002478 case -NFS4ERR_BADNAME:
2479 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002480 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002481 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002482 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002483 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002484 }
2485 err = nfs4_handle_exception(NFS_SERVER(dir),
2486 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 } while (exception.retry);
2488 return err;
2489}
2490
2491static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2492{
Trond Myklebust76b32992007-08-10 17:45:11 -04002493 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct nfs4_accessargs args = {
2495 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002496 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002498 struct nfs4_accessres res = {
2499 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 struct rpc_message msg = {
2502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2503 .rpc_argp = &args,
2504 .rpc_resp = &res,
2505 .rpc_cred = entry->cred,
2506 };
2507 int mode = entry->mask;
2508 int status;
2509
2510 /*
2511 * Determine which access bits we want to ask for...
2512 */
2513 if (mode & MAY_READ)
2514 args.access |= NFS4_ACCESS_READ;
2515 if (S_ISDIR(inode->i_mode)) {
2516 if (mode & MAY_WRITE)
2517 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2518 if (mode & MAY_EXEC)
2519 args.access |= NFS4_ACCESS_LOOKUP;
2520 } else {
2521 if (mode & MAY_WRITE)
2522 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2523 if (mode & MAY_EXEC)
2524 args.access |= NFS4_ACCESS_EXECUTE;
2525 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002526
2527 res.fattr = nfs_alloc_fattr();
2528 if (res.fattr == NULL)
2529 return -ENOMEM;
2530
Bryan Schumaker7c513052011-03-24 17:12:24 +00002531 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (!status) {
2533 entry->mask = 0;
2534 if (res.access & NFS4_ACCESS_READ)
2535 entry->mask |= MAY_READ;
2536 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2537 entry->mask |= MAY_WRITE;
2538 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2539 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002540 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002542 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 return status;
2544}
2545
2546static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2547{
2548 struct nfs4_exception exception = { };
2549 int err;
2550 do {
2551 err = nfs4_handle_exception(NFS_SERVER(inode),
2552 _nfs4_proc_access(inode, entry),
2553 &exception);
2554 } while (exception.retry);
2555 return err;
2556}
2557
2558/*
2559 * TODO: For the time being, we don't try to get any attributes
2560 * along with any of the zero-copy operations READ, READDIR,
2561 * READLINK, WRITE.
2562 *
2563 * In the case of the first three, we want to put the GETATTR
2564 * after the read-type operation -- this is because it is hard
2565 * to predict the length of a GETATTR response in v4, and thus
2566 * align the READ data correctly. This means that the GETATTR
2567 * may end up partially falling into the page cache, and we should
2568 * shift it into the 'tail' of the xdr_buf before processing.
2569 * To do this efficiently, we need to know the total length
2570 * of data received, which doesn't seem to be available outside
2571 * of the RPC layer.
2572 *
2573 * In the case of WRITE, we also want to put the GETATTR after
2574 * the operation -- in this case because we want to make sure
2575 * we get the post-operation mtime and size. This means that
2576 * we can't use xdr_encode_pages() as written: we need a variant
2577 * of it which would leave room in the 'tail' iovec.
2578 *
2579 * Both of these changes to the XDR layer would in fact be quite
2580 * minor, but I decided to leave them for a subsequent patch.
2581 */
2582static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2583 unsigned int pgbase, unsigned int pglen)
2584{
2585 struct nfs4_readlink args = {
2586 .fh = NFS_FH(inode),
2587 .pgbase = pgbase,
2588 .pglen = pglen,
2589 .pages = &page,
2590 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002591 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 struct rpc_message msg = {
2593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2594 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002595 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 };
2597
Bryan Schumaker7c513052011-03-24 17:12:24 +00002598 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 -07002599}
2600
2601static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2602 unsigned int pgbase, unsigned int pglen)
2603{
2604 struct nfs4_exception exception = { };
2605 int err;
2606 do {
2607 err = nfs4_handle_exception(NFS_SERVER(inode),
2608 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2609 &exception);
2610 } while (exception.retry);
2611 return err;
2612}
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614/*
2615 * Got race?
2616 * We will need to arrange for the VFS layer to provide an atomic open.
2617 * Until then, this create/open method is prone to inefficiency and race
2618 * conditions due to the lookup, create, and open VFS calls from sys_open()
2619 * placed on the wire.
2620 *
2621 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2622 * The file will be opened again in the subsequent VFS open call
2623 * (nfs4_proc_file_open).
2624 *
2625 * The open for read will just hang around to be used by any process that
2626 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2627 */
2628
2629static int
2630nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002631 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Al Viro82a2c1b2011-06-22 18:30:55 -04002633 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002635 struct rpc_cred *cred = NULL;
2636 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 int status = 0;
2638
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002639 if (ctx != NULL) {
2640 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002641 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002642 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002644 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002645 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002646 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (IS_ERR(state)) {
2648 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002651 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002652 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002653 if (ctx != NULL)
2654 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002655 else
Al Viro643168c2011-06-22 18:20:23 -04002656 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657out:
2658 return status;
2659}
2660
2661static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2662{
Trond Myklebust16e42952005-10-27 22:12:44 -04002663 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002664 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002666 .name.len = name->len,
2667 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002668 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002670 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002671 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002672 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2675 .rpc_argp = &args,
2676 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002678 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Trond Myklebustd3468902010-04-16 16:22:50 -04002680 res.dir_attr = nfs_alloc_fattr();
2681 if (res.dir_attr == NULL)
2682 goto out;
2683
Bryan Schumaker7c513052011-03-24 17:12:24 +00002684 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002685 if (status == 0) {
2686 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002687 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002688 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002689 nfs_free_fattr(res.dir_attr);
2690out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return status;
2692}
2693
2694static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2695{
2696 struct nfs4_exception exception = { };
2697 int err;
2698 do {
2699 err = nfs4_handle_exception(NFS_SERVER(dir),
2700 _nfs4_proc_remove(dir, name),
2701 &exception);
2702 } while (exception.retry);
2703 return err;
2704}
2705
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002706static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708 struct nfs_server *server = NFS_SERVER(dir);
2709 struct nfs_removeargs *args = msg->rpc_argp;
2710 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Trond Myklebusta65318b2009-03-11 14:10:28 -04002712 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002714 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002720 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2721
Trond Myklebust14516c32010-07-31 14:29:06 -04002722 if (!nfs4_sequence_done(task, &res->seq_res))
2723 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002724 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002725 return 0;
2726 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002727 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002728 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Jeff Laytond3d41522010-09-17 17:31:57 -04002731static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2732{
2733 struct nfs_server *server = NFS_SERVER(dir);
2734 struct nfs_renameargs *arg = msg->rpc_argp;
2735 struct nfs_renameres *res = msg->rpc_resp;
2736
2737 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2738 arg->bitmask = server->attr_bitmask;
2739 res->server = server;
2740}
2741
2742static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2743 struct inode *new_dir)
2744{
2745 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2746
2747 if (!nfs4_sequence_done(task, &res->seq_res))
2748 return 0;
2749 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2750 return 0;
2751
2752 update_changeattr(old_dir, &res->old_cinfo);
2753 nfs_post_op_update_inode(old_dir, res->old_fattr);
2754 update_changeattr(new_dir, &res->new_cinfo);
2755 nfs_post_op_update_inode(new_dir, res->new_fattr);
2756 return 1;
2757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2760 struct inode *new_dir, struct qstr *new_name)
2761{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002762 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002763 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .old_dir = NFS_FH(old_dir),
2765 .new_dir = NFS_FH(new_dir),
2766 .old_name = old_name,
2767 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002768 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002770 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002771 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002772 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct rpc_message msg = {
2774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2775 .rpc_argp = &arg,
2776 .rpc_resp = &res,
2777 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002778 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Trond Myklebust011fff72010-04-16 16:22:49 -04002780 res.old_fattr = nfs_alloc_fattr();
2781 res.new_fattr = nfs_alloc_fattr();
2782 if (res.old_fattr == NULL || res.new_fattr == NULL)
2783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Bryan Schumaker7c513052011-03-24 17:12:24 +00002785 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (!status) {
2787 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002788 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002790 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002792out:
2793 nfs_free_fattr(res.new_fattr);
2794 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return status;
2796}
2797
2798static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2799 struct inode *new_dir, struct qstr *new_name)
2800{
2801 struct nfs4_exception exception = { };
2802 int err;
2803 do {
2804 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2805 _nfs4_proc_rename(old_dir, old_name,
2806 new_dir, new_name),
2807 &exception);
2808 } while (exception.retry);
2809 return err;
2810}
2811
2812static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2813{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002814 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct nfs4_link_arg arg = {
2816 .fh = NFS_FH(inode),
2817 .dir_fh = NFS_FH(dir),
2818 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002819 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002821 struct nfs4_link_res res = {
2822 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002823 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 struct rpc_message msg = {
2825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2826 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002827 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002829 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Trond Myklebust136f2622010-04-16 16:22:49 -04002831 res.fattr = nfs_alloc_fattr();
2832 res.dir_attr = nfs_alloc_fattr();
2833 if (res.fattr == NULL || res.dir_attr == NULL)
2834 goto out;
2835
Bryan Schumaker7c513052011-03-24 17:12:24 +00002836 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002837 if (!status) {
2838 update_changeattr(dir, &res.cinfo);
2839 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002840 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002841 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002842out:
2843 nfs_free_fattr(res.dir_attr);
2844 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return status;
2846}
2847
2848static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2849{
2850 struct nfs4_exception exception = { };
2851 int err;
2852 do {
2853 err = nfs4_handle_exception(NFS_SERVER(inode),
2854 _nfs4_proc_link(inode, dir, name),
2855 &exception);
2856 } while (exception.retry);
2857 return err;
2858}
2859
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002860struct nfs4_createdata {
2861 struct rpc_message msg;
2862 struct nfs4_create_arg arg;
2863 struct nfs4_create_res res;
2864 struct nfs_fh fh;
2865 struct nfs_fattr fattr;
2866 struct nfs_fattr dir_fattr;
2867};
2868
2869static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2870 struct qstr *name, struct iattr *sattr, u32 ftype)
2871{
2872 struct nfs4_createdata *data;
2873
2874 data = kzalloc(sizeof(*data), GFP_KERNEL);
2875 if (data != NULL) {
2876 struct nfs_server *server = NFS_SERVER(dir);
2877
2878 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2879 data->msg.rpc_argp = &data->arg;
2880 data->msg.rpc_resp = &data->res;
2881 data->arg.dir_fh = NFS_FH(dir);
2882 data->arg.server = server;
2883 data->arg.name = name;
2884 data->arg.attrs = sattr;
2885 data->arg.ftype = ftype;
2886 data->arg.bitmask = server->attr_bitmask;
2887 data->res.server = server;
2888 data->res.fh = &data->fh;
2889 data->res.fattr = &data->fattr;
2890 data->res.dir_fattr = &data->dir_fattr;
2891 nfs_fattr_init(data->res.fattr);
2892 nfs_fattr_init(data->res.dir_fattr);
2893 }
2894 return data;
2895}
2896
2897static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2898{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002899 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002900 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002901 if (status == 0) {
2902 update_changeattr(dir, &data->res.dir_cinfo);
2903 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2904 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2905 }
2906 return status;
2907}
2908
2909static void nfs4_free_createdata(struct nfs4_createdata *data)
2910{
2911 kfree(data);
2912}
2913
Chuck Lever4f390c12006-08-22 20:06:22 -04002914static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002915 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002917 struct nfs4_createdata *data;
2918 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Chuck Lever94a6d752006-08-22 20:06:23 -04002920 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002921 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002922
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923 status = -ENOMEM;
2924 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2925 if (data == NULL)
2926 goto out;
2927
2928 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2929 data->arg.u.symlink.pages = &page;
2930 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002932 status = nfs4_do_create(dir, dentry, data);
2933
2934 nfs4_free_createdata(data);
2935out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return status;
2937}
2938
Chuck Lever4f390c12006-08-22 20:06:22 -04002939static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002940 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 struct nfs4_exception exception = { };
2943 int err;
2944 do {
2945 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002946 _nfs4_proc_symlink(dir, dentry, page,
2947 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 &exception);
2949 } while (exception.retry);
2950 return err;
2951}
2952
2953static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2954 struct iattr *sattr)
2955{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002956 struct nfs4_createdata *data;
2957 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002959 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2960 if (data == NULL)
2961 goto out;
2962
2963 status = nfs4_do_create(dir, dentry, data);
2964
2965 nfs4_free_createdata(data);
2966out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return status;
2968}
2969
2970static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2971 struct iattr *sattr)
2972{
2973 struct nfs4_exception exception = { };
2974 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002975
2976 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 do {
2978 err = nfs4_handle_exception(NFS_SERVER(dir),
2979 _nfs4_proc_mkdir(dir, dentry, sattr),
2980 &exception);
2981 } while (exception.retry);
2982 return err;
2983}
2984
2985static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002986 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct inode *dir = dentry->d_inode;
2989 struct nfs4_readdir_arg args = {
2990 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002991 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .pgbase = 0,
2993 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002994 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002995 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 };
2997 struct nfs4_readdir_res res;
2998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3000 .rpc_argp = &args,
3001 .rpc_resp = &res,
3002 .rpc_cred = cred,
3003 };
3004 int status;
3005
Harvey Harrison3110ff82008-05-02 13:42:44 -07003006 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003007 dentry->d_parent->d_name.name,
3008 dentry->d_name.name,
3009 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3011 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003012 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 -05003013 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003015 status += args.pgbase;
3016 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003017
3018 nfs_invalidate_atime(dir);
3019
Harvey Harrison3110ff82008-05-02 13:42:44 -07003020 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return status;
3022}
3023
3024static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003025 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
3027 struct nfs4_exception exception = { };
3028 int err;
3029 do {
3030 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3031 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003032 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 &exception);
3034 } while (exception.retry);
3035 return err;
3036}
3037
3038static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3039 struct iattr *sattr, dev_t rdev)
3040{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041 struct nfs4_createdata *data;
3042 int mode = sattr->ia_mode;
3043 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3046 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047
3048 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3049 if (data == NULL)
3050 goto out;
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003053 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 data->arg.ftype = NF4BLK;
3056 data->arg.u.device.specdata1 = MAJOR(rdev);
3057 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 data->arg.ftype = NF4CHR;
3061 data->arg.u.device.specdata1 = MAJOR(rdev);
3062 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003065 status = nfs4_do_create(dir, dentry, data);
3066
3067 nfs4_free_createdata(data);
3068out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return status;
3070}
3071
3072static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3073 struct iattr *sattr, dev_t rdev)
3074{
3075 struct nfs4_exception exception = { };
3076 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003077
3078 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 do {
3080 err = nfs4_handle_exception(NFS_SERVER(dir),
3081 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3082 &exception);
3083 } while (exception.retry);
3084 return err;
3085}
3086
3087static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3088 struct nfs_fsstat *fsstat)
3089{
3090 struct nfs4_statfs_arg args = {
3091 .fh = fhandle,
3092 .bitmask = server->attr_bitmask,
3093 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003094 struct nfs4_statfs_res res = {
3095 .fsstat = fsstat,
3096 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 struct rpc_message msg = {
3098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3099 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003100 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 };
3102
Trond Myklebust0e574af2005-10-27 22:12:38 -04003103 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003104 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105}
3106
3107static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3108{
3109 struct nfs4_exception exception = { };
3110 int err;
3111 do {
3112 err = nfs4_handle_exception(server,
3113 _nfs4_proc_statfs(server, fhandle, fsstat),
3114 &exception);
3115 } while (exception.retry);
3116 return err;
3117}
3118
3119static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3120 struct nfs_fsinfo *fsinfo)
3121{
3122 struct nfs4_fsinfo_arg args = {
3123 .fh = fhandle,
3124 .bitmask = server->attr_bitmask,
3125 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003126 struct nfs4_fsinfo_res res = {
3127 .fsinfo = fsinfo,
3128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3131 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003132 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 };
3134
Bryan Schumaker7c513052011-03-24 17:12:24 +00003135 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
3138static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3139{
3140 struct nfs4_exception exception = { };
3141 int err;
3142
3143 do {
3144 err = nfs4_handle_exception(server,
3145 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3146 &exception);
3147 } while (exception.retry);
3148 return err;
3149}
3150
3151static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3152{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003153 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3155}
3156
3157static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3158 struct nfs_pathconf *pathconf)
3159{
3160 struct nfs4_pathconf_arg args = {
3161 .fh = fhandle,
3162 .bitmask = server->attr_bitmask,
3163 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003164 struct nfs4_pathconf_res res = {
3165 .pathconf = pathconf,
3166 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3169 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003170 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 };
3172
3173 /* None of the pathconf attributes are mandatory to implement */
3174 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3175 memset(pathconf, 0, sizeof(*pathconf));
3176 return 0;
3177 }
3178
Trond Myklebust0e574af2005-10-27 22:12:38 -04003179 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003180 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
3183static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3184 struct nfs_pathconf *pathconf)
3185{
3186 struct nfs4_exception exception = { };
3187 int err;
3188
3189 do {
3190 err = nfs4_handle_exception(server,
3191 _nfs4_proc_pathconf(server, fhandle, pathconf),
3192 &exception);
3193 } while (exception.retry);
3194 return err;
3195}
3196
Benny Halevyd20581a2011-05-22 19:52:03 +03003197void __nfs4_read_done_cb(struct nfs_read_data *data)
3198{
3199 nfs_invalidate_atime(data->inode);
3200}
3201
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003202static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Trond Myklebustec06c092006-03-20 13:44:27 -05003204 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003206 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003207 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003208 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003210
Benny Halevyd20581a2011-05-22 19:52:03 +03003211 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003213 renew_lease(server, data->timestamp);
3214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003217static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3218{
3219
3220 dprintk("--> %s\n", __func__);
3221
3222 if (!nfs4_sequence_done(task, &data->res.seq_res))
3223 return -EAGAIN;
3224
Benny Halevyd20581a2011-05-22 19:52:03 +03003225 return data->read_done_cb ? data->read_done_cb(task, data) :
3226 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003227}
3228
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003229static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003232 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003233 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003236/* Reset the the nfs_read_data to send the read to the MDS. */
3237void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3238{
3239 dprintk("%s Reset task for i/o through\n", __func__);
3240 put_lseg(data->lseg);
3241 data->lseg = NULL;
3242 /* offsets will differ in the dense stripe case */
3243 data->args.offset = data->mds_offset;
3244 data->ds_clp = NULL;
3245 data->args.fh = NFS_FH(data->inode);
3246 data->read_done_cb = nfs4_read_done_cb;
3247 task->tk_ops = data->mds_ops;
3248 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3249}
3250EXPORT_SYMBOL_GPL(nfs4_reset_read);
3251
Fred Isamanb029bc92011-03-03 15:13:42 +00003252static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct inode *inode = data->inode;
3255
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003256 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003257 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003258 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003260 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003262 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003263 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
Fred Isamanb029bc92011-03-03 15:13:42 +00003267static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3268{
3269 if (!nfs4_sequence_done(task, &data->res.seq_res))
3270 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003271 return data->write_done_cb ? data->write_done_cb(task, data) :
3272 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003273}
3274
Fred Isamana69aef12011-03-03 15:13:47 +00003275/* Reset the the nfs_write_data to send the write to the MDS. */
3276void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3277{
3278 dprintk("%s Reset task for i/o through\n", __func__);
3279 put_lseg(data->lseg);
3280 data->lseg = NULL;
3281 data->ds_clp = NULL;
3282 data->write_done_cb = nfs4_write_done_cb;
3283 data->args.fh = NFS_FH(data->inode);
3284 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3285 data->args.offset = data->mds_offset;
3286 data->res.fattr = &data->fattr;
3287 task->tk_ops = data->mds_ops;
3288 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3289}
3290EXPORT_SYMBOL_GPL(nfs4_reset_write);
3291
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003292static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003294 struct nfs_server *server = NFS_SERVER(data->inode);
3295
Fred Isaman7ffd1062011-03-03 15:13:46 +00003296 if (data->lseg) {
3297 data->args.bitmask = NULL;
3298 data->res.fattr = NULL;
3299 } else
3300 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003301 if (!data->write_done_cb)
3302 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003303 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 data->timestamp = jiffies;
3305
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003306 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Fred Isaman5f452432011-03-23 13:27:46 +00003309static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003312
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003313 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003314 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003315 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003317 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319}
3320
Fred Isaman5f452432011-03-23 13:27:46 +00003321static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3322{
3323 if (!nfs4_sequence_done(task, &data->res.seq_res))
3324 return -EAGAIN;
3325 return data->write_done_cb(task, data);
3326}
3327
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003328static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003330 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003331
3332 if (data->lseg) {
3333 data->args.bitmask = NULL;
3334 data->res.fattr = NULL;
3335 } else
3336 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003337 if (!data->write_done_cb)
3338 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003339 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003340 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003343struct nfs4_renewdata {
3344 struct nfs_client *client;
3345 unsigned long timestamp;
3346};
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348/*
3349 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3350 * standalone procedure for queueing an asynchronous RENEW.
3351 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003352static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003353{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003354 struct nfs4_renewdata *data = calldata;
3355 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003356
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003357 if (atomic_read(&clp->cl_count) > 1)
3358 nfs4_schedule_state_renewal(clp);
3359 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003360 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003361}
3362
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003363static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003365 struct nfs4_renewdata *data = calldata;
3366 struct nfs_client *clp = data->client;
3367 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003370 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003371 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3372 return;
3373 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003374 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003375 return;
3376 }
3377 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003379 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380}
3381
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003382static const struct rpc_call_ops nfs4_renew_ops = {
3383 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003384 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003385};
3386
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003387static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388{
3389 struct rpc_message msg = {
3390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3391 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003392 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003394 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003396 if (renew_flags == 0)
3397 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003398 if (!atomic_inc_not_zero(&clp->cl_count))
3399 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003400 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003401 if (data == NULL)
3402 return -ENOMEM;
3403 data->client = clp;
3404 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003406 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003409static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 struct rpc_message msg = {
3412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3413 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003414 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 };
3416 unsigned long now = jiffies;
3417 int status;
3418
3419 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3420 if (status < 0)
3421 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003422 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return 0;
3424}
3425
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003426static inline int nfs4_server_supports_acls(struct nfs_server *server)
3427{
3428 return (server->caps & NFS_CAP_ACLS)
3429 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3430 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3431}
3432
3433/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3434 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3435 * the stack.
3436 */
3437#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3438
Neil Hormane9e3d722011-03-04 19:26:03 -05003439static int buf_to_pages_noslab(const void *buf, size_t buflen,
3440 struct page **pages, unsigned int *pgbase)
3441{
3442 struct page *newpage, **spages;
3443 int rc = 0;
3444 size_t len;
3445 spages = pages;
3446
3447 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003448 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003449 newpage = alloc_page(GFP_KERNEL);
3450
3451 if (newpage == NULL)
3452 goto unwind;
3453 memcpy(page_address(newpage), buf, len);
3454 buf += len;
3455 buflen -= len;
3456 *pages++ = newpage;
3457 rc++;
3458 } while (buflen != 0);
3459
3460 return rc;
3461
3462unwind:
3463 for(; rc > 0; rc--)
3464 __free_page(spages[rc-1]);
3465 return -ENOMEM;
3466}
3467
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003468struct nfs4_cached_acl {
3469 int cached;
3470 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003471 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003472};
3473
3474static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003475{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003476 struct nfs_inode *nfsi = NFS_I(inode);
3477
3478 spin_lock(&inode->i_lock);
3479 kfree(nfsi->nfs4_acl);
3480 nfsi->nfs4_acl = acl;
3481 spin_unlock(&inode->i_lock);
3482}
3483
3484static void nfs4_zap_acl_attr(struct inode *inode)
3485{
3486 nfs4_set_cached_acl(inode, NULL);
3487}
3488
3489static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3490{
3491 struct nfs_inode *nfsi = NFS_I(inode);
3492 struct nfs4_cached_acl *acl;
3493 int ret = -ENOENT;
3494
3495 spin_lock(&inode->i_lock);
3496 acl = nfsi->nfs4_acl;
3497 if (acl == NULL)
3498 goto out;
3499 if (buf == NULL) /* user is just asking for length */
3500 goto out_len;
3501 if (acl->cached == 0)
3502 goto out;
3503 ret = -ERANGE; /* see getxattr(2) man page */
3504 if (acl->len > buflen)
3505 goto out;
3506 memcpy(buf, acl->data, acl->len);
3507out_len:
3508 ret = acl->len;
3509out:
3510 spin_unlock(&inode->i_lock);
3511 return ret;
3512}
3513
3514static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3515{
3516 struct nfs4_cached_acl *acl;
3517
3518 if (buf && acl_len <= PAGE_SIZE) {
3519 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3520 if (acl == NULL)
3521 goto out;
3522 acl->cached = 1;
3523 memcpy(acl->data, buf, acl_len);
3524 } else {
3525 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3526 if (acl == NULL)
3527 goto out;
3528 acl->cached = 0;
3529 }
3530 acl->len = acl_len;
3531out:
3532 nfs4_set_cached_acl(inode, acl);
3533}
3534
Andy Adamsonbf118a32011-12-07 11:55:27 -05003535/*
3536 * The getxattr API returns the required buffer length when called with a
3537 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3538 * the required buf. On a NULL buf, we send a page of data to the server
3539 * guessing that the ACL request can be serviced by a page. If so, we cache
3540 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3541 * the cache. If not so, we throw away the page, and cache the required
3542 * length. The next getxattr call will then produce another round trip to
3543 * the server, this time with the input buf of the required size.
3544 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003545static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003546{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003547 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003548 struct nfs_getaclargs args = {
3549 .fh = NFS_FH(inode),
3550 .acl_pages = pages,
3551 .acl_len = buflen,
3552 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003553 struct nfs_getaclres res = {
3554 .acl_len = buflen,
3555 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003556 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3559 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003560 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003561 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003562 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003563
Andy Adamsonbf118a32011-12-07 11:55:27 -05003564 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3565 /* As long as we're doing a round trip to the server anyway,
3566 * let's be prepared for a page of acl data. */
3567 if (npages == 0)
3568 npages = 1;
3569
3570 for (i = 0; i < npages; i++) {
3571 pages[i] = alloc_page(GFP_KERNEL);
3572 if (!pages[i])
3573 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003574 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003575 if (npages > 1) {
3576 /* for decoding across pages */
3577 args.acl_scratch = alloc_page(GFP_KERNEL);
3578 if (!args.acl_scratch)
3579 goto out_free;
3580 }
3581 args.acl_len = npages * PAGE_SIZE;
3582 args.acl_pgbase = 0;
3583 /* Let decode_getfacl know not to fail if the ACL data is larger than
3584 * the page we send as a guess */
3585 if (buf == NULL)
3586 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3587 resp_buf = page_address(pages[0]);
3588
3589 dprintk("%s buf %p buflen %ld npages %d args.acl_len %ld\n",
3590 __func__, buf, buflen, npages, args.acl_len);
3591 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3592 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003593 if (ret)
3594 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003595
3596 acl_len = res.acl_len - res.acl_data_offset;
3597 if (acl_len > args.acl_len)
3598 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003599 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003600 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3601 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003602 if (buf) {
3603 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003604 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003605 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003606 _copy_from_pages(buf, pages, res.acl_data_offset,
3607 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003608 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003609 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003611 for (i = 0; i < npages; i++)
3612 if (pages[i])
3613 __free_page(pages[i]);
3614 if (args.acl_scratch)
3615 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003616 return ret;
3617}
3618
Trond Myklebust16b42892006-08-24 12:27:15 -04003619static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3620{
3621 struct nfs4_exception exception = { };
3622 ssize_t ret;
3623 do {
3624 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3625 if (ret >= 0)
3626 break;
3627 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3628 } while (exception.retry);
3629 return ret;
3630}
3631
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003632static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3633{
3634 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003635 int ret;
3636
3637 if (!nfs4_server_supports_acls(server))
3638 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003639 ret = nfs_revalidate_inode(server, inode);
3640 if (ret < 0)
3641 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003642 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3643 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003644 ret = nfs4_read_cached_acl(inode, buf, buflen);
3645 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003646 /* -ENOENT is returned if there is no ACL or if there is an ACL
3647 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003648 return ret;
3649 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003650}
3651
Trond Myklebust16b42892006-08-24 12:27:15 -04003652static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003653{
3654 struct nfs_server *server = NFS_SERVER(inode);
3655 struct page *pages[NFS4ACL_MAXPAGES];
3656 struct nfs_setaclargs arg = {
3657 .fh = NFS_FH(inode),
3658 .acl_pages = pages,
3659 .acl_len = buflen,
3660 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003661 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003662 struct rpc_message msg = {
3663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3664 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003665 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003666 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003667 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003668
3669 if (!nfs4_server_supports_acls(server))
3670 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003671 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3672 if (i < 0)
3673 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003674 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003675 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003676
3677 /*
3678 * Free each page after tx, so the only ref left is
3679 * held by the network stack
3680 */
3681 for (; i > 0; i--)
3682 put_page(pages[i-1]);
3683
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003684 /*
3685 * Acl update can result in inode attribute update.
3686 * so mark the attribute cache invalid.
3687 */
3688 spin_lock(&inode->i_lock);
3689 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3690 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003691 nfs_access_zap_cache(inode);
3692 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003693 return ret;
3694}
3695
Trond Myklebust16b42892006-08-24 12:27:15 -04003696static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3697{
3698 struct nfs4_exception exception = { };
3699 int err;
3700 do {
3701 err = nfs4_handle_exception(NFS_SERVER(inode),
3702 __nfs4_proc_set_acl(inode, buf, buflen),
3703 &exception);
3704 } while (exception.retry);
3705 return err;
3706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003709nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003711 struct nfs_client *clp = server->nfs_client;
3712
3713 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return 0;
3715 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003716 case -NFS4ERR_ADMIN_REVOKED:
3717 case -NFS4ERR_BAD_STATEID:
3718 case -NFS4ERR_OPENMODE:
3719 if (state == NULL)
3720 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003721 nfs4_schedule_stateid_recovery(server, state);
3722 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003723 case -NFS4ERR_EXPIRED:
3724 if (state != NULL)
3725 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003727 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003728 nfs4_schedule_lease_recovery(clp);
3729 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003730#if defined(CONFIG_NFS_V4_1)
3731 case -NFS4ERR_BADSESSION:
3732 case -NFS4ERR_BADSLOT:
3733 case -NFS4ERR_BAD_HIGH_SLOT:
3734 case -NFS4ERR_DEADSESSION:
3735 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3736 case -NFS4ERR_SEQ_FALSE_RETRY:
3737 case -NFS4ERR_SEQ_MISORDERED:
3738 dprintk("%s ERROR %d, Reset session\n", __func__,
3739 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003740 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003741 task->tk_status = 0;
3742 return -EAGAIN;
3743#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003745 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003746 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003747 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3749 task->tk_status = 0;
3750 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003751 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 case -NFS4ERR_OLD_STATEID:
3753 task->tk_status = 0;
3754 return -EAGAIN;
3755 }
3756 task->tk_status = nfs4_map_errors(task->tk_status);
3757 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003758wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003759 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003760 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3761 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3762 task->tk_status = 0;
3763 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003766int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3767 unsigned short port, struct rpc_cred *cred,
3768 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 nfs4_verifier sc_verifier;
3771 struct nfs4_setclientid setclientid = {
3772 .sc_verifier = &sc_verifier,
3773 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003774 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 };
3776 struct rpc_message msg = {
3777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3778 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003779 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003780 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 };
Al Virobc4785c2006-10-19 23:28:51 -07003782 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 int loop = 0;
3784 int status;
3785
Al Virobc4785c2006-10-19 23:28:51 -07003786 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3788 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3789
3790 for(;;) {
3791 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003792 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003793 clp->cl_ipaddr,
3794 rpc_peeraddr2str(clp->cl_rpcclient,
3795 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003796 rpc_peeraddr2str(clp->cl_rpcclient,
3797 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003798 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 clp->cl_id_uniquifier);
3800 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003801 sizeof(setclientid.sc_netid),
3802 rpc_peeraddr2str(clp->cl_rpcclient,
3803 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003805 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 clp->cl_ipaddr, port >> 8, port & 255);
3807
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003808 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 if (status != -NFS4ERR_CLID_INUSE)
3810 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003811 if (loop != 0) {
3812 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003814 }
3815 ++loop;
3816 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818 return status;
3819}
3820
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003821int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003822 struct nfs4_setclientid_res *arg,
3823 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 struct nfs_fsinfo fsinfo;
3826 struct rpc_message msg = {
3827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003828 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003830 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 };
3832 unsigned long now;
3833 int status;
3834
3835 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003836 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 if (status == 0) {
3838 spin_lock(&clp->cl_lock);
3839 clp->cl_lease_time = fsinfo.lease_time * HZ;
3840 clp->cl_last_renewal = now;
3841 spin_unlock(&clp->cl_lock);
3842 }
3843 return status;
3844}
3845
Trond Myklebustfe650402006-01-03 09:55:18 +01003846struct nfs4_delegreturndata {
3847 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003848 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003849 struct nfs_fh fh;
3850 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003851 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003852 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003853 int rpc_status;
3854};
3855
Trond Myklebustfe650402006-01-03 09:55:18 +01003856static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3857{
3858 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003859
Trond Myklebust14516c32010-07-31 14:29:06 -04003860 if (!nfs4_sequence_done(task, &data->res.seq_res))
3861 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003862
Ricardo Labiaga79708862009-12-07 09:23:21 -05003863 switch (task->tk_status) {
3864 case -NFS4ERR_STALE_STATEID:
3865 case -NFS4ERR_EXPIRED:
3866 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003867 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003868 break;
3869 default:
3870 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3871 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003872 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003873 return;
3874 }
3875 }
3876 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003877}
3878
3879static void nfs4_delegreturn_release(void *calldata)
3880{
Trond Myklebustfe650402006-01-03 09:55:18 +01003881 kfree(calldata);
3882}
3883
Andy Adamson938e1012009-04-01 09:22:28 -04003884#if defined(CONFIG_NFS_V4_1)
3885static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3886{
3887 struct nfs4_delegreturndata *d_data;
3888
3889 d_data = (struct nfs4_delegreturndata *)data;
3890
Trond Myklebust035168a2010-06-16 09:52:26 -04003891 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003892 &d_data->args.seq_args,
3893 &d_data->res.seq_res, 1, task))
3894 return;
3895 rpc_call_start(task);
3896}
3897#endif /* CONFIG_NFS_V4_1 */
3898
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003899static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003900#if defined(CONFIG_NFS_V4_1)
3901 .rpc_call_prepare = nfs4_delegreturn_prepare,
3902#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003903 .rpc_call_done = nfs4_delegreturn_done,
3904 .rpc_release = nfs4_delegreturn_release,
3905};
3906
Trond Myklebuste6f81072008-01-24 18:14:34 -05003907static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003908{
3909 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003910 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003911 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003912 struct rpc_message msg = {
3913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3914 .rpc_cred = cred,
3915 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003916 struct rpc_task_setup task_setup_data = {
3917 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003918 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003919 .callback_ops = &nfs4_delegreturn_ops,
3920 .flags = RPC_TASK_ASYNC,
3921 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003922 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003923
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003924 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003925 if (data == NULL)
3926 return -ENOMEM;
3927 data->args.fhandle = &data->fh;
3928 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003929 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003930 nfs_copy_fh(&data->fh, NFS_FH(inode));
3931 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003932 data->res.fattr = &data->fattr;
3933 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003934 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003935 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003936 data->rpc_status = 0;
3937
Trond Myklebustc970aa82007-07-14 15:39:59 -04003938 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003939 msg.rpc_argp = &data->args;
3940 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003941 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003942 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003943 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003944 if (!issync)
3945 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003946 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003947 if (status != 0)
3948 goto out;
3949 status = data->rpc_status;
3950 if (status != 0)
3951 goto out;
3952 nfs_refresh_inode(inode, &data->fattr);
3953out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003954 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003955 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Trond Myklebuste6f81072008-01-24 18:14:34 -05003958int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
3960 struct nfs_server *server = NFS_SERVER(inode);
3961 struct nfs4_exception exception = { };
3962 int err;
3963 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003964 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 switch (err) {
3966 case -NFS4ERR_STALE_STATEID:
3967 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 case 0:
3969 return 0;
3970 }
3971 err = nfs4_handle_exception(server, err, &exception);
3972 } while (exception.retry);
3973 return err;
3974}
3975
3976#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3977#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3978
3979/*
3980 * sleep, with exponential backoff, and retry the LOCK operation.
3981 */
3982static unsigned long
3983nfs4_set_lock_task_retry(unsigned long timeout)
3984{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003985 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 timeout <<= 1;
3987 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3988 return NFS4_LOCK_MAXTIMEOUT;
3989 return timeout;
3990}
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3993{
3994 struct inode *inode = state->inode;
3995 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003996 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003997 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003999 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004001 struct nfs_lockt_res res = {
4002 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 };
4004 struct rpc_message msg = {
4005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4006 .rpc_argp = &arg,
4007 .rpc_resp = &res,
4008 .rpc_cred = state->owner->so_cred,
4009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 struct nfs4_lock_state *lsp;
4011 int status;
4012
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004013 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004014 status = nfs4_set_lock_state(state, request);
4015 if (status != 0)
4016 goto out;
4017 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004018 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004019 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004020 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004021 switch (status) {
4022 case 0:
4023 request->fl_type = F_UNLCK;
4024 break;
4025 case -NFS4ERR_DENIED:
4026 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004028 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return status;
4031}
4032
4033static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4034{
4035 struct nfs4_exception exception = { };
4036 int err;
4037
4038 do {
4039 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4040 _nfs4_proc_getlk(state, cmd, request),
4041 &exception);
4042 } while (exception.retry);
4043 return err;
4044}
4045
4046static int do_vfs_lock(struct file *file, struct file_lock *fl)
4047{
4048 int res = 0;
4049 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4050 case FL_POSIX:
4051 res = posix_lock_file_wait(file, fl);
4052 break;
4053 case FL_FLOCK:
4054 res = flock_lock_file_wait(file, fl);
4055 break;
4056 default:
4057 BUG();
4058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return res;
4060}
4061
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004062struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 struct nfs_locku_args arg;
4064 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004065 struct nfs4_lock_state *lsp;
4066 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004067 struct file_lock fl;
4068 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004069 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004070};
4071
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004072static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4073 struct nfs_open_context *ctx,
4074 struct nfs4_lock_state *lsp,
4075 struct nfs_seqid *seqid)
4076{
4077 struct nfs4_unlockdata *p;
4078 struct inode *inode = lsp->ls_state->inode;
4079
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004080 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004081 if (p == NULL)
4082 return NULL;
4083 p->arg.fh = NFS_FH(inode);
4084 p->arg.fl = &p->fl;
4085 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004086 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004087 p->arg.stateid = &lsp->ls_stateid;
4088 p->lsp = lsp;
4089 atomic_inc(&lsp->ls_count);
4090 /* Ensure we don't close file until we're done freeing locks! */
4091 p->ctx = get_nfs_open_context(ctx);
4092 memcpy(&p->fl, fl, sizeof(p->fl));
4093 p->server = NFS_SERVER(inode);
4094 return p;
4095}
4096
Trond Myklebust06f814a2006-01-03 09:55:07 +01004097static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004098{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004099 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004100 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004101 nfs4_put_lock_state(calldata->lsp);
4102 put_nfs_open_context(calldata->ctx);
4103 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104}
4105
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004106static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004107{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004108 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004109
Trond Myklebust14516c32010-07-31 14:29:06 -04004110 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4111 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004112 switch (task->tk_status) {
4113 case 0:
4114 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004115 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004116 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004117 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004118 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004119 case -NFS4ERR_BAD_STATEID:
4120 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004121 case -NFS4ERR_STALE_STATEID:
4122 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004123 break;
4124 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004125 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004126 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004127 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004128}
4129
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004130static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004131{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004132 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004134 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004135 return;
4136 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004137 /* Note: exit _without_ running nfs4_locku_done */
4138 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004139 return;
4140 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004141 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004142 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004143 &calldata->arg.seq_args,
4144 &calldata->res.seq_res, 1, task))
4145 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004146 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
4148
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004149static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004150 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004151 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004152 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004153};
4154
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4156 struct nfs_open_context *ctx,
4157 struct nfs4_lock_state *lsp,
4158 struct nfs_seqid *seqid)
4159{
4160 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004161 struct rpc_message msg = {
4162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4163 .rpc_cred = ctx->cred,
4164 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004165 struct rpc_task_setup task_setup_data = {
4166 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004167 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004168 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004169 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004170 .flags = RPC_TASK_ASYNC,
4171 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172
Frank Filz137d6ac2007-07-09 15:32:29 -07004173 /* Ensure this is an unlock - when canceling a lock, the
4174 * canceled lock is passed in, and it won't be an unlock.
4175 */
4176 fl->fl_type = F_UNLCK;
4177
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004178 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4179 if (data == NULL) {
4180 nfs_free_seqid(seqid);
4181 return ERR_PTR(-ENOMEM);
4182 }
4183
Trond Myklebust1174dd12010-12-21 10:52:24 -05004184 msg.rpc_argp = &data->arg;
4185 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004186 task_setup_data.callback_data = data;
4187 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188}
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4191{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004192 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004193 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004194 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004195 struct rpc_task *task;
4196 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004197 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Trond Myklebust9b073572006-06-29 16:38:34 -04004199 status = nfs4_set_lock_state(state, request);
4200 /* Unlock _before_ we do the RPC call */
4201 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004202 down_read(&nfsi->rwsem);
4203 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4204 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004205 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004206 }
4207 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004208 if (status != 0)
4209 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004210 /* Is this a delegated lock? */
4211 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004212 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004213 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004214 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004215 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004216 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004217 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004218 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219 status = PTR_ERR(task);
4220 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004221 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004223 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004224out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004225 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004226 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004229struct nfs4_lockdata {
4230 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004231 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 struct nfs4_lock_state *lsp;
4233 struct nfs_open_context *ctx;
4234 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004235 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 int rpc_status;
4237 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004238 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239};
4240
4241static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004242 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4243 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004244{
4245 struct nfs4_lockdata *p;
4246 struct inode *inode = lsp->ls_state->inode;
4247 struct nfs_server *server = NFS_SERVER(inode);
4248
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004249 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250 if (p == NULL)
4251 return NULL;
4252
4253 p->arg.fh = NFS_FH(inode);
4254 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004255 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004256 if (p->arg.open_seqid == NULL)
4257 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004258 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004259 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004260 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004261 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004262 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004263 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004264 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004265 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004266 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004267 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268 atomic_inc(&lsp->ls_count);
4269 p->ctx = get_nfs_open_context(ctx);
4270 memcpy(&p->fl, fl, sizeof(p->fl));
4271 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004272out_free_seqid:
4273 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004274out_free:
4275 kfree(p);
4276 return NULL;
4277}
4278
4279static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4280{
4281 struct nfs4_lockdata *data = calldata;
4282 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004285 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4286 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287 /* Do we need to do an open_to_lock_owner? */
4288 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004289 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4290 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291 data->arg.open_stateid = &state->stateid;
4292 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004293 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004294 } else
4295 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004296 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004297 if (nfs4_setup_sequence(data->server,
4298 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004299 &data->res.seq_res, 1, task))
4300 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004301 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004302 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004303}
4304
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004305static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4306{
4307 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4308 nfs4_lock_prepare(task, calldata);
4309}
4310
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4312{
4313 struct nfs4_lockdata *data = calldata;
4314
Harvey Harrison3110ff82008-05-02 13:42:44 -07004315 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004316
Trond Myklebust14516c32010-07-31 14:29:06 -04004317 if (!nfs4_sequence_done(task, &data->res.seq_res))
4318 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004319
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004320 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 if (data->rpc_status == 0)
4323 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4324 else
4325 goto out;
4326 }
4327 if (data->rpc_status == 0) {
4328 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4329 sizeof(data->lsp->ls_stateid.data));
4330 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004331 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004332 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004334 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004335}
4336
4337static void nfs4_lock_release(void *calldata)
4338{
4339 struct nfs4_lockdata *data = calldata;
4340
Harvey Harrison3110ff82008-05-02 13:42:44 -07004341 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004342 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004343 if (data->cancelled != 0) {
4344 struct rpc_task *task;
4345 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4346 data->arg.lock_seqid);
4347 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004348 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004349 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004350 } else
4351 nfs_free_seqid(data->arg.lock_seqid);
4352 nfs4_put_lock_state(data->lsp);
4353 put_nfs_open_context(data->ctx);
4354 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004355 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004356}
4357
4358static const struct rpc_call_ops nfs4_lock_ops = {
4359 .rpc_call_prepare = nfs4_lock_prepare,
4360 .rpc_call_done = nfs4_lock_done,
4361 .rpc_release = nfs4_lock_release,
4362};
4363
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004364static const struct rpc_call_ops nfs4_recover_lock_ops = {
4365 .rpc_call_prepare = nfs4_recover_lock_prepare,
4366 .rpc_call_done = nfs4_lock_done,
4367 .rpc_release = nfs4_lock_release,
4368};
4369
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004370static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4371{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004372 switch (error) {
4373 case -NFS4ERR_ADMIN_REVOKED:
4374 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004375 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004376 if (new_lock_owner != 0 ||
4377 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004378 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004379 break;
4380 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004381 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004382 case -NFS4ERR_EXPIRED:
4383 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004384 };
4385}
4386
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004387static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004388{
4389 struct nfs4_lockdata *data;
4390 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004391 struct rpc_message msg = {
4392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4393 .rpc_cred = state->owner->so_cred,
4394 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004395 struct rpc_task_setup task_setup_data = {
4396 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004397 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004398 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004399 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004400 .flags = RPC_TASK_ASYNC,
4401 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004402 int ret;
4403
Harvey Harrison3110ff82008-05-02 13:42:44 -07004404 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004405 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004406 fl->fl_u.nfs4_fl.owner,
4407 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004408 if (data == NULL)
4409 return -ENOMEM;
4410 if (IS_SETLKW(cmd))
4411 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004412 if (recovery_type > NFS_LOCK_NEW) {
4413 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004414 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004415 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4416 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004417 msg.rpc_argp = &data->arg;
4418 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004419 task_setup_data.callback_data = data;
4420 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004421 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423 ret = nfs4_wait_for_completion_rpc_task(task);
4424 if (ret == 0) {
4425 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004426 if (ret)
4427 nfs4_handle_setlk_error(data->server, data->lsp,
4428 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004429 } else
4430 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004431 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004432 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
4436static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4437{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004438 struct nfs_server *server = NFS_SERVER(state->inode);
4439 struct nfs4_exception exception = { };
4440 int err;
4441
4442 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004443 /* Cache the lock if possible... */
4444 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4445 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004446 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004447 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004448 break;
4449 nfs4_handle_exception(server, err, &exception);
4450 } while (exception.retry);
4451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
4454static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4455{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004456 struct nfs_server *server = NFS_SERVER(state->inode);
4457 struct nfs4_exception exception = { };
4458 int err;
4459
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004460 err = nfs4_set_lock_state(state, request);
4461 if (err != 0)
4462 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004463 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004464 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4465 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004466 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004467 switch (err) {
4468 default:
4469 goto out;
4470 case -NFS4ERR_GRACE:
4471 case -NFS4ERR_DELAY:
4472 nfs4_handle_exception(server, err, &exception);
4473 err = 0;
4474 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004475 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004476out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004477 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004480#if defined(CONFIG_NFS_V4_1)
4481static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4482{
4483 int status;
4484 struct nfs_server *server = NFS_SERVER(state->inode);
4485
4486 status = nfs41_test_stateid(server, state);
4487 if (status == NFS_OK)
4488 return 0;
4489 nfs41_free_stateid(server, state);
4490 return nfs4_lock_expired(state, request);
4491}
4492#endif
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4495{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004496 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004498 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004500 if ((fl_flags & FL_POSIX) &&
4501 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4502 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004503 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004504 status = nfs4_set_lock_state(state, request);
4505 if (status != 0)
4506 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004507 request->fl_flags |= FL_ACCESS;
4508 status = do_vfs_lock(request->fl_file, request);
4509 if (status < 0)
4510 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004511 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004512 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004513 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004514 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004515 request->fl_flags = fl_flags & ~FL_SLEEP;
4516 status = do_vfs_lock(request->fl_file, request);
4517 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004518 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004519 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004520 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004521 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004522 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004523 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004524 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004525 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004526out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004527 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004528out:
4529 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return status;
4531}
4532
4533static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4534{
4535 struct nfs4_exception exception = { };
4536 int err;
4537
4538 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004539 err = _nfs4_proc_setlk(state, cmd, request);
4540 if (err == -NFS4ERR_DENIED)
4541 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004543 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 } while (exception.retry);
4545 return err;
4546}
4547
4548static int
4549nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4550{
4551 struct nfs_open_context *ctx;
4552 struct nfs4_state *state;
4553 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4554 int status;
4555
4556 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004557 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 state = ctx->state;
4559
4560 if (request->fl_start < 0 || request->fl_end < 0)
4561 return -EINVAL;
4562
Trond Myklebustd9531262009-07-21 19:22:38 -04004563 if (IS_GETLK(cmd)) {
4564 if (state != NULL)
4565 return nfs4_proc_getlk(state, F_GETLK, request);
4566 return 0;
4567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4570 return -EINVAL;
4571
Trond Myklebustd9531262009-07-21 19:22:38 -04004572 if (request->fl_type == F_UNLCK) {
4573 if (state != NULL)
4574 return nfs4_proc_unlck(state, cmd, request);
4575 return 0;
4576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577
Trond Myklebustd9531262009-07-21 19:22:38 -04004578 if (state == NULL)
4579 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 do {
4581 status = nfs4_proc_setlk(state, cmd, request);
4582 if ((status != -EAGAIN) || IS_SETLK(cmd))
4583 break;
4584 timeout = nfs4_set_lock_task_retry(timeout);
4585 status = -ERESTARTSYS;
4586 if (signalled())
4587 break;
4588 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 return status;
4590}
4591
Trond Myklebust888e6942005-11-04 15:38:11 -05004592int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4593{
4594 struct nfs_server *server = NFS_SERVER(state->inode);
4595 struct nfs4_exception exception = { };
4596 int err;
4597
4598 err = nfs4_set_lock_state(state, fl);
4599 if (err != 0)
4600 goto out;
4601 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004602 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004603 switch (err) {
4604 default:
4605 printk(KERN_ERR "%s: unhandled error %d.\n",
4606 __func__, err);
4607 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004608 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004609 goto out;
4610 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004611 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004612 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004613 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004614 nfs4_schedule_lease_recovery(server->nfs_client);
4615 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004616 case -NFS4ERR_BADSESSION:
4617 case -NFS4ERR_BADSLOT:
4618 case -NFS4ERR_BAD_HIGH_SLOT:
4619 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4620 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004621 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004622 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004623 case -ERESTARTSYS:
4624 /*
4625 * The show must go on: exit, but mark the
4626 * stateid as needing recovery.
4627 */
4628 case -NFS4ERR_ADMIN_REVOKED:
4629 case -NFS4ERR_BAD_STATEID:
4630 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004631 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004632 err = 0;
4633 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004634 case -EKEYEXPIRED:
4635 /*
4636 * User RPCSEC_GSS context has expired.
4637 * We cannot recover this stateid now, so
4638 * skip it and allow recovery thread to
4639 * proceed.
4640 */
4641 err = 0;
4642 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004643 case -ENOMEM:
4644 case -NFS4ERR_DENIED:
4645 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4646 err = 0;
4647 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004648 case -NFS4ERR_DELAY:
4649 break;
4650 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004651 err = nfs4_handle_exception(server, err, &exception);
4652 } while (exception.retry);
4653out:
4654 return err;
4655}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004656
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004657static void nfs4_release_lockowner_release(void *calldata)
4658{
4659 kfree(calldata);
4660}
4661
4662const struct rpc_call_ops nfs4_release_lockowner_ops = {
4663 .rpc_release = nfs4_release_lockowner_release,
4664};
4665
4666void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4667{
4668 struct nfs_server *server = lsp->ls_state->owner->so_server;
4669 struct nfs_release_lockowner_args *args;
4670 struct rpc_message msg = {
4671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4672 };
4673
4674 if (server->nfs_client->cl_mvops->minor_version != 0)
4675 return;
4676 args = kmalloc(sizeof(*args), GFP_NOFS);
4677 if (!args)
4678 return;
4679 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4680 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004681 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004682 msg.rpc_argp = args;
4683 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4684}
4685
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004686#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4687
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004688static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4689 const void *buf, size_t buflen,
4690 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004691{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004692 if (strcmp(key, "") != 0)
4693 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004694
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004695 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004696}
4697
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004698static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4699 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004700{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004701 if (strcmp(key, "") != 0)
4702 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004703
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004704 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004705}
4706
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004707static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4708 size_t list_len, const char *name,
4709 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004710{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004711 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004712
J. Bruce Fields096455a2006-03-20 23:23:42 -05004713 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4714 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004715
4716 if (list && len <= list_len)
4717 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004718 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004719}
4720
Andy Adamson533eb462011-06-13 18:25:56 -04004721/*
4722 * nfs_fhget will use either the mounted_on_fileid or the fileid
4723 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004724static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4725{
Andy Adamson533eb462011-06-13 18:25:56 -04004726 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4727 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4728 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4729 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004730 return;
4731
4732 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4733 NFS_ATTR_FATTR_NLINK;
4734 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4735 fattr->nlink = 2;
4736}
4737
Trond Myklebust56659e92007-07-17 21:52:39 -04004738int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004739 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004740{
4741 struct nfs_server *server = NFS_SERVER(dir);
4742 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004743 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004744 };
4745 struct nfs4_fs_locations_arg args = {
4746 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004747 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004748 .page = page,
4749 .bitmask = bitmask,
4750 };
Benny Halevy22958462009-04-01 09:22:02 -04004751 struct nfs4_fs_locations_res res = {
4752 .fs_locations = fs_locations,
4753 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004754 struct rpc_message msg = {
4755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4756 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004757 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004758 };
4759 int status;
4760
Harvey Harrison3110ff82008-05-02 13:42:44 -07004761 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004762
4763 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4764 * is not supported */
4765 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4766 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4767 else
4768 bitmask[0] |= FATTR4_WORD0_FILEID;
4769
Trond Myklebustc228fd32007-01-13 02:28:11 -05004770 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004771 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004772 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004773 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004774 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004775 return status;
4776}
4777
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004778static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4779{
4780 int status;
4781 struct nfs4_secinfo_arg args = {
4782 .dir_fh = NFS_FH(dir),
4783 .name = name,
4784 };
4785 struct nfs4_secinfo_res res = {
4786 .flavors = flavors,
4787 };
4788 struct rpc_message msg = {
4789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4790 .rpc_argp = &args,
4791 .rpc_resp = &res,
4792 };
4793
4794 dprintk("NFS call secinfo %s\n", name->name);
4795 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4796 dprintk("NFS reply secinfo: %d\n", status);
4797 return status;
4798}
4799
4800int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4801{
4802 struct nfs4_exception exception = { };
4803 int err;
4804 do {
4805 err = nfs4_handle_exception(NFS_SERVER(dir),
4806 _nfs4_proc_secinfo(dir, name, flavors),
4807 &exception);
4808 } while (exception.retry);
4809 return err;
4810}
4811
Andy Adamson557134a2009-04-01 09:21:53 -04004812#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004813/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004814 * Check the exchange flags returned by the server for invalid flags, having
4815 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4816 * DS flags set.
4817 */
4818static int nfs4_check_cl_exchange_flags(u32 flags)
4819{
4820 if (flags & ~EXCHGID4_FLAG_MASK_R)
4821 goto out_inval;
4822 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4823 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4824 goto out_inval;
4825 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4826 goto out_inval;
4827 return NFS_OK;
4828out_inval:
4829 return -NFS4ERR_INVAL;
4830}
4831
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004832static bool
4833nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4834{
4835 if (a->server_scope_sz == b->server_scope_sz &&
4836 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4837 return true;
4838
4839 return false;
4840}
4841
Andy Adamson357f54d2010-12-14 10:11:57 -05004842/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004843 * nfs4_proc_exchange_id()
4844 *
4845 * Since the clientid has expired, all compounds using sessions
4846 * associated with the stale clientid will be returning
4847 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4848 * be in some phase of session reset.
4849 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004850int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004851{
4852 nfs4_verifier verifier;
4853 struct nfs41_exchange_id_args args = {
4854 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004855 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004856 };
4857 struct nfs41_exchange_id_res res = {
4858 .client = clp,
4859 };
4860 int status;
4861 struct rpc_message msg = {
4862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4863 .rpc_argp = &args,
4864 .rpc_resp = &res,
4865 .rpc_cred = cred,
4866 };
4867 __be32 *p;
4868
4869 dprintk("--> %s\n", __func__);
4870 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004871
Benny Halevy99fe60d2009-04-01 09:22:29 -04004872 p = (u32 *)verifier.data;
4873 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4874 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4875 args.verifier = &verifier;
4876
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004877 args.id_len = scnprintf(args.id, sizeof(args.id),
4878 "%s/%s.%s/%u",
4879 clp->cl_ipaddr,
4880 init_utsname()->nodename,
4881 init_utsname()->domainname,
4882 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004883
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004884 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4885 if (unlikely(!res.server_scope))
4886 return -ENOMEM;
4887
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004888 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004889 if (!status)
4890 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004891
4892 if (!status) {
4893 if (clp->server_scope &&
4894 !nfs41_same_server_scope(clp->server_scope,
4895 res.server_scope)) {
4896 dprintk("%s: server_scope mismatch detected\n",
4897 __func__);
4898 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4899 kfree(clp->server_scope);
4900 clp->server_scope = NULL;
4901 }
4902
4903 if (!clp->server_scope)
4904 clp->server_scope = res.server_scope;
4905 else
4906 kfree(res.server_scope);
4907 }
4908
Benny Halevy99fe60d2009-04-01 09:22:29 -04004909 dprintk("<-- %s status= %d\n", __func__, status);
4910 return status;
4911}
4912
Andy Adamson2050f0c2009-04-01 09:22:30 -04004913struct nfs4_get_lease_time_data {
4914 struct nfs4_get_lease_time_args *args;
4915 struct nfs4_get_lease_time_res *res;
4916 struct nfs_client *clp;
4917};
4918
4919static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4920 void *calldata)
4921{
4922 int ret;
4923 struct nfs4_get_lease_time_data *data =
4924 (struct nfs4_get_lease_time_data *)calldata;
4925
4926 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004927 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004928 /* just setup sequence, do not trigger session recovery
4929 since we're invoked within one */
4930 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004931 &data->args->la_seq_args,
4932 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004933
4934 BUG_ON(ret == -EAGAIN);
4935 rpc_call_start(task);
4936 dprintk("<-- %s\n", __func__);
4937}
4938
4939/*
4940 * Called from nfs4_state_manager thread for session setup, so don't recover
4941 * from sequence operation or clientid errors.
4942 */
4943static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4944{
4945 struct nfs4_get_lease_time_data *data =
4946 (struct nfs4_get_lease_time_data *)calldata;
4947
4948 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004949 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4950 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004951 switch (task->tk_status) {
4952 case -NFS4ERR_DELAY:
4953 case -NFS4ERR_GRACE:
4954 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4955 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4956 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004957 /* fall through */
4958 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004959 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004960 return;
4961 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004962 dprintk("<-- %s\n", __func__);
4963}
4964
4965struct rpc_call_ops nfs4_get_lease_time_ops = {
4966 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4967 .rpc_call_done = nfs4_get_lease_time_done,
4968};
4969
4970int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4971{
4972 struct rpc_task *task;
4973 struct nfs4_get_lease_time_args args;
4974 struct nfs4_get_lease_time_res res = {
4975 .lr_fsinfo = fsinfo,
4976 };
4977 struct nfs4_get_lease_time_data data = {
4978 .args = &args,
4979 .res = &res,
4980 .clp = clp,
4981 };
4982 struct rpc_message msg = {
4983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4984 .rpc_argp = &args,
4985 .rpc_resp = &res,
4986 };
4987 struct rpc_task_setup task_setup = {
4988 .rpc_client = clp->cl_rpcclient,
4989 .rpc_message = &msg,
4990 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004991 .callback_data = &data,
4992 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004993 };
4994 int status;
4995
Andy Adamson2050f0c2009-04-01 09:22:30 -04004996 dprintk("--> %s\n", __func__);
4997 task = rpc_run_task(&task_setup);
4998
4999 if (IS_ERR(task))
5000 status = PTR_ERR(task);
5001 else {
5002 status = task->tk_status;
5003 rpc_put_task(task);
5004 }
5005 dprintk("<-- %s return %d\n", __func__, status);
5006
5007 return status;
5008}
5009
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005010/*
5011 * Reset a slot table
5012 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005013static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5014 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005015{
Andy Adamson104aeba2010-01-14 17:45:10 -05005016 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005017 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005018 int ret = 0;
5019
Andy Adamson104aeba2010-01-14 17:45:10 -05005020 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5021 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005022
Andy Adamson104aeba2010-01-14 17:45:10 -05005023 /* Does the newly negotiated max_reqs match the existing slot table? */
5024 if (max_reqs != tbl->max_slots) {
5025 ret = -ENOMEM;
5026 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005027 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005028 if (!new)
5029 goto out;
5030 ret = 0;
5031 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005032 }
5033 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005034 if (new) {
5035 tbl->slots = new;
5036 tbl->max_slots = max_reqs;
5037 }
5038 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005039 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005040 spin_unlock(&tbl->slot_tbl_lock);
5041 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5042 tbl, tbl->slots, tbl->max_slots);
5043out:
5044 dprintk("<-- %s: return %d\n", __func__, ret);
5045 return ret;
5046}
5047
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005048/* Destroy the slot table */
5049static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5050{
5051 if (session->fc_slot_table.slots != NULL) {
5052 kfree(session->fc_slot_table.slots);
5053 session->fc_slot_table.slots = NULL;
5054 }
5055 if (session->bc_slot_table.slots != NULL) {
5056 kfree(session->bc_slot_table.slots);
5057 session->bc_slot_table.slots = NULL;
5058 }
5059 return;
5060}
5061
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005062/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005063 * Initialize slot table
5064 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005065static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5066 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005067{
Andy Adamsonfc931582009-04-01 09:22:31 -04005068 struct nfs4_slot *slot;
5069 int ret = -ENOMEM;
5070
5071 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5072
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005073 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005074
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005075 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005076 if (!slot)
5077 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005078 ret = 0;
5079
5080 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005081 tbl->max_slots = max_slots;
5082 tbl->slots = slot;
5083 tbl->highest_used_slotid = -1; /* no slot is currently used */
5084 spin_unlock(&tbl->slot_tbl_lock);
5085 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5086 tbl, tbl->slots, tbl->max_slots);
5087out:
5088 dprintk("<-- %s: return %d\n", __func__, ret);
5089 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005090}
5091
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005092/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005093 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005094 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005095static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005096{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005097 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005098 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005099
Andy Adamsonaacd5532011-11-09 13:58:21 -05005100 dprintk("--> %s\n", __func__);
5101 /* Fore channel */
5102 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005103 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005104 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5105 if (status) /* -ENOMEM */
5106 return status;
5107 } else {
5108 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005109 if (status)
5110 return status;
5111 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005112 /* Back channel */
5113 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005114 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005115 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005116 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005117 /* Fore and back channel share a connection so get
5118 * both slot tables or neither */
5119 nfs4_destroy_slot_tables(ses);
5120 } else
5121 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005122 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005123}
5124
5125struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5126{
5127 struct nfs4_session *session;
5128 struct nfs4_slot_table *tbl;
5129
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005130 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005131 if (!session)
5132 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005133
Andy Adamson557134a2009-04-01 09:21:53 -04005134 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005135 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005136 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005137 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005138 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005139
5140 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005141 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005142 spin_lock_init(&tbl->slot_tbl_lock);
5143 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005144 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005145
Trond Myklebust1055d762010-06-16 09:52:27 -04005146 session->session_state = 1<<NFS4_SESSION_INITING;
5147
Andy Adamson557134a2009-04-01 09:21:53 -04005148 session->clp = clp;
5149 return session;
5150}
5151
5152void nfs4_destroy_session(struct nfs4_session *session)
5153{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005154 nfs4_proc_destroy_session(session);
5155 dprintk("%s Destroy backchannel for xprt %p\n",
5156 __func__, session->clp->cl_rpcclient->cl_xprt);
5157 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5158 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005159 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005160 kfree(session);
5161}
5162
Andy Adamsonfc931582009-04-01 09:22:31 -04005163/*
5164 * Initialize the values to be used by the client in CREATE_SESSION
5165 * If nfs4_init_session set the fore channel request and response sizes,
5166 * use them.
5167 *
5168 * Set the back channel max_resp_sz_cached to zero to force the client to
5169 * always set csa_cachethis to FALSE because the current implementation
5170 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5171 */
5172static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5173{
5174 struct nfs4_session *session = args->client->cl_session;
5175 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5176 mxresp_sz = session->fc_attrs.max_resp_sz;
5177
5178 if (mxrqst_sz == 0)
5179 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5180 if (mxresp_sz == 0)
5181 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5182 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005183 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5184 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005185 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5186 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5187
5188 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005189 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005190 __func__,
5191 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005192 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005193
5194 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005195 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5196 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5197 args->bc_attrs.max_resp_sz_cached = 0;
5198 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5199 args->bc_attrs.max_reqs = 1;
5200
5201 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5202 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5203 __func__,
5204 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5205 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5206 args->bc_attrs.max_reqs);
5207}
5208
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005209static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005210{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005211 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5212 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5213
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005214 if (rcvd->max_resp_sz > sent->max_resp_sz)
5215 return -EINVAL;
5216 /*
5217 * Our requested max_ops is the minimum we need; we're not
5218 * prepared to break up compounds into smaller pieces than that.
5219 * So, no point even trying to continue if the server won't
5220 * cooperate:
5221 */
5222 if (rcvd->max_ops < sent->max_ops)
5223 return -EINVAL;
5224 if (rcvd->max_reqs == 0)
5225 return -EINVAL;
5226 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005227}
5228
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005229static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5230{
5231 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5232 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005233
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005234 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5235 return -EINVAL;
5236 if (rcvd->max_resp_sz < sent->max_resp_sz)
5237 return -EINVAL;
5238 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5239 return -EINVAL;
5240 /* These would render the backchannel useless: */
5241 if (rcvd->max_ops == 0)
5242 return -EINVAL;
5243 if (rcvd->max_reqs == 0)
5244 return -EINVAL;
5245 return 0;
5246}
Andy Adamson8d353012009-04-01 09:22:32 -04005247
Andy Adamson8d353012009-04-01 09:22:32 -04005248static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5249 struct nfs4_session *session)
5250{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005251 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005252
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005253 ret = nfs4_verify_fore_channel_attrs(args, session);
5254 if (ret)
5255 return ret;
5256 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005257}
5258
Andy Adamsonfc931582009-04-01 09:22:31 -04005259static int _nfs4_proc_create_session(struct nfs_client *clp)
5260{
5261 struct nfs4_session *session = clp->cl_session;
5262 struct nfs41_create_session_args args = {
5263 .client = clp,
5264 .cb_program = NFS4_CALLBACK,
5265 };
5266 struct nfs41_create_session_res res = {
5267 .client = clp,
5268 };
5269 struct rpc_message msg = {
5270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5271 .rpc_argp = &args,
5272 .rpc_resp = &res,
5273 };
5274 int status;
5275
5276 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005277 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005278
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005279 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005280
Andy Adamson8d353012009-04-01 09:22:32 -04005281 if (!status)
5282 /* Verify the session's negotiated channel_attrs values */
5283 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005284 if (!status) {
5285 /* Increment the clientid slot sequence id */
5286 clp->cl_seqid++;
5287 }
5288
5289 return status;
5290}
5291
5292/*
5293 * Issues a CREATE_SESSION operation to the server.
5294 * It is the responsibility of the caller to verify the session is
5295 * expired before calling this routine.
5296 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005297int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005298{
5299 int status;
5300 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005301 struct nfs4_session *session = clp->cl_session;
5302
5303 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5304
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005305 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005306 if (status)
5307 goto out;
5308
Andy Adamsonaacd5532011-11-09 13:58:21 -05005309 /* Init or reset the session slot tables */
5310 status = nfs4_setup_session_slot_tables(session);
5311 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005312 if (status)
5313 goto out;
5314
5315 ptr = (unsigned *)&session->sess_id.data[0];
5316 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5317 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005318out:
5319 dprintk("<-- %s\n", __func__);
5320 return status;
5321}
5322
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005323/*
5324 * Issue the over-the-wire RPC DESTROY_SESSION.
5325 * The caller must serialize access to this routine.
5326 */
5327int nfs4_proc_destroy_session(struct nfs4_session *session)
5328{
5329 int status = 0;
5330 struct rpc_message msg;
5331
5332 dprintk("--> nfs4_proc_destroy_session\n");
5333
5334 /* session is still being setup */
5335 if (session->clp->cl_cons_state != NFS_CS_READY)
5336 return status;
5337
5338 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5339 msg.rpc_argp = session;
5340 msg.rpc_resp = NULL;
5341 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005342 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005343
5344 if (status)
5345 printk(KERN_WARNING
5346 "Got error %d from the server on DESTROY_SESSION. "
5347 "Session has been destroyed regardless...\n", status);
5348
5349 dprintk("<-- nfs4_proc_destroy_session\n");
5350 return status;
5351}
5352
Trond Myklebustfccba802009-07-21 16:48:07 -04005353int nfs4_init_session(struct nfs_server *server)
5354{
5355 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005356 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005357 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005358 int ret;
5359
5360 if (!nfs4_has_session(clp))
5361 return 0;
5362
Trond Myklebust1055d762010-06-16 09:52:27 -04005363 session = clp->cl_session;
5364 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5365 return 0;
5366
Andy Adamson68bf05e2009-12-15 12:55:02 -05005367 rsize = server->rsize;
5368 if (rsize == 0)
5369 rsize = NFS_MAX_FILE_IO_SIZE;
5370 wsize = server->wsize;
5371 if (wsize == 0)
5372 wsize = NFS_MAX_FILE_IO_SIZE;
5373
Andy Adamson68bf05e2009-12-15 12:55:02 -05005374 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5375 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005376
Trond Myklebustfccba802009-07-21 16:48:07 -04005377 ret = nfs4_recover_expired_lease(server);
5378 if (!ret)
5379 ret = nfs4_check_client_ready(clp);
5380 return ret;
5381}
5382
Andy Adamsond83217c2011-03-01 01:34:17 +00005383int nfs4_init_ds_session(struct nfs_client *clp)
5384{
5385 struct nfs4_session *session = clp->cl_session;
5386 int ret;
5387
5388 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5389 return 0;
5390
5391 ret = nfs4_client_recover_expired_lease(clp);
5392 if (!ret)
5393 /* Test for the DS role */
5394 if (!is_ds_client(clp))
5395 ret = -ENODEV;
5396 if (!ret)
5397 ret = nfs4_check_client_ready(clp);
5398 return ret;
5399
5400}
5401EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5402
5403
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005404/*
5405 * Renew the cl_session lease.
5406 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005407struct nfs4_sequence_data {
5408 struct nfs_client *clp;
5409 struct nfs4_sequence_args args;
5410 struct nfs4_sequence_res res;
5411};
5412
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005413static void nfs41_sequence_release(void *data)
5414{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005415 struct nfs4_sequence_data *calldata = data;
5416 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005417
Alexandros Batsakis71358402010-02-05 03:45:05 -08005418 if (atomic_read(&clp->cl_count) > 1)
5419 nfs4_schedule_state_renewal(clp);
5420 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005421 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005422}
5423
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005424static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5425{
5426 switch(task->tk_status) {
5427 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005428 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5429 return -EAGAIN;
5430 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005431 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005432 }
5433 return 0;
5434}
5435
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005436static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005437{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005438 struct nfs4_sequence_data *calldata = data;
5439 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005440
Trond Myklebust14516c32010-07-31 14:29:06 -04005441 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5442 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005443
5444 if (task->tk_status < 0) {
5445 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005446 if (atomic_read(&clp->cl_count) == 1)
5447 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005448
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005449 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5450 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005451 return;
5452 }
5453 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005454 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005455out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005456 dprintk("<-- %s\n", __func__);
5457}
5458
5459static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5460{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005461 struct nfs4_sequence_data *calldata = data;
5462 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005463 struct nfs4_sequence_args *args;
5464 struct nfs4_sequence_res *res;
5465
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005466 args = task->tk_msg.rpc_argp;
5467 res = task->tk_msg.rpc_resp;
5468
Trond Myklebust035168a2010-06-16 09:52:26 -04005469 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005470 return;
5471 rpc_call_start(task);
5472}
5473
5474static const struct rpc_call_ops nfs41_sequence_ops = {
5475 .rpc_call_done = nfs41_sequence_call_done,
5476 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005477 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005478};
5479
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005480static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005481{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005482 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005483 struct rpc_message msg = {
5484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5485 .rpc_cred = cred,
5486 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005487 struct rpc_task_setup task_setup_data = {
5488 .rpc_client = clp->cl_rpcclient,
5489 .rpc_message = &msg,
5490 .callback_ops = &nfs41_sequence_ops,
5491 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5492 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005493
Alexandros Batsakis71358402010-02-05 03:45:05 -08005494 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005495 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005496 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005497 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005498 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005499 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005500 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005501 msg.rpc_argp = &calldata->args;
5502 msg.rpc_resp = &calldata->res;
5503 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005504 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005505
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005506 return rpc_run_task(&task_setup_data);
5507}
5508
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005509static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005510{
5511 struct rpc_task *task;
5512 int ret = 0;
5513
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005514 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5515 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005516 task = _nfs41_proc_sequence(clp, cred);
5517 if (IS_ERR(task))
5518 ret = PTR_ERR(task);
5519 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005520 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005521 dprintk("<-- %s status=%d\n", __func__, ret);
5522 return ret;
5523}
5524
5525static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5526{
5527 struct rpc_task *task;
5528 int ret;
5529
5530 task = _nfs41_proc_sequence(clp, cred);
5531 if (IS_ERR(task)) {
5532 ret = PTR_ERR(task);
5533 goto out;
5534 }
5535 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005536 if (!ret) {
5537 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5538
5539 if (task->tk_status == 0)
5540 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005541 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005542 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005543 rpc_put_task(task);
5544out:
5545 dprintk("<-- %s status=%d\n", __func__, ret);
5546 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005547}
5548
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005549struct nfs4_reclaim_complete_data {
5550 struct nfs_client *clp;
5551 struct nfs41_reclaim_complete_args arg;
5552 struct nfs41_reclaim_complete_res res;
5553};
5554
5555static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5556{
5557 struct nfs4_reclaim_complete_data *calldata = data;
5558
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005559 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005560 if (nfs41_setup_sequence(calldata->clp->cl_session,
5561 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005562 &calldata->res.seq_res, 0, task))
5563 return;
5564
5565 rpc_call_start(task);
5566}
5567
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005568static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5569{
5570 switch(task->tk_status) {
5571 case 0:
5572 case -NFS4ERR_COMPLETE_ALREADY:
5573 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5574 break;
5575 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005576 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005577 /* fall through */
5578 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005579 return -EAGAIN;
5580 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005581 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005582 }
5583 return 0;
5584}
5585
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005586static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5587{
5588 struct nfs4_reclaim_complete_data *calldata = data;
5589 struct nfs_client *clp = calldata->clp;
5590 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5591
5592 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005593 if (!nfs41_sequence_done(task, res))
5594 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005595
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005596 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5597 rpc_restart_call_prepare(task);
5598 return;
5599 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005600 dprintk("<-- %s\n", __func__);
5601}
5602
5603static void nfs4_free_reclaim_complete_data(void *data)
5604{
5605 struct nfs4_reclaim_complete_data *calldata = data;
5606
5607 kfree(calldata);
5608}
5609
5610static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5611 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5612 .rpc_call_done = nfs4_reclaim_complete_done,
5613 .rpc_release = nfs4_free_reclaim_complete_data,
5614};
5615
5616/*
5617 * Issue a global reclaim complete.
5618 */
5619static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5620{
5621 struct nfs4_reclaim_complete_data *calldata;
5622 struct rpc_task *task;
5623 struct rpc_message msg = {
5624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5625 };
5626 struct rpc_task_setup task_setup_data = {
5627 .rpc_client = clp->cl_rpcclient,
5628 .rpc_message = &msg,
5629 .callback_ops = &nfs4_reclaim_complete_call_ops,
5630 .flags = RPC_TASK_ASYNC,
5631 };
5632 int status = -ENOMEM;
5633
5634 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005635 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005636 if (calldata == NULL)
5637 goto out;
5638 calldata->clp = clp;
5639 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005640
5641 msg.rpc_argp = &calldata->arg;
5642 msg.rpc_resp = &calldata->res;
5643 task_setup_data.callback_data = calldata;
5644 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005645 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005646 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005647 goto out;
5648 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005649 status = nfs4_wait_for_completion_rpc_task(task);
5650 if (status == 0)
5651 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005652 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005653 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005654out:
5655 dprintk("<-- %s status=%d\n", __func__, status);
5656 return status;
5657}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005658
5659static void
5660nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5661{
5662 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005663 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005664
5665 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005666 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5667 * right now covering the LAYOUTGET we are about to send.
5668 * However, that is not so catastrophic, and there seems
5669 * to be no way to prevent it completely.
5670 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005671 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5672 &lgp->res.seq_res, 0, task))
5673 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005674 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5675 NFS_I(lgp->args.inode)->layout,
5676 lgp->args.ctx->state)) {
5677 rpc_exit(task, NFS4_OK);
5678 return;
5679 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005680 rpc_call_start(task);
5681}
5682
5683static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5684{
5685 struct nfs4_layoutget *lgp = calldata;
5686 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5687
5688 dprintk("--> %s\n", __func__);
5689
5690 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5691 return;
5692
5693 switch (task->tk_status) {
5694 case 0:
5695 break;
5696 case -NFS4ERR_LAYOUTTRYLATER:
5697 case -NFS4ERR_RECALLCONFLICT:
5698 task->tk_status = -NFS4ERR_DELAY;
5699 /* Fall through */
5700 default:
5701 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5702 rpc_restart_call_prepare(task);
5703 return;
5704 }
5705 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005706 dprintk("<-- %s\n", __func__);
5707}
5708
5709static void nfs4_layoutget_release(void *calldata)
5710{
5711 struct nfs4_layoutget *lgp = calldata;
5712
5713 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005714 put_nfs_open_context(lgp->args.ctx);
5715 kfree(calldata);
5716 dprintk("<-- %s\n", __func__);
5717}
5718
5719static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5720 .rpc_call_prepare = nfs4_layoutget_prepare,
5721 .rpc_call_done = nfs4_layoutget_done,
5722 .rpc_release = nfs4_layoutget_release,
5723};
5724
5725int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5726{
5727 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5728 struct rpc_task *task;
5729 struct rpc_message msg = {
5730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5731 .rpc_argp = &lgp->args,
5732 .rpc_resp = &lgp->res,
5733 };
5734 struct rpc_task_setup task_setup_data = {
5735 .rpc_client = server->client,
5736 .rpc_message = &msg,
5737 .callback_ops = &nfs4_layoutget_call_ops,
5738 .callback_data = lgp,
5739 .flags = RPC_TASK_ASYNC,
5740 };
5741 int status = 0;
5742
5743 dprintk("--> %s\n", __func__);
5744
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005745 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005746 lgp->res.seq_res.sr_slot = NULL;
5747 task = rpc_run_task(&task_setup_data);
5748 if (IS_ERR(task))
5749 return PTR_ERR(task);
5750 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005751 if (status == 0)
5752 status = task->tk_status;
5753 if (status == 0)
5754 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005755 rpc_put_task(task);
5756 dprintk("<-- %s status=%d\n", __func__, status);
5757 return status;
5758}
5759
Benny Halevycbe82602011-05-22 19:52:37 +03005760static void
5761nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5762{
5763 struct nfs4_layoutreturn *lrp = calldata;
5764
5765 dprintk("--> %s\n", __func__);
5766 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5767 &lrp->res.seq_res, 0, task))
5768 return;
5769 rpc_call_start(task);
5770}
5771
5772static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5773{
5774 struct nfs4_layoutreturn *lrp = calldata;
5775 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005776 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005777
5778 dprintk("--> %s\n", __func__);
5779
5780 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5781 return;
5782
5783 server = NFS_SERVER(lrp->args.inode);
5784 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005785 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005786 return;
5787 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005788 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005789 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005790 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005791 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005792 } else
5793 BUG_ON(!list_empty(&lo->plh_segs));
5794 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005795 lo->plh_block_lgets--;
5796 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005797 dprintk("<-- %s\n", __func__);
5798}
5799
5800static void nfs4_layoutreturn_release(void *calldata)
5801{
5802 struct nfs4_layoutreturn *lrp = calldata;
5803
5804 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005805 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005806 kfree(calldata);
5807 dprintk("<-- %s\n", __func__);
5808}
5809
5810static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5811 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5812 .rpc_call_done = nfs4_layoutreturn_done,
5813 .rpc_release = nfs4_layoutreturn_release,
5814};
5815
5816int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5817{
5818 struct rpc_task *task;
5819 struct rpc_message msg = {
5820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5821 .rpc_argp = &lrp->args,
5822 .rpc_resp = &lrp->res,
5823 };
5824 struct rpc_task_setup task_setup_data = {
5825 .rpc_client = lrp->clp->cl_rpcclient,
5826 .rpc_message = &msg,
5827 .callback_ops = &nfs4_layoutreturn_call_ops,
5828 .callback_data = lrp,
5829 };
5830 int status;
5831
5832 dprintk("--> %s\n", __func__);
5833 task = rpc_run_task(&task_setup_data);
5834 if (IS_ERR(task))
5835 return PTR_ERR(task);
5836 status = task->tk_status;
5837 dprintk("<-- %s status=%d\n", __func__, status);
5838 rpc_put_task(task);
5839 return status;
5840}
5841
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005842/*
5843 * Retrieve the list of Data Server devices from the MDS.
5844 */
5845static int _nfs4_getdevicelist(struct nfs_server *server,
5846 const struct nfs_fh *fh,
5847 struct pnfs_devicelist *devlist)
5848{
5849 struct nfs4_getdevicelist_args args = {
5850 .fh = fh,
5851 .layoutclass = server->pnfs_curr_ld->id,
5852 };
5853 struct nfs4_getdevicelist_res res = {
5854 .devlist = devlist,
5855 };
5856 struct rpc_message msg = {
5857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5858 .rpc_argp = &args,
5859 .rpc_resp = &res,
5860 };
5861 int status;
5862
5863 dprintk("--> %s\n", __func__);
5864 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5865 &res.seq_res, 0);
5866 dprintk("<-- %s status=%d\n", __func__, status);
5867 return status;
5868}
5869
5870int nfs4_proc_getdevicelist(struct nfs_server *server,
5871 const struct nfs_fh *fh,
5872 struct pnfs_devicelist *devlist)
5873{
5874 struct nfs4_exception exception = { };
5875 int err;
5876
5877 do {
5878 err = nfs4_handle_exception(server,
5879 _nfs4_getdevicelist(server, fh, devlist),
5880 &exception);
5881 } while (exception.retry);
5882
5883 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5884 err, devlist->num_devs);
5885
5886 return err;
5887}
5888EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5889
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005890static int
5891_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5892{
5893 struct nfs4_getdeviceinfo_args args = {
5894 .pdev = pdev,
5895 };
5896 struct nfs4_getdeviceinfo_res res = {
5897 .pdev = pdev,
5898 };
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5901 .rpc_argp = &args,
5902 .rpc_resp = &res,
5903 };
5904 int status;
5905
5906 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005907 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005908 dprintk("<-- %s status=%d\n", __func__, status);
5909
5910 return status;
5911}
5912
5913int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5914{
5915 struct nfs4_exception exception = { };
5916 int err;
5917
5918 do {
5919 err = nfs4_handle_exception(server,
5920 _nfs4_proc_getdeviceinfo(server, pdev),
5921 &exception);
5922 } while (exception.retry);
5923 return err;
5924}
5925EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5926
Andy Adamson863a3c62011-03-23 13:27:54 +00005927static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5928{
5929 struct nfs4_layoutcommit_data *data = calldata;
5930 struct nfs_server *server = NFS_SERVER(data->args.inode);
5931
5932 if (nfs4_setup_sequence(server, &data->args.seq_args,
5933 &data->res.seq_res, 1, task))
5934 return;
5935 rpc_call_start(task);
5936}
5937
5938static void
5939nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5940{
5941 struct nfs4_layoutcommit_data *data = calldata;
5942 struct nfs_server *server = NFS_SERVER(data->args.inode);
5943
5944 if (!nfs4_sequence_done(task, &data->res.seq_res))
5945 return;
5946
5947 switch (task->tk_status) { /* Just ignore these failures */
5948 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5949 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5950 case NFS4ERR_BADLAYOUT: /* no layout */
5951 case NFS4ERR_GRACE: /* loca_recalim always false */
5952 task->tk_status = 0;
5953 }
5954
5955 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005956 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005957 return;
5958 }
5959
5960 if (task->tk_status == 0)
5961 nfs_post_op_update_inode_force_wcc(data->args.inode,
5962 data->res.fattr);
5963}
5964
5965static void nfs4_layoutcommit_release(void *calldata)
5966{
5967 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005968 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005969 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005970
Andy Adamsondb29c082011-07-30 20:52:38 -04005971 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005972 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005973 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5974 list_del_init(&lseg->pls_lc_list);
5975 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5976 &lseg->pls_flags))
5977 put_lseg(lseg);
5978 }
Peng Tao92407e72011-10-23 20:21:17 -07005979
5980 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5981 smp_mb__after_clear_bit();
5982 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5983
Andy Adamson863a3c62011-03-23 13:27:54 +00005984 put_rpccred(data->cred);
5985 kfree(data);
5986}
5987
5988static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5989 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5990 .rpc_call_done = nfs4_layoutcommit_done,
5991 .rpc_release = nfs4_layoutcommit_release,
5992};
5993
5994int
Andy Adamsonef311532011-03-12 02:58:10 -05005995nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005996{
5997 struct rpc_message msg = {
5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5999 .rpc_argp = &data->args,
6000 .rpc_resp = &data->res,
6001 .rpc_cred = data->cred,
6002 };
6003 struct rpc_task_setup task_setup_data = {
6004 .task = &data->task,
6005 .rpc_client = NFS_CLIENT(data->args.inode),
6006 .rpc_message = &msg,
6007 .callback_ops = &nfs4_layoutcommit_ops,
6008 .callback_data = data,
6009 .flags = RPC_TASK_ASYNC,
6010 };
6011 struct rpc_task *task;
6012 int status = 0;
6013
6014 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6015 "lbw: %llu inode %lu\n",
6016 data->task.tk_pid, sync,
6017 data->args.lastbytewritten,
6018 data->args.inode->i_ino);
6019
6020 task = rpc_run_task(&task_setup_data);
6021 if (IS_ERR(task))
6022 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006023 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006024 goto out;
6025 status = nfs4_wait_for_completion_rpc_task(task);
6026 if (status != 0)
6027 goto out;
6028 status = task->tk_status;
6029out:
6030 dprintk("%s: status %d\n", __func__, status);
6031 rpc_put_task(task);
6032 return status;
6033}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006034
6035static int
6036_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6037 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6038{
6039 struct nfs41_secinfo_no_name_args args = {
6040 .style = SECINFO_STYLE_CURRENT_FH,
6041 };
6042 struct nfs4_secinfo_res res = {
6043 .flavors = flavors,
6044 };
6045 struct rpc_message msg = {
6046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6047 .rpc_argp = &args,
6048 .rpc_resp = &res,
6049 };
6050 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6051}
6052
6053static int
6054nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6055 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6056{
6057 struct nfs4_exception exception = { };
6058 int err;
6059 do {
6060 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6061 switch (err) {
6062 case 0:
6063 case -NFS4ERR_WRONGSEC:
6064 case -NFS4ERR_NOTSUPP:
6065 break;
6066 default:
6067 err = nfs4_handle_exception(server, err, &exception);
6068 }
6069 } while (exception.retry);
6070 return err;
6071}
6072
6073static int
6074nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6075 struct nfs_fsinfo *info)
6076{
6077 int err;
6078 struct page *page;
6079 rpc_authflavor_t flavor;
6080 struct nfs4_secinfo_flavors *flavors;
6081
6082 page = alloc_page(GFP_KERNEL);
6083 if (!page) {
6084 err = -ENOMEM;
6085 goto out;
6086 }
6087
6088 flavors = page_address(page);
6089 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6090
6091 /*
6092 * Fall back on "guess and check" method if
6093 * the server doesn't support SECINFO_NO_NAME
6094 */
6095 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6096 err = nfs4_find_root_sec(server, fhandle, info);
6097 goto out_freepage;
6098 }
6099 if (err)
6100 goto out_freepage;
6101
6102 flavor = nfs_find_best_sec(flavors);
6103 if (err == 0)
6104 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6105
6106out_freepage:
6107 put_page(page);
6108 if (err == -EACCES)
6109 return -EPERM;
6110out:
6111 return err;
6112}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006113static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6114{
6115 int status;
6116 struct nfs41_test_stateid_args args = {
6117 .stateid = &state->stateid,
6118 };
6119 struct nfs41_test_stateid_res res;
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6122 .rpc_argp = &args,
6123 .rpc_resp = &res,
6124 };
6125 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6126 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6127 return status;
6128}
6129
6130static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6131{
6132 struct nfs4_exception exception = { };
6133 int err;
6134 do {
6135 err = nfs4_handle_exception(server,
6136 _nfs41_test_stateid(server, state),
6137 &exception);
6138 } while (exception.retry);
6139 return err;
6140}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006141
6142static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6143{
6144 int status;
6145 struct nfs41_free_stateid_args args = {
6146 .stateid = &state->stateid,
6147 };
6148 struct nfs41_free_stateid_res res;
6149 struct rpc_message msg = {
6150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6151 .rpc_argp = &args,
6152 .rpc_resp = &res,
6153 };
6154
6155 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6156 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6157 return status;
6158}
6159
6160static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6161{
6162 struct nfs4_exception exception = { };
6163 int err;
6164 do {
6165 err = nfs4_handle_exception(server,
6166 _nfs4_free_stateid(server, state),
6167 &exception);
6168 } while (exception.retry);
6169 return err;
6170}
Andy Adamson557134a2009-04-01 09:21:53 -04006171#endif /* CONFIG_NFS_V4_1 */
6172
Andy Adamson591d71c2009-04-01 09:22:47 -04006173struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 .recover_open = nfs4_open_reclaim,
6177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006178 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006179 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180};
6181
Andy Adamson591d71c2009-04-01 09:22:47 -04006182#if defined(CONFIG_NFS_V4_1)
6183struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6184 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6185 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6186 .recover_open = nfs4_open_reclaim,
6187 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006188 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006189 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006190 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006191};
6192#endif /* CONFIG_NFS_V4_1 */
6193
6194struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006195 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006196 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 .recover_open = nfs4_open_expired,
6198 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006199 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006200 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201};
6202
Andy Adamson591d71c2009-04-01 09:22:47 -04006203#if defined(CONFIG_NFS_V4_1)
6204struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6205 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6206 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006207 .recover_open = nfs41_open_expired,
6208 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006209 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006210 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006211};
6212#endif /* CONFIG_NFS_V4_1 */
6213
Benny Halevy29fba382009-04-01 09:22:44 -04006214struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6215 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006216 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006217 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006218};
6219
6220#if defined(CONFIG_NFS_V4_1)
6221struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6222 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006223 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006224 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006225};
6226#endif
6227
Trond Myklebust97dc1352010-06-16 09:52:26 -04006228static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6229 .minor_version = 0,
6230 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006231 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006232 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006233 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6234 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6235 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006236};
6237
6238#if defined(CONFIG_NFS_V4_1)
6239static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6240 .minor_version = 1,
6241 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006242 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006243 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006244 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6245 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6246 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006247};
6248#endif
6249
Trond Myklebust97dc1352010-06-16 09:52:26 -04006250const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6251 [0] = &nfs_v4_0_minor_ops,
6252#if defined(CONFIG_NFS_V4_1)
6253 [1] = &nfs_v4_1_minor_ops,
6254#endif
6255};
6256
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006257static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006258 .permission = nfs_permission,
6259 .getattr = nfs_getattr,
6260 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006261 .getxattr = generic_getxattr,
6262 .setxattr = generic_setxattr,
6263 .listxattr = generic_listxattr,
6264 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006265};
6266
David Howells509de812006-08-22 20:06:11 -04006267const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 .version = 4, /* protocol version */
6269 .dentry_ops = &nfs4_dentry_operations,
6270 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006271 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006272 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 .getroot = nfs4_proc_get_root,
6274 .getattr = nfs4_proc_getattr,
6275 .setattr = nfs4_proc_setattr,
6276 .lookup = nfs4_proc_lookup,
6277 .access = nfs4_proc_access,
6278 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 .create = nfs4_proc_create,
6280 .remove = nfs4_proc_remove,
6281 .unlink_setup = nfs4_proc_unlink_setup,
6282 .unlink_done = nfs4_proc_unlink_done,
6283 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006284 .rename_setup = nfs4_proc_rename_setup,
6285 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 .link = nfs4_proc_link,
6287 .symlink = nfs4_proc_symlink,
6288 .mkdir = nfs4_proc_mkdir,
6289 .rmdir = nfs4_proc_remove,
6290 .readdir = nfs4_proc_readdir,
6291 .mknod = nfs4_proc_mknod,
6292 .statfs = nfs4_proc_statfs,
6293 .fsinfo = nfs4_proc_fsinfo,
6294 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006295 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 .decode_dirent = nfs4_decode_dirent,
6297 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006298 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006300 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006302 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006304 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006305 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006306 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006307 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006308 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309};
6310
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006311static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6312 .prefix = XATTR_NAME_NFSV4_ACL,
6313 .list = nfs4_xattr_list_nfs4_acl,
6314 .get = nfs4_xattr_get_nfs4_acl,
6315 .set = nfs4_xattr_set_nfs4_acl,
6316};
6317
6318const struct xattr_handler *nfs4_xattr_handlers[] = {
6319 &nfs4_xattr_nfs4_acl_handler,
6320 NULL
6321};
6322
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323/*
6324 * Local variables:
6325 * c-basic-offset: 8
6326 * End:
6327 */