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