blob: 5b4124e4c22f556289bc43b78aa03b5a8e8b360c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000044#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/nfs.h>
46#include <linux/nfs4.h>
47#include <linux/nfs_fs.h>
48#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040049#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070051#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040052#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040053#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Trond Myklebust4ce79712005-06-22 17:16:21 +000057#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050059#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050060#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040061#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040062#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define NFSDBG_FACILITY NFSDBG_PROC
65
Trond Myklebust2066fe82006-09-15 08:30:46 -040066#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define NFS4_POLL_RETRY_MAX (15*HZ)
68
Trond Myklebusta78cb572009-08-09 15:06:19 -040069#define NFS4_MAX_LOOP_ON_RECOVER (10)
70
Trond Myklebustcdd4e682006-01-03 09:55:12 +010071struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010072static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080073static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050075static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Bryan Schumaker7c513052011-03-24 17:12:24 +000076static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
77 const struct qstr *name, struct nfs_fh *fhandle,
78 struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040079static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81 struct nfs_fattr *fattr, struct iattr *sattr,
82 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050085static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Trond Myklebust52567b02009-10-23 14:46:42 -040087 if (err >= -1000)
88 return err;
89 switch (err) {
90 case -NFS4ERR_RESOURCE:
91 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000092 case -NFS4ERR_WRONGSEC:
93 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080094 case -NFS4ERR_BADOWNER:
95 case -NFS4ERR_BADNAME:
96 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040097 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070099 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400102 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * This is our standard bitmap for GETATTR requests.
107 */
108const u32 nfs4_fattr_bitmap[2] = {
109 FATTR4_WORD0_TYPE
110 | FATTR4_WORD0_CHANGE
111 | FATTR4_WORD0_SIZE
112 | FATTR4_WORD0_FSID
113 | FATTR4_WORD0_FILEID,
114 FATTR4_WORD1_MODE
115 | FATTR4_WORD1_NUMLINKS
116 | FATTR4_WORD1_OWNER
117 | FATTR4_WORD1_OWNER_GROUP
118 | FATTR4_WORD1_RAWDEV
119 | FATTR4_WORD1_SPACE_USED
120 | FATTR4_WORD1_TIME_ACCESS
121 | FATTR4_WORD1_TIME_METADATA
122 | FATTR4_WORD1_TIME_MODIFY
123};
124
125const u32 nfs4_statfs_bitmap[2] = {
126 FATTR4_WORD0_FILES_AVAIL
127 | FATTR4_WORD0_FILES_FREE
128 | FATTR4_WORD0_FILES_TOTAL,
129 FATTR4_WORD1_SPACE_AVAIL
130 | FATTR4_WORD1_SPACE_FREE
131 | FATTR4_WORD1_SPACE_TOTAL
132};
133
Trond Myklebust4ce79712005-06-22 17:16:21 +0000134const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 FATTR4_WORD0_MAXLINK
136 | FATTR4_WORD0_MAXNAME,
137 0
138};
139
140const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
141 | FATTR4_WORD0_MAXREAD
142 | FATTR4_WORD0_MAXWRITE
143 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700144 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400145 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Manoj Naik830b8e32006-06-09 09:34:25 -0400148const u32 nfs4_fs_locations_bitmap[2] = {
149 FATTR4_WORD0_TYPE
150 | FATTR4_WORD0_CHANGE
151 | FATTR4_WORD0_SIZE
152 | FATTR4_WORD0_FSID
153 | FATTR4_WORD0_FILEID
154 | FATTR4_WORD0_FS_LOCATIONS,
155 FATTR4_WORD1_MODE
156 | FATTR4_WORD1_NUMLINKS
157 | FATTR4_WORD1_OWNER
158 | FATTR4_WORD1_OWNER_GROUP
159 | FATTR4_WORD1_RAWDEV
160 | FATTR4_WORD1_SPACE_USED
161 | FATTR4_WORD1_TIME_ACCESS
162 | FATTR4_WORD1_TIME_METADATA
163 | FATTR4_WORD1_TIME_MODIFY
164 | FATTR4_WORD1_MOUNTED_ON_FILEID
165};
166
Al Virobc4785c2006-10-19 23:28:51 -0700167static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct nfs4_readdir_arg *readdir)
169{
Al Viro0dbb4c62006-10-19 23:28:49 -0700170 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 BUG_ON(readdir->count < 80);
173 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000174 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
176 return;
177 }
178
179 readdir->cookie = 0;
180 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
181 if (cookie == 2)
182 return;
183
184 /*
185 * NFSv4 servers do not return entries for '.' and '..'
186 * Therefore, we fake these entries here. We let '.'
187 * have cookie 0 and '..' have cookie 1. Note that
188 * when talking to the server, we always send cookie 0
189 * instead of 1 or 2.
190 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700191 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (cookie == 0) {
194 *p++ = xdr_one; /* next */
195 *p++ = xdr_zero; /* cookie, first word */
196 *p++ = xdr_one; /* cookie, second word */
197 *p++ = xdr_one; /* entry len */
198 memcpy(p, ".\0\0\0", 4); /* entry */
199 p++;
200 *p++ = xdr_one; /* bitmap length */
201 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
202 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400203 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 *p++ = xdr_one; /* next */
207 *p++ = xdr_zero; /* cookie, first word */
208 *p++ = xdr_two; /* cookie, second word */
209 *p++ = xdr_two; /* entry len */
210 memcpy(p, "..\0\0", 4); /* entry */
211 p++;
212 *p++ = xdr_one; /* bitmap length */
213 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
214 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400215 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 readdir->pgbase = (char *)p - (char *)start;
218 readdir->count -= readdir->pgbase;
219 kunmap_atomic(start, KM_USER0);
220}
221
Trond Myklebust65de8722008-12-23 15:21:44 -0500222static int nfs4_wait_clnt_recover(struct nfs_client *clp)
223{
224 int res;
225
226 might_sleep();
227
Trond Myklebuste005e802008-12-23 15:21:48 -0500228 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400229 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500230 return res;
231}
232
233static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
234{
235 int res = 0;
236
237 might_sleep();
238
239 if (*timeout <= 0)
240 *timeout = NFS4_POLL_RETRY_MIN;
241 if (*timeout > NFS4_POLL_RETRY_MAX)
242 *timeout = NFS4_POLL_RETRY_MAX;
243 schedule_timeout_killable(*timeout);
244 if (fatal_signal_pending(current))
245 res = -ERESTARTSYS;
246 *timeout <<= 1;
247 return res;
248}
249
250/* This is the error handling routine for processes that are allowed
251 * to sleep.
252 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800253static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500254{
255 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500256 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 int ret = errorcode;
258
259 exception->retry = 0;
260 switch(errorcode) {
261 case 0:
262 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500263 case -NFS4ERR_ADMIN_REVOKED:
264 case -NFS4ERR_BAD_STATEID:
265 case -NFS4ERR_OPENMODE:
266 if (state == NULL)
267 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500268 nfs4_schedule_stateid_recovery(server, state);
269 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500270 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500271 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500273 nfs4_schedule_lease_recovery(clp);
274 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500275#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400276 case -NFS4ERR_BADSESSION:
277 case -NFS4ERR_BADSLOT:
278 case -NFS4ERR_BAD_HIGH_SLOT:
279 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
280 case -NFS4ERR_DEADSESSION:
281 case -NFS4ERR_SEQ_FALSE_RETRY:
282 case -NFS4ERR_SEQ_MISORDERED:
283 dprintk("%s ERROR: %d Reset session\n", __func__,
284 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500285 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400286 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500287 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500288#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500290 if (exception->timeout > HZ) {
291 /* We have retried a decent amount, time to
292 * fail
293 */
294 ret = -EBUSY;
295 break;
296 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500297 case -NFS4ERR_GRACE:
298 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500299 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500300 ret = nfs4_delay(server->client, &exception->timeout);
301 if (ret != 0)
302 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400303 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500304 case -NFS4ERR_OLD_STATEID:
305 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800306 break;
307 case -NFS4ERR_BADOWNER:
308 /* The following works around a Linux server bug! */
309 case -NFS4ERR_BADNAME:
310 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
311 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
312 exception->retry = 1;
313 printk(KERN_WARNING "NFS: v4 server %s "
314 "does not accept raw "
315 "uid/gids. "
316 "Reenabling the idmapper.\n",
317 server->nfs_client->cl_hostname);
318 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500319 }
320 /* We failed to handle the error */
321 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500322wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500323 ret = nfs4_wait_clnt_recover(clp);
324 if (ret == 0)
325 exception->retry = 1;
326 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500327}
328
329
Trond Myklebust452e9352010-07-31 14:29:06 -0400330static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_lock(&clp->cl_lock);
333 if (time_before(clp->cl_last_renewal,timestamp))
334 clp->cl_last_renewal = timestamp;
335 spin_unlock(&clp->cl_lock);
336}
337
Trond Myklebust452e9352010-07-31 14:29:06 -0400338static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
339{
340 do_renew_lease(server->nfs_client, timestamp);
341}
342
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400343#if defined(CONFIG_NFS_V4_1)
344
Benny Halevy510b8172009-04-01 09:22:14 -0400345/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400346 * nfs4_free_slot - free a slot and efficiently update slot table.
347 *
348 * freeing a slot is trivially done by clearing its respective bit
349 * in the bitmap.
350 * If the freed slotid equals highest_used_slotid we want to update it
351 * so that the server would be able to size down the slot table if needed,
352 * otherwise we know that the highest_used_slotid is still in use.
353 * When updating highest_used_slotid there may be "holes" in the bitmap
354 * so we need to scan down from highest_used_slotid to 0 looking for the now
355 * highest slotid in use.
356 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500357 *
358 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400359 */
360static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400361nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400362{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400363 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400364 int slotid = free_slotid;
365
Benny Halevydfb4f3092010-09-24 09:17:01 -0400366 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367 /* clear used bit in bitmap */
368 __clear_bit(slotid, tbl->used_slots);
369
370 /* update highest_used_slotid when it is freed */
371 if (slotid == tbl->highest_used_slotid) {
372 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500373 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400374 tbl->highest_used_slotid = slotid;
375 else
376 tbl->highest_used_slotid = -1;
377 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400378 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
379 free_slotid, tbl->highest_used_slotid);
380}
381
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800382/*
Andy Adamson42acd022011-01-06 02:04:34 +0000383 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800384 */
Andy Adamson42acd022011-01-06 02:04:34 +0000385static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800386{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800387 struct rpc_task *task;
388
Trond Myklebusta2118c32010-06-16 09:52:26 -0400389 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800390 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
391 if (task)
392 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800393 return;
394 }
395
396 if (ses->fc_slot_table.highest_used_slotid != -1)
397 return;
398
Andy Adamson42acd022011-01-06 02:04:34 +0000399 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
400 complete(&ses->fc_slot_table.complete);
401}
402
403/*
404 * Signal state manager thread if session back channel is drained
405 */
406void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
407{
408 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
409 ses->bc_slot_table.highest_used_slotid != -1)
410 return;
411 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
412 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800413}
414
Trond Myklebustd185a332010-06-16 09:52:25 -0400415static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400416{
417 struct nfs4_slot_table *tbl;
418
Trond Myklebustd185a332010-06-16 09:52:25 -0400419 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400421 /* just wake up the next guy waiting since
422 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500423 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500424 return;
Andy Adamson13615872009-04-01 09:22:17 -0400425 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500426
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400428 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000429 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400431 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400432}
433
Trond Myklebust14516c32010-07-31 14:29:06 -0400434static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435{
436 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400438
439 /*
440 * sr_status remains 1 if an RPC level error occurred. The server
441 * may or may not have processed the sequence operation..
442 * Proceed as if the server received and processed the sequence
443 * operation.
444 */
445 if (res->sr_status == 1)
446 res->sr_status = NFS_OK;
447
Bryan Schumaker468f8612011-04-18 15:57:32 -0400448 /* don't increment the sequence number if the task wasn't sent */
449 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400450 goto out;
451
Andy Adamson691daf32009-12-04 15:55:39 -0500452 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400453 switch (res->sr_status) {
454 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400456 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400459 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500460 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500461 if (res->sr_status_flags != 0)
462 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 break;
464 case -NFS4ERR_DELAY:
465 /* The server detected a resend of the RPC call and
466 * returned NFS4ERR_DELAY as per Section 2.10.6.2
467 * of RFC5661.
468 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200469 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400470 __func__,
471 res->sr_slot - res->sr_session->fc_slot_table.slots,
472 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400473 goto out_retry;
474 default:
475 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400477 }
478out:
479 /* The session may be reset by one of the error handlers. */
480 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400481 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 return 1;
483out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400484 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 goto out;
486 rpc_delay(task, NFS4_POLL_RETRY_MAX);
487 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400488}
489
Trond Myklebust14516c32010-07-31 14:29:06 -0400490static int nfs4_sequence_done(struct rpc_task *task,
491 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400492{
Trond Myklebust14516c32010-07-31 14:29:06 -0400493 if (res->sr_session == NULL)
494 return 1;
495 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400496}
497
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400498/*
Benny Halevy510b8172009-04-01 09:22:14 -0400499 * nfs4_find_slot - efficiently look for a free slot
500 *
501 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
502 * If found, we mark the slot as used, update the highest_used_slotid,
503 * and respectively set up the sequence operation args.
504 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400505 *
506 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400507 */
508static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800509nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400510{
511 int slotid;
512 u8 ret_id = NFS4_MAX_SLOT_TABLE;
513 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
514
Benny Halevy510b8172009-04-01 09:22:14 -0400515 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
516 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
517 tbl->max_slots);
518 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
519 if (slotid >= tbl->max_slots)
520 goto out;
521 __set_bit(slotid, tbl->used_slots);
522 if (slotid > tbl->highest_used_slotid)
523 tbl->highest_used_slotid = slotid;
524 ret_id = slotid;
525out:
526 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
527 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400528 return ret_id;
529}
530
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000531int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400532 struct nfs4_sequence_args *args,
533 struct nfs4_sequence_res *res,
534 int cache_reply,
535 struct rpc_task *task)
536{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400537 struct nfs4_slot *slot;
538 struct nfs4_slot_table *tbl;
539 u8 slotid;
540
541 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400542 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400543 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400544 return 0;
545
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 tbl = &session->fc_slot_table;
547
548 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400549 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800550 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500551 /*
552 * The state manager will wait until the slot table is empty.
553 * Schedule the reset thread
554 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500555 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400556 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500557 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500558 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400559 }
560
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800561 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
562 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
563 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
564 spin_unlock(&tbl->slot_tbl_lock);
565 dprintk("%s enforce FIFO order\n", __func__);
566 return -EAGAIN;
567 }
568
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800569 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 if (slotid == NFS4_MAX_SLOT_TABLE) {
571 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
572 spin_unlock(&tbl->slot_tbl_lock);
573 dprintk("<-- %s: no free slots\n", __func__);
574 return -EAGAIN;
575 }
576 spin_unlock(&tbl->slot_tbl_lock);
577
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800578 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400579 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400580 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581 args->sa_slotid = slotid;
582 args->sa_cache_this = cache_reply;
583
584 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
585
Benny Halevy99fe60d2009-04-01 09:22:29 -0400586 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400587 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400588 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400589 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590 /*
591 * sr_status is only set in decode_sequence, and so will remain
592 * set to 1 if an rpc level failure occurs.
593 */
594 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 return 0;
596}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000597EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598
Trond Myklebust035168a2010-06-16 09:52:26 -0400599int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
602 int cache_reply,
603 struct rpc_task *task)
604{
Trond Myklebust035168a2010-06-16 09:52:26 -0400605 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 int ret = 0;
607
Trond Myklebust035168a2010-06-16 09:52:26 -0400608 if (session == NULL) {
609 args->sa_session = NULL;
610 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 }
613
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200614 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400615 __func__, session->clp, session, res->sr_slot ?
616 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400617
618 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620out:
621 dprintk("<-- %s status=%d\n", __func__, ret);
622 return ret;
623}
624
625struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400626 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627 struct nfs4_sequence_args *seq_args;
628 struct nfs4_sequence_res *seq_res;
629 int cache_reply;
630};
631
632static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
633{
634 struct nfs41_call_sync_data *data = calldata;
635
Trond Myklebust035168a2010-06-16 09:52:26 -0400636 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
637
638 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639 data->seq_res, data->cache_reply, task))
640 return;
641 rpc_call_start(task);
642}
643
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800644static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
645{
646 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
647 nfs41_call_sync_prepare(task, calldata);
648}
649
Andy Adamson69ab40c2009-04-01 09:22:19 -0400650static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
651{
652 struct nfs41_call_sync_data *data = calldata;
653
Trond Myklebust14516c32010-07-31 14:29:06 -0400654 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655}
656
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657struct rpc_call_ops nfs41_call_sync_ops = {
658 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660};
661
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800662struct rpc_call_ops nfs41_call_priv_sync_ops = {
663 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
664 .rpc_call_done = nfs41_call_sync_done,
665};
666
Bryan Schumaker7c513052011-03-24 17:12:24 +0000667static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
668 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800672 int cache_reply,
673 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674{
675 int ret;
676 struct rpc_task *task;
677 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400678 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 .seq_args = args,
680 .seq_res = res,
681 .cache_reply = cache_reply,
682 };
683 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000684 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 .rpc_message = msg,
686 .callback_ops = &nfs41_call_sync_ops,
687 .callback_data = &data
688 };
689
Benny Halevydfb4f3092010-09-24 09:17:01 -0400690 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800691 if (privileged)
692 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 task = rpc_run_task(&task_setup);
694 if (IS_ERR(task))
695 ret = PTR_ERR(task);
696 else {
697 ret = task->tk_status;
698 rpc_put_task(task);
699 }
700 return ret;
701}
702
Bryan Schumaker7c513052011-03-24 17:12:24 +0000703int _nfs4_call_sync_session(struct rpc_clnt *clnt,
704 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400705 struct rpc_message *msg,
706 struct nfs4_sequence_args *args,
707 struct nfs4_sequence_res *res,
708 int cache_reply)
709{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000710 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711}
712
Trond Myklebustdf896452010-06-16 09:52:26 -0400713#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400714static int nfs4_sequence_done(struct rpc_task *task,
715 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400716{
Trond Myklebust14516c32010-07-31 14:29:06 -0400717 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400718}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719#endif /* CONFIG_NFS_V4_1 */
720
Bryan Schumaker7c513052011-03-24 17:12:24 +0000721int _nfs4_call_sync(struct rpc_clnt *clnt,
722 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
725 struct nfs4_sequence_res *res,
726 int cache_reply)
727{
Benny Halevyf3752972009-04-01 09:22:04 -0400728 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000729 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400730}
731
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000732static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000733int nfs4_call_sync(struct rpc_clnt *clnt,
734 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000735 struct rpc_message *msg,
736 struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res,
738 int cache_reply)
739{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000740 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
741 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000742}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400743
Trond Myklebust38478b22006-05-25 01:40:57 -0400744static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Trond Myklebust38478b22006-05-25 01:40:57 -0400746 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400749 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
750 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400751 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400752 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct nfs_openargs o_arg;
759 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100760 struct nfs_open_confirmargs c_arg;
761 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct nfs_fattr f_attr;
763 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400764 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
766 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400767 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100769 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400770 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100771 int rpc_status;
772 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773};
774
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400775
776static void nfs4_init_opendata_res(struct nfs4_opendata *p)
777{
778 p->o_res.f_attr = &p->f_attr;
779 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400780 p->o_res.seqid = p->o_arg.seqid;
781 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400782 p->o_res.server = p->o_arg.server;
783 nfs_fattr_init(&p->f_attr);
784 nfs_fattr_init(&p->dir_attr);
785}
786
Trond Myklebustad389da2007-06-05 12:30:00 -0400787static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500788 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400789 const struct iattr *attrs,
790 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791{
Trond Myklebustad389da2007-06-05 12:30:00 -0400792 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 struct inode *dir = parent->d_inode;
794 struct nfs_server *server = NFS_SERVER(dir);
795 struct nfs4_opendata *p;
796
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400797 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 if (p == NULL)
799 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400800 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 if (p->o_arg.seqid == NULL)
802 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500803 path_get(path);
804 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805 p->dir = parent;
806 p->owner = sp;
807 atomic_inc(&sp->so_count);
808 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500809 p->o_arg.open_flags = flags;
810 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400811 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400812 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400813 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 p->o_arg.server = server;
815 p->o_arg.bitmask = server->attr_bitmask;
816 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400817 if (flags & O_CREAT) {
818 u32 *s;
819
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 p->o_arg.u.attrs = &p->attrs;
821 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400822 s = (u32 *) p->o_arg.u.verifier.data;
823 s[0] = jiffies;
824 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100826 p->c_arg.fh = &p->o_res.fh;
827 p->c_arg.stateid = &p->o_res.stateid;
828 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400829 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400830 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 return p;
832err_free:
833 kfree(p);
834err:
835 dput(parent);
836 return NULL;
837}
838
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400841 struct nfs4_opendata *p = container_of(kref,
842 struct nfs4_opendata, kref);
843
844 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400845 if (p->state != NULL)
846 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 nfs4_put_state_owner(p->owner);
848 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700849 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400850 kfree(p);
851}
852
853static void nfs4_opendata_put(struct nfs4_opendata *p)
854{
855 if (p != NULL)
856 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857}
858
Trond Myklebust06f814a2006-01-03 09:55:07 +0100859static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
860{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100861 int ret;
862
Trond Myklebust06f814a2006-01-03 09:55:07 +0100863 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 return ret;
865}
866
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400868{
869 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870
871 if (open_mode & O_EXCL)
872 goto out;
873 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500875 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
876 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877 break;
878 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500879 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
880 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400881 break;
882 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500883 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
884 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 return ret;
888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400891{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400893 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500894 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500896 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897 return 1;
898}
899
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100901{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100903 case FMODE_WRITE:
904 state->n_wronly++;
905 break;
906 case FMODE_READ:
907 state->n_rdonly++;
908 break;
909 case FMODE_READ|FMODE_WRITE:
910 state->n_rdwr++;
911 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100913}
914
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400916{
917 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
918 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
919 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 case FMODE_READ:
922 set_bit(NFS_O_RDONLY_STATE, &state->flags);
923 break;
924 case FMODE_WRITE:
925 set_bit(NFS_O_WRONLY_STATE, &state->flags);
926 break;
927 case FMODE_READ|FMODE_WRITE:
928 set_bit(NFS_O_RDWR_STATE, &state->flags);
929 }
930}
931
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400933{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400934 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400936 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400937}
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939static 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 -0700940{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 /*
942 * Protect the call to nfs4_state_set_mode_locked and
943 * serialise the stateid update
944 */
945 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400946 if (deleg_stateid != NULL) {
947 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
948 set_bit(NFS_DELEGATED_STATE, &state->flags);
949 }
950 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400952 write_sequnlock(&state->seqlock);
953 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700955 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958static 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 -0500959{
960 struct nfs_inode *nfsi = NFS_I(state->inode);
961 struct nfs_delegation *deleg_cur;
962 int ret = 0;
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500965
966 rcu_read_lock();
967 deleg_cur = rcu_dereference(nfsi->delegation);
968 if (deleg_cur == NULL)
969 goto no_delegation;
970
971 spin_lock(&deleg_cur->lock);
972 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500974 goto no_delegation_unlock;
975
976 if (delegation == NULL)
977 delegation = &deleg_cur->stateid;
978 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
979 goto no_delegation_unlock;
980
Trond Myklebustb7391f42008-12-23 15:21:52 -0500981 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500983 ret = 1;
984no_delegation_unlock:
985 spin_unlock(&deleg_cur->lock);
986no_delegation:
987 rcu_read_unlock();
988
989 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500991 ret = 1;
992 }
993
994 return ret;
995}
996
997
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999{
1000 struct nfs_delegation *delegation;
1001
1002 rcu_read_lock();
1003 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 rcu_read_unlock();
1006 return;
1007 }
1008 rcu_read_unlock();
1009 nfs_inode_return_delegation(inode);
1010}
1011
Trond Myklebust6ee41262007-07-08 14:11:36 -04001012static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013{
1014 struct nfs4_state *state = opendata->state;
1015 struct nfs_inode *nfsi = NFS_I(state->inode);
1016 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1018 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 nfs4_stateid stateid;
1020 int ret = -EAGAIN;
1021
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 if (can_open_cached(state, fmode, open_mode)) {
1026 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001027 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001028 goto out_return_state;
1029 }
1030 spin_unlock(&state->owner->so_lock);
1031 }
Trond Myklebust34310432008-12-23 15:21:38 -05001032 rcu_read_lock();
1033 delegation = rcu_dereference(nfsi->delegation);
1034 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001036 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001037 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001038 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039 /* Save the delegation */
1040 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1041 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001042 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 if (ret != 0)
1044 goto out;
1045 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001046
1047 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001049 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051out:
1052 return ERR_PTR(ret);
1053out_return_state:
1054 atomic_inc(&state->count);
1055 return state;
1056}
1057
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001058static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1059{
1060 struct inode *inode;
1061 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001062 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001063 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001064
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001066 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001067 goto out;
1068 }
1069
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001071 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001072 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001073 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001075 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 goto err;
1077 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001078 state = nfs4_get_open_state(inode, data->owner);
1079 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001081 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001082 int delegation_flags = 0;
1083
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 rcu_read_lock();
1085 delegation = rcu_dereference(NFS_I(inode)->delegation);
1086 if (delegation)
1087 delegation_flags = delegation->flags;
1088 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001089 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001090 nfs_inode_set_delegation(state->inode,
1091 data->owner->so_cred,
1092 &data->o_res);
1093 else
1094 nfs_inode_reclaim_delegation(state->inode,
1095 data->owner->so_cred,
1096 &data->o_res);
1097 }
Trond Myklebust34310432008-12-23 15:21:38 -05001098
1099 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104err_put_inode:
1105 iput(inode);
1106err:
1107 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108}
1109
Trond Myklebust864472e2006-01-03 09:55:15 +01001110static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1111{
1112 struct nfs_inode *nfsi = NFS_I(state->inode);
1113 struct nfs_open_context *ctx;
1114
1115 spin_lock(&state->inode->i_lock);
1116 list_for_each_entry(ctx, &nfsi->open_files, list) {
1117 if (ctx->state != state)
1118 continue;
1119 get_nfs_open_context(ctx);
1120 spin_unlock(&state->inode->i_lock);
1121 return ctx;
1122 }
1123 spin_unlock(&state->inode->i_lock);
1124 return ERR_PTR(-ENOENT);
1125}
1126
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001127static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1128{
1129 struct nfs4_opendata *opendata;
1130
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001131 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001132 if (opendata == NULL)
1133 return ERR_PTR(-ENOMEM);
1134 opendata->state = state;
1135 atomic_inc(&state->count);
1136 return opendata;
1137}
1138
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001140{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001142 int ret;
1143
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 opendata->o_arg.open_flags = 0;
1145 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001146 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1147 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1148 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001149 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001150 if (ret != 0)
1151 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001152 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001153 if (IS_ERR(newstate))
1154 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001155 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001156 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 return 0;
1158}
1159
1160static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1161{
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001163 int ret;
1164
1165 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001166 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001167 smp_rmb();
1168 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001169 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 if (ret != 0)
1172 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001173 if (newstate != state)
1174 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 }
1176 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001177 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 if (ret != 0)
1180 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 if (newstate != state)
1182 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 }
1184 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001185 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 if (ret != 0)
1188 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 if (newstate != state)
1190 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001192 /*
1193 * We may have performed cached opens for all three recoveries.
1194 * Check if we need to update the current stateid.
1195 */
1196 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1197 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001198 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001199 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1200 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001201 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001202 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 return 0;
1204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/*
1207 * OPEN_RECLAIM:
1208 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001210static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001212 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001213 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int status;
1216
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001217 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1218 if (IS_ERR(opendata))
1219 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1221 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001222 rcu_read_lock();
1223 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001224 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001225 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 opendata->o_arg.u.delegation_type = delegation_type;
1228 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return status;
1231}
1232
Trond Myklebust539cd032007-06-05 11:46:42 -04001233static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 struct nfs_server *server = NFS_SERVER(state->inode);
1236 struct nfs4_exception exception = { };
1237 int err;
1238 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001239 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001240 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001241 break;
1242 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } while (exception.retry);
1244 return err;
1245}
1246
Trond Myklebust864472e2006-01-03 09:55:15 +01001247static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1248{
1249 struct nfs_open_context *ctx;
1250 int ret;
1251
1252 ctx = nfs4_state_find_open_context(state);
1253 if (IS_ERR(ctx))
1254 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001255 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001256 put_nfs_open_context(ctx);
1257 return ret;
1258}
1259
Trond Myklebust13437e12007-07-06 15:10:43 -04001260static 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 -07001261{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001262 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001265 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1266 if (IS_ERR(opendata))
1267 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001269 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001272 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Trond Myklebust13437e12007-07-06 15:10:43 -04001276int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001279 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int err;
1281 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001282 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 switch (err) {
1284 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001285 case -ENOENT:
1286 case -ESTALE:
1287 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001288 case -NFS4ERR_BADSESSION:
1289 case -NFS4ERR_BADSLOT:
1290 case -NFS4ERR_BAD_HIGH_SLOT:
1291 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1292 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001293 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 case -NFS4ERR_STALE_CLIENTID:
1296 case -NFS4ERR_STALE_STATEID:
1297 case -NFS4ERR_EXPIRED:
1298 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001299 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001300 goto out;
1301 case -ERESTARTSYS:
1302 /*
1303 * The show must go on: exit, but mark the
1304 * stateid as needing recovery.
1305 */
1306 case -NFS4ERR_ADMIN_REVOKED:
1307 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001308 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001309 case -EKEYEXPIRED:
1310 /*
1311 * User RPCSEC_GSS context has expired.
1312 * We cannot recover this stateid now, so
1313 * skip it and allow recovery thread to
1314 * proceed.
1315 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001316 case -ENOMEM:
1317 err = 0;
1318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 err = nfs4_handle_exception(server, err, &exception);
1321 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001322out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return err;
1324}
1325
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001326static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1327{
1328 struct nfs4_opendata *data = calldata;
1329
1330 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001331 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001332 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1333 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001334 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001335 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001336 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001337 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338}
1339
1340static void nfs4_open_confirm_release(void *calldata)
1341{
1342 struct nfs4_opendata *data = calldata;
1343 struct nfs4_state *state = NULL;
1344
1345 /* If this request hasn't been cancelled, do nothing */
1346 if (data->cancelled == 0)
1347 goto out_free;
1348 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001349 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001350 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001351 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001352 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001355 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356}
1357
1358static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 .rpc_call_done = nfs4_open_confirm_done,
1360 .rpc_release = nfs4_open_confirm_release,
1361};
1362
1363/*
1364 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1365 */
1366static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1367{
1368 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1369 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001370 struct rpc_message msg = {
1371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1372 .rpc_argp = &data->c_arg,
1373 .rpc_resp = &data->c_res,
1374 .rpc_cred = data->owner->so_cred,
1375 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001376 struct rpc_task_setup task_setup_data = {
1377 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001378 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001379 .callback_ops = &nfs4_open_confirm_ops,
1380 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001381 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001382 .flags = RPC_TASK_ASYNC,
1383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int status;
1385
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001386 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001387 data->rpc_done = 0;
1388 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001389 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001390 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001391 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 status = nfs4_wait_for_completion_rpc_task(task);
1394 if (status != 0) {
1395 data->cancelled = 1;
1396 smp_wmb();
1397 } else
1398 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001399 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return status;
1401}
1402
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405 struct nfs4_opendata *data = calldata;
1406 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001408 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1409 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001410 /*
1411 * Check if we still need to send an OPEN call, or if we can use
1412 * a delegation instead.
1413 */
1414 if (data->state != NULL) {
1415 struct nfs_delegation *delegation;
1416
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001417 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001418 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 rcu_read_lock();
1420 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1421 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001422 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001423 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001424 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425 }
1426 rcu_read_unlock();
1427 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001429 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001430 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001431 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001432 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001433 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1434 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001435 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001436 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001437 &data->o_arg.seq_args,
1438 &data->o_res.seq_res, 1, task))
1439 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001440 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001441 return;
1442out_no_action:
1443 task->tk_action = NULL;
1444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001447static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1448{
1449 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1450 nfs4_open_prepare(task, calldata);
1451}
1452
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453static void nfs4_open_done(struct rpc_task *task, void *calldata)
1454{
1455 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001458
Trond Myklebust14516c32010-07-31 14:29:06 -04001459 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1460 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 if (task->tk_status == 0) {
1463 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001464 case S_IFREG:
1465 break;
1466 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001468 break;
1469 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001471 break;
1472 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001473 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001474 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001475 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001476 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1477 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001478 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001479 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001481
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482static void nfs4_open_release(void *calldata)
1483{
1484 struct nfs4_opendata *data = calldata;
1485 struct nfs4_state *state = NULL;
1486
1487 /* If this request hasn't been cancelled, do nothing */
1488 if (data->cancelled == 0)
1489 goto out_free;
1490 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001491 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492 goto out_free;
1493 /* In case we need an open_confirm, no cleanup! */
1494 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1495 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001497 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001500 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501}
1502
1503static const struct rpc_call_ops nfs4_open_ops = {
1504 .rpc_call_prepare = nfs4_open_prepare,
1505 .rpc_call_done = nfs4_open_done,
1506 .rpc_release = nfs4_open_release,
1507};
1508
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001509static const struct rpc_call_ops nfs4_recover_open_ops = {
1510 .rpc_call_prepare = nfs4_recover_open_prepare,
1511 .rpc_call_done = nfs4_open_done,
1512 .rpc_release = nfs4_open_release,
1513};
1514
1515static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001516{
1517 struct inode *dir = data->dir->d_inode;
1518 struct nfs_server *server = NFS_SERVER(dir);
1519 struct nfs_openargs *o_arg = &data->o_arg;
1520 struct nfs_openres *o_res = &data->o_res;
1521 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001522 struct rpc_message msg = {
1523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1524 .rpc_argp = o_arg,
1525 .rpc_resp = o_res,
1526 .rpc_cred = data->owner->so_cred,
1527 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001528 struct rpc_task_setup task_setup_data = {
1529 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001530 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001531 .callback_ops = &nfs4_open_ops,
1532 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001533 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001534 .flags = RPC_TASK_ASYNC,
1535 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 int status;
1537
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001538 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001539 data->rpc_done = 0;
1540 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001541 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001542 if (isrecover)
1543 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001544 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001545 if (IS_ERR(task))
1546 return PTR_ERR(task);
1547 status = nfs4_wait_for_completion_rpc_task(task);
1548 if (status != 0) {
1549 data->cancelled = 1;
1550 smp_wmb();
1551 } else
1552 status = data->rpc_status;
1553 rpc_put_task(task);
1554
1555 return status;
1556}
1557
1558static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1559{
1560 struct inode *dir = data->dir->d_inode;
1561 struct nfs_openres *o_res = &data->o_res;
1562 int status;
1563
1564 status = nfs4_run_open_task(data, 1);
1565 if (status != 0 || !data->rpc_done)
1566 return status;
1567
1568 nfs_refresh_inode(dir, o_res->dir_attr);
1569
1570 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1571 status = _nfs4_proc_open_confirm(data);
1572 if (status != 0)
1573 return status;
1574 }
1575
1576 return status;
1577}
1578
1579/*
1580 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1581 */
1582static int _nfs4_proc_open(struct nfs4_opendata *data)
1583{
1584 struct inode *dir = data->dir->d_inode;
1585 struct nfs_server *server = NFS_SERVER(dir);
1586 struct nfs_openargs *o_arg = &data->o_arg;
1587 struct nfs_openres *o_res = &data->o_res;
1588 int status;
1589
1590 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001591 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001592 return status;
1593
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001594 if (o_arg->open_flags & O_CREAT) {
1595 update_changeattr(dir, &o_res->cinfo);
1596 nfs_post_op_update_inode(dir, o_res->dir_attr);
1597 } else
1598 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001599 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1600 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001602 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001607 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Andy Adamsond83217c2011-03-01 01:34:17 +00001611static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001612{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001613 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001614 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001615
Trond Myklebusta78cb572009-08-09 15:06:19 -04001616 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001617 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001618 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001619 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001620 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1621 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001622 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001623 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001624 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001625 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001626 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001627}
1628
Andy Adamsond83217c2011-03-01 01:34:17 +00001629static int nfs4_recover_expired_lease(struct nfs_server *server)
1630{
1631 return nfs4_client_recover_expired_lease(server->nfs_client);
1632}
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634/*
1635 * OPEN_EXPIRED:
1636 * reclaim state on the server after a network partition.
1637 * Assumes caller holds the appropriate lock
1638 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001639static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001641 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001642 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001644 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1645 if (IS_ERR(opendata))
1646 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001647 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001648 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001649 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001650 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001654static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001655{
Trond Myklebust539cd032007-06-05 11:46:42 -04001656 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001657 struct nfs4_exception exception = { };
1658 int err;
1659
1660 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001661 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001662 switch (err) {
1663 default:
1664 goto out;
1665 case -NFS4ERR_GRACE:
1666 case -NFS4ERR_DELAY:
1667 nfs4_handle_exception(server, err, &exception);
1668 err = 0;
1669 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001670 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001671out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001672 return err;
1673}
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1676{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Trond Myklebust864472e2006-01-03 09:55:15 +01001680 ctx = nfs4_state_find_open_context(state);
1681 if (IS_ERR(ctx))
1682 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001683 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001684 put_nfs_open_context(ctx);
1685 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001689 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1690 * fields corresponding to attributes that were used to store the verifier.
1691 * Make sure we clobber those fields in the later setattr call
1692 */
1693static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1694{
1695 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1696 !(sattr->ia_valid & ATTR_ATIME_SET))
1697 sattr->ia_valid |= ATTR_ATIME;
1698
1699 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1700 !(sattr->ia_valid & ATTR_MTIME_SET))
1701 sattr->ia_valid |= ATTR_MTIME;
1702}
1703
1704/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 struct nfs4_state_owner *sp;
1710 struct nfs4_state *state = NULL;
1711 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001712 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 status = -ENOMEM;
1717 if (!(sp = nfs4_get_state_owner(server, cred))) {
1718 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1719 goto out_err;
1720 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001721 status = nfs4_recover_expired_lease(server);
1722 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001723 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001725 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001726 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001727 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001728 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001729 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Trond Myklebustaac00a82007-07-05 19:02:21 -04001731 if (path->dentry->d_inode != NULL)
1732 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1733
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001736 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001739 status = PTR_ERR(state);
1740 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001741 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001742 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001743 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001744
1745 if (opendata->o_arg.open_flags & O_EXCL) {
1746 nfs4_exclusive_attrset(opendata, sattr);
1747
1748 nfs_fattr_init(opendata->o_res.f_attr);
1749 status = nfs4_do_setattr(state->inode, cred,
1750 opendata->o_res.f_attr, sattr,
1751 state);
1752 if (status == 0)
1753 nfs_setattr_update_inode(state->inode, sattr);
1754 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1755 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001756 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 *res = state;
1759 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001760err_opendata_put:
1761 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001762err_put_state_owner:
1763 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *res = NULL;
1766 return status;
1767}
1768
1769
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001770static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 struct nfs4_exception exception = { };
1773 struct nfs4_state *res;
1774 int status;
1775
1776 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001777 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (status == 0)
1779 break;
1780 /* NOTE: BAD_SEQID means the server and client disagree about the
1781 * book-keeping w.r.t. state-changing operations
1782 * (OPEN/CLOSE/LOCK/LOCKU...)
1783 * It is actually a sign of a bug on the client or on the server.
1784 *
1785 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001786 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * have unhashed the old state_owner for us, and that we can
1788 * therefore safely retry using a new one. We should still warn
1789 * the user though...
1790 */
1791 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001792 printk(KERN_WARNING "NFS: v4 server %s "
1793 " returned a bad sequence-id error!\n",
1794 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 exception.retry = 1;
1796 continue;
1797 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001798 /*
1799 * BAD_STATEID on OPEN means that the server cancelled our
1800 * state before it received the OPEN_CONFIRM.
1801 * Recover by retrying the request as per the discussion
1802 * on Page 181 of RFC3530.
1803 */
1804 if (status == -NFS4ERR_BAD_STATEID) {
1805 exception.retry = 1;
1806 continue;
1807 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001808 if (status == -EAGAIN) {
1809 /* We must have found a delegation */
1810 exception.retry = 1;
1811 continue;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1814 status, &exception));
1815 } while (exception.retry);
1816 return res;
1817}
1818
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001819static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1820 struct nfs_fattr *fattr, struct iattr *sattr,
1821 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001823 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001825 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 .iap = sattr,
1827 .server = server,
1828 .bitmask = server->attr_bitmask,
1829 };
1830 struct nfs_setattrres res = {
1831 .fattr = fattr,
1832 .server = server,
1833 };
1834 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1836 .rpc_argp = &arg,
1837 .rpc_resp = &res,
1838 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001840 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001841 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Trond Myklebust0e574af2005-10-27 22:12:38 -04001843 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001845 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1846 /* Use that stateid */
1847 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001848 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001849 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1851
Bryan Schumaker7c513052011-03-24 17:12:24 +00001852 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001853 if (status == 0 && state != NULL)
1854 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001855 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001858static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1859 struct nfs_fattr *fattr, struct iattr *sattr,
1860 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001862 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct nfs4_exception exception = { };
1864 int err;
1865 do {
1866 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001867 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 &exception);
1869 } while (exception.retry);
1870 return err;
1871}
1872
1873struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001874 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct inode *inode;
1876 struct nfs4_state *state;
1877 struct nfs_closeargs arg;
1878 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001879 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001880 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001881 bool roc;
1882 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
1884
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001885static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001886{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001887 struct nfs4_closedata *calldata = data;
1888 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001889
Fred Isamanf7e89172011-01-06 11:36:32 +00001890 if (calldata->roc)
1891 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001892 nfs4_put_open_state(calldata->state);
1893 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001894 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001895 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001896 kfree(calldata);
1897}
1898
Trond Myklebust88069f72009-12-08 08:33:16 -05001899static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1900 fmode_t fmode)
1901{
1902 spin_lock(&state->owner->so_lock);
1903 if (!(fmode & FMODE_READ))
1904 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1905 if (!(fmode & FMODE_WRITE))
1906 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1907 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1908 spin_unlock(&state->owner->so_lock);
1909}
1910
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001911static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct nfs_server *server = NFS_SERVER(calldata->inode);
1916
Trond Myklebust14516c32010-07-31 14:29:06 -04001917 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1918 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* hmm. we are done with the inode, and in the process of freeing
1920 * the state_owner. we keep this around to process errors
1921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 switch (task->tk_status) {
1923 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001924 if (calldata->roc)
1925 pnfs_roc_set_barrier(state->inode,
1926 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001927 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001928 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001929 nfs4_close_clear_stateid_flags(state,
1930 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001933 case -NFS4ERR_OLD_STATEID:
1934 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001936 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001939 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1940 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001942 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001943 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001946static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001948 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001949 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001950 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001951
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001952 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001953 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001954
Trond Myklebust88069f72009-12-08 08:33:16 -05001955 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1956 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001957 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001958 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001959 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001960 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001961 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1962 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1963 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001964 }
1965 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001966 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1967 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1968 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001969 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001970 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001971 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001972
1973 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001974 /* Note: exit _without_ calling nfs4_close_done */
1975 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001976 return;
1977 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001978
Fred Isamanf7e89172011-01-06 11:36:32 +00001979 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001980 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001981 if (calldata->roc &&
1982 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1983 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1984 task, NULL);
1985 return;
1986 }
1987 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001988
Trond Myklebust516a6af2005-10-27 22:12:41 -04001989 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001990 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001991 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001992 &calldata->arg.seq_args, &calldata->res.seq_res,
1993 1, task))
1994 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001995 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001998static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001999 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002000 .rpc_call_done = nfs4_close_done,
2001 .rpc_release = nfs4_free_closedata,
2002};
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/*
2005 * It is possible for data to be read/written from a mem-mapped file
2006 * after the sys_close call (which hits the vfs layer as a flush).
2007 * This means that we can't safely call nfsv4 close on a file until
2008 * the inode is cleared. This in turn means that we are not good
2009 * NFSv4 citizens - we do not indicate to the server to update the file's
2010 * share state even when we are done with one of the three share
2011 * stateid's in the inode.
2012 *
2013 * NOTE: Caller must be holding the sp->so_owner semaphore!
2014 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002015int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002017 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002019 struct nfs4_state_owner *sp = state->owner;
2020 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2023 .rpc_cred = state->owner->so_cred,
2024 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 struct rpc_task_setup task_setup_data = {
2026 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002027 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002028 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002029 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002030 .flags = RPC_TASK_ASYNC,
2031 };
Trond Myklebust95121352005-10-18 14:20:12 -07002032 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002034 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002036 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002037 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002039 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002040 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002042 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002043 if (calldata->arg.seqid == NULL)
2044 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002045 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002046 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002047 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002048 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002049 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002050 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002051 path_get(path);
2052 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002053
Trond Myklebust1174dd12010-12-21 10:52:24 -05002054 msg.rpc_argp = &calldata->arg;
2055 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002056 task_setup_data.callback_data = calldata;
2057 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002058 if (IS_ERR(task))
2059 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002060 status = 0;
2061 if (wait)
2062 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002063 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002064 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002065out_free_calldata:
2066 kfree(calldata);
2067out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002068 if (roc)
2069 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002070 nfs4_put_open_state(state);
2071 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002072 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Trond Myklebust2b484292010-09-17 10:56:51 -04002075static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002076nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct nfs4_state *state;
2079
Trond Myklebust565277f2007-10-15 18:17:53 -04002080 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002081 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002082 if (IS_ERR(state))
2083 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002084 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002085 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Trond Myklebust1185a552009-12-03 15:54:02 -05002088static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002089{
2090 if (ctx->state == NULL)
2091 return;
2092 if (is_sync)
2093 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2094 else
2095 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2096}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2099{
Benny Halevy43652ad2009-04-01 09:21:54 -04002100 struct nfs4_server_caps_arg args = {
2101 .fhandle = fhandle,
2102 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct nfs4_server_caps_res res = {};
2104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002106 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 .rpc_resp = &res,
2108 };
2109 int status;
2110
Bryan Schumaker7c513052011-03-24 17:12:24 +00002111 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (status == 0) {
2113 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002114 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2115 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2116 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2117 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2118 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2120 server->caps |= NFS_CAP_ACLS;
2121 if (res.has_links != 0)
2122 server->caps |= NFS_CAP_HARDLINKS;
2123 if (res.has_symlinks != 0)
2124 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002125 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2126 server->caps |= NFS_CAP_FILEID;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2128 server->caps |= NFS_CAP_MODE;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2130 server->caps |= NFS_CAP_NLINK;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2132 server->caps |= NFS_CAP_OWNER;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2134 server->caps |= NFS_CAP_OWNER_GROUP;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2136 server->caps |= NFS_CAP_ATIME;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2138 server->caps |= NFS_CAP_CTIME;
2139 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2140 server->caps |= NFS_CAP_MTIME;
2141
Trond Myklebusta65318b2009-03-11 14:10:28 -04002142 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2143 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2144 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 server->acl_bitmask = res.acl_bitmask;
2146 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return status;
2149}
2150
Trond Myklebust55a97592006-06-09 09:34:19 -04002151int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 struct nfs4_exception exception = { };
2154 int err;
2155 do {
2156 err = nfs4_handle_exception(server,
2157 _nfs4_server_capabilities(server, fhandle),
2158 &exception);
2159 } while (exception.retry);
2160 return err;
2161}
2162
2163static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2164 struct nfs_fsinfo *info)
2165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 struct nfs4_lookup_root_arg args = {
2167 .bitmask = nfs4_fattr_bitmap,
2168 };
2169 struct nfs4_lookup_res res = {
2170 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002171 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 .fh = fhandle,
2173 };
2174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2176 .rpc_argp = &args,
2177 .rpc_resp = &res,
2178 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002179
Trond Myklebust0e574af2005-10-27 22:12:38 -04002180 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002181 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2185 struct nfs_fsinfo *info)
2186{
2187 struct nfs4_exception exception = { };
2188 int err;
2189 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002190 err = _nfs4_lookup_root(server, fhandle, info);
2191 switch (err) {
2192 case 0:
2193 case -NFS4ERR_WRONGSEC:
2194 break;
2195 default:
2196 err = nfs4_handle_exception(server, err, &exception);
2197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 } while (exception.retry);
2199 return err;
2200}
2201
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002202static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2203 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2204{
2205 struct rpc_auth *auth;
2206 int ret;
2207
2208 auth = rpcauth_create(flavor, server->client);
2209 if (!auth) {
2210 ret = -EIO;
2211 goto out;
2212 }
2213 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002214out:
2215 return ret;
2216}
2217
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002218static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002219 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002221 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002222 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002224 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2225 flav_array[len] = RPC_AUTH_NULL;
2226 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002227
2228 for (i = 0; i < len; i++) {
2229 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002230 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002231 continue;
2232 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002233 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002234 /*
2235 * -EACCESS could mean that the user doesn't have correct permissions
2236 * to access the mount. It could also mean that we tried to mount
2237 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2238 * existing mount programs don't handle -EACCES very well so it should
2239 * be mapped to -EPERM instead.
2240 */
2241 if (status == -EACCES)
2242 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002243 return status;
2244}
2245
2246/*
2247 * get the file handle for the "/" directory on the server
2248 */
2249static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2250 struct nfs_fsinfo *info)
2251{
2252 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002253 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2254 /*
2255 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2256 * by nfs4_map_errors() as this function exits.
2257 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002258 status = nfs4_find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (status == 0)
2260 status = nfs4_server_capabilities(server, fhandle);
2261 if (status == 0)
2262 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002263 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265
Manoj Naik6b97fd32006-06-09 09:34:29 -04002266/*
2267 * Get locations and (maybe) other attributes of a referral.
2268 * Note that we'll actually follow the referral later when
2269 * we detect fsid mismatch in inode revalidation
2270 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002271static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002272{
2273 int status = -ENOMEM;
2274 struct page *page = NULL;
2275 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002276
2277 page = alloc_page(GFP_KERNEL);
2278 if (page == NULL)
2279 goto out;
2280 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2281 if (locations == NULL)
2282 goto out;
2283
Trond Myklebustc228fd32007-01-13 02:28:11 -05002284 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002285 if (status != 0)
2286 goto out;
2287 /* Make sure server returned a different fsid for the referral */
2288 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002289 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002290 status = -EIO;
2291 goto out;
2292 }
2293
2294 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2295 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2296 if (!fattr->mode)
2297 fattr->mode = S_IFDIR;
2298 memset(fhandle, 0, sizeof(struct nfs_fh));
2299out:
2300 if (page)
2301 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002302 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002303 return status;
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2307{
2308 struct nfs4_getattr_arg args = {
2309 .fh = fhandle,
2310 .bitmask = server->attr_bitmask,
2311 };
2312 struct nfs4_getattr_res res = {
2313 .fattr = fattr,
2314 .server = server,
2315 };
2316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2318 .rpc_argp = &args,
2319 .rpc_resp = &res,
2320 };
2321
Trond Myklebust0e574af2005-10-27 22:12:38 -04002322 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002323 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
2326static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2327{
2328 struct nfs4_exception exception = { };
2329 int err;
2330 do {
2331 err = nfs4_handle_exception(server,
2332 _nfs4_proc_getattr(server, fhandle, fattr),
2333 &exception);
2334 } while (exception.retry);
2335 return err;
2336}
2337
2338/*
2339 * The file is not closed if it is opened due to the a request to change
2340 * the size of the file. The open call will not be needed once the
2341 * VFS layer lookup-intents are implemented.
2342 *
2343 * Close is called when the inode is destroyed.
2344 * If we haven't opened the file for O_WRONLY, we
2345 * need to in the size_change case to obtain a stateid.
2346 *
2347 * Got race?
2348 * Because OPEN is always done by name in nfsv4, it is
2349 * possible that we opened a different file by the same
2350 * name. We can recognize this race condition, but we
2351 * can't do anything about it besides returning an error.
2352 *
2353 * This will be fixed with VFS changes (lookup-intent).
2354 */
2355static int
2356nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2357 struct iattr *sattr)
2358{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002359 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002360 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002361 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int status;
2363
Trond Myklebust0e574af2005-10-27 22:12:38 -04002364 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Trond Myklebustd5308382005-11-04 15:33:38 -05002366 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002367 if (sattr->ia_valid & ATTR_FILE) {
2368 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002369
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002370 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002371 if (ctx) {
2372 cred = ctx->cred;
2373 state = ctx->state;
2374 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002375 }
2376
2377 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002378 if (status == 0)
2379 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return status;
2381}
2382
Bryan Schumaker7c513052011-03-24 17:12:24 +00002383static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2384 const struct nfs_fh *dirfh, const struct qstr *name,
2385 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002386{
2387 int status;
2388 struct nfs4_lookup_arg args = {
2389 .bitmask = server->attr_bitmask,
2390 .dir_fh = dirfh,
2391 .name = name,
2392 };
2393 struct nfs4_lookup_res res = {
2394 .server = server,
2395 .fattr = fattr,
2396 .fh = fhandle,
2397 };
2398 struct rpc_message msg = {
2399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2400 .rpc_argp = &args,
2401 .rpc_resp = &res,
2402 };
2403
2404 nfs_fattr_init(fattr);
2405
2406 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002407 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002408 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002409 return status;
2410}
2411
2412static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2413 struct qstr *name, struct nfs_fh *fhandle,
2414 struct nfs_fattr *fattr)
2415{
2416 struct nfs4_exception exception = { };
2417 int err;
2418 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002419 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002420 /* FIXME: !!!! */
2421 if (err == -NFS4ERR_MOVED) {
2422 err = -EREMOTE;
2423 break;
2424 }
2425 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002426 } while (exception.retry);
2427 return err;
2428}
2429
Bryan Schumaker7c513052011-03-24 17:12:24 +00002430static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2431 const struct qstr *name, struct nfs_fh *fhandle,
2432 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002434 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002437 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002438 if (status == -NFS4ERR_MOVED)
2439 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 dprintk("NFS reply lookup: %d\n", status);
2441 return status;
2442}
2443
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002444void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2445{
2446 memset(fh, 0, sizeof(struct nfs_fh));
2447 fattr->fsid.major = 1;
2448 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2449 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2450 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2451 fattr->nlink = 2;
2452}
2453
Bryan Schumaker7c513052011-03-24 17:12:24 +00002454static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2455 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 struct nfs4_exception exception = { };
2458 int err;
2459 do {
2460 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002461 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002463 if (err == -EPERM)
2464 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 } while (exception.retry);
2466 return err;
2467}
2468
2469static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2470{
Trond Myklebust76b32992007-08-10 17:45:11 -04002471 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct nfs4_accessargs args = {
2473 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002474 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002476 struct nfs4_accessres res = {
2477 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002478 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct rpc_message msg = {
2480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2481 .rpc_argp = &args,
2482 .rpc_resp = &res,
2483 .rpc_cred = entry->cred,
2484 };
2485 int mode = entry->mask;
2486 int status;
2487
2488 /*
2489 * Determine which access bits we want to ask for...
2490 */
2491 if (mode & MAY_READ)
2492 args.access |= NFS4_ACCESS_READ;
2493 if (S_ISDIR(inode->i_mode)) {
2494 if (mode & MAY_WRITE)
2495 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2496 if (mode & MAY_EXEC)
2497 args.access |= NFS4_ACCESS_LOOKUP;
2498 } else {
2499 if (mode & MAY_WRITE)
2500 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2501 if (mode & MAY_EXEC)
2502 args.access |= NFS4_ACCESS_EXECUTE;
2503 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002504
2505 res.fattr = nfs_alloc_fattr();
2506 if (res.fattr == NULL)
2507 return -ENOMEM;
2508
Bryan Schumaker7c513052011-03-24 17:12:24 +00002509 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 if (!status) {
2511 entry->mask = 0;
2512 if (res.access & NFS4_ACCESS_READ)
2513 entry->mask |= MAY_READ;
2514 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2515 entry->mask |= MAY_WRITE;
2516 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2517 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002518 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002520 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return status;
2522}
2523
2524static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2525{
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_access(inode, entry),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2534}
2535
2536/*
2537 * TODO: For the time being, we don't try to get any attributes
2538 * along with any of the zero-copy operations READ, READDIR,
2539 * READLINK, WRITE.
2540 *
2541 * In the case of the first three, we want to put the GETATTR
2542 * after the read-type operation -- this is because it is hard
2543 * to predict the length of a GETATTR response in v4, and thus
2544 * align the READ data correctly. This means that the GETATTR
2545 * may end up partially falling into the page cache, and we should
2546 * shift it into the 'tail' of the xdr_buf before processing.
2547 * To do this efficiently, we need to know the total length
2548 * of data received, which doesn't seem to be available outside
2549 * of the RPC layer.
2550 *
2551 * In the case of WRITE, we also want to put the GETATTR after
2552 * the operation -- in this case because we want to make sure
2553 * we get the post-operation mtime and size. This means that
2554 * we can't use xdr_encode_pages() as written: we need a variant
2555 * of it which would leave room in the 'tail' iovec.
2556 *
2557 * Both of these changes to the XDR layer would in fact be quite
2558 * minor, but I decided to leave them for a subsequent patch.
2559 */
2560static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2561 unsigned int pgbase, unsigned int pglen)
2562{
2563 struct nfs4_readlink args = {
2564 .fh = NFS_FH(inode),
2565 .pgbase = pgbase,
2566 .pglen = pglen,
2567 .pages = &page,
2568 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002569 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct rpc_message msg = {
2571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2572 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002573 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 };
2575
Bryan Schumaker7c513052011-03-24 17:12:24 +00002576 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 -07002577}
2578
2579static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2580 unsigned int pgbase, unsigned int pglen)
2581{
2582 struct nfs4_exception exception = { };
2583 int err;
2584 do {
2585 err = nfs4_handle_exception(NFS_SERVER(inode),
2586 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2587 &exception);
2588 } while (exception.retry);
2589 return err;
2590}
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592/*
2593 * Got race?
2594 * We will need to arrange for the VFS layer to provide an atomic open.
2595 * Until then, this create/open method is prone to inefficiency and race
2596 * conditions due to the lookup, create, and open VFS calls from sys_open()
2597 * placed on the wire.
2598 *
2599 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2600 * The file will be opened again in the subsequent VFS open call
2601 * (nfs4_proc_file_open).
2602 *
2603 * The open for read will just hang around to be used by any process that
2604 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2605 */
2606
2607static int
2608nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002609 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002611 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002612 .dentry = dentry,
2613 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002614 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002616 struct rpc_cred *cred = NULL;
2617 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 int status = 0;
2619
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002620 if (ctx != NULL) {
2621 cred = ctx->cred;
2622 path = &ctx->path;
2623 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002625 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002626 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002627 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (IS_ERR(state)) {
2629 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002632 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002633 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002634 if (ctx != NULL)
2635 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002636 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638out:
2639 return status;
2640}
2641
2642static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2643{
Trond Myklebust16e42952005-10-27 22:12:44 -04002644 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002645 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002647 .name.len = name->len,
2648 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002649 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002651 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002652 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002653 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2656 .rpc_argp = &args,
2657 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002659 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Trond Myklebustd3468902010-04-16 16:22:50 -04002661 res.dir_attr = nfs_alloc_fattr();
2662 if (res.dir_attr == NULL)
2663 goto out;
2664
Bryan Schumaker7c513052011-03-24 17:12:24 +00002665 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002666 if (status == 0) {
2667 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002669 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002670 nfs_free_fattr(res.dir_attr);
2671out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 return status;
2673}
2674
2675static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2676{
2677 struct nfs4_exception exception = { };
2678 int err;
2679 do {
2680 err = nfs4_handle_exception(NFS_SERVER(dir),
2681 _nfs4_proc_remove(dir, name),
2682 &exception);
2683 } while (exception.retry);
2684 return err;
2685}
2686
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002687static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002689 struct nfs_server *server = NFS_SERVER(dir);
2690 struct nfs_removeargs *args = msg->rpc_argp;
2691 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Trond Myklebusta65318b2009-03-11 14:10:28 -04002693 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002694 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002695 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002699static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002701 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2702
Trond Myklebust14516c32010-07-31 14:29:06 -04002703 if (!nfs4_sequence_done(task, &res->seq_res))
2704 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002705 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002706 return 0;
2707 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002708 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002709 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Jeff Laytond3d41522010-09-17 17:31:57 -04002712static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2713{
2714 struct nfs_server *server = NFS_SERVER(dir);
2715 struct nfs_renameargs *arg = msg->rpc_argp;
2716 struct nfs_renameres *res = msg->rpc_resp;
2717
2718 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2719 arg->bitmask = server->attr_bitmask;
2720 res->server = server;
2721}
2722
2723static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2724 struct inode *new_dir)
2725{
2726 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2727
2728 if (!nfs4_sequence_done(task, &res->seq_res))
2729 return 0;
2730 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2731 return 0;
2732
2733 update_changeattr(old_dir, &res->old_cinfo);
2734 nfs_post_op_update_inode(old_dir, res->old_fattr);
2735 update_changeattr(new_dir, &res->new_cinfo);
2736 nfs_post_op_update_inode(new_dir, res->new_fattr);
2737 return 1;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2741 struct inode *new_dir, struct qstr *new_name)
2742{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002743 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002744 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 .old_dir = NFS_FH(old_dir),
2746 .new_dir = NFS_FH(new_dir),
2747 .old_name = old_name,
2748 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002749 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002751 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002752 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002753 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 struct rpc_message msg = {
2755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2756 .rpc_argp = &arg,
2757 .rpc_resp = &res,
2758 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002759 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Trond Myklebust011fff72010-04-16 16:22:49 -04002761 res.old_fattr = nfs_alloc_fattr();
2762 res.new_fattr = nfs_alloc_fattr();
2763 if (res.old_fattr == NULL || res.new_fattr == NULL)
2764 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Bryan Schumaker7c513052011-03-24 17:12:24 +00002766 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (!status) {
2768 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002769 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002771 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002773out:
2774 nfs_free_fattr(res.new_fattr);
2775 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return status;
2777}
2778
2779static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2780 struct inode *new_dir, struct qstr *new_name)
2781{
2782 struct nfs4_exception exception = { };
2783 int err;
2784 do {
2785 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2786 _nfs4_proc_rename(old_dir, old_name,
2787 new_dir, new_name),
2788 &exception);
2789 } while (exception.retry);
2790 return err;
2791}
2792
2793static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2794{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002795 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 struct nfs4_link_arg arg = {
2797 .fh = NFS_FH(inode),
2798 .dir_fh = NFS_FH(dir),
2799 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002800 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002802 struct nfs4_link_res res = {
2803 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002804 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct rpc_message msg = {
2806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2807 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002808 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002810 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Trond Myklebust136f2622010-04-16 16:22:49 -04002812 res.fattr = nfs_alloc_fattr();
2813 res.dir_attr = nfs_alloc_fattr();
2814 if (res.fattr == NULL || res.dir_attr == NULL)
2815 goto out;
2816
Bryan Schumaker7c513052011-03-24 17:12:24 +00002817 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002818 if (!status) {
2819 update_changeattr(dir, &res.cinfo);
2820 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002821 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002822 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002823out:
2824 nfs_free_fattr(res.dir_attr);
2825 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return status;
2827}
2828
2829static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2830{
2831 struct nfs4_exception exception = { };
2832 int err;
2833 do {
2834 err = nfs4_handle_exception(NFS_SERVER(inode),
2835 _nfs4_proc_link(inode, dir, name),
2836 &exception);
2837 } while (exception.retry);
2838 return err;
2839}
2840
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002841struct nfs4_createdata {
2842 struct rpc_message msg;
2843 struct nfs4_create_arg arg;
2844 struct nfs4_create_res res;
2845 struct nfs_fh fh;
2846 struct nfs_fattr fattr;
2847 struct nfs_fattr dir_fattr;
2848};
2849
2850static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2851 struct qstr *name, struct iattr *sattr, u32 ftype)
2852{
2853 struct nfs4_createdata *data;
2854
2855 data = kzalloc(sizeof(*data), GFP_KERNEL);
2856 if (data != NULL) {
2857 struct nfs_server *server = NFS_SERVER(dir);
2858
2859 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2860 data->msg.rpc_argp = &data->arg;
2861 data->msg.rpc_resp = &data->res;
2862 data->arg.dir_fh = NFS_FH(dir);
2863 data->arg.server = server;
2864 data->arg.name = name;
2865 data->arg.attrs = sattr;
2866 data->arg.ftype = ftype;
2867 data->arg.bitmask = server->attr_bitmask;
2868 data->res.server = server;
2869 data->res.fh = &data->fh;
2870 data->res.fattr = &data->fattr;
2871 data->res.dir_fattr = &data->dir_fattr;
2872 nfs_fattr_init(data->res.fattr);
2873 nfs_fattr_init(data->res.dir_fattr);
2874 }
2875 return data;
2876}
2877
2878static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2879{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002880 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002881 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002882 if (status == 0) {
2883 update_changeattr(dir, &data->res.dir_cinfo);
2884 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2885 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2886 }
2887 return status;
2888}
2889
2890static void nfs4_free_createdata(struct nfs4_createdata *data)
2891{
2892 kfree(data);
2893}
2894
Chuck Lever4f390c12006-08-22 20:06:22 -04002895static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002896 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002898 struct nfs4_createdata *data;
2899 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Chuck Lever94a6d752006-08-22 20:06:23 -04002901 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002902 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002903
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002904 status = -ENOMEM;
2905 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2906 if (data == NULL)
2907 goto out;
2908
2909 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2910 data->arg.u.symlink.pages = &page;
2911 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002913 status = nfs4_do_create(dir, dentry, data);
2914
2915 nfs4_free_createdata(data);
2916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return status;
2918}
2919
Chuck Lever4f390c12006-08-22 20:06:22 -04002920static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002921 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 struct nfs4_exception exception = { };
2924 int err;
2925 do {
2926 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002927 _nfs4_proc_symlink(dir, dentry, page,
2928 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 &exception);
2930 } while (exception.retry);
2931 return err;
2932}
2933
2934static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2935 struct iattr *sattr)
2936{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002937 struct nfs4_createdata *data;
2938 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002940 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2941 if (data == NULL)
2942 goto out;
2943
2944 status = nfs4_do_create(dir, dentry, data);
2945
2946 nfs4_free_createdata(data);
2947out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return status;
2949}
2950
2951static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2952 struct iattr *sattr)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002956
2957 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 do {
2959 err = nfs4_handle_exception(NFS_SERVER(dir),
2960 _nfs4_proc_mkdir(dir, dentry, sattr),
2961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002967 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 struct inode *dir = dentry->d_inode;
2970 struct nfs4_readdir_arg args = {
2971 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002972 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 .pgbase = 0,
2974 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002975 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002976 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 };
2978 struct nfs4_readdir_res res;
2979 struct rpc_message msg = {
2980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2981 .rpc_argp = &args,
2982 .rpc_resp = &res,
2983 .rpc_cred = cred,
2984 };
2985 int status;
2986
Harvey Harrison3110ff82008-05-02 13:42:44 -07002987 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002988 dentry->d_parent->d_name.name,
2989 dentry->d_name.name,
2990 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2992 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002993 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 -05002994 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002996 status += args.pgbase;
2997 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002998
2999 nfs_invalidate_atime(dir);
3000
Harvey Harrison3110ff82008-05-02 13:42:44 -07003001 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return status;
3003}
3004
3005static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003006 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 struct nfs4_exception exception = { };
3009 int err;
3010 do {
3011 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3012 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003013 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 &exception);
3015 } while (exception.retry);
3016 return err;
3017}
3018
3019static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3020 struct iattr *sattr, dev_t rdev)
3021{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003022 struct nfs4_createdata *data;
3023 int mode = sattr->ia_mode;
3024 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3027 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003028
3029 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3030 if (data == NULL)
3031 goto out;
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003036 data->arg.ftype = NF4BLK;
3037 data->arg.u.device.specdata1 = MAJOR(rdev);
3038 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
3040 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041 data->arg.ftype = NF4CHR;
3042 data->arg.u.device.specdata1 = MAJOR(rdev);
3043 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046 status = nfs4_do_create(dir, dentry, data);
3047
3048 nfs4_free_createdata(data);
3049out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return status;
3051}
3052
3053static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3054 struct iattr *sattr, dev_t rdev)
3055{
3056 struct nfs4_exception exception = { };
3057 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003058
3059 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 do {
3061 err = nfs4_handle_exception(NFS_SERVER(dir),
3062 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3063 &exception);
3064 } while (exception.retry);
3065 return err;
3066}
3067
3068static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3069 struct nfs_fsstat *fsstat)
3070{
3071 struct nfs4_statfs_arg args = {
3072 .fh = fhandle,
3073 .bitmask = server->attr_bitmask,
3074 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003075 struct nfs4_statfs_res res = {
3076 .fsstat = fsstat,
3077 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct rpc_message msg = {
3079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3080 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003081 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 };
3083
Trond Myklebust0e574af2005-10-27 22:12:38 -04003084 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003085 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086}
3087
3088static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3089{
3090 struct nfs4_exception exception = { };
3091 int err;
3092 do {
3093 err = nfs4_handle_exception(server,
3094 _nfs4_proc_statfs(server, fhandle, fsstat),
3095 &exception);
3096 } while (exception.retry);
3097 return err;
3098}
3099
3100static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3101 struct nfs_fsinfo *fsinfo)
3102{
3103 struct nfs4_fsinfo_arg args = {
3104 .fh = fhandle,
3105 .bitmask = server->attr_bitmask,
3106 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003107 struct nfs4_fsinfo_res res = {
3108 .fsinfo = fsinfo,
3109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct rpc_message msg = {
3111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3112 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003113 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 };
3115
Bryan Schumaker7c513052011-03-24 17:12:24 +00003116 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
3119static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3120{
3121 struct nfs4_exception exception = { };
3122 int err;
3123
3124 do {
3125 err = nfs4_handle_exception(server,
3126 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3127 &exception);
3128 } while (exception.retry);
3129 return err;
3130}
3131
3132static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3133{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003134 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3136}
3137
3138static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3139 struct nfs_pathconf *pathconf)
3140{
3141 struct nfs4_pathconf_arg args = {
3142 .fh = fhandle,
3143 .bitmask = server->attr_bitmask,
3144 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003145 struct nfs4_pathconf_res res = {
3146 .pathconf = pathconf,
3147 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct rpc_message msg = {
3149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3150 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003151 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 };
3153
3154 /* None of the pathconf attributes are mandatory to implement */
3155 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3156 memset(pathconf, 0, sizeof(*pathconf));
3157 return 0;
3158 }
3159
Trond Myklebust0e574af2005-10-27 22:12:38 -04003160 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003161 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
3164static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3165 struct nfs_pathconf *pathconf)
3166{
3167 struct nfs4_exception exception = { };
3168 int err;
3169
3170 do {
3171 err = nfs4_handle_exception(server,
3172 _nfs4_proc_pathconf(server, fhandle, pathconf),
3173 &exception);
3174 } while (exception.retry);
3175 return err;
3176}
3177
Benny Halevyd20581a2011-05-22 19:52:03 +03003178void __nfs4_read_done_cb(struct nfs_read_data *data)
3179{
3180 nfs_invalidate_atime(data->inode);
3181}
3182
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003183static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Trond Myklebustec06c092006-03-20 13:44:27 -05003185 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003187 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003188 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003189 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003191
Benny Halevyd20581a2011-05-22 19:52:03 +03003192 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003194 renew_lease(server, data->timestamp);
3195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003198static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3199{
3200
3201 dprintk("--> %s\n", __func__);
3202
3203 if (!nfs4_sequence_done(task, &data->res.seq_res))
3204 return -EAGAIN;
3205
Benny Halevyd20581a2011-05-22 19:52:03 +03003206 return data->read_done_cb ? data->read_done_cb(task, data) :
3207 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003208}
3209
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003210static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003213 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003214 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003217/* Reset the the nfs_read_data to send the read to the MDS. */
3218void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3219{
3220 dprintk("%s Reset task for i/o through\n", __func__);
3221 put_lseg(data->lseg);
3222 data->lseg = NULL;
3223 /* offsets will differ in the dense stripe case */
3224 data->args.offset = data->mds_offset;
3225 data->ds_clp = NULL;
3226 data->args.fh = NFS_FH(data->inode);
3227 data->read_done_cb = nfs4_read_done_cb;
3228 task->tk_ops = data->mds_ops;
3229 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3230}
3231EXPORT_SYMBOL_GPL(nfs4_reset_read);
3232
Fred Isamanb029bc92011-03-03 15:13:42 +00003233static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 struct inode *inode = data->inode;
3236
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003237 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003238 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003239 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003241 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003243 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003244 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Fred Isamanb029bc92011-03-03 15:13:42 +00003248static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3249{
3250 if (!nfs4_sequence_done(task, &data->res.seq_res))
3251 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003252 return data->write_done_cb ? data->write_done_cb(task, data) :
3253 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003254}
3255
Fred Isamana69aef12011-03-03 15:13:47 +00003256/* Reset the the nfs_write_data to send the write to the MDS. */
3257void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3258{
3259 dprintk("%s Reset task for i/o through\n", __func__);
3260 put_lseg(data->lseg);
3261 data->lseg = NULL;
3262 data->ds_clp = NULL;
3263 data->write_done_cb = nfs4_write_done_cb;
3264 data->args.fh = NFS_FH(data->inode);
3265 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3266 data->args.offset = data->mds_offset;
3267 data->res.fattr = &data->fattr;
3268 task->tk_ops = data->mds_ops;
3269 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3270}
3271EXPORT_SYMBOL_GPL(nfs4_reset_write);
3272
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003273static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003275 struct nfs_server *server = NFS_SERVER(data->inode);
3276
Fred Isaman7ffd1062011-03-03 15:13:46 +00003277 if (data->lseg) {
3278 data->args.bitmask = NULL;
3279 data->res.fattr = NULL;
3280 } else
3281 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003282 if (!data->write_done_cb)
3283 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003284 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 data->timestamp = jiffies;
3286
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003287 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288}
3289
Fred Isaman5f452432011-03-23 13:27:46 +00003290static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003293
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003294 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003295 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003296 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003298 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Fred Isaman5f452432011-03-23 13:27:46 +00003302static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3303{
3304 if (!nfs4_sequence_done(task, &data->res.seq_res))
3305 return -EAGAIN;
3306 return data->write_done_cb(task, data);
3307}
3308
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003309static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003311 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003312
3313 if (data->lseg) {
3314 data->args.bitmask = NULL;
3315 data->res.fattr = NULL;
3316 } else
3317 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003318 if (!data->write_done_cb)
3319 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003320 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003321 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003324struct nfs4_renewdata {
3325 struct nfs_client *client;
3326 unsigned long timestamp;
3327};
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/*
3330 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3331 * standalone procedure for queueing an asynchronous RENEW.
3332 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003333static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003334{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003335 struct nfs4_renewdata *data = calldata;
3336 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003337
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003338 if (atomic_read(&clp->cl_count) > 1)
3339 nfs4_schedule_state_renewal(clp);
3340 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003341 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003342}
3343
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003346 struct nfs4_renewdata *data = calldata;
3347 struct nfs_client *clp = data->client;
3348 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003351 /* Unless we're shutting down, schedule state recovery! */
3352 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003353 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return;
3355 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003356 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357}
3358
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003359static const struct rpc_call_ops nfs4_renew_ops = {
3360 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003361 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003362};
3363
David Howellsadfa6f92006-08-22 20:06:08 -04003364int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3368 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003369 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003371 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003373 if (!atomic_inc_not_zero(&clp->cl_count))
3374 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003375 data = kmalloc(sizeof(*data), GFP_KERNEL);
3376 if (data == NULL)
3377 return -ENOMEM;
3378 data->client = clp;
3379 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003381 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
David Howellsadfa6f92006-08-22 20:06:08 -04003384int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385{
3386 struct rpc_message msg = {
3387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3388 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003389 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 };
3391 unsigned long now = jiffies;
3392 int status;
3393
3394 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3395 if (status < 0)
3396 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003397 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return 0;
3399}
3400
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003401static inline int nfs4_server_supports_acls(struct nfs_server *server)
3402{
3403 return (server->caps & NFS_CAP_ACLS)
3404 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3405 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3406}
3407
3408/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3409 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3410 * the stack.
3411 */
3412#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3413
3414static void buf_to_pages(const void *buf, size_t buflen,
3415 struct page **pages, unsigned int *pgbase)
3416{
3417 const void *p = buf;
3418
3419 *pgbase = offset_in_page(buf);
3420 p -= *pgbase;
3421 while (p < buf + buflen) {
3422 *(pages++) = virt_to_page(p);
3423 p += PAGE_CACHE_SIZE;
3424 }
3425}
3426
Neil Hormane9e3d722011-03-04 19:26:03 -05003427static int buf_to_pages_noslab(const void *buf, size_t buflen,
3428 struct page **pages, unsigned int *pgbase)
3429{
3430 struct page *newpage, **spages;
3431 int rc = 0;
3432 size_t len;
3433 spages = pages;
3434
3435 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003436 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003437 newpage = alloc_page(GFP_KERNEL);
3438
3439 if (newpage == NULL)
3440 goto unwind;
3441 memcpy(page_address(newpage), buf, len);
3442 buf += len;
3443 buflen -= len;
3444 *pages++ = newpage;
3445 rc++;
3446 } while (buflen != 0);
3447
3448 return rc;
3449
3450unwind:
3451 for(; rc > 0; rc--)
3452 __free_page(spages[rc-1]);
3453 return -ENOMEM;
3454}
3455
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003456struct nfs4_cached_acl {
3457 int cached;
3458 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003459 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003460};
3461
3462static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003463{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003464 struct nfs_inode *nfsi = NFS_I(inode);
3465
3466 spin_lock(&inode->i_lock);
3467 kfree(nfsi->nfs4_acl);
3468 nfsi->nfs4_acl = acl;
3469 spin_unlock(&inode->i_lock);
3470}
3471
3472static void nfs4_zap_acl_attr(struct inode *inode)
3473{
3474 nfs4_set_cached_acl(inode, NULL);
3475}
3476
3477static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3478{
3479 struct nfs_inode *nfsi = NFS_I(inode);
3480 struct nfs4_cached_acl *acl;
3481 int ret = -ENOENT;
3482
3483 spin_lock(&inode->i_lock);
3484 acl = nfsi->nfs4_acl;
3485 if (acl == NULL)
3486 goto out;
3487 if (buf == NULL) /* user is just asking for length */
3488 goto out_len;
3489 if (acl->cached == 0)
3490 goto out;
3491 ret = -ERANGE; /* see getxattr(2) man page */
3492 if (acl->len > buflen)
3493 goto out;
3494 memcpy(buf, acl->data, acl->len);
3495out_len:
3496 ret = acl->len;
3497out:
3498 spin_unlock(&inode->i_lock);
3499 return ret;
3500}
3501
3502static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3503{
3504 struct nfs4_cached_acl *acl;
3505
3506 if (buf && acl_len <= PAGE_SIZE) {
3507 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3508 if (acl == NULL)
3509 goto out;
3510 acl->cached = 1;
3511 memcpy(acl->data, buf, acl_len);
3512 } else {
3513 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3514 if (acl == NULL)
3515 goto out;
3516 acl->cached = 0;
3517 }
3518 acl->len = acl_len;
3519out:
3520 nfs4_set_cached_acl(inode, acl);
3521}
3522
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003523static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003524{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003525 struct page *pages[NFS4ACL_MAXPAGES];
3526 struct nfs_getaclargs args = {
3527 .fh = NFS_FH(inode),
3528 .acl_pages = pages,
3529 .acl_len = buflen,
3530 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003531 struct nfs_getaclres res = {
3532 .acl_len = buflen,
3533 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003534 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003535 struct rpc_message msg = {
3536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3537 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003538 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003539 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003540 struct page *localpage = NULL;
3541 int ret;
3542
3543 if (buflen < PAGE_SIZE) {
3544 /* As long as we're doing a round trip to the server anyway,
3545 * let's be prepared for a page of acl data. */
3546 localpage = alloc_page(GFP_KERNEL);
3547 resp_buf = page_address(localpage);
3548 if (localpage == NULL)
3549 return -ENOMEM;
3550 args.acl_pages[0] = localpage;
3551 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003552 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003553 } else {
3554 resp_buf = buf;
3555 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3556 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003557 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 +00003558 if (ret)
3559 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003560 if (res.acl_len > args.acl_len)
3561 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003562 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003563 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003564 if (buf) {
3565 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003566 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003567 goto out_free;
3568 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003569 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003570 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003571 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003572out_free:
3573 if (localpage)
3574 __free_page(localpage);
3575 return ret;
3576}
3577
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003578static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3579{
3580 struct nfs4_exception exception = { };
3581 ssize_t ret;
3582 do {
3583 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3584 if (ret >= 0)
3585 break;
3586 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3587 } while (exception.retry);
3588 return ret;
3589}
3590
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003591static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3592{
3593 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003594 int ret;
3595
3596 if (!nfs4_server_supports_acls(server))
3597 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003598 ret = nfs_revalidate_inode(server, inode);
3599 if (ret < 0)
3600 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003601 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3602 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003603 ret = nfs4_read_cached_acl(inode, buf, buflen);
3604 if (ret != -ENOENT)
3605 return ret;
3606 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003607}
3608
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003609static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003610{
3611 struct nfs_server *server = NFS_SERVER(inode);
3612 struct page *pages[NFS4ACL_MAXPAGES];
3613 struct nfs_setaclargs arg = {
3614 .fh = NFS_FH(inode),
3615 .acl_pages = pages,
3616 .acl_len = buflen,
3617 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003618 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003619 struct rpc_message msg = {
3620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3621 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003622 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003623 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003624 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003625
3626 if (!nfs4_server_supports_acls(server))
3627 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003628 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3629 if (i < 0)
3630 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003631 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003632 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003633
3634 /*
3635 * Free each page after tx, so the only ref left is
3636 * held by the network stack
3637 */
3638 for (; i > 0; i--)
3639 put_page(pages[i-1]);
3640
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003641 /*
3642 * Acl update can result in inode attribute update.
3643 * so mark the attribute cache invalid.
3644 */
3645 spin_lock(&inode->i_lock);
3646 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3647 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003648 nfs_access_zap_cache(inode);
3649 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003650 return ret;
3651}
3652
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003653static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3654{
3655 struct nfs4_exception exception = { };
3656 int err;
3657 do {
3658 err = nfs4_handle_exception(NFS_SERVER(inode),
3659 __nfs4_proc_set_acl(inode, buf, buflen),
3660 &exception);
3661 } while (exception.retry);
3662 return err;
3663}
3664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003666nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003668 struct nfs_client *clp = server->nfs_client;
3669
3670 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return 0;
3672 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003673 case -NFS4ERR_ADMIN_REVOKED:
3674 case -NFS4ERR_BAD_STATEID:
3675 case -NFS4ERR_OPENMODE:
3676 if (state == NULL)
3677 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003678 nfs4_schedule_stateid_recovery(server, state);
3679 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003681 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003683 nfs4_schedule_lease_recovery(clp);
3684 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003685#if defined(CONFIG_NFS_V4_1)
3686 case -NFS4ERR_BADSESSION:
3687 case -NFS4ERR_BADSLOT:
3688 case -NFS4ERR_BAD_HIGH_SLOT:
3689 case -NFS4ERR_DEADSESSION:
3690 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3691 case -NFS4ERR_SEQ_FALSE_RETRY:
3692 case -NFS4ERR_SEQ_MISORDERED:
3693 dprintk("%s ERROR %d, Reset session\n", __func__,
3694 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003695 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003696 task->tk_status = 0;
3697 return -EAGAIN;
3698#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003700 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003701 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003702 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3704 task->tk_status = 0;
3705 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003706 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 case -NFS4ERR_OLD_STATEID:
3708 task->tk_status = 0;
3709 return -EAGAIN;
3710 }
3711 task->tk_status = nfs4_map_errors(task->tk_status);
3712 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003713wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003714 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003715 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3716 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3717 task->tk_status = 0;
3718 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003721int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3722 unsigned short port, struct rpc_cred *cred,
3723 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 nfs4_verifier sc_verifier;
3726 struct nfs4_setclientid setclientid = {
3727 .sc_verifier = &sc_verifier,
3728 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003729 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 };
3731 struct rpc_message msg = {
3732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3733 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003734 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003735 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 };
Al Virobc4785c2006-10-19 23:28:51 -07003737 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 int loop = 0;
3739 int status;
3740
Al Virobc4785c2006-10-19 23:28:51 -07003741 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3743 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3744
3745 for(;;) {
3746 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003747 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003748 clp->cl_ipaddr,
3749 rpc_peeraddr2str(clp->cl_rpcclient,
3750 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003751 rpc_peeraddr2str(clp->cl_rpcclient,
3752 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003753 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 clp->cl_id_uniquifier);
3755 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003756 sizeof(setclientid.sc_netid),
3757 rpc_peeraddr2str(clp->cl_rpcclient,
3758 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003760 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 clp->cl_ipaddr, port >> 8, port & 255);
3762
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003763 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 if (status != -NFS4ERR_CLID_INUSE)
3765 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003766 if (loop != 0) {
3767 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003769 }
3770 ++loop;
3771 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
3773 return status;
3774}
3775
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003776int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003777 struct nfs4_setclientid_res *arg,
3778 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
3780 struct nfs_fsinfo fsinfo;
3781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003783 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003785 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 };
3787 unsigned long now;
3788 int status;
3789
3790 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003791 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (status == 0) {
3793 spin_lock(&clp->cl_lock);
3794 clp->cl_lease_time = fsinfo.lease_time * HZ;
3795 clp->cl_last_renewal = now;
3796 spin_unlock(&clp->cl_lock);
3797 }
3798 return status;
3799}
3800
Trond Myklebustfe650402006-01-03 09:55:18 +01003801struct nfs4_delegreturndata {
3802 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003803 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003804 struct nfs_fh fh;
3805 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003806 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003807 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003808 int rpc_status;
3809};
3810
Trond Myklebustfe650402006-01-03 09:55:18 +01003811static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3812{
3813 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003814
Trond Myklebust14516c32010-07-31 14:29:06 -04003815 if (!nfs4_sequence_done(task, &data->res.seq_res))
3816 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003817
Ricardo Labiaga79708862009-12-07 09:23:21 -05003818 switch (task->tk_status) {
3819 case -NFS4ERR_STALE_STATEID:
3820 case -NFS4ERR_EXPIRED:
3821 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003822 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003823 break;
3824 default:
3825 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3826 -EAGAIN) {
3827 nfs_restart_rpc(task, data->res.server->nfs_client);
3828 return;
3829 }
3830 }
3831 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003832}
3833
3834static void nfs4_delegreturn_release(void *calldata)
3835{
Trond Myklebustfe650402006-01-03 09:55:18 +01003836 kfree(calldata);
3837}
3838
Andy Adamson938e1012009-04-01 09:22:28 -04003839#if defined(CONFIG_NFS_V4_1)
3840static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3841{
3842 struct nfs4_delegreturndata *d_data;
3843
3844 d_data = (struct nfs4_delegreturndata *)data;
3845
Trond Myklebust035168a2010-06-16 09:52:26 -04003846 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003847 &d_data->args.seq_args,
3848 &d_data->res.seq_res, 1, task))
3849 return;
3850 rpc_call_start(task);
3851}
3852#endif /* CONFIG_NFS_V4_1 */
3853
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003854static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003855#if defined(CONFIG_NFS_V4_1)
3856 .rpc_call_prepare = nfs4_delegreturn_prepare,
3857#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003858 .rpc_call_done = nfs4_delegreturn_done,
3859 .rpc_release = nfs4_delegreturn_release,
3860};
3861
Trond Myklebuste6f81072008-01-24 18:14:34 -05003862static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003863{
3864 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003865 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003866 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003867 struct rpc_message msg = {
3868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3869 .rpc_cred = cred,
3870 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003871 struct rpc_task_setup task_setup_data = {
3872 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003873 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003874 .callback_ops = &nfs4_delegreturn_ops,
3875 .flags = RPC_TASK_ASYNC,
3876 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003877 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003878
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003879 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003880 if (data == NULL)
3881 return -ENOMEM;
3882 data->args.fhandle = &data->fh;
3883 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003884 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003885 nfs_copy_fh(&data->fh, NFS_FH(inode));
3886 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003887 data->res.fattr = &data->fattr;
3888 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003889 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003890 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003891 data->rpc_status = 0;
3892
Trond Myklebustc970aa82007-07-14 15:39:59 -04003893 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003894 msg.rpc_argp = &data->args;
3895 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003896 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003897 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003898 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003899 if (!issync)
3900 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003901 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003902 if (status != 0)
3903 goto out;
3904 status = data->rpc_status;
3905 if (status != 0)
3906 goto out;
3907 nfs_refresh_inode(inode, &data->fattr);
3908out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003909 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003910 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
Trond Myklebuste6f81072008-01-24 18:14:34 -05003913int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914{
3915 struct nfs_server *server = NFS_SERVER(inode);
3916 struct nfs4_exception exception = { };
3917 int err;
3918 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003919 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 switch (err) {
3921 case -NFS4ERR_STALE_STATEID:
3922 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 case 0:
3924 return 0;
3925 }
3926 err = nfs4_handle_exception(server, err, &exception);
3927 } while (exception.retry);
3928 return err;
3929}
3930
3931#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3932#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3933
3934/*
3935 * sleep, with exponential backoff, and retry the LOCK operation.
3936 */
3937static unsigned long
3938nfs4_set_lock_task_retry(unsigned long timeout)
3939{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003940 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 timeout <<= 1;
3942 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3943 return NFS4_LOCK_MAXTIMEOUT;
3944 return timeout;
3945}
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3948{
3949 struct inode *inode = state->inode;
3950 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003951 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003952 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003954 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003956 struct nfs_lockt_res res = {
3957 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 };
3959 struct rpc_message msg = {
3960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3961 .rpc_argp = &arg,
3962 .rpc_resp = &res,
3963 .rpc_cred = state->owner->so_cred,
3964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 struct nfs4_lock_state *lsp;
3966 int status;
3967
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003968 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003969 status = nfs4_set_lock_state(state, request);
3970 if (status != 0)
3971 goto out;
3972 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003973 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003974 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003975 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003976 switch (status) {
3977 case 0:
3978 request->fl_type = F_UNLCK;
3979 break;
3980 case -NFS4ERR_DENIED:
3981 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003983 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return status;
3986}
3987
3988static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3989{
3990 struct nfs4_exception exception = { };
3991 int err;
3992
3993 do {
3994 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3995 _nfs4_proc_getlk(state, cmd, request),
3996 &exception);
3997 } while (exception.retry);
3998 return err;
3999}
4000
4001static int do_vfs_lock(struct file *file, struct file_lock *fl)
4002{
4003 int res = 0;
4004 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4005 case FL_POSIX:
4006 res = posix_lock_file_wait(file, fl);
4007 break;
4008 case FL_FLOCK:
4009 res = flock_lock_file_wait(file, fl);
4010 break;
4011 default:
4012 BUG();
4013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return res;
4015}
4016
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004017struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004018 struct nfs_locku_args arg;
4019 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004020 struct nfs4_lock_state *lsp;
4021 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004022 struct file_lock fl;
4023 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004024 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004025};
4026
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004027static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4028 struct nfs_open_context *ctx,
4029 struct nfs4_lock_state *lsp,
4030 struct nfs_seqid *seqid)
4031{
4032 struct nfs4_unlockdata *p;
4033 struct inode *inode = lsp->ls_state->inode;
4034
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004035 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004036 if (p == NULL)
4037 return NULL;
4038 p->arg.fh = NFS_FH(inode);
4039 p->arg.fl = &p->fl;
4040 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004041 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004042 p->arg.stateid = &lsp->ls_stateid;
4043 p->lsp = lsp;
4044 atomic_inc(&lsp->ls_count);
4045 /* Ensure we don't close file until we're done freeing locks! */
4046 p->ctx = get_nfs_open_context(ctx);
4047 memcpy(&p->fl, fl, sizeof(p->fl));
4048 p->server = NFS_SERVER(inode);
4049 return p;
4050}
4051
Trond Myklebust06f814a2006-01-03 09:55:07 +01004052static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004053{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004054 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004055 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004056 nfs4_put_lock_state(calldata->lsp);
4057 put_nfs_open_context(calldata->ctx);
4058 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004059}
4060
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004061static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004063 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004064
Trond Myklebust14516c32010-07-31 14:29:06 -04004065 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4066 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067 switch (task->tk_status) {
4068 case 0:
4069 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004070 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004071 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004072 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004073 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004074 case -NFS4ERR_BAD_STATEID:
4075 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004076 case -NFS4ERR_STALE_STATEID:
4077 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004078 break;
4079 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004080 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004081 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004082 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004083 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084}
4085
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004086static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004088 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004090 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004091 return;
4092 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004093 /* Note: exit _without_ running nfs4_locku_done */
4094 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004095 return;
4096 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004097 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004098 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004099 &calldata->arg.seq_args,
4100 &calldata->res.seq_res, 1, task))
4101 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004102 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004105static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004106 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004107 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004108 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004109};
4110
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4112 struct nfs_open_context *ctx,
4113 struct nfs4_lock_state *lsp,
4114 struct nfs_seqid *seqid)
4115{
4116 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4119 .rpc_cred = ctx->cred,
4120 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004121 struct rpc_task_setup task_setup_data = {
4122 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004123 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004124 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004125 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004126 .flags = RPC_TASK_ASYNC,
4127 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128
Frank Filz137d6ac2007-07-09 15:32:29 -07004129 /* Ensure this is an unlock - when canceling a lock, the
4130 * canceled lock is passed in, and it won't be an unlock.
4131 */
4132 fl->fl_type = F_UNLCK;
4133
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004134 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4135 if (data == NULL) {
4136 nfs_free_seqid(seqid);
4137 return ERR_PTR(-ENOMEM);
4138 }
4139
Trond Myklebust1174dd12010-12-21 10:52:24 -05004140 msg.rpc_argp = &data->arg;
4141 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004142 task_setup_data.callback_data = data;
4143 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4147{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004148 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004149 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004150 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004151 struct rpc_task *task;
4152 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004153 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Trond Myklebust9b073572006-06-29 16:38:34 -04004155 status = nfs4_set_lock_state(state, request);
4156 /* Unlock _before_ we do the RPC call */
4157 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004158 down_read(&nfsi->rwsem);
4159 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4160 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004161 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004162 }
4163 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004164 if (status != 0)
4165 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004166 /* Is this a delegated lock? */
4167 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004168 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004169 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004170 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004171 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004172 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004173 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004174 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004175 status = PTR_ERR(task);
4176 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004177 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004178 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004179 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004180out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004181 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004182 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004185struct nfs4_lockdata {
4186 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004187 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188 struct nfs4_lock_state *lsp;
4189 struct nfs_open_context *ctx;
4190 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004191 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004192 int rpc_status;
4193 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004194 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195};
4196
4197static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004198 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4199 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200{
4201 struct nfs4_lockdata *p;
4202 struct inode *inode = lsp->ls_state->inode;
4203 struct nfs_server *server = NFS_SERVER(inode);
4204
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004205 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206 if (p == NULL)
4207 return NULL;
4208
4209 p->arg.fh = NFS_FH(inode);
4210 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004211 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004212 if (p->arg.open_seqid == NULL)
4213 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004214 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004216 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004218 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004219 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004220 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004221 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004223 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 atomic_inc(&lsp->ls_count);
4225 p->ctx = get_nfs_open_context(ctx);
4226 memcpy(&p->fl, fl, sizeof(p->fl));
4227 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004228out_free_seqid:
4229 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230out_free:
4231 kfree(p);
4232 return NULL;
4233}
4234
4235static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4236{
4237 struct nfs4_lockdata *data = calldata;
4238 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Harvey Harrison3110ff82008-05-02 13:42:44 -07004240 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004241 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4242 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 /* Do we need to do an open_to_lock_owner? */
4244 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004245 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4246 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004247 data->arg.open_stateid = &state->stateid;
4248 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004249 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004250 } else
4251 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004252 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004253 if (nfs4_setup_sequence(data->server,
4254 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004255 &data->res.seq_res, 1, task))
4256 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004257 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004258 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004259}
4260
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004261static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4262{
4263 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4264 nfs4_lock_prepare(task, calldata);
4265}
4266
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004267static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4268{
4269 struct nfs4_lockdata *data = calldata;
4270
Harvey Harrison3110ff82008-05-02 13:42:44 -07004271 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272
Trond Myklebust14516c32010-07-31 14:29:06 -04004273 if (!nfs4_sequence_done(task, &data->res.seq_res))
4274 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004275
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004277 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004278 if (data->rpc_status == 0)
4279 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4280 else
4281 goto out;
4282 }
4283 if (data->rpc_status == 0) {
4284 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4285 sizeof(data->lsp->ls_stateid.data));
4286 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004287 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004289out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004290 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291}
4292
4293static void nfs4_lock_release(void *calldata)
4294{
4295 struct nfs4_lockdata *data = calldata;
4296
Harvey Harrison3110ff82008-05-02 13:42:44 -07004297 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004298 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299 if (data->cancelled != 0) {
4300 struct rpc_task *task;
4301 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4302 data->arg.lock_seqid);
4303 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004304 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004305 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306 } else
4307 nfs_free_seqid(data->arg.lock_seqid);
4308 nfs4_put_lock_state(data->lsp);
4309 put_nfs_open_context(data->ctx);
4310 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312}
4313
4314static const struct rpc_call_ops nfs4_lock_ops = {
4315 .rpc_call_prepare = nfs4_lock_prepare,
4316 .rpc_call_done = nfs4_lock_done,
4317 .rpc_release = nfs4_lock_release,
4318};
4319
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004320static const struct rpc_call_ops nfs4_recover_lock_ops = {
4321 .rpc_call_prepare = nfs4_recover_lock_prepare,
4322 .rpc_call_done = nfs4_lock_done,
4323 .rpc_release = nfs4_lock_release,
4324};
4325
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004326static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4327{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004328 switch (error) {
4329 case -NFS4ERR_ADMIN_REVOKED:
4330 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004331 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004332 if (new_lock_owner != 0 ||
4333 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004334 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004335 break;
4336 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004337 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004338 case -NFS4ERR_EXPIRED:
4339 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004340 };
4341}
4342
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004343static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004344{
4345 struct nfs4_lockdata *data;
4346 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4349 .rpc_cred = state->owner->so_cred,
4350 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004351 struct rpc_task_setup task_setup_data = {
4352 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004353 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004354 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004355 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004356 .flags = RPC_TASK_ASYNC,
4357 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358 int ret;
4359
Harvey Harrison3110ff82008-05-02 13:42:44 -07004360 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004361 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004362 fl->fl_u.nfs4_fl.owner,
4363 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004364 if (data == NULL)
4365 return -ENOMEM;
4366 if (IS_SETLKW(cmd))
4367 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004368 if (recovery_type > NFS_LOCK_NEW) {
4369 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004370 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004371 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4372 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004373 msg.rpc_argp = &data->arg;
4374 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004375 task_setup_data.callback_data = data;
4376 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004377 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 ret = nfs4_wait_for_completion_rpc_task(task);
4380 if (ret == 0) {
4381 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004382 if (ret)
4383 nfs4_handle_setlk_error(data->server, data->lsp,
4384 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385 } else
4386 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004387 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004388 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
4392static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4393{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004394 struct nfs_server *server = NFS_SERVER(state->inode);
4395 struct nfs4_exception exception = { };
4396 int err;
4397
4398 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004399 /* Cache the lock if possible... */
4400 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4401 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004402 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004403 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004404 break;
4405 nfs4_handle_exception(server, err, &exception);
4406 } while (exception.retry);
4407 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408}
4409
4410static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4411{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004412 struct nfs_server *server = NFS_SERVER(state->inode);
4413 struct nfs4_exception exception = { };
4414 int err;
4415
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004416 err = nfs4_set_lock_state(state, request);
4417 if (err != 0)
4418 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004419 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004420 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4421 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004422 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004423 switch (err) {
4424 default:
4425 goto out;
4426 case -NFS4ERR_GRACE:
4427 case -NFS4ERR_DELAY:
4428 nfs4_handle_exception(server, err, &exception);
4429 err = 0;
4430 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004431 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004432out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
4436static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4437{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004438 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004439 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004440 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004442 if ((fl_flags & FL_POSIX) &&
4443 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4444 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004445 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004446 status = nfs4_set_lock_state(state, request);
4447 if (status != 0)
4448 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004449 request->fl_flags |= FL_ACCESS;
4450 status = do_vfs_lock(request->fl_file, request);
4451 if (status < 0)
4452 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004453 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004454 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004455 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004456 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004457 request->fl_flags = fl_flags & ~FL_SLEEP;
4458 status = do_vfs_lock(request->fl_file, request);
4459 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004460 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004461 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004462 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004463 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004464 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004465 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004466 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004467 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004468out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004469 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004470out:
4471 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return status;
4473}
4474
4475static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4476{
4477 struct nfs4_exception exception = { };
4478 int err;
4479
4480 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004481 err = _nfs4_proc_setlk(state, cmd, request);
4482 if (err == -NFS4ERR_DENIED)
4483 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004485 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 } while (exception.retry);
4487 return err;
4488}
4489
4490static int
4491nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4492{
4493 struct nfs_open_context *ctx;
4494 struct nfs4_state *state;
4495 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4496 int status;
4497
4498 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004499 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 state = ctx->state;
4501
4502 if (request->fl_start < 0 || request->fl_end < 0)
4503 return -EINVAL;
4504
Trond Myklebustd9531262009-07-21 19:22:38 -04004505 if (IS_GETLK(cmd)) {
4506 if (state != NULL)
4507 return nfs4_proc_getlk(state, F_GETLK, request);
4508 return 0;
4509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4512 return -EINVAL;
4513
Trond Myklebustd9531262009-07-21 19:22:38 -04004514 if (request->fl_type == F_UNLCK) {
4515 if (state != NULL)
4516 return nfs4_proc_unlck(state, cmd, request);
4517 return 0;
4518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Trond Myklebustd9531262009-07-21 19:22:38 -04004520 if (state == NULL)
4521 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 do {
4523 status = nfs4_proc_setlk(state, cmd, request);
4524 if ((status != -EAGAIN) || IS_SETLK(cmd))
4525 break;
4526 timeout = nfs4_set_lock_task_retry(timeout);
4527 status = -ERESTARTSYS;
4528 if (signalled())
4529 break;
4530 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return status;
4532}
4533
Trond Myklebust888e6942005-11-04 15:38:11 -05004534int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4535{
4536 struct nfs_server *server = NFS_SERVER(state->inode);
4537 struct nfs4_exception exception = { };
4538 int err;
4539
4540 err = nfs4_set_lock_state(state, fl);
4541 if (err != 0)
4542 goto out;
4543 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004544 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004545 switch (err) {
4546 default:
4547 printk(KERN_ERR "%s: unhandled error %d.\n",
4548 __func__, err);
4549 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004550 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004551 goto out;
4552 case -NFS4ERR_EXPIRED:
4553 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004554 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004555 nfs4_schedule_lease_recovery(server->nfs_client);
4556 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004557 case -NFS4ERR_BADSESSION:
4558 case -NFS4ERR_BADSLOT:
4559 case -NFS4ERR_BAD_HIGH_SLOT:
4560 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4561 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004562 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004563 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004564 case -ERESTARTSYS:
4565 /*
4566 * The show must go on: exit, but mark the
4567 * stateid as needing recovery.
4568 */
4569 case -NFS4ERR_ADMIN_REVOKED:
4570 case -NFS4ERR_BAD_STATEID:
4571 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004572 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004573 err = 0;
4574 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004575 case -EKEYEXPIRED:
4576 /*
4577 * User RPCSEC_GSS context has expired.
4578 * We cannot recover this stateid now, so
4579 * skip it and allow recovery thread to
4580 * proceed.
4581 */
4582 err = 0;
4583 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004584 case -ENOMEM:
4585 case -NFS4ERR_DENIED:
4586 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4587 err = 0;
4588 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004589 case -NFS4ERR_DELAY:
4590 break;
4591 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004592 err = nfs4_handle_exception(server, err, &exception);
4593 } while (exception.retry);
4594out:
4595 return err;
4596}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004597
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004598static void nfs4_release_lockowner_release(void *calldata)
4599{
4600 kfree(calldata);
4601}
4602
4603const struct rpc_call_ops nfs4_release_lockowner_ops = {
4604 .rpc_release = nfs4_release_lockowner_release,
4605};
4606
4607void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4608{
4609 struct nfs_server *server = lsp->ls_state->owner->so_server;
4610 struct nfs_release_lockowner_args *args;
4611 struct rpc_message msg = {
4612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4613 };
4614
4615 if (server->nfs_client->cl_mvops->minor_version != 0)
4616 return;
4617 args = kmalloc(sizeof(*args), GFP_NOFS);
4618 if (!args)
4619 return;
4620 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4621 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004622 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004623 msg.rpc_argp = args;
4624 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4625}
4626
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004627#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4628
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004629static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4630 const void *buf, size_t buflen,
4631 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004632{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004633 if (strcmp(key, "") != 0)
4634 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004635
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004636 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004637}
4638
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004639static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4640 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004641{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004642 if (strcmp(key, "") != 0)
4643 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004644
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004645 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004646}
4647
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004648static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4649 size_t list_len, const char *name,
4650 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004651{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004652 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004653
J. Bruce Fields096455a2006-03-20 23:23:42 -05004654 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4655 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004656
4657 if (list && len <= list_len)
4658 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004659 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004660}
4661
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004662static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4663{
4664 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4665 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4666 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4667 return;
4668
4669 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4670 NFS_ATTR_FATTR_NLINK;
4671 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4672 fattr->nlink = 2;
4673}
4674
Trond Myklebust56659e92007-07-17 21:52:39 -04004675int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004676 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004677{
4678 struct nfs_server *server = NFS_SERVER(dir);
4679 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004680 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4681 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004682 };
4683 struct nfs4_fs_locations_arg args = {
4684 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004685 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004686 .page = page,
4687 .bitmask = bitmask,
4688 };
Benny Halevy22958462009-04-01 09:22:02 -04004689 struct nfs4_fs_locations_res res = {
4690 .fs_locations = fs_locations,
4691 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004692 struct rpc_message msg = {
4693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4694 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004695 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004696 };
4697 int status;
4698
Harvey Harrison3110ff82008-05-02 13:42:44 -07004699 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004700 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004701 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004702 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004703 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004704 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004705 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004706 return status;
4707}
4708
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004709static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4710{
4711 int status;
4712 struct nfs4_secinfo_arg args = {
4713 .dir_fh = NFS_FH(dir),
4714 .name = name,
4715 };
4716 struct nfs4_secinfo_res res = {
4717 .flavors = flavors,
4718 };
4719 struct rpc_message msg = {
4720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4721 .rpc_argp = &args,
4722 .rpc_resp = &res,
4723 };
4724
4725 dprintk("NFS call secinfo %s\n", name->name);
4726 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4727 dprintk("NFS reply secinfo: %d\n", status);
4728 return status;
4729}
4730
4731int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4732{
4733 struct nfs4_exception exception = { };
4734 int err;
4735 do {
4736 err = nfs4_handle_exception(NFS_SERVER(dir),
4737 _nfs4_proc_secinfo(dir, name, flavors),
4738 &exception);
4739 } while (exception.retry);
4740 return err;
4741}
4742
Andy Adamson557134a2009-04-01 09:21:53 -04004743#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004744/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004745 * Check the exchange flags returned by the server for invalid flags, having
4746 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4747 * DS flags set.
4748 */
4749static int nfs4_check_cl_exchange_flags(u32 flags)
4750{
4751 if (flags & ~EXCHGID4_FLAG_MASK_R)
4752 goto out_inval;
4753 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4754 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4755 goto out_inval;
4756 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4757 goto out_inval;
4758 return NFS_OK;
4759out_inval:
4760 return -NFS4ERR_INVAL;
4761}
4762
4763/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004764 * nfs4_proc_exchange_id()
4765 *
4766 * Since the clientid has expired, all compounds using sessions
4767 * associated with the stale clientid will be returning
4768 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4769 * be in some phase of session reset.
4770 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004771int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004772{
4773 nfs4_verifier verifier;
4774 struct nfs41_exchange_id_args args = {
4775 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004776 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004777 };
4778 struct nfs41_exchange_id_res res = {
4779 .client = clp,
4780 };
4781 int status;
4782 struct rpc_message msg = {
4783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4784 .rpc_argp = &args,
4785 .rpc_resp = &res,
4786 .rpc_cred = cred,
4787 };
4788 __be32 *p;
4789
4790 dprintk("--> %s\n", __func__);
4791 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004792
Benny Halevy99fe60d2009-04-01 09:22:29 -04004793 p = (u32 *)verifier.data;
4794 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4795 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4796 args.verifier = &verifier;
4797
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004798 args.id_len = scnprintf(args.id, sizeof(args.id),
4799 "%s/%s.%s/%u",
4800 clp->cl_ipaddr,
4801 init_utsname()->nodename,
4802 init_utsname()->domainname,
4803 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004804
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004805 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004806 if (!status)
4807 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004808 dprintk("<-- %s status= %d\n", __func__, status);
4809 return status;
4810}
4811
Andy Adamson2050f0c2009-04-01 09:22:30 -04004812struct nfs4_get_lease_time_data {
4813 struct nfs4_get_lease_time_args *args;
4814 struct nfs4_get_lease_time_res *res;
4815 struct nfs_client *clp;
4816};
4817
4818static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4819 void *calldata)
4820{
4821 int ret;
4822 struct nfs4_get_lease_time_data *data =
4823 (struct nfs4_get_lease_time_data *)calldata;
4824
4825 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004826 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004827 /* just setup sequence, do not trigger session recovery
4828 since we're invoked within one */
4829 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004830 &data->args->la_seq_args,
4831 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004832
4833 BUG_ON(ret == -EAGAIN);
4834 rpc_call_start(task);
4835 dprintk("<-- %s\n", __func__);
4836}
4837
4838/*
4839 * Called from nfs4_state_manager thread for session setup, so don't recover
4840 * from sequence operation or clientid errors.
4841 */
4842static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4843{
4844 struct nfs4_get_lease_time_data *data =
4845 (struct nfs4_get_lease_time_data *)calldata;
4846
4847 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004848 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4849 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004850 switch (task->tk_status) {
4851 case -NFS4ERR_DELAY:
4852 case -NFS4ERR_GRACE:
4853 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4854 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4855 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004856 /* fall through */
4857 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004858 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004859 return;
4860 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004861 dprintk("<-- %s\n", __func__);
4862}
4863
4864struct rpc_call_ops nfs4_get_lease_time_ops = {
4865 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4866 .rpc_call_done = nfs4_get_lease_time_done,
4867};
4868
4869int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4870{
4871 struct rpc_task *task;
4872 struct nfs4_get_lease_time_args args;
4873 struct nfs4_get_lease_time_res res = {
4874 .lr_fsinfo = fsinfo,
4875 };
4876 struct nfs4_get_lease_time_data data = {
4877 .args = &args,
4878 .res = &res,
4879 .clp = clp,
4880 };
4881 struct rpc_message msg = {
4882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4883 .rpc_argp = &args,
4884 .rpc_resp = &res,
4885 };
4886 struct rpc_task_setup task_setup = {
4887 .rpc_client = clp->cl_rpcclient,
4888 .rpc_message = &msg,
4889 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004890 .callback_data = &data,
4891 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004892 };
4893 int status;
4894
Andy Adamson2050f0c2009-04-01 09:22:30 -04004895 dprintk("--> %s\n", __func__);
4896 task = rpc_run_task(&task_setup);
4897
4898 if (IS_ERR(task))
4899 status = PTR_ERR(task);
4900 else {
4901 status = task->tk_status;
4902 rpc_put_task(task);
4903 }
4904 dprintk("<-- %s return %d\n", __func__, status);
4905
4906 return status;
4907}
4908
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004909/*
4910 * Reset a slot table
4911 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004912static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4913 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004914{
Andy Adamson104aeba2010-01-14 17:45:10 -05004915 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004916 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004917 int ret = 0;
4918
Andy Adamson104aeba2010-01-14 17:45:10 -05004919 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4920 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004921
Andy Adamson104aeba2010-01-14 17:45:10 -05004922 /* Does the newly negotiated max_reqs match the existing slot table? */
4923 if (max_reqs != tbl->max_slots) {
4924 ret = -ENOMEM;
4925 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004926 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004927 if (!new)
4928 goto out;
4929 ret = 0;
4930 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004931 }
4932 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004933 if (new) {
4934 tbl->slots = new;
4935 tbl->max_slots = max_reqs;
4936 }
4937 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004938 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004939 spin_unlock(&tbl->slot_tbl_lock);
4940 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4941 tbl, tbl->slots, tbl->max_slots);
4942out:
4943 dprintk("<-- %s: return %d\n", __func__, ret);
4944 return ret;
4945}
4946
Andy Adamsonfc931582009-04-01 09:22:31 -04004947/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004948 * Reset the forechannel and backchannel slot tables
4949 */
4950static int nfs4_reset_slot_tables(struct nfs4_session *session)
4951{
4952 int status;
4953
4954 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004955 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004956 if (status)
4957 return status;
4958
4959 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004960 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004961 return status;
4962}
4963
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004964/* Destroy the slot table */
4965static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4966{
4967 if (session->fc_slot_table.slots != NULL) {
4968 kfree(session->fc_slot_table.slots);
4969 session->fc_slot_table.slots = NULL;
4970 }
4971 if (session->bc_slot_table.slots != NULL) {
4972 kfree(session->bc_slot_table.slots);
4973 session->bc_slot_table.slots = NULL;
4974 }
4975 return;
4976}
4977
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004978/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004979 * Initialize slot table
4980 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004981static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4982 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004983{
Andy Adamsonfc931582009-04-01 09:22:31 -04004984 struct nfs4_slot *slot;
4985 int ret = -ENOMEM;
4986
4987 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4988
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004989 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004990
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004991 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004992 if (!slot)
4993 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004994 ret = 0;
4995
4996 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004997 tbl->max_slots = max_slots;
4998 tbl->slots = slot;
4999 tbl->highest_used_slotid = -1; /* no slot is currently used */
5000 spin_unlock(&tbl->slot_tbl_lock);
5001 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5002 tbl, tbl->slots, tbl->max_slots);
5003out:
5004 dprintk("<-- %s: return %d\n", __func__, ret);
5005 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005006}
5007
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005008/*
5009 * Initialize the forechannel and backchannel tables
5010 */
5011static int nfs4_init_slot_tables(struct nfs4_session *session)
5012{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005013 struct nfs4_slot_table *tbl;
5014 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005015
Trond Myklebustf26468f2009-12-05 19:32:11 -05005016 tbl = &session->fc_slot_table;
5017 if (tbl->slots == NULL) {
5018 status = nfs4_init_slot_table(tbl,
5019 session->fc_attrs.max_reqs, 1);
5020 if (status)
5021 return status;
5022 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005023
Trond Myklebustf26468f2009-12-05 19:32:11 -05005024 tbl = &session->bc_slot_table;
5025 if (tbl->slots == NULL) {
5026 status = nfs4_init_slot_table(tbl,
5027 session->bc_attrs.max_reqs, 0);
5028 if (status)
5029 nfs4_destroy_slot_tables(session);
5030 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005031
5032 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005033}
5034
5035struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5036{
5037 struct nfs4_session *session;
5038 struct nfs4_slot_table *tbl;
5039
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005040 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005041 if (!session)
5042 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005043
Andy Adamson557134a2009-04-01 09:21:53 -04005044 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005045 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005046 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005047 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005048 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005049
5050 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005051 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005052 spin_lock_init(&tbl->slot_tbl_lock);
5053 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005054 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005055
Trond Myklebust1055d762010-06-16 09:52:27 -04005056 session->session_state = 1<<NFS4_SESSION_INITING;
5057
Andy Adamson557134a2009-04-01 09:21:53 -04005058 session->clp = clp;
5059 return session;
5060}
5061
5062void nfs4_destroy_session(struct nfs4_session *session)
5063{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005064 nfs4_proc_destroy_session(session);
5065 dprintk("%s Destroy backchannel for xprt %p\n",
5066 __func__, session->clp->cl_rpcclient->cl_xprt);
5067 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5068 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005069 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005070 kfree(session);
5071}
5072
Andy Adamsonfc931582009-04-01 09:22:31 -04005073/*
5074 * Initialize the values to be used by the client in CREATE_SESSION
5075 * If nfs4_init_session set the fore channel request and response sizes,
5076 * use them.
5077 *
5078 * Set the back channel max_resp_sz_cached to zero to force the client to
5079 * always set csa_cachethis to FALSE because the current implementation
5080 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5081 */
5082static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5083{
5084 struct nfs4_session *session = args->client->cl_session;
5085 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5086 mxresp_sz = session->fc_attrs.max_resp_sz;
5087
5088 if (mxrqst_sz == 0)
5089 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5090 if (mxresp_sz == 0)
5091 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5092 /* Fore channel attributes */
5093 args->fc_attrs.headerpadsz = 0;
5094 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5095 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005096 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5097 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5098
5099 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005100 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005101 __func__,
5102 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005103 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005104
5105 /* Back channel attributes */
5106 args->bc_attrs.headerpadsz = 0;
5107 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5108 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5109 args->bc_attrs.max_resp_sz_cached = 0;
5110 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5111 args->bc_attrs.max_reqs = 1;
5112
5113 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5114 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5115 __func__,
5116 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5117 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5118 args->bc_attrs.max_reqs);
5119}
5120
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005121static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005122{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005123 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5124 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5125
5126 if (rcvd->headerpadsz > sent->headerpadsz)
5127 return -EINVAL;
5128 if (rcvd->max_resp_sz > sent->max_resp_sz)
5129 return -EINVAL;
5130 /*
5131 * Our requested max_ops is the minimum we need; we're not
5132 * prepared to break up compounds into smaller pieces than that.
5133 * So, no point even trying to continue if the server won't
5134 * cooperate:
5135 */
5136 if (rcvd->max_ops < sent->max_ops)
5137 return -EINVAL;
5138 if (rcvd->max_reqs == 0)
5139 return -EINVAL;
5140 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005141}
5142
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005143static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5144{
5145 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5146 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005147
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005148 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5149 return -EINVAL;
5150 if (rcvd->max_resp_sz < sent->max_resp_sz)
5151 return -EINVAL;
5152 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5153 return -EINVAL;
5154 /* These would render the backchannel useless: */
5155 if (rcvd->max_ops == 0)
5156 return -EINVAL;
5157 if (rcvd->max_reqs == 0)
5158 return -EINVAL;
5159 return 0;
5160}
Andy Adamson8d353012009-04-01 09:22:32 -04005161
Andy Adamson8d353012009-04-01 09:22:32 -04005162static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5163 struct nfs4_session *session)
5164{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005165 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005166
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005167 ret = nfs4_verify_fore_channel_attrs(args, session);
5168 if (ret)
5169 return ret;
5170 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005171}
5172
Andy Adamsonfc931582009-04-01 09:22:31 -04005173static int _nfs4_proc_create_session(struct nfs_client *clp)
5174{
5175 struct nfs4_session *session = clp->cl_session;
5176 struct nfs41_create_session_args args = {
5177 .client = clp,
5178 .cb_program = NFS4_CALLBACK,
5179 };
5180 struct nfs41_create_session_res res = {
5181 .client = clp,
5182 };
5183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5185 .rpc_argp = &args,
5186 .rpc_resp = &res,
5187 };
5188 int status;
5189
5190 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005191 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005192
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005193 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005194
Andy Adamson8d353012009-04-01 09:22:32 -04005195 if (!status)
5196 /* Verify the session's negotiated channel_attrs values */
5197 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005198 if (!status) {
5199 /* Increment the clientid slot sequence id */
5200 clp->cl_seqid++;
5201 }
5202
5203 return status;
5204}
5205
5206/*
5207 * Issues a CREATE_SESSION operation to the server.
5208 * It is the responsibility of the caller to verify the session is
5209 * expired before calling this routine.
5210 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005211int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005212{
5213 int status;
5214 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005215 struct nfs4_session *session = clp->cl_session;
5216
5217 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5218
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005219 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005220 if (status)
5221 goto out;
5222
Trond Myklebustf26468f2009-12-05 19:32:11 -05005223 /* Init and reset the fore channel */
5224 status = nfs4_init_slot_tables(session);
5225 dprintk("slot table initialization returned %d\n", status);
5226 if (status)
5227 goto out;
5228 status = nfs4_reset_slot_tables(session);
5229 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005230 if (status)
5231 goto out;
5232
5233 ptr = (unsigned *)&session->sess_id.data[0];
5234 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5235 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005236out:
5237 dprintk("<-- %s\n", __func__);
5238 return status;
5239}
5240
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005241/*
5242 * Issue the over-the-wire RPC DESTROY_SESSION.
5243 * The caller must serialize access to this routine.
5244 */
5245int nfs4_proc_destroy_session(struct nfs4_session *session)
5246{
5247 int status = 0;
5248 struct rpc_message msg;
5249
5250 dprintk("--> nfs4_proc_destroy_session\n");
5251
5252 /* session is still being setup */
5253 if (session->clp->cl_cons_state != NFS_CS_READY)
5254 return status;
5255
5256 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5257 msg.rpc_argp = session;
5258 msg.rpc_resp = NULL;
5259 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005260 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005261
5262 if (status)
5263 printk(KERN_WARNING
5264 "Got error %d from the server on DESTROY_SESSION. "
5265 "Session has been destroyed regardless...\n", status);
5266
5267 dprintk("<-- nfs4_proc_destroy_session\n");
5268 return status;
5269}
5270
Trond Myklebustfccba802009-07-21 16:48:07 -04005271int nfs4_init_session(struct nfs_server *server)
5272{
5273 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005274 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005275 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005276 int ret;
5277
5278 if (!nfs4_has_session(clp))
5279 return 0;
5280
Trond Myklebust1055d762010-06-16 09:52:27 -04005281 session = clp->cl_session;
5282 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5283 return 0;
5284
Andy Adamson68bf05e2009-12-15 12:55:02 -05005285 rsize = server->rsize;
5286 if (rsize == 0)
5287 rsize = NFS_MAX_FILE_IO_SIZE;
5288 wsize = server->wsize;
5289 if (wsize == 0)
5290 wsize = NFS_MAX_FILE_IO_SIZE;
5291
Andy Adamson68bf05e2009-12-15 12:55:02 -05005292 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5293 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005294
Trond Myklebustfccba802009-07-21 16:48:07 -04005295 ret = nfs4_recover_expired_lease(server);
5296 if (!ret)
5297 ret = nfs4_check_client_ready(clp);
5298 return ret;
5299}
5300
Andy Adamsond83217c2011-03-01 01:34:17 +00005301int nfs4_init_ds_session(struct nfs_client *clp)
5302{
5303 struct nfs4_session *session = clp->cl_session;
5304 int ret;
5305
5306 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5307 return 0;
5308
5309 ret = nfs4_client_recover_expired_lease(clp);
5310 if (!ret)
5311 /* Test for the DS role */
5312 if (!is_ds_client(clp))
5313 ret = -ENODEV;
5314 if (!ret)
5315 ret = nfs4_check_client_ready(clp);
5316 return ret;
5317
5318}
5319EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5320
5321
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005322/*
5323 * Renew the cl_session lease.
5324 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005325struct nfs4_sequence_data {
5326 struct nfs_client *clp;
5327 struct nfs4_sequence_args args;
5328 struct nfs4_sequence_res res;
5329};
5330
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005331static void nfs41_sequence_release(void *data)
5332{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005333 struct nfs4_sequence_data *calldata = data;
5334 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005335
Alexandros Batsakis71358402010-02-05 03:45:05 -08005336 if (atomic_read(&clp->cl_count) > 1)
5337 nfs4_schedule_state_renewal(clp);
5338 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005339 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005340}
5341
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005342static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5343{
5344 switch(task->tk_status) {
5345 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005346 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5347 return -EAGAIN;
5348 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005349 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005350 }
5351 return 0;
5352}
5353
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005354static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005355{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005356 struct nfs4_sequence_data *calldata = data;
5357 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005358
Trond Myklebust14516c32010-07-31 14:29:06 -04005359 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5360 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005361
5362 if (task->tk_status < 0) {
5363 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005364 if (atomic_read(&clp->cl_count) == 1)
5365 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005366
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005367 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5368 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005369 return;
5370 }
5371 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005372 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005373out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005374 dprintk("<-- %s\n", __func__);
5375}
5376
5377static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5378{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005379 struct nfs4_sequence_data *calldata = data;
5380 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005381 struct nfs4_sequence_args *args;
5382 struct nfs4_sequence_res *res;
5383
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005384 args = task->tk_msg.rpc_argp;
5385 res = task->tk_msg.rpc_resp;
5386
Trond Myklebust035168a2010-06-16 09:52:26 -04005387 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005388 return;
5389 rpc_call_start(task);
5390}
5391
5392static const struct rpc_call_ops nfs41_sequence_ops = {
5393 .rpc_call_done = nfs41_sequence_call_done,
5394 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005395 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005396};
5397
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005398static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005399{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005400 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005401 struct rpc_message msg = {
5402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5403 .rpc_cred = cred,
5404 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005405 struct rpc_task_setup task_setup_data = {
5406 .rpc_client = clp->cl_rpcclient,
5407 .rpc_message = &msg,
5408 .callback_ops = &nfs41_sequence_ops,
5409 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5410 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005411
Alexandros Batsakis71358402010-02-05 03:45:05 -08005412 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005413 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005414 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005415 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005416 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005417 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005418 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005419 msg.rpc_argp = &calldata->args;
5420 msg.rpc_resp = &calldata->res;
5421 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005422 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005423
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005424 return rpc_run_task(&task_setup_data);
5425}
5426
5427static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5428{
5429 struct rpc_task *task;
5430 int ret = 0;
5431
5432 task = _nfs41_proc_sequence(clp, cred);
5433 if (IS_ERR(task))
5434 ret = PTR_ERR(task);
5435 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005436 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005437 dprintk("<-- %s status=%d\n", __func__, ret);
5438 return ret;
5439}
5440
5441static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5442{
5443 struct rpc_task *task;
5444 int ret;
5445
5446 task = _nfs41_proc_sequence(clp, cred);
5447 if (IS_ERR(task)) {
5448 ret = PTR_ERR(task);
5449 goto out;
5450 }
5451 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005452 if (!ret) {
5453 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5454
5455 if (task->tk_status == 0)
5456 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005457 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005458 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005459 rpc_put_task(task);
5460out:
5461 dprintk("<-- %s status=%d\n", __func__, ret);
5462 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005463}
5464
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005465struct nfs4_reclaim_complete_data {
5466 struct nfs_client *clp;
5467 struct nfs41_reclaim_complete_args arg;
5468 struct nfs41_reclaim_complete_res res;
5469};
5470
5471static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5472{
5473 struct nfs4_reclaim_complete_data *calldata = data;
5474
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005475 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005476 if (nfs41_setup_sequence(calldata->clp->cl_session,
5477 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005478 &calldata->res.seq_res, 0, task))
5479 return;
5480
5481 rpc_call_start(task);
5482}
5483
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005484static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5485{
5486 switch(task->tk_status) {
5487 case 0:
5488 case -NFS4ERR_COMPLETE_ALREADY:
5489 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5490 break;
5491 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005493 /* fall through */
5494 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005495 return -EAGAIN;
5496 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005497 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005498 }
5499 return 0;
5500}
5501
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005502static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5503{
5504 struct nfs4_reclaim_complete_data *calldata = data;
5505 struct nfs_client *clp = calldata->clp;
5506 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5507
5508 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005509 if (!nfs41_sequence_done(task, res))
5510 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005511
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005512 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5513 rpc_restart_call_prepare(task);
5514 return;
5515 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005516 dprintk("<-- %s\n", __func__);
5517}
5518
5519static void nfs4_free_reclaim_complete_data(void *data)
5520{
5521 struct nfs4_reclaim_complete_data *calldata = data;
5522
5523 kfree(calldata);
5524}
5525
5526static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5527 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5528 .rpc_call_done = nfs4_reclaim_complete_done,
5529 .rpc_release = nfs4_free_reclaim_complete_data,
5530};
5531
5532/*
5533 * Issue a global reclaim complete.
5534 */
5535static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5536{
5537 struct nfs4_reclaim_complete_data *calldata;
5538 struct rpc_task *task;
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5541 };
5542 struct rpc_task_setup task_setup_data = {
5543 .rpc_client = clp->cl_rpcclient,
5544 .rpc_message = &msg,
5545 .callback_ops = &nfs4_reclaim_complete_call_ops,
5546 .flags = RPC_TASK_ASYNC,
5547 };
5548 int status = -ENOMEM;
5549
5550 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005551 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005552 if (calldata == NULL)
5553 goto out;
5554 calldata->clp = clp;
5555 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005556
5557 msg.rpc_argp = &calldata->arg;
5558 msg.rpc_resp = &calldata->res;
5559 task_setup_data.callback_data = calldata;
5560 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005561 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005562 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005563 goto out;
5564 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005565 status = nfs4_wait_for_completion_rpc_task(task);
5566 if (status == 0)
5567 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005568 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005569 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005570out:
5571 dprintk("<-- %s status=%d\n", __func__, status);
5572 return status;
5573}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005574
5575static void
5576nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5577{
5578 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005579 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005580
5581 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005582 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5583 * right now covering the LAYOUTGET we are about to send.
5584 * However, that is not so catastrophic, and there seems
5585 * to be no way to prevent it completely.
5586 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005587 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5588 &lgp->res.seq_res, 0, task))
5589 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005590 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5591 NFS_I(lgp->args.inode)->layout,
5592 lgp->args.ctx->state)) {
5593 rpc_exit(task, NFS4_OK);
5594 return;
5595 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005596 rpc_call_start(task);
5597}
5598
5599static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5600{
5601 struct nfs4_layoutget *lgp = calldata;
5602 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5603
5604 dprintk("--> %s\n", __func__);
5605
5606 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5607 return;
5608
5609 switch (task->tk_status) {
5610 case 0:
5611 break;
5612 case -NFS4ERR_LAYOUTTRYLATER:
5613 case -NFS4ERR_RECALLCONFLICT:
5614 task->tk_status = -NFS4ERR_DELAY;
5615 /* Fall through */
5616 default:
5617 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5618 rpc_restart_call_prepare(task);
5619 return;
5620 }
5621 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005622 dprintk("<-- %s\n", __func__);
5623}
5624
5625static void nfs4_layoutget_release(void *calldata)
5626{
5627 struct nfs4_layoutget *lgp = calldata;
5628
5629 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005630 put_nfs_open_context(lgp->args.ctx);
5631 kfree(calldata);
5632 dprintk("<-- %s\n", __func__);
5633}
5634
5635static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5636 .rpc_call_prepare = nfs4_layoutget_prepare,
5637 .rpc_call_done = nfs4_layoutget_done,
5638 .rpc_release = nfs4_layoutget_release,
5639};
5640
5641int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5642{
5643 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5644 struct rpc_task *task;
5645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5647 .rpc_argp = &lgp->args,
5648 .rpc_resp = &lgp->res,
5649 };
5650 struct rpc_task_setup task_setup_data = {
5651 .rpc_client = server->client,
5652 .rpc_message = &msg,
5653 .callback_ops = &nfs4_layoutget_call_ops,
5654 .callback_data = lgp,
5655 .flags = RPC_TASK_ASYNC,
5656 };
5657 int status = 0;
5658
5659 dprintk("--> %s\n", __func__);
5660
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005661 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005662 lgp->res.seq_res.sr_slot = NULL;
5663 task = rpc_run_task(&task_setup_data);
5664 if (IS_ERR(task))
5665 return PTR_ERR(task);
5666 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005667 if (status == 0)
5668 status = task->tk_status;
5669 if (status == 0)
5670 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005671 rpc_put_task(task);
5672 dprintk("<-- %s status=%d\n", __func__, status);
5673 return status;
5674}
5675
Benny Halevycbe82602011-05-22 19:52:37 +03005676static void
5677nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5678{
5679 struct nfs4_layoutreturn *lrp = calldata;
5680
5681 dprintk("--> %s\n", __func__);
5682 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5683 &lrp->res.seq_res, 0, task))
5684 return;
5685 rpc_call_start(task);
5686}
5687
5688static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5689{
5690 struct nfs4_layoutreturn *lrp = calldata;
5691 struct nfs_server *server;
5692
5693 dprintk("--> %s\n", __func__);
5694
5695 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5696 return;
5697
5698 server = NFS_SERVER(lrp->args.inode);
5699 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5700 nfs_restart_rpc(task, lrp->clp);
5701 return;
5702 }
5703 if (task->tk_status == 0) {
5704 struct pnfs_layout_hdr *lo = NFS_I(lrp->args.inode)->layout;
5705
5706 if (lrp->res.lrs_present) {
5707 spin_lock(&lo->plh_inode->i_lock);
5708 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5709 spin_unlock(&lo->plh_inode->i_lock);
5710 } else
5711 BUG_ON(!list_empty(&lo->plh_segs));
5712 }
5713 dprintk("<-- %s\n", __func__);
5714}
5715
5716static void nfs4_layoutreturn_release(void *calldata)
5717{
5718 struct nfs4_layoutreturn *lrp = calldata;
5719
5720 dprintk("--> %s\n", __func__);
5721 put_layout_hdr(NFS_I(lrp->args.inode)->layout);
5722 kfree(calldata);
5723 dprintk("<-- %s\n", __func__);
5724}
5725
5726static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5727 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5728 .rpc_call_done = nfs4_layoutreturn_done,
5729 .rpc_release = nfs4_layoutreturn_release,
5730};
5731
5732int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5733{
5734 struct rpc_task *task;
5735 struct rpc_message msg = {
5736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5737 .rpc_argp = &lrp->args,
5738 .rpc_resp = &lrp->res,
5739 };
5740 struct rpc_task_setup task_setup_data = {
5741 .rpc_client = lrp->clp->cl_rpcclient,
5742 .rpc_message = &msg,
5743 .callback_ops = &nfs4_layoutreturn_call_ops,
5744 .callback_data = lrp,
5745 };
5746 int status;
5747
5748 dprintk("--> %s\n", __func__);
5749 task = rpc_run_task(&task_setup_data);
5750 if (IS_ERR(task))
5751 return PTR_ERR(task);
5752 status = task->tk_status;
5753 dprintk("<-- %s status=%d\n", __func__, status);
5754 rpc_put_task(task);
5755 return status;
5756}
5757
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005758static int
5759_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5760{
5761 struct nfs4_getdeviceinfo_args args = {
5762 .pdev = pdev,
5763 };
5764 struct nfs4_getdeviceinfo_res res = {
5765 .pdev = pdev,
5766 };
5767 struct rpc_message msg = {
5768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5769 .rpc_argp = &args,
5770 .rpc_resp = &res,
5771 };
5772 int status;
5773
5774 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005775 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005776 dprintk("<-- %s status=%d\n", __func__, status);
5777
5778 return status;
5779}
5780
5781int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5782{
5783 struct nfs4_exception exception = { };
5784 int err;
5785
5786 do {
5787 err = nfs4_handle_exception(server,
5788 _nfs4_proc_getdeviceinfo(server, pdev),
5789 &exception);
5790 } while (exception.retry);
5791 return err;
5792}
5793EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5794
Andy Adamson863a3c62011-03-23 13:27:54 +00005795static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5796{
5797 struct nfs4_layoutcommit_data *data = calldata;
5798 struct nfs_server *server = NFS_SERVER(data->args.inode);
5799
5800 if (nfs4_setup_sequence(server, &data->args.seq_args,
5801 &data->res.seq_res, 1, task))
5802 return;
5803 rpc_call_start(task);
5804}
5805
5806static void
5807nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5808{
5809 struct nfs4_layoutcommit_data *data = calldata;
5810 struct nfs_server *server = NFS_SERVER(data->args.inode);
5811
5812 if (!nfs4_sequence_done(task, &data->res.seq_res))
5813 return;
5814
5815 switch (task->tk_status) { /* Just ignore these failures */
5816 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5817 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5818 case NFS4ERR_BADLAYOUT: /* no layout */
5819 case NFS4ERR_GRACE: /* loca_recalim always false */
5820 task->tk_status = 0;
5821 }
5822
5823 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5824 nfs_restart_rpc(task, server->nfs_client);
5825 return;
5826 }
5827
5828 if (task->tk_status == 0)
5829 nfs_post_op_update_inode_force_wcc(data->args.inode,
5830 data->res.fattr);
5831}
5832
5833static void nfs4_layoutcommit_release(void *calldata)
5834{
5835 struct nfs4_layoutcommit_data *data = calldata;
5836
5837 /* Matched by references in pnfs_set_layoutcommit */
5838 put_lseg(data->lseg);
5839 put_rpccred(data->cred);
5840 kfree(data);
5841}
5842
5843static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5844 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5845 .rpc_call_done = nfs4_layoutcommit_done,
5846 .rpc_release = nfs4_layoutcommit_release,
5847};
5848
5849int
Andy Adamsonef311532011-03-12 02:58:10 -05005850nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005851{
5852 struct rpc_message msg = {
5853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5854 .rpc_argp = &data->args,
5855 .rpc_resp = &data->res,
5856 .rpc_cred = data->cred,
5857 };
5858 struct rpc_task_setup task_setup_data = {
5859 .task = &data->task,
5860 .rpc_client = NFS_CLIENT(data->args.inode),
5861 .rpc_message = &msg,
5862 .callback_ops = &nfs4_layoutcommit_ops,
5863 .callback_data = data,
5864 .flags = RPC_TASK_ASYNC,
5865 };
5866 struct rpc_task *task;
5867 int status = 0;
5868
5869 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5870 "lbw: %llu inode %lu\n",
5871 data->task.tk_pid, sync,
5872 data->args.lastbytewritten,
5873 data->args.inode->i_ino);
5874
5875 task = rpc_run_task(&task_setup_data);
5876 if (IS_ERR(task))
5877 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005878 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005879 goto out;
5880 status = nfs4_wait_for_completion_rpc_task(task);
5881 if (status != 0)
5882 goto out;
5883 status = task->tk_status;
5884out:
5885 dprintk("%s: status %d\n", __func__, status);
5886 rpc_put_task(task);
5887 return status;
5888}
Andy Adamson557134a2009-04-01 09:21:53 -04005889#endif /* CONFIG_NFS_V4_1 */
5890
Andy Adamson591d71c2009-04-01 09:22:47 -04005891struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005892 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005893 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 .recover_open = nfs4_open_reclaim,
5895 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005896 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005897 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898};
5899
Andy Adamson591d71c2009-04-01 09:22:47 -04005900#if defined(CONFIG_NFS_V4_1)
5901struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5902 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5903 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5904 .recover_open = nfs4_open_reclaim,
5905 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005906 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005907 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005908 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005909};
5910#endif /* CONFIG_NFS_V4_1 */
5911
5912struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005913 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005914 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 .recover_open = nfs4_open_expired,
5916 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005917 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005918 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919};
5920
Andy Adamson591d71c2009-04-01 09:22:47 -04005921#if defined(CONFIG_NFS_V4_1)
5922struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5923 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5924 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5925 .recover_open = nfs4_open_expired,
5926 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005927 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005928 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005929};
5930#endif /* CONFIG_NFS_V4_1 */
5931
Benny Halevy29fba382009-04-01 09:22:44 -04005932struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5933 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005934 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005935 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005936};
5937
5938#if defined(CONFIG_NFS_V4_1)
5939struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5940 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005941 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005942 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005943};
5944#endif
5945
Trond Myklebust97dc1352010-06-16 09:52:26 -04005946static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5947 .minor_version = 0,
5948 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005949 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005950 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5951 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5952 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005953};
5954
5955#if defined(CONFIG_NFS_V4_1)
5956static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5957 .minor_version = 1,
5958 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005959 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005960 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5961 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5962 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005963};
5964#endif
5965
Trond Myklebust97dc1352010-06-16 09:52:26 -04005966const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5967 [0] = &nfs_v4_0_minor_ops,
5968#if defined(CONFIG_NFS_V4_1)
5969 [1] = &nfs_v4_1_minor_ops,
5970#endif
5971};
5972
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005973static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005974 .permission = nfs_permission,
5975 .getattr = nfs_getattr,
5976 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005977 .getxattr = generic_getxattr,
5978 .setxattr = generic_setxattr,
5979 .listxattr = generic_listxattr,
5980 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005981};
5982
David Howells509de812006-08-22 20:06:11 -04005983const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 .version = 4, /* protocol version */
5985 .dentry_ops = &nfs4_dentry_operations,
5986 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005987 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 .getroot = nfs4_proc_get_root,
5989 .getattr = nfs4_proc_getattr,
5990 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005991 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 .lookup = nfs4_proc_lookup,
5993 .access = nfs4_proc_access,
5994 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 .create = nfs4_proc_create,
5996 .remove = nfs4_proc_remove,
5997 .unlink_setup = nfs4_proc_unlink_setup,
5998 .unlink_done = nfs4_proc_unlink_done,
5999 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006000 .rename_setup = nfs4_proc_rename_setup,
6001 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 .link = nfs4_proc_link,
6003 .symlink = nfs4_proc_symlink,
6004 .mkdir = nfs4_proc_mkdir,
6005 .rmdir = nfs4_proc_remove,
6006 .readdir = nfs4_proc_readdir,
6007 .mknod = nfs4_proc_mknod,
6008 .statfs = nfs4_proc_statfs,
6009 .fsinfo = nfs4_proc_fsinfo,
6010 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006011 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 .decode_dirent = nfs4_decode_dirent,
6013 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006014 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006016 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006018 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006020 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006021 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006022 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006023 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006024 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025};
6026
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006027static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6028 .prefix = XATTR_NAME_NFSV4_ACL,
6029 .list = nfs4_xattr_list_nfs4_acl,
6030 .get = nfs4_xattr_get_nfs4_acl,
6031 .set = nfs4_xattr_set_nfs4_acl,
6032};
6033
6034const struct xattr_handler *nfs4_xattr_handlers[] = {
6035 &nfs4_xattr_nfs4_acl_handler,
6036 NULL
6037};
6038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039/*
6040 * Local variables:
6041 * c-basic-offset: 8
6042 * End:
6043 */