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