~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nfs/nfs4proc.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  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>
 39 #include <linux/delay.h>
 40 #include <linux/errno.h>
 41 #include <linux/string.h>
 42 #include <linux/ratelimit.h>
 43 #include <linux/printk.h>
 44 #include <linux/slab.h>
 45 #include <linux/sunrpc/clnt.h>
 46 #include <linux/sunrpc/gss_api.h>
 47 #include <linux/nfs.h>
 48 #include <linux/nfs4.h>
 49 #include <linux/nfs_fs.h>
 50 #include <linux/nfs_page.h>
 51 #include <linux/nfs_mount.h>
 52 #include <linux/namei.h>
 53 #include <linux/mount.h>
 54 #include <linux/module.h>
 55 #include <linux/nfs_idmap.h>
 56 #include <linux/sunrpc/bc_xprt.h>
 57 #include <linux/xattr.h>
 58 #include <linux/utsname.h>
 59 #include <linux/freezer.h>
 60 
 61 #include "nfs4_fs.h"
 62 #include "delegation.h"
 63 #include "internal.h"
 64 #include "iostat.h"
 65 #include "callback.h"
 66 #include "pnfs.h"
 67 
 68 #define NFSDBG_FACILITY         NFSDBG_PROC
 69 
 70 #define NFS4_POLL_RETRY_MIN     (HZ/10)
 71 #define NFS4_POLL_RETRY_MAX     (15*HZ)
 72 
 73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
 74 
 75 static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
 76 
 77 struct nfs4_opendata;
 78 static int _nfs4_proc_open(struct nfs4_opendata *data);
 79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
 80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
 81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
 82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
 83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
 84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
 85                             struct nfs_fattr *fattr, struct iattr *sattr,
 86                             struct nfs4_state *state);
 87 #ifdef CONFIG_NFS_V4_1
 88 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
 89 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
 90 #endif
 91 /* Prevent leaks of NFSv4 errors into userland */
 92 static int nfs4_map_errors(int err)
 93 {
 94         if (err >= -1000)
 95                 return err;
 96         switch (err) {
 97         case -NFS4ERR_RESOURCE:
 98                 return -EREMOTEIO;
 99         case -NFS4ERR_WRONGSEC:
100                 return -EPERM;
101         case -NFS4ERR_BADOWNER:
102         case -NFS4ERR_BADNAME:
103                 return -EINVAL;
104         case -NFS4ERR_SHARE_DENIED:
105                 return -EACCES;
106         default:
107                 dprintk("%s could not handle NFSv4 error %d\n",
108                                 __func__, -err);
109                 break;
110         }
111         return -EIO;
112 }
113 
114 /*
115  * This is our standard bitmap for GETATTR requests.
116  */
117 const u32 nfs4_fattr_bitmap[2] = {
118         FATTR4_WORD0_TYPE
119         | FATTR4_WORD0_CHANGE
120         | FATTR4_WORD0_SIZE
121         | FATTR4_WORD0_FSID
122         | FATTR4_WORD0_FILEID,
123         FATTR4_WORD1_MODE
124         | FATTR4_WORD1_NUMLINKS
125         | FATTR4_WORD1_OWNER
126         | FATTR4_WORD1_OWNER_GROUP
127         | FATTR4_WORD1_RAWDEV
128         | FATTR4_WORD1_SPACE_USED
129         | FATTR4_WORD1_TIME_ACCESS
130         | FATTR4_WORD1_TIME_METADATA
131         | FATTR4_WORD1_TIME_MODIFY
132 };
133 
134 const u32 nfs4_statfs_bitmap[2] = {
135         FATTR4_WORD0_FILES_AVAIL
136         | FATTR4_WORD0_FILES_FREE
137         | FATTR4_WORD0_FILES_TOTAL,
138         FATTR4_WORD1_SPACE_AVAIL
139         | FATTR4_WORD1_SPACE_FREE
140         | FATTR4_WORD1_SPACE_TOTAL
141 };
142 
143 const u32 nfs4_pathconf_bitmap[2] = {
144         FATTR4_WORD0_MAXLINK
145         | FATTR4_WORD0_MAXNAME,
146         0
147 };
148 
149 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
150                         | FATTR4_WORD0_MAXREAD
151                         | FATTR4_WORD0_MAXWRITE
152                         | FATTR4_WORD0_LEASE_TIME,
153                         FATTR4_WORD1_TIME_DELTA
154                         | FATTR4_WORD1_FS_LAYOUT_TYPES,
155                         FATTR4_WORD2_LAYOUT_BLKSIZE
156 };
157 
158 const u32 nfs4_fs_locations_bitmap[2] = {
159         FATTR4_WORD0_TYPE
160         | FATTR4_WORD0_CHANGE
161         | FATTR4_WORD0_SIZE
162         | FATTR4_WORD0_FSID
163         | FATTR4_WORD0_FILEID
164         | FATTR4_WORD0_FS_LOCATIONS,
165         FATTR4_WORD1_MODE
166         | FATTR4_WORD1_NUMLINKS
167         | FATTR4_WORD1_OWNER
168         | FATTR4_WORD1_OWNER_GROUP
169         | FATTR4_WORD1_RAWDEV
170         | FATTR4_WORD1_SPACE_USED
171         | FATTR4_WORD1_TIME_ACCESS
172         | FATTR4_WORD1_TIME_METADATA
173         | FATTR4_WORD1_TIME_MODIFY
174         | FATTR4_WORD1_MOUNTED_ON_FILEID
175 };
176 
177 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
178                 struct nfs4_readdir_arg *readdir)
179 {
180         __be32 *start, *p;
181 
182         BUG_ON(readdir->count < 80);
183         if (cookie > 2) {
184                 readdir->cookie = cookie;
185                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186                 return;
187         }
188 
189         readdir->cookie = 0;
190         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191         if (cookie == 2)
192                 return;
193         
194         /*
195          * NFSv4 servers do not return entries for '.' and '..'
196          * Therefore, we fake these entries here.  We let '.'
197          * have cookie 0 and '..' have cookie 1.  Note that
198          * when talking to the server, we always send cookie 0
199          * instead of 1 or 2.
200          */
201         start = p = kmap_atomic(*readdir->pages);
202         
203         if (cookie == 0) {
204                 *p++ = xdr_one;                                  /* next */
205                 *p++ = xdr_zero;                   /* cookie, first word */
206                 *p++ = xdr_one;                   /* cookie, second word */
207                 *p++ = xdr_one;                             /* entry len */
208                 memcpy(p, ".\0\0\0", 4);                        /* entry */
209                 p++;
210                 *p++ = xdr_one;                         /* bitmap length */
211                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
212                 *p++ = htonl(8);              /* attribute buffer length */
213                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
214         }
215         
216         *p++ = xdr_one;                                  /* next */
217         *p++ = xdr_zero;                   /* cookie, first word */
218         *p++ = xdr_two;                   /* cookie, second word */
219         *p++ = xdr_two;                             /* entry len */
220         memcpy(p, "..\0\0", 4);                         /* entry */
221         p++;
222         *p++ = xdr_one;                         /* bitmap length */
223         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
224         *p++ = htonl(8);              /* attribute buffer length */
225         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
226 
227         readdir->pgbase = (char *)p - (char *)start;
228         readdir->count -= readdir->pgbase;
229         kunmap_atomic(start);
230 }
231 
232 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233 {
234         int res;
235 
236         might_sleep();
237 
238         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
239                         nfs_wait_bit_killable, TASK_KILLABLE);
240         return res;
241 }
242 
243 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244 {
245         int res = 0;
246 
247         might_sleep();
248 
249         if (*timeout <= 0)
250                 *timeout = NFS4_POLL_RETRY_MIN;
251         if (*timeout > NFS4_POLL_RETRY_MAX)
252                 *timeout = NFS4_POLL_RETRY_MAX;
253         freezable_schedule_timeout_killable(*timeout);
254         if (fatal_signal_pending(current))
255                 res = -ERESTARTSYS;
256         *timeout <<= 1;
257         return res;
258 }
259 
260 /* This is the error handling routine for processes that are allowed
261  * to sleep.
262  */
263 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
264 {
265         struct nfs_client *clp = server->nfs_client;
266         struct nfs4_state *state = exception->state;
267         struct inode *inode = exception->inode;
268         int ret = errorcode;
269 
270         exception->retry = 0;
271         switch(errorcode) {
272                 case 0:
273                         return 0;
274                 case -NFS4ERR_OPENMODE:
275                         if (inode && nfs_have_delegation(inode, FMODE_READ)) {
276                                 nfs_inode_return_delegation(inode);
277                                 exception->retry = 1;
278                                 return 0;
279                         }
280                         if (state == NULL)
281                                 break;
282                         nfs4_schedule_stateid_recovery(server, state);
283                         goto wait_on_recovery;
284                 case -NFS4ERR_DELEG_REVOKED:
285                 case -NFS4ERR_ADMIN_REVOKED:
286                 case -NFS4ERR_BAD_STATEID:
287                         if (state == NULL)
288                                 break;
289                         nfs_remove_bad_delegation(state->inode);
290                         nfs4_schedule_stateid_recovery(server, state);
291                         goto wait_on_recovery;
292                 case -NFS4ERR_EXPIRED:
293                         if (state != NULL)
294                                 nfs4_schedule_stateid_recovery(server, state);
295                 case -NFS4ERR_STALE_STATEID:
296                 case -NFS4ERR_STALE_CLIENTID:
297                         nfs4_schedule_lease_recovery(clp);
298                         goto wait_on_recovery;
299 #if defined(CONFIG_NFS_V4_1)
300                 case -NFS4ERR_BADSESSION:
301                 case -NFS4ERR_BADSLOT:
302                 case -NFS4ERR_BAD_HIGH_SLOT:
303                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304                 case -NFS4ERR_DEADSESSION:
305                 case -NFS4ERR_SEQ_FALSE_RETRY:
306                 case -NFS4ERR_SEQ_MISORDERED:
307                         dprintk("%s ERROR: %d Reset session\n", __func__,
308                                 errorcode);
309                         nfs4_schedule_session_recovery(clp->cl_session);
310                         goto wait_on_recovery;
311 #endif /* defined(CONFIG_NFS_V4_1) */
312                 case -NFS4ERR_FILE_OPEN:
313                         if (exception->timeout > HZ) {
314                                 /* We have retried a decent amount, time to
315                                  * fail
316                                  */
317                                 ret = -EBUSY;
318                                 break;
319                         }
320                 case -NFS4ERR_GRACE:
321                 case -NFS4ERR_DELAY:
322                         ret = nfs4_delay(server->client, &exception->timeout);
323                         if (ret != 0)
324                                 break;
325                 case -NFS4ERR_RETRY_UNCACHED_REP:
326                 case -NFS4ERR_OLD_STATEID:
327                         exception->retry = 1;
328                         break;
329                 case -NFS4ERR_BADOWNER:
330                         /* The following works around a Linux server bug! */
331                 case -NFS4ERR_BADNAME:
332                         if (server->caps & NFS_CAP_UIDGID_NOMAP) {
333                                 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
334                                 exception->retry = 1;
335                                 printk(KERN_WARNING "NFS: v4 server %s "
336                                                 "does not accept raw "
337                                                 "uid/gids. "
338                                                 "Reenabling the idmapper.\n",
339                                                 server->nfs_client->cl_hostname);
340                         }
341         }
342         /* We failed to handle the error */
343         return nfs4_map_errors(ret);
344 wait_on_recovery:
345         ret = nfs4_wait_clnt_recover(clp);
346         if (ret == 0)
347                 exception->retry = 1;
348         return ret;
349 }
350 
351 
352 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
353 {
354         spin_lock(&clp->cl_lock);
355         if (time_before(clp->cl_last_renewal,timestamp))
356                 clp->cl_last_renewal = timestamp;
357         spin_unlock(&clp->cl_lock);
358 }
359 
360 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
361 {
362         do_renew_lease(server->nfs_client, timestamp);
363 }
364 
365 #if defined(CONFIG_NFS_V4_1)
366 
367 /*
368  * nfs4_free_slot - free a slot and efficiently update slot table.
369  *
370  * freeing a slot is trivially done by clearing its respective bit
371  * in the bitmap.
372  * If the freed slotid equals highest_used_slotid we want to update it
373  * so that the server would be able to size down the slot table if needed,
374  * otherwise we know that the highest_used_slotid is still in use.
375  * When updating highest_used_slotid there may be "holes" in the bitmap
376  * so we need to scan down from highest_used_slotid to 0 looking for the now
377  * highest slotid in use.
378  * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
379  *
380  * Must be called while holding tbl->slot_tbl_lock
381  */
382 static void
383 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
384 {
385         BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
386         /* clear used bit in bitmap */
387         __clear_bit(slotid, tbl->used_slots);
388 
389         /* update highest_used_slotid when it is freed */
390         if (slotid == tbl->highest_used_slotid) {
391                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
392                 if (slotid < tbl->max_slots)
393                         tbl->highest_used_slotid = slotid;
394                 else
395                         tbl->highest_used_slotid = NFS4_NO_SLOT;
396         }
397         dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
398                 slotid, tbl->highest_used_slotid);
399 }
400 
401 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
402 {
403         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
404         return true;
405 }
406 
407 /*
408  * Signal state manager thread if session fore channel is drained
409  */
410 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
411 {
412         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
413                 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
414                                 nfs4_set_task_privileged, NULL);
415                 return;
416         }
417 
418         if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
419                 return;
420 
421         dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
422         complete(&ses->fc_slot_table.complete);
423 }
424 
425 /*
426  * Signal state manager thread if session back channel is drained
427  */
428 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
429 {
430         if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
431             ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
432                 return;
433         dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
434         complete(&ses->bc_slot_table.complete);
435 }
436 
437 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
438 {
439         struct nfs4_slot_table *tbl;
440 
441         tbl = &res->sr_session->fc_slot_table;
442         if (!res->sr_slot) {
443                 /* just wake up the next guy waiting since
444                  * we may have not consumed a slot after all */
445                 dprintk("%s: No slot\n", __func__);
446                 return;
447         }
448 
449         spin_lock(&tbl->slot_tbl_lock);
450         nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
451         nfs4_check_drain_fc_complete(res->sr_session);
452         spin_unlock(&tbl->slot_tbl_lock);
453         res->sr_slot = NULL;
454 }
455 
456 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
457 {
458         unsigned long timestamp;
459         struct nfs_client *clp;
460 
461         /*
462          * sr_status remains 1 if an RPC level error occurred. The server
463          * may or may not have processed the sequence operation..
464          * Proceed as if the server received and processed the sequence
465          * operation.
466          */
467         if (res->sr_status == 1)
468                 res->sr_status = NFS_OK;
469 
470         /* don't increment the sequence number if the task wasn't sent */
471         if (!RPC_WAS_SENT(task))
472                 goto out;
473 
474         /* Check the SEQUENCE operation status */
475         switch (res->sr_status) {
476         case 0:
477                 /* Update the slot's sequence and clientid lease timer */
478                 ++res->sr_slot->seq_nr;
479                 timestamp = res->sr_renewal_time;
480                 clp = res->sr_session->clp;
481                 do_renew_lease(clp, timestamp);
482                 /* Check sequence flags */
483                 if (res->sr_status_flags != 0)
484                         nfs4_schedule_lease_recovery(clp);
485                 break;
486         case -NFS4ERR_DELAY:
487                 /* The server detected a resend of the RPC call and
488                  * returned NFS4ERR_DELAY as per Section 2.10.6.2
489                  * of RFC5661.
490                  */
491                 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
492                         __func__,
493                         res->sr_slot - res->sr_session->fc_slot_table.slots,
494                         res->sr_slot->seq_nr);
495                 goto out_retry;
496         default:
497                 /* Just update the slot sequence no. */
498                 ++res->sr_slot->seq_nr;
499         }
500 out:
501         /* The session may be reset by one of the error handlers. */
502         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503         nfs41_sequence_free_slot(res);
504         return 1;
505 out_retry:
506         if (!rpc_restart_call(task))
507                 goto out;
508         rpc_delay(task, NFS4_POLL_RETRY_MAX);
509         return 0;
510 }
511 
512 static int nfs4_sequence_done(struct rpc_task *task,
513                                struct nfs4_sequence_res *res)
514 {
515         if (res->sr_session == NULL)
516                 return 1;
517         return nfs41_sequence_done(task, res);
518 }
519 
520 /*
521  * nfs4_find_slot - efficiently look for a free slot
522  *
523  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
524  * If found, we mark the slot as used, update the highest_used_slotid,
525  * and respectively set up the sequence operation args.
526  * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
527  *
528  * Note: must be called with under the slot_tbl_lock.
529  */
530 static u32
531 nfs4_find_slot(struct nfs4_slot_table *tbl)
532 {
533         u32 slotid;
534         u32 ret_id = NFS4_NO_SLOT;
535 
536         dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
537                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
538                 tbl->max_slots);
539         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
540         if (slotid >= tbl->max_slots)
541                 goto out;
542         __set_bit(slotid, tbl->used_slots);
543         if (slotid > tbl->highest_used_slotid ||
544                         tbl->highest_used_slotid == NFS4_NO_SLOT)
545                 tbl->highest_used_slotid = slotid;
546         ret_id = slotid;
547 out:
548         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
549                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
550         return ret_id;
551 }
552 
553 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
554                 struct nfs4_sequence_res *res, int cache_reply)
555 {
556         args->sa_session = NULL;
557         args->sa_cache_this = 0;
558         if (cache_reply)
559                 args->sa_cache_this = 1;
560         res->sr_session = NULL;
561         res->sr_slot = NULL;
562 }
563 
564 int nfs41_setup_sequence(struct nfs4_session *session,
565                                 struct nfs4_sequence_args *args,
566                                 struct nfs4_sequence_res *res,
567                                 struct rpc_task *task)
568 {
569         struct nfs4_slot *slot;
570         struct nfs4_slot_table *tbl;
571         u32 slotid;
572 
573         dprintk("--> %s\n", __func__);
574         /* slot already allocated? */
575         if (res->sr_slot != NULL)
576                 return 0;
577 
578         tbl = &session->fc_slot_table;
579 
580         spin_lock(&tbl->slot_tbl_lock);
581         if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
582             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
583                 /* The state manager will wait until the slot table is empty */
584                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
585                 spin_unlock(&tbl->slot_tbl_lock);
586                 dprintk("%s session is draining\n", __func__);
587                 return -EAGAIN;
588         }
589 
590         if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
591             !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
592                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
593                 spin_unlock(&tbl->slot_tbl_lock);
594                 dprintk("%s enforce FIFO order\n", __func__);
595                 return -EAGAIN;
596         }
597 
598         slotid = nfs4_find_slot(tbl);
599         if (slotid == NFS4_NO_SLOT) {
600                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
601                 spin_unlock(&tbl->slot_tbl_lock);
602                 dprintk("<-- %s: no free slots\n", __func__);
603                 return -EAGAIN;
604         }
605         spin_unlock(&tbl->slot_tbl_lock);
606 
607         rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
608         slot = tbl->slots + slotid;
609         args->sa_session = session;
610         args->sa_slotid = slotid;
611 
612         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
613 
614         res->sr_session = session;
615         res->sr_slot = slot;
616         res->sr_renewal_time = jiffies;
617         res->sr_status_flags = 0;
618         /*
619          * sr_status is only set in decode_sequence, and so will remain
620          * set to 1 if an rpc level failure occurs.
621          */
622         res->sr_status = 1;
623         return 0;
624 }
625 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
626 
627 int nfs4_setup_sequence(const struct nfs_server *server,
628                         struct nfs4_sequence_args *args,
629                         struct nfs4_sequence_res *res,
630                         struct rpc_task *task)
631 {
632         struct nfs4_session *session = nfs4_get_session(server);
633         int ret = 0;
634 
635         if (session == NULL)
636                 goto out;
637 
638         dprintk("--> %s clp %p session %p sr_slot %td\n",
639                 __func__, session->clp, session, res->sr_slot ?
640                         res->sr_slot - session->fc_slot_table.slots : -1);
641 
642         ret = nfs41_setup_sequence(session, args, res, task);
643 out:
644         dprintk("<-- %s status=%d\n", __func__, ret);
645         return ret;
646 }
647 
648 struct nfs41_call_sync_data {
649         const struct nfs_server *seq_server;
650         struct nfs4_sequence_args *seq_args;
651         struct nfs4_sequence_res *seq_res;
652 };
653 
654 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
655 {
656         struct nfs41_call_sync_data *data = calldata;
657 
658         dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
659 
660         if (nfs4_setup_sequence(data->seq_server, data->seq_args,
661                                 data->seq_res, task))
662                 return;
663         rpc_call_start(task);
664 }
665 
666 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
667 {
668         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
669         nfs41_call_sync_prepare(task, calldata);
670 }
671 
672 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
673 {
674         struct nfs41_call_sync_data *data = calldata;
675 
676         nfs41_sequence_done(task, data->seq_res);
677 }
678 
679 static const struct rpc_call_ops nfs41_call_sync_ops = {
680         .rpc_call_prepare = nfs41_call_sync_prepare,
681         .rpc_call_done = nfs41_call_sync_done,
682 };
683 
684 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
685         .rpc_call_prepare = nfs41_call_priv_sync_prepare,
686         .rpc_call_done = nfs41_call_sync_done,
687 };
688 
689 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
690                                    struct nfs_server *server,
691                                    struct rpc_message *msg,
692                                    struct nfs4_sequence_args *args,
693                                    struct nfs4_sequence_res *res,
694                                    int privileged)
695 {
696         int ret;
697         struct rpc_task *task;
698         struct nfs41_call_sync_data data = {
699                 .seq_server = server,
700                 .seq_args = args,
701                 .seq_res = res,
702         };
703         struct rpc_task_setup task_setup = {
704                 .rpc_client = clnt,
705                 .rpc_message = msg,
706                 .callback_ops = &nfs41_call_sync_ops,
707                 .callback_data = &data
708         };
709 
710         if (privileged)
711                 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
712         task = rpc_run_task(&task_setup);
713         if (IS_ERR(task))
714                 ret = PTR_ERR(task);
715         else {
716                 ret = task->tk_status;
717                 rpc_put_task(task);
718         }
719         return ret;
720 }
721 
722 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723                             struct nfs_server *server,
724                             struct rpc_message *msg,
725                             struct nfs4_sequence_args *args,
726                             struct nfs4_sequence_res *res,
727                             int cache_reply)
728 {
729         nfs41_init_sequence(args, res, cache_reply);
730         return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
731 }
732 
733 #else
734 static inline
735 void nfs41_init_sequence(struct nfs4_sequence_args *args,
736                 struct nfs4_sequence_res *res, int cache_reply)
737 {
738 }
739 
740 static int nfs4_sequence_done(struct rpc_task *task,
741                                struct nfs4_sequence_res *res)
742 {
743         return 1;
744 }
745 #endif /* CONFIG_NFS_V4_1 */
746 
747 int _nfs4_call_sync(struct rpc_clnt *clnt,
748                     struct nfs_server *server,
749                     struct rpc_message *msg,
750                     struct nfs4_sequence_args *args,
751                     struct nfs4_sequence_res *res,
752                     int cache_reply)
753 {
754         nfs41_init_sequence(args, res, cache_reply);
755         return rpc_call_sync(clnt, msg, 0);
756 }
757 
758 static inline
759 int nfs4_call_sync(struct rpc_clnt *clnt,
760                    struct nfs_server *server,
761                    struct rpc_message *msg,
762                    struct nfs4_sequence_args *args,
763                    struct nfs4_sequence_res *res,
764                    int cache_reply)
765 {
766         return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
767                                                 args, res, cache_reply);
768 }
769 
770 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
771 {
772         struct nfs_inode *nfsi = NFS_I(dir);
773 
774         spin_lock(&dir->i_lock);
775         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
776         if (!cinfo->atomic || cinfo->before != dir->i_version)
777                 nfs_force_lookup_revalidate(dir);
778         dir->i_version = cinfo->after;
779         spin_unlock(&dir->i_lock);
780 }
781 
782 struct nfs4_opendata {
783         struct kref kref;
784         struct nfs_openargs o_arg;
785         struct nfs_openres o_res;
786         struct nfs_open_confirmargs c_arg;
787         struct nfs_open_confirmres c_res;
788         struct nfs4_string owner_name;
789         struct nfs4_string group_name;
790         struct nfs_fattr f_attr;
791         struct nfs_fattr dir_attr;
792         struct dentry *dir;
793         struct dentry *dentry;
794         struct nfs4_state_owner *owner;
795         struct nfs4_state *state;
796         struct iattr attrs;
797         unsigned long timestamp;
798         unsigned int rpc_done : 1;
799         int rpc_status;
800         int cancelled;
801 };
802 
803 
804 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
805 {
806         p->o_res.f_attr = &p->f_attr;
807         p->o_res.dir_attr = &p->dir_attr;
808         p->o_res.seqid = p->o_arg.seqid;
809         p->c_res.seqid = p->c_arg.seqid;
810         p->o_res.server = p->o_arg.server;
811         nfs_fattr_init(&p->f_attr);
812         nfs_fattr_init(&p->dir_attr);
813         nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
814 }
815 
816 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
817                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
818                 const struct iattr *attrs,
819                 gfp_t gfp_mask)
820 {
821         struct dentry *parent = dget_parent(dentry);
822         struct inode *dir = parent->d_inode;
823         struct nfs_server *server = NFS_SERVER(dir);
824         struct nfs4_opendata *p;
825 
826         p = kzalloc(sizeof(*p), gfp_mask);
827         if (p == NULL)
828                 goto err;
829         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
830         if (p->o_arg.seqid == NULL)
831                 goto err_free;
832         nfs_sb_active(dentry->d_sb);
833         p->dentry = dget(dentry);
834         p->dir = parent;
835         p->owner = sp;
836         atomic_inc(&sp->so_count);
837         p->o_arg.fh = NFS_FH(dir);
838         p->o_arg.open_flags = flags;
839         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
840         p->o_arg.clientid = server->nfs_client->cl_clientid;
841         p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
842         p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
843         p->o_arg.name = &dentry->d_name;
844         p->o_arg.server = server;
845         p->o_arg.bitmask = server->attr_bitmask;
846         p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
847         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
848         if (attrs != NULL && attrs->ia_valid != 0) {
849                 __be32 verf[2];
850 
851                 p->o_arg.u.attrs = &p->attrs;
852                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
853 
854                 verf[0] = jiffies;
855                 verf[1] = current->pid;
856                 memcpy(p->o_arg.u.verifier.data, verf,
857                                 sizeof(p->o_arg.u.verifier.data));
858         }
859         p->c_arg.fh = &p->o_res.fh;
860         p->c_arg.stateid = &p->o_res.stateid;
861         p->c_arg.seqid = p->o_arg.seqid;
862         nfs4_init_opendata_res(p);
863         kref_init(&p->kref);
864         return p;
865 err_free:
866         kfree(p);
867 err:
868         dput(parent);
869         return NULL;
870 }
871 
872 static void nfs4_opendata_free(struct kref *kref)
873 {
874         struct nfs4_opendata *p = container_of(kref,
875                         struct nfs4_opendata, kref);
876         struct super_block *sb = p->dentry->d_sb;
877 
878         nfs_free_seqid(p->o_arg.seqid);
879         if (p->state != NULL)
880                 nfs4_put_open_state(p->state);
881         nfs4_put_state_owner(p->owner);
882         dput(p->dir);
883         dput(p->dentry);
884         nfs_sb_deactive(sb);
885         nfs_fattr_free_names(&p->f_attr);
886         kfree(p);
887 }
888 
889 static void nfs4_opendata_put(struct nfs4_opendata *p)
890 {
891         if (p != NULL)
892                 kref_put(&p->kref, nfs4_opendata_free);
893 }
894 
895 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896 {
897         int ret;
898 
899         ret = rpc_wait_for_completion_task(task);
900         return ret;
901 }
902 
903 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
904 {
905         int ret = 0;
906 
907         if (open_mode & (O_EXCL|O_TRUNC))
908                 goto out;
909         switch (mode & (FMODE_READ|FMODE_WRITE)) {
910                 case FMODE_READ:
911                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912                                 && state->n_rdonly != 0;
913                         break;
914                 case FMODE_WRITE:
915                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916                                 && state->n_wronly != 0;
917                         break;
918                 case FMODE_READ|FMODE_WRITE:
919                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920                                 && state->n_rdwr != 0;
921         }
922 out:
923         return ret;
924 }
925 
926 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
927 {
928         if (delegation == NULL)
929                 return 0;
930         if ((delegation->type & fmode) != fmode)
931                 return 0;
932         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
933                 return 0;
934         nfs_mark_delegation_referenced(delegation);
935         return 1;
936 }
937 
938 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
939 {
940         switch (fmode) {
941                 case FMODE_WRITE:
942                         state->n_wronly++;
943                         break;
944                 case FMODE_READ:
945                         state->n_rdonly++;
946                         break;
947                 case FMODE_READ|FMODE_WRITE:
948                         state->n_rdwr++;
949         }
950         nfs4_state_set_mode_locked(state, state->state | fmode);
951 }
952 
953 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
954 {
955         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
956                 nfs4_stateid_copy(&state->stateid, stateid);
957         nfs4_stateid_copy(&state->open_stateid, stateid);
958         switch (fmode) {
959                 case FMODE_READ:
960                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
961                         break;
962                 case FMODE_WRITE:
963                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
964                         break;
965                 case FMODE_READ|FMODE_WRITE:
966                         set_bit(NFS_O_RDWR_STATE, &state->flags);
967         }
968 }
969 
970 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
971 {
972         write_seqlock(&state->seqlock);
973         nfs_set_open_stateid_locked(state, stateid, fmode);
974         write_sequnlock(&state->seqlock);
975 }
976 
977 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
978 {
979         /*
980          * Protect the call to nfs4_state_set_mode_locked and
981          * serialise the stateid update
982          */
983         write_seqlock(&state->seqlock);
984         if (deleg_stateid != NULL) {
985                 nfs4_stateid_copy(&state->stateid, deleg_stateid);
986                 set_bit(NFS_DELEGATED_STATE, &state->flags);
987         }
988         if (open_stateid != NULL)
989                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
990         write_sequnlock(&state->seqlock);
991         spin_lock(&state->owner->so_lock);
992         update_open_stateflags(state, fmode);
993         spin_unlock(&state->owner->so_lock);
994 }
995 
996 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
997 {
998         struct nfs_inode *nfsi = NFS_I(state->inode);
999         struct nfs_delegation *deleg_cur;
1000         int ret = 0;
1001 
1002         fmode &= (FMODE_READ|FMODE_WRITE);
1003 
1004         rcu_read_lock();
1005         deleg_cur = rcu_dereference(nfsi->delegation);
1006         if (deleg_cur == NULL)
1007                 goto no_delegation;
1008 
1009         spin_lock(&deleg_cur->lock);
1010         if (nfsi->delegation != deleg_cur ||
1011             (deleg_cur->type & fmode) != fmode)
1012                 goto no_delegation_unlock;
1013 
1014         if (delegation == NULL)
1015                 delegation = &deleg_cur->stateid;
1016         else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1017                 goto no_delegation_unlock;
1018 
1019         nfs_mark_delegation_referenced(deleg_cur);
1020         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1021         ret = 1;
1022 no_delegation_unlock:
1023         spin_unlock(&deleg_cur->lock);
1024 no_delegation:
1025         rcu_read_unlock();
1026 
1027         if (!ret && open_stateid != NULL) {
1028                 __update_open_stateid(state, open_stateid, NULL, fmode);
1029                 ret = 1;
1030         }
1031 
1032         return ret;
1033 }
1034 
1035 
1036 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1037 {
1038         struct nfs_delegation *delegation;
1039 
1040         rcu_read_lock();
1041         delegation = rcu_dereference(NFS_I(inode)->delegation);
1042         if (delegation == NULL || (delegation->type & fmode) == fmode) {
1043                 rcu_read_unlock();
1044                 return;
1045         }
1046         rcu_read_unlock();
1047         nfs_inode_return_delegation(inode);
1048 }
1049 
1050 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1051 {
1052         struct nfs4_state *state = opendata->state;
1053         struct nfs_inode *nfsi = NFS_I(state->inode);
1054         struct nfs_delegation *delegation;
1055         int open_mode = opendata->o_arg.open_flags;
1056         fmode_t fmode = opendata->o_arg.fmode;
1057         nfs4_stateid stateid;
1058         int ret = -EAGAIN;
1059 
1060         for (;;) {
1061                 if (can_open_cached(state, fmode, open_mode)) {
1062                         spin_lock(&state->owner->so_lock);
1063                         if (can_open_cached(state, fmode, open_mode)) {
1064                                 update_open_stateflags(state, fmode);
1065                                 spin_unlock(&state->owner->so_lock);
1066                                 goto out_return_state;
1067                         }
1068                         spin_unlock(&state->owner->so_lock);
1069                 }
1070                 rcu_read_lock();
1071                 delegation = rcu_dereference(nfsi->delegation);
1072                 if (!can_open_delegated(delegation, fmode)) {
1073                         rcu_read_unlock();
1074                         break;
1075                 }
1076                 /* Save the delegation */
1077                 nfs4_stateid_copy(&stateid, &delegation->stateid);
1078                 rcu_read_unlock();
1079                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1080                 if (ret != 0)
1081                         goto out;
1082                 ret = -EAGAIN;
1083 
1084                 /* Try to update the stateid using the delegation */
1085                 if (update_open_stateid(state, NULL, &stateid, fmode))
1086                         goto out_return_state;
1087         }
1088 out:
1089         return ERR_PTR(ret);
1090 out_return_state:
1091         atomic_inc(&state->count);
1092         return state;
1093 }
1094 
1095 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1096 {
1097         struct inode *inode;
1098         struct nfs4_state *state = NULL;
1099         struct nfs_delegation *delegation;
1100         int ret;
1101 
1102         if (!data->rpc_done) {
1103                 state = nfs4_try_open_cached(data);
1104                 goto out;
1105         }
1106 
1107         ret = -EAGAIN;
1108         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1109                 goto err;
1110         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1111         ret = PTR_ERR(inode);
1112         if (IS_ERR(inode))
1113                 goto err;
1114         ret = -ENOMEM;
1115         state = nfs4_get_open_state(inode, data->owner);
1116         if (state == NULL)
1117                 goto err_put_inode;
1118         if (data->o_res.delegation_type != 0) {
1119                 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1120                 int delegation_flags = 0;
1121 
1122                 rcu_read_lock();
1123                 delegation = rcu_dereference(NFS_I(inode)->delegation);
1124                 if (delegation)
1125                         delegation_flags = delegation->flags;
1126                 rcu_read_unlock();
1127                 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128                         pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129                                         "returning a delegation for "
1130                                         "OPEN(CLAIM_DELEGATE_CUR)\n",
1131                                         clp->cl_hostname);
1132                 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133                         nfs_inode_set_delegation(state->inode,
1134                                         data->owner->so_cred,
1135                                         &data->o_res);
1136                 else
1137                         nfs_inode_reclaim_delegation(state->inode,
1138                                         data->owner->so_cred,
1139                                         &data->o_res);
1140         }
1141 
1142         update_open_stateid(state, &data->o_res.stateid, NULL,
1143                         data->o_arg.fmode);
1144         iput(inode);
1145 out:
1146         return state;
1147 err_put_inode:
1148         iput(inode);
1149 err:
1150         return ERR_PTR(ret);
1151 }
1152 
1153 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1154 {
1155         struct nfs_inode *nfsi = NFS_I(state->inode);
1156         struct nfs_open_context *ctx;
1157 
1158         spin_lock(&state->inode->i_lock);
1159         list_for_each_entry(ctx, &nfsi->open_files, list) {
1160                 if (ctx->state != state)
1161                         continue;
1162                 get_nfs_open_context(ctx);
1163                 spin_unlock(&state->inode->i_lock);
1164                 return ctx;
1165         }
1166         spin_unlock(&state->inode->i_lock);
1167         return ERR_PTR(-ENOENT);
1168 }
1169 
1170 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1171 {
1172         struct nfs4_opendata *opendata;
1173 
1174         opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1175         if (opendata == NULL)
1176                 return ERR_PTR(-ENOMEM);
1177         opendata->state = state;
1178         atomic_inc(&state->count);
1179         return opendata;
1180 }
1181 
1182 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1183 {
1184         struct nfs4_state *newstate;
1185         int ret;
1186 
1187         opendata->o_arg.open_flags = 0;
1188         opendata->o_arg.fmode = fmode;
1189         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191         nfs4_init_opendata_res(opendata);
1192         ret = _nfs4_recover_proc_open(opendata);
1193         if (ret != 0)
1194                 return ret; 
1195         newstate = nfs4_opendata_to_nfs4_state(opendata);
1196         if (IS_ERR(newstate))
1197                 return PTR_ERR(newstate);
1198         nfs4_close_state(newstate, fmode);
1199         *res = newstate;
1200         return 0;
1201 }
1202 
1203 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1204 {
1205         struct nfs4_state *newstate;
1206         int ret;
1207 
1208         /* memory barrier prior to reading state->n_* */
1209         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1210         smp_rmb();
1211         if (state->n_rdwr != 0) {
1212                 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1213                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1214                 if (ret != 0)
1215                         return ret;
1216                 if (newstate != state)
1217                         return -ESTALE;
1218         }
1219         if (state->n_wronly != 0) {
1220                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1221                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1222                 if (ret != 0)
1223                         return ret;
1224                 if (newstate != state)
1225                         return -ESTALE;
1226         }
1227         if (state->n_rdonly != 0) {
1228                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1229                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1230                 if (ret != 0)
1231                         return ret;
1232                 if (newstate != state)
1233                         return -ESTALE;
1234         }
1235         /*
1236          * We may have performed cached opens for all three recoveries.
1237          * Check if we need to update the current stateid.
1238          */
1239         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1240             !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1241                 write_seqlock(&state->seqlock);
1242                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1243                         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1244                 write_sequnlock(&state->seqlock);
1245         }
1246         return 0;
1247 }
1248 
1249 /*
1250  * OPEN_RECLAIM:
1251  *      reclaim state on the server after a reboot.
1252  */
1253 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1254 {
1255         struct nfs_delegation *delegation;
1256         struct nfs4_opendata *opendata;
1257         fmode_t delegation_type = 0;
1258         int status;
1259 
1260         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261         if (IS_ERR(opendata))
1262                 return PTR_ERR(opendata);
1263         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264         opendata->o_arg.fh = NFS_FH(state->inode);
1265         rcu_read_lock();
1266         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1267         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1268                 delegation_type = delegation->type;
1269         rcu_read_unlock();
1270         opendata->o_arg.u.delegation_type = delegation_type;
1271         status = nfs4_open_recover(opendata, state);
1272         nfs4_opendata_put(opendata);
1273         return status;
1274 }
1275 
1276 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1277 {
1278         struct nfs_server *server = NFS_SERVER(state->inode);
1279         struct nfs4_exception exception = { };
1280         int err;
1281         do {
1282                 err = _nfs4_do_open_reclaim(ctx, state);
1283                 if (err != -NFS4ERR_DELAY)
1284                         break;
1285                 nfs4_handle_exception(server, err, &exception);
1286         } while (exception.retry);
1287         return err;
1288 }
1289 
1290 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1291 {
1292         struct nfs_open_context *ctx;
1293         int ret;
1294 
1295         ctx = nfs4_state_find_open_context(state);
1296         if (IS_ERR(ctx))
1297                 return PTR_ERR(ctx);
1298         ret = nfs4_do_open_reclaim(ctx, state);
1299         put_nfs_open_context(ctx);
1300         return ret;
1301 }
1302 
1303 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1304 {
1305         struct nfs4_opendata *opendata;
1306         int ret;
1307 
1308         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309         if (IS_ERR(opendata))
1310                 return PTR_ERR(opendata);
1311         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1312         nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1313         ret = nfs4_open_recover(opendata, state);
1314         nfs4_opendata_put(opendata);
1315         return ret;
1316 }
1317 
1318 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1319 {
1320         struct nfs4_exception exception = { };
1321         struct nfs_server *server = NFS_SERVER(state->inode);
1322         int err;
1323         do {
1324                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1325                 switch (err) {
1326                         case 0:
1327                         case -ENOENT:
1328                         case -ESTALE:
1329                                 goto out;
1330                         case -NFS4ERR_BADSESSION:
1331                         case -NFS4ERR_BADSLOT:
1332                         case -NFS4ERR_BAD_HIGH_SLOT:
1333                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334                         case -NFS4ERR_DEADSESSION:
1335                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1336                                 goto out;
1337                         case -NFS4ERR_STALE_CLIENTID:
1338                         case -NFS4ERR_STALE_STATEID:
1339                         case -NFS4ERR_EXPIRED:
1340                                 /* Don't recall a delegation if it was lost */
1341                                 nfs4_schedule_lease_recovery(server->nfs_client);
1342                                 goto out;
1343                         case -ERESTARTSYS:
1344                                 /*
1345                                  * The show must go on: exit, but mark the
1346                                  * stateid as needing recovery.
1347                                  */
1348                         case -NFS4ERR_DELEG_REVOKED:
1349                         case -NFS4ERR_ADMIN_REVOKED:
1350                         case -NFS4ERR_BAD_STATEID:
1351                                 nfs_inode_find_state_and_recover(state->inode,
1352                                                 stateid);
1353                                 nfs4_schedule_stateid_recovery(server, state);
1354                         case -ENOMEM:
1355                                 err = 0;
1356                                 goto out;
1357                         case -NFS4ERR_DELAY:
1358                         case -NFS4ERR_GRACE:
1359                                 set_bit(NFS_DELEGATED_STATE, &state->flags);
1360                                 ssleep(1);
1361                                 err = -EAGAIN;
1362                                 goto out;
1363                 }
1364                 err = nfs4_handle_exception(server, err, &exception);
1365         } while (exception.retry);
1366 out:
1367         return err;
1368 }
1369 
1370 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1371 {
1372         struct nfs4_opendata *data = calldata;
1373 
1374         data->rpc_status = task->tk_status;
1375         if (data->rpc_status == 0) {
1376                 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1377                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1378                 renew_lease(data->o_res.server, data->timestamp);
1379                 data->rpc_done = 1;
1380         }
1381 }
1382 
1383 static void nfs4_open_confirm_release(void *calldata)
1384 {
1385         struct nfs4_opendata *data = calldata;
1386         struct nfs4_state *state = NULL;
1387 
1388         /* If this request hasn't been cancelled, do nothing */
1389         if (data->cancelled == 0)
1390                 goto out_free;
1391         /* In case of error, no cleanup! */
1392         if (!data->rpc_done)
1393                 goto out_free;
1394         state = nfs4_opendata_to_nfs4_state(data);
1395         if (!IS_ERR(state))
1396                 nfs4_close_state(state, data->o_arg.fmode);
1397 out_free:
1398         nfs4_opendata_put(data);
1399 }
1400 
1401 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1402         .rpc_call_done = nfs4_open_confirm_done,
1403         .rpc_release = nfs4_open_confirm_release,
1404 };
1405 
1406 /*
1407  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1408  */
1409 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1410 {
1411         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1412         struct rpc_task *task;
1413         struct  rpc_message msg = {
1414                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1415                 .rpc_argp = &data->c_arg,
1416                 .rpc_resp = &data->c_res,
1417                 .rpc_cred = data->owner->so_cred,
1418         };
1419         struct rpc_task_setup task_setup_data = {
1420                 .rpc_client = server->client,
1421                 .rpc_message = &msg,
1422                 .callback_ops = &nfs4_open_confirm_ops,
1423                 .callback_data = data,
1424                 .workqueue = nfsiod_workqueue,
1425                 .flags = RPC_TASK_ASYNC,
1426         };
1427         int status;
1428 
1429         kref_get(&data->kref);
1430         data->rpc_done = 0;
1431         data->rpc_status = 0;
1432         data->timestamp = jiffies;
1433         task = rpc_run_task(&task_setup_data);
1434         if (IS_ERR(task))
1435                 return PTR_ERR(task);
1436         status = nfs4_wait_for_completion_rpc_task(task);
1437         if (status != 0) {
1438                 data->cancelled = 1;
1439                 smp_wmb();
1440         } else
1441                 status = data->rpc_status;
1442         rpc_put_task(task);
1443         return status;
1444 }
1445 
1446 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1447 {
1448         struct nfs4_opendata *data = calldata;
1449         struct nfs4_state_owner *sp = data->owner;
1450 
1451         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1452                 return;
1453         /*
1454          * Check if we still need to send an OPEN call, or if we can use
1455          * a delegation instead.
1456          */
1457         if (data->state != NULL) {
1458                 struct nfs_delegation *delegation;
1459 
1460                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1461                         goto out_no_action;
1462                 rcu_read_lock();
1463                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1464                 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1465                     can_open_delegated(delegation, data->o_arg.fmode))
1466                         goto unlock_no_action;
1467                 rcu_read_unlock();
1468         }
1469         /* Update client id. */
1470         data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1471         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1472                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1473                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1474         }
1475         data->timestamp = jiffies;
1476         if (nfs4_setup_sequence(data->o_arg.server,
1477                                 &data->o_arg.seq_args,
1478                                 &data->o_res.seq_res,
1479                                 task) != 0)
1480                 nfs_release_seqid(data->o_arg.seqid);
1481         else
1482                 rpc_call_start(task);
1483         return;
1484 unlock_no_action:
1485         rcu_read_unlock();
1486 out_no_action:
1487         task->tk_action = NULL;
1488 
1489 }
1490 
1491 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1492 {
1493         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1494         nfs4_open_prepare(task, calldata);
1495 }
1496 
1497 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1498 {
1499         struct nfs4_opendata *data = calldata;
1500 
1501         data->rpc_status = task->tk_status;
1502 
1503         if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1504                 return;
1505 
1506         if (task->tk_status == 0) {
1507                 switch (data->o_res.f_attr->mode & S_IFMT) {
1508                         case S_IFREG:
1509                                 break;
1510                         case S_IFLNK:
1511                                 data->rpc_status = -ELOOP;
1512                                 break;
1513                         case S_IFDIR:
1514                                 data->rpc_status = -EISDIR;
1515                                 break;
1516                         default:
1517                                 data->rpc_status = -ENOTDIR;
1518                 }
1519                 renew_lease(data->o_res.server, data->timestamp);
1520                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1521                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1522         }
1523         data->rpc_done = 1;
1524 }
1525 
1526 static void nfs4_open_release(void *calldata)
1527 {
1528         struct nfs4_opendata *data = calldata;
1529         struct nfs4_state *state = NULL;
1530 
1531         /* If this request hasn't been cancelled, do nothing */
1532         if (data->cancelled == 0)
1533                 goto out_free;
1534         /* In case of error, no cleanup! */
1535         if (data->rpc_status != 0 || !data->rpc_done)
1536                 goto out_free;
1537         /* In case we need an open_confirm, no cleanup! */
1538         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1539                 goto out_free;
1540         state = nfs4_opendata_to_nfs4_state(data);
1541         if (!IS_ERR(state))
1542                 nfs4_close_state(state, data->o_arg.fmode);
1543 out_free:
1544         nfs4_opendata_put(data);
1545 }
1546 
1547 static const struct rpc_call_ops nfs4_open_ops = {
1548         .rpc_call_prepare = nfs4_open_prepare,
1549         .rpc_call_done = nfs4_open_done,
1550         .rpc_release = nfs4_open_release,
1551 };
1552 
1553 static const struct rpc_call_ops nfs4_recover_open_ops = {
1554         .rpc_call_prepare = nfs4_recover_open_prepare,
1555         .rpc_call_done = nfs4_open_done,
1556         .rpc_release = nfs4_open_release,
1557 };
1558 
1559 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1560 {
1561         struct inode *dir = data->dir->d_inode;
1562         struct nfs_server *server = NFS_SERVER(dir);
1563         struct nfs_openargs *o_arg = &data->o_arg;
1564         struct nfs_openres *o_res = &data->o_res;
1565         struct rpc_task *task;
1566         struct rpc_message msg = {
1567                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1568                 .rpc_argp = o_arg,
1569                 .rpc_resp = o_res,
1570                 .rpc_cred = data->owner->so_cred,
1571         };
1572         struct rpc_task_setup task_setup_data = {
1573                 .rpc_client = server->client,
1574                 .rpc_message = &msg,
1575                 .callback_ops = &nfs4_open_ops,
1576                 .callback_data = data,
1577                 .workqueue = nfsiod_workqueue,
1578                 .flags = RPC_TASK_ASYNC,
1579         };
1580         int status;
1581 
1582         nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1583         kref_get(&data->kref);
1584         data->rpc_done = 0;
1585         data->rpc_status = 0;
1586         data->cancelled = 0;
1587         if (isrecover)
1588                 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1589         task = rpc_run_task(&task_setup_data);
1590         if (IS_ERR(task))
1591                 return PTR_ERR(task);
1592         status = nfs4_wait_for_completion_rpc_task(task);
1593         if (status != 0) {
1594                 data->cancelled = 1;
1595                 smp_wmb();
1596         } else
1597                 status = data->rpc_status;
1598         rpc_put_task(task);
1599 
1600         return status;
1601 }
1602 
1603 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1604 {
1605         struct inode *dir = data->dir->d_inode;
1606         struct nfs_openres *o_res = &data->o_res;
1607         int status;
1608 
1609         status = nfs4_run_open_task(data, 1);
1610         if (status != 0 || !data->rpc_done)
1611                 return status;
1612 
1613         nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1614 
1615         nfs_refresh_inode(dir, o_res->dir_attr);
1616 
1617         if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618                 status = _nfs4_proc_open_confirm(data);
1619                 if (status != 0)
1620                         return status;
1621         }
1622 
1623         return status;
1624 }
1625 
1626 /*
1627  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1628  */
1629 static int _nfs4_proc_open(struct nfs4_opendata *data)
1630 {
1631         struct inode *dir = data->dir->d_inode;
1632         struct nfs_server *server = NFS_SERVER(dir);
1633         struct nfs_openargs *o_arg = &data->o_arg;
1634         struct nfs_openres *o_res = &data->o_res;
1635         int status;
1636 
1637         status = nfs4_run_open_task(data, 0);
1638         if (!data->rpc_done)
1639                 return status;
1640         if (status != 0) {
1641                 if (status == -NFS4ERR_BADNAME &&
1642                                 !(o_arg->open_flags & O_CREAT))
1643                         return -ENOENT;
1644                 return status;
1645         }
1646 
1647         nfs_fattr_map_and_free_names(server, &data->f_attr);
1648 
1649         if (o_arg->open_flags & O_CREAT) {
1650                 update_changeattr(dir, &o_res->cinfo);
1651                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1652         } else
1653                 nfs_refresh_inode(dir, o_res->dir_attr);
1654         if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1655                 server->caps &= ~NFS_CAP_POSIX_LOCK;
1656         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1657                 status = _nfs4_proc_open_confirm(data);
1658                 if (status != 0)
1659                         return status;
1660         }
1661         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1662                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1663         return 0;
1664 }
1665 
1666 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1667 {
1668         unsigned int loop;
1669         int ret;
1670 
1671         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1672                 ret = nfs4_wait_clnt_recover(clp);
1673                 if (ret != 0)
1674                         break;
1675                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1676                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1677                         break;
1678                 nfs4_schedule_state_manager(clp);
1679                 ret = -EIO;
1680         }
1681         return ret;
1682 }
1683 
1684 static int nfs4_recover_expired_lease(struct nfs_server *server)
1685 {
1686         return nfs4_client_recover_expired_lease(server->nfs_client);
1687 }
1688 
1689 /*
1690  * OPEN_EXPIRED:
1691  *      reclaim state on the server after a network partition.
1692  *      Assumes caller holds the appropriate lock
1693  */
1694 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1695 {
1696         struct nfs4_opendata *opendata;
1697         int ret;
1698 
1699         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1700         if (IS_ERR(opendata))
1701                 return PTR_ERR(opendata);
1702         ret = nfs4_open_recover(opendata, state);
1703         if (ret == -ESTALE)
1704                 d_drop(ctx->dentry);
1705         nfs4_opendata_put(opendata);
1706         return ret;
1707 }
1708 
1709 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1710 {
1711         struct nfs_server *server = NFS_SERVER(state->inode);
1712         struct nfs4_exception exception = { };
1713         int err;
1714 
1715         do {
1716                 err = _nfs4_open_expired(ctx, state);
1717                 switch (err) {
1718                 default:
1719                         goto out;
1720                 case -NFS4ERR_GRACE:
1721                 case -NFS4ERR_DELAY:
1722                         nfs4_handle_exception(server, err, &exception);
1723                         err = 0;
1724                 }
1725         } while (exception.retry);
1726 out:
1727         return err;
1728 }
1729 
1730 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1731 {
1732         struct nfs_open_context *ctx;
1733         int ret;
1734 
1735         ctx = nfs4_state_find_open_context(state);
1736         if (IS_ERR(ctx))
1737                 return PTR_ERR(ctx);
1738         ret = nfs4_do_open_expired(ctx, state);
1739         put_nfs_open_context(ctx);
1740         return ret;
1741 }
1742 
1743 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
1744 {
1745         nfs_remove_bad_delegation(state->inode);
1746         write_seqlock(&state->seqlock);
1747         nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1748         write_sequnlock(&state->seqlock);
1749         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1750 }
1751 
1752 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
1753 {
1754         if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
1755                 nfs_finish_clear_delegation_stateid(state);
1756 }
1757 
1758 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1759 {
1760         /* NFSv4.0 doesn't allow for delegation recovery on open expire */
1761         nfs40_clear_delegation_stateid(state);
1762         return nfs4_open_expired(sp, state);
1763 }
1764 
1765 #if defined(CONFIG_NFS_V4_1)
1766 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1767 {
1768         int status = NFS_OK;
1769         struct nfs_server *server = NFS_SERVER(state->inode);
1770 
1771         if (state->flags & flags) {
1772                 status = nfs41_test_stateid(server, stateid);
1773                 if (status != NFS_OK) {
1774                         nfs41_free_stateid(server, stateid);
1775                         state->flags &= ~flags;
1776                 }
1777         }
1778         return status;
1779 }
1780 
1781 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1782 {
1783         int deleg_status, open_status;
1784         int deleg_flags = 1 << NFS_DELEGATED_STATE;
1785         int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1786 
1787         deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1788         open_status = nfs41_check_expired_stateid(state,  &state->open_stateid, open_flags);
1789 
1790         if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1791                 return NFS_OK;
1792         return nfs4_open_expired(sp, state);
1793 }
1794 #endif
1795 
1796 /*
1797  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1798  * fields corresponding to attributes that were used to store the verifier.
1799  * Make sure we clobber those fields in the later setattr call
1800  */
1801 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1802 {
1803         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1804             !(sattr->ia_valid & ATTR_ATIME_SET))
1805                 sattr->ia_valid |= ATTR_ATIME;
1806 
1807         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1808             !(sattr->ia_valid & ATTR_MTIME_SET))
1809                 sattr->ia_valid |= ATTR_MTIME;
1810 }
1811 
1812 /*
1813  * Returns a referenced nfs4_state
1814  */
1815 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1816 {
1817         struct nfs4_state_owner  *sp;
1818         struct nfs4_state     *state = NULL;
1819         struct nfs_server       *server = NFS_SERVER(dir);
1820         struct nfs4_opendata *opendata;
1821         int status;
1822 
1823         /* Protect against reboot recovery conflicts */
1824         status = -ENOMEM;
1825         sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1826         if (sp == NULL) {
1827                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1828                 goto out_err;
1829         }
1830         status = nfs4_recover_expired_lease(server);
1831         if (status != 0)
1832                 goto err_put_state_owner;
1833         if (dentry->d_inode != NULL)
1834                 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1835         status = -ENOMEM;
1836         opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1837         if (opendata == NULL)
1838                 goto err_put_state_owner;
1839 
1840         if (dentry->d_inode != NULL)
1841                 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1842 
1843         status = _nfs4_proc_open(opendata);
1844         if (status != 0)
1845                 goto err_opendata_put;
1846 
1847         state = nfs4_opendata_to_nfs4_state(opendata);
1848         status = PTR_ERR(state);
1849         if (IS_ERR(state))
1850                 goto err_opendata_put;
1851         if (server->caps & NFS_CAP_POSIX_LOCK)
1852                 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1853 
1854         if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) {
1855                 nfs4_exclusive_attrset(opendata, sattr);
1856 
1857                 nfs_fattr_init(opendata->o_res.f_attr);
1858                 status = nfs4_do_setattr(state->inode, cred,
1859                                 opendata->o_res.f_attr, sattr,
1860                                 state);
1861                 if (status == 0)
1862                         nfs_setattr_update_inode(state->inode, sattr);
1863                 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1864         }
1865         nfs4_opendata_put(opendata);
1866         nfs4_put_state_owner(sp);
1867         *res = state;
1868         return 0;
1869 err_opendata_put:
1870         nfs4_opendata_put(opendata);
1871 err_put_state_owner:
1872         nfs4_put_state_owner(sp);
1873 out_err:
1874         *res = NULL;
1875         return status;
1876 }
1877 
1878 
1879 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1880 {
1881         struct nfs4_exception exception = { };
1882         struct nfs4_state *res;
1883         int status;
1884 
1885         fmode &= FMODE_READ|FMODE_WRITE;
1886         do {
1887                 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1888                 if (status == 0)
1889                         break;
1890                 /* NOTE: BAD_SEQID means the server and client disagree about the
1891                  * book-keeping w.r.t. state-changing operations
1892                  * (OPEN/CLOSE/LOCK/LOCKU...)
1893                  * It is actually a sign of a bug on the client or on the server.
1894                  *
1895                  * If we receive a BAD_SEQID error in the particular case of
1896                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1897                  * have unhashed the old state_owner for us, and that we can
1898                  * therefore safely retry using a new one. We should still warn
1899                  * the user though...
1900                  */
1901                 if (status == -NFS4ERR_BAD_SEQID) {
1902                         pr_warn_ratelimited("NFS: v4 server %s "
1903                                         " returned a bad sequence-id error!\n",
1904                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1905                         exception.retry = 1;
1906                         continue;
1907                 }
1908                 /*
1909                  * BAD_STATEID on OPEN means that the server cancelled our
1910                  * state before it received the OPEN_CONFIRM.
1911                  * Recover by retrying the request as per the discussion
1912                  * on Page 181 of RFC3530.
1913                  */
1914                 if (status == -NFS4ERR_BAD_STATEID) {
1915                         exception.retry = 1;
1916                         continue;
1917                 }
1918                 if (status == -EAGAIN) {
1919                         /* We must have found a delegation */
1920                         exception.retry = 1;
1921                         continue;
1922                 }
1923                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1924                                         status, &exception));
1925         } while (exception.retry);
1926         return res;
1927 }
1928 
1929 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1930                             struct nfs_fattr *fattr, struct iattr *sattr,
1931                             struct nfs4_state *state)
1932 {
1933         struct nfs_server *server = NFS_SERVER(inode);
1934         struct nfs_setattrargs  arg = {
1935                 .fh             = NFS_FH(inode),
1936                 .iap            = sattr,
1937                 .server         = server,
1938                 .bitmask = server->attr_bitmask,
1939         };
1940         struct nfs_setattrres  res = {
1941                 .fattr          = fattr,
1942                 .server         = server,
1943         };
1944         struct rpc_message msg = {
1945                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1946                 .rpc_argp       = &arg,
1947                 .rpc_resp       = &res,
1948                 .rpc_cred       = cred,
1949         };
1950         unsigned long timestamp = jiffies;
1951         int status;
1952 
1953         nfs_fattr_init(fattr);
1954 
1955         if (state != NULL) {
1956                 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1957                                 current->files, current->tgid);
1958         } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1959                                 FMODE_WRITE)) {
1960                 /* Use that stateid */
1961         } else
1962                 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1963 
1964         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1965         if (status == 0 && state != NULL)
1966                 renew_lease(server, timestamp);
1967         return status;
1968 }
1969 
1970 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1971                            struct nfs_fattr *fattr, struct iattr *sattr,
1972                            struct nfs4_state *state)
1973 {
1974         struct nfs_server *server = NFS_SERVER(inode);
1975         struct nfs4_exception exception = {
1976                 .state = state,
1977                 .inode = inode,
1978         };
1979         int err;
1980         do {
1981                 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1982                 switch (err) {
1983                 case -NFS4ERR_OPENMODE:
1984                         if (state && !(state->state & FMODE_WRITE)) {
1985                                 err = -EBADF;
1986                                 if (sattr->ia_valid & ATTR_OPEN)
1987                                         err = -EACCES;
1988                                 goto out;
1989                         }
1990                 }
1991                 err = nfs4_handle_exception(server, err, &exception);
1992         } while (exception.retry);
1993 out:
1994         return err;
1995 }
1996 
1997 struct nfs4_closedata {
1998         struct inode *inode;
1999         struct nfs4_state *state;
2000         struct nfs_closeargs arg;
2001         struct nfs_closeres res;
2002         struct nfs_fattr fattr;
2003         unsigned long timestamp;
2004         bool roc;
2005         u32 roc_barrier;
2006 };
2007 
2008 static void nfs4_free_closedata(void *data)
2009 {
2010         struct nfs4_closedata *calldata = data;
2011         struct nfs4_state_owner *sp = calldata->state->owner;
2012         struct super_block *sb = calldata->state->inode->i_sb;
2013 
2014         if (calldata->roc)
2015                 pnfs_roc_release(calldata->state->inode);
2016         nfs4_put_open_state(calldata->state);
2017         nfs_free_seqid(calldata->arg.seqid);
2018         nfs4_put_state_owner(sp);
2019         nfs_sb_deactive(sb);
2020         kfree(calldata);
2021 }
2022 
2023 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2024                 fmode_t fmode)
2025 {
2026         spin_lock(&state->owner->so_lock);
2027         if (!(fmode & FMODE_READ))
2028                 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2029         if (!(fmode & FMODE_WRITE))
2030                 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2031         clear_bit(NFS_O_RDWR_STATE, &state->flags);
2032         spin_unlock(&state->owner->so_lock);
2033 }
2034 
2035 static void nfs4_close_done(struct rpc_task *task, void *data)
2036 {
2037         struct nfs4_closedata *calldata = data;
2038         struct nfs4_state *state = calldata->state;
2039         struct nfs_server *server = NFS_SERVER(calldata->inode);
2040 
2041         dprintk("%s: begin!\n", __func__);
2042         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2043                 return;
2044         /* hmm. we are done with the inode, and in the process of freeing
2045          * the state_owner. we keep this around to process errors
2046          */
2047         switch (task->tk_status) {
2048                 case 0:
2049                         if (calldata->roc)
2050                                 pnfs_roc_set_barrier(state->inode,
2051                                                      calldata->roc_barrier);
2052                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2053                         renew_lease(server, calldata->timestamp);
2054                         nfs4_close_clear_stateid_flags(state,
2055                                         calldata->arg.fmode);
2056                         break;
2057                 case -NFS4ERR_STALE_STATEID:
2058                 case -NFS4ERR_OLD_STATEID:
2059                 case -NFS4ERR_BAD_STATEID:
2060                 case -NFS4ERR_EXPIRED:
2061                         if (calldata->arg.fmode == 0)
2062                                 break;
2063                 default:
2064                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2065                                 rpc_restart_call_prepare(task);
2066         }
2067         nfs_release_seqid(calldata->arg.seqid);
2068         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2069         dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2070 }
2071 
2072 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2073 {
2074         struct nfs4_closedata *calldata = data;
2075         struct nfs4_state *state = calldata->state;
2076         bool is_rdonly, is_wronly, is_rdwr;
2077         int call_close = 0;
2078 
2079         dprintk("%s: begin!\n", __func__);
2080         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2081                 return;
2082 
2083         task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2084         spin_lock(&state->owner->so_lock);
2085         is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2086         is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2087         is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2088         /* Calculate the change in open mode */
2089         calldata->arg.fmode = 0;
2090         if (state->n_rdwr == 0) {
2091                 if (state->n_rdonly == 0)
2092                         call_close |= is_rdonly;
2093                 else if (is_rdonly)
2094                         calldata->arg.fmode |= FMODE_READ;
2095                 if (state->n_wronly == 0)
2096                         call_close |= is_wronly;
2097                 else if (is_wronly)
2098                         calldata->arg.fmode |= FMODE_WRITE;
2099         } else if (is_rdwr)
2100                 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2101 
2102         if (calldata->arg.fmode == 0)
2103                 call_close |= is_rdwr;
2104 
2105         spin_unlock(&state->owner->so_lock);
2106 
2107         if (!call_close) {
2108                 /* Note: exit _without_ calling nfs4_close_done */
2109                 task->tk_action = NULL;
2110                 goto out;
2111         }
2112 
2113         if (calldata->arg.fmode == 0) {
2114                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2115                 if (calldata->roc &&
2116                     pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2117                         rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2118                                      task, NULL);
2119                         goto out;
2120                 }
2121         }
2122 
2123         nfs_fattr_init(calldata->res.fattr);
2124         calldata->timestamp = jiffies;
2125         if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2126                                 &calldata->arg.seq_args,
2127                                 &calldata->res.seq_res,
2128                                 task) != 0)
2129                 nfs_release_seqid(calldata->arg.seqid);
2130         else
2131                 rpc_call_start(task);
2132 out:
2133         dprintk("%s: done!\n", __func__);
2134 }
2135 
2136 static const struct rpc_call_ops nfs4_close_ops = {
2137         .rpc_call_prepare = nfs4_close_prepare,
2138         .rpc_call_done = nfs4_close_done,
2139         .rpc_release = nfs4_free_closedata,
2140 };
2141 
2142 /* 
2143  * It is possible for data to be read/written from a mem-mapped file 
2144  * after the sys_close call (which hits the vfs layer as a flush).
2145  * This means that we can't safely call nfsv4 close on a file until 
2146  * the inode is cleared. This in turn means that we are not good
2147  * NFSv4 citizens - we do not indicate to the server to update the file's 
2148  * share state even when we are done with one of the three share 
2149  * stateid's in the inode.
2150  *
2151  * NOTE: Caller must be holding the sp->so_owner semaphore!
2152  */
2153 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2154 {
2155         struct nfs_server *server = NFS_SERVER(state->inode);
2156         struct nfs4_closedata *calldata;
2157         struct nfs4_state_owner *sp = state->owner;
2158         struct rpc_task *task;
2159         struct rpc_message msg = {
2160                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2161                 .rpc_cred = state->owner->so_cred,
2162         };
2163         struct rpc_task_setup task_setup_data = {
2164                 .rpc_client = server->client,
2165                 .rpc_message = &msg,
2166                 .callback_ops = &nfs4_close_ops,
2167                 .workqueue = nfsiod_workqueue,
2168                 .flags = RPC_TASK_ASYNC,
2169         };
2170         int status = -ENOMEM;
2171 
2172         calldata = kzalloc(sizeof(*calldata), gfp_mask);
2173         if (calldata == NULL)
2174                 goto out;
2175         nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2176         calldata->inode = state->inode;
2177         calldata->state = state;
2178         calldata->arg.fh = NFS_FH(state->inode);
2179         calldata->arg.stateid = &state->open_stateid;
2180         /* Serialization for the sequence id */
2181         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2182         if (calldata->arg.seqid == NULL)
2183                 goto out_free_calldata;
2184         calldata->arg.fmode = 0;
2185         calldata->arg.bitmask = server->cache_consistency_bitmask;
2186         calldata->res.fattr = &calldata->fattr;
2187         calldata->res.seqid = calldata->arg.seqid;
2188         calldata->res.server = server;
2189         calldata->roc = roc;
2190         nfs_sb_active(calldata->inode->i_sb);
2191 
2192         msg.rpc_argp = &calldata->arg;
2193         msg.rpc_resp = &calldata->res;
2194         task_setup_data.callback_data = calldata;
2195         task = rpc_run_task(&task_setup_data);
2196         if (IS_ERR(task))
2197                 return PTR_ERR(task);
2198         status = 0;
2199         if (wait)
2200                 status = rpc_wait_for_completion_task(task);
2201         rpc_put_task(task);
2202         return status;
2203 out_free_calldata:
2204         kfree(calldata);
2205 out:
2206         if (roc)
2207                 pnfs_roc_release(state->inode);
2208         nfs4_put_open_state(state);
2209         nfs4_put_state_owner(sp);
2210         return status;
2211 }
2212 
2213 static struct inode *
2214 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2215 {
2216         struct nfs4_state *state;
2217 
2218         /* Protect against concurrent sillydeletes */
2219         state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2220         if (IS_ERR(state))
2221                 return ERR_CAST(state);
2222         ctx->state = state;
2223         return igrab(state->inode);
2224 }
2225 
2226 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2227 {
2228         if (ctx->state == NULL)
2229                 return;
2230         if (is_sync)
2231                 nfs4_close_sync(ctx->state, ctx->mode);
2232         else
2233                 nfs4_close_state(ctx->state, ctx->mode);
2234 }
2235 
2236 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2237 {
2238         struct nfs4_server_caps_arg args = {
2239                 .fhandle = fhandle,
2240         };
2241         struct nfs4_server_caps_res res = {};
2242         struct rpc_message msg = {
2243                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2244                 .rpc_argp = &args,
2245                 .rpc_resp = &res,
2246         };
2247         int status;
2248 
2249         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2250         if (status == 0) {
2251                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2252                 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2253                                 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2254                                 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2255                                 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2256                                 NFS_CAP_CTIME|NFS_CAP_MTIME);
2257                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2258                         server->caps |= NFS_CAP_ACLS;
2259                 if (res.has_links != 0)
2260                         server->caps |= NFS_CAP_HARDLINKS;
2261                 if (res.has_symlinks != 0)
2262                         server->caps |= NFS_CAP_SYMLINKS;
2263                 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2264                         server->caps |= NFS_CAP_FILEID;
2265                 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2266                         server->caps |= NFS_CAP_MODE;
2267                 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2268                         server->caps |= NFS_CAP_NLINK;
2269                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2270                         server->caps |= NFS_CAP_OWNER;
2271                 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2272                         server->caps |= NFS_CAP_OWNER_GROUP;
2273                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2274                         server->caps |= NFS_CAP_ATIME;
2275                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2276                         server->caps |= NFS_CAP_CTIME;
2277                 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2278                         server->caps |= NFS_CAP_MTIME;
2279 
2280                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2281                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2282                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2283                 server->acl_bitmask = res.acl_bitmask;
2284                 server->fh_expire_type = res.fh_expire_type;
2285         }
2286 
2287         return status;
2288 }
2289 
2290 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2291 {
2292         struct nfs4_exception exception = { };
2293         int err;
2294         do {
2295                 err = nfs4_handle_exception(server,
2296                                 _nfs4_server_capabilities(server, fhandle),
2297                                 &exception);
2298         } while (exception.retry);
2299         return err;
2300 }
2301 
2302 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2303                 struct nfs_fsinfo *info)
2304 {
2305         struct nfs4_lookup_root_arg args = {
2306                 .bitmask = nfs4_fattr_bitmap,
2307         };
2308         struct nfs4_lookup_res res = {
2309                 .server = server,
2310                 .fattr = info->fattr,
2311                 .fh = fhandle,
2312         };
2313         struct rpc_message msg = {
2314                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2315                 .rpc_argp = &args,
2316                 .rpc_resp = &res,
2317         };
2318 
2319         nfs_fattr_init(info->fattr);
2320         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2321 }
2322 
2323 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2324                 struct nfs_fsinfo *info)
2325 {
2326         struct nfs4_exception exception = { };
2327         int err;
2328         do {
2329                 err = _nfs4_lookup_root(server, fhandle, info);
2330                 switch (err) {
2331                 case 0:
2332                 case -NFS4ERR_WRONGSEC:
2333                         goto out;
2334                 default:
2335                         err = nfs4_handle_exception(server, err, &exception);
2336                 }
2337         } while (exception.retry);
2338 out:
2339         return err;
2340 }
2341 
2342 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2343                                 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2344 {
2345         struct rpc_auth *auth;
2346         int ret;
2347 
2348         auth = rpcauth_create(flavor, server->client);
2349         if (!auth) {
2350                 ret = -EIO;
2351                 goto out;
2352         }
2353         ret = nfs4_lookup_root(server, fhandle, info);
2354 out:
2355         return ret;
2356 }
2357 
2358 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2359                               struct nfs_fsinfo *info)
2360 {
2361         int i, len, status = 0;
2362         rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2363 
2364         len = gss_mech_list_pseudoflavors(&flav_array[0]);
2365         flav_array[len] = RPC_AUTH_NULL;
2366         len += 1;
2367 
2368         for (i = 0; i < len; i++) {
2369                 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2370                 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2371                         continue;
2372                 break;
2373         }
2374         /*
2375          * -EACCESS could mean that the user doesn't have correct permissions
2376          * to access the mount.  It could also mean that we tried to mount
2377          * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
2378          * existing mount programs don't handle -EACCES very well so it should
2379          * be mapped to -EPERM instead.
2380          */
2381         if (status == -EACCES)
2382                 status = -EPERM;
2383         return status;
2384 }
2385 
2386 /*
2387  * get the file handle for the "/" directory on the server
2388  */
2389 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2390                               struct nfs_fsinfo *info)
2391 {
2392         int minor_version = server->nfs_client->cl_minorversion;
2393         int status = nfs4_lookup_root(server, fhandle, info);
2394         if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2395                 /*
2396                  * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2397                  * by nfs4_map_errors() as this function exits.
2398                  */
2399                 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2400         if (status == 0)
2401                 status = nfs4_server_capabilities(server, fhandle);
2402         if (status == 0)
2403                 status = nfs4_do_fsinfo(server, fhandle, info);
2404         return nfs4_map_errors(status);
2405 }
2406 
2407 /*
2408  * Get locations and (maybe) other attributes of a referral.
2409  * Note that we'll actually follow the referral later when
2410  * we detect fsid mismatch in inode revalidation
2411  */
2412 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2413                              const struct qstr *name, struct nfs_fattr *fattr,
2414                              struct nfs_fh *fhandle)
2415 {
2416         int status = -ENOMEM;
2417         struct page *page = NULL;
2418         struct nfs4_fs_locations *locations = NULL;
2419 
2420         page = alloc_page(GFP_KERNEL);
2421         if (page == NULL)
2422                 goto out;
2423         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2424         if (locations == NULL)
2425                 goto out;
2426 
2427         status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2428         if (status != 0)
2429                 goto out;
2430         /* Make sure server returned a different fsid for the referral */
2431         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2432                 dprintk("%s: server did not return a different fsid for"
2433                         " a referral at %s\n", __func__, name->name);
2434                 status = -EIO;
2435                 goto out;
2436         }
2437         /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2438         nfs_fixup_referral_attributes(&locations->fattr);
2439 
2440         /* replace the lookup nfs_fattr with the locations nfs_fattr */
2441         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2442         memset(fhandle, 0, sizeof(struct nfs_fh));
2443 out:
2444         if (page)
2445                 __free_page(page);
2446         kfree(locations);
2447         return status;
2448 }
2449 
2450 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2451 {
2452         struct nfs4_getattr_arg args = {
2453                 .fh = fhandle,
2454                 .bitmask = server->attr_bitmask,
2455         };
2456         struct nfs4_getattr_res res = {
2457                 .fattr = fattr,
2458                 .server = server,
2459         };
2460         struct rpc_message msg = {
2461                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2462                 .rpc_argp = &args,
2463                 .rpc_resp = &res,
2464         };
2465         
2466         nfs_fattr_init(fattr);
2467         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2468 }
2469 
2470 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2471 {
2472         struct nfs4_exception exception = { };
2473         int err;
2474         do {
2475                 err = nfs4_handle_exception(server,
2476                                 _nfs4_proc_getattr(server, fhandle, fattr),
2477                                 &exception);
2478         } while (exception.retry);
2479         return err;
2480 }
2481 
2482 /* 
2483  * The file is not closed if it is opened due to the a request to change
2484  * the size of the file. The open call will not be needed once the
2485  * VFS layer lookup-intents are implemented.
2486  *
2487  * Close is called when the inode is destroyed.
2488  * If we haven't opened the file for O_WRONLY, we
2489  * need to in the size_change case to obtain a stateid.
2490  *
2491  * Got race?
2492  * Because OPEN is always done by name in nfsv4, it is
2493  * possible that we opened a different file by the same
2494  * name.  We can recognize this race condition, but we
2495  * can't do anything about it besides returning an error.
2496  *
2497  * This will be fixed with VFS changes (lookup-intent).
2498  */
2499 static int
2500 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2501                   struct iattr *sattr)
2502 {
2503         struct inode *inode = dentry->d_inode;
2504         struct rpc_cred *cred = NULL;
2505         struct nfs4_state *state = NULL;
2506         int status;
2507 
2508         if (pnfs_ld_layoutret_on_setattr(inode))
2509                 pnfs_return_layout(inode);
2510 
2511         nfs_fattr_init(fattr);
2512         
2513         /* Search for an existing open(O_WRITE) file */
2514         if (sattr->ia_valid & ATTR_FILE) {
2515                 struct nfs_open_context *ctx;
2516 
2517                 ctx = nfs_file_open_context(sattr->ia_file);
2518                 if (ctx) {
2519                         cred = ctx->cred;
2520                         state = ctx->state;
2521                 }
2522         }
2523 
2524         /* Deal with open(O_TRUNC) */
2525         if (sattr->ia_valid & ATTR_OPEN)
2526                 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2527 
2528         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2529         if (status == 0)
2530                 nfs_setattr_update_inode(inode, sattr);
2531         return status;
2532 }
2533 
2534 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2535                 const struct qstr *name, struct nfs_fh *fhandle,
2536                 struct nfs_fattr *fattr)
2537 {
2538         struct nfs_server *server = NFS_SERVER(dir);
2539         int                    status;
2540         struct nfs4_lookup_arg args = {
2541                 .bitmask = server->attr_bitmask,
2542                 .dir_fh = NFS_FH(dir),
2543                 .name = name,
2544         };
2545         struct nfs4_lookup_res res = {
2546                 .server = server,
2547                 .fattr = fattr,
2548                 .fh = fhandle,
2549         };
2550         struct rpc_message msg = {
2551                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2552                 .rpc_argp = &args,
2553                 .rpc_resp = &res,
2554         };
2555 
2556         nfs_fattr_init(fattr);
2557 
2558         dprintk("NFS call  lookup %s\n", name->name);
2559         status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2560         dprintk("NFS reply lookup: %d\n", status);
2561         return status;
2562 }
2563 
2564 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2565 {
2566         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2567                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2568         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2569         fattr->nlink = 2;
2570 }
2571 
2572 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2573                                    struct qstr *name, struct nfs_fh *fhandle,
2574                                    struct nfs_fattr *fattr)
2575 {
2576         struct nfs4_exception exception = { };
2577         struct rpc_clnt *client = *clnt;
2578         int err;
2579         do {
2580                 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2581                 switch (err) {
2582                 case -NFS4ERR_BADNAME:
2583                         err = -ENOENT;
2584                         goto out;
2585                 case -NFS4ERR_MOVED:
2586                         err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2587                         goto out;
2588                 case -NFS4ERR_WRONGSEC:
2589                         err = -EPERM;
2590                         if (client != *clnt)
2591                                 goto out;
2592 
2593                         client = nfs4_create_sec_client(client, dir, name);
2594                         if (IS_ERR(client))
2595                                 return PTR_ERR(client);
2596 
2597                         exception.retry = 1;
2598                         break;
2599                 default:
2600                         err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2601                 }
2602         } while (exception.retry);
2603 
2604 out:
2605         if (err == 0)
2606                 *clnt = client;
2607         else if (client != *clnt)
2608                 rpc_shutdown_client(client);
2609 
2610         return err;
2611 }
2612 
2613 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2614                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2615 {
2616         int status;
2617         struct rpc_clnt *client = NFS_CLIENT(dir);
2618 
2619         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2620         if (client != NFS_CLIENT(dir)) {
2621                 rpc_shutdown_client(client);
2622                 nfs_fixup_secinfo_attributes(fattr);
2623         }
2624         return status;
2625 }
2626 
2627 struct rpc_clnt *
2628 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2629                             struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2630 {
2631         int status;
2632         struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2633 
2634         status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2635         if (status < 0) {
2636                 rpc_shutdown_client(client);
2637                 return ERR_PTR(status);
2638         }
2639         return client;
2640 }
2641 
2642 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2643 {
2644         struct nfs_server *server = NFS_SERVER(inode);
2645         struct nfs4_accessargs args = {
2646                 .fh = NFS_FH(inode),
2647                 .bitmask = server->cache_consistency_bitmask,
2648         };
2649         struct nfs4_accessres res = {
2650                 .server = server,
2651         };
2652         struct rpc_message msg = {
2653                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2654                 .rpc_argp = &args,
2655                 .rpc_resp = &res,
2656                 .rpc_cred = entry->cred,
2657         };
2658         int mode = entry->mask;
2659         int status;
2660 
2661         /*
2662          * Determine which access bits we want to ask for...
2663          */
2664         if (mode & MAY_READ)
2665                 args.access |= NFS4_ACCESS_READ;
2666         if (S_ISDIR(inode->i_mode)) {
2667                 if (mode & MAY_WRITE)
2668                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2669                 if (mode & MAY_EXEC)
2670                         args.access |= NFS4_ACCESS_LOOKUP;
2671         } else {
2672                 if (mode & MAY_WRITE)
2673                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2674                 if (mode & MAY_EXEC)
2675                         args.access |= NFS4_ACCESS_EXECUTE;
2676         }
2677 
2678         res.fattr = nfs_alloc_fattr();
2679         if (res.fattr == NULL)
2680                 return -ENOMEM;
2681 
2682         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2683         if (!status) {
2684                 entry->mask = 0;
2685                 if (res.access & NFS4_ACCESS_READ)
2686                         entry->mask |= MAY_READ;
2687                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2688                         entry->mask |= MAY_WRITE;
2689                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2690                         entry->mask |= MAY_EXEC;
2691                 nfs_refresh_inode(inode, res.fattr);
2692         }
2693         nfs_free_fattr(res.fattr);
2694         return status;
2695 }
2696 
2697 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2698 {
2699         struct nfs4_exception exception = { };
2700         int err;
2701         do {
2702                 err = nfs4_handle_exception(NFS_SERVER(inode),
2703                                 _nfs4_proc_access(inode, entry),
2704                                 &exception);
2705         } while (exception.retry);
2706         return err;
2707 }
2708 
2709 /*
2710  * TODO: For the time being, we don't try to get any attributes
2711  * along with any of the zero-copy operations READ, READDIR,
2712  * READLINK, WRITE.
2713  *
2714  * In the case of the first three, we want to put the GETATTR
2715  * after the read-type operation -- this is because it is hard
2716  * to predict the length of a GETATTR response in v4, and thus
2717  * align the READ data correctly.  This means that the GETATTR
2718  * may end up partially falling into the page cache, and we should
2719  * shift it into the 'tail' of the xdr_buf before processing.
2720  * To do this efficiently, we need to know the total length
2721  * of data received, which doesn't seem to be available outside
2722  * of the RPC layer.
2723  *
2724  * In the case of WRITE, we also want to put the GETATTR after
2725  * the operation -- in this case because we want to make sure
2726  * we get the post-operation mtime and size.  This means that
2727  * we can't use xdr_encode_pages() as written: we need a variant
2728  * of it which would leave room in the 'tail' iovec.
2729  *
2730  * Both of these changes to the XDR layer would in fact be quite
2731  * minor, but I decided to leave them for a subsequent patch.
2732  */
2733 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2734                 unsigned int pgbase, unsigned int pglen)
2735 {
2736         struct nfs4_readlink args = {
2737                 .fh       = NFS_FH(inode),
2738                 .pgbase   = pgbase,
2739                 .pglen    = pglen,
2740                 .pages    = &page,
2741         };
2742         struct nfs4_readlink_res res;
2743         struct rpc_message msg = {
2744                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2745                 .rpc_argp = &args,
2746                 .rpc_resp = &res,
2747         };
2748 
2749         return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2750 }
2751 
2752 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2753                 unsigned int pgbase, unsigned int pglen)
2754 {
2755         struct nfs4_exception exception = { };
2756         int err;
2757         do {
2758                 err = nfs4_handle_exception(NFS_SERVER(inode),
2759                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2760                                 &exception);
2761         } while (exception.retry);
2762         return err;
2763 }
2764 
2765 /*
2766  * Got race?
2767  * We will need to arrange for the VFS layer to provide an atomic open.
2768  * Until then, this create/open method is prone to inefficiency and race
2769  * conditions due to the lookup, create, and open VFS calls from sys_open()
2770  * placed on the wire.
2771  *
2772  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2773  * The file will be opened again in the subsequent VFS open call
2774  * (nfs4_proc_file_open).
2775  *
2776  * The open for read will just hang around to be used by any process that
2777  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2778  */
2779 
2780 static int
2781 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2782                  int flags, struct nfs_open_context *ctx)
2783 {
2784         struct dentry *de = dentry;
2785         struct nfs4_state *state;
2786         struct rpc_cred *cred = NULL;
2787         fmode_t fmode = 0;
2788         int status = 0;
2789 
2790         if (ctx != NULL) {
2791                 cred = ctx->cred;
2792                 de = ctx->dentry;
2793                 fmode = ctx->mode;
2794         }
2795         sattr->ia_mode &= ~current_umask();
2796         state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2797         d_drop(dentry);
2798         if (IS_ERR(state)) {
2799                 status = PTR_ERR(state);
2800                 goto out;
2801         }
2802         d_add(dentry, igrab(state->inode));
2803         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2804         if (ctx != NULL)
2805                 ctx->state = state;
2806         else
2807                 nfs4_close_sync(state, fmode);
2808 out:
2809         return status;
2810 }
2811 
2812 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2813 {
2814         struct nfs_server *server = NFS_SERVER(dir);
2815         struct nfs_removeargs args = {
2816                 .fh = NFS_FH(dir),
2817                 .name.len = name->len,
2818                 .name.name = name->name,
2819                 .bitmask = server->attr_bitmask,
2820         };
2821         struct nfs_removeres res = {
2822                 .server = server,
2823         };
2824         struct rpc_message msg = {
2825                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2826                 .rpc_argp = &args,
2827                 .rpc_resp = &res,
2828         };
2829         int status = -ENOMEM;
2830 
2831         res.dir_attr = nfs_alloc_fattr();
2832         if (res.dir_attr == NULL)
2833                 goto out;
2834 
2835         status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2836         if (status == 0) {
2837                 update_changeattr(dir, &res.cinfo);
2838                 nfs_post_op_update_inode(dir, res.dir_attr);
2839         }
2840         nfs_free_fattr(res.dir_attr);
2841 out:
2842         return status;
2843 }
2844 
2845 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2846 {
2847         struct nfs4_exception exception = { };
2848         int err;
2849         do {
2850                 err = nfs4_handle_exception(NFS_SERVER(dir),
2851                                 _nfs4_proc_remove(dir, name),
2852                                 &exception);
2853         } while (exception.retry);
2854         return err;
2855 }
2856 
2857 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2858 {
2859         struct nfs_server *server = NFS_SERVER(dir);
2860         struct nfs_removeargs *args = msg->rpc_argp;
2861         struct nfs_removeres *res = msg->rpc_resp;
2862 
2863         args->bitmask = server->cache_consistency_bitmask;
2864         res->server = server;
2865         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2866         nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2867 }
2868 
2869 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2870 {
2871         if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2872                                 &data->args.seq_args,
2873                                 &data->res.seq_res,
2874                                 task))
2875                 return;
2876         rpc_call_start(task);
2877 }
2878 
2879 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2880 {
2881         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2882 
2883         if (!nfs4_sequence_done(task, &res->seq_res))
2884                 return 0;
2885         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2886                 return 0;
2887         update_changeattr(dir, &res->cinfo);
2888         nfs_post_op_update_inode(dir, res->dir_attr);
2889         return 1;
2890 }
2891 
2892 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2893 {
2894         struct nfs_server *server = NFS_SERVER(dir);
2895         struct nfs_renameargs *arg = msg->rpc_argp;
2896         struct nfs_renameres *res = msg->rpc_resp;
2897 
2898         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2899         arg->bitmask = server->attr_bitmask;
2900         res->server = server;
2901         nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2902 }
2903 
2904 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2905 {
2906         if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2907                                 &data->args.seq_args,
2908                                 &data->res.seq_res,
2909                                 task))
2910                 return;
2911         rpc_call_start(task);
2912 }
2913 
2914 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2915                                  struct inode *new_dir)
2916 {
2917         struct nfs_renameres *res = task->tk_msg.rpc_resp;
2918 
2919         if (!nfs4_sequence_done(task, &res->seq_res))
2920                 return 0;
2921         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2922                 return 0;
2923 
2924         update_changeattr(old_dir, &res->old_cinfo);
2925         nfs_post_op_update_inode(old_dir, res->old_fattr);
2926         update_changeattr(new_dir, &res->new_cinfo);
2927         nfs_post_op_update_inode(new_dir, res->new_fattr);
2928         return 1;
2929 }
2930 
2931 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2932                 struct inode *new_dir, struct qstr *new_name)
2933 {
2934         struct nfs_server *server = NFS_SERVER(old_dir);
2935         struct nfs_renameargs arg = {
2936                 .old_dir = NFS_FH(old_dir),
2937                 .new_dir = NFS_FH(new_dir),
2938                 .old_name = old_name,
2939                 .new_name = new_name,
2940                 .bitmask = server->attr_bitmask,
2941         };
2942         struct nfs_renameres res = {
2943                 .server = server,
2944         };
2945         struct rpc_message msg = {
2946                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2947                 .rpc_argp = &arg,
2948                 .rpc_resp = &res,
2949         };
2950         int status = -ENOMEM;
2951         
2952         res.old_fattr = nfs_alloc_fattr();
2953         res.new_fattr = nfs_alloc_fattr();
2954         if (res.old_fattr == NULL || res.new_fattr == NULL)
2955                 goto out;
2956 
2957         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2958         if (!status) {
2959                 update_changeattr(old_dir, &res.old_cinfo);
2960                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2961                 update_changeattr(new_dir, &res.new_cinfo);
2962                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2963         }
2964 out:
2965         nfs_free_fattr(res.new_fattr);
2966         nfs_free_fattr(res.old_fattr);
2967         return status;
2968 }
2969 
2970 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2971                 struct inode *new_dir, struct qstr *new_name)
2972 {
2973         struct nfs4_exception exception = { };
2974         int err;
2975         do {
2976                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2977                                 _nfs4_proc_rename(old_dir, old_name,
2978                                         new_dir, new_name),
2979                                 &exception);
2980         } while (exception.retry);
2981         return err;
2982 }
2983 
2984 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2985 {
2986         struct nfs_server *server = NFS_SERVER(inode);
2987         struct nfs4_link_arg arg = {
2988                 .fh     = NFS_FH(inode),
2989                 .dir_fh = NFS_FH(dir),
2990                 .name   = name,
2991                 .bitmask = server->attr_bitmask,
2992         };
2993         struct nfs4_link_res res = {
2994                 .server = server,
2995         };
2996         struct rpc_message msg = {
2997                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2998                 .rpc_argp = &arg,
2999                 .rpc_resp = &res,
3000         };
3001         int status = -ENOMEM;
3002 
3003         res.fattr = nfs_alloc_fattr();
3004         res.dir_attr = nfs_alloc_fattr();
3005         if (res.fattr == NULL || res.dir_attr == NULL)
3006                 goto out;
3007 
3008         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3009         if (!status) {
3010                 update_changeattr(dir, &res.cinfo);
3011                 nfs_post_op_update_inode(dir, res.dir_attr);
3012                 nfs_post_op_update_inode(inode, res.fattr);
3013         }
3014 out:
3015         nfs_free_fattr(res.dir_attr);
3016         nfs_free_fattr(res.fattr);
3017         return status;
3018 }
3019 
3020 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3021 {
3022         struct nfs4_exception exception = { };
3023         int err;
3024         do {
3025                 err = nfs4_handle_exception(NFS_SERVER(inode),
3026                                 _nfs4_proc_link(inode, dir, name),
3027                                 &exception);
3028         } while (exception.retry);
3029         return err;
3030 }
3031 
3032 struct nfs4_createdata {
3033         struct rpc_message msg;
3034         struct nfs4_create_arg arg;
3035         struct nfs4_create_res res;
3036         struct nfs_fh fh;
3037         struct nfs_fattr fattr;
3038         struct nfs_fattr dir_fattr;
3039 };
3040 
3041 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3042                 struct qstr *name, struct iattr *sattr, u32 ftype)
3043 {
3044         struct nfs4_createdata *data;
3045 
3046         data = kzalloc(sizeof(*data), GFP_KERNEL);
3047         if (data != NULL) {
3048                 struct nfs_server *server = NFS_SERVER(dir);
3049 
3050                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3051                 data->msg.rpc_argp = &data->arg;
3052                 data->msg.rpc_resp = &data->res;
3053                 data->arg.dir_fh = NFS_FH(dir);
3054                 data->arg.server = server;
3055                 data->arg.name = name;
3056                 data->arg.attrs = sattr;
3057                 data->arg.ftype = ftype;
3058                 data->arg.bitmask = server->attr_bitmask;
3059                 data->res.server = server;
3060                 data->res.fh = &data->fh;
3061                 data->res.fattr = &data->fattr;
3062                 data->res.dir_fattr = &data->dir_fattr;
3063                 nfs_fattr_init(data->res.fattr);
3064                 nfs_fattr_init(data->res.dir_fattr);
3065         }
3066         return data;
3067 }
3068 
3069 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3070 {
3071         int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3072                                     &data->arg.seq_args, &data->res.seq_res, 1);
3073         if (status == 0) {
3074                 update_changeattr(dir, &data->res.dir_cinfo);
3075                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3076                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3077         }
3078         return status;
3079 }
3080 
3081 static void nfs4_free_createdata(struct nfs4_createdata *data)
3082 {
3083         kfree(data);
3084 }
3085 
3086 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3087                 struct page *page, unsigned int len, struct iattr *sattr)
3088 {
3089         struct nfs4_createdata *data;
3090         int status = -ENAMETOOLONG;
3091 
3092         if (len > NFS4_MAXPATHLEN)
3093                 goto out;
3094 
3095         status = -ENOMEM;
3096         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3097         if (data == NULL)
3098                 goto out;
3099 
3100         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3101         data->arg.u.symlink.pages = &page;
3102         data->arg.u.symlink.len = len;
3103         
3104         status = nfs4_do_create(dir, dentry, data);
3105 
3106         nfs4_free_createdata(data);
3107 out:
3108         return status;
3109 }
3110 
3111 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3112                 struct page *page, unsigned int len, struct iattr *sattr)
3113 {
3114         struct nfs4_exception exception = { };
3115         int err;
3116         do {
3117                 err = nfs4_handle_exception(NFS_SERVER(dir),
3118                                 _nfs4_proc_symlink(dir, dentry, page,
3119                                                         len, sattr),
3120                                 &exception);
3121         } while (exception.retry);
3122         return err;
3123 }
3124 
3125 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3126                 struct iattr *sattr)
3127 {
3128         struct nfs4_createdata *data;
3129         int status = -ENOMEM;
3130 
3131         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3132         if (data == NULL)
3133                 goto out;
3134 
3135         status = nfs4_do_create(dir, dentry, data);
3136 
3137         nfs4_free_createdata(data);
3138 out:
3139         return status;
3140 }
3141 
3142 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3143                 struct iattr *sattr)
3144 {
3145         struct nfs4_exception exception = { };
3146         int err;
3147 
3148         sattr->ia_mode &= ~current_umask();
3149         do {
3150                 err = nfs4_handle_exception(NFS_SERVER(dir),
3151                                 _nfs4_proc_mkdir(dir, dentry, sattr),
3152                                 &exception);
3153         } while (exception.retry);
3154         return err;
3155 }
3156 
3157 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3158                 u64 cookie, struct page **pages, unsigned int count, int plus)
3159 {
3160         struct inode            *dir = dentry->d_inode;
3161         struct nfs4_readdir_arg args = {
3162                 .fh = NFS_FH(dir),
3163                 .pages = pages,
3164                 .pgbase = 0,
3165                 .count = count,
3166                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3167                 .plus = plus,
3168         };
3169         struct nfs4_readdir_res res;
3170         struct rpc_message msg = {
3171                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3172                 .rpc_argp = &args,
3173                 .rpc_resp = &res,
3174                 .rpc_cred = cred,
3175         };
3176         int                     status;
3177 
3178         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3179                         dentry->d_parent->d_name.name,
3180                         dentry->d_name.name,
3181                         (unsigned long long)cookie);
3182         nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3183         res.pgbase = args.pgbase;
3184         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3185         if (status >= 0) {
3186                 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3187                 status += args.pgbase;
3188         }
3189 
3190         nfs_invalidate_atime(dir);
3191 
3192         dprintk("%s: returns %d\n", __func__, status);
3193         return status;
3194 }
3195 
3196 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3197                 u64 cookie, struct page **pages, unsigned int count, int plus)
3198 {
3199         struct nfs4_exception exception = { };
3200         int err;
3201         do {
3202                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3203                                 _nfs4_proc_readdir(dentry, cred, cookie,
3204                                         pages, count, plus),
3205                                 &exception);
3206         } while (exception.retry);
3207         return err;
3208 }
3209 
3210 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3211                 struct iattr *sattr, dev_t rdev)
3212 {
3213         struct nfs4_createdata *data;
3214         int mode = sattr->ia_mode;
3215         int status = -ENOMEM;
3216 
3217         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3218         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3219 
3220         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3221         if (data == NULL)
3222                 goto out;
3223 
3224         if (S_ISFIFO(mode))
3225                 data->arg.ftype = NF4FIFO;
3226         else if (S_ISBLK(mode)) {
3227                 data->arg.ftype = NF4BLK;
3228                 data->arg.u.device.specdata1 = MAJOR(rdev);
3229                 data->arg.u.device.specdata2 = MINOR(rdev);
3230         }
3231         else if (S_ISCHR(mode)) {
3232                 data->arg.ftype = NF4CHR;
3233                 data->arg.u.device.specdata1 = MAJOR(rdev);
3234                 data->arg.u.device.specdata2 = MINOR(rdev);
3235         }
3236         
3237         status = nfs4_do_create(dir, dentry, data);
3238 
3239         nfs4_free_createdata(data);
3240 out:
3241         return status;
3242 }
3243 
3244 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3245                 struct iattr *sattr, dev_t rdev)
3246 {
3247         struct nfs4_exception exception = { };
3248         int err;
3249 
3250         sattr->ia_mode &= ~current_umask();
3251         do {
3252                 err = nfs4_handle_exception(NFS_SERVER(dir),
3253                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3254                                 &exception);
3255         } while (exception.retry);
3256         return err;
3257 }
3258 
3259 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3260                  struct nfs_fsstat *fsstat)
3261 {
3262         struct nfs4_statfs_arg args = {
3263                 .fh = fhandle,
3264                 .bitmask = server->attr_bitmask,
3265         };
3266         struct nfs4_statfs_res res = {
3267                 .fsstat = fsstat,
3268         };
3269         struct rpc_message msg = {
3270                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3271                 .rpc_argp = &args,
3272                 .rpc_resp = &res,
3273         };
3274 
3275         nfs_fattr_init(fsstat->fattr);
3276         return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3277 }
3278 
3279 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3280 {
3281         struct nfs4_exception exception = { };
3282         int err;
3283         do {
3284                 err = nfs4_handle_exception(server,
3285                                 _nfs4_proc_statfs(server, fhandle, fsstat),
3286                                 &exception);
3287         } while (exception.retry);
3288         return err;
3289 }
3290 
3291 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3292                 struct nfs_fsinfo *fsinfo)
3293 {
3294         struct nfs4_fsinfo_arg args = {
3295                 .fh = fhandle,
3296                 .bitmask = server->attr_bitmask,
3297         };
3298         struct nfs4_fsinfo_res res = {
3299                 .fsinfo = fsinfo,
3300         };
3301         struct rpc_message msg = {
3302                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3303                 .rpc_argp = &args,
3304                 .rpc_resp = &res,
3305         };
3306 
3307         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3308 }
3309 
3310 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3311 {
3312         struct nfs4_exception exception = { };
3313         int err;
3314 
3315         do {
3316                 err = nfs4_handle_exception(server,
3317                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3318                                 &exception);
3319         } while (exception.retry);
3320         return err;
3321 }
3322 
3323 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3324 {
3325         nfs_fattr_init(fsinfo->fattr);
3326         return nfs4_do_fsinfo(server, fhandle, fsinfo);
3327 }
3328 
3329 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3330                 struct nfs_pathconf *pathconf)
3331 {
3332         struct nfs4_pathconf_arg args = {
3333                 .fh = fhandle,
3334                 .bitmask = server->attr_bitmask,
3335         };
3336         struct nfs4_pathconf_res res = {
3337                 .pathconf = pathconf,
3338         };
3339         struct rpc_message msg = {
3340                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3341                 .rpc_argp = &args,
3342                 .rpc_resp = &res,
3343         };
3344 
3345         /* None of the pathconf attributes are mandatory to implement */
3346         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3347                 memset(pathconf, 0, sizeof(*pathconf));
3348                 return 0;
3349         }
3350 
3351         nfs_fattr_init(pathconf->fattr);
3352         return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3353 }
3354 
3355 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3356                 struct nfs_pathconf *pathconf)
3357 {
3358         struct nfs4_exception exception = { };
3359         int err;
3360 
3361         do {
3362                 err = nfs4_handle_exception(server,
3363                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
3364                                 &exception);
3365         } while (exception.retry);
3366         return err;
3367 }
3368 
3369 void __nfs4_read_done_cb(struct nfs_read_data *data)
3370 {
3371         nfs_invalidate_atime(data->inode);
3372 }
3373 
3374 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3375 {
3376         struct nfs_server *server = NFS_SERVER(data->inode);
3377 
3378         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3379                 rpc_restart_call_prepare(task);
3380                 return -EAGAIN;
3381         }
3382 
3383         __nfs4_read_done_cb(data);
3384         if (task->tk_status > 0)
3385                 renew_lease(server, data->timestamp);
3386         return 0;
3387 }
3388 
3389 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3390 {
3391 
3392         dprintk("--> %s\n", __func__);
3393 
3394         if (!nfs4_sequence_done(task, &data->res.seq_res))
3395                 return -EAGAIN;
3396 
3397         return data->read_done_cb ? data->read_done_cb(task, data) :
3398                                     nfs4_read_done_cb(task, data);
3399 }
3400 
3401 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3402 {
3403         data->timestamp   = jiffies;
3404         data->read_done_cb = nfs4_read_done_cb;
3405         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3406         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3407 }
3408 
3409 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3410 {
3411         if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3412                                 &data->args.seq_args,
3413                                 &data->res.seq_res,
3414                                 task))
3415                 return;
3416         rpc_call_start(task);
3417 }
3418 
3419 /* Reset the the nfs_read_data to send the read to the MDS. */
3420 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3421 {
3422         dprintk("%s Reset task for i/o through\n", __func__);
3423         put_lseg(data->lseg);
3424         data->lseg = NULL;
3425         /* offsets will differ in the dense stripe case */
3426         data->args.offset = data->mds_offset;
3427         data->ds_clp = NULL;
3428         data->args.fh     = NFS_FH(data->inode);
3429         data->read_done_cb = nfs4_read_done_cb;
3430         task->tk_ops = data->mds_ops;
3431         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3432 }
3433 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3434 
3435 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3436 {
3437         struct inode *inode = data->inode;
3438         
3439         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3440                 rpc_restart_call_prepare(task);
3441                 return -EAGAIN;
3442         }
3443         if (task->tk_status >= 0) {
3444                 renew_lease(NFS_SERVER(inode), data->timestamp);
3445                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3446         }
3447         return 0;
3448 }
3449 
3450 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3451 {
3452         if (!nfs4_sequence_done(task, &data->res.seq_res))
3453                 return -EAGAIN;
3454         return data->write_done_cb ? data->write_done_cb(task, data) :
3455                 nfs4_write_done_cb(task, data);
3456 }
3457 
3458 /* Reset the the nfs_write_data to send the write to the MDS. */
3459 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3460 {
3461         dprintk("%s Reset task for i/o through\n", __func__);
3462         put_lseg(data->lseg);
3463         data->lseg          = NULL;
3464         data->ds_clp        = NULL;
3465         data->write_done_cb = nfs4_write_done_cb;
3466         data->args.fh       = NFS_FH(data->inode);
3467         data->args.bitmask  = data->res.server->cache_consistency_bitmask;
3468         data->args.offset   = data->mds_offset;
3469         data->res.fattr     = &data->fattr;
3470         task->tk_ops        = data->mds_ops;
3471         rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3472 }
3473 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3474 
3475 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3476 {
3477         struct nfs_server *server = NFS_SERVER(data->inode);
3478 
3479         if (data->lseg) {
3480                 data->args.bitmask = NULL;
3481                 data->res.fattr = NULL;
3482         } else
3483                 data->args.bitmask = server->cache_consistency_bitmask;
3484         if (!data->write_done_cb)
3485                 data->write_done_cb = nfs4_write_done_cb;
3486         data->res.server = server;
3487         data->timestamp   = jiffies;
3488 
3489         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3490         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3491 }
3492 
3493 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3494 {
3495         if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3496                                 &data->args.seq_args,
3497                                 &data->res.seq_res,
3498                                 task))
3499                 return;
3500         rpc_call_start(task);
3501 }
3502 
3503 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3504 {
3505         struct inode *inode = data->inode;
3506 
3507         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3508                 rpc_restart_call_prepare(task);
3509                 return -EAGAIN;
3510         }
3511         nfs_refresh_inode(inode, data->res.fattr);
3512         return 0;
3513 }
3514 
3515 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3516 {
3517         if (!nfs4_sequence_done(task, &data->res.seq_res))
3518                 return -EAGAIN;
3519         return data->write_done_cb(task, data);
3520 }
3521 
3522 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3523 {
3524         struct nfs_server *server = NFS_SERVER(data->inode);
3525 
3526         if (data->lseg) {
3527                 data->args.bitmask = NULL;
3528                 data->res.fattr = NULL;
3529         } else
3530                 data->args.bitmask = server->cache_consistency_bitmask;
3531         if (!data->write_done_cb)
3532                 data->write_done_cb = nfs4_commit_done_cb;
3533         data->res.server = server;
3534         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3535         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3536 }
3537 
3538 struct nfs4_renewdata {
3539         struct nfs_client       *client;
3540         unsigned long           timestamp;
3541 };
3542 
3543 /*
3544  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3545  * standalone procedure for queueing an asynchronous RENEW.
3546  */
3547 static void nfs4_renew_release(void *calldata)
3548 {
3549         struct nfs4_renewdata *data = calldata;
3550         struct nfs_client *clp = data->client;
3551 
3552         if (atomic_read(&clp->cl_count) > 1)
3553                 nfs4_schedule_state_renewal(clp);
3554         nfs_put_client(clp);
3555         kfree(data);
3556 }
3557 
3558 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3559 {
3560         struct nfs4_renewdata *data = calldata;
3561         struct nfs_client *clp = data->client;
3562         unsigned long timestamp = data->timestamp;
3563 
3564         if (task->tk_status < 0) {
3565                 /* Unless we're shutting down, schedule state recovery! */
3566                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3567                         return;
3568                 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3569                         nfs4_schedule_lease_recovery(clp);
3570                         return;
3571                 }
3572                 nfs4_schedule_path_down_recovery(clp);
3573         }
3574         do_renew_lease(clp, timestamp);
3575 }
3576 
3577 static const struct rpc_call_ops nfs4_renew_ops = {
3578         .rpc_call_done = nfs4_renew_done,
3579         .rpc_release = nfs4_renew_release,
3580 };
3581 
3582 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3583 {
3584         struct rpc_message msg = {
3585                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3586                 .rpc_argp       = clp,
3587                 .rpc_cred       = cred,
3588         };
3589         struct nfs4_renewdata *data;
3590 
3591         if (renew_flags == 0)
3592                 return 0;
3593         if (!atomic_inc_not_zero(&clp->cl_count))
3594                 return -EIO;
3595         data = kmalloc(sizeof(*data), GFP_NOFS);
3596         if (data == NULL)
3597                 return -ENOMEM;
3598         data->client = clp;
3599         data->timestamp = jiffies;
3600         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3601                         &nfs4_renew_ops, data);
3602 }
3603 
3604 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3605 {
3606         struct rpc_message msg = {
3607                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3608                 .rpc_argp       = clp,
3609                 .rpc_cred       = cred,
3610         };
3611         unsigned long now = jiffies;
3612         int status;
3613 
3614         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3615         if (status < 0)
3616                 return status;
3617         do_renew_lease(clp, now);
3618         return 0;
3619 }
3620 
3621 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3622 {
3623         return (server->caps & NFS_CAP_ACLS)
3624                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3625                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3626 }
3627 
3628 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3629  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3630  * the stack.
3631  */
3632 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3633 
3634 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3635                 struct page **pages, unsigned int *pgbase)
3636 {
3637         struct page *newpage, **spages;
3638         int rc = 0;
3639         size_t len;
3640         spages = pages;
3641 
3642         do {
3643                 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3644                 newpage = alloc_page(GFP_KERNEL);
3645 
3646                 if (newpage == NULL)
3647                         goto unwind;
3648                 memcpy(page_address(newpage), buf, len);
3649                 buf += len;
3650                 buflen -= len;
3651                 *pages++ = newpage;
3652                 rc++;
3653         } while (buflen != 0);
3654 
3655         return rc;
3656 
3657 unwind:
3658         for(; rc > 0; rc--)
3659                 __free_page(spages[rc-1]);
3660         return -ENOMEM;
3661 }
3662 
3663 struct nfs4_cached_acl {
3664         int cached;
3665         size_t len;
3666         char data[0];
3667 };
3668 
3669 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3670 {
3671         struct nfs_inode *nfsi = NFS_I(inode);
3672 
3673         spin_lock(&inode->i_lock);
3674         kfree(nfsi->nfs4_acl);
3675         nfsi->nfs4_acl = acl;
3676         spin_unlock(&inode->i_lock);
3677 }
3678 
3679 static void nfs4_zap_acl_attr(struct inode *inode)
3680 {
3681         nfs4_set_cached_acl(inode, NULL);
3682 }
3683 
3684 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3685 {
3686         struct nfs_inode *nfsi = NFS_I(inode);
3687         struct nfs4_cached_acl *acl;
3688         int ret = -ENOENT;
3689 
3690         spin_lock(&inode->i_lock);
3691         acl = nfsi->nfs4_acl;
3692         if (acl == NULL)
3693                 goto out;
3694         if (buf == NULL) /* user is just asking for length */
3695                 goto out_len;
3696         if (acl->cached == 0)
3697                 goto out;
3698         ret = -ERANGE; /* see getxattr(2) man page */
3699         if (acl->len > buflen)
3700                 goto out;
3701         memcpy(buf, acl->data, acl->len);
3702 out_len:
3703         ret = acl->len;
3704 out:
3705         spin_unlock(&inode->i_lock);
3706         return ret;
3707 }
3708 
3709 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3710 {
3711         struct nfs4_cached_acl *acl;
3712 
3713         if (pages && acl_len <= PAGE_SIZE) {
3714                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3715                 if (acl == NULL)
3716                         goto out;
3717                 acl->cached = 1;
3718                 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3719         } else {
3720                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3721                 if (acl == NULL)
3722                         goto out;
3723                 acl->cached = 0;
3724         }
3725         acl->len = acl_len;
3726 out:
3727         nfs4_set_cached_acl(inode, acl);
3728 }
3729 
3730 /*
3731  * The getxattr API returns the required buffer length when called with a
3732  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3733  * the required buf.  On a NULL buf, we send a page of data to the server
3734  * guessing that the ACL request can be serviced by a page. If so, we cache
3735  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3736  * the cache. If not so, we throw away the page, and cache the required
3737  * length. The next getxattr call will then produce another round trip to
3738  * the server, this time with the input buf of the required size.
3739  */
3740 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3741 {
3742         struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3743         struct nfs_getaclargs args = {
3744                 .fh = NFS_FH(inode),
3745                 .acl_pages = pages,
3746                 .acl_len = buflen,
3747         };
3748         struct nfs_getaclres res = {
3749                 .acl_len = buflen,
3750         };
3751         struct rpc_message msg = {
3752                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3753                 .rpc_argp = &args,
3754                 .rpc_resp = &res,
3755         };
3756         int ret = -ENOMEM, npages, i;
3757         size_t acl_len = 0;
3758 
3759         npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3760         /* As long as we're doing a round trip to the server anyway,
3761          * let's be prepared for a page of acl data. */
3762         if (npages == 0)
3763                 npages = 1;
3764 
3765         /* Add an extra page to handle the bitmap returned */
3766         npages++;
3767 
3768         for (i = 0; i < npages; i++) {
3769                 pages[i] = alloc_page(GFP_KERNEL);
3770                 if (!pages[i])
3771                         goto out_free;
3772         }
3773 
3774         /* for decoding across pages */
3775         res.acl_scratch = alloc_page(GFP_KERNEL);
3776         if (!res.acl_scratch)
3777                 goto out_free;
3778 
3779         args.acl_len = npages * PAGE_SIZE;
3780         args.acl_pgbase = 0;
3781 
3782         /* Let decode_getfacl know not to fail if the ACL data is larger than
3783          * the page we send as a guess */
3784         if (buf == NULL)
3785                 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3786 
3787         dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
3788                 __func__, buf, buflen, npages, args.acl_len);
3789         ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3790                              &msg, &args.seq_args, &res.seq_res, 0);
3791         if (ret)
3792                 goto out_free;
3793 
3794         acl_len = res.acl_len - res.acl_data_offset;
3795         if (acl_len > args.acl_len)
3796                 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3797         else
3798                 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3799                                       acl_len);
3800         if (buf) {
3801                 ret = -ERANGE;
3802                 if (acl_len > buflen)
3803                         goto out_free;
3804                 _copy_from_pages(buf, pages, res.acl_data_offset,
3805                                 acl_len);
3806         }
3807         ret = acl_len;
3808 out_free:
3809         for (i = 0; i < npages; i++)
3810                 if (pages[i])
3811                         __free_page(pages[i]);
3812         if (res.acl_scratch)
3813                 __free_page(res.acl_scratch);
3814         return ret;
3815 }
3816 
3817 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3818 {
3819         struct nfs4_exception exception = { };
3820         ssize_t ret;
3821         do {
3822                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3823                 if (ret >= 0)
3824                         break;
3825                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3826         } while (exception.retry);
3827         return ret;
3828 }
3829 
3830 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3831 {
3832         struct nfs_server *server = NFS_SERVER(inode);
3833         int ret;
3834 
3835         if (!nfs4_server_supports_acls(server))
3836                 return -EOPNOTSUPP;
3837         ret = nfs_revalidate_inode(server, inode);
3838         if (ret < 0)
3839                 return ret;
3840         if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3841                 nfs_zap_acl_cache(inode);
3842         ret = nfs4_read_cached_acl(inode, buf, buflen);
3843         if (ret != -ENOENT)
3844                 /* -ENOENT is returned if there is no ACL or if there is an ACL
3845                  * but no cached acl data, just the acl length */
3846                 return ret;
3847         return nfs4_get_acl_uncached(inode, buf, buflen);
3848 }
3849 
3850 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3851 {
3852         struct nfs_server *server = NFS_SERVER(inode);
3853         struct page *pages[NFS4ACL_MAXPAGES];
3854         struct nfs_setaclargs arg = {
3855                 .fh             = NFS_FH(inode),
3856                 .acl_pages      = pages,
3857                 .acl_len        = buflen,
3858         };
3859         struct nfs_setaclres res;
3860         struct rpc_message msg = {
3861                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3862                 .rpc_argp       = &arg,
3863                 .rpc_resp       = &res,
3864         };
3865         int ret, i;
3866 
3867         if (!nfs4_server_supports_acls(server))
3868                 return -EOPNOTSUPP;
3869         i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3870         if (i < 0)
3871                 return i;
3872         nfs_inode_return_delegation(inode);
3873         ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3874 
3875         /*
3876          * Free each page after tx, so the only ref left is
3877          * held by the network stack
3878          */
3879         for (; i > 0; i--)
3880                 put_page(pages[i-1]);
3881 
3882         /*
3883          * Acl update can result in inode attribute update.
3884          * so mark the attribute cache invalid.
3885          */
3886         spin_lock(&inode->i_lock);
3887         NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3888         spin_unlock(&inode->i_lock);
3889         nfs_access_zap_cache(inode);
3890         nfs_zap_acl_cache(inode);
3891         return ret;
3892 }
3893 
3894 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3895 {
3896         struct nfs4_exception exception = { };
3897         int err;
3898         do {
3899                 err = nfs4_handle_exception(NFS_SERVER(inode),
3900                                 __nfs4_proc_set_acl(inode, buf, buflen),
3901                                 &exception);
3902         } while (exception.retry);
3903         return err;
3904 }
3905 
3906 static int
3907 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3908 {
3909         struct nfs_client *clp = server->nfs_client;
3910 
3911         if (task->tk_status >= 0)
3912                 return 0;
3913         switch(task->tk_status) {
3914                 case -NFS4ERR_DELEG_REVOKED:
3915                 case -NFS4ERR_ADMIN_REVOKED:
3916                 case -NFS4ERR_BAD_STATEID:
3917                         if (state == NULL)
3918                                 break;
3919                         nfs_remove_bad_delegation(state->inode);
3920                 case -NFS4ERR_OPENMODE:
3921                         if (state == NULL)
3922                                 break;
3923                         nfs4_schedule_stateid_recovery(server, state);
3924                         goto wait_on_recovery;
3925                 case -NFS4ERR_EXPIRED:
3926                         if (state != NULL)
3927                                 nfs4_schedule_stateid_recovery(server, state);
3928                 case -NFS4ERR_STALE_STATEID:
3929                 case -NFS4ERR_STALE_CLIENTID:
3930                         nfs4_schedule_lease_recovery(clp);
3931                         goto wait_on_recovery;
3932 #if defined(CONFIG_NFS_V4_1)
3933                 case -NFS4ERR_BADSESSION:
3934                 case -NFS4ERR_BADSLOT:
3935                 case -NFS4ERR_BAD_HIGH_SLOT:
3936                 case -NFS4ERR_DEADSESSION:
3937                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3938                 case -NFS4ERR_SEQ_FALSE_RETRY:
3939                 case -NFS4ERR_SEQ_MISORDERED:
3940                         dprintk("%s ERROR %d, Reset session\n", __func__,
3941                                 task->tk_status);
3942                         nfs4_schedule_session_recovery(clp->cl_session);
3943                         goto wait_on_recovery;
3944 #endif /* CONFIG_NFS_V4_1 */
3945                 case -NFS4ERR_DELAY:
3946                         nfs_inc_server_stats(server, NFSIOS_DELAY);
3947                 case -NFS4ERR_GRACE:
3948                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
3949                         task->tk_status = 0;
3950                         return -EAGAIN;
3951                 case -NFS4ERR_RETRY_UNCACHED_REP:
3952                 case -NFS4ERR_OLD_STATEID:
3953                         task->tk_status = 0;
3954                         return -EAGAIN;
3955         }
3956         task->tk_status = nfs4_map_errors(task->tk_status);
3957         return 0;
3958 wait_on_recovery:
3959         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3960         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3961                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3962         task->tk_status = 0;
3963         return -EAGAIN;
3964 }
3965 
3966 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3967                                          nfs4_verifier *bootverf)
3968 {
3969         __be32 verf[2];
3970 
3971         verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3972         verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3973         memcpy(bootverf->data, verf, sizeof(bootverf->data));
3974 }
3975 
3976 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3977                 unsigned short port, struct rpc_cred *cred,
3978                 struct nfs4_setclientid_res *res)
3979 {
3980         nfs4_verifier sc_verifier;
3981         struct nfs4_setclientid setclientid = {
3982                 .sc_verifier = &sc_verifier,
3983                 .sc_prog = program,
3984                 .sc_cb_ident = clp->cl_cb_ident,
3985         };
3986         struct rpc_message msg = {
3987                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3988                 .rpc_argp = &setclientid,
3989                 .rpc_resp = res,
3990                 .rpc_cred = cred,
3991         };
3992         int loop = 0;
3993         int status;
3994 
3995         nfs4_construct_boot_verifier(clp, &sc_verifier);
3996 
3997         for(;;) {
3998                 rcu_read_lock();
3999                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
4000                                 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
4001                                 clp->cl_ipaddr,
4002                                 rpc_peeraddr2str(clp->cl_rpcclient,
4003                                                         RPC_DISPLAY_ADDR),
4004                                 rpc_peeraddr2str(clp->cl_rpcclient,
4005                                                         RPC_DISPLAY_PROTO),
4006                                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4007                                 clp->cl_id_uniquifier);
4008                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4009                                 sizeof(setclientid.sc_netid),
4010                                 rpc_peeraddr2str(clp->cl_rpcclient,
4011                                                         RPC_DISPLAY_NETID));
4012                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4013                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4014                                 clp->cl_ipaddr, port >> 8, port & 255);
4015                 rcu_read_unlock();
4016 
4017                 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4018                 if (status != -NFS4ERR_CLID_INUSE)
4019                         break;
4020                 if (loop != 0) {
4021                         ++clp->cl_id_uniquifier;
4022                         break;
4023                 }
4024                 ++loop;
4025                 ssleep(clp->cl_lease_time / HZ + 1);
4026         }
4027         return status;
4028 }
4029 
4030 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4031                 struct nfs4_setclientid_res *arg,
4032                 struct rpc_cred *cred)
4033 {
4034         struct nfs_fsinfo fsinfo;
4035         struct rpc_message msg = {
4036                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4037                 .rpc_argp = arg,
4038                 .rpc_resp = &fsinfo,
4039                 .rpc_cred = cred,
4040         };
4041         unsigned long now;
4042         int status;
4043 
4044         now = jiffies;
4045         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4046         if (status == 0) {
4047                 spin_lock(&clp->cl_lock);
4048                 clp->cl_lease_time = fsinfo.lease_time * HZ;
4049                 clp->cl_last_renewal = now;
4050                 spin_unlock(&clp->cl_lock);
4051         }
4052         return status;
4053 }
4054 
4055 struct nfs4_delegreturndata {
4056         struct nfs4_delegreturnargs args;
4057         struct nfs4_delegreturnres res;
4058         struct nfs_fh fh;
4059         nfs4_stateid stateid;
4060         unsigned long timestamp;
4061         struct nfs_fattr fattr;
4062         int rpc_status;
4063 };
4064 
4065 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4066 {
4067         struct nfs4_delegreturndata *data = calldata;
4068 
4069         if (!nfs4_sequence_done(task, &data->res.seq_res))
4070                 return;
4071 
4072         switch (task->tk_status) {
4073         case 0:
4074                 renew_lease(data->res.server, data->timestamp);
4075                 break;
4076         case -NFS4ERR_ADMIN_REVOKED:
4077         case -NFS4ERR_DELEG_REVOKED:
4078         case -NFS4ERR_BAD_STATEID:
4079         case -NFS4ERR_OLD_STATEID:
4080         case -NFS4ERR_STALE_STATEID:
4081         case -NFS4ERR_EXPIRED:
4082                 task->tk_status = 0;
4083                 break;
4084         default:
4085                 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4086                                 -EAGAIN) {
4087                         rpc_restart_call_prepare(task);
4088                         return;
4089                 }
4090         }
4091         data->rpc_status = task->tk_status;
4092 }
4093 
4094 static void nfs4_delegreturn_release(void *calldata)
4095 {
4096         kfree(calldata);
4097 }
4098 
4099 #if defined(CONFIG_NFS_V4_1)
4100 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4101 {
4102         struct nfs4_delegreturndata *d_data;
4103 
4104         d_data = (struct nfs4_delegreturndata *)data;
4105 
4106         if (nfs4_setup_sequence(d_data->res.server,
4107                                 &d_data->args.seq_args,
4108                                 &d_data->res.seq_res, task))
4109                 return;
4110         rpc_call_start(task);
4111 }
4112 #endif /* CONFIG_NFS_V4_1 */
4113 
4114 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4115 #if defined(CONFIG_NFS_V4_1)
4116         .rpc_call_prepare = nfs4_delegreturn_prepare,
4117 #endif /* CONFIG_NFS_V4_1 */
4118         .rpc_call_done = nfs4_delegreturn_done,
4119         .rpc_release = nfs4_delegreturn_release,
4120 };
4121 
4122 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4123 {
4124         struct nfs4_delegreturndata *data;
4125         struct nfs_server *server = NFS_SERVER(inode);
4126         struct rpc_task *task;
4127         struct rpc_message msg = {
4128                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4129                 .rpc_cred = cred,
4130         };
4131         struct rpc_task_setup task_setup_data = {
4132                 .rpc_client = server->client,
4133                 .rpc_message = &msg,
4134                 .callback_ops = &nfs4_delegreturn_ops,
4135                 .flags = RPC_TASK_ASYNC,
4136         };
4137         int status = 0;
4138 
4139         data = kzalloc(sizeof(*data), GFP_NOFS);
4140         if (data == NULL)
4141                 return -ENOMEM;
4142         nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4143         data->args.fhandle = &data->fh;
4144         data->args.stateid = &data->stateid;
4145         data->args.bitmask = server->attr_bitmask;
4146         nfs_copy_fh(&data->fh, NFS_FH(inode));
4147         nfs4_stateid_copy(&data->stateid, stateid);
4148         data->res.fattr = &data->fattr;
4149         data->res.server = server;
4150         nfs_fattr_init(data->res.fattr);
4151         data->timestamp = jiffies;
4152         data->rpc_status = 0;
4153 
4154         task_setup_data.callback_data = data;
4155         msg.rpc_argp = &data->args;
4156         msg.rpc_resp = &data->res;
4157         task = rpc_run_task(&task_setup_data);
4158         if (IS_ERR(task))
4159                 return PTR_ERR(task);
4160         if (!issync)
4161                 goto out;
4162         status = nfs4_wait_for_completion_rpc_task(task);
4163         if (status != 0)
4164                 goto out;
4165         status = data->rpc_status;
4166         if (status != 0)
4167                 goto out;
4168         nfs_refresh_inode(inode, &data->fattr);
4169 out:
4170         rpc_put_task(task);
4171         return status;
4172 }
4173 
4174 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4175 {
4176         struct nfs_server *server = NFS_SERVER(inode);
4177         struct nfs4_exception exception = { };
4178         int err;
4179         do {
4180                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4181                 switch (err) {
4182                         case -NFS4ERR_STALE_STATEID:
4183                         case -NFS4ERR_EXPIRED:
4184                         case 0:
4185                                 return 0;
4186                 }
4187                 err = nfs4_handle_exception(server, err, &exception);
4188         } while (exception.retry);
4189         return err;
4190 }
4191 
4192 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4193 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4194 
4195 /* 
4196  * sleep, with exponential backoff, and retry the LOCK operation. 
4197  */
4198 static unsigned long
4199 nfs4_set_lock_task_retry(unsigned long timeout)
4200 {
4201         freezable_schedule_timeout_killable(timeout);
4202         timeout <<= 1;
4203         if (timeout > NFS4_LOCK_MAXTIMEOUT)
4204                 return NFS4_LOCK_MAXTIMEOUT;
4205         return timeout;
4206 }
4207 
4208 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4209 {
4210         struct inode *inode = state->inode;
4211         struct nfs_server *server = NFS_SERVER(inode);
4212         struct nfs_client *clp = server->nfs_client;
4213         struct nfs_lockt_args arg = {
4214                 .fh = NFS_FH(inode),
4215                 .fl = request,
4216         };
4217         struct nfs_lockt_res res = {
4218                 .denied = request,
4219         };
4220         struct rpc_message msg = {
4221                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4222                 .rpc_argp       = &arg,
4223                 .rpc_resp       = &res,
4224                 .rpc_cred       = state->owner->so_cred,
4225         };
4226         struct nfs4_lock_state *lsp;
4227         int status;
4228 
4229         arg.lock_owner.clientid = clp->cl_clientid;
4230         status = nfs4_set_lock_state(state, request);
4231         if (status != 0)
4232                 goto out;
4233         lsp = request->fl_u.nfs4_fl.owner;
4234         arg.lock_owner.id = lsp->ls_seqid.owner_id;
4235         arg.lock_owner.s_dev = server->s_dev;
4236         status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4237         switch (status) {
4238                 case 0:
4239                         request->fl_type = F_UNLCK;
4240                         break;
4241                 case -NFS4ERR_DENIED:
4242                         status = 0;
4243         }
4244         request->fl_ops->fl_release_private(request);
4245         request->fl_ops = NULL;
4246 out:
4247         return status;
4248 }
4249 
4250 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4251 {
4252         struct nfs4_exception exception = { };
4253         int err;
4254 
4255         do {
4256                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4257                                 _nfs4_proc_getlk(state, cmd, request),
4258                                 &exception);
4259         } while (exception.retry);
4260         return err;
4261 }
4262 
4263 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4264 {
4265         int res = 0;
4266         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4267                 case FL_POSIX:
4268                         res = posix_lock_file_wait(file, fl);
4269                         break;
4270                 case FL_FLOCK:
4271                         res = flock_lock_file_wait(file, fl);
4272                         break;
4273                 default:
4274                         BUG();
4275         }
4276         return res;
4277 }
4278 
4279 struct nfs4_unlockdata {
4280         struct nfs_locku_args arg;
4281         struct nfs_locku_res res;
4282         struct nfs4_lock_state *lsp;
4283         struct nfs_open_context *ctx;
4284         struct file_lock fl;
4285         const struct nfs_server *server;
4286         unsigned long timestamp;
4287 };
4288 
4289 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4290                 struct nfs_open_context *ctx,
4291                 struct nfs4_lock_state *lsp,
4292                 struct nfs_seqid *seqid)
4293 {
4294         struct nfs4_unlockdata *p;
4295         struct inode *inode = lsp->ls_state->inode;
4296 
4297         p = kzalloc(sizeof(*p), GFP_NOFS);
4298         if (p == NULL)
4299                 return NULL;
4300         p->arg.fh = NFS_FH(inode);
4301         p->arg.fl = &p->fl;
4302         p->arg.seqid = seqid;
4303         p->res.seqid = seqid;
4304         p->arg.stateid = &lsp->ls_stateid;
4305         p->lsp = lsp;
4306         atomic_inc(&lsp->ls_count);
4307         /* Ensure we don't close file until we're done freeing locks! */
4308         p->ctx = get_nfs_open_context(ctx);
4309         memcpy(&p->fl, fl, sizeof(p->fl));
4310         p->server = NFS_SERVER(inode);
4311         return p;
4312 }
4313 
4314 static void nfs4_locku_release_calldata(void *data)
4315 {
4316         struct nfs4_unlockdata *calldata = data;
4317         nfs_free_seqid(calldata->arg.seqid);
4318         nfs4_put_lock_state(calldata->lsp);
4319         put_nfs_open_context(calldata->ctx);
4320         kfree(calldata);
4321 }
4322 
4323 static void nfs4_locku_done(struct rpc_task *task, void *data)
4324 {
4325         struct nfs4_unlockdata *calldata = data;
4326 
4327         if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4328                 return;
4329         switch (task->tk_status) {
4330                 case 0:
4331                         nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4332                                         &calldata->res.stateid);
4333                         renew_lease(calldata->server, calldata->timestamp);
4334                         break;
4335                 case -NFS4ERR_BAD_STATEID:
4336                 case -NFS4ERR_OLD_STATEID:
4337                 case -NFS4ERR_STALE_STATEID:
4338                 case -NFS4ERR_EXPIRED:
4339                         break;
4340                 default:
4341                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4342                                 rpc_restart_call_prepare(task);
4343         }
4344         nfs_release_seqid(calldata->arg.seqid);
4345 }
4346 
4347 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4348 {
4349         struct nfs4_unlockdata *calldata = data;
4350 
4351         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4352                 return;
4353         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4354                 /* Note: exit _without_ running nfs4_locku_done */
4355                 task->tk_action = NULL;
4356                 return;
4357         }
4358         calldata->timestamp = jiffies;
4359         if (nfs4_setup_sequence(calldata->server,
4360                                 &calldata->arg.seq_args,
4361                                 &calldata->res.seq_res,
4362                                 task) != 0)
4363                 nfs_release_seqid(calldata->arg.seqid);
4364         else
4365                 rpc_call_start(task);
4366 }
4367 
4368 static const struct rpc_call_ops nfs4_locku_ops = {
4369         .rpc_call_prepare = nfs4_locku_prepare,
4370         .rpc_call_done = nfs4_locku_done,
4371         .rpc_release = nfs4_locku_release_calldata,
4372 };
4373 
4374 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4375                 struct nfs_open_context *ctx,
4376                 struct nfs4_lock_state *lsp,
4377                 struct nfs_seqid *seqid)
4378 {
4379         struct nfs4_unlockdata *data;
4380         struct rpc_message msg = {
4381                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4382                 .rpc_cred = ctx->cred,
4383         };
4384         struct rpc_task_setup task_setup_data = {
4385                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4386                 .rpc_message = &msg,
4387                 .callback_ops = &nfs4_locku_ops,
4388                 .workqueue = nfsiod_workqueue,
4389                 .flags = RPC_TASK_ASYNC,
4390         };
4391 
4392         /* Ensure this is an unlock - when canceling a lock, the
4393          * canceled lock is passed in, and it won't be an unlock.
4394          */
4395         fl->fl_type = F_UNLCK;
4396 
4397         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4398         if (data == NULL) {
4399                 nfs_free_seqid(seqid);
4400                 return ERR_PTR(-ENOMEM);
4401         }
4402 
4403         nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4404         msg.rpc_argp = &data->arg;
4405         msg.rpc_resp = &data->res;
4406         task_setup_data.callback_data = data;
4407         return rpc_run_task(&task_setup_data);
4408 }
4409 
4410 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4411 {
4412         struct nfs_inode *nfsi = NFS_I(state->inode);
4413         struct nfs_seqid *seqid;
4414         struct nfs4_lock_state *lsp;
4415         struct rpc_task *task;
4416         int status = 0;
4417         unsigned char fl_flags = request->fl_flags;
4418 
4419         status = nfs4_set_lock_state(state, request);
4420         /* Unlock _before_ we do the RPC call */
4421         request->fl_flags |= FL_EXISTS;
4422         down_read(&nfsi->rwsem);
4423         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4424                 up_read(&nfsi->rwsem);
4425                 goto out;
4426         }
4427         up_read(&nfsi->rwsem);
4428         if (status != 0)
4429                 goto out;
4430         /* Is this a delegated lock? */
4431         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4432                 goto out;
4433         lsp = request->fl_u.nfs4_fl.owner;
4434         seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4435         status = -ENOMEM;
4436         if (seqid == NULL)
4437                 goto out;
4438         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4439         status = PTR_ERR(task);
4440         if (IS_ERR(task))
4441                 goto out;
4442         status = nfs4_wait_for_completion_rpc_task(task);
4443         rpc_put_task(task);
4444 out:
4445         request->fl_flags = fl_flags;
4446         return status;
4447 }
4448 
4449 struct nfs4_lockdata {
4450         struct nfs_lock_args arg;
4451         struct nfs_lock_res res;
4452         struct nfs4_lock_state *lsp;
4453         struct nfs_open_context *ctx;
4454         struct file_lock fl;
4455         unsigned long timestamp;
4456         int rpc_status;
4457         int cancelled;
4458         struct nfs_server *server;
4459 };
4460 
4461 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4462                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4463                 gfp_t gfp_mask)
4464 {
4465         struct nfs4_lockdata *p;
4466         struct inode *inode = lsp->ls_state->inode;
4467         struct nfs_server *server = NFS_SERVER(inode);
4468 
4469         p = kzalloc(sizeof(*p), gfp_mask);
4470         if (p == NULL)
4471                 return NULL;
4472 
4473         p->arg.fh = NFS_FH(inode);
4474         p->arg.fl = &p->fl;
4475         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4476         if (p->arg.open_seqid == NULL)
4477                 goto out_free;
4478         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4479         if (p->arg.lock_seqid == NULL)
4480                 goto out_free_seqid;
4481         p->arg.lock_stateid = &lsp->ls_stateid;
4482         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4483         p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4484         p->arg.lock_owner.s_dev = server->s_dev;
4485         p->res.lock_seqid = p->arg.lock_seqid;
4486         p->lsp = lsp;
4487         p->server = server;
4488         atomic_inc(&lsp->ls_count);
4489         p->ctx = get_nfs_open_context(ctx);
4490         memcpy(&p->fl, fl, sizeof(p->fl));
4491         return p;
4492 out_free_seqid:
4493         nfs_free_seqid(p->arg.open_seqid);
4494 out_free:
4495         kfree(p);
4496         return NULL;
4497 }
4498 
4499 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4500 {
4501         struct nfs4_lockdata *data = calldata;
4502         struct nfs4_state *state = data->lsp->ls_state;
4503 
4504         dprintk("%s: begin!\n", __func__);
4505         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4506                 return;
4507         /* Do we need to do an open_to_lock_owner? */
4508         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4509                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4510                         goto out_release_lock_seqid;
4511                 data->arg.open_stateid = &state->stateid;
4512                 data->arg.new_lock_owner = 1;
4513                 data->res.open_seqid = data->arg.open_seqid;
4514         } else
4515                 data->arg.new_lock_owner = 0;
4516         data->timestamp = jiffies;
4517         if (nfs4_setup_sequence(data->server,
4518                                 &data->arg.seq_args,
4519                                 &data->res.seq_res,
4520                                 task) == 0) {
4521                 rpc_call_start(task);
4522                 return;
4523         }
4524         nfs_release_seqid(data->arg.open_seqid);
4525 out_release_lock_seqid:
4526         nfs_release_seqid(data->arg.lock_seqid);
4527         dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4528 }
4529 
4530 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4531 {
4532         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4533         nfs4_lock_prepare(task, calldata);
4534 }
4535 
4536 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4537 {
4538         struct nfs4_lockdata *data = calldata;
4539 
4540         dprintk("%s: begin!\n", __func__);
4541 
4542         if (!nfs4_sequence_done(task, &data->res.seq_res))
4543                 return;
4544 
4545         data->rpc_status = task->tk_status;
4546         if (data->arg.new_lock_owner != 0) {
4547                 if (data->rpc_status == 0)
4548                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4549                 else
4550                         goto out;
4551         }
4552         if (data->rpc_status == 0) {
4553                 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4554                 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4555                 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4556         }
4557 out:
4558         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4559 }
4560 
4561 static void nfs4_lock_release(void *calldata)
4562 {
4563         struct nfs4_lockdata *data = calldata;
4564 
4565         dprintk("%s: begin!\n", __func__);
4566         nfs_free_seqid(data->arg.open_seqid);
4567         if (data->cancelled != 0) {
4568                 struct rpc_task *task;
4569                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4570                                 data->arg.lock_seqid);
4571                 if (!IS_ERR(task))
4572                         rpc_put_task_async(task);
4573                 dprintk("%s: cancelling lock!\n", __func__);
4574         } else
4575                 nfs_free_seqid(data->arg.lock_seqid);
4576         nfs4_put_lock_state(data->lsp);
4577         put_nfs_open_context(data->ctx);
4578         kfree(data);
4579         dprintk("%s: done!\n", __func__);
4580 }
4581 
4582 static const struct rpc_call_ops nfs4_lock_ops = {
4583         .rpc_call_prepare = nfs4_lock_prepare,
4584         .rpc_call_done = nfs4_lock_done,
4585         .rpc_release = nfs4_lock_release,
4586 };
4587 
4588 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4589         .rpc_call_prepare = nfs4_recover_lock_prepare,
4590         .rpc_call_done = nfs4_lock_done,
4591         .rpc_release = nfs4_lock_release,
4592 };
4593 
4594 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4595 {
4596         switch (error) {
4597         case -NFS4ERR_ADMIN_REVOKED:
4598         case -NFS4ERR_BAD_STATEID:
4599                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4600                 if (new_lock_owner != 0 ||
4601                    (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4602                         nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4603                 break;
4604         case -NFS4ERR_STALE_STATEID:
4605                 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4606         case -NFS4ERR_EXPIRED:
4607                 nfs4_schedule_lease_recovery(server->nfs_client);
4608         };
4609 }
4610 
4611 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4612 {
4613         struct nfs4_lockdata *data;
4614         struct rpc_task *task;
4615         struct rpc_message msg = {
4616                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4617                 .rpc_cred = state->owner->so_cred,
4618         };
4619         struct rpc_task_setup task_setup_data = {
4620                 .rpc_client = NFS_CLIENT(state->inode),
4621                 .rpc_message = &msg,
4622                 .callback_ops = &nfs4_lock_ops,
4623                 .workqueue = nfsiod_workqueue,
4624                 .flags = RPC_TASK_ASYNC,
4625         };
4626         int ret;
4627 
4628         dprintk("%s: begin!\n", __func__);
4629         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4630                         fl->fl_u.nfs4_fl.owner,
4631                         recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4632         if (data == NULL)
4633                 return -ENOMEM;
4634         if (IS_SETLKW(cmd))
4635                 data->arg.block = 1;
4636         if (recovery_type > NFS_LOCK_NEW) {
4637                 if (recovery_type == NFS_LOCK_RECLAIM)
4638                         data->arg.reclaim = NFS_LOCK_RECLAIM;
4639                 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4640         }
4641         nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4642         msg.rpc_argp = &data->arg;
4643         msg.rpc_resp = &data->res;
4644         task_setup_data.callback_data = data;
4645         task = rpc_run_task(&task_setup_data);
4646         if (IS_ERR(task))
4647                 return PTR_ERR(task);
4648         ret = nfs4_wait_for_completion_rpc_task(task);
4649         if (ret == 0) {
4650                 ret = data->rpc_status;
4651                 if (ret)
4652                         nfs4_handle_setlk_error(data->server, data->lsp,
4653                                         data->arg.new_lock_owner, ret);
4654         } else
4655                 data->cancelled = 1;
4656         rpc_put_task(task);
4657         dprintk("%s: done, ret = %d!\n", __func__, ret);
4658         return ret;
4659 }
4660 
4661 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4662 {
4663         struct nfs_server *server = NFS_SERVER(state->inode);
4664         struct nfs4_exception exception = {
4665                 .inode = state->inode,
4666         };
4667         int err;
4668 
4669         do {
4670                 /* Cache the lock if possible... */
4671                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4672                         return 0;
4673                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4674                 if (err != -NFS4ERR_DELAY)
4675                         break;
4676                 nfs4_handle_exception(server, err, &exception);
4677         } while (exception.retry);
4678         return err;
4679 }
4680 
4681 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4682 {
4683         struct nfs_server *server = NFS_SERVER(state->inode);
4684         struct nfs4_exception exception = {
4685                 .inode = state->inode,
4686         };
4687         int err;
4688 
4689         err = nfs4_set_lock_state(state, request);
4690         if (err != 0)
4691                 return err;
4692         do {
4693                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4694                         return 0;
4695                 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4696                 switch (err) {
4697                 default:
4698                         goto out;
4699                 case -NFS4ERR_GRACE:
4700                 case -NFS4ERR_DELAY:
4701                         nfs4_handle_exception(server, err, &exception);
4702                         err = 0;
4703                 }
4704         } while (exception.retry);
4705 out:
4706         return err;
4707 }
4708 
4709 #if defined(CONFIG_NFS_V4_1)
4710 static int nfs41_check_expired_locks(struct nfs4_state *state)
4711 {
4712         int status, ret = NFS_OK;
4713         struct nfs4_lock_state *lsp;
4714         struct nfs_server *server = NFS_SERVER(state->inode);
4715 
4716         list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4717                 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4718                         status = nfs41_test_stateid(server, &lsp->ls_stateid);
4719                         if (status != NFS_OK) {
4720                                 nfs41_free_stateid(server, &lsp->ls_stateid);
4721                                 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4722                                 ret = status;
4723                         }
4724                 }
4725         };
4726 
4727         return ret;
4728 }
4729 
4730 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4731 {
4732         int status = NFS_OK;
4733 
4734         if (test_bit(LK_STATE_IN_USE, &state->flags))
4735                 status = nfs41_check_expired_locks(state);
4736         if (status == NFS_OK)
4737                 return status;
4738         return nfs4_lock_expired(state, request);
4739 }
4740 #endif
4741 
4742 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4743 {
4744         struct nfs_inode *nfsi = NFS_I(state->inode);
4745         unsigned char fl_flags = request->fl_flags;
4746         int status = -ENOLCK;
4747 
4748         if ((fl_flags & FL_POSIX) &&
4749                         !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4750                 goto out;
4751         /* Is this a delegated open? */
4752         status = nfs4_set_lock_state(state, request);
4753         if (status != 0)
4754                 goto out;
4755         request->fl_flags |= FL_ACCESS;
4756         status = do_vfs_lock(request->fl_file, request);
4757         if (status < 0)
4758                 goto out;
4759         down_read(&nfsi->rwsem);
4760         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4761                 /* Yes: cache locks! */
4762                 /* ...but avoid races with delegation recall... */
4763                 request->fl_flags = fl_flags & ~FL_SLEEP;
4764                 status = do_vfs_lock(request->fl_file, request);
4765                 goto out_unlock;
4766         }
4767         status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4768         if (status != 0)
4769                 goto out_unlock;
4770         /* Note: we always want to sleep here! */
4771         request->fl_flags = fl_flags | FL_SLEEP;
4772         if (do_vfs_lock(request->fl_file, request) < 0)
4773                 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4774                         "manager!\n", __func__);
4775 out_unlock:
4776         up_read(&nfsi->rwsem);
4777 out:
4778         request->fl_flags = fl_flags;
4779         return status;
4780 }
4781 
4782 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4783 {
4784         struct nfs4_exception exception = {
4785                 .state = state,
4786                 .inode = state->inode,
4787         };
4788         int err;
4789 
4790         do {
4791                 err = _nfs4_proc_setlk(state, cmd, request);
4792                 if (err == -NFS4ERR_DENIED)
4793                         err = -EAGAIN;
4794                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4795                                 err, &exception);
4796         } while (exception.retry);
4797         return err;
4798 }
4799 
4800 static int
4801 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4802 {
4803         struct nfs_open_context *ctx;
4804         struct nfs4_state *state;
4805         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4806         int status;
4807 
4808         /* verify open state */
4809         ctx = nfs_file_open_context(filp);
4810         state = ctx->state;
4811 
4812         if (request->fl_start < 0 || request->fl_end < 0)
4813                 return -EINVAL;
4814 
4815         if (IS_GETLK(cmd)) {
4816                 if (state != NULL)
4817                         return nfs4_proc_getlk(state, F_GETLK, request);
4818                 return 0;
4819         }
4820 
4821         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4822                 return -EINVAL;
4823 
4824         if (request->fl_type == F_UNLCK) {
4825                 if (state != NULL)
4826                         return nfs4_proc_unlck(state, cmd, request);
4827                 return 0;
4828         }
4829 
4830         if (state == NULL)
4831                 return -ENOLCK;
4832         /*
4833          * Don't rely on the VFS having checked the file open mode,
4834          * since it won't do this for flock() locks.
4835          */
4836         switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4837         case F_RDLCK:
4838                 if (!(filp->f_mode & FMODE_READ))
4839                         return -EBADF;
4840                 break;
4841         case F_WRLCK:
4842                 if (!(filp->f_mode & FMODE_WRITE))
4843                         return -EBADF;
4844         }
4845 
4846         do {
4847                 status = nfs4_proc_setlk(state, cmd, request);
4848                 if ((status != -EAGAIN) || IS_SETLK(cmd))
4849                         break;
4850                 timeout = nfs4_set_lock_task_retry(timeout);
4851                 status = -ERESTARTSYS;
4852                 if (signalled())
4853                         break;
4854         } while(status < 0);
4855         return status;
4856 }
4857 
4858 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4859 {
4860         struct nfs_server *server = NFS_SERVER(state->inode);
4861         struct nfs4_exception exception = { };
4862         int err;
4863 
4864         err = nfs4_set_lock_state(state, fl);
4865         if (err != 0)
4866                 goto out;
4867         do {
4868                 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4869                 switch (err) {
4870                         default:
4871                                 printk(KERN_ERR "NFS: %s: unhandled error "
4872                                         "%d.\n", __func__, err);
4873                         case 0:
4874                         case -ESTALE:
4875                                 goto out;
4876                         case -NFS4ERR_EXPIRED:
4877                                 nfs4_schedule_stateid_recovery(server, state);
4878                         case -NFS4ERR_STALE_CLIENTID:
4879                         case -NFS4ERR_STALE_STATEID:
4880                                 nfs4_schedule_lease_recovery(server->nfs_client);
4881                                 goto out;
4882                         case -NFS4ERR_BADSESSION:
4883                         case -NFS4ERR_BADSLOT:
4884                         case -NFS4ERR_BAD_HIGH_SLOT:
4885                         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4886                         case -NFS4ERR_DEADSESSION:
4887                                 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4888                                 goto out;
4889                         case -ERESTARTSYS:
4890                                 /*
4891                                  * The show must go on: exit, but mark the
4892                                  * stateid as needing recovery.
4893                                  */
4894                         case -NFS4ERR_DELEG_REVOKED:
4895                         case -NFS4ERR_ADMIN_REVOKED:
4896                         case -NFS4ERR_BAD_STATEID:
4897                         case -NFS4ERR_OPENMODE:
4898                                 nfs4_schedule_stateid_recovery(server, state);
4899                                 err = 0;
4900                                 goto out;
4901                         case -ENOMEM:
4902                         case -NFS4ERR_DENIED:
4903                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4904                                 err = 0;
4905                                 goto out;
4906                         case -NFS4ERR_DELAY:
4907                                 break;
4908                 }
4909                 err = nfs4_handle_exception(server, err, &exception);
4910         } while (exception.retry);
4911 out:
4912         return err;
4913 }
4914 
4915 struct nfs_release_lockowner_data {
4916         struct nfs4_lock_state *lsp;
4917         struct nfs_server *server;
4918         struct nfs_release_lockowner_args args;
4919 };
4920 
4921 static void nfs4_release_lockowner_release(void *calldata)
4922 {
4923         struct nfs_release_lockowner_data *data = calldata;
4924         nfs4_free_lock_state(data->server, data->lsp);
4925         kfree(calldata);
4926 }
4927 
4928 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4929         .rpc_release = nfs4_release_lockowner_release,
4930 };
4931 
4932 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4933 {
4934         struct nfs_server *server = lsp->ls_state->owner->so_server;
4935         struct nfs_release_lockowner_data *data;
4936         struct rpc_message msg = {
4937                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4938         };
4939 
4940         if (server->nfs_client->cl_mvops->minor_version != 0)
4941                 return -EINVAL;
4942         data = kmalloc(sizeof(*data), GFP_NOFS);
4943         if (!data)
4944                 return -ENOMEM;
4945         data->lsp = lsp;
4946         data->server = server;
4947         data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4948         data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4949         data->args.lock_owner.s_dev = server->s_dev;
4950         msg.rpc_argp = &data->args;
4951         rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4952         return 0;
4953 }
4954 
4955 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4956 
4957 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4958                                    const void *buf, size_t buflen,
4959                                    int flags, int type)
4960 {
4961         if (strcmp(key, "") != 0)
4962                 return -EINVAL;
4963 
4964         return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4965 }
4966 
4967 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4968                                    void *buf, size_t buflen, int type)
4969 {
4970         if (strcmp(key, "") != 0)
4971                 return -EINVAL;
4972 
4973         return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4974 }
4975 
4976 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4977                                        size_t list_len, const char *name,
4978                                        size_t name_len, int type)
4979 {
4980         size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4981 
4982         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4983                 return 0;
4984 
4985         if (list && len <= list_len)
4986                 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4987         return len;
4988 }
4989 
4990 /*
4991  * nfs_fhget will use either the mounted_on_fileid or the fileid
4992  */
4993 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4994 {
4995         if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4996                (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4997               (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4998               (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4999                 return;
5000 
5001         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5002                 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5003         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5004         fattr->nlink = 2;
5005 }
5006 
5007 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5008                                    const struct qstr *name,
5009                                    struct nfs4_fs_locations *fs_locations,
5010                                    struct page *page)
5011 {
5012         struct nfs_server *server = NFS_SERVER(dir);
5013         u32 bitmask[2] = {
5014                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5015         };
5016         struct nfs4_fs_locations_arg args = {
5017                 .dir_fh = NFS_FH(dir),
5018                 .name = name,
5019                 .page = page,
5020                 .bitmask = bitmask,
5021         };
5022         struct nfs4_fs_locations_res res = {
5023                 .fs_locations = fs_locations,
5024         };
5025         struct rpc_message msg = {
5026                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5027                 .rpc_argp = &args,
5028                 .rpc_resp = &res,
5029         };
5030         int status;
5031 
5032         dprintk("%s: start\n", __func__);
5033 
5034         /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5035          * is not supported */
5036         if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5037                 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5038         else
5039                 bitmask[0] |= FATTR4_WORD0_FILEID;
5040 
5041         nfs_fattr_init(&fs_locations->fattr);
5042         fs_locations->server = server;
5043         fs_locations->nlocations = 0;
5044         status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5045         dprintk("%s: returned status = %d\n", __func__, status);
5046         return status;
5047 }
5048 
5049 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5050                            const struct qstr *name,
5051                            struct nfs4_fs_locations *fs_locations,
5052                            struct page *page)
5053 {
5054         struct nfs4_exception exception = { };
5055         int err;
5056         do {
5057                 err = nfs4_handle_exception(NFS_SERVER(dir),
5058                                 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5059                                 &exception);
5060         } while (exception.retry);
5061         return err;
5062 }
5063 
5064 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5065 {
5066         int status;
5067         struct nfs4_secinfo_arg args = {
5068                 .dir_fh = NFS_FH(dir),
5069                 .name   = name,
5070         };
5071         struct nfs4_secinfo_res res = {
5072                 .flavors     = flavors,
5073         };
5074         struct rpc_message msg = {
5075                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5076                 .rpc_argp = &args,
5077                 .rpc_resp = &res,
5078         };
5079 
5080         dprintk("NFS call  secinfo %s\n", name->name);
5081         status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5082         dprintk("NFS reply  secinfo: %d\n", status);
5083         return status;
5084 }
5085 
5086 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5087                       struct nfs4_secinfo_flavors *flavors)
5088 {
5089         struct nfs4_exception exception = { };
5090         int err;
5091         do {
5092                 err = nfs4_handle_exception(NFS_SERVER(dir),
5093                                 _nfs4_proc_secinfo(dir, name, flavors),
5094                                 &exception);
5095         } while (exception.retry);
5096         return err;
5097 }
5098 
5099 #ifdef CONFIG_NFS_V4_1
5100 /*
5101  * Check the exchange flags returned by the server for invalid flags, having
5102  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5103  * DS flags set.
5104  */
5105 static int nfs4_check_cl_exchange_flags(u32 flags)
5106 {
5107         if (flags & ~EXCHGID4_FLAG_MASK_R)
5108                 goto out_inval;
5109         if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5110             (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5111                 goto out_inval;
5112         if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5113                 goto out_inval;
5114         return NFS_OK;
5115 out_inval:
5116         return -NFS4ERR_INVAL;
5117 }
5118 
5119 static bool
5120 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5121 {
5122         if (a->server_scope_sz == b->server_scope_sz &&
5123             memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5124                 return true;
5125 
5126         return false;
5127 }
5128 
5129 /*
5130  * nfs4_proc_exchange_id()
5131  *
5132  * Since the clientid has expired, all compounds using sessions
5133  * associated with the stale clientid will be returning
5134  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5135  * be in some phase of session reset.
5136  */
5137 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5138 {
5139         nfs4_verifier verifier;
5140         struct nfs41_exchange_id_args args = {
5141                 .verifier = &verifier,
5142                 .client = clp,
5143                 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5144         };
5145         struct nfs41_exchange_id_res res = {
5146                 .client = clp,
5147         };
5148         int status;
5149         struct rpc_message msg = {
5150                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5151                 .rpc_argp = &args,
5152                 .rpc_resp = &res,
5153                 .rpc_cred = cred,
5154         };
5155 
5156         dprintk("--> %s\n", __func__);
5157         BUG_ON(clp == NULL);
5158 
5159         nfs4_construct_boot_verifier(clp, &verifier);
5160 
5161         args.id_len = scnprintf(args.id, sizeof(args.id),
5162                                 "%s/%s/%u",
5163                                 clp->cl_ipaddr,
5164                                 clp->cl_rpcclient->cl_nodename,
5165                                 clp->cl_rpcclient->cl_auth->au_flavor);
5166 
5167         res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
5168         if (unlikely(!res.server_scope)) {
5169                 status = -ENOMEM;
5170                 goto out;
5171         }
5172 
5173         res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5174         if (unlikely(!res.impl_id)) {
5175                 status = -ENOMEM;
5176                 goto out_server_scope;
5177         }
5178 
5179         status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5180         if (!status)
5181                 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5182 
5183         if (!status) {
5184                 /* use the most recent implementation id */
5185                 kfree(clp->impl_id);
5186                 clp->impl_id = res.impl_id;
5187         } else
5188                 kfree(res.impl_id);
5189 
5190         if (!status) {
5191                 if (clp->server_scope &&
5192                     !nfs41_same_server_scope(clp->server_scope,
5193                                              res.server_scope)) {
5194                         dprintk("%s: server_scope mismatch detected\n",
5195                                 __func__);
5196                         set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5197                         kfree(clp->server_scope);
5198                         clp->server_scope = NULL;
5199                 }
5200 
5201                 if (!clp->server_scope) {
5202                         clp->server_scope = res.server_scope;
5203                         goto out;
5204                 }
5205         }
5206 
5207 out_server_scope:
5208         kfree(res.server_scope);
5209 out:
5210         if (clp->impl_id)
5211                 dprintk("%s: Server Implementation ID: "
5212                         "domain: %s, name: %s, date: %llu,%u\n",
5213                         __func__, clp->impl_id->domain, clp->impl_id->name,
5214                         clp->impl_id->date.seconds,
5215                         clp->impl_id->date.nseconds);
5216         dprintk("<-- %s status= %d\n", __func__, status);
5217         return status;
5218 }
5219 
5220 struct nfs4_get_lease_time_data {
5221         struct nfs4_get_lease_time_args *args;
5222         struct nfs4_get_lease_time_res *res;
5223         struct nfs_client *clp;
5224 };
5225 
5226 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5227                                         void *calldata)
5228 {
5229         int ret;
5230         struct nfs4_get_lease_time_data *data =
5231                         (struct nfs4_get_lease_time_data *)calldata;
5232 
5233         dprintk("--> %s\n", __func__);
5234         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5235         /* just setup sequence, do not trigger session recovery
5236            since we're invoked within one */
5237         ret = nfs41_setup_sequence(data->clp->cl_session,
5238                                    &data->args->la_seq_args,
5239                                    &data->res->lr_seq_res, task);
5240 
5241         BUG_ON(ret == -EAGAIN);
5242         rpc_call_start(task);
5243         dprintk("<-- %s\n", __func__);
5244 }
5245 
5246 /*
5247  * Called from nfs4_state_manager thread for session setup, so don't recover
5248  * from sequence operation or clientid errors.
5249  */
5250 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5251 {
5252         struct nfs4_get_lease_time_data *data =
5253                         (struct nfs4_get_lease_time_data *)calldata;
5254 
5255         dprintk("--> %s\n", __func__);
5256         if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5257                 return;
5258         switch (task->tk_status) {
5259         case -NFS4ERR_DELAY:
5260         case -NFS4ERR_GRACE:
5261                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5262                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5263                 task->tk_status = 0;
5264                 /* fall through */
5265         case -NFS4ERR_RETRY_UNCACHED_REP:
5266                 rpc_restart_call_prepare(task);
5267                 return;
5268         }
5269         dprintk("<-- %s\n", __func__);
5270 }
5271 
5272 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5273         .rpc_call_prepare = nfs4_get_lease_time_prepare,
5274         .rpc_call_done = nfs4_get_lease_time_done,
5275 };
5276 
5277 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5278 {
5279         struct rpc_task *task;
5280         struct nfs4_get_lease_time_args args;
5281         struct nfs4_get_lease_time_res res = {
5282                 .lr_fsinfo = fsinfo,
5283         };
5284         struct nfs4_get_lease_time_data data = {
5285                 .args = &args,
5286                 .res = &res,
5287                 .clp = clp,
5288         };
5289         struct rpc_message msg = {
5290                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5291                 .rpc_argp = &args,
5292                 .rpc_resp = &res,
5293         };
5294         struct rpc_task_setup task_setup = {
5295                 .rpc_client = clp->cl_rpcclient,
5296                 .rpc_message = &msg,
5297                 .callback_ops = &nfs4_get_lease_time_ops,
5298                 .callback_data = &data,
5299                 .flags = RPC_TASK_TIMEOUT,
5300         };
5301         int status;
5302 
5303         nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5304         dprintk("--> %s\n", __func__);
5305         task = rpc_run_task(&task_setup);
5306 
5307         if (IS_ERR(task))
5308                 status = PTR_ERR(task);
5309         else {
5310                 status = task->tk_status;
5311                 rpc_put_task(task);
5312         }
5313         dprintk("<-- %s return %d\n", __func__, status);
5314 
5315         return status;
5316 }
5317 
5318 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5319 {
5320         return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5321 }
5322 
5323 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5324                 struct nfs4_slot *new,
5325                 u32 max_slots,
5326                 u32 ivalue)
5327 {
5328         struct nfs4_slot *old = NULL;
5329         u32 i;
5330 
5331         spin_lock(&tbl->slot_tbl_lock);
5332         if (new) {
5333                 old = tbl->slots;
5334                 tbl->slots = new;
5335                 tbl->max_slots = max_slots;
5336         }
5337         tbl->highest_used_slotid = -1;  /* no slot is currently used */
5338         for (i = 0; i < tbl->max_slots; i++)
5339                 tbl->slots[i].seq_nr = ivalue;
5340         spin_unlock(&tbl->slot_tbl_lock);
5341         kfree(old);
5342 }
5343 
5344 /*
5345  * (re)Initialise a slot table
5346  */
5347 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5348                                  u32 ivalue)
5349 {
5350         struct nfs4_slot *new = NULL;
5351         int ret = -ENOMEM;
5352 
5353         dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5354                 max_reqs, tbl->max_slots);
5355 
5356         /* Does the newly negotiated max_reqs match the existing slot table? */
5357         if (max_reqs != tbl->max_slots) {
5358                 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5359                 if (!new)
5360                         goto out;
5361         }
5362         ret = 0;
5363 
5364         nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5365         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5366                 tbl, tbl->slots, tbl->max_slots);
5367 out:
5368         dprintk("<-- %s: return %d\n", __func__, ret);
5369         return ret;
5370 }
5371 
5372 /* Destroy the slot table */
5373 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5374 {
5375         if (session->fc_slot_table.slots != NULL) {
5376                 kfree(session->fc_slot_table.slots);
5377                 session->fc_slot_table.slots = NULL;
5378         }
5379         if (session->bc_slot_table.slots != NULL) {
5380                 kfree(session->bc_slot_table.slots);
5381                 session->bc_slot_table.slots = NULL;
5382         }
5383         return;
5384 }
5385 
5386 /*
5387  * Initialize or reset the forechannel and backchannel tables
5388  */
5389 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5390 {
5391         struct nfs4_slot_table *tbl;
5392         int status;
5393 
5394         dprintk("--> %s\n", __func__);
5395         /* Fore channel */
5396         tbl = &ses->fc_slot_table;
5397         status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5398         if (status) /* -ENOMEM */
5399                 return status;
5400         /* Back channel */
5401         tbl = &ses->bc_slot_table;
5402         status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5403         if (status && tbl->slots == NULL)
5404                 /* Fore and back channel share a connection so get
5405                  * both slot tables or neither */
5406                 nfs4_destroy_slot_tables(ses);
5407         return status;
5408 }
5409 
5410 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5411 {
5412         struct nfs4_session *session;
5413         struct nfs4_slot_table *tbl;
5414 
5415         session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5416         if (!session)
5417                 return NULL;
5418 
5419         tbl = &session->fc_slot_table;
5420         tbl->highest_used_slotid = NFS4_NO_SLOT;
5421         spin_lock_init(&tbl->slot_tbl_lock);
5422         rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5423         init_completion(&tbl->complete);
5424 
5425         tbl = &session->bc_slot_table;
5426         tbl->highest_used_slotid = NFS4_NO_SLOT;
5427         spin_lock_init(&tbl->slot_tbl_lock);
5428         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5429         init_completion(&tbl->complete);
5430 
5431         session->session_state = 1<<NFS4_SESSION_INITING;
5432 
5433         session->clp = clp;
5434         return session;
5435 }
5436 
5437 void nfs4_destroy_session(struct nfs4_session *session)
5438 {
5439         struct rpc_xprt *xprt;
5440 
5441         nfs4_proc_destroy_session(session);
5442 
5443         rcu_read_lock();
5444         xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5445         rcu_read_unlock();
5446         dprintk("%s Destroy backchannel for xprt %p\n",
5447                 __func__, xprt);
5448         xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5449         nfs4_destroy_slot_tables(session);
5450         kfree(session);
5451 }
5452 
5453 /*
5454  * Initialize the values to be used by the client in CREATE_SESSION
5455  * If nfs4_init_session set the fore channel request and response sizes,
5456  * use them.
5457  *
5458  * Set the back channel max_resp_sz_cached to zero to force the client to
5459  * always set csa_cachethis to FALSE because the current implementation
5460  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5461  */
5462 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5463 {
5464         struct nfs4_session *session = args->client->cl_session;
5465         unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5466                      mxresp_sz = session->fc_attrs.max_resp_sz;
5467 
5468         if (mxrqst_sz == 0)
5469                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5470         if (mxresp_sz == 0)
5471                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5472         /* Fore channel attributes */
5473         args->fc_attrs.max_rqst_sz = mxrqst_sz;
5474         args->fc_attrs.max_resp_sz = mxresp_sz;
5475         args->fc_attrs.max_ops = NFS4_MAX_OPS;
5476         args->fc_attrs.max_reqs = max_session_slots;
5477 
5478         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5479                 "max_ops=%u max_reqs=%u\n",
5480                 __func__,
5481                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5482                 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5483 
5484         /* Back channel attributes */
5485         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5486         args->bc_attrs.max_resp_sz = PAGE_SIZE;
5487         args->bc_attrs.max_resp_sz_cached = 0;
5488         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5489         args->bc_attrs.max_reqs = 1;
5490 
5491         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5492                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5493                 __func__,
5494                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5495                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5496                 args->bc_attrs.max_reqs);
5497 }
5498 
5499 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5500 {
5501         struct nfs4_channel_attrs *sent = &args->fc_attrs;
5502         struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5503 
5504         if (rcvd->max_resp_sz > sent->max_resp_sz)
5505                 return -EINVAL;
5506         /*
5507          * Our requested max_ops is the minimum we need; we're not
5508          * prepared to break up compounds into smaller pieces than that.
5509          * So, no point even trying to continue if the server won't
5510          * cooperate:
5511          */
5512         if (rcvd->max_ops < sent->max_ops)
5513                 return -EINVAL;
5514         if (rcvd->max_reqs == 0)
5515                 return -EINVAL;
5516         if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5517                 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5518         return 0;
5519 }
5520 
5521 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5522 {
5523         struct nfs4_channel_attrs *sent = &args->bc_attrs;
5524         struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5525 
5526         if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5527                 return -EINVAL;
5528         if (rcvd->max_resp_sz < sent->max_resp_sz)
5529                 return -EINVAL;
5530         if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5531                 return -EINVAL;
5532         /* These would render the backchannel useless: */
5533         if (rcvd->max_ops != sent->max_ops)
5534                 return -EINVAL;
5535         if (rcvd->max_reqs != sent->max_reqs)
5536                 return -EINVAL;
5537         return 0;
5538 }
5539 
5540 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5541                                      struct nfs4_session *session)
5542 {
5543         int ret;
5544 
5545         ret = nfs4_verify_fore_channel_attrs(args, session);
5546         if (ret)
5547                 return ret;
5548         return nfs4_verify_back_channel_attrs(args, session);
5549 }
5550 
5551 static int _nfs4_proc_create_session(struct nfs_client *clp)
5552 {
5553         struct nfs4_session *session = clp->cl_session;
5554         struct nfs41_create_session_args args = {
5555                 .client = clp,
5556                 .cb_program = NFS4_CALLBACK,
5557         };
5558         struct nfs41_create_session_res res = {
5559                 .client = clp,
5560         };
5561         struct rpc_message msg = {
5562                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5563                 .rpc_argp = &args,
5564                 .rpc_resp = &res,
5565         };
5566         int status;
5567 
5568         nfs4_init_channel_attrs(&args);
5569         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5570 
5571         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5572 
5573         if (!status)
5574                 /* Verify the session's negotiated channel_attrs values */
5575                 status = nfs4_verify_channel_attrs(&args, session);
5576         if (!status) {
5577                 /* Increment the clientid slot sequence id */
5578                 clp->cl_seqid++;
5579         }
5580 
5581         return status;
5582 }
5583 
5584 /*
5585  * Issues a CREATE_SESSION operation to the server.
5586  * It is the responsibility of the caller to verify the session is
5587  * expired before calling this routine.
5588  */
5589 int nfs4_proc_create_session(struct nfs_client *clp)
5590 {
5591         int status;
5592         unsigned *ptr;
5593         struct nfs4_session *session = clp->cl_session;
5594 
5595         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5596 
5597         status = _nfs4_proc_create_session(clp);
5598         if (status)
5599                 goto out;
5600 
5601         /* Init or reset the session slot tables */
5602         status = nfs4_setup_session_slot_tables(session);
5603         dprintk("slot table setup returned %d\n", status);
5604         if (status)
5605                 goto out;
5606 
5607         ptr = (unsigned *)&session->sess_id.data[0];
5608         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5609                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5610 out:
5611         dprintk("<-- %s\n", __func__);
5612         return status;
5613 }
5614 
5615 /*
5616  * Issue the over-the-wire RPC DESTROY_SESSION.
5617  * The caller must serialize access to this routine.
5618  */
5619 int nfs4_proc_destroy_session(struct nfs4_session *session)
5620 {
5621         int status = 0;
5622         struct rpc_message msg;
5623 
5624         dprintk("--> nfs4_proc_destroy_session\n");
5625 
5626         /* session is still being setup */
5627         if (session->clp->cl_cons_state != NFS_CS_READY)
5628                 return status;
5629 
5630         msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5631         msg.rpc_argp = session;
5632         msg.rpc_resp = NULL;
5633         msg.rpc_cred = NULL;
5634         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5635 
5636         if (status)
5637                 printk(KERN_WARNING
5638                         "NFS: Got error %d from the server on DESTROY_SESSION. "
5639                         "Session has been destroyed regardless...\n", status);
5640 
5641         dprintk("<-- nfs4_proc_destroy_session\n");
5642         return status;
5643 }
5644 
5645 int nfs4_init_session(struct nfs_server *server)
5646 {
5647         struct nfs_client *clp = server->nfs_client;
5648         struct nfs4_session *session;
5649         unsigned int rsize, wsize;
5650         int ret;
5651 
5652         if (!nfs4_has_session(clp))
5653                 return 0;
5654 
5655         session = clp->cl_session;
5656         if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5657                 return 0;
5658 
5659         rsize = server->rsize;
5660         if (rsize == 0)
5661                 rsize = NFS_MAX_FILE_IO_SIZE;
5662         wsize = server->wsize;
5663         if (wsize == 0)
5664                 wsize = NFS_MAX_FILE_IO_SIZE;
5665 
5666         session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5667         session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5668 
5669         ret = nfs4_recover_expired_lease(server);
5670         if (!ret)
5671                 ret = nfs4_check_client_ready(clp);
5672         return ret;
5673 }
5674 
5675 int nfs4_init_ds_session(struct nfs_client *clp)
5676 {
5677         struct nfs4_session *session = clp->cl_session;
5678         int ret;
5679 
5680         if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5681                 return 0;
5682 
5683         ret = nfs4_client_recover_expired_lease(clp);
5684         if (!ret)
5685                 /* Test for the DS role */
5686                 if (!is_ds_client(clp))
5687                         ret = -ENODEV;
5688         if (!ret)
5689                 ret = nfs4_check_client_ready(clp);
5690         return ret;
5691 
5692 }
5693 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5694 
5695 
5696 /*
5697  * Renew the cl_session lease.
5698  */
5699 struct nfs4_sequence_data {
5700         struct nfs_client *clp;
5701         struct nfs4_sequence_args args;
5702         struct nfs4_sequence_res res;
5703 };
5704 
5705 static void nfs41_sequence_release(void *data)
5706 {
5707         struct nfs4_sequence_data *calldata = data;
5708         struct nfs_client *clp = calldata->clp;
5709 
5710         if (atomic_read(&clp->cl_count) > 1)
5711                 nfs4_schedule_state_renewal(clp);
5712         nfs_put_client(clp);
5713         kfree(calldata);
5714 }
5715 
5716 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5717 {
5718         switch(task->tk_status) {
5719         case -NFS4ERR_DELAY:
5720                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5721                 return -EAGAIN;
5722         default:
5723                 nfs4_schedule_lease_recovery(clp);
5724         }
5725         return 0;
5726 }
5727 
5728 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5729 {
5730         struct nfs4_sequence_data *calldata = data;
5731         struct nfs_client *clp = calldata->clp;
5732 
5733         if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5734                 return;
5735 
5736         if (task->tk_status < 0) {
5737                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5738                 if (atomic_read(&clp->cl_count) == 1)
5739                         goto out;
5740 
5741                 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5742                         rpc_restart_call_prepare(task);
5743                         return;
5744                 }
5745         }
5746         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5747 out:
5748         dprintk("<-- %s\n", __func__);
5749 }
5750 
5751 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5752 {
5753         struct nfs4_sequence_data *calldata = data;
5754         struct nfs_client *clp = calldata->clp;
5755         struct nfs4_sequence_args *args;
5756         struct nfs4_sequence_res *res;
5757 
5758         args = task->tk_msg.rpc_argp;
5759         res = task->tk_msg.rpc_resp;
5760 
5761         if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5762                 return;
5763         rpc_call_start(task);
5764 }
5765 
5766 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
5767 {
5768         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5769         nfs41_sequence_prepare(task, data);
5770 }
5771 
5772 static const struct rpc_call_ops nfs41_sequence_ops = {
5773         .rpc_call_done = nfs41_sequence_call_done,
5774         .rpc_call_prepare = nfs41_sequence_prepare,
5775         .rpc_release = nfs41_sequence_release,
5776 };
5777 
5778 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
5779         .rpc_call_done = nfs41_sequence_call_done,
5780         .rpc_call_prepare = nfs41_sequence_prepare_privileged,
5781         .rpc_release = nfs41_sequence_release,
5782 };
5783 
5784 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
5785                                              const struct rpc_call_ops *seq_ops)
5786 {
5787         struct nfs4_sequence_data *calldata;
5788         struct rpc_message msg = {
5789                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5790                 .rpc_cred = cred,
5791         };
5792         struct rpc_task_setup task_setup_data = {
5793                 .rpc_client = clp->cl_rpcclient,
5794                 .rpc_message = &msg,
5795                 .callback_ops = seq_ops,
5796                 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5797         };
5798 
5799         if (!atomic_inc_not_zero(&clp->cl_count))
5800                 return ERR_PTR(-EIO);
5801         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5802         if (calldata == NULL) {
5803                 nfs_put_client(clp);
5804                 return ERR_PTR(-ENOMEM);
5805         }
5806         nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5807         msg.rpc_argp = &calldata->args;
5808         msg.rpc_resp = &calldata->res;
5809         calldata->clp = clp;
5810         task_setup_data.callback_data = calldata;
5811 
5812         return rpc_run_task(&task_setup_data);
5813 }
5814 
5815 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5816 {
5817         struct rpc_task *task;
5818         int ret = 0;
5819 
5820         if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5821                 return -EAGAIN;
5822         task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
5823         if (IS_ERR(task))
5824                 ret = PTR_ERR(task);
5825         else
5826                 rpc_put_task_async(task);
5827         dprintk("<-- %s status=%d\n", __func__, ret);
5828         return ret;
5829 }
5830 
5831 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5832 {
5833         struct rpc_task *task;
5834         int ret;
5835 
5836         task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
5837         if (IS_ERR(task)) {
5838                 ret = PTR_ERR(task);
5839                 goto out;
5840         }
5841         ret = rpc_wait_for_completion_task(task);
5842         if (!ret) {
5843                 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5844 
5845                 if (task->tk_status == 0)
5846                         nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5847                 ret = task->tk_status;
5848         }
5849         rpc_put_task(task);
5850 out:
5851         dprintk("<-- %s status=%d\n", __func__, ret);
5852         return ret;
5853 }
5854 
5855 struct nfs4_reclaim_complete_data {
5856         struct nfs_client *clp;
5857         struct nfs41_reclaim_complete_args arg;
5858         struct nfs41_reclaim_complete_res res;
5859 };
5860 
5861 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5862 {
5863         struct nfs4_reclaim_complete_data *calldata = data;
5864 
5865         rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5866         if (nfs41_setup_sequence(calldata->clp->cl_session,
5867                                 &calldata->arg.seq_args,
5868                                 &calldata->res.seq_res, task))
5869                 return;
5870 
5871         rpc_call_start(task);
5872 }
5873 
5874 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5875 {
5876         switch(task->tk_status) {
5877         case 0:
5878         case -NFS4ERR_COMPLETE_ALREADY:
5879         case -NFS4ERR_WRONG_CRED: /* What to do here? */
5880                 break;
5881         case -NFS4ERR_DELAY:
5882                 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5883                 /* fall through */
5884         case -NFS4ERR_RETRY_UNCACHED_REP:
5885                 return -EAGAIN;
5886         default:
5887                 nfs4_schedule_lease_recovery(clp);
5888         }
5889         return 0;
5890 }
5891 
5892 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5893 {
5894         struct nfs4_reclaim_complete_data *calldata = data;
5895         struct nfs_client *clp = calldata->clp;
5896         struct nfs4_sequence_res *res = &calldata->res.seq_res;
5897 
5898         dprintk("--> %s\n", __func__);
5899         if (!nfs41_sequence_done(task, res))
5900                 return;
5901 
5902         if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5903                 rpc_restart_call_prepare(task);
5904                 return;
5905         }
5906         dprintk("<-- %s\n", __func__);
5907 }
5908 
5909 static void nfs4_free_reclaim_complete_data(void *data)
5910 {
5911         struct nfs4_reclaim_complete_data *calldata = data;
5912 
5913         kfree(calldata);
5914 }
5915 
5916 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5917         .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5918         .rpc_call_done = nfs4_reclaim_complete_done,
5919         .rpc_release = nfs4_free_reclaim_complete_data,
5920 };
5921 
5922 /*
5923  * Issue a global reclaim complete.
5924  */
5925 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5926 {
5927         struct nfs4_reclaim_complete_data *calldata;
5928         struct rpc_task *task;
5929         struct rpc_message msg = {
5930                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5931         };
5932         struct rpc_task_setup task_setup_data = {
5933                 .rpc_client = clp->cl_rpcclient,
5934                 .rpc_message = &msg,
5935                 .callback_ops = &nfs4_reclaim_complete_call_ops,
5936                 .flags = RPC_TASK_ASYNC,
5937         };
5938         int status = -ENOMEM;
5939 
5940         dprintk("--> %s\n", __func__);
5941         calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5942         if (calldata == NULL)
5943                 goto out;
5944         calldata->clp = clp;
5945         calldata->arg.one_fs = 0;
5946 
5947         nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5948         msg.rpc_argp = &calldata->arg;
5949         msg.rpc_resp = &calldata->res;
5950         task_setup_data.callback_data = calldata;
5951         task = rpc_run_task(&task_setup_data);
5952         if (IS_ERR(task)) {
5953                 status = PTR_ERR(task);
5954                 goto out;
5955         }
5956         status = nfs4_wait_for_completion_rpc_task(task);
5957         if (status == 0)
5958                 status = task->tk_status;
5959         rpc_put_task(task);
5960         return 0;
5961 out:
5962         dprintk("<-- %s status=%d\n", __func__, status);
5963         return status;
5964 }
5965 
5966 static void
5967 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5968 {
5969         struct nfs4_layoutget *lgp = calldata;
5970         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5971 
5972         dprintk("--> %s\n", __func__);
5973         /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5974          * right now covering the LAYOUTGET we are about to send.
5975          * However, that is not so catastrophic, and there seems
5976          * to be no way to prevent it completely.
5977          */
5978         if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5979                                 &lgp->res.seq_res, task))
5980                 return;
5981         if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5982                                           NFS_I(lgp->args.inode)->layout,
5983                                           lgp->args.ctx->state)) {
5984                 rpc_exit(task, NFS4_OK);
5985                 return;
5986         }
5987         rpc_call_start(task);
5988 }
5989 
5990 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5991 {
5992         struct nfs4_layoutget *lgp = calldata;
5993         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5994 
5995         dprintk("--> %s\n", __func__);
5996 
5997         if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5998                 return;
5999 
6000         switch (task->tk_status) {
6001         case 0:
6002                 break;
6003         case -NFS4ERR_LAYOUTTRYLATER:
6004         case -NFS4ERR_RECALLCONFLICT:
6005                 task->tk_status = -NFS4ERR_DELAY;
6006                 /* Fall through */
6007         default:
6008                 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6009                         rpc_restart_call_prepare(task);
6010                         return;
6011                 }
6012         }
6013         dprintk("<-- %s\n", __func__);
6014 }
6015 
6016 static size_t max_response_pages(struct nfs_server *server)
6017 {
6018         u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6019         return nfs_page_array_len(0, max_resp_sz);
6020 }
6021 
6022 static void nfs4_free_pages(struct page **pages, size_t size)
6023 {
6024         int i;
6025 
6026         if (!pages)
6027                 return;
6028 
6029         for (i = 0; i < size; i++) {
6030                 if (!pages[i])
6031                         break;
6032                 __free_page(pages[i]);
6033         }
6034         kfree(pages);
6035 }
6036 
6037 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6038 {
6039         struct page **pages;
6040         int i;
6041 
6042         pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6043         if (!pages) {
6044                 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6045                 return NULL;
6046         }
6047 
6048         for (i = 0; i < size; i++) {
6049                 pages[i] = alloc_page(gfp_flags);
6050                 if (!pages[i]) {
6051                         dprintk("%s: failed to allocate page\n", __func__);
6052                         nfs4_free_pages(pages, size);
6053                         return NULL;
6054                 }
6055         }
6056 
6057         return pages;
6058 }
6059 
6060 static void nfs4_layoutget_release(void *calldata)
6061 {
6062         struct nfs4_layoutget *lgp = calldata;
6063         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6064         size_t max_pages = max_response_pages(server);
6065 
6066         dprintk("--> %s\n", __func__);
6067         nfs4_free_pages(lgp->args.layout.pages, max_pages);
6068         put_nfs_open_context(lgp->args.ctx);
6069         kfree(calldata);
6070         dprintk("<-- %s\n", __func__);
6071 }
6072 
6073 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6074         .rpc_call_prepare = nfs4_layoutget_prepare,
6075         .rpc_call_done = nfs4_layoutget_done,
6076         .rpc_release = nfs4_layoutget_release,
6077 };
6078 
6079 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6080 {
6081         struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6082         size_t max_pages = max_response_pages(server);
6083         struct rpc_task *task;
6084         struct rpc_message msg = {
6085                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6086                 .rpc_argp = &lgp->args,
6087                 .rpc_resp = &lgp->res,
6088         };
6089         struct rpc_task_setup task_setup_data = {
6090                 .rpc_client = server->client,
6091                 .rpc_message = &msg,
6092                 .callback_ops = &nfs4_layoutget_call_ops,
6093                 .callback_data = lgp,
6094                 .flags = RPC_TASK_ASYNC,
6095         };
6096         int status = 0;
6097 
6098         dprintk("--> %s\n", __func__);
6099 
6100         lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6101         if (!lgp->args.layout.pages) {
6102                 nfs4_layoutget_release(lgp);
6103                 return -ENOMEM;
6104         }
6105         lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6106 
6107         lgp->res.layoutp = &lgp->args.layout;
6108         lgp->res.seq_res.sr_slot = NULL;
6109         nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6110         task = rpc_run_task(&task_setup_data);
6111         if (IS_ERR(task))
6112                 return PTR_ERR(task);
6113         status = nfs4_wait_for_completion_rpc_task(task);
6114         if (status == 0)
6115                 status = task->tk_status;
6116         /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6117         if (status == 0 && lgp->res.layoutp->len)
6118                 status = pnfs_layout_process(lgp);
6119         rpc_put_task(task);
6120         dprintk("<-- %s status=%d\n", __func__, status);
6121         return status;
6122 }
6123 
6124 static void
6125 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6126 {
6127         struct nfs4_layoutreturn *lrp = calldata;
6128 
6129         dprintk("--> %s\n", __func__);
6130         if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6131                                 &lrp->res.seq_res, task))
6132                 return;
6133         rpc_call_start(task);
6134 }
6135 
6136 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6137 {
6138         struct nfs4_layoutreturn *lrp = calldata;
6139         struct nfs_server *server;
6140         struct pnfs_layout_hdr *lo = lrp->args.layout;
6141 
6142         dprintk("--> %s\n", __func__);
6143 
6144         if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6145                 return;
6146 
6147         server = NFS_SERVER(lrp->args.inode);
6148         if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6149                 rpc_restart_call_prepare(task);
6150                 return;
6151         }
6152         spin_lock(&lo->plh_inode->i_lock);
6153         if (task->tk_status == 0 && lrp->res.lrs_present)
6154                 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6155         lo->plh_block_lgets--;
6156         spin_unlock(&lo->plh_inode->i_lock);
6157         dprintk("<-- %s\n", __func__);
6158 }
6159 
6160 static void nfs4_layoutreturn_release(void *calldata)
6161 {
6162         struct nfs4_layoutreturn *lrp = calldata;
6163 
6164         dprintk("--> %s\n", __func__);
6165         put_layout_hdr(lrp->args.layout);
6166         kfree(