[PATCH] make fs/jbd2/transaction.c:__kbd2_journal_temp_unlink_buffer() static
[linux-2.6/linux-loongson.git] / fs / ocfs2 / dlmglue.c
blob69fba16efbd1e08241ebe46551d12b506a442b15
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmglue.c
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/mm.h>
30 #include <linux/smp_lock.h>
31 #include <linux/crc32.h>
32 #include <linux/kthread.h>
33 #include <linux/pagemap.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
37 #include <cluster/heartbeat.h>
38 #include <cluster/nodemanager.h>
39 #include <cluster/tcp.h>
41 #include <dlm/dlmapi.h>
43 #define MLOG_MASK_PREFIX ML_DLM_GLUE
44 #include <cluster/masklog.h>
46 #include "ocfs2.h"
48 #include "alloc.h"
49 #include "dcache.h"
50 #include "dlmglue.h"
51 #include "extent_map.h"
52 #include "file.h"
53 #include "heartbeat.h"
54 #include "inode.h"
55 #include "journal.h"
56 #include "slot_map.h"
57 #include "super.h"
58 #include "uptodate.h"
59 #include "vote.h"
61 #include "buffer_head_io.h"
63 struct ocfs2_mask_waiter {
64 struct list_head mw_item;
65 int mw_status;
66 struct completion mw_complete;
67 unsigned long mw_mask;
68 unsigned long mw_goal;
71 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
72 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
75 * Return value from ->downconvert_worker functions.
77 * These control the precise actions of ocfs2_unblock_lock()
78 * and ocfs2_process_blocked_lock()
81 enum ocfs2_unblock_action {
82 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
83 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
84 * ->post_unlock callback */
85 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
86 * ->post_unlock() callback. */
89 struct ocfs2_unblock_ctl {
90 int requeue;
91 enum ocfs2_unblock_action unblock_action;
94 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
95 int new_level);
96 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
98 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
99 int blocking);
101 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
102 int blocking);
104 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105 struct ocfs2_lock_res *lockres);
108 * OCFS2 Lock Resource Operations
110 * These fine tune the behavior of the generic dlmglue locking infrastructure.
112 * The most basic of lock types can point ->l_priv to their respective
113 * struct ocfs2_super and allow the default actions to manage things.
115 * Right now, each lock type also needs to implement an init function,
116 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
117 * should be called when the lock is no longer needed (i.e., object
118 * destruction time).
120 struct ocfs2_lock_res_ops {
122 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
123 * this callback if ->l_priv is not an ocfs2_super pointer
125 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
128 * Optionally called in the downconvert (or "vote") thread
129 * after a successful downconvert. The lockres will not be
130 * referenced after this callback is called, so it is safe to
131 * free memory, etc.
133 * The exact semantics of when this is called are controlled
134 * by ->downconvert_worker()
136 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
139 * Allow a lock type to add checks to determine whether it is
140 * safe to downconvert a lock. Return 0 to re-queue the
141 * downconvert at a later time, nonzero to continue.
143 * For most locks, the default checks that there are no
144 * incompatible holders are sufficient.
146 * Called with the lockres spinlock held.
148 int (*check_downconvert)(struct ocfs2_lock_res *, int);
151 * Allows a lock type to populate the lock value block. This
152 * is called on downconvert, and when we drop a lock.
154 * Locks that want to use this should set LOCK_TYPE_USES_LVB
155 * in the flags field.
157 * Called with the lockres spinlock held.
159 void (*set_lvb)(struct ocfs2_lock_res *);
162 * Called from the downconvert thread when it is determined
163 * that a lock will be downconverted. This is called without
164 * any locks held so the function can do work that might
165 * schedule (syncing out data, etc).
167 * This should return any one of the ocfs2_unblock_action
168 * values, depending on what it wants the thread to do.
170 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
173 * LOCK_TYPE_* flags which describe the specific requirements
174 * of a lock type. Descriptions of each individual flag follow.
176 int flags;
180 * Some locks want to "refresh" potentially stale data when a
181 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
182 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
183 * individual lockres l_flags member from the ast function. It is
184 * expected that the locking wrapper will clear the
185 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
187 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
190 * Indicate that a lock type makes use of the lock value block. The
191 * ->set_lvb lock type callback must be defined.
193 #define LOCK_TYPE_USES_LVB 0x2
195 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
196 .get_osb = ocfs2_get_inode_osb,
197 .flags = 0,
200 static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
201 .get_osb = ocfs2_get_inode_osb,
202 .check_downconvert = ocfs2_check_meta_downconvert,
203 .set_lvb = ocfs2_set_meta_lvb,
204 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
207 static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
208 .get_osb = ocfs2_get_inode_osb,
209 .downconvert_worker = ocfs2_data_convert_worker,
210 .flags = 0,
213 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
214 .flags = LOCK_TYPE_REQUIRES_REFRESH,
217 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
218 .flags = 0,
221 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
222 .get_osb = ocfs2_get_dentry_osb,
223 .post_unlock = ocfs2_dentry_post_unlock,
224 .downconvert_worker = ocfs2_dentry_convert_worker,
225 .flags = 0,
228 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
230 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
231 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
232 lockres->l_type == OCFS2_LOCK_TYPE_RW;
235 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
237 BUG_ON(!ocfs2_is_inode_lock(lockres));
239 return (struct inode *) lockres->l_priv;
242 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
244 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
246 return (struct ocfs2_dentry_lock *)lockres->l_priv;
249 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
251 if (lockres->l_ops->get_osb)
252 return lockres->l_ops->get_osb(lockres);
254 return (struct ocfs2_super *)lockres->l_priv;
257 static int ocfs2_lock_create(struct ocfs2_super *osb,
258 struct ocfs2_lock_res *lockres,
259 int level,
260 int dlm_flags);
261 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
262 int wanted);
263 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
264 struct ocfs2_lock_res *lockres,
265 int level);
266 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
267 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
268 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
269 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
270 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
271 struct ocfs2_lock_res *lockres);
272 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
273 int convert);
274 #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
275 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
276 "resource %s: %s\n", dlm_errname(_stat), _func, \
277 _lockres->l_name, dlm_errmsg(_stat)); \
278 } while (0)
279 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
280 struct ocfs2_lock_res *lockres);
281 static int ocfs2_meta_lock_update(struct inode *inode,
282 struct buffer_head **bh);
283 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
284 static inline int ocfs2_highest_compat_lock_level(int level);
286 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
287 u64 blkno,
288 u32 generation,
289 char *name)
291 int len;
293 mlog_entry_void();
295 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
297 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
298 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
299 (long long)blkno, generation);
301 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
303 mlog(0, "built lock resource with name: %s\n", name);
305 mlog_exit_void();
308 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
310 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
311 struct ocfs2_dlm_debug *dlm_debug)
313 mlog(0, "Add tracking for lockres %s\n", res->l_name);
315 spin_lock(&ocfs2_dlm_tracking_lock);
316 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
317 spin_unlock(&ocfs2_dlm_tracking_lock);
320 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
322 spin_lock(&ocfs2_dlm_tracking_lock);
323 if (!list_empty(&res->l_debug_list))
324 list_del_init(&res->l_debug_list);
325 spin_unlock(&ocfs2_dlm_tracking_lock);
328 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
329 struct ocfs2_lock_res *res,
330 enum ocfs2_lock_type type,
331 struct ocfs2_lock_res_ops *ops,
332 void *priv)
334 res->l_type = type;
335 res->l_ops = ops;
336 res->l_priv = priv;
338 res->l_level = LKM_IVMODE;
339 res->l_requested = LKM_IVMODE;
340 res->l_blocking = LKM_IVMODE;
341 res->l_action = OCFS2_AST_INVALID;
342 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
344 res->l_flags = OCFS2_LOCK_INITIALIZED;
346 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
349 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
351 /* This also clears out the lock status block */
352 memset(res, 0, sizeof(struct ocfs2_lock_res));
353 spin_lock_init(&res->l_lock);
354 init_waitqueue_head(&res->l_event);
355 INIT_LIST_HEAD(&res->l_blocked_list);
356 INIT_LIST_HEAD(&res->l_mask_waiters);
359 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
360 enum ocfs2_lock_type type,
361 unsigned int generation,
362 struct inode *inode)
364 struct ocfs2_lock_res_ops *ops;
366 switch(type) {
367 case OCFS2_LOCK_TYPE_RW:
368 ops = &ocfs2_inode_rw_lops;
369 break;
370 case OCFS2_LOCK_TYPE_META:
371 ops = &ocfs2_inode_meta_lops;
372 break;
373 case OCFS2_LOCK_TYPE_DATA:
374 ops = &ocfs2_inode_data_lops;
375 break;
376 default:
377 mlog_bug_on_msg(1, "type: %d\n", type);
378 ops = NULL; /* thanks, gcc */
379 break;
382 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
383 generation, res->l_name);
384 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
387 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
389 struct inode *inode = ocfs2_lock_res_inode(lockres);
391 return OCFS2_SB(inode->i_sb);
394 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
396 __be64 inode_blkno_be;
398 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
399 sizeof(__be64));
401 return be64_to_cpu(inode_blkno_be);
404 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
406 struct ocfs2_dentry_lock *dl = lockres->l_priv;
408 return OCFS2_SB(dl->dl_inode->i_sb);
411 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
412 u64 parent, struct inode *inode)
414 int len;
415 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
416 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
417 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
419 ocfs2_lock_res_init_once(lockres);
422 * Unfortunately, the standard lock naming scheme won't work
423 * here because we have two 16 byte values to use. Instead,
424 * we'll stuff the inode number as a binary value. We still
425 * want error prints to show something without garbling the
426 * display, so drop a null byte in there before the inode
427 * number. A future version of OCFS2 will likely use all
428 * binary lock names. The stringified names have been a
429 * tremendous aid in debugging, but now that the debugfs
430 * interface exists, we can mangle things there if need be.
432 * NOTE: We also drop the standard "pad" value (the total lock
433 * name size stays the same though - the last part is all
434 * zeros due to the memset in ocfs2_lock_res_init_once()
436 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
437 "%c%016llx",
438 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
439 (long long)parent);
441 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
443 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
444 sizeof(__be64));
446 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
447 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
448 dl);
451 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
452 struct ocfs2_super *osb)
454 /* Superblock lockres doesn't come from a slab so we call init
455 * once on it manually. */
456 ocfs2_lock_res_init_once(res);
457 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
458 0, res->l_name);
459 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
460 &ocfs2_super_lops, osb);
463 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
464 struct ocfs2_super *osb)
466 /* Rename lockres doesn't come from a slab so we call init
467 * once on it manually. */
468 ocfs2_lock_res_init_once(res);
469 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
470 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
471 &ocfs2_rename_lops, osb);
474 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
476 mlog_entry_void();
478 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
479 return;
481 ocfs2_remove_lockres_tracking(res);
483 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
484 "Lockres %s is on the blocked list\n",
485 res->l_name);
486 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
487 "Lockres %s has mask waiters pending\n",
488 res->l_name);
489 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
490 "Lockres %s is locked\n",
491 res->l_name);
492 mlog_bug_on_msg(res->l_ro_holders,
493 "Lockres %s has %u ro holders\n",
494 res->l_name, res->l_ro_holders);
495 mlog_bug_on_msg(res->l_ex_holders,
496 "Lockres %s has %u ex holders\n",
497 res->l_name, res->l_ex_holders);
499 /* Need to clear out the lock status block for the dlm */
500 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
502 res->l_flags = 0UL;
503 mlog_exit_void();
506 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
507 int level)
509 mlog_entry_void();
511 BUG_ON(!lockres);
513 switch(level) {
514 case LKM_EXMODE:
515 lockres->l_ex_holders++;
516 break;
517 case LKM_PRMODE:
518 lockres->l_ro_holders++;
519 break;
520 default:
521 BUG();
524 mlog_exit_void();
527 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
528 int level)
530 mlog_entry_void();
532 BUG_ON(!lockres);
534 switch(level) {
535 case LKM_EXMODE:
536 BUG_ON(!lockres->l_ex_holders);
537 lockres->l_ex_holders--;
538 break;
539 case LKM_PRMODE:
540 BUG_ON(!lockres->l_ro_holders);
541 lockres->l_ro_holders--;
542 break;
543 default:
544 BUG();
546 mlog_exit_void();
549 /* WARNING: This function lives in a world where the only three lock
550 * levels are EX, PR, and NL. It *will* have to be adjusted when more
551 * lock types are added. */
552 static inline int ocfs2_highest_compat_lock_level(int level)
554 int new_level = LKM_EXMODE;
556 if (level == LKM_EXMODE)
557 new_level = LKM_NLMODE;
558 else if (level == LKM_PRMODE)
559 new_level = LKM_PRMODE;
560 return new_level;
563 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
564 unsigned long newflags)
566 struct list_head *pos, *tmp;
567 struct ocfs2_mask_waiter *mw;
569 assert_spin_locked(&lockres->l_lock);
571 lockres->l_flags = newflags;
573 list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
574 mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
575 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
576 continue;
578 list_del_init(&mw->mw_item);
579 mw->mw_status = 0;
580 complete(&mw->mw_complete);
583 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
585 lockres_set_flags(lockres, lockres->l_flags | or);
587 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
588 unsigned long clear)
590 lockres_set_flags(lockres, lockres->l_flags & ~clear);
593 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
595 mlog_entry_void();
597 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
598 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
599 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
600 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
602 lockres->l_level = lockres->l_requested;
603 if (lockres->l_level <=
604 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
605 lockres->l_blocking = LKM_NLMODE;
606 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
608 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
610 mlog_exit_void();
613 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
615 mlog_entry_void();
617 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
618 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
620 /* Convert from RO to EX doesn't really need anything as our
621 * information is already up to data. Convert from NL to
622 * *anything* however should mark ourselves as needing an
623 * update */
624 if (lockres->l_level == LKM_NLMODE &&
625 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
626 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
628 lockres->l_level = lockres->l_requested;
629 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
631 mlog_exit_void();
634 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
636 mlog_entry_void();
638 BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
639 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
641 if (lockres->l_requested > LKM_NLMODE &&
642 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
643 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
644 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
646 lockres->l_level = lockres->l_requested;
647 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
648 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
650 mlog_exit_void();
653 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
654 int level)
656 int needs_downconvert = 0;
657 mlog_entry_void();
659 assert_spin_locked(&lockres->l_lock);
661 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
663 if (level > lockres->l_blocking) {
664 /* only schedule a downconvert if we haven't already scheduled
665 * one that goes low enough to satisfy the level we're
666 * blocking. this also catches the case where we get
667 * duplicate BASTs */
668 if (ocfs2_highest_compat_lock_level(level) <
669 ocfs2_highest_compat_lock_level(lockres->l_blocking))
670 needs_downconvert = 1;
672 lockres->l_blocking = level;
675 mlog_exit(needs_downconvert);
676 return needs_downconvert;
679 static void ocfs2_blocking_ast(void *opaque, int level)
681 struct ocfs2_lock_res *lockres = opaque;
682 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
683 int needs_downconvert;
684 unsigned long flags;
686 BUG_ON(level <= LKM_NLMODE);
688 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
689 lockres->l_name, level, lockres->l_level,
690 ocfs2_lock_type_string(lockres->l_type));
692 spin_lock_irqsave(&lockres->l_lock, flags);
693 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
694 if (needs_downconvert)
695 ocfs2_schedule_blocked_lock(osb, lockres);
696 spin_unlock_irqrestore(&lockres->l_lock, flags);
698 wake_up(&lockres->l_event);
700 ocfs2_kick_vote_thread(osb);
703 static void ocfs2_locking_ast(void *opaque)
705 struct ocfs2_lock_res *lockres = opaque;
706 struct dlm_lockstatus *lksb = &lockres->l_lksb;
707 unsigned long flags;
709 spin_lock_irqsave(&lockres->l_lock, flags);
711 if (lksb->status != DLM_NORMAL) {
712 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
713 lockres->l_name, lksb->status);
714 spin_unlock_irqrestore(&lockres->l_lock, flags);
715 return;
718 switch(lockres->l_action) {
719 case OCFS2_AST_ATTACH:
720 ocfs2_generic_handle_attach_action(lockres);
721 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
722 break;
723 case OCFS2_AST_CONVERT:
724 ocfs2_generic_handle_convert_action(lockres);
725 break;
726 case OCFS2_AST_DOWNCONVERT:
727 ocfs2_generic_handle_downconvert_action(lockres);
728 break;
729 default:
730 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
731 "lockres flags = 0x%lx, unlock action: %u\n",
732 lockres->l_name, lockres->l_action, lockres->l_flags,
733 lockres->l_unlock_action);
734 BUG();
737 /* set it to something invalid so if we get called again we
738 * can catch it. */
739 lockres->l_action = OCFS2_AST_INVALID;
741 wake_up(&lockres->l_event);
742 spin_unlock_irqrestore(&lockres->l_lock, flags);
745 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
746 int convert)
748 unsigned long flags;
750 mlog_entry_void();
751 spin_lock_irqsave(&lockres->l_lock, flags);
752 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
753 if (convert)
754 lockres->l_action = OCFS2_AST_INVALID;
755 else
756 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
757 spin_unlock_irqrestore(&lockres->l_lock, flags);
759 wake_up(&lockres->l_event);
760 mlog_exit_void();
763 /* Note: If we detect another process working on the lock (i.e.,
764 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
765 * to do the right thing in that case.
767 static int ocfs2_lock_create(struct ocfs2_super *osb,
768 struct ocfs2_lock_res *lockres,
769 int level,
770 int dlm_flags)
772 int ret = 0;
773 enum dlm_status status;
774 unsigned long flags;
776 mlog_entry_void();
778 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
779 dlm_flags);
781 spin_lock_irqsave(&lockres->l_lock, flags);
782 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
783 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
784 spin_unlock_irqrestore(&lockres->l_lock, flags);
785 goto bail;
788 lockres->l_action = OCFS2_AST_ATTACH;
789 lockres->l_requested = level;
790 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
791 spin_unlock_irqrestore(&lockres->l_lock, flags);
793 status = dlmlock(osb->dlm,
794 level,
795 &lockres->l_lksb,
796 dlm_flags,
797 lockres->l_name,
798 OCFS2_LOCK_ID_MAX_LEN - 1,
799 ocfs2_locking_ast,
800 lockres,
801 ocfs2_blocking_ast);
802 if (status != DLM_NORMAL) {
803 ocfs2_log_dlm_error("dlmlock", status, lockres);
804 ret = -EINVAL;
805 ocfs2_recover_from_dlm_error(lockres, 1);
808 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
810 bail:
811 mlog_exit(ret);
812 return ret;
815 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
816 int flag)
818 unsigned long flags;
819 int ret;
821 spin_lock_irqsave(&lockres->l_lock, flags);
822 ret = lockres->l_flags & flag;
823 spin_unlock_irqrestore(&lockres->l_lock, flags);
825 return ret;
828 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
831 wait_event(lockres->l_event,
832 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
835 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
838 wait_event(lockres->l_event,
839 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
842 /* predict what lock level we'll be dropping down to on behalf
843 * of another node, and return true if the currently wanted
844 * level will be compatible with it. */
845 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
846 int wanted)
848 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
850 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
853 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
855 INIT_LIST_HEAD(&mw->mw_item);
856 init_completion(&mw->mw_complete);
859 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
861 wait_for_completion(&mw->mw_complete);
862 /* Re-arm the completion in case we want to wait on it again */
863 INIT_COMPLETION(mw->mw_complete);
864 return mw->mw_status;
867 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
868 struct ocfs2_mask_waiter *mw,
869 unsigned long mask,
870 unsigned long goal)
872 BUG_ON(!list_empty(&mw->mw_item));
874 assert_spin_locked(&lockres->l_lock);
876 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
877 mw->mw_mask = mask;
878 mw->mw_goal = goal;
881 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
882 * if the mask still hadn't reached its goal */
883 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
884 struct ocfs2_mask_waiter *mw)
886 unsigned long flags;
887 int ret = 0;
889 spin_lock_irqsave(&lockres->l_lock, flags);
890 if (!list_empty(&mw->mw_item)) {
891 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
892 ret = -EBUSY;
894 list_del_init(&mw->mw_item);
895 init_completion(&mw->mw_complete);
897 spin_unlock_irqrestore(&lockres->l_lock, flags);
899 return ret;
903 static int ocfs2_cluster_lock(struct ocfs2_super *osb,
904 struct ocfs2_lock_res *lockres,
905 int level,
906 int lkm_flags,
907 int arg_flags)
909 struct ocfs2_mask_waiter mw;
910 enum dlm_status status;
911 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
912 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
913 unsigned long flags;
915 mlog_entry_void();
917 ocfs2_init_mask_waiter(&mw);
919 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
920 lkm_flags |= LKM_VALBLK;
922 again:
923 wait = 0;
925 if (catch_signals && signal_pending(current)) {
926 ret = -ERESTARTSYS;
927 goto out;
930 spin_lock_irqsave(&lockres->l_lock, flags);
932 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
933 "Cluster lock called on freeing lockres %s! flags "
934 "0x%lx\n", lockres->l_name, lockres->l_flags);
936 /* We only compare against the currently granted level
937 * here. If the lock is blocked waiting on a downconvert,
938 * we'll get caught below. */
939 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
940 level > lockres->l_level) {
941 /* is someone sitting in dlm_lock? If so, wait on
942 * them. */
943 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
944 wait = 1;
945 goto unlock;
948 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
949 /* lock has not been created yet. */
950 spin_unlock_irqrestore(&lockres->l_lock, flags);
952 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
953 if (ret < 0) {
954 mlog_errno(ret);
955 goto out;
957 goto again;
960 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
961 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
962 /* is the lock is currently blocked on behalf of
963 * another node */
964 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
965 wait = 1;
966 goto unlock;
969 if (level > lockres->l_level) {
970 if (lockres->l_action != OCFS2_AST_INVALID)
971 mlog(ML_ERROR, "lockres %s has action %u pending\n",
972 lockres->l_name, lockres->l_action);
974 lockres->l_action = OCFS2_AST_CONVERT;
975 lockres->l_requested = level;
976 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
977 spin_unlock_irqrestore(&lockres->l_lock, flags);
979 BUG_ON(level == LKM_IVMODE);
980 BUG_ON(level == LKM_NLMODE);
982 mlog(0, "lock %s, convert from %d to level = %d\n",
983 lockres->l_name, lockres->l_level, level);
985 /* call dlm_lock to upgrade lock now */
986 status = dlmlock(osb->dlm,
987 level,
988 &lockres->l_lksb,
989 lkm_flags|LKM_CONVERT,
990 lockres->l_name,
991 OCFS2_LOCK_ID_MAX_LEN - 1,
992 ocfs2_locking_ast,
993 lockres,
994 ocfs2_blocking_ast);
995 if (status != DLM_NORMAL) {
996 if ((lkm_flags & LKM_NOQUEUE) &&
997 (status == DLM_NOTQUEUED))
998 ret = -EAGAIN;
999 else {
1000 ocfs2_log_dlm_error("dlmlock", status,
1001 lockres);
1002 ret = -EINVAL;
1004 ocfs2_recover_from_dlm_error(lockres, 1);
1005 goto out;
1008 mlog(0, "lock %s, successfull return from dlmlock\n",
1009 lockres->l_name);
1011 /* At this point we've gone inside the dlm and need to
1012 * complete our work regardless. */
1013 catch_signals = 0;
1015 /* wait for busy to clear and carry on */
1016 goto again;
1019 /* Ok, if we get here then we're good to go. */
1020 ocfs2_inc_holders(lockres, level);
1022 ret = 0;
1023 unlock:
1024 spin_unlock_irqrestore(&lockres->l_lock, flags);
1025 out:
1027 * This is helping work around a lock inversion between the page lock
1028 * and dlm locks. One path holds the page lock while calling aops
1029 * which block acquiring dlm locks. The voting thread holds dlm
1030 * locks while acquiring page locks while down converting data locks.
1031 * This block is helping an aop path notice the inversion and back
1032 * off to unlock its page lock before trying the dlm lock again.
1034 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1035 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1036 wait = 0;
1037 if (lockres_remove_mask_waiter(lockres, &mw))
1038 ret = -EAGAIN;
1039 else
1040 goto again;
1042 if (wait) {
1043 ret = ocfs2_wait_for_mask(&mw);
1044 if (ret == 0)
1045 goto again;
1046 mlog_errno(ret);
1049 mlog_exit(ret);
1050 return ret;
1053 static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1054 struct ocfs2_lock_res *lockres,
1055 int level)
1057 unsigned long flags;
1059 mlog_entry_void();
1060 spin_lock_irqsave(&lockres->l_lock, flags);
1061 ocfs2_dec_holders(lockres, level);
1062 ocfs2_vote_on_unlock(osb, lockres);
1063 spin_unlock_irqrestore(&lockres->l_lock, flags);
1064 mlog_exit_void();
1067 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1068 struct ocfs2_lock_res *lockres,
1069 int ex,
1070 int local)
1072 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1073 unsigned long flags;
1074 int lkm_flags = local ? LKM_LOCAL : 0;
1076 spin_lock_irqsave(&lockres->l_lock, flags);
1077 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1078 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1079 spin_unlock_irqrestore(&lockres->l_lock, flags);
1081 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1084 /* Grants us an EX lock on the data and metadata resources, skipping
1085 * the normal cluster directory lookup. Use this ONLY on newly created
1086 * inodes which other nodes can't possibly see, and which haven't been
1087 * hashed in the inode hash yet. This can give us a good performance
1088 * increase as it'll skip the network broadcast normally associated
1089 * with creating a new lock resource. */
1090 int ocfs2_create_new_inode_locks(struct inode *inode)
1092 int ret;
1093 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1095 BUG_ON(!inode);
1096 BUG_ON(!ocfs2_inode_is_new(inode));
1098 mlog_entry_void();
1100 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1102 /* NOTE: That we don't increment any of the holder counts, nor
1103 * do we add anything to a journal handle. Since this is
1104 * supposed to be a new inode which the cluster doesn't know
1105 * about yet, there is no need to. As far as the LVB handling
1106 * is concerned, this is basically like acquiring an EX lock
1107 * on a resource which has an invalid one -- we'll set it
1108 * valid when we release the EX. */
1110 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1111 if (ret) {
1112 mlog_errno(ret);
1113 goto bail;
1117 * We don't want to use LKM_LOCAL on a meta data lock as they
1118 * don't use a generation in their lock names.
1120 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
1121 if (ret) {
1122 mlog_errno(ret);
1123 goto bail;
1126 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
1127 if (ret) {
1128 mlog_errno(ret);
1129 goto bail;
1132 bail:
1133 mlog_exit(ret);
1134 return ret;
1137 int ocfs2_rw_lock(struct inode *inode, int write)
1139 int status, level;
1140 struct ocfs2_lock_res *lockres;
1142 BUG_ON(!inode);
1144 mlog_entry_void();
1146 mlog(0, "inode %llu take %s RW lock\n",
1147 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1148 write ? "EXMODE" : "PRMODE");
1150 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1152 level = write ? LKM_EXMODE : LKM_PRMODE;
1154 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1156 if (status < 0)
1157 mlog_errno(status);
1159 mlog_exit(status);
1160 return status;
1163 void ocfs2_rw_unlock(struct inode *inode, int write)
1165 int level = write ? LKM_EXMODE : LKM_PRMODE;
1166 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1168 mlog_entry_void();
1170 mlog(0, "inode %llu drop %s RW lock\n",
1171 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1172 write ? "EXMODE" : "PRMODE");
1174 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1176 mlog_exit_void();
1179 int ocfs2_data_lock_full(struct inode *inode,
1180 int write,
1181 int arg_flags)
1183 int status = 0, level;
1184 struct ocfs2_lock_res *lockres;
1186 BUG_ON(!inode);
1188 mlog_entry_void();
1190 mlog(0, "inode %llu take %s DATA lock\n",
1191 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1192 write ? "EXMODE" : "PRMODE");
1194 /* We'll allow faking a readonly data lock for
1195 * rodevices. */
1196 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1197 if (write) {
1198 status = -EROFS;
1199 mlog_errno(status);
1201 goto out;
1204 lockres = &OCFS2_I(inode)->ip_data_lockres;
1206 level = write ? LKM_EXMODE : LKM_PRMODE;
1208 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1209 0, arg_flags);
1210 if (status < 0 && status != -EAGAIN)
1211 mlog_errno(status);
1213 out:
1214 mlog_exit(status);
1215 return status;
1218 /* see ocfs2_meta_lock_with_page() */
1219 int ocfs2_data_lock_with_page(struct inode *inode,
1220 int write,
1221 struct page *page)
1223 int ret;
1225 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1226 if (ret == -EAGAIN) {
1227 unlock_page(page);
1228 if (ocfs2_data_lock(inode, write) == 0)
1229 ocfs2_data_unlock(inode, write);
1230 ret = AOP_TRUNCATED_PAGE;
1233 return ret;
1236 static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1237 struct ocfs2_lock_res *lockres)
1239 int kick = 0;
1241 mlog_entry_void();
1243 /* If we know that another node is waiting on our lock, kick
1244 * the vote thread * pre-emptively when we reach a release
1245 * condition. */
1246 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1247 switch(lockres->l_blocking) {
1248 case LKM_EXMODE:
1249 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1250 kick = 1;
1251 break;
1252 case LKM_PRMODE:
1253 if (!lockres->l_ex_holders)
1254 kick = 1;
1255 break;
1256 default:
1257 BUG();
1261 if (kick)
1262 ocfs2_kick_vote_thread(osb);
1264 mlog_exit_void();
1267 void ocfs2_data_unlock(struct inode *inode,
1268 int write)
1270 int level = write ? LKM_EXMODE : LKM_PRMODE;
1271 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1273 mlog_entry_void();
1275 mlog(0, "inode %llu drop %s DATA lock\n",
1276 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1277 write ? "EXMODE" : "PRMODE");
1279 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1280 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1282 mlog_exit_void();
1285 #define OCFS2_SEC_BITS 34
1286 #define OCFS2_SEC_SHIFT (64 - 34)
1287 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1289 /* LVB only has room for 64 bits of time here so we pack it for
1290 * now. */
1291 static u64 ocfs2_pack_timespec(struct timespec *spec)
1293 u64 res;
1294 u64 sec = spec->tv_sec;
1295 u32 nsec = spec->tv_nsec;
1297 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1299 return res;
1302 /* Call this with the lockres locked. I am reasonably sure we don't
1303 * need ip_lock in this function as anyone who would be changing those
1304 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1305 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1307 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1308 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1309 struct ocfs2_meta_lvb *lvb;
1311 mlog_entry_void();
1313 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1316 * Invalidate the LVB of a deleted inode - this way other
1317 * nodes are forced to go to disk and discover the new inode
1318 * status.
1320 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1321 lvb->lvb_version = 0;
1322 goto out;
1325 lvb->lvb_version = OCFS2_LVB_VERSION;
1326 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1327 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1328 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1329 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1330 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1331 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1332 lvb->lvb_iatime_packed =
1333 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1334 lvb->lvb_ictime_packed =
1335 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1336 lvb->lvb_imtime_packed =
1337 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1338 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
1339 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1341 out:
1342 mlog_meta_lvb(0, lockres);
1344 mlog_exit_void();
1347 static void ocfs2_unpack_timespec(struct timespec *spec,
1348 u64 packed_time)
1350 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1351 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1354 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1356 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1357 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1358 struct ocfs2_meta_lvb *lvb;
1360 mlog_entry_void();
1362 mlog_meta_lvb(0, lockres);
1364 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1366 /* We're safe here without the lockres lock... */
1367 spin_lock(&oi->ip_lock);
1368 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1369 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1371 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1372 ocfs2_set_inode_flags(inode);
1374 /* fast-symlinks are a special case */
1375 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1376 inode->i_blocks = 0;
1377 else
1378 inode->i_blocks =
1379 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1381 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1382 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1383 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1384 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1385 ocfs2_unpack_timespec(&inode->i_atime,
1386 be64_to_cpu(lvb->lvb_iatime_packed));
1387 ocfs2_unpack_timespec(&inode->i_mtime,
1388 be64_to_cpu(lvb->lvb_imtime_packed));
1389 ocfs2_unpack_timespec(&inode->i_ctime,
1390 be64_to_cpu(lvb->lvb_ictime_packed));
1391 spin_unlock(&oi->ip_lock);
1393 mlog_exit_void();
1396 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1397 struct ocfs2_lock_res *lockres)
1399 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1401 if (lvb->lvb_version == OCFS2_LVB_VERSION
1402 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1403 return 1;
1404 return 0;
1407 /* Determine whether a lock resource needs to be refreshed, and
1408 * arbitrate who gets to refresh it.
1410 * 0 means no refresh needed.
1412 * > 0 means you need to refresh this and you MUST call
1413 * ocfs2_complete_lock_res_refresh afterwards. */
1414 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1416 unsigned long flags;
1417 int status = 0;
1419 mlog_entry_void();
1421 refresh_check:
1422 spin_lock_irqsave(&lockres->l_lock, flags);
1423 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1424 spin_unlock_irqrestore(&lockres->l_lock, flags);
1425 goto bail;
1428 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1429 spin_unlock_irqrestore(&lockres->l_lock, flags);
1431 ocfs2_wait_on_refreshing_lock(lockres);
1432 goto refresh_check;
1435 /* Ok, I'll be the one to refresh this lock. */
1436 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1437 spin_unlock_irqrestore(&lockres->l_lock, flags);
1439 status = 1;
1440 bail:
1441 mlog_exit(status);
1442 return status;
1445 /* If status is non zero, I'll mark it as not being in refresh
1446 * anymroe, but i won't clear the needs refresh flag. */
1447 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1448 int status)
1450 unsigned long flags;
1451 mlog_entry_void();
1453 spin_lock_irqsave(&lockres->l_lock, flags);
1454 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1455 if (!status)
1456 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1457 spin_unlock_irqrestore(&lockres->l_lock, flags);
1459 wake_up(&lockres->l_event);
1461 mlog_exit_void();
1464 /* may or may not return a bh if it went to disk. */
1465 static int ocfs2_meta_lock_update(struct inode *inode,
1466 struct buffer_head **bh)
1468 int status = 0;
1469 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1470 struct ocfs2_lock_res *lockres;
1471 struct ocfs2_dinode *fe;
1473 mlog_entry_void();
1475 spin_lock(&oi->ip_lock);
1476 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1477 mlog(0, "Orphaned inode %llu was deleted while we "
1478 "were waiting on a lock. ip_flags = 0x%x\n",
1479 (unsigned long long)oi->ip_blkno, oi->ip_flags);
1480 spin_unlock(&oi->ip_lock);
1481 status = -ENOENT;
1482 goto bail;
1484 spin_unlock(&oi->ip_lock);
1486 lockres = &oi->ip_meta_lockres;
1488 if (!ocfs2_should_refresh_lock_res(lockres))
1489 goto bail;
1491 /* This will discard any caching information we might have had
1492 * for the inode metadata. */
1493 ocfs2_metadata_cache_purge(inode);
1495 /* will do nothing for inode types that don't use the extent
1496 * map (directories, bitmap files, etc) */
1497 ocfs2_extent_map_trunc(inode, 0);
1499 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1500 mlog(0, "Trusting LVB on inode %llu\n",
1501 (unsigned long long)oi->ip_blkno);
1502 ocfs2_refresh_inode_from_lvb(inode);
1503 } else {
1504 /* Boo, we have to go to disk. */
1505 /* read bh, cast, ocfs2_refresh_inode */
1506 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1507 bh, OCFS2_BH_CACHED, inode);
1508 if (status < 0) {
1509 mlog_errno(status);
1510 goto bail_refresh;
1512 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1514 /* This is a good chance to make sure we're not
1515 * locking an invalid object.
1517 * We bug on a stale inode here because we checked
1518 * above whether it was wiped from disk. The wiping
1519 * node provides a guarantee that we receive that
1520 * message and can mark the inode before dropping any
1521 * locks associated with it. */
1522 if (!OCFS2_IS_VALID_DINODE(fe)) {
1523 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1524 status = -EIO;
1525 goto bail_refresh;
1527 mlog_bug_on_msg(inode->i_generation !=
1528 le32_to_cpu(fe->i_generation),
1529 "Invalid dinode %llu disk generation: %u "
1530 "inode->i_generation: %u\n",
1531 (unsigned long long)oi->ip_blkno,
1532 le32_to_cpu(fe->i_generation),
1533 inode->i_generation);
1534 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1535 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1536 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1537 (unsigned long long)oi->ip_blkno,
1538 (unsigned long long)le64_to_cpu(fe->i_dtime),
1539 le32_to_cpu(fe->i_flags));
1541 ocfs2_refresh_inode(inode, fe);
1544 status = 0;
1545 bail_refresh:
1546 ocfs2_complete_lock_res_refresh(lockres, status);
1547 bail:
1548 mlog_exit(status);
1549 return status;
1552 static int ocfs2_assign_bh(struct inode *inode,
1553 struct buffer_head **ret_bh,
1554 struct buffer_head *passed_bh)
1556 int status;
1558 if (passed_bh) {
1559 /* Ok, the update went to disk for us, use the
1560 * returned bh. */
1561 *ret_bh = passed_bh;
1562 get_bh(*ret_bh);
1564 return 0;
1567 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1568 OCFS2_I(inode)->ip_blkno,
1569 ret_bh,
1570 OCFS2_BH_CACHED,
1571 inode);
1572 if (status < 0)
1573 mlog_errno(status);
1575 return status;
1579 * returns < 0 error if the callback will never be called, otherwise
1580 * the result of the lock will be communicated via the callback.
1582 int ocfs2_meta_lock_full(struct inode *inode,
1583 struct buffer_head **ret_bh,
1584 int ex,
1585 int arg_flags)
1587 int status, level, dlm_flags, acquired;
1588 struct ocfs2_lock_res *lockres;
1589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct buffer_head *local_bh = NULL;
1592 BUG_ON(!inode);
1594 mlog_entry_void();
1596 mlog(0, "inode %llu, take %s META lock\n",
1597 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1598 ex ? "EXMODE" : "PRMODE");
1600 status = 0;
1601 acquired = 0;
1602 /* We'll allow faking a readonly metadata lock for
1603 * rodevices. */
1604 if (ocfs2_is_hard_readonly(osb)) {
1605 if (ex)
1606 status = -EROFS;
1607 goto bail;
1610 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1611 wait_event(osb->recovery_event,
1612 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1614 acquired = 0;
1615 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1616 level = ex ? LKM_EXMODE : LKM_PRMODE;
1617 dlm_flags = 0;
1618 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1619 dlm_flags |= LKM_NOQUEUE;
1621 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1622 if (status < 0) {
1623 if (status != -EAGAIN && status != -EIOCBRETRY)
1624 mlog_errno(status);
1625 goto bail;
1628 /* Notify the error cleanup path to drop the cluster lock. */
1629 acquired = 1;
1631 /* We wait twice because a node may have died while we were in
1632 * the lower dlm layers. The second time though, we've
1633 * committed to owning this lock so we don't allow signals to
1634 * abort the operation. */
1635 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1636 wait_event(osb->recovery_event,
1637 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1640 * We only see this flag if we're being called from
1641 * ocfs2_read_locked_inode(). It means we're locking an inode
1642 * which hasn't been populated yet, so clear the refresh flag
1643 * and let the caller handle it.
1645 if (inode->i_state & I_NEW) {
1646 status = 0;
1647 ocfs2_complete_lock_res_refresh(lockres, 0);
1648 goto bail;
1651 /* This is fun. The caller may want a bh back, or it may
1652 * not. ocfs2_meta_lock_update definitely wants one in, but
1653 * may or may not read one, depending on what's in the
1654 * LVB. The result of all of this is that we've *only* gone to
1655 * disk if we have to, so the complexity is worthwhile. */
1656 status = ocfs2_meta_lock_update(inode, &local_bh);
1657 if (status < 0) {
1658 if (status != -ENOENT)
1659 mlog_errno(status);
1660 goto bail;
1663 if (ret_bh) {
1664 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1665 if (status < 0) {
1666 mlog_errno(status);
1667 goto bail;
1671 bail:
1672 if (status < 0) {
1673 if (ret_bh && (*ret_bh)) {
1674 brelse(*ret_bh);
1675 *ret_bh = NULL;
1677 if (acquired)
1678 ocfs2_meta_unlock(inode, ex);
1681 if (local_bh)
1682 brelse(local_bh);
1684 mlog_exit(status);
1685 return status;
1689 * This is working around a lock inversion between tasks acquiring DLM locks
1690 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1691 * while acquiring page locks.
1693 * ** These _with_page variantes are only intended to be called from aop
1694 * methods that hold page locks and return a very specific *positive* error
1695 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1697 * The DLM is called such that it returns -EAGAIN if it would have blocked
1698 * waiting for the vote thread. In that case we unlock our page so the vote
1699 * thread can make progress. Once we've done this we have to return
1700 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1701 * into the VFS who will then immediately retry the aop call.
1703 * We do a blocking lock and immediate unlock before returning, though, so that
1704 * the lock has a great chance of being cached on this node by the time the VFS
1705 * calls back to retry the aop. This has a potential to livelock as nodes
1706 * ping locks back and forth, but that's a risk we're willing to take to avoid
1707 * the lock inversion simply.
1709 int ocfs2_meta_lock_with_page(struct inode *inode,
1710 struct buffer_head **ret_bh,
1711 int ex,
1712 struct page *page)
1714 int ret;
1716 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
1717 if (ret == -EAGAIN) {
1718 unlock_page(page);
1719 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
1720 ocfs2_meta_unlock(inode, ex);
1721 ret = AOP_TRUNCATED_PAGE;
1724 return ret;
1727 int ocfs2_meta_lock_atime(struct inode *inode,
1728 struct vfsmount *vfsmnt,
1729 int *level)
1731 int ret;
1733 mlog_entry_void();
1734 ret = ocfs2_meta_lock(inode, NULL, 0);
1735 if (ret < 0) {
1736 mlog_errno(ret);
1737 return ret;
1741 * If we should update atime, we will get EX lock,
1742 * otherwise we just get PR lock.
1744 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1745 struct buffer_head *bh = NULL;
1747 ocfs2_meta_unlock(inode, 0);
1748 ret = ocfs2_meta_lock(inode, &bh, 1);
1749 if (ret < 0) {
1750 mlog_errno(ret);
1751 return ret;
1753 *level = 1;
1754 if (ocfs2_should_update_atime(inode, vfsmnt))
1755 ocfs2_update_inode_atime(inode, bh);
1756 if (bh)
1757 brelse(bh);
1758 } else
1759 *level = 0;
1761 mlog_exit(ret);
1762 return ret;
1765 void ocfs2_meta_unlock(struct inode *inode,
1766 int ex)
1768 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1769 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1771 mlog_entry_void();
1773 mlog(0, "inode %llu drop %s META lock\n",
1774 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1775 ex ? "EXMODE" : "PRMODE");
1777 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1778 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1780 mlog_exit_void();
1783 int ocfs2_super_lock(struct ocfs2_super *osb,
1784 int ex)
1786 int status;
1787 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1788 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1789 struct buffer_head *bh;
1790 struct ocfs2_slot_info *si = osb->slot_info;
1792 mlog_entry_void();
1794 if (ocfs2_is_hard_readonly(osb))
1795 return -EROFS;
1797 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1798 if (status < 0) {
1799 mlog_errno(status);
1800 goto bail;
1803 /* The super block lock path is really in the best position to
1804 * know when resources covered by the lock need to be
1805 * refreshed, so we do it here. Of course, making sense of
1806 * everything is up to the caller :) */
1807 status = ocfs2_should_refresh_lock_res(lockres);
1808 if (status < 0) {
1809 mlog_errno(status);
1810 goto bail;
1812 if (status) {
1813 bh = si->si_bh;
1814 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1815 si->si_inode);
1816 if (status == 0)
1817 ocfs2_update_slot_info(si);
1819 ocfs2_complete_lock_res_refresh(lockres, status);
1821 if (status < 0)
1822 mlog_errno(status);
1824 bail:
1825 mlog_exit(status);
1826 return status;
1829 void ocfs2_super_unlock(struct ocfs2_super *osb,
1830 int ex)
1832 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1833 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1835 ocfs2_cluster_unlock(osb, lockres, level);
1838 int ocfs2_rename_lock(struct ocfs2_super *osb)
1840 int status;
1841 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1843 if (ocfs2_is_hard_readonly(osb))
1844 return -EROFS;
1846 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1847 if (status < 0)
1848 mlog_errno(status);
1850 return status;
1853 void ocfs2_rename_unlock(struct ocfs2_super *osb)
1855 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1857 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1860 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1862 int ret;
1863 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1864 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1865 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1867 BUG_ON(!dl);
1869 if (ocfs2_is_hard_readonly(osb))
1870 return -EROFS;
1872 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1873 if (ret < 0)
1874 mlog_errno(ret);
1876 return ret;
1879 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1881 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1882 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1883 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1885 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1888 /* Reference counting of the dlm debug structure. We want this because
1889 * open references on the debug inodes can live on after a mount, so
1890 * we can't rely on the ocfs2_super to always exist. */
1891 static void ocfs2_dlm_debug_free(struct kref *kref)
1893 struct ocfs2_dlm_debug *dlm_debug;
1895 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1897 kfree(dlm_debug);
1900 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1902 if (dlm_debug)
1903 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1906 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1908 kref_get(&debug->d_refcnt);
1911 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1913 struct ocfs2_dlm_debug *dlm_debug;
1915 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1916 if (!dlm_debug) {
1917 mlog_errno(-ENOMEM);
1918 goto out;
1921 kref_init(&dlm_debug->d_refcnt);
1922 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1923 dlm_debug->d_locking_state = NULL;
1924 out:
1925 return dlm_debug;
1928 /* Access to this is arbitrated for us via seq_file->sem. */
1929 struct ocfs2_dlm_seq_priv {
1930 struct ocfs2_dlm_debug *p_dlm_debug;
1931 struct ocfs2_lock_res p_iter_res;
1932 struct ocfs2_lock_res p_tmp_res;
1935 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1936 struct ocfs2_dlm_seq_priv *priv)
1938 struct ocfs2_lock_res *iter, *ret = NULL;
1939 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1941 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1943 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1944 /* discover the head of the list */
1945 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1946 mlog(0, "End of list found, %p\n", ret);
1947 break;
1950 /* We track our "dummy" iteration lockres' by a NULL
1951 * l_ops field. */
1952 if (iter->l_ops != NULL) {
1953 ret = iter;
1954 break;
1958 return ret;
1961 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1963 struct ocfs2_dlm_seq_priv *priv = m->private;
1964 struct ocfs2_lock_res *iter;
1966 spin_lock(&ocfs2_dlm_tracking_lock);
1967 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1968 if (iter) {
1969 /* Since lockres' have the lifetime of their container
1970 * (which can be inodes, ocfs2_supers, etc) we want to
1971 * copy this out to a temporary lockres while still
1972 * under the spinlock. Obviously after this we can't
1973 * trust any pointers on the copy returned, but that's
1974 * ok as the information we want isn't typically held
1975 * in them. */
1976 priv->p_tmp_res = *iter;
1977 iter = &priv->p_tmp_res;
1979 spin_unlock(&ocfs2_dlm_tracking_lock);
1981 return iter;
1984 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1988 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1990 struct ocfs2_dlm_seq_priv *priv = m->private;
1991 struct ocfs2_lock_res *iter = v;
1992 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1994 spin_lock(&ocfs2_dlm_tracking_lock);
1995 iter = ocfs2_dlm_next_res(iter, priv);
1996 list_del_init(&dummy->l_debug_list);
1997 if (iter) {
1998 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1999 priv->p_tmp_res = *iter;
2000 iter = &priv->p_tmp_res;
2002 spin_unlock(&ocfs2_dlm_tracking_lock);
2004 return iter;
2007 /* So that debugfs.ocfs2 can determine which format is being used */
2008 #define OCFS2_DLM_DEBUG_STR_VERSION 1
2009 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2011 int i;
2012 char *lvb;
2013 struct ocfs2_lock_res *lockres = v;
2015 if (!lockres)
2016 return -EINVAL;
2018 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2020 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2021 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2022 lockres->l_name,
2023 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2024 else
2025 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2027 seq_printf(m, "%d\t"
2028 "0x%lx\t"
2029 "0x%x\t"
2030 "0x%x\t"
2031 "%u\t"
2032 "%u\t"
2033 "%d\t"
2034 "%d\t",
2035 lockres->l_level,
2036 lockres->l_flags,
2037 lockres->l_action,
2038 lockres->l_unlock_action,
2039 lockres->l_ro_holders,
2040 lockres->l_ex_holders,
2041 lockres->l_requested,
2042 lockres->l_blocking);
2044 /* Dump the raw LVB */
2045 lvb = lockres->l_lksb.lvb;
2046 for(i = 0; i < DLM_LVB_LEN; i++)
2047 seq_printf(m, "0x%x\t", lvb[i]);
2049 /* End the line */
2050 seq_printf(m, "\n");
2051 return 0;
2054 static struct seq_operations ocfs2_dlm_seq_ops = {
2055 .start = ocfs2_dlm_seq_start,
2056 .stop = ocfs2_dlm_seq_stop,
2057 .next = ocfs2_dlm_seq_next,
2058 .show = ocfs2_dlm_seq_show,
2061 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2063 struct seq_file *seq = (struct seq_file *) file->private_data;
2064 struct ocfs2_dlm_seq_priv *priv = seq->private;
2065 struct ocfs2_lock_res *res = &priv->p_iter_res;
2067 ocfs2_remove_lockres_tracking(res);
2068 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2069 return seq_release_private(inode, file);
2072 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2074 int ret;
2075 struct ocfs2_dlm_seq_priv *priv;
2076 struct seq_file *seq;
2077 struct ocfs2_super *osb;
2079 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2080 if (!priv) {
2081 ret = -ENOMEM;
2082 mlog_errno(ret);
2083 goto out;
2085 osb = inode->i_private;
2086 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2087 priv->p_dlm_debug = osb->osb_dlm_debug;
2088 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2090 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2091 if (ret) {
2092 kfree(priv);
2093 mlog_errno(ret);
2094 goto out;
2097 seq = (struct seq_file *) file->private_data;
2098 seq->private = priv;
2100 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2101 priv->p_dlm_debug);
2103 out:
2104 return ret;
2107 static const struct file_operations ocfs2_dlm_debug_fops = {
2108 .open = ocfs2_dlm_debug_open,
2109 .release = ocfs2_dlm_debug_release,
2110 .read = seq_read,
2111 .llseek = seq_lseek,
2114 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2116 int ret = 0;
2117 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2119 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2120 S_IFREG|S_IRUSR,
2121 osb->osb_debug_root,
2122 osb,
2123 &ocfs2_dlm_debug_fops);
2124 if (!dlm_debug->d_locking_state) {
2125 ret = -EINVAL;
2126 mlog(ML_ERROR,
2127 "Unable to create locking state debugfs file.\n");
2128 goto out;
2131 ocfs2_get_dlm_debug(dlm_debug);
2132 out:
2133 return ret;
2136 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2138 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2140 if (dlm_debug) {
2141 debugfs_remove(dlm_debug->d_locking_state);
2142 ocfs2_put_dlm_debug(dlm_debug);
2146 int ocfs2_dlm_init(struct ocfs2_super *osb)
2148 int status;
2149 u32 dlm_key;
2150 struct dlm_ctxt *dlm;
2152 mlog_entry_void();
2154 status = ocfs2_dlm_init_debug(osb);
2155 if (status < 0) {
2156 mlog_errno(status);
2157 goto bail;
2160 /* launch vote thread */
2161 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2162 if (IS_ERR(osb->vote_task)) {
2163 status = PTR_ERR(osb->vote_task);
2164 osb->vote_task = NULL;
2165 mlog_errno(status);
2166 goto bail;
2169 /* used by the dlm code to make message headers unique, each
2170 * node in this domain must agree on this. */
2171 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2173 /* for now, uuid == domain */
2174 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2175 if (IS_ERR(dlm)) {
2176 status = PTR_ERR(dlm);
2177 mlog_errno(status);
2178 goto bail;
2181 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2182 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2184 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2186 osb->dlm = dlm;
2188 status = 0;
2189 bail:
2190 if (status < 0) {
2191 ocfs2_dlm_shutdown_debug(osb);
2192 if (osb->vote_task)
2193 kthread_stop(osb->vote_task);
2196 mlog_exit(status);
2197 return status;
2200 void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2202 mlog_entry_void();
2204 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2206 ocfs2_drop_osb_locks(osb);
2208 if (osb->vote_task) {
2209 kthread_stop(osb->vote_task);
2210 osb->vote_task = NULL;
2213 ocfs2_lock_res_free(&osb->osb_super_lockres);
2214 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2216 dlm_unregister_domain(osb->dlm);
2217 osb->dlm = NULL;
2219 ocfs2_dlm_shutdown_debug(osb);
2221 mlog_exit_void();
2224 static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
2226 struct ocfs2_lock_res *lockres = opaque;
2227 unsigned long flags;
2229 mlog_entry_void();
2231 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2232 lockres->l_unlock_action);
2234 spin_lock_irqsave(&lockres->l_lock, flags);
2235 /* We tried to cancel a convert request, but it was already
2236 * granted. All we want to do here is clear our unlock
2237 * state. The wake_up call done at the bottom is redundant
2238 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2239 * hurt anything anyway */
2240 if (status == DLM_CANCELGRANT &&
2241 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2242 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2244 /* We don't clear the busy flag in this case as it
2245 * should have been cleared by the ast which the dlm
2246 * has called. */
2247 goto complete_unlock;
2250 if (status != DLM_NORMAL) {
2251 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2252 "unlock_action %d\n", status, lockres->l_name,
2253 lockres->l_unlock_action);
2254 spin_unlock_irqrestore(&lockres->l_lock, flags);
2255 return;
2258 switch(lockres->l_unlock_action) {
2259 case OCFS2_UNLOCK_CANCEL_CONVERT:
2260 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2261 lockres->l_action = OCFS2_AST_INVALID;
2262 break;
2263 case OCFS2_UNLOCK_DROP_LOCK:
2264 lockres->l_level = LKM_IVMODE;
2265 break;
2266 default:
2267 BUG();
2270 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2271 complete_unlock:
2272 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2273 spin_unlock_irqrestore(&lockres->l_lock, flags);
2275 wake_up(&lockres->l_event);
2277 mlog_exit_void();
2280 static int ocfs2_drop_lock(struct ocfs2_super *osb,
2281 struct ocfs2_lock_res *lockres)
2283 enum dlm_status status;
2284 unsigned long flags;
2285 int lkm_flags = 0;
2287 /* We didn't get anywhere near actually using this lockres. */
2288 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2289 goto out;
2291 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2292 lkm_flags |= LKM_VALBLK;
2294 spin_lock_irqsave(&lockres->l_lock, flags);
2296 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2297 "lockres %s, flags 0x%lx\n",
2298 lockres->l_name, lockres->l_flags);
2300 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2301 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2302 "%u, unlock_action = %u\n",
2303 lockres->l_name, lockres->l_flags, lockres->l_action,
2304 lockres->l_unlock_action);
2306 spin_unlock_irqrestore(&lockres->l_lock, flags);
2308 /* XXX: Today we just wait on any busy
2309 * locks... Perhaps we need to cancel converts in the
2310 * future? */
2311 ocfs2_wait_on_busy_lock(lockres);
2313 spin_lock_irqsave(&lockres->l_lock, flags);
2316 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2317 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2318 lockres->l_level == LKM_EXMODE &&
2319 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2320 lockres->l_ops->set_lvb(lockres);
2323 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2324 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2325 lockres->l_name);
2326 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2327 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2329 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2330 spin_unlock_irqrestore(&lockres->l_lock, flags);
2331 goto out;
2334 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2336 /* make sure we never get here while waiting for an ast to
2337 * fire. */
2338 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2340 /* is this necessary? */
2341 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2342 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2343 spin_unlock_irqrestore(&lockres->l_lock, flags);
2345 mlog(0, "lock %s\n", lockres->l_name);
2347 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2348 ocfs2_unlock_ast, lockres);
2349 if (status != DLM_NORMAL) {
2350 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2351 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2352 dlm_print_one_lock(lockres->l_lksb.lockid);
2353 BUG();
2355 mlog(0, "lock %s, successfull return from dlmunlock\n",
2356 lockres->l_name);
2358 ocfs2_wait_on_busy_lock(lockres);
2359 out:
2360 mlog_exit(0);
2361 return 0;
2364 /* Mark the lockres as being dropped. It will no longer be
2365 * queued if blocking, but we still may have to wait on it
2366 * being dequeued from the vote thread before we can consider
2367 * it safe to drop.
2369 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2370 void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2372 int status;
2373 struct ocfs2_mask_waiter mw;
2374 unsigned long flags;
2376 ocfs2_init_mask_waiter(&mw);
2378 spin_lock_irqsave(&lockres->l_lock, flags);
2379 lockres->l_flags |= OCFS2_LOCK_FREEING;
2380 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2381 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2382 spin_unlock_irqrestore(&lockres->l_lock, flags);
2384 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2386 status = ocfs2_wait_for_mask(&mw);
2387 if (status)
2388 mlog_errno(status);
2390 spin_lock_irqsave(&lockres->l_lock, flags);
2392 spin_unlock_irqrestore(&lockres->l_lock, flags);
2395 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2396 struct ocfs2_lock_res *lockres)
2398 int ret;
2400 ocfs2_mark_lockres_freeing(lockres);
2401 ret = ocfs2_drop_lock(osb, lockres);
2402 if (ret)
2403 mlog_errno(ret);
2406 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2408 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2409 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2412 int ocfs2_drop_inode_locks(struct inode *inode)
2414 int status, err;
2416 mlog_entry_void();
2418 /* No need to call ocfs2_mark_lockres_freeing here -
2419 * ocfs2_clear_inode has done it for us. */
2421 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2422 &OCFS2_I(inode)->ip_data_lockres);
2423 if (err < 0)
2424 mlog_errno(err);
2426 status = err;
2428 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2429 &OCFS2_I(inode)->ip_meta_lockres);
2430 if (err < 0)
2431 mlog_errno(err);
2432 if (err < 0 && !status)
2433 status = err;
2435 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2436 &OCFS2_I(inode)->ip_rw_lockres);
2437 if (err < 0)
2438 mlog_errno(err);
2439 if (err < 0 && !status)
2440 status = err;
2442 mlog_exit(status);
2443 return status;
2446 static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2447 int new_level)
2449 assert_spin_locked(&lockres->l_lock);
2451 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2453 if (lockres->l_level <= new_level) {
2454 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2455 lockres->l_level, new_level);
2456 BUG();
2459 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2460 lockres->l_name, new_level, lockres->l_blocking);
2462 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2463 lockres->l_requested = new_level;
2464 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2467 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2468 struct ocfs2_lock_res *lockres,
2469 int new_level,
2470 int lvb)
2472 int ret, dlm_flags = LKM_CONVERT;
2473 enum dlm_status status;
2475 mlog_entry_void();
2477 if (lvb)
2478 dlm_flags |= LKM_VALBLK;
2480 status = dlmlock(osb->dlm,
2481 new_level,
2482 &lockres->l_lksb,
2483 dlm_flags,
2484 lockres->l_name,
2485 OCFS2_LOCK_ID_MAX_LEN - 1,
2486 ocfs2_locking_ast,
2487 lockres,
2488 ocfs2_blocking_ast);
2489 if (status != DLM_NORMAL) {
2490 ocfs2_log_dlm_error("dlmlock", status, lockres);
2491 ret = -EINVAL;
2492 ocfs2_recover_from_dlm_error(lockres, 1);
2493 goto bail;
2496 ret = 0;
2497 bail:
2498 mlog_exit(ret);
2499 return ret;
2502 /* returns 1 when the caller should unlock and call dlmunlock */
2503 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2504 struct ocfs2_lock_res *lockres)
2506 assert_spin_locked(&lockres->l_lock);
2508 mlog_entry_void();
2509 mlog(0, "lock %s\n", lockres->l_name);
2511 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2512 /* If we're already trying to cancel a lock conversion
2513 * then just drop the spinlock and allow the caller to
2514 * requeue this lock. */
2516 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2517 return 0;
2520 /* were we in a convert when we got the bast fire? */
2521 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2522 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2523 /* set things up for the unlockast to know to just
2524 * clear out the ast_action and unset busy, etc. */
2525 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2527 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2528 "lock %s, invalid flags: 0x%lx\n",
2529 lockres->l_name, lockres->l_flags);
2531 return 1;
2534 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2535 struct ocfs2_lock_res *lockres)
2537 int ret;
2538 enum dlm_status status;
2540 mlog_entry_void();
2541 mlog(0, "lock %s\n", lockres->l_name);
2543 ret = 0;
2544 status = dlmunlock(osb->dlm,
2545 &lockres->l_lksb,
2546 LKM_CANCEL,
2547 ocfs2_unlock_ast,
2548 lockres);
2549 if (status != DLM_NORMAL) {
2550 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2551 ret = -EINVAL;
2552 ocfs2_recover_from_dlm_error(lockres, 0);
2555 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2557 mlog_exit(ret);
2558 return ret;
2561 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2562 struct ocfs2_lock_res *lockres,
2563 struct ocfs2_unblock_ctl *ctl)
2565 unsigned long flags;
2566 int blocking;
2567 int new_level;
2568 int ret = 0;
2569 int set_lvb = 0;
2571 mlog_entry_void();
2573 spin_lock_irqsave(&lockres->l_lock, flags);
2575 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2577 recheck:
2578 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2579 ctl->requeue = 1;
2580 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2581 spin_unlock_irqrestore(&lockres->l_lock, flags);
2582 if (ret) {
2583 ret = ocfs2_cancel_convert(osb, lockres);
2584 if (ret < 0)
2585 mlog_errno(ret);
2587 goto leave;
2590 /* if we're blocking an exclusive and we have *any* holders,
2591 * then requeue. */
2592 if ((lockres->l_blocking == LKM_EXMODE)
2593 && (lockres->l_ex_holders || lockres->l_ro_holders))
2594 goto leave_requeue;
2596 /* If it's a PR we're blocking, then only
2597 * requeue if we've got any EX holders */
2598 if (lockres->l_blocking == LKM_PRMODE &&
2599 lockres->l_ex_holders)
2600 goto leave_requeue;
2603 * Can we get a lock in this state if the holder counts are
2604 * zero? The meta data unblock code used to check this.
2606 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2607 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2608 goto leave_requeue;
2610 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2612 if (lockres->l_ops->check_downconvert
2613 && !lockres->l_ops->check_downconvert(lockres, new_level))
2614 goto leave_requeue;
2616 /* If we get here, then we know that there are no more
2617 * incompatible holders (and anyone asking for an incompatible
2618 * lock is blocked). We can now downconvert the lock */
2619 if (!lockres->l_ops->downconvert_worker)
2620 goto downconvert;
2622 /* Some lockres types want to do a bit of work before
2623 * downconverting a lock. Allow that here. The worker function
2624 * may sleep, so we save off a copy of what we're blocking as
2625 * it may change while we're not holding the spin lock. */
2626 blocking = lockres->l_blocking;
2627 spin_unlock_irqrestore(&lockres->l_lock, flags);
2629 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
2631 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2632 goto leave;
2634 spin_lock_irqsave(&lockres->l_lock, flags);
2635 if (blocking != lockres->l_blocking) {
2636 /* If this changed underneath us, then we can't drop
2637 * it just yet. */
2638 goto recheck;
2641 downconvert:
2642 ctl->requeue = 0;
2644 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2645 if (lockres->l_level == LKM_EXMODE)
2646 set_lvb = 1;
2649 * We only set the lvb if the lock has been fully
2650 * refreshed - otherwise we risk setting stale
2651 * data. Otherwise, there's no need to actually clear
2652 * out the lvb here as it's value is still valid.
2654 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2655 lockres->l_ops->set_lvb(lockres);
2658 ocfs2_prepare_downconvert(lockres, new_level);
2659 spin_unlock_irqrestore(&lockres->l_lock, flags);
2660 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2661 leave:
2662 mlog_exit(ret);
2663 return ret;
2665 leave_requeue:
2666 spin_unlock_irqrestore(&lockres->l_lock, flags);
2667 ctl->requeue = 1;
2669 mlog_exit(0);
2670 return 0;
2673 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2674 int blocking)
2676 struct inode *inode;
2677 struct address_space *mapping;
2679 inode = ocfs2_lock_res_inode(lockres);
2680 mapping = inode->i_mapping;
2682 if (filemap_fdatawrite(mapping)) {
2683 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2684 (unsigned long long)OCFS2_I(inode)->ip_blkno);
2686 sync_mapping_buffers(mapping);
2687 if (blocking == LKM_EXMODE) {
2688 truncate_inode_pages(mapping, 0);
2689 unmap_mapping_range(mapping, 0, 0, 0);
2690 } else {
2691 /* We only need to wait on the I/O if we're not also
2692 * truncating pages because truncate_inode_pages waits
2693 * for us above. We don't truncate pages if we're
2694 * blocking anything < EXMODE because we want to keep
2695 * them around in that case. */
2696 filemap_fdatawait(mapping);
2699 return UNBLOCK_CONTINUE;
2702 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2703 int new_level)
2705 struct inode *inode = ocfs2_lock_res_inode(lockres);
2706 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2708 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2709 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2711 if (checkpointed)
2712 return 1;
2714 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2715 return 0;
2718 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2720 struct inode *inode = ocfs2_lock_res_inode(lockres);
2722 __ocfs2_stuff_meta_lvb(inode);
2726 * Does the final reference drop on our dentry lock. Right now this
2727 * happens in the vote thread, but we could choose to simplify the
2728 * dlmglue API and push these off to the ocfs2_wq in the future.
2730 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2731 struct ocfs2_lock_res *lockres)
2733 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2734 ocfs2_dentry_lock_put(osb, dl);
2738 * d_delete() matching dentries before the lock downconvert.
2740 * At this point, any process waiting to destroy the
2741 * dentry_lock due to last ref count is stopped by the
2742 * OCFS2_LOCK_QUEUED flag.
2744 * We have two potential problems
2746 * 1) If we do the last reference drop on our dentry_lock (via dput)
2747 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2748 * the downconvert to finish. Instead we take an elevated
2749 * reference and push the drop until after we've completed our
2750 * unblock processing.
2752 * 2) There might be another process with a final reference,
2753 * waiting on us to finish processing. If this is the case, we
2754 * detect it and exit out - there's no more dentries anyway.
2756 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2757 int blocking)
2759 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2760 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2761 struct dentry *dentry;
2762 unsigned long flags;
2763 int extra_ref = 0;
2766 * This node is blocking another node from getting a read
2767 * lock. This happens when we've renamed within a
2768 * directory. We've forced the other nodes to d_delete(), but
2769 * we never actually dropped our lock because it's still
2770 * valid. The downconvert code will retain a PR for this node,
2771 * so there's no further work to do.
2773 if (blocking == LKM_PRMODE)
2774 return UNBLOCK_CONTINUE;
2777 * Mark this inode as potentially orphaned. The code in
2778 * ocfs2_delete_inode() will figure out whether it actually
2779 * needs to be freed or not.
2781 spin_lock(&oi->ip_lock);
2782 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2783 spin_unlock(&oi->ip_lock);
2786 * Yuck. We need to make sure however that the check of
2787 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2788 * respect to a reference decrement or the setting of that
2789 * flag.
2791 spin_lock_irqsave(&lockres->l_lock, flags);
2792 spin_lock(&dentry_attach_lock);
2793 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2794 && dl->dl_count) {
2795 dl->dl_count++;
2796 extra_ref = 1;
2798 spin_unlock(&dentry_attach_lock);
2799 spin_unlock_irqrestore(&lockres->l_lock, flags);
2801 mlog(0, "extra_ref = %d\n", extra_ref);
2804 * We have a process waiting on us in ocfs2_dentry_iput(),
2805 * which means we can't have any more outstanding
2806 * aliases. There's no need to do any more work.
2808 if (!extra_ref)
2809 return UNBLOCK_CONTINUE;
2811 spin_lock(&dentry_attach_lock);
2812 while (1) {
2813 dentry = ocfs2_find_local_alias(dl->dl_inode,
2814 dl->dl_parent_blkno, 1);
2815 if (!dentry)
2816 break;
2817 spin_unlock(&dentry_attach_lock);
2819 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2820 dentry->d_name.name);
2823 * The following dcache calls may do an
2824 * iput(). Normally we don't want that from the
2825 * downconverting thread, but in this case it's ok
2826 * because the requesting node already has an
2827 * exclusive lock on the inode, so it can't be queued
2828 * for a downconvert.
2830 d_delete(dentry);
2831 dput(dentry);
2833 spin_lock(&dentry_attach_lock);
2835 spin_unlock(&dentry_attach_lock);
2838 * If we are the last holder of this dentry lock, there is no
2839 * reason to downconvert so skip straight to the unlock.
2841 if (dl->dl_count == 1)
2842 return UNBLOCK_STOP_POST;
2844 return UNBLOCK_CONTINUE_POST;
2847 void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2848 struct ocfs2_lock_res *lockres)
2850 int status;
2851 struct ocfs2_unblock_ctl ctl = {0, 0,};
2852 unsigned long flags;
2854 /* Our reference to the lockres in this function can be
2855 * considered valid until we remove the OCFS2_LOCK_QUEUED
2856 * flag. */
2858 mlog_entry_void();
2860 BUG_ON(!lockres);
2861 BUG_ON(!lockres->l_ops);
2863 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2865 /* Detect whether a lock has been marked as going away while
2866 * the vote thread was processing other things. A lock can
2867 * still be marked with OCFS2_LOCK_FREEING after this check,
2868 * but short circuiting here will still save us some
2869 * performance. */
2870 spin_lock_irqsave(&lockres->l_lock, flags);
2871 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2872 goto unqueue;
2873 spin_unlock_irqrestore(&lockres->l_lock, flags);
2875 status = ocfs2_unblock_lock(osb, lockres, &ctl);
2876 if (status < 0)
2877 mlog_errno(status);
2879 spin_lock_irqsave(&lockres->l_lock, flags);
2880 unqueue:
2881 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
2882 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2883 } else
2884 ocfs2_schedule_blocked_lock(osb, lockres);
2886 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
2887 ctl.requeue ? "yes" : "no");
2888 spin_unlock_irqrestore(&lockres->l_lock, flags);
2890 if (ctl.unblock_action != UNBLOCK_CONTINUE
2891 && lockres->l_ops->post_unlock)
2892 lockres->l_ops->post_unlock(osb, lockres);
2894 mlog_exit_void();
2897 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2898 struct ocfs2_lock_res *lockres)
2900 mlog_entry_void();
2902 assert_spin_locked(&lockres->l_lock);
2904 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2905 /* Do not schedule a lock for downconvert when it's on
2906 * the way to destruction - any nodes wanting access
2907 * to the resource will get it soon. */
2908 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2909 lockres->l_name, lockres->l_flags);
2910 return;
2913 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2915 spin_lock(&osb->vote_task_lock);
2916 if (list_empty(&lockres->l_blocked_list)) {
2917 list_add_tail(&lockres->l_blocked_list,
2918 &osb->blocked_lock_list);
2919 osb->blocked_lock_count++;
2921 spin_unlock(&osb->vote_task_lock);
2923 mlog_exit_void();
2926 /* This aids in debugging situations where a bad LVB might be involved. */
2927 void ocfs2_dump_meta_lvb_info(u64 level,
2928 const char *function,
2929 unsigned int line,
2930 struct ocfs2_lock_res *lockres)
2932 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2934 mlog(level, "LVB information for %s (called from %s:%u):\n",
2935 lockres->l_name, function, line);
2936 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2937 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2938 be32_to_cpu(lvb->lvb_igeneration));
2939 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2940 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2941 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2942 be16_to_cpu(lvb->lvb_imode));
2943 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
2944 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
2945 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2946 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
2947 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2948 be32_to_cpu(lvb->lvb_iattr));