blob: 91731353dfa435c0621d2cabbeb8d9aed7f35f5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/string.h>
11#include <linux/time.h>
12#include <linux/in.h>
Ingo Molnar353ab6e2006-03-26 01:37:12 -080013#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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 */
Olaf Kirch07ba8062006-10-04 02:15:58 -070028#define FILE_HASH_BITS 7
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define FILE_NRHASH (1<<FILE_HASH_BITS)
Olaf Kirch07ba8062006-10-04 02:15:58 -070030static struct hlist_head nlm_files[FILE_NRHASH];
Ingo Molnar353ab6e2006-03-26 01:37:12 -080031static DEFINE_MUTEX(nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Chuck Lever0bbacc42005-11-01 16:53:32 -050033#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static 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{
Olaf Kirch07ba8062006-10-04 02:15:58 -070085 struct hlist_node *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct nlm_file *file;
87 unsigned int hash;
88 u32 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Chuck Lever0bbacc42005-11-01 16:53:32 -050090 nlm_debug_print_fh("nlm_file_lookup", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 hash = file_hash(f);
93
94 /* Lock file table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -080095 mutex_lock(&nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Olaf Kirch07ba8062006-10-04 02:15:58 -070097 hlist_for_each_entry(file, pos, &nlm_files[hash], f_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!nfs_compare_fh(&file->f_handle, f))
99 goto found;
100
Chuck Lever0bbacc42005-11-01 16:53:32 -0500101 nlm_debug_print_fh("creating file for", f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 nfserr = nlm_lck_denied_nolocks;
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700104 file = kzalloc(sizeof(*file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (!file)
106 goto out_unlock;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 memcpy(&file->f_handle, f, sizeof(struct nfs_fh));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 init_MUTEX(&file->f_sema);
Olaf Kirch07ba8062006-10-04 02:15:58 -0700110 INIT_HLIST_NODE(&file->f_list);
Olaf Kirch68a2d762006-10-04 02:15:57 -0700111 INIT_LIST_HEAD(&file->f_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
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) {
Olaf Kirchf0737a32006-10-04 02:15:54 -0700120 dprintk("lockd: open failed (error %d)\n", nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto out_free;
122 }
123
Olaf Kirch07ba8062006-10-04 02:15:58 -0700124 hlist_add_head(&file->f_list, &nlm_files[hash]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
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:
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800133 mutex_unlock(&nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 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{
Chuck Lever0bbacc42005-11-01 16:53:32 -0500153 nlm_debug_print_file("closing file", file);
Olaf Kirch07ba8062006-10-04 02:15:58 -0700154 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/*
164 * Loop over all locks on the given file and perform the specified
165 * action.
166 */
167static int
168nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file, int action)
169{
170 struct inode *inode = nlmsvc_file_inode(file);
171 struct file_lock *fl;
172 struct nlm_host *lockhost;
173
174again:
175 file->f_locks = 0;
176 for (fl = inode->i_flock; fl; fl = fl->fl_next) {
J. Bruce Fields7117bf32006-03-20 13:44:26 -0500177 if (fl->fl_lmops != &nlmsvc_lock_operations)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 continue;
179
180 /* update current lock count */
181 file->f_locks++;
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)
186 return 1;
187 else if (action == NLM_ACT_UNLOCK) {
188 struct file_lock lock = *fl;
189
190 if (host && lockhost != host)
191 continue;
192
193 lock.fl_type = F_UNLCK;
194 lock.fl_start = 0;
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",
198 __FILE__, __LINE__);
199 return 1;
200 }
201 goto again;
202 }
203 }
204
205 return 0;
206}
207
208/*
209 * Operate on a single file
210 */
211static inline int
212nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, int action)
213{
214 if (action == NLM_ACT_CHECK) {
215 /* Fast path for mark and sweep garbage collection */
Olaf Kirch68a2d762006-10-04 02:15:57 -0700216 if (file->f_count || list_empty(&file->f_blocks) || file->f_shares)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 1;
218 } else {
J. Bruce Fieldsf3ee4392006-03-20 23:24:13 -0500219 nlmsvc_traverse_blocks(host, file, action);
J. Bruce Fields5f121912006-03-20 23:24:25 -0500220 nlmsvc_traverse_shares(host, file, action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 return nlm_traverse_locks(host, file, action);
223}
224
225/*
226 * Loop over all files in the file table.
227 */
228static int
229nlm_traverse_files(struct nlm_host *host, int action)
230{
Olaf Kirch07ba8062006-10-04 02:15:58 -0700231 struct hlist_node *pos, *next;
232 struct nlm_file *file;
Trond Myklebust01df9c52006-08-10 11:58:57 -0400233 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800235 mutex_lock(&nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 for (i = 0; i < FILE_NRHASH; i++) {
Olaf Kirch07ba8062006-10-04 02:15:58 -0700237 hlist_for_each_entry_safe(file, pos, next, &nlm_files[i], f_list) {
Trond Myklebust01df9c52006-08-10 11:58:57 -0400238 file->f_count++;
239 mutex_unlock(&nlm_file_mutex);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Traverse locks, blocks and shares of this file
242 * and update file->f_locks count */
Trond Myklebust01df9c52006-08-10 11:58:57 -0400243 if (nlm_inspect_file(host, file, action))
244 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Trond Myklebust01df9c52006-08-10 11:58:57 -0400246 mutex_lock(&nlm_file_mutex);
247 file->f_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* No more references to this file. Let go of it. */
Olaf Kirch68a2d762006-10-04 02:15:57 -0700249 if (list_empty(&file->f_blocks) && !file->f_locks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 && !file->f_shares && !file->f_count) {
Olaf Kirch07ba8062006-10-04 02:15:58 -0700251 hlist_del(&file->f_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 nlmsvc_ops->fclose(file->f_file);
253 kfree(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
256 }
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800257 mutex_unlock(&nlm_file_mutex);
Trond Myklebust01df9c52006-08-10 11:58:57 -0400258 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * Release file. If there are no more remote locks on this file,
263 * close it and free the handle.
264 *
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.
269 */
270void
271nlm_release_file(struct nlm_file *file)
272{
273 dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
274 file, file->f_count);
275
276 /* Lock file table */
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800277 mutex_lock(&nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
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);
283 }
284
Ingo Molnar353ab6e2006-03-26 01:37:12 -0800285 mutex_unlock(&nlm_file_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * Mark all hosts that still hold resources
290 */
291void
292nlmsvc_mark_resources(void)
293{
294 dprintk("lockd: nlmsvc_mark_resources\n");
295
296 nlm_traverse_files(NULL, NLM_ACT_MARK);
297}
298
299/*
300 * Release all resources held by the given client
301 */
302void
303nlmsvc_free_host_resources(struct nlm_host *host)
304{
305 dprintk("lockd: nlmsvc_free_host_resources\n");
306
Olaf Kirchf0737a32006-10-04 02:15:54 -0700307 if (nlm_traverse_files(host, NLM_ACT_UNLOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 printk(KERN_WARNING
Olaf Kirchf0737a32006-10-04 02:15:54 -0700309 "lockd: couldn't remove all locks held by %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 host->h_name);
Olaf Kirchf0737a32006-10-04 02:15:54 -0700311 BUG();
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * delete all hosts structs for clients
317 */
318void
319nlmsvc_invalidate_all(void)
320{
321 struct nlm_host *host;
322 while ((host = nlm_find_client()) != NULL) {
323 nlmsvc_free_host_resources(host);
324 host->h_expires = 0;
325 host->h_killed = 1;
326 nlm_release_host(host);
327 }
328}