svcsubs.c 8.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * linux/fs/lockd/svcsubs.c
 *
 * Various support routines for the NLM server.
 *
 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
 */

#include <linux/types.h>
#include <linux/string.h>
#include <linux/time.h>
#include <linux/in.h>
I
Ingo Molnar 已提交
13
#include <linux/mutex.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19 20
#include <linux/sunrpc/svc.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfsd/nfsfh.h>
#include <linux/nfsd/export.h>
#include <linux/lockd/lockd.h>
#include <linux/lockd/share.h>
#include <linux/lockd/sm_inter.h>
21 22
#include <linux/module.h>
#include <linux/mount.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29

#define NLMDBG_FACILITY		NLMDBG_SVCSUBS


/*
 * Global file hash table
 */
30
#define FILE_HASH_BITS		7
L
Linus Torvalds 已提交
31
#define FILE_NRHASH		(1<<FILE_HASH_BITS)
32
static struct hlist_head	nlm_files[FILE_NRHASH];
I
Ingo Molnar 已提交
33
static DEFINE_MUTEX(nlm_file_mutex);
L
Linus Torvalds 已提交
34

35 36 37 38 39 40 41 42 43 44 45 46 47
#ifdef NFSD_DEBUG
static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
{
	u32 *fhp = (u32*)f->data;

	/* print the first 32 bytes of the fh */
	dprintk("lockd: %s (%08x %08x %08x %08x %08x %08x %08x %08x)\n",
		msg, fhp[0], fhp[1], fhp[2], fhp[3],
		fhp[4], fhp[5], fhp[6], fhp[7]);
}

static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
{
J
Josef Sipek 已提交
48
	struct inode *inode = file->f_file->f_path.dentry->d_inode;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	dprintk("lockd: %s %s/%ld\n",
		msg, inode->i_sb->s_id, inode->i_ino);
}
#else
static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
{
	return;
}

static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
{
	return;
}
#endif

L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static inline unsigned int file_hash(struct nfs_fh *f)
{
	unsigned int tmp=0;
	int i;
	for (i=0; i<NFS2_FHSIZE;i++)
		tmp += f->data[i];
	return tmp & (FILE_NRHASH - 1);
}

/*
 * Lookup file info. If it doesn't exist, create a file info struct
 * and open a (VFS) file for the given inode.
 *
 * FIXME:
 * Note that we open the file O_RDONLY even when creating write locks.
 * This is not quite right, but for now, we assume the client performs
 * the proper R/W checking.
 */
A
Al Viro 已提交
83
__be32
L
Linus Torvalds 已提交
84 85 86
nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
					struct nfs_fh *f)
{
87
	struct hlist_node *pos;
L
Linus Torvalds 已提交
88 89
	struct nlm_file	*file;
	unsigned int	hash;
A
Al Viro 已提交
90
	__be32		nfserr;
L
Linus Torvalds 已提交
91

J
J. Bruce Fields 已提交
92
	nlm_debug_print_fh("nlm_lookup_file", f);
L
Linus Torvalds 已提交
93 94 95 96

	hash = file_hash(f);

	/* Lock file table */
I
Ingo Molnar 已提交
97
	mutex_lock(&nlm_file_mutex);
L
Linus Torvalds 已提交
98

99
	hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
L
Linus Torvalds 已提交
100 101 102
		if (!nfs_compare_fh(&file->f_handle, f))
			goto found;

103
	nlm_debug_print_fh("creating file for", f);
L
Linus Torvalds 已提交
104 105

	nfserr = nlm_lck_denied_nolocks;
106
	file = kzalloc(sizeof(*file), GFP_KERNEL);
L
Linus Torvalds 已提交
107 108 109 110
	if (!file)
		goto out_unlock;

	memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
111
	mutex_init(&file->f_mutex);
112
	INIT_HLIST_NODE(&file->f_list);
113
	INIT_LIST_HEAD(&file->f_blocks);
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121

	/* Open the file. Note that this must not sleep for too long, else
	 * we would lock up lockd:-) So no NFS re-exports, folks.
	 *
	 * We have to make sure we have the right credential to open
	 * the file.
	 */
	if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) {
122
		dprintk("lockd: open failed (error %d)\n", nfserr);
L
Linus Torvalds 已提交
123 124 125
		goto out_free;
	}

126
	hlist_add_head(&file->f_list, &nlm_files[hash]);
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134

