]> git.ipfire.org Git - people/ms/linux.git/blame - fs/lockd/svcsubs.c
[PATCH] knfsd: nfsd4: Fix error handling in nfsd's callback client
[people/ms/linux.git] / fs / lockd / svcsubs.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
9#include <linux/types.h>
10#include <linux/string.h>
11#include <linux/time.h>
12#include <linux/in.h>
353ab6e9 13#include <linux/mutex.h>
1da177e4
LT
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 */
07ba8063 28#define FILE_HASH_BITS 7
1da177e4 29#define FILE_NRHASH (1<<FILE_HASH_BITS)
07ba8063 30static struct hlist_head nlm_files[FILE_NRHASH];
353ab6e9 31static DEFINE_MUTEX(nlm_file_mutex);
1da177e4 32
0bbacc40
CL
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_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
1da177e4
LT
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/*
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 */
81u32
82nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
83 struct nfs_fh *f)
84{
07ba8063 85 struct hlist_node *pos;
1da177e4
LT
86 struct nlm_file *file;
87 unsigned int hash;
88 u32 nfserr;
1da177e4 89
0bbacc40 90 nlm_debug_print_fh("nlm_file_lookup", f);
1da177e4
LT
91
92 hash = file_hash(f);
93
94 /* Lock file table */
353ab6e9 95 mutex_lock(&nlm_file_mutex);
1da177e4 96
07ba8063 97 hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
1da177e4
LT
98 if (!nfs_compare_fh(&file->f_handle, f))
99 goto found;
100
0bbacc40 101 nlm_debug_print_fh("creating file for", f);
1da177e4
LT
102
103 nfserr = nlm_lck_denied_nolocks;
f8314dc6 104 file = kzalloc(sizeof(*file), GFP_KERNEL);
1da177e4
LT
105 if (!file)
106 goto out_unlock;
107
1da177e4 108 memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
89e63ef6 109 mutex_init(&file->f_mutex);
07ba8063 110 INIT_HLIST_NODE(&file->f_list);
68a2d76c 111 INIT_LIST_HEAD(&file->f_blocks);
1da177e4
LT
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) {
f0737a39 120 dprintk("lockd: open failed (error %d)\n", nfserr);
1da177e4
LT
121 goto out_free;
122 }
123
07ba8063 124 hlist_add_head(&file->f_list, &nlm_files[hash]);
1da177e4
LT
125
126found:
127 dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
128 *result = file;
129 file->f_count++;
130 nfserr = 0;
131
132out_unlock:
353ab6e9 133 mutex_unlock(&nlm_file_mutex);
1da177e4
LT
134 return nfserr;
135
136out_free:
137 kfree(file);
138#ifdef CONFIG_LOCKD_V4
139 if (nfserr == 1)
140 nfserr = nlm4_stale_fh;
141 else
142#endif
143 nfserr = nlm_lck_denied;
144 goto out_unlock;
145}
146
147/*
148 * Delete a file after having released all locks, blocks and shares
149 */
150static inline void
151nlm_delete_file(struct nlm_file *file)
152{
0bbacc40 153 nlm_debug_print_file("closing file", file);
07ba8063
OK
154 if (!hlist_unhashed(&file->f_list)) {
155 hlist_del(&file->f_list);
156 nlmsvc_ops->fclose(file->f_file);
157 kfree(file);
158 } else {
159 printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
1da177e4 160 }
1da177e4
LT
161}
162
163/*
164 * Loop over all locks on the given file and perform the specified
165 * action.
166 */
167static int
f2af793d
OK
168nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
169 nlm_host_match_fn_t match)
1da177e4
LT
170{
171 struct inode *inode = nlmsvc_file_inode(file);
172 struct file_lock *fl;
173 struct nlm_host *lockhost;
174
175again:
176 file->f_locks = 0;
177 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
7117bf3d 178 if (fl->fl_lmops != &nlmsvc_lock_operations)
1da177e4
LT
179 continue;
180
181 /* update current lock count */
182 file->f_locks++;
f2af793d 183
1da177e4 184 lockhost = (struct nlm_host *) fl->fl_owner;
f2af793d 185 if (match(lockhost, host)) {
1da177e4
LT
186 struct file_lock lock = *fl;
187
1da177e4
LT
188 lock.fl_type = F_UNLCK;
189 lock.fl_start = 0;
190 lock.fl_end = OFFSET_MAX;
191 if (posix_lock_file(file->f_file, &lock) < 0) {
192 printk("lockd: unlock failure in %s:%d\n",
193 __FILE__, __LINE__);
194 return 1;
195 }
196 goto again;
197 }
198 }
199
200 return 0;
201}
202
203/*
f2af793d
OK
204 * Inspect a single file
205 */
206static inline int
207nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
208{
209 nlmsvc_traverse_blocks(host, file, match);
210 nlmsvc_traverse_shares(host, file, match);
211 return nlm_traverse_locks(host, file, match);
212}
213
214/*
215 * Quick check whether there are still any locks, blocks or
216 * shares on a given file.
1da177e4
LT
217 */
218static inline int
f2af793d 219nlm_file_inuse(struct nlm_file *file)
1da177e4 220{
f2af793d
OK
221 struct inode *inode = nlmsvc_file_inode(file);
222 struct file_lock *fl;
223
224 if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
225 return 1;
226
227 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
228 if (fl->fl_lmops == &nlmsvc_lock_operations)
1da177e4 229 return 1;
1da177e4 230 }
f2af793d
OK
231 file->f_locks = 0;
232 return 0;
1da177e4
LT
233}
234
235/*
236 * Loop over all files in the file table.
237 */
238static int
f2af793d 239nlm_traverse_files(struct nlm_host *host, nlm_host_match_fn_t match)
1da177e4 240{
07ba8063
OK
241 struct hlist_node *pos, *next;
242 struct nlm_file *file;
01df9c5e 243 int i, ret = 0;
1da177e4 244
353ab6e9 245 mutex_lock(&nlm_file_mutex);
1da177e4 246 for (i = 0; i < FILE_NRHASH; i++) {
07ba8063 247 hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
01df9c5e
TM
248 file->f_count++;
249 mutex_unlock(&nlm_file_mutex);
250
1da177e4
LT
251 /* Traverse locks, blocks and shares of this file
252 * and update file->f_locks count */
f2af793d 253 if (nlm_inspect_file(host, file, match))
01df9c5e 254 ret = 1;
1da177e4 255
01df9c5e
TM
256 mutex_lock(&nlm_file_mutex);
257 file->f_count--;
1da177e4 258 /* No more references to this file. Let go of it. */
68a2d76c 259 if (list_empty(&file->f_blocks) && !file->f_locks
1da177e4 260 && !file->f_shares && !file->f_count) {
07ba8063 261 hlist_del(&file->f_list);
1da177e4
LT
262 nlmsvc_ops->fclose(file->f_file);
263 kfree(file);
1da177e4
LT
264 }
265 }
266 }
353ab6e9 267 mutex_unlock(&nlm_file_mutex);
01df9c5e 268 return ret;
1da177e4
LT
269}
270
271/*
272 * Release file. If there are no more remote locks on this file,
273 * close it and free the handle.
274 *
275 * Note that we can't do proper reference counting without major
276 * contortions because the code in fs/locks.c creates, deletes and
277 * splits locks without notification. Our only way is to walk the
278 * entire lock list each time we remove a lock.
279 */
280void
281nlm_release_file(struct nlm_file *file)
282{
283 dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
284 file, file->f_count);
285
286 /* Lock file table */
353ab6e9 287 mutex_lock(&nlm_file_mutex);
1da177e4
LT
288
289 /* If there are no more locks etc, delete the file */
f2af793d
OK
290 if (--file->f_count == 0 && !nlm_file_inuse(file))
291 nlm_delete_file(file);
1da177e4 292
353ab6e9 293 mutex_unlock(&nlm_file_mutex);
1da177e4
LT
294}
295
f2af793d
OK
296/*
297 * Helpers function for resource traversal
298 *
299 * nlmsvc_mark_host:
300 * used by the garbage collector; simply sets h_inuse.
301 * Always returns 0.
302 *
303 * nlmsvc_same_host:
304 * returns 1 iff the two hosts match. Used to release
305 * all resources bound to a specific host.
306 *
307 * nlmsvc_is_client:
308 * returns 1 iff the host is a client.
309 * Used by nlmsvc_invalidate_all
310 */
311static int
312nlmsvc_mark_host(struct nlm_host *host, struct nlm_host *dummy)
313{
314 host->h_inuse = 1;
315 return 0;
316}
317
318static int
319nlmsvc_same_host(struct nlm_host *host, struct nlm_host *other)
320{
321 return host == other;
322}
323
324static int
325nlmsvc_is_client(struct nlm_host *host, struct nlm_host *dummy)
326{
327 return host->h_server;
328}
329
1da177e4
LT
330/*
331 * Mark all hosts that still hold resources
332 */
333void
334nlmsvc_mark_resources(void)
335{
336 dprintk("lockd: nlmsvc_mark_resources\n");
f2af793d 337 nlm_traverse_files(NULL, nlmsvc_mark_host);
1da177e4
LT
338}
339
340/*
341 * Release all resources held by the given client
342 */
343void
344nlmsvc_free_host_resources(struct nlm_host *host)
345{
346 dprintk("lockd: nlmsvc_free_host_resources\n");
347
f2af793d 348 if (nlm_traverse_files(host, nlmsvc_same_host)) {
1da177e4 349 printk(KERN_WARNING
f0737a39 350 "lockd: couldn't remove all locks held by %s\n",
1da177e4 351 host->h_name);
f0737a39
OK
352 BUG();
353 }
1da177e4
LT
354}
355
356/*
350fce8d 357 * Remove all locks held for clients
1da177e4
LT
358 */
359void
360nlmsvc_invalidate_all(void)
361{
f2af793d
OK
362 /* Release all locks held by NFS clients.
363 * Previously, the code would call
364 * nlmsvc_free_host_resources for each client in
365 * turn, which is about as inefficient as it gets.
366 * Now we just do it once in nlm_traverse_files.
367 */
368 nlm_traverse_files(NULL, nlmsvc_is_client);
1da177e4 369}