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