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