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

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

Version: ~ [ linux-5.13-rc1 ] ~ [ linux-5.12.2 ] ~ [ linux-5.11.19 ] ~ [ linux-5.10.35 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.117 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.190 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.232 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.268 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.268 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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