]>
Commit | Line | Data |
---|---|---|
8df08c89 MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * userdlm.c | |
5 | * | |
6 | * Code which implements the kernel side of a minimal userspace | |
7 | * interface to our DLM. | |
8 | * | |
9 | * Many of the functions here are pared down versions of dlmglue.c | |
10 | * functions. | |
11 | * | |
12 | * Copyright (C) 2003, 2004 Oracle. All rights reserved. | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or | |
15 | * modify it under the terms of the GNU General Public | |
16 | * License as published by the Free Software Foundation; either | |
17 | * version 2 of the License, or (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 | * General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public | |
25 | * License along with this program; if not, write to the | |
26 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
27 | * Boston, MA 021110-1307, USA. | |
28 | */ | |
29 | ||
aee93ac4 | 30 | #include <linux/signal.h> |
8df08c89 MF |
31 | |
32 | #include <linux/module.h> | |
33 | #include <linux/fs.h> | |
34 | #include <linux/types.h> | |
35 | #include <linux/crc32.h> | |
36 | ||
37 | ||
38 | #include "cluster/nodemanager.h" | |
39 | #include "cluster/heartbeat.h" | |
40 | #include "cluster/tcp.h" | |
41 | ||
42 | #include "dlmapi.h" | |
43 | ||
44 | #include "userdlm.h" | |
45 | ||
46 | #define MLOG_MASK_PREFIX ML_DLMFS | |
47 | #include "cluster/masklog.h" | |
48 | ||
49 | static inline int user_check_wait_flag(struct user_lock_res *lockres, | |
50 | int flag) | |
51 | { | |
52 | int ret; | |
53 | ||
54 | spin_lock(&lockres->l_lock); | |
55 | ret = lockres->l_flags & flag; | |
56 | spin_unlock(&lockres->l_lock); | |
57 | ||
58 | return ret; | |
59 | } | |
60 | ||
61 | static inline void user_wait_on_busy_lock(struct user_lock_res *lockres) | |
62 | ||
63 | { | |
64 | wait_event(lockres->l_event, | |
65 | !user_check_wait_flag(lockres, USER_LOCK_BUSY)); | |
66 | } | |
67 | ||
68 | static inline void user_wait_on_blocked_lock(struct user_lock_res *lockres) | |
69 | ||
70 | { | |
71 | wait_event(lockres->l_event, | |
72 | !user_check_wait_flag(lockres, USER_LOCK_BLOCKED)); | |
73 | } | |
74 | ||
75 | /* I heart container_of... */ | |
76 | static inline struct dlm_ctxt * | |
77 | dlm_ctxt_from_user_lockres(struct user_lock_res *lockres) | |
78 | { | |
79 | struct dlmfs_inode_private *ip; | |
80 | ||
81 | ip = container_of(lockres, | |
82 | struct dlmfs_inode_private, | |
83 | ip_lockres); | |
84 | return ip->ip_dlm; | |
85 | } | |
86 | ||
87 | static struct inode * | |
88 | user_dlm_inode_from_user_lockres(struct user_lock_res *lockres) | |
89 | { | |
90 | struct dlmfs_inode_private *ip; | |
91 | ||
92 | ip = container_of(lockres, | |
93 | struct dlmfs_inode_private, | |
94 | ip_lockres); | |
95 | return &ip->ip_vfs_inode; | |
96 | } | |
97 | ||
98 | static inline void user_recover_from_dlm_error(struct user_lock_res *lockres) | |
99 | { | |
100 | spin_lock(&lockres->l_lock); | |
101 | lockres->l_flags &= ~USER_LOCK_BUSY; | |
102 | spin_unlock(&lockres->l_lock); | |
103 | } | |
104 | ||
105 | #define user_log_dlm_error(_func, _stat, _lockres) do { \ | |
106 | mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \ | |
107 | "resource %s: %s\n", dlm_errname(_stat), _func, \ | |
108 | _lockres->l_name, dlm_errmsg(_stat)); \ | |
109 | } while (0) | |
110 | ||
111 | /* WARNING: This function lives in a world where the only three lock | |
112 | * levels are EX, PR, and NL. It *will* have to be adjusted when more | |
113 | * lock types are added. */ | |
114 | static inline int user_highest_compat_lock_level(int level) | |
115 | { | |
116 | int new_level = LKM_EXMODE; | |
117 | ||
118 | if (level == LKM_EXMODE) | |
119 | new_level = LKM_NLMODE; | |
120 | else if (level == LKM_PRMODE) | |
121 | new_level = LKM_PRMODE; | |
122 | return new_level; | |
123 | } | |
124 | ||
125 | static void user_ast(void *opaque) | |
126 | { | |
127 | struct user_lock_res *lockres = opaque; | |
128 | struct dlm_lockstatus *lksb; | |
129 | ||
130 | mlog(0, "AST fired for lockres %s\n", lockres->l_name); | |
131 | ||
132 | spin_lock(&lockres->l_lock); | |
133 | ||
134 | lksb = &(lockres->l_lksb); | |
135 | if (lksb->status != DLM_NORMAL) { | |
136 | mlog(ML_ERROR, "lksb status value of %u on lockres %s\n", | |
137 | lksb->status, lockres->l_name); | |
138 | spin_unlock(&lockres->l_lock); | |
139 | return; | |
140 | } | |
141 | ||
cc6eb725 MF |
142 | mlog_bug_on_msg(lockres->l_requested == LKM_IVMODE, |
143 | "Lockres %s, requested ivmode. flags 0x%x\n", | |
144 | lockres->l_name, lockres->l_flags); | |
145 | ||
8df08c89 MF |
146 | /* we're downconverting. */ |
147 | if (lockres->l_requested < lockres->l_level) { | |
148 | if (lockres->l_requested <= | |
149 | user_highest_compat_lock_level(lockres->l_blocking)) { | |
150 | lockres->l_blocking = LKM_NLMODE; | |
151 | lockres->l_flags &= ~USER_LOCK_BLOCKED; | |
152 | } | |
153 | } | |
154 | ||
155 | lockres->l_level = lockres->l_requested; | |
156 | lockres->l_requested = LKM_IVMODE; | |
157 | lockres->l_flags |= USER_LOCK_ATTACHED; | |
158 | lockres->l_flags &= ~USER_LOCK_BUSY; | |
159 | ||
160 | spin_unlock(&lockres->l_lock); | |
161 | ||
162 | wake_up(&lockres->l_event); | |
163 | } | |
164 | ||
165 | static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres) | |
166 | { | |
167 | struct inode *inode; | |
168 | inode = user_dlm_inode_from_user_lockres(lockres); | |
169 | if (!igrab(inode)) | |
170 | BUG(); | |
171 | } | |
172 | ||
173 | static void user_dlm_unblock_lock(void *opaque); | |
174 | ||
175 | static void __user_dlm_queue_lockres(struct user_lock_res *lockres) | |
176 | { | |
177 | if (!(lockres->l_flags & USER_LOCK_QUEUED)) { | |
178 | user_dlm_grab_inode_ref(lockres); | |
179 | ||
180 | INIT_WORK(&lockres->l_work, user_dlm_unblock_lock, | |
181 | lockres); | |
182 | ||
183 | queue_work(user_dlm_worker, &lockres->l_work); | |
184 | lockres->l_flags |= USER_LOCK_QUEUED; | |
185 | } | |
186 | } | |
187 | ||
188 | static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres) | |
189 | { | |
190 | int queue = 0; | |
191 | ||
192 | if (!(lockres->l_flags & USER_LOCK_BLOCKED)) | |
193 | return; | |
194 | ||
195 | switch (lockres->l_blocking) { | |
196 | case LKM_EXMODE: | |
197 | if (!lockres->l_ex_holders && !lockres->l_ro_holders) | |
198 | queue = 1; | |
199 | break; | |
200 | case LKM_PRMODE: | |
201 | if (!lockres->l_ex_holders) | |
202 | queue = 1; | |
203 | break; | |
204 | default: | |
205 | BUG(); | |
206 | } | |
207 | ||
208 | if (queue) | |
209 | __user_dlm_queue_lockres(lockres); | |
210 | } | |
211 | ||
212 | static void user_bast(void *opaque, int level) | |
213 | { | |
214 | struct user_lock_res *lockres = opaque; | |
215 | ||
216 | mlog(0, "Blocking AST fired for lockres %s. Blocking level %d\n", | |
217 | lockres->l_name, level); | |
218 | ||
219 | spin_lock(&lockres->l_lock); | |
220 | lockres->l_flags |= USER_LOCK_BLOCKED; | |
221 | if (level > lockres->l_blocking) | |
222 | lockres->l_blocking = level; | |
223 | ||
224 | __user_dlm_queue_lockres(lockres); | |
225 | spin_unlock(&lockres->l_lock); | |
226 | ||
227 | wake_up(&lockres->l_event); | |
228 | } | |
229 | ||
230 | static void user_unlock_ast(void *opaque, enum dlm_status status) | |
231 | { | |
232 | struct user_lock_res *lockres = opaque; | |
233 | ||
234 | mlog(0, "UNLOCK AST called on lock %s\n", lockres->l_name); | |
235 | ||
f43e6918 | 236 | if (status != DLM_NORMAL && status != DLM_CANCELGRANT) |
8df08c89 MF |
237 | mlog(ML_ERROR, "Dlm returns status %d\n", status); |
238 | ||
239 | spin_lock(&lockres->l_lock); | |
240 | if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) | |
241 | lockres->l_level = LKM_IVMODE; | |
f43e6918 MF |
242 | else if (status == DLM_CANCELGRANT) { |
243 | mlog(0, "Lock %s, cancel fails, flags 0x%x\n", | |
244 | lockres->l_name, lockres->l_flags); | |
245 | /* We tried to cancel a convert request, but it was | |
246 | * already granted. Don't clear the busy flag - the | |
247 | * ast should've done this already. */ | |
248 | BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL)); | |
249 | lockres->l_flags &= ~USER_LOCK_IN_CANCEL; | |
250 | goto out_noclear; | |
251 | } else { | |
252 | BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL)); | |
253 | /* Cancel succeeded, we want to re-queue */ | |
254 | mlog(0, "Lock %s, cancel succeeds, flags 0x%x\n", | |
255 | lockres->l_name, lockres->l_flags); | |
8df08c89 MF |
256 | lockres->l_requested = LKM_IVMODE; /* cancel an |
257 | * upconvert | |
258 | * request. */ | |
259 | lockres->l_flags &= ~USER_LOCK_IN_CANCEL; | |
260 | /* we want the unblock thread to look at it again | |
261 | * now. */ | |
f43e6918 MF |
262 | if (lockres->l_flags & USER_LOCK_BLOCKED) |
263 | __user_dlm_queue_lockres(lockres); | |
8df08c89 MF |
264 | } |
265 | ||
266 | lockres->l_flags &= ~USER_LOCK_BUSY; | |
f43e6918 | 267 | out_noclear: |
8df08c89 MF |
268 | spin_unlock(&lockres->l_lock); |
269 | ||
270 | wake_up(&lockres->l_event); | |
271 | } | |
272 | ||
273 | static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres) | |
274 | { | |
275 | struct inode *inode; | |
276 | inode = user_dlm_inode_from_user_lockres(lockres); | |
277 | iput(inode); | |
278 | } | |
279 | ||
280 | static void user_dlm_unblock_lock(void *opaque) | |
281 | { | |
282 | int new_level, status; | |
283 | struct user_lock_res *lockres = (struct user_lock_res *) opaque; | |
284 | struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres); | |
285 | ||
286 | mlog(0, "processing lockres %s\n", lockres->l_name); | |
287 | ||
288 | spin_lock(&lockres->l_lock); | |
289 | ||
1f7bc828 MF |
290 | mlog_bug_on_msg(!(lockres->l_flags & USER_LOCK_QUEUED), |
291 | "Lockres %s, flags 0x%x\n", | |
292 | lockres->l_name, lockres->l_flags); | |
8df08c89 | 293 | |
1f7bc828 MF |
294 | /* notice that we don't clear USER_LOCK_BLOCKED here. If it's |
295 | * set, we want user_ast clear it. */ | |
8df08c89 MF |
296 | lockres->l_flags &= ~USER_LOCK_QUEUED; |
297 | ||
1f7bc828 MF |
298 | /* It's valid to get here and no longer be blocked - if we get |
299 | * several basts in a row, we might be queued by the first | |
300 | * one, the unblock thread might run and clear the queued | |
301 | * flag, and finally we might get another bast which re-queues | |
302 | * us before our ast for the downconvert is called. */ | |
303 | if (!(lockres->l_flags & USER_LOCK_BLOCKED)) { | |
304 | mlog(0, "Lockres %s, flags 0x%x: queued but not blocking\n", | |
305 | lockres->l_name, lockres->l_flags); | |
306 | spin_unlock(&lockres->l_lock); | |
307 | goto drop_ref; | |
308 | } | |
309 | ||
8df08c89 MF |
310 | if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) { |
311 | mlog(0, "lock is in teardown so we do nothing\n"); | |
312 | spin_unlock(&lockres->l_lock); | |
313 | goto drop_ref; | |
314 | } | |
315 | ||
316 | if (lockres->l_flags & USER_LOCK_BUSY) { | |
f43e6918 MF |
317 | mlog(0, "Cancel lock %s, flags 0x%x\n", |
318 | lockres->l_name, lockres->l_flags); | |
319 | ||
8df08c89 MF |
320 | if (lockres->l_flags & USER_LOCK_IN_CANCEL) { |
321 | spin_unlock(&lockres->l_lock); | |
322 | goto drop_ref; | |
323 | } | |
324 | ||
325 | lockres->l_flags |= USER_LOCK_IN_CANCEL; | |
326 | spin_unlock(&lockres->l_lock); | |
327 | ||
328 | status = dlmunlock(dlm, | |
329 | &lockres->l_lksb, | |
330 | LKM_CANCEL, | |
331 | user_unlock_ast, | |
332 | lockres); | |
f43e6918 | 333 | if (status != DLM_NORMAL) |
8df08c89 MF |
334 | user_log_dlm_error("dlmunlock", status, lockres); |
335 | goto drop_ref; | |
336 | } | |
337 | ||
338 | /* If there are still incompat holders, we can exit safely | |
339 | * without worrying about re-queueing this lock as that will | |
340 | * happen on the last call to user_cluster_unlock. */ | |
341 | if ((lockres->l_blocking == LKM_EXMODE) | |
342 | && (lockres->l_ex_holders || lockres->l_ro_holders)) { | |
343 | spin_unlock(&lockres->l_lock); | |
344 | mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n", | |
345 | lockres->l_ro_holders, lockres->l_ex_holders); | |
346 | goto drop_ref; | |
347 | } | |
348 | ||
349 | if ((lockres->l_blocking == LKM_PRMODE) | |
350 | && lockres->l_ex_holders) { | |
351 | spin_unlock(&lockres->l_lock); | |
352 | mlog(0, "can't downconvert for pr: ex = %u\n", | |
353 | lockres->l_ex_holders); | |
354 | goto drop_ref; | |
355 | } | |
356 | ||
357 | /* yay, we can downconvert now. */ | |
358 | new_level = user_highest_compat_lock_level(lockres->l_blocking); | |
359 | lockres->l_requested = new_level; | |
360 | lockres->l_flags |= USER_LOCK_BUSY; | |
361 | mlog(0, "Downconvert lock from %d to %d\n", | |
362 | lockres->l_level, new_level); | |
363 | spin_unlock(&lockres->l_lock); | |
364 | ||
365 | /* need lock downconvert request now... */ | |
366 | status = dlmlock(dlm, | |
367 | new_level, | |
368 | &lockres->l_lksb, | |
369 | LKM_CONVERT|LKM_VALBLK, | |
370 | lockres->l_name, | |
371 | user_ast, | |
372 | lockres, | |
373 | user_bast); | |
374 | if (status != DLM_NORMAL) { | |
375 | user_log_dlm_error("dlmlock", status, lockres); | |
376 | user_recover_from_dlm_error(lockres); | |
377 | } | |
378 | ||
379 | drop_ref: | |
380 | user_dlm_drop_inode_ref(lockres); | |
381 | } | |
382 | ||
383 | static inline void user_dlm_inc_holders(struct user_lock_res *lockres, | |
384 | int level) | |
385 | { | |
386 | switch(level) { | |
387 | case LKM_EXMODE: | |
388 | lockres->l_ex_holders++; | |
389 | break; | |
390 | case LKM_PRMODE: | |
391 | lockres->l_ro_holders++; | |
392 | break; | |
393 | default: | |
394 | BUG(); | |
395 | } | |
396 | } | |
397 | ||
398 | /* predict what lock level we'll be dropping down to on behalf | |
399 | * of another node, and return true if the currently wanted | |
400 | * level will be compatible with it. */ | |
401 | static inline int | |
402 | user_may_continue_on_blocked_lock(struct user_lock_res *lockres, | |
403 | int wanted) | |
404 | { | |
405 | BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED)); | |
406 | ||
407 | return wanted <= user_highest_compat_lock_level(lockres->l_blocking); | |
408 | } | |
409 | ||
410 | int user_dlm_cluster_lock(struct user_lock_res *lockres, | |
411 | int level, | |
412 | int lkm_flags) | |
413 | { | |
414 | int status, local_flags; | |
415 | struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres); | |
416 | ||
417 | if (level != LKM_EXMODE && | |
418 | level != LKM_PRMODE) { | |
419 | mlog(ML_ERROR, "lockres %s: invalid request!\n", | |
420 | lockres->l_name); | |
421 | status = -EINVAL; | |
422 | goto bail; | |
423 | } | |
424 | ||
425 | mlog(0, "lockres %s: asking for %s lock, passed flags = 0x%x\n", | |
426 | lockres->l_name, | |
427 | (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE", | |
428 | lkm_flags); | |
429 | ||
430 | again: | |
431 | if (signal_pending(current)) { | |
432 | status = -ERESTARTSYS; | |
433 | goto bail; | |
434 | } | |
435 | ||
436 | spin_lock(&lockres->l_lock); | |
437 | ||
438 | /* We only compare against the currently granted level | |
439 | * here. If the lock is blocked waiting on a downconvert, | |
440 | * we'll get caught below. */ | |
441 | if ((lockres->l_flags & USER_LOCK_BUSY) && | |
442 | (level > lockres->l_level)) { | |
443 | /* is someone sitting in dlm_lock? If so, wait on | |
444 | * them. */ | |
445 | spin_unlock(&lockres->l_lock); | |
446 | ||
447 | user_wait_on_busy_lock(lockres); | |
448 | goto again; | |
449 | } | |
450 | ||
451 | if ((lockres->l_flags & USER_LOCK_BLOCKED) && | |
452 | (!user_may_continue_on_blocked_lock(lockres, level))) { | |
453 | /* is the lock is currently blocked on behalf of | |
454 | * another node */ | |
455 | spin_unlock(&lockres->l_lock); | |
456 | ||
457 | user_wait_on_blocked_lock(lockres); | |
458 | goto again; | |
459 | } | |
460 | ||
461 | if (level > lockres->l_level) { | |
462 | local_flags = lkm_flags | LKM_VALBLK; | |
463 | if (lockres->l_level != LKM_IVMODE) | |
464 | local_flags |= LKM_CONVERT; | |
465 | ||
466 | lockres->l_requested = level; | |
467 | lockres->l_flags |= USER_LOCK_BUSY; | |
468 | spin_unlock(&lockres->l_lock); | |
469 | ||
470 | BUG_ON(level == LKM_IVMODE); | |
471 | BUG_ON(level == LKM_NLMODE); | |
472 | ||
473 | mlog(0, "lock %s, get lock from %d to level = %d\n", | |
474 | lockres->l_name, lockres->l_level, level); | |
475 | ||
476 | /* call dlm_lock to upgrade lock now */ | |
477 | status = dlmlock(dlm, | |
478 | level, | |
479 | &lockres->l_lksb, | |
480 | local_flags, | |
481 | lockres->l_name, | |
482 | user_ast, | |
483 | lockres, | |
484 | user_bast); | |
485 | if (status != DLM_NORMAL) { | |
486 | if ((lkm_flags & LKM_NOQUEUE) && | |
487 | (status == DLM_NOTQUEUED)) | |
488 | status = -EAGAIN; | |
489 | else { | |
490 | user_log_dlm_error("dlmlock", status, lockres); | |
491 | status = -EINVAL; | |
492 | } | |
493 | user_recover_from_dlm_error(lockres); | |
494 | goto bail; | |
495 | } | |
496 | ||
497 | mlog(0, "lock %s, successfull return from dlmlock\n", | |
498 | lockres->l_name); | |
499 | ||
500 | user_wait_on_busy_lock(lockres); | |
501 | goto again; | |
502 | } | |
503 | ||
504 | user_dlm_inc_holders(lockres, level); | |
505 | spin_unlock(&lockres->l_lock); | |
506 | ||
507 | mlog(0, "lockres %s: Got %s lock!\n", lockres->l_name, | |
508 | (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE"); | |
509 | ||
510 | status = 0; | |
511 | bail: | |
512 | return status; | |
513 | } | |
514 | ||
515 | static inline void user_dlm_dec_holders(struct user_lock_res *lockres, | |
516 | int level) | |
517 | { | |
518 | switch(level) { | |
519 | case LKM_EXMODE: | |
520 | BUG_ON(!lockres->l_ex_holders); | |
521 | lockres->l_ex_holders--; | |
522 | break; | |
523 | case LKM_PRMODE: | |
524 | BUG_ON(!lockres->l_ro_holders); | |
525 | lockres->l_ro_holders--; | |
526 | break; | |
527 | default: | |
528 | BUG(); | |
529 | } | |
530 | } | |
531 | ||
532 | void user_dlm_cluster_unlock(struct user_lock_res *lockres, | |
533 | int level) | |
534 | { | |
535 | if (level != LKM_EXMODE && | |
536 | level != LKM_PRMODE) { | |
537 | mlog(ML_ERROR, "lockres %s: invalid request!\n", lockres->l_name); | |
538 | return; | |
539 | } | |
540 | ||
541 | mlog(0, "lockres %s: dropping %s lock\n", lockres->l_name, | |
542 | (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE"); | |
543 | ||
544 | spin_lock(&lockres->l_lock); | |
545 | user_dlm_dec_holders(lockres, level); | |
546 | __user_dlm_cond_queue_lockres(lockres); | |
547 | spin_unlock(&lockres->l_lock); | |
548 | } | |
549 | ||
550 | void user_dlm_write_lvb(struct inode *inode, | |
551 | const char *val, | |
552 | unsigned int len) | |
553 | { | |
554 | struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres; | |
555 | char *lvb = lockres->l_lksb.lvb; | |
556 | ||
557 | BUG_ON(len > DLM_LVB_LEN); | |
558 | ||
559 | spin_lock(&lockres->l_lock); | |
560 | ||
561 | BUG_ON(lockres->l_level < LKM_EXMODE); | |
562 | memcpy(lvb, val, len); | |
563 | ||
564 | spin_unlock(&lockres->l_lock); | |
565 | } | |
566 | ||
567 | void user_dlm_read_lvb(struct inode *inode, | |
568 | char *val, | |
569 | unsigned int len) | |
570 | { | |
571 | struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres; | |
572 | char *lvb = lockres->l_lksb.lvb; | |
573 | ||
574 | BUG_ON(len > DLM_LVB_LEN); | |
575 | ||
576 | spin_lock(&lockres->l_lock); | |
577 | ||
578 | BUG_ON(lockres->l_level < LKM_PRMODE); | |
579 | memcpy(val, lvb, len); | |
580 | ||
581 | spin_unlock(&lockres->l_lock); | |
582 | } | |
583 | ||
584 | void user_dlm_lock_res_init(struct user_lock_res *lockres, | |
585 | struct dentry *dentry) | |
586 | { | |
587 | memset(lockres, 0, sizeof(*lockres)); | |
588 | ||
589 | spin_lock_init(&lockres->l_lock); | |
590 | init_waitqueue_head(&lockres->l_event); | |
591 | lockres->l_level = LKM_IVMODE; | |
592 | lockres->l_requested = LKM_IVMODE; | |
593 | lockres->l_blocking = LKM_IVMODE; | |
594 | ||
595 | /* should have been checked before getting here. */ | |
596 | BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN); | |
597 | ||
598 | memcpy(lockres->l_name, | |
599 | dentry->d_name.name, | |
600 | dentry->d_name.len); | |
601 | } | |
602 | ||
603 | int user_dlm_destroy_lock(struct user_lock_res *lockres) | |
604 | { | |
605 | int status = -EBUSY; | |
606 | struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres); | |
607 | ||
608 | mlog(0, "asked to destroy %s\n", lockres->l_name); | |
609 | ||
610 | spin_lock(&lockres->l_lock); | |
611 | while (lockres->l_flags & USER_LOCK_BUSY) { | |
612 | spin_unlock(&lockres->l_lock); | |
613 | ||
614 | mlog(0, "lock %s is busy\n", lockres->l_name); | |
615 | ||
616 | user_wait_on_busy_lock(lockres); | |
617 | ||
618 | spin_lock(&lockres->l_lock); | |
619 | } | |
620 | ||
621 | if (lockres->l_ro_holders || lockres->l_ex_holders) { | |
622 | spin_unlock(&lockres->l_lock); | |
623 | mlog(0, "lock %s has holders\n", lockres->l_name); | |
624 | goto bail; | |
625 | } | |
626 | ||
627 | status = 0; | |
628 | if (!(lockres->l_flags & USER_LOCK_ATTACHED)) { | |
629 | spin_unlock(&lockres->l_lock); | |
630 | mlog(0, "lock %s is not attached\n", lockres->l_name); | |
631 | goto bail; | |
632 | } | |
633 | ||
634 | lockres->l_flags &= ~USER_LOCK_ATTACHED; | |
635 | lockres->l_flags |= USER_LOCK_BUSY; | |
636 | lockres->l_flags |= USER_LOCK_IN_TEARDOWN; | |
637 | spin_unlock(&lockres->l_lock); | |
638 | ||
639 | mlog(0, "unlocking lockres %s\n", lockres->l_name); | |
640 | status = dlmunlock(dlm, | |
641 | &lockres->l_lksb, | |
642 | LKM_VALBLK, | |
643 | user_unlock_ast, | |
644 | lockres); | |
645 | if (status != DLM_NORMAL) { | |
646 | user_log_dlm_error("dlmunlock", status, lockres); | |
647 | status = -EINVAL; | |
648 | goto bail; | |
649 | } | |
650 | ||
651 | user_wait_on_busy_lock(lockres); | |
652 | ||
653 | status = 0; | |
654 | bail: | |
655 | return status; | |
656 | } | |
657 | ||
658 | struct dlm_ctxt *user_dlm_register_context(struct qstr *name) | |
659 | { | |
660 | struct dlm_ctxt *dlm; | |
661 | u32 dlm_key; | |
662 | char *domain; | |
663 | ||
664 | domain = kmalloc(name->len + 1, GFP_KERNEL); | |
665 | if (!domain) { | |
666 | mlog_errno(-ENOMEM); | |
667 | return ERR_PTR(-ENOMEM); | |
668 | } | |
669 | ||
670 | dlm_key = crc32_le(0, name->name, name->len); | |
671 | ||
672 | snprintf(domain, name->len + 1, "%.*s", name->len, name->name); | |
673 | ||
674 | dlm = dlm_register_domain(domain, dlm_key); | |
675 | if (IS_ERR(dlm)) | |
676 | mlog_errno(PTR_ERR(dlm)); | |
677 | ||
678 | kfree(domain); | |
679 | return dlm; | |
680 | } | |
681 | ||
682 | void user_dlm_unregister_context(struct dlm_ctxt *dlm) | |
683 | { | |
684 | dlm_unregister_domain(dlm); | |
685 | } |