blob: dcda0ba7af6034b6c8d983082ee00ffbbb4d6cde [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>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010058#include <linux/freezer.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 Myklebust9936781d2007-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;
Jeff Laytond3103102011-12-01 22:44:39 +0100247 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500248 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
Benny Halevydfb4f3092010-09-24 09:17:01 -0400367nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400369 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400370 int slotid = free_slotid;
371
Benny Halevydfb4f3092010-09-24 09:17:01 -0400372 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
375
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500379 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 tbl->highest_used_slotid = slotid;
381 else
382 tbl->highest_used_slotid = -1;
383 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
385 free_slotid, tbl->highest_used_slotid);
386}
387
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388/*
Andy Adamson42acd022011-01-06 02:04:34 +0000389 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390 */
Andy Adamson42acd022011-01-06 02:04:34 +0000391static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800393 struct rpc_task *task;
394
Trond Myklebusta2118c32010-06-16 09:52:26 -0400395 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800396 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
397 if (task)
398 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800399 return;
400 }
401
402 if (ses->fc_slot_table.highest_used_slotid != -1)
403 return;
404
Andy Adamson42acd022011-01-06 02:04:34 +0000405 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
406 complete(&ses->fc_slot_table.complete);
407}
408
409/*
410 * Signal state manager thread if session back channel is drained
411 */
412void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
413{
414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
415 ses->bc_slot_table.highest_used_slotid != -1)
416 return;
417 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
418 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419}
420
Trond Myklebustd185a332010-06-16 09:52:25 -0400421static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400422{
423 struct nfs4_slot_table *tbl;
424
Trond Myklebustd185a332010-06-16 09:52:25 -0400425 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400427 /* just wake up the next guy waiting since
428 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500429 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 return;
Andy Adamson13615872009-04-01 09:22:17 -0400431 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500432
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400434 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000435 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400437 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400438}
439
Trond Myklebust14516c32010-07-31 14:29:06 -0400440static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441{
442 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444
445 /*
446 * sr_status remains 1 if an RPC level error occurred. The server
447 * may or may not have processed the sequence operation..
448 * Proceed as if the server received and processed the sequence
449 * operation.
450 */
451 if (res->sr_status == 1)
452 res->sr_status = NFS_OK;
453
Bryan Schumaker468f8612011-04-18 15:57:32 -0400454 /* don't increment the sequence number if the task wasn't sent */
455 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 goto out;
457
Andy Adamson691daf32009-12-04 15:55:39 -0500458 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 switch (res->sr_status) {
460 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400462 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400463 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400465 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500466 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500467 if (res->sr_status_flags != 0)
468 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400469 break;
470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200475 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
477 res->sr_slot - res->sr_session->fc_slot_table.slots,
478 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
480 default:
481 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483 }
484out:
485 /* The session may be reset by one of the error handlers. */
486 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400487 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 return 1;
489out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400490 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 goto out;
492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
493 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494}
495
Trond Myklebust14516c32010-07-31 14:29:06 -0400496static int nfs4_sequence_done(struct rpc_task *task,
497 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400498{
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 if (res->sr_session == NULL)
500 return 1;
501 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400502}
503
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400504/*
Benny Halevy510b8172009-04-01 09:22:14 -0400505 * nfs4_find_slot - efficiently look for a free slot
506 *
507 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
508 * If found, we mark the slot as used, update the highest_used_slotid,
509 * and respectively set up the sequence operation args.
510 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511 *
512 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400513 */
514static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800515nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400516{
517 int slotid;
518 u8 ret_id = NFS4_MAX_SLOT_TABLE;
519 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
520
Benny Halevy510b8172009-04-01 09:22:14 -0400521 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
523 tbl->max_slots);
524 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
525 if (slotid >= tbl->max_slots)
526 goto out;
527 __set_bit(slotid, tbl->used_slots);
528 if (slotid > tbl->highest_used_slotid)
529 tbl->highest_used_slotid = slotid;
530 ret_id = slotid;
531out:
532 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
533 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400534 return ret_id;
535}
536
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000537int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 struct nfs4_sequence_args *args,
539 struct nfs4_sequence_res *res,
540 int cache_reply,
541 struct rpc_task *task)
542{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 struct nfs4_slot *slot;
544 struct nfs4_slot_table *tbl;
545 u8 slotid;
546
547 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400549 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400550 return 0;
551
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 tbl = &session->fc_slot_table;
553
554 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400555 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800556 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500557 /*
558 * The state manager will wait until the slot table is empty.
559 * Schedule the reset thread
560 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400562 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500563 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500564 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400565 }
566
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800567 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
568 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
569 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
570 spin_unlock(&tbl->slot_tbl_lock);
571 dprintk("%s enforce FIFO order\n", __func__);
572 return -EAGAIN;
573 }
574
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800575 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 if (slotid == NFS4_MAX_SLOT_TABLE) {
577 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
578 spin_unlock(&tbl->slot_tbl_lock);
579 dprintk("<-- %s: no free slots\n", __func__);
580 return -EAGAIN;
581 }
582 spin_unlock(&tbl->slot_tbl_lock);
583
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800584 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400586 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 args->sa_slotid = slotid;
588 args->sa_cache_this = cache_reply;
589
590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
591
Benny Halevy99fe60d2009-04-01 09:22:29 -0400592 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400595 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 /*
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
599 */
600 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 return 0;
602}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000603EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604
Trond Myklebust035168ab2010-06-16 09:52:26 -0400605int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
608 int cache_reply,
609 struct rpc_task *task)
610{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400611 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 int ret = 0;
613
Trond Myklebust035168ab2010-06-16 09:52:26 -0400614 if (session == NULL) {
615 args->sa_session = NULL;
616 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400618 }
619
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200620 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400621 __func__, session->clp, session, res->sr_slot ?
622 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400623
624 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626out:
627 dprintk("<-- %s status=%d\n", __func__, ret);
628 return ret;
629}
630
631struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400632 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 struct nfs4_sequence_args *seq_args;
634 struct nfs4_sequence_res *seq_res;
635 int cache_reply;
636};
637
638static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
639{
640 struct nfs41_call_sync_data *data = calldata;
641
Trond Myklebust035168ab2010-06-16 09:52:26 -0400642 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
643
644 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 data->seq_res, data->cache_reply, task))
646 return;
647 rpc_call_start(task);
648}
649
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800650static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
653 nfs41_call_sync_prepare(task, calldata);
654}
655
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661}
662
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663struct rpc_call_ops nfs41_call_sync_ops = {
664 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666};
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668struct rpc_call_ops nfs41_call_priv_sync_ops = {
669 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
670 .rpc_call_done = nfs41_call_sync_done,
671};
672
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
674 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct rpc_message *msg,
676 struct nfs4_sequence_args *args,
677 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800678 int cache_reply,
679 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680{
681 int ret;
682 struct rpc_task *task;
683 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400684 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 .seq_args = args,
686 .seq_res = res,
687 .cache_reply = cache_reply,
688 };
689 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 .rpc_message = msg,
692 .callback_ops = &nfs41_call_sync_ops,
693 .callback_data = &data
694 };
695
Benny Halevydfb4f3092010-09-24 09:17:01 -0400696 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800697 if (privileged)
698 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 task = rpc_run_task(&task_setup);
700 if (IS_ERR(task))
701 ret = PTR_ERR(task);
702 else {
703 ret = task->tk_status;
704 rpc_put_task(task);
705 }
706 return ret;
707}
708
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709int _nfs4_call_sync_session(struct rpc_clnt *clnt,
710 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711 struct rpc_message *msg,
712 struct nfs4_sequence_args *args,
713 struct nfs4_sequence_res *res,
714 int cache_reply)
715{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717}
718
Trond Myklebustdf896452010-06-16 09:52:26 -0400719#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400720static int nfs4_sequence_done(struct rpc_task *task,
721 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400722{
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400724}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725#endif /* CONFIG_NFS_V4_1 */
726
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int _nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Benny Halevyf3752972009-04-01 09:22:04 -0400734 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736}
737
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000738static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739int nfs4_call_sync(struct rpc_clnt *clnt,
740 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741 struct rpc_message *msg,
742 struct nfs4_sequence_args *args,
743 struct nfs4_sequence_res *res,
744 int cache_reply)
745{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
747 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000748}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400749
Trond Myklebust38478b22006-05-25 01:40:57 -0400750static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Trond Myklebust38478b22006-05-25 01:40:57 -0400752 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400755 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700756 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400757 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700758 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400759 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400763 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_openargs o_arg;
765 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100766 struct nfs_open_confirmargs c_arg;
767 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct nfs_fattr f_attr;
769 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400771 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400773 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100775 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400776 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100777 int rpc_status;
778 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779};
780
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400781
782static void nfs4_init_opendata_res(struct nfs4_opendata *p)
783{
784 p->o_res.f_attr = &p->f_attr;
785 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400786 p->o_res.seqid = p->o_arg.seqid;
787 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400788 p->o_res.server = p->o_arg.server;
789 nfs_fattr_init(&p->f_attr);
790 nfs_fattr_init(&p->dir_attr);
791}
792
Al Viro82a2c1b2011-06-22 18:30:55 -0400793static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 const struct iattr *attrs,
796 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797{
Al Viro82a2c1b2011-06-22 18:30:55 -0400798 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 struct inode *dir = parent->d_inode;
800 struct nfs_server *server = NFS_SERVER(dir);
801 struct nfs4_opendata *p;
802
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400803 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804 if (p == NULL)
805 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400806 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 if (p->o_arg.seqid == NULL)
808 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 nfs_sb_active(dentry->d_sb);
810 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->dir = parent;
812 p->owner = sp;
813 atomic_inc(&sp->so_count);
814 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815 p->o_arg.open_flags = flags;
816 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400817 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400818 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 p->o_arg.server = server;
821 p->o_arg.bitmask = server->attr_bitmask;
822 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76ff2010-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 Myklebustd77d76ff2010-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 Myklebustc6d00e62007-06-17 16:02:44 -0400858 kfree(p);
859}
860
861static void nfs4_opendata_put(struct nfs4_opendata *p)
862{
863 if (p != NULL)
864 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865}
866
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
868{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869 int ret;
870
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100872 return ret;
873}
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876{
877 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878
879 if (open_mode & O_EXCL)
880 goto out;
881 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500883 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
884 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 break;
886 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500887 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
888 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400889 break;
890 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500891 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
892 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400893 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 return ret;
896}
897
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500900 if (delegation == NULL)
901 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500904 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400905 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500906 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400907 return 1;
908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100911{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100913 case FMODE_WRITE:
914 state->n_wronly++;
915 break;
916 case FMODE_READ:
917 state->n_rdonly++;
918 break;
919 case FMODE_READ|FMODE_WRITE:
920 state->n_rdwr++;
921 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100923}
924
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400926{
927 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
928 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
929 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400931 case FMODE_READ:
932 set_bit(NFS_O_RDONLY_STATE, &state->flags);
933 break;
934 case FMODE_WRITE:
935 set_bit(NFS_O_WRONLY_STATE, &state->flags);
936 break;
937 case FMODE_READ|FMODE_WRITE:
938 set_bit(NFS_O_RDWR_STATE, &state->flags);
939 }
940}
941
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400943{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400947}
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static 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 -0700950{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400951 /*
952 * Protect the call to nfs4_state_set_mode_locked and
953 * serialise the stateid update
954 */
955 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400956 if (deleg_stateid != NULL) {
957 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
958 set_bit(NFS_DELEGATED_STATE, &state->flags);
959 }
960 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400962 write_sequnlock(&state->seqlock);
963 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700965 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968static 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 -0500969{
970 struct nfs_inode *nfsi = NFS_I(state->inode);
971 struct nfs_delegation *deleg_cur;
972 int ret = 0;
973
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500975
976 rcu_read_lock();
977 deleg_cur = rcu_dereference(nfsi->delegation);
978 if (deleg_cur == NULL)
979 goto no_delegation;
980
981 spin_lock(&deleg_cur->lock);
982 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500984 goto no_delegation_unlock;
985
986 if (delegation == NULL)
987 delegation = &deleg_cur->stateid;
988 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
989 goto no_delegation_unlock;
990
Trond Myklebustb7391f42008-12-23 15:21:52 -0500991 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500993 ret = 1;
994no_delegation_unlock:
995 spin_unlock(&deleg_cur->lock);
996no_delegation:
997 rcu_read_unlock();
998
999 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001001 ret = 1;
1002 }
1003
1004 return ret;
1005}
1006
1007
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009{
1010 struct nfs_delegation *delegation;
1011
1012 rcu_read_lock();
1013 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 rcu_read_unlock();
1016 return;
1017 }
1018 rcu_read_unlock();
1019 nfs_inode_return_delegation(inode);
1020}
1021
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023{
1024 struct nfs4_state *state = opendata->state;
1025 struct nfs_inode *nfsi = NFS_I(state->inode);
1026 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1028 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029 nfs4_stateid stateid;
1030 int ret = -EAGAIN;
1031
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 if (can_open_cached(state, fmode, open_mode)) {
1036 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001037 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001038 goto out_return_state;
1039 }
1040 spin_unlock(&state->owner->so_lock);
1041 }
Trond Myklebust34310432008-12-23 15:21:38 -05001042 rcu_read_lock();
1043 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001044 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001045 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001046 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001047 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 /* Save the delegation */
1049 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1050 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001051 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 if (ret != 0)
1053 goto out;
1054 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001055
1056 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001057 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001058 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060out:
1061 return ERR_PTR(ret);
1062out_return_state:
1063 atomic_inc(&state->count);
1064 return state;
1065}
1066
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001067static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1068{
1069 struct inode *inode;
1070 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001071 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001072 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001073
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001075 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 goto out;
1077 }
1078
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001079 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001084 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 goto err;
1086 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001087 state = nfs4_get_open_state(inode, data->owner);
1088 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001089 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001090 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001091 int delegation_flags = 0;
1092
Trond Myklebust003707c2007-07-05 18:07:55 -04001093 rcu_read_lock();
1094 delegation = rcu_dereference(NFS_I(inode)->delegation);
1095 if (delegation)
1096 delegation_flags = delegation->flags;
1097 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001098 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1099 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1100 "returning a delegation for "
1101 "OPEN(CLAIM_DELEGATE_CUR)\n",
1102 NFS_CLIENT(inode)->cl_server);
1103 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001104 nfs_inode_set_delegation(state->inode,
1105 data->owner->so_cred,
1106 &data->o_res);
1107 else
1108 nfs_inode_reclaim_delegation(state->inode,
1109 data->owner->so_cred,
1110 &data->o_res);
1111 }
Trond Myklebust34310432008-12-23 15:21:38 -05001112
1113 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001114 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001118err_put_inode:
1119 iput(inode);
1120err:
1121 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001122}
1123
Trond Myklebust864472e2006-01-03 09:55:15 +01001124static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1125{
1126 struct nfs_inode *nfsi = NFS_I(state->inode);
1127 struct nfs_open_context *ctx;
1128
1129 spin_lock(&state->inode->i_lock);
1130 list_for_each_entry(ctx, &nfsi->open_files, list) {
1131 if (ctx->state != state)
1132 continue;
1133 get_nfs_open_context(ctx);
1134 spin_unlock(&state->inode->i_lock);
1135 return ctx;
1136 }
1137 spin_unlock(&state->inode->i_lock);
1138 return ERR_PTR(-ENOENT);
1139}
1140
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001141static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1142{
1143 struct nfs4_opendata *opendata;
1144
Al Viro3d4ff432011-06-22 18:40:12 -04001145 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001146 if (opendata == NULL)
1147 return ERR_PTR(-ENOMEM);
1148 opendata->state = state;
1149 atomic_inc(&state->count);
1150 return opendata;
1151}
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001154{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 int ret;
1157
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 opendata->o_arg.open_flags = 0;
1159 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001160 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1161 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1162 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001163 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 if (ret != 0)
1165 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001166 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001167 if (IS_ERR(newstate))
1168 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001169 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 return 0;
1172}
1173
1174static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1175{
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 int ret;
1178
1179 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 smp_rmb();
1182 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001183 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 if (ret != 0)
1186 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 if (newstate != state)
1188 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 }
1190 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001191 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 if (ret != 0)
1194 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 if (newstate != state)
1196 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 }
1198 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001199 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001200 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 if (ret != 0)
1202 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001203 if (newstate != state)
1204 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001206 /*
1207 * We may have performed cached opens for all three recoveries.
1208 * Check if we need to update the current stateid.
1209 */
1210 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1211 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001212 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001213 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1214 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001215 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001216 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 return 0;
1218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/*
1221 * OPEN_RECLAIM:
1222 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001224static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001228 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int status;
1230
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001231 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1232 if (IS_ERR(opendata))
1233 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1235 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001236 rcu_read_lock();
1237 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001238 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001239 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001241 opendata->o_arg.u.delegation_type = delegation_type;
1242 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001243 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return status;
1245}
1246
Trond Myklebust539cd032007-06-05 11:46:42 -04001247static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct nfs_server *server = NFS_SERVER(state->inode);
1250 struct nfs4_exception exception = { };
1251 int err;
1252 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001253 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001254 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001255 break;
1256 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } while (exception.retry);
1258 return err;
1259}
1260
Trond Myklebust864472e2006-01-03 09:55:15 +01001261static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1262{
1263 struct nfs_open_context *ctx;
1264 int ret;
1265
1266 ctx = nfs4_state_find_open_context(state);
1267 if (IS_ERR(ctx))
1268 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001269 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 put_nfs_open_context(ctx);
1271 return ret;
1272}
1273
Trond Myklebust13437e12007-07-06 15:10:43 -04001274static 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 -07001275{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001279 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1280 if (IS_ERR(opendata))
1281 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001282 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001283 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001285 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001287 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Trond Myklebust13437e12007-07-06 15:10:43 -04001290int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001293 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 int err;
1295 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001296 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 switch (err) {
1298 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001299 case -ENOENT:
1300 case -ESTALE:
1301 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001302 case -NFS4ERR_BADSESSION:
1303 case -NFS4ERR_BADSLOT:
1304 case -NFS4ERR_BAD_HIGH_SLOT:
1305 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1306 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001307 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 case -NFS4ERR_STALE_CLIENTID:
1310 case -NFS4ERR_STALE_STATEID:
1311 case -NFS4ERR_EXPIRED:
1312 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001313 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001314 goto out;
1315 case -ERESTARTSYS:
1316 /*
1317 * The show must go on: exit, but mark the
1318 * stateid as needing recovery.
1319 */
1320 case -NFS4ERR_ADMIN_REVOKED:
1321 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001322 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001323 case -EKEYEXPIRED:
1324 /*
1325 * User RPCSEC_GSS context has expired.
1326 * We cannot recover this stateid now, so
1327 * skip it and allow recovery thread to
1328 * proceed.
1329 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001330 case -ENOMEM:
1331 err = 0;
1332 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 err = nfs4_handle_exception(server, err, &exception);
1335 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001336out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return err;
1338}
1339
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1341{
1342 struct nfs4_opendata *data = calldata;
1343
1344 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001345 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001346 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1347 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001348 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001349 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001350 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001351 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352}
1353
1354static void nfs4_open_confirm_release(void *calldata)
1355{
1356 struct nfs4_opendata *data = calldata;
1357 struct nfs4_state *state = NULL;
1358
1359 /* If this request hasn't been cancelled, do nothing */
1360 if (data->cancelled == 0)
1361 goto out_free;
1362 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001363 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001366 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001367 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001368out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001369 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001370}
1371
1372static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001373 .rpc_call_done = nfs4_open_confirm_done,
1374 .rpc_release = nfs4_open_confirm_release,
1375};
1376
1377/*
1378 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1379 */
1380static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1381{
1382 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1383 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001384 struct rpc_message msg = {
1385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1386 .rpc_argp = &data->c_arg,
1387 .rpc_resp = &data->c_res,
1388 .rpc_cred = data->owner->so_cred,
1389 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001390 struct rpc_task_setup task_setup_data = {
1391 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001392 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001393 .callback_ops = &nfs4_open_confirm_ops,
1394 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001395 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001396 .flags = RPC_TASK_ASYNC,
1397 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int status;
1399
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001400 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001401 data->rpc_done = 0;
1402 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001403 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001404 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001405 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001407 status = nfs4_wait_for_completion_rpc_task(task);
1408 if (status != 0) {
1409 data->cancelled = 1;
1410 smp_wmb();
1411 } else
1412 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001413 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return status;
1415}
1416
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 struct nfs4_opendata *data = calldata;
1420 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1423 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001424 /*
1425 * Check if we still need to send an OPEN call, or if we can use
1426 * a delegation instead.
1427 */
1428 if (data->state != NULL) {
1429 struct nfs_delegation *delegation;
1430
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001431 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001432 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 rcu_read_lock();
1434 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001435 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1436 can_open_delegated(delegation, data->o_arg.fmode))
1437 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438 rcu_read_unlock();
1439 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001441 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001442 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001443 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001444 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001445 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1446 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001447 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001448 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001449 &data->o_arg.seq_args,
1450 &data->o_res.seq_res, 1, task))
1451 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001452 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001453 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001454unlock_no_action:
1455 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001456out_no_action:
1457 task->tk_action = NULL;
1458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001461static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1462{
1463 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1464 nfs4_open_prepare(task, calldata);
1465}
1466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467static void nfs4_open_done(struct rpc_task *task, void *calldata)
1468{
1469 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001472
Trond Myklebust14516c32010-07-31 14:29:06 -04001473 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1474 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 if (task->tk_status == 0) {
1477 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001478 case S_IFREG:
1479 break;
1480 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001482 break;
1483 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001485 break;
1486 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001488 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001489 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001490 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1491 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001492 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001493 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496static void nfs4_open_release(void *calldata)
1497{
1498 struct nfs4_opendata *data = calldata;
1499 struct nfs4_state *state = NULL;
1500
1501 /* If this request hasn't been cancelled, do nothing */
1502 if (data->cancelled == 0)
1503 goto out_free;
1504 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001505 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506 goto out_free;
1507 /* In case we need an open_confirm, no cleanup! */
1508 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1509 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001511 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001512 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001514 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515}
1516
1517static const struct rpc_call_ops nfs4_open_ops = {
1518 .rpc_call_prepare = nfs4_open_prepare,
1519 .rpc_call_done = nfs4_open_done,
1520 .rpc_release = nfs4_open_release,
1521};
1522
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001523static const struct rpc_call_ops nfs4_recover_open_ops = {
1524 .rpc_call_prepare = nfs4_recover_open_prepare,
1525 .rpc_call_done = nfs4_open_done,
1526 .rpc_release = nfs4_open_release,
1527};
1528
1529static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001530{
1531 struct inode *dir = data->dir->d_inode;
1532 struct nfs_server *server = NFS_SERVER(dir);
1533 struct nfs_openargs *o_arg = &data->o_arg;
1534 struct nfs_openres *o_res = &data->o_res;
1535 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001536 struct rpc_message msg = {
1537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1538 .rpc_argp = o_arg,
1539 .rpc_resp = o_res,
1540 .rpc_cred = data->owner->so_cred,
1541 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001542 struct rpc_task_setup task_setup_data = {
1543 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001544 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001545 .callback_ops = &nfs4_open_ops,
1546 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001547 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001548 .flags = RPC_TASK_ASYNC,
1549 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550 int status;
1551
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001552 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001553 data->rpc_done = 0;
1554 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001555 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001556 if (isrecover)
1557 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001558 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001559 if (IS_ERR(task))
1560 return PTR_ERR(task);
1561 status = nfs4_wait_for_completion_rpc_task(task);
1562 if (status != 0) {
1563 data->cancelled = 1;
1564 smp_wmb();
1565 } else
1566 status = data->rpc_status;
1567 rpc_put_task(task);
1568
1569 return status;
1570}
1571
1572static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1573{
1574 struct inode *dir = data->dir->d_inode;
1575 struct nfs_openres *o_res = &data->o_res;
1576 int status;
1577
1578 status = nfs4_run_open_task(data, 1);
1579 if (status != 0 || !data->rpc_done)
1580 return status;
1581
1582 nfs_refresh_inode(dir, o_res->dir_attr);
1583
1584 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1585 status = _nfs4_proc_open_confirm(data);
1586 if (status != 0)
1587 return status;
1588 }
1589
1590 return status;
1591}
1592
1593/*
1594 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1595 */
1596static int _nfs4_proc_open(struct nfs4_opendata *data)
1597{
1598 struct inode *dir = data->dir->d_inode;
1599 struct nfs_server *server = NFS_SERVER(dir);
1600 struct nfs_openargs *o_arg = &data->o_arg;
1601 struct nfs_openres *o_res = &data->o_res;
1602 int status;
1603
1604 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001605 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001607 if (status != 0) {
1608 if (status == -NFS4ERR_BADNAME &&
1609 !(o_arg->open_flags & O_CREAT))
1610 return -ENOENT;
1611 return status;
1612 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001614 if (o_arg->open_flags & O_CREAT) {
1615 update_changeattr(dir, &o_res->cinfo);
1616 nfs_post_op_update_inode(dir, o_res->dir_attr);
1617 } else
1618 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001619 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1620 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001622 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001624 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust9936781d2007-07-17 21:52:41 -04001627 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Andy Adamsond83217c2011-03-01 01:34:17 +00001631static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001632{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001633 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001634 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001635
Trond Myklebusta78cb572009-08-09 15:06:19 -04001636 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001637 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001638 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001639 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001640 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1641 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001642 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001643 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001644 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001645 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001646 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001647}
1648
Andy Adamsond83217c2011-03-01 01:34:17 +00001649static int nfs4_recover_expired_lease(struct nfs_server *server)
1650{
1651 return nfs4_client_recover_expired_lease(server->nfs_client);
1652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654/*
1655 * OPEN_EXPIRED:
1656 * reclaim state on the server after a network partition.
1657 * Assumes caller holds the appropriate lock
1658 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001659static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001661 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001662 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001664 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1665 if (IS_ERR(opendata))
1666 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001667 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001668 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001669 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001670 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001674static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001675{
Trond Myklebust539cd032007-06-05 11:46:42 -04001676 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001677 struct nfs4_exception exception = { };
1678 int err;
1679
1680 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001681 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001682 switch (err) {
1683 default:
1684 goto out;
1685 case -NFS4ERR_GRACE:
1686 case -NFS4ERR_DELAY:
1687 nfs4_handle_exception(server, err, &exception);
1688 err = 0;
1689 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001690 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001691out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001692 return err;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Trond Myklebust864472e2006-01-03 09:55:15 +01001700 ctx = nfs4_state_find_open_context(state);
1701 if (IS_ERR(ctx))
1702 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001703 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001704 put_nfs_open_context(ctx);
1705 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001708#if defined(CONFIG_NFS_V4_1)
1709static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1710{
1711 int status;
1712 struct nfs_server *server = NFS_SERVER(state->inode);
1713
1714 status = nfs41_test_stateid(server, state);
1715 if (status == NFS_OK)
1716 return 0;
1717 nfs41_free_stateid(server, state);
1718 return nfs4_open_expired(sp, state);
1719}
1720#endif
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001723 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1724 * fields corresponding to attributes that were used to store the verifier.
1725 * Make sure we clobber those fields in the later setattr call
1726 */
1727static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1728{
1729 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1730 !(sattr->ia_valid & ATTR_ATIME_SET))
1731 sattr->ia_valid |= ATTR_ATIME;
1732
1733 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1734 !(sattr->ia_valid & ATTR_MTIME_SET))
1735 sattr->ia_valid |= ATTR_MTIME;
1736}
1737
1738/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001739 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001741static 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 -07001742{
1743 struct nfs4_state_owner *sp;
1744 struct nfs4_state *state = NULL;
1745 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001746 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001747 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 status = -ENOMEM;
1751 if (!(sp = nfs4_get_state_owner(server, cred))) {
1752 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1753 goto out_err;
1754 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001755 status = nfs4_recover_expired_lease(server);
1756 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001757 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001758 if (dentry->d_inode != NULL)
1759 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001760 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001761 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001762 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001763 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Al Viro82a2c1b2011-06-22 18:30:55 -04001765 if (dentry->d_inode != NULL)
1766 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001767
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001770 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001773 status = PTR_ERR(state);
1774 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001775 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001776 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001777 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001778
1779 if (opendata->o_arg.open_flags & O_EXCL) {
1780 nfs4_exclusive_attrset(opendata, sattr);
1781
1782 nfs_fattr_init(opendata->o_res.f_attr);
1783 status = nfs4_do_setattr(state->inode, cred,
1784 opendata->o_res.f_attr, sattr,
1785 state);
1786 if (status == 0)
1787 nfs_setattr_update_inode(state->inode, sattr);
1788 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1789 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001790 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 *res = state;
1793 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001794err_opendata_put:
1795 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001796err_put_state_owner:
1797 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 *res = NULL;
1800 return status;
1801}
1802
1803
Al Viro82a2c1b2011-06-22 18:30:55 -04001804static 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 -07001805{
1806 struct nfs4_exception exception = { };
1807 struct nfs4_state *res;
1808 int status;
1809
1810 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001811 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (status == 0)
1813 break;
1814 /* NOTE: BAD_SEQID means the server and client disagree about the
1815 * book-keeping w.r.t. state-changing operations
1816 * (OPEN/CLOSE/LOCK/LOCKU...)
1817 * It is actually a sign of a bug on the client or on the server.
1818 *
1819 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001820 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 * have unhashed the old state_owner for us, and that we can
1822 * therefore safely retry using a new one. We should still warn
1823 * the user though...
1824 */
1825 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001826 printk(KERN_WARNING "NFS: v4 server %s "
1827 " returned a bad sequence-id error!\n",
1828 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 exception.retry = 1;
1830 continue;
1831 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001832 /*
1833 * BAD_STATEID on OPEN means that the server cancelled our
1834 * state before it received the OPEN_CONFIRM.
1835 * Recover by retrying the request as per the discussion
1836 * on Page 181 of RFC3530.
1837 */
1838 if (status == -NFS4ERR_BAD_STATEID) {
1839 exception.retry = 1;
1840 continue;
1841 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 if (status == -EAGAIN) {
1843 /* We must have found a delegation */
1844 exception.retry = 1;
1845 continue;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1848 status, &exception));
1849 } while (exception.retry);
1850 return res;
1851}
1852
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001853static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1854 struct nfs_fattr *fattr, struct iattr *sattr,
1855 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001859 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 .iap = sattr,
1861 .server = server,
1862 .bitmask = server->attr_bitmask,
1863 };
1864 struct nfs_setattrres res = {
1865 .fattr = fattr,
1866 .server = server,
1867 };
1868 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1870 .rpc_argp = &arg,
1871 .rpc_resp = &res,
1872 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001874 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001875 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Trond Myklebust0e574af2005-10-27 22:12:38 -04001877 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001879 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1880 /* Use that stateid */
1881 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001882 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001883 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1885
Bryan Schumaker7c513052011-03-24 17:12:24 +00001886 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001887 if (status == 0 && state != NULL)
1888 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001889 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001892static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1893 struct nfs_fattr *fattr, struct iattr *sattr,
1894 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001896 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct nfs4_exception exception = { };
1898 int err;
1899 do {
1900 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001901 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 &exception);
1903 } while (exception.retry);
1904 return err;
1905}
1906
1907struct nfs4_closedata {
1908 struct inode *inode;
1909 struct nfs4_state *state;
1910 struct nfs_closeargs arg;
1911 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001912 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001913 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001914 bool roc;
1915 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916};
1917
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001918static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001919{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001920 struct nfs4_closedata *calldata = data;
1921 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001922 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001923
Fred Isamanf7e89172011-01-06 11:36:32 +00001924 if (calldata->roc)
1925 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001926 nfs4_put_open_state(calldata->state);
1927 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001928 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001929 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001930 kfree(calldata);
1931}
1932
Trond Myklebust88069f72009-12-08 08:33:16 -05001933static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1934 fmode_t fmode)
1935{
1936 spin_lock(&state->owner->so_lock);
1937 if (!(fmode & FMODE_READ))
1938 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1939 if (!(fmode & FMODE_WRITE))
1940 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1941 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1942 spin_unlock(&state->owner->so_lock);
1943}
1944
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001945static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001947 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 struct nfs_server *server = NFS_SERVER(calldata->inode);
1950
Trond Myklebust14516c32010-07-31 14:29:06 -04001951 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1952 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /* hmm. we are done with the inode, and in the process of freeing
1954 * the state_owner. we keep this around to process errors
1955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 switch (task->tk_status) {
1957 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001958 if (calldata->roc)
1959 pnfs_roc_set_barrier(state->inode,
1960 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001961 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001962 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001963 nfs4_close_clear_stateid_flags(state,
1964 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
1966 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001967 case -NFS4ERR_OLD_STATEID:
1968 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001970 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001973 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1974 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001976 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001977 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001980static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001982 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001983 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001984 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001985
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001986 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001987 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001988
Trond Myklebust88069f72009-12-08 08:33:16 -05001989 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1990 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001991 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001992 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001993 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001994 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001995 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1996 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1997 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001998 }
1999 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002000 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2001 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2002 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002003 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002004 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002005 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002006
2007 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002008 /* Note: exit _without_ calling nfs4_close_done */
2009 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002010 return;
2011 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002012
Fred Isamanf7e89172011-01-06 11:36:32 +00002013 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002014 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002015 if (calldata->roc &&
2016 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2017 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2018 task, NULL);
2019 return;
2020 }
2021 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002022
Trond Myklebust516a6af2005-10-27 22:12:41 -04002023 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002024 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002025 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002026 &calldata->arg.seq_args, &calldata->res.seq_res,
2027 1, task))
2028 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002029 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002032static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002033 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002034 .rpc_call_done = nfs4_close_done,
2035 .rpc_release = nfs4_free_closedata,
2036};
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/*
2039 * It is possible for data to be read/written from a mem-mapped file
2040 * after the sys_close call (which hits the vfs layer as a flush).
2041 * This means that we can't safely call nfsv4 close on a file until
2042 * the inode is cleared. This in turn means that we are not good
2043 * NFSv4 citizens - we do not indicate to the server to update the file's
2044 * share state even when we are done with one of the three share
2045 * stateid's in the inode.
2046 *
2047 * NOTE: Caller must be holding the sp->so_owner semaphore!
2048 */
Al Viro643168c2011-06-22 18:20:23 -04002049int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002051 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002053 struct nfs4_state_owner *sp = state->owner;
2054 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002055 struct rpc_message msg = {
2056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2057 .rpc_cred = state->owner->so_cred,
2058 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002059 struct rpc_task_setup task_setup_data = {
2060 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002061 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002062 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002063 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002064 .flags = RPC_TASK_ASYNC,
2065 };
Trond Myklebust95121352005-10-18 14:20:12 -07002066 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002068 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002070 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002071 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002073 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002074 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002076 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002077 if (calldata->arg.seqid == NULL)
2078 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002079 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002080 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002081 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002082 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002083 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002084 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002085 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002086
Trond Myklebust1174dd12010-12-21 10:52:24 -05002087 msg.rpc_argp = &calldata->arg;
2088 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002089 task_setup_data.callback_data = calldata;
2090 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002091 if (IS_ERR(task))
2092 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002093 status = 0;
2094 if (wait)
2095 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002096 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002097 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002098out_free_calldata:
2099 kfree(calldata);
2100out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002101 if (roc)
2102 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002103 nfs4_put_open_state(state);
2104 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002105 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Trond Myklebust2b484292010-09-17 10:56:51 -04002108static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002109nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 struct nfs4_state *state;
2112
Trond Myklebust565277f2007-10-15 18:17:53 -04002113 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002114 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002115 if (IS_ERR(state))
2116 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002117 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002118 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119}
2120
Trond Myklebust1185a552009-12-03 15:54:02 -05002121static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002122{
2123 if (ctx->state == NULL)
2124 return;
2125 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002126 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002127 else
Al Viro643168c2011-06-22 18:20:23 -04002128 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2132{
Benny Halevy43652ad2009-04-01 09:21:54 -04002133 struct nfs4_server_caps_arg args = {
2134 .fhandle = fhandle,
2135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct nfs4_server_caps_res res = {};
2137 struct rpc_message msg = {
2138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002139 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 .rpc_resp = &res,
2141 };
2142 int status;
2143
Bryan Schumaker7c513052011-03-24 17:12:24 +00002144 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (status == 0) {
2146 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002147 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2148 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2149 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2150 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2151 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2153 server->caps |= NFS_CAP_ACLS;
2154 if (res.has_links != 0)
2155 server->caps |= NFS_CAP_HARDLINKS;
2156 if (res.has_symlinks != 0)
2157 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002158 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2159 server->caps |= NFS_CAP_FILEID;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2161 server->caps |= NFS_CAP_MODE;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2163 server->caps |= NFS_CAP_NLINK;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2165 server->caps |= NFS_CAP_OWNER;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2167 server->caps |= NFS_CAP_OWNER_GROUP;
2168 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2169 server->caps |= NFS_CAP_ATIME;
2170 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2171 server->caps |= NFS_CAP_CTIME;
2172 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2173 server->caps |= NFS_CAP_MTIME;
2174
Trond Myklebusta65318b2009-03-11 14:10:28 -04002175 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2176 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2177 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 server->acl_bitmask = res.acl_bitmask;
2179 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return status;
2182}
2183
Trond Myklebust55a97592006-06-09 09:34:19 -04002184int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct nfs4_exception exception = { };
2187 int err;
2188 do {
2189 err = nfs4_handle_exception(server,
2190 _nfs4_server_capabilities(server, fhandle),
2191 &exception);
2192 } while (exception.retry);
2193 return err;
2194}
2195
2196static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2197 struct nfs_fsinfo *info)
2198{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct nfs4_lookup_root_arg args = {
2200 .bitmask = nfs4_fattr_bitmap,
2201 };
2202 struct nfs4_lookup_res res = {
2203 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002204 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 .fh = fhandle,
2206 };
2207 struct rpc_message msg = {
2208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2209 .rpc_argp = &args,
2210 .rpc_resp = &res,
2211 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002212
Trond Myklebust0e574af2005-10-27 22:12:38 -04002213 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002214 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2218 struct nfs_fsinfo *info)
2219{
2220 struct nfs4_exception exception = { };
2221 int err;
2222 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002223 err = _nfs4_lookup_root(server, fhandle, info);
2224 switch (err) {
2225 case 0:
2226 case -NFS4ERR_WRONGSEC:
2227 break;
2228 default:
2229 err = nfs4_handle_exception(server, err, &exception);
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } while (exception.retry);
2232 return err;
2233}
2234
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002235static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2236 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2237{
2238 struct rpc_auth *auth;
2239 int ret;
2240
2241 auth = rpcauth_create(flavor, server->client);
2242 if (!auth) {
2243 ret = -EIO;
2244 goto out;
2245 }
2246 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002247out:
2248 return ret;
2249}
2250
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002251static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002252 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002254 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002255 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002257 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2258 flav_array[len] = RPC_AUTH_NULL;
2259 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002260
2261 for (i = 0; i < len; i++) {
2262 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002263 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002264 continue;
2265 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002266 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002267 /*
2268 * -EACCESS could mean that the user doesn't have correct permissions
2269 * to access the mount. It could also mean that we tried to mount
2270 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2271 * existing mount programs don't handle -EACCES very well so it should
2272 * be mapped to -EPERM instead.
2273 */
2274 if (status == -EACCES)
2275 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002276 return status;
2277}
2278
2279/*
2280 * get the file handle for the "/" directory on the server
2281 */
2282static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2283 struct nfs_fsinfo *info)
2284{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002285 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002286 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002287 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2288 /*
2289 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2290 * by nfs4_map_errors() as this function exits.
2291 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002292 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (status == 0)
2294 status = nfs4_server_capabilities(server, fhandle);
2295 if (status == 0)
2296 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002297 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Andy Adamson533eb462011-06-13 18:25:56 -04002300static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002301/*
2302 * Get locations and (maybe) other attributes of a referral.
2303 * Note that we'll actually follow the referral later when
2304 * we detect fsid mismatch in inode revalidation
2305 */
Andy Adamson533eb462011-06-13 18:25:56 -04002306static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2307 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002308{
2309 int status = -ENOMEM;
2310 struct page *page = NULL;
2311 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002312
2313 page = alloc_page(GFP_KERNEL);
2314 if (page == NULL)
2315 goto out;
2316 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2317 if (locations == NULL)
2318 goto out;
2319
Trond Myklebustc228fd32007-01-13 02:28:11 -05002320 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002321 if (status != 0)
2322 goto out;
2323 /* Make sure server returned a different fsid for the referral */
2324 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002325 dprintk("%s: server did not return a different fsid for"
2326 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327 status = -EIO;
2328 goto out;
2329 }
Andy Adamson533eb462011-06-13 18:25:56 -04002330 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2331 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002332
Andy Adamson533eb462011-06-13 18:25:56 -04002333 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002334 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002335 memset(fhandle, 0, sizeof(struct nfs_fh));
2336out:
2337 if (page)
2338 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002339 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002340 return status;
2341}
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2344{
2345 struct nfs4_getattr_arg args = {
2346 .fh = fhandle,
2347 .bitmask = server->attr_bitmask,
2348 };
2349 struct nfs4_getattr_res res = {
2350 .fattr = fattr,
2351 .server = server,
2352 };
2353 struct rpc_message msg = {
2354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2355 .rpc_argp = &args,
2356 .rpc_resp = &res,
2357 };
2358
Trond Myklebust0e574af2005-10-27 22:12:38 -04002359 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
2362
2363static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2364{
2365 struct nfs4_exception exception = { };
2366 int err;
2367 do {
2368 err = nfs4_handle_exception(server,
2369 _nfs4_proc_getattr(server, fhandle, fattr),
2370 &exception);
2371 } while (exception.retry);
2372 return err;
2373}
2374
2375/*
2376 * The file is not closed if it is opened due to the a request to change
2377 * the size of the file. The open call will not be needed once the
2378 * VFS layer lookup-intents are implemented.
2379 *
2380 * Close is called when the inode is destroyed.
2381 * If we haven't opened the file for O_WRONLY, we
2382 * need to in the size_change case to obtain a stateid.
2383 *
2384 * Got race?
2385 * Because OPEN is always done by name in nfsv4, it is
2386 * possible that we opened a different file by the same
2387 * name. We can recognize this race condition, but we
2388 * can't do anything about it besides returning an error.
2389 *
2390 * This will be fixed with VFS changes (lookup-intent).
2391 */
2392static int
2393nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2394 struct iattr *sattr)
2395{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002396 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002397 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002398 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int status;
2400
Benny Halevy8a1636c2010-07-14 15:43:57 -04002401 if (pnfs_ld_layoutret_on_setattr(inode))
2402 pnfs_return_layout(inode);
2403
Trond Myklebust0e574af2005-10-27 22:12:38 -04002404 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Trond Myklebustd5308382005-11-04 15:33:38 -05002406 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002407 if (sattr->ia_valid & ATTR_FILE) {
2408 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002409
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002410 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002411 if (ctx) {
2412 cred = ctx->cred;
2413 state = ctx->state;
2414 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002415 }
2416
2417 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002418 if (status == 0)
2419 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return status;
2421}
2422
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002423static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2424 const struct qstr *name, struct nfs_fh *fhandle,
2425 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002426{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002427 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002428 int status;
2429 struct nfs4_lookup_arg args = {
2430 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002431 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002432 .name = name,
2433 };
2434 struct nfs4_lookup_res res = {
2435 .server = server,
2436 .fattr = fattr,
2437 .fh = fhandle,
2438 };
2439 struct rpc_message msg = {
2440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2441 .rpc_argp = &args,
2442 .rpc_resp = &res,
2443 };
2444
2445 nfs_fattr_init(fattr);
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002448 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 dprintk("NFS reply lookup: %d\n", status);
2450 return status;
2451}
2452
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002453void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2454{
2455 memset(fh, 0, sizeof(struct nfs_fh));
2456 fattr->fsid.major = 1;
2457 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2458 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2459 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2460 fattr->nlink = 2;
2461}
2462
Bryan Schumaker7c513052011-03-24 17:12:24 +00002463static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2464 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 struct nfs4_exception exception = { };
2467 int err;
2468 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002469 int status;
2470
2471 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2472 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002473 case -NFS4ERR_BADNAME:
2474 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002475 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002476 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002477 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002478 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002479 }
2480 err = nfs4_handle_exception(NFS_SERVER(dir),
2481 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 } while (exception.retry);
2483 return err;
2484}
2485
2486static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2487{
Trond Myklebust76b32992007-08-10 17:45:11 -04002488 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct nfs4_accessargs args = {
2490 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002491 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002493 struct nfs4_accessres res = {
2494 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct rpc_message msg = {
2497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2498 .rpc_argp = &args,
2499 .rpc_resp = &res,
2500 .rpc_cred = entry->cred,
2501 };
2502 int mode = entry->mask;
2503 int status;
2504
2505 /*
2506 * Determine which access bits we want to ask for...
2507 */
2508 if (mode & MAY_READ)
2509 args.access |= NFS4_ACCESS_READ;
2510 if (S_ISDIR(inode->i_mode)) {
2511 if (mode & MAY_WRITE)
2512 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2513 if (mode & MAY_EXEC)
2514 args.access |= NFS4_ACCESS_LOOKUP;
2515 } else {
2516 if (mode & MAY_WRITE)
2517 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2518 if (mode & MAY_EXEC)
2519 args.access |= NFS4_ACCESS_EXECUTE;
2520 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002521
2522 res.fattr = nfs_alloc_fattr();
2523 if (res.fattr == NULL)
2524 return -ENOMEM;
2525
Bryan Schumaker7c513052011-03-24 17:12:24 +00002526 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (!status) {
2528 entry->mask = 0;
2529 if (res.access & NFS4_ACCESS_READ)
2530 entry->mask |= MAY_READ;
2531 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2532 entry->mask |= MAY_WRITE;
2533 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2534 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002535 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002537 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return status;
2539}
2540
2541static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2542{
2543 struct nfs4_exception exception = { };
2544 int err;
2545 do {
2546 err = nfs4_handle_exception(NFS_SERVER(inode),
2547 _nfs4_proc_access(inode, entry),
2548 &exception);
2549 } while (exception.retry);
2550 return err;
2551}
2552
2553/*
2554 * TODO: For the time being, we don't try to get any attributes
2555 * along with any of the zero-copy operations READ, READDIR,
2556 * READLINK, WRITE.
2557 *
2558 * In the case of the first three, we want to put the GETATTR
2559 * after the read-type operation -- this is because it is hard
2560 * to predict the length of a GETATTR response in v4, and thus
2561 * align the READ data correctly. This means that the GETATTR
2562 * may end up partially falling into the page cache, and we should
2563 * shift it into the 'tail' of the xdr_buf before processing.
2564 * To do this efficiently, we need to know the total length
2565 * of data received, which doesn't seem to be available outside
2566 * of the RPC layer.
2567 *
2568 * In the case of WRITE, we also want to put the GETATTR after
2569 * the operation -- in this case because we want to make sure
2570 * we get the post-operation mtime and size. This means that
2571 * we can't use xdr_encode_pages() as written: we need a variant
2572 * of it which would leave room in the 'tail' iovec.
2573 *
2574 * Both of these changes to the XDR layer would in fact be quite
2575 * minor, but I decided to leave them for a subsequent patch.
2576 */
2577static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2578 unsigned int pgbase, unsigned int pglen)
2579{
2580 struct nfs4_readlink args = {
2581 .fh = NFS_FH(inode),
2582 .pgbase = pgbase,
2583 .pglen = pglen,
2584 .pages = &page,
2585 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002586 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 struct rpc_message msg = {
2588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2589 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002590 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 };
2592
Bryan Schumaker7c513052011-03-24 17:12:24 +00002593 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 -07002594}
2595
2596static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2597 unsigned int pgbase, unsigned int pglen)
2598{
2599 struct nfs4_exception exception = { };
2600 int err;
2601 do {
2602 err = nfs4_handle_exception(NFS_SERVER(inode),
2603 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2604 &exception);
2605 } while (exception.retry);
2606 return err;
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/*
2610 * Got race?
2611 * We will need to arrange for the VFS layer to provide an atomic open.
2612 * Until then, this create/open method is prone to inefficiency and race
2613 * conditions due to the lookup, create, and open VFS calls from sys_open()
2614 * placed on the wire.
2615 *
2616 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2617 * The file will be opened again in the subsequent VFS open call
2618 * (nfs4_proc_file_open).
2619 *
2620 * The open for read will just hang around to be used by any process that
2621 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2622 */
2623
2624static int
2625nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002626 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Al Viro82a2c1b2011-06-22 18:30:55 -04002628 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002630 struct rpc_cred *cred = NULL;
2631 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int status = 0;
2633
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002634 if (ctx != NULL) {
2635 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002636 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002639 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002640 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002641 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (IS_ERR(state)) {
2643 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002644 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002646 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002647 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002648 if (ctx != NULL)
2649 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002650 else
Al Viro643168c2011-06-22 18:20:23 -04002651 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652out:
2653 return status;
2654}
2655
2656static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2657{
Trond Myklebust16e42952005-10-27 22:12:44 -04002658 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002659 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002661 .name.len = name->len,
2662 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002663 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002665 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002666 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002667 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2670 .rpc_argp = &args,
2671 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002673 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Trond Myklebustd3468902010-04-16 16:22:50 -04002675 res.dir_attr = nfs_alloc_fattr();
2676 if (res.dir_attr == NULL)
2677 goto out;
2678
Bryan Schumaker7c513052011-03-24 17:12:24 +00002679 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002680 if (status == 0) {
2681 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002682 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002683 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002684 nfs_free_fattr(res.dir_attr);
2685out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return status;
2687}
2688
2689static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2690{
2691 struct nfs4_exception exception = { };
2692 int err;
2693 do {
2694 err = nfs4_handle_exception(NFS_SERVER(dir),
2695 _nfs4_proc_remove(dir, name),
2696 &exception);
2697 } while (exception.retry);
2698 return err;
2699}
2700
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002701static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002703 struct nfs_server *server = NFS_SERVER(dir);
2704 struct nfs_removeargs *args = msg->rpc_argp;
2705 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Trond Myklebusta65318b2009-03-11 14:10:28 -04002707 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002709 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2716
Trond Myklebust14516c32010-07-31 14:29:06 -04002717 if (!nfs4_sequence_done(task, &res->seq_res))
2718 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002719 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002720 return 0;
2721 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002722 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002723 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Jeff Laytond3d41522010-09-17 17:31:57 -04002726static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2727{
2728 struct nfs_server *server = NFS_SERVER(dir);
2729 struct nfs_renameargs *arg = msg->rpc_argp;
2730 struct nfs_renameres *res = msg->rpc_resp;
2731
2732 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2733 arg->bitmask = server->attr_bitmask;
2734 res->server = server;
2735}
2736
2737static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2738 struct inode *new_dir)
2739{
2740 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2741
2742 if (!nfs4_sequence_done(task, &res->seq_res))
2743 return 0;
2744 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2745 return 0;
2746
2747 update_changeattr(old_dir, &res->old_cinfo);
2748 nfs_post_op_update_inode(old_dir, res->old_fattr);
2749 update_changeattr(new_dir, &res->new_cinfo);
2750 nfs_post_op_update_inode(new_dir, res->new_fattr);
2751 return 1;
2752}
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2755 struct inode *new_dir, struct qstr *new_name)
2756{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002757 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002758 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 .old_dir = NFS_FH(old_dir),
2760 .new_dir = NFS_FH(new_dir),
2761 .old_name = old_name,
2762 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002763 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002765 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002766 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002767 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 struct rpc_message msg = {
2769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2770 .rpc_argp = &arg,
2771 .rpc_resp = &res,
2772 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002773 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Trond Myklebust011fff72010-04-16 16:22:49 -04002775 res.old_fattr = nfs_alloc_fattr();
2776 res.new_fattr = nfs_alloc_fattr();
2777 if (res.old_fattr == NULL || res.new_fattr == NULL)
2778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Bryan Schumaker7c513052011-03-24 17:12:24 +00002780 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (!status) {
2782 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002783 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002785 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002787out:
2788 nfs_free_fattr(res.new_fattr);
2789 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return status;
2791}
2792
2793static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2794 struct inode *new_dir, struct qstr *new_name)
2795{
2796 struct nfs4_exception exception = { };
2797 int err;
2798 do {
2799 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2800 _nfs4_proc_rename(old_dir, old_name,
2801 new_dir, new_name),
2802 &exception);
2803 } while (exception.retry);
2804 return err;
2805}
2806
2807static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2808{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002809 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 struct nfs4_link_arg arg = {
2811 .fh = NFS_FH(inode),
2812 .dir_fh = NFS_FH(dir),
2813 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002814 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002816 struct nfs4_link_res res = {
2817 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002818 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 struct rpc_message msg = {
2820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2821 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002822 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002824 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Trond Myklebust136f2622010-04-16 16:22:49 -04002826 res.fattr = nfs_alloc_fattr();
2827 res.dir_attr = nfs_alloc_fattr();
2828 if (res.fattr == NULL || res.dir_attr == NULL)
2829 goto out;
2830
Bryan Schumaker7c513052011-03-24 17:12:24 +00002831 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002832 if (!status) {
2833 update_changeattr(dir, &res.cinfo);
2834 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002835 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002836 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002837out:
2838 nfs_free_fattr(res.dir_attr);
2839 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return status;
2841}
2842
2843static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(inode),
2849 _nfs4_proc_link(inode, dir, name),
2850 &exception);
2851 } while (exception.retry);
2852 return err;
2853}
2854
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002855struct nfs4_createdata {
2856 struct rpc_message msg;
2857 struct nfs4_create_arg arg;
2858 struct nfs4_create_res res;
2859 struct nfs_fh fh;
2860 struct nfs_fattr fattr;
2861 struct nfs_fattr dir_fattr;
2862};
2863
2864static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2865 struct qstr *name, struct iattr *sattr, u32 ftype)
2866{
2867 struct nfs4_createdata *data;
2868
2869 data = kzalloc(sizeof(*data), GFP_KERNEL);
2870 if (data != NULL) {
2871 struct nfs_server *server = NFS_SERVER(dir);
2872
2873 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2874 data->msg.rpc_argp = &data->arg;
2875 data->msg.rpc_resp = &data->res;
2876 data->arg.dir_fh = NFS_FH(dir);
2877 data->arg.server = server;
2878 data->arg.name = name;
2879 data->arg.attrs = sattr;
2880 data->arg.ftype = ftype;
2881 data->arg.bitmask = server->attr_bitmask;
2882 data->res.server = server;
2883 data->res.fh = &data->fh;
2884 data->res.fattr = &data->fattr;
2885 data->res.dir_fattr = &data->dir_fattr;
2886 nfs_fattr_init(data->res.fattr);
2887 nfs_fattr_init(data->res.dir_fattr);
2888 }
2889 return data;
2890}
2891
2892static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2893{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002894 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002895 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002896 if (status == 0) {
2897 update_changeattr(dir, &data->res.dir_cinfo);
2898 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2899 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2900 }
2901 return status;
2902}
2903
2904static void nfs4_free_createdata(struct nfs4_createdata *data)
2905{
2906 kfree(data);
2907}
2908
Chuck Lever4f390c12006-08-22 20:06:22 -04002909static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002910 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002912 struct nfs4_createdata *data;
2913 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Chuck Lever94a6d752006-08-22 20:06:23 -04002915 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002916 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002917
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002918 status = -ENOMEM;
2919 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2920 if (data == NULL)
2921 goto out;
2922
2923 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2924 data->arg.u.symlink.pages = &page;
2925 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002927 status = nfs4_do_create(dir, dentry, data);
2928
2929 nfs4_free_createdata(data);
2930out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return status;
2932}
2933
Chuck Lever4f390c12006-08-22 20:06:22 -04002934static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002935 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 struct nfs4_exception exception = { };
2938 int err;
2939 do {
2940 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002941 _nfs4_proc_symlink(dir, dentry, page,
2942 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 &exception);
2944 } while (exception.retry);
2945 return err;
2946}
2947
2948static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2949 struct iattr *sattr)
2950{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002951 struct nfs4_createdata *data;
2952 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002954 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2955 if (data == NULL)
2956 goto out;
2957
2958 status = nfs4_do_create(dir, dentry, data);
2959
2960 nfs4_free_createdata(data);
2961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return status;
2963}
2964
2965static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2966 struct iattr *sattr)
2967{
2968 struct nfs4_exception exception = { };
2969 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002970
2971 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 do {
2973 err = nfs4_handle_exception(NFS_SERVER(dir),
2974 _nfs4_proc_mkdir(dir, dentry, sattr),
2975 &exception);
2976 } while (exception.retry);
2977 return err;
2978}
2979
2980static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002981 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 struct inode *dir = dentry->d_inode;
2984 struct nfs4_readdir_arg args = {
2985 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002986 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .pgbase = 0,
2988 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002989 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002990 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 };
2992 struct nfs4_readdir_res res;
2993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2995 .rpc_argp = &args,
2996 .rpc_resp = &res,
2997 .rpc_cred = cred,
2998 };
2999 int status;
3000
Harvey Harrison3110ff82008-05-02 13:42:44 -07003001 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003002 dentry->d_parent->d_name.name,
3003 dentry->d_name.name,
3004 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3006 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003007 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 -05003008 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003010 status += args.pgbase;
3011 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003012
3013 nfs_invalidate_atime(dir);
3014
Harvey Harrison3110ff82008-05-02 13:42:44 -07003015 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return status;
3017}
3018
3019static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003020 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 struct nfs4_exception exception = { };
3023 int err;
3024 do {
3025 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3026 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003027 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 &exception);
3029 } while (exception.retry);
3030 return err;
3031}
3032
3033static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3034 struct iattr *sattr, dev_t rdev)
3035{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003036 struct nfs4_createdata *data;
3037 int mode = sattr->ia_mode;
3038 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3041 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042
3043 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3044 if (data == NULL)
3045 goto out;
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003050 data->arg.ftype = NF4BLK;
3051 data->arg.u.device.specdata1 = MAJOR(rdev);
3052 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 data->arg.ftype = NF4CHR;
3056 data->arg.u.device.specdata1 = MAJOR(rdev);
3057 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 status = nfs4_do_create(dir, dentry, data);
3061
3062 nfs4_free_createdata(data);
3063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return status;
3065}
3066
3067static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3068 struct iattr *sattr, dev_t rdev)
3069{
3070 struct nfs4_exception exception = { };
3071 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003072
3073 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 do {
3075 err = nfs4_handle_exception(NFS_SERVER(dir),
3076 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3077 &exception);
3078 } while (exception.retry);
3079 return err;
3080}
3081
3082static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3083 struct nfs_fsstat *fsstat)
3084{
3085 struct nfs4_statfs_arg args = {
3086 .fh = fhandle,
3087 .bitmask = server->attr_bitmask,
3088 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003089 struct nfs4_statfs_res res = {
3090 .fsstat = fsstat,
3091 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct rpc_message msg = {
3093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3094 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003095 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 };
3097
Trond Myklebust0e574af2005-10-27 22:12:38 -04003098 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
3102static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3103{
3104 struct nfs4_exception exception = { };
3105 int err;
3106 do {
3107 err = nfs4_handle_exception(server,
3108 _nfs4_proc_statfs(server, fhandle, fsstat),
3109 &exception);
3110 } while (exception.retry);
3111 return err;
3112}
3113
3114static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3115 struct nfs_fsinfo *fsinfo)
3116{
3117 struct nfs4_fsinfo_arg args = {
3118 .fh = fhandle,
3119 .bitmask = server->attr_bitmask,
3120 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003121 struct nfs4_fsinfo_res res = {
3122 .fsinfo = fsinfo,
3123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct rpc_message msg = {
3125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3126 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003127 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 };
3129
Bryan Schumaker7c513052011-03-24 17:12:24 +00003130 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
3133static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3134{
3135 struct nfs4_exception exception = { };
3136 int err;
3137
3138 do {
3139 err = nfs4_handle_exception(server,
3140 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3141 &exception);
3142 } while (exception.retry);
3143 return err;
3144}
3145
3146static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3147{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003148 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3150}
3151
3152static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3153 struct nfs_pathconf *pathconf)
3154{
3155 struct nfs4_pathconf_arg args = {
3156 .fh = fhandle,
3157 .bitmask = server->attr_bitmask,
3158 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003159 struct nfs4_pathconf_res res = {
3160 .pathconf = pathconf,
3161 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 struct rpc_message msg = {
3163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3164 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003165 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 };
3167
3168 /* None of the pathconf attributes are mandatory to implement */
3169 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3170 memset(pathconf, 0, sizeof(*pathconf));
3171 return 0;
3172 }
3173
Trond Myklebust0e574af2005-10-27 22:12:38 -04003174 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003175 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
3178static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3179 struct nfs_pathconf *pathconf)
3180{
3181 struct nfs4_exception exception = { };
3182 int err;
3183
3184 do {
3185 err = nfs4_handle_exception(server,
3186 _nfs4_proc_pathconf(server, fhandle, pathconf),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
Benny Halevyd20581a2011-05-22 19:52:03 +03003192void __nfs4_read_done_cb(struct nfs_read_data *data)
3193{
3194 nfs_invalidate_atime(data->inode);
3195}
3196
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003197static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Trond Myklebustec06c092006-03-20 13:44:27 -05003199 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003201 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003202 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003203 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003205
Benny Halevyd20581a2011-05-22 19:52:03 +03003206 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003208 renew_lease(server, data->timestamp);
3209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003212static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3213{
3214
3215 dprintk("--> %s\n", __func__);
3216
3217 if (!nfs4_sequence_done(task, &data->res.seq_res))
3218 return -EAGAIN;
3219
Benny Halevyd20581a2011-05-22 19:52:03 +03003220 return data->read_done_cb ? data->read_done_cb(task, data) :
3221 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003222}
3223
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003224static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003227 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003228 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003231/* Reset the the nfs_read_data to send the read to the MDS. */
3232void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3233{
3234 dprintk("%s Reset task for i/o through\n", __func__);
3235 put_lseg(data->lseg);
3236 data->lseg = NULL;
3237 /* offsets will differ in the dense stripe case */
3238 data->args.offset = data->mds_offset;
3239 data->ds_clp = NULL;
3240 data->args.fh = NFS_FH(data->inode);
3241 data->read_done_cb = nfs4_read_done_cb;
3242 task->tk_ops = data->mds_ops;
3243 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3244}
3245EXPORT_SYMBOL_GPL(nfs4_reset_read);
3246
Fred Isamanb029bc92011-03-03 15:13:42 +00003247static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 struct inode *inode = data->inode;
3250
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003251 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003252 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003253 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 }
Trond Myklebust4f9838c72005-10-27 22:12:44 -04003255 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003257 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c72005-10-27 22:12:44 -04003258 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Fred Isamanb029bc92011-03-03 15:13:42 +00003262static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3263{
3264 if (!nfs4_sequence_done(task, &data->res.seq_res))
3265 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003266 return data->write_done_cb ? data->write_done_cb(task, data) :
3267 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003268}
3269
Fred Isamana69aef12011-03-03 15:13:47 +00003270/* Reset the the nfs_write_data to send the write to the MDS. */
3271void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3272{
3273 dprintk("%s Reset task for i/o through\n", __func__);
3274 put_lseg(data->lseg);
3275 data->lseg = NULL;
3276 data->ds_clp = NULL;
3277 data->write_done_cb = nfs4_write_done_cb;
3278 data->args.fh = NFS_FH(data->inode);
3279 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3280 data->args.offset = data->mds_offset;
3281 data->res.fattr = &data->fattr;
3282 task->tk_ops = data->mds_ops;
3283 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3284}
3285EXPORT_SYMBOL_GPL(nfs4_reset_write);
3286
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003287static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003289 struct nfs_server *server = NFS_SERVER(data->inode);
3290
Fred Isaman7ffd1062011-03-03 15:13:46 +00003291 if (data->lseg) {
3292 data->args.bitmask = NULL;
3293 data->res.fattr = NULL;
3294 } else
3295 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003296 if (!data->write_done_cb)
3297 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c72005-10-27 22:12:44 -04003298 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 data->timestamp = jiffies;
3300
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003301 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Fred Isaman5f452432011-03-23 13:27:46 +00003304static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003307
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003308 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003309 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003310 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003312 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Fred Isaman5f452432011-03-23 13:27:46 +00003316static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3317{
3318 if (!nfs4_sequence_done(task, &data->res.seq_res))
3319 return -EAGAIN;
3320 return data->write_done_cb(task, data);
3321}
3322
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003323static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003325 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003326
3327 if (data->lseg) {
3328 data->args.bitmask = NULL;
3329 data->res.fattr = NULL;
3330 } else
3331 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003332 if (!data->write_done_cb)
3333 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c72005-10-27 22:12:44 -04003334 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003335 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336}
3337
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003338struct nfs4_renewdata {
3339 struct nfs_client *client;
3340 unsigned long timestamp;
3341};
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343/*
3344 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3345 * standalone procedure for queueing an asynchronous RENEW.
3346 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003347static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003348{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003349 struct nfs4_renewdata *data = calldata;
3350 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003351
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003352 if (atomic_read(&clp->cl_count) > 1)
3353 nfs4_schedule_state_renewal(clp);
3354 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003355 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003356}
3357
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003358static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003360 struct nfs4_renewdata *data = calldata;
3361 struct nfs_client *clp = data->client;
3362 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003365 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003366 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3367 return;
3368 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003369 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003370 return;
3371 }
3372 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003374 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}
3376
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003377static const struct rpc_call_ops nfs4_renew_ops = {
3378 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003379 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003380};
3381
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003382static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
3384 struct rpc_message msg = {
3385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3386 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003387 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003389 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003391 if (renew_flags == 0)
3392 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003393 if (!atomic_inc_not_zero(&clp->cl_count))
3394 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003395 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003396 if (data == NULL)
3397 return -ENOMEM;
3398 data->client = clp;
3399 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003401 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003404static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 struct rpc_message msg = {
3407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3408 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003409 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 };
3411 unsigned long now = jiffies;
3412 int status;
3413
3414 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3415 if (status < 0)
3416 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003417 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return 0;
3419}
3420
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003421static inline int nfs4_server_supports_acls(struct nfs_server *server)
3422{
3423 return (server->caps & NFS_CAP_ACLS)
3424 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3425 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3426}
3427
3428/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3429 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3430 * the stack.
3431 */
3432#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3433
3434static void buf_to_pages(const void *buf, size_t buflen,
3435 struct page **pages, unsigned int *pgbase)
3436{
3437 const void *p = buf;
3438
3439 *pgbase = offset_in_page(buf);
3440 p -= *pgbase;
3441 while (p < buf + buflen) {
3442 *(pages++) = virt_to_page(p);
3443 p += PAGE_CACHE_SIZE;
3444 }
3445}
3446
Neil Hormane9e3d722011-03-04 19:26:03 -05003447static int buf_to_pages_noslab(const void *buf, size_t buflen,
3448 struct page **pages, unsigned int *pgbase)
3449{
3450 struct page *newpage, **spages;
3451 int rc = 0;
3452 size_t len;
3453 spages = pages;
3454
3455 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003456 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003457 newpage = alloc_page(GFP_KERNEL);
3458
3459 if (newpage == NULL)
3460 goto unwind;
3461 memcpy(page_address(newpage), buf, len);
3462 buf += len;
3463 buflen -= len;
3464 *pages++ = newpage;
3465 rc++;
3466 } while (buflen != 0);
3467
3468 return rc;
3469
3470unwind:
3471 for(; rc > 0; rc--)
3472 __free_page(spages[rc-1]);
3473 return -ENOMEM;
3474}
3475
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003476struct nfs4_cached_acl {
3477 int cached;
3478 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003479 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003480};
3481
3482static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003483{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003484 struct nfs_inode *nfsi = NFS_I(inode);
3485
3486 spin_lock(&inode->i_lock);
3487 kfree(nfsi->nfs4_acl);
3488 nfsi->nfs4_acl = acl;
3489 spin_unlock(&inode->i_lock);
3490}
3491
3492static void nfs4_zap_acl_attr(struct inode *inode)
3493{
3494 nfs4_set_cached_acl(inode, NULL);
3495}
3496
3497static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3498{
3499 struct nfs_inode *nfsi = NFS_I(inode);
3500 struct nfs4_cached_acl *acl;
3501 int ret = -ENOENT;
3502
3503 spin_lock(&inode->i_lock);
3504 acl = nfsi->nfs4_acl;
3505 if (acl == NULL)
3506 goto out;
3507 if (buf == NULL) /* user is just asking for length */
3508 goto out_len;
3509 if (acl->cached == 0)
3510 goto out;
3511 ret = -ERANGE; /* see getxattr(2) man page */
3512 if (acl->len > buflen)
3513 goto out;
3514 memcpy(buf, acl->data, acl->len);
3515out_len:
3516 ret = acl->len;
3517out:
3518 spin_unlock(&inode->i_lock);
3519 return ret;
3520}
3521
3522static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3523{
3524 struct nfs4_cached_acl *acl;
3525
3526 if (buf && acl_len <= PAGE_SIZE) {
3527 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3528 if (acl == NULL)
3529 goto out;
3530 acl->cached = 1;
3531 memcpy(acl->data, buf, acl_len);
3532 } else {
3533 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3534 if (acl == NULL)
3535 goto out;
3536 acl->cached = 0;
3537 }
3538 acl->len = acl_len;
3539out:
3540 nfs4_set_cached_acl(inode, acl);
3541}
3542
Trond Myklebust16b42892006-08-24 12:27:15 -04003543static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003544{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003545 struct page *pages[NFS4ACL_MAXPAGES];
3546 struct nfs_getaclargs args = {
3547 .fh = NFS_FH(inode),
3548 .acl_pages = pages,
3549 .acl_len = buflen,
3550 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003551 struct nfs_getaclres res = {
3552 .acl_len = buflen,
3553 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003554 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3557 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003558 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003559 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003560 struct page *localpage = NULL;
3561 int ret;
3562
3563 if (buflen < PAGE_SIZE) {
3564 /* As long as we're doing a round trip to the server anyway,
3565 * let's be prepared for a page of acl data. */
3566 localpage = alloc_page(GFP_KERNEL);
3567 resp_buf = page_address(localpage);
3568 if (localpage == NULL)
3569 return -ENOMEM;
3570 args.acl_pages[0] = localpage;
3571 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003572 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003573 } else {
3574 resp_buf = buf;
3575 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3576 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003577 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003578 if (ret)
3579 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003580 if (res.acl_len > args.acl_len)
3581 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003582 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003583 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003584 if (buf) {
3585 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003586 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003587 goto out_free;
3588 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003589 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003590 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003591 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003592out_free:
3593 if (localpage)
3594 __free_page(localpage);
3595 return ret;
3596}
3597
Trond Myklebust16b42892006-08-24 12:27:15 -04003598static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3599{
3600 struct nfs4_exception exception = { };
3601 ssize_t ret;
3602 do {
3603 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3604 if (ret >= 0)
3605 break;
3606 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3607 } while (exception.retry);
3608 return ret;
3609}
3610
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003611static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3612{
3613 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003614 int ret;
3615
3616 if (!nfs4_server_supports_acls(server))
3617 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003618 ret = nfs_revalidate_inode(server, inode);
3619 if (ret < 0)
3620 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003621 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3622 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003623 ret = nfs4_read_cached_acl(inode, buf, buflen);
3624 if (ret != -ENOENT)
3625 return ret;
3626 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003627}
3628
Trond Myklebust16b42892006-08-24 12:27:15 -04003629static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003630{
3631 struct nfs_server *server = NFS_SERVER(inode);
3632 struct page *pages[NFS4ACL_MAXPAGES];
3633 struct nfs_setaclargs arg = {
3634 .fh = NFS_FH(inode),
3635 .acl_pages = pages,
3636 .acl_len = buflen,
3637 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003638 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3641 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003642 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003643 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003644 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003645
3646 if (!nfs4_server_supports_acls(server))
3647 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003648 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3649 if (i < 0)
3650 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003651 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003652 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003653
3654 /*
3655 * Free each page after tx, so the only ref left is
3656 * held by the network stack
3657 */
3658 for (; i > 0; i--)
3659 put_page(pages[i-1]);
3660
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003661 /*
3662 * Acl update can result in inode attribute update.
3663 * so mark the attribute cache invalid.
3664 */
3665 spin_lock(&inode->i_lock);
3666 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3667 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003668 nfs_access_zap_cache(inode);
3669 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003670 return ret;
3671}
3672
Trond Myklebust16b42892006-08-24 12:27:15 -04003673static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3674{
3675 struct nfs4_exception exception = { };
3676 int err;
3677 do {
3678 err = nfs4_handle_exception(NFS_SERVER(inode),
3679 __nfs4_proc_set_acl(inode, buf, buflen),
3680 &exception);
3681 } while (exception.retry);
3682 return err;
3683}
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003686nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003688 struct nfs_client *clp = server->nfs_client;
3689
3690 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return 0;
3692 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003693 case -NFS4ERR_ADMIN_REVOKED:
3694 case -NFS4ERR_BAD_STATEID:
3695 case -NFS4ERR_OPENMODE:
3696 if (state == NULL)
3697 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003698 nfs4_schedule_stateid_recovery(server, state);
3699 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003700 case -NFS4ERR_EXPIRED:
3701 if (state != NULL)
3702 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003704 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003705 nfs4_schedule_lease_recovery(clp);
3706 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003707#if defined(CONFIG_NFS_V4_1)
3708 case -NFS4ERR_BADSESSION:
3709 case -NFS4ERR_BADSLOT:
3710 case -NFS4ERR_BAD_HIGH_SLOT:
3711 case -NFS4ERR_DEADSESSION:
3712 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3713 case -NFS4ERR_SEQ_FALSE_RETRY:
3714 case -NFS4ERR_SEQ_MISORDERED:
3715 dprintk("%s ERROR %d, Reset session\n", __func__,
3716 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003717 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003718 task->tk_status = 0;
3719 return -EAGAIN;
3720#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003722 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003723 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003724 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3726 task->tk_status = 0;
3727 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003728 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 case -NFS4ERR_OLD_STATEID:
3730 task->tk_status = 0;
3731 return -EAGAIN;
3732 }
3733 task->tk_status = nfs4_map_errors(task->tk_status);
3734 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003735wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003736 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003737 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3738 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3739 task->tk_status = 0;
3740 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003743int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3744 unsigned short port, struct rpc_cred *cred,
3745 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
3747 nfs4_verifier sc_verifier;
3748 struct nfs4_setclientid setclientid = {
3749 .sc_verifier = &sc_verifier,
3750 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003751 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 };
3753 struct rpc_message msg = {
3754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3755 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003756 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003757 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 };
Al Virobc4785c2006-10-19 23:28:51 -07003759 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 int loop = 0;
3761 int status;
3762
Al Virobc4785c2006-10-19 23:28:51 -07003763 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3765 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3766
3767 for(;;) {
3768 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003769 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003770 clp->cl_ipaddr,
3771 rpc_peeraddr2str(clp->cl_rpcclient,
3772 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003773 rpc_peeraddr2str(clp->cl_rpcclient,
3774 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003775 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 clp->cl_id_uniquifier);
3777 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003778 sizeof(setclientid.sc_netid),
3779 rpc_peeraddr2str(clp->cl_rpcclient,
3780 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003782 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 clp->cl_ipaddr, port >> 8, port & 255);
3784
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003785 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (status != -NFS4ERR_CLID_INUSE)
3787 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003788 if (loop != 0) {
3789 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003791 }
3792 ++loop;
3793 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
3795 return status;
3796}
3797
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003798int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003799 struct nfs4_setclientid_res *arg,
3800 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
3802 struct nfs_fsinfo fsinfo;
3803 struct rpc_message msg = {
3804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003805 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003807 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 };
3809 unsigned long now;
3810 int status;
3811
3812 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003813 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (status == 0) {
3815 spin_lock(&clp->cl_lock);
3816 clp->cl_lease_time = fsinfo.lease_time * HZ;
3817 clp->cl_last_renewal = now;
3818 spin_unlock(&clp->cl_lock);
3819 }
3820 return status;
3821}
3822
Trond Myklebustfe650402006-01-03 09:55:18 +01003823struct nfs4_delegreturndata {
3824 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003825 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003826 struct nfs_fh fh;
3827 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003828 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003829 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003830 int rpc_status;
3831};
3832
Trond Myklebustfe650402006-01-03 09:55:18 +01003833static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3834{
3835 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003836
Trond Myklebust14516c32010-07-31 14:29:06 -04003837 if (!nfs4_sequence_done(task, &data->res.seq_res))
3838 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003839
Ricardo Labiaga79708862009-12-07 09:23:21 -05003840 switch (task->tk_status) {
3841 case -NFS4ERR_STALE_STATEID:
3842 case -NFS4ERR_EXPIRED:
3843 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003844 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003845 break;
3846 default:
3847 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3848 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003849 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003850 return;
3851 }
3852 }
3853 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003854}
3855
3856static void nfs4_delegreturn_release(void *calldata)
3857{
Trond Myklebustfe650402006-01-03 09:55:18 +01003858 kfree(calldata);
3859}
3860
Andy Adamson938e1012009-04-01 09:22:28 -04003861#if defined(CONFIG_NFS_V4_1)
3862static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3863{
3864 struct nfs4_delegreturndata *d_data;
3865
3866 d_data = (struct nfs4_delegreturndata *)data;
3867
Trond Myklebust035168ab2010-06-16 09:52:26 -04003868 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003869 &d_data->args.seq_args,
3870 &d_data->res.seq_res, 1, task))
3871 return;
3872 rpc_call_start(task);
3873}
3874#endif /* CONFIG_NFS_V4_1 */
3875
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003876static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003877#if defined(CONFIG_NFS_V4_1)
3878 .rpc_call_prepare = nfs4_delegreturn_prepare,
3879#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003880 .rpc_call_done = nfs4_delegreturn_done,
3881 .rpc_release = nfs4_delegreturn_release,
3882};
3883
Trond Myklebuste6f81072008-01-24 18:14:34 -05003884static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003885{
3886 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003887 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003888 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003889 struct rpc_message msg = {
3890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3891 .rpc_cred = cred,
3892 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003893 struct rpc_task_setup task_setup_data = {
3894 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003895 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003896 .callback_ops = &nfs4_delegreturn_ops,
3897 .flags = RPC_TASK_ASYNC,
3898 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003899 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003900
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003901 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003902 if (data == NULL)
3903 return -ENOMEM;
3904 data->args.fhandle = &data->fh;
3905 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003906 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003907 nfs_copy_fh(&data->fh, NFS_FH(inode));
3908 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003909 data->res.fattr = &data->fattr;
3910 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003911 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003912 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003913 data->rpc_status = 0;
3914
Trond Myklebustc970aa82007-07-14 15:39:59 -04003915 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003916 msg.rpc_argp = &data->args;
3917 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003918 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003919 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003920 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003921 if (!issync)
3922 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003923 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003924 if (status != 0)
3925 goto out;
3926 status = data->rpc_status;
3927 if (status != 0)
3928 goto out;
3929 nfs_refresh_inode(inode, &data->fattr);
3930out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003931 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003932 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
Trond Myklebuste6f81072008-01-24 18:14:34 -05003935int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
3937 struct nfs_server *server = NFS_SERVER(inode);
3938 struct nfs4_exception exception = { };
3939 int err;
3940 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003941 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 switch (err) {
3943 case -NFS4ERR_STALE_STATEID:
3944 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 case 0:
3946 return 0;
3947 }
3948 err = nfs4_handle_exception(server, err, &exception);
3949 } while (exception.retry);
3950 return err;
3951}
3952
3953#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3954#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3955
3956/*
3957 * sleep, with exponential backoff, and retry the LOCK operation.
3958 */
3959static unsigned long
3960nfs4_set_lock_task_retry(unsigned long timeout)
3961{
Jeff Laytond3103102011-12-01 22:44:39 +01003962 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 timeout <<= 1;
3964 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3965 return NFS4_LOCK_MAXTIMEOUT;
3966 return timeout;
3967}
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3970{
3971 struct inode *inode = state->inode;
3972 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003973 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003976 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003978 struct nfs_lockt_res res = {
3979 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 };
3981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3983 .rpc_argp = &arg,
3984 .rpc_resp = &res,
3985 .rpc_cred = state->owner->so_cred,
3986 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct nfs4_lock_state *lsp;
3988 int status;
3989
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003990 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003991 status = nfs4_set_lock_state(state, request);
3992 if (status != 0)
3993 goto out;
3994 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003995 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003996 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003997 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003998 switch (status) {
3999 case 0:
4000 request->fl_type = F_UNLCK;
4001 break;
4002 case -NFS4ERR_DENIED:
4003 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004005 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004006out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return status;
4008}
4009
4010static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4011{
4012 struct nfs4_exception exception = { };
4013 int err;
4014
4015 do {
4016 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4017 _nfs4_proc_getlk(state, cmd, request),
4018 &exception);
4019 } while (exception.retry);
4020 return err;
4021}
4022
4023static int do_vfs_lock(struct file *file, struct file_lock *fl)
4024{
4025 int res = 0;
4026 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4027 case FL_POSIX:
4028 res = posix_lock_file_wait(file, fl);
4029 break;
4030 case FL_FLOCK:
4031 res = flock_lock_file_wait(file, fl);
4032 break;
4033 default:
4034 BUG();
4035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return res;
4037}
4038
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004039struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004040 struct nfs_locku_args arg;
4041 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004042 struct nfs4_lock_state *lsp;
4043 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004044 struct file_lock fl;
4045 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004046 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004047};
4048
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004049static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4050 struct nfs_open_context *ctx,
4051 struct nfs4_lock_state *lsp,
4052 struct nfs_seqid *seqid)
4053{
4054 struct nfs4_unlockdata *p;
4055 struct inode *inode = lsp->ls_state->inode;
4056
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004057 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004058 if (p == NULL)
4059 return NULL;
4060 p->arg.fh = NFS_FH(inode);
4061 p->arg.fl = &p->fl;
4062 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004063 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004064 p->arg.stateid = &lsp->ls_stateid;
4065 p->lsp = lsp;
4066 atomic_inc(&lsp->ls_count);
4067 /* Ensure we don't close file until we're done freeing locks! */
4068 p->ctx = get_nfs_open_context(ctx);
4069 memcpy(&p->fl, fl, sizeof(p->fl));
4070 p->server = NFS_SERVER(inode);
4071 return p;
4072}
4073
Trond Myklebust06f814a2006-01-03 09:55:07 +01004074static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004075{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004076 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004077 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004078 nfs4_put_lock_state(calldata->lsp);
4079 put_nfs_open_context(calldata->ctx);
4080 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081}
4082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004083static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004085 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004086
Trond Myklebust14516c32010-07-31 14:29:06 -04004087 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4088 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004089 switch (task->tk_status) {
4090 case 0:
4091 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004092 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004094 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004095 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004096 case -NFS4ERR_BAD_STATEID:
4097 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004098 case -NFS4ERR_STALE_STATEID:
4099 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100 break;
4101 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004102 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004103 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004105}
4106
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004107static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004108{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004109 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004111 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004112 return;
4113 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004114 /* Note: exit _without_ running nfs4_locku_done */
4115 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004116 return;
4117 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004118 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004119 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004120 &calldata->arg.seq_args,
4121 &calldata->res.seq_res, 1, task))
4122 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004123 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004126static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004127 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004128 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004129 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004130};
4131
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004132static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4133 struct nfs_open_context *ctx,
4134 struct nfs4_lock_state *lsp,
4135 struct nfs_seqid *seqid)
4136{
4137 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004138 struct rpc_message msg = {
4139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4140 .rpc_cred = ctx->cred,
4141 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004142 struct rpc_task_setup task_setup_data = {
4143 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004144 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004145 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004146 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004147 .flags = RPC_TASK_ASYNC,
4148 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004149
Frank Filz137d6ac2007-07-09 15:32:29 -07004150 /* Ensure this is an unlock - when canceling a lock, the
4151 * canceled lock is passed in, and it won't be an unlock.
4152 */
4153 fl->fl_type = F_UNLCK;
4154
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4156 if (data == NULL) {
4157 nfs_free_seqid(seqid);
4158 return ERR_PTR(-ENOMEM);
4159 }
4160
Trond Myklebust1174dd12010-12-21 10:52:24 -05004161 msg.rpc_argp = &data->arg;
4162 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004163 task_setup_data.callback_data = data;
4164 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004165}
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4168{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004169 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004170 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004171 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004172 struct rpc_task *task;
4173 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004174 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Trond Myklebust9b073572006-06-29 16:38:34 -04004176 status = nfs4_set_lock_state(state, request);
4177 /* Unlock _before_ we do the RPC call */
4178 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004179 down_read(&nfsi->rwsem);
4180 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4181 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004182 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004183 }
4184 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004185 if (status != 0)
4186 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004187 /* Is this a delegated lock? */
4188 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004189 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004190 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004191 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004192 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004193 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004194 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004195 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196 status = PTR_ERR(task);
4197 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004198 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004199 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004200 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004201out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004202 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004203 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206struct nfs4_lockdata {
4207 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004208 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 struct nfs4_lock_state *lsp;
4210 struct nfs_open_context *ctx;
4211 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004212 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213 int rpc_status;
4214 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004215 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216};
4217
4218static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004219 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4220 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221{
4222 struct nfs4_lockdata *p;
4223 struct inode *inode = lsp->ls_state->inode;
4224 struct nfs_server *server = NFS_SERVER(inode);
4225
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004226 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004227 if (p == NULL)
4228 return NULL;
4229
4230 p->arg.fh = NFS_FH(inode);
4231 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004232 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004233 if (p->arg.open_seqid == NULL)
4234 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004235 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004237 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004239 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004240 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004241 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004242 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004244 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004245 atomic_inc(&lsp->ls_count);
4246 p->ctx = get_nfs_open_context(ctx);
4247 memcpy(&p->fl, fl, sizeof(p->fl));
4248 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004249out_free_seqid:
4250 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251out_free:
4252 kfree(p);
4253 return NULL;
4254}
4255
4256static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4257{
4258 struct nfs4_lockdata *data = calldata;
4259 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Harvey Harrison3110ff82008-05-02 13:42:44 -07004261 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004262 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4263 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004264 /* Do we need to do an open_to_lock_owner? */
4265 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004266 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4267 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268 data->arg.open_stateid = &state->stateid;
4269 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004270 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004271 } else
4272 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004273 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004274 if (nfs4_setup_sequence(data->server,
4275 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004276 &data->res.seq_res, 1, task))
4277 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004278 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004279 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004280}
4281
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004282static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4283{
4284 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4285 nfs4_lock_prepare(task, calldata);
4286}
4287
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4289{
4290 struct nfs4_lockdata *data = calldata;
4291
Harvey Harrison3110ff82008-05-02 13:42:44 -07004292 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293
Trond Myklebust14516c32010-07-31 14:29:06 -04004294 if (!nfs4_sequence_done(task, &data->res.seq_res))
4295 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004296
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004298 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299 if (data->rpc_status == 0)
4300 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4301 else
4302 goto out;
4303 }
4304 if (data->rpc_status == 0) {
4305 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4306 sizeof(data->lsp->ls_stateid.data));
4307 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004308 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312}
4313
4314static void nfs4_lock_release(void *calldata)
4315{
4316 struct nfs4_lockdata *data = calldata;
4317
Harvey Harrison3110ff82008-05-02 13:42:44 -07004318 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004319 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004320 if (data->cancelled != 0) {
4321 struct rpc_task *task;
4322 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4323 data->arg.lock_seqid);
4324 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004325 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004326 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004327 } else
4328 nfs_free_seqid(data->arg.lock_seqid);
4329 nfs4_put_lock_state(data->lsp);
4330 put_nfs_open_context(data->ctx);
4331 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004332 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333}
4334
4335static const struct rpc_call_ops nfs4_lock_ops = {
4336 .rpc_call_prepare = nfs4_lock_prepare,
4337 .rpc_call_done = nfs4_lock_done,
4338 .rpc_release = nfs4_lock_release,
4339};
4340
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004341static const struct rpc_call_ops nfs4_recover_lock_ops = {
4342 .rpc_call_prepare = nfs4_recover_lock_prepare,
4343 .rpc_call_done = nfs4_lock_done,
4344 .rpc_release = nfs4_lock_release,
4345};
4346
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004347static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4348{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004349 switch (error) {
4350 case -NFS4ERR_ADMIN_REVOKED:
4351 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004352 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004353 if (new_lock_owner != 0 ||
4354 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004355 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004356 break;
4357 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004358 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004359 case -NFS4ERR_EXPIRED:
4360 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004361 };
4362}
4363
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004364static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365{
4366 struct nfs4_lockdata *data;
4367 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004368 struct rpc_message msg = {
4369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4370 .rpc_cred = state->owner->so_cred,
4371 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004372 struct rpc_task_setup task_setup_data = {
4373 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004374 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004375 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004376 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004377 .flags = RPC_TASK_ASYNC,
4378 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 int ret;
4380
Harvey Harrison3110ff82008-05-02 13:42:44 -07004381 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004382 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004383 fl->fl_u.nfs4_fl.owner,
4384 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385 if (data == NULL)
4386 return -ENOMEM;
4387 if (IS_SETLKW(cmd))
4388 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004389 if (recovery_type > NFS_LOCK_NEW) {
4390 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004391 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004392 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4393 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004394 msg.rpc_argp = &data->arg;
4395 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004396 task_setup_data.callback_data = data;
4397 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004398 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004400 ret = nfs4_wait_for_completion_rpc_task(task);
4401 if (ret == 0) {
4402 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004403 if (ret)
4404 nfs4_handle_setlk_error(data->server, data->lsp,
4405 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406 } else
4407 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004408 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004409 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411}
4412
4413static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4414{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004415 struct nfs_server *server = NFS_SERVER(state->inode);
4416 struct nfs4_exception exception = { };
4417 int err;
4418
4419 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004420 /* Cache the lock if possible... */
4421 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4422 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004423 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004424 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004425 break;
4426 nfs4_handle_exception(server, err, &exception);
4427 } while (exception.retry);
4428 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
4431static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4432{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004433 struct nfs_server *server = NFS_SERVER(state->inode);
4434 struct nfs4_exception exception = { };
4435 int err;
4436
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004437 err = nfs4_set_lock_state(state, request);
4438 if (err != 0)
4439 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004440 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004441 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4442 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004443 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004444 switch (err) {
4445 default:
4446 goto out;
4447 case -NFS4ERR_GRACE:
4448 case -NFS4ERR_DELAY:
4449 nfs4_handle_exception(server, err, &exception);
4450 err = 0;
4451 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004452 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004453out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004457#if defined(CONFIG_NFS_V4_1)
4458static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4459{
4460 int status;
4461 struct nfs_server *server = NFS_SERVER(state->inode);
4462
4463 status = nfs41_test_stateid(server, state);
4464 if (status == NFS_OK)
4465 return 0;
4466 nfs41_free_stateid(server, state);
4467 return nfs4_lock_expired(state, request);
4468}
4469#endif
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4472{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004473 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004474 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004475 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004477 if ((fl_flags & FL_POSIX) &&
4478 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4479 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004480 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004481 status = nfs4_set_lock_state(state, request);
4482 if (status != 0)
4483 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004484 request->fl_flags |= FL_ACCESS;
4485 status = do_vfs_lock(request->fl_file, request);
4486 if (status < 0)
4487 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004488 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004489 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004491 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004492 request->fl_flags = fl_flags & ~FL_SLEEP;
4493 status = do_vfs_lock(request->fl_file, request);
4494 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004495 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004496 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004497 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004498 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004499 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004500 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004501 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004502 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004503out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004504 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004505out:
4506 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return status;
4508}
4509
4510static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4511{
4512 struct nfs4_exception exception = { };
4513 int err;
4514
4515 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004516 err = _nfs4_proc_setlk(state, cmd, request);
4517 if (err == -NFS4ERR_DENIED)
4518 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004520 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 } while (exception.retry);
4522 return err;
4523}
4524
4525static int
4526nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4527{
4528 struct nfs_open_context *ctx;
4529 struct nfs4_state *state;
4530 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4531 int status;
4532
4533 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004534 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 state = ctx->state;
4536
4537 if (request->fl_start < 0 || request->fl_end < 0)
4538 return -EINVAL;
4539
Trond Myklebustd9531262009-07-21 19:22:38 -04004540 if (IS_GETLK(cmd)) {
4541 if (state != NULL)
4542 return nfs4_proc_getlk(state, F_GETLK, request);
4543 return 0;
4544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4547 return -EINVAL;
4548
Trond Myklebustd9531262009-07-21 19:22:38 -04004549 if (request->fl_type == F_UNLCK) {
4550 if (state != NULL)
4551 return nfs4_proc_unlck(state, cmd, request);
4552 return 0;
4553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Trond Myklebustd9531262009-07-21 19:22:38 -04004555 if (state == NULL)
4556 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 do {
4558 status = nfs4_proc_setlk(state, cmd, request);
4559 if ((status != -EAGAIN) || IS_SETLK(cmd))
4560 break;
4561 timeout = nfs4_set_lock_task_retry(timeout);
4562 status = -ERESTARTSYS;
4563 if (signalled())
4564 break;
4565 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 return status;
4567}
4568
Trond Myklebust888e6942005-11-04 15:38:11 -05004569int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4570{
4571 struct nfs_server *server = NFS_SERVER(state->inode);
4572 struct nfs4_exception exception = { };
4573 int err;
4574
4575 err = nfs4_set_lock_state(state, fl);
4576 if (err != 0)
4577 goto out;
4578 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004579 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004580 switch (err) {
4581 default:
4582 printk(KERN_ERR "%s: unhandled error %d.\n",
4583 __func__, err);
4584 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004585 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004586 goto out;
4587 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004588 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004589 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004590 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004591 nfs4_schedule_lease_recovery(server->nfs_client);
4592 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004593 case -NFS4ERR_BADSESSION:
4594 case -NFS4ERR_BADSLOT:
4595 case -NFS4ERR_BAD_HIGH_SLOT:
4596 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4597 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004598 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004599 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004600 case -ERESTARTSYS:
4601 /*
4602 * The show must go on: exit, but mark the
4603 * stateid as needing recovery.
4604 */
4605 case -NFS4ERR_ADMIN_REVOKED:
4606 case -NFS4ERR_BAD_STATEID:
4607 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004608 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004609 err = 0;
4610 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004611 case -EKEYEXPIRED:
4612 /*
4613 * User RPCSEC_GSS context has expired.
4614 * We cannot recover this stateid now, so
4615 * skip it and allow recovery thread to
4616 * proceed.
4617 */
4618 err = 0;
4619 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004620 case -ENOMEM:
4621 case -NFS4ERR_DENIED:
4622 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4623 err = 0;
4624 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004625 case -NFS4ERR_DELAY:
4626 break;
4627 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004628 err = nfs4_handle_exception(server, err, &exception);
4629 } while (exception.retry);
4630out:
4631 return err;
4632}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004633
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004634static void nfs4_release_lockowner_release(void *calldata)
4635{
4636 kfree(calldata);
4637}
4638
4639const struct rpc_call_ops nfs4_release_lockowner_ops = {
4640 .rpc_release = nfs4_release_lockowner_release,
4641};
4642
4643void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4644{
4645 struct nfs_server *server = lsp->ls_state->owner->so_server;
4646 struct nfs_release_lockowner_args *args;
4647 struct rpc_message msg = {
4648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4649 };
4650
4651 if (server->nfs_client->cl_mvops->minor_version != 0)
4652 return;
4653 args = kmalloc(sizeof(*args), GFP_NOFS);
4654 if (!args)
4655 return;
4656 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4657 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004658 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004659 msg.rpc_argp = args;
4660 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4661}
4662
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004663#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4664
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004665static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4666 const void *buf, size_t buflen,
4667 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004668{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004669 if (strcmp(key, "") != 0)
4670 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004671
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004672 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004673}
4674
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004675static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4676 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004677{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004678 if (strcmp(key, "") != 0)
4679 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004680
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004681 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004682}
4683
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004684static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4685 size_t list_len, const char *name,
4686 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004687{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004688 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004689
J. Bruce Fields096455a2006-03-20 23:23:42 -05004690 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4691 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004692
4693 if (list && len <= list_len)
4694 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004695 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004696}
4697
Andy Adamson533eb462011-06-13 18:25:56 -04004698/*
4699 * nfs_fhget will use either the mounted_on_fileid or the fileid
4700 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004701static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4702{
Andy Adamson533eb462011-06-13 18:25:56 -04004703 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4704 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4705 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4706 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004707 return;
4708
4709 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4710 NFS_ATTR_FATTR_NLINK;
4711 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4712 fattr->nlink = 2;
4713}
4714
Trond Myklebust56659e92007-07-17 21:52:39 -04004715int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004716 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004717{
4718 struct nfs_server *server = NFS_SERVER(dir);
4719 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004720 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004721 };
4722 struct nfs4_fs_locations_arg args = {
4723 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004724 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004725 .page = page,
4726 .bitmask = bitmask,
4727 };
Benny Halevy22958462009-04-01 09:22:02 -04004728 struct nfs4_fs_locations_res res = {
4729 .fs_locations = fs_locations,
4730 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004731 struct rpc_message msg = {
4732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4733 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004734 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004735 };
4736 int status;
4737
Harvey Harrison3110ff82008-05-02 13:42:44 -07004738 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004739
4740 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4741 * is not supported */
4742 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4743 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4744 else
4745 bitmask[0] |= FATTR4_WORD0_FILEID;
4746
Trond Myklebustc228fd32007-01-13 02:28:11 -05004747 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004748 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004749 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004750 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004751 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004752 return status;
4753}
4754
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004755static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4756{
4757 int status;
4758 struct nfs4_secinfo_arg args = {
4759 .dir_fh = NFS_FH(dir),
4760 .name = name,
4761 };
4762 struct nfs4_secinfo_res res = {
4763 .flavors = flavors,
4764 };
4765 struct rpc_message msg = {
4766 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4767 .rpc_argp = &args,
4768 .rpc_resp = &res,
4769 };
4770
4771 dprintk("NFS call secinfo %s\n", name->name);
4772 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4773 dprintk("NFS reply secinfo: %d\n", status);
4774 return status;
4775}
4776
4777int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4778{
4779 struct nfs4_exception exception = { };
4780 int err;
4781 do {
4782 err = nfs4_handle_exception(NFS_SERVER(dir),
4783 _nfs4_proc_secinfo(dir, name, flavors),
4784 &exception);
4785 } while (exception.retry);
4786 return err;
4787}
4788
Andy Adamson557134a2009-04-01 09:21:53 -04004789#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004790/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004791 * Check the exchange flags returned by the server for invalid flags, having
4792 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4793 * DS flags set.
4794 */
4795static int nfs4_check_cl_exchange_flags(u32 flags)
4796{
4797 if (flags & ~EXCHGID4_FLAG_MASK_R)
4798 goto out_inval;
4799 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4800 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4801 goto out_inval;
4802 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4803 goto out_inval;
4804 return NFS_OK;
4805out_inval:
4806 return -NFS4ERR_INVAL;
4807}
4808
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004809static bool
4810nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4811{
4812 if (a->server_scope_sz == b->server_scope_sz &&
4813 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4814 return true;
4815
4816 return false;
4817}
4818
Andy Adamson357f54d2010-12-14 10:11:57 -05004819/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004820 * nfs4_proc_exchange_id()
4821 *
4822 * Since the clientid has expired, all compounds using sessions
4823 * associated with the stale clientid will be returning
4824 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4825 * be in some phase of session reset.
4826 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004827int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004828{
4829 nfs4_verifier verifier;
4830 struct nfs41_exchange_id_args args = {
4831 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004832 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004833 };
4834 struct nfs41_exchange_id_res res = {
4835 .client = clp,
4836 };
4837 int status;
4838 struct rpc_message msg = {
4839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4840 .rpc_argp = &args,
4841 .rpc_resp = &res,
4842 .rpc_cred = cred,
4843 };
4844 __be32 *p;
4845
4846 dprintk("--> %s\n", __func__);
4847 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004848
Benny Halevy99fe60d2009-04-01 09:22:29 -04004849 p = (u32 *)verifier.data;
4850 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4851 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4852 args.verifier = &verifier;
4853
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004854 args.id_len = scnprintf(args.id, sizeof(args.id),
4855 "%s/%s.%s/%u",
4856 clp->cl_ipaddr,
4857 init_utsname()->nodename,
4858 init_utsname()->domainname,
4859 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004860
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004861 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4862 if (unlikely(!res.server_scope))
4863 return -ENOMEM;
4864
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004865 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004866 if (!status)
4867 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004868
4869 if (!status) {
4870 if (clp->server_scope &&
4871 !nfs41_same_server_scope(clp->server_scope,
4872 res.server_scope)) {
4873 dprintk("%s: server_scope mismatch detected\n",
4874 __func__);
4875 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4876 kfree(clp->server_scope);
4877 clp->server_scope = NULL;
4878 }
4879
4880 if (!clp->server_scope)
4881 clp->server_scope = res.server_scope;
4882 else
4883 kfree(res.server_scope);
4884 }
4885
Benny Halevy99fe60d2009-04-01 09:22:29 -04004886 dprintk("<-- %s status= %d\n", __func__, status);
4887 return status;
4888}
4889
Andy Adamson2050f0c2009-04-01 09:22:30 -04004890struct nfs4_get_lease_time_data {
4891 struct nfs4_get_lease_time_args *args;
4892 struct nfs4_get_lease_time_res *res;
4893 struct nfs_client *clp;
4894};
4895
4896static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4897 void *calldata)
4898{
4899 int ret;
4900 struct nfs4_get_lease_time_data *data =
4901 (struct nfs4_get_lease_time_data *)calldata;
4902
4903 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004904 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004905 /* just setup sequence, do not trigger session recovery
4906 since we're invoked within one */
4907 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004908 &data->args->la_seq_args,
4909 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004910
4911 BUG_ON(ret == -EAGAIN);
4912 rpc_call_start(task);
4913 dprintk("<-- %s\n", __func__);
4914}
4915
4916/*
4917 * Called from nfs4_state_manager thread for session setup, so don't recover
4918 * from sequence operation or clientid errors.
4919 */
4920static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4921{
4922 struct nfs4_get_lease_time_data *data =
4923 (struct nfs4_get_lease_time_data *)calldata;
4924
4925 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004926 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4927 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004928 switch (task->tk_status) {
4929 case -NFS4ERR_DELAY:
4930 case -NFS4ERR_GRACE:
4931 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4932 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4933 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004934 /* fall through */
4935 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004936 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004937 return;
4938 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004939 dprintk("<-- %s\n", __func__);
4940}
4941
4942struct rpc_call_ops nfs4_get_lease_time_ops = {
4943 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4944 .rpc_call_done = nfs4_get_lease_time_done,
4945};
4946
4947int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4948{
4949 struct rpc_task *task;
4950 struct nfs4_get_lease_time_args args;
4951 struct nfs4_get_lease_time_res res = {
4952 .lr_fsinfo = fsinfo,
4953 };
4954 struct nfs4_get_lease_time_data data = {
4955 .args = &args,
4956 .res = &res,
4957 .clp = clp,
4958 };
4959 struct rpc_message msg = {
4960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4961 .rpc_argp = &args,
4962 .rpc_resp = &res,
4963 };
4964 struct rpc_task_setup task_setup = {
4965 .rpc_client = clp->cl_rpcclient,
4966 .rpc_message = &msg,
4967 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004968 .callback_data = &data,
4969 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004970 };
4971 int status;
4972
Andy Adamson2050f0c2009-04-01 09:22:30 -04004973 dprintk("--> %s\n", __func__);
4974 task = rpc_run_task(&task_setup);
4975
4976 if (IS_ERR(task))
4977 status = PTR_ERR(task);
4978 else {
4979 status = task->tk_status;
4980 rpc_put_task(task);
4981 }
4982 dprintk("<-- %s return %d\n", __func__, status);
4983
4984 return status;
4985}
4986
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004987/*
4988 * Reset a slot table
4989 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004990static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4991 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004992{
Andy Adamson104aeba2010-01-14 17:45:10 -05004993 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004994 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004995 int ret = 0;
4996
Andy Adamson104aeba2010-01-14 17:45:10 -05004997 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4998 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004999
Andy Adamson104aeba2010-01-14 17:45:10 -05005000 /* Does the newly negotiated max_reqs match the existing slot table? */
5001 if (max_reqs != tbl->max_slots) {
5002 ret = -ENOMEM;
5003 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005004 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005005 if (!new)
5006 goto out;
5007 ret = 0;
5008 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005009 }
5010 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005011 if (new) {
5012 tbl->slots = new;
5013 tbl->max_slots = max_reqs;
5014 }
5015 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005016 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005017 spin_unlock(&tbl->slot_tbl_lock);
5018 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5019 tbl, tbl->slots, tbl->max_slots);
5020out:
5021 dprintk("<-- %s: return %d\n", __func__, ret);
5022 return ret;
5023}
5024
Andy Adamsonfc931582009-04-01 09:22:31 -04005025/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005026 * Reset the forechannel and backchannel slot tables
5027 */
5028static int nfs4_reset_slot_tables(struct nfs4_session *session)
5029{
5030 int status;
5031
5032 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005033 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005034 if (status)
5035 return status;
5036
5037 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005038 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005039 return status;
5040}
5041
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005042/* Destroy the slot table */
5043static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5044{
5045 if (session->fc_slot_table.slots != NULL) {
5046 kfree(session->fc_slot_table.slots);
5047 session->fc_slot_table.slots = NULL;
5048 }
5049 if (session->bc_slot_table.slots != NULL) {
5050 kfree(session->bc_slot_table.slots);
5051 session->bc_slot_table.slots = NULL;
5052 }
5053 return;
5054}
5055
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005056/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005057 * Initialize slot table
5058 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005059static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5060 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005061{
Andy Adamsonfc931582009-04-01 09:22:31 -04005062 struct nfs4_slot *slot;
5063 int ret = -ENOMEM;
5064
5065 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5066
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005067 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005068
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005069 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005070 if (!slot)
5071 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005072 ret = 0;
5073
5074 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005075 tbl->max_slots = max_slots;
5076 tbl->slots = slot;
5077 tbl->highest_used_slotid = -1; /* no slot is currently used */
5078 spin_unlock(&tbl->slot_tbl_lock);
5079 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5080 tbl, tbl->slots, tbl->max_slots);
5081out:
5082 dprintk("<-- %s: return %d\n", __func__, ret);
5083 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005084}
5085
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005086/*
5087 * Initialize the forechannel and backchannel tables
5088 */
5089static int nfs4_init_slot_tables(struct nfs4_session *session)
5090{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005091 struct nfs4_slot_table *tbl;
5092 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005093
Trond Myklebustf26468f2009-12-05 19:32:11 -05005094 tbl = &session->fc_slot_table;
5095 if (tbl->slots == NULL) {
5096 status = nfs4_init_slot_table(tbl,
5097 session->fc_attrs.max_reqs, 1);
5098 if (status)
5099 return status;
5100 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005101
Trond Myklebustf26468f2009-12-05 19:32:11 -05005102 tbl = &session->bc_slot_table;
5103 if (tbl->slots == NULL) {
5104 status = nfs4_init_slot_table(tbl,
5105 session->bc_attrs.max_reqs, 0);
5106 if (status)
5107 nfs4_destroy_slot_tables(session);
5108 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005109
5110 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005111}
5112
5113struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5114{
5115 struct nfs4_session *session;
5116 struct nfs4_slot_table *tbl;
5117
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005118 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005119 if (!session)
5120 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005121
Andy Adamson557134a2009-04-01 09:21:53 -04005122 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005123 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005124 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005125 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005126 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005127
5128 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005129 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005130 spin_lock_init(&tbl->slot_tbl_lock);
5131 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005132 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005133
Trond Myklebust1055d762010-06-16 09:52:27 -04005134 session->session_state = 1<<NFS4_SESSION_INITING;
5135
Andy Adamson557134a2009-04-01 09:21:53 -04005136 session->clp = clp;
5137 return session;
5138}
5139
5140void nfs4_destroy_session(struct nfs4_session *session)
5141{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005142 nfs4_proc_destroy_session(session);
5143 dprintk("%s Destroy backchannel for xprt %p\n",
5144 __func__, session->clp->cl_rpcclient->cl_xprt);
5145 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5146 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005147 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005148 kfree(session);
5149}
5150
Andy Adamsonfc931582009-04-01 09:22:31 -04005151/*
5152 * Initialize the values to be used by the client in CREATE_SESSION
5153 * If nfs4_init_session set the fore channel request and response sizes,
5154 * use them.
5155 *
5156 * Set the back channel max_resp_sz_cached to zero to force the client to
5157 * always set csa_cachethis to FALSE because the current implementation
5158 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5159 */
5160static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5161{
5162 struct nfs4_session *session = args->client->cl_session;
5163 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5164 mxresp_sz = session->fc_attrs.max_resp_sz;
5165
5166 if (mxrqst_sz == 0)
5167 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5168 if (mxresp_sz == 0)
5169 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5170 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005171 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5172 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005173 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5174 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5175
5176 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005177 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005178 __func__,
5179 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005180 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005181
5182 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005183 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5184 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5185 args->bc_attrs.max_resp_sz_cached = 0;
5186 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5187 args->bc_attrs.max_reqs = 1;
5188
5189 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5190 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5191 __func__,
5192 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5193 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5194 args->bc_attrs.max_reqs);
5195}
5196
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005197static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005198{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005199 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5200 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5201
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005202 if (rcvd->max_resp_sz > sent->max_resp_sz)
5203 return -EINVAL;
5204 /*
5205 * Our requested max_ops is the minimum we need; we're not
5206 * prepared to break up compounds into smaller pieces than that.
5207 * So, no point even trying to continue if the server won't
5208 * cooperate:
5209 */
5210 if (rcvd->max_ops < sent->max_ops)
5211 return -EINVAL;
5212 if (rcvd->max_reqs == 0)
5213 return -EINVAL;
5214 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005215}
5216
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005217static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5218{
5219 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5220 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005221
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005222 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5223 return -EINVAL;
5224 if (rcvd->max_resp_sz < sent->max_resp_sz)
5225 return -EINVAL;
5226 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5227 return -EINVAL;
5228 /* These would render the backchannel useless: */
5229 if (rcvd->max_ops == 0)
5230 return -EINVAL;
5231 if (rcvd->max_reqs == 0)
5232 return -EINVAL;
5233 return 0;
5234}
Andy Adamson8d353012009-04-01 09:22:32 -04005235
Andy Adamson8d353012009-04-01 09:22:32 -04005236static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5237 struct nfs4_session *session)
5238{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005239 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005240
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005241 ret = nfs4_verify_fore_channel_attrs(args, session);
5242 if (ret)
5243 return ret;
5244 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005245}
5246
Andy Adamsonfc931582009-04-01 09:22:31 -04005247static int _nfs4_proc_create_session(struct nfs_client *clp)
5248{
5249 struct nfs4_session *session = clp->cl_session;
5250 struct nfs41_create_session_args args = {
5251 .client = clp,
5252 .cb_program = NFS4_CALLBACK,
5253 };
5254 struct nfs41_create_session_res res = {
5255 .client = clp,
5256 };
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5259 .rpc_argp = &args,
5260 .rpc_resp = &res,
5261 };
5262 int status;
5263
5264 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005265 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005266
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005267 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005268
Andy Adamson8d353012009-04-01 09:22:32 -04005269 if (!status)
5270 /* Verify the session's negotiated channel_attrs values */
5271 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005272 if (!status) {
5273 /* Increment the clientid slot sequence id */
5274 clp->cl_seqid++;
5275 }
5276
5277 return status;
5278}
5279
5280/*
5281 * Issues a CREATE_SESSION operation to the server.
5282 * It is the responsibility of the caller to verify the session is
5283 * expired before calling this routine.
5284 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005285int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005286{
5287 int status;
5288 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005289 struct nfs4_session *session = clp->cl_session;
5290
5291 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5292
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005293 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005294 if (status)
5295 goto out;
5296
Trond Myklebustf26468f2009-12-05 19:32:11 -05005297 /* Init and reset the fore channel */
5298 status = nfs4_init_slot_tables(session);
5299 dprintk("slot table initialization returned %d\n", status);
5300 if (status)
5301 goto out;
5302 status = nfs4_reset_slot_tables(session);
5303 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005304 if (status)
5305 goto out;
5306
5307 ptr = (unsigned *)&session->sess_id.data[0];
5308 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5309 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005310out:
5311 dprintk("<-- %s\n", __func__);
5312 return status;
5313}
5314
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005315/*
5316 * Issue the over-the-wire RPC DESTROY_SESSION.
5317 * The caller must serialize access to this routine.
5318 */
5319int nfs4_proc_destroy_session(struct nfs4_session *session)
5320{
5321 int status = 0;
5322 struct rpc_message msg;
5323
5324 dprintk("--> nfs4_proc_destroy_session\n");
5325
5326 /* session is still being setup */
5327 if (session->clp->cl_cons_state != NFS_CS_READY)
5328 return status;
5329
5330 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5331 msg.rpc_argp = session;
5332 msg.rpc_resp = NULL;
5333 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005334 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005335
5336 if (status)
5337 printk(KERN_WARNING
5338 "Got error %d from the server on DESTROY_SESSION. "
5339 "Session has been destroyed regardless...\n", status);
5340
5341 dprintk("<-- nfs4_proc_destroy_session\n");
5342 return status;
5343}
5344
Trond Myklebustfccba802009-07-21 16:48:07 -04005345int nfs4_init_session(struct nfs_server *server)
5346{
5347 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005348 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005349 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005350 int ret;
5351
5352 if (!nfs4_has_session(clp))
5353 return 0;
5354
Trond Myklebust1055d762010-06-16 09:52:27 -04005355 session = clp->cl_session;
5356 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5357 return 0;
5358
Andy Adamson68bf05e2009-12-15 12:55:02 -05005359 rsize = server->rsize;
5360 if (rsize == 0)
5361 rsize = NFS_MAX_FILE_IO_SIZE;
5362 wsize = server->wsize;
5363 if (wsize == 0)
5364 wsize = NFS_MAX_FILE_IO_SIZE;
5365
Andy Adamson68bf05e2009-12-15 12:55:02 -05005366 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5367 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005368
Trond Myklebustfccba802009-07-21 16:48:07 -04005369 ret = nfs4_recover_expired_lease(server);
5370 if (!ret)
5371 ret = nfs4_check_client_ready(clp);
5372 return ret;
5373}
5374
Andy Adamsond83217c2011-03-01 01:34:17 +00005375int nfs4_init_ds_session(struct nfs_client *clp)
5376{
5377 struct nfs4_session *session = clp->cl_session;
5378 int ret;
5379
5380 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5381 return 0;
5382
5383 ret = nfs4_client_recover_expired_lease(clp);
5384 if (!ret)
5385 /* Test for the DS role */
5386 if (!is_ds_client(clp))
5387 ret = -ENODEV;
5388 if (!ret)
5389 ret = nfs4_check_client_ready(clp);
5390 return ret;
5391
5392}
5393EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5394
5395
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005396/*
5397 * Renew the cl_session lease.
5398 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005399struct nfs4_sequence_data {
5400 struct nfs_client *clp;
5401 struct nfs4_sequence_args args;
5402 struct nfs4_sequence_res res;
5403};
5404
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005405static void nfs41_sequence_release(void *data)
5406{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005407 struct nfs4_sequence_data *calldata = data;
5408 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005409
Alexandros Batsakis71358402010-02-05 03:45:05 -08005410 if (atomic_read(&clp->cl_count) > 1)
5411 nfs4_schedule_state_renewal(clp);
5412 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005413 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005414}
5415
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005416static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5417{
5418 switch(task->tk_status) {
5419 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005420 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5421 return -EAGAIN;
5422 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005423 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005424 }
5425 return 0;
5426}
5427
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005428static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005429{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005430 struct nfs4_sequence_data *calldata = data;
5431 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005432
Trond Myklebust14516c32010-07-31 14:29:06 -04005433 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5434 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005435
5436 if (task->tk_status < 0) {
5437 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005438 if (atomic_read(&clp->cl_count) == 1)
5439 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005440
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005441 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5442 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005443 return;
5444 }
5445 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005446 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005447out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005448 dprintk("<-- %s\n", __func__);
5449}
5450
5451static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5452{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005453 struct nfs4_sequence_data *calldata = data;
5454 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005455 struct nfs4_sequence_args *args;
5456 struct nfs4_sequence_res *res;
5457
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005458 args = task->tk_msg.rpc_argp;
5459 res = task->tk_msg.rpc_resp;
5460
Trond Myklebust035168ab2010-06-16 09:52:26 -04005461 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005462 return;
5463 rpc_call_start(task);
5464}
5465
5466static const struct rpc_call_ops nfs41_sequence_ops = {
5467 .rpc_call_done = nfs41_sequence_call_done,
5468 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005469 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005470};
5471
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005472static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005473{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005474 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005475 struct rpc_message msg = {
5476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5477 .rpc_cred = cred,
5478 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005479 struct rpc_task_setup task_setup_data = {
5480 .rpc_client = clp->cl_rpcclient,
5481 .rpc_message = &msg,
5482 .callback_ops = &nfs41_sequence_ops,
5483 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5484 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005485
Alexandros Batsakis71358402010-02-05 03:45:05 -08005486 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005487 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005488 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005489 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005490 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005491 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005492 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005493 msg.rpc_argp = &calldata->args;
5494 msg.rpc_resp = &calldata->res;
5495 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005496 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005497
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005498 return rpc_run_task(&task_setup_data);
5499}
5500
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005501static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005502{
5503 struct rpc_task *task;
5504 int ret = 0;
5505
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005506 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5507 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005508 task = _nfs41_proc_sequence(clp, cred);
5509 if (IS_ERR(task))
5510 ret = PTR_ERR(task);
5511 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005512 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005513 dprintk("<-- %s status=%d\n", __func__, ret);
5514 return ret;
5515}
5516
5517static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5518{
5519 struct rpc_task *task;
5520 int ret;
5521
5522 task = _nfs41_proc_sequence(clp, cred);
5523 if (IS_ERR(task)) {
5524 ret = PTR_ERR(task);
5525 goto out;
5526 }
5527 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005528 if (!ret) {
5529 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5530
5531 if (task->tk_status == 0)
5532 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005533 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005534 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005535 rpc_put_task(task);
5536out:
5537 dprintk("<-- %s status=%d\n", __func__, ret);
5538 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005539}
5540
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005541struct nfs4_reclaim_complete_data {
5542 struct nfs_client *clp;
5543 struct nfs41_reclaim_complete_args arg;
5544 struct nfs41_reclaim_complete_res res;
5545};
5546
5547static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5548{
5549 struct nfs4_reclaim_complete_data *calldata = data;
5550
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005551 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005552 if (nfs41_setup_sequence(calldata->clp->cl_session,
5553 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005554 &calldata->res.seq_res, 0, task))
5555 return;
5556
5557 rpc_call_start(task);
5558}
5559
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005560static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5561{
5562 switch(task->tk_status) {
5563 case 0:
5564 case -NFS4ERR_COMPLETE_ALREADY:
5565 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5566 break;
5567 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005568 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005569 /* fall through */
5570 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005571 return -EAGAIN;
5572 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005573 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005574 }
5575 return 0;
5576}
5577
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005578static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5579{
5580 struct nfs4_reclaim_complete_data *calldata = data;
5581 struct nfs_client *clp = calldata->clp;
5582 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5583
5584 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005585 if (!nfs41_sequence_done(task, res))
5586 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005587
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005588 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5589 rpc_restart_call_prepare(task);
5590 return;
5591 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005592 dprintk("<-- %s\n", __func__);
5593}
5594
5595static void nfs4_free_reclaim_complete_data(void *data)
5596{
5597 struct nfs4_reclaim_complete_data *calldata = data;
5598
5599 kfree(calldata);
5600}
5601
5602static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5603 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5604 .rpc_call_done = nfs4_reclaim_complete_done,
5605 .rpc_release = nfs4_free_reclaim_complete_data,
5606};
5607
5608/*
5609 * Issue a global reclaim complete.
5610 */
5611static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5612{
5613 struct nfs4_reclaim_complete_data *calldata;
5614 struct rpc_task *task;
5615 struct rpc_message msg = {
5616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5617 };
5618 struct rpc_task_setup task_setup_data = {
5619 .rpc_client = clp->cl_rpcclient,
5620 .rpc_message = &msg,
5621 .callback_ops = &nfs4_reclaim_complete_call_ops,
5622 .flags = RPC_TASK_ASYNC,
5623 };
5624 int status = -ENOMEM;
5625
5626 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005627 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005628 if (calldata == NULL)
5629 goto out;
5630 calldata->clp = clp;
5631 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005632
5633 msg.rpc_argp = &calldata->arg;
5634 msg.rpc_resp = &calldata->res;
5635 task_setup_data.callback_data = calldata;
5636 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005637 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005638 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005639 goto out;
5640 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005641 status = nfs4_wait_for_completion_rpc_task(task);
5642 if (status == 0)
5643 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005644 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005645 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005646out:
5647 dprintk("<-- %s status=%d\n", __func__, status);
5648 return status;
5649}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005650
5651static void
5652nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5653{
5654 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005655 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005656
5657 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005658 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5659 * right now covering the LAYOUTGET we are about to send.
5660 * However, that is not so catastrophic, and there seems
5661 * to be no way to prevent it completely.
5662 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005663 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5664 &lgp->res.seq_res, 0, task))
5665 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005666 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5667 NFS_I(lgp->args.inode)->layout,
5668 lgp->args.ctx->state)) {
5669 rpc_exit(task, NFS4_OK);
5670 return;
5671 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005672 rpc_call_start(task);
5673}
5674
5675static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5676{
5677 struct nfs4_layoutget *lgp = calldata;
5678 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5679
5680 dprintk("--> %s\n", __func__);
5681
5682 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5683 return;
5684
5685 switch (task->tk_status) {
5686 case 0:
5687 break;
5688 case -NFS4ERR_LAYOUTTRYLATER:
5689 case -NFS4ERR_RECALLCONFLICT:
5690 task->tk_status = -NFS4ERR_DELAY;
5691 /* Fall through */
5692 default:
5693 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5694 rpc_restart_call_prepare(task);
5695 return;
5696 }
5697 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005698 dprintk("<-- %s\n", __func__);
5699}
5700
5701static void nfs4_layoutget_release(void *calldata)
5702{
5703 struct nfs4_layoutget *lgp = calldata;
5704
5705 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005706 put_nfs_open_context(lgp->args.ctx);
5707 kfree(calldata);
5708 dprintk("<-- %s\n", __func__);
5709}
5710
5711static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5712 .rpc_call_prepare = nfs4_layoutget_prepare,
5713 .rpc_call_done = nfs4_layoutget_done,
5714 .rpc_release = nfs4_layoutget_release,
5715};
5716
5717int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5718{
5719 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5720 struct rpc_task *task;
5721 struct rpc_message msg = {
5722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5723 .rpc_argp = &lgp->args,
5724 .rpc_resp = &lgp->res,
5725 };
5726 struct rpc_task_setup task_setup_data = {
5727 .rpc_client = server->client,
5728 .rpc_message = &msg,
5729 .callback_ops = &nfs4_layoutget_call_ops,
5730 .callback_data = lgp,
5731 .flags = RPC_TASK_ASYNC,
5732 };
5733 int status = 0;
5734
5735 dprintk("--> %s\n", __func__);
5736
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005737 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005738 lgp->res.seq_res.sr_slot = NULL;
5739 task = rpc_run_task(&task_setup_data);
5740 if (IS_ERR(task))
5741 return PTR_ERR(task);
5742 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005743 if (status == 0)
5744 status = task->tk_status;
5745 if (status == 0)
5746 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005747 rpc_put_task(task);
5748 dprintk("<-- %s status=%d\n", __func__, status);
5749 return status;
5750}
5751
Benny Halevycbe82602011-05-22 19:52:37 +03005752static void
5753nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5754{
5755 struct nfs4_layoutreturn *lrp = calldata;
5756
5757 dprintk("--> %s\n", __func__);
5758 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5759 &lrp->res.seq_res, 0, task))
5760 return;
5761 rpc_call_start(task);
5762}
5763
5764static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5765{
5766 struct nfs4_layoutreturn *lrp = calldata;
5767 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005768 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005769
5770 dprintk("--> %s\n", __func__);
5771
5772 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5773 return;
5774
5775 server = NFS_SERVER(lrp->args.inode);
5776 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005777 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005778 return;
5779 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005780 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005781 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005782 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005783 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005784 } else
5785 BUG_ON(!list_empty(&lo->plh_segs));
5786 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005787 lo->plh_block_lgets--;
5788 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005789 dprintk("<-- %s\n", __func__);
5790}
5791
5792static void nfs4_layoutreturn_release(void *calldata)
5793{
5794 struct nfs4_layoutreturn *lrp = calldata;
5795
5796 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005797 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005798 kfree(calldata);
5799 dprintk("<-- %s\n", __func__);
5800}
5801
5802static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5803 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5804 .rpc_call_done = nfs4_layoutreturn_done,
5805 .rpc_release = nfs4_layoutreturn_release,
5806};
5807
5808int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5809{
5810 struct rpc_task *task;
5811 struct rpc_message msg = {
5812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5813 .rpc_argp = &lrp->args,
5814 .rpc_resp = &lrp->res,
5815 };
5816 struct rpc_task_setup task_setup_data = {
5817 .rpc_client = lrp->clp->cl_rpcclient,
5818 .rpc_message = &msg,
5819 .callback_ops = &nfs4_layoutreturn_call_ops,
5820 .callback_data = lrp,
5821 };
5822 int status;
5823
5824 dprintk("--> %s\n", __func__);
5825 task = rpc_run_task(&task_setup_data);
5826 if (IS_ERR(task))
5827 return PTR_ERR(task);
5828 status = task->tk_status;
5829 dprintk("<-- %s status=%d\n", __func__, status);
5830 rpc_put_task(task);
5831 return status;
5832}
5833
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005834/*
5835 * Retrieve the list of Data Server devices from the MDS.
5836 */
5837static int _nfs4_getdevicelist(struct nfs_server *server,
5838 const struct nfs_fh *fh,
5839 struct pnfs_devicelist *devlist)
5840{
5841 struct nfs4_getdevicelist_args args = {
5842 .fh = fh,
5843 .layoutclass = server->pnfs_curr_ld->id,
5844 };
5845 struct nfs4_getdevicelist_res res = {
5846 .devlist = devlist,
5847 };
5848 struct rpc_message msg = {
5849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5850 .rpc_argp = &args,
5851 .rpc_resp = &res,
5852 };
5853 int status;
5854
5855 dprintk("--> %s\n", __func__);
5856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5857 &res.seq_res, 0);
5858 dprintk("<-- %s status=%d\n", __func__, status);
5859 return status;
5860}
5861
5862int nfs4_proc_getdevicelist(struct nfs_server *server,
5863 const struct nfs_fh *fh,
5864 struct pnfs_devicelist *devlist)
5865{
5866 struct nfs4_exception exception = { };
5867 int err;
5868
5869 do {
5870 err = nfs4_handle_exception(server,
5871 _nfs4_getdevicelist(server, fh, devlist),
5872 &exception);
5873 } while (exception.retry);
5874
5875 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5876 err, devlist->num_devs);
5877
5878 return err;
5879}
5880EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5881
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005882static int
5883_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5884{
5885 struct nfs4_getdeviceinfo_args args = {
5886 .pdev = pdev,
5887 };
5888 struct nfs4_getdeviceinfo_res res = {
5889 .pdev = pdev,
5890 };
5891 struct rpc_message msg = {
5892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5893 .rpc_argp = &args,
5894 .rpc_resp = &res,
5895 };
5896 int status;
5897
5898 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005899 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005900 dprintk("<-- %s status=%d\n", __func__, status);
5901
5902 return status;
5903}
5904
5905int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5906{
5907 struct nfs4_exception exception = { };
5908 int err;
5909
5910 do {
5911 err = nfs4_handle_exception(server,
5912 _nfs4_proc_getdeviceinfo(server, pdev),
5913 &exception);
5914 } while (exception.retry);
5915 return err;
5916}
5917EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5918
Andy Adamson863a3c62011-03-23 13:27:54 +00005919static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5920{
5921 struct nfs4_layoutcommit_data *data = calldata;
5922 struct nfs_server *server = NFS_SERVER(data->args.inode);
5923
5924 if (nfs4_setup_sequence(server, &data->args.seq_args,
5925 &data->res.seq_res, 1, task))
5926 return;
5927 rpc_call_start(task);
5928}
5929
5930static void
5931nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5932{
5933 struct nfs4_layoutcommit_data *data = calldata;
5934 struct nfs_server *server = NFS_SERVER(data->args.inode);
5935
5936 if (!nfs4_sequence_done(task, &data->res.seq_res))
5937 return;
5938
5939 switch (task->tk_status) { /* Just ignore these failures */
5940 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5941 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5942 case NFS4ERR_BADLAYOUT: /* no layout */
5943 case NFS4ERR_GRACE: /* loca_recalim always false */
5944 task->tk_status = 0;
5945 }
5946
5947 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005948 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005949 return;
5950 }
5951
5952 if (task->tk_status == 0)
5953 nfs_post_op_update_inode_force_wcc(data->args.inode,
5954 data->res.fattr);
5955}
5956
5957static void nfs4_layoutcommit_release(void *calldata)
5958{
5959 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005960 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005961 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005962
Andy Adamsondb29c082011-07-30 20:52:38 -04005963 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005964 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005965 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5966 list_del_init(&lseg->pls_lc_list);
5967 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5968 &lseg->pls_flags))
5969 put_lseg(lseg);
5970 }
Peng Tao92407e72011-10-23 20:21:17 -07005971
5972 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5973 smp_mb__after_clear_bit();
5974 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5975
Andy Adamson863a3c62011-03-23 13:27:54 +00005976 put_rpccred(data->cred);
5977 kfree(data);
5978}
5979
5980static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5981 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5982 .rpc_call_done = nfs4_layoutcommit_done,
5983 .rpc_release = nfs4_layoutcommit_release,
5984};
5985
5986int
Andy Adamsonef311532011-03-12 02:58:10 -05005987nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005988{
5989 struct rpc_message msg = {
5990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5991 .rpc_argp = &data->args,
5992 .rpc_resp = &data->res,
5993 .rpc_cred = data->cred,
5994 };
5995 struct rpc_task_setup task_setup_data = {
5996 .task = &data->task,
5997 .rpc_client = NFS_CLIENT(data->args.inode),
5998 .rpc_message = &msg,
5999 .callback_ops = &nfs4_layoutcommit_ops,
6000 .callback_data = data,
6001 .flags = RPC_TASK_ASYNC,
6002 };
6003 struct rpc_task *task;
6004 int status = 0;
6005
6006 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6007 "lbw: %llu inode %lu\n",
6008 data->task.tk_pid, sync,
6009 data->args.lastbytewritten,
6010 data->args.inode->i_ino);
6011
6012 task = rpc_run_task(&task_setup_data);
6013 if (IS_ERR(task))
6014 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006015 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006016 goto out;
6017 status = nfs4_wait_for_completion_rpc_task(task);
6018 if (status != 0)
6019 goto out;
6020 status = task->tk_status;
6021out:
6022 dprintk("%s: status %d\n", __func__, status);
6023 rpc_put_task(task);
6024 return status;
6025}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006026
6027static int
6028_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6029 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6030{
6031 struct nfs41_secinfo_no_name_args args = {
6032 .style = SECINFO_STYLE_CURRENT_FH,
6033 };
6034 struct nfs4_secinfo_res res = {
6035 .flavors = flavors,
6036 };
6037 struct rpc_message msg = {
6038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6039 .rpc_argp = &args,
6040 .rpc_resp = &res,
6041 };
6042 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6043}
6044
6045static int
6046nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6047 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6048{
6049 struct nfs4_exception exception = { };
6050 int err;
6051 do {
6052 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6053 switch (err) {
6054 case 0:
6055 case -NFS4ERR_WRONGSEC:
6056 case -NFS4ERR_NOTSUPP:
6057 break;
6058 default:
6059 err = nfs4_handle_exception(server, err, &exception);
6060 }
6061 } while (exception.retry);
6062 return err;
6063}
6064
6065static int
6066nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6067 struct nfs_fsinfo *info)
6068{
6069 int err;
6070 struct page *page;
6071 rpc_authflavor_t flavor;
6072 struct nfs4_secinfo_flavors *flavors;
6073
6074 page = alloc_page(GFP_KERNEL);
6075 if (!page) {
6076 err = -ENOMEM;
6077 goto out;
6078 }
6079
6080 flavors = page_address(page);
6081 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6082
6083 /*
6084 * Fall back on "guess and check" method if
6085 * the server doesn't support SECINFO_NO_NAME
6086 */
6087 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6088 err = nfs4_find_root_sec(server, fhandle, info);
6089 goto out_freepage;
6090 }
6091 if (err)
6092 goto out_freepage;
6093
6094 flavor = nfs_find_best_sec(flavors);
6095 if (err == 0)
6096 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6097
6098out_freepage:
6099 put_page(page);
6100 if (err == -EACCES)
6101 return -EPERM;
6102out:
6103 return err;
6104}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006105static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6106{
6107 int status;
6108 struct nfs41_test_stateid_args args = {
6109 .stateid = &state->stateid,
6110 };
6111 struct nfs41_test_stateid_res res;
6112 struct rpc_message msg = {
6113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6114 .rpc_argp = &args,
6115 .rpc_resp = &res,
6116 };
6117 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6118 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6119 return status;
6120}
6121
6122static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6123{
6124 struct nfs4_exception exception = { };
6125 int err;
6126 do {
6127 err = nfs4_handle_exception(server,
6128 _nfs41_test_stateid(server, state),
6129 &exception);
6130 } while (exception.retry);
6131 return err;
6132}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006133
6134static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6135{
6136 int status;
6137 struct nfs41_free_stateid_args args = {
6138 .stateid = &state->stateid,
6139 };
6140 struct nfs41_free_stateid_res res;
6141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6143 .rpc_argp = &args,
6144 .rpc_resp = &res,
6145 };
6146
6147 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6148 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6149 return status;
6150}
6151
6152static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6153{
6154 struct nfs4_exception exception = { };
6155 int err;
6156 do {
6157 err = nfs4_handle_exception(server,
6158 _nfs4_free_stateid(server, state),
6159 &exception);
6160 } while (exception.retry);
6161 return err;
6162}
Andy Adamson557134a2009-04-01 09:21:53 -04006163#endif /* CONFIG_NFS_V4_1 */
6164
Andy Adamson591d71c2009-04-01 09:22:47 -04006165struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006166 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006167 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 .recover_open = nfs4_open_reclaim,
6169 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006170 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006171 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172};
6173
Andy Adamson591d71c2009-04-01 09:22:47 -04006174#if defined(CONFIG_NFS_V4_1)
6175struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6176 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6177 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6178 .recover_open = nfs4_open_reclaim,
6179 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006180 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006181 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006182 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006183};
6184#endif /* CONFIG_NFS_V4_1 */
6185
6186struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006187 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006188 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 .recover_open = nfs4_open_expired,
6190 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006191 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006192 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193};
6194
Andy Adamson591d71c2009-04-01 09:22:47 -04006195#if defined(CONFIG_NFS_V4_1)
6196struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6197 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6198 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006199 .recover_open = nfs41_open_expired,
6200 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006201 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006202 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006203};
6204#endif /* CONFIG_NFS_V4_1 */
6205
Benny Halevy29fba382009-04-01 09:22:44 -04006206struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6207 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006208 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006209 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006210};
6211
6212#if defined(CONFIG_NFS_V4_1)
6213struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6214 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006215 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006216 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006217};
6218#endif
6219
Trond Myklebust97dc1352010-06-16 09:52:26 -04006220static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6221 .minor_version = 0,
6222 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006223 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006224 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006225 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6226 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6227 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006228};
6229
6230#if defined(CONFIG_NFS_V4_1)
6231static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6232 .minor_version = 1,
6233 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006234 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006235 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006236 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6237 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6238 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006239};
6240#endif
6241
Trond Myklebust97dc1352010-06-16 09:52:26 -04006242const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6243 [0] = &nfs_v4_0_minor_ops,
6244#if defined(CONFIG_NFS_V4_1)
6245 [1] = &nfs_v4_1_minor_ops,
6246#endif
6247};
6248
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006249static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006250 .permission = nfs_permission,
6251 .getattr = nfs_getattr,
6252 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006253 .getxattr = generic_getxattr,
6254 .setxattr = generic_setxattr,
6255 .listxattr = generic_listxattr,
6256 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006257};
6258
David Howells509de812006-08-22 20:06:11 -04006259const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 .version = 4, /* protocol version */
6261 .dentry_ops = &nfs4_dentry_operations,
6262 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006263 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006264 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 .getroot = nfs4_proc_get_root,
6266 .getattr = nfs4_proc_getattr,
6267 .setattr = nfs4_proc_setattr,
6268 .lookup = nfs4_proc_lookup,
6269 .access = nfs4_proc_access,
6270 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 .create = nfs4_proc_create,
6272 .remove = nfs4_proc_remove,
6273 .unlink_setup = nfs4_proc_unlink_setup,
6274 .unlink_done = nfs4_proc_unlink_done,
6275 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006276 .rename_setup = nfs4_proc_rename_setup,
6277 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 .link = nfs4_proc_link,
6279 .symlink = nfs4_proc_symlink,
6280 .mkdir = nfs4_proc_mkdir,
6281 .rmdir = nfs4_proc_remove,
6282 .readdir = nfs4_proc_readdir,
6283 .mknod = nfs4_proc_mknod,
6284 .statfs = nfs4_proc_statfs,
6285 .fsinfo = nfs4_proc_fsinfo,
6286 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006287 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 .decode_dirent = nfs4_decode_dirent,
6289 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006290 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006292 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006294 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006296 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006297 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006298 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006299 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006300 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301};
6302
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006303static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6304 .prefix = XATTR_NAME_NFSV4_ACL,
6305 .list = nfs4_xattr_list_nfs4_acl,
6306 .get = nfs4_xattr_get_nfs4_acl,
6307 .set = nfs4_xattr_set_nfs4_acl,
6308};
6309
6310const struct xattr_handler *nfs4_xattr_handlers[] = {
6311 &nfs4_xattr_nfs4_acl_handler,
6312 NULL
6313};
6314
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315/*
6316 * Local variables:
6317 * c-basic-offset: 8
6318 * End:
6319 */