Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfsd/nfs4state.c |
| 3 | * |
| 4 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Kendrick Smith <kmsmith@umich.edu> |
| 8 | * Andy Adamson <kandros@umich.edu> |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * 3. Neither the name of the University nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 24 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 26 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 30 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <linux/param.h> |
| 38 | #include <linux/major.h> |
| 39 | #include <linux/slab.h> |
| 40 | |
| 41 | #include <linux/sunrpc/svc.h> |
| 42 | #include <linux/nfsd/nfsd.h> |
| 43 | #include <linux/nfsd/cache.h> |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 44 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/mount.h> |
| 46 | #include <linux/workqueue.h> |
| 47 | #include <linux/smp_lock.h> |
| 48 | #include <linux/kthread.h> |
| 49 | #include <linux/nfs4.h> |
| 50 | #include <linux/nfsd/state.h> |
| 51 | #include <linux/nfsd/xdr4.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 52 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 53 | #include <linux/swap.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 54 | #include <linux/mutex.h> |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 55 | #include <linux/lockd/bind.h> |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 56 | #include <linux/module.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 57 | #include <linux/sunrpc/svcauth_gss.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 60 | |
| 61 | /* Globals */ |
| 62 | static time_t lease_time = 90; /* default lease time */ |
NeilBrown | d99a05a | 2005-06-23 22:03:21 -0700 | [diff] [blame] | 63 | static time_t user_lease_time = 90; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 64 | static time_t boot_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static u32 current_ownerid = 1; |
| 66 | static u32 current_fileid = 1; |
| 67 | static u32 current_delegid = 1; |
| 68 | static u32 nfs4_init; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 69 | static stateid_t zerostateid; /* bits all 0 */ |
| 70 | static stateid_t onestateid; /* bits all 1 */ |
| 71 | |
| 72 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t))) |
| 73 | #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* forward declarations */ |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 76 | static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 78 | static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; |
| 79 | static void nfs4_set_recdir(char *recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* Locking: |
| 82 | * |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 83 | * client_mutex: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * protects clientid_hashtbl[], clientstr_hashtbl[], |
| 85 | * unconfstr_hashtbl[], uncofid_hashtbl[]. |
| 86 | */ |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 87 | static DEFINE_MUTEX(client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 89 | static struct kmem_cache *stateowner_slab = NULL; |
| 90 | static struct kmem_cache *file_slab = NULL; |
| 91 | static struct kmem_cache *stateid_slab = NULL; |
| 92 | static struct kmem_cache *deleg_slab = NULL; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | void |
| 95 | nfs4_lock_state(void) |
| 96 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 97 | mutex_lock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | nfs4_unlock_state(void) |
| 102 | { |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 103 | mutex_unlock(&client_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline u32 |
| 107 | opaque_hashval(const void *ptr, int nbytes) |
| 108 | { |
| 109 | unsigned char *cptr = (unsigned char *) ptr; |
| 110 | |
| 111 | u32 x = 0; |
| 112 | while (nbytes--) { |
| 113 | x *= 37; |
| 114 | x += *cptr++; |
| 115 | } |
| 116 | return x; |
| 117 | } |
| 118 | |
| 119 | /* forward declarations */ |
| 120 | static void release_stateowner(struct nfs4_stateowner *sop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * Delegation state |
| 124 | */ |
| 125 | |
| 126 | /* recall_lock protects the del_recall_lru */ |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 127 | static DEFINE_SPINLOCK(recall_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | static struct list_head del_recall_lru; |
| 129 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 130 | static void |
| 131 | free_nfs4_file(struct kref *kref) |
| 132 | { |
| 133 | struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref); |
| 134 | list_del(&fp->fi_hash); |
| 135 | iput(fp->fi_inode); |
| 136 | kmem_cache_free(file_slab, fp); |
| 137 | } |
| 138 | |
| 139 | static inline void |
| 140 | put_nfs4_file(struct nfs4_file *fi) |
| 141 | { |
| 142 | kref_put(&fi->fi_ref, free_nfs4_file); |
| 143 | } |
| 144 | |
| 145 | static inline void |
| 146 | get_nfs4_file(struct nfs4_file *fi) |
| 147 | { |
| 148 | kref_get(&fi->fi_ref); |
| 149 | } |
| 150 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 151 | static int num_delegations; |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 152 | unsigned int max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Open owner state (share locks) |
| 156 | */ |
| 157 | |
| 158 | /* hash tables for nfs4_stateowner */ |
| 159 | #define OWNER_HASH_BITS 8 |
| 160 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 161 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
| 162 | |
| 163 | #define ownerid_hashval(id) \ |
| 164 | ((id) & OWNER_HASH_MASK) |
| 165 | #define ownerstr_hashval(clientid, ownername) \ |
| 166 | (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK) |
| 167 | |
| 168 | static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE]; |
| 169 | static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE]; |
| 170 | |
| 171 | /* hash table for nfs4_file */ |
| 172 | #define FILE_HASH_BITS 8 |
| 173 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
| 174 | #define FILE_HASH_MASK (FILE_HASH_SIZE - 1) |
| 175 | /* hash table for (open)nfs4_stateid */ |
| 176 | #define STATEID_HASH_BITS 10 |
| 177 | #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS) |
| 178 | #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1) |
| 179 | |
| 180 | #define file_hashval(x) \ |
| 181 | hash_ptr(x, FILE_HASH_BITS) |
| 182 | #define stateid_hashval(owner_id, file_id) \ |
| 183 | (((owner_id) + (file_id)) & STATEID_HASH_MASK) |
| 184 | |
| 185 | static struct list_head file_hashtbl[FILE_HASH_SIZE]; |
| 186 | static struct list_head stateid_hashtbl[STATEID_HASH_SIZE]; |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static struct nfs4_delegation * |
| 189 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type) |
| 190 | { |
| 191 | struct nfs4_delegation *dp; |
| 192 | struct nfs4_file *fp = stp->st_file; |
| 193 | struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback; |
| 194 | |
| 195 | dprintk("NFSD alloc_init_deleg\n"); |
Meelap Shah | 47f9940 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 196 | if (fp->fi_had_conflict) |
| 197 | return NULL; |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 198 | if (num_delegations > max_delegations) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 199 | return NULL; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 200 | dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL); |
| 201 | if (dp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | return dp; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 203 | num_delegations++; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 204 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 205 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
| 207 | dp->dl_client = clp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 208 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | dp->dl_file = fp; |
| 210 | dp->dl_flock = NULL; |
| 211 | get_file(stp->st_vfs_file); |
| 212 | dp->dl_vfs_file = stp->st_vfs_file; |
| 213 | dp->dl_type = type; |
| 214 | dp->dl_recall.cbr_dp = NULL; |
| 215 | dp->dl_recall.cbr_ident = cb->cb_ident; |
| 216 | dp->dl_recall.cbr_trunc = 0; |
| 217 | dp->dl_stateid.si_boot = boot_time; |
| 218 | dp->dl_stateid.si_stateownerid = current_delegid++; |
| 219 | dp->dl_stateid.si_fileid = 0; |
| 220 | dp->dl_stateid.si_generation = 0; |
| 221 | dp->dl_fhlen = current_fh->fh_handle.fh_size; |
| 222 | memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base, |
| 223 | current_fh->fh_handle.fh_size); |
| 224 | dp->dl_time = 0; |
| 225 | atomic_set(&dp->dl_count, 1); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 226 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
| 227 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | return dp; |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | nfs4_put_delegation(struct nfs4_delegation *dp) |
| 233 | { |
| 234 | if (atomic_dec_and_test(&dp->dl_count)) { |
| 235 | dprintk("NFSD: freeing dp %p\n",dp); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 236 | put_nfs4_file(dp->dl_file); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 237 | kmem_cache_free(deleg_slab, dp); |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 238 | num_delegations--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | /* Remove the associated file_lock first, then remove the delegation. |
| 243 | * lease_modify() is called to remove the FS_LEASE file_lock from |
| 244 | * the i_flock list, eventually calling nfsd's lock_manager |
| 245 | * fl_release_callback. |
| 246 | */ |
| 247 | static void |
| 248 | nfs4_close_delegation(struct nfs4_delegation *dp) |
| 249 | { |
| 250 | struct file *filp = dp->dl_vfs_file; |
| 251 | |
| 252 | dprintk("NFSD: close_delegation dp %p\n",dp); |
| 253 | dp->dl_vfs_file = NULL; |
| 254 | /* The following nfsd_close may not actually close the file, |
| 255 | * but we want to remove the lease in any case. */ |
NeilBrown | c907132 | 2005-04-16 15:26:38 -0700 | [diff] [blame] | 256 | if (dp->dl_flock) |
J. Bruce Fields | a9933ce | 2007-06-07 17:09:49 -0400 | [diff] [blame] | 257 | vfs_setlease(filp, F_UNLCK, &dp->dl_flock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | nfsd_close(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* Called under the state lock. */ |
| 262 | static void |
| 263 | unhash_delegation(struct nfs4_delegation *dp) |
| 264 | { |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 265 | list_del_init(&dp->dl_perfile); |
| 266 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | spin_lock(&recall_lock); |
| 268 | list_del_init(&dp->dl_recall_lru); |
| 269 | spin_unlock(&recall_lock); |
| 270 | nfs4_close_delegation(dp); |
| 271 | nfs4_put_delegation(dp); |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * SETCLIENTID state |
| 276 | */ |
| 277 | |
| 278 | /* Hash tables for nfs4_clientid state */ |
| 279 | #define CLIENT_HASH_BITS 4 |
| 280 | #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS) |
| 281 | #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1) |
| 282 | |
| 283 | #define clientid_hashval(id) \ |
| 284 | ((id) & CLIENT_HASH_MASK) |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 285 | #define clientstr_hashval(name) \ |
| 286 | (opaque_hashval((name), 8) & CLIENT_HASH_MASK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | /* |
| 288 | * reclaim_str_hashtbl[] holds known client info from previous reset/reboot |
| 289 | * used in reboot/reset lease grace period processing |
| 290 | * |
| 291 | * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed |
| 292 | * setclientid_confirmed info. |
| 293 | * |
| 294 | * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed |
| 295 | * setclientid info. |
| 296 | * |
| 297 | * client_lru holds client queue ordered by nfs4_client.cl_time |
| 298 | * for lease renewal. |
| 299 | * |
| 300 | * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time |
| 301 | * for last close replay. |
| 302 | */ |
| 303 | static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE]; |
| 304 | static int reclaim_str_hashtbl_size = 0; |
| 305 | static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE]; |
| 306 | static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE]; |
| 307 | static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE]; |
| 308 | static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE]; |
| 309 | static struct list_head client_lru; |
| 310 | static struct list_head close_lru; |
| 311 | |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 312 | static void unhash_generic_stateid(struct nfs4_stateid *stp) |
| 313 | { |
| 314 | list_del(&stp->st_hash); |
| 315 | list_del(&stp->st_perfile); |
| 316 | list_del(&stp->st_perstateowner); |
| 317 | } |
| 318 | |
| 319 | static void free_generic_stateid(struct nfs4_stateid *stp) |
| 320 | { |
| 321 | put_nfs4_file(stp->st_file); |
| 322 | kmem_cache_free(stateid_slab, stp); |
| 323 | } |
| 324 | |
| 325 | static void release_lock_stateid(struct nfs4_stateid *stp) |
| 326 | { |
| 327 | unhash_generic_stateid(stp); |
| 328 | locks_remove_posix(stp->st_vfs_file, (fl_owner_t)stp->st_stateowner); |
| 329 | free_generic_stateid(stp); |
| 330 | } |
| 331 | |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame^] | 332 | static void |
| 333 | release_stateid_lockowners(struct nfs4_stateid *open_stp) |
| 334 | { |
| 335 | struct nfs4_stateowner *lock_sop; |
| 336 | |
| 337 | while (!list_empty(&open_stp->st_lockowners)) { |
| 338 | lock_sop = list_entry(open_stp->st_lockowners.next, |
| 339 | struct nfs4_stateowner, so_perstateid); |
| 340 | /* list_del(&open_stp->st_lockowners); */ |
| 341 | BUG_ON(lock_sop->so_is_open_owner); |
| 342 | release_stateowner(lock_sop); |
| 343 | } |
| 344 | } |
| 345 | |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 346 | static void release_open_stateid(struct nfs4_stateid *stp) |
| 347 | { |
| 348 | unhash_generic_stateid(stp); |
| 349 | release_stateid_lockowners(stp); |
| 350 | nfsd_close(stp->st_vfs_file); |
| 351 | free_generic_stateid(stp); |
| 352 | } |
| 353 | |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame^] | 354 | static void |
| 355 | unhash_stateowner(struct nfs4_stateowner *sop) |
| 356 | { |
| 357 | struct nfs4_stateid *stp; |
| 358 | |
| 359 | list_del(&sop->so_idhash); |
| 360 | list_del(&sop->so_strhash); |
| 361 | if (sop->so_is_open_owner) |
| 362 | list_del(&sop->so_perclient); |
| 363 | list_del(&sop->so_perstateid); |
| 364 | while (!list_empty(&sop->so_stateids)) { |
| 365 | stp = list_entry(sop->so_stateids.next, |
| 366 | struct nfs4_stateid, st_perstateowner); |
| 367 | if (sop->so_is_open_owner) |
| 368 | release_open_stateid(stp); |
| 369 | else |
| 370 | release_lock_stateid(stp); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void |
| 375 | release_stateowner(struct nfs4_stateowner *sop) |
| 376 | { |
| 377 | unhash_stateowner(sop); |
| 378 | list_del(&sop->so_close_lru); |
| 379 | nfs4_put_stateowner(sop); |
| 380 | } |
| 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | static inline void |
| 383 | renew_client(struct nfs4_client *clp) |
| 384 | { |
| 385 | /* |
| 386 | * Move client to the end to the LRU list. |
| 387 | */ |
| 388 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 389 | clp->cl_clientid.cl_boot, |
| 390 | clp->cl_clientid.cl_id); |
| 391 | list_move_tail(&clp->cl_lru, &client_lru); |
| 392 | clp->cl_time = get_seconds(); |
| 393 | } |
| 394 | |
| 395 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 396 | static int |
| 397 | STALE_CLIENTID(clientid_t *clid) |
| 398 | { |
| 399 | if (clid->cl_boot == boot_time) |
| 400 | return 0; |
| 401 | dprintk("NFSD stale clientid (%08x/%08x)\n", |
| 402 | clid->cl_boot, clid->cl_id); |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * XXX Should we use a slab cache ? |
| 408 | * This type of memory management is somewhat inefficient, but we use it |
| 409 | * anyway since SETCLIENTID is not a common operation. |
| 410 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 411 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct nfs4_client *clp; |
| 414 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 415 | clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL); |
| 416 | if (clp == NULL) |
| 417 | return NULL; |
| 418 | clp->cl_name.data = kmalloc(name.len, GFP_KERNEL); |
| 419 | if (clp->cl_name.data == NULL) { |
| 420 | kfree(clp); |
| 421 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 423 | memcpy(clp->cl_name.data, name.data, name.len); |
| 424 | clp->cl_name.len = name.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return clp; |
| 426 | } |
| 427 | |
J. Bruce Fields | 1b1a9b3 | 2007-09-12 08:43:59 -0400 | [diff] [blame] | 428 | static void |
| 429 | shutdown_callback_client(struct nfs4_client *clp) |
| 430 | { |
| 431 | struct rpc_clnt *clnt = clp->cl_callback.cb_client; |
| 432 | |
J. Bruce Fields | 1b1a9b3 | 2007-09-12 08:43:59 -0400 | [diff] [blame] | 433 | if (clnt) { |
J. Bruce Fields | 404ec11 | 2007-11-23 22:26:18 -0500 | [diff] [blame] | 434 | /* |
| 435 | * Callback threads take a reference on the client, so there |
| 436 | * should be no outstanding callbacks at this point. |
| 437 | */ |
J. Bruce Fields | 1b1a9b3 | 2007-09-12 08:43:59 -0400 | [diff] [blame] | 438 | clp->cl_callback.cb_client = NULL; |
| 439 | rpc_shutdown_client(clnt); |
| 440 | } |
| 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | static inline void |
| 444 | free_client(struct nfs4_client *clp) |
| 445 | { |
J. Bruce Fields | 1b1a9b3 | 2007-09-12 08:43:59 -0400 | [diff] [blame] | 446 | shutdown_callback_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (clp->cl_cred.cr_group_info) |
| 448 | put_group_info(clp->cl_cred.cr_group_info); |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 449 | kfree(clp->cl_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | kfree(clp->cl_name.data); |
| 451 | kfree(clp); |
| 452 | } |
| 453 | |
| 454 | void |
| 455 | put_nfs4_client(struct nfs4_client *clp) |
| 456 | { |
| 457 | if (atomic_dec_and_test(&clp->cl_count)) |
| 458 | free_client(clp); |
| 459 | } |
| 460 | |
| 461 | static void |
| 462 | expire_client(struct nfs4_client *clp) |
| 463 | { |
| 464 | struct nfs4_stateowner *sop; |
| 465 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | struct list_head reaplist; |
| 467 | |
| 468 | dprintk("NFSD: expire_client cl_count %d\n", |
| 469 | atomic_read(&clp->cl_count)); |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | INIT_LIST_HEAD(&reaplist); |
| 472 | spin_lock(&recall_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 473 | while (!list_empty(&clp->cl_delegations)) { |
| 474 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | dprintk("NFSD: expire client. dp %p, fp %p\n", dp, |
| 476 | dp->dl_flock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 477 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | list_move(&dp->dl_recall_lru, &reaplist); |
| 479 | } |
| 480 | spin_unlock(&recall_lock); |
| 481 | while (!list_empty(&reaplist)) { |
| 482 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
| 483 | list_del_init(&dp->dl_recall_lru); |
| 484 | unhash_delegation(dp); |
| 485 | } |
| 486 | list_del(&clp->cl_idhash); |
| 487 | list_del(&clp->cl_strhash); |
| 488 | list_del(&clp->cl_lru); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 489 | while (!list_empty(&clp->cl_openowners)) { |
| 490 | sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | release_stateowner(sop); |
| 492 | } |
| 493 | put_nfs4_client(clp); |
| 494 | } |
| 495 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 496 | static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir) |
| 497 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | struct nfs4_client *clp; |
| 499 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 500 | clp = alloc_client(name); |
| 501 | if (clp == NULL) |
| 502 | return NULL; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 503 | memcpy(clp->cl_recdir, recdir, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | atomic_set(&clp->cl_count, 1); |
| 505 | atomic_set(&clp->cl_callback.cb_set, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 507 | INIT_LIST_HEAD(&clp->cl_strhash); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 508 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 509 | INIT_LIST_HEAD(&clp->cl_delegations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | INIT_LIST_HEAD(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return clp; |
| 512 | } |
| 513 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 514 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 515 | { |
| 516 | memcpy(target->cl_verifier.data, source->data, |
| 517 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 520 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 521 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 523 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 524 | } |
| 525 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 526 | static void copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 527 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | target->cr_uid = source->cr_uid; |
| 529 | target->cr_gid = source->cr_gid; |
| 530 | target->cr_group_info = source->cr_group_info; |
| 531 | get_group_info(target->cr_group_info); |
| 532 | } |
| 533 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 534 | static int same_name(const char *n1, const char *n2) |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 535 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 536 | return 0 == memcmp(n1, n2, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 540 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 541 | { |
| 542 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 546 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 547 | { |
| 548 | 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] | 549 | } |
| 550 | |
| 551 | /* XXX what about NGROUP */ |
| 552 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 553 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 554 | { |
| 555 | return cr1->cr_uid == cr2->cr_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
J. Bruce Fields | 5ec7b46 | 2007-11-21 21:58:56 -0500 | [diff] [blame] | 558 | static void gen_clid(struct nfs4_client *clp) |
| 559 | { |
| 560 | static u32 current_clientid = 1; |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | clp->cl_clientid.cl_boot = boot_time; |
| 563 | clp->cl_clientid.cl_id = current_clientid++; |
| 564 | } |
| 565 | |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 566 | static void gen_confirm(struct nfs4_client *clp) |
| 567 | { |
| 568 | static u32 i; |
| 569 | u32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | p = (u32 *)clp->cl_confirm.data; |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 572 | *p++ = get_seconds(); |
| 573 | *p++ = i++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 576 | static int check_name(struct xdr_netobj name) |
| 577 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | if (name.len == 0) |
| 579 | return 0; |
| 580 | if (name.len > NFS4_OPAQUE_LIMIT) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 581 | dprintk("NFSD: check_name: name too long(%d)!\n", name.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 587 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval) |
| 589 | { |
| 590 | unsigned int idhashval; |
| 591 | |
| 592 | list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]); |
| 593 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 594 | list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]); |
| 595 | list_add_tail(&clp->cl_lru, &client_lru); |
| 596 | clp->cl_time = get_seconds(); |
| 597 | } |
| 598 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 599 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | move_to_confirmed(struct nfs4_client *clp) |
| 601 | { |
| 602 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
| 603 | unsigned int strhashval; |
| 604 | |
| 605 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
| 606 | list_del_init(&clp->cl_strhash); |
Akinobu Mita | f116629 | 2006-06-26 00:24:46 -0700 | [diff] [blame] | 607 | list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 608 | strhashval = clientstr_hashval(clp->cl_recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]); |
| 610 | renew_client(clp); |
| 611 | } |
| 612 | |
| 613 | static struct nfs4_client * |
| 614 | find_confirmed_client(clientid_t *clid) |
| 615 | { |
| 616 | struct nfs4_client *clp; |
| 617 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 618 | |
| 619 | list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 620 | if (same_clid(&clp->cl_clientid, clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | return clp; |
| 622 | } |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | static struct nfs4_client * |
| 627 | find_unconfirmed_client(clientid_t *clid) |
| 628 | { |
| 629 | struct nfs4_client *clp; |
| 630 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 631 | |
| 632 | list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 633 | if (same_clid(&clp->cl_clientid, clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return clp; |
| 635 | } |
| 636 | return NULL; |
| 637 | } |
| 638 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 639 | static struct nfs4_client * |
| 640 | find_confirmed_client_by_str(const char *dname, unsigned int hashval) |
| 641 | { |
| 642 | struct nfs4_client *clp; |
| 643 | |
| 644 | list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) { |
| 645 | if (same_name(clp->cl_recdir, dname)) |
| 646 | return clp; |
| 647 | } |
| 648 | return NULL; |
| 649 | } |
| 650 | |
| 651 | static struct nfs4_client * |
| 652 | find_unconfirmed_client_by_str(const char *dname, unsigned int hashval) |
| 653 | { |
| 654 | struct nfs4_client *clp; |
| 655 | |
| 656 | list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) { |
| 657 | if (same_name(clp->cl_recdir, dname)) |
| 658 | return clp; |
| 659 | } |
| 660 | return NULL; |
| 661 | } |
| 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | /* a helper function for parse_callback */ |
| 664 | static int |
| 665 | parse_octet(unsigned int *lenp, char **addrp) |
| 666 | { |
| 667 | unsigned int len = *lenp; |
| 668 | char *p = *addrp; |
| 669 | int n = -1; |
| 670 | char c; |
| 671 | |
| 672 | for (;;) { |
| 673 | if (!len) |
| 674 | break; |
| 675 | len--; |
| 676 | c = *p++; |
| 677 | if (c == '.') |
| 678 | break; |
| 679 | if ((c < '0') || (c > '9')) { |
| 680 | n = -1; |
| 681 | break; |
| 682 | } |
| 683 | if (n < 0) |
| 684 | n = 0; |
| 685 | n = (n * 10) + (c - '0'); |
| 686 | if (n > 255) { |
| 687 | n = -1; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | *lenp = len; |
| 692 | *addrp = p; |
| 693 | return n; |
| 694 | } |
| 695 | |
| 696 | /* parse and set the setclientid ipv4 callback address */ |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 697 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp) |
| 699 | { |
| 700 | int temp = 0; |
| 701 | u32 cbaddr = 0; |
| 702 | u16 cbport = 0; |
| 703 | u32 addrlen = addr_len; |
| 704 | char *addr = addr_val; |
| 705 | int i, shift; |
| 706 | |
| 707 | /* ipaddress */ |
| 708 | shift = 24; |
| 709 | for(i = 4; i > 0 ; i--) { |
| 710 | if ((temp = parse_octet(&addrlen, &addr)) < 0) { |
| 711 | return 0; |
| 712 | } |
| 713 | cbaddr |= (temp << shift); |
| 714 | if (shift > 0) |
| 715 | shift -= 8; |
| 716 | } |
| 717 | *cbaddrp = cbaddr; |
| 718 | |
| 719 | /* port */ |
| 720 | shift = 8; |
| 721 | for(i = 2; i > 0 ; i--) { |
| 722 | if ((temp = parse_octet(&addrlen, &addr)) < 0) { |
| 723 | return 0; |
| 724 | } |
| 725 | cbport |= (temp << shift); |
| 726 | if (shift > 0) |
| 727 | shift -= 8; |
| 728 | } |
| 729 | *cbportp = cbport; |
| 730 | return 1; |
| 731 | } |
| 732 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 733 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se) |
| 735 | { |
| 736 | struct nfs4_callback *cb = &clp->cl_callback; |
| 737 | |
| 738 | /* Currently, we only support tcp for the callback channel */ |
| 739 | if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3)) |
| 740 | goto out_err; |
| 741 | |
| 742 | if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val, |
| 743 | &cb->cb_addr, &cb->cb_port))) |
| 744 | goto out_err; |
| 745 | cb->cb_prog = se->se_callback_prog; |
| 746 | cb->cb_ident = se->se_callback_ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return; |
| 748 | out_err: |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 749 | dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | "will not receive delegations\n", |
| 751 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 756 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 757 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 758 | struct nfsd4_setclientid *setclid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 760 | struct sockaddr_in *sin = svc_addr_in(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | struct xdr_netobj clname = { |
| 762 | .len = setclid->se_namelen, |
| 763 | .data = setclid->se_name, |
| 764 | }; |
| 765 | nfs4_verifier clverifier = setclid->se_verf; |
| 766 | unsigned int strhashval; |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 767 | struct nfs4_client *conf, *unconf, *new; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 768 | __be32 status; |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 769 | char *princ; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 770 | char dname[HEXDIR_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if (!check_name(clname)) |
Neil Brown | 73aea4e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 773 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 775 | status = nfs4_make_rec_clidname(dname, &clname); |
| 776 | if (status) |
Neil Brown | 73aea4e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 777 | return status; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* |
| 780 | * XXX The Duplicate Request Cache (DRC) has been checked (??) |
| 781 | * We get here on a DRC miss. |
| 782 | */ |
| 783 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 784 | strhashval = clientstr_hashval(dname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | nfs4_lock_state(); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 787 | conf = find_confirmed_client_by_str(dname, strhashval); |
| 788 | if (conf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 789 | /* RFC 3530 14.2.33 CASE 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | status = nfserr_clid_inuse; |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 791 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred) |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 792 | || conf->cl_addr != sin->sin_addr.s_addr) { |
Harvey Harrison | be85940 | 2008-10-31 00:56:28 -0700 | [diff] [blame] | 793 | dprintk("NFSD: setclientid: string in use by clientat %pI4\n", |
| 794 | &conf->cl_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | goto out; |
| 796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 798 | /* |
| 799 | * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION") |
| 800 | * has a description of SETCLIENTID request processing consisting |
| 801 | * of 5 bullet points, labeled as CASE0 - CASE4 below. |
| 802 | */ |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 803 | unconf = find_unconfirmed_client_by_str(dname, strhashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | status = nfserr_resource; |
| 805 | if (!conf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 806 | /* |
| 807 | * RFC 3530 14.2.33 CASE 4: |
| 808 | * placed first, because it is the normal case |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | */ |
| 810 | if (unconf) |
| 811 | expire_client(unconf); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 812 | new = create_client(clname, dname); |
| 813 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | gen_clid(new); |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 816 | } else if (same_verf(&conf->cl_verifier, &clverifier)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 818 | * RFC 3530 14.2.33 CASE 1: |
| 819 | * probable callback update |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | */ |
NeilBrown | 31f4a6c | 2005-06-23 22:04:06 -0700 | [diff] [blame] | 821 | if (unconf) { |
| 822 | /* Note this is removing unconfirmed {*x***}, |
| 823 | * which is stronger than RFC recommended {vxc**}. |
| 824 | * This has the advantage that there is at most |
| 825 | * one {*x***} in either list at any time. |
| 826 | */ |
| 827 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 829 | new = create_client(clname, dname); |
| 830 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | copy_clid(new, conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } else if (!unconf) { |
| 834 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 835 | * RFC 3530 14.2.33 CASE 2: |
| 836 | * probable client reboot; state will be removed if |
| 837 | * confirmed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | */ |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 839 | new = create_client(clname, dname); |
| 840 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | gen_clid(new); |
J. Bruce Fields | 49ba878 | 2007-11-19 19:09:50 -0500 | [diff] [blame] | 843 | } else { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 844 | /* |
| 845 | * RFC 3530 14.2.33 CASE 3: |
| 846 | * probable client reboot; state will be removed if |
| 847 | * confirmed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | */ |
| 849 | expire_client(unconf); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 850 | new = create_client(clname, dname); |
| 851 | if (new == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | gen_clid(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | } |
J. Bruce Fields | c175b83 | 2007-08-09 18:34:32 -0400 | [diff] [blame] | 855 | copy_verf(new, &clverifier); |
| 856 | new->cl_addr = sin->sin_addr.s_addr; |
Olga Kornievskaia | 61054b1 | 2008-12-23 16:19:00 -0500 | [diff] [blame] | 857 | new->cl_flavor = rqstp->rq_flavor; |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 858 | princ = svc_gss_principal(rqstp); |
| 859 | if (princ) { |
| 860 | new->cl_principal = kstrdup(princ, GFP_KERNEL); |
| 861 | if (new->cl_principal == NULL) { |
| 862 | free_client(new); |
| 863 | goto out; |
| 864 | } |
| 865 | } |
J. Bruce Fields | c175b83 | 2007-08-09 18:34:32 -0400 | [diff] [blame] | 866 | copy_cred(&new->cl_cred, &rqstp->rq_cred); |
| 867 | gen_confirm(new); |
| 868 | gen_callback(new, setclid); |
| 869 | add_to_unconfirmed(new, strhashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 871 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 872 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
| 873 | status = nfs_ok; |
| 874 | out: |
| 875 | nfs4_unlock_state(); |
| 876 | return status; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /* |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 881 | * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has |
| 882 | * a description of SETCLIENTID_CONFIRM request processing consisting of 4 |
| 883 | * bullets, labeled as CASE1 - CASE4 below. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 885 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 886 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
| 887 | struct nfsd4_compound_state *cstate, |
| 888 | struct nfsd4_setclientid_confirm *setclientid_confirm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 890 | struct sockaddr_in *sin = svc_addr_in(rqstp); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 891 | struct nfs4_client *conf, *unconf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 893 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 894 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
| 896 | if (STALE_CLIENTID(clid)) |
| 897 | return nfserr_stale_clientid; |
| 898 | /* |
| 899 | * XXX The Duplicate Request Cache (DRC) has been checked (??) |
| 900 | * We get here on a DRC miss. |
| 901 | */ |
| 902 | |
| 903 | nfs4_lock_state(); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 904 | |
| 905 | conf = find_confirmed_client(clid); |
| 906 | unconf = find_unconfirmed_client(clid); |
| 907 | |
| 908 | status = nfserr_clid_inuse; |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 909 | if (conf && conf->cl_addr != sin->sin_addr.s_addr) |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 910 | goto out; |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 911 | if (unconf && unconf->cl_addr != sin->sin_addr.s_addr) |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 912 | goto out; |
| 913 | |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 914 | /* |
| 915 | * section 14.2.34 of RFC 3530 has a description of |
| 916 | * SETCLIENTID_CONFIRM request processing consisting |
| 917 | * of 4 bullet points, labeled as CASE1 - CASE4 below. |
| 918 | */ |
J. Bruce Fields | 366e0c1 | 2007-11-20 15:54:10 -0500 | [diff] [blame] | 919 | if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 920 | /* |
| 921 | * RFC 3530 14.2.34 CASE 1: |
| 922 | * callback update |
| 923 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 924 | if (!same_creds(&conf->cl_cred, &unconf->cl_cred)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | status = nfserr_clid_inuse; |
| 926 | else { |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 927 | /* XXX: We just turn off callbacks until we can handle |
| 928 | * change request correctly. */ |
NeilBrown | cb36d63 | 2005-06-23 22:04:23 -0700 | [diff] [blame] | 929 | atomic_set(&conf->cl_callback.cb_set, 0); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 930 | gen_confirm(conf); |
NeilBrown | 4630902 | 2005-07-07 17:59:10 -0700 | [diff] [blame] | 931 | nfsd4_remove_clid_dir(unconf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 932 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | status = nfs_ok; |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
J. Bruce Fields | f3aba4e | 2007-11-20 16:52:07 -0500 | [diff] [blame] | 936 | } else if (conf && !unconf) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 937 | /* |
| 938 | * RFC 3530 14.2.34 CASE 2: |
| 939 | * probable retransmitted request; play it safe and |
| 940 | * do nothing. |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 941 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 942 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | status = nfserr_clid_inuse; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 944 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | status = nfs_ok; |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 946 | } else if (!conf && unconf |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 947 | && same_verf(&unconf->cl_confirm, &confirm)) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 948 | /* |
| 949 | * RFC 3530 14.2.34 CASE 3: |
| 950 | * Normal case; new or rebooted client: |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 951 | */ |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 952 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | status = nfserr_clid_inuse; |
| 954 | } else { |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 955 | unsigned int hash = |
| 956 | clientstr_hashval(unconf->cl_recdir); |
| 957 | conf = find_confirmed_client_by_str(unconf->cl_recdir, |
| 958 | hash); |
| 959 | if (conf) { |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 960 | nfsd4_remove_clid_dir(conf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 961 | expire_client(conf); |
| 962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | move_to_confirmed(unconf); |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 964 | conf = unconf; |
J. Bruce Fields | 46f8a64 | 2007-11-22 13:54:18 -0500 | [diff] [blame] | 965 | nfsd4_probe_callback(conf); |
NeilBrown | 1a69c17 | 2005-06-23 22:04:08 -0700 | [diff] [blame] | 966 | status = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 968 | } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm))) |
| 969 | && (!unconf || (unconf && !same_verf(&unconf->cl_confirm, |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 970 | &confirm)))) { |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 971 | /* |
| 972 | * RFC 3530 14.2.34 CASE 4: |
| 973 | * Client probably hasn't noticed that we rebooted yet. |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 974 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | status = nfserr_stale_clientid; |
NeilBrown | 7c79f73 | 2005-06-23 22:04:13 -0700 | [diff] [blame] | 976 | } else { |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 977 | /* check that we have hit one of the cases...*/ |
| 978 | status = nfserr_clid_inuse; |
| 979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | nfs4_unlock_state(); |
| 982 | return status; |
| 983 | } |
| 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | /* OPEN Share state helper functions */ |
| 986 | static inline struct nfs4_file * |
| 987 | alloc_init_file(struct inode *ino) |
| 988 | { |
| 989 | struct nfs4_file *fp; |
| 990 | unsigned int hashval = file_hashval(ino); |
| 991 | |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 992 | fp = kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 993 | if (fp) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 994 | kref_init(&fp->fi_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | INIT_LIST_HEAD(&fp->fi_hash); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 996 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 997 | INIT_LIST_HEAD(&fp->fi_delegations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | list_add(&fp->fi_hash, &file_hashtbl[hashval]); |
| 999 | fp->fi_inode = igrab(ino); |
| 1000 | fp->fi_id = current_fileid++; |
Meelap Shah | 47f9940 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 1001 | fp->fi_had_conflict = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | return fp; |
| 1003 | } |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
| 1007 | static void |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1008 | nfsd4_free_slab(struct kmem_cache **slab) |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1009 | { |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1010 | if (*slab == NULL) |
| 1011 | return; |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1012 | kmem_cache_destroy(*slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1013 | *slab = NULL; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 1016 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1017 | nfsd4_free_slabs(void) |
| 1018 | { |
| 1019 | nfsd4_free_slab(&stateowner_slab); |
| 1020 | nfsd4_free_slab(&file_slab); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1021 | nfsd4_free_slab(&stateid_slab); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 1022 | nfsd4_free_slab(&deleg_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1023 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | static int |
| 1026 | nfsd4_init_slabs(void) |
| 1027 | { |
| 1028 | stateowner_slab = kmem_cache_create("nfsd4_stateowners", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1029 | sizeof(struct nfs4_stateowner), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1030 | if (stateowner_slab == NULL) |
| 1031 | goto out_nomem; |
| 1032 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1033 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1034 | if (file_slab == NULL) |
| 1035 | goto out_nomem; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1036 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1037 | sizeof(struct nfs4_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1038 | if (stateid_slab == NULL) |
| 1039 | goto out_nomem; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 1040 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1041 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 1042 | if (deleg_slab == NULL) |
| 1043 | goto out_nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | return 0; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 1045 | out_nomem: |
| 1046 | nfsd4_free_slabs(); |
| 1047 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 1048 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | void |
| 1052 | nfs4_free_stateowner(struct kref *kref) |
| 1053 | { |
| 1054 | struct nfs4_stateowner *sop = |
| 1055 | container_of(kref, struct nfs4_stateowner, so_ref); |
| 1056 | kfree(sop->so_owner.data); |
| 1057 | kmem_cache_free(stateowner_slab, sop); |
| 1058 | } |
| 1059 | |
| 1060 | static inline struct nfs4_stateowner * |
| 1061 | alloc_stateowner(struct xdr_netobj *owner) |
| 1062 | { |
| 1063 | struct nfs4_stateowner *sop; |
| 1064 | |
| 1065 | if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) { |
| 1066 | if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) { |
| 1067 | memcpy(sop->so_owner.data, owner->data, owner->len); |
| 1068 | sop->so_owner.len = owner->len; |
| 1069 | kref_init(&sop->so_ref); |
| 1070 | return sop; |
| 1071 | } |
| 1072 | kmem_cache_free(stateowner_slab, sop); |
| 1073 | } |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | |
| 1077 | static struct nfs4_stateowner * |
| 1078 | alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) { |
| 1079 | struct nfs4_stateowner *sop; |
| 1080 | struct nfs4_replay *rp; |
| 1081 | unsigned int idhashval; |
| 1082 | |
| 1083 | if (!(sop = alloc_stateowner(&open->op_owner))) |
| 1084 | return NULL; |
| 1085 | idhashval = ownerid_hashval(current_ownerid); |
| 1086 | INIT_LIST_HEAD(&sop->so_idhash); |
| 1087 | INIT_LIST_HEAD(&sop->so_strhash); |
| 1088 | INIT_LIST_HEAD(&sop->so_perclient); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1089 | INIT_LIST_HEAD(&sop->so_stateids); |
| 1090 | INIT_LIST_HEAD(&sop->so_perstateid); /* not used */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | INIT_LIST_HEAD(&sop->so_close_lru); |
| 1092 | sop->so_time = 0; |
| 1093 | list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]); |
| 1094 | list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1095 | list_add(&sop->so_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | sop->so_is_open_owner = 1; |
| 1097 | sop->so_id = current_ownerid++; |
| 1098 | sop->so_client = clp; |
| 1099 | sop->so_seqid = open->op_seqid; |
| 1100 | sop->so_confirmed = 0; |
| 1101 | rp = &sop->so_replay; |
Al Viro | de1ae28 | 2006-01-18 17:43:47 -0800 | [diff] [blame] | 1102 | rp->rp_status = nfserr_serverfault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | rp->rp_buflen = 0; |
| 1104 | rp->rp_buf = rp->rp_ibuf; |
| 1105 | return sop; |
| 1106 | } |
| 1107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | static inline void |
| 1109 | init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) { |
| 1110 | struct nfs4_stateowner *sop = open->op_stateowner; |
| 1111 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); |
| 1112 | |
| 1113 | INIT_LIST_HEAD(&stp->st_hash); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1114 | INIT_LIST_HEAD(&stp->st_perstateowner); |
| 1115 | INIT_LIST_HEAD(&stp->st_lockowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | INIT_LIST_HEAD(&stp->st_perfile); |
| 1117 | list_add(&stp->st_hash, &stateid_hashtbl[hashval]); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1118 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 1119 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | stp->st_stateowner = sop; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1121 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | stp->st_file = fp; |
| 1123 | stp->st_stateid.si_boot = boot_time; |
| 1124 | stp->st_stateid.si_stateownerid = sop->so_id; |
| 1125 | stp->st_stateid.si_fileid = fp->fi_id; |
| 1126 | stp->st_stateid.si_generation = 0; |
| 1127 | stp->st_access_bmap = 0; |
| 1128 | stp->st_deny_bmap = 0; |
| 1129 | __set_bit(open->op_share_access, &stp->st_access_bmap); |
| 1130 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 1131 | stp->st_openstp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | move_to_close_lru(struct nfs4_stateowner *sop) |
| 1136 | { |
| 1137 | dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop); |
| 1138 | |
NeilBrown | 358dd55 | 2006-04-10 22:55:42 -0700 | [diff] [blame] | 1139 | list_move_tail(&sop->so_close_lru, &close_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | sop->so_time = get_seconds(); |
| 1141 | } |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1144 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, |
| 1145 | clientid_t *clid) |
| 1146 | { |
| 1147 | return (sop->so_owner.len == owner->len) && |
| 1148 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len) && |
| 1149 | (sop->so_client->cl_clientid.cl_id == clid->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static struct nfs4_stateowner * |
| 1153 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open) |
| 1154 | { |
| 1155 | struct nfs4_stateowner *so = NULL; |
| 1156 | |
| 1157 | list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 1158 | if (same_owner_str(so, &open->op_owner, &open->op_clientid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return so; |
| 1160 | } |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
| 1164 | /* search file_hashtbl[] for file */ |
| 1165 | static struct nfs4_file * |
| 1166 | find_file(struct inode *ino) |
| 1167 | { |
| 1168 | unsigned int hashval = file_hashval(ino); |
| 1169 | struct nfs4_file *fp; |
| 1170 | |
| 1171 | list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1172 | if (fp->fi_inode == ino) { |
| 1173 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 1180 | static inline int access_valid(u32 x) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 1181 | { |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 1182 | if (x < NFS4_SHARE_ACCESS_READ) |
| 1183 | return 0; |
| 1184 | if (x > NFS4_SHARE_ACCESS_BOTH) |
| 1185 | return 0; |
| 1186 | return 1; |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 1189 | static inline int deny_valid(u32 x) |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 1190 | { |
J. Bruce Fields | 8838dc4 | 2008-01-14 13:12:19 -0500 | [diff] [blame] | 1191 | /* Note: unlike access bits, deny bits may be zero. */ |
| 1192 | return x <= NFS4_SHARE_DENY_BOTH; |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 1193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 1195 | /* |
| 1196 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1197 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1198 | * only what share bits are currently in force, but also what |
| 1199 | * combinations of share bits previous opens have used. This allows us |
| 1200 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1201 | * return an error if the client attempt to downgrade to a combination |
| 1202 | * of share bits not explicable by closing some of its previous opens. |
| 1203 | * |
| 1204 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1205 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1206 | * |
| 1207 | * OPEN allow read, deny write |
| 1208 | * OPEN allow both, deny none |
| 1209 | * DOWNGRADE allow read, deny none |
| 1210 | * |
| 1211 | * which we should reject. |
| 1212 | */ |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1213 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | set_access(unsigned int *access, unsigned long bmap) { |
| 1215 | int i; |
| 1216 | |
| 1217 | *access = 0; |
| 1218 | for (i = 1; i < 4; i++) { |
| 1219 | if (test_bit(i, &bmap)) |
| 1220 | *access |= i; |
| 1221 | } |
| 1222 | } |
| 1223 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1224 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | set_deny(unsigned int *deny, unsigned long bmap) { |
| 1226 | int i; |
| 1227 | |
| 1228 | *deny = 0; |
| 1229 | for (i = 0; i < 4; i++) { |
| 1230 | if (test_bit(i, &bmap)) |
| 1231 | *deny |= i ; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | static int |
| 1236 | test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) { |
| 1237 | unsigned int access, deny; |
| 1238 | |
| 1239 | set_access(&access, stp->st_access_bmap); |
| 1240 | set_deny(&deny, stp->st_deny_bmap); |
| 1241 | if ((access & open->op_share_deny) || (deny & open->op_share_access)) |
| 1242 | return 0; |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Called to check deny when READ with all zero stateid or |
| 1248 | * WRITE with all zero or all one stateid |
| 1249 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1250 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 1252 | { |
| 1253 | struct inode *ino = current_fh->fh_dentry->d_inode; |
| 1254 | struct nfs4_file *fp; |
| 1255 | struct nfs4_stateid *stp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1256 | __be32 ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | dprintk("NFSD: nfs4_share_conflict\n"); |
| 1259 | |
| 1260 | fp = find_file(ino); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1261 | if (!fp) |
| 1262 | return nfs_ok; |
NeilBrown | b700949 | 2005-07-07 17:59:23 -0700 | [diff] [blame] | 1263 | ret = nfserr_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | /* Search for conflicting share reservations */ |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1265 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) { |
| 1266 | if (test_bit(deny_type, &stp->st_deny_bmap) || |
| 1267 | test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap)) |
| 1268 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1270 | ret = nfs_ok; |
| 1271 | out: |
| 1272 | put_nfs4_file(fp); |
| 1273 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | static inline void |
| 1277 | nfs4_file_downgrade(struct file *filp, unsigned int share_access) |
| 1278 | { |
| 1279 | if (share_access & NFS4_SHARE_ACCESS_WRITE) { |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 1280 | drop_file_write_access(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Recall a delegation |
| 1287 | */ |
| 1288 | static int |
| 1289 | do_recall(void *__dp) |
| 1290 | { |
| 1291 | struct nfs4_delegation *dp = __dp; |
| 1292 | |
Meelap Shah | 47f9940 | 2007-07-17 04:04:40 -0700 | [diff] [blame] | 1293 | dp->dl_file->fi_had_conflict = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | nfsd4_cb_recall(dp); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * Spawn a thread to perform a recall on the delegation represented |
| 1300 | * by the lease (file_lock) |
| 1301 | * |
| 1302 | * Called from break_lease() with lock_kernel() held. |
| 1303 | * Note: we assume break_lease will only call this *once* for any given |
| 1304 | * lease. |
| 1305 | */ |
| 1306 | static |
| 1307 | void nfsd_break_deleg_cb(struct file_lock *fl) |
| 1308 | { |
| 1309 | struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner; |
| 1310 | struct task_struct *t; |
| 1311 | |
| 1312 | dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl); |
| 1313 | if (!dp) |
| 1314 | return; |
| 1315 | |
| 1316 | /* We're assuming the state code never drops its reference |
| 1317 | * without first removing the lease. Since we're in this lease |
| 1318 | * callback (and since the lease code is serialized by the kernel |
| 1319 | * lock) we know the server hasn't removed the lease yet, we know |
| 1320 | * it's safe to take a reference: */ |
| 1321 | atomic_inc(&dp->dl_count); |
J. Bruce Fields | cfdcad4 | 2007-09-12 20:35:15 -0400 | [diff] [blame] | 1322 | atomic_inc(&dp->dl_client->cl_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
| 1324 | spin_lock(&recall_lock); |
| 1325 | list_add_tail(&dp->dl_recall_lru, &del_recall_lru); |
| 1326 | spin_unlock(&recall_lock); |
| 1327 | |
| 1328 | /* only place dl_time is set. protected by lock_kernel*/ |
| 1329 | dp->dl_time = get_seconds(); |
| 1330 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 1331 | /* |
| 1332 | * We don't want the locks code to timeout the lease for us; |
| 1333 | * we'll remove it ourself if the delegation isn't returned |
| 1334 | * in time. |
| 1335 | */ |
| 1336 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall"); |
| 1339 | if (IS_ERR(t)) { |
| 1340 | struct nfs4_client *clp = dp->dl_client; |
| 1341 | |
| 1342 | printk(KERN_INFO "NFSD: Callback thread failed for " |
| 1343 | "for client (clientid %08x/%08x)\n", |
| 1344 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
J. Bruce Fields | cfdcad4 | 2007-09-12 20:35:15 -0400 | [diff] [blame] | 1345 | put_nfs4_client(dp->dl_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | nfs4_put_delegation(dp); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | * The file_lock is being reapd. |
| 1352 | * |
| 1353 | * Called by locks_free_lock() with lock_kernel() held. |
| 1354 | */ |
| 1355 | static |
| 1356 | void nfsd_release_deleg_cb(struct file_lock *fl) |
| 1357 | { |
| 1358 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 1359 | |
| 1360 | dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count)); |
| 1361 | |
| 1362 | if (!(fl->fl_flags & FL_LEASE) || !dp) |
| 1363 | return; |
| 1364 | dp->dl_flock = NULL; |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Set the delegation file_lock back pointer. |
| 1369 | * |
J. Bruce Fields | a9933ce | 2007-06-07 17:09:49 -0400 | [diff] [blame] | 1370 | * Called from setlease() with lock_kernel() held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | */ |
| 1372 | static |
| 1373 | void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl) |
| 1374 | { |
| 1375 | struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner; |
| 1376 | |
| 1377 | dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp); |
| 1378 | if (!dp) |
| 1379 | return; |
| 1380 | dp->dl_flock = new; |
| 1381 | } |
| 1382 | |
| 1383 | /* |
J. Bruce Fields | a9933ce | 2007-06-07 17:09:49 -0400 | [diff] [blame] | 1384 | * Called from setlease() with lock_kernel() held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | */ |
| 1386 | static |
| 1387 | int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try) |
| 1388 | { |
| 1389 | struct nfs4_delegation *onlistd = |
| 1390 | (struct nfs4_delegation *)onlist->fl_owner; |
| 1391 | struct nfs4_delegation *tryd = |
| 1392 | (struct nfs4_delegation *)try->fl_owner; |
| 1393 | |
| 1394 | if (onlist->fl_lmops != try->fl_lmops) |
| 1395 | return 0; |
| 1396 | |
| 1397 | return onlistd->dl_client == tryd->dl_client; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | static |
| 1402 | int nfsd_change_deleg_cb(struct file_lock **onlist, int arg) |
| 1403 | { |
| 1404 | if (arg & F_UNLCK) |
| 1405 | return lease_modify(onlist, arg); |
| 1406 | else |
| 1407 | return -EAGAIN; |
| 1408 | } |
| 1409 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1410 | static struct lock_manager_operations nfsd_lease_mng_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | .fl_break = nfsd_break_deleg_cb, |
| 1412 | .fl_release_private = nfsd_release_deleg_cb, |
| 1413 | .fl_copy_lock = nfsd_copy_lock_deleg_cb, |
| 1414 | .fl_mylease = nfsd_same_client_deleg_cb, |
| 1415 | .fl_change = nfsd_change_deleg_cb, |
| 1416 | }; |
| 1417 | |
| 1418 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1419 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | nfsd4_process_open1(struct nfsd4_open *open) |
| 1421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | clientid_t *clientid = &open->op_clientid; |
| 1423 | struct nfs4_client *clp = NULL; |
| 1424 | unsigned int strhashval; |
| 1425 | struct nfs4_stateowner *sop = NULL; |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | if (!check_name(open->op_owner)) |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1428 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | |
| 1430 | if (STALE_CLIENTID(&open->op_clientid)) |
| 1431 | return nfserr_stale_clientid; |
| 1432 | |
| 1433 | strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner); |
| 1434 | sop = find_openstateowner_str(strhashval, open); |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1435 | open->op_stateowner = sop; |
| 1436 | if (!sop) { |
| 1437 | /* Make sure the client's lease hasn't expired. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | clp = find_confirmed_client(clientid); |
| 1439 | if (clp == NULL) |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1440 | return nfserr_expired; |
| 1441 | goto renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | } |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1443 | if (!sop->so_confirmed) { |
| 1444 | /* Replace unconfirmed owners without checking for replay. */ |
| 1445 | clp = sop->so_client; |
| 1446 | release_stateowner(sop); |
| 1447 | open->op_stateowner = NULL; |
| 1448 | goto renew; |
| 1449 | } |
| 1450 | if (open->op_seqid == sop->so_seqid - 1) { |
| 1451 | if (sop->so_replay.rp_buflen) |
Al Viro | a90b061 | 2006-10-19 23:29:03 -0700 | [diff] [blame] | 1452 | return nfserr_replay_me; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1453 | /* The original OPEN failed so spectacularly |
| 1454 | * that we don't even have replay data saved! |
| 1455 | * Therefore, we have no choice but to continue |
| 1456 | * processing this OPEN; presumably, we'll |
| 1457 | * fail again for the same reason. |
| 1458 | */ |
| 1459 | dprintk("nfsd4_process_open1: replay with no replay cache\n"); |
| 1460 | goto renew; |
| 1461 | } |
| 1462 | if (open->op_seqid != sop->so_seqid) |
| 1463 | return nfserr_bad_seqid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | renew: |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1465 | if (open->op_stateowner == NULL) { |
| 1466 | sop = alloc_init_open_stateowner(strhashval, clp, open); |
| 1467 | if (sop == NULL) |
| 1468 | return nfserr_resource; |
| 1469 | open->op_stateowner = sop; |
| 1470 | } |
| 1471 | list_del_init(&sop->so_close_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | renew_client(sop->so_client); |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 1473 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1476 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 1477 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 1478 | { |
| 1479 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 1480 | return nfserr_openmode; |
| 1481 | else |
| 1482 | return nfs_ok; |
| 1483 | } |
| 1484 | |
NeilBrown | 52f4fb4 | 2005-06-23 22:02:49 -0700 | [diff] [blame] | 1485 | static struct nfs4_delegation * |
| 1486 | find_delegation_file(struct nfs4_file *fp, stateid_t *stid) |
| 1487 | { |
| 1488 | struct nfs4_delegation *dp; |
| 1489 | |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1490 | list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) { |
NeilBrown | 52f4fb4 | 2005-06-23 22:02:49 -0700 | [diff] [blame] | 1491 | if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) |
| 1492 | return dp; |
| 1493 | } |
| 1494 | return NULL; |
| 1495 | } |
| 1496 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1497 | static __be32 |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1498 | nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open, |
| 1499 | struct nfs4_delegation **dp) |
| 1500 | { |
| 1501 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1502 | __be32 status = nfserr_bad_stateid; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1503 | |
| 1504 | *dp = find_delegation_file(fp, &open->op_delegate_stateid); |
| 1505 | if (*dp == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 1506 | goto out; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1507 | flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ? |
| 1508 | RD_STATE : WR_STATE; |
| 1509 | status = nfs4_check_delegmode(*dp, flags); |
| 1510 | if (status) |
| 1511 | *dp = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 1512 | out: |
| 1513 | if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR) |
| 1514 | return nfs_ok; |
| 1515 | if (status) |
| 1516 | return status; |
| 1517 | open->op_stateowner->so_confirmed = 1; |
| 1518 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1521 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp) |
| 1523 | { |
| 1524 | struct nfs4_stateid *local; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1525 | __be32 status = nfserr_share_denied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | struct nfs4_stateowner *sop = open->op_stateowner; |
| 1527 | |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 1528 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | /* ignore lock owners */ |
| 1530 | if (local->st_stateowner->so_is_open_owner == 0) |
| 1531 | continue; |
| 1532 | /* remember if we have seen this open owner */ |
| 1533 | if (local->st_stateowner == sop) |
| 1534 | *stpp = local; |
| 1535 | /* check for conflicting share reservations */ |
| 1536 | if (!test_share(local, open)) |
| 1537 | goto out; |
| 1538 | } |
| 1539 | status = 0; |
| 1540 | out: |
| 1541 | return status; |
| 1542 | } |
| 1543 | |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1544 | static inline struct nfs4_stateid * |
| 1545 | nfs4_alloc_stateid(void) |
| 1546 | { |
| 1547 | return kmem_cache_alloc(stateid_slab, GFP_KERNEL); |
| 1548 | } |
| 1549 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1550 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1552 | struct nfs4_delegation *dp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | struct svc_fh *cur_fh, int flags) |
| 1554 | { |
| 1555 | struct nfs4_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1557 | stp = nfs4_alloc_stateid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | if (stp == NULL) |
| 1559 | return nfserr_resource; |
| 1560 | |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1561 | if (dp) { |
| 1562 | get_file(dp->dl_vfs_file); |
| 1563 | stp->st_vfs_file = dp->dl_vfs_file; |
| 1564 | } else { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1565 | __be32 status; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1566 | status = nfsd_open(rqstp, cur_fh, S_IFREG, flags, |
| 1567 | &stp->st_vfs_file); |
| 1568 | if (status) { |
| 1569 | if (status == nfserr_dropit) |
| 1570 | status = nfserr_jukebox; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 1571 | kmem_cache_free(stateid_slab, stp); |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1572 | return status; |
| 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | *stpp = stp; |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1579 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 1581 | struct nfsd4_open *open) |
| 1582 | { |
| 1583 | struct iattr iattr = { |
| 1584 | .ia_valid = ATTR_SIZE, |
| 1585 | .ia_size = 0, |
| 1586 | }; |
| 1587 | if (!open->op_truncate) |
| 1588 | return 0; |
| 1589 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 1590 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 1592 | } |
| 1593 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1594 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open) |
| 1596 | { |
| 1597 | struct file *filp = stp->st_vfs_file; |
Josef "Jeff" Sipek | 7eaa36e | 2006-12-08 02:36:41 -0800 | [diff] [blame] | 1598 | struct inode *inode = filp->f_path.dentry->d_inode; |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1599 | unsigned int share_access, new_writer; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1600 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
| 1602 | set_access(&share_access, stp->st_access_bmap); |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1603 | new_writer = (~share_access) & open->op_share_access |
| 1604 | & NFS4_SHARE_ACCESS_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1606 | if (new_writer) { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1607 | int err = get_write_access(inode); |
| 1608 | if (err) |
| 1609 | return nfserrno(err); |
Benny Halevy | e518f05 | 2008-07-04 15:38:41 +0300 | [diff] [blame] | 1610 | err = mnt_want_write(cur_fh->fh_export->ex_path.mnt); |
| 1611 | if (err) |
| 1612 | return nfserrno(err); |
| 1613 | file_take_write(filp); |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1614 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 1616 | if (status) { |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1617 | if (new_writer) |
| 1618 | put_write_access(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | return status; |
| 1620 | } |
| 1621 | /* remember the open */ |
J. Bruce Fields | 6c26d08 | 2006-01-18 17:43:38 -0800 | [diff] [blame] | 1622 | filp->f_mode |= open->op_share_access; |
J. Bruce Fields | b55e0ba | 2008-04-28 18:22:50 -0400 | [diff] [blame] | 1623 | __set_bit(open->op_share_access, &stp->st_access_bmap); |
| 1624 | __set_bit(open->op_share_deny, &stp->st_deny_bmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
| 1626 | return nfs_ok; |
| 1627 | } |
| 1628 | |
| 1629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | static void |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 1631 | nfs4_set_claim_prev(struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 1633 | open->op_stateowner->so_confirmed = 1; |
| 1634 | open->op_stateowner->so_client->cl_firststate = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * Attempt to hand out a delegation. |
| 1639 | */ |
| 1640 | static void |
| 1641 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp) |
| 1642 | { |
| 1643 | struct nfs4_delegation *dp; |
| 1644 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 1645 | struct nfs4_callback *cb = &sop->so_client->cl_callback; |
| 1646 | struct file_lock fl, *flp = &fl; |
| 1647 | int status, flag = 0; |
| 1648 | |
| 1649 | flag = NFS4_OPEN_DELEGATE_NONE; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 1650 | open->op_recall = 0; |
| 1651 | switch (open->op_claim_type) { |
| 1652 | case NFS4_OPEN_CLAIM_PREVIOUS: |
| 1653 | if (!atomic_read(&cb->cb_set)) |
| 1654 | open->op_recall = 1; |
| 1655 | flag = open->op_delegate_type; |
| 1656 | if (flag == NFS4_OPEN_DELEGATE_NONE) |
| 1657 | goto out; |
| 1658 | break; |
| 1659 | case NFS4_OPEN_CLAIM_NULL: |
| 1660 | /* Let's not give out any delegations till everyone's |
| 1661 | * had the chance to reclaim theirs.... */ |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1662 | if (locks_in_grace()) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 1663 | goto out; |
| 1664 | if (!atomic_read(&cb->cb_set) || !sop->so_confirmed) |
| 1665 | goto out; |
| 1666 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
| 1667 | flag = NFS4_OPEN_DELEGATE_WRITE; |
| 1668 | else |
| 1669 | flag = NFS4_OPEN_DELEGATE_READ; |
| 1670 | break; |
| 1671 | default: |
| 1672 | goto out; |
| 1673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
| 1675 | dp = alloc_init_deleg(sop->so_client, stp, fh, flag); |
| 1676 | if (dp == NULL) { |
| 1677 | flag = NFS4_OPEN_DELEGATE_NONE; |
| 1678 | goto out; |
| 1679 | } |
| 1680 | locks_init_lock(&fl); |
| 1681 | fl.fl_lmops = &nfsd_lease_mng_ops; |
| 1682 | fl.fl_flags = FL_LEASE; |
Felix Blyakher | 9167f50 | 2008-02-26 10:54:36 -0800 | [diff] [blame] | 1683 | fl.fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | fl.fl_end = OFFSET_MAX; |
| 1685 | fl.fl_owner = (fl_owner_t)dp; |
| 1686 | fl.fl_file = stp->st_vfs_file; |
| 1687 | fl.fl_pid = current->tgid; |
| 1688 | |
J. Bruce Fields | a9933ce | 2007-06-07 17:09:49 -0400 | [diff] [blame] | 1689 | /* vfs_setlease checks to see if delegation should be handed out. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | * the lock_manager callbacks fl_mylease and fl_change are used |
| 1691 | */ |
Felix Blyakher | 9167f50 | 2008-02-26 10:54:36 -0800 | [diff] [blame] | 1692 | if ((status = vfs_setlease(stp->st_vfs_file, fl.fl_type, &flp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | dprintk("NFSD: setlease failed [%d], no delegation\n", status); |
NeilBrown | c907132 | 2005-04-16 15:26:38 -0700 | [diff] [blame] | 1694 | unhash_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | flag = NFS4_OPEN_DELEGATE_NONE; |
| 1696 | goto out; |
| 1697 | } |
| 1698 | |
| 1699 | memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid)); |
| 1700 | |
| 1701 | dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n", |
| 1702 | dp->dl_stateid.si_boot, |
| 1703 | dp->dl_stateid.si_stateownerid, |
| 1704 | dp->dl_stateid.si_fileid, |
| 1705 | dp->dl_stateid.si_generation); |
| 1706 | out: |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 1707 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS |
| 1708 | && flag == NFS4_OPEN_DELEGATE_NONE |
| 1709 | && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 1710 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | open->op_delegate_type = flag; |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * called with nfs4_lock_state() held. |
| 1716 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1717 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 1719 | { |
| 1720 | struct nfs4_file *fp = NULL; |
| 1721 | struct inode *ino = current_fh->fh_dentry->d_inode; |
| 1722 | struct nfs4_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1723 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1724 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
| 1726 | status = nfserr_inval; |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 1727 | if (!access_valid(open->op_share_access) |
| 1728 | || !deny_valid(open->op_share_deny)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | goto out; |
| 1730 | /* |
| 1731 | * Lookup file; if found, lookup stateid and check open request, |
| 1732 | * and check for delegations in the process of being recalled. |
| 1733 | * If not found, create the nfs4_file struct |
| 1734 | */ |
| 1735 | fp = find_file(ino); |
| 1736 | if (fp) { |
| 1737 | if ((status = nfs4_check_open(fp, open, &stp))) |
| 1738 | goto out; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 1739 | status = nfs4_check_deleg(fp, open, &dp); |
| 1740 | if (status) |
| 1741 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | } else { |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 1743 | status = nfserr_bad_stateid; |
| 1744 | if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR) |
| 1745 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | status = nfserr_resource; |
| 1747 | fp = alloc_init_file(ino); |
| 1748 | if (fp == NULL) |
| 1749 | goto out; |
| 1750 | } |
| 1751 | |
| 1752 | /* |
| 1753 | * OPEN the file, or upgrade an existing OPEN. |
| 1754 | * If truncate fails, the OPEN fails. |
| 1755 | */ |
| 1756 | if (stp) { |
| 1757 | /* Stateid was found, this is an OPEN upgrade */ |
| 1758 | status = nfs4_upgrade_open(rqstp, current_fh, stp, open); |
| 1759 | if (status) |
| 1760 | goto out; |
NeilBrown | 444c2c0 | 2005-07-07 17:59:22 -0700 | [diff] [blame] | 1761 | update_stateid(&stp->st_stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } else { |
| 1763 | /* Stateid was not found, this is a new OPEN */ |
| 1764 | int flags = 0; |
J. Bruce Fields | 9ecb6a0 | 2006-06-30 01:56:17 -0700 | [diff] [blame] | 1765 | if (open->op_share_access & NFS4_SHARE_ACCESS_READ) |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1766 | flags |= NFSD_MAY_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1768 | flags |= NFSD_MAY_WRITE; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 1769 | status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags); |
| 1770 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | goto out; |
| 1772 | init_stateid(stp, fp, open); |
| 1773 | status = nfsd4_truncate(rqstp, current_fh, open); |
| 1774 | if (status) { |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1775 | release_open_stateid(stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | goto out; |
| 1777 | } |
| 1778 | } |
| 1779 | memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t)); |
| 1780 | |
| 1781 | /* |
| 1782 | * Attempt to hand out a delegation. No error return, because the |
| 1783 | * OPEN succeeds even if we fail. |
| 1784 | */ |
| 1785 | nfs4_open_delegation(current_fh, open, stp); |
| 1786 | |
| 1787 | status = nfs_ok; |
| 1788 | |
| 1789 | dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n", |
| 1790 | stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid, |
| 1791 | stp->st_stateid.si_fileid, stp->st_stateid.si_generation); |
| 1792 | out: |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 1793 | if (fp) |
| 1794 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 1795 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
| 1796 | nfs4_set_claim_prev(open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | /* |
| 1798 | * To finish the open response, we just need to set the rflags. |
| 1799 | */ |
| 1800 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
| 1801 | if (!open->op_stateowner->so_confirmed) |
| 1802 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
| 1803 | |
| 1804 | return status; |
| 1805 | } |
| 1806 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1807 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 1808 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 1809 | clientid_t *clid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | { |
| 1811 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1812 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | |
| 1814 | nfs4_lock_state(); |
| 1815 | dprintk("process_renew(%08x/%08x): starting\n", |
| 1816 | clid->cl_boot, clid->cl_id); |
| 1817 | status = nfserr_stale_clientid; |
| 1818 | if (STALE_CLIENTID(clid)) |
| 1819 | goto out; |
| 1820 | clp = find_confirmed_client(clid); |
| 1821 | status = nfserr_expired; |
| 1822 | if (clp == NULL) { |
| 1823 | /* We assume the client took too long to RENEW. */ |
| 1824 | dprintk("nfsd4_renew: clientid not found!\n"); |
| 1825 | goto out; |
| 1826 | } |
| 1827 | renew_client(clp); |
| 1828 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1829 | if (!list_empty(&clp->cl_delegations) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | && !atomic_read(&clp->cl_callback.cb_set)) |
| 1831 | goto out; |
| 1832 | status = nfs_ok; |
| 1833 | out: |
| 1834 | nfs4_unlock_state(); |
| 1835 | return status; |
| 1836 | } |
| 1837 | |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1838 | struct lock_manager nfsd4_manager = { |
| 1839 | }; |
| 1840 | |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 1841 | static void |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1842 | nfsd4_end_grace(void) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 1843 | { |
| 1844 | dprintk("NFSD: end of grace period\n"); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 1845 | nfsd4_recdir_purge_old(); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1846 | locks_end_grace(&nfsd4_manager); |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1849 | static time_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | nfs4_laundromat(void) |
| 1851 | { |
| 1852 | struct nfs4_client *clp; |
| 1853 | struct nfs4_stateowner *sop; |
| 1854 | struct nfs4_delegation *dp; |
| 1855 | struct list_head *pos, *next, reaplist; |
| 1856 | time_t cutoff = get_seconds() - NFSD_LEASE_TIME; |
| 1857 | time_t t, clientid_val = NFSD_LEASE_TIME; |
| 1858 | time_t u, test_val = NFSD_LEASE_TIME; |
| 1859 | |
| 1860 | nfs4_lock_state(); |
| 1861 | |
| 1862 | dprintk("NFSD: laundromat service - starting\n"); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1863 | if (locks_in_grace()) |
| 1864 | nfsd4_end_grace(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | list_for_each_safe(pos, next, &client_lru) { |
| 1866 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 1867 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 1868 | t = clp->cl_time - cutoff; |
| 1869 | if (clientid_val > t) |
| 1870 | clientid_val = t; |
| 1871 | break; |
| 1872 | } |
| 1873 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 1874 | clp->cl_clientid.cl_id); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 1875 | nfsd4_remove_clid_dir(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | expire_client(clp); |
| 1877 | } |
| 1878 | INIT_LIST_HEAD(&reaplist); |
| 1879 | spin_lock(&recall_lock); |
| 1880 | list_for_each_safe(pos, next, &del_recall_lru) { |
| 1881 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 1882 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
| 1883 | u = dp->dl_time - cutoff; |
| 1884 | if (test_val > u) |
| 1885 | test_val = u; |
| 1886 | break; |
| 1887 | } |
| 1888 | dprintk("NFSD: purging unused delegation dp %p, fp %p\n", |
| 1889 | dp, dp->dl_flock); |
| 1890 | list_move(&dp->dl_recall_lru, &reaplist); |
| 1891 | } |
| 1892 | spin_unlock(&recall_lock); |
| 1893 | list_for_each_safe(pos, next, &reaplist) { |
| 1894 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 1895 | list_del_init(&dp->dl_recall_lru); |
| 1896 | unhash_delegation(dp); |
| 1897 | } |
| 1898 | test_val = NFSD_LEASE_TIME; |
| 1899 | list_for_each_safe(pos, next, &close_lru) { |
| 1900 | sop = list_entry(pos, struct nfs4_stateowner, so_close_lru); |
| 1901 | if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) { |
| 1902 | u = sop->so_time - cutoff; |
| 1903 | if (test_val > u) |
| 1904 | test_val = u; |
| 1905 | break; |
| 1906 | } |
| 1907 | dprintk("NFSD: purging unused open stateowner (so_id %d)\n", |
| 1908 | sop->so_id); |
NeilBrown | 358dd55 | 2006-04-10 22:55:42 -0700 | [diff] [blame] | 1909 | release_stateowner(sop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | } |
| 1911 | if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT) |
| 1912 | clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT; |
| 1913 | nfs4_unlock_state(); |
| 1914 | return clientid_val; |
| 1915 | } |
| 1916 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 1917 | static struct workqueue_struct *laundry_wq; |
| 1918 | static void laundromat_main(struct work_struct *); |
| 1919 | static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main); |
| 1920 | |
| 1921 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1922 | laundromat_main(struct work_struct *not_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | { |
| 1924 | time_t t; |
| 1925 | |
| 1926 | t = nfs4_laundromat(); |
| 1927 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 1928 | queue_delayed_work(laundry_wq, &laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 1931 | static struct nfs4_stateowner * |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 1932 | search_close_lru(u32 st_id, int flags) |
| 1933 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | struct nfs4_stateowner *local = NULL; |
| 1935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | if (flags & CLOSE_STATE) { |
| 1937 | list_for_each_entry(local, &close_lru, so_close_lru) { |
| 1938 | if (local->so_id == st_id) |
| 1939 | return local; |
| 1940 | } |
| 1941 | } |
| 1942 | return NULL; |
| 1943 | } |
| 1944 | |
| 1945 | static inline int |
| 1946 | nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp) |
| 1947 | { |
Josef "Jeff" Sipek | 7eaa36e | 2006-12-08 02:36:41 -0800 | [diff] [blame] | 1948 | return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | static int |
| 1952 | STALE_STATEID(stateid_t *stateid) |
| 1953 | { |
| 1954 | if (stateid->si_boot == boot_time) |
| 1955 | return 0; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 1956 | dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid, |
| 1958 | stateid->si_generation); |
| 1959 | return 1; |
| 1960 | } |
| 1961 | |
| 1962 | static inline int |
| 1963 | access_permit_read(unsigned long access_bmap) |
| 1964 | { |
| 1965 | return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) || |
| 1966 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) || |
| 1967 | test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap); |
| 1968 | } |
| 1969 | |
| 1970 | static inline int |
| 1971 | access_permit_write(unsigned long access_bmap) |
| 1972 | { |
| 1973 | return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) || |
| 1974 | test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap); |
| 1975 | } |
| 1976 | |
| 1977 | static |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1978 | __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1980 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
| 1982 | if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap))) |
| 1983 | goto out; |
| 1984 | if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap))) |
| 1985 | goto out; |
| 1986 | status = nfs_ok; |
| 1987 | out: |
| 1988 | return status; |
| 1989 | } |
| 1990 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 1991 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags) |
| 1993 | { |
| 1994 | /* Trying to call delegreturn with a special stateid? Yuch: */ |
| 1995 | if (!(flags & (RD_STATE | WR_STATE))) |
| 1996 | return nfserr_bad_stateid; |
| 1997 | else if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
| 1998 | return nfs_ok; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 1999 | else if (locks_in_grace()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | /* Answer in remaining cases depends on existance of |
| 2001 | * conflicting state; so we must wait out the grace period. */ |
| 2002 | return nfserr_grace; |
| 2003 | } else if (flags & WR_STATE) |
| 2004 | return nfs4_share_conflict(current_fh, |
| 2005 | NFS4_SHARE_DENY_WRITE); |
| 2006 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 2007 | return nfs4_share_conflict(current_fh, |
| 2008 | NFS4_SHARE_DENY_READ); |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * Allow READ/WRITE during grace period on recovered state only for files |
| 2013 | * that are not able to provide mandatory locking. |
| 2014 | */ |
| 2015 | static inline int |
| 2016 | io_during_grace_disallowed(struct inode *inode, int flags) |
| 2017 | { |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 2018 | return locks_in_grace() && (flags & (RD_STATE | WR_STATE)) |
Pavel Emelyanov | a16877c | 2007-10-01 14:41:11 -0700 | [diff] [blame] | 2019 | && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 2022 | static int check_stateid_generation(stateid_t *in, stateid_t *ref) |
| 2023 | { |
| 2024 | /* If the client sends us a stateid from the future, it's buggy: */ |
| 2025 | if (in->si_generation > ref->si_generation) |
| 2026 | return nfserr_bad_stateid; |
| 2027 | /* |
| 2028 | * The following, however, can happen. For example, if the |
| 2029 | * client sends an open and some IO at the same time, the open |
| 2030 | * may bump si_generation while the IO is still in flight. |
| 2031 | * Thanks to hard links and renames, the client never knows what |
| 2032 | * file an open will affect. So it could avoid that situation |
| 2033 | * only by serializing all opens and IO from the same open |
| 2034 | * owner. To recover from the old_stateid error, the client |
| 2035 | * will just have to retry the IO: |
| 2036 | */ |
| 2037 | if (in->si_generation < ref->si_generation) |
| 2038 | return nfserr_old_stateid; |
| 2039 | return nfs_ok; |
| 2040 | } |
| 2041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | /* |
| 2043 | * Checks for stateid operations |
| 2044 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2045 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp) |
| 2047 | { |
| 2048 | struct nfs4_stateid *stp = NULL; |
| 2049 | struct nfs4_delegation *dp = NULL; |
| 2050 | stateid_t *stidp; |
| 2051 | struct inode *ino = current_fh->fh_dentry->d_inode; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2052 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
| 2054 | dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n", |
| 2055 | stateid->si_boot, stateid->si_stateownerid, |
| 2056 | stateid->si_fileid, stateid->si_generation); |
| 2057 | if (filpp) |
| 2058 | *filpp = NULL; |
| 2059 | |
| 2060 | if (io_during_grace_disallowed(ino, flags)) |
| 2061 | return nfserr_grace; |
| 2062 | |
| 2063 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) |
| 2064 | return check_special_stateids(current_fh, stateid, flags); |
| 2065 | |
| 2066 | /* STALE STATEID */ |
| 2067 | status = nfserr_stale_stateid; |
| 2068 | if (STALE_STATEID(stateid)) |
| 2069 | goto out; |
| 2070 | |
| 2071 | /* BAD STATEID */ |
| 2072 | status = nfserr_bad_stateid; |
| 2073 | if (!stateid->si_fileid) { /* delegation stateid */ |
| 2074 | if(!(dp = find_delegation_stateid(ino, stateid))) { |
| 2075 | dprintk("NFSD: delegation stateid not found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | goto out; |
| 2077 | } |
| 2078 | stidp = &dp->dl_stateid; |
| 2079 | } else { /* open or lock stateid */ |
| 2080 | if (!(stp = find_stateid(stateid, flags))) { |
| 2081 | dprintk("NFSD: open or lock stateid not found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | goto out; |
| 2083 | } |
| 2084 | if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) |
| 2085 | goto out; |
| 2086 | if (!stp->st_stateowner->so_confirmed) |
| 2087 | goto out; |
| 2088 | stidp = &stp->st_stateid; |
| 2089 | } |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 2090 | status = check_stateid_generation(stateid, stidp); |
| 2091 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | goto out; |
| 2093 | if (stp) { |
| 2094 | if ((status = nfs4_check_openmode(stp,flags))) |
| 2095 | goto out; |
| 2096 | renew_client(stp->st_stateowner->so_client); |
| 2097 | if (filpp) |
| 2098 | *filpp = stp->st_vfs_file; |
J. Bruce Fields | 6a85fa3 | 2008-01-26 23:36:48 -0500 | [diff] [blame] | 2099 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | if ((status = nfs4_check_delegmode(dp, flags))) |
| 2101 | goto out; |
| 2102 | renew_client(dp->dl_client); |
| 2103 | if (flags & DELEG_RET) |
| 2104 | unhash_delegation(dp); |
| 2105 | if (filpp) |
| 2106 | *filpp = dp->dl_vfs_file; |
| 2107 | } |
| 2108 | status = nfs_ok; |
| 2109 | out: |
| 2110 | return status; |
| 2111 | } |
| 2112 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2113 | static inline int |
| 2114 | setlkflg (int type) |
| 2115 | { |
| 2116 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 2117 | RD_STATE : WR_STATE; |
| 2118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | /* |
| 2121 | * Checks for sequence id mutating operations. |
| 2122 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2123 | static __be32 |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2124 | nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | struct nfs4_stateid *stp; |
| 2127 | struct nfs4_stateowner *sop; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 2128 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
| 2130 | dprintk("NFSD: preprocess_seqid_op: seqid=%d " |
| 2131 | "stateid = (%08x/%08x/%08x/%08x)\n", seqid, |
| 2132 | stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid, |
| 2133 | stateid->si_generation); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | *stpp = NULL; |
| 2136 | *sopp = NULL; |
| 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2139 | dprintk("NFSD: preprocess_seqid_op: magic stateid!\n"); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2140 | return nfserr_bad_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | } |
| 2142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | if (STALE_STATEID(stateid)) |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2144 | return nfserr_stale_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | /* |
| 2146 | * We return BAD_STATEID if filehandle doesn't match stateid, |
| 2147 | * the confirmed flag is incorrecly set, or the generation |
| 2148 | * number is incorrect. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | */ |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 2150 | stp = find_stateid(stateid, flags); |
| 2151 | if (stp == NULL) { |
| 2152 | /* |
| 2153 | * Also, we should make sure this isn't just the result of |
| 2154 | * a replayed close: |
| 2155 | */ |
| 2156 | sop = search_close_lru(stateid->si_stateownerid, flags); |
| 2157 | if (sop == NULL) |
| 2158 | return nfserr_bad_stateid; |
| 2159 | *sopp = sop; |
| 2160 | goto check_replay; |
| 2161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 2163 | *stpp = stp; |
| 2164 | *sopp = sop = stp->st_stateowner; |
| 2165 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2166 | if (lock) { |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2167 | clientid_t *lockclid = &lock->v.new.clientid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | struct nfs4_client *clp = sop->so_client; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2169 | int lkflg = 0; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2170 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2172 | lkflg = setlkflg(lock->lk_type); |
| 2173 | |
| 2174 | if (lock->lk_is_new) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2175 | if (!sop->so_is_open_owner) |
| 2176 | return nfserr_bad_stateid; |
| 2177 | if (!same_clid(&clp->cl_clientid, lockclid)) |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2178 | return nfserr_bad_stateid; |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2179 | /* stp is the open stateid */ |
| 2180 | status = nfs4_check_openmode(stp, lkflg); |
| 2181 | if (status) |
| 2182 | return status; |
| 2183 | } else { |
| 2184 | /* stp is the lock stateid */ |
| 2185 | status = nfs4_check_openmode(stp->st_openstp, lkflg); |
| 2186 | if (status) |
| 2187 | return status; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2191 | if (nfs4_check_fh(current_fh, stp)) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2192 | dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n"); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2193 | return nfserr_bad_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | /* |
| 2197 | * We now validate the seqid and stateid generation numbers. |
| 2198 | * For the moment, we ignore the possibility of |
| 2199 | * generation number wraparound. |
| 2200 | */ |
NeilBrown | bd9aac5 | 2005-07-07 17:59:19 -0700 | [diff] [blame] | 2201 | if (seqid != sop->so_seqid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | goto check_replay; |
| 2203 | |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2204 | if (sop->so_confirmed && flags & CONFIRM) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2205 | dprintk("NFSD: preprocess_seqid_op: expected" |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2206 | " unconfirmed stateowner!\n"); |
| 2207 | return nfserr_bad_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | } |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2209 | if (!sop->so_confirmed && !(flags & CONFIRM)) { |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2210 | dprintk("NFSD: preprocess_seqid_op: stateowner not" |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2211 | " confirmed yet!\n"); |
| 2212 | return nfserr_bad_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | } |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 2214 | status = check_stateid_generation(stateid, &stp->st_stateid); |
| 2215 | if (status) |
| 2216 | return status; |
NeilBrown | 52fd004 | 2005-07-07 17:59:24 -0700 | [diff] [blame] | 2217 | renew_client(sop->so_client); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2218 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | check_replay: |
NeilBrown | bd9aac5 | 2005-07-07 17:59:19 -0700 | [diff] [blame] | 2221 | if (seqid == sop->so_seqid - 1) { |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2222 | dprintk("NFSD: preprocess_seqid_op: retransmission?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | /* indicate replay to calling function */ |
Al Viro | a90b061 | 2006-10-19 23:29:03 -0700 | [diff] [blame] | 2224 | return nfserr_replay_me; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | } |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2226 | dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n", |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 2227 | sop->so_seqid, seqid); |
| 2228 | *sopp = NULL; |
| 2229 | return nfserr_bad_seqid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2232 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2233 | 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] | 2234 | struct nfsd4_open_confirm *oc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2236 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | struct nfs4_stateowner *sop; |
| 2238 | struct nfs4_stateid *stp; |
| 2239 | |
| 2240 | dprintk("NFSD: nfsd4_open_confirm on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2241 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 2242 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2244 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 2245 | if (status) |
| 2246 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | |
| 2248 | nfs4_lock_state(); |
| 2249 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2250 | if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
| 2251 | oc->oc_seqid, &oc->oc_req_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2252 | CONFIRM | OPEN_STATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | &oc->oc_stateowner, &stp, NULL))) |
| 2254 | goto out; |
| 2255 | |
| 2256 | sop = oc->oc_stateowner; |
| 2257 | sop->so_confirmed = 1; |
| 2258 | update_stateid(&stp->st_stateid); |
| 2259 | memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t)); |
| 2260 | dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d " |
| 2261 | "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid, |
| 2262 | stp->st_stateid.si_boot, |
| 2263 | stp->st_stateid.si_stateownerid, |
| 2264 | stp->st_stateid.si_fileid, |
| 2265 | stp->st_stateid.si_generation); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 2266 | |
| 2267 | nfsd4_create_clid_dir(sop->so_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | out: |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2269 | if (oc->oc_stateowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | nfs4_get_stateowner(oc->oc_stateowner); |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2271 | cstate->replay_owner = oc->oc_stateowner; |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | nfs4_unlock_state(); |
| 2274 | return status; |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | /* |
| 2279 | * unset all bits in union bitmap (bmap) that |
| 2280 | * do not exist in share (from successful OPEN_DOWNGRADE) |
| 2281 | */ |
| 2282 | static void |
| 2283 | reset_union_bmap_access(unsigned long access, unsigned long *bmap) |
| 2284 | { |
| 2285 | int i; |
| 2286 | for (i = 1; i < 4; i++) { |
| 2287 | if ((i & access) != i) |
| 2288 | __clear_bit(i, bmap); |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | static void |
| 2293 | reset_union_bmap_deny(unsigned long deny, unsigned long *bmap) |
| 2294 | { |
| 2295 | int i; |
| 2296 | for (i = 0; i < 4; i++) { |
| 2297 | if ((i & deny) != i) |
| 2298 | __clear_bit(i, bmap); |
| 2299 | } |
| 2300 | } |
| 2301 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2302 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2303 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
| 2304 | struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2305 | struct nfsd4_open_downgrade *od) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2307 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | struct nfs4_stateid *stp; |
| 2309 | unsigned int share_access; |
| 2310 | |
| 2311 | dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2312 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 2313 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
J. Bruce Fields | ba5a6a1 | 2006-06-30 01:56:16 -0700 | [diff] [blame] | 2315 | if (!access_valid(od->od_share_access) |
| 2316 | || !deny_valid(od->od_share_deny)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | return nfserr_inval; |
| 2318 | |
| 2319 | nfs4_lock_state(); |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2320 | if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
| 2321 | od->od_seqid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | &od->od_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2323 | OPEN_STATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | &od->od_stateowner, &stp, NULL))) |
| 2325 | goto out; |
| 2326 | |
| 2327 | status = nfserr_inval; |
| 2328 | if (!test_bit(od->od_share_access, &stp->st_access_bmap)) { |
| 2329 | dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n", |
| 2330 | stp->st_access_bmap, od->od_share_access); |
| 2331 | goto out; |
| 2332 | } |
| 2333 | if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) { |
| 2334 | dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n", |
| 2335 | stp->st_deny_bmap, od->od_share_deny); |
| 2336 | goto out; |
| 2337 | } |
| 2338 | set_access(&share_access, stp->st_access_bmap); |
| 2339 | nfs4_file_downgrade(stp->st_vfs_file, |
| 2340 | share_access & ~od->od_share_access); |
| 2341 | |
| 2342 | reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap); |
| 2343 | reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap); |
| 2344 | |
| 2345 | update_stateid(&stp->st_stateid); |
| 2346 | memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t)); |
| 2347 | status = nfs_ok; |
| 2348 | out: |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2349 | if (od->od_stateowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | nfs4_get_stateowner(od->od_stateowner); |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2351 | cstate->replay_owner = od->od_stateowner; |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | nfs4_unlock_state(); |
| 2354 | return status; |
| 2355 | } |
| 2356 | |
| 2357 | /* |
| 2358 | * nfs4_unlock_state() called after encode |
| 2359 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2360 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2361 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2362 | struct nfsd4_close *close) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2364 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | struct nfs4_stateid *stp; |
| 2366 | |
| 2367 | dprintk("NFSD: nfsd4_close on file %.*s\n", |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2368 | (int)cstate->current_fh.fh_dentry->d_name.len, |
| 2369 | cstate->current_fh.fh_dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | |
| 2371 | nfs4_lock_state(); |
| 2372 | /* check close_lru for replay */ |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2373 | if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
| 2374 | close->cl_seqid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | &close->cl_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2376 | OPEN_STATE | CLOSE_STATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | &close->cl_stateowner, &stp, NULL))) |
| 2378 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | status = nfs_ok; |
| 2380 | update_stateid(&stp->st_stateid); |
| 2381 | memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t)); |
| 2382 | |
J. Bruce Fields | 04ef595 | 2006-01-18 17:43:21 -0800 | [diff] [blame] | 2383 | /* release_stateid() calls nfsd_close() if needed */ |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 2384 | release_open_stateid(stp); |
J. Bruce Fields | 04ef595 | 2006-01-18 17:43:21 -0800 | [diff] [blame] | 2385 | |
| 2386 | /* place unused nfs4_stateowners on so_close_lru list to be |
| 2387 | * released by the laundromat service after the lease period |
| 2388 | * to enable us to handle CLOSE replay |
| 2389 | */ |
| 2390 | if (list_empty(&close->cl_stateowner->so_stateids)) |
| 2391 | move_to_close_lru(close->cl_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | out: |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2393 | if (close->cl_stateowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | nfs4_get_stateowner(close->cl_stateowner); |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2395 | cstate->replay_owner = close->cl_stateowner; |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | nfs4_unlock_state(); |
| 2398 | return status; |
| 2399 | } |
| 2400 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2401 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2402 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2403 | struct nfsd4_delegreturn *dr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2405 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2407 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | goto out; |
| 2409 | |
| 2410 | nfs4_lock_state(); |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2411 | status = nfs4_preprocess_stateid_op(&cstate->current_fh, |
| 2412 | &dr->dr_stateid, DELEG_RET, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | nfs4_unlock_state(); |
| 2414 | out: |
| 2415 | return status; |
| 2416 | } |
| 2417 | |
| 2418 | |
| 2419 | /* |
| 2420 | * Lock owner state (byte-range locks) |
| 2421 | */ |
| 2422 | #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start)) |
| 2423 | #define LOCK_HASH_BITS 8 |
| 2424 | #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS) |
| 2425 | #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1) |
| 2426 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2427 | static inline u64 |
| 2428 | end_offset(u64 start, u64 len) |
| 2429 | { |
| 2430 | u64 end; |
| 2431 | |
| 2432 | end = start + len; |
| 2433 | return end >= start ? end: NFS4_MAX_UINT64; |
| 2434 | } |
| 2435 | |
| 2436 | /* last octet in a range */ |
| 2437 | static inline u64 |
| 2438 | last_byte_offset(u64 start, u64 len) |
| 2439 | { |
| 2440 | u64 end; |
| 2441 | |
| 2442 | BUG_ON(!len); |
| 2443 | end = start + len; |
| 2444 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 2445 | } |
| 2446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | #define lockownerid_hashval(id) \ |
| 2448 | ((id) & LOCK_HASH_MASK) |
| 2449 | |
| 2450 | static inline unsigned int |
| 2451 | lock_ownerstr_hashval(struct inode *inode, u32 cl_id, |
| 2452 | struct xdr_netobj *ownername) |
| 2453 | { |
| 2454 | return (file_hashval(inode) + cl_id |
| 2455 | + opaque_hashval(ownername->data, ownername->len)) |
| 2456 | & LOCK_HASH_MASK; |
| 2457 | } |
| 2458 | |
| 2459 | static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE]; |
| 2460 | static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE]; |
| 2461 | static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE]; |
| 2462 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2463 | static struct nfs4_stateid * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | find_stateid(stateid_t *stid, int flags) |
| 2465 | { |
Krishna Kumar | 9346eff | 2008-10-20 11:44:28 +0530 | [diff] [blame] | 2466 | struct nfs4_stateid *local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | u32 st_id = stid->si_stateownerid; |
| 2468 | u32 f_id = stid->si_fileid; |
| 2469 | unsigned int hashval; |
| 2470 | |
| 2471 | dprintk("NFSD: find_stateid flags 0x%x\n",flags); |
Krishna Kumar | 9346eff | 2008-10-20 11:44:28 +0530 | [diff] [blame] | 2472 | if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | hashval = stateid_hashval(st_id, f_id); |
| 2474 | list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) { |
| 2475 | if ((local->st_stateid.si_stateownerid == st_id) && |
| 2476 | (local->st_stateid.si_fileid == f_id)) |
| 2477 | return local; |
| 2478 | } |
| 2479 | } |
Krishna Kumar | 9346eff | 2008-10-20 11:44:28 +0530 | [diff] [blame] | 2480 | |
| 2481 | if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | hashval = stateid_hashval(st_id, f_id); |
| 2483 | list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) { |
| 2484 | if ((local->st_stateid.si_stateownerid == st_id) && |
| 2485 | (local->st_stateid.si_fileid == f_id)) |
| 2486 | return local; |
| 2487 | } |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | return NULL; |
| 2490 | } |
| 2491 | |
| 2492 | static struct nfs4_delegation * |
| 2493 | find_delegation_stateid(struct inode *ino, stateid_t *stid) |
| 2494 | { |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2495 | struct nfs4_file *fp; |
| 2496 | struct nfs4_delegation *dl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
| 2498 | dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n", |
| 2499 | stid->si_boot, stid->si_stateownerid, |
| 2500 | stid->si_fileid, stid->si_generation); |
| 2501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | fp = find_file(ino); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2503 | if (!fp) |
| 2504 | return NULL; |
| 2505 | dl = find_delegation_file(fp, stid); |
| 2506 | put_nfs4_file(fp); |
| 2507 | return dl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 2512 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 2513 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 2514 | * locking, this prevents us from being completely protocol-compliant. The |
| 2515 | * real solution to this problem is to start using unsigned file offsets in |
| 2516 | * the VFS, but this is a very deep change! |
| 2517 | */ |
| 2518 | static inline void |
| 2519 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 2520 | { |
| 2521 | if (lock->fl_start < 0) |
| 2522 | lock->fl_start = OFFSET_MAX; |
| 2523 | if (lock->fl_end < 0) |
| 2524 | lock->fl_end = OFFSET_MAX; |
| 2525 | } |
| 2526 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2527 | /* Hack!: For now, we're defining this just so we can use a pointer to it |
| 2528 | * as a unique cookie to identify our (NFSv4's) posix locks. */ |
Adrian Bunk | e465a77 | 2006-04-10 22:55:23 -0700 | [diff] [blame] | 2529 | static struct lock_manager_operations nfsd_posix_mng_ops = { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2530 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | |
| 2532 | static inline void |
| 2533 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 2534 | { |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2535 | struct nfs4_stateowner *sop; |
| 2536 | unsigned int hval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2538 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
| 2539 | sop = (struct nfs4_stateowner *) fl->fl_owner; |
| 2540 | hval = lockownerid_hashval(sop->so_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | kref_get(&sop->so_ref); |
| 2542 | deny->ld_sop = sop; |
| 2543 | deny->ld_clientid = sop->so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2544 | } else { |
| 2545 | deny->ld_sop = NULL; |
| 2546 | deny->ld_clientid.cl_boot = 0; |
| 2547 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | } |
| 2549 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2550 | deny->ld_length = NFS4_MAX_UINT64; |
| 2551 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 2553 | deny->ld_type = NFS4_READ_LT; |
| 2554 | if (fl->fl_type != F_RDLCK) |
| 2555 | deny->ld_type = NFS4_WRITE_LT; |
| 2556 | } |
| 2557 | |
| 2558 | static struct nfs4_stateowner * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | find_lockstateowner_str(struct inode *inode, clientid_t *clid, |
| 2560 | struct xdr_netobj *owner) |
| 2561 | { |
| 2562 | unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner); |
| 2563 | struct nfs4_stateowner *op; |
| 2564 | |
| 2565 | list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2566 | if (same_owner_str(op, owner, clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | return op; |
| 2568 | } |
| 2569 | return NULL; |
| 2570 | } |
| 2571 | |
| 2572 | /* |
| 2573 | * Alloc a lock owner structure. |
| 2574 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has |
| 2575 | * occured. |
| 2576 | * |
| 2577 | * strhashval = lock_ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | */ |
| 2579 | |
| 2580 | static struct nfs4_stateowner * |
| 2581 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) { |
| 2582 | struct nfs4_stateowner *sop; |
| 2583 | struct nfs4_replay *rp; |
| 2584 | unsigned int idhashval; |
| 2585 | |
| 2586 | if (!(sop = alloc_stateowner(&lock->lk_new_owner))) |
| 2587 | return NULL; |
| 2588 | idhashval = lockownerid_hashval(current_ownerid); |
| 2589 | INIT_LIST_HEAD(&sop->so_idhash); |
| 2590 | INIT_LIST_HEAD(&sop->so_strhash); |
| 2591 | INIT_LIST_HEAD(&sop->so_perclient); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2592 | INIT_LIST_HEAD(&sop->so_stateids); |
| 2593 | INIT_LIST_HEAD(&sop->so_perstateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | INIT_LIST_HEAD(&sop->so_close_lru); /* not used */ |
| 2595 | sop->so_time = 0; |
| 2596 | list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]); |
| 2597 | list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2598 | list_add(&sop->so_perstateid, &open_stp->st_lockowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | sop->so_is_open_owner = 0; |
| 2600 | sop->so_id = current_ownerid++; |
| 2601 | sop->so_client = clp; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2602 | /* It is the openowner seqid that will be incremented in encode in the |
| 2603 | * case of new lockowners; so increment the lock seqid manually: */ |
| 2604 | sop->so_seqid = lock->lk_new_lock_seqid + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | sop->so_confirmed = 1; |
| 2606 | rp = &sop->so_replay; |
Al Viro | de1ae28 | 2006-01-18 17:43:47 -0800 | [diff] [blame] | 2607 | rp->rp_status = nfserr_serverfault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | rp->rp_buflen = 0; |
| 2609 | rp->rp_buf = rp->rp_ibuf; |
| 2610 | return sop; |
| 2611 | } |
| 2612 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2613 | static struct nfs4_stateid * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp) |
| 2615 | { |
| 2616 | struct nfs4_stateid *stp; |
| 2617 | unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id); |
| 2618 | |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 2619 | stp = nfs4_alloc_stateid(); |
| 2620 | if (stp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | goto out; |
| 2622 | INIT_LIST_HEAD(&stp->st_hash); |
| 2623 | INIT_LIST_HEAD(&stp->st_perfile); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2624 | INIT_LIST_HEAD(&stp->st_perstateowner); |
| 2625 | INIT_LIST_HEAD(&stp->st_lockowners); /* not used */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]); |
NeilBrown | 8beefa2 | 2005-06-23 22:03:08 -0700 | [diff] [blame] | 2627 | list_add(&stp->st_perfile, &fp->fi_stateids); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2628 | list_add(&stp->st_perstateowner, &sop->so_stateids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | stp->st_stateowner = sop; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 2630 | get_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | stp->st_file = fp; |
| 2632 | stp->st_stateid.si_boot = boot_time; |
| 2633 | stp->st_stateid.si_stateownerid = sop->so_id; |
| 2634 | stp->st_stateid.si_fileid = fp->fi_id; |
| 2635 | stp->st_stateid.si_generation = 0; |
| 2636 | stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */ |
| 2637 | stp->st_access_bmap = open_stp->st_access_bmap; |
| 2638 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 2639 | stp->st_openstp = open_stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | |
| 2641 | out: |
| 2642 | return stp; |
| 2643 | } |
| 2644 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2645 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | check_lock_length(u64 offset, u64 length) |
| 2647 | { |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2648 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2649 | LOFF_OVERFLOW(offset, length))); |
| 2650 | } |
| 2651 | |
| 2652 | /* |
| 2653 | * LOCK operation |
| 2654 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2655 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2656 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2657 | struct nfsd4_lock *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | { |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 2659 | struct nfs4_stateowner *open_sop = NULL; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2660 | struct nfs4_stateowner *lock_sop = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | struct nfs4_stateid *lock_stp; |
| 2662 | struct file *filp; |
| 2663 | struct file_lock file_lock; |
Andy Adamson | 8dc7c31 | 2006-03-20 13:44:26 -0500 | [diff] [blame] | 2664 | struct file_lock conflock; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2665 | __be32 status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2666 | unsigned int strhashval; |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2667 | unsigned int cmd; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2668 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | |
| 2670 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 2671 | (long long) lock->lk_offset, |
| 2672 | (long long) lock->lk_length); |
| 2673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 2675 | return nfserr_inval; |
| 2676 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2677 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2678 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 2679 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 2680 | return status; |
| 2681 | } |
| 2682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | nfs4_lock_state(); |
| 2684 | |
| 2685 | if (lock->lk_is_new) { |
NeilBrown | 893f877 | 2005-07-07 17:59:17 -0700 | [diff] [blame] | 2686 | /* |
| 2687 | * Client indicates that this is a new lockowner. |
| 2688 | * Use open owner and open stateid to create lock owner and |
| 2689 | * lock stateid. |
| 2690 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | struct nfs4_stateid *open_stp = NULL; |
| 2692 | struct nfs4_file *fp; |
| 2693 | |
| 2694 | status = nfserr_stale_clientid; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2695 | if (STALE_CLIENTID(&lock->lk_new_clientid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | /* validate and update open stateid and open seqid */ |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2699 | status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | lock->lk_new_open_seqid, |
| 2701 | &lock->lk_new_open_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2702 | OPEN_STATE, |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2703 | &lock->lk_replay_owner, &open_stp, |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2704 | lock); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 2705 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | goto out; |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2707 | open_sop = lock->lk_replay_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | /* create lockowner and lock stateid */ |
| 2709 | fp = open_stp->st_file; |
| 2710 | strhashval = lock_ownerstr_hashval(fp->fi_inode, |
| 2711 | open_sop->so_client->cl_clientid.cl_id, |
| 2712 | &lock->v.new.owner); |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 2713 | /* XXX: Do we need to check for duplicate stateowners on |
| 2714 | * the same file, or should they just be allowed (and |
| 2715 | * create new stateids)? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | status = nfserr_resource; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2717 | lock_sop = alloc_init_lock_stateowner(strhashval, |
| 2718 | open_sop->so_client, open_stp, lock); |
| 2719 | if (lock_sop == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | goto out; |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2721 | lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp); |
J. Bruce Fields | 8a28051 | 2006-01-18 17:43:18 -0800 | [diff] [blame] | 2722 | if (lock_stp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | } else { |
| 2725 | /* lock (lock owner + lock stateid) already exists */ |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2726 | status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | lock->lk_old_lock_seqid, |
| 2728 | &lock->lk_old_lock_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2729 | LOCK_STATE, |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2730 | &lock->lk_replay_owner, &lock_stp, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | if (status) |
| 2732 | goto out; |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2733 | lock_sop = lock->lk_replay_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | } |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2735 | /* lock->lk_replay_owner and lock_stp have been created or found */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | filp = lock_stp->st_vfs_file; |
| 2737 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 2738 | status = nfserr_grace; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 2739 | if (locks_in_grace() && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 2740 | goto out; |
| 2741 | status = nfserr_no_grace; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 2742 | if (!locks_in_grace() && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 2743 | goto out; |
| 2744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | locks_init_lock(&file_lock); |
| 2746 | switch (lock->lk_type) { |
| 2747 | case NFS4_READ_LT: |
| 2748 | case NFS4_READW_LT: |
| 2749 | file_lock.fl_type = F_RDLCK; |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2750 | cmd = F_SETLK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | break; |
| 2752 | case NFS4_WRITE_LT: |
| 2753 | case NFS4_WRITEW_LT: |
| 2754 | file_lock.fl_type = F_WRLCK; |
Marc Eshel | 150b393 | 2007-01-18 16:15:35 -0500 | [diff] [blame] | 2755 | cmd = F_SETLK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | break; |
| 2757 | default: |
| 2758 | status = nfserr_inval; |
| 2759 | goto out; |
| 2760 | } |
Neil Brown | b59e3c0 | 2005-09-13 01:25:38 -0700 | [diff] [blame] | 2761 | file_lock.fl_owner = (fl_owner_t)lock_sop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | file_lock.fl_pid = current->tgid; |
| 2763 | file_lock.fl_file = filp; |
| 2764 | file_lock.fl_flags = FL_POSIX; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2765 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | |
| 2767 | file_lock.fl_start = lock->lk_offset; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2768 | 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] | 2769 | nfs4_transform_lock_offset(&file_lock); |
| 2770 | |
| 2771 | /* |
| 2772 | * Try to lock the file in the VFS. |
| 2773 | * Note: locks.c uses the BKL to protect the inode's lock list. |
| 2774 | */ |
| 2775 | |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2776 | err = vfs_lock_file(filp, cmd, &file_lock, &conflock); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2777 | switch (-err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | case 0: /* success! */ |
| 2779 | update_stateid(&lock_stp->st_stateid); |
| 2780 | memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, |
| 2781 | sizeof(stateid_t)); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2782 | status = 0; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 2783 | break; |
| 2784 | case (EAGAIN): /* conflock holds conflicting lock */ |
| 2785 | status = nfserr_denied; |
| 2786 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
| 2787 | nfs4_set_lock_denied(&conflock, &lock->lk_denied); |
| 2788 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | case (EDEADLK): |
| 2790 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 2791 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2793 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 2794 | status = nfserr_resource; |
| 2795 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | out: |
J. Bruce Fields | 8a28051 | 2006-01-18 17:43:18 -0800 | [diff] [blame] | 2798 | if (status && lock->lk_is_new && lock_sop) |
| 2799 | release_stateowner(lock_sop); |
J. Bruce Fields | 3a65588 | 2006-01-18 17:43:19 -0800 | [diff] [blame] | 2800 | if (lock->lk_replay_owner) { |
| 2801 | nfs4_get_stateowner(lock->lk_replay_owner); |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2802 | cstate->replay_owner = lock->lk_replay_owner; |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | nfs4_unlock_state(); |
| 2805 | return status; |
| 2806 | } |
| 2807 | |
| 2808 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 2809 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 2810 | * so we do a temporary open here just to get an open file to pass to |
| 2811 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 2812 | * inode operation.) |
| 2813 | */ |
| 2814 | static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
| 2815 | { |
| 2816 | struct file *file; |
| 2817 | int err; |
| 2818 | |
| 2819 | err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file); |
| 2820 | if (err) |
| 2821 | return err; |
| 2822 | err = vfs_test_lock(file, lock); |
| 2823 | nfsd_close(file); |
| 2824 | return err; |
| 2825 | } |
| 2826 | |
| 2827 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | * LOCKT operation |
| 2829 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2830 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2831 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 2832 | struct nfsd4_lockt *lockt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | { |
| 2834 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | struct file_lock file_lock; |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2836 | int error; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2837 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 2839 | if (locks_in_grace()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | return nfserr_grace; |
| 2841 | |
| 2842 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 2843 | return nfserr_inval; |
| 2844 | |
| 2845 | lockt->lt_stateowner = NULL; |
| 2846 | nfs4_lock_state(); |
| 2847 | |
| 2848 | status = nfserr_stale_clientid; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2849 | if (STALE_CLIENTID(&lockt->lt_clientid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2852 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) { |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 2853 | dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | if (status == nfserr_symlink) |
| 2855 | status = nfserr_inval; |
| 2856 | goto out; |
| 2857 | } |
| 2858 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2859 | inode = cstate->current_fh.fh_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | locks_init_lock(&file_lock); |
| 2861 | switch (lockt->lt_type) { |
| 2862 | case NFS4_READ_LT: |
| 2863 | case NFS4_READW_LT: |
| 2864 | file_lock.fl_type = F_RDLCK; |
| 2865 | break; |
| 2866 | case NFS4_WRITE_LT: |
| 2867 | case NFS4_WRITEW_LT: |
| 2868 | file_lock.fl_type = F_WRLCK; |
| 2869 | break; |
| 2870 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 2871 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | status = nfserr_inval; |
| 2873 | goto out; |
| 2874 | } |
| 2875 | |
| 2876 | lockt->lt_stateowner = find_lockstateowner_str(inode, |
| 2877 | &lockt->lt_clientid, &lockt->lt_owner); |
| 2878 | if (lockt->lt_stateowner) |
| 2879 | file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner; |
| 2880 | file_lock.fl_pid = current->tgid; |
| 2881 | file_lock.fl_flags = FL_POSIX; |
| 2882 | |
| 2883 | file_lock.fl_start = lockt->lt_offset; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2884 | 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] | 2885 | |
| 2886 | nfs4_transform_lock_offset(&file_lock); |
| 2887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | status = nfs_ok; |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 2889 | error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock); |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2890 | if (error) { |
| 2891 | status = nfserrno(error); |
| 2892 | goto out; |
| 2893 | } |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 2894 | if (file_lock.fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | status = nfserr_denied; |
Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 2896 | nfs4_set_lock_denied(&file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | } |
| 2898 | out: |
| 2899 | nfs4_unlock_state(); |
| 2900 | return status; |
| 2901 | } |
| 2902 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2903 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2904 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2905 | struct nfsd4_locku *locku) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | { |
| 2907 | struct nfs4_stateid *stp; |
| 2908 | struct file *filp = NULL; |
| 2909 | struct file_lock file_lock; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2910 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2911 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
| 2913 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 2914 | (long long) locku->lu_offset, |
| 2915 | (long long) locku->lu_length); |
| 2916 | |
| 2917 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 2918 | return nfserr_inval; |
| 2919 | |
| 2920 | nfs4_lock_state(); |
| 2921 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 2922 | if ((status = nfs4_preprocess_seqid_op(&cstate->current_fh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | locku->lu_seqid, |
| 2924 | &locku->lu_stateid, |
J. Bruce Fields | f336273 | 2008-01-26 14:58:45 -0500 | [diff] [blame] | 2925 | LOCK_STATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | &locku->lu_stateowner, &stp, NULL))) |
| 2927 | goto out; |
| 2928 | |
| 2929 | filp = stp->st_vfs_file; |
| 2930 | BUG_ON(!filp); |
| 2931 | locks_init_lock(&file_lock); |
| 2932 | file_lock.fl_type = F_UNLCK; |
| 2933 | file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner; |
| 2934 | file_lock.fl_pid = current->tgid; |
| 2935 | file_lock.fl_file = filp; |
| 2936 | file_lock.fl_flags = FL_POSIX; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 2937 | file_lock.fl_lmops = &nfsd_posix_mng_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | file_lock.fl_start = locku->lu_offset; |
| 2939 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 2940 | 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] | 2941 | nfs4_transform_lock_offset(&file_lock); |
| 2942 | |
| 2943 | /* |
| 2944 | * Try to unlock the file in the VFS. |
| 2945 | */ |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2946 | err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2947 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 2948 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | goto out_nfserr; |
| 2950 | } |
| 2951 | /* |
| 2952 | * OK, unlock succeeded; the only thing left to do is update the stateid. |
| 2953 | */ |
| 2954 | update_stateid(&stp->st_stateid); |
| 2955 | memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t)); |
| 2956 | |
| 2957 | out: |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2958 | if (locku->lu_stateowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | nfs4_get_stateowner(locku->lu_stateowner); |
J.Bruce Fields | a4f1706a9 | 2006-12-13 00:35:28 -0800 | [diff] [blame] | 2960 | cstate->replay_owner = locku->lu_stateowner; |
Neil Brown | f2327d9 | 2005-09-13 01:25:37 -0700 | [diff] [blame] | 2961 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | nfs4_unlock_state(); |
| 2963 | return status; |
| 2964 | |
| 2965 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 2966 | status = nfserrno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | goto out; |
| 2968 | } |
| 2969 | |
| 2970 | /* |
| 2971 | * returns |
| 2972 | * 1: locks held by lockowner |
| 2973 | * 0: no locks held by lockowner |
| 2974 | */ |
| 2975 | static int |
| 2976 | check_for_locks(struct file *filp, struct nfs4_stateowner *lowner) |
| 2977 | { |
| 2978 | struct file_lock **flpp; |
Josef "Jeff" Sipek | 7eaa36e | 2006-12-08 02:36:41 -0800 | [diff] [blame] | 2979 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | int status = 0; |
| 2981 | |
| 2982 | lock_kernel(); |
| 2983 | for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) { |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 2984 | if ((*flpp)->fl_owner == (fl_owner_t)lowner) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | status = 1; |
| 2986 | goto out; |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 2987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | } |
| 2989 | out: |
| 2990 | unlock_kernel(); |
| 2991 | return status; |
| 2992 | } |
| 2993 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 2994 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 2995 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 2996 | struct nfsd4_compound_state *cstate, |
| 2997 | struct nfsd4_release_lockowner *rlockowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | { |
| 2999 | clientid_t *clid = &rlockowner->rl_clientid; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3000 | struct nfs4_stateowner *sop; |
| 3001 | struct nfs4_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3003 | struct list_head matches; |
| 3004 | int i; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3005 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
| 3007 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 3008 | clid->cl_boot, clid->cl_id); |
| 3009 | |
| 3010 | /* XXX check for lease expiration */ |
| 3011 | |
| 3012 | status = nfserr_stale_clientid; |
Neil Brown | 849823c | 2005-09-13 01:25:36 -0700 | [diff] [blame] | 3013 | if (STALE_CLIENTID(clid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | |
| 3016 | nfs4_lock_state(); |
| 3017 | |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3018 | status = nfserr_locks_held; |
| 3019 | /* XXX: we're doing a linear search through all the lockowners. |
| 3020 | * Yipes! For now we'll just hope clients aren't really using |
| 3021 | * release_lockowner much, but eventually we have to fix these |
| 3022 | * data structures. */ |
| 3023 | INIT_LIST_HEAD(&matches); |
| 3024 | for (i = 0; i < LOCK_HASH_SIZE; i++) { |
| 3025 | list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) { |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 3026 | if (!same_owner_str(sop, owner, clid)) |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3027 | continue; |
| 3028 | list_for_each_entry(stp, &sop->so_stateids, |
| 3029 | st_perstateowner) { |
| 3030 | if (check_for_locks(stp->st_vfs_file, sop)) |
| 3031 | goto out; |
| 3032 | /* Note: so_perclient unused for lockowners, |
| 3033 | * so it's OK to fool with here. */ |
| 3034 | list_add(&sop->so_perclient, &matches); |
| 3035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | } |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3037 | } |
| 3038 | /* Clients probably won't expect us to return with some (but not all) |
| 3039 | * of the lockowner state released; so don't release any until all |
| 3040 | * have been checked. */ |
| 3041 | status = nfs_ok; |
NeilBrown | 0fa822e | 2005-07-07 17:59:14 -0700 | [diff] [blame] | 3042 | while (!list_empty(&matches)) { |
| 3043 | sop = list_entry(matches.next, struct nfs4_stateowner, |
| 3044 | so_perclient); |
| 3045 | /* unhash_stateowner deletes so_perclient only |
| 3046 | * for openowners. */ |
| 3047 | list_del(&sop->so_perclient); |
NeilBrown | 3e9e3db | 2005-06-23 22:04:20 -0700 | [diff] [blame] | 3048 | release_stateowner(sop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | } |
| 3050 | out: |
| 3051 | nfs4_unlock_state(); |
| 3052 | return status; |
| 3053 | } |
| 3054 | |
| 3055 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3056 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3058 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | } |
| 3060 | |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 3061 | int |
| 3062 | nfs4_has_reclaimed_state(const char *name) |
| 3063 | { |
| 3064 | unsigned int strhashval = clientstr_hashval(name); |
| 3065 | struct nfs4_client *clp; |
| 3066 | |
| 3067 | clp = find_confirmed_client_by_str(name, strhashval); |
| 3068 | return clp ? 1 : 0; |
| 3069 | } |
| 3070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | /* |
| 3072 | * failure => all reset bets are off, nfserr_no_grace... |
| 3073 | */ |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3074 | int |
| 3075 | nfs4_client_to_reclaim(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | { |
| 3077 | unsigned int strhashval; |
| 3078 | struct nfs4_client_reclaim *crp = NULL; |
| 3079 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3080 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name); |
| 3081 | crp = alloc_reclaim(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | if (!crp) |
| 3083 | return 0; |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3084 | strhashval = clientstr_hashval(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | INIT_LIST_HEAD(&crp->cr_strhash); |
| 3086 | list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3087 | memcpy(crp->cr_recdir, name, HEXDIR_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | reclaim_str_hashtbl_size++; |
| 3089 | return 1; |
| 3090 | } |
| 3091 | |
| 3092 | static void |
| 3093 | nfs4_release_reclaim(void) |
| 3094 | { |
| 3095 | struct nfs4_client_reclaim *crp = NULL; |
| 3096 | int i; |
| 3097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 3099 | while (!list_empty(&reclaim_str_hashtbl[i])) { |
| 3100 | crp = list_entry(reclaim_str_hashtbl[i].next, |
| 3101 | struct nfs4_client_reclaim, cr_strhash); |
| 3102 | list_del(&crp->cr_strhash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | kfree(crp); |
| 3104 | reclaim_str_hashtbl_size--; |
| 3105 | } |
| 3106 | } |
| 3107 | BUG_ON(reclaim_str_hashtbl_size); |
| 3108 | } |
| 3109 | |
| 3110 | /* |
| 3111 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 3112 | static struct nfs4_client_reclaim * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | nfs4_find_reclaim_client(clientid_t *clid) |
| 3114 | { |
| 3115 | unsigned int strhashval; |
| 3116 | struct nfs4_client *clp; |
| 3117 | struct nfs4_client_reclaim *crp = NULL; |
| 3118 | |
| 3119 | |
| 3120 | /* find clientid in conf_id_hashtbl */ |
| 3121 | clp = find_confirmed_client(clid); |
| 3122 | if (clp == NULL) |
| 3123 | return NULL; |
| 3124 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3125 | dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n", |
| 3126 | clp->cl_name.len, clp->cl_name.data, |
| 3127 | clp->cl_recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
| 3129 | /* find clp->cl_name in reclaim_str_hashtbl */ |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3130 | strhashval = clientstr_hashval(clp->cl_recdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 3132 | if (same_name(crp->cr_recdir, clp->cl_recdir)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | return crp; |
| 3134 | } |
| 3135 | } |
| 3136 | return NULL; |
| 3137 | } |
| 3138 | |
| 3139 | /* |
| 3140 | * Called from OPEN. Look for clientid in reclaim list. |
| 3141 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3142 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | nfs4_check_open_reclaim(clientid_t *clid) |
| 3144 | { |
NeilBrown | dfc8356 | 2005-06-23 22:03:16 -0700 | [diff] [blame] | 3145 | return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3148 | /* initialization to perform at module load time: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3150 | int |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3151 | nfs4_state_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | { |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3153 | int i, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3155 | status = nfsd4_init_slabs(); |
| 3156 | if (status) |
| 3157 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 3159 | INIT_LIST_HEAD(&conf_id_hashtbl[i]); |
| 3160 | INIT_LIST_HEAD(&conf_str_hashtbl[i]); |
| 3161 | INIT_LIST_HEAD(&unconf_str_hashtbl[i]); |
| 3162 | INIT_LIST_HEAD(&unconf_id_hashtbl[i]); |
| 3163 | } |
| 3164 | for (i = 0; i < FILE_HASH_SIZE; i++) { |
| 3165 | INIT_LIST_HEAD(&file_hashtbl[i]); |
| 3166 | } |
| 3167 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 3168 | INIT_LIST_HEAD(&ownerstr_hashtbl[i]); |
| 3169 | INIT_LIST_HEAD(&ownerid_hashtbl[i]); |
| 3170 | } |
| 3171 | for (i = 0; i < STATEID_HASH_SIZE; i++) { |
| 3172 | INIT_LIST_HEAD(&stateid_hashtbl[i]); |
| 3173 | INIT_LIST_HEAD(&lockstateid_hashtbl[i]); |
| 3174 | } |
| 3175 | for (i = 0; i < LOCK_HASH_SIZE; i++) { |
| 3176 | INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]); |
| 3177 | INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]); |
| 3178 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | memset(&onestateid, ~0, sizeof(stateid_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | INIT_LIST_HEAD(&close_lru); |
| 3181 | INIT_LIST_HEAD(&client_lru); |
| 3182 | INIT_LIST_HEAD(&del_recall_lru); |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3183 | for (i = 0; i < CLIENT_HASH_SIZE; i++) |
| 3184 | INIT_LIST_HEAD(&reclaim_str_hashtbl[i]); |
| 3185 | reclaim_str_hashtbl_size = 0; |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3186 | return 0; |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3187 | } |
| 3188 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3189 | static void |
| 3190 | nfsd4_load_reboot_recovery_data(void) |
| 3191 | { |
| 3192 | int status; |
| 3193 | |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3194 | nfs4_lock_state(); |
| 3195 | nfsd4_init_recdir(user_recovery_dirname); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3196 | status = nfsd4_recdir_load(); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3197 | nfs4_unlock_state(); |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3198 | if (status) |
| 3199 | printk("NFSD: Failure reading reboot recovery data\n"); |
| 3200 | } |
| 3201 | |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 3202 | unsigned long |
| 3203 | get_nfs4_grace_period(void) |
| 3204 | { |
| 3205 | return max(user_lease_time, lease_time) * HZ; |
| 3206 | } |
| 3207 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 3208 | /* |
| 3209 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 3210 | * client may quite reasonably hang on to a delegation as long as it has |
| 3211 | * the inode cached. This becomes an obvious problem the first time a |
| 3212 | * client's inode cache approaches the size of the server's total memory. |
| 3213 | * |
| 3214 | * For now we avoid this problem by imposing a hard limit on the number |
| 3215 | * of delegations, which varies according to the server's memory size. |
| 3216 | */ |
| 3217 | static void |
| 3218 | set_max_delegations(void) |
| 3219 | { |
| 3220 | /* |
| 3221 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 3222 | * estimates suggest that in the worst case (where every delegation |
| 3223 | * is for a different inode), a delegation could take about 1.5K, |
| 3224 | * giving a worst case usage of about 6% of memory. |
| 3225 | */ |
| 3226 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 3227 | } |
| 3228 | |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3229 | /* initialization to perform when the nfsd service is started: */ |
| 3230 | |
| 3231 | static void |
| 3232 | __nfs4_state_start(void) |
| 3233 | { |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 3234 | unsigned long grace_time; |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 3235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | boot_time = get_seconds(); |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3237 | grace_time = get_nfs4_grace_period(); |
NeilBrown | d99a05a | 2005-06-23 22:03:21 -0700 | [diff] [blame] | 3238 | lease_time = user_lease_time; |
J. Bruce Fields | af558e3 | 2007-09-06 12:34:25 -0400 | [diff] [blame] | 3239 | locks_start_grace(&nfsd4_manager); |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 3240 | printk(KERN_INFO "NFSD: starting %ld-second grace period\n", |
| 3241 | grace_time/HZ); |
NeilBrown | 58da282 | 2005-06-23 22:03:19 -0700 | [diff] [blame] | 3242 | laundry_wq = create_singlethread_workqueue("nfsd4"); |
Marc Eshel | 9a8db97 | 2007-07-17 04:04:35 -0700 | [diff] [blame] | 3243 | queue_delayed_work(laundry_wq, &laundromat_work, grace_time); |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 3244 | set_max_delegations(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3247 | void |
NeilBrown | 76a3550 | 2005-06-23 22:03:26 -0700 | [diff] [blame] | 3248 | nfs4_state_start(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3249 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | if (nfs4_init) |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3251 | return; |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3252 | nfsd4_load_reboot_recovery_data(); |
NeilBrown | 76a3550 | 2005-06-23 22:03:26 -0700 | [diff] [blame] | 3253 | __nfs4_state_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | nfs4_init = 1; |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 3255 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | time_t |
| 3259 | nfs4_lease_time(void) |
| 3260 | { |
| 3261 | return lease_time; |
| 3262 | } |
| 3263 | |
| 3264 | static void |
| 3265 | __nfs4_state_shutdown(void) |
| 3266 | { |
| 3267 | int i; |
| 3268 | struct nfs4_client *clp = NULL; |
| 3269 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | struct list_head *pos, *next, reaplist; |
| 3271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 3273 | while (!list_empty(&conf_id_hashtbl[i])) { |
| 3274 | clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 3275 | expire_client(clp); |
| 3276 | } |
| 3277 | while (!list_empty(&unconf_str_hashtbl[i])) { |
| 3278 | clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash); |
| 3279 | expire_client(clp); |
| 3280 | } |
| 3281 | } |
| 3282 | INIT_LIST_HEAD(&reaplist); |
| 3283 | spin_lock(&recall_lock); |
| 3284 | list_for_each_safe(pos, next, &del_recall_lru) { |
| 3285 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 3286 | list_move(&dp->dl_recall_lru, &reaplist); |
| 3287 | } |
| 3288 | spin_unlock(&recall_lock); |
| 3289 | list_for_each_safe(pos, next, &reaplist) { |
| 3290 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 3291 | list_del_init(&dp->dl_recall_lru); |
| 3292 | unhash_delegation(dp); |
| 3293 | } |
| 3294 | |
NeilBrown | 190e4fb | 2005-06-23 22:04:25 -0700 | [diff] [blame] | 3295 | nfsd4_shutdown_recdir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | nfs4_init = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | void |
| 3300 | nfs4_state_shutdown(void) |
| 3301 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 3302 | cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work); |
| 3303 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | 2c5e761 | 2008-11-20 14:36:17 -0600 | [diff] [blame] | 3304 | locks_end_grace(&nfsd4_manager); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | nfs4_lock_state(); |
| 3306 | nfs4_release_reclaim(); |
| 3307 | __nfs4_state_shutdown(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | nfs4_unlock_state(); |
| 3309 | } |
| 3310 | |
Jeff Layton | 3dd98a3 | 2008-06-10 08:40:36 -0400 | [diff] [blame] | 3311 | /* |
| 3312 | * user_recovery_dirname is protected by the nfsd_mutex since it's only |
| 3313 | * accessed when nfsd is starting. |
| 3314 | */ |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3315 | static void |
| 3316 | nfs4_set_recdir(char *recdir) |
| 3317 | { |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3318 | strcpy(user_recovery_dirname, recdir); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | /* |
| 3322 | * Change the NFSv4 recovery directory to recdir. |
| 3323 | */ |
| 3324 | int |
| 3325 | nfs4_reset_recoverydir(char *recdir) |
| 3326 | { |
| 3327 | int status; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 3328 | struct path path; |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3329 | |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 3330 | status = kern_path(recdir, LOOKUP_FOLLOW, &path); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3331 | if (status) |
| 3332 | return status; |
| 3333 | status = -ENOTDIR; |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 3334 | if (S_ISDIR(path.dentry->d_inode->i_mode)) { |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3335 | nfs4_set_recdir(recdir); |
| 3336 | status = 0; |
| 3337 | } |
Al Viro | a63bb99 | 2008-08-02 01:03:36 -0400 | [diff] [blame] | 3338 | path_put(&path); |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 3339 | return status; |
| 3340 | } |
| 3341 | |
Jeff Layton | 3dd98a3 | 2008-06-10 08:40:36 -0400 | [diff] [blame] | 3342 | char * |
| 3343 | nfs4_recoverydir(void) |
| 3344 | { |
| 3345 | return user_recovery_dirname; |
| 3346 | } |
| 3347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | /* |
| 3349 | * Called when leasetime is changed. |
| 3350 | * |
NeilBrown | d99a05a | 2005-06-23 22:03:21 -0700 | [diff] [blame] | 3351 | * The only way the protocol gives us to handle on-the-fly lease changes is to |
| 3352 | * simulate a reboot. Instead of doing that, we just wait till the next time |
| 3353 | * we start to register any changes in lease time. If the administrator |
| 3354 | * really wants to change the lease time *now*, they can go ahead and bring |
| 3355 | * nfsd down and then back up again after changing the lease time. |
Jeff Layton | 3dd98a3 | 2008-06-10 08:40:36 -0400 | [diff] [blame] | 3356 | * |
| 3357 | * user_lease_time is protected by nfsd_mutex since it's only really accessed |
| 3358 | * when nfsd is starting |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | */ |
| 3360 | void |
| 3361 | nfs4_reset_lease(time_t leasetime) |
| 3362 | { |
NeilBrown | d99a05a | 2005-06-23 22:03:21 -0700 | [diff] [blame] | 3363 | user_lease_time = leasetime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | } |