This should hopefully fix the warnings reported.
[linux-2.6/linux-mips.git] / fs / lockd / svcsubs.c
blobb3489a88dfb241560e88148d87aeee7ae09f1ed9
1 /*
2 * linux/fs/lockd/svcsubs.c
4 * Various support routines for the NLM server.
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/sched.h>
13 #include <linux/in.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_NRHASH 32
29 #define FILE_HASH_BITS 5
30 static struct nlm_file * nlm_files[FILE_NRHASH];
31 static DECLARE_MUTEX(nlm_file_sema);
33 static unsigned int file_hash(dev_t dev, ino_t ino)
35 unsigned long tmp = (unsigned long) ino | (unsigned long) dev;
36 tmp = tmp + (tmp >> FILE_HASH_BITS) + (tmp >> FILE_HASH_BITS*2);
37 return tmp & (FILE_NRHASH - 1);
41 * Lookup file info. If it doesn't exist, create a file info struct
42 * and open a (VFS) file for the given inode.
44 * FIXME:
45 * Note that we open the file O_RDONLY even when creating write locks.
46 * This is not quite right, but for now, we assume the client performs
47 * the proper R/W checking.
49 u32
50 nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
51 struct nfs_fh *f)
53 struct knfs_fh *fh = (struct knfs_fh *) f;
54 struct nlm_file *file;
55 unsigned int hash;
56 u32 nfserr;
58 dprintk("lockd: nlm_file_lookup(%s/%u)\n",
59 kdevname(u32_to_kdev_t(fh->fh_dev)), fh->fh_ino);
61 hash = file_hash(u32_to_kdev_t(fh->fh_dev), u32_to_ino_t(fh->fh_ino));
63 /* Lock file table */
64 down(&nlm_file_sema);
66 for (file = nlm_files[hash]; file; file = file->f_next) {
67 if (file->f_handle.fh_dcookie == fh->fh_dcookie &&
68 !memcmp(&file->f_handle, fh, sizeof(*fh)))
69 goto found;
72 dprintk("lockd: creating file for %s/%u\n",
73 kdevname(u32_to_kdev_t(fh->fh_dev)), fh->fh_ino);
74 nfserr = nlm_lck_denied_nolocks;
75 file = (struct nlm_file *) kmalloc(sizeof(*file), GFP_KERNEL);
76 if (!file)
77 goto out_unlock;
79 memset(file, 0, sizeof(*file));
80 file->f_handle = *fh;
81 init_MUTEX(&file->f_sema);
83 /* Open the file. Note that this must not sleep for too long, else
84 * we would lock up lockd:-) So no NFS re-exports, folks.
86 * We have to make sure we have the right credential to open
87 * the file.
89 if ((nfserr = nlmsvc_ops->fopen(rqstp, fh, &file->f_file)) != 0) {
90 dprintk("lockd: open failed (nfserr %d)\n", ntohl(nfserr));
91 goto out_free;
94 file->f_next = nlm_files[hash];
95 nlm_files[hash] = file;
97 found:
98 dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
99 *result = file;
100 file->f_count++;
101 nfserr = 0;
103 out_unlock:
104 up(&nlm_file_sema);
105 return nfserr;
107 out_free:
108 kfree(file);
109 #ifdef CONFIG_LOCKD_V4
110 if (nfserr == 1)
111 nfserr = nlm4_stale_fh;
112 else
113 #endif
114 nfserr = nlm_lck_denied;
115 goto out_unlock;
119 * Delete a file after having released all locks, blocks and shares
121 static inline void
122 nlm_delete_file(struct nlm_file *file)
124 struct inode *inode = file->f_file.f_dentry->d_inode;
125 struct nlm_file **fp, *f;
127 dprintk("lockd: closing file %s/%ld\n",
128 kdevname(inode->i_dev), inode->i_ino);
129 fp = nlm_files + file_hash(inode->i_dev, inode->i_ino);
130 while ((f = *fp) != NULL) {
131 if (f == file) {
132 *fp = file->f_next;
133 nlmsvc_ops->fclose(&file->f_file);
134 kfree(file);
135 return;
137 fp = &f->f_next;
140 printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
144 * Loop over all locks on the given file and perform the specified
145 * action.
147 static int
148 nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, int action)
150 struct inode *inode = nlmsvc_file_inode(file);
151 struct file_lock *fl;
152 struct nlm_host *lockhost;
154 again:
155 file->f_locks = 0;
156 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
157 if (!(fl->fl_flags & FL_LOCKD))
158 continue;
160 /* update current lock count */
161 file->f_locks++;
162 lockhost = (struct nlm_host *) fl->fl_owner;
163 if (action == NLM_ACT_MARK)
164 lockhost->h_inuse = 1;
165 else if (action == NLM_ACT_CHECK)
166 return 1;
167 else if (action == NLM_ACT_UNLOCK) {
168 struct file_lock lock = *fl;
170 if (host && lockhost != host)
171 continue;
173 lock.fl_type = F_UNLCK;
174 lock.fl_start = 0;
175 lock.fl_end = NLM_OFFSET_MAX;
176 if (posix_lock_file(&file->f_file, &lock, 0) < 0) {
177 printk("lockd: unlock failure in %s:%d\n",
178 __FILE__, __LINE__);
179 return 1;
181 goto again;
185 return 0;
189 * Operate on a single file
191 static inline int
192 nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, int action)
194 if (action == NLM_ACT_CHECK) {
195 /* Fast path for mark and sweep garbage collection */
196 if (file->f_count || file->f_blocks || file->f_shares)
197 return 1;
198 } else {
199 if (nlmsvc_traverse_blocks(host, file, action)
200 || nlmsvc_traverse_shares(host, file, action))
201 return 1;
203 return nlm_traverse_locks(host, file, action);
207 * Loop over all files in the file table.
209 static int
210 nlm_traverse_files(struct nlm_host *host, int action)
212 struct nlm_file *file, **fp;
213 int i;
215 down(&nlm_file_sema);
216 for (i = 0; i < FILE_NRHASH; i++) {
217 fp = nlm_files + i;
218 while ((file = *fp) != NULL) {
219 /* Traverse locks, blocks and shares of this file
220 * and update file->f_locks count */
221 if (nlm_inspect_file(host, file, action)) {
222 up(&nlm_file_sema);
223 return 1;
226 /* No more references to this file. Let go of it. */
227 if (!file->f_blocks && !file->f_locks
228 && !file->f_shares && !file->f_count) {
229 *fp = file->f_next;
230 nlmsvc_ops->fclose(&file->f_file);
231 kfree(file);
232 } else {
233 fp = &file->f_next;
237 up(&nlm_file_sema);
238 return 0;
242 * Release file. If there are no more remote locks on this file,
243 * close it and free the handle.
245 * Note that we can't do proper reference counting without major
246 * contortions because the code in fs/locks.c creates, deletes and
247 * splits locks without notification. Our only way is to walk the
248 * entire lock list each time we remove a lock.
250 void
251 nlm_release_file(struct nlm_file *file)
253 dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
254 file, file->f_count);
256 /* Lock file table */
257 down(&nlm_file_sema);
259 /* If there are no more locks etc, delete the file */
260 if(--file->f_count == 0) {
261 if(!nlm_inspect_file(NULL, file, NLM_ACT_CHECK))
262 nlm_delete_file(file);
265 up(&nlm_file_sema);
269 * Mark all hosts that still hold resources
271 void
272 nlmsvc_mark_resources(void)
274 dprintk("lockd: nlmsvc_mark_resources\n");
276 nlm_traverse_files(NULL, NLM_ACT_MARK);
280 * Release all resources held by the given client
282 void
283 nlmsvc_free_host_resources(struct nlm_host *host)
285 dprintk("lockd: nlmsvc_free_host_resources\n");
287 if (nlm_traverse_files(host, NLM_ACT_UNLOCK))
288 printk(KERN_WARNING
289 "lockd: couldn't remove all locks held by %s",
290 host->h_name);
294 * Delete a client when the nfsd entry is removed.
296 void
297 nlmsvc_invalidate_client(struct svc_client *clnt)
299 struct nlm_host *host;
301 if ((host = nlm_lookup_host(clnt, NULL, 0, 0)) != NULL) {
302 dprintk("lockd: invalidating client for %s\n", host->h_name);
303 nlmsvc_free_host_resources(host);
304 host->h_expires = 0;
305 nlm_release_host(host);