Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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/utsname.h>
 40 #include <linux/delay.h>
 41 #include <linux/errno.h>
 42 #include <linux/string.h>
 43 #include <linux/sunrpc/clnt.h>
 44 #include <linux/nfs.h>
 45 #include <linux/nfs4.h>
 46 #include <linux/nfs_fs.h>
 47 #include <linux/nfs_page.h>
 48 #include <linux/namei.h>
 49 #include <linux/mount.h>
 50 #include <linux/module.h>
 51 #include <linux/sunrpc/bc_xprt.h>
 52 
 53 #include "nfs4_fs.h"
 54 #include "delegation.h"
 55 #include "internal.h"
 56 #include "iostat.h"
 57 #include "callback.h"
 58 
 59 #define NFSDBG_FACILITY         NFSDBG_PROC
 60 
 61 #define NFS4_POLL_RETRY_MIN     (HZ/10)
 62 #define NFS4_POLL_RETRY_MAX     (15*HZ)
 63 
 64 struct nfs4_opendata;
 65 static int _nfs4_proc_open(struct nfs4_opendata *data);
 66 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
 67 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
 68 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
 69 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
 70 
 71 /* Prevent leaks of NFSv4 errors into userland */
 72 static int nfs4_map_errors(int err)
 73 {
 74         if (err >= -1000)
 75                 return err;
 76         switch (err) {
 77         case -NFS4ERR_RESOURCE:
 78                 return -EREMOTEIO;
 79         default:
 80                 dprintk("%s could not handle NFSv4 error %d\n",
 81                                 __func__, -err);
 82                 break;
 83         }
 84         return -EIO;
 85 }
 86 
 87 /*
 88  * This is our standard bitmap for GETATTR requests.
 89  */
 90 const u32 nfs4_fattr_bitmap[2] = {
 91         FATTR4_WORD0_TYPE
 92         | FATTR4_WORD0_CHANGE
 93         | FATTR4_WORD0_SIZE
 94         | FATTR4_WORD0_FSID
 95         | FATTR4_WORD0_FILEID,
 96         FATTR4_WORD1_MODE
 97         | FATTR4_WORD1_NUMLINKS
 98         | FATTR4_WORD1_OWNER
 99         | FATTR4_WORD1_OWNER_GROUP
100         | FATTR4_WORD1_RAWDEV
101         | FATTR4_WORD1_SPACE_USED
102         | FATTR4_WORD1_TIME_ACCESS
103         | FATTR4_WORD1_TIME_METADATA
104         | FATTR4_WORD1_TIME_MODIFY
105 };
106 
107 const u32 nfs4_statfs_bitmap[2] = {
108         FATTR4_WORD0_FILES_AVAIL
109         | FATTR4_WORD0_FILES_FREE
110         | FATTR4_WORD0_FILES_TOTAL,
111         FATTR4_WORD1_SPACE_AVAIL
112         | FATTR4_WORD1_SPACE_FREE
113         | FATTR4_WORD1_SPACE_TOTAL
114 };
115 
116 const u32 nfs4_pathconf_bitmap[2] = {
117         FATTR4_WORD0_MAXLINK
118         | FATTR4_WORD0_MAXNAME,
119         0
120 };
121 
122 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
123                         | FATTR4_WORD0_MAXREAD
124                         | FATTR4_WORD0_MAXWRITE
125                         | FATTR4_WORD0_LEASE_TIME,
126                         0
127 };
128 
129 const u32 nfs4_fs_locations_bitmap[2] = {
130         FATTR4_WORD0_TYPE
131         | FATTR4_WORD0_CHANGE
132         | FATTR4_WORD0_SIZE
133         | FATTR4_WORD0_FSID
134         | FATTR4_WORD0_FILEID
135         | FATTR4_WORD0_FS_LOCATIONS,
136         FATTR4_WORD1_MODE
137         | FATTR4_WORD1_NUMLINKS
138         | FATTR4_WORD1_OWNER
139         | FATTR4_WORD1_OWNER_GROUP
140         | FATTR4_WORD1_RAWDEV
141         | FATTR4_WORD1_SPACE_USED
142         | FATTR4_WORD1_TIME_ACCESS
143         | FATTR4_WORD1_TIME_METADATA
144         | FATTR4_WORD1_TIME_MODIFY
145         | FATTR4_WORD1_MOUNTED_ON_FILEID
146 };
147 
148 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
149                 struct nfs4_readdir_arg *readdir)
150 {
151         __be32 *start, *p;
152 
153         BUG_ON(readdir->count < 80);
154         if (cookie > 2) {
155                 readdir->cookie = cookie;
156                 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
157                 return;
158         }
159 
160         readdir->cookie = 0;
161         memset(&readdir->verifier, 0, sizeof(readdir->verifier));
162         if (cookie == 2)
163                 return;
164         
165         /*
166          * NFSv4 servers do not return entries for '.' and '..'
167          * Therefore, we fake these entries here.  We let '.'
168          * have cookie 0 and '..' have cookie 1.  Note that
169          * when talking to the server, we always send cookie 0
170          * instead of 1 or 2.
171          */
172         start = p = kmap_atomic(*readdir->pages, KM_USER0);
173         
174         if (cookie == 0) {
175                 *p++ = xdr_one;                                  /* next */
176                 *p++ = xdr_zero;                   /* cookie, first word */
177                 *p++ = xdr_one;                   /* cookie, second word */
178                 *p++ = xdr_one;                             /* entry len */
179                 memcpy(p, ".\0\0\0", 4);                        /* entry */
180                 p++;
181                 *p++ = xdr_one;                         /* bitmap length */
182                 *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
183                 *p++ = htonl(8);              /* attribute buffer length */
184                 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
185         }
186         
187         *p++ = xdr_one;                                  /* next */
188         *p++ = xdr_zero;                   /* cookie, first word */
189         *p++ = xdr_two;                   /* cookie, second word */
190         *p++ = xdr_two;                             /* entry len */
191         memcpy(p, "..\0\0", 4);                         /* entry */
192         p++;
193         *p++ = xdr_one;                         /* bitmap length */
194         *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
195         *p++ = htonl(8);              /* attribute buffer length */
196         p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
197 
198         readdir->pgbase = (char *)p - (char *)start;
199         readdir->count -= readdir->pgbase;
200         kunmap_atomic(start, KM_USER0);
201 }
202 
203 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
204 {
205         int res;
206 
207         might_sleep();
208 
209         res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
210                         nfs_wait_bit_killable, TASK_KILLABLE);
211         return res;
212 }
213 
214 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
215 {
216         int res = 0;
217 
218         might_sleep();
219 
220         if (*timeout <= 0)
221                 *timeout = NFS4_POLL_RETRY_MIN;
222         if (*timeout > NFS4_POLL_RETRY_MAX)
223                 *timeout = NFS4_POLL_RETRY_MAX;
224         schedule_timeout_killable(*timeout);
225         if (fatal_signal_pending(current))
226                 res = -ERESTARTSYS;
227         *timeout <<= 1;
228         return res;
229 }
230 
231 /* This is the error handling routine for processes that are allowed
232  * to sleep.
233  */
234 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
235 {
236         struct nfs_client *clp = server->nfs_client;
237         struct nfs4_state *state = exception->state;
238         int ret = errorcode;
239 
240         exception->retry = 0;
241         switch(errorcode) {
242                 case 0:
243                         return 0;
244                 case -NFS4ERR_ADMIN_REVOKED:
245                 case -NFS4ERR_BAD_STATEID:
246                 case -NFS4ERR_OPENMODE:
247                         if (state == NULL)
248                                 break;
249                         nfs4_state_mark_reclaim_nograce(clp, state);
250                 case -NFS4ERR_STALE_CLIENTID:
251                 case -NFS4ERR_STALE_STATEID:
252                 case -NFS4ERR_EXPIRED:
253                         nfs4_schedule_state_recovery(clp);
254                         ret = nfs4_wait_clnt_recover(clp);
255                         if (ret == 0)
256                                 exception->retry = 1;
257 #if !defined(CONFIG_NFS_V4_1)
258                         break;
259 #else /* !defined(CONFIG_NFS_V4_1) */
260                         if (!nfs4_has_session(server->nfs_client))
261                                 break;
262                         /* FALLTHROUGH */
263                 case -NFS4ERR_BADSESSION:
264                 case -NFS4ERR_BADSLOT:
265                 case -NFS4ERR_BAD_HIGH_SLOT:
266                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
267                 case -NFS4ERR_DEADSESSION:
268                 case -NFS4ERR_SEQ_FALSE_RETRY:
269                 case -NFS4ERR_SEQ_MISORDERED:
270                         dprintk("%s ERROR: %d Reset session\n", __func__,
271                                 errorcode);
272                         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
273                         exception->retry = 1;
274                         /* FALLTHROUGH */
275 #endif /* !defined(CONFIG_NFS_V4_1) */
276                 case -NFS4ERR_FILE_OPEN:
277                 case -NFS4ERR_GRACE:
278                 case -NFS4ERR_DELAY:
279                         ret = nfs4_delay(server->client, &exception->timeout);
280                         if (ret != 0)
281                                 break;
282                 case -NFS4ERR_OLD_STATEID:
283                         exception->retry = 1;
284         }
285         /* We failed to handle the error */
286         return nfs4_map_errors(ret);
287 }
288 
289 
290 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
291 {
292         struct nfs_client *clp = server->nfs_client;
293         spin_lock(&clp->cl_lock);
294         if (time_before(clp->cl_last_renewal,timestamp))
295                 clp->cl_last_renewal = timestamp;
296         spin_unlock(&clp->cl_lock);
297 }
298 
299 #if defined(CONFIG_NFS_V4_1)
300 
301 /*
302  * nfs4_free_slot - free a slot and efficiently update slot table.
303  *
304  * freeing a slot is trivially done by clearing its respective bit
305  * in the bitmap.
306  * If the freed slotid equals highest_used_slotid we want to update it
307  * so that the server would be able to size down the slot table if needed,
308  * otherwise we know that the highest_used_slotid is still in use.
309  * When updating highest_used_slotid there may be "holes" in the bitmap
310  * so we need to scan down from highest_used_slotid to 0 looking for the now
311  * highest slotid in use.
312  * If none found, highest_used_slotid is set to -1.
313  */
314 static void
315 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
316 {
317         int slotid = free_slotid;
318 
319         spin_lock(&tbl->slot_tbl_lock);
320         /* clear used bit in bitmap */
321         __clear_bit(slotid, tbl->used_slots);
322 
323         /* update highest_used_slotid when it is freed */
324         if (slotid == tbl->highest_used_slotid) {
325                 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
326                 if (slotid >= 0 && slotid < tbl->max_slots)
327                         tbl->highest_used_slotid = slotid;
328                 else
329                         tbl->highest_used_slotid = -1;
330         }
331         rpc_wake_up_next(&tbl->slot_tbl_waitq);
332         spin_unlock(&tbl->slot_tbl_lock);
333         dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
334                 free_slotid, tbl->highest_used_slotid);
335 }
336 
337 void nfs41_sequence_free_slot(const struct nfs_client *clp,
338                               struct nfs4_sequence_res *res)
339 {
340         struct nfs4_slot_table *tbl;
341 
342         if (!nfs4_has_session(clp)) {
343                 dprintk("%s: No session\n", __func__);
344                 return;
345         }
346         tbl = &clp->cl_session->fc_slot_table;
347         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
348                 dprintk("%s: No slot\n", __func__);
349                 /* just wake up the next guy waiting since
350                  * we may have not consumed a slot after all */
351                 rpc_wake_up_next(&tbl->slot_tbl_waitq);
352                 return;
353         }
354         nfs4_free_slot(tbl, res->sr_slotid);
355         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
356 }
357 
358 static void nfs41_sequence_done(struct nfs_client *clp,
359                                 struct nfs4_sequence_res *res,
360                                 int rpc_status)
361 {
362         unsigned long timestamp;
363         struct nfs4_slot_table *tbl;
364         struct nfs4_slot *slot;
365 
366         /*
367          * sr_status remains 1 if an RPC level error occurred. The server
368          * may or may not have processed the sequence operation..
369          * Proceed as if the server received and processed the sequence
370          * operation.
371          */
372         if (res->sr_status == 1)
373                 res->sr_status = NFS_OK;
374 
375         /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
376         if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
377                 goto out;
378 
379         tbl = &clp->cl_session->fc_slot_table;
380         slot = tbl->slots + res->sr_slotid;
381 
382         if (res->sr_status == 0) {
383                 /* Update the slot's sequence and clientid lease timer */
384                 ++slot->seq_nr;
385                 timestamp = res->sr_renewal_time;
386                 spin_lock(&clp->cl_lock);
387                 if (time_before(clp->cl_last_renewal, timestamp))
388                         clp->cl_last_renewal = timestamp;
389                 spin_unlock(&clp->cl_lock);
390                 return;
391         }
392 out:
393         /* The session may be reset by one of the error handlers. */
394         dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
395         nfs41_sequence_free_slot(clp, res);
396 }
397 
398 /*
399  * nfs4_find_slot - efficiently look for a free slot
400  *
401  * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
402  * If found, we mark the slot as used, update the highest_used_slotid,
403  * and respectively set up the sequence operation args.
404  * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
405  *
406  * Note: must be called with under the slot_tbl_lock.
407  */
408 static u8
409 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
410 {
411         int slotid;
412         u8 ret_id = NFS4_MAX_SLOT_TABLE;
413         BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
414 
415         dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
416                 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
417                 tbl->max_slots);
418         slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
419         if (slotid >= tbl->max_slots)
420                 goto out;
421         __set_bit(slotid, tbl->used_slots);
422         if (slotid > tbl->highest_used_slotid)
423                 tbl->highest_used_slotid = slotid;
424         ret_id = slotid;
425 out:
426         dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
427                 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
428         return ret_id;
429 }
430 
431 static int nfs4_recover_session(struct nfs4_session *session)
432 {
433         struct nfs_client *clp = session->clp;
434         int ret;
435 
436         for (;;) {
437                 ret = nfs4_wait_clnt_recover(clp);
438                 if (ret != 0)
439                                 return ret;
440                 if (!test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
441                         break;
442                 nfs4_schedule_state_manager(clp);
443         }
444         return 0;
445 }
446 
447 static int nfs41_setup_sequence(struct nfs4_session *session,
448                                 struct nfs4_sequence_args *args,
449                                 struct nfs4_sequence_res *res,
450                                 int cache_reply,
451                                 struct rpc_task *task)
452 {
453         struct nfs4_slot *slot;
454         struct nfs4_slot_table *tbl;
455         int status = 0;
456         u8 slotid;
457 
458         dprintk("--> %s\n", __func__);
459         /* slot already allocated? */
460         if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
461                 return 0;
462 
463         memset(res, 0, sizeof(*res));
464         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
465         tbl = &session->fc_slot_table;
466 
467         spin_lock(&tbl->slot_tbl_lock);
468         if (test_bit(NFS4CLNT_SESSION_SETUP, &session->clp->cl_state)) {
469                 if (tbl->highest_used_slotid != -1) {
470                         rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
471                         spin_unlock(&tbl->slot_tbl_lock);
472                         dprintk("<-- %s: Session reset: draining\n", __func__);
473                         return -EAGAIN;
474                 }
475 
476                 /* The slot table is empty; start the reset thread */
477                 dprintk("%s Session Reset\n", __func__);
478                 spin_unlock(&tbl->slot_tbl_lock);
479                 status = nfs4_recover_session(session);
480                 if (status)
481                         return status;
482                 spin_lock(&tbl->slot_tbl_lock);
483         }
484 
485         slotid = nfs4_find_slot(tbl, task);
486         if (slotid == NFS4_MAX_SLOT_TABLE) {
487                 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
488                 spin_unlock(&tbl->slot_tbl_lock);
489                 dprintk("<-- %s: no free slots\n", __func__);
490                 return -EAGAIN;
491         }
492         spin_unlock(&tbl->slot_tbl_lock);
493 
494         slot = tbl->slots + slotid;
495         args->sa_session = session;
496         args->sa_slotid = slotid;
497         args->sa_cache_this = cache_reply;
498 
499         dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
500 
501         res->sr_session = session;
502         res->sr_slotid = slotid;
503         res->sr_renewal_time = jiffies;
504         /*
505          * sr_status is only set in decode_sequence, and so will remain
506          * set to 1 if an rpc level failure occurs.
507          */
508         res->sr_status = 1;
509         return 0;
510 }
511 
512 int nfs4_setup_sequence(struct nfs_client *clp,
513                         struct nfs4_sequence_args *args,
514                         struct nfs4_sequence_res *res,
515                         int cache_reply,
516                         struct rpc_task *task)
517 {
518         int ret = 0;
519 
520         dprintk("--> %s clp %p session %p sr_slotid %d\n",
521                 __func__, clp, clp->cl_session, res->sr_slotid);
522 
523         if (!nfs4_has_session(clp))
524                 goto out;
525         ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
526                                    task);
527         if (ret != -EAGAIN) {
528                 /* terminate rpc task */
529                 task->tk_status = ret;
530                 task->tk_action = NULL;
531         }
532 out:
533         dprintk("<-- %s status=%d\n", __func__, ret);
534         return ret;
535 }
536 
537 struct nfs41_call_sync_data {
538         struct nfs_client *clp;
539         struct nfs4_sequence_args *seq_args;
540         struct nfs4_sequence_res *seq_res;
541         int cache_reply;
542 };
543 
544 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
545 {
546         struct nfs41_call_sync_data *data = calldata;
547 
548         dprintk("--> %s data->clp->cl_session %p\n", __func__,
549                 data->clp->cl_session);
550         if (nfs4_setup_sequence(data->clp, data->seq_args,
551                                 data->seq_res, data->cache_reply, task))
552                 return;
553         rpc_call_start(task);
554 }
555 
556 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
557 {
558         struct nfs41_call_sync_data *data = calldata;
559 
560         nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
561         nfs41_sequence_free_slot(data->clp, data->seq_res);
562 }
563 
564 struct rpc_call_ops nfs41_call_sync_ops = {
565         .rpc_call_prepare = nfs41_call_sync_prepare,
566         .rpc_call_done = nfs41_call_sync_done,
567 };
568 
569 static int nfs4_call_sync_sequence(struct nfs_client *clp,
570                                    struct rpc_clnt *clnt,
571                                    struct rpc_message *msg,
572                                    struct nfs4_sequence_args *args,
573                                    struct nfs4_sequence_res *res,
574                                    int cache_reply)
575 {
576         int ret;
577         struct rpc_task *task;
578         struct nfs41_call_sync_data data = {
579                 .clp = clp,
580                 .seq_args = args,
581                 .seq_res = res,
582                 .cache_reply = cache_reply,
583         };
584         struct rpc_task_setup task_setup = {
585                 .rpc_client = clnt,
586                 .rpc_message = msg,
587                 .callback_ops = &nfs41_call_sync_ops,
588                 .callback_data = &data
589         };
590 
591         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
592         task = rpc_run_task(&task_setup);
593         if (IS_ERR(task))
594                 ret = PTR_ERR(task);
595         else {
596                 ret = task->tk_status;
597                 rpc_put_task(task);
598         }
599         return ret;
600 }
601 
602 int _nfs4_call_sync_session(struct nfs_server *server,
603                             struct rpc_message *msg,
604                             struct nfs4_sequence_args *args,
605                             struct nfs4_sequence_res *res,
606                             int cache_reply)
607 {
608         return nfs4_call_sync_sequence(server->nfs_client, server->client,
609                                        msg, args, res, cache_reply);
610 }
611 
612 #endif /* CONFIG_NFS_V4_1 */
613 
614 int _nfs4_call_sync(struct nfs_server *server,
615                     struct rpc_message *msg,
616                     struct nfs4_sequence_args *args,
617                     struct nfs4_sequence_res *res,
618                     int cache_reply)
619 {
620         args->sa_session = res->sr_session = NULL;
621         return rpc_call_sync(server->client, msg, 0);
622 }
623 
624 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
625         (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
626                         &(res)->seq_res, (cache_reply))
627 
628 static void nfs4_sequence_done(const struct nfs_server *server,
629                                struct nfs4_sequence_res *res, int rpc_status)
630 {
631 #ifdef CONFIG_NFS_V4_1
632         if (nfs4_has_session(server->nfs_client))
633                 nfs41_sequence_done(server->nfs_client, res, rpc_status);
634 #endif /* CONFIG_NFS_V4_1 */
635 }
636 
637 /* no restart, therefore free slot here */
638 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
639                                          struct nfs4_sequence_res *res,
640                                          int rpc_status)
641 {
642         nfs4_sequence_done(server, res, rpc_status);
643         nfs4_sequence_free_slot(server->nfs_client, res);
644 }
645 
646 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
647 {
648         struct nfs_inode *nfsi = NFS_I(dir);
649 
650         spin_lock(&dir->i_lock);
651         nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
652         if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
653                 nfs_force_lookup_revalidate(dir);
654         nfsi->change_attr = cinfo->after;
655         spin_unlock(&dir->i_lock);
656 }
657 
658 struct nfs4_opendata {
659         struct kref kref;
660         struct nfs_openargs o_arg;
661         struct nfs_openres o_res;
662         struct nfs_open_confirmargs c_arg;
663         struct nfs_open_confirmres c_res;
664         struct nfs_fattr f_attr;
665         struct nfs_fattr dir_attr;
666         struct path path;
667         struct dentry *dir;
668         struct nfs4_state_owner *owner;
669         struct nfs4_state *state;
670         struct iattr attrs;
671         unsigned long timestamp;
672         unsigned int rpc_done : 1;
673         int rpc_status;
674         int cancelled;
675 };
676 
677 
678 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
679 {
680         p->o_res.f_attr = &p->f_attr;
681         p->o_res.dir_attr = &p->dir_attr;
682         p->o_res.seqid = p->o_arg.seqid;
683         p->c_res.seqid = p->c_arg.seqid;
684         p->o_res.server = p->o_arg.server;
685         nfs_fattr_init(&p->f_attr);
686         nfs_fattr_init(&p->dir_attr);
687         p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
688 }
689 
690 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
691                 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
692                 const struct iattr *attrs)
693 {
694         struct dentry *parent = dget_parent(path->dentry);
695         struct inode *dir = parent->d_inode;
696         struct nfs_server *server = NFS_SERVER(dir);
697         struct nfs4_opendata *p;
698 
699         p = kzalloc(sizeof(*p), GFP_KERNEL);
700         if (p == NULL)
701                 goto err;
702         p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
703         if (p->o_arg.seqid == NULL)
704                 goto err_free;
705         p->path.mnt = mntget(path->mnt);
706         p->path.dentry = dget(path->dentry);
707         p->dir = parent;
708         p->owner = sp;
709         atomic_inc(&sp->so_count);
710         p->o_arg.fh = NFS_FH(dir);
711         p->o_arg.open_flags = flags;
712         p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
713         p->o_arg.clientid = server->nfs_client->cl_clientid;
714         p->o_arg.id = sp->so_owner_id.id;
715         p->o_arg.name = &p->path.dentry->d_name;
716         p->o_arg.server = server;
717         p->o_arg.bitmask = server->attr_bitmask;
718         p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
719         if (flags & O_EXCL) {
720                 u32 *s = (u32 *) p->o_arg.u.verifier.data;
721                 s[0] = jiffies;
722                 s[1] = current->pid;
723         } else if (flags & O_CREAT) {
724                 p->o_arg.u.attrs = &p->attrs;
725                 memcpy(&p->attrs, attrs, sizeof(p->attrs));
726         }
727         p->c_arg.fh = &p->o_res.fh;
728         p->c_arg.stateid = &p->o_res.stateid;
729         p->c_arg.seqid = p->o_arg.seqid;
730         nfs4_init_opendata_res(p);
731         kref_init(&p->kref);
732         return p;
733 err_free:
734         kfree(p);
735 err:
736         dput(parent);
737         return NULL;
738 }
739 
740 static void nfs4_opendata_free(struct kref *kref)
741 {
742         struct nfs4_opendata *p = container_of(kref,
743                         struct nfs4_opendata, kref);
744 
745         nfs_free_seqid(p->o_arg.seqid);
746         if (p->state != NULL)
747                 nfs4_put_open_state(p->state);
748         nfs4_put_state_owner(p->owner);
749         dput(p->dir);
750         path_put(&p->path);
751         kfree(p);
752 }
753 
754 static void nfs4_opendata_put(struct nfs4_opendata *p)
755 {
756         if (p != NULL)
757                 kref_put(&p->kref, nfs4_opendata_free);
758 }
759 
760 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
761 {
762         int ret;
763 
764         ret = rpc_wait_for_completion_task(task);
765         return ret;
766 }
767 
768 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
769 {
770         int ret = 0;
771 
772         if (open_mode & O_EXCL)
773                 goto out;
774         switch (mode & (FMODE_READ|FMODE_WRITE)) {
775                 case FMODE_READ:
776                         ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
777                         break;
778                 case FMODE_WRITE:
779                         ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
780                         break;
781                 case FMODE_READ|FMODE_WRITE:
782                         ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
783         }
784 out:
785         return ret;
786 }
787 
788 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
789 {
790         if ((delegation->type & fmode) != fmode)
791                 return 0;
792         if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
793                 return 0;
794         nfs_mark_delegation_referenced(delegation);
795         return 1;
796 }
797 
798 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
799 {
800         switch (fmode) {
801                 case FMODE_WRITE:
802                         state->n_wronly++;
803                         break;
804                 case FMODE_READ:
805                         state->n_rdonly++;
806                         break;
807                 case FMODE_READ|FMODE_WRITE:
808                         state->n_rdwr++;
809         }
810         nfs4_state_set_mode_locked(state, state->state | fmode);
811 }
812 
813 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
814 {
815         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
816                 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
817         memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
818         switch (fmode) {
819                 case FMODE_READ:
820                         set_bit(NFS_O_RDONLY_STATE, &state->flags);
821                         break;
822                 case FMODE_WRITE:
823                         set_bit(NFS_O_WRONLY_STATE, &state->flags);
824                         break;
825                 case FMODE_READ|FMODE_WRITE:
826                         set_bit(NFS_O_RDWR_STATE, &state->flags);
827         }
828 }
829 
830 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
831 {
832         write_seqlock(&state->seqlock);
833         nfs_set_open_stateid_locked(state, stateid, fmode);
834         write_sequnlock(&state->seqlock);
835 }
836 
837 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
838 {
839         /*
840          * Protect the call to nfs4_state_set_mode_locked and
841          * serialise the stateid update
842          */
843         write_seqlock(&state->seqlock);
844         if (deleg_stateid != NULL) {
845                 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
846                 set_bit(NFS_DELEGATED_STATE, &state->flags);
847         }
848         if (open_stateid != NULL)
849                 nfs_set_open_stateid_locked(state, open_stateid, fmode);
850         write_sequnlock(&state->seqlock);
851         spin_lock(&state->owner->so_lock);
852         update_open_stateflags(state, fmode);
853         spin_unlock(&state->owner->so_lock);
854 }
855 
856 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
857 {
858         struct nfs_inode *nfsi = NFS_I(state->inode);
859         struct nfs_delegation *deleg_cur;
860         int ret = 0;
861 
862         fmode &= (FMODE_READ|FMODE_WRITE);
863 
864         rcu_read_lock();
865         deleg_cur = rcu_dereference(nfsi->delegation);
866         if (deleg_cur == NULL)
867                 goto no_delegation;
868 
869         spin_lock(&deleg_cur->lock);
870         if (nfsi->delegation != deleg_cur ||
871             (deleg_cur->type & fmode) != fmode)
872                 goto no_delegation_unlock;
873 
874         if (delegation == NULL)
875                 delegation = &deleg_cur->stateid;
876         else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
877                 goto no_delegation_unlock;
878 
879         nfs_mark_delegation_referenced(deleg_cur);
880         __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
881         ret = 1;
882 no_delegation_unlock:
883         spin_unlock(&deleg_cur->lock);
884 no_delegation:
885         rcu_read_unlock();
886 
887         if (!ret && open_stateid != NULL) {
888                 __update_open_stateid(state, open_stateid, NULL, fmode);
889                 ret = 1;
890         }
891 
892         return ret;
893 }
894 
895 
896 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
897 {
898         struct nfs_delegation *delegation;
899 
900         rcu_read_lock();
901         delegation = rcu_dereference(NFS_I(inode)->delegation);
902         if (delegation == NULL || (delegation->type & fmode) == fmode) {
903                 rcu_read_unlock();
904                 return;
905         }
906         rcu_read_unlock();
907         nfs_inode_return_delegation(inode);
908 }
909 
910 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
911 {
912         struct nfs4_state *state = opendata->state;
913         struct nfs_inode *nfsi = NFS_I(state->inode);
914         struct nfs_delegation *delegation;
915         int open_mode = opendata->o_arg.open_flags & O_EXCL;
916         fmode_t fmode = opendata->o_arg.fmode;
917         nfs4_stateid stateid;
918         int ret = -EAGAIN;
919 
920         for (;;) {
921                 if (can_open_cached(state, fmode, open_mode)) {
922                         spin_lock(&state->owner->so_lock);
923                         if (can_open_cached(state, fmode, open_mode)) {
924                                 update_open_stateflags(state, fmode);
925                                 spin_unlock(&state->owner->so_lock);
926                                 goto out_return_state;
927                         }
928                         spin_unlock(&state->owner->so_lock);
929                 }
930                 rcu_read_lock();
931                 delegation = rcu_dereference(nfsi->delegation);
932                 if (delegation == NULL ||
933                     !can_open_delegated(delegation, fmode)) {
934                         rcu_read_unlock();
935                         break;
936                 }
937                 /* Save the delegation */
938                 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
939                 rcu_read_unlock();
940                 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
941                 if (ret != 0)
942                         goto out;
943                 ret = -EAGAIN;
944 
945                 /* Try to update the stateid using the delegation */
946                 if (update_open_stateid(state, NULL, &stateid, fmode))
947                         goto out_return_state;
948         }
949 out:
950         return ERR_PTR(ret);
951 out_return_state:
952         atomic_inc(&state->count);
953         return state;
954 }
955 
956 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
957 {
958         struct inode *inode;
959         struct nfs4_state *state = NULL;
960         struct nfs_delegation *delegation;
961         int ret;
962 
963         if (!data->rpc_done) {
964                 state = nfs4_try_open_cached(data);
965                 goto out;
966         }
967 
968         ret = -EAGAIN;
969         if (!(data->f_attr.valid & NFS_ATTR_FATTR))
970                 goto err;
971         inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
972         ret = PTR_ERR(inode);
973         if (IS_ERR(inode))
974                 goto err;
975         ret = -ENOMEM;
976         state = nfs4_get_open_state(inode, data->owner);
977         if (state == NULL)
978                 goto err_put_inode;
979         if (data->o_res.delegation_type != 0) {
980                 int delegation_flags = 0;
981 
982                 rcu_read_lock();
983                 delegation = rcu_dereference(NFS_I(inode)->delegation);
984                 if (delegation)
985                         delegation_flags = delegation->flags;
986                 rcu_read_unlock();
987                 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
988                         nfs_inode_set_delegation(state->inode,
989                                         data->owner->so_cred,
990                                         &data->o_res);
991                 else
992                         nfs_inode_reclaim_delegation(state->inode,
993                                         data->owner->so_cred,
994                                         &data->o_res);
995         }
996 
997         update_open_stateid(state, &data->o_res.stateid, NULL,
998                         data->o_arg.fmode);
999         iput(inode);
1000 out:
1001         return state;
1002 err_put_inode:
1003         iput(inode);
1004 err:
1005         return ERR_PTR(ret);
1006 }
1007 
1008 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1009 {
1010         struct nfs_inode *nfsi = NFS_I(state->inode);
1011         struct nfs_open_context *ctx;
1012 
1013         spin_lock(&state->inode->i_lock);
1014         list_for_each_entry(ctx, &nfsi->open_files, list) {
1015                 if (ctx->state != state)
1016                         continue;
1017                 get_nfs_open_context(ctx);
1018                 spin_unlock(&state->inode->i_lock);
1019                 return ctx;
1020         }
1021         spin_unlock(&state->inode->i_lock);
1022         return ERR_PTR(-ENOENT);
1023 }
1024 
1025 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1026 {
1027         struct nfs4_opendata *opendata;
1028 
1029         opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
1030         if (opendata == NULL)
1031                 return ERR_PTR(-ENOMEM);
1032         opendata->state = state;
1033         atomic_inc(&state->count);
1034         return opendata;
1035 }
1036 
1037 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1038 {
1039         struct nfs4_state *newstate;
1040         int ret;
1041 
1042         opendata->o_arg.open_flags = 0;
1043         opendata->o_arg.fmode = fmode;
1044         memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1045         memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1046         nfs4_init_opendata_res(opendata);
1047         ret = _nfs4_proc_open(opendata);
1048         if (ret != 0)
1049                 return ret; 
1050         newstate = nfs4_opendata_to_nfs4_state(opendata);
1051         if (IS_ERR(newstate))
1052                 return PTR_ERR(newstate);
1053         nfs4_close_state(&opendata->path, newstate, fmode);
1054         *res = newstate;
1055         return 0;
1056 }
1057 
1058 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1059 {
1060         struct nfs4_state *newstate;
1061         int ret;
1062 
1063         /* memory barrier prior to reading state->n_* */
1064         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1065         smp_rmb();
1066         if (state->n_rdwr != 0) {
1067                 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1068                 if (ret != 0)
1069                         return ret;
1070                 if (newstate != state)
1071                         return -ESTALE;
1072         }
1073         if (state->n_wronly != 0) {
1074                 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1075                 if (ret != 0)
1076                         return ret;
1077                 if (newstate != state)
1078                         return -ESTALE;
1079         }
1080         if (state->n_rdonly != 0) {
1081                 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1082                 if (ret != 0)
1083                         return ret;
1084                 if (newstate != state)
1085                         return -ESTALE;
1086         }
1087         /*
1088          * We may have performed cached opens for all three recoveries.
1089          * Check if we need to update the current stateid.
1090          */
1091         if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1092             memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1093                 write_seqlock(&state->seqlock);
1094                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1095                         memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1096                 write_sequnlock(&state->seqlock);
1097         }
1098         return 0;
1099 }
1100 
1101 /*
1102  * OPEN_RECLAIM:
1103  *      reclaim state on the server after a reboot.
1104  */
1105 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1106 {
1107         struct nfs_delegation *delegation;
1108         struct nfs4_opendata *opendata;
1109         fmode_t delegation_type = 0;
1110         int status;
1111 
1112         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1113         if (IS_ERR(opendata))
1114                 return PTR_ERR(opendata);
1115         opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1116         opendata->o_arg.fh = NFS_FH(state->inode);
1117         rcu_read_lock();
1118         delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1119         if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1120                 delegation_type = delegation->type;
1121         rcu_read_unlock();
1122         opendata->o_arg.u.delegation_type = delegation_type;
1123         status = nfs4_open_recover(opendata, state);
1124         nfs4_opendata_put(opendata);
1125         return status;
1126 }
1127 
1128 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1129 {
1130         struct nfs_server *server = NFS_SERVER(state->inode);
1131         struct nfs4_exception exception = { };
1132         int err;
1133         do {
1134                 err = _nfs4_do_open_reclaim(ctx, state);
1135                 if (err != -NFS4ERR_DELAY)
1136                         break;
1137                 nfs4_handle_exception(server, err, &exception);
1138         } while (exception.retry);
1139         return err;
1140 }
1141 
1142 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1143 {
1144         struct nfs_open_context *ctx;
1145         int ret;
1146 
1147         ctx = nfs4_state_find_open_context(state);
1148         if (IS_ERR(ctx))
1149                 return PTR_ERR(ctx);
1150         ret = nfs4_do_open_reclaim(ctx, state);
1151         put_nfs_open_context(ctx);
1152         return ret;
1153 }
1154 
1155 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1156 {
1157         struct nfs4_opendata *opendata;
1158         int ret;
1159 
1160         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1161         if (IS_ERR(opendata))
1162                 return PTR_ERR(opendata);
1163         opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1164         memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1165                         sizeof(opendata->o_arg.u.delegation.data));
1166         ret = nfs4_open_recover(opendata, state);
1167         nfs4_opendata_put(opendata);
1168         return ret;
1169 }
1170 
1171 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1172 {
1173         struct nfs4_exception exception = { };
1174         struct nfs_server *server = NFS_SERVER(state->inode);
1175         int err;
1176         do {
1177                 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1178                 switch (err) {
1179                         case 0:
1180                         case -ENOENT:
1181                         case -ESTALE:
1182                                 goto out;
1183                         case -NFS4ERR_STALE_CLIENTID:
1184                         case -NFS4ERR_STALE_STATEID:
1185                         case -NFS4ERR_EXPIRED:
1186                                 /* Don't recall a delegation if it was lost */
1187                                 nfs4_schedule_state_recovery(server->nfs_client);
1188                                 goto out;
1189                         case -ERESTARTSYS:
1190                                 /*
1191                                  * The show must go on: exit, but mark the
1192                                  * stateid as needing recovery.
1193                                  */
1194                         case -NFS4ERR_ADMIN_REVOKED:
1195                         case -NFS4ERR_BAD_STATEID:
1196                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1197                         case -ENOMEM:
1198                                 err = 0;
1199                                 goto out;
1200                 }
1201                 err = nfs4_handle_exception(server, err, &exception);
1202         } while (exception.retry);
1203 out:
1204         return err;
1205 }
1206 
1207 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1208 {
1209         struct nfs4_opendata *data = calldata;
1210 
1211         data->rpc_status = task->tk_status;
1212         if (RPC_ASSASSINATED(task))
1213                 return;
1214         if (data->rpc_status == 0) {
1215                 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1216                                 sizeof(data->o_res.stateid.data));
1217                 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1218                 renew_lease(data->o_res.server, data->timestamp);
1219                 data->rpc_done = 1;
1220         }
1221 }
1222 
1223 static void nfs4_open_confirm_release(void *calldata)
1224 {
1225         struct nfs4_opendata *data = calldata;
1226         struct nfs4_state *state = NULL;
1227 
1228         /* If this request hasn't been cancelled, do nothing */
1229         if (data->cancelled == 0)
1230                 goto out_free;
1231         /* In case of error, no cleanup! */
1232         if (!data->rpc_done)
1233                 goto out_free;
1234         state = nfs4_opendata_to_nfs4_state(data);
1235         if (!IS_ERR(state))
1236                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1237 out_free:
1238         nfs4_opendata_put(data);
1239 }
1240 
1241 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1242         .rpc_call_done = nfs4_open_confirm_done,
1243         .rpc_release = nfs4_open_confirm_release,
1244 };
1245 
1246 /*
1247  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1248  */
1249 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1250 {
1251         struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1252         struct rpc_task *task;
1253         struct  rpc_message msg = {
1254                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1255                 .rpc_argp = &data->c_arg,
1256                 .rpc_resp = &data->c_res,
1257                 .rpc_cred = data->owner->so_cred,
1258         };
1259         struct rpc_task_setup task_setup_data = {
1260                 .rpc_client = server->client,
1261                 .rpc_message = &msg,
1262                 .callback_ops = &nfs4_open_confirm_ops,
1263                 .callback_data = data,
1264                 .workqueue = nfsiod_workqueue,
1265                 .flags = RPC_TASK_ASYNC,
1266         };
1267         int status;
1268 
1269         kref_get(&data->kref);
1270         data->rpc_done = 0;
1271         data->rpc_status = 0;
1272         data->timestamp = jiffies;
1273         task = rpc_run_task(&task_setup_data);
1274         if (IS_ERR(task))
1275                 return PTR_ERR(task);
1276         status = nfs4_wait_for_completion_rpc_task(task);
1277         if (status != 0) {
1278                 data->cancelled = 1;
1279                 smp_wmb();
1280         } else
1281                 status = data->rpc_status;
1282         rpc_put_task(task);
1283         return status;
1284 }
1285 
1286 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1287 {
1288         struct nfs4_opendata *data = calldata;
1289         struct nfs4_state_owner *sp = data->owner;
1290 
1291         if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1292                 return;
1293         /*
1294          * Check if we still need to send an OPEN call, or if we can use
1295          * a delegation instead.
1296          */
1297         if (data->state != NULL) {
1298                 struct nfs_delegation *delegation;
1299 
1300                 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1301                         goto out_no_action;
1302                 rcu_read_lock();
1303                 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1304                 if (delegation != NULL &&
1305                     test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1306                         rcu_read_unlock();
1307                         goto out_no_action;
1308                 }
1309                 rcu_read_unlock();
1310         }
1311         /* Update sequence id. */
1312         data->o_arg.id = sp->so_owner_id.id;
1313         data->o_arg.clientid = sp->so_client->cl_clientid;
1314         if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1315                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1316                 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1317         }
1318         data->timestamp = jiffies;
1319         if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1320                                 &data->o_arg.seq_args,
1321                                 &data->o_res.seq_res, 1, task))
1322                 return;
1323         rpc_call_start(task);
1324         return;
1325 out_no_action:
1326         task->tk_action = NULL;
1327 
1328 }
1329 
1330 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1331 {
1332         struct nfs4_opendata *data = calldata;
1333 
1334         data->rpc_status = task->tk_status;
1335 
1336         nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1337                                      task->tk_status);
1338 
1339         if (RPC_ASSASSINATED(task))
1340                 return;
1341         if (task->tk_status == 0) {
1342                 switch (data->o_res.f_attr->mode & S_IFMT) {
1343                         case S_IFREG:
1344                                 break;
1345                         case S_IFLNK:
1346                                 data->rpc_status = -ELOOP;
1347                                 break;
1348                         case S_IFDIR:
1349                                 data->rpc_status = -EISDIR;
1350                                 break;
1351                         default:
1352                                 data->rpc_status = -ENOTDIR;
1353                 }
1354                 renew_lease(data->o_res.server, data->timestamp);
1355                 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1356                         nfs_confirm_seqid(&data->owner->so_seqid, 0);
1357         }
1358         data->rpc_done = 1;
1359 }
1360 
1361 static void nfs4_open_release(void *calldata)
1362 {
1363         struct nfs4_opendata *data = calldata;
1364         struct nfs4_state *state = NULL;
1365 
1366         /* If this request hasn't been cancelled, do nothing */
1367         if (data->cancelled == 0)
1368                 goto out_free;
1369         /* In case of error, no cleanup! */
1370         if (data->rpc_status != 0 || !data->rpc_done)
1371                 goto out_free;
1372         /* In case we need an open_confirm, no cleanup! */
1373         if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1374                 goto out_free;
1375         state = nfs4_opendata_to_nfs4_state(data);
1376         if (!IS_ERR(state))
1377                 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1378 out_free:
1379         nfs4_opendata_put(data);
1380 }
1381 
1382 static const struct rpc_call_ops nfs4_open_ops = {
1383         .rpc_call_prepare = nfs4_open_prepare,
1384         .rpc_call_done = nfs4_open_done,
1385         .rpc_release = nfs4_open_release,
1386 };
1387 
1388 /*
1389  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1390  */
1391 static int _nfs4_proc_open(struct nfs4_opendata *data)
1392 {
1393         struct inode *dir = data->dir->d_inode;
1394         struct nfs_server *server = NFS_SERVER(dir);
1395         struct nfs_openargs *o_arg = &data->o_arg;
1396         struct nfs_openres *o_res = &data->o_res;
1397         struct rpc_task *task;
1398         struct rpc_message msg = {
1399                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1400                 .rpc_argp = o_arg,
1401                 .rpc_resp = o_res,
1402                 .rpc_cred = data->owner->so_cred,
1403         };
1404         struct rpc_task_setup task_setup_data = {
1405                 .rpc_client = server->client,
1406                 .rpc_message = &msg,
1407                 .callback_ops = &nfs4_open_ops,
1408                 .callback_data = data,
1409                 .workqueue = nfsiod_workqueue,
1410                 .flags = RPC_TASK_ASYNC,
1411         };
1412         int status;
1413 
1414         kref_get(&data->kref);
1415         data->rpc_done = 0;
1416         data->rpc_status = 0;
1417         data->cancelled = 0;
1418         task = rpc_run_task(&task_setup_data);
1419         if (IS_ERR(task))
1420                 return PTR_ERR(task);
1421         status = nfs4_wait_for_completion_rpc_task(task);
1422         if (status != 0) {
1423                 data->cancelled = 1;
1424                 smp_wmb();
1425         } else
1426                 status = data->rpc_status;
1427         rpc_put_task(task);
1428         if (status != 0 || !data->rpc_done)
1429                 return status;
1430 
1431         if (o_res->fh.size == 0)
1432                 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1433 
1434         if (o_arg->open_flags & O_CREAT) {
1435                 update_changeattr(dir, &o_res->cinfo);
1436                 nfs_post_op_update_inode(dir, o_res->dir_attr);
1437         } else
1438                 nfs_refresh_inode(dir, o_res->dir_attr);
1439         if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1440                 status = _nfs4_proc_open_confirm(data);
1441                 if (status != 0)
1442                         return status;
1443         }
1444         if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1445                 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1446         return 0;
1447 }
1448 
1449 static int nfs4_recover_expired_lease(struct nfs_server *server)
1450 {
1451         struct nfs_client *clp = server->nfs_client;
1452         int ret;
1453 
1454         for (;;) {
1455                 ret = nfs4_wait_clnt_recover(clp);
1456                 if (ret != 0)
1457                         return ret;
1458                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1459                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1460                         break;
1461                 nfs4_schedule_state_recovery(clp);
1462         }
1463         return 0;
1464 }
1465 
1466 /*
1467  * OPEN_EXPIRED:
1468  *      reclaim state on the server after a network partition.
1469  *      Assumes caller holds the appropriate lock
1470  */
1471 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1472 {
1473         struct nfs4_opendata *opendata;
1474         int ret;
1475 
1476         opendata = nfs4_open_recoverdata_alloc(ctx, state);
1477         if (IS_ERR(opendata))
1478                 return PTR_ERR(opendata);
1479         ret = nfs4_open_recover(opendata, state);
1480         if (ret == -ESTALE)
1481                 d_drop(ctx->path.dentry);
1482         nfs4_opendata_put(opendata);
1483         return ret;
1484 }
1485 
1486 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1487 {
1488         struct nfs_server *server = NFS_SERVER(state->inode);
1489         struct nfs4_exception exception = { };
1490         int err;
1491 
1492         do {
1493                 err = _nfs4_open_expired(ctx, state);
1494                 if (err != -NFS4ERR_DELAY)
1495                         break;
1496                 nfs4_handle_exception(server, err, &exception);
1497         } while (exception.retry);
1498         return err;
1499 }
1500 
1501 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1502 {
1503         struct nfs_open_context *ctx;
1504         int ret;
1505 
1506         ctx = nfs4_state_find_open_context(state);
1507         if (IS_ERR(ctx))
1508                 return PTR_ERR(ctx);
1509         ret = nfs4_do_open_expired(ctx, state);
1510         put_nfs_open_context(ctx);
1511         return ret;
1512 }
1513 
1514 /*
1515  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1516  * fields corresponding to attributes that were used to store the verifier.
1517  * Make sure we clobber those fields in the later setattr call
1518  */
1519 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1520 {
1521         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1522             !(sattr->ia_valid & ATTR_ATIME_SET))
1523                 sattr->ia_valid |= ATTR_ATIME;
1524 
1525         if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1526             !(sattr->ia_valid & ATTR_MTIME_SET))
1527                 sattr->ia_valid |= ATTR_MTIME;
1528 }
1529 
1530 /*
1531  * Returns a referenced nfs4_state
1532  */
1533 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1534 {
1535         struct nfs4_state_owner  *sp;
1536         struct nfs4_state     *state = NULL;
1537         struct nfs_server       *server = NFS_SERVER(dir);
1538         struct nfs4_opendata *opendata;
1539         int status;
1540 
1541         /* Protect against reboot recovery conflicts */
1542         status = -ENOMEM;
1543         if (!(sp = nfs4_get_state_owner(server, cred))) {
1544                 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1545                 goto out_err;
1546         }
1547         status = nfs4_recover_expired_lease(server);
1548         if (status != 0)
1549                 goto err_put_state_owner;
1550         if (path->dentry->d_inode != NULL)
1551                 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1552         status = -ENOMEM;
1553         opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1554         if (opendata == NULL)
1555                 goto err_put_state_owner;
1556 
1557         if (path->dentry->d_inode != NULL)
1558                 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1559 
1560         status = _nfs4_proc_open(opendata);
1561         if (status != 0)
1562                 goto err_opendata_put;
1563 
1564         if (opendata->o_arg.open_flags & O_EXCL)
1565                 nfs4_exclusive_attrset(opendata, sattr);
1566 
1567         state = nfs4_opendata_to_nfs4_state(opendata);
1568         status = PTR_ERR(state);
1569         if (IS_ERR(state))
1570                 goto err_opendata_put;
1571         nfs4_opendata_put(opendata);
1572         nfs4_put_state_owner(sp);
1573         *res = state;
1574         return 0;
1575 err_opendata_put:
1576         nfs4_opendata_put(opendata);
1577 err_put_state_owner:
1578         nfs4_put_state_owner(sp);
1579 out_err:
1580         *res = NULL;
1581         return status;
1582 }
1583 
1584 
1585 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1586 {
1587         struct nfs4_exception exception = { };
1588         struct nfs4_state *res;
1589         int status;
1590 
1591         do {
1592                 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1593                 if (status == 0)
1594                         break;
1595                 /* NOTE: BAD_SEQID means the server and client disagree about the
1596                  * book-keeping w.r.t. state-changing operations
1597                  * (OPEN/CLOSE/LOCK/LOCKU...)
1598                  * It is actually a sign of a bug on the client or on the server.
1599                  *
1600                  * If we receive a BAD_SEQID error in the particular case of
1601                  * doing an OPEN, we assume that nfs_increment_open_seqid() will
1602                  * have unhashed the old state_owner for us, and that we can
1603                  * therefore safely retry using a new one. We should still warn
1604                  * the user though...
1605                  */
1606                 if (status == -NFS4ERR_BAD_SEQID) {
1607                         printk(KERN_WARNING "NFS: v4 server %s "
1608                                         " returned a bad sequence-id error!\n",
1609                                         NFS_SERVER(dir)->nfs_client->cl_hostname);
1610                         exception.retry = 1;
1611                         continue;
1612                 }
1613                 /*
1614                  * BAD_STATEID on OPEN means that the server cancelled our
1615                  * state before it received the OPEN_CONFIRM.
1616                  * Recover by retrying the request as per the discussion
1617                  * on Page 181 of RFC3530.
1618                  */
1619                 if (status == -NFS4ERR_BAD_STATEID) {
1620                         exception.retry = 1;
1621                         continue;
1622                 }
1623                 if (status == -EAGAIN) {
1624                         /* We must have found a delegation */
1625                         exception.retry = 1;
1626                         continue;
1627                 }
1628                 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1629                                         status, &exception));
1630         } while (exception.retry);
1631         return res;
1632 }
1633 
1634 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1635                             struct nfs_fattr *fattr, struct iattr *sattr,
1636                             struct nfs4_state *state)
1637 {
1638         struct nfs_server *server = NFS_SERVER(inode);
1639         struct nfs_setattrargs  arg = {
1640                 .fh             = NFS_FH(inode),
1641                 .iap            = sattr,
1642                 .server         = server,
1643                 .bitmask = server->attr_bitmask,
1644         };
1645         struct nfs_setattrres  res = {
1646                 .fattr          = fattr,
1647                 .server         = server,
1648         };
1649         struct rpc_message msg = {
1650                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1651                 .rpc_argp       = &arg,
1652                 .rpc_resp       = &res,
1653                 .rpc_cred       = cred,
1654         };
1655         unsigned long timestamp = jiffies;
1656         int status;
1657 
1658         nfs_fattr_init(fattr);
1659 
1660         if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1661                 /* Use that stateid */
1662         } else if (state != NULL) {
1663                 nfs4_copy_stateid(&arg.stateid, state, current->files);
1664         } else
1665                 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1666 
1667         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1668         if (status == 0 && state != NULL)
1669                 renew_lease(server, timestamp);
1670         return status;
1671 }
1672 
1673 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1674                            struct nfs_fattr *fattr, struct iattr *sattr,
1675                            struct nfs4_state *state)
1676 {
1677         struct nfs_server *server = NFS_SERVER(inode);
1678         struct nfs4_exception exception = { };
1679         int err;
1680         do {
1681                 err = nfs4_handle_exception(server,
1682                                 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1683                                 &exception);
1684         } while (exception.retry);
1685         return err;
1686 }
1687 
1688 struct nfs4_closedata {
1689         struct path path;
1690         struct inode *inode;
1691         struct nfs4_state *state;
1692         struct nfs_closeargs arg;
1693         struct nfs_closeres res;
1694         struct nfs_fattr fattr;
1695         unsigned long timestamp;
1696 };
1697 
1698 static void nfs4_free_closedata(void *data)
1699 {
1700         struct nfs4_closedata *calldata = data;
1701         struct nfs4_state_owner *sp = calldata->state->owner;
1702 
1703         nfs4_put_open_state(calldata->state);
1704         nfs_free_seqid(calldata->arg.seqid);
1705         nfs4_put_state_owner(sp);
1706         path_put(&calldata->path);
1707         kfree(calldata);
1708 }
1709 
1710 static void nfs4_close_done(struct rpc_task *task, void *data)
1711 {
1712         struct nfs4_closedata *calldata = data;
1713         struct nfs4_state *state = calldata->state;
1714         struct nfs_server *server = NFS_SERVER(calldata->inode);
1715 
1716         nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1717         if (RPC_ASSASSINATED(task))
1718                 return;
1719         /* hmm. we are done with the inode, and in the process of freeing
1720          * the state_owner. we keep this around to process errors
1721          */
1722         switch (task->tk_status) {
1723                 case 0:
1724                         nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1725                         renew_lease(server, calldata->timestamp);
1726                         break;
1727                 case -NFS4ERR_STALE_STATEID:
1728                 case -NFS4ERR_OLD_STATEID:
1729                 case -NFS4ERR_BAD_STATEID:
1730                 case -NFS4ERR_EXPIRED:
1731                         if (calldata->arg.fmode == 0)
1732                                 break;
1733                 default:
1734                         if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1735                                 nfs4_restart_rpc(task, server->nfs_client);
1736                                 return;
1737                         }
1738         }
1739         nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1740         nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1741 }
1742 
1743 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1744 {
1745         struct nfs4_closedata *calldata = data;
1746         struct nfs4_state *state = calldata->state;
1747         int clear_rd, clear_wr, clear_rdwr;
1748 
1749         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1750                 return;
1751 
1752         clear_rd = clear_wr = clear_rdwr = 0;
1753         spin_lock(&state->owner->so_lock);
1754         /* Calculate the change in open mode */
1755         if (state->n_rdwr == 0) {
1756                 if (state->n_rdonly == 0) {
1757                         clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1758                         clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1759                 }
1760                 if (state->n_wronly == 0) {
1761                         clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1762                         clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1763                 }
1764         }
1765         spin_unlock(&state->owner->so_lock);
1766         if (!clear_rd && !clear_wr && !clear_rdwr) {
1767                 /* Note: exit _without_ calling nfs4_close_done */
1768                 task->tk_action = NULL;
1769                 return;
1770         }
1771         nfs_fattr_init(calldata->res.fattr);
1772         if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1773                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1774                 calldata->arg.fmode = FMODE_READ;
1775         } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1776                 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1777                 calldata->arg.fmode = FMODE_WRITE;
1778         }
1779         calldata->timestamp = jiffies;
1780         if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1781                                 &calldata->arg.seq_args, &calldata->res.seq_res,
1782                                 1, task))
1783                 return;
1784         rpc_call_start(task);
1785 }
1786 
1787 static const struct rpc_call_ops nfs4_close_ops = {
1788         .rpc_call_prepare = nfs4_close_prepare,
1789         .rpc_call_done = nfs4_close_done,
1790         .rpc_release = nfs4_free_closedata,
1791 };
1792 
1793 /* 
1794  * It is possible for data to be read/written from a mem-mapped file 
1795  * after the sys_close call (which hits the vfs layer as a flush).
1796  * This means that we can't safely call nfsv4 close on a file until 
1797  * the inode is cleared. This in turn means that we are not good
1798  * NFSv4 citizens - we do not indicate to the server to update the file's 
1799  * share state even when we are done with one of the three share 
1800  * stateid's in the inode.
1801  *
1802  * NOTE: Caller must be holding the sp->so_owner semaphore!
1803  */
1804 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1805 {
1806         struct nfs_server *server = NFS_SERVER(state->inode);
1807         struct nfs4_closedata *calldata;
1808         struct nfs4_state_owner *sp = state->owner;
1809         struct rpc_task *task;
1810         struct rpc_message msg = {
1811                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1812                 .rpc_cred = state->owner->so_cred,
1813         };
1814         struct rpc_task_setup task_setup_data = {
1815                 .rpc_client = server->client,
1816                 .rpc_message = &msg,
1817                 .callback_ops = &nfs4_close_ops,
1818                 .workqueue = nfsiod_workqueue,
1819                 .flags = RPC_TASK_ASYNC,
1820         };
1821         int status = -ENOMEM;
1822 
1823         calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1824         if (calldata == NULL)
1825                 goto out;
1826         calldata->inode = state->inode;
1827         calldata->state = state;
1828         calldata->arg.fh = NFS_FH(state->inode);
1829         calldata->arg.stateid = &state->open_stateid;
1830         if (nfs4_has_session(server->nfs_client))
1831                 memset(calldata->arg.stateid->data, 0, 4);    /* clear seqid */
1832         /* Serialization for the sequence id */
1833         calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1834         if (calldata->arg.seqid == NULL)
1835                 goto out_free_calldata;
1836         calldata->arg.fmode = 0;
1837         calldata->arg.bitmask = server->cache_consistency_bitmask;
1838         calldata->res.fattr = &calldata->fattr;
1839         calldata->res.seqid = calldata->arg.seqid;
1840         calldata->res.server = server;
1841         calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1842         calldata->path.mnt = mntget(path->mnt);
1843         calldata->path.dentry = dget(path->dentry);
1844 
1845         msg.rpc_argp = &calldata->arg,
1846         msg.rpc_resp = &calldata->res,
1847         task_setup_data.callback_data = calldata;
1848         task = rpc_run_task(&task_setup_data);
1849         if (IS_ERR(task))
1850                 return PTR_ERR(task);
1851         status = 0;
1852         if (wait)
1853                 status = rpc_wait_for_completion_task(task);
1854         rpc_put_task(task);
1855         return status;
1856 out_free_calldata:
1857         kfree(calldata);
1858 out:
1859         nfs4_put_open_state(state);
1860         nfs4_put_state_owner(sp);
1861         return status;
1862 }
1863 
1864 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1865 {
1866         struct file *filp;
1867         int ret;
1868 
1869         /* If the open_intent is for execute, we have an extra check to make */
1870         if (fmode & FMODE_EXEC) {
1871                 ret = nfs_may_open(state->inode,
1872                                 state->owner->so_cred,
1873                                 nd->intent.open.flags);
1874                 if (ret < 0)
1875                         goto out_close;
1876         }
1877         filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1878         if (!IS_ERR(filp)) {
1879                 struct nfs_open_context *ctx;
1880                 ctx = nfs_file_open_context(filp);
1881                 ctx->state = state;
1882                 return 0;
1883         }
1884         ret = PTR_ERR(filp);
1885 out_close:
1886         nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1887         return ret;
1888 }
1889 
1890 struct dentry *
1891 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1892 {
1893         struct path path = {
1894                 .mnt = nd->path.mnt,
1895                 .dentry = dentry,
1896         };
1897         struct dentry *parent;
1898         struct iattr attr;
1899         struct rpc_cred *cred;
1900         struct nfs4_state *state;
1901         struct dentry *res;
1902         fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1903 
1904         if (nd->flags & LOOKUP_CREATE) {
1905                 attr.ia_mode = nd->intent.open.create_mode;
1906                 attr.ia_valid = ATTR_MODE;
1907                 if (!IS_POSIXACL(dir))
1908                         attr.ia_mode &= ~current_umask();
1909         } else {
1910                 attr.ia_valid = 0;
1911                 BUG_ON(nd->intent.open.flags & O_CREAT);
1912         }
1913 
1914         cred = rpc_lookup_cred();
1915         if (IS_ERR(cred))
1916                 return (struct dentry *)cred;
1917         parent = dentry->d_parent;
1918         /* Protect against concurrent sillydeletes */
1919         nfs_block_sillyrename(parent);
1920         state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1921         put_rpccred(cred);
1922         if (IS_ERR(state)) {
1923                 if (PTR_ERR(state) == -ENOENT) {
1924                         d_add(dentry, NULL);
1925                         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1926                 }
1927                 nfs_unblock_sillyrename(parent);
1928                 return (struct dentry *)state;
1929         }
1930         res = d_add_unique(dentry, igrab(state->inode));
1931         if (res != NULL)
1932                 path.dentry = res;
1933         nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1934         nfs_unblock_sillyrename(parent);
1935         nfs4_intent_set_file(nd, &path, state, fmode);
1936         return res;
1937 }
1938 
1939 int
1940 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1941 {
1942         struct path path = {
1943                 .mnt = nd->path.mnt,
1944                 .dentry = dentry,
1945         };
1946         struct rpc_cred *cred;
1947         struct nfs4_state *state;
1948         fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1949 
1950         cred = rpc_lookup_cred();
1951         if (IS_ERR(cred))
1952                 return PTR_ERR(cred);
1953         state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1954         put_rpccred(cred);
1955         if (IS_ERR(state)) {
1956                 switch (PTR_ERR(state)) {
1957                         case -EPERM:
1958                         case -EACCES:
1959                         case -EDQUOT:
1960                         case -ENOSPC:
1961                         case -EROFS:
1962                                 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1963                                 return 1;
1964                         default:
1965                                 goto out_drop;
1966                 }
1967         }
1968         if (state->inode == dentry->d_inode) {
1969                 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1970                 nfs4_intent_set_file(nd, &path, state, fmode);
1971                 return 1;
1972         }
1973         nfs4_close_sync(&path, state, fmode);
1974 out_drop:
1975         d_drop(dentry);
1976         return 0;
1977 }
1978 
1979 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1980 {
1981         if (ctx->state == NULL)
1982                 return;
1983         if (is_sync)
1984                 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1985         else
1986                 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1987 }
1988 
1989 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1990 {
1991         struct nfs4_server_caps_arg args = {
1992                 .fhandle = fhandle,
1993         };
1994         struct nfs4_server_caps_res res = {};
1995         struct rpc_message msg = {
1996                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1997                 .rpc_argp = &args,
1998                 .rpc_resp = &res,
1999         };
2000         int status;
2001 
2002         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2003         if (status == 0) {
2004                 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2005                 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2006                         server->caps |= NFS_CAP_ACLS;
2007                 if (res.has_links != 0)
2008                         server->caps |= NFS_CAP_HARDLINKS;
2009                 if (res.has_symlinks != 0)
2010                         server->caps |= NFS_CAP_SYMLINKS;
2011                 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2012                 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2013                 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2014                 server->acl_bitmask = res.acl_bitmask;
2015         }
2016 
2017         return status;
2018 }
2019 
2020 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2021 {
2022         struct nfs4_exception exception = { };
2023         int err;
2024         do {
2025                 err = nfs4_handle_exception(server,
2026                                 _nfs4_server_capabilities(server, fhandle),
2027                                 &exception);
2028         } while (exception.retry);
2029         return err;
2030 }
2031 
2032 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2033                 struct nfs_fsinfo *info)
2034 {
2035         struct nfs4_lookup_root_arg args = {
2036                 .bitmask = nfs4_fattr_bitmap,
2037         };
2038         struct nfs4_lookup_res res = {
2039                 .server = server,
2040                 .fattr = info->fattr,
2041                 .fh = fhandle,
2042         };
2043         struct rpc_message msg = {
2044                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2045                 .rpc_argp = &args,
2046                 .rpc_resp = &res,
2047         };
2048 
2049         nfs_fattr_init(info->fattr);
2050         return nfs4_call_sync(server, &msg, &args, &res, 0);
2051 }
2052 
2053 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2054                 struct nfs_fsinfo *info)
2055 {
2056         struct nfs4_exception exception = { };
2057         int err;
2058         do {
2059                 err = nfs4_handle_exception(server,
2060                                 _nfs4_lookup_root(server, fhandle, info),
2061                                 &exception);
2062         } while (exception.retry);
2063         return err;
2064 }
2065 
2066 /*
2067  * get the file handle for the "/" directory on the server
2068  */
2069 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2070                               struct nfs_fsinfo *info)
2071 {
2072         int status;
2073 
2074         status = nfs4_lookup_root(server, fhandle, info);
2075         if (status == 0)
2076                 status = nfs4_server_capabilities(server, fhandle);
2077         if (status == 0)
2078                 status = nfs4_do_fsinfo(server, fhandle, info);
2079         return nfs4_map_errors(status);
2080 }
2081 
2082 /*
2083  * Get locations and (maybe) other attributes of a referral.
2084  * Note that we'll actually follow the referral later when
2085  * we detect fsid mismatch in inode revalidation
2086  */
2087 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2088 {
2089         int status = -ENOMEM;
2090         struct page *page = NULL;
2091         struct nfs4_fs_locations *locations = NULL;
2092 
2093         page = alloc_page(GFP_KERNEL);
2094         if (page == NULL)
2095                 goto out;
2096         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2097         if (locations == NULL)
2098                 goto out;
2099 
2100         status = nfs4_proc_fs_locations(dir, name, locations, page);
2101         if (status != 0)
2102                 goto out;
2103         /* Make sure server returned a different fsid for the referral */
2104         if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2105                 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2106                 status = -EIO;
2107                 goto out;
2108         }
2109 
2110         memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2111         fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2112         if (!fattr->mode)
2113                 fattr->mode = S_IFDIR;
2114         memset(fhandle, 0, sizeof(struct nfs_fh));
2115 out:
2116         if (page)
2117                 __free_page(page);
2118         if (locations)
2119                 kfree(locations);
2120         return status;
2121 }
2122 
2123 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2124 {
2125         struct nfs4_getattr_arg args = {
2126                 .fh = fhandle,
2127                 .bitmask = server->attr_bitmask,
2128         };
2129         struct nfs4_getattr_res res = {
2130                 .fattr = fattr,
2131                 .server = server,
2132         };
2133         struct rpc_message msg = {
2134                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2135                 .rpc_argp = &args,
2136                 .rpc_resp = &res,
2137         };
2138         
2139         nfs_fattr_init(fattr);
2140         return nfs4_call_sync(server, &msg, &args, &res, 0);
2141 }
2142 
2143 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2144 {
2145         struct nfs4_exception exception = { };
2146         int err;
2147         do {
2148                 err = nfs4_handle_exception(server,
2149                                 _nfs4_proc_getattr(server, fhandle, fattr),
2150                                 &exception);
2151         } while (exception.retry);
2152         return err;
2153 }
2154 
2155 /* 
2156  * The file is not closed if it is opened due to the a request to change
2157  * the size of the file. The open call will not be needed once the
2158  * VFS layer lookup-intents are implemented.
2159  *
2160  * Close is called when the inode is destroyed.
2161  * If we haven't opened the file for O_WRONLY, we
2162  * need to in the size_change case to obtain a stateid.
2163  *
2164  * Got race?
2165  * Because OPEN is always done by name in nfsv4, it is
2166  * possible that we opened a different file by the same
2167  * name.  We can recognize this race condition, but we
2168  * can't do anything about it besides returning an error.
2169  *
2170  * This will be fixed with VFS changes (lookup-intent).
2171  */
2172 static int
2173 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2174                   struct iattr *sattr)
2175 {
2176         struct inode *inode = dentry->d_inode;
2177         struct rpc_cred *cred = NULL;
2178         struct nfs4_state *state = NULL;
2179         int status;
2180 
2181         nfs_fattr_init(fattr);
2182         
2183         /* Search for an existing open(O_WRITE) file */
2184         if (sattr->ia_valid & ATTR_FILE) {
2185                 struct nfs_open_context *ctx;
2186 
2187                 ctx = nfs_file_open_context(sattr->ia_file);
2188                 if (ctx) {
2189                         cred = ctx->cred;
2190                         state = ctx->state;
2191                 }
2192         }
2193 
2194         status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2195         if (status == 0)
2196                 nfs_setattr_update_inode(inode, sattr);
2197         return status;
2198 }
2199 
2200 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2201                 const struct qstr *name, struct nfs_fh *fhandle,
2202                 struct nfs_fattr *fattr)
2203 {
2204         int                    status;
2205         struct nfs4_lookup_arg args = {
2206                 .bitmask = server->attr_bitmask,
2207                 .dir_fh = dirfh,
2208                 .name = name,
2209         };
2210         struct nfs4_lookup_res res = {
2211                 .server = server,
2212                 .fattr = fattr,
2213                 .fh = fhandle,
2214         };
2215         struct rpc_message msg = {
2216                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2217                 .rpc_argp = &args,
2218                 .rpc_resp = &res,
2219         };
2220 
2221         nfs_fattr_init(fattr);
2222 
2223         dprintk("NFS call  lookupfh %s\n", name->name);
2224         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2225         dprintk("NFS reply lookupfh: %d\n", status);
2226         return status;
2227 }
2228 
2229 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2230                               struct qstr *name, struct nfs_fh *fhandle,
2231                               struct nfs_fattr *fattr)
2232 {
2233         struct nfs4_exception exception = { };
2234         int err;
2235         do {
2236                 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2237                 /* FIXME: !!!! */
2238                 if (err == -NFS4ERR_MOVED) {
2239                         err = -EREMOTE;
2240                         break;
2241                 }
2242                 err = nfs4_handle_exception(server, err, &exception);
2243         } while (exception.retry);
2244         return err;
2245 }
2246 
2247 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2248                 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2249 {
2250         int status;
2251         
2252         dprintk("NFS call  lookup %s\n", name->name);
2253         status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2254         if (status == -NFS4ERR_MOVED)
2255                 status = nfs4_get_referral(dir, name, fattr, fhandle);
2256         dprintk("NFS reply lookup: %d\n", status);
2257         return status;
2258 }
2259 
2260 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2261 {
2262         struct nfs4_exception exception = { };
2263         int err;
2264         do {
2265                 err = nfs4_handle_exception(NFS_SERVER(dir),
2266                                 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2267                                 &exception);
2268         } while (exception.retry);
2269         return err;
2270 }
2271 
2272 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2273 {
2274         struct nfs_server *server = NFS_SERVER(inode);
2275         struct nfs_fattr fattr;
2276         struct nfs4_accessargs args = {
2277                 .fh = NFS_FH(inode),
2278                 .bitmask = server->attr_bitmask,
2279         };
2280         struct nfs4_accessres res = {
2281                 .server = server,
2282                 .fattr = &fattr,
2283         };
2284         struct rpc_message msg = {
2285                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2286                 .rpc_argp = &args,
2287                 .rpc_resp = &res,
2288                 .rpc_cred = entry->cred,
2289         };
2290         int mode = entry->mask;
2291         int status;
2292 
2293         /*
2294          * Determine which access bits we want to ask for...
2295          */
2296         if (mode & MAY_READ)
2297                 args.access |= NFS4_ACCESS_READ;
2298         if (S_ISDIR(inode->i_mode)) {
2299                 if (mode & MAY_WRITE)
2300                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2301                 if (mode & MAY_EXEC)
2302                         args.access |= NFS4_ACCESS_LOOKUP;
2303         } else {
2304                 if (mode & MAY_WRITE)
2305                         args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2306                 if (mode & MAY_EXEC)
2307                         args.access |= NFS4_ACCESS_EXECUTE;
2308         }
2309         nfs_fattr_init(&fattr);
2310         status = nfs4_call_sync(server, &msg, &args, &res, 0);
2311         if (!status) {
2312                 entry->mask = 0;
2313                 if (res.access & NFS4_ACCESS_READ)
2314                         entry->mask |= MAY_READ;
2315                 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2316                         entry->mask |= MAY_WRITE;
2317                 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2318                         entry->mask |= MAY_EXEC;
2319                 nfs_refresh_inode(inode, &fattr);
2320         }
2321         return status;
2322 }
2323 
2324 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2325 {
2326         struct nfs4_exception exception = { };
2327         int err;
2328         do {
2329                 err = nfs4_handle_exception(NFS_SERVER(inode),
2330                                 _nfs4_proc_access(inode, entry),
2331                                 &exception);
2332         } while (exception.retry);
2333         return err;
2334 }
2335 
2336 /*
2337  * TODO: For the time being, we don't try to get any attributes
2338  * along with any of the zero-copy operations READ, READDIR,
2339  * READLINK, WRITE.
2340  *
2341  * In the case of the first three, we want to put the GETATTR
2342  * after the read-type operation -- this is because it is hard
2343  * to predict the length of a GETATTR response in v4, and thus
2344  * align the READ data correctly.  This means that the GETATTR
2345  * may end up partially falling into the page cache, and we should
2346  * shift it into the 'tail' of the xdr_buf before processing.
2347  * To do this efficiently, we need to know the total length
2348  * of data received, which doesn't seem to be available outside
2349  * of the RPC layer.
2350  *
2351  * In the case of WRITE, we also want to put the GETATTR after
2352  * the operation -- in this case because we want to make sure
2353  * we get the post-operation mtime and size.  This means that
2354  * we can't use xdr_encode_pages() as written: we need a variant
2355  * of it which would leave room in the 'tail' iovec.
2356  *
2357  * Both of these changes to the XDR layer would in fact be quite
2358  * minor, but I decided to leave them for a subsequent patch.
2359  */
2360 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2361                 unsigned int pgbase, unsigned int pglen)
2362 {
2363         struct nfs4_readlink args = {
2364                 .fh       = NFS_FH(inode),
2365                 .pgbase   = pgbase,
2366                 .pglen    = pglen,
2367                 .pages    = &page,
2368         };
2369         struct nfs4_readlink_res res;
2370         struct rpc_message msg = {
2371                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2372                 .rpc_argp = &args,
2373                 .rpc_resp = &res,
2374         };
2375 
2376         return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2377 }
2378 
2379 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2380                 unsigned int pgbase, unsigned int pglen)
2381 {
2382         struct nfs4_exception exception = { };
2383         int err;
2384         do {
2385                 err = nfs4_handle_exception(NFS_SERVER(inode),
2386                                 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2387                                 &exception);
2388         } while (exception.retry);
2389         return err;
2390 }
2391 
2392 /*
2393  * Got race?
2394  * We will need to arrange for the VFS layer to provide an atomic open.
2395  * Until then, this create/open method is prone to inefficiency and race
2396  * conditions due to the lookup, create, and open VFS calls from sys_open()
2397  * placed on the wire.
2398  *
2399  * Given the above sorry state of affairs, I'm simply sending an OPEN.
2400  * The file will be opened again in the subsequent VFS open call
2401  * (nfs4_proc_file_open).
2402  *
2403  * The open for read will just hang around to be used by any process that
2404  * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2405  */
2406 
2407 static int
2408 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2409                  int flags, struct nameidata *nd)
2410 {
2411         struct path path = {
2412                 .mnt = nd->path.mnt,
2413                 .dentry = dentry,
2414         };
2415         struct nfs4_state *state;
2416         struct rpc_cred *cred;
2417         fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2418         int status = 0;
2419 
2420         cred = rpc_lookup_cred();
2421         if (IS_ERR(cred)) {
2422                 status = PTR_ERR(cred);
2423                 goto out;
2424         }
2425         state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2426         d_drop(dentry);
2427         if (IS_ERR(state)) {
2428                 status = PTR_ERR(state);
2429                 goto out_putcred;
2430         }
2431         d_add(dentry, igrab(state->inode));
2432         nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2433         if (flags & O_EXCL) {
2434                 struct nfs_fattr fattr;
2435                 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2436                 if (status == 0)
2437                         nfs_setattr_update_inode(state->inode, sattr);
2438                 nfs_post_op_update_inode(state->inode, &fattr);
2439         }
2440         if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2441                 status = nfs4_intent_set_file(nd, &path, state, fmode);
2442         else
2443                 nfs4_close_sync(&path, state, fmode);
2444 out_putcred:
2445         put_rpccred(cred);
2446 out:
2447         return status;
2448 }
2449 
2450 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2451 {
2452         struct nfs_server *server = NFS_SERVER(dir);
2453         struct nfs_removeargs args = {
2454                 .fh = NFS_FH(dir),
2455                 .name.len = name->len,
2456                 .name.name = name->name,
2457                 .bitmask = server->attr_bitmask,
2458         };
2459         struct nfs_removeres res = {
2460                 .server = server,
2461         };
2462         struct rpc_message msg = {
2463                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2464                 .rpc_argp = &args,
2465                 .rpc_resp = &res,
2466         };
2467         int                     status;
2468 
2469         nfs_fattr_init(&res.dir_attr);
2470         status = nfs4_call_sync(server, &msg, &args, &res, 1);
2471         if (status == 0) {
2472                 update_changeattr(dir, &res.cinfo);
2473                 nfs_post_op_update_inode(dir, &res.dir_attr);
2474         }
2475         return status;
2476 }
2477 
2478 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2479 {
2480         struct nfs4_exception exception = { };
2481         int err;
2482         do {
2483                 err = nfs4_handle_exception(NFS_SERVER(dir),
2484                                 _nfs4_proc_remove(dir, name),
2485                                 &exception);
2486         } while (exception.retry);
2487         return err;
2488 }
2489 
2490 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2491 {
2492         struct nfs_server *server = NFS_SERVER(dir);
2493         struct nfs_removeargs *args = msg->rpc_argp;
2494         struct nfs_removeres *res = msg->rpc_resp;
2495 
2496         args->bitmask = server->cache_consistency_bitmask;
2497         res->server = server;
2498         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2499 }
2500 
2501 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2502 {
2503         struct nfs_removeres *res = task->tk_msg.rpc_resp;
2504 
2505         nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2506         if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2507                 return 0;
2508         nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2509         update_changeattr(dir, &res->cinfo);
2510         nfs_post_op_update_inode(dir, &res->dir_attr);
2511         return 1;
2512 }
2513 
2514 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2515                 struct inode *new_dir, struct qstr *new_name)
2516 {
2517         struct nfs_server *server = NFS_SERVER(old_dir);
2518         struct nfs4_rename_arg arg = {
2519                 .old_dir = NFS_FH(old_dir),
2520                 .new_dir = NFS_FH(new_dir),
2521                 .old_name = old_name,
2522                 .new_name = new_name,
2523                 .bitmask = server->attr_bitmask,
2524         };
2525         struct nfs_fattr old_fattr, new_fattr;
2526         struct nfs4_rename_res res = {
2527                 .server = server,
2528                 .old_fattr = &old_fattr,
2529                 .new_fattr = &new_fattr,
2530         };
2531         struct rpc_message msg = {
2532                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2533                 .rpc_argp = &arg,
2534                 .rpc_resp = &res,
2535         };
2536         int                     status;
2537         
2538         nfs_fattr_init(res.old_fattr);
2539         nfs_fattr_init(res.new_fattr);
2540         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2541 
2542         if (!status) {
2543                 update_changeattr(old_dir, &res.old_cinfo);
2544                 nfs_post_op_update_inode(old_dir, res.old_fattr);
2545                 update_changeattr(new_dir, &res.new_cinfo);
2546                 nfs_post_op_update_inode(new_dir, res.new_fattr);
2547         }
2548         return status;
2549 }
2550 
2551 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2552                 struct inode *new_dir, struct qstr *new_name)
2553 {
2554         struct nfs4_exception exception = { };
2555         int err;
2556         do {
2557                 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2558                                 _nfs4_proc_rename(old_dir, old_name,
2559                                         new_dir, new_name),
2560                                 &exception);
2561         } while (exception.retry);
2562         return err;
2563 }
2564 
2565 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2566 {
2567         struct nfs_server *server = NFS_SERVER(inode);
2568         struct nfs4_link_arg arg = {
2569                 .fh     = NFS_FH(inode),
2570                 .dir_fh = NFS_FH(dir),
2571                 .name   = name,
2572                 .bitmask = server->attr_bitmask,
2573         };
2574         struct nfs_fattr fattr, dir_attr;
2575         struct nfs4_link_res res = {
2576                 .server = server,
2577                 .fattr = &fattr,
2578                 .dir_attr = &dir_attr,
2579         };
2580         struct rpc_message msg = {
2581                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2582                 .rpc_argp = &arg,
2583                 .rpc_resp = &res,
2584         };
2585         int                     status;
2586 
2587         nfs_fattr_init(res.fattr);
2588         nfs_fattr_init(res.dir_attr);
2589         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2590         if (!status) {
2591                 update_changeattr(dir, &res.cinfo);
2592                 nfs_post_op_update_inode(dir, res.dir_attr);
2593                 nfs_post_op_update_inode(inode, res.fattr);
2594         }
2595 
2596         return status;
2597 }
2598 
2599 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2600 {
2601         struct nfs4_exception exception = { };
2602         int err;
2603         do {
2604                 err = nfs4_handle_exception(NFS_SERVER(inode),
2605                                 _nfs4_proc_link(inode, dir, name),
2606                                 &exception);
2607         } while (exception.retry);
2608         return err;
2609 }
2610 
2611 struct nfs4_createdata {
2612         struct rpc_message msg;
2613         struct nfs4_create_arg arg;
2614         struct nfs4_create_res res;
2615         struct nfs_fh fh;
2616         struct nfs_fattr fattr;
2617         struct nfs_fattr dir_fattr;
2618 };
2619 
2620 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2621                 struct qstr *name, struct iattr *sattr, u32 ftype)
2622 {
2623         struct nfs4_createdata *data;
2624 
2625         data = kzalloc(sizeof(*data), GFP_KERNEL);
2626         if (data != NULL) {
2627                 struct nfs_server *server = NFS_SERVER(dir);
2628 
2629                 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2630                 data->msg.rpc_argp = &data->arg;
2631                 data->msg.rpc_resp = &data->res;
2632                 data->arg.dir_fh = NFS_FH(dir);
2633                 data->arg.server = server;
2634                 data->arg.name = name;
2635                 data->arg.attrs = sattr;
2636                 data->arg.ftype = ftype;
2637                 data->arg.bitmask = server->attr_bitmask;
2638                 data->res.server = server;
2639                 data->res.fh = &data->fh;
2640                 data->res.fattr = &data->fattr;
2641                 data->res.dir_fattr = &data->dir_fattr;
2642                 nfs_fattr_init(data->res.fattr);
2643                 nfs_fattr_init(data->res.dir_fattr);
2644         }
2645         return data;
2646 }
2647 
2648 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2649 {
2650         int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2651                                     &data->arg, &data->res, 1);
2652         if (status == 0) {
2653                 update_changeattr(dir, &data->res.dir_cinfo);
2654                 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2655                 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2656         }
2657         return status;
2658 }
2659 
2660 static void nfs4_free_createdata(struct nfs4_createdata *data)
2661 {
2662         kfree(data);
2663 }
2664 
2665 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2666                 struct page *page, unsigned int len, struct iattr *sattr)
2667 {
2668         struct nfs4_createdata *data;
2669         int status = -ENAMETOOLONG;
2670 
2671         if (len > NFS4_MAXPATHLEN)
2672                 goto out;
2673 
2674         status = -ENOMEM;
2675         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2676         if (data == NULL)
2677                 goto out;
2678 
2679         data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2680         data->arg.u.symlink.pages = &page;
2681         data->arg.u.symlink.len = len;
2682         
2683         status = nfs4_do_create(dir, dentry, data);
2684 
2685         nfs4_free_createdata(data);
2686 out:
2687         return status;
2688 }
2689 
2690 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2691                 struct page *page, unsigned int len, struct iattr *sattr)
2692 {
2693         struct nfs4_exception exception = { };
2694         int err;
2695         do {
2696                 err = nfs4_handle_exception(NFS_SERVER(dir),
2697                                 _nfs4_proc_symlink(dir, dentry, page,
2698                                                         len, sattr),
2699                                 &exception);
2700         } while (exception.retry);
2701         return err;
2702 }
2703 
2704 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2705                 struct iattr *sattr)
2706 {
2707         struct nfs4_createdata *data;
2708         int status = -ENOMEM;
2709 
2710         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2711         if (data == NULL)
2712                 goto out;
2713 
2714         status = nfs4_do_create(dir, dentry, data);
2715 
2716         nfs4_free_createdata(data);
2717 out:
2718         return status;
2719 }
2720 
2721 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2722                 struct iattr *sattr)
2723 {
2724         struct nfs4_exception exception = { };
2725         int err;
2726         do {
2727                 err = nfs4_handle_exception(NFS_SERVER(dir),
2728                                 _nfs4_proc_mkdir(dir, dentry, sattr),
2729                                 &exception);
2730         } while (exception.retry);
2731         return err;
2732 }
2733 
2734 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2735                   u64 cookie, struct page *page, unsigned int count, int plus)
2736 {
2737         struct inode            *dir = dentry->d_inode;
2738         struct nfs4_readdir_arg args = {
2739                 .fh = NFS_FH(dir),
2740                 .pages = &page,
2741                 .pgbase = 0,
2742                 .count = count,
2743                 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2744         };
2745         struct nfs4_readdir_res res;
2746         struct rpc_message msg = {
2747                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2748                 .rpc_argp = &args,
2749                 .rpc_resp = &res,
2750                 .rpc_cred = cred,
2751         };
2752         int                     status;
2753 
2754         dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2755                         dentry->d_parent->d_name.name,
2756                         dentry->d_name.name,
2757                         (unsigned long long)cookie);
2758         nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2759         res.pgbase = args.pgbase;
2760         status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2761         if (status == 0)
2762                 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2763 
2764         nfs_invalidate_atime(dir);
2765 
2766         dprintk("%s: returns %d\n", __func__, status);
2767         return status;
2768 }
2769 
2770 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2771                   u64 cookie, struct page *page, unsigned int count, int plus)
2772 {
2773         struct nfs4_exception exception = { };
2774         int err;
2775         do {
2776                 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2777                                 _nfs4_proc_readdir(dentry, cred, cookie,
2778                                         page, count, plus),
2779                                 &exception);
2780         } while (exception.retry);
2781         return err;
2782 }
2783 
2784 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2785                 struct iattr *sattr, dev_t rdev)
2786 {
2787         struct nfs4_createdata *data;
2788         int mode = sattr->ia_mode;
2789         int status = -ENOMEM;
2790 
2791         BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2792         BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2793 
2794         data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2795         if (data == NULL)
2796                 goto out;
2797 
2798         if (S_ISFIFO(mode))
2799                 data->arg.ftype = NF4FIFO;
2800         else if (S_ISBLK(mode)) {
2801                 data->arg.ftype = NF4BLK;
2802                 data->arg.u.device.specdata1 = MAJOR(rdev);
2803                 data->arg.u.device.specdata2 = MINOR(rdev);
2804         }
2805         else if (S_ISCHR(mode)) {
2806                 data->arg.ftype = NF4CHR;
2807                 data->arg.u.device.specdata1 = MAJOR(rdev);
2808                 data->arg.u.device.specdata2 = MINOR(rdev);
2809         }
2810         
2811         status = nfs4_do_create(dir, dentry, data);
2812 
2813         nfs4_free_createdata(data);
2814 out:
2815         return status;
2816 }
2817 
2818 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2819                 struct iattr *sattr, dev_t rdev)
2820 {
2821         struct nfs4_exception exception = { };
2822         int err;
2823         do {
2824                 err = nfs4_handle_exception(NFS_SERVER(dir),
2825                                 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2826                                 &exception);
2827         } while (exception.retry);
2828         return err;
2829 }
2830 
2831 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2832                  struct nfs_fsstat *fsstat)
2833 {
2834         struct nfs4_statfs_arg args = {
2835                 .fh = fhandle,
2836                 .bitmask = server->attr_bitmask,
2837         };
2838         struct nfs4_statfs_res res = {
2839                 .fsstat = fsstat,
2840         };
2841         struct rpc_message msg = {
2842                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2843                 .rpc_argp = &args,
2844                 .rpc_resp = &res,
2845         };
2846 
2847         nfs_fattr_init(fsstat->fattr);
2848         return  nfs4_call_sync(server, &msg, &args, &res, 0);
2849 }
2850 
2851 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2852 {
2853         struct nfs4_exception exception = { };
2854         int err;
2855         do {
2856                 err = nfs4_handle_exception(server,
2857                                 _nfs4_proc_statfs(server, fhandle, fsstat),
2858                                 &exception);
2859         } while (exception.retry);
2860         return err;
2861 }
2862 
2863 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2864                 struct nfs_fsinfo *fsinfo)
2865 {
2866         struct nfs4_fsinfo_arg args = {
2867                 .fh = fhandle,
2868                 .bitmask = server->attr_bitmask,
2869         };
2870         struct nfs4_fsinfo_res res = {
2871                 .fsinfo = fsinfo,
2872         };
2873         struct rpc_message msg = {
2874                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2875                 .rpc_argp = &args,
2876                 .rpc_resp = &res,
2877         };
2878 
2879         return nfs4_call_sync(server, &msg, &args, &res, 0);
2880 }
2881 
2882 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2883 {
2884         struct nfs4_exception exception = { };
2885         int err;
2886 
2887         do {
2888                 err = nfs4_handle_exception(server,
2889                                 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2890                                 &exception);
2891         } while (exception.retry);
2892         return err;
2893 }
2894 
2895 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2896 {
2897         nfs_fattr_init(fsinfo->fattr);
2898         return nfs4_do_fsinfo(server, fhandle, fsinfo);
2899 }
2900 
2901 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2902                 struct nfs_pathconf *pathconf)
2903 {
2904         struct nfs4_pathconf_arg args = {
2905                 .fh = fhandle,
2906                 .bitmask = server->attr_bitmask,
2907         };
2908         struct nfs4_pathconf_res res = {
2909                 .pathconf = pathconf,
2910         };
2911         struct rpc_message msg = {
2912                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2913                 .rpc_argp = &args,
2914                 .rpc_resp = &res,
2915         };
2916 
2917         /* None of the pathconf attributes are mandatory to implement */
2918         if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2919                 memset(pathconf, 0, sizeof(*pathconf));
2920                 return 0;
2921         }
2922 
2923         nfs_fattr_init(pathconf->fattr);
2924         return nfs4_call_sync(server, &msg, &args, &res, 0);
2925 }
2926 
2927 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2928                 struct nfs_pathconf *pathconf)
2929 {
2930         struct nfs4_exception exception = { };
2931         int err;
2932 
2933         do {
2934                 err = nfs4_handle_exception(server,
2935                                 _nfs4_proc_pathconf(server, fhandle, pathconf),
2936                                 &exception);
2937         } while (exception.retry);
2938         return err;
2939 }
2940 
2941 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2942 {
2943         struct nfs_server *server = NFS_SERVER(data->inode);
2944 
2945         dprintk("--> %s\n", __func__);
2946 
2947         /* nfs4_sequence_free_slot called in the read rpc_call_done */
2948         nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2949 
2950         if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2951                 nfs4_restart_rpc(task, server->nfs_client);
2952                 return -EAGAIN;
2953         }
2954 
2955         nfs_invalidate_atime(data->inode);
2956         if (task->tk_status > 0)
2957                 renew_lease(server, data->timestamp);
2958         return 0;
2959 }
2960 
2961 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2962 {
2963         data->timestamp   = jiffies;
2964         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2965 }
2966 
2967 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2968 {
2969         struct inode *inode = data->inode;
2970         
2971         /* slot is freed in nfs_writeback_done */
2972         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2973                            task->tk_status);
2974 
2975         if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2976                 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
2977                 return -EAGAIN;
2978         }
2979         if (task->tk_status >= 0) {
2980                 renew_lease(NFS_SERVER(inode), data->timestamp);
2981                 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2982         }
2983         return 0;
2984 }
2985 
2986 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2987 {
2988         struct nfs_server *server = NFS_SERVER(data->inode);
2989 
2990         data->args.bitmask = server->cache_consistency_bitmask;
2991         data->res.server = server;
2992         data->timestamp   = jiffies;
2993 
2994         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2995 }
2996 
2997 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2998 {
2999         struct inode *inode = data->inode;
3000         
3001         nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3002                            task->tk_status);
3003         if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3004                 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3005                 return -EAGAIN;
3006         }
3007         nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3008                                 &data->res.seq_res);
3009         nfs_refresh_inode(inode, data->res.fattr);
3010         return 0;
3011 }
3012 
3013 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3014 {
3015         struct nfs_server *server = NFS_SERVER(data->inode);
3016         
3017         data->args.bitmask = server->cache_consistency_bitmask;
3018         data->res.server = server;
3019         msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3020 }
3021 
3022 /*
3023  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3024  * standalone procedure for queueing an asynchronous RENEW.
3025  */
3026 static void nfs4_renew_done(struct rpc_task *task, void *data)
3027 {
3028         struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
3029         unsigned long timestamp = (unsigned long)data;
3030 
3031         if (task->tk_status < 0) {
3032                 /* Unless we're shutting down, schedule state recovery! */
3033                 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3034                         nfs4_schedule_state_recovery(clp);
3035                 return;
3036         }
3037         spin_lock(&clp->cl_lock);
3038         if (time_before(clp->cl_last_renewal,timestamp))
3039                 clp->cl_last_renewal = timestamp;
3040         spin_unlock(&clp->cl_lock);
3041 }
3042 
3043 static const struct rpc_call_ops nfs4_renew_ops = {
3044         .rpc_call_done = nfs4_renew_done,
3045 };
3046 
3047 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3048 {
3049         struct rpc_message msg = {
3050                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3051                 .rpc_argp       = clp,
3052                 .rpc_cred       = cred,
3053         };
3054 
3055         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3056                         &nfs4_renew_ops, (void *)jiffies);
3057 }
3058 
3059 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3060 {
3061         struct rpc_message msg = {
3062                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3063                 .rpc_argp       = clp,
3064                 .rpc_cred       = cred,
3065         };
3066         unsigned long now = jiffies;
3067         int status;
3068 
3069         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3070         if (status < 0)
3071                 return status;
3072         spin_lock(&clp->cl_lock);
3073         if (time_before(clp->cl_last_renewal,now))
3074                 clp->cl_last_renewal = now;
3075         spin_unlock(&clp->cl_lock);
3076         return 0;
3077 }
3078 
3079 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3080 {
3081         return (server->caps & NFS_CAP_ACLS)
3082                 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3083                 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3084 }
3085 
3086 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3087  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3088  * the stack.
3089  */
3090 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3091 
3092 static void buf_to_pages(const void *buf, size_t buflen,
3093                 struct page **pages, unsigned int *pgbase)
3094 {
3095         const void *p = buf;
3096 
3097         *pgbase = offset_in_page(buf);
3098         p -= *pgbase;
3099         while (p < buf + buflen) {
3100                 *(pages++) = virt_to_page(p);
3101                 p += PAGE_CACHE_SIZE;
3102         }
3103 }
3104 
3105 struct nfs4_cached_acl {
3106         int cached;
3107         size_t len;
3108         char data[0];
3109 };
3110 
3111 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3112 {
3113         struct nfs_inode *nfsi = NFS_I(inode);
3114 
3115         spin_lock(&inode->i_lock);
3116         kfree(nfsi->nfs4_acl);
3117         nfsi->nfs4_acl = acl;
3118         spin_unlock(&inode->i_lock);
3119 }
3120 
3121 static void nfs4_zap_acl_attr(struct inode *inode)
3122 {
3123         nfs4_set_cached_acl(inode, NULL);
3124 }
3125 
3126 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3127 {
3128         struct nfs_inode *nfsi = NFS_I(inode);
3129         struct nfs4_cached_acl *acl;
3130         int ret = -ENOENT;
3131 
3132         spin_lock(&inode->i_lock);
3133         acl = nfsi->nfs4_acl;
3134         if (acl == NULL)
3135                 goto out;
3136         if (buf == NULL) /* user is just asking for length */
3137                 goto out_len;
3138         if (acl->cached == 0)
3139                 goto out;
3140         ret = -ERANGE; /* see getxattr(2) man page */
3141         if (acl->len > buflen)
3142                 goto out;
3143         memcpy(buf, acl->data, acl->len);
3144 out_len:
3145         ret = acl->len;
3146 out:
3147         spin_unlock(&inode->i_lock);
3148         return ret;
3149 }
3150 
3151 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3152 {
3153         struct nfs4_cached_acl *acl;
3154 
3155         if (buf && acl_len <= PAGE_SIZE) {
3156                 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3157                 if (acl == NULL)
3158                         goto out;
3159                 acl->cached = 1;
3160                 memcpy(acl->data, buf, acl_len);
3161         } else {
3162                 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3163                 if (acl == NULL)
3164                         goto out;
3165                 acl->cached = 0;
3166         }
3167         acl->len = acl_len;
3168 out:
3169         nfs4_set_cached_acl(inode, acl);
3170 }
3171 
3172 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3173 {
3174         struct page *pages[NFS4ACL_MAXPAGES];
3175         struct nfs_getaclargs args = {
3176                 .fh = NFS_FH(inode),
3177                 .acl_pages = pages,
3178                 .acl_len = buflen,
3179         };
3180         struct nfs_getaclres res = {
3181                 .acl_len = buflen,
3182         };
3183         void *resp_buf;
3184         struct rpc_message msg = {
3185                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3186                 .rpc_argp = &args,
3187                 .rpc_resp = &res,
3188         };
3189         struct page *localpage = NULL;
3190         int ret;
3191 
3192         if (buflen < PAGE_SIZE) {
3193                 /* As long as we're doing a round trip to the server anyway,
3194                  * let's be prepared for a page of acl data. */
3195                 localpage = alloc_page(GFP_KERNEL);
3196                 resp_buf = page_address(localpage);
3197                 if (localpage == NULL)
3198                         return -ENOMEM;
3199                 args.acl_pages[0] = localpage;
3200                 args.acl_pgbase = 0;
3201                 args.acl_len = PAGE_SIZE;
3202         } else {
3203                 resp_buf = buf;
3204                 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3205         }
3206         ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3207         if (ret)
3208                 goto out_free;
3209         if (res.acl_len > args.acl_len)
3210                 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3211         else
3212                 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3213         if (buf) {
3214                 ret = -ERANGE;
3215                 if (res.acl_len > buflen)
3216                         goto out_free;
3217                 if (localpage)
3218                         memcpy(buf, resp_buf, res.acl_len);
3219         }
3220         ret = res.acl_len;
3221 out_free:
3222         if (localpage)
3223                 __free_page(localpage);
3224         return ret;
3225 }
3226 
3227 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3228 {
3229         struct nfs4_exception exception = { };
3230         ssize_t ret;
3231         do {
3232                 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3233                 if (ret >= 0)
3234                         break;
3235                 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3236         } while (exception.retry);
3237         return ret;
3238 }
3239 
3240 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3241 {
3242         struct nfs_server *server = NFS_SERVER(inode);
3243         int ret;
3244 
3245         if (!nfs4_server_supports_acls(server))
3246                 return -EOPNOTSUPP;
3247         ret = nfs_revalidate_inode(server, inode);
3248         if (ret < 0)
3249                 return ret;
3250         ret = nfs4_read_cached_acl(inode, buf, buflen);
3251         if (ret != -ENOENT)
3252                 return ret;
3253         return nfs4_get_acl_uncached(inode, buf, buflen);
3254 }
3255 
3256 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3257 {
3258         struct nfs_server *server = NFS_SERVER(inode);
3259         struct page *pages[NFS4ACL_MAXPAGES];
3260         struct nfs_setaclargs arg = {
3261                 .fh             = NFS_FH(inode),
3262                 .acl_pages      = pages,
3263                 .acl_len        = buflen,
3264         };
3265         struct nfs_setaclres res;
3266         struct rpc_message msg = {
3267                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3268                 .rpc_argp       = &arg,
3269                 .rpc_resp       = &res,
3270         };
3271         int ret;
3272 
3273         if (!nfs4_server_supports_acls(server))
3274                 return -EOPNOTSUPP;
3275         nfs_inode_return_delegation(inode);
3276         buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3277         ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3278         nfs_access_zap_cache(inode);
3279         nfs_zap_acl_cache(inode);
3280         return ret;
3281 }
3282 
3283 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3284 {
3285         struct nfs4_exception exception = { };
3286         int err;
3287         do {
3288                 err = nfs4_handle_exception(NFS_SERVER(inode),
3289                                 __nfs4_proc_set_acl(inode, buf, buflen),
3290                                 &exception);
3291         } while (exception.retry);
3292         return err;
3293 }
3294 
3295 static int
3296 _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
3297 {
3298         if (!clp || task->tk_status >= 0)
3299                 return 0;
3300         switch(task->tk_status) {
3301                 case -NFS4ERR_ADMIN_REVOKED:
3302                 case -NFS4ERR_BAD_STATEID:
3303                 case -NFS4ERR_OPENMODE:
3304                         if (state == NULL)
3305                                 break;
3306                         nfs4_state_mark_reclaim_nograce(clp, state);
3307                 case -NFS4ERR_STALE_CLIENTID:
3308                 case -NFS4ERR_STALE_STATEID:
3309                 case -NFS4ERR_EXPIRED:
3310                         rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3311                         nfs4_schedule_state_recovery(clp);
3312                         if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3313                                 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3314                         task->tk_status = 0;
3315                         return -EAGAIN;
3316 #if defined(CONFIG_NFS_V4_1)
3317                 case -NFS4ERR_BADSESSION:
3318                 case -NFS4ERR_BADSLOT:
3319                 case -NFS4ERR_BAD_HIGH_SLOT:
3320                 case -NFS4ERR_DEADSESSION:
3321                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3322                 case -NFS4ERR_SEQ_FALSE_RETRY:
3323                 case -NFS4ERR_SEQ_MISORDERED:
3324                         dprintk("%s ERROR %d, Reset session\n", __func__,
3325                                 task->tk_status);
3326                         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
3327                         task->tk_status = 0;
3328                         return -EAGAIN;
3329 #endif /* CONFIG_NFS_V4_1 */
3330                 case -NFS4ERR_DELAY:
3331                         if (server)
3332                                 nfs_inc_server_stats(server, NFSIOS_DELAY);
3333                 case -NFS4ERR_GRACE:
3334                         rpc_delay(task, NFS4_POLL_RETRY_MAX);
3335                         task->tk_status = 0;
3336                         return -EAGAIN;
3337                 case -NFS4ERR_OLD_STATEID:
3338                         task->tk_status = 0;
3339                         return -EAGAIN;
3340         }
3341         task->tk_status = nfs4_map_errors(task->tk_status);
3342         return 0;
3343 }
3344 
3345 static int
3346 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3347 {
3348         return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3349 }
3350 
3351 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3352 {
3353         nfs4_verifier sc_verifier;
3354         struct nfs4_setclientid setclientid = {
3355                 .sc_verifier = &sc_verifier,
3356                 .sc_prog = program,
3357         };
3358         struct rpc_message msg = {
3359                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3360                 .rpc_argp = &setclientid,
3361                 .rpc_resp = clp,
3362                 .rpc_cred = cred,
3363         };
3364         __be32 *p;
3365         int loop = 0;
3366         int status;
3367 
3368         p = (__be32*)sc_verifier.data;
3369         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3370         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3371 
3372         for(;;) {
3373                 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3374                                 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3375                                 clp->cl_ipaddr,
3376                                 rpc_peeraddr2str(clp->cl_rpcclient,
3377                                                         RPC_DISPLAY_ADDR),
3378                                 rpc_peeraddr2str(clp->cl_rpcclient,
3379                                                         RPC_DISPLAY_PROTO),
3380                                 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3381                                 clp->cl_id_uniquifier);
3382                 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3383                                 sizeof(setclientid.sc_netid),
3384                                 rpc_peeraddr2str(clp->cl_rpcclient,
3385                                                         RPC_DISPLAY_NETID));
3386                 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3387                                 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3388                                 clp->cl_ipaddr, port >> 8, port & 255);
3389 
3390                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3391                 if (status != -NFS4ERR_CLID_INUSE)
3392                         break;
3393                 if (signalled())
3394                         break;
3395                 if (loop++ & 1)
3396                         ssleep(clp->cl_lease_time + 1);
3397                 else
3398                         if (++clp->cl_id_uniquifier == 0)
3399                                 break;
3400         }
3401         return status;
3402 }
3403 
3404 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3405 {
3406         struct nfs_fsinfo fsinfo;
3407         struct rpc_message msg = {
3408                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3409                 .rpc_argp = clp,
3410                 .rpc_resp = &fsinfo,
3411                 .rpc_cred = cred,
3412         };
3413         unsigned long now;
3414         int status;
3415 
3416         now = jiffies;
3417         status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3418         if (status == 0) {
3419                 spin_lock(&clp->cl_lock);
3420                 clp->cl_lease_time = fsinfo.lease_time * HZ;
3421                 clp->cl_last_renewal = now;
3422                 spin_unlock(&clp->cl_lock);
3423         }
3424         return status;
3425 }
3426 
3427 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3428 {
3429         long timeout = 0;
3430         int err;
3431         do {
3432                 err = _nfs4_proc_setclientid_confirm(clp, cred);
3433                 switch (err) {
3434                         case 0:
3435                                 return err;
3436                         case -NFS4ERR_RESOURCE:
3437                                 /* The IBM lawyers misread another document! */
3438                         case -NFS4ERR_DELAY:
3439                                 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3440                 }
3441         } while (err == 0);
3442         return err;
3443 }
3444 
3445 struct nfs4_delegreturndata {
3446         struct nfs4_delegreturnargs args;
3447         struct nfs4_delegreturnres res;
3448         struct nfs_fh fh;
3449         nfs4_stateid stateid;
3450         unsigned long timestamp;
3451         struct nfs_fattr fattr;
3452         int rpc_status;
3453 };
3454 
3455 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3456 {
3457         struct nfs4_delegreturndata *data = calldata;
3458 
3459         nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3460                                      task->tk_status);
3461 
3462         data->rpc_status = task->tk_status;
3463         if (data->rpc_status == 0)
3464                 renew_lease(data->res.server, data->timestamp);
3465 }
3466 
3467 static void nfs4_delegreturn_release(void *calldata)
3468 {
3469         kfree(calldata);
3470 }
3471 
3472 #if defined(CONFIG_NFS_V4_1)
3473 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3474 {
3475         struct nfs4_delegreturndata *d_data;
3476 
3477         d_data = (struct nfs4_delegreturndata *)data;
3478 
3479         if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3480                                 &d_data->args.seq_args,
3481                                 &d_data->res.seq_res, 1, task))
3482                 return;
3483         rpc_call_start(task);
3484 }
3485 #endif /* CONFIG_NFS_V4_1 */
3486 
3487 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3488 #if defined(CONFIG_NFS_V4_1)
3489         .rpc_call_prepare = nfs4_delegreturn_prepare,
3490 #endif /* CONFIG_NFS_V4_1 */
3491         .rpc_call_done = nfs4_delegreturn_done,
3492         .rpc_release = nfs4_delegreturn_release,
3493 };
3494 
3495 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3496 {
3497         struct nfs4_delegreturndata *data;
3498         struct nfs_server *server = NFS_SERVER(inode);
3499         struct rpc_task *task;
3500         struct rpc_message msg = {
3501                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3502                 .rpc_cred = cred,
3503         };
3504         struct rpc_task_setup task_setup_data = {
3505                 .rpc_client = server->client,
3506                 .rpc_message = &msg,
3507                 .callback_ops = &nfs4_delegreturn_ops,
3508                 .flags = RPC_TASK_ASYNC,
3509         };
3510         int status = 0;
3511 
3512         data = kzalloc(sizeof(*data), GFP_KERNEL);
3513         if (data == NULL)
3514                 return -ENOMEM;
3515         data->args.fhandle = &data->fh;
3516         data->args.stateid = &data->stateid;
3517         data->args.bitmask = server->attr_bitmask;
3518         nfs_copy_fh(&data->fh, NFS_FH(inode));
3519         memcpy(&data->stateid, stateid, sizeof(data->stateid));
3520         data->res.fattr = &data->fattr;
3521         data->res.server = server;
3522         data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3523         nfs_fattr_init(data->res.fattr);
3524         data->timestamp = jiffies;
3525         data->rpc_status = 0;
3526 
3527         task_setup_data.callback_data = data;
3528         msg.rpc_argp = &data->args,
3529         msg.rpc_resp = &data->res,
3530         task = rpc_run_task(&task_setup_data);
3531         if (IS_ERR(task))
3532                 return PTR_ERR(task);
3533         if (!issync)
3534                 goto out;
3535         status = nfs4_wait_for_completion_rpc_task(task);
3536         if (status != 0)
3537                 goto out;
3538         status = data->rpc_status;
3539         if (status != 0)
3540                 goto out;
3541         nfs_refresh_inode(inode, &data->fattr);
3542 out:
3543         rpc_put_task(task);
3544         return status;
3545 }
3546 
3547 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3548 {
3549         struct nfs_server *server = NFS_SERVER(inode);
3550         struct nfs4_exception exception = { };
3551         int err;
3552         do {
3553                 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3554                 switch (err) {
3555                         case -NFS4ERR_STALE_STATEID:
3556                         case -NFS4ERR_EXPIRED:
3557                         case 0:
3558                                 return 0;
3559                 }
3560                 err = nfs4_handle_exception(server, err, &exception);
3561         } while (exception.retry);
3562         return err;
3563 }
3564 
3565 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3566 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3567 
3568 /* 
3569  * sleep, with exponential backoff, and retry the LOCK operation. 
3570  */
3571 static unsigned long
3572 nfs4_set_lock_task_retry(unsigned long timeout)
3573 {
3574         schedule_timeout_killable(timeout);
3575         timeout <<= 1;
3576         if (timeout > NFS4_LOCK_MAXTIMEOUT)
3577                 return NFS4_LOCK_MAXTIMEOUT;
3578         return timeout;
3579 }
3580 
3581 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3582 {
3583         struct inode *inode = state->inode;
3584         struct nfs_server *server = NFS_SERVER(inode);
3585         struct nfs_client *clp = server->nfs_client;
3586         struct nfs_lockt_args arg = {
3587                 .fh = NFS_FH(inode),
3588                 .fl = request,
3589         };
3590         struct nfs_lockt_res res = {
3591                 .denied = request,
3592         };
3593         struct rpc_message msg = {
3594                 .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3595                 .rpc_argp       = &arg,
3596                 .rpc_resp       = &res,
3597                 .rpc_cred       = state->owner->so_cred,
3598         };
3599         struct nfs4_lock_state *lsp;
3600         int status;
3601 
3602         arg.lock_owner.clientid = clp->cl_clientid;
3603         status = nfs4_set_lock_state(state, request);
3604         if (status != 0)
3605                 goto out;
3606         lsp = request->fl_u.nfs4_fl.owner;
3607         arg.lock_owner.id = lsp->ls_id.id;
3608         status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3609         switch (status) {
3610                 case 0:
3611                         request->fl_type = F_UNLCK;
3612                         break;
3613                 case -NFS4ERR_DENIED:
3614                         status = 0;
3615         }
3616         request->fl_ops->fl_release_private(request);
3617 out:
3618         return status;
3619 }
3620 
3621 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3622 {
3623         struct nfs4_exception exception = { };
3624         int err;
3625 
3626         do {
3627                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3628                                 _nfs4_proc_getlk(state, cmd, request),
3629                                 &exception);
3630         } while (exception.retry);
3631         return err;
3632 }
3633 
3634 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3635 {
3636         int res = 0;
3637         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3638                 case FL_POSIX:
3639                         res = posix_lock_file_wait(file, fl);
3640                         break;
3641                 case FL_FLOCK:
3642                         res = flock_lock_file_wait(file, fl);
3643                         break;
3644                 default:
3645                         BUG();
3646         }
3647         return res;
3648 }
3649 
3650 struct nfs4_unlockdata {
3651         struct nfs_locku_args arg;
3652         struct nfs_locku_res res;
3653         struct nfs4_lock_state *lsp;
3654         struct nfs_open_context *ctx;
3655         struct file_lock fl;
3656         const struct nfs_server *server;
3657         unsigned long timestamp;
3658 };
3659 
3660 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3661                 struct nfs_open_context *ctx,
3662                 struct nfs4_lock_state *lsp,
3663                 struct nfs_seqid *seqid)
3664 {
3665         struct nfs4_unlockdata *p;
3666         struct inode *inode = lsp->ls_state->inode;
3667 
3668         p = kzalloc(sizeof(*p), GFP_KERNEL);
3669         if (p == NULL)
3670                 return NULL;
3671         p->arg.fh = NFS_FH(inode);
3672         p->arg.fl = &p->fl;
3673         p->arg.seqid = seqid;
3674         p->res.seqid = seqid;
3675         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3676         p->arg.stateid = &lsp->ls_stateid;
3677         p->lsp = lsp;
3678         atomic_inc(&lsp->ls_count);
3679         /* Ensure we don't close file until we're done freeing locks! */
3680         p->ctx = get_nfs_open_context(ctx);
3681         memcpy(&p->fl, fl, sizeof(p->fl));
3682         p->server = NFS_SERVER(inode);
3683         return p;
3684 }
3685 
3686 static void nfs4_locku_release_calldata(void *data)
3687 {
3688         struct nfs4_unlockdata *calldata = data;
3689         nfs_free_seqid(calldata->arg.seqid);
3690         nfs4_put_lock_state(calldata->lsp);
3691         put_nfs_open_context(calldata->ctx);
3692         kfree(calldata);
3693 }
3694 
3695 static void nfs4_locku_done(struct rpc_task *task, void *data)
3696 {
3697         struct nfs4_unlockdata *calldata = data;
3698 
3699         nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3700                            task->tk_status);
3701         if (RPC_ASSASSINATED(task))
3702                 return;
3703         switch (task->tk_status) {
3704                 case 0:
3705                         memcpy(calldata->lsp->ls_stateid.data,
3706                                         calldata->res.stateid.data,
3707                                         sizeof(calldata->lsp->ls_stateid.data));
3708                         renew_lease(calldata->server, calldata->timestamp);
3709                         break;
3710                 case -NFS4ERR_BAD_STATEID:
3711                 case -NFS4ERR_OLD_STATEID:
3712                 case -NFS4ERR_STALE_STATEID:
3713                 case -NFS4ERR_EXPIRED:
3714                         break;
3715                 default:
3716                         if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3717                                 nfs4_restart_rpc(task,
3718                                                 calldata->server->nfs_client);
3719         }
3720         nfs4_sequence_free_slot(calldata->server->nfs_client,
3721                                 &calldata->res.seq_res);
3722 }
3723 
3724 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3725 {
3726         struct nfs4_unlockdata *calldata = data;
3727 
3728         if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3729                 return;
3730         if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3731                 /* Note: exit _without_ running nfs4_locku_done */
3732                 task->tk_action = NULL;
3733                 return;
3734         }
3735         calldata->timestamp = jiffies;
3736         if (nfs4_setup_sequence(calldata->server->nfs_client,
3737                                 &calldata->arg.seq_args,
3738                                 &calldata->res.seq_res, 1, task))
3739                 return;
3740         rpc_call_start(task);
3741 }
3742 
3743 static const struct rpc_call_ops nfs4_locku_ops = {
3744         .rpc_call_prepare = nfs4_locku_prepare,
3745         .rpc_call_done = nfs4_locku_done,
3746         .rpc_release = nfs4_locku_release_calldata,
3747 };
3748 
3749 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3750                 struct nfs_open_context *ctx,
3751                 struct nfs4_lock_state *lsp,
3752                 struct nfs_seqid *seqid)
3753 {
3754         struct nfs4_unlockdata *data;
3755         struct rpc_message msg = {
3756                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3757                 .rpc_cred = ctx->cred,
3758         };
3759         struct rpc_task_setup task_setup_data = {
3760                 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3761                 .rpc_message = &msg,
3762                 .callback_ops = &nfs4_locku_ops,
3763                 .workqueue = nfsiod_workqueue,
3764                 .flags = RPC_TASK_ASYNC,
3765         };
3766 
3767         /* Ensure this is an unlock - when canceling a lock, the
3768          * canceled lock is passed in, and it won't be an unlock.
3769          */
3770         fl->fl_type = F_UNLCK;
3771 
3772         data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3773         if (data == NULL) {
3774                 nfs_free_seqid(seqid);
3775                 return ERR_PTR(-ENOMEM);
3776         }
3777 
3778         msg.rpc_argp = &data->arg,
3779         msg.rpc_resp = &data->res,
3780         task_setup_data.callback_data = data;
3781         return rpc_run_task(&task_setup_data);
3782 }
3783 
3784 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3785 {
3786         struct nfs_inode *nfsi = NFS_I(state->inode);
3787         struct nfs_seqid *seqid;
3788         struct nfs4_lock_state *lsp;
3789         struct rpc_task *task;
3790         int status = 0;
3791         unsigned char fl_flags = request->fl_flags;
3792 
3793         status = nfs4_set_lock_state(state, request);
3794         /* Unlock _before_ we do the RPC call */
3795         request->fl_flags |= FL_EXISTS;
3796         down_read(&nfsi->rwsem);
3797         if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3798                 up_read(&nfsi->rwsem);
3799                 goto out;
3800         }
3801         up_read(&nfsi->rwsem);
3802         if (status != 0)
3803                 goto out;
3804         /* Is this a delegated lock? */
3805         if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3806                 goto out;
3807         lsp = request->fl_u.nfs4_fl.owner;
3808         seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3809         status = -ENOMEM;
3810         if (seqid == NULL)
3811                 goto out;
3812         task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3813         status = PTR_ERR(task);
3814         if (IS_ERR(task))
3815                 goto out;
3816         status = nfs4_wait_for_completion_rpc_task(task);
3817         rpc_put_task(task);
3818 out:
3819         request->fl_flags = fl_flags;
3820         return status;
3821 }
3822 
3823 struct nfs4_lockdata {
3824         struct nfs_lock_args arg;
3825         struct nfs_lock_res res;
3826         struct nfs4_lock_state *lsp;
3827         struct nfs_open_context *ctx;
3828         struct file_lock fl;
3829         unsigned long timestamp;
3830         int rpc_status;
3831         int cancelled;
3832         struct nfs_server *server;
3833 };
3834 
3835 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3836                 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3837 {
3838         struct nfs4_lockdata *p;
3839         struct inode *inode = lsp->ls_state->inode;
3840         struct nfs_server *server = NFS_SERVER(inode);
3841 
3842         p = kzalloc(sizeof(*p), GFP_KERNEL);
3843         if (p == NULL)
3844                 return NULL;
3845 
3846         p->arg.fh = NFS_FH(inode);
3847         p->arg.fl = &p->fl;
3848         p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3849         if (p->arg.open_seqid == NULL)
3850                 goto out_free;
3851         p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3852         if (p->arg.lock_seqid == NULL)
3853                 goto out_free_seqid;
3854         p->arg.lock_stateid = &lsp->ls_stateid;
3855         p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3856         p->arg.lock_owner.id = lsp->ls_id.id;
3857         p->res.lock_seqid = p->arg.lock_seqid;
3858         p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3859         p->lsp = lsp;
3860         p->server = server;
3861         atomic_inc(&lsp->ls_count);
3862         p->ctx = get_nfs_open_context(ctx);
3863         memcpy(&p->fl, fl, sizeof(p->fl));
3864         return p;
3865 out_free_seqid:
3866         nfs_free_seqid(p->arg.open_seqid);
3867 out_free:
3868         kfree(p);
3869         return NULL;
3870 }
3871 
3872 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3873 {
3874         struct nfs4_lockdata *data = calldata;
3875         struct nfs4_state *state = data->lsp->ls_state;
3876 
3877         dprintk("%s: begin!\n", __func__);
3878         if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3879                 return;
3880         /* Do we need to do an open_to_lock_owner? */
3881         if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3882                 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3883                         return;
3884                 data->arg.open_stateid = &state->stateid;
3885                 data->arg.new_lock_owner = 1;
3886                 data->res.open_seqid = data->arg.open_seqid;
3887         } else
3888                 data->arg.new_lock_owner = 0;
3889         data->timestamp = jiffies;
3890         if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3891                                 &data->res.seq_res, 1, task))
3892                 return;
3893         rpc_call_start(task);
3894         dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3895 }
3896 
3897 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3898 {
3899         struct nfs4_lockdata *data = calldata;
3900 
3901         dprintk("%s: begin!\n", __func__);
3902 
3903         nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3904                                      task->tk_status);
3905 
3906         data->rpc_status = task->tk_status;
3907         if (RPC_ASSASSINATED(task))
3908                 goto out;
3909         if (data->arg.new_lock_owner != 0) {
3910                 if (data->rpc_status == 0)
3911                         nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3912                 else
3913                         goto out;
3914         }
3915         if (data->rpc_status == 0) {
3916                 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3917                                         sizeof(data->lsp->ls_stateid.data));
3918                 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3919                 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3920         }
3921 out:
3922         dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3923 }
3924 
3925 static void nfs4_lock_release(void *calldata)
3926 {
3927         struct nfs4_lockdata *data = calldata;
3928 
3929         dprintk("%s: begin!\n", __func__);
3930         nfs_free_seqid(data->arg.open_seqid);
3931         if (data->cancelled != 0) {
3932                 struct rpc_task *task;
3933                 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3934                                 data->arg.lock_seqid);
3935                 if (!IS_ERR(task))
3936                         rpc_put_task(task);
3937                 dprintk("%s: cancelling lock!\n", __func__);
3938         } else
3939                 nfs_free_seqid(data->arg.lock_seqid);
3940         nfs4_put_lock_state(data->lsp);
3941         put_nfs_open_context(data->ctx);
3942         kfree(data);
3943         dprintk("%s: done!\n", __func__);
3944 }
3945 
3946 static const struct rpc_call_ops nfs4_lock_ops = {
3947         .rpc_call_prepare = nfs4_lock_prepare,
3948         .rpc_call_done = nfs4_lock_done,
3949         .rpc_release = nfs4_lock_release,
3950 };
3951 
3952 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3953 {
3954         struct nfs4_lockdata *data;
3955         struct rpc_task *task;
3956         struct rpc_message msg = {
3957                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3958                 .rpc_cred = state->owner->so_cred,
3959         };
3960         struct rpc_task_setup task_setup_data = {
3961                 .rpc_client = NFS_CLIENT(state->inode),
3962                 .rpc_message = &msg,
3963                 .callback_ops = &nfs4_lock_ops,
3964                 .workqueue = nfsiod_workqueue,
3965                 .flags = RPC_TASK_ASYNC,
3966         };
3967         int ret;
3968 
3969         dprintk("%s: begin!\n", __func__);
3970         data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3971                         fl->fl_u.nfs4_fl.owner);
3972         if (data == NULL)
3973                 return -ENOMEM;
3974         if (IS_SETLKW(cmd))
3975                 data->arg.block = 1;
3976         if (reclaim != 0)
3977                 data->arg.reclaim = 1;
3978         msg.rpc_argp = &data->arg,
3979         msg.rpc_resp = &data->res,
3980         task_setup_data.callback_data = data;
3981         task = rpc_run_task(&task_setup_data);
3982         if (IS_ERR(task))
3983                 return PTR_ERR(task);
3984         ret = nfs4_wait_for_completion_rpc_task(task);
3985         if (ret == 0) {
3986                 ret = data->rpc_status;
3987         } else
3988                 data->cancelled = 1;
3989         rpc_put_task(task);
3990         dprintk("%s: done, ret = %d!\n", __func__, ret);
3991         return ret;
3992 }
3993 
3994 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3995 {
3996         struct nfs_server *server = NFS_SERVER(state->inode);
3997         struct nfs4_exception exception = { };
3998         int err;
3999 
4000         do {
4001                 /* Cache the lock if possible... */
4002                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4003                         return 0;
4004                 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4005                 if (err != -NFS4ERR_DELAY)
4006                         break;
4007                 nfs4_handle_exception(server, err, &exception);
4008         } while (exception.retry);
4009         return err;
4010 }
4011 
4012 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4013 {
4014         struct nfs_server *server = NFS_SERVER(state->inode);
4015         struct nfs4_exception exception = { };
4016         int err;
4017 
4018         err = nfs4_set_lock_state(state, request);
4019         if (err != 0)
4020                 return err;
4021         do {
4022                 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4023                         return 0;
4024                 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
4025                 if (err != -NFS4ERR_DELAY)
4026                         break;
4027                 nfs4_handle_exception(server, err, &exception);
4028         } while (exception.retry);
4029         return err;
4030 }
4031 
4032 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4033 {
4034         struct nfs_inode *nfsi = NFS_I(state->inode);
4035         unsigned char fl_flags = request->fl_flags;
4036         int status;
4037 
4038         /* Is this a delegated open? */
4039         status = nfs4_set_lock_state(state, request);
4040         if (status != 0)
4041                 goto out;
4042         request->fl_flags |= FL_ACCESS;
4043         status = do_vfs_lock(request->fl_file, request);
4044         if (status < 0)
4045                 goto out;
4046         down_read(&nfsi->rwsem);
4047         if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4048                 /* Yes: cache locks! */
4049                 /* ...but avoid races with delegation recall... */
4050                 request->fl_flags = fl_flags & ~FL_SLEEP;
4051                 status = do_vfs_lock(request->fl_file, request);
4052                 goto out_unlock;
4053         }
4054         status = _nfs4_do_setlk(state, cmd, request, 0);
4055         if (status != 0)
4056                 goto out_unlock;
4057         /* Note: we always want to sleep here! */
4058         request->fl_flags = fl_flags | FL_SLEEP;
4059         if (do_vfs_lock(request->fl_file, request) < 0)
4060                 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4061 out_unlock:
4062         up_read(&nfsi->rwsem);
4063 out:
4064         request->fl_flags = fl_flags;
4065         return status;
4066 }
4067 
4068 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4069 {
4070         struct nfs4_exception exception = { };
4071         int err;
4072 
4073         do {
4074                 err = _nfs4_proc_setlk(state, cmd, request);
4075                 if (err == -NFS4ERR_DENIED)
4076                         err = -EAGAIN;
4077                 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4078                                 err, &exception);
4079         } while (exception.retry);
4080         return err;
4081 }
4082 
4083 static int
4084 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4085 {
4086         struct nfs_open_context *ctx;
4087         struct nfs4_state *state;
4088         unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4089         int status;
4090 
4091         /* verify open state */
4092         ctx = nfs_file_open_context(filp);
4093         state = ctx->state;
4094 
4095         if (request->fl_start < 0 || request->fl_end < 0)
4096                 return -EINVAL;
4097 
4098         if (IS_GETLK(cmd)) {
4099                 if (state != NULL)
4100                         return nfs4_proc_getlk(state, F_GETLK, request);
4101                 return 0;
4102         }
4103 
4104         if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4105                 return -EINVAL;
4106 
4107         if (request->fl_type == F_UNLCK) {
4108                 if (state != NULL)
4109                         return nfs4_proc_unlck(state, cmd, request);
4110                 return 0;
4111         }
4112 
4113         if (state == NULL)
4114                 return -ENOLCK;
4115         do {
4116                 status = nfs4_proc_setlk(state, cmd, request);
4117                 if ((status != -EAGAIN) || IS_SETLK(cmd))
4118                         break;
4119                 timeout = nfs4_set_lock_task_retry(timeout);
4120                 status = -ERESTARTSYS;
4121                 if (signalled())
4122                         break;
4123         } while(status < 0);
4124         return status;
4125 }
4126 
4127 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4128 {
4129         struct nfs_server *server = NFS_SERVER(state->inode);
4130         struct nfs4_exception exception = { };
4131         int err;
4132 
4133         err = nfs4_set_lock_state(state, fl);
4134         if (err != 0)
4135                 goto out;
4136         do {
4137                 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4138                 switch (err) {
4139                         default:
4140                                 printk(KERN_ERR "%s: unhandled error %d.\n",
4141                                                 __func__, err);
4142                         case 0:
4143                         case -ESTALE:
4144                                 goto out;
4145                         case -NFS4ERR_EXPIRED:
4146                         case -NFS4ERR_STALE_CLIENTID:
4147                         case -NFS4ERR_STALE_STATEID:
4148                                 nfs4_schedule_state_recovery(server->nfs_client);
4149                                 goto out;
4150                         case -ERESTARTSYS:
4151                                 /*
4152                                  * The show must go on: exit, but mark the
4153                                  * stateid as needing recovery.
4154                                  */
4155                         case -NFS4ERR_ADMIN_REVOKED:
4156                         case -NFS4ERR_BAD_STATEID:
4157                         case -NFS4ERR_OPENMODE:
4158                                 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4159                                 err = 0;
4160                                 goto out;
4161                         case -ENOMEM:
4162                         case -NFS4ERR_DENIED:
4163                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4164                                 err = 0;
4165                                 goto out;
4166                         case -NFS4ERR_DELAY:
4167                                 break;
4168                 }
4169                 err = nfs4_handle_exception(server, err, &exception);
4170         } while (exception.retry);
4171 out:
4172         return err;
4173 }
4174 
4175 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4176 
4177 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4178                 size_t buflen, int flags)
4179 {
4180         struct inode *inode = dentry->d_inode;
4181 
4182         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4183                 return -EOPNOTSUPP;
4184 
4185         return nfs4_proc_set_acl(inode, buf, buflen);
4186 }
4187 
4188 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4189  * and that's what we'll do for e.g. user attributes that haven't been set.
4190  * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4191  * attributes in kernel-managed attribute namespaces. */
4192 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4193                 size_t buflen)
4194 {
4195         struct inode *inode = dentry->d_inode;
4196 
4197         if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4198                 return -EOPNOTSUPP;
4199 
4200         return nfs4_proc_get_acl(inode, buf, buflen);
4201 }
4202 
4203 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4204 {
4205         size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4206 
4207         if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4208                 return 0;
4209         if (buf && buflen < len)
4210                 return -ERANGE;
4211         if (buf)
4212                 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4213         return len;
4214 }
4215 
4216 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4217 {
4218         if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4219                 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4220                 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4221                 return;
4222 
4223         fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4224                 NFS_ATTR_FATTR_NLINK;
4225         fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4226         fattr->nlink = 2;
4227 }
4228 
4229 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4230                 struct nfs4_fs_locations *fs_locations, struct page *page)
4231 {
4232         struct nfs_server *server = NFS_SERVER(dir);
4233         u32 bitmask[2] = {
4234                 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4235                 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4236         };
4237         struct nfs4_fs_locations_arg args = {
4238                 .dir_fh = NFS_FH(dir),
4239                 .name = name,
4240                 .page = page,
4241                 .bitmask = bitmask,
4242         };
4243         struct nfs4_fs_locations_res res = {
4244                 .fs_locations = fs_locations,
4245         };
4246         struct rpc_message msg = {
4247                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4248                 .rpc_argp = &args,
4249                 .rpc_resp = &res,
4250         };
4251         int status;
4252 
4253         dprintk("%s: start\n", __func__);
4254         nfs_fattr_init(&fs_locations->fattr);
4255         fs_locations->server = server;
4256         fs_locations->nlocations = 0;
4257         status = nfs4_call_sync(server, &msg, &args, &res, 0);
4258         nfs_fixup_referral_attributes(&fs_locations->fattr);
4259         dprintk("%s: returned status = %d\n", __func__, status);
4260         return status;
4261 }
4262 
4263 #ifdef CONFIG_NFS_V4_1
4264 /*
4265  * nfs4_proc_exchange_id()
4266  *
4267  * Since the clientid has expired, all compounds using sessions
4268  * associated with the stale clientid will be returning
4269  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4270  * be in some phase of session reset.
4271  */
4272 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4273 {
4274         nfs4_verifier verifier;
4275         struct nfs41_exchange_id_args args = {
4276                 .client = clp,
4277                 .flags = clp->cl_exchange_flags,
4278         };
4279         struct nfs41_exchange_id_res res = {
4280                 .client = clp,
4281         };
4282         int status;
4283         struct rpc_message msg = {
4284                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4285                 .rpc_argp = &args,
4286                 .rpc_resp = &res,
4287                 .rpc_cred = cred,
4288         };
4289         __be32 *p;
4290 
4291         dprintk("--> %s\n", __func__);
4292         BUG_ON(clp == NULL);
4293 
4294         p = (u32 *)verifier.data;
4295         *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4296         *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4297         args.verifier = &verifier;
4298 
4299         while (1) {
4300                 args.id_len = scnprintf(args.id, sizeof(args.id),
4301                                         "%s/%s %u",
4302                                         clp->cl_ipaddr,
4303                                         rpc_peeraddr2str(clp->cl_rpcclient,
4304                                                          RPC_DISPLAY_ADDR),
4305                                         clp->cl_id_uniquifier);
4306 
4307                 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4308 
4309                 if (status != NFS4ERR_CLID_INUSE)
4310                         break;
4311 
4312                 if (signalled())
4313                         break;
4314 
4315                 if (++clp->cl_id_uniquifier == 0)
4316                         break;
4317         }
4318 
4319         dprintk("<-- %s status= %d\n", __func__, status);
4320         return status;
4321 }
4322 
4323 struct nfs4_get_lease_time_data {
4324         struct nfs4_get_lease_time_args *args;
4325         struct nfs4_get_lease_time_res *res;
4326         struct nfs_client *clp;
4327 };
4328 
4329 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4330                                         void *calldata)
4331 {
4332         int ret;
4333         struct nfs4_get_lease_time_data *data =
4334                         (struct nfs4_get_lease_time_data *)calldata;
4335 
4336         dprintk("--> %s\n", __func__);
4337         /* just setup sequence, do not trigger session recovery
4338            since we're invoked within one */
4339         ret = nfs41_setup_sequence(data->clp->cl_session,
4340                                         &data->args->la_seq_args,
4341                                         &data->res->lr_seq_res, 0, task);
4342 
4343         BUG_ON(ret == -EAGAIN);
4344         rpc_call_start(task);
4345         dprintk("<-- %s\n", __func__);
4346 }
4347 
4348 /*
4349  * Called from nfs4_state_manager thread for session setup, so don't recover
4350  * from sequence operation or clientid errors.
4351  */
4352 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4353 {
4354         struct nfs4_get_lease_time_data *data =
4355                         (struct nfs4_get_lease_time_data *)calldata;
4356 
4357         dprintk("--> %s\n", __func__);
4358         nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4359         switch (task->tk_status) {
4360         case -NFS4ERR_DELAY:
4361         case -NFS4ERR_GRACE:
4362                 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4363                 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4364                 task->tk_status = 0;
4365                 nfs4_restart_rpc(task, data->clp);
4366                 return;
4367         }
4368         nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4369         dprintk("<-- %s\n", __func__);
4370 }
4371 
4372 struct rpc_call_ops nfs4_get_lease_time_ops = {
4373         .rpc_call_prepare = nfs4_get_lease_time_prepare,
4374         .rpc_call_done = nfs4_get_lease_time_done,
4375 };
4376 
4377 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4378 {
4379         struct rpc_task *task;
4380         struct nfs4_get_lease_time_args args;
4381         struct nfs4_get_lease_time_res res = {
4382                 .lr_fsinfo = fsinfo,
4383         };
4384         struct nfs4_get_lease_time_data data = {
4385                 .args = &args,
4386                 .res = &res,
4387                 .clp = clp,
4388         };
4389         struct rpc_message msg = {
4390                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4391                 .rpc_argp = &args,
4392                 .rpc_resp = &res,
4393         };
4394         struct rpc_task_setup task_setup = {
4395                 .rpc_client = clp->cl_rpcclient,
4396                 .rpc_message = &msg,
4397                 .callback_ops = &nfs4_get_lease_time_ops,
4398                 .callback_data = &data
4399         };
4400         int status;
4401 
4402         res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4403         dprintk("--> %s\n", __func__);
4404         task = rpc_run_task(&task_setup);
4405 
4406         if (IS_ERR(task))
4407                 status = PTR_ERR(task);
4408         else {
4409                 status = task->tk_status;
4410                 rpc_put_task(task);
4411         }
4412         dprintk("<-- %s return %d\n", __func__, status);
4413 
4414         return status;
4415 }
4416 
4417 /*
4418  * Reset a slot table
4419  */
4420 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4421                 int old_max_slots, int ivalue)
4422 {
4423         int i;
4424         int ret = 0;
4425 
4426         dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
4427 
4428         /*
4429          * Until we have dynamic slot table adjustment, insist
4430          * upon the same slot table size
4431          */
4432         if (max_slots != old_max_slots) {
4433                 dprintk("%s reset slot table does't match old\n",
4434                         __func__);
4435                 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4436                 goto out;
4437         }
4438         spin_lock(&tbl->slot_tbl_lock);
4439         for (i = 0; i < max_slots; ++i)
4440                 tbl->slots[i].seq_nr = ivalue;
4441         tbl->highest_used_slotid = -1;
4442         spin_unlock(&tbl->slot_tbl_lock);
4443         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4444                 tbl, tbl->slots, tbl->max_slots);
4445 out:
4446         dprintk("<-- %s: return %d\n", __func__, ret);
4447         return ret;
4448 }
4449 
4450 /*
4451  * Reset the forechannel and backchannel slot tables
4452  */
4453 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4454 {
4455         int status;
4456 
4457         status = nfs4_reset_slot_table(&session->fc_slot_table,
4458                         session->fc_attrs.max_reqs,
4459                         session->fc_slot_table.max_slots,
4460                         1);
4461         if (status)
4462                 return status;
4463 
4464         status = nfs4_reset_slot_table(&session->bc_slot_table,
4465                         session->bc_attrs.max_reqs,
4466                         session->bc_slot_table.max_slots,
4467                         0);
4468         return status;
4469 }
4470 
4471 /* Destroy the slot table */
4472 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4473 {
4474         if (session->fc_slot_table.slots != NULL) {
4475                 kfree(session->fc_slot_table.slots);
4476                 session->fc_slot_table.slots = NULL;
4477         }
4478         if (session->bc_slot_table.slots != NULL) {
4479                 kfree(session->bc_slot_table.slots);
4480                 session->bc_slot_table.slots = NULL;
4481         }
4482         return;
4483 }
4484 
4485 /*
4486  * Initialize slot table
4487  */
4488 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4489                 int max_slots, int ivalue)
4490 {
4491         int i;
4492         struct nfs4_slot *slot;
4493         int ret = -ENOMEM;
4494 
4495         BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4496 
4497         dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4498 
4499         slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4500         if (!slot)
4501                 goto out;
4502         for (i = 0; i < max_slots; ++i)
4503                 slot[i].seq_nr = ivalue;
4504         ret = 0;
4505 
4506         spin_lock(&tbl->slot_tbl_lock);
4507         if (tbl->slots != NULL) {
4508                 spin_unlock(&tbl->slot_tbl_lock);
4509                 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4510                         __func__, tbl, tbl->slots);
4511                 WARN_ON(1);
4512                 goto out_free;
4513         }
4514         tbl->max_slots = max_slots;
4515         tbl->slots = slot;
4516         tbl->highest_used_slotid = -1;  /* no slot is currently used */
4517         spin_unlock(&tbl->slot_tbl_lock);
4518         dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4519                 tbl, tbl->slots, tbl->max_slots);
4520 out:
4521         dprintk("<-- %s: return %d\n", __func__, ret);
4522         return ret;
4523 
4524 out_free:
4525         kfree(slot);
4526         goto out;
4527 }
4528 
4529 /*
4530  * Initialize the forechannel and backchannel tables
4531  */
4532 static int nfs4_init_slot_tables(struct nfs4_session *session)
4533 {
4534         int status;
4535 
4536         status = nfs4_init_slot_table(&session->fc_slot_table,
4537                         session->fc_attrs.max_reqs, 1);
4538         if (status)
4539                 return status;
4540 
4541         status = nfs4_init_slot_table(&session->bc_slot_table,
4542                         session->bc_attrs.max_reqs, 0);
4543         if (status)
4544                 nfs4_destroy_slot_tables(session);
4545 
4546         return status;
4547 }
4548 
4549 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4550 {
4551         struct nfs4_session *session;
4552         struct nfs4_slot_table *tbl;
4553 
4554         session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4555         if (!session)
4556                 return NULL;
4557 
4558         set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
4559         /*
4560          * The create session reply races with the server back
4561          * channel probe. Mark the client NFS_CS_SESSION_INITING
4562          * so that the client back channel can find the
4563          * nfs_client struct
4564          */
4565         clp->cl_cons_state = NFS_CS_SESSION_INITING;
4566 
4567         tbl = &session->fc_slot_table;
4568         spin_lock_init(&tbl->slot_tbl_lock);
4569         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4570 
4571         tbl = &session->bc_slot_table;
4572         spin_lock_init(&tbl->slot_tbl_lock);
4573         rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4574 
4575         session->clp = clp;
4576         return session;
4577 }
4578 
4579 void nfs4_destroy_session(struct nfs4_session *session)
4580 {
4581         nfs4_proc_destroy_session(session);
4582         dprintk("%s Destroy backchannel for xprt %p\n",
4583                 __func__, session->clp->cl_rpcclient->cl_xprt);
4584         xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4585                                 NFS41_BC_MIN_CALLBACKS);
4586         nfs4_destroy_slot_tables(session);
4587         kfree(session);
4588 }
4589 
4590 /*
4591  * Initialize the values to be used by the client in CREATE_SESSION
4592  * If nfs4_init_session set the fore channel request and response sizes,
4593  * use them.
4594  *
4595  * Set the back channel max_resp_sz_cached to zero to force the client to
4596  * always set csa_cachethis to FALSE because the current implementation
4597  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4598  */
4599 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4600 {
4601         struct nfs4_session *session = args->client->cl_session;
4602         unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4603                      mxresp_sz = session->fc_attrs.max_resp_sz;
4604 
4605         if (mxrqst_sz == 0)
4606                 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4607         if (mxresp_sz == 0)
4608                 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4609         /* Fore channel attributes */
4610         args->fc_attrs.headerpadsz = 0;
4611         args->fc_attrs.max_rqst_sz = mxrqst_sz;
4612         args->fc_attrs.max_resp_sz = mxresp_sz;
4613         args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4614         args->fc_attrs.max_ops = NFS4_MAX_OPS;
4615         args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4616 
4617         dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4618                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4619                 __func__,
4620                 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4621                 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4622                 args->fc_attrs.max_reqs);
4623 
4624         /* Back channel attributes */
4625         args->bc_attrs.headerpadsz = 0;
4626         args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4627         args->bc_attrs.max_resp_sz = PAGE_SIZE;
4628         args->bc_attrs.max_resp_sz_cached = 0;
4629         args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4630         args->bc_attrs.max_reqs = 1;
4631 
4632         dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4633                 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4634                 __func__,
4635                 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4636                 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4637                 args->bc_attrs.max_reqs);
4638 }
4639 
4640 static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4641 {
4642         if (rcvd <= sent)
4643                 return 0;
4644         printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4645                 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4646         return -EINVAL;
4647 }
4648 
4649 #define _verify_fore_channel_attr(_name_) \
4650         _verify_channel_attr("fore", #_name_, \
4651                              args->fc_attrs._name_, \
4652                              session->fc_attrs._name_)
4653 
4654 #define _verify_back_channel_attr(_name_) \
4655         _verify_channel_attr("back", #_name_, \
4656                              args->bc_attrs._name_, \
4657                              session->bc_attrs._name_)
4658 
4659 /*
4660  * The server is not allowed to increase the fore channel header pad size,
4661  * maximum response size, or maximum number of operations.
4662  *
4663  * The back channel attributes are only negotiatied down: We send what the
4664  * (back channel) server insists upon.
4665  */
4666 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4667                                      struct nfs4_session *session)
4668 {
4669         int ret = 0;
4670 
4671         ret |= _verify_fore_channel_attr(headerpadsz);
4672         ret |= _verify_fore_channel_attr(max_resp_sz);
4673         ret |= _verify_fore_channel_attr(max_ops);
4674 
4675         ret |= _verify_back_channel_attr(headerpadsz);
4676         ret |= _verify_back_channel_attr(max_rqst_sz);
4677         ret |= _verify_back_channel_attr(max_resp_sz);
4678         ret |= _verify_back_channel_attr(max_resp_sz_cached);
4679         ret |= _verify_back_channel_attr(max_ops);
4680         ret |= _verify_back_channel_attr(max_reqs);
4681 
4682         return ret;
4683 }
4684 
4685 static int _nfs4_proc_create_session(struct nfs_client *clp)
4686 {
4687         struct nfs4_session *session = clp->cl_session;
4688         struct nfs41_create_session_args args = {
4689                 .client = clp,
4690                 .cb_program = NFS4_CALLBACK,
4691         };
4692         struct nfs41_create_session_res res = {
4693                 .client = clp,
4694         };
4695         struct rpc_message msg = {
4696                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4697                 .rpc_argp = &args,
4698                 .rpc_resp = &res,
4699         };
4700         int status;
4701 
4702         nfs4_init_channel_attrs(&args);
4703         args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
4704 
4705         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4706 
4707         if (!status)
4708                 /* Verify the session's negotiated channel_attrs values */
4709                 status = nfs4_verify_channel_attrs(&args, session);
4710         if (!status) {
4711                 /* Increment the clientid slot sequence id */
4712                 clp->cl_seqid++;
4713         }
4714 
4715         return status;
4716 }
4717 
4718 /*
4719  * Issues a CREATE_SESSION operation to the server.
4720  * It is the responsibility of the caller to verify the session is
4721  * expired before calling this routine.
4722  */
4723 int nfs4_proc_create_session(struct nfs_client *clp, int reset)
4724 {
4725         int status;
4726         unsigned *ptr;
4727         struct nfs_fsinfo fsinfo;
4728         struct nfs4_session *session = clp->cl_session;
4729 
4730         dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4731 
4732         status = _nfs4_proc_create_session(clp);
4733         if (status)
4734                 goto out;
4735 
4736         /* Init or reset the fore channel */
4737         if (reset)
4738                 status = nfs4_reset_slot_tables(session);
4739         else
4740                 status = nfs4_init_slot_tables(session);
4741         dprintk("fore channel slot table initialization returned %d\n", status);
4742         if (status)
4743                 goto out;
4744 
4745         ptr = (unsigned *)&session->sess_id.data[0];
4746         dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4747                 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4748 
4749         if (reset)
4750                 /* Lease time is aleady set */
4751                 goto out;
4752 
4753         /* Get the lease time */
4754         status = nfs4_proc_get_lease_time(clp, &fsinfo);
4755         if (status == 0) {
4756                 /* Update lease time and schedule renewal */
4757                 spin_lock(&clp->cl_lock);
4758                 clp->cl_lease_time = fsinfo.lease_time * HZ;
4759                 clp->cl_last_renewal = jiffies;
4760                 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4761                 spin_unlock(&clp->cl_lock);
4762 
4763                 nfs4_schedule_state_renewal(clp);
4764         }
4765 out:
4766         dprintk("<-- %s\n", __func__);
4767         return status;
4768 }
4769 
4770 /*
4771  * Issue the over-the-wire RPC DESTROY_SESSION.
4772  * The caller must serialize access to this routine.
4773  */
4774 int nfs4_proc_destroy_session(struct nfs4_session *session)
4775 {
4776         int status = 0;
4777         struct rpc_message msg;
4778 
4779         dprintk("--> nfs4_proc_destroy_session\n");
4780 
4781         /* session is still being setup */
4782         if (session->clp->cl_cons_state != NFS_CS_READY)
4783                 return status;
4784 
4785         msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4786         msg.rpc_argp = session;
4787         msg.rpc_resp = NULL;
4788         msg.rpc_cred = NULL;
4789         status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4790 
4791         if (status)
4792                 printk(KERN_WARNING
4793                         "Got error %d from the server on DESTROY_SESSION. "
4794                         "Session has been destroyed regardless...\n", status);
4795 
4796         dprintk("<-- nfs4_proc_destroy_session\n");
4797         return status;
4798 }
4799 
4800 int nfs4_init_session(struct nfs_server *server)
4801 {
4802         struct nfs_client *clp = server->nfs_client;
4803         int ret;
4804 
4805         if (!nfs4_has_session(clp))
4806                 return 0;
4807 
4808         clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4809         clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4810         ret = nfs4_recover_expired_lease(server);
4811         if (!ret)
4812                 ret = nfs4_check_client_ready(clp);
4813         return ret;
4814 }
4815 
4816 /*
4817  * Renew the cl_session lease.
4818  */
4819 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4820 {
4821         struct nfs4_sequence_args args;
4822         struct nfs4_sequence_res res;
4823 
4824         struct rpc_message msg = {
4825                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4826                 .rpc_argp = &args,
4827                 .rpc_resp = &res,
4828                 .rpc_cred = cred,
4829         };
4830 
4831         args.sa_cache_this = 0;
4832 
4833         return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4834                                        &res, 0);
4835 }
4836 
4837 void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4838 {
4839         struct nfs_client *clp = (struct nfs_client *)data;
4840 
4841         nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4842 
4843         if (task->tk_status < 0) {
4844                 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4845 
4846                 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4847                                                                 == -EAGAIN) {
4848                         nfs4_restart_rpc(task, clp);
4849                         return;
4850                 }
4851         }
4852         nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4853         dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4854 
4855         kfree(task->tk_msg.rpc_argp);
4856         kfree(task->tk_msg.rpc_resp);
4857 
4858         dprintk("<-- %s\n", __func__);
4859 }
4860 
4861 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4862 {
4863         struct nfs_client *clp;
4864         struct nfs4_sequence_args *args;
4865         struct nfs4_sequence_res *res;
4866 
4867         clp = (struct nfs_client *)data;
4868         args = task->tk_msg.rpc_argp;
4869         res = task->tk_msg.rpc_resp;
4870 
4871         if (nfs4_setup_sequence(clp, args, res, 0, task))
4872                 return;
4873         rpc_call_start(task);
4874 }
4875 
4876 static const struct rpc_call_ops nfs41_sequence_ops = {
4877         .rpc_call_done = nfs41_sequence_call_done,
4878         .rpc_call_prepare = nfs41_sequence_prepare,
4879 };
4880 
4881 static int nfs41_proc_async_sequence(struct nfs_client *clp,
4882                                      struct rpc_cred *cred)
4883 {
4884         struct nfs4_sequence_args *args;
4885         struct nfs4_sequence_res *res;
4886         struct rpc_message msg = {
4887                 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4888                 .rpc_cred = cred,
4889         };
4890 
4891         args = kzalloc(sizeof(*args), GFP_KERNEL);
4892         if (!args)
4893                 return -ENOMEM;
4894         res = kzalloc(sizeof(*res), GFP_KERNEL);
4895         if (!res) {
4896                 kfree(args);
4897                 return -ENOMEM;
4898         }
4899         res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4900         msg.rpc_argp = args;
4901         msg.rpc_resp = res;
4902 
4903         return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4904                               &nfs41_sequence_ops, (void *)clp);
4905 }
4906 
4907 #endif /* CONFIG_NFS_V4_1 */
4908 
4909 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
4910         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4911         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4912         .recover_open   = nfs4_open_reclaim,
4913         .recover_lock   = nfs4_lock_reclaim,
4914         .establish_clid = nfs4_init_clientid,
4915         .get_clid_cred  = nfs4_get_setclientid_cred,
4916 };
4917 
4918 #if defined(CONFIG_NFS_V4_1)
4919 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4920         .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4921         .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4922         .recover_open   = nfs4_open_reclaim,
4923         .recover_lock   = nfs4_lock_reclaim,
4924         .establish_clid = nfs4_proc_exchange_id,
4925         .get_clid_cred  = nfs4_get_exchange_id_cred,
4926 };
4927 #endif /* CONFIG_NFS_V4_1 */
4928 
4929 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
4930         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4931         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4932         .recover_open   = nfs4_open_expired,
4933         .recover_lock   = nfs4_lock_expired,
4934         .establish_clid = nfs4_init_clientid,
4935         .get_clid_cred  = nfs4_get_setclientid_cred,
4936 };
4937 
4938 #if defined(CONFIG_NFS_V4_1)
4939 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4940         .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4941         .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4942         .recover_open   = nfs4_open_expired,
4943         .recover_lock   = nfs4_lock_expired,
4944         .establish_clid = nfs4_proc_exchange_id,
4945         .get_clid_cred  = nfs4_get_exchange_id_cred,
4946 };
4947 #endif /* CONFIG_NFS_V4_1 */
4948 
4949 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
4950         .sched_state_renewal = nfs4_proc_async_renew,
4951         .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
4952         .renew_lease = nfs4_proc_renew,
4953 };
4954 
4955 #if defined(CONFIG_NFS_V4_1)
4956 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
4957         .sched_state_renewal = nfs41_proc_async_sequence,
4958         .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
4959         .renew_lease = nfs4_proc_sequence,
4960 };
4961 #endif
4962 
4963 /*
4964  * Per minor version reboot and network partition recovery ops
4965  */
4966 
4967 struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
4968         &nfs40_reboot_recovery_ops,
4969 #if defined(CONFIG_NFS_V4_1)
4970         &nfs41_reboot_recovery_ops,
4971 #endif
4972 };
4973 
4974 struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
4975         &nfs40_nograce_recovery_ops,
4976 #if defined(CONFIG_NFS_V4_1)
4977         &nfs41_nograce_recovery_ops,
4978 #endif
4979 };
4980 
4981 struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
4982         &nfs40_state_renewal_ops,
4983 #if defined(CONFIG_NFS_V4_1)
4984         &nfs41_state_renewal_ops,
4985 #endif
4986 };
4987 
4988 static const struct inode_operations nfs4_file_inode_operations = {
4989         .permission     = nfs_permission,
4990         .getattr        = nfs_getattr,
4991         .setattr        = nfs_setattr,
4992         .getxattr       = nfs4_getxattr,
4993         .setxattr       = nfs4_setxattr,
4994         .listxattr      = nfs4_listxattr,
4995 };
4996 
4997 const struct nfs_rpc_ops nfs_v4_clientops = {
4998         .version        = 4,                    /* protocol version */
4999         .dentry_ops     = &nfs4_dentry_operations,
5000         .dir_inode_ops  = &nfs4_dir_inode_operations,
5001         .file_inode_ops = &nfs4_file_inode_operations,
5002         .getroot        = nfs4_proc_get_root,
5003         .getattr        = nfs4_proc_getattr,
5004         .setattr        = nfs4_proc_setattr,
5005         .lookupfh       = nfs4_proc_lookupfh,
5006         .lookup         = nfs4_proc_lookup,
5007         .access         = nfs4_proc_access,
5008         .readlink       = nfs4_proc_readlink,
5009         .create         = nfs4_proc_create,
5010         .remove         = nfs4_proc_remove,
5011         .unlink_setup   = nfs4_proc_unlink_setup,
5012         .unlink_done    = nfs4_proc_unlink_done,
5013         .rename         = nfs4_proc_rename,
5014         .link           = nfs4_proc_link,
5015         .symlink        = nfs4_proc_symlink,
5016         .mkdir          = nfs4_proc_mkdir,
5017         .rmdir          = nfs4_proc_remove,
5018         .readdir        = nfs4_proc_readdir,
5019         .mknod          = nfs4_proc_mknod,
5020         .statfs         = nfs4_proc_statfs,
5021         .fsinfo         = nfs4_proc_fsinfo,
5022         .pathconf       = nfs4_proc_pathconf,
5023         .set_capabilities = nfs4_server_capabilities,
5024         .decode_dirent  = nfs4_decode_dirent,
5025         .read_setup     = nfs4_proc_read_setup,
5026         .read_done      = nfs4_read_done,
5027         .write_setup    = nfs4_proc_write_setup,
5028         .write_done     = nfs4_write_done,
5029         .commit_setup   = nfs4_proc_commit_setup,
5030         .commit_done    = nfs4_commit_done,
5031         .lock           = nfs4_proc_lock,
5032         .clear_acl_cache = nfs4_zap_acl_attr,
5033         .close_context  = nfs4_close_context,
5034 };
5035 
5036 /*
5037  * Local variables:
5038  *  c-basic-offset: 8
5039  * End:
5040  */
5041 
  This page was automatically generated by the LXR engine.