Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * Copyright (C) 2005-2008 Red Hat, Inc.  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/dlm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/dlm_plock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "dlm_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "lockspace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) static spinlock_t ops_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static struct list_head send_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static struct list_head recv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static wait_queue_head_t send_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static wait_queue_head_t recv_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct plock_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	int done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct dlm_plock_info info;
^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) struct plock_xop {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct plock_op xop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int (*callback)(struct file_lock *fl, int result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	void *fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	void *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct file_lock flc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static inline void set_version(struct dlm_plock_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	info->version[0] = DLM_PLOCK_VERSION_MAJOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	info->version[1] = DLM_PLOCK_VERSION_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	info->version[2] = DLM_PLOCK_VERSION_PATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static int check_version(struct dlm_plock_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	    (DLM_PLOCK_VERSION_MINOR < info->version[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		log_print("plock device version mismatch: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			  "kernel (%u.%u.%u), user (%u.%u.%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			  DLM_PLOCK_VERSION_MAJOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			  DLM_PLOCK_VERSION_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			  DLM_PLOCK_VERSION_PATCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			  info->version[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 			  info->version[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			  info->version[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void send_op(struct plock_op *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	set_version(&op->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	INIT_LIST_HEAD(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	list_add_tail(&op->list, &send_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	wake_up(&send_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) /* If a process was killed while waiting for the only plock on a file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)    locks_remove_posix will not see any lock on the file so it won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)    send an unlock-close to us to pass on to userspace to clean up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)    abandoned waiter.  So, we have to insert the unlock-close when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)    lock call is interrupted. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static void do_unlock_close(struct dlm_ls *ls, u64 number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			    struct file *file, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct plock_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	op = kzalloc(sizeof(*op), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (!op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	op->info.optype		= DLM_PLOCK_OP_UNLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	op->info.pid		= fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	op->info.fsid		= ls->ls_global_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	op->info.number		= number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	op->info.start		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	op->info.end		= OFFSET_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (fl->fl_lmops && fl->fl_lmops->lm_grant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		op->info.owner	= (__u64) fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		op->info.owner	= (__u64)(long) fl->fl_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	op->info.flags |= DLM_PLOCK_FL_CLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	send_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		   int cmd, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct dlm_ls *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct plock_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct plock_xop *xop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	ls = dlm_find_lockspace_local(lockspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	xop = kzalloc(sizeof(*xop), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!xop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		rv = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	op = &xop->xop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	op->info.optype		= DLM_PLOCK_OP_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	op->info.pid		= fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	op->info.ex		= (fl->fl_type == F_WRLCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	op->info.wait		= IS_SETLKW(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	op->info.fsid		= ls->ls_global_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	op->info.number		= number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	op->info.start		= fl->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	op->info.end		= fl->fl_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		/* fl_owner is lockd which doesn't distinguish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		   processes on the nfs client */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		op->info.owner	= (__u64) fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		xop->callback	= fl->fl_lmops->lm_grant;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		locks_init_lock(&xop->flc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		locks_copy_lock(&xop->flc, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		xop->fl		= fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		xop->file	= file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		op->info.owner	= (__u64)(long) fl->fl_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		xop->callback	= NULL;
^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) 	send_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	if (xop->callback == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		rv = wait_event_interruptible(recv_wq, (op->done != 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		if (rv == -ERESTARTSYS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			log_debug(ls, "dlm_posix_lock: wait killed %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				  (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			kfree(xop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			do_unlock_close(ls, number, file, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		rv = FILE_LOCK_DEFERRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (!list_empty(&op->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		log_error(ls, "dlm_posix_lock: op on list %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			  (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	rv = op->info.rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		if (locks_lock_file_wait(file, fl) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			log_error(ls, "dlm_posix_lock: vfs lock error %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				  (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	kfree(xop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	dlm_put_lockspace(ls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) EXPORT_SYMBOL_GPL(dlm_posix_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Returns failure iff a successful lock operation should be canceled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int dlm_plock_callback(struct plock_op *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	struct file_lock *fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct file_lock *flc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int (*notify)(struct file_lock *fl, int result) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	struct plock_xop *xop = (struct plock_xop *)op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (!list_empty(&op->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		log_print("dlm_plock_callback: op on list %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			  (unsigned long long)op->info.number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/* check if the following 2 are still valid or make a copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	file = xop->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	flc = &xop->flc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	fl = xop->fl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	notify = xop->callback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (op->info.rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		notify(fl, op->info.rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/* got fs lock; bookkeep locally as well: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	flc->fl_flags &= ~FL_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (posix_lock_file(file, flc, NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		 * This can only happen in the case of kmalloc() failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		 * The filesystem's own lock is the authoritative lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		 * so a failure to get the lock locally is not a disaster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		 * As long as the fs cannot reliably cancel locks (especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		 * in a low-memory situation), we're better off ignoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		 * this failure than trying to recover.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			  (unsigned long long)op->info.number, file, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	rv = notify(fl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (rv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		/* XXX: We need to cancel the fs lock here: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		log_print("dlm_plock_callback: lock granted after lock request "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			  "failed; dangling lock!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	kfree(xop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		     struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	struct dlm_ls *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct plock_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	unsigned char fl_flags = fl->fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	ls = dlm_find_lockspace_local(lockspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	op = kzalloc(sizeof(*op), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (!op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		rv = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* cause the vfs unlock to return ENOENT if lock is not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	fl->fl_flags |= FL_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	rv = locks_lock_file_wait(file, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (rv == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		log_error(ls, "dlm_posix_unlock: vfs unlock error %d %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			  rv, (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	op->info.optype		= DLM_PLOCK_OP_UNLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	op->info.pid		= fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	op->info.fsid		= ls->ls_global_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	op->info.number		= number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	op->info.start		= fl->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	op->info.end		= fl->fl_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (fl->fl_lmops && fl->fl_lmops->lm_grant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		op->info.owner	= (__u64) fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		op->info.owner	= (__u64)(long) fl->fl_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (fl->fl_flags & FL_CLOSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		op->info.flags |= DLM_PLOCK_FL_CLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		send_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	send_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	wait_event(recv_wq, (op->done != 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (!list_empty(&op->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		log_error(ls, "dlm_posix_unlock: op on list %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			  (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	rv = op->info.rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (rv == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	kfree(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	dlm_put_lockspace(ls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	fl->fl_flags = fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) EXPORT_SYMBOL_GPL(dlm_posix_unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		  struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct dlm_ls *ls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct plock_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	ls = dlm_find_lockspace_local(lockspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (!ls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	op = kzalloc(sizeof(*op), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (!op) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		rv = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	op->info.optype		= DLM_PLOCK_OP_GET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	op->info.pid		= fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	op->info.ex		= (fl->fl_type == F_WRLCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	op->info.fsid		= ls->ls_global_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	op->info.number		= number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	op->info.start		= fl->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	op->info.end		= fl->fl_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (fl->fl_lmops && fl->fl_lmops->lm_grant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		op->info.owner	= (__u64) fl->fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		op->info.owner	= (__u64)(long) fl->fl_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	send_op(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	wait_event(recv_wq, (op->done != 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (!list_empty(&op->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		log_error(ls, "dlm_posix_get: op on list %llx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			  (unsigned long long)number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/* info.rv from userspace is 1 for conflict, 0 for no-conflict,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	   -ENOENT if there are no locks on the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	rv = op->info.rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	fl->fl_type = F_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (rv == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	else if (rv > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		locks_init_lock(fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		fl->fl_flags = FL_POSIX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		fl->fl_pid = -op->info.pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		fl->fl_start = op->info.start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		fl->fl_end = op->info.end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		rv = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	kfree(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	dlm_put_lockspace(ls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) EXPORT_SYMBOL_GPL(dlm_posix_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* a read copies out one plock request from the send list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static ssize_t dev_read(struct file *file, char __user *u, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct dlm_plock_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	struct plock_op *op = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (count < sizeof(info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (!list_empty(&send_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		op = list_entry(send_list.next, struct plock_op, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (op->info.flags & DLM_PLOCK_FL_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			list_del(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			list_move(&op->list, &recv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		memcpy(&info, &op->info, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (!op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	/* there is no need to get a reply from userspace for unlocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	   that were generated by the vfs cleaning up for a close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	   (the process did not make an unlock call). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (op->info.flags & DLM_PLOCK_FL_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		kfree(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	if (copy_to_user(u, &info, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	return sizeof(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* a write copies in one plock result that should match a plock_op
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)    on the recv list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static ssize_t dev_write(struct file *file, const char __user *u, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			 loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	struct dlm_plock_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	struct plock_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	int found = 0, do_callback = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (count != sizeof(info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (copy_from_user(&info, u, sizeof(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (check_version(&info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	list_for_each_entry(op, &recv_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		if (op->info.fsid == info.fsid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		    op->info.number == info.number &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		    op->info.owner == info.owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			struct plock_xop *xop = (struct plock_xop *)op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			list_del_init(&op->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			memcpy(&op->info, &info, sizeof(info));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			if (xop->callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 				do_callback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 				op->done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (do_callback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			dlm_plock_callback(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			wake_up(&recv_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		log_print("dev_write no op %x %llx", info.fsid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			  (unsigned long long)info.number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static __poll_t dev_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	__poll_t mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	poll_wait(file, &send_wq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	spin_lock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (!list_empty(&send_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		mask = EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	spin_unlock(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static const struct file_operations dev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	.read    = dev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	.write   = dev_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	.poll    = dev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	.owner   = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	.llseek  = noop_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static struct miscdevice plock_dev_misc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	.minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	.name = DLM_PLOCK_MISC_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	.fops = &dev_fops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int dlm_plock_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	int rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	spin_lock_init(&ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	INIT_LIST_HEAD(&send_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	INIT_LIST_HEAD(&recv_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	init_waitqueue_head(&send_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	init_waitqueue_head(&recv_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	rv = misc_register(&plock_dev_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (rv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		log_print("dlm_plock_init: misc_register failed %d", rv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	return rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) void dlm_plock_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	misc_deregister(&plock_dev_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)