2 * linux/fs/lockd/svcsubs.c
4 * Various support routines for the NLM server.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/time.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>
22 #define NLMDBG_FACILITY NLMDBG_SVCSUBS
26 * Global file hash table
28 #define FILE_HASH_BITS 5
29 #define FILE_NRHASH (1<<FILE_HASH_BITS)
30 static struct nlm_file * nlm_files[FILE_NRHASH];
31 static DECLARE_MUTEX(nlm_file_sema);
33 static inline unsigned int file_hash(struct nfs_fh *f)
37 for (i=0; i<NFS2_FHSIZE;i++)
39 return tmp & (FILE_NRHASH - 1);
43 * Lookup file info. If it doesn't exist, create a file info struct
44 * and open a (VFS) file for the given inode.
47 * Note that we open the file O_RDONLY even when creating write locks.
48 * This is not quite right, but for now, we assume the client performs
49 * the proper R/W checking.
52 nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
55 struct nlm_file *file;
58 u32 *fhp = (u32*)f->data;
60 dprintk("lockd: nlm_file_lookup(%08x %08x %08x %08x %08x %08x)\n",
61 fhp[0], fhp[1], fhp[2], fhp[3], fhp[4], fhp[5]);
69 for (file = nlm_files[hash]; file; file = file->f_next)
70 if (!nfs_compare_fh(&file->f_handle, f))
73 dprintk("lockd: creating file for (%08x %08x %08x %08x %08x %08x)\n",
74 fhp[0], fhp[1], fhp[2], fhp[3], fhp[4], fhp[5]);
76 nfserr = nlm_lck_denied_nolocks;
77 file = (struct nlm_file *) kmalloc(sizeof(*file), GFP_KERNEL);
81 memset(file, 0, sizeof(*file));
82 memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
84 init_MUTEX(&file->f_sema);
86 /* Open the file. Note that this must not sleep for too long, else
87 * we would lock up lockd:-) So no NFS re-exports, folks.
89 * We have to make sure we have the right credential to open
92 if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) {
93 dprintk("lockd: open failed (nfserr %d)\n", nfserr);
97 file->f_next = nlm_files[hash];
98 nlm_files[hash] = file;
101 dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
112 #ifdef CONFIG_LOCKD_V4
114 nfserr = nlm4_stale_fh;
118 nfserr = nlm_lck_dropit;
120 nfserr = nlm_lck_denied;
125 * Delete a file after having released all locks, blocks and shares
128 nlm_delete_file(struct nlm_file *file)
130 struct inode *inode = file->f_file->f_dentry->d_inode;
131 struct nlm_file **fp, *f;
133 dprintk("lockd: closing file %s/%ld\n",
134 inode->i_sb->s_id, inode->i_ino);
135 fp = nlm_files + file->f_hash;
136 while ((f = *fp) != NULL) {
139 nlmsvc_ops->fclose(file->f_file);
146 printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
150 * Loop over all locks on the given file and perform the specified
154 nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, int action)
156 struct inode *inode = nlmsvc_file_inode(file);
157 struct file_lock *fl;
158 struct nlm_host *lockhost;
162 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
163 if (!(fl->fl_flags & FL_LOCKD))
166 /* update current lock count */
168 lockhost = (struct nlm_host *) fl->fl_owner;
169 if (action == NLM_ACT_MARK)
170 lockhost->h_inuse = 1;
171 else if (action == NLM_ACT_CHECK)
173 else if (action == NLM_ACT_UNLOCK) {
174 struct file_lock lock = *fl;
176 if (host && lockhost != host)
179 lock.fl_type = F_UNLCK;
181 lock.fl_end = OFFSET_MAX;
182 if (posix_lock_file(file->f_file, &lock) < 0) {
183 printk("lockd: unlock failure in %s:%d\n",
195 * Operate on a single file
198 nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, int action)
200 if (action == NLM_ACT_CHECK) {
201 /* Fast path for mark and sweep garbage collection */
202 if (file->f_count || file->f_blocks || file->f_shares)
205 if (nlmsvc_traverse_blocks(host, file, action)
206 || nlmsvc_traverse_shares(host, file, action))
209 return nlm_traverse_locks(host, file, action);
213 * Loop over all files in the file table.
216 nlm_traverse_files(struct nlm_host *host, int action)
218 struct nlm_file *file, **fp;
221 down(&nlm_file_sema);
222 for (i = 0; i < FILE_NRHASH; i++) {
224 while ((file = *fp) != NULL) {
225 /* Traverse locks, blocks and shares of this file
226 * and update file->f_locks count */
227 if (nlm_inspect_file(host, file, action)) {
232 /* No more references to this file. Let go of it. */
233 if (!file->f_blocks && !file->f_locks
234 && !file->f_shares && !file->f_count) {
236 nlmsvc_ops->fclose(file->f_file);
248 * Release file. If there are no more remote locks on this file,
249 * close it and free the handle.
251 * Note that we can't do proper reference counting without major
252 * contortions because the code in fs/locks.c creates, deletes and
253 * splits locks without notification. Our only way is to walk the
254 * entire lock list each time we remove a lock.
257 nlm_release_file(struct nlm_file *file)
259 dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
260 file, file->f_count);
262 /* Lock file table */
263 down(&nlm_file_sema);
265 /* If there are no more locks etc, delete the file */
266 if(--file->f_count == 0) {
267 if(!nlm_inspect_file(NULL, file, NLM_ACT_CHECK))
268 nlm_delete_file(file);
275 * Mark all hosts that still hold resources
278 nlmsvc_mark_resources(void)
280 dprintk("lockd: nlmsvc_mark_resources\n");
282 nlm_traverse_files(NULL, NLM_ACT_MARK);
286 * Release all resources held by the given client
289 nlmsvc_free_host_resources(struct nlm_host *host)
291 dprintk("lockd: nlmsvc_free_host_resources\n");
293 if (nlm_traverse_files(host, NLM_ACT_UNLOCK))
295 "lockd: couldn't remove all locks held by %s",
300 * delete all hosts structs for clients
303 nlmsvc_invalidate_all(void)
305 struct nlm_host *host;
306 while ((host = nlm_find_client()) != NULL) {
307 nlmsvc_free_host_resources(host);
310 nlm_release_host(host);