| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 1 | /* | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 2 |  * Copyright (C) 2005-2008 Red Hat, Inc.  All rights reserved. | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 3 |  * | 
 | 4 |  * This copyrighted material is made available to anyone wishing to use, | 
 | 5 |  * modify, copy, or redistribute it subject to the terms and conditions | 
| Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 6 |  * of the GNU General Public License version 2. | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 7 |  */ | 
 | 8 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 9 | #include <linux/fs.h> | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 10 | #include <linux/miscdevice.h> | 
| Al Viro | bd01f84 | 2006-10-19 17:23:57 -0400 | [diff] [blame] | 11 | #include <linux/poll.h> | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 12 | #include <linux/dlm.h> | 
 | 13 | #include <linux/dlm_plock.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 15 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 16 | #include "dlm_internal.h" | 
 | 17 | #include "lockspace.h" | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 18 |  | 
 | 19 | static spinlock_t ops_lock; | 
 | 20 | static struct list_head send_list; | 
 | 21 | static struct list_head recv_list; | 
 | 22 | static wait_queue_head_t send_wq; | 
 | 23 | static wait_queue_head_t recv_wq; | 
 | 24 |  | 
 | 25 | struct plock_op { | 
 | 26 | 	struct list_head list; | 
 | 27 | 	int done; | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 28 | 	struct dlm_plock_info info; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 29 | }; | 
 | 30 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 31 | struct plock_xop { | 
 | 32 | 	struct plock_op xop; | 
 | 33 | 	void *callback; | 
 | 34 | 	void *fl; | 
 | 35 | 	void *file; | 
 | 36 | 	struct file_lock flc; | 
 | 37 | }; | 
 | 38 |  | 
 | 39 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 40 | static inline void set_version(struct dlm_plock_info *info) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 41 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 42 | 	info->version[0] = DLM_PLOCK_VERSION_MAJOR; | 
 | 43 | 	info->version[1] = DLM_PLOCK_VERSION_MINOR; | 
 | 44 | 	info->version[2] = DLM_PLOCK_VERSION_PATCH; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 45 | } | 
 | 46 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 47 | static int check_version(struct dlm_plock_info *info) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 48 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 49 | 	if ((DLM_PLOCK_VERSION_MAJOR != info->version[0]) || | 
 | 50 | 	    (DLM_PLOCK_VERSION_MINOR < info->version[1])) { | 
 | 51 | 		log_print("plock device version mismatch: " | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 52 | 			  "kernel (%u.%u.%u), user (%u.%u.%u)", | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 53 | 			  DLM_PLOCK_VERSION_MAJOR, | 
 | 54 | 			  DLM_PLOCK_VERSION_MINOR, | 
 | 55 | 			  DLM_PLOCK_VERSION_PATCH, | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 56 | 			  info->version[0], | 
 | 57 | 			  info->version[1], | 
 | 58 | 			  info->version[2]); | 
 | 59 | 		return -EINVAL; | 
 | 60 | 	} | 
 | 61 | 	return 0; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | static void send_op(struct plock_op *op) | 
 | 65 | { | 
 | 66 | 	set_version(&op->info); | 
 | 67 | 	INIT_LIST_HEAD(&op->list); | 
 | 68 | 	spin_lock(&ops_lock); | 
 | 69 | 	list_add_tail(&op->list, &send_list); | 
 | 70 | 	spin_unlock(&ops_lock); | 
 | 71 | 	wake_up(&send_wq); | 
 | 72 | } | 
 | 73 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 74 | int dlm_posix_lock(dlm_lockspace_t *lockspace, u64 number, struct file *file, | 
 | 75 | 		   int cmd, struct file_lock *fl) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 76 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 77 | 	struct dlm_ls *ls; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 78 | 	struct plock_op *op; | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 79 | 	struct plock_xop *xop; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 80 | 	int rv; | 
 | 81 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 82 | 	ls = dlm_find_lockspace_local(lockspace); | 
 | 83 | 	if (!ls) | 
 | 84 | 		return -EINVAL; | 
 | 85 |  | 
| David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 86 | 	xop = kzalloc(sizeof(*xop), GFP_NOFS); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 87 | 	if (!xop) { | 
 | 88 | 		rv = -ENOMEM; | 
 | 89 | 		goto out; | 
 | 90 | 	} | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 91 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 92 | 	op = &xop->xop; | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 93 | 	op->info.optype		= DLM_PLOCK_OP_LOCK; | 
| David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 94 | 	op->info.pid		= fl->fl_pid; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 95 | 	op->info.ex		= (fl->fl_type == F_WRLCK); | 
 | 96 | 	op->info.wait		= IS_SETLKW(cmd); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 97 | 	op->info.fsid		= ls->ls_global_id; | 
 | 98 | 	op->info.number		= number; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 99 | 	op->info.start		= fl->fl_start; | 
 | 100 | 	op->info.end		= fl->fl_end; | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 101 | 	if (fl->fl_lmops && fl->fl_lmops->fl_grant) { | 
| David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 102 | 		/* fl_owner is lockd which doesn't distinguish | 
 | 103 | 		   processes on the nfs client */ | 
 | 104 | 		op->info.owner	= (__u64) fl->fl_pid; | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 105 | 		xop->callback	= fl->fl_lmops->fl_grant; | 
 | 106 | 		locks_init_lock(&xop->flc); | 
 | 107 | 		locks_copy_lock(&xop->flc, fl); | 
 | 108 | 		xop->fl		= fl; | 
 | 109 | 		xop->file	= file; | 
| David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 110 | 	} else { | 
 | 111 | 		op->info.owner	= (__u64)(long) fl->fl_owner; | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 112 | 		xop->callback	= NULL; | 
| David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 113 | 	} | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 114 |  | 
 | 115 | 	send_op(op); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 116 |  | 
 | 117 | 	if (xop->callback == NULL) | 
 | 118 | 		wait_event(recv_wq, (op->done != 0)); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 119 | 	else { | 
| Miklos Szeredi | bde74e4 | 2008-07-25 01:48:57 -0700 | [diff] [blame] | 120 | 		rv = FILE_LOCK_DEFERRED; | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 121 | 		goto out; | 
 | 122 | 	} | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 123 |  | 
 | 124 | 	spin_lock(&ops_lock); | 
 | 125 | 	if (!list_empty(&op->list)) { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 126 | 		log_error(ls, "dlm_posix_lock: op on list %llx", | 
 | 127 | 			  (unsigned long long)number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 128 | 		list_del(&op->list); | 
 | 129 | 	} | 
 | 130 | 	spin_unlock(&ops_lock); | 
 | 131 |  | 
 | 132 | 	rv = op->info.rv; | 
 | 133 |  | 
 | 134 | 	if (!rv) { | 
 | 135 | 		if (posix_lock_file_wait(file, fl) < 0) | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 136 | 			log_error(ls, "dlm_posix_lock: vfs lock error %llx", | 
 | 137 | 				  (unsigned long long)number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 138 | 	} | 
 | 139 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 140 | 	kfree(xop); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 141 | out: | 
 | 142 | 	dlm_put_lockspace(ls); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 143 | 	return rv; | 
 | 144 | } | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 145 | EXPORT_SYMBOL_GPL(dlm_posix_lock); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 146 |  | 
| André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 147 | /* Returns failure iff a successful lock operation should be canceled */ | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 148 | static int dlm_plock_callback(struct plock_op *op) | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 149 | { | 
 | 150 | 	struct file *file; | 
 | 151 | 	struct file_lock *fl; | 
 | 152 | 	struct file_lock *flc; | 
 | 153 | 	int (*notify)(void *, void *, int) = NULL; | 
 | 154 | 	struct plock_xop *xop = (struct plock_xop *)op; | 
 | 155 | 	int rv = 0; | 
 | 156 |  | 
 | 157 | 	spin_lock(&ops_lock); | 
 | 158 | 	if (!list_empty(&op->list)) { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 159 | 		log_print("dlm_plock_callback: op on list %llx", | 
 | 160 | 			  (unsigned long long)op->info.number); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 161 | 		list_del(&op->list); | 
 | 162 | 	} | 
 | 163 | 	spin_unlock(&ops_lock); | 
 | 164 |  | 
 | 165 | 	/* check if the following 2 are still valid or make a copy */ | 
 | 166 | 	file = xop->file; | 
 | 167 | 	flc = &xop->flc; | 
 | 168 | 	fl = xop->fl; | 
 | 169 | 	notify = xop->callback; | 
 | 170 |  | 
 | 171 | 	if (op->info.rv) { | 
| David Teigland | 24179f4 | 2009-01-19 13:13:33 -0600 | [diff] [blame] | 172 | 		notify(fl, NULL, op->info.rv); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 173 | 		goto out; | 
 | 174 | 	} | 
 | 175 |  | 
 | 176 | 	/* got fs lock; bookkeep locally as well: */ | 
 | 177 | 	flc->fl_flags &= ~FL_SLEEP; | 
 | 178 | 	if (posix_lock_file(file, flc, NULL)) { | 
 | 179 | 		/* | 
 | 180 | 		 * This can only happen in the case of kmalloc() failure. | 
 | 181 | 		 * The filesystem's own lock is the authoritative lock, | 
 | 182 | 		 * so a failure to get the lock locally is not a disaster. | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 183 | 		 * As long as the fs cannot reliably cancel locks (especially | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 184 | 		 * in a low-memory situation), we're better off ignoring | 
 | 185 | 		 * this failure than trying to recover. | 
 | 186 | 		 */ | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 187 | 		log_print("dlm_plock_callback: vfs lock error %llx file %p fl %p", | 
 | 188 | 			  (unsigned long long)op->info.number, file, fl); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 189 | 	} | 
 | 190 |  | 
| David Teigland | 24179f4 | 2009-01-19 13:13:33 -0600 | [diff] [blame] | 191 | 	rv = notify(fl, NULL, 0); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 192 | 	if (rv) { | 
 | 193 | 		/* XXX: We need to cancel the fs lock here: */ | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 194 | 		log_print("dlm_plock_callback: lock granted after lock request " | 
 | 195 | 			  "failed; dangling lock!\n"); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 196 | 		goto out; | 
 | 197 | 	} | 
 | 198 |  | 
 | 199 | out: | 
 | 200 | 	kfree(xop); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 201 | 	return rv; | 
 | 202 | } | 
 | 203 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 204 | int dlm_posix_unlock(dlm_lockspace_t *lockspace, u64 number, struct file *file, | 
 | 205 | 		     struct file_lock *fl) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 206 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 207 | 	struct dlm_ls *ls; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 208 | 	struct plock_op *op; | 
 | 209 | 	int rv; | 
 | 210 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 211 | 	ls = dlm_find_lockspace_local(lockspace); | 
 | 212 | 	if (!ls) | 
 | 213 | 		return -EINVAL; | 
 | 214 |  | 
| David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 215 | 	op = kzalloc(sizeof(*op), GFP_NOFS); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 216 | 	if (!op) { | 
 | 217 | 		rv = -ENOMEM; | 
 | 218 | 		goto out; | 
 | 219 | 	} | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 220 |  | 
 | 221 | 	if (posix_lock_file_wait(file, fl) < 0) | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 222 | 		log_error(ls, "dlm_posix_unlock: vfs unlock error %llx", | 
 | 223 | 			  (unsigned long long)number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 224 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 225 | 	op->info.optype		= DLM_PLOCK_OP_UNLOCK; | 
| David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 226 | 	op->info.pid		= fl->fl_pid; | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 227 | 	op->info.fsid		= ls->ls_global_id; | 
 | 228 | 	op->info.number		= number; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 229 | 	op->info.start		= fl->fl_start; | 
 | 230 | 	op->info.end		= fl->fl_end; | 
| David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 231 | 	if (fl->fl_lmops && fl->fl_lmops->fl_grant) | 
 | 232 | 		op->info.owner	= (__u64) fl->fl_pid; | 
 | 233 | 	else | 
 | 234 | 		op->info.owner	= (__u64)(long) fl->fl_owner; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 235 |  | 
 | 236 | 	send_op(op); | 
 | 237 | 	wait_event(recv_wq, (op->done != 0)); | 
 | 238 |  | 
 | 239 | 	spin_lock(&ops_lock); | 
 | 240 | 	if (!list_empty(&op->list)) { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 241 | 		log_error(ls, "dlm_posix_unlock: op on list %llx", | 
 | 242 | 			  (unsigned long long)number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 243 | 		list_del(&op->list); | 
 | 244 | 	} | 
 | 245 | 	spin_unlock(&ops_lock); | 
 | 246 |  | 
 | 247 | 	rv = op->info.rv; | 
 | 248 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 249 | 	if (rv == -ENOENT) | 
 | 250 | 		rv = 0; | 
 | 251 |  | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 252 | 	kfree(op); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 253 | out: | 
 | 254 | 	dlm_put_lockspace(ls); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 255 | 	return rv; | 
 | 256 | } | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 257 | EXPORT_SYMBOL_GPL(dlm_posix_unlock); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 258 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 259 | int dlm_posix_get(dlm_lockspace_t *lockspace, u64 number, struct file *file, | 
 | 260 | 		  struct file_lock *fl) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 261 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 262 | 	struct dlm_ls *ls; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 263 | 	struct plock_op *op; | 
 | 264 | 	int rv; | 
 | 265 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 266 | 	ls = dlm_find_lockspace_local(lockspace); | 
 | 267 | 	if (!ls) | 
 | 268 | 		return -EINVAL; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 269 |  | 
| David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 270 | 	op = kzalloc(sizeof(*op), GFP_NOFS); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 271 | 	if (!op) { | 
 | 272 | 		rv = -ENOMEM; | 
 | 273 | 		goto out; | 
 | 274 | 	} | 
 | 275 |  | 
 | 276 | 	op->info.optype		= DLM_PLOCK_OP_GET; | 
| David Teigland | 3a2a9c9 | 2006-04-25 15:45:51 -0400 | [diff] [blame] | 277 | 	op->info.pid		= fl->fl_pid; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 278 | 	op->info.ex		= (fl->fl_type == F_WRLCK); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 279 | 	op->info.fsid		= ls->ls_global_id; | 
 | 280 | 	op->info.number		= number; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 281 | 	op->info.start		= fl->fl_start; | 
 | 282 | 	op->info.end		= fl->fl_end; | 
| David Teigland | 2066b58 | 2007-12-06 09:35:25 -0600 | [diff] [blame] | 283 | 	if (fl->fl_lmops && fl->fl_lmops->fl_grant) | 
 | 284 | 		op->info.owner	= (__u64) fl->fl_pid; | 
 | 285 | 	else | 
 | 286 | 		op->info.owner	= (__u64)(long) fl->fl_owner; | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 287 |  | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 288 | 	send_op(op); | 
 | 289 | 	wait_event(recv_wq, (op->done != 0)); | 
 | 290 |  | 
 | 291 | 	spin_lock(&ops_lock); | 
 | 292 | 	if (!list_empty(&op->list)) { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 293 | 		log_error(ls, "dlm_posix_get: op on list %llx", | 
 | 294 | 			  (unsigned long long)number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 295 | 		list_del(&op->list); | 
 | 296 | 	} | 
 | 297 | 	spin_unlock(&ops_lock); | 
 | 298 |  | 
| David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 299 | 	/* info.rv from userspace is 1 for conflict, 0 for no-conflict, | 
 | 300 | 	   -ENOENT if there are no locks on the file */ | 
 | 301 |  | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 302 | 	rv = op->info.rv; | 
 | 303 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 304 | 	fl->fl_type = F_UNLCK; | 
 | 305 | 	if (rv == -ENOENT) | 
 | 306 | 		rv = 0; | 
| David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 307 | 	else if (rv > 0) { | 
| Jeff Layton | 20d5a39 | 2009-01-21 11:34:50 -0500 | [diff] [blame] | 308 | 		locks_init_lock(fl); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 309 | 		fl->fl_type = (op->info.ex) ? F_WRLCK : F_RDLCK; | 
| Jeff Layton | 20d5a39 | 2009-01-21 11:34:50 -0500 | [diff] [blame] | 310 | 		fl->fl_flags = FL_POSIX; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 311 | 		fl->fl_pid = op->info.pid; | 
 | 312 | 		fl->fl_start = op->info.start; | 
 | 313 | 		fl->fl_end = op->info.end; | 
| David Teigland | a7a2ff8 | 2007-06-08 17:01:40 -0500 | [diff] [blame] | 314 | 		rv = 0; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 315 | 	} | 
 | 316 |  | 
 | 317 | 	kfree(op); | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 318 | out: | 
 | 319 | 	dlm_put_lockspace(ls); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 320 | 	return rv; | 
 | 321 | } | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 322 | EXPORT_SYMBOL_GPL(dlm_posix_get); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 323 |  | 
 | 324 | /* a read copies out one plock request from the send list */ | 
 | 325 | static ssize_t dev_read(struct file *file, char __user *u, size_t count, | 
 | 326 | 			loff_t *ppos) | 
 | 327 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 328 | 	struct dlm_plock_info info; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 329 | 	struct plock_op *op = NULL; | 
 | 330 |  | 
 | 331 | 	if (count < sizeof(info)) | 
 | 332 | 		return -EINVAL; | 
 | 333 |  | 
 | 334 | 	spin_lock(&ops_lock); | 
 | 335 | 	if (!list_empty(&send_list)) { | 
 | 336 | 		op = list_entry(send_list.next, struct plock_op, list); | 
 | 337 | 		list_move(&op->list, &recv_list); | 
 | 338 | 		memcpy(&info, &op->info, sizeof(info)); | 
 | 339 | 	} | 
 | 340 | 	spin_unlock(&ops_lock); | 
 | 341 |  | 
 | 342 | 	if (!op) | 
 | 343 | 		return -EAGAIN; | 
 | 344 |  | 
 | 345 | 	if (copy_to_user(u, &info, sizeof(info))) | 
 | 346 | 		return -EFAULT; | 
 | 347 | 	return sizeof(info); | 
 | 348 | } | 
 | 349 |  | 
 | 350 | /* a write copies in one plock result that should match a plock_op | 
 | 351 |    on the recv list */ | 
 | 352 | static ssize_t dev_write(struct file *file, const char __user *u, size_t count, | 
 | 353 | 			 loff_t *ppos) | 
 | 354 | { | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 355 | 	struct dlm_plock_info info; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 356 | 	struct plock_op *op; | 
| David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 357 | 	int found = 0, do_callback = 0; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 358 |  | 
 | 359 | 	if (count != sizeof(info)) | 
 | 360 | 		return -EINVAL; | 
 | 361 |  | 
 | 362 | 	if (copy_from_user(&info, u, sizeof(info))) | 
 | 363 | 		return -EFAULT; | 
 | 364 |  | 
 | 365 | 	if (check_version(&info)) | 
 | 366 | 		return -EINVAL; | 
 | 367 |  | 
 | 368 | 	spin_lock(&ops_lock); | 
 | 369 | 	list_for_each_entry(op, &recv_list, list) { | 
| David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 370 | 		if (op->info.fsid == info.fsid && | 
 | 371 | 		    op->info.number == info.number && | 
| David Teigland | 08eac93 | 2006-08-04 16:19:20 -0500 | [diff] [blame] | 372 | 		    op->info.owner == info.owner) { | 
| David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 373 | 			struct plock_xop *xop = (struct plock_xop *)op; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 374 | 			list_del_init(&op->list); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 375 | 			memcpy(&op->info, &info, sizeof(info)); | 
| David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 376 | 			if (xop->callback) | 
 | 377 | 				do_callback = 1; | 
 | 378 | 			else | 
 | 379 | 				op->done = 1; | 
 | 380 | 			found = 1; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 381 | 			break; | 
 | 382 | 		} | 
 | 383 | 	} | 
 | 384 | 	spin_unlock(&ops_lock); | 
 | 385 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 386 | 	if (found) { | 
| David Teigland | c78a87d | 2009-06-18 13:20:24 -0500 | [diff] [blame] | 387 | 		if (do_callback) | 
| David Teigland | 817d10b | 2008-05-13 14:28:26 -0500 | [diff] [blame] | 388 | 			dlm_plock_callback(op); | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 389 | 		else | 
 | 390 | 			wake_up(&recv_wq); | 
 | 391 | 	} else | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 392 | 		log_print("dev_write no op %x %llx", info.fsid, | 
 | 393 | 			  (unsigned long long)info.number); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 394 | 	return count; | 
 | 395 | } | 
 | 396 |  | 
 | 397 | static unsigned int dev_poll(struct file *file, poll_table *wait) | 
 | 398 | { | 
| Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 399 | 	unsigned int mask = 0; | 
 | 400 |  | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 401 | 	poll_wait(file, &send_wq, wait); | 
 | 402 |  | 
 | 403 | 	spin_lock(&ops_lock); | 
| Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 404 | 	if (!list_empty(&send_list)) | 
 | 405 | 		mask = POLLIN | POLLRDNORM; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 406 | 	spin_unlock(&ops_lock); | 
| Denis Cheng | cee23c7 | 2007-07-25 17:53:58 +0800 | [diff] [blame] | 407 |  | 
 | 408 | 	return mask; | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 409 | } | 
 | 410 |  | 
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 411 | static const struct file_operations dev_fops = { | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 412 | 	.read    = dev_read, | 
 | 413 | 	.write   = dev_write, | 
 | 414 | 	.poll    = dev_poll, | 
| Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 415 | 	.owner   = THIS_MODULE, | 
 | 416 | 	.llseek  = noop_llseek, | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 417 | }; | 
 | 418 |  | 
 | 419 | static struct miscdevice plock_dev_misc = { | 
 | 420 | 	.minor = MISC_DYNAMIC_MINOR, | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 421 | 	.name = DLM_PLOCK_MISC_NAME, | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 422 | 	.fops = &dev_fops | 
 | 423 | }; | 
 | 424 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 425 | int dlm_plock_init(void) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 426 | { | 
 | 427 | 	int rv; | 
 | 428 |  | 
 | 429 | 	spin_lock_init(&ops_lock); | 
 | 430 | 	INIT_LIST_HEAD(&send_list); | 
 | 431 | 	INIT_LIST_HEAD(&recv_list); | 
 | 432 | 	init_waitqueue_head(&send_wq); | 
 | 433 | 	init_waitqueue_head(&recv_wq); | 
 | 434 |  | 
 | 435 | 	rv = misc_register(&plock_dev_misc); | 
 | 436 | 	if (rv) | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 437 | 		log_print("dlm_plock_init: misc_register failed %d", rv); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 438 | 	return rv; | 
 | 439 | } | 
 | 440 |  | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 441 | void dlm_plock_exit(void) | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 442 | { | 
 | 443 | 	if (misc_deregister(&plock_dev_misc) < 0) | 
| David Teigland | 2402211 | 2008-03-14 15:09:15 -0500 | [diff] [blame] | 444 | 		log_print("dlm_plock_exit: misc_deregister failed"); | 
| David Teigland | 869d81d | 2006-01-17 08:47:12 +0000 | [diff] [blame] | 445 | } | 
 | 446 |  |