found:
	dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
	*result = file;
	file->f_count++;
	nfserr = 0;

out_unlock:
I
Ingo Molnar 已提交
135
	mutex_unlock(&nlm_file_mutex);
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
	return nfserr;

out_free:
	kfree(file);
	goto out_unlock;
}

/*
 * Delete a file after having released all locks, blocks and shares
 */
static inline void
nlm_delete_file(struct nlm_file *file)
{
149
	nlm_debug_print_file("closing file", file);
150 151 152 153 154 155
	if (!hlist_unhashed(&file->f_list)) {
		hlist_del(&file->f_list);
		nlmsvc_ops->fclose(file->f_file);
		kfree(file);
	} else {
		printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
L
Linus Torvalds 已提交
156 157 158 159 160 161 162 163
	}
}

/*
 * Loop over all locks on the given file and perform the specified
 * action.
 */
static int
164 165
nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
			nlm_host_match_fn_t match)
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173
{
	struct inode	 *inode = nlmsvc_file_inode(file);
	struct file_lock *fl;
	struct nlm_host	 *lockhost;

again:
	file->f_locks = 0;
	for (fl = inode->i_flock; fl; fl = fl->fl_next) {
J
J. Bruce Fields 已提交
174
		if (fl->fl_lmops != &nlmsvc_lock_operations)
L
Linus Torvalds 已提交
175 176 177 178
			continue;

		/* update current lock count */
		file->f_locks++;
179

L
Linus Torvalds 已提交
180
		lockhost = (struct nlm_host *) fl->fl_owner;
181
		if (match(lockhost, host)) {
L
Linus Torvalds 已提交
182 183 184 185 186
			struct file_lock lock = *fl;

			lock.fl_type  = F_UNLCK;
			lock.fl_start = 0;
			lock.fl_end   = OFFSET_MAX;
187
			if (vfs_lock_file(file->f_file, F_SETLK, &lock, NULL) < 0) {
L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198 199
				printk("lockd: unlock failure in %s:%d\n",
						__FILE__, __LINE__);
				return 1;
			}
			goto again;
		}
	}

	return 0;
}

/*
200 201 202 203 204 205 206 207 208 209 210 211 212
 * Inspect a single file
 */
static inline int
nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
{
	nlmsvc_traverse_blocks(host, file, match);
	nlmsvc_traverse_shares(host, file, match);
	return nlm_traverse_locks(host, file, match);
}

/*
 * Quick check whether there are still any locks, blocks or
 * shares on a given file.
L
Linus Torvalds 已提交
213 214
 */
static inline int
215
nlm_file_inuse(struct nlm_file *file)
L
Linus Torvalds 已提交
216
{
217 218 219 220 221 222 223 224
	struct inode	 *inode = nlmsvc_file_inode(file);
	struct file_lock *fl;

	if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
		return 1;

	for (fl = inode->i_flock; fl; fl = fl->fl_next) {
		if (fl->fl_lmops == &nlmsvc_lock_operations)
L
Linus Torvalds 已提交
225 226
			return 1;
	}
227 228
	file->f_locks = 0;
	return 0;
L
Linus Torvalds 已提交
229 230 231 232 233 234
}

/*
 * Loop over all files in the file table.
 */
static int
235
nlm_traverse_files(void *data, nlm_host_match_fn_t match)
L
Linus Torvalds 已提交
236
{
237 238
	struct hlist_node *pos, *next;
	struct nlm_file	*file;
239
	int i, ret = 0;
L
Linus Torvalds 已提交
240

I
Ingo Molnar 已提交
241
	mutex_lock(&nlm_file_mutex);
L
Linus Torvalds 已提交
242
	for (i = 0; i < FILE_NRHASH; i++) {
243
		hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
244 245 246
			file->f_count++;
			mutex_unlock(&nlm_file_mutex);

L
Linus Torvalds 已提交
247 248
			/* Traverse locks, blocks and shares of this file
			 * and update file->f_locks count */
249
			if (nlm_inspect_file(data, file, match))
250
				ret = 1;
L
Linus Torvalds 已提交
251

252 253
			mutex_lock(&nlm_file_mutex);
			file->f_count--;
L
Linus Torvalds 已提交
254
			/* No more references to this file. Let go of it. */
255
			if (list_empty(&file->f_blocks) && !file->f_locks
L
Linus Torvalds 已提交
256
			 && !file->f_shares && !file->f_count) {
257
				hlist_del(&file->f_list);
L
Linus Torvalds 已提交
258 259 260 261 262
				nlmsvc_ops->fclose(file->f_file);
				kfree(file);
			}
		}
	}
I
Ingo Molnar 已提交
263
	mutex_unlock(&nlm_file_mutex);
264
	return ret;
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

/*
 * Release file. If there are no more remote locks on this file,
 * close it and free the handle.
 *
 * Note that we can't do proper reference counting without major
 * contortions because the code in fs/locks.c creates, deletes and
 * splits locks without notification. Our only way is to walk the
 * entire lock list each time we remove a lock.
 */
void
nlm_release_file(struct nlm_file *file)
{
	dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
				file, file->f_count);

	/* Lock file table */
I
Ingo Molnar 已提交
283
	mutex_lock(&nlm_file_mutex);
L
Linus Torvalds 已提交
284 285

	/* If there are no more locks etc, delete the file */
286 287
	if (--file->f_count == 0 && !nlm_file_inuse(file))
		nlm_delete_file(file);
L
Linus Torvalds 已提交
288

I
Ingo Molnar 已提交
289
	mutex_unlock(&nlm_file_mutex);
L
Linus Torvalds 已提交
290 291
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/*
 * Helpers function for resource traversal
 *
 * nlmsvc_mark_host:
 *	used by the garbage collector; simply sets h_inuse.
 *	Always returns 0.
 *
 * nlmsvc_same_host:
 *	returns 1 iff the two hosts match. Used to release
 *	all resources bound to a specific host.
 *
 * nlmsvc_is_client:
 *	returns 1 iff the host is a client.
 *	Used by nlmsvc_invalidate_all
 */
static int
308
nlmsvc_mark_host(void *data, struct nlm_host *dummy)
309
{
310 311
	struct nlm_host *host = data;

312 313 314 315 316
	host->h_inuse = 1;
	return 0;
}

static int
317
nlmsvc_same_host(void *data, struct nlm_host *other)
318
{
319 320
	struct nlm_host *host = data;

321 322 323 324
	return host == other;
}

static int
325
nlmsvc_is_client(void *data, struct nlm_host *dummy)
326
{
327 328
	struct nlm_host *host = data;

329 330 331 332 333 334 335 336 337 338
	if (host->h_server) {
		/* we are destroying locks even though the client
		 * hasn't asked us too, so don't unmonitor the
		 * client
		 */
		if (host->h_nsmhandle)
			host->h_nsmhandle->sm_sticky = 1;
		return 1;
	} else
		return 0;
339 340
}

L
Linus Torvalds 已提交
341 342 343 344 345 346 347
/*
 * Mark all hosts that still hold resources
 */
void
nlmsvc_mark_resources(void)
{
	dprintk("lockd: nlmsvc_mark_resources\n");
348
	nlm_traverse_files(NULL, nlmsvc_mark_host);
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358
}

/*
 * Release all resources held by the given client
 */
void
nlmsvc_free_host_resources(struct nlm_host *host)
{
	dprintk("lockd: nlmsvc_free_host_resources\n");

359
	if (nlm_traverse_files(host, nlmsvc_same_host)) {
L
Linus Torvalds 已提交
360
		printk(KERN_WARNING
361
			"lockd: couldn't remove all locks held by %s\n",
L
Linus Torvalds 已提交
362
			host->h_name);
363 364
		BUG();
	}
L
Linus Torvalds 已提交
365 366 367
}

/*
368
 * Remove all locks held for clients
L
Linus Torvalds 已提交
369 370 371 372
 */
void
nlmsvc_invalidate_all(void)
{
373 374 375 376 377 378 379
	/* Release all locks held by NFS clients.
	 * Previously, the code would call
	 * nlmsvc_free_host_resources for each client in
	 * turn, which is about as inefficient as it gets.
	 * Now we just do it once in nlm_traverse_files.
	 */
	nlm_traverse_files(NULL, nlmsvc_is_client);
L
Linus Torvalds 已提交
380
}
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

static int
nlmsvc_match_ip(void *datap, struct nlm_host *host)
{
	__be32 *server_addr = datap;

	return host->h_saddr.sin_addr.s_addr == *server_addr;
}

int
nlmsvc_unlock_all_by_ip(__be32 server_addr)
{
	int ret;
	ret = nlm_traverse_files(&server_addr, nlmsvc_match_ip);
	return ret ? -EIO : 0;

}
EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip);