svcsubs.c 8.48 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
Ingo Molnar's avatar
Ingo Molnar committed
13
#include <linux/mutex.h>
Linus Torvalds's avatar
Linus Torvalds committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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>

#define NLMDBG_FACILITY		NLMDBG_SVCSUBS


/*
 * Global file hash table
 */
28
#define FILE_HASH_BITS		7
Linus Torvalds's avatar
Linus Torvalds committed
29
#define FILE_NRHASH		(1<<FILE_HASH_BITS)
30
static struct hlist_head	nlm_files[FILE_NRHASH];
Ingo Molnar's avatar
Ingo Molnar committed
31
static DEFINE_MUTEX(nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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)
{
	struct inode *inode = file->f_file->f_dentry->d_inode;

	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

Linus Torvalds's avatar
Linus Torvalds committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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.
 */
u32
nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
					struct nfs_fh *f)
{
85
	struct hlist_node *pos;
Linus Torvalds's avatar
Linus Torvalds committed
86
87
88
89
	struct nlm_file	*file;
	unsigned int	hash;
	u32		nfserr;

90
	nlm_debug_print_fh("nlm_file_lookup", f);
Linus Torvalds's avatar
Linus Torvalds committed
91
92
93
94

	hash = file_hash(f);

	/* Lock file table */
Ingo Molnar's avatar
Ingo Molnar committed
95
	mutex_lock(&nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
96

97
	hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
Linus Torvalds's avatar
Linus Torvalds committed
98
99
100
		if (!nfs_compare_fh(&file->f_handle, f))
			goto found;

101
	nlm_debug_print_fh("creating file for", f);
Linus Torvalds's avatar
Linus Torvalds committed
102
103

	nfserr = nlm_lck_denied_nolocks;
104
	file = kzalloc(sizeof(*file), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
105
106
107
108
	if (!file)
		goto out_unlock;

	memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
109
	mutex_init(&file->f_mutex);
110
	INIT_HLIST_NODE(&file->f_list);
111
	INIT_LIST_HEAD(&file->f_blocks);
Linus Torvalds's avatar
Linus Torvalds committed
112
113
114
115
116
117
118
119

	/* 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) {
120
		dprintk("lockd: open failed (error %d)\n", nfserr);
Linus Torvalds's avatar
Linus Torvalds committed
121
122
123
		goto out_free;
	}

124
	hlist_add_head(&file->f_list, &nlm_files[hash]);
Linus Torvalds's avatar
Linus Torvalds committed
125
126
127
128
129
130
131
132

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

out_unlock:
Ingo Molnar's avatar
Ingo Molnar committed
133
	mutex_unlock(&nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	return nfserr;

out_free:
	kfree(file);
#ifdef CONFIG_LOCKD_V4
	if (nfserr == 1)
		nfserr = nlm4_stale_fh;
	else
#endif
	nfserr = nlm_lck_denied;
	goto out_unlock;
}

/*
 * Delete a file after having released all locks, blocks and shares
 */
static inline void
nlm_delete_file(struct nlm_file *file)
{
153
	nlm_debug_print_file("closing file", file);
154
155
156
157
158
159
	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");
Linus Torvalds's avatar
Linus Torvalds committed
160
161
162
163
164
165
166
167
	}
}

/*
 * Loop over all locks on the given file and perform the specified
 * action.
 */
static int
168
169
nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
			nlm_host_match_fn_t match)
Linus Torvalds's avatar
Linus Torvalds committed
170
171
172
173
174
175
176
177
{
	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. Bruce Fields's avatar
J. Bruce Fields committed
178
		if (fl->fl_lmops != &nlmsvc_lock_operations)
Linus Torvalds's avatar
Linus Torvalds committed
179
180
181
182
			continue;

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

Linus Torvalds's avatar
Linus Torvalds committed
184
		lockhost = (struct nlm_host *) fl->fl_owner;
185
		if (match(lockhost, host)) {
Linus Torvalds's avatar
Linus Torvalds committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
			struct file_lock lock = *fl;

			lock.fl_type  = F_UNLCK;
			lock.fl_start = 0;
			lock.fl_end   = OFFSET_MAX;
			if (posix_lock_file(file->f_file, &lock) < 0) {
				printk("lockd: unlock failure in %s:%d\n",
						__FILE__, __LINE__);
				return 1;
			}
			goto again;
		}
	}

	return 0;
}

/*
204
205
206
207
208
209
210
211
212
213
214
215
216
 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
217
218
 */
static inline int
219
nlm_file_inuse(struct nlm_file *file)
Linus Torvalds's avatar
Linus Torvalds committed
220
{
221
222
223
224
225
226
227
228
	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)
Linus Torvalds's avatar
Linus Torvalds committed
229
230
			return 1;
	}
231
232
	file->f_locks = 0;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
233
234
235
236
237
238
}

/*
 * Loop over all files in the file table.
 */
static int
239
nlm_traverse_files(struct nlm_host *host, nlm_host_match_fn_t match)
Linus Torvalds's avatar
Linus Torvalds committed
240
{
241
242
	struct hlist_node *pos, *next;
	struct nlm_file	*file;
243
	int i, ret = 0;
Linus Torvalds's avatar
Linus Torvalds committed
244

Ingo Molnar's avatar
Ingo Molnar committed
245
	mutex_lock(&nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
246
	for (i = 0; i < FILE_NRHASH; i++) {
247
		hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
248
249
250
			file->f_count++;
			mutex_unlock(&nlm_file_mutex);

Linus Torvalds's avatar
Linus Torvalds committed
251
252
			/* Traverse locks, blocks and shares of this file
			 * and update file->f_locks count */
253
			if (nlm_inspect_file(host, file, match))
254
				ret = 1;
Linus Torvalds's avatar
Linus Torvalds committed
255

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

/*
 * 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 */
Ingo Molnar's avatar
Ingo Molnar committed
287
	mutex_lock(&nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
288
289

	/* If there are no more locks etc, delete the file */
290
291
	if (--file->f_count == 0 && !nlm_file_inuse(file))
		nlm_delete_file(file);
Linus Torvalds's avatar
Linus Torvalds committed
292

Ingo Molnar's avatar
Ingo Molnar committed
293
	mutex_unlock(&nlm_file_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
294
295
}

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * 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
nlmsvc_mark_host(struct nlm_host *host, struct nlm_host *dummy)
{
	host->h_inuse = 1;
	return 0;
}

static int
nlmsvc_same_host(struct nlm_host *host, struct nlm_host *other)
{
	return host == other;
}

static int
nlmsvc_is_client(struct nlm_host *host, struct nlm_host *dummy)
{
327
328
329
330
331
332
333
334
335
336
	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;
337
338
}

Linus Torvalds's avatar
Linus Torvalds committed
339
340
341
342
343
344
345
/*
 * Mark all hosts that still hold resources
 */
void
nlmsvc_mark_resources(void)
{
	dprintk("lockd: nlmsvc_mark_resources\n");
346
	nlm_traverse_files(NULL, nlmsvc_mark_host);
Linus Torvalds's avatar
Linus Torvalds committed
347
348
349
350
351
352
353
354
355
356
}

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

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

/*
366
 * Remove all locks held for clients
Linus Torvalds's avatar
Linus Torvalds committed
367
368
369
370
 */
void
nlmsvc_invalidate_all(void)
{
371
372
373
374
375
376
377
	/* 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);
Linus Torvalds's avatar
Linus Torvalds committed
378
}