xref: /linux/fs/lockd/svcsubs.c (revision eb3f4b7426cfd2b79d65b7d37155480b32259a11)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/fs/lockd/svcsubs.c
4  *
5  * Various support routines for the NLM server.
6  *
7  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/time.h>
13 #include <linux/in.h>
14 #include <linux/slab.h>
15 #include <linux/mutex.h>
16 #include <linux/sunrpc/svc.h>
17 #include <linux/sunrpc/addr.h>
18 #include <linux/module.h>
19 #include <linux/mount.h>
20 #include <uapi/linux/nfs2.h>
21 
22 #include "lockd.h"
23 #include "share.h"
24 
25 #define NLMDBG_FACILITY		NLMDBG_SVCSUBS
26 
27 
28 /*
29  * Global file hash table
30  */
31 #define FILE_HASH_BITS		7
32 #define FILE_NRHASH		(1<<FILE_HASH_BITS)
33 static struct hlist_head	nlm_files[FILE_NRHASH];
34 static DEFINE_MUTEX(nlm_file_mutex);
35 
36 #ifdef CONFIG_SUNRPC_DEBUG
nlm_debug_print_fh(char * msg,struct nfs_fh * f)37 static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
38 {
39 	u32 *fhp = (u32*)f->data;
40 
41 	/* print the first 32 bytes of the fh */
42 	dprintk("lockd: %s (%08x %08x %08x %08x %08x %08x %08x %08x)\n",
43 		msg, fhp[0], fhp[1], fhp[2], fhp[3],
44 		fhp[4], fhp[5], fhp[6], fhp[7]);
45 }
46 
nlm_debug_print_file(char * msg,struct nlm_file * file)47 static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
48 {
49 	struct inode *inode = nlmsvc_file_inode(file);
50 
51 	dprintk("lockd: %s %s/%llu\n",
52 		msg, inode->i_sb->s_id, inode->i_ino);
53 }
54 #else
nlm_debug_print_fh(char * msg,struct nfs_fh * f)55 static inline void nlm_debug_print_fh(char *msg, struct nfs_fh *f)
56 {
57 	return;
58 }
59 
nlm_debug_print_file(char * msg,struct nlm_file * file)60 static inline void nlm_debug_print_file(char *msg, struct nlm_file *file)
61 {
62 	return;
63 }
64 #endif
65 
file_hash(struct nfs_fh * f)66 static inline unsigned int file_hash(struct nfs_fh *f)
67 {
68 	unsigned int tmp=0;
69 	int i;
70 	for (i=0; i<NFS2_FHSIZE;i++)
71 		tmp += f->data[i];
72 	return tmp & (FILE_NRHASH - 1);
73 }
74 
lock_to_openmode(struct file_lock * lock)75 int lock_to_openmode(struct file_lock *lock)
76 {
77 	return lock_is_write(lock) ? O_WRONLY : O_RDONLY;
78 }
79 
80 /*
81  * Open the file. Note that if we're reexporting, for example,
82  * this could block the lockd thread for a while.
83  *
84  * We have to make sure we have the right credential to open
85  * the file.
86  *
87  * @mode is O_RDONLY, O_WRONLY, or O_RDWR. O_RDWR means success
88  * is achieved with EITHER O_RDONLY or O_WRONLY; it does not
89  * require both.
90  */
nlm_do_fopen(struct svc_rqst * rqstp,struct nlm_file * file,int mode)91 static __be32 nlm_do_fopen(struct svc_rqst *rqstp,
92 			   struct nlm_file *file, int mode)
93 {
94 	__be32 nlmerr = nlm__int__failed;
95 	__be32 deferred = 0;
96 	int error;
97 	int m;
98 
99 	for (m = O_RDONLY; m <= O_WRONLY; m++) {
100 		struct file **fp = &file->f_file[m];
101 
102 		if (mode != O_RDWR && mode != m)
103 			continue;
104 		if (*fp)
105 			return nlm_granted;
106 
107 		error = nlmsvc_ops->fopen(rqstp, &file->f_handle, fp, m);
108 		if (!error)
109 			return nlm_granted;
110 
111 		dprintk("lockd: open failed (errno %d)\n", error);
112 		switch (error) {
113 		case -EWOULDBLOCK:
114 			nlmerr = nlm__int__drop_reply;
115 			deferred = nlmerr;
116 			break;
117 		case -ESTALE:
118 			nlmerr = nlm__int__stale_fh;
119 			break;
120 		default:
121 			nlmerr = nlm__int__failed;
122 			break;
123 		}
124 	}
125 
126 	return deferred ? deferred : nlmerr;
127 }
128 
129 /*
130  * Lookup file info. If it doesn't exist, create a file info struct
131  * and open a (VFS) file for the given inode.
132  */
133 __be32
nlm_lookup_file(struct svc_rqst * rqstp,struct nlm_file ** result,struct nlm_lock * lock,int mode)134 nlm_lookup_file(struct svc_rqst *rqstp, struct nlm_file **result,
135 		struct nlm_lock *lock, int mode)
136 {
137 	struct nlm_file	*file;
138 	unsigned int	hash;
139 	__be32		nfserr;
140 
141 	nlm_debug_print_fh("nlm_lookup_file", &lock->fh);
142 
143 	hash = file_hash(&lock->fh);
144 
145 	/* Lock file table */
146 	mutex_lock(&nlm_file_mutex);
147 
148 	hlist_for_each_entry(file, &nlm_files[hash], f_list)
149 		if (!nfs_compare_fh(&file->f_handle, &lock->fh)) {
150 			mutex_lock(&file->f_mutex);
151 			nfserr = nlm_do_fopen(rqstp, file, mode);
152 			mutex_unlock(&file->f_mutex);
153 			goto found;
154 		}
155 	nlm_debug_print_fh("creating file for", &lock->fh);
156 
157 	nfserr = nlm_lck_denied_nolocks;
158 	file = kzalloc_obj(*file);
159 	if (!file)
160 		goto out_free;
161 
162 	memcpy(&file->f_handle, &lock->fh, sizeof(struct nfs_fh));
163 	mutex_init(&file->f_mutex);
164 	INIT_HLIST_NODE(&file->f_list);
165 	INIT_LIST_HEAD(&file->f_blocks);
166 
167 	nfserr = nlm_do_fopen(rqstp, file, mode);
168 	if (nfserr)
169 		goto out_unlock;
170 
171 	hlist_add_head(&file->f_list, &nlm_files[hash]);
172 
173 found:
174 	dprintk("lockd: found file %p (count %d)\n", file, file->f_count);
175 	*result = file;
176 	file->f_count++;
177 
178 out_unlock:
179 	mutex_unlock(&nlm_file_mutex);
180 	return nfserr;
181 
182 out_free:
183 	kfree(file);
184 	goto out_unlock;
185 }
186 
187 /*
188  * Delete a file after having released all locks, blocks and shares
189  */
190 static inline void
nlm_delete_file(struct nlm_file * file)191 nlm_delete_file(struct nlm_file *file)
192 {
193 	nlm_debug_print_file("closing file", file);
194 	if (!hlist_unhashed(&file->f_list)) {
195 		hlist_del(&file->f_list);
196 		if (file->f_file[O_RDONLY])
197 			nlmsvc_ops->fclose(file->f_file[O_RDONLY]);
198 		if (file->f_file[O_WRONLY])
199 			nlmsvc_ops->fclose(file->f_file[O_WRONLY]);
200 		kfree(file);
201 	} else {
202 		printk(KERN_WARNING "lockd: attempt to release unknown file!\n");
203 	}
204 }
205 
nlm_unlock_files(struct nlm_file * file,const struct file_lock * fl)206 static int nlm_unlock_files(struct nlm_file *file, const struct file_lock *fl)
207 {
208 	struct file_lock lock;
209 
210 	locks_init_lock(&lock);
211 	lock.c.flc_type  = F_UNLCK;
212 	lock.fl_start = 0;
213 	lock.fl_end   = OFFSET_MAX;
214 	lock.c.flc_owner = fl->c.flc_owner;
215 	lock.c.flc_pid   = fl->c.flc_pid;
216 	lock.c.flc_flags = FL_POSIX;
217 
218 	lock.c.flc_file = file->f_file[O_RDONLY];
219 	if (lock.c.flc_file && vfs_lock_file(lock.c.flc_file, F_SETLK, &lock, NULL))
220 		goto out_err;
221 	lock.c.flc_file = file->f_file[O_WRONLY];
222 	if (lock.c.flc_file && vfs_lock_file(lock.c.flc_file, F_SETLK, &lock, NULL))
223 		goto out_err;
224 	return 0;
225 out_err:
226 	pr_warn("lockd: unlock failure in %s:%d\n", __FILE__, __LINE__);
227 	return 1;
228 }
229 
230 /*
231  * Loop over all locks on the given file and perform the specified
232  * action.
233  */
234 static int
nlm_traverse_locks(struct nlm_host * host,struct nlm_file * file,nlm_host_match_fn_t match)235 nlm_traverse_locks(struct nlm_host *host, struct nlm_file *file,
236 			nlm_host_match_fn_t match)
237 {
238 	struct inode	 *inode = nlmsvc_file_inode(file);
239 	struct file_lock *fl;
240 	struct file_lock_context *flctx = locks_inode_context(inode);
241 	struct nlm_host	 *lockhost;
242 
243 	if (!flctx || list_empty_careful(&flctx->flc_posix))
244 		return 0;
245 again:
246 	file->f_locks = 0;
247 	spin_lock(&flctx->flc_lock);
248 	for_each_file_lock(fl, &flctx->flc_posix) {
249 		if (fl->fl_lmops != &nlmsvc_lock_operations)
250 			continue;
251 
252 		/* update current lock count */
253 		file->f_locks++;
254 
255 		lockhost = ((struct nlm_lockowner *) fl->c.flc_owner)->host;
256 		if (match(lockhost, host)) {
257 
258 			spin_unlock(&flctx->flc_lock);
259 			if (nlm_unlock_files(file, fl))
260 				return 1;
261 			goto again;
262 		}
263 	}
264 	spin_unlock(&flctx->flc_lock);
265 
266 	return 0;
267 }
268 
269 static int
nlmsvc_always_match(void * dummy1,struct nlm_host * dummy2)270 nlmsvc_always_match(void *dummy1, struct nlm_host *dummy2)
271 {
272 	return 1;
273 }
274 
275 /*
276  * Inspect a single file
277  */
278 static inline int
nlm_inspect_file(struct nlm_host * host,struct nlm_file * file,nlm_host_match_fn_t match)279 nlm_inspect_file(struct nlm_host *host, struct nlm_file *file, nlm_host_match_fn_t match)
280 {
281 	nlmsvc_traverse_blocks(host, file, match);
282 	nlmsvc_traverse_shares(host, file, match);
283 	return nlm_traverse_locks(host, file, match);
284 }
285 
286 /*
287  * Quick check whether there are still any locks, blocks or
288  * shares on a given file.
289  */
290 static inline int
nlm_file_inuse(struct nlm_file * file)291 nlm_file_inuse(struct nlm_file *file)
292 {
293 	struct inode	 *inode = nlmsvc_file_inode(file);
294 	struct file_lock *fl;
295 	struct file_lock_context *flctx = locks_inode_context(inode);
296 
297 	if (file->f_count || !list_empty(&file->f_blocks) || file->f_shares)
298 		return 1;
299 
300 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
301 		spin_lock(&flctx->flc_lock);
302 		for_each_file_lock(fl, &flctx->flc_posix) {
303 			if (fl->fl_lmops == &nlmsvc_lock_operations) {
304 				spin_unlock(&flctx->flc_lock);
305 				return 1;
306 			}
307 		}
308 		spin_unlock(&flctx->flc_lock);
309 	}
310 	file->f_locks = 0;
311 	return 0;
312 }
313 
nlm_close_files(struct nlm_file * file)314 static void nlm_close_files(struct nlm_file *file)
315 {
316 	if (file->f_file[O_RDONLY])
317 		nlmsvc_ops->fclose(file->f_file[O_RDONLY]);
318 	if (file->f_file[O_WRONLY])
319 		nlmsvc_ops->fclose(file->f_file[O_WRONLY]);
320 }
321 
322 /*
323  * Loop over all files in the file table.
324  */
325 static int
nlm_traverse_files(void * data,nlm_host_match_fn_t match,int (* is_failover_file)(void * data,struct nlm_file * file))326 nlm_traverse_files(void *data, nlm_host_match_fn_t match,
327 		int (*is_failover_file)(void *data, struct nlm_file *file))
328 {
329 	struct hlist_node *next;
330 	struct nlm_file	*file;
331 	int i, ret = 0;
332 
333 	mutex_lock(&nlm_file_mutex);
334 	for (i = 0; i < FILE_NRHASH; i++) {
335 		hlist_for_each_entry_safe(file, next, &nlm_files[i], f_list) {
336 			if (is_failover_file && !is_failover_file(data, file))
337 				continue;
338 			file->f_count++;
339 			mutex_unlock(&nlm_file_mutex);
340 
341 			/* Traverse locks, blocks and shares of this file
342 			 * and update file->f_locks count */
343 			if (nlm_inspect_file(data, file, match))
344 				ret = 1;
345 
346 			mutex_lock(&nlm_file_mutex);
347 			file->f_count--;
348 			/* No more references to this file. Let go of it. */
349 			if (list_empty(&file->f_blocks) && !file->f_locks
350 			 && !file->f_shares && !file->f_count) {
351 				hlist_del(&file->f_list);
352 				nlm_close_files(file);
353 				kfree(file);
354 			}
355 		}
356 	}
357 	mutex_unlock(&nlm_file_mutex);
358 	return ret;
359 }
360 
361 /*
362  * Release file. If there are no more remote locks on this file,
363  * close it and free the handle.
364  *
365  * Note that we can't do proper reference counting without major
366  * contortions because the code in fs/locks.c creates, deletes and
367  * splits locks without notification. Our only way is to walk the
368  * entire lock list each time we remove a lock.
369  */
370 void
nlm_release_file(struct nlm_file * file)371 nlm_release_file(struct nlm_file *file)
372 {
373 	dprintk("lockd: nlm_release_file(%p, ct = %d)\n",
374 				file, file->f_count);
375 
376 	/* Lock file table */
377 	mutex_lock(&nlm_file_mutex);
378 
379 	/* If there are no more locks etc, delete the file */
380 	if (--file->f_count == 0 && !nlm_file_inuse(file))
381 		nlm_delete_file(file);
382 
383 	mutex_unlock(&nlm_file_mutex);
384 }
385 
386 /*
387  * Helpers function for resource traversal
388  *
389  * nlmsvc_mark_host:
390  *	used by the garbage collector; simply sets h_inuse only for those
391  *	hosts, which passed network check.
392  *	Always returns 0.
393  *
394  * nlmsvc_same_host:
395  *	returns 1 iff the two hosts match. Used to release
396  *	all resources bound to a specific host.
397  *
398  * nlmsvc_is_client:
399  *	returns 1 iff the host is a client.
400  *	Used by nlmsvc_invalidate_all
401  */
402 
403 static int
nlmsvc_mark_host(void * data,struct nlm_host * hint)404 nlmsvc_mark_host(void *data, struct nlm_host *hint)
405 {
406 	struct nlm_host *host = data;
407 
408 	if ((hint->net == NULL) ||
409 	    (host->net == hint->net))
410 		host->h_inuse = 1;
411 	return 0;
412 }
413 
414 static int
nlmsvc_same_host(void * data,struct nlm_host * other)415 nlmsvc_same_host(void *data, struct nlm_host *other)
416 {
417 	struct nlm_host *host = data;
418 
419 	return host == other;
420 }
421 
422 static int
nlmsvc_is_client(void * data,struct nlm_host * dummy)423 nlmsvc_is_client(void *data, struct nlm_host *dummy)
424 {
425 	struct nlm_host *host = data;
426 
427 	if (host->h_server) {
428 		/* we are destroying locks even though the client
429 		 * hasn't asked us too, so don't unmonitor the
430 		 * client
431 		 */
432 		if (host->h_nsmhandle)
433 			host->h_nsmhandle->sm_sticky = 1;
434 		return 1;
435 	} else
436 		return 0;
437 }
438 
439 /*
440  * Mark all hosts that still hold resources
441  */
442 void
nlmsvc_mark_resources(struct net * net)443 nlmsvc_mark_resources(struct net *net)
444 {
445 	struct nlm_host hint;
446 
447 	dprintk("lockd: %s for net %x\n", __func__, net ? net->ns.inum : 0);
448 	hint.net = net;
449 	nlm_traverse_files(&hint, nlmsvc_mark_host, NULL);
450 }
451 
452 /*
453  * Release all resources held by the given client
454  */
455 void
nlmsvc_free_host_resources(struct nlm_host * host)456 nlmsvc_free_host_resources(struct nlm_host *host)
457 {
458 	dprintk("lockd: nlmsvc_free_host_resources\n");
459 
460 	if (nlm_traverse_files(host, nlmsvc_same_host, NULL)) {
461 		printk(KERN_WARNING
462 			"lockd: couldn't remove all locks held by %s\n",
463 			host->h_name);
464 		BUG();
465 	}
466 }
467 
468 /**
469  * nlmsvc_invalidate_all - remove all locks held for clients
470  *
471  * Release all locks held by NFS clients.
472  *
473  */
474 void
nlmsvc_invalidate_all(void)475 nlmsvc_invalidate_all(void)
476 {
477 	/*
478 	 * Previously, the code would call
479 	 * nlmsvc_free_host_resources for each client in
480 	 * turn, which is about as inefficient as it gets.
481 	 * Now we just do it once in nlm_traverse_files.
482 	 */
483 	nlm_traverse_files(NULL, nlmsvc_is_client, NULL);
484 }
485 
486 
487 static int
nlmsvc_match_sb(void * datap,struct nlm_file * file)488 nlmsvc_match_sb(void *datap, struct nlm_file *file)
489 {
490 	struct super_block *sb = datap;
491 
492 	return sb == nlmsvc_file_inode(file)->i_sb;
493 }
494 
495 /**
496  * nlmsvc_unlock_all_by_sb - release locks held on this file system
497  * @sb: super block
498  *
499  * Release all locks held by clients accessing this file system.
500  */
501 int
nlmsvc_unlock_all_by_sb(struct super_block * sb)502 nlmsvc_unlock_all_by_sb(struct super_block *sb)
503 {
504 	int ret;
505 
506 	ret = nlm_traverse_files(sb, nlmsvc_always_match, nlmsvc_match_sb);
507 	return ret ? -EIO : 0;
508 }
509 EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_sb);
510 
511 static int
nlmsvc_match_ip(void * datap,struct nlm_host * host)512 nlmsvc_match_ip(void *datap, struct nlm_host *host)
513 {
514 	return rpc_cmp_addr(nlm_srcaddr(host), datap);
515 }
516 
517 /**
518  * nlmsvc_unlock_all_by_ip - release local locks by IP address
519  * @server_addr: server's IP address as seen by clients
520  *
521  * Release all locks held by clients accessing this host
522  * via the passed in IP address.
523  */
524 int
nlmsvc_unlock_all_by_ip(struct sockaddr * server_addr)525 nlmsvc_unlock_all_by_ip(struct sockaddr *server_addr)
526 {
527 	int ret;
528 
529 	ret = nlm_traverse_files(server_addr, nlmsvc_match_ip, NULL);
530 	return ret ? -EIO : 0;
531 }
532 EXPORT_SYMBOL_GPL(nlmsvc_unlock_all_by_ip);
533