1/* 2 * linux/fs/lockd/svcsubs.c 3 * 4 * Various support routines for the NLM server. 5 * 6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 7 */ 8 9#include <linux/types.h> 10#include <linux/string.h> 11#include <linux/time.h> 12#include <linux/in.h> 13#include <linux/slab.h> 14#include <linux/mutex.h> 15#include <linux/sunrpc/svc.h> 16#include <linux/sunrpc/clnt.h> 17#include <linux/nfsd/nfsfh.h> 18#include <linux/nfsd/export.h> 19#include <linux/lockd/lockd.h> 20#include <linux/lockd/share.h> 21#include <linux/module.h> 22#include <linux/mount.h> 23 24#define NLMDBG_FACILITY NLMDBG_SVCSUBS 25 26 27/* 28 * Global file hash table 29 */ 30#define FILE_HASH_BITS 7 31#define FILE_NRHASH (1<<FILE_HASH_BITS) 32static struct hlist_head nlm_files[FILE_NRHASH]; 33static DEFINE_MUTEX(nlm_file_mutex); 34 35#ifdef NFSD_DEBUG 36static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f) 37{ 38 u32 *fhp = (u32*)f->data; 39 40 /* print the first 32 bytes of the fh */ 41 dprintk("lockd: %s (%08x %08x %08x %08x %08x %08x %08x %08x)\n", 42 msg, fhp[0], fhp[1], fhp[2], fhp[3], 43 fhp[4], fhp[5], fhp[6], fhp[7]); 44} 45 46static inline void nlm_debug_print_file(char *msg, struct nlm_file *file) 47{ 48 struct inode *inode = file->f_file->f_path.dentry->d_inode; 49 50 dprintk("lockd: %s %s/%ld\n", 51 msg, inode->i_sb->s_id, inode->i_ino); 52} 53#else 54static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f) 55{ 56 return; 57} 58 59static inline void nlm_debug_print_file(char *msg, struct nlm_file *file) 60{ 61 return; 62} 63#endif 64 65static inline unsigned int file_hash(struct nfs_fh *f) 66{ 67 unsigned int tmp=0; 68 int i; 69 for (i=0; i<NFS2_FHSIZE;i++) 70 tmp += f->data[i]; 71 return tmp & (FILE_NRHASH - 1); 72} 73 74__be32 75nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result, 76 struct nfs_fh *f) 77{ 78 struct hlist_node *pos; 79 struct nlm_file *file; 80 unsigned int hash; 81 __be32 nfserr; 82 83 nlm_debug_print_fh("nlm_lookup_file", f); 84 85 hash = file_hash(f); 86 87 /* Lock file table */ 88 mutex_lock(&nlm_file_mutex); 89 90 hlist_for_each_entry(file, pos, &nlm_files[hash], f_list) 91 if (!nfs_compare_fh(&file->f_handle, f)) 92 goto found; 93 94 nlm_debug_print_fh("creating file for", f); 95 96 nfserr = nlm_lck_denied_nolocks; 97 file = kzalloc(sizeof(*file), GFP_KERNEL); 98 if (!file) 99 goto out_unlock; 100 101 memcpy(&file->f_handle, f, sizeof(struct nfs_fh)); 102 mutex_init(&file->f_mutex); 103 INIT_HLIST_NODE(&file->f_list); 104 INIT_LIST_HEAD(&file->f_blocks); 105 106 /* Open the file. Note that this must not sleep for too long, else 107 * we would lock up lockd:-) So no NFS re-exports, folks. 108 * 109 * We have to make sure we have the right credential to open 110 * the file. 111 */ 112 if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) { 113 dprintk("lockd: open failed (error %d)\n", nfserr); 114 goto out_free; 115 } 116 117 hlist_add_head(&file->f_list, &nlm_files[hash]); 118 119found: 120 dprintk("lockd: found file %p (count %d)\n", file, file->f_count); 121 *result = file; 122 file->f_count++; 123 nfserr = 0; 124 125out_unlock: 126 mutex_unlock(&nlm_file_mutex); 127 return nfserr; 128 129out_free: 130 kfree(file); 131 goto out_unlock; 132} 133 134/* 135 * Delete a file after having released all locks, blocks and shares 136 */ 137static inline void 138nlm_delete_file(struct nlm_file *file) 139{ 140 nlm_debug_print_file("closing file", file); 141 if (!hlist_unhashed(&file->f_list)) { 142 hlist_del(&file->f_list); 143 nlmsvc_ops->fclose(file->f_file); 144 kfree(file); 145 } else { 146 printk(KERN_WARNING "lockd: attempt to release unknown file!\n"); 147 } 148} 149 150/* 151 * Loop over all locks on the given file and perform the specified 152 * action. 153 */ 154static int 155nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, 156 nlm_host_match_fn_t match) 157{ 158 struct inode *inode = nlmsvc_file_inode(file); 159 struct file_lock *fl; 160 struct nlm_host *lockhost; 161 162again: 163 file->f_locks = 0; 164 for (fl = inode->i_flock; fl; fl = fl->fl_next) { 165 if (fl->fl_lmops != &nlmsvc_lock_operations) 166 continue; 167 168 /* update current lock count */ 169 file->f_locks++; 170 171 lockhost = (struct nlm_host *) fl->fl_owner; 172 if (match(lockhost, host)) { 173 struct file_lock lock = *fl; 174 175 lock.fl_type = F_UNLCK; 176 lock.fl_start = 0; 177 lock.fl_end = OFFSET_MAX; 178 if (vfs_lock_file(file->f_file, F_SETLK, &lock, NULL) < 0) { 179 printk("lockd: unlock failure in %s:%d\n", 180 __FILE__, __LINE__); 181 return 1; 182 } 183 goto again; 184 } 185 } 186 187 return 0; 188} 189 190static int 191nlmsvc_always_match(void *dummy1, struct nlm_host *dummy2) 192{ 193 return 1; 194} 195 196/* 197 * Inspect a single file 198 */ 199static inline int 200nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match) 201{ 202 nlmsvc_traverse_blocks(host, file, match); 203 nlmsvc_traverse_shares(host, file, match); 204 return nlm_traverse_locks(host, file, match); 205} 206 207/* 208 * Quick check whether there are still any locks, blocks or 209 * shares on a given file. 210 */ 211static inline int 212nlm_file_inuse(struct nlm_file *file) 213{ 214 struct inode *inode = nlmsvc_file_inode(file); 215 struct file_lock *fl; 216 217 if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares) 218 return 1; 219 220 for (fl = inode->i_flock; fl; fl = fl->fl_next) { 221 if (fl->fl_lmops == &nlmsvc_lock_operations) 222 return 1; 223 } 224 file->f_locks = 0; 225 return 0; 226} 227 228/* 229 * Loop over all files in the file table. 230 */ 231static int 232nlm_traverse_files(void *data, nlm_host_match_fn_t match, 233 int (*is_failover_file)(void *data, struct nlm_file *file)) 234{ 235 struct hlist_node *pos, *next; 236 struct nlm_file *file; 237 int i, ret = 0; 238 239 mutex_lock(&nlm_file_mutex); 240 for (i = 0; i < FILE_NRHASH; i++) { 241 hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) { 242 if (is_failover_file && !is_failover_file(data, file)) 243 continue; 244 file->f_count++; 245 mutex_unlock(&nlm_file_mutex); 246 247 /* Traverse locks, blocks and shares of this file 248 * and update file->f_locks count */ 249 if (nlm_inspect_file(data, file, match)) 250 ret = 1; 251 252 mutex_lock(&nlm_file_mutex); 253 file->f_count--; 254 /* No more references to this file. Let go of it. */ 255 if (list_empty(&file->f_blocks) && !file->f_locks 256 && !file->f_shares && !file->f_count) { 257 hlist_del(&file->f_list); 258 nlmsvc_ops->fclose(file->f_file); 259 kfree(file); 260 } 261 } 262 } 263 mutex_unlock(&nlm_file_mutex); 264 return ret; 265} 266 267/* 268 * Release file. If there are no more remote locks on this file, 269 * close it and free the handle. 270 * 271 * Note that we can't do proper reference counting without major 272 * contortions because the code in fs/locks.c creates, deletes and 273 * splits locks without notification. Our only way is to walk the 274 * entire lock list each time we remove a lock. 275 */ 276void 277nlm_release_file(struct nlm_file *file) 278{ 279 dprintk("lockd: nlm_release_file(%p, ct = %d)\n", 280 file, file->f_count); 281 282 /* Lock file table */ 283 mutex_lock(&nlm_file_mutex); 284 285 /* If there are no more locks etc, delete the file */ 286 if (--file->f_count == 0 && !nlm_file_inuse(file)) 287 nlm_delete_file(file); 288 289 mutex_unlock(&nlm_file_mutex); 290} 291 292/* 293 * Helpers function for resource traversal 294 * 295 * nlmsvc_mark_host: 296 * used by the garbage collector; simply sets h_inuse. 297 * Always returns 0. 298 * 299 * nlmsvc_same_host: 300 * returns 1 iff the two hosts match. Used to release 301 * all resources bound to a specific host. 302 * 303 * nlmsvc_is_client: 304 * returns 1 iff the host is a client. 305 * Used by nlmsvc_invalidate_all 306 */ 307static int 308nlmsvc_mark_host(void *data, struct nlm_host *dummy) 309{ 310 struct nlm_host *host = data; 311 312 host->h_inuse = 1; 313 return 0; 314} 315 316static int 317nlmsvc_same_host(void *data, struct nlm_host *other) 318{ 319 struct nlm_host *host = data; 320 321 return host == other; 322} 323 324static int 325nlmsvc_is_client(void *data, struct nlm_host *dummy) 326{ 327 struct nlm_host *host = data; 328 329 if (host->h_server) { 330 /* we are destroying locks even though the client 331 * hasn't asked us too, so don't unmonitor the 332 * client 333 */ 334 if (host->h_nsmhandle) 335 host->h_nsmhandle->sm_sticky = 1; 336 return 1; 337 } else 338 return 0; 339} 340 341/* 342 * Mark all hosts that still hold resources 343 */ 344void 345nlmsvc_mark_resources(void) 346{ 347 dprintk("lockd: nlmsvc_mark_resources\n"); 348 nlm_traverse_files(NULL, nlmsvc_mark_host, NULL); 349} 350 351/* 352 * Release all resources held by the given client 353 */ 354void 355nlmsvc_free_host_resources(struct nlm_host *host) 356{ 357 dprintk("lockd: nlmsvc_free_host_resources\n"); 358 359 if (nlm_traverse_files(host, nlmsvc_same_host, NULL)) { 360 printk(KERN_WARNING 361 "lockd: couldn't remove all locks held by %s\n", 362 host->h_name); 363 BUG(); 364 } 365} 366 367/** 368 * nlmsvc_invalidate_all - remove all locks held for clients 369 * 370 * Release all locks held by NFS clients. 371 * 372 */ 373void 374nlmsvc_invalidate_all(void) 375{ 376 /* 377 * Previously, the code would call 378 * nlmsvc_free_host_resources for each client in 379 * turn, which is about as inefficient as it gets. 380 * Now we just do it once in nlm_traverse_files. 381 */ 382 nlm_traverse_files(NULL, nlmsvc_is_client, NULL); 383} 384 385static int 386nlmsvc_match_sb(void *datap, struct nlm_file *file) 387{ 388 struct super_block *sb = datap; 389 390 return sb == file->f_file->f_path.mnt->mnt_sb; 391} 392 393/** 394 * nlmsvc_unlock_all_by_sb - release locks held on this file system 395 * @sb: super block 396 * 397 * Release all locks held by clients accessing this file system. 398 */ 399int 400nlmsvc_unlock_all_by_sb(struct super_block *sb) 401{ 402 int ret; 403 404 ret = nlm_traverse_files(sb, nlmsvc_always_match, nlmsvc_match_sb); 405 return ret ? -EIO : 0; 406} 407EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_sb); 408 409static int 410nlmsvc_match_ip(void *datap, struct nlm_host *host) 411{ 412 return rpc_cmp_addr(nlm_srcaddr(host), datap); 413} 414 415/** 416 * nlmsvc_unlock_all_by_ip - release local locks by IP address 417 * @server_addr: server's IP address as seen by clients 418 * 419 * Release all locks held by clients accessing this host 420 * via the passed in IP address. 421 */ 422int 423nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr) 424{ 425 int ret; 426 427 ret = nlm_traverse_files(server_addr, nlmsvc_match_ip, NULL); 428 return ret ? -EIO : 0; 429} 430EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip); 431