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