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