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