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/types.h>
10 #include <linux/string.h>
11 #include <linux/time.h>
13 #include <linux/mutex.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 7
29 #define FILE_NRHASH (1<<FILE_HASH_BITS)
30 static struct hlist_head nlm_files[FILE_NRHASH];
31 static DEFINE_MUTEX(nlm_file_mutex);
34 static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
36 u32 *fhp = (u32*)f->data;
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]);
44 static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
46 struct inode *inode = file->f_file->f_dentry->d_inode;
48 dprintk("lockd: %s %s/%ld\n",
49 msg, inode->i_sb->s_id, inode->i_ino);
52 static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
57 static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
63 static inline unsigned int file_hash(struct nfs_fh *f)
67 for (i=0; i<NFS2_FHSIZE;i++)
69 return tmp & (FILE_NRHASH - 1);
73 * Lookup file info. If it doesn't exist, create a file info struct
74 * and open a (VFS) file for the given inode.
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.
82 nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
85 struct hlist_node *pos;
86 struct nlm_file *file;
90 nlm_debug_print_fh("nlm_file_lookup", f);
95 mutex_lock(&nlm_file_mutex);
97 hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
98 if (!nfs_compare_fh(&file->f_handle, f))
101 nlm_debug_print_fh("creating file for", f);
103 nfserr = nlm_lck_denied_nolocks;
104 file = kzalloc(sizeof(*file), GFP_KERNEL);
108 memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
109 init_MUTEX(&file->f_sema);
110 INIT_HLIST_NODE(&file->f_list);
111 INIT_LIST_HEAD(&file->f_blocks);
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.
116 * We have to make sure we have the right credential to open
119 if ((nfserr = nlmsvc_ops->fopen(rqstp, f, &file->f_file)) != 0) {
120 dprintk("lockd: open failed (error %d)\n", nfserr);
124 hlist_add_head(&file->f_list, &nlm_files[hash]);
127 dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
133 mutex_unlock(&nlm_file_mutex);
138 #ifdef CONFIG_LOCKD_V4
140 nfserr = nlm4_stale_fh;
143 nfserr = nlm_lck_denied;
148 * Delete a file after having released all locks, blocks and shares
151 nlm_delete_file(struct nlm_file *file)
153 nlm_debug_print_file("closing file", file);
154 if (!hlist_unhashed(&file->f_list)) {
155 hlist_del(&file->f_list);
156 nlmsvc_ops->fclose(file->f_file);
159 printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
164 * Loop over all locks on the given file and perform the specified
168 nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, int action)
170 struct inode *inode = nlmsvc_file_inode(file);
171 struct file_lock *fl;
172 struct nlm_host *lockhost;
176 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
177 if (fl->fl_lmops != &nlmsvc_lock_operations)
180 /* update current lock count */
182 lockhost = (struct nlm_host *) fl->fl_owner;
183 if (action == NLM_ACT_MARK)
184 lockhost->h_inuse = 1;
185 else if (action == NLM_ACT_CHECK)
187 else if (action == NLM_ACT_UNLOCK) {
188 struct file_lock lock = *fl;
190 if (host && lockhost != host)
193 lock.fl_type = F_UNLCK;
195 lock.fl_end = OFFSET_MAX;
196 if (posix_lock_file(file->f_file, &lock) < 0) {
197 printk("lockd: unlock failure in %s:%d\n",
209 * Operate on a single file
212 nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, int action)
214 if (action == NLM_ACT_CHECK) {
215 /* Fast path for mark and sweep garbage collection */
216 if (file->f_count || list_empty(&file->f_blocks) || file->f_shares)
219 nlmsvc_traverse_blocks(host, file, action);
220 nlmsvc_traverse_shares(host, file, action);
222 return nlm_traverse_locks(host, file, action);
226 * Loop over all files in the file table.
229 nlm_traverse_files(struct nlm_host *host, int action)
231 struct hlist_node *pos, *next;
232 struct nlm_file *file;
235 mutex_lock(&nlm_file_mutex);
236 for (i = 0; i < FILE_NRHASH; i++) {
237 hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
239 mutex_unlock(&nlm_file_mutex);
241 /* Traverse locks, blocks and shares of this file
242 * and update file->f_locks count */
243 if (nlm_inspect_file(host, file, action))
246 mutex_lock(&nlm_file_mutex);
248 /* No more references to this file. Let go of it. */
249 if (list_empty(&file->f_blocks) && !file->f_locks
250 && !file->f_shares && !file->f_count) {
251 hlist_del(&file->f_list);
252 nlmsvc_ops->fclose(file->f_file);
257 mutex_unlock(&nlm_file_mutex);
262 * Release file. If there are no more remote locks on this file,
263 * close it and free the handle.
265 * Note that we can't do proper reference counting without major
266 * contortions because the code in fs/locks.c creates, deletes and
267 * splits locks without notification. Our only way is to walk the
268 * entire lock list each time we remove a lock.
271 nlm_release_file(struct nlm_file *file)
273 dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
274 file, file->f_count);
276 /* Lock file table */
277 mutex_lock(&nlm_file_mutex);
279 /* If there are no more locks etc, delete the file */
280 if(--file->f_count == 0) {
281 if(!nlm_inspect_file(NULL, file, NLM_ACT_CHECK))
282 nlm_delete_file(file);
285 mutex_unlock(&nlm_file_mutex);
289 * Mark all hosts that still hold resources
292 nlmsvc_mark_resources(void)
294 dprintk("lockd: nlmsvc_mark_resources\n");
296 nlm_traverse_files(NULL, NLM_ACT_MARK);
300 * Release all resources held by the given client
303 nlmsvc_free_host_resources(struct nlm_host *host)
305 dprintk("lockd: nlmsvc_free_host_resources\n");
307 if (nlm_traverse_files(host, NLM_ACT_UNLOCK)) {
309 "lockd: couldn't remove all locks held by %s\n",
316 * delete all hosts structs for clients
319 nlmsvc_invalidate_all(void)
321 struct nlm_host *host;
322 while ((host = nlm_find_client()) != NULL) {
323 nlmsvc_free_host_resources(host);
326 nlm_release_host(host);