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