Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Kendrick Smith <kmsmith@umich.edu> |
| 6 | * Andy Adamson <kandros@umich.edu> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 35 | #include <linux/file.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 36 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 38 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 39 | #include <linux/swap.h> |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 41 | #include <linux/sunrpc/svcauth_gss.h> |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 42 | #include <linux/sunrpc/clnt.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 43 | #include "xdr4.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 44 | #include "vfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 47 | |
| 48 | /* Globals */ |
J. Bruce Fields | cf07d2e | 2010-02-28 23:20:19 -0500 | [diff] [blame] | 49 | time_t nfsd4_lease = 90; /* default lease time */ |
J. Bruce Fields | efc4bb4f | 2010-03-02 11:04:06 -0500 | [diff] [blame] | 50 | time_t nfsd4_grace = 90; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 51 | static time_t boot_time; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 52 | static u32 current_stateid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 53 | static stateid_t zerostateid; /* bits all 0 */ |
| 54 | static stateid_t onestateid; /* bits all 1 */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 55 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 56 | |
| 57 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t))) |
| 58 | #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /* forward declarations */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 61 | static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 63 | /* Locking: */ |
| 64 | |
| 65 | /* Currently used for almost all code touching nfsv4 state: */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 66 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Currently used for the del_recall_lru and file hash table. In an |
| 70 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 71 | * eventually cover more: |
| 72 | */ |
| 73 | static DEFINE_SPINLOCK(recall_lock); |
| 74 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 75 | static struct kmem_cache *openowner_slab = NULL; |
| 76 | static struct kmem_cache *lockowner_slab = NULL; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 77 | static struct kmem_cache *file_slab = NULL; |
| 78 | static struct kmem_cache *stateid_slab = NULL; |
| 79 | static struct kmem_cache *deleg_slab = NULL; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | void |
| 82 | nfs4_lock_state(void) |
| 83 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 84 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
| 88 | nfs4_unlock_state(void) |
| 89 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 90 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static inline u32 |
| 94 | opaque_hashval(const void *ptr, int nbytes) |
| 95 | { |
| 96 | unsigned char *cptr = (unsigned char *) ptr; |
| 97 | |
| 98 | u32 x = 0; |
| 99 | while (nbytes--) { |
| 100 | x *= 37; |
| 101 | x += *cptr++; |
| 102 | } |
| 103 | return x; |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static struct list_head del_recall_lru; |
| 107 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 108 | static inline void |
| 109 | put_nfs4_file(struct nfs4_file *fi) |
| 110 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 111 | if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) { |
| 112 | list_del(&fi->fi_hash); |
| 113 | spin_unlock(&recall_lock); |
| 114 | iput(fi->fi_inode); |
| 115 | kmem_cache_free(file_slab, fi); |
| 116 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static inline void |
| 120 | get_nfs4_file(struct nfs4_file *fi) |
| 121 | { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 122 | atomic_inc(&fi->fi_ref); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 123 | } |
| 124 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 125 | static int num_delegations; |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 126 | unsigned int max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Open owner state (share locks) |
| 130 | */ |
| 131 | |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 132 | /* hash tables for open owners */ |
| 133 | #define OPEN_OWNER_HASH_BITS 8 |
| 134 | #define OPEN_OWNER_HASH_SIZE (1 << OPEN_OWNER_HASH_BITS) |
| 135 | #define OPEN_OWNER_HASH_MASK (OPEN_OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 136 | |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 137 | static unsigned int open_ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 138 | { |
| 139 | unsigned int ret; |
| 140 | |
| 141 | ret = opaque_hashval(ownername->data, ownername->len); |
| 142 | ret += clientid; |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 143 | return ret & OPEN_OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 144 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 145 | |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 146 | static struct list_head open_ownerstr_hashtbl[OPEN_OWNER_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 147 | |
| 148 | /* hash table for nfs4_file */ |
| 149 | #define FILE_HASH_BITS 8 |
| 150 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 151 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 152 | /* hash table for (open)nfs4_ol_stateid */ |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 153 | #define STATEID_HASH_BITS 10 |
| 154 | #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS) |
| 155 | #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1) |
| 156 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 157 | static unsigned int file_hashval(struct inode *ino) |
| 158 | { |
| 159 | /* XXX: why are we hashing on inode pointer, anyway? */ |
| 160 | return hash_ptr(ino, FILE_HASH_BITS); |
| 161 | } |
| 162 | |
J. Bruce Fields | ee626a7 | 2011-09-11 13:48:41 -0400 | [diff] [blame] | 163 | static unsigned int stateid_hashval(stateid_t *s) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 164 | { |
J. Bruce Fields | ee626a7 | 2011-09-11 13:48:41 -0400 | [diff] [blame] | 165 | return opaque_hashval(&s->si_opaque, sizeof(stateid_opaque_t)) & STATEID_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 166 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 167 | |
| 168 | static struct list_head file_hashtbl[FILE_HASH_SIZE]; |
| 169 | static struct list_head stateid_hashtbl[STATEID_HASH_SIZE]; |
| 170 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 171 | static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 172 | { |
| 173 | BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR])); |
| 174 | atomic_inc(&fp->fi_access[oflag]); |
| 175 | } |
| 176 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 177 | static void nfs4_file_get_access(struct nfs4_file *fp, int oflag) |
| 178 | { |
| 179 | if (oflag == O_RDWR) { |
| 180 | __nfs4_file_get_access(fp, O_RDONLY); |
| 181 | __nfs4_file_get_access(fp, O_WRONLY); |
| 182 | } else |
| 183 | __nfs4_file_get_access(fp, oflag); |
| 184 | } |
| 185 | |
| 186 | static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 187 | { |
| 188 | if (fp->fi_fds[oflag]) { |
| 189 | fput(fp->fi_fds[oflag]); |
| 190 | fp->fi_fds[oflag] = NULL; |
| 191 | } |
| 192 | } |
| 193 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 194 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 195 | { |
| 196 | if (atomic_dec_and_test(&fp->fi_access[oflag])) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 197 | nfs4_file_put_fd(fp, oflag); |
J. Bruce Fields | 3d02fa2 | 2011-09-19 15:07:41 -0400 | [diff] [blame^] | 198 | /* |
| 199 | * It's also safe to get rid of the RDWR open *if* |
| 200 | * we no longer have need of the other kind of access |
| 201 | * or if we already have the other kind of open: |
| 202 | */ |
| 203 | if (fp->fi_fds[1-oflag] |
| 204 | || atomic_read(&fp->fi_access[1 - oflag]) == 0) |
| 205 | nfs4_file_put_fd(fp, O_RDWR); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 209 | static void nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
| 210 | { |
| 211 | if (oflag == O_RDWR) { |
| 212 | __nfs4_file_put_access(fp, O_RDONLY); |
| 213 | __nfs4_file_put_access(fp, O_WRONLY); |
| 214 | } else |
| 215 | __nfs4_file_put_access(fp, oflag); |
| 216 | } |
| 217 | |
J. Bruce Fields | 36d44c6 | 2011-09-08 12:16:03 -0400 | [diff] [blame] | 218 | static inline void hash_stid(struct nfs4_stid *stid) |
| 219 | { |
| 220 | stateid_t *s = &stid->sc_stateid; |
| 221 | unsigned int hashval; |
| 222 | |
J. Bruce Fields | ee626a7 | 2011-09-11 13:48:41 -0400 | [diff] [blame] | 223 | hashval = stateid_hashval(s); |
J. Bruce Fields | 36d44c6 | 2011-09-08 12:16:03 -0400 | [diff] [blame] | 224 | list_add(&stid->sc_hash, &stateid_hashtbl[hashval]); |
| 225 | } |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | static struct nfs4_delegation * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 228 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
| 230 | struct nfs4_delegation *dp; |
| 231 | struct nfs4_file *fp = stp->st_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | dprintk("NFSD alloc_init_deleg\n"); |
J. Bruce Fields | c3e4808 | 2010-07-28 10:08:57 -0400 | [diff] [blame] | 234 | /* |
| 235 | * Major work on the lease subsystem (for example, to support |
| 236 | * calbacks on stat) will be required before we can support |
| 237 | * write delegations properly. |
| 238 | */ |
| 239 | if (type != NFS4_OPEN_DELEGATE_READ) |
| 240 | return NULL; |
Meelap Shah | 47f9940 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 241 | if (fp->fi_had_conflict) |
| 242 | return NULL; |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 243 | if (num_delegations > max_delegations) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 244 | return NULL; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 245 | dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL); |
| 246 | if (dp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return dp; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 248 | num_delegations++; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 249 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 250 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
| 252 | dp->dl_client = clp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 253 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | dp->dl_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | dp->dl_type = type; |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 256 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 257 | dp->dl_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid; |
| 258 | dp->dl_stid.sc_stateid.si_opaque.so_id = current_stateid++; |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 259 | dp->dl_stid.sc_stateid.si_generation = 1; |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 260 | hash_stid(&dp->dl_stid); |
J. Bruce Fields | 6c02eaa | 2009-02-02 17:30:51 -0500 | [diff] [blame] | 261 | fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | dp->dl_time = 0; |
| 263 | atomic_set(&dp->dl_count, 1); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 264 | INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return dp; |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | nfs4_put_delegation(struct nfs4_delegation *dp) |
| 270 | { |
| 271 | if (atomic_dec_and_test(&dp->dl_count)) { |
| 272 | dprintk("NFSD: freeing dp %p\n",dp); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 273 | put_nfs4_file(dp->dl_file); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 274 | kmem_cache_free(deleg_slab, dp); |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 275 | num_delegations--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 279 | static void nfs4_put_deleg_lease(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 281 | if (atomic_dec_and_test(&fp->fi_delegees)) { |
| 282 | vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease); |
| 283 | fp->fi_lease = NULL; |
J. Bruce Fields | 4ee6362 | 2011-04-15 18:08:26 -0400 | [diff] [blame] | 284 | fput(fp->fi_deleg_file); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 285 | fp->fi_deleg_file = NULL; |
| 286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* Called under the state lock. */ |
| 290 | static void |
| 291 | unhash_delegation(struct nfs4_delegation *dp) |
| 292 | { |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 293 | list_del_init(&dp->dl_stid.sc_hash); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 294 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | spin_lock(&recall_lock); |
J. Bruce Fields | 5d926e8 | 2011-02-07 16:53:46 -0500 | [diff] [blame] | 296 | list_del_init(&dp->dl_perfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | list_del_init(&dp->dl_recall_lru); |
| 298 | spin_unlock(&recall_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 299 | nfs4_put_deleg_lease(dp->dl_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | nfs4_put_delegation(dp); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * SETCLIENTID state |
| 305 | */ |
| 306 | |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 307 | /* client_lock protects the client lru list and session hash table */ |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 308 | static DEFINE_SPINLOCK(client_lock); |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* Hash tables for nfs4_clientid state */ |
| 311 | #define CLIENT_HASH_BITS 4 |
| 312 | #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS) |
| 313 | #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1) |
| 314 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 315 | static unsigned int clientid_hashval(u32 id) |
| 316 | { |
| 317 | return id & CLIENT_HASH_MASK; |
| 318 | } |
| 319 | |
| 320 | static unsigned int clientstr_hashval(const char *name) |
| 321 | { |
| 322 | return opaque_hashval(name, 8) & CLIENT_HASH_MASK; |
| 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* |
| 326 | * reclaim_str_hashtbl[] holds known client info from previous reset/reboot |
| 327 | * used in reboot/reset lease grace period processing |
| 328 | * |
| 329 | * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed |
| 330 | * setclientid_confirmed info. |
| 331 | * |
| 332 | * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed |
| 333 | * setclientid info. |
| 334 | * |
| 335 | * client_lru holds client queue ordered by nfs4_client.cl_time |
| 336 | * for lease renewal. |
| 337 | * |
| 338 | * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time |
| 339 | * for last close replay. |
| 340 | */ |
| 341 | static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE]; |
| 342 | static int reclaim_str_hashtbl_size = 0; |
| 343 | static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE]; |
| 344 | static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE]; |
| 345 | static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE]; |
| 346 | static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE]; |
| 347 | static struct list_head client_lru; |
| 348 | static struct list_head close_lru; |
| 349 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 350 | /* |
| 351 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 352 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 353 | * only what share bits are currently in force, but also what |
| 354 | * combinations of share bits previous opens have used. This allows us |
| 355 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 356 | * return an error if the client attempt to downgrade to a combination |
| 357 | * of share bits not explicable by closing some of its previous opens. |
| 358 | * |
| 359 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 360 | * track of access/deny bit combinations; so, e.g., we allow: |
| 361 | * |
| 362 | * OPEN allow read, deny write |
| 363 | * OPEN allow both, deny none |
| 364 | * DOWNGRADE allow read, deny none |
| 365 | * |
| 366 | * which we should reject. |
| 367 | */ |
| 368 | static void |
| 369 | set_access(unsigned int *access, unsigned long bmap) { |
| 370 | int i; |
| 371 | |
| 372 | *access = 0; |
| 373 | for (i = 1; i < 4; i++) { |
| 374 | if (test_bit(i, &bmap)) |
| 375 | *access |= i; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | static void |
| 380 | set_deny(unsigned int *deny, unsigned long bmap) { |
| 381 | int i; |
| 382 | |
| 383 | *deny = 0; |
| 384 | for (i = 0; i < 4; i++) { |
| 385 | if (test_bit(i, &bmap)) |
| 386 | *deny |= i ; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | static int |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 391 | test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 392 | unsigned int access, deny; |
| 393 | |
| 394 | set_access(&access, stp->st_access_bmap); |
| 395 | set_deny(&deny, stp->st_deny_bmap); |
| 396 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) |
| 397 | return 0; |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | static int nfs4_access_to_omode(u32 access) |
| 402 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 403 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 404 | case NFS4_SHARE_ACCESS_READ: |
| 405 | return O_RDONLY; |
| 406 | case NFS4_SHARE_ACCESS_WRITE: |
| 407 | return O_WRONLY; |
| 408 | case NFS4_SHARE_ACCESS_BOTH: |
| 409 | return O_RDWR; |
| 410 | } |
| 411 | BUG(); |
| 412 | } |
| 413 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 414 | static void unhash_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 415 | { |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 416 | list_del(&stp->st_perfile); |
| 417 | list_del(&stp->st_perstateowner); |
| 418 | } |
| 419 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 420 | static void close_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 421 | { |
J. Bruce Fields | 499f3ed | 2011-06-27 16:57:12 -0400 | [diff] [blame] | 422 | int i; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 423 | |
J. Bruce Fields | 23fcf2e | 2011-03-28 15:15:09 +0800 | [diff] [blame] | 424 | if (stp->st_access_bmap) { |
J. Bruce Fields | 499f3ed | 2011-06-27 16:57:12 -0400 | [diff] [blame] | 425 | for (i = 1; i < 4; i++) { |
| 426 | if (test_bit(i, &stp->st_access_bmap)) |
| 427 | nfs4_file_put_access(stp->st_file, |
| 428 | nfs4_access_to_omode(i)); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 429 | __clear_bit(i, &stp->st_access_bmap); |
J. Bruce Fields | 499f3ed | 2011-06-27 16:57:12 -0400 | [diff] [blame] | 430 | } |
J. Bruce Fields | 23fcf2e | 2011-03-28 15:15:09 +0800 | [diff] [blame] | 431 | } |
OGAWA Hirofumi | a96e5b9 | 2011-04-18 11:48:55 -0400 | [diff] [blame] | 432 | put_nfs4_file(stp->st_file); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 433 | stp->st_file = NULL; |
| 434 | } |
| 435 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 436 | static void free_generic_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 437 | { |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 438 | kmem_cache_free(stateid_slab, stp); |
| 439 | } |
| 440 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 441 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 442 | { |
| 443 | struct file *file; |
| 444 | |
| 445 | unhash_generic_stateid(stp); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 446 | list_del(&stp->st_stid.sc_hash); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 447 | file = find_any_file(stp->st_file); |
| 448 | if (file) |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 449 | locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner)); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 450 | close_generic_stateid(stp); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 451 | free_generic_stateid(stp); |
| 452 | } |
| 453 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 454 | static void unhash_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 455 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 456 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 457 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 458 | list_del(&lo->lo_owner.so_strhash); |
| 459 | list_del(&lo->lo_perstateid); |
| 460 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 461 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 462 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 463 | release_lock_stateid(stp); |
| 464 | } |
| 465 | } |
| 466 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 467 | static void release_lockowner(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 468 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 469 | unhash_lockowner(lo); |
| 470 | nfs4_free_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static void |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 474 | release_stateid_lockowners(struct nfs4_ol_stateid *open_stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 475 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 476 | struct nfs4_lockowner *lo; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 477 | |
| 478 | while (!list_empty(&open_stp->st_lockowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 479 | lo = list_entry(open_stp->st_lockowners.next, |
| 480 | struct nfs4_lockowner, lo_perstateid); |
| 481 | release_lockowner(lo); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 485 | static void unhash_open_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 486 | { |
| 487 | unhash_generic_stateid(stp); |
| 488 | release_stateid_lockowners(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 489 | close_generic_stateid(stp); |
| 490 | } |
| 491 | |
| 492 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 493 | { |
| 494 | unhash_open_stateid(stp); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 495 | list_del(&stp->st_stid.sc_hash); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 496 | free_generic_stateid(stp); |
| 497 | } |
| 498 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 499 | static void unhash_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 500 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 501 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 502 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 503 | list_del(&oo->oo_owner.so_strhash); |
| 504 | list_del(&oo->oo_perclient); |
| 505 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 506 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 507 | struct nfs4_ol_stateid, st_perstateowner); |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 508 | release_open_stateid(stp); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 512 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 513 | { |
| 514 | struct nfs4_ol_stateid *s = oo->oo_last_closed_stid; |
| 515 | |
| 516 | if (s) { |
| 517 | list_del_init(&s->st_stid.sc_hash); |
| 518 | free_generic_stateid(s); |
| 519 | oo->oo_last_closed_stid = NULL; |
| 520 | } |
| 521 | } |
| 522 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 523 | static void release_openowner(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 524 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 525 | unhash_openowner(oo); |
| 526 | list_del(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 527 | release_last_closed_stateid(oo); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 528 | nfs4_free_openowner(oo); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 531 | #define SESSION_HASH_SIZE 512 |
| 532 | static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE]; |
| 533 | |
| 534 | static inline int |
| 535 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 536 | { |
| 537 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 538 | |
| 539 | return sid->sequence % SESSION_HASH_SIZE; |
| 540 | } |
| 541 | |
| 542 | static inline void |
| 543 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 544 | { |
| 545 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 546 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 547 | } |
| 548 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 549 | static void |
| 550 | gen_sessionid(struct nfsd4_session *ses) |
| 551 | { |
| 552 | struct nfs4_client *clp = ses->se_client; |
| 553 | struct nfsd4_sessionid *sid; |
| 554 | |
| 555 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 556 | sid->clientid = clp->cl_clientid; |
| 557 | sid->sequence = current_sessionid++; |
| 558 | sid->reserved = 0; |
| 559 | } |
| 560 | |
| 561 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 562 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 563 | * the rpc header, but all we need to cache is the data starting after |
| 564 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 565 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 566 | * value that is the number of bytes in our cache plus a few additional |
| 567 | * bytes. In order to stay on the safe side, and not promise more than |
| 568 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 569 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 570 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 571 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 572 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 573 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 574 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 575 | static void |
| 576 | free_session_slots(struct nfsd4_session *ses) |
| 577 | { |
| 578 | int i; |
| 579 | |
| 580 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) |
| 581 | kfree(ses->se_slots[i]); |
| 582 | } |
| 583 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 584 | /* |
| 585 | * We don't actually need to cache the rpc and session headers, so we |
| 586 | * can allocate a little less for each slot: |
| 587 | */ |
| 588 | static inline int slot_bytes(struct nfsd4_channel_attrs *ca) |
| 589 | { |
| 590 | return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 591 | } |
| 592 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 593 | static int nfsd4_sanitize_slot_size(u32 size) |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 594 | { |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 595 | size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */ |
| 596 | size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 597 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 598 | return size; |
| 599 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 600 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 601 | /* |
| 602 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 603 | * re-negotiate active sessions and reduce their slot usage to make |
| 604 | * rooom for new connections. For now we just fail the create session. |
| 605 | */ |
| 606 | static int nfsd4_get_drc_mem(int slotsize, u32 num) |
| 607 | { |
| 608 | int avail; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 609 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 610 | num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 611 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 612 | spin_lock(&nfsd_drc_lock); |
| 613 | avail = min_t(int, NFSD_MAX_MEM_PER_SESSION, |
| 614 | nfsd_drc_max_mem - nfsd_drc_mem_used); |
| 615 | num = min_t(int, num, avail / slotsize); |
| 616 | nfsd_drc_mem_used += num * slotsize; |
| 617 | spin_unlock(&nfsd_drc_lock); |
| 618 | |
| 619 | return num; |
| 620 | } |
| 621 | |
| 622 | static void nfsd4_put_drc_mem(int slotsize, int num) |
| 623 | { |
| 624 | spin_lock(&nfsd_drc_lock); |
| 625 | nfsd_drc_mem_used -= slotsize * num; |
| 626 | spin_unlock(&nfsd_drc_lock); |
| 627 | } |
| 628 | |
| 629 | static struct nfsd4_session *alloc_session(int slotsize, int numslots) |
| 630 | { |
| 631 | struct nfsd4_session *new; |
| 632 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 633 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 634 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 635 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 636 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 637 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 638 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 639 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 640 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 641 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 642 | for (i = 0; i < numslots; i++) { |
| 643 | mem = sizeof(struct nfsd4_slot) + slotsize; |
| 644 | new->se_slots[i] = kzalloc(mem, GFP_KERNEL); |
| 645 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 646 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 647 | } |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 648 | return new; |
| 649 | out_free: |
| 650 | while (i--) |
| 651 | kfree(new->se_slots[i]); |
| 652 | kfree(new); |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize) |
| 657 | { |
| 658 | u32 maxrpc = nfsd_serv->sv_max_mesg; |
| 659 | |
| 660 | new->maxreqs = numslots; |
Mi Jinlong | d2b2174 | 2011-03-10 17:43:37 +0800 | [diff] [blame] | 661 | new->maxresp_cached = min_t(u32, req->maxresp_cached, |
| 662 | slotsize + NFSD_MIN_HDR_SEQ_SZ); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 663 | new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc); |
| 664 | new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc); |
| 665 | new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 666 | } |
| 667 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 668 | static void free_conn(struct nfsd4_conn *c) |
| 669 | { |
| 670 | svc_xprt_put(c->cn_xprt); |
| 671 | kfree(c); |
| 672 | } |
| 673 | |
| 674 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 675 | { |
| 676 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 677 | struct nfs4_client *clp = c->cn_session->se_client; |
| 678 | |
| 679 | spin_lock(&clp->cl_lock); |
| 680 | if (!list_empty(&c->cn_persession)) { |
| 681 | list_del(&c->cn_persession); |
| 682 | free_conn(c); |
| 683 | } |
| 684 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 685 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 686 | } |
| 687 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 688 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 689 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 690 | struct nfsd4_conn *conn; |
| 691 | |
| 692 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 693 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 694 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 695 | svc_xprt_get(rqstp->rq_xprt); |
| 696 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 697 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 698 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 699 | return conn; |
| 700 | } |
| 701 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 702 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 703 | { |
| 704 | conn->cn_session = ses; |
| 705 | list_add(&conn->cn_persession, &ses->se_conns); |
| 706 | } |
| 707 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 708 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 709 | { |
| 710 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 711 | |
| 712 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 713 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 714 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 715 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 716 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 717 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 718 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 719 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 720 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 721 | } |
| 722 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 723 | static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 724 | { |
| 725 | struct nfsd4_conn *conn; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 726 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 727 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 728 | conn = alloc_conn(rqstp, dir); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 729 | if (!conn) |
| 730 | return nfserr_jukebox; |
| 731 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 732 | ret = nfsd4_register_conn(conn); |
| 733 | if (ret) |
| 734 | /* oops; xprt is already down: */ |
| 735 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 736 | return nfs_ok; |
| 737 | } |
| 738 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 739 | static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses) |
| 740 | { |
| 741 | u32 dir = NFS4_CDFC4_FORE; |
| 742 | |
| 743 | if (ses->se_flags & SESSION4_BACK_CHAN) |
| 744 | dir |= NFS4_CDFC4_BACK; |
| 745 | |
| 746 | return nfsd4_new_conn(rqstp, ses, dir); |
| 747 | } |
| 748 | |
| 749 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 750 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 751 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 752 | struct nfs4_client *clp = s->se_client; |
| 753 | struct nfsd4_conn *c; |
| 754 | |
| 755 | spin_lock(&clp->cl_lock); |
| 756 | while (!list_empty(&s->se_conns)) { |
| 757 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 758 | list_del_init(&c->cn_persession); |
| 759 | spin_unlock(&clp->cl_lock); |
| 760 | |
| 761 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 762 | free_conn(c); |
| 763 | |
| 764 | spin_lock(&clp->cl_lock); |
| 765 | } |
| 766 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void free_session(struct kref *kref) |
| 770 | { |
| 771 | struct nfsd4_session *ses; |
| 772 | int mem; |
| 773 | |
| 774 | ses = container_of(kref, struct nfsd4_session, se_ref); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 775 | nfsd4_del_conns(ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 776 | spin_lock(&nfsd_drc_lock); |
| 777 | mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel); |
| 778 | nfsd_drc_mem_used -= mem; |
| 779 | spin_unlock(&nfsd_drc_lock); |
| 780 | free_session_slots(ses); |
| 781 | kfree(ses); |
| 782 | } |
| 783 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 784 | static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 785 | { |
| 786 | struct nfsd4_session *new; |
| 787 | struct nfsd4_channel_attrs *fchan = &cses->fore_channel; |
| 788 | int numslots, slotsize; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 789 | int status; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 790 | int idx; |
| 791 | |
| 792 | /* |
| 793 | * Note decreasing slot size below client's request may |
| 794 | * make it difficult for client to function correctly, whereas |
| 795 | * decreasing the number of slots will (just?) affect |
| 796 | * performance. When short on memory we therefore prefer to |
| 797 | * decrease number of slots instead of their size. |
| 798 | */ |
| 799 | slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached); |
| 800 | numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs); |
Mi Jinlong | ced6dfe | 2010-11-11 18:03:50 +0800 | [diff] [blame] | 801 | if (numslots < 1) |
| 802 | return NULL; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 803 | |
| 804 | new = alloc_session(slotsize, numslots); |
| 805 | if (!new) { |
| 806 | nfsd4_put_drc_mem(slotsize, fchan->maxreqs); |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 807 | return NULL; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 808 | } |
| 809 | init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 810 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 811 | new->se_client = clp; |
| 812 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 813 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 814 | INIT_LIST_HEAD(&new->se_conns); |
| 815 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 816 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 817 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 818 | new->se_cb_prog = cses->callback_prog; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 819 | kref_init(&new->se_ref); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 820 | idx = hash_sessionid(&new->se_sessionid); |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 821 | spin_lock(&client_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 822 | list_add(&new->se_hash, &sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 823 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 824 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 825 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 826 | spin_unlock(&client_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 827 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 828 | status = nfsd4_new_conn_from_crses(rqstp, new); |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 829 | /* whoops: benny points out, status is ignored! (err, or bogus) */ |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 830 | if (status) { |
| 831 | free_session(&new->se_ref); |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 832 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 833 | } |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 834 | if (cses->flags & SESSION4_BACK_CHAN) { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 835 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 836 | /* |
| 837 | * This is a little silly; with sessions there's no real |
| 838 | * use for the callback address. Use the peer address |
| 839 | * as a reasonable default for now, but consider fixing |
| 840 | * the rpc client not to require an address in the |
| 841 | * future: |
| 842 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 843 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 844 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 845 | } |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 846 | nfsd4_probe_callback(clp); |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 847 | return new; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 848 | } |
| 849 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 850 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 851 | static struct nfsd4_session * |
| 852 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid) |
| 853 | { |
| 854 | struct nfsd4_session *elem; |
| 855 | int idx; |
| 856 | |
| 857 | dump_sessionid(__func__, sessionid); |
| 858 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 859 | /* Search in the appropriate list */ |
| 860 | list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 861 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 862 | NFS4_MAX_SESSIONID_LEN)) { |
| 863 | return elem; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | dprintk("%s: session not found\n", __func__); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 871 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 872 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 873 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 874 | { |
| 875 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 876 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 877 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 878 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 879 | } |
| 880 | |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 881 | /* must be called under the client_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | static inline void |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 883 | renew_client_locked(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | { |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 885 | if (is_client_expired(clp)) { |
| 886 | dprintk("%s: client (clientid %08x/%08x) already expired\n", |
| 887 | __func__, |
| 888 | clp->cl_clientid.cl_boot, |
| 889 | clp->cl_clientid.cl_id); |
| 890 | return; |
| 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | /* |
| 894 | * Move client to the end to the LRU list. |
| 895 | */ |
| 896 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 897 | clp->cl_clientid.cl_boot, |
| 898 | clp->cl_clientid.cl_id); |
| 899 | list_move_tail(&clp->cl_lru, &client_lru); |
| 900 | clp->cl_time = get_seconds(); |
| 901 | } |
| 902 | |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 903 | static inline void |
| 904 | renew_client(struct nfs4_client *clp) |
| 905 | { |
| 906 | spin_lock(&client_lock); |
| 907 | renew_client_locked(clp); |
| 908 | spin_unlock(&client_lock); |
| 909 | } |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 912 | static int |
| 913 | STALE_CLIENTID(clientid_t *clid) |
| 914 | { |
| 915 | if (clid->cl_boot == boot_time) |
| 916 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 917 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
| 918 | clid->cl_boot, clid->cl_id, boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * XXX Should we use a slab cache ? |
| 924 | * This type of memory management is somewhat inefficient, but we use it |
| 925 | * anyway since SETCLIENTID is not a common operation. |
| 926 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 927 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | struct nfs4_client *clp; |
| 930 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 931 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 932 | if (clp == NULL) |
| 933 | return NULL; |
| 934 | clp->cl_name.data = kmalloc(name.len, GFP_KERNEL); |
| 935 | if (clp->cl_name.data == NULL) { |
| 936 | kfree(clp); |
| 937 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 939 | memcpy(clp->cl_name.data, name.data, name.len); |
| 940 | clp->cl_name.len = name.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return clp; |
| 942 | } |
| 943 | |
| 944 | static inline void |
| 945 | free_client(struct nfs4_client *clp) |
| 946 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 947 | while (!list_empty(&clp->cl_sessions)) { |
| 948 | struct nfsd4_session *ses; |
| 949 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 950 | se_perclnt); |
| 951 | list_del(&ses->se_perclnt); |
| 952 | nfsd4_put_session(ses); |
| 953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (clp->cl_cred.cr_group_info) |
| 955 | put_group_info(clp->cl_cred.cr_group_info); |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 956 | kfree(clp->cl_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | kfree(clp->cl_name.data); |
| 958 | kfree(clp); |
| 959 | } |
| 960 | |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 961 | void |
| 962 | release_session_client(struct nfsd4_session *session) |
| 963 | { |
| 964 | struct nfs4_client *clp = session->se_client; |
| 965 | |
| 966 | if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock)) |
| 967 | return; |
| 968 | if (is_client_expired(clp)) { |
| 969 | free_client(clp); |
| 970 | session->se_client = NULL; |
| 971 | } else |
| 972 | renew_client_locked(clp); |
| 973 | spin_unlock(&client_lock); |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 974 | } |
| 975 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 976 | /* must be called under the client_lock */ |
| 977 | static inline void |
| 978 | unhash_client_locked(struct nfs4_client *clp) |
| 979 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 980 | struct nfsd4_session *ses; |
| 981 | |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 982 | mark_client_expired(clp); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 983 | list_del(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 984 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 985 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 986 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 987 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 988 | } |
| 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | static void |
| 991 | expire_client(struct nfs4_client *clp) |
| 992 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 993 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | struct list_head reaplist; |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | INIT_LIST_HEAD(&reaplist); |
| 998 | spin_lock(&recall_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 999 | while (!list_empty(&clp->cl_delegations)) { |
| 1000 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1001 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | list_move(&dp->dl_recall_lru, &reaplist); |
| 1003 | } |
| 1004 | spin_unlock(&recall_lock); |
| 1005 | while (!list_empty(&reaplist)) { |
| 1006 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
| 1007 | list_del_init(&dp->dl_recall_lru); |
| 1008 | unhash_delegation(dp); |
| 1009 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1010 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 1011 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
| 1012 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1014 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 1015 | if (clp->cl_cb_conn.cb_xprt) |
| 1016 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1017 | list_del(&clp->cl_idhash); |
| 1018 | list_del(&clp->cl_strhash); |
| 1019 | spin_lock(&client_lock); |
| 1020 | unhash_client_locked(clp); |
Benny Halevy | 46583e2 | 2010-05-12 00:13:29 +0300 | [diff] [blame] | 1021 | if (atomic_read(&clp->cl_refcount) == 0) |
| 1022 | free_client(clp); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 1023 | spin_unlock(&client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1026 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 1027 | { |
| 1028 | memcpy(target->cl_verifier.data, source->data, |
| 1029 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1032 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 1033 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 1035 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 1036 | } |
| 1037 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1038 | static void copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 1039 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | target->cr_uid = source->cr_uid; |
| 1041 | target->cr_gid = source->cr_gid; |
| 1042 | target->cr_group_info = source->cr_group_info; |
| 1043 | get_group_info(target->cr_group_info); |
| 1044 | } |
| 1045 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1046 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1047 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1048 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1052 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 1053 | { |
| 1054 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1058 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 1059 | { |
| 1060 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /* XXX what about NGROUP */ |
| 1064 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1065 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 1066 | { |
| 1067 | return cr1->cr_uid == cr2->cr_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
J. Bruce Fields | 5ec7b46 | 2007-11-21 21:58:56 -0500 | [diff] [blame] | 1070 | static void gen_clid(struct nfs4_client *clp) |
| 1071 | { |
| 1072 | static u32 current_clientid = 1; |
| 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | clp->cl_clientid.cl_boot = boot_time; |
| 1075 | clp->cl_clientid.cl_id = current_clientid++; |
| 1076 | } |
| 1077 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1078 | static void gen_confirm(struct nfs4_client *clp) |
| 1079 | { |
| 1080 | static u32 i; |
| 1081 | u32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | p = (u32 *)clp->cl_confirm.data; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 1084 | *p++ = get_seconds(); |
| 1085 | *p++ = i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1088 | static int |
| 1089 | same_stateid(stateid_t *id_one, stateid_t *id_two) |
| 1090 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 1091 | return 0 == memcmp(&id_one->si_opaque, &id_two->si_opaque, |
| 1092 | sizeof(stateid_opaque_t)); |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1093 | } |
| 1094 | |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1095 | static struct nfs4_stid *find_stateid(stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1096 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 1097 | struct nfs4_stid *s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1098 | unsigned int hashval; |
| 1099 | |
J. Bruce Fields | ee626a7 | 2011-09-11 13:48:41 -0400 | [diff] [blame] | 1100 | hashval = stateid_hashval(t); |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 1101 | list_for_each_entry(s, &stateid_hashtbl[hashval], sc_hash) |
| 1102 | if (same_stateid(&s->sc_stateid, t)) |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1103 | return s; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1104 | return NULL; |
| 1105 | } |
| 1106 | |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1107 | static struct nfs4_stid *find_stateid_by_type(stateid_t *t, char typemask) |
| 1108 | { |
| 1109 | struct nfs4_stid *s; |
| 1110 | |
| 1111 | s = find_stateid(t); |
| 1112 | if (!s) |
| 1113 | return NULL; |
| 1114 | if (typemask & s->sc_type) |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 1115 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 1116 | return NULL; |
| 1117 | } |
| 1118 | |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 1119 | static struct nfs4_ol_stateid *find_ol_stateid_by_type(stateid_t *t, char typemask) |
| 1120 | { |
| 1121 | struct nfs4_stid *s; |
| 1122 | |
| 1123 | s = find_stateid_by_type(t, typemask); |
| 1124 | if (!s) |
| 1125 | return NULL; |
| 1126 | return openlockstateid(s); |
| 1127 | } |
| 1128 | |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1129 | static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir, |
| 1130 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 1131 | { |
| 1132 | struct nfs4_client *clp; |
| 1133 | struct sockaddr *sa = svc_addr(rqstp); |
| 1134 | char *princ; |
| 1135 | |
| 1136 | clp = alloc_client(name); |
| 1137 | if (clp == NULL) |
| 1138 | return NULL; |
| 1139 | |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1140 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1141 | |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1142 | princ = svc_gss_principal(rqstp); |
| 1143 | if (princ) { |
| 1144 | clp->cl_principal = kstrdup(princ, GFP_KERNEL); |
| 1145 | if (clp->cl_principal == NULL) { |
| 1146 | free_client(clp); |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | memcpy(clp->cl_recdir, recdir, HEXDIR_LEN); |
Benny Halevy | 46583e2 | 2010-05-12 00:13:29 +0300 | [diff] [blame] | 1152 | atomic_set(&clp->cl_refcount, 0); |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 1153 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1154 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1155 | INIT_LIST_HEAD(&clp->cl_strhash); |
| 1156 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1157 | INIT_LIST_HEAD(&clp->cl_delegations); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1158 | INIT_LIST_HEAD(&clp->cl_lru); |
J. Bruce Fields | 5ce8ba2 | 2011-01-10 16:44:41 -0500 | [diff] [blame] | 1159 | INIT_LIST_HEAD(&clp->cl_callbacks); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 1160 | spin_lock_init(&clp->cl_lock); |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 1161 | INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 1162 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1163 | clear_bit(0, &clp->cl_cb_slot_busy); |
| 1164 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
| 1165 | copy_verf(clp, verf); |
| 1166 | rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa); |
| 1167 | clp->cl_flavor = rqstp->rq_flavor; |
| 1168 | copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 1169 | gen_confirm(clp); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1170 | clp->cl_cb_session = NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1171 | return clp; |
| 1172 | } |
| 1173 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1174 | static int check_name(struct xdr_netobj name) |
| 1175 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | if (name.len == 0) |
| 1177 | return 0; |
| 1178 | if (name.len > NFS4_OPAQUE_LIMIT) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 1179 | dprintk("NFSD: check_name: name too long(%d)!\n", name.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | return 1; |
| 1183 | } |
| 1184 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1185 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval) |
| 1187 | { |
| 1188 | unsigned int idhashval; |
| 1189 | |
| 1190 | list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]); |
| 1191 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 1192 | list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1193 | renew_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1196 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | move_to_confirmed(struct nfs4_client *clp) |
| 1198 | { |
| 1199 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 1200 | unsigned int strhashval; |
| 1201 | |
| 1202 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 1203 | list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1204 | strhashval = clientstr_hashval(clp->cl_recdir); |
Benny Halevy | 328efba | 2010-05-12 00:12:51 +0300 | [diff] [blame] | 1205 | list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | renew_client(clp); |
| 1207 | } |
| 1208 | |
| 1209 | static struct nfs4_client * |
| 1210 | find_confirmed_client(clientid_t *clid) |
| 1211 | { |
| 1212 | struct nfs4_client *clp; |
| 1213 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1214 | |
| 1215 | list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1216 | if (same_clid(&clp->cl_clientid, clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | return clp; |
| 1218 | } |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | |
| 1222 | static struct nfs4_client * |
| 1223 | find_unconfirmed_client(clientid_t *clid) |
| 1224 | { |
| 1225 | struct nfs4_client *clp; |
| 1226 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 1227 | |
| 1228 | list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1229 | if (same_clid(&clp->cl_clientid, clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | return clp; |
| 1231 | } |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1235 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1236 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 1237 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1238 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 1239 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1240 | static struct nfs4_client * |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1241 | find_confirmed_client_by_str(const char *dname, unsigned int hashval) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1242 | { |
| 1243 | struct nfs4_client *clp; |
| 1244 | |
| 1245 | list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) { |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1246 | if (same_name(clp->cl_recdir, dname)) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1247 | return clp; |
| 1248 | } |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | |
| 1252 | static struct nfs4_client * |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1253 | find_unconfirmed_client_by_str(const char *dname, unsigned int hashval) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1254 | { |
| 1255 | struct nfs4_client *clp; |
| 1256 | |
| 1257 | list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) { |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1258 | if (same_name(clp->cl_recdir, dname)) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1259 | return clp; |
| 1260 | } |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1264 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1265 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1267 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 1268 | struct sockaddr *sa = svc_addr(rqstp); |
| 1269 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1270 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 1272 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 1273 | if (se->se_callback_netid_len == 3 && |
| 1274 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 1275 | expected_family = AF_INET; |
| 1276 | else if (se->se_callback_netid_len == 4 && |
| 1277 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 1278 | expected_family = AF_INET6; |
| 1279 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | goto out_err; |
| 1281 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1282 | conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1283 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1284 | (struct sockaddr *)&conn->cb_addr, |
| 1285 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1286 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1287 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 1289 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1290 | if (conn->cb_addr.ss_family == AF_INET6) |
| 1291 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 1292 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1293 | conn->cb_prog = se->se_callback_prog; |
| 1294 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 1295 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | return; |
| 1297 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 1298 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 1299 | conn->cb_addrlen = 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1300 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | "will not receive delegations\n", |
| 1302 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | return; |
| 1305 | } |
| 1306 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1307 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1308 | * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1309 | */ |
| 1310 | void |
| 1311 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 1312 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1313 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 1314 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1315 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1316 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1317 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1318 | slot->sl_opcnt = resp->opcnt; |
| 1319 | slot->sl_status = resp->cstate.status; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1320 | |
| 1321 | if (nfsd4_not_cached(resp)) { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1322 | slot->sl_datalen = 0; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1323 | return; |
| 1324 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1325 | slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap; |
| 1326 | base = (char *)resp->cstate.datap - |
| 1327 | (char *)resp->xbuf->head[0].iov_base; |
| 1328 | if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data, |
| 1329 | slot->sl_datalen)) |
| 1330 | WARN("%s: sessions DRC could not cache compound\n", __func__); |
| 1331 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1335 | * Encode the replay sequence operation from the slot values. |
| 1336 | * If cachethis is FALSE encode the uncached rep error on the next |
| 1337 | * operation which sets resp->p and increments resp->opcnt for |
| 1338 | * nfs4svc_encode_compoundres. |
| 1339 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1340 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1341 | static __be32 |
| 1342 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 1343 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1344 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1345 | struct nfsd4_op *op; |
| 1346 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1347 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1348 | dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__, |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1349 | resp->opcnt, resp->cstate.slot->sl_cachethis); |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1350 | |
| 1351 | /* Encode the replayed sequence operation */ |
| 1352 | op = &args->ops[resp->opcnt - 1]; |
| 1353 | nfsd4_encode_operation(resp, op); |
| 1354 | |
| 1355 | /* Return nfserr_retry_uncached_rep in next operation. */ |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1356 | if (args->opcnt > 1 && slot->sl_cachethis == 0) { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1357 | op = &args->ops[resp->opcnt++]; |
| 1358 | op->status = nfserr_retry_uncached_rep; |
| 1359 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1360 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1361 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1365 | * The sequence operation is not cached because we can use the slot and |
| 1366 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1367 | */ |
| 1368 | __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1369 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 1370 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1371 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1372 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1373 | __be32 status; |
| 1374 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1375 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1376 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 1377 | /* Either returns 0 or nfserr_retry_uncached */ |
| 1378 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
| 1379 | if (status == nfserr_retry_uncached_rep) |
| 1380 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1381 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1382 | /* The sequence operation has been encoded, cstate->datap set. */ |
| 1383 | memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1384 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1385 | resp->opcnt = slot->sl_opcnt; |
| 1386 | resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen); |
| 1387 | status = slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 1388 | |
| 1389 | return status; |
| 1390 | } |
| 1391 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1392 | /* |
| 1393 | * Set the exchange_id flags returned by the server. |
| 1394 | */ |
| 1395 | static void |
| 1396 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 1397 | { |
| 1398 | /* pNFS is not supported */ |
| 1399 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
| 1400 | |
| 1401 | /* Referrals are supported, Migration is not. */ |
| 1402 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 1403 | |
| 1404 | /* set the wire flags to return to client. */ |
| 1405 | clid->flags = new->cl_exchange_flags; |
| 1406 | } |
| 1407 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1408 | __be32 |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1409 | nfsd4_exchange_id(struct svc_rqst *rqstp, |
| 1410 | struct nfsd4_compound_state *cstate, |
| 1411 | struct nfsd4_exchange_id *exid) |
| 1412 | { |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1413 | struct nfs4_client *unconf, *conf, *new; |
| 1414 | int status; |
| 1415 | unsigned int strhashval; |
| 1416 | char dname[HEXDIR_LEN]; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1417 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1418 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1419 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1420 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1421 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1422 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1423 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1424 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1425 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1426 | |
| 1427 | if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A)) |
| 1428 | return nfserr_inval; |
| 1429 | |
| 1430 | /* Currently only support SP4_NONE */ |
| 1431 | switch (exid->spa_how) { |
| 1432 | case SP4_NONE: |
| 1433 | break; |
| 1434 | case SP4_SSV: |
J. Bruce Fields | 044bc1d | 2010-11-12 14:36:06 -0500 | [diff] [blame] | 1435 | return nfserr_serverfault; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1436 | default: |
| 1437 | BUG(); /* checked by xdr code */ |
| 1438 | case SP4_MACH_CRED: |
| 1439 | return nfserr_serverfault; /* no excuse :-/ */ |
| 1440 | } |
| 1441 | |
| 1442 | status = nfs4_make_rec_clidname(dname, &exid->clname); |
| 1443 | |
| 1444 | if (status) |
| 1445 | goto error; |
| 1446 | |
| 1447 | strhashval = clientstr_hashval(dname); |
| 1448 | |
| 1449 | nfs4_lock_state(); |
| 1450 | status = nfs_ok; |
| 1451 | |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1452 | conf = find_confirmed_client_by_str(dname, strhashval); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1453 | if (conf) { |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1454 | if (!clp_used_exchangeid(conf)) { |
| 1455 | status = nfserr_clid_inuse; /* XXX: ? */ |
| 1456 | goto out; |
| 1457 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1458 | if (!same_verf(&verf, &conf->cl_verifier)) { |
| 1459 | /* 18.35.4 case 8 */ |
| 1460 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { |
| 1461 | status = nfserr_not_same; |
| 1462 | goto out; |
| 1463 | } |
| 1464 | /* Client reboot: destroy old state */ |
| 1465 | expire_client(conf); |
| 1466 | goto out_new; |
| 1467 | } |
| 1468 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
| 1469 | /* 18.35.4 case 9 */ |
| 1470 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { |
| 1471 | status = nfserr_perm; |
| 1472 | goto out; |
| 1473 | } |
| 1474 | expire_client(conf); |
| 1475 | goto out_new; |
| 1476 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1477 | /* |
| 1478 | * Set bit when the owner id and verifier map to an already |
| 1479 | * confirmed client id (18.35.3). |
| 1480 | */ |
| 1481 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
| 1482 | |
| 1483 | /* |
| 1484 | * Falling into 18.35.4 case 2, possible router replay. |
| 1485 | * Leave confirmed record intact and return same result. |
| 1486 | */ |
| 1487 | copy_verf(conf, &verf); |
| 1488 | new = conf; |
| 1489 | goto out_copy; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* 18.35.4 case 7 */ |
| 1493 | if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) { |
| 1494 | status = nfserr_noent; |
| 1495 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1496 | } |
| 1497 | |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 1498 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1499 | if (unconf) { |
| 1500 | /* |
| 1501 | * Possible retry or client restart. Per 18.35.4 case 4, |
| 1502 | * a new unconfirmed record should be generated regardless |
| 1503 | * of whether any properties have changed. |
| 1504 | */ |
| 1505 | expire_client(unconf); |
| 1506 | } |
| 1507 | |
| 1508 | out_new: |
| 1509 | /* Normal case */ |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 1510 | new = create_client(exid->clname, dname, rqstp, &verf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1511 | if (new == NULL) { |
J. Bruce Fields | 4731030 | 2010-06-22 16:17:12 -0400 | [diff] [blame] | 1512 | status = nfserr_jukebox; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1513 | goto out; |
| 1514 | } |
| 1515 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1516 | gen_clid(new); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1517 | add_to_unconfirmed(new, strhashval); |
| 1518 | out_copy: |
| 1519 | exid->clientid.cl_boot = new->cl_clientid.cl_boot; |
| 1520 | exid->clientid.cl_id = new->cl_clientid.cl_id; |
| 1521 | |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 1522 | exid->seqid = 1; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1523 | nfsd4_set_ex_flags(new, exid); |
| 1524 | |
| 1525 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1526 | new->cl_cs_slot.sl_seqid, new->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 1527 | status = nfs_ok; |
| 1528 | |
| 1529 | out: |
| 1530 | nfs4_unlock_state(); |
| 1531 | error: |
| 1532 | dprintk("nfsd4_exchange_id returns %d\n", ntohl(status)); |
| 1533 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1534 | } |
| 1535 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1536 | static int |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1537 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1538 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1539 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 1540 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1541 | |
| 1542 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1543 | if (slot_inuse) { |
| 1544 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1545 | return nfserr_jukebox; |
| 1546 | else |
| 1547 | return nfserr_seq_misordered; |
| 1548 | } |
| 1549 | /* Normal */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1550 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1551 | return nfs_ok; |
| 1552 | /* Replay */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1553 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1554 | return nfserr_replay_cache; |
| 1555 | /* Wraparound */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1556 | if (seqid == 1 && (slot_seqid + 1) == 0) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1557 | return nfs_ok; |
| 1558 | /* Misordered replay or misordered new request */ |
| 1559 | return nfserr_seq_misordered; |
| 1560 | } |
| 1561 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1562 | /* |
| 1563 | * Cache the create session result into the create session single DRC |
| 1564 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 1565 | * Do this for solo or embedded create session operations. |
| 1566 | */ |
| 1567 | static void |
| 1568 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
| 1569 | struct nfsd4_clid_slot *slot, int nfserr) |
| 1570 | { |
| 1571 | slot->sl_status = nfserr; |
| 1572 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 1573 | } |
| 1574 | |
| 1575 | static __be32 |
| 1576 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 1577 | struct nfsd4_clid_slot *slot) |
| 1578 | { |
| 1579 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 1580 | return slot->sl_status; |
| 1581 | } |
| 1582 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 1583 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 1584 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 1585 | 1 + /* MIN tag is length with zero, only length */ \ |
| 1586 | 3 + /* version, opcount, opcode */ \ |
| 1587 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 1588 | /* seqid, slotID, slotID, cache */ \ |
| 1589 | 4 ) * sizeof(__be32)) |
| 1590 | |
| 1591 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 1592 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 1593 | 1 + /* status */ \ |
| 1594 | 1 + /* MIN tag is length with zero, only length */ \ |
| 1595 | 3 + /* opcount, opcode, opstatus*/ \ |
| 1596 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 1597 | /* seqid, slotID, slotID, slotID, status */ \ |
| 1598 | 5 ) * sizeof(__be32)) |
| 1599 | |
| 1600 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel) |
| 1601 | { |
| 1602 | return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ |
| 1603 | || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ; |
| 1604 | } |
| 1605 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1606 | __be32 |
| 1607 | nfsd4_create_session(struct svc_rqst *rqstp, |
| 1608 | struct nfsd4_compound_state *cstate, |
| 1609 | struct nfsd4_create_session *cr_ses) |
| 1610 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1611 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1612 | struct nfs4_client *conf, *unconf; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1613 | struct nfsd4_session *new; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1614 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 1615 | bool confirm_me = false; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1616 | int status = 0; |
| 1617 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 1618 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 1619 | return nfserr_inval; |
| 1620 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1621 | nfs4_lock_state(); |
| 1622 | unconf = find_unconfirmed_client(&cr_ses->clientid); |
| 1623 | conf = find_confirmed_client(&cr_ses->clientid); |
| 1624 | |
| 1625 | if (conf) { |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1626 | cs_slot = &conf->cl_cs_slot; |
| 1627 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 1628 | if (status == nfserr_replay_cache) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1629 | dprintk("Got a create_session replay! seqid= %d\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1630 | cs_slot->sl_seqid); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 1631 | /* Return the cached reply status */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1632 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 1633 | goto out; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1634 | } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1635 | status = nfserr_seq_misordered; |
| 1636 | dprintk("Sequence misordered!\n"); |
| 1637 | dprintk("Expected seqid= %d but got seqid= %d\n", |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1638 | cs_slot->sl_seqid, cr_ses->seqid); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1639 | goto out; |
| 1640 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1641 | } else if (unconf) { |
| 1642 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 1643 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1644 | status = nfserr_clid_inuse; |
| 1645 | goto out; |
| 1646 | } |
| 1647 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1648 | cs_slot = &unconf->cl_cs_slot; |
| 1649 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 1650 | if (status) { |
| 1651 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1652 | status = nfserr_seq_misordered; |
J. Bruce Fields | cd5b814 | 2010-10-02 17:03:35 -0400 | [diff] [blame] | 1653 | goto out; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1654 | } |
| 1655 | |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 1656 | confirm_me = true; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1657 | conf = unconf; |
| 1658 | } else { |
| 1659 | status = nfserr_stale_clientid; |
| 1660 | goto out; |
| 1661 | } |
| 1662 | |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 1663 | /* |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 1664 | * XXX: we should probably set this at creation time, and check |
| 1665 | * for consistent minorversion use throughout: |
| 1666 | */ |
| 1667 | conf->cl_minorversion = 1; |
| 1668 | /* |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 1669 | * We do not support RDMA or persistent sessions |
| 1670 | */ |
| 1671 | cr_ses->flags &= ~SESSION4_PERSIST; |
| 1672 | cr_ses->flags &= ~SESSION4_RDMA; |
| 1673 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 1674 | status = nfserr_toosmall; |
| 1675 | if (check_forechannel_attrs(cr_ses->fore_channel)) |
| 1676 | goto out; |
| 1677 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1678 | status = nfserr_jukebox; |
| 1679 | new = alloc_init_session(rqstp, conf, cr_ses); |
| 1680 | if (!new) |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1681 | goto out; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1682 | status = nfs_ok; |
| 1683 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1684 | NFS4_MAX_SESSIONID_LEN); |
Mi Jinlong | 1205065 | 2010-11-11 18:03:40 +0800 | [diff] [blame] | 1685 | memcpy(&cr_ses->fore_channel, &new->se_fchannel, |
| 1686 | sizeof(struct nfsd4_channel_attrs)); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 1687 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1688 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1689 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 1690 | /* cache solo and embedded create sessions under the state lock */ |
| 1691 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 1692 | if (confirm_me) |
| 1693 | move_to_confirmed(conf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1694 | out: |
| 1695 | nfs4_unlock_state(); |
| 1696 | dprintk("%s returns %d\n", __func__, ntohl(status)); |
| 1697 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1698 | } |
| 1699 | |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 1700 | static bool nfsd4_last_compound_op(struct svc_rqst *rqstp) |
| 1701 | { |
| 1702 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
| 1703 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 1704 | |
| 1705 | return argp->opcnt == resp->opcnt; |
| 1706 | } |
| 1707 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1708 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 1709 | { |
| 1710 | switch (*dir) { |
| 1711 | case NFS4_CDFC4_FORE: |
| 1712 | case NFS4_CDFC4_BACK: |
| 1713 | return nfs_ok; |
| 1714 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 1715 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 1716 | *dir = NFS4_CDFC4_BOTH; |
| 1717 | return nfs_ok; |
| 1718 | }; |
| 1719 | return nfserr_inval; |
| 1720 | } |
| 1721 | |
| 1722 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 1723 | struct nfsd4_compound_state *cstate, |
| 1724 | struct nfsd4_bind_conn_to_session *bcts) |
| 1725 | { |
| 1726 | __be32 status; |
| 1727 | |
| 1728 | if (!nfsd4_last_compound_op(rqstp)) |
| 1729 | return nfserr_not_only_op; |
| 1730 | spin_lock(&client_lock); |
| 1731 | cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid); |
| 1732 | /* Sorta weird: we only need the refcnt'ing because new_conn acquires |
| 1733 | * client_lock iself: */ |
| 1734 | if (cstate->session) { |
| 1735 | nfsd4_get_session(cstate->session); |
| 1736 | atomic_inc(&cstate->session->se_client->cl_refcount); |
| 1737 | } |
| 1738 | spin_unlock(&client_lock); |
| 1739 | if (!cstate->session) |
| 1740 | return nfserr_badsession; |
| 1741 | |
| 1742 | status = nfsd4_map_bcts_dir(&bcts->dir); |
Bryan Schumaker | 1db2b9d | 2011-04-27 15:47:15 -0400 | [diff] [blame] | 1743 | if (!status) |
| 1744 | nfsd4_new_conn(rqstp, cstate->session, bcts->dir); |
| 1745 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1746 | } |
| 1747 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 1748 | static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid) |
| 1749 | { |
| 1750 | if (!session) |
| 1751 | return 0; |
| 1752 | return !memcmp(sid, &session->se_sessionid, sizeof(*sid)); |
| 1753 | } |
| 1754 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1755 | __be32 |
| 1756 | nfsd4_destroy_session(struct svc_rqst *r, |
| 1757 | struct nfsd4_compound_state *cstate, |
| 1758 | struct nfsd4_destroy_session *sessionid) |
| 1759 | { |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1760 | struct nfsd4_session *ses; |
| 1761 | u32 status = nfserr_badsession; |
| 1762 | |
| 1763 | /* Notes: |
| 1764 | * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid |
| 1765 | * - Should we return nfserr_back_chan_busy if waiting for |
| 1766 | * callbacks on to-be-destroyed session? |
| 1767 | * - Do we need to clear any callback info from previous session? |
| 1768 | */ |
| 1769 | |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 1770 | if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 1771 | if (!nfsd4_last_compound_op(r)) |
| 1772 | return nfserr_not_only_op; |
| 1773 | } |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1774 | dump_sessionid(__func__, &sessionid->sessionid); |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1775 | spin_lock(&client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1776 | ses = find_in_sessionid_hashtbl(&sessionid->sessionid); |
| 1777 | if (!ses) { |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1778 | spin_unlock(&client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1779 | goto out; |
| 1780 | } |
| 1781 | |
| 1782 | unhash_session(ses); |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1783 | spin_unlock(&client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1784 | |
Benny Halevy | ab707e15 | 2010-05-12 00:14:06 +0300 | [diff] [blame] | 1785 | nfs4_lock_state(); |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 1786 | nfsd4_probe_callback_sync(ses->se_client); |
Benny Halevy | ab707e15 | 2010-05-12 00:14:06 +0300 | [diff] [blame] | 1787 | nfs4_unlock_state(); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1788 | |
| 1789 | nfsd4_del_conns(ses); |
| 1790 | |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 1791 | nfsd4_put_session(ses); |
| 1792 | status = nfs_ok; |
| 1793 | out: |
| 1794 | dprintk("%s returns %d\n", __func__, ntohl(status)); |
| 1795 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1796 | } |
| 1797 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1798 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1799 | { |
| 1800 | struct nfsd4_conn *c; |
| 1801 | |
| 1802 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1803 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1804 | return c; |
| 1805 | } |
| 1806 | } |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1810 | static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1811 | { |
| 1812 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1813 | struct nfsd4_conn *c; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1814 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1815 | |
| 1816 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1817 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1818 | if (c) { |
| 1819 | spin_unlock(&clp->cl_lock); |
| 1820 | free_conn(new); |
| 1821 | return; |
| 1822 | } |
| 1823 | __nfsd4_hash_conn(new, ses); |
| 1824 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1825 | ret = nfsd4_register_conn(new); |
| 1826 | if (ret) |
| 1827 | /* oops; xprt is already down: */ |
| 1828 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1829 | return; |
| 1830 | } |
| 1831 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 1832 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 1833 | { |
| 1834 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 1835 | |
| 1836 | return args->opcnt > session->se_fchannel.maxops; |
| 1837 | } |
| 1838 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 1839 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 1840 | struct nfsd4_session *session) |
| 1841 | { |
| 1842 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 1843 | |
| 1844 | return xb->len > session->se_fchannel.maxreq_sz; |
| 1845 | } |
| 1846 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1847 | __be32 |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1848 | nfsd4_sequence(struct svc_rqst *rqstp, |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1849 | struct nfsd4_compound_state *cstate, |
| 1850 | struct nfsd4_sequence *seq) |
| 1851 | { |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 1852 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1853 | struct nfsd4_session *session; |
| 1854 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1855 | struct nfsd4_conn *conn; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1856 | int status; |
| 1857 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 1858 | if (resp->opcnt != 1) |
| 1859 | return nfserr_sequence_pos; |
| 1860 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1861 | /* |
| 1862 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 1863 | * below. |
| 1864 | */ |
| 1865 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 1866 | if (!conn) |
| 1867 | return nfserr_jukebox; |
| 1868 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1869 | spin_lock(&client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1870 | status = nfserr_badsession; |
| 1871 | session = find_in_sessionid_hashtbl(&seq->sessionid); |
| 1872 | if (!session) |
| 1873 | goto out; |
| 1874 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 1875 | status = nfserr_too_many_ops; |
| 1876 | if (nfsd4_session_too_many_ops(rqstp, session)) |
| 1877 | goto out; |
| 1878 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 1879 | status = nfserr_req_too_big; |
| 1880 | if (nfsd4_request_too_big(rqstp, session)) |
| 1881 | goto out; |
| 1882 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1883 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 1884 | if (seq->slotid >= session->se_fchannel.maxreqs) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1885 | goto out; |
| 1886 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1887 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1888 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 1889 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 1890 | /* We do not negotiate the number of slots yet, so set the |
| 1891 | * maxslots to the session maxreqs which is used to encode |
| 1892 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 1893 | seq->maxslots = session->se_fchannel.maxreqs; |
| 1894 | |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 1895 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1896 | if (status == nfserr_replay_cache) { |
| 1897 | cstate->slot = slot; |
| 1898 | cstate->session = session; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 1899 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1900 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 1901 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 1902 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 1903 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1904 | } |
| 1905 | if (status) |
| 1906 | goto out; |
| 1907 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1908 | nfsd4_sequence_check_conn(conn, session); |
| 1909 | conn = NULL; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1910 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1911 | /* Success! bump slot seqid */ |
| 1912 | slot->sl_inuse = true; |
| 1913 | slot->sl_seqid = seq->seqid; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1914 | slot->sl_cachethis = seq->cachethis; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1915 | |
| 1916 | cstate->slot = slot; |
| 1917 | cstate->session = session; |
| 1918 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1919 | out: |
J. Bruce Fields | 26c0c75 | 2010-04-24 15:35:43 -0400 | [diff] [blame] | 1920 | /* Hold a session reference until done processing the compound. */ |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 1921 | if (cstate->session) { |
J. Bruce Fields | 0d7bb71 | 2010-11-18 08:30:33 -0500 | [diff] [blame] | 1922 | struct nfs4_client *clp = session->se_client; |
| 1923 | |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1924 | nfsd4_get_session(cstate->session); |
J. Bruce Fields | 0d7bb71 | 2010-11-18 08:30:33 -0500 | [diff] [blame] | 1925 | atomic_inc(&clp->cl_refcount); |
| 1926 | if (clp->cl_cb_state == NFSD4_CB_DOWN) |
| 1927 | seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 1928 | } |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 1929 | kfree(conn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 1930 | spin_unlock(&client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 1931 | dprintk("%s: return %d\n", __func__, ntohl(status)); |
| 1932 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | __be32 |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1936 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc) |
| 1937 | { |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1938 | int status = 0; |
| 1939 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1940 | if (rc->rca_one_fs) { |
| 1941 | if (!cstate->current_fh.fh_dentry) |
| 1942 | return nfserr_nofilehandle; |
| 1943 | /* |
| 1944 | * We don't take advantage of the rca_one_fs case. |
| 1945 | * That's OK, it's optional, we can safely ignore it. |
| 1946 | */ |
| 1947 | return nfs_ok; |
| 1948 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1949 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1950 | nfs4_lock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1951 | status = nfserr_complete_already; |
| 1952 | if (cstate->session->se_client->cl_firststate) |
| 1953 | goto out; |
| 1954 | |
| 1955 | status = nfserr_stale_clientid; |
| 1956 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1957 | /* |
| 1958 | * The following error isn't really legal. |
| 1959 | * But we only get here if the client just explicitly |
| 1960 | * destroyed the client. Surely it no longer cares what |
| 1961 | * error it gets back on an operation for the dead |
| 1962 | * client. |
| 1963 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1964 | goto out; |
| 1965 | |
| 1966 | status = nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1967 | nfsd4_create_clid_dir(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1968 | out: |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1969 | nfs4_unlock_state(); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 1970 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 1974 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 1975 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | struct xdr_netobj clname = { |
| 1978 | .len = setclid->se_namelen, |
| 1979 | .data = setclid->se_name, |
| 1980 | }; |
| 1981 | nfs4_verifier clverifier = setclid->se_verf; |
| 1982 | unsigned int strhashval; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 1983 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1984 | __be32 status; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1985 | char dname[HEXDIR_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | if (!check_name(clname)) |
Neil Brown | 73aea4e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1988 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1990 | status = nfs4_make_rec_clidname(dname, &clname); |
| 1991 | if (status) |
Neil Brown | 73aea4e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1992 | return status; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | /* |
| 1995 | * XXX The Duplicate Request Cache (DRC) has been checked (??) |
| 1996 | * We get here on a DRC miss. |
| 1997 | */ |
| 1998 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 1999 | strhashval = clientstr_hashval(dname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | nfs4_lock_state(); |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2002 | conf = find_confirmed_client_by_str(dname, strhashval); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2003 | if (conf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2004 | /* RFC 3530 14.2.33 CASE 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2006 | if (clp_used_exchangeid(conf)) |
| 2007 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 2008 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2009 | char addr_str[INET6_ADDRSTRLEN]; |
| 2010 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 2011 | sizeof(addr_str)); |
| 2012 | dprintk("NFSD: setclientid: string in use by client " |
| 2013 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | goto out; |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | } |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2017 | /* |
| 2018 | * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION") |
| 2019 | * has a description of SETCLIENTID request processing consisting |
| 2020 | * of 5 bullet points, labeled as CASE0 - CASE4 below. |
| 2021 | */ |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2022 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 2023 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | if (!conf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2025 | /* |
| 2026 | * RFC 3530 14.2.33 CASE 4: |
| 2027 | * placed first, because it is the normal case |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | */ |
| 2029 | if (unconf) |
| 2030 | expire_client(unconf); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2031 | new = create_client(clname, dname, rqstp, &clverifier); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 2032 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | gen_clid(new); |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2035 | } else if (same_verf(&conf->cl_verifier, &clverifier)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2037 | * RFC 3530 14.2.33 CASE 1: |
| 2038 | * probable callback update |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | */ |
NeilBrown | 31f4a6c | 2005-06-23 22:04:06 -0700 | [diff] [blame] | 2040 | if (unconf) { |
| 2041 | /* Note this is removing unconfirmed {*x***}, |
| 2042 | * which is stronger than RFC recommended {vxc**}. |
| 2043 | * This has the advantage that there is at most |
| 2044 | * one {*x***} in either list at any time. |
| 2045 | */ |
| 2046 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2048 | new = create_client(clname, dname, rqstp, &clverifier); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 2049 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | copy_clid(new, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | } else if (!unconf) { |
| 2053 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2054 | * RFC 3530 14.2.33 CASE 2: |
| 2055 | * probable client reboot; state will be removed if |
| 2056 | * confirmed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | */ |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2058 | new = create_client(clname, dname, rqstp, &clverifier); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 2059 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | gen_clid(new); |
J. Bruce Fields | 49ba878 | 2007-11-19 19:09:50 -0500 | [diff] [blame] | 2062 | } else { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2063 | /* |
| 2064 | * RFC 3530 14.2.33 CASE 3: |
| 2065 | * probable client reboot; state will be removed if |
| 2066 | * confirmed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | */ |
| 2068 | expire_client(unconf); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2069 | new = create_client(clname, dname, rqstp, &clverifier); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 2070 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | gen_clid(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | } |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 2074 | /* |
| 2075 | * XXX: we should probably set this at creation time, and check |
| 2076 | * for consistent minorversion use throughout: |
| 2077 | */ |
| 2078 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2079 | gen_callback(new, setclid, rqstp); |
J. Bruce Fields | c175b83 | 2007-08-09 18:34:32 -0400 | [diff] [blame] | 2080 | add_to_unconfirmed(new, strhashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 2082 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 2083 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 2084 | status = nfs_ok; |
| 2085 | out: |
| 2086 | nfs4_unlock_state(); |
| 2087 | return status; |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2092 | * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has |
| 2093 | * a description of SETCLIENTID_CONFIRM request processing consisting of 4 |
| 2094 | * bullets, labeled as CASE1 - CASE4 below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2096 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2097 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 2098 | struct nfsd4_compound_state *cstate, |
| 2099 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2101 | struct sockaddr *sa = svc_addr(rqstp); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2102 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 2104 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2105 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | |
| 2107 | if (STALE_CLIENTID(clid)) |
| 2108 | return nfserr_stale_clientid; |
| 2109 | /* |
| 2110 | * XXX The Duplicate Request Cache (DRC) has been checked (??) |
| 2111 | * We get here on a DRC miss. |
| 2112 | */ |
| 2113 | |
| 2114 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2115 | |
| 2116 | conf = find_confirmed_client(clid); |
| 2117 | unconf = find_unconfirmed_client(clid); |
| 2118 | |
| 2119 | status = nfserr_clid_inuse; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2120 | if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa)) |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2121 | goto out; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 2122 | if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa)) |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2123 | goto out; |
| 2124 | |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2125 | /* |
| 2126 | * section 14.2.34 of RFC 3530 has a description of |
| 2127 | * SETCLIENTID_CONFIRM request processing consisting |
| 2128 | * of 4 bullet points, labeled as CASE1 - CASE4 below. |
| 2129 | */ |
J. Bruce Fields | 366e0c1 | 2007-11-20 15:54:10 -0500 | [diff] [blame] | 2130 | if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2131 | /* |
| 2132 | * RFC 3530 14.2.34 CASE 1: |
| 2133 | * callback update |
| 2134 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2135 | if (!same_creds(&conf->cl_cred, &unconf->cl_cred)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | status = nfserr_clid_inuse; |
| 2137 | else { |
J. Bruce Fields | 5a3c9d7 | 2010-10-19 17:56:52 -0400 | [diff] [blame] | 2138 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
| 2139 | nfsd4_probe_callback(conf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2140 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | status = nfs_ok; |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
J. Bruce Fields | f3aba4e | 2007-11-20 16:52:07 -0500 | [diff] [blame] | 2144 | } else if (conf && !unconf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2145 | /* |
| 2146 | * RFC 3530 14.2.34 CASE 2: |
| 2147 | * probable retransmitted request; play it safe and |
| 2148 | * do nothing. |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2149 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2150 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | status = nfserr_clid_inuse; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2152 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | status = nfs_ok; |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2154 | } else if (!conf && unconf |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2155 | && same_verf(&unconf->cl_confirm, &confirm)) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2156 | /* |
| 2157 | * RFC 3530 14.2.34 CASE 3: |
| 2158 | * Normal case; new or rebooted client: |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2159 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2160 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | status = nfserr_clid_inuse; |
| 2162 | } else { |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2163 | unsigned int hash = |
| 2164 | clientstr_hashval(unconf->cl_recdir); |
| 2165 | conf = find_confirmed_client_by_str(unconf->cl_recdir, |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2166 | hash); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2167 | if (conf) { |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 2168 | nfsd4_remove_clid_dir(conf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2169 | expire_client(conf); |
| 2170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | move_to_confirmed(unconf); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 2172 | conf = unconf; |
J. Bruce Fields | 5a3c9d7 | 2010-10-19 17:56:52 -0400 | [diff] [blame] | 2173 | nfsd4_probe_callback(conf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 2174 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2176 | } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm))) |
| 2177 | && (!unconf || (unconf && !same_verf(&unconf->cl_confirm, |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2178 | &confirm)))) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 2179 | /* |
| 2180 | * RFC 3530 14.2.34 CASE 4: |
| 2181 | * Client probably hasn't noticed that we rebooted yet. |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2182 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | status = nfserr_stale_clientid; |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 2184 | } else { |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 2185 | /* check that we have hit one of the cases...*/ |
| 2186 | status = nfserr_clid_inuse; |
| 2187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | nfs4_unlock_state(); |
| 2190 | return status; |
| 2191 | } |
| 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | /* OPEN Share state helper functions */ |
| 2194 | static inline struct nfs4_file * |
| 2195 | alloc_init_file(struct inode *ino) |
| 2196 | { |
| 2197 | struct nfs4_file *fp; |
| 2198 | unsigned int hashval = file_hashval(ino); |
| 2199 | |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2200 | fp = kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 2201 | if (fp) { |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 2202 | atomic_set(&fp->fi_ref, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | INIT_LIST_HEAD(&fp->fi_hash); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 2204 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 2205 | INIT_LIST_HEAD(&fp->fi_delegations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | fp->fi_inode = igrab(ino); |
Meelap Shah | 47f9940 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 2207 | fp->fi_had_conflict = false; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2208 | fp->fi_lease = NULL; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2209 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 2210 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Pavel Emelyanov | 47cee54 | 2010-05-17 20:00:37 +0400 | [diff] [blame] | 2211 | spin_lock(&recall_lock); |
| 2212 | list_add(&fp->fi_hash, &file_hashtbl[hashval]); |
| 2213 | spin_unlock(&recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | return fp; |
| 2215 | } |
| 2216 | return NULL; |
| 2217 | } |
| 2218 | |
| 2219 | static void |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2220 | nfsd4_free_slab(struct kmem_cache **slab) |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2221 | { |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2222 | if (*slab == NULL) |
| 2223 | return; |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 2224 | kmem_cache_destroy(*slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2225 | *slab = NULL; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 2228 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2229 | nfsd4_free_slabs(void) |
| 2230 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2231 | nfsd4_free_slab(&openowner_slab); |
| 2232 | nfsd4_free_slab(&lockowner_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2233 | nfsd4_free_slab(&file_slab); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2234 | nfsd4_free_slab(&stateid_slab); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2235 | nfsd4_free_slab(&deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
| 2238 | static int |
| 2239 | nfsd4_init_slabs(void) |
| 2240 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2241 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 2242 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2243 | if (openowner_slab == NULL) |
| 2244 | goto out_nomem; |
| 2245 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
| 2246 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 2247 | if (lockowner_slab == NULL) |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2248 | goto out_nomem; |
| 2249 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2250 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2251 | if (file_slab == NULL) |
| 2252 | goto out_nomem; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2253 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2254 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2255 | if (stateid_slab == NULL) |
| 2256 | goto out_nomem; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2257 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2258 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 2259 | if (deleg_slab == NULL) |
| 2260 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | return 0; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 2262 | out_nomem: |
| 2263 | nfsd4_free_slabs(); |
| 2264 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 2265 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2268 | void nfs4_free_openowner(struct nfs4_openowner *oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2270 | kfree(oo->oo_owner.so_owner.data); |
| 2271 | kmem_cache_free(openowner_slab, oo); |
| 2272 | } |
| 2273 | |
| 2274 | void nfs4_free_lockowner(struct nfs4_lockowner *lo) |
| 2275 | { |
| 2276 | kfree(lo->lo_owner.so_owner.data); |
| 2277 | kmem_cache_free(lockowner_slab, lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2280 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 2281 | { |
| 2282 | rp->rp_status = nfserr_serverfault; |
| 2283 | rp->rp_buflen = 0; |
| 2284 | rp->rp_buf = rp->rp_ibuf; |
| 2285 | } |
| 2286 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2287 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | { |
| 2289 | struct nfs4_stateowner *sop; |
| 2290 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2291 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2292 | if (!sop) |
| 2293 | return NULL; |
| 2294 | |
| 2295 | sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL); |
| 2296 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2297 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2298 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2300 | sop->so_owner.len = owner->len; |
| 2301 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2302 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2303 | sop->so_client = clp; |
| 2304 | init_nfs4_replay(&sop->so_replay); |
| 2305 | return sop; |
| 2306 | } |
| 2307 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2308 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 2309 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2310 | list_add(&oo->oo_owner.so_strhash, &open_ownerstr_hashtbl[strhashval]); |
| 2311 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2314 | static struct nfs4_openowner * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2316 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2318 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 2319 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2321 | oo->oo_owner.so_is_open_owner = 1; |
| 2322 | oo->oo_owner.so_seqid = open->op_seqid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2323 | oo->oo_flags = 0; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2324 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 2325 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2326 | INIT_LIST_HEAD(&oo->oo_close_lru); |
| 2327 | hash_openowner(oo, clp, strhashval); |
| 2328 | return oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | static inline void |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2332 | init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2333 | struct nfs4_openowner *oo = open->op_openowner; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 2334 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2336 | INIT_LIST_HEAD(&stp->st_lockowners); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2337 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 2338 | list_add(&stp->st_perfile, &fp->fi_stateids); |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2339 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2340 | stp->st_stateowner = &oo->oo_owner; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2341 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | stp->st_file = fp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 2343 | stp->st_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid; |
| 2344 | stp->st_stid.sc_stateid.si_opaque.so_id = current_stateid++; |
J. Bruce Fields | 73997dc | 2011-08-31 15:47:21 -0400 | [diff] [blame] | 2345 | /* note will be incremented before first return to client: */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2346 | stp->st_stid.sc_stateid.si_generation = 0; |
J. Bruce Fields | 36d44c6 | 2011-09-08 12:16:03 -0400 | [diff] [blame] | 2347 | hash_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | stp->st_access_bmap = 0; |
| 2349 | stp->st_deny_bmap = 0; |
Andy Adamson | 84459a1 | 2009-04-03 08:28:56 +0300 | [diff] [blame] | 2350 | __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK, |
| 2351 | &stp->st_access_bmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2353 | stp->st_openstp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | static void |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2357 | move_to_close_lru(struct nfs4_openowner *oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2359 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2361 | list_move_tail(&oo->oo_close_lru, &close_lru); |
| 2362 | oo->oo_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2366 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 2367 | clientid_t *clid) |
| 2368 | { |
| 2369 | return (sop->so_owner.len == owner->len) && |
| 2370 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 2371 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2374 | static struct nfs4_openowner * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open) |
| 2376 | { |
| 2377 | struct nfs4_stateowner *so = NULL; |
| 2378 | |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 2379 | list_for_each_entry(so, &open_ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2380 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2381 | return container_of(so, struct nfs4_openowner, oo_owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | } |
| 2383 | return NULL; |
| 2384 | } |
| 2385 | |
| 2386 | /* search file_hashtbl[] for file */ |
| 2387 | static struct nfs4_file * |
| 2388 | find_file(struct inode *ino) |
| 2389 | { |
| 2390 | unsigned int hashval = file_hashval(ino); |
| 2391 | struct nfs4_file *fp; |
| 2392 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 2393 | spin_lock(&recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2395 | if (fp->fi_inode == ino) { |
| 2396 | get_nfs4_file(fp); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 2397 | spin_unlock(&recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2399 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | } |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 2401 | spin_unlock(&recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | return NULL; |
| 2403 | } |
| 2404 | |
Andy Adamson | d87a8ad | 2009-04-03 08:28:53 +0300 | [diff] [blame] | 2405 | static inline int access_valid(u32 x, u32 minorversion) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2406 | { |
Andy Adamson | d87a8ad | 2009-04-03 08:28:53 +0300 | [diff] [blame] | 2407 | if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ) |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 2408 | return 0; |
Andy Adamson | d87a8ad | 2009-04-03 08:28:53 +0300 | [diff] [blame] | 2409 | if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH) |
| 2410 | return 0; |
| 2411 | x &= ~NFS4_SHARE_ACCESS_MASK; |
| 2412 | if (minorversion && x) { |
| 2413 | if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL) |
| 2414 | return 0; |
| 2415 | if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED) |
| 2416 | return 0; |
| 2417 | x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK); |
| 2418 | } |
| 2419 | if (x) |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 2420 | return 0; |
| 2421 | return 1; |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 2424 | static inline int deny_valid(u32 x) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2425 | { |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 2426 | /* Note: unlike access bits, deny bits may be zero. */ |
| 2427 | return x <= NFS4_SHARE_DENY_BOTH; |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2428 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 2430 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | * Called to check deny when READ with all zero stateid or |
| 2432 | * WRITE with all zero or all one stateid |
| 2433 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2434 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 2436 | { |
| 2437 | struct inode *ino = current_fh->fh_dentry->d_inode; |
| 2438 | struct nfs4_file *fp; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2439 | struct nfs4_ol_stateid *stp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2440 | __be32 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
| 2442 | dprintk("NFSD: nfs4_share_conflict\n"); |
| 2443 | |
| 2444 | fp = find_file(ino); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2445 | if (!fp) |
| 2446 | return nfs_ok; |
NeilBrown | b700949 | 2005-07-07 17:59:23 -0700 | [diff] [blame] | 2447 | ret = nfserr_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | /* Search for conflicting share reservations */ |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2449 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
| 2450 | if (test_bit(deny_type, &stp->st_deny_bmap) || |
| 2451 | test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap)) |
| 2452 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2454 | ret = nfs_ok; |
| 2455 | out: |
| 2456 | put_nfs4_file(fp); |
| 2457 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2460 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | /* We're assuming the state code never drops its reference |
| 2463 | * without first removing the lease. Since we're in this lease |
| 2464 | * callback (and since the lease code is serialized by the kernel |
| 2465 | * lock) we know the server hasn't removed the lease yet, we know |
| 2466 | * it's safe to take a reference: */ |
| 2467 | atomic_inc(&dp->dl_count); |
| 2468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | list_add_tail(&dp->dl_recall_lru, &del_recall_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | |
Arnd Bergmann | 460781b | 2010-11-17 16:26:56 +0100 | [diff] [blame] | 2471 | /* only place dl_time is set. protected by lock_flocks*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | dp->dl_time = get_seconds(); |
| 2473 | |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2474 | nfsd4_cb_recall(dp); |
| 2475 | } |
| 2476 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2477 | /* Called from break_lease() with lock_flocks() held. */ |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2478 | static void nfsd_break_deleg_cb(struct file_lock *fl) |
| 2479 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2480 | struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner; |
| 2481 | struct nfs4_delegation *dp; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2482 | |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2483 | BUG_ON(!fp); |
| 2484 | /* We assume break_lease is only called once per lease: */ |
| 2485 | BUG_ON(fp->fi_had_conflict); |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 2486 | /* |
| 2487 | * We don't want the locks code to timeout the lease for us; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2488 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 2489 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 2490 | */ |
| 2491 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | |
J. Bruce Fields | 5d926e8 | 2011-02-07 16:53:46 -0500 | [diff] [blame] | 2493 | spin_lock(&recall_lock); |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2494 | fp->fi_had_conflict = true; |
| 2495 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) |
| 2496 | nfsd_break_one_deleg(dp); |
J. Bruce Fields | 5d926e8 | 2011-02-07 16:53:46 -0500 | [diff] [blame] | 2497 | spin_unlock(&recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | } |
| 2499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | static |
| 2501 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 2502 | { |
| 2503 | if (arg & F_UNLCK) |
| 2504 | return lease_modify(onlist, arg); |
| 2505 | else |
| 2506 | return -EAGAIN; |
| 2507 | } |
| 2508 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 2509 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 2510 | .lm_break = nfsd_break_deleg_cb, |
| 2511 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | }; |
| 2513 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 2514 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 2515 | { |
| 2516 | if (nfsd4_has_session(cstate)) |
| 2517 | return nfs_ok; |
| 2518 | if (seqid == so->so_seqid - 1) |
| 2519 | return nfserr_replay_me; |
| 2520 | if (seqid == so->so_seqid) |
| 2521 | return nfs_ok; |
| 2522 | return nfserr_bad_seqid; |
| 2523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2525 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 2526 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
| 2527 | struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | clientid_t *clientid = &open->op_clientid; |
| 2530 | struct nfs4_client *clp = NULL; |
| 2531 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2532 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 2533 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | if (!check_name(open->op_owner)) |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2536 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
| 2538 | if (STALE_CLIENTID(&open->op_clientid)) |
| 2539 | return nfserr_stale_clientid; |
| 2540 | |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 2541 | strhashval = open_ownerstr_hashval(clientid->cl_id, &open->op_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2542 | oo = find_openstateowner_str(strhashval, open); |
| 2543 | open->op_openowner = oo; |
| 2544 | if (!oo) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2545 | /* Make sure the client's lease hasn't expired. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | clp = find_confirmed_client(clientid); |
| 2547 | if (clp == NULL) |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2548 | return nfserr_expired; |
| 2549 | goto renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2551 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2552 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2553 | clp = oo->oo_owner.so_client; |
| 2554 | release_openowner(oo); |
| 2555 | open->op_openowner = NULL; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2556 | goto renew; |
| 2557 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2558 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 2559 | if (status) |
| 2560 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | renew: |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2562 | if (open->op_openowner == NULL) { |
| 2563 | oo = alloc_init_open_stateowner(strhashval, clp, open); |
| 2564 | if (oo == NULL) |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 2565 | return nfserr_jukebox; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2566 | open->op_openowner = oo; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2567 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2568 | list_del_init(&oo->oo_close_lru); |
| 2569 | renew_client(oo->oo_owner.so_client); |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 2570 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2573 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 2574 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 2575 | { |
| 2576 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 2577 | return nfserr_openmode; |
| 2578 | else |
| 2579 | return nfs_ok; |
| 2580 | } |
| 2581 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 2582 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 2583 | { |
| 2584 | share_access &= ~NFS4_SHARE_WANT_MASK; |
| 2585 | |
| 2586 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 2587 | } |
| 2588 | |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 2589 | static struct nfs4_delegation *find_deleg_stateid(stateid_t *s) |
| 2590 | { |
| 2591 | struct nfs4_stid *ret; |
| 2592 | |
| 2593 | ret = find_stateid_by_type(s, NFS4_DELEG_STID); |
| 2594 | if (!ret) |
| 2595 | return NULL; |
| 2596 | return delegstateid(ret); |
| 2597 | } |
| 2598 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2599 | static __be32 |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2600 | nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open, |
| 2601 | struct nfs4_delegation **dp) |
| 2602 | { |
| 2603 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2604 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2605 | |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 2606 | *dp = find_deleg_stateid(&open->op_delegate_stateid); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2607 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 2608 | goto out; |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 2609 | flags = share_access_to_flags(open->op_share_access); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2610 | status = nfs4_check_delegmode(*dp, flags); |
| 2611 | if (status) |
| 2612 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 2613 | out: |
| 2614 | if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR) |
| 2615 | return nfs_ok; |
| 2616 | if (status) |
| 2617 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2618 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 2619 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2622 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2623 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2625 | struct nfs4_ol_stateid *local; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2626 | struct nfs4_openowner *oo = open->op_openowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 2628 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | /* ignore lock owners */ |
| 2630 | if (local->st_stateowner->so_is_open_owner == 0) |
| 2631 | continue; |
| 2632 | /* remember if we have seen this open owner */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2633 | if (local->st_stateowner == &oo->oo_owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | *stpp = local; |
| 2635 | /* check for conflicting share reservations */ |
| 2636 | if (!test_share(local, open)) |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 2637 | return nfserr_share_denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | } |
J. Bruce Fields | 77eaae8 | 2011-09-02 12:08:20 -0400 | [diff] [blame] | 2639 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | } |
| 2641 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2642 | static inline struct nfs4_ol_stateid * |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2643 | nfs4_alloc_stateid(void) |
| 2644 | { |
| 2645 | return kmem_cache_alloc(stateid_slab, GFP_KERNEL); |
| 2646 | } |
| 2647 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 2648 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 2649 | { |
| 2650 | int flags = 0; |
| 2651 | |
| 2652 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 2653 | flags |= NFSD_MAY_READ; |
| 2654 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 2655 | flags |= NFSD_MAY_WRITE; |
| 2656 | return flags; |
| 2657 | } |
| 2658 | |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 2659 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
| 2660 | struct svc_fh *cur_fh, struct nfsd4_open *open) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2661 | { |
| 2662 | __be32 status; |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 2663 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 2664 | int access = nfs4_access_to_access(open->op_share_access); |
| 2665 | |
| 2666 | /* CLAIM_DELEGATE_CUR is used in response to a broken lease; |
| 2667 | * allowing it to break the lease and return EAGAIN leaves the |
| 2668 | * client unable to make progress in returning the delegation */ |
| 2669 | if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) |
| 2670 | access |= NFSD_MAY_NOT_BREAK_LEASE; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2671 | |
| 2672 | if (!fp->fi_fds[oflag]) { |
| 2673 | status = nfsd_open(rqstp, cur_fh, S_IFREG, access, |
| 2674 | &fp->fi_fds[oflag]); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2675 | if (status) |
| 2676 | return status; |
| 2677 | } |
| 2678 | nfs4_file_get_access(fp, oflag); |
| 2679 | |
| 2680 | return nfs_ok; |
| 2681 | } |
| 2682 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2683 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2684 | nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_ol_stateid **stpp, |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2685 | struct nfs4_file *fp, struct svc_fh *cur_fh, |
| 2686 | struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2688 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2689 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2691 | stp = nfs4_alloc_stateid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | if (stp == NULL) |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 2693 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 2695 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2696 | if (status) { |
| 2697 | kmem_cache_free(stateid_slab, stp); |
| 2698 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | *stpp = stp; |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2704 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 2706 | struct nfsd4_open *open) |
| 2707 | { |
| 2708 | struct iattr iattr = { |
| 2709 | .ia_valid = ATTR_SIZE, |
| 2710 | .ia_size = 0, |
| 2711 | }; |
| 2712 | if (!open->op_truncate) |
| 2713 | return 0; |
| 2714 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 2715 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 2717 | } |
| 2718 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2719 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2720 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | { |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 2722 | u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK; |
| 2723 | bool new_access; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2724 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 2726 | new_access = !test_bit(op_share_access, &stp->st_access_bmap); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2727 | if (new_access) { |
Casey Bodley | 0c12eaf | 2011-07-23 14:58:10 -0400 | [diff] [blame] | 2728 | status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2729 | if (status) |
| 2730 | return status; |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 2731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 2733 | if (status) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2734 | if (new_access) { |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 2735 | int oflag = nfs4_access_to_omode(op_share_access); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2736 | nfs4_file_put_access(fp, oflag); |
| 2737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | return status; |
| 2739 | } |
| 2740 | /* remember the open */ |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 2741 | __set_bit(op_share_access, &stp->st_access_bmap); |
J. Bruce Fields | b55e0ba | 2008-04-28 18:22:50 -0400 | [diff] [blame] | 2742 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
| 2744 | return nfs_ok; |
| 2745 | } |
| 2746 | |
| 2747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | static void |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 2749 | nfs4_set_claim_prev(struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | { |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2751 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2752 | open->op_openowner->oo_owner.so_client->cl_firststate = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 2755 | /* Should we give out recallable state?: */ |
| 2756 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 2757 | { |
| 2758 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 2759 | return true; |
| 2760 | /* |
| 2761 | * In the sessions case, since we don't have to establish a |
| 2762 | * separate connection for callbacks, we assume it's OK |
| 2763 | * until we hear otherwise: |
| 2764 | */ |
| 2765 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 2766 | } |
| 2767 | |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 2768 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag) |
| 2769 | { |
| 2770 | struct file_lock *fl; |
| 2771 | |
| 2772 | fl = locks_alloc_lock(); |
| 2773 | if (!fl) |
| 2774 | return NULL; |
| 2775 | locks_init_lock(fl); |
| 2776 | fl->fl_lmops = &nfsd_lease_mng_ops; |
| 2777 | fl->fl_flags = FL_LEASE; |
| 2778 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 2779 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2780 | fl->fl_owner = (fl_owner_t)(dp->dl_file); |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 2781 | fl->fl_pid = current->tgid; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 2782 | return fl; |
| 2783 | } |
| 2784 | |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2785 | static int nfs4_setlease(struct nfs4_delegation *dp, int flag) |
| 2786 | { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2787 | struct nfs4_file *fp = dp->dl_file; |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2788 | struct file_lock *fl; |
| 2789 | int status; |
| 2790 | |
| 2791 | fl = nfs4_alloc_init_lease(dp, flag); |
| 2792 | if (!fl) |
| 2793 | return -ENOMEM; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2794 | fl->fl_file = find_readable_file(fp); |
| 2795 | list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations); |
| 2796 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2797 | if (status) { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2798 | list_del_init(&dp->dl_perclnt); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2799 | locks_free_lock(fl); |
| 2800 | return -ENOMEM; |
| 2801 | } |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2802 | fp->fi_lease = fl; |
| 2803 | fp->fi_deleg_file = fl->fl_file; |
| 2804 | get_file(fp->fi_deleg_file); |
| 2805 | atomic_set(&fp->fi_delegees, 1); |
| 2806 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag) |
| 2811 | { |
| 2812 | struct nfs4_file *fp = dp->dl_file; |
| 2813 | |
| 2814 | if (!fp->fi_lease) |
| 2815 | return nfs4_setlease(dp, flag); |
| 2816 | spin_lock(&recall_lock); |
| 2817 | if (fp->fi_had_conflict) { |
| 2818 | spin_unlock(&recall_lock); |
| 2819 | return -EAGAIN; |
| 2820 | } |
| 2821 | atomic_inc(&fp->fi_delegees); |
| 2822 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 2823 | spin_unlock(&recall_lock); |
| 2824 | list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2825 | return 0; |
| 2826 | } |
| 2827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | /* |
| 2829 | * Attempt to hand out a delegation. |
| 2830 | */ |
| 2831 | static void |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2832 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | { |
| 2834 | struct nfs4_delegation *dp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2835 | struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner); |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 2836 | int cb_up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | int status, flag = 0; |
| 2838 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2839 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | flag = NFS4_OPEN_DELEGATE_NONE; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 2841 | open->op_recall = 0; |
| 2842 | switch (open->op_claim_type) { |
| 2843 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2844 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 2845 | open->op_recall = 1; |
| 2846 | flag = open->op_delegate_type; |
| 2847 | if (flag == NFS4_OPEN_DELEGATE_NONE) |
| 2848 | goto out; |
| 2849 | break; |
| 2850 | case NFS4_OPEN_CLAIM_NULL: |
| 2851 | /* Let's not give out any delegations till everyone's |
| 2852 | * had the chance to reclaim theirs.... */ |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 2853 | if (locks_in_grace()) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 2854 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2855 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 2856 | goto out; |
| 2857 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
| 2858 | flag = NFS4_OPEN_DELEGATE_WRITE; |
| 2859 | else |
| 2860 | flag = NFS4_OPEN_DELEGATE_READ; |
| 2861 | break; |
| 2862 | default: |
| 2863 | goto out; |
| 2864 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2866 | dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 2867 | if (dp == NULL) |
| 2868 | goto out_no_deleg; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2869 | status = nfs4_set_delegation(dp, flag); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 2870 | if (status) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 2871 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 2873 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 2875 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 2876 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | out: |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 2878 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS |
| 2879 | && flag == NFS4_OPEN_DELEGATE_NONE |
| 2880 | && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2881 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | open->op_delegate_type = flag; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 2883 | return; |
| 2884 | out_free: |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 2885 | nfs4_put_delegation(dp); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 2886 | out_no_deleg: |
| 2887 | flag = NFS4_OPEN_DELEGATE_NONE; |
| 2888 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | * called with nfs4_lock_state() held. |
| 2893 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2894 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 2896 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 2897 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | struct nfs4_file *fp = NULL; |
| 2899 | struct inode *ino = current_fh->fh_dentry->d_inode; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2900 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2901 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2902 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | |
| 2904 | status = nfserr_inval; |
Andy Adamson | d87a8ad | 2009-04-03 08:28:53 +0300 | [diff] [blame] | 2905 | if (!access_valid(open->op_share_access, resp->cstate.minorversion) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2906 | || !deny_valid(open->op_share_deny)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | goto out; |
| 2908 | /* |
| 2909 | * Lookup file; if found, lookup stateid and check open request, |
| 2910 | * and check for delegations in the process of being recalled. |
| 2911 | * If not found, create the nfs4_file struct |
| 2912 | */ |
| 2913 | fp = find_file(ino); |
| 2914 | if (fp) { |
| 2915 | if ((status = nfs4_check_open(fp, open, &stp))) |
| 2916 | goto out; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 2917 | status = nfs4_check_deleg(fp, open, &dp); |
| 2918 | if (status) |
| 2919 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | } else { |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 2921 | status = nfserr_bad_stateid; |
| 2922 | if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) |
| 2923 | goto out; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 2924 | status = nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | fp = alloc_init_file(ino); |
| 2926 | if (fp == NULL) |
| 2927 | goto out; |
| 2928 | } |
| 2929 | |
| 2930 | /* |
| 2931 | * OPEN the file, or upgrade an existing OPEN. |
| 2932 | * If truncate fails, the OPEN fails. |
| 2933 | */ |
| 2934 | if (stp) { |
| 2935 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2936 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | if (status) |
| 2938 | goto out; |
| 2939 | } else { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 2940 | status = nfs4_new_open(rqstp, &stp, fp, current_fh, open); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 2941 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | goto out; |
J. Bruce Fields | 881ea2b | 2011-09-06 17:20:34 -0400 | [diff] [blame] | 2943 | init_open_stateid(stp, fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | status = nfsd4_truncate(rqstp, current_fh, open); |
| 2945 | if (status) { |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 2946 | release_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | goto out; |
| 2948 | } |
| 2949 | } |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2950 | update_stateid(&stp->st_stid.sc_stateid); |
| 2951 | memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 2953 | if (nfsd4_has_session(&resp->cstate)) |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2954 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 2955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | /* |
| 2957 | * Attempt to hand out a delegation. No error return, because the |
| 2958 | * OPEN succeeds even if we fail. |
| 2959 | */ |
| 2960 | nfs4_open_delegation(current_fh, open, stp); |
| 2961 | |
| 2962 | status = nfs_ok; |
| 2963 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 2964 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 2965 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | out: |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2967 | if (fp) |
| 2968 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 2969 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
| 2970 | nfs4_set_claim_prev(open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | /* |
| 2972 | * To finish the open response, we just need to set the rflags. |
| 2973 | */ |
| 2974 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 2975 | if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) && |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 2976 | !nfsd4_has_session(&resp->cstate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 2978 | |
| 2979 | return status; |
| 2980 | } |
| 2981 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2982 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2983 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2984 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | { |
| 2986 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2987 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | |
| 2989 | nfs4_lock_state(); |
| 2990 | dprintk("process_renew(%08x/%08x): starting\n", |
| 2991 | clid->cl_boot, clid->cl_id); |
| 2992 | status = nfserr_stale_clientid; |
| 2993 | if (STALE_CLIENTID(clid)) |
| 2994 | goto out; |
| 2995 | clp = find_confirmed_client(clid); |
| 2996 | status = nfserr_expired; |
| 2997 | if (clp == NULL) { |
| 2998 | /* We assume the client took too long to RENEW. */ |
| 2999 | dprintk("nfsd4_renew: clientid not found!\n"); |
| 3000 | goto out; |
| 3001 | } |
| 3002 | renew_client(clp); |
| 3003 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 3004 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 3005 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | goto out; |
| 3007 | status = nfs_ok; |
| 3008 | out: |
| 3009 | nfs4_unlock_state(); |
| 3010 | return status; |
| 3011 | } |
| 3012 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 3013 | static struct lock_manager nfsd4_manager = { |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3014 | }; |
| 3015 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3016 | static void |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3017 | nfsd4_end_grace(void) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3018 | { |
| 3019 | dprintk("NFSD: end of grace period\n"); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 3020 | nfsd4_recdir_purge_old(); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3021 | locks_end_grace(&nfsd4_manager); |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 3022 | /* |
| 3023 | * Now that every NFSv4 client has had the chance to recover and |
| 3024 | * to see the (possibly new, possibly shorter) lease time, we |
| 3025 | * can safely set the next grace time to the current lease time: |
| 3026 | */ |
| 3027 | nfsd4_grace = nfsd4_lease; |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3030 | static time_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | nfs4_laundromat(void) |
| 3032 | { |
| 3033 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3034 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | struct nfs4_delegation *dp; |
| 3036 | struct list_head *pos, *next, reaplist; |
J. Bruce Fields | cf07d2e | 2010-02-28 23:20:19 -0500 | [diff] [blame] | 3037 | time_t cutoff = get_seconds() - nfsd4_lease; |
| 3038 | time_t t, clientid_val = nfsd4_lease; |
| 3039 | time_t u, test_val = nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | |
| 3041 | nfs4_lock_state(); |
| 3042 | |
| 3043 | dprintk("NFSD: laundromat service - starting\n"); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3044 | if (locks_in_grace()) |
| 3045 | nfsd4_end_grace(); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3046 | INIT_LIST_HEAD(&reaplist); |
| 3047 | spin_lock(&client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | list_for_each_safe(pos, next, &client_lru) { |
| 3049 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 3050 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 3051 | t = clp->cl_time - cutoff; |
| 3052 | if (clientid_val > t) |
| 3053 | clientid_val = t; |
| 3054 | break; |
| 3055 | } |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 3056 | if (atomic_read(&clp->cl_refcount)) { |
| 3057 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 3058 | clp->cl_clientid.cl_id); |
| 3059 | continue; |
| 3060 | } |
| 3061 | unhash_client_locked(clp); |
| 3062 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 3063 | } |
| 3064 | spin_unlock(&client_lock); |
| 3065 | list_for_each_safe(pos, next, &reaplist) { |
| 3066 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 3068 | clp->cl_clientid.cl_id); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 3069 | nfsd4_remove_clid_dir(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | expire_client(clp); |
| 3071 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | spin_lock(&recall_lock); |
| 3073 | list_for_each_safe(pos, next, &del_recall_lru) { |
| 3074 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 3075 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
| 3076 | u = dp->dl_time - cutoff; |
| 3077 | if (test_val > u) |
| 3078 | test_val = u; |
| 3079 | break; |
| 3080 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | list_move(&dp->dl_recall_lru, &reaplist); |
| 3082 | } |
| 3083 | spin_unlock(&recall_lock); |
| 3084 | list_for_each_safe(pos, next, &reaplist) { |
| 3085 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 3086 | list_del_init(&dp->dl_recall_lru); |
| 3087 | unhash_delegation(dp); |
| 3088 | } |
J. Bruce Fields | cf07d2e | 2010-02-28 23:20:19 -0500 | [diff] [blame] | 3089 | test_val = nfsd4_lease; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | list_for_each_safe(pos, next, &close_lru) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3091 | oo = container_of(pos, struct nfs4_openowner, oo_close_lru); |
| 3092 | if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) { |
| 3093 | u = oo->oo_time - cutoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | if (test_val > u) |
| 3095 | test_val = u; |
| 3096 | break; |
| 3097 | } |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3098 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | } |
| 3100 | if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT) |
| 3101 | clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT; |
| 3102 | nfs4_unlock_state(); |
| 3103 | return clientid_val; |
| 3104 | } |
| 3105 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 3106 | static struct workqueue_struct *laundry_wq; |
| 3107 | static void laundromat_main(struct work_struct *); |
| 3108 | static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main); |
| 3109 | |
| 3110 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 3111 | laundromat_main(struct work_struct *not_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | { |
| 3113 | time_t t; |
| 3114 | |
| 3115 | t = nfs4_laundromat(); |
| 3116 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 3117 | queue_delayed_work(laundry_wq, &laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3120 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp) |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 3121 | { |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3122 | if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode) |
| 3123 | return nfserr_bad_stateid; |
| 3124 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | static int |
| 3128 | STALE_STATEID(stateid_t *stateid) |
| 3129 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 3130 | if (stateid->si_opaque.so_clid.cl_boot == boot_time) |
J. Bruce Fields | e4e83ea | 2010-04-22 16:21:39 -0400 | [diff] [blame] | 3131 | return 0; |
| 3132 | dprintk("NFSD: stale stateid " STATEID_FMT "!\n", |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3133 | STATEID_VAL(stateid)); |
J. Bruce Fields | e4e83ea | 2010-04-22 16:21:39 -0400 | [diff] [blame] | 3134 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | static inline int |
| 3138 | access_permit_read(unsigned long access_bmap) |
| 3139 | { |
| 3140 | return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) || |
| 3141 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) || |
| 3142 | test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap); |
| 3143 | } |
| 3144 | |
| 3145 | static inline int |
| 3146 | access_permit_write(unsigned long access_bmap) |
| 3147 | { |
| 3148 | return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) || |
| 3149 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap); |
| 3150 | } |
| 3151 | |
| 3152 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3153 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3155 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 3157 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 3158 | if (stp->st_openstp) |
| 3159 | stp = stp->st_openstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap))) |
| 3161 | goto out; |
| 3162 | if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap))) |
| 3163 | goto out; |
| 3164 | status = nfs_ok; |
| 3165 | out: |
| 3166 | return status; |
| 3167 | } |
| 3168 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3169 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags) |
| 3171 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3172 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | return nfs_ok; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3174 | else if (locks_in_grace()) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3175 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | * conflicting state; so we must wait out the grace period. */ |
| 3177 | return nfserr_grace; |
| 3178 | } else if (flags & WR_STATE) |
| 3179 | return nfs4_share_conflict(current_fh, |
| 3180 | NFS4_SHARE_DENY_WRITE); |
| 3181 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 3182 | return nfs4_share_conflict(current_fh, |
| 3183 | NFS4_SHARE_DENY_READ); |
| 3184 | } |
| 3185 | |
| 3186 | /* |
| 3187 | * Allow READ/WRITE during grace period on recovered state only for files |
| 3188 | * that are not able to provide mandatory locking. |
| 3189 | */ |
| 3190 | static inline int |
J. Bruce Fields | 18f8273 | 2009-02-21 15:23:01 -0800 | [diff] [blame] | 3191 | grace_disallows_io(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3193 | return locks_in_grace() && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3196 | /* Returns true iff a is later than b: */ |
| 3197 | static bool stateid_generation_after(stateid_t *a, stateid_t *b) |
| 3198 | { |
| 3199 | return (s32)a->si_generation - (s32)b->si_generation > 0; |
| 3200 | } |
| 3201 | |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 3202 | static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3203 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3204 | /* |
| 3205 | * When sessions are used the stateid generation number is ignored |
| 3206 | * when it is zero. |
| 3207 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 3208 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3209 | return nfs_ok; |
| 3210 | |
| 3211 | if (in->si_generation == ref->si_generation) |
| 3212 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 3213 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3214 | /* If the client sends us a stateid from the future, it's buggy: */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3215 | if (stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3216 | return nfserr_bad_stateid; |
| 3217 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3218 | * However, we could see a stateid from the past, even from a |
| 3219 | * non-buggy client. For example, if the client sends a lock |
| 3220 | * while some IO is outstanding, the lock may bump si_generation |
| 3221 | * while the IO is still in flight. The client could avoid that |
| 3222 | * situation by waiting for responses on all the IO requests, |
| 3223 | * but better performance may result in retrying IO that |
| 3224 | * receives an old_stateid error if requests are rarely |
| 3225 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3226 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 3227 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3228 | } |
| 3229 | |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 3230 | __be32 nfs4_validate_stateid(stateid_t *stateid, bool has_session) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3231 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3232 | struct nfs4_stid *s; |
| 3233 | struct nfs4_ol_stateid *ols; |
| 3234 | __be32 status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3235 | |
| 3236 | if (STALE_STATEID(stateid)) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3237 | return nfserr_stale_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3238 | |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3239 | s = find_stateid(stateid); |
| 3240 | if (!s) |
| 3241 | return nfserr_stale_stateid; |
| 3242 | status = check_stateid_generation(stateid, &s->sc_stateid, has_session); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3243 | if (status) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3244 | return status; |
| 3245 | if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID))) |
| 3246 | return nfs_ok; |
| 3247 | ols = openlockstateid(s); |
| 3248 | if (ols->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3249 | && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 3250 | return nfserr_bad_stateid; |
| 3251 | return nfs_ok; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3252 | } |
| 3253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | /* |
| 3255 | * Checks for stateid operations |
| 3256 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3257 | __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3258 | nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate, |
| 3259 | stateid_t *stateid, int flags, struct file **filpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | { |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3261 | struct nfs4_stid *s; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3262 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | struct nfs4_delegation *dp = NULL; |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3264 | struct svc_fh *current_fh = &cstate->current_fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3266 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3268 | if (filpp) |
| 3269 | *filpp = NULL; |
| 3270 | |
J. Bruce Fields | 18f8273 | 2009-02-21 15:23:01 -0800 | [diff] [blame] | 3271 | if (grace_disallows_io(ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | return nfserr_grace; |
| 3273 | |
| 3274 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3275 | return check_special_stateids(current_fh, stateid, flags); |
| 3276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | status = nfserr_stale_stateid; |
| 3278 | if (STALE_STATEID(stateid)) |
| 3279 | goto out; |
| 3280 | |
J. Bruce Fields | 3351514 | 2010-10-02 18:42:39 -0400 | [diff] [blame] | 3281 | /* |
| 3282 | * We assume that any stateid that has the current boot time, |
| 3283 | * but that we can't find, is expired: |
| 3284 | */ |
| 3285 | status = nfserr_expired; |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3286 | s = find_stateid(stateid); |
| 3287 | if (!s) |
| 3288 | goto out; |
| 3289 | status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate)); |
| 3290 | if (status) |
| 3291 | goto out; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3292 | switch (s->sc_type) { |
| 3293 | case NFS4_DELEG_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3294 | dp = delegstateid(s); |
J. Bruce Fields | dc9bf70 | 2009-02-21 11:14:43 -0800 | [diff] [blame] | 3295 | status = nfs4_check_delegmode(dp, flags); |
| 3296 | if (status) |
| 3297 | goto out; |
| 3298 | renew_client(dp->dl_client); |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 3299 | if (filpp) { |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 3300 | *filpp = dp->dl_file->fi_deleg_file; |
Dan Carpenter | 43b0178 | 2010-10-27 23:19:04 +0200 | [diff] [blame] | 3301 | BUG_ON(!*filpp); |
| 3302 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3303 | break; |
| 3304 | case NFS4_OPEN_STID: |
| 3305 | case NFS4_LOCK_STID: |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 3306 | stp = openlockstateid(s); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3307 | status = nfs4_check_fh(current_fh, stp); |
| 3308 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3310 | if (stp->st_stateowner->so_is_open_owner |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3311 | && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | goto out; |
J. Bruce Fields | a4455be | 2009-02-21 10:40:22 -0800 | [diff] [blame] | 3313 | status = nfs4_check_openmode(stp, flags); |
| 3314 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | goto out; |
| 3316 | renew_client(stp->st_stateowner->so_client); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3317 | if (filpp) { |
| 3318 | if (flags & RD_STATE) |
| 3319 | *filpp = find_readable_file(stp->st_file); |
| 3320 | else |
| 3321 | *filpp = find_writeable_file(stp->st_file); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3322 | } |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3323 | break; |
| 3324 | default: |
| 3325 | return nfserr_bad_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
| 3327 | status = nfs_ok; |
| 3328 | out: |
| 3329 | return status; |
| 3330 | } |
| 3331 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3332 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3333 | nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3334 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3335 | if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner))) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3336 | return nfserr_locks_held; |
| 3337 | release_lock_stateid(stp); |
| 3338 | return nfs_ok; |
| 3339 | } |
| 3340 | |
| 3341 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 3342 | * Test if the stateid is valid |
| 3343 | */ |
| 3344 | __be32 |
| 3345 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3346 | struct nfsd4_test_stateid *test_stateid) |
| 3347 | { |
| 3348 | test_stateid->ts_has_session = nfsd4_has_session(cstate); |
| 3349 | return nfs_ok; |
| 3350 | } |
| 3351 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3352 | __be32 |
| 3353 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3354 | struct nfsd4_free_stateid *free_stateid) |
| 3355 | { |
| 3356 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3357 | struct nfs4_stid *s; |
| 3358 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3359 | |
| 3360 | nfs4_lock_state(); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3361 | s = find_stateid(stateid); |
| 3362 | if (!s) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3363 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3364 | switch (s->sc_type) { |
| 3365 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3366 | ret = nfserr_locks_held; |
| 3367 | goto out; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 3368 | case NFS4_OPEN_STID: |
| 3369 | case NFS4_LOCK_STID: |
| 3370 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 3371 | if (ret) |
| 3372 | goto out; |
| 3373 | if (s->sc_type == NFS4_LOCK_STID) |
| 3374 | ret = nfsd4_free_lock_stateid(openlockstateid(s)); |
| 3375 | else |
| 3376 | ret = nfserr_locks_held; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3377 | break; |
| 3378 | default: |
| 3379 | ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3380 | } |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 3381 | out: |
| 3382 | nfs4_unlock_state(); |
| 3383 | return ret; |
| 3384 | } |
| 3385 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3386 | static inline int |
| 3387 | setlkflg (int type) |
| 3388 | { |
| 3389 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 3390 | RD_STATE : WR_STATE; |
| 3391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3393 | static __be32 nfs4_nospecial_stateid_checks(stateid_t *stateid) |
| 3394 | { |
| 3395 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3396 | return nfserr_bad_stateid; |
| 3397 | if (STALE_STATEID(stateid)) |
| 3398 | return nfserr_stale_stateid; |
| 3399 | return nfs_ok; |
| 3400 | } |
| 3401 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3402 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3403 | { |
| 3404 | struct svc_fh *current_fh = &cstate->current_fh; |
| 3405 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 3406 | __be32 status; |
| 3407 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3408 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 3409 | if (status) |
| 3410 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3411 | if (stp->st_stid.sc_type == NFS4_CLOSED_STID) |
| 3412 | /* |
| 3413 | * "Closed" stateid's exist *only* to return |
| 3414 | * nfserr_replay_me from the previous step. |
| 3415 | */ |
| 3416 | return nfserr_bad_stateid; |
| 3417 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
| 3418 | if (status) |
| 3419 | return status; |
| 3420 | return nfs4_check_fh(current_fh, stp); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3421 | } |
| 3422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | /* |
| 3424 | * Checks for sequence id mutating operations. |
| 3425 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3426 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3427 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 3428 | stateid_t *stateid, char typemask, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3429 | struct nfs4_ol_stateid **stpp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 3431 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3433 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 3434 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 3435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | *stpp = NULL; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3437 | status = nfs4_nospecial_stateid_checks(stateid); |
| 3438 | if (status) |
| 3439 | return status; |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3440 | *stpp = find_ol_stateid_by_type(stateid, typemask); |
J. Bruce Fields | f4dee24 | 2011-09-02 16:36:49 -0400 | [diff] [blame] | 3441 | if (*stpp == NULL) |
| 3442 | return nfserr_expired; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3443 | cstate->replay_owner = (*stpp)->st_stateowner; |
| 3444 | renew_client((*stpp)->st_stateowner->so_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3446 | return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp); |
| 3447 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 3448 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3449 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp) |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3450 | { |
| 3451 | __be32 status; |
| 3452 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3454 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 3455 | NFS4_OPEN_STID, stpp); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 3456 | if (status) |
| 3457 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3458 | oo = openowner((*stpp)->st_stateowner); |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3459 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 3460 | return nfserr_bad_stateid; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 3461 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3464 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3465 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 3466 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3468 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3469 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3470 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | |
| 3472 | dprintk("NFSD: nfsd4_open_confirm on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3473 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 3474 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3476 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 3477 | if (status) |
| 3478 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | |
| 3480 | nfs4_lock_state(); |
| 3481 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 3482 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3483 | oc->oc_seqid, &oc->oc_req_stateid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 3484 | NFS4_OPEN_STID, &stp); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 3485 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 3486 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3487 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 3488 | status = nfserr_bad_stateid; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3489 | if (oo->oo_flags & NFS4_OO_CONFIRMED) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 3490 | goto out; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 3491 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3492 | update_stateid(&stp->st_stid.sc_stateid); |
| 3493 | memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 3494 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3495 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 3496 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3497 | nfsd4_create_clid_dir(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 3498 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 3500 | if (!cstate->replay_owner) |
| 3501 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | return status; |
| 3503 | } |
| 3504 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3505 | static inline void nfs4_file_downgrade(struct nfs4_ol_stateid *stp, unsigned int to_access) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | { |
| 3507 | int i; |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 3508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3509 | for (i = 1; i < 4; i++) { |
J. Bruce Fields | 3d02fa2 | 2011-09-19 15:07:41 -0400 | [diff] [blame^] | 3510 | if (test_bit(i, &stp->st_access_bmap) |
| 3511 | && ((i & to_access) != i)) { |
| 3512 | nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i)); |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 3513 | __clear_bit(i, &stp->st_access_bmap); |
| 3514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | static void |
| 3519 | reset_union_bmap_deny(unsigned long deny, unsigned long *bmap) |
| 3520 | { |
| 3521 | int i; |
| 3522 | for (i = 0; i < 4; i++) { |
| 3523 | if ((i & deny) != i) |
| 3524 | __clear_bit(i, bmap); |
| 3525 | } |
| 3526 | } |
| 3527 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3528 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3529 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 3530 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 3531 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3533 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3534 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | |
| 3536 | dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3537 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 3538 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | |
Andy Adamson | d87a8ad | 2009-04-03 08:28:53 +0300 | [diff] [blame] | 3540 | if (!access_valid(od->od_share_access, cstate->minorversion) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 3541 | || !deny_valid(od->od_share_deny)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | return nfserr_inval; |
| 3543 | |
| 3544 | nfs4_lock_state(); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3545 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
| 3546 | &od->od_stateid, &stp); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 3547 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | status = nfserr_inval; |
| 3550 | if (!test_bit(od->od_share_access, &stp->st_access_bmap)) { |
| 3551 | dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n", |
| 3552 | stp->st_access_bmap, od->od_share_access); |
| 3553 | goto out; |
| 3554 | } |
| 3555 | if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) { |
| 3556 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", |
| 3557 | stp->st_deny_bmap, od->od_share_deny); |
| 3558 | goto out; |
| 3559 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 3560 | nfs4_file_downgrade(stp, od->od_share_access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap); |
| 3563 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3564 | update_stateid(&stp->st_stid.sc_stateid); |
| 3565 | memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | status = nfs_ok; |
| 3567 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 3568 | if (!cstate->replay_owner) |
| 3569 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | return status; |
| 3571 | } |
| 3572 | |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3573 | void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so) |
| 3574 | { |
| 3575 | struct nfs4_openowner *oo; |
| 3576 | struct nfs4_ol_stateid *s; |
| 3577 | |
| 3578 | if (!so->so_is_open_owner) |
| 3579 | return; |
| 3580 | oo = openowner(so); |
| 3581 | s = oo->oo_last_closed_stid; |
| 3582 | if (!s) |
| 3583 | return; |
| 3584 | if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) { |
| 3585 | /* Release the last_closed_stid on the next seqid bump: */ |
| 3586 | oo->oo_flags |= NFS4_OO_PURGE_CLOSE; |
| 3587 | return; |
| 3588 | } |
| 3589 | oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3590 | release_last_closed_stateid(oo); |
| 3591 | } |
| 3592 | |
| 3593 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 3594 | { |
| 3595 | unhash_open_stateid(s); |
| 3596 | s->st_stid.sc_type = NFS4_CLOSED_STID; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3597 | } |
| 3598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | /* |
| 3600 | * nfs4_unlock_state() called after encode |
| 3601 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3602 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3603 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 3604 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3606 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3607 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3608 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | |
| 3610 | dprintk("NFSD: nfsd4_close on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3611 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 3612 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3613 | |
| 3614 | nfs4_lock_state(); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3615 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 3616 | &close->cl_stateid, |
| 3617 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
| 3618 | &stp); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 3619 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3621 | oo = openowner(stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | status = nfs_ok; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3623 | update_stateid(&stp->st_stid.sc_stateid); |
| 3624 | memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 3626 | nfsd4_close_open_stateid(stp); |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 3627 | oo->oo_last_closed_stid = stp; |
J. Bruce Fields | 04ef595 | 2006-01-18 17:43:21 -0800 | [diff] [blame] | 3628 | |
| 3629 | /* place unused nfs4_stateowners on so_close_lru list to be |
| 3630 | * released by the laundromat service after the lease period |
| 3631 | * to enable us to handle CLOSE replay |
| 3632 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3633 | if (list_empty(&oo->oo_owner.so_stateids)) |
| 3634 | move_to_close_lru(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | out: |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 3636 | if (!cstate->replay_owner) |
| 3637 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | return status; |
| 3639 | } |
| 3640 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3641 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3642 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3643 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3645 | struct nfs4_delegation *dp; |
| 3646 | stateid_t *stateid = &dr->dr_stateid; |
| 3647 | struct inode *inode; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3648 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3650 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3651 | return status; |
| 3652 | inode = cstate->current_fh.fh_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | |
| 3654 | nfs4_lock_state(); |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3655 | status = nfserr_bad_stateid; |
| 3656 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 3657 | goto out; |
| 3658 | status = nfserr_stale_stateid; |
| 3659 | if (STALE_STATEID(stateid)) |
| 3660 | goto out; |
J. Bruce Fields | 3351514 | 2010-10-02 18:42:39 -0400 | [diff] [blame] | 3661 | status = nfserr_expired; |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 3662 | dp = find_deleg_stateid(stateid); |
J. Bruce Fields | e4e83ea | 2010-04-22 16:21:39 -0400 | [diff] [blame] | 3663 | if (!dp) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3664 | goto out; |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 3665 | status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate)); |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3666 | if (status) |
| 3667 | goto out; |
| 3668 | renew_client(dp->dl_client); |
| 3669 | |
| 3670 | unhash_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | out: |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 3672 | nfs4_unlock_state(); |
| 3673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | return status; |
| 3675 | } |
| 3676 | |
| 3677 | |
| 3678 | /* |
| 3679 | * Lock owner state (byte-range locks) |
| 3680 | */ |
| 3681 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
| 3682 | #define LOCK_HASH_BITS 8 |
| 3683 | #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS) |
| 3684 | #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1) |
| 3685 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 3686 | static inline u64 |
| 3687 | end_offset(u64 start, u64 len) |
| 3688 | { |
| 3689 | u64 end; |
| 3690 | |
| 3691 | end = start + len; |
| 3692 | return end >= start ? end: NFS4_MAX_UINT64; |
| 3693 | } |
| 3694 | |
| 3695 | /* last octet in a range */ |
| 3696 | static inline u64 |
| 3697 | last_byte_offset(u64 start, u64 len) |
| 3698 | { |
| 3699 | u64 end; |
| 3700 | |
| 3701 | BUG_ON(!len); |
| 3702 | end = start + len; |
| 3703 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 3704 | } |
| 3705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3706 | static inline unsigned int |
| 3707 | lock_ownerstr_hashval(struct inode *inode, u32 cl_id, |
| 3708 | struct xdr_netobj *ownername) |
| 3709 | { |
| 3710 | return (file_hashval(inode) + cl_id |
| 3711 | + opaque_hashval(ownername->data, ownername->len)) |
| 3712 | & LOCK_HASH_MASK; |
| 3713 | } |
| 3714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | /* |
| 3718 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 3719 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 3720 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 3721 | * locking, this prevents us from being completely protocol-compliant. The |
| 3722 | * real solution to this problem is to start using unsigned file offsets in |
| 3723 | * the VFS, but this is a very deep change! |
| 3724 | */ |
| 3725 | static inline void |
| 3726 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 3727 | { |
| 3728 | if (lock->fl_start < 0) |
| 3729 | lock->fl_start = OFFSET_MAX; |
| 3730 | if (lock->fl_end < 0) |
| 3731 | lock->fl_end = OFFSET_MAX; |
| 3732 | } |
| 3733 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3734 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 3735 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 3736 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3737 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | |
| 3739 | static inline void |
| 3740 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 3741 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3742 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3744 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3745 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
| 3746 | deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data, |
| 3747 | lo->lo_owner.so_owner.len, GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 3748 | if (!deny->ld_owner.data) |
| 3749 | /* We just don't care that much */ |
| 3750 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3751 | deny->ld_owner.len = lo->lo_owner.so_owner.len; |
| 3752 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3753 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 3754 | nevermind: |
| 3755 | deny->ld_owner.len = 0; |
| 3756 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3757 | deny->ld_clientid.cl_boot = 0; |
| 3758 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | } |
| 3760 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 3761 | deny->ld_length = NFS4_MAX_UINT64; |
| 3762 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 3764 | deny->ld_type = NFS4_READ_LT; |
| 3765 | if (fl->fl_type != F_RDLCK) |
| 3766 | deny->ld_type = NFS4_WRITE_LT; |
| 3767 | } |
| 3768 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3769 | static struct nfs4_lockowner * |
| 3770 | find_lockowner_str(struct inode *inode, clientid_t *clid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | struct xdr_netobj *owner) |
| 3772 | { |
| 3773 | unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner); |
| 3774 | struct nfs4_stateowner *op; |
| 3775 | |
| 3776 | list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 3777 | if (same_owner_str(op, owner, clid)) |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3778 | return lockowner(op); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | } |
| 3780 | return NULL; |
| 3781 | } |
| 3782 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3783 | static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp) |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3784 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3785 | list_add(&lo->lo_owner.so_strhash, &lock_ownerstr_hashtbl[strhashval]); |
| 3786 | list_add(&lo->lo_perstateid, &open_stp->st_lockowners); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 3787 | } |
| 3788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | /* |
| 3790 | * Alloc a lock owner structure. |
| 3791 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3792 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | * |
| 3794 | * strhashval = lock_ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | */ |
| 3796 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3797 | static struct nfs4_lockowner * |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3798 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3799 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3801 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 3802 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | return NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3804 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 3805 | lo->lo_owner.so_is_open_owner = 0; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 3806 | /* It is the openowner seqid that will be incremented in encode in the |
| 3807 | * case of new lockowners; so increment the lock seqid manually: */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3808 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1; |
| 3809 | hash_lockowner(lo, strhashval, clp, open_stp); |
| 3810 | return lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3813 | static struct nfs4_ol_stateid * |
| 3814 | alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3816 | struct nfs4_ol_stateid *stp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 3817 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 3819 | stp = nfs4_alloc_stateid(); |
| 3820 | if (stp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | goto out; |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 3822 | list_add(&stp->st_perfile, &fp->fi_stateids); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3823 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
| 3824 | stp->st_stateowner = &lo->lo_owner; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3825 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 3826 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | stp->st_file = fp; |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 3828 | stp->st_stid.sc_stateid.si_opaque.so_clid = clp->cl_clientid; |
| 3829 | stp->st_stid.sc_stateid.si_opaque.so_id = current_stateid++; |
J. Bruce Fields | 73997dc | 2011-08-31 15:47:21 -0400 | [diff] [blame] | 3830 | /* note will be incremented before first return to client: */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3831 | stp->st_stid.sc_stateid.si_generation = 0; |
J. Bruce Fields | 36d44c6 | 2011-09-08 12:16:03 -0400 | [diff] [blame] | 3832 | hash_stid(&stp->st_stid); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 3833 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 3835 | stp->st_openstp = open_stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | |
| 3837 | out: |
| 3838 | return stp; |
| 3839 | } |
| 3840 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3841 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | check_lock_length(u64 offset, u64 length) |
| 3843 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 3844 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | LOFF_OVERFLOW(offset, length))); |
| 3846 | } |
| 3847 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3848 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 3849 | { |
| 3850 | struct nfs4_file *fp = lock_stp->st_file; |
| 3851 | int oflag = nfs4_access_to_omode(access); |
| 3852 | |
| 3853 | if (test_bit(access, &lock_stp->st_access_bmap)) |
| 3854 | return; |
| 3855 | nfs4_file_get_access(fp, oflag); |
| 3856 | __set_bit(access, &lock_stp->st_access_bmap); |
| 3857 | } |
| 3858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | /* |
| 3860 | * LOCK operation |
| 3861 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3862 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3863 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 3864 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3866 | struct nfs4_openowner *open_sop = NULL; |
| 3867 | struct nfs4_lockowner *lock_sop = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3868 | struct nfs4_ol_stateid *lock_stp; |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 3869 | struct nfs4_file *fp; |
| 3870 | struct file *filp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | struct file_lock file_lock; |
Andy Adamson | 8dc7c31 | 2006-03-20 13:44:26 -0500 | [diff] [blame] | 3872 | struct file_lock conflock; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3873 | __be32 status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | unsigned int strhashval; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 3875 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 3876 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | |
| 3878 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 3879 | (long long) lock->lk_offset, |
| 3880 | (long long) lock->lk_length); |
| 3881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 3883 | return nfserr_inval; |
| 3884 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 3885 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 3886 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 3887 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 3888 | return status; |
| 3889 | } |
| 3890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | nfs4_lock_state(); |
| 3892 | |
| 3893 | if (lock->lk_is_new) { |
NeilBrown | 893f877 | 2005-07-07 17:59:17 -0700 | [diff] [blame] | 3894 | /* |
| 3895 | * Client indicates that this is a new lockowner. |
| 3896 | * Use open owner and open stateid to create lock owner and |
| 3897 | * lock stateid. |
| 3898 | */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 3899 | struct nfs4_ol_stateid *open_stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | |
| 3901 | status = nfserr_stale_clientid; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 3902 | if (!nfsd4_has_session(cstate) && |
| 3903 | STALE_CLIENTID(&lock->lk_new_clientid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3907 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | lock->lk_new_open_seqid, |
| 3909 | &lock->lk_new_open_stateid, |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 3910 | &open_stp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 3911 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3913 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 3914 | status = nfserr_bad_stateid; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 3915 | if (!nfsd4_has_session(cstate) && |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3916 | !same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 3917 | &lock->v.new.clientid)) |
| 3918 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | /* create lockowner and lock stateid */ |
| 3920 | fp = open_stp->st_file; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3921 | strhashval = lock_ownerstr_hashval(fp->fi_inode, |
| 3922 | open_sop->oo_owner.so_client->cl_clientid.cl_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | &lock->v.new.owner); |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3924 | /* XXX: Do we need to check for duplicate stateowners on |
| 3925 | * the same file, or should they just be allowed (and |
| 3926 | * create new stateids)? */ |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 3927 | status = nfserr_jukebox; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 3928 | lock_sop = alloc_init_lock_stateowner(strhashval, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3929 | open_sop->oo_owner.so_client, open_stp, lock); |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 3930 | if (lock_sop == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | goto out; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 3932 | lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp); |
J. Bruce Fields | 8a28051 | 2006-01-18 17:43:18 -0800 | [diff] [blame] | 3933 | if (lock_stp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | } else { |
| 3936 | /* lock (lock owner + lock stateid) already exists */ |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 3937 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3938 | lock->lk_old_lock_seqid, |
| 3939 | &lock->lk_old_lock_stateid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 3940 | NFS4_LOCK_STID, &lock_stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3941 | if (status) |
| 3942 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 3943 | lock_sop = lockowner(lock_stp->st_stateowner); |
J. Bruce Fields | 7d94784 | 2010-08-20 18:09:31 -0400 | [diff] [blame] | 3944 | fp = lock_stp->st_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | } |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 3946 | /* lock_sop and lock_stp have been created or found */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 3948 | lkflg = setlkflg(lock->lk_type); |
| 3949 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 3950 | if (status) |
| 3951 | goto out; |
| 3952 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 3953 | status = nfserr_grace; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3954 | if (locks_in_grace() && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 3955 | goto out; |
| 3956 | status = nfserr_no_grace; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3957 | if (!locks_in_grace() && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 3958 | goto out; |
| 3959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | locks_init_lock(&file_lock); |
| 3961 | switch (lock->lk_type) { |
| 3962 | case NFS4_READ_LT: |
| 3963 | case NFS4_READW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 3964 | filp = find_readable_file(lock_stp->st_file); |
| 3965 | if (filp) |
| 3966 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | file_lock.fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 3968 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | case NFS4_WRITE_LT: |
| 3970 | case NFS4_WRITEW_LT: |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 3971 | filp = find_writeable_file(lock_stp->st_file); |
| 3972 | if (filp) |
| 3973 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | file_lock.fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 3975 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | default: |
| 3977 | status = nfserr_inval; |
| 3978 | goto out; |
| 3979 | } |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 3980 | if (!filp) { |
| 3981 | status = nfserr_openmode; |
| 3982 | goto out; |
| 3983 | } |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 3984 | file_lock.fl_owner = (fl_owner_t)lock_sop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | file_lock.fl_pid = current->tgid; |
| 3986 | file_lock.fl_file = filp; |
| 3987 | file_lock.fl_flags = FL_POSIX; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 3988 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | |
| 3990 | file_lock.fl_start = lock->lk_offset; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 3991 | file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | nfs4_transform_lock_offset(&file_lock); |
| 3993 | |
| 3994 | /* |
| 3995 | * Try to lock the file in the VFS. |
| 3996 | * Note: locks.c uses the BKL to protect the inode's lock list. |
| 3997 | */ |
| 3998 | |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 3999 | err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4000 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | case 0: /* success! */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4002 | update_stateid(&lock_stp->st_stid.sc_stateid); |
| 4003 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4004 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4005 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4006 | break; |
| 4007 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 4008 | status = nfserr_denied; |
| 4009 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
| 4010 | nfs4_set_lock_denied(&conflock, &lock->lk_denied); |
| 4011 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | case (EDEADLK): |
| 4013 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4014 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4015 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4016 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 4017 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 4018 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | out: |
J. Bruce Fields | 8a28051 | 2006-01-18 17:43:18 -0800 | [diff] [blame] | 4021 | if (status && lock->lk_is_new && lock_sop) |
J. Bruce Fields | f044ff8 | 2009-01-11 15:24:04 -0500 | [diff] [blame] | 4022 | release_lockowner(lock_sop); |
J. Bruce Fields | 5ec094c | 2011-08-30 17:02:48 -0400 | [diff] [blame] | 4023 | if (!cstate->replay_owner) |
| 4024 | nfs4_unlock_state(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | return status; |
| 4026 | } |
| 4027 | |
| 4028 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4029 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 4030 | * so we do a temporary open here just to get an open file to pass to |
| 4031 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 4032 | * inode operation.) |
| 4033 | */ |
| 4034 | static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
| 4035 | { |
| 4036 | struct file *file; |
| 4037 | int err; |
| 4038 | |
| 4039 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 4040 | if (err) |
| 4041 | return err; |
| 4042 | err = vfs_test_lock(file, lock); |
| 4043 | nfsd_close(file); |
| 4044 | return err; |
| 4045 | } |
| 4046 | |
| 4047 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | * LOCKT operation |
| 4049 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4050 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4051 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 4052 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | { |
| 4054 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | struct file_lock file_lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4056 | struct nfs4_lockowner *lo; |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4057 | int error; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4058 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 4060 | if (locks_in_grace()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | return nfserr_grace; |
| 4062 | |
| 4063 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 4064 | return nfserr_inval; |
| 4065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | nfs4_lock_state(); |
| 4067 | |
| 4068 | status = nfserr_stale_clientid; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 4069 | if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 4072 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4075 | inode = cstate->current_fh.fh_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | locks_init_lock(&file_lock); |
| 4077 | switch (lockt->lt_type) { |
| 4078 | case NFS4_READ_LT: |
| 4079 | case NFS4_READW_LT: |
| 4080 | file_lock.fl_type = F_RDLCK; |
| 4081 | break; |
| 4082 | case NFS4_WRITE_LT: |
| 4083 | case NFS4_WRITEW_LT: |
| 4084 | file_lock.fl_type = F_WRLCK; |
| 4085 | break; |
| 4086 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 4087 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | status = nfserr_inval; |
| 4089 | goto out; |
| 4090 | } |
| 4091 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4092 | lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner); |
| 4093 | if (lo) |
| 4094 | file_lock.fl_owner = (fl_owner_t)lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | file_lock.fl_pid = current->tgid; |
| 4096 | file_lock.fl_flags = FL_POSIX; |
| 4097 | |
| 4098 | file_lock.fl_start = lockt->lt_offset; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4099 | file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | |
| 4101 | nfs4_transform_lock_offset(&file_lock); |
| 4102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4103 | status = nfs_ok; |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 4104 | error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock); |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4105 | if (error) { |
| 4106 | status = nfserrno(error); |
| 4107 | goto out; |
| 4108 | } |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 4109 | if (file_lock.fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | status = nfserr_denied; |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 4111 | nfs4_set_lock_denied(&file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | } |
| 4113 | out: |
| 4114 | nfs4_unlock_state(); |
| 4115 | return status; |
| 4116 | } |
| 4117 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4118 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 4119 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 4120 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | { |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4122 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | struct file *filp = NULL; |
| 4124 | struct file_lock file_lock; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4125 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4126 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | |
| 4128 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 4129 | (long long) locku->lu_offset, |
| 4130 | (long long) locku->lu_length); |
| 4131 | |
| 4132 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 4133 | return nfserr_inval; |
| 4134 | |
| 4135 | nfs4_lock_state(); |
| 4136 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4137 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 4138 | &locku->lu_stateid, NFS4_LOCK_STID, &stp); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 4139 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | goto out; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4141 | filp = find_any_file(stp->st_file); |
| 4142 | if (!filp) { |
| 4143 | status = nfserr_lock_range; |
| 4144 | goto out; |
| 4145 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | BUG_ON(!filp); |
| 4147 | locks_init_lock(&file_lock); |
| 4148 | file_lock.fl_type = F_UNLCK; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4149 | file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | file_lock.fl_pid = current->tgid; |
| 4151 | file_lock.fl_file = filp; |
| 4152 | file_lock.fl_flags = FL_POSIX; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 4153 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | file_lock.fl_start = locku->lu_offset; |
| 4155 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 4156 | file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | nfs4_transform_lock_offset(&file_lock); |
| 4158 | |
| 4159 | /* |
| 4160 | * Try to unlock the file in the VFS. |
| 4161 | */ |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4162 | err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4163 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 4164 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | goto out_nfserr; |
| 4166 | } |
| 4167 | /* |
| 4168 | * OK, unlock succeeded; the only thing left to do is update the stateid. |
| 4169 | */ |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4170 | update_stateid(&stp->st_stid.sc_stateid); |
| 4171 | memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
| 4173 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | nfs4_unlock_state(); |
| 4175 | return status; |
| 4176 | |
| 4177 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 4178 | status = nfserrno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | goto out; |
| 4180 | } |
| 4181 | |
| 4182 | /* |
| 4183 | * returns |
| 4184 | * 1: locks held by lockowner |
| 4185 | * 0: no locks held by lockowner |
| 4186 | */ |
| 4187 | static int |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4188 | check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | { |
| 4190 | struct file_lock **flpp; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 4191 | struct inode *inode = filp->fi_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | int status = 0; |
| 4193 | |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 4194 | lock_flocks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4196 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | status = 1; |
| 4198 | goto out; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 4199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | } |
| 4201 | out: |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 4202 | unlock_flocks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | return status; |
| 4204 | } |
| 4205 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4206 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4207 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 4208 | struct nfsd4_compound_state *cstate, |
| 4209 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | { |
| 4211 | clientid_t *clid = &rlockowner->rl_clientid; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4212 | struct nfs4_stateowner *sop; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4213 | struct nfs4_lockowner *lo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4214 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4216 | struct list_head matches; |
| 4217 | int i; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4218 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
| 4220 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 4221 | clid->cl_boot, clid->cl_id); |
| 4222 | |
| 4223 | /* XXX check for lease expiration */ |
| 4224 | |
| 4225 | status = nfserr_stale_clientid; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 4226 | if (STALE_CLIENTID(clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | |
| 4229 | nfs4_lock_state(); |
| 4230 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4231 | status = nfserr_locks_held; |
| 4232 | /* XXX: we're doing a linear search through all the lockowners. |
| 4233 | * Yipes! For now we'll just hope clients aren't really using |
| 4234 | * release_lockowner much, but eventually we have to fix these |
| 4235 | * data structures. */ |
| 4236 | INIT_LIST_HEAD(&matches); |
| 4237 | for (i = 0; i < LOCK_HASH_SIZE; i++) { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 4238 | list_for_each_entry(sop, &lock_ownerstr_hashtbl[i], so_strhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 4239 | if (!same_owner_str(sop, owner, clid)) |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4240 | continue; |
| 4241 | list_for_each_entry(stp, &sop->so_stateids, |
| 4242 | st_perstateowner) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4243 | lo = lockowner(sop); |
| 4244 | if (check_for_locks(stp->st_file, lo)) |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4245 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4246 | list_add(&lo->lo_list, &matches); |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 4249 | } |
| 4250 | /* Clients probably won't expect us to return with some (but not all) |
| 4251 | * of the lockowner state released; so don't release any until all |
| 4252 | * have been checked. */ |
| 4253 | status = nfs_ok; |
NeilBrown | 0fa822e | 2005-07-07 17:59:14 -0700 | [diff] [blame] | 4254 | while (!list_empty(&matches)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4255 | lo = list_entry(matches.next, struct nfs4_lockowner, |
| 4256 | lo_list); |
NeilBrown | 0fa822e | 2005-07-07 17:59:14 -0700 | [diff] [blame] | 4257 | /* unhash_stateowner deletes so_perclient only |
| 4258 | * for openowners. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4259 | list_del(&lo->lo_list); |
| 4260 | release_lockowner(lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | } |
| 4262 | out: |
| 4263 | nfs4_unlock_state(); |
| 4264 | return status; |
| 4265 | } |
| 4266 | |
| 4267 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4268 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4270 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4273 | int |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 4274 | nfs4_has_reclaimed_state(const char *name, bool use_exchange_id) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4275 | { |
| 4276 | unsigned int strhashval = clientstr_hashval(name); |
| 4277 | struct nfs4_client *clp; |
| 4278 | |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 4279 | clp = find_confirmed_client_by_str(name, strhashval); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 4280 | return clp ? 1 : 0; |
| 4281 | } |
| 4282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | /* |
| 4284 | * failure => all reset bets are off, nfserr_no_grace... |
| 4285 | */ |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4286 | int |
| 4287 | nfs4_client_to_reclaim(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | { |
| 4289 | unsigned int strhashval; |
| 4290 | struct nfs4_client_reclaim *crp = NULL; |
| 4291 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4292 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 4293 | crp = alloc_reclaim(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4294 | if (!crp) |
| 4295 | return 0; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4296 | strhashval = clientstr_hashval(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | INIT_LIST_HEAD(&crp->cr_strhash); |
| 4298 | list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4299 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | reclaim_str_hashtbl_size++; |
| 4301 | return 1; |
| 4302 | } |
| 4303 | |
| 4304 | static void |
| 4305 | nfs4_release_reclaim(void) |
| 4306 | { |
| 4307 | struct nfs4_client_reclaim *crp = NULL; |
| 4308 | int i; |
| 4309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 4311 | while (!list_empty(&reclaim_str_hashtbl[i])) { |
| 4312 | crp = list_entry(reclaim_str_hashtbl[i].next, |
| 4313 | struct nfs4_client_reclaim, cr_strhash); |
| 4314 | list_del(&crp->cr_strhash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | kfree(crp); |
| 4316 | reclaim_str_hashtbl_size--; |
| 4317 | } |
| 4318 | } |
| 4319 | BUG_ON(reclaim_str_hashtbl_size); |
| 4320 | } |
| 4321 | |
| 4322 | /* |
| 4323 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 4324 | static struct nfs4_client_reclaim * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | nfs4_find_reclaim_client(clientid_t *clid) |
| 4326 | { |
| 4327 | unsigned int strhashval; |
| 4328 | struct nfs4_client *clp; |
| 4329 | struct nfs4_client_reclaim *crp = NULL; |
| 4330 | |
| 4331 | |
| 4332 | /* find clientid in conf_id_hashtbl */ |
| 4333 | clp = find_confirmed_client(clid); |
| 4334 | if (clp == NULL) |
| 4335 | return NULL; |
| 4336 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4337 | dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n", |
| 4338 | clp->cl_name.len, clp->cl_name.data, |
| 4339 | clp->cl_recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | |
| 4341 | /* find clp->cl_name in reclaim_str_hashtbl */ |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4342 | strhashval = clientstr_hashval(clp->cl_recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 4344 | if (same_name(crp->cr_recdir, clp->cl_recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | return crp; |
| 4346 | } |
| 4347 | } |
| 4348 | return NULL; |
| 4349 | } |
| 4350 | |
| 4351 | /* |
| 4352 | * Called from OPEN. Look for clientid in reclaim list. |
| 4353 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4354 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | nfs4_check_open_reclaim(clientid_t *clid) |
| 4356 | { |
NeilBrown | dfc8356 | 2005-06-23 22:03:16 -0700 | [diff] [blame] | 4357 | return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4360 | /* initialization to perform at module load time: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4362 | int |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4363 | nfs4_state_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | { |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4365 | int i, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4367 | status = nfsd4_init_slabs(); |
| 4368 | if (status) |
| 4369 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 4371 | INIT_LIST_HEAD(&conf_id_hashtbl[i]); |
| 4372 | INIT_LIST_HEAD(&conf_str_hashtbl[i]); |
| 4373 | INIT_LIST_HEAD(&unconf_str_hashtbl[i]); |
| 4374 | INIT_LIST_HEAD(&unconf_id_hashtbl[i]); |
Wang Chen | 02cb285 | 2009-04-24 15:41:57 +0800 | [diff] [blame] | 4375 | INIT_LIST_HEAD(&reclaim_str_hashtbl[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 4377 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 4378 | INIT_LIST_HEAD(&sessionid_hashtbl[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | for (i = 0; i < FILE_HASH_SIZE; i++) { |
| 4380 | INIT_LIST_HEAD(&file_hashtbl[i]); |
| 4381 | } |
J. Bruce Fields | 506f275 | 2011-08-11 18:50:18 -0400 | [diff] [blame] | 4382 | for (i = 0; i < OPEN_OWNER_HASH_SIZE; i++) { |
| 4383 | INIT_LIST_HEAD(&open_ownerstr_hashtbl[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | } |
J. Bruce Fields | b79abad | 2011-08-22 18:01:43 -0400 | [diff] [blame] | 4385 | for (i = 0; i < STATEID_HASH_SIZE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | INIT_LIST_HEAD(&stateid_hashtbl[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | for (i = 0; i < LOCK_HASH_SIZE; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]); |
| 4389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | memset(&onestateid, ~0, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | INIT_LIST_HEAD(&close_lru); |
| 4392 | INIT_LIST_HEAD(&client_lru); |
| 4393 | INIT_LIST_HEAD(&del_recall_lru); |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4394 | reclaim_str_hashtbl_size = 0; |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4395 | return 0; |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4396 | } |
| 4397 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4398 | static void |
| 4399 | nfsd4_load_reboot_recovery_data(void) |
| 4400 | { |
| 4401 | int status; |
| 4402 | |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 4403 | nfs4_lock_state(); |
J. Bruce Fields | 48483bf | 2011-08-26 20:40:28 -0400 | [diff] [blame] | 4404 | nfsd4_init_recdir(); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4405 | status = nfsd4_recdir_load(); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 4406 | nfs4_unlock_state(); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4407 | if (status) |
| 4408 | printk("NFSD: Failure reading reboot recovery data\n"); |
| 4409 | } |
| 4410 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 4411 | /* |
| 4412 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 4413 | * client may quite reasonably hang on to a delegation as long as it has |
| 4414 | * the inode cached. This becomes an obvious problem the first time a |
| 4415 | * client's inode cache approaches the size of the server's total memory. |
| 4416 | * |
| 4417 | * For now we avoid this problem by imposing a hard limit on the number |
| 4418 | * of delegations, which varies according to the server's memory size. |
| 4419 | */ |
| 4420 | static void |
| 4421 | set_max_delegations(void) |
| 4422 | { |
| 4423 | /* |
| 4424 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 4425 | * estimates suggest that in the worst case (where every delegation |
| 4426 | * is for a different inode), a delegation could take about 1.5K, |
| 4427 | * giving a worst case usage of about 6% of memory. |
| 4428 | */ |
| 4429 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 4430 | } |
| 4431 | |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4432 | /* initialization to perform when the nfsd service is started: */ |
| 4433 | |
J. Bruce Fields | 29ab23c | 2009-09-15 15:56:50 -0400 | [diff] [blame] | 4434 | static int |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 4435 | __nfs4_state_start(void) |
| 4436 | { |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 4437 | int ret; |
| 4438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | boot_time = get_seconds(); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 4440 | locks_start_grace(&nfsd4_manager); |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 4441 | printk(KERN_INFO "NFSD: starting %ld-second grace period\n", |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 4442 | nfsd4_grace); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 4443 | ret = set_callback_cred(); |
| 4444 | if (ret) |
| 4445 | return -ENOMEM; |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 4446 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
J. Bruce Fields | 29ab23c | 2009-09-15 15:56:50 -0400 | [diff] [blame] | 4447 | if (laundry_wq == NULL) |
| 4448 | return -ENOMEM; |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 4449 | ret = nfsd4_create_callback_queue(); |
| 4450 | if (ret) |
| 4451 | goto out_free_laundry; |
J. Bruce Fields | e46b498 | 2010-03-01 19:21:21 -0500 | [diff] [blame] | 4452 | queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ); |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 4453 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 4454 | return 0; |
| 4455 | out_free_laundry: |
| 4456 | destroy_workqueue(laundry_wq); |
| 4457 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | } |
| 4459 | |
J. Bruce Fields | 29ab23c | 2009-09-15 15:56:50 -0400 | [diff] [blame] | 4460 | int |
NeilBrown | 76a3550 | 2005-06-23 22:03:26 -0700 | [diff] [blame] | 4461 | nfs4_state_start(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | { |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4463 | nfsd4_load_reboot_recovery_data(); |
Jeff Layton | 4ad9a34 | 2010-07-19 16:50:04 -0400 | [diff] [blame] | 4464 | return __nfs4_state_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | } |
| 4466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | static void |
| 4468 | __nfs4_state_shutdown(void) |
| 4469 | { |
| 4470 | int i; |
| 4471 | struct nfs4_client *clp = NULL; |
| 4472 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | struct list_head *pos, *next, reaplist; |
| 4474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 4476 | while (!list_empty(&conf_id_hashtbl[i])) { |
| 4477 | clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 4478 | expire_client(clp); |
| 4479 | } |
| 4480 | while (!list_empty(&unconf_str_hashtbl[i])) { |
| 4481 | clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash); |
| 4482 | expire_client(clp); |
| 4483 | } |
| 4484 | } |
| 4485 | INIT_LIST_HEAD(&reaplist); |
| 4486 | spin_lock(&recall_lock); |
| 4487 | list_for_each_safe(pos, next, &del_recall_lru) { |
| 4488 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4489 | list_move(&dp->dl_recall_lru, &reaplist); |
| 4490 | } |
| 4491 | spin_unlock(&recall_lock); |
| 4492 | list_for_each_safe(pos, next, &reaplist) { |
| 4493 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 4494 | list_del_init(&dp->dl_recall_lru); |
| 4495 | unhash_delegation(dp); |
| 4496 | } |
| 4497 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 4498 | nfsd4_shutdown_recdir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | void |
| 4502 | nfs4_state_shutdown(void) |
| 4503 | { |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 4504 | cancel_delayed_work_sync(&laundromat_work); |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 4505 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | 2c5e761 | 2008-11-20 14:36:17 -0600 | [diff] [blame] | 4506 | locks_end_grace(&nfsd4_manager); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | nfs4_lock_state(); |
| 4508 | nfs4_release_reclaim(); |
| 4509 | __nfs4_state_shutdown(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | nfs4_unlock_state(); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 4511 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | } |