dlmglue.c revision f9e2d82e6395cfa0802446b54b63cc412089d82c
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmglue.c
5 *
6 * Code which implements an OCFS2 specific interface to our DLM.
7 *
8 * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
9 *
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.
14 *
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.
19 *
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.
24 */
25
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>
36
37#include <cluster/heartbeat.h>
38#include <cluster/nodemanager.h>
39#include <cluster/tcp.h>
40
41#include <dlm/dlmapi.h>
42
43#define MLOG_MASK_PREFIX ML_DLM_GLUE
44#include <cluster/masklog.h>
45
46#include "ocfs2.h"
47
48#include "alloc.h"
49#include "dcache.h"
50#include "dlmglue.h"
51#include "extent_map.h"
52#include "heartbeat.h"
53#include "inode.h"
54#include "journal.h"
55#include "slot_map.h"
56#include "super.h"
57#include "uptodate.h"
58#include "vote.h"
59
60#include "buffer_head_io.h"
61
62struct ocfs2_mask_waiter {
63	struct list_head	mw_item;
64	int			mw_status;
65	struct completion	mw_complete;
66	unsigned long		mw_mask;
67	unsigned long		mw_goal;
68};
69
70static void ocfs2_inode_ast_func(void *opaque);
71static void ocfs2_inode_bast_func(void *opaque,
72				  int level);
73static void ocfs2_dentry_ast_func(void *opaque);
74static void ocfs2_dentry_bast_func(void *opaque,
75				  int level);
76static void ocfs2_super_ast_func(void *opaque);
77static void ocfs2_super_bast_func(void *opaque,
78				  int level);
79static void ocfs2_rename_ast_func(void *opaque);
80static void ocfs2_rename_bast_func(void *opaque,
81				   int level);
82
83/*
84 * Return value from ocfs2_convert_worker_t functions.
85 *
86 * These control the precise actions of ocfs2_generic_unblock_lock()
87 * and ocfs2_process_blocked_lock()
88 *
89 */
90enum ocfs2_unblock_action {
91	UNBLOCK_CONTINUE	= 0, /* Continue downconvert */
92	UNBLOCK_CONTINUE_POST	= 1, /* Continue downconvert, fire
93				      * ->post_unlock callback */
94	UNBLOCK_STOP_POST	= 2, /* Do not downconvert, fire
95				      * ->post_unlock() callback. */
96};
97
98struct ocfs2_unblock_ctl {
99	int requeue;
100	enum ocfs2_unblock_action unblock_action;
101};
102
103/* so far, all locks have gotten along with the same unlock ast */
104static void ocfs2_unlock_ast_func(void *opaque,
105				  enum dlm_status status);
106static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
107			      struct ocfs2_unblock_ctl *ctl);
108static int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
109			      struct ocfs2_unblock_ctl *ctl);
110static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
111				    struct ocfs2_unblock_ctl *ctl);
112static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
113				     struct ocfs2_unblock_ctl *ctl);
114static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
115				  struct ocfs2_unblock_ctl *ctl);
116
117static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
118				     struct ocfs2_lock_res *lockres);
119
120struct ocfs2_lock_res_ops {
121	void (*ast)(void *);
122	void (*bast)(void *, int);
123	void (*unlock_ast)(void *, enum dlm_status);
124	int  (*unblock)(struct ocfs2_lock_res *, struct ocfs2_unblock_ctl *);
125	void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
126};
127
128typedef int (ocfs2_convert_worker_t)(struct ocfs2_lock_res *, int);
129static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
130				      struct ocfs2_lock_res *lockres,
131				      struct ocfs2_unblock_ctl *ctl,
132				      ocfs2_convert_worker_t *worker);
133
134static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
135	.ast		= ocfs2_inode_ast_func,
136	.bast		= ocfs2_inode_bast_func,
137	.unlock_ast	= ocfs2_unlock_ast_func,
138	.unblock	= ocfs2_unblock_inode_lock,
139};
140
141static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
142	.ast		= ocfs2_inode_ast_func,
143	.bast		= ocfs2_inode_bast_func,
144	.unlock_ast	= ocfs2_unlock_ast_func,
145	.unblock	= ocfs2_unblock_meta,
146};
147
148static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
149	.ast		= ocfs2_inode_ast_func,
150	.bast		= ocfs2_inode_bast_func,
151	.unlock_ast	= ocfs2_unlock_ast_func,
152	.unblock	= ocfs2_unblock_data,
153};
154
155static struct ocfs2_lock_res_ops ocfs2_super_lops = {
156	.ast		= ocfs2_super_ast_func,
157	.bast		= ocfs2_super_bast_func,
158	.unlock_ast	= ocfs2_unlock_ast_func,
159	.unblock	= ocfs2_unblock_osb_lock,
160};
161
162static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
163	.ast		= ocfs2_rename_ast_func,
164	.bast		= ocfs2_rename_bast_func,
165	.unlock_ast	= ocfs2_unlock_ast_func,
166	.unblock	= ocfs2_unblock_osb_lock,
167};
168
169static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
170	.ast		= ocfs2_dentry_ast_func,
171	.bast		= ocfs2_dentry_bast_func,
172	.unlock_ast	= ocfs2_unlock_ast_func,
173	.unblock	= ocfs2_unblock_dentry_lock,
174	.post_unlock	= ocfs2_dentry_post_unlock,
175};
176
177static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
178{
179	return lockres->l_type == OCFS2_LOCK_TYPE_META ||
180		lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
181		lockres->l_type == OCFS2_LOCK_TYPE_RW;
182}
183
184static inline int ocfs2_is_super_lock(struct ocfs2_lock_res *lockres)
185{
186	return lockres->l_type == OCFS2_LOCK_TYPE_SUPER;
187}
188
189static inline int ocfs2_is_rename_lock(struct ocfs2_lock_res *lockres)
190{
191	return lockres->l_type == OCFS2_LOCK_TYPE_RENAME;
192}
193
194static inline struct ocfs2_super *ocfs2_lock_res_super(struct ocfs2_lock_res *lockres)
195{
196	BUG_ON(!ocfs2_is_super_lock(lockres)
197	       && !ocfs2_is_rename_lock(lockres));
198
199	return (struct ocfs2_super *) lockres->l_priv;
200}
201
202static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
203{
204	BUG_ON(!ocfs2_is_inode_lock(lockres));
205
206	return (struct inode *) lockres->l_priv;
207}
208
209static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
210{
211	BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
212
213	return (struct ocfs2_dentry_lock *)lockres->l_priv;
214}
215
216static int ocfs2_lock_create(struct ocfs2_super *osb,
217			     struct ocfs2_lock_res *lockres,
218			     int level,
219			     int dlm_flags);
220static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
221						     int wanted);
222static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
223				 struct ocfs2_lock_res *lockres,
224				 int level);
225static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
226static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
227static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
228static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
229static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
230					struct ocfs2_lock_res *lockres);
231static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
232						int convert);
233#define ocfs2_log_dlm_error(_func, _stat, _lockres) do {	\
234	mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on "	\
235		"resource %s: %s\n", dlm_errname(_stat), _func,	\
236		_lockres->l_name, dlm_errmsg(_stat));		\
237} while (0)
238static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
239				 struct ocfs2_lock_res *lockres);
240static int ocfs2_meta_lock_update(struct inode *inode,
241				  struct buffer_head **bh);
242static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
243static inline int ocfs2_highest_compat_lock_level(int level);
244static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
245						  struct ocfs2_lock_res *lockres,
246						  int new_level);
247
248static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
249				  u64 blkno,
250				  u32 generation,
251				  char *name)
252{
253	int len;
254
255	mlog_entry_void();
256
257	BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
258
259	len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
260		       ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
261		       (long long)blkno, generation);
262
263	BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
264
265	mlog(0, "built lock resource with name: %s\n", name);
266
267	mlog_exit_void();
268}
269
270static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
271
272static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
273				       struct ocfs2_dlm_debug *dlm_debug)
274{
275	mlog(0, "Add tracking for lockres %s\n", res->l_name);
276
277	spin_lock(&ocfs2_dlm_tracking_lock);
278	list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
279	spin_unlock(&ocfs2_dlm_tracking_lock);
280}
281
282static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
283{
284	spin_lock(&ocfs2_dlm_tracking_lock);
285	if (!list_empty(&res->l_debug_list))
286		list_del_init(&res->l_debug_list);
287	spin_unlock(&ocfs2_dlm_tracking_lock);
288}
289
290static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
291				       struct ocfs2_lock_res *res,
292				       enum ocfs2_lock_type type,
293				       struct ocfs2_lock_res_ops *ops,
294				       void *priv)
295{
296	res->l_type          = type;
297	res->l_ops           = ops;
298	res->l_priv          = priv;
299
300	res->l_level         = LKM_IVMODE;
301	res->l_requested     = LKM_IVMODE;
302	res->l_blocking      = LKM_IVMODE;
303	res->l_action        = OCFS2_AST_INVALID;
304	res->l_unlock_action = OCFS2_UNLOCK_INVALID;
305
306	res->l_flags         = OCFS2_LOCK_INITIALIZED;
307
308	ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
309}
310
311void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
312{
313	/* This also clears out the lock status block */
314	memset(res, 0, sizeof(struct ocfs2_lock_res));
315	spin_lock_init(&res->l_lock);
316	init_waitqueue_head(&res->l_event);
317	INIT_LIST_HEAD(&res->l_blocked_list);
318	INIT_LIST_HEAD(&res->l_mask_waiters);
319}
320
321void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
322			       enum ocfs2_lock_type type,
323			       struct inode *inode)
324{
325	struct ocfs2_lock_res_ops *ops;
326
327	switch(type) {
328		case OCFS2_LOCK_TYPE_RW:
329			ops = &ocfs2_inode_rw_lops;
330			break;
331		case OCFS2_LOCK_TYPE_META:
332			ops = &ocfs2_inode_meta_lops;
333			break;
334		case OCFS2_LOCK_TYPE_DATA:
335			ops = &ocfs2_inode_data_lops;
336			break;
337		default:
338			mlog_bug_on_msg(1, "type: %d\n", type);
339			ops = NULL; /* thanks, gcc */
340			break;
341	};
342
343	ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
344			      inode->i_generation, res->l_name);
345	ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
346}
347
348static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
349{
350	__be64 inode_blkno_be;
351
352	memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
353	       sizeof(__be64));
354
355	return be64_to_cpu(inode_blkno_be);
356}
357
358void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
359				u64 parent, struct inode *inode)
360{
361	int len;
362	u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
363	__be64 inode_blkno_be = cpu_to_be64(inode_blkno);
364	struct ocfs2_lock_res *lockres = &dl->dl_lockres;
365
366	ocfs2_lock_res_init_once(lockres);
367
368	/*
369	 * Unfortunately, the standard lock naming scheme won't work
370	 * here because we have two 16 byte values to use. Instead,
371	 * we'll stuff the inode number as a binary value. We still
372	 * want error prints to show something without garbling the
373	 * display, so drop a null byte in there before the inode
374	 * number. A future version of OCFS2 will likely use all
375	 * binary lock names. The stringified names have been a
376	 * tremendous aid in debugging, but now that the debugfs
377	 * interface exists, we can mangle things there if need be.
378	 *
379	 * NOTE: We also drop the standard "pad" value (the total lock
380	 * name size stays the same though - the last part is all
381	 * zeros due to the memset in ocfs2_lock_res_init_once()
382	 */
383	len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
384		       "%c%016llx",
385		       ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
386		       (long long)parent);
387
388	BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
389
390	memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
391	       sizeof(__be64));
392
393	ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
394				   OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
395				   dl);
396}
397
398static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
399				      struct ocfs2_super *osb)
400{
401	/* Superblock lockres doesn't come from a slab so we call init
402	 * once on it manually.  */
403	ocfs2_lock_res_init_once(res);
404	ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
405			      0, res->l_name);
406	ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
407				   &ocfs2_super_lops, osb);
408}
409
410static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
411				       struct ocfs2_super *osb)
412{
413	/* Rename lockres doesn't come from a slab so we call init
414	 * once on it manually.  */
415	ocfs2_lock_res_init_once(res);
416	ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
417	ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
418				   &ocfs2_rename_lops, osb);
419}
420
421void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
422{
423	mlog_entry_void();
424
425	if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
426		return;
427
428	ocfs2_remove_lockres_tracking(res);
429
430	mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
431			"Lockres %s is on the blocked list\n",
432			res->l_name);
433	mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
434			"Lockres %s has mask waiters pending\n",
435			res->l_name);
436	mlog_bug_on_msg(spin_is_locked(&res->l_lock),
437			"Lockres %s is locked\n",
438			res->l_name);
439	mlog_bug_on_msg(res->l_ro_holders,
440			"Lockres %s has %u ro holders\n",
441			res->l_name, res->l_ro_holders);
442	mlog_bug_on_msg(res->l_ex_holders,
443			"Lockres %s has %u ex holders\n",
444			res->l_name, res->l_ex_holders);
445
446	/* Need to clear out the lock status block for the dlm */
447	memset(&res->l_lksb, 0, sizeof(res->l_lksb));
448
449	res->l_flags = 0UL;
450	mlog_exit_void();
451}
452
453static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
454				     int level)
455{
456	mlog_entry_void();
457
458	BUG_ON(!lockres);
459
460	switch(level) {
461	case LKM_EXMODE:
462		lockres->l_ex_holders++;
463		break;
464	case LKM_PRMODE:
465		lockres->l_ro_holders++;
466		break;
467	default:
468		BUG();
469	}
470
471	mlog_exit_void();
472}
473
474static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
475				     int level)
476{
477	mlog_entry_void();
478
479	BUG_ON(!lockres);
480
481	switch(level) {
482	case LKM_EXMODE:
483		BUG_ON(!lockres->l_ex_holders);
484		lockres->l_ex_holders--;
485		break;
486	case LKM_PRMODE:
487		BUG_ON(!lockres->l_ro_holders);
488		lockres->l_ro_holders--;
489		break;
490	default:
491		BUG();
492	}
493	mlog_exit_void();
494}
495
496/* WARNING: This function lives in a world where the only three lock
497 * levels are EX, PR, and NL. It *will* have to be adjusted when more
498 * lock types are added. */
499static inline int ocfs2_highest_compat_lock_level(int level)
500{
501	int new_level = LKM_EXMODE;
502
503	if (level == LKM_EXMODE)
504		new_level = LKM_NLMODE;
505	else if (level == LKM_PRMODE)
506		new_level = LKM_PRMODE;
507	return new_level;
508}
509
510static void lockres_set_flags(struct ocfs2_lock_res *lockres,
511			      unsigned long newflags)
512{
513	struct list_head *pos, *tmp;
514	struct ocfs2_mask_waiter *mw;
515
516 	assert_spin_locked(&lockres->l_lock);
517
518	lockres->l_flags = newflags;
519
520	list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
521		mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
522		if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
523			continue;
524
525		list_del_init(&mw->mw_item);
526		mw->mw_status = 0;
527		complete(&mw->mw_complete);
528	}
529}
530static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
531{
532	lockres_set_flags(lockres, lockres->l_flags | or);
533}
534static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
535				unsigned long clear)
536{
537	lockres_set_flags(lockres, lockres->l_flags & ~clear);
538}
539
540static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
541{
542	mlog_entry_void();
543
544	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
545	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
546	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
547	BUG_ON(lockres->l_blocking <= LKM_NLMODE);
548
549	lockres->l_level = lockres->l_requested;
550	if (lockres->l_level <=
551	    ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
552		lockres->l_blocking = LKM_NLMODE;
553		lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
554	}
555	lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
556
557	mlog_exit_void();
558}
559
560static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
561{
562	mlog_entry_void();
563
564	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
565	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
566
567	/* Convert from RO to EX doesn't really need anything as our
568	 * information is already up to data. Convert from NL to
569	 * *anything* however should mark ourselves as needing an
570	 * update */
571	if (lockres->l_level == LKM_NLMODE)
572		lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
573
574	lockres->l_level = lockres->l_requested;
575	lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
576
577	mlog_exit_void();
578}
579
580static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
581{
582	mlog_entry_void();
583
584	BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
585	BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
586
587	if (lockres->l_requested > LKM_NLMODE &&
588	    !(lockres->l_flags & OCFS2_LOCK_LOCAL))
589		lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
590
591	lockres->l_level = lockres->l_requested;
592	lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
593	lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
594
595	mlog_exit_void();
596}
597
598static void ocfs2_inode_ast_func(void *opaque)
599{
600	struct ocfs2_lock_res *lockres = opaque;
601	struct inode *inode;
602	struct dlm_lockstatus *lksb;
603	unsigned long flags;
604
605	mlog_entry_void();
606
607	inode = ocfs2_lock_res_inode(lockres);
608
609	mlog(0, "AST fired for inode %llu, l_action = %u, type = %s\n",
610	     (unsigned long long)OCFS2_I(inode)->ip_blkno, lockres->l_action,
611	     ocfs2_lock_type_string(lockres->l_type));
612
613	BUG_ON(!ocfs2_is_inode_lock(lockres));
614
615	spin_lock_irqsave(&lockres->l_lock, flags);
616
617	lksb = &(lockres->l_lksb);
618	if (lksb->status != DLM_NORMAL) {
619		mlog(ML_ERROR, "ocfs2_inode_ast_func: lksb status value of %u "
620		     "on inode %llu\n", lksb->status,
621		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
622		spin_unlock_irqrestore(&lockres->l_lock, flags);
623		mlog_exit_void();
624		return;
625	}
626
627	switch(lockres->l_action) {
628	case OCFS2_AST_ATTACH:
629		ocfs2_generic_handle_attach_action(lockres);
630		lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
631		break;
632	case OCFS2_AST_CONVERT:
633		ocfs2_generic_handle_convert_action(lockres);
634		break;
635	case OCFS2_AST_DOWNCONVERT:
636		ocfs2_generic_handle_downconvert_action(lockres);
637		break;
638	default:
639		mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
640		     "lockres flags = 0x%lx, unlock action: %u\n",
641		     lockres->l_name, lockres->l_action, lockres->l_flags,
642		     lockres->l_unlock_action);
643
644		BUG();
645	}
646
647	/* data and rw locking ignores refresh flag for now. */
648	if (lockres->l_type != OCFS2_LOCK_TYPE_META)
649		lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
650
651	/* set it to something invalid so if we get called again we
652	 * can catch it. */
653	lockres->l_action = OCFS2_AST_INVALID;
654	spin_unlock_irqrestore(&lockres->l_lock, flags);
655	wake_up(&lockres->l_event);
656
657	mlog_exit_void();
658}
659
660static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
661				     int level)
662{
663	int needs_downconvert = 0;
664	mlog_entry_void();
665
666	assert_spin_locked(&lockres->l_lock);
667
668	lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
669
670	if (level > lockres->l_blocking) {
671		/* only schedule a downconvert if we haven't already scheduled
672		 * one that goes low enough to satisfy the level we're
673		 * blocking.  this also catches the case where we get
674		 * duplicate BASTs */
675		if (ocfs2_highest_compat_lock_level(level) <
676		    ocfs2_highest_compat_lock_level(lockres->l_blocking))
677			needs_downconvert = 1;
678
679		lockres->l_blocking = level;
680	}
681
682	mlog_exit(needs_downconvert);
683	return needs_downconvert;
684}
685
686static void ocfs2_generic_bast_func(struct ocfs2_super *osb,
687				    struct ocfs2_lock_res *lockres,
688				    int level)
689{
690	int needs_downconvert;
691	unsigned long flags;
692
693	mlog_entry_void();
694
695	BUG_ON(level <= LKM_NLMODE);
696
697	spin_lock_irqsave(&lockres->l_lock, flags);
698	needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
699	if (needs_downconvert)
700		ocfs2_schedule_blocked_lock(osb, lockres);
701	spin_unlock_irqrestore(&lockres->l_lock, flags);
702
703	wake_up(&lockres->l_event);
704
705	ocfs2_kick_vote_thread(osb);
706
707	mlog_exit_void();
708}
709
710static void ocfs2_inode_bast_func(void *opaque, int level)
711{
712	struct ocfs2_lock_res *lockres = opaque;
713	struct inode *inode;
714	struct ocfs2_super *osb;
715
716	mlog_entry_void();
717
718	BUG_ON(!ocfs2_is_inode_lock(lockres));
719
720	inode = ocfs2_lock_res_inode(lockres);
721	osb = OCFS2_SB(inode->i_sb);
722
723	mlog(0, "BAST fired for inode %llu, blocking %d, level %d type %s\n",
724	     (unsigned long long)OCFS2_I(inode)->ip_blkno, level,
725	     lockres->l_level, ocfs2_lock_type_string(lockres->l_type));
726
727	ocfs2_generic_bast_func(osb, lockres, level);
728
729	mlog_exit_void();
730}
731
732static void ocfs2_generic_ast_func(struct ocfs2_lock_res *lockres,
733				   int ignore_refresh)
734{
735	struct dlm_lockstatus *lksb = &lockres->l_lksb;
736	unsigned long flags;
737
738	spin_lock_irqsave(&lockres->l_lock, flags);
739
740	if (lksb->status != DLM_NORMAL) {
741		mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
742		     lockres->l_name, lksb->status);
743		spin_unlock_irqrestore(&lockres->l_lock, flags);
744		return;
745	}
746
747	switch(lockres->l_action) {
748	case OCFS2_AST_ATTACH:
749		ocfs2_generic_handle_attach_action(lockres);
750		break;
751	case OCFS2_AST_CONVERT:
752		ocfs2_generic_handle_convert_action(lockres);
753		break;
754	case OCFS2_AST_DOWNCONVERT:
755		ocfs2_generic_handle_downconvert_action(lockres);
756		break;
757	default:
758		BUG();
759	}
760
761	if (ignore_refresh)
762		lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
763
764	/* set it to something invalid so if we get called again we
765	 * can catch it. */
766	lockres->l_action = OCFS2_AST_INVALID;
767
768	wake_up(&lockres->l_event);
769	spin_unlock_irqrestore(&lockres->l_lock, flags);
770}
771
772static void ocfs2_super_ast_func(void *opaque)
773{
774	struct ocfs2_lock_res *lockres = opaque;
775
776	mlog_entry_void();
777	mlog(0, "Superblock AST fired\n");
778
779	BUG_ON(!ocfs2_is_super_lock(lockres));
780	ocfs2_generic_ast_func(lockres, 0);
781
782	mlog_exit_void();
783}
784
785static void ocfs2_super_bast_func(void *opaque,
786				  int level)
787{
788	struct ocfs2_lock_res *lockres = opaque;
789	struct ocfs2_super *osb;
790
791	mlog_entry_void();
792	mlog(0, "Superblock BAST fired\n");
793
794	BUG_ON(!ocfs2_is_super_lock(lockres));
795       	osb = ocfs2_lock_res_super(lockres);
796	ocfs2_generic_bast_func(osb, lockres, level);
797
798	mlog_exit_void();
799}
800
801static void ocfs2_rename_ast_func(void *opaque)
802{
803	struct ocfs2_lock_res *lockres = opaque;
804
805	mlog_entry_void();
806
807	mlog(0, "Rename AST fired\n");
808
809	BUG_ON(!ocfs2_is_rename_lock(lockres));
810
811	ocfs2_generic_ast_func(lockres, 1);
812
813	mlog_exit_void();
814}
815
816static void ocfs2_rename_bast_func(void *opaque,
817				   int level)
818{
819	struct ocfs2_lock_res *lockres = opaque;
820	struct ocfs2_super *osb;
821
822	mlog_entry_void();
823
824	mlog(0, "Rename BAST fired\n");
825
826	BUG_ON(!ocfs2_is_rename_lock(lockres));
827
828	osb = ocfs2_lock_res_super(lockres);
829	ocfs2_generic_bast_func(osb, lockres, level);
830
831	mlog_exit_void();
832}
833
834static void ocfs2_dentry_ast_func(void *opaque)
835{
836	struct ocfs2_lock_res *lockres = opaque;
837
838	BUG_ON(!lockres);
839
840	ocfs2_generic_ast_func(lockres, 1);
841}
842
843static void ocfs2_dentry_bast_func(void *opaque, int level)
844{
845	struct ocfs2_lock_res *lockres = opaque;
846	struct ocfs2_dentry_lock *dl = lockres->l_priv;
847	struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
848
849	mlog(0, "Dentry bast: level: %d, name: %s\n", level,
850	     lockres->l_name);
851
852	ocfs2_generic_bast_func(osb, lockres, level);
853}
854
855static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
856						int convert)
857{
858	unsigned long flags;
859
860	mlog_entry_void();
861	spin_lock_irqsave(&lockres->l_lock, flags);
862	lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
863	if (convert)
864		lockres->l_action = OCFS2_AST_INVALID;
865	else
866		lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
867	spin_unlock_irqrestore(&lockres->l_lock, flags);
868
869	wake_up(&lockres->l_event);
870	mlog_exit_void();
871}
872
873/* Note: If we detect another process working on the lock (i.e.,
874 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
875 * to do the right thing in that case.
876 */
877static int ocfs2_lock_create(struct ocfs2_super *osb,
878			     struct ocfs2_lock_res *lockres,
879			     int level,
880			     int dlm_flags)
881{
882	int ret = 0;
883	enum dlm_status status;
884	unsigned long flags;
885
886	mlog_entry_void();
887
888	mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
889	     dlm_flags);
890
891	spin_lock_irqsave(&lockres->l_lock, flags);
892	if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
893	    (lockres->l_flags & OCFS2_LOCK_BUSY)) {
894		spin_unlock_irqrestore(&lockres->l_lock, flags);
895		goto bail;
896	}
897
898	lockres->l_action = OCFS2_AST_ATTACH;
899	lockres->l_requested = level;
900	lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
901	spin_unlock_irqrestore(&lockres->l_lock, flags);
902
903	status = dlmlock(osb->dlm,
904			 level,
905			 &lockres->l_lksb,
906			 dlm_flags,
907			 lockres->l_name,
908			 OCFS2_LOCK_ID_MAX_LEN - 1,
909			 lockres->l_ops->ast,
910			 lockres,
911			 lockres->l_ops->bast);
912	if (status != DLM_NORMAL) {
913		ocfs2_log_dlm_error("dlmlock", status, lockres);
914		ret = -EINVAL;
915		ocfs2_recover_from_dlm_error(lockres, 1);
916	}
917
918	mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
919
920bail:
921	mlog_exit(ret);
922	return ret;
923}
924
925static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
926					int flag)
927{
928	unsigned long flags;
929	int ret;
930
931	spin_lock_irqsave(&lockres->l_lock, flags);
932	ret = lockres->l_flags & flag;
933	spin_unlock_irqrestore(&lockres->l_lock, flags);
934
935	return ret;
936}
937
938static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
939
940{
941	wait_event(lockres->l_event,
942		   !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
943}
944
945static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
946
947{
948	wait_event(lockres->l_event,
949		   !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
950}
951
952/* predict what lock level we'll be dropping down to on behalf
953 * of another node, and return true if the currently wanted
954 * level will be compatible with it. */
955static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
956						     int wanted)
957{
958	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
959
960	return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
961}
962
963static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
964{
965	INIT_LIST_HEAD(&mw->mw_item);
966	init_completion(&mw->mw_complete);
967}
968
969static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
970{
971	wait_for_completion(&mw->mw_complete);
972	/* Re-arm the completion in case we want to wait on it again */
973	INIT_COMPLETION(mw->mw_complete);
974	return mw->mw_status;
975}
976
977static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
978				    struct ocfs2_mask_waiter *mw,
979				    unsigned long mask,
980				    unsigned long goal)
981{
982	BUG_ON(!list_empty(&mw->mw_item));
983
984	assert_spin_locked(&lockres->l_lock);
985
986	list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
987	mw->mw_mask = mask;
988	mw->mw_goal = goal;
989}
990
991/* returns 0 if the mw that was removed was already satisfied, -EBUSY
992 * if the mask still hadn't reached its goal */
993static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
994				      struct ocfs2_mask_waiter *mw)
995{
996	unsigned long flags;
997	int ret = 0;
998
999	spin_lock_irqsave(&lockres->l_lock, flags);
1000	if (!list_empty(&mw->mw_item)) {
1001		if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1002			ret = -EBUSY;
1003
1004		list_del_init(&mw->mw_item);
1005		init_completion(&mw->mw_complete);
1006	}
1007	spin_unlock_irqrestore(&lockres->l_lock, flags);
1008
1009	return ret;
1010
1011}
1012
1013static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1014			      struct ocfs2_lock_res *lockres,
1015			      int level,
1016			      int lkm_flags,
1017			      int arg_flags)
1018{
1019	struct ocfs2_mask_waiter mw;
1020	enum dlm_status status;
1021	int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1022	int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1023	unsigned long flags;
1024
1025	mlog_entry_void();
1026
1027	ocfs2_init_mask_waiter(&mw);
1028
1029again:
1030	wait = 0;
1031
1032	if (catch_signals && signal_pending(current)) {
1033		ret = -ERESTARTSYS;
1034		goto out;
1035	}
1036
1037	spin_lock_irqsave(&lockres->l_lock, flags);
1038
1039	mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1040			"Cluster lock called on freeing lockres %s! flags "
1041			"0x%lx\n", lockres->l_name, lockres->l_flags);
1042
1043	/* We only compare against the currently granted level
1044	 * here. If the lock is blocked waiting on a downconvert,
1045	 * we'll get caught below. */
1046	if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1047	    level > lockres->l_level) {
1048		/* is someone sitting in dlm_lock? If so, wait on
1049		 * them. */
1050		lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1051		wait = 1;
1052		goto unlock;
1053	}
1054
1055	if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1056		/* lock has not been created yet. */
1057		spin_unlock_irqrestore(&lockres->l_lock, flags);
1058
1059		ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1060		if (ret < 0) {
1061			mlog_errno(ret);
1062			goto out;
1063		}
1064		goto again;
1065	}
1066
1067	if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1068	    !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1069		/* is the lock is currently blocked on behalf of
1070		 * another node */
1071		lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1072		wait = 1;
1073		goto unlock;
1074	}
1075
1076	if (level > lockres->l_level) {
1077		if (lockres->l_action != OCFS2_AST_INVALID)
1078			mlog(ML_ERROR, "lockres %s has action %u pending\n",
1079			     lockres->l_name, lockres->l_action);
1080
1081		lockres->l_action = OCFS2_AST_CONVERT;
1082		lockres->l_requested = level;
1083		lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1084		spin_unlock_irqrestore(&lockres->l_lock, flags);
1085
1086		BUG_ON(level == LKM_IVMODE);
1087		BUG_ON(level == LKM_NLMODE);
1088
1089		mlog(0, "lock %s, convert from %d to level = %d\n",
1090		     lockres->l_name, lockres->l_level, level);
1091
1092		/* call dlm_lock to upgrade lock now */
1093		status = dlmlock(osb->dlm,
1094				 level,
1095				 &lockres->l_lksb,
1096				 lkm_flags|LKM_CONVERT|LKM_VALBLK,
1097				 lockres->l_name,
1098				 OCFS2_LOCK_ID_MAX_LEN - 1,
1099				 lockres->l_ops->ast,
1100				 lockres,
1101				 lockres->l_ops->bast);
1102		if (status != DLM_NORMAL) {
1103			if ((lkm_flags & LKM_NOQUEUE) &&
1104			    (status == DLM_NOTQUEUED))
1105				ret = -EAGAIN;
1106			else {
1107				ocfs2_log_dlm_error("dlmlock", status,
1108						    lockres);
1109				ret = -EINVAL;
1110			}
1111			ocfs2_recover_from_dlm_error(lockres, 1);
1112			goto out;
1113		}
1114
1115		mlog(0, "lock %s, successfull return from dlmlock\n",
1116		     lockres->l_name);
1117
1118		/* At this point we've gone inside the dlm and need to
1119		 * complete our work regardless. */
1120		catch_signals = 0;
1121
1122		/* wait for busy to clear and carry on */
1123		goto again;
1124	}
1125
1126	/* Ok, if we get here then we're good to go. */
1127	ocfs2_inc_holders(lockres, level);
1128
1129	ret = 0;
1130unlock:
1131	spin_unlock_irqrestore(&lockres->l_lock, flags);
1132out:
1133	/*
1134	 * This is helping work around a lock inversion between the page lock
1135	 * and dlm locks.  One path holds the page lock while calling aops
1136	 * which block acquiring dlm locks.  The voting thread holds dlm
1137	 * locks while acquiring page locks while down converting data locks.
1138	 * This block is helping an aop path notice the inversion and back
1139	 * off to unlock its page lock before trying the dlm lock again.
1140	 */
1141	if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1142	    mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1143		wait = 0;
1144		if (lockres_remove_mask_waiter(lockres, &mw))
1145			ret = -EAGAIN;
1146		else
1147			goto again;
1148	}
1149	if (wait) {
1150		ret = ocfs2_wait_for_mask(&mw);
1151		if (ret == 0)
1152			goto again;
1153		mlog_errno(ret);
1154	}
1155
1156	mlog_exit(ret);
1157	return ret;
1158}
1159
1160static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1161				 struct ocfs2_lock_res *lockres,
1162				 int level)
1163{
1164	unsigned long flags;
1165
1166	mlog_entry_void();
1167	spin_lock_irqsave(&lockres->l_lock, flags);
1168	ocfs2_dec_holders(lockres, level);
1169	ocfs2_vote_on_unlock(osb, lockres);
1170	spin_unlock_irqrestore(&lockres->l_lock, flags);
1171	mlog_exit_void();
1172}
1173
1174int ocfs2_create_new_lock(struct ocfs2_super *osb,
1175			  struct ocfs2_lock_res *lockres,
1176			  int ex)
1177{
1178	int level =  ex ? LKM_EXMODE : LKM_PRMODE;
1179	unsigned long flags;
1180
1181	spin_lock_irqsave(&lockres->l_lock, flags);
1182	BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1183	lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1184	spin_unlock_irqrestore(&lockres->l_lock, flags);
1185
1186	return ocfs2_lock_create(osb, lockres, level, LKM_LOCAL);
1187}
1188
1189/* Grants us an EX lock on the data and metadata resources, skipping
1190 * the normal cluster directory lookup. Use this ONLY on newly created
1191 * inodes which other nodes can't possibly see, and which haven't been
1192 * hashed in the inode hash yet. This can give us a good performance
1193 * increase as it'll skip the network broadcast normally associated
1194 * with creating a new lock resource. */
1195int ocfs2_create_new_inode_locks(struct inode *inode)
1196{
1197	int ret;
1198	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1199
1200	BUG_ON(!inode);
1201	BUG_ON(!ocfs2_inode_is_new(inode));
1202
1203	mlog_entry_void();
1204
1205	mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1206
1207	/* NOTE: That we don't increment any of the holder counts, nor
1208	 * do we add anything to a journal handle. Since this is
1209	 * supposed to be a new inode which the cluster doesn't know
1210	 * about yet, there is no need to.  As far as the LVB handling
1211	 * is concerned, this is basically like acquiring an EX lock
1212	 * on a resource which has an invalid one -- we'll set it
1213	 * valid when we release the EX. */
1214
1215	ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1);
1216	if (ret) {
1217		mlog_errno(ret);
1218		goto bail;
1219	}
1220
1221	ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1);
1222	if (ret) {
1223		mlog_errno(ret);
1224		goto bail;
1225	}
1226
1227	ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1);
1228	if (ret) {
1229		mlog_errno(ret);
1230		goto bail;
1231	}
1232
1233bail:
1234	mlog_exit(ret);
1235	return ret;
1236}
1237
1238int ocfs2_rw_lock(struct inode *inode, int write)
1239{
1240	int status, level;
1241	struct ocfs2_lock_res *lockres;
1242
1243	BUG_ON(!inode);
1244
1245	mlog_entry_void();
1246
1247	mlog(0, "inode %llu take %s RW lock\n",
1248	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1249	     write ? "EXMODE" : "PRMODE");
1250
1251	lockres = &OCFS2_I(inode)->ip_rw_lockres;
1252
1253	level = write ? LKM_EXMODE : LKM_PRMODE;
1254
1255	status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1256				    0);
1257	if (status < 0)
1258		mlog_errno(status);
1259
1260	mlog_exit(status);
1261	return status;
1262}
1263
1264void ocfs2_rw_unlock(struct inode *inode, int write)
1265{
1266	int level = write ? LKM_EXMODE : LKM_PRMODE;
1267	struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1268
1269	mlog_entry_void();
1270
1271	mlog(0, "inode %llu drop %s RW lock\n",
1272	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1273	     write ? "EXMODE" : "PRMODE");
1274
1275	ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1276
1277	mlog_exit_void();
1278}
1279
1280int ocfs2_data_lock_full(struct inode *inode,
1281			 int write,
1282			 int arg_flags)
1283{
1284	int status = 0, level;
1285	struct ocfs2_lock_res *lockres;
1286
1287	BUG_ON(!inode);
1288
1289	mlog_entry_void();
1290
1291	mlog(0, "inode %llu take %s DATA lock\n",
1292	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1293	     write ? "EXMODE" : "PRMODE");
1294
1295	/* We'll allow faking a readonly data lock for
1296	 * rodevices. */
1297	if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1298		if (write) {
1299			status = -EROFS;
1300			mlog_errno(status);
1301		}
1302		goto out;
1303	}
1304
1305	lockres = &OCFS2_I(inode)->ip_data_lockres;
1306
1307	level = write ? LKM_EXMODE : LKM_PRMODE;
1308
1309	status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1310				    0, arg_flags);
1311	if (status < 0 && status != -EAGAIN)
1312		mlog_errno(status);
1313
1314out:
1315	mlog_exit(status);
1316	return status;
1317}
1318
1319/* see ocfs2_meta_lock_with_page() */
1320int ocfs2_data_lock_with_page(struct inode *inode,
1321			      int write,
1322			      struct page *page)
1323{
1324	int ret;
1325
1326	ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1327	if (ret == -EAGAIN) {
1328		unlock_page(page);
1329		if (ocfs2_data_lock(inode, write) == 0)
1330			ocfs2_data_unlock(inode, write);
1331		ret = AOP_TRUNCATED_PAGE;
1332	}
1333
1334	return ret;
1335}
1336
1337static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1338				 struct ocfs2_lock_res *lockres)
1339{
1340	int kick = 0;
1341
1342	mlog_entry_void();
1343
1344	/* If we know that another node is waiting on our lock, kick
1345	 * the vote thread * pre-emptively when we reach a release
1346	 * condition. */
1347	if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1348		switch(lockres->l_blocking) {
1349		case LKM_EXMODE:
1350			if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1351				kick = 1;
1352			break;
1353		case LKM_PRMODE:
1354			if (!lockres->l_ex_holders)
1355				kick = 1;
1356			break;
1357		default:
1358			BUG();
1359		}
1360	}
1361
1362	if (kick)
1363		ocfs2_kick_vote_thread(osb);
1364
1365	mlog_exit_void();
1366}
1367
1368void ocfs2_data_unlock(struct inode *inode,
1369		       int write)
1370{
1371	int level = write ? LKM_EXMODE : LKM_PRMODE;
1372	struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1373
1374	mlog_entry_void();
1375
1376	mlog(0, "inode %llu drop %s DATA lock\n",
1377	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1378	     write ? "EXMODE" : "PRMODE");
1379
1380	if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1381		ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1382
1383	mlog_exit_void();
1384}
1385
1386#define OCFS2_SEC_BITS   34
1387#define OCFS2_SEC_SHIFT  (64 - 34)
1388#define OCFS2_NSEC_MASK  ((1ULL << OCFS2_SEC_SHIFT) - 1)
1389
1390/* LVB only has room for 64 bits of time here so we pack it for
1391 * now. */
1392static u64 ocfs2_pack_timespec(struct timespec *spec)
1393{
1394	u64 res;
1395	u64 sec = spec->tv_sec;
1396	u32 nsec = spec->tv_nsec;
1397
1398	res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1399
1400	return res;
1401}
1402
1403/* Call this with the lockres locked. I am reasonably sure we don't
1404 * need ip_lock in this function as anyone who would be changing those
1405 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1406static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1407{
1408	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1409	struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1410	struct ocfs2_meta_lvb *lvb;
1411
1412	mlog_entry_void();
1413
1414	lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1415
1416	lvb->lvb_version   = OCFS2_LVB_VERSION;
1417	lvb->lvb_isize	   = cpu_to_be64(i_size_read(inode));
1418	lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1419	lvb->lvb_iuid      = cpu_to_be32(inode->i_uid);
1420	lvb->lvb_igid      = cpu_to_be32(inode->i_gid);
1421	lvb->lvb_imode     = cpu_to_be16(inode->i_mode);
1422	lvb->lvb_inlink    = cpu_to_be16(inode->i_nlink);
1423	lvb->lvb_iatime_packed  =
1424		cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1425	lvb->lvb_ictime_packed =
1426		cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1427	lvb->lvb_imtime_packed =
1428		cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
1429	lvb->lvb_iattr    = cpu_to_be32(oi->ip_attr);
1430	lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
1431
1432	mlog_meta_lvb(0, lockres);
1433
1434	mlog_exit_void();
1435}
1436
1437static void ocfs2_unpack_timespec(struct timespec *spec,
1438				  u64 packed_time)
1439{
1440	spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1441	spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1442}
1443
1444static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1445{
1446	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1447	struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1448	struct ocfs2_meta_lvb *lvb;
1449
1450	mlog_entry_void();
1451
1452	mlog_meta_lvb(0, lockres);
1453
1454	lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1455
1456	/* We're safe here without the lockres lock... */
1457	spin_lock(&oi->ip_lock);
1458	oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1459	i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1460
1461	oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1462	ocfs2_set_inode_flags(inode);
1463
1464	/* fast-symlinks are a special case */
1465	if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1466		inode->i_blocks = 0;
1467	else
1468		inode->i_blocks =
1469			ocfs2_align_bytes_to_sectors(i_size_read(inode));
1470
1471	inode->i_uid     = be32_to_cpu(lvb->lvb_iuid);
1472	inode->i_gid     = be32_to_cpu(lvb->lvb_igid);
1473	inode->i_mode    = be16_to_cpu(lvb->lvb_imode);
1474	inode->i_nlink   = be16_to_cpu(lvb->lvb_inlink);
1475	ocfs2_unpack_timespec(&inode->i_atime,
1476			      be64_to_cpu(lvb->lvb_iatime_packed));
1477	ocfs2_unpack_timespec(&inode->i_mtime,
1478			      be64_to_cpu(lvb->lvb_imtime_packed));
1479	ocfs2_unpack_timespec(&inode->i_ctime,
1480			      be64_to_cpu(lvb->lvb_ictime_packed));
1481	spin_unlock(&oi->ip_lock);
1482
1483	mlog_exit_void();
1484}
1485
1486static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1487					      struct ocfs2_lock_res *lockres)
1488{
1489	struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1490
1491	if (lvb->lvb_version == OCFS2_LVB_VERSION
1492	    && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
1493		return 1;
1494	return 0;
1495}
1496
1497/* Determine whether a lock resource needs to be refreshed, and
1498 * arbitrate who gets to refresh it.
1499 *
1500 *   0 means no refresh needed.
1501 *
1502 *   > 0 means you need to refresh this and you MUST call
1503 *   ocfs2_complete_lock_res_refresh afterwards. */
1504static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1505{
1506	unsigned long flags;
1507	int status = 0;
1508
1509	mlog_entry_void();
1510
1511refresh_check:
1512	spin_lock_irqsave(&lockres->l_lock, flags);
1513	if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1514		spin_unlock_irqrestore(&lockres->l_lock, flags);
1515		goto bail;
1516	}
1517
1518	if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1519		spin_unlock_irqrestore(&lockres->l_lock, flags);
1520
1521		ocfs2_wait_on_refreshing_lock(lockres);
1522		goto refresh_check;
1523	}
1524
1525	/* Ok, I'll be the one to refresh this lock. */
1526	lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1527	spin_unlock_irqrestore(&lockres->l_lock, flags);
1528
1529	status = 1;
1530bail:
1531	mlog_exit(status);
1532	return status;
1533}
1534
1535/* If status is non zero, I'll mark it as not being in refresh
1536 * anymroe, but i won't clear the needs refresh flag. */
1537static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1538						   int status)
1539{
1540	unsigned long flags;
1541	mlog_entry_void();
1542
1543	spin_lock_irqsave(&lockres->l_lock, flags);
1544	lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1545	if (!status)
1546		lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1547	spin_unlock_irqrestore(&lockres->l_lock, flags);
1548
1549	wake_up(&lockres->l_event);
1550
1551	mlog_exit_void();
1552}
1553
1554/* may or may not return a bh if it went to disk. */
1555static int ocfs2_meta_lock_update(struct inode *inode,
1556				  struct buffer_head **bh)
1557{
1558	int status = 0;
1559	struct ocfs2_inode_info *oi = OCFS2_I(inode);
1560	struct ocfs2_lock_res *lockres;
1561	struct ocfs2_dinode *fe;
1562
1563	mlog_entry_void();
1564
1565	spin_lock(&oi->ip_lock);
1566	if (oi->ip_flags & OCFS2_INODE_DELETED) {
1567		mlog(0, "Orphaned inode %llu was deleted while we "
1568		     "were waiting on a lock. ip_flags = 0x%x\n",
1569		     (unsigned long long)oi->ip_blkno, oi->ip_flags);
1570		spin_unlock(&oi->ip_lock);
1571		status = -ENOENT;
1572		goto bail;
1573	}
1574	spin_unlock(&oi->ip_lock);
1575
1576	lockres = &oi->ip_meta_lockres;
1577
1578	if (!ocfs2_should_refresh_lock_res(lockres))
1579		goto bail;
1580
1581	/* This will discard any caching information we might have had
1582	 * for the inode metadata. */
1583	ocfs2_metadata_cache_purge(inode);
1584
1585	/* will do nothing for inode types that don't use the extent
1586	 * map (directories, bitmap files, etc) */
1587	ocfs2_extent_map_trunc(inode, 0);
1588
1589	if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
1590		mlog(0, "Trusting LVB on inode %llu\n",
1591		     (unsigned long long)oi->ip_blkno);
1592		ocfs2_refresh_inode_from_lvb(inode);
1593	} else {
1594		/* Boo, we have to go to disk. */
1595		/* read bh, cast, ocfs2_refresh_inode */
1596		status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1597					  bh, OCFS2_BH_CACHED, inode);
1598		if (status < 0) {
1599			mlog_errno(status);
1600			goto bail_refresh;
1601		}
1602		fe = (struct ocfs2_dinode *) (*bh)->b_data;
1603
1604		/* This is a good chance to make sure we're not
1605		 * locking an invalid object.
1606		 *
1607		 * We bug on a stale inode here because we checked
1608		 * above whether it was wiped from disk. The wiping
1609		 * node provides a guarantee that we receive that
1610		 * message and can mark the inode before dropping any
1611		 * locks associated with it. */
1612		if (!OCFS2_IS_VALID_DINODE(fe)) {
1613			OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1614			status = -EIO;
1615			goto bail_refresh;
1616		}
1617		mlog_bug_on_msg(inode->i_generation !=
1618				le32_to_cpu(fe->i_generation),
1619				"Invalid dinode %llu disk generation: %u "
1620				"inode->i_generation: %u\n",
1621				(unsigned long long)oi->ip_blkno,
1622				le32_to_cpu(fe->i_generation),
1623				inode->i_generation);
1624		mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1625				!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
1626				"Stale dinode %llu dtime: %llu flags: 0x%x\n",
1627				(unsigned long long)oi->ip_blkno,
1628				(unsigned long long)le64_to_cpu(fe->i_dtime),
1629				le32_to_cpu(fe->i_flags));
1630
1631		ocfs2_refresh_inode(inode, fe);
1632	}
1633
1634	status = 0;
1635bail_refresh:
1636	ocfs2_complete_lock_res_refresh(lockres, status);
1637bail:
1638	mlog_exit(status);
1639	return status;
1640}
1641
1642static int ocfs2_assign_bh(struct inode *inode,
1643			   struct buffer_head **ret_bh,
1644			   struct buffer_head *passed_bh)
1645{
1646	int status;
1647
1648	if (passed_bh) {
1649		/* Ok, the update went to disk for us, use the
1650		 * returned bh. */
1651		*ret_bh = passed_bh;
1652		get_bh(*ret_bh);
1653
1654		return 0;
1655	}
1656
1657	status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1658				  OCFS2_I(inode)->ip_blkno,
1659				  ret_bh,
1660				  OCFS2_BH_CACHED,
1661				  inode);
1662	if (status < 0)
1663		mlog_errno(status);
1664
1665	return status;
1666}
1667
1668/*
1669 * returns < 0 error if the callback will never be called, otherwise
1670 * the result of the lock will be communicated via the callback.
1671 */
1672int ocfs2_meta_lock_full(struct inode *inode,
1673			 struct ocfs2_journal_handle *handle,
1674			 struct buffer_head **ret_bh,
1675			 int ex,
1676			 int arg_flags)
1677{
1678	int status, level, dlm_flags, acquired;
1679	struct ocfs2_lock_res *lockres;
1680	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1681	struct buffer_head *local_bh = NULL;
1682
1683	BUG_ON(!inode);
1684
1685	mlog_entry_void();
1686
1687	mlog(0, "inode %llu, take %s META lock\n",
1688	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1689	     ex ? "EXMODE" : "PRMODE");
1690
1691	status = 0;
1692	acquired = 0;
1693	/* We'll allow faking a readonly metadata lock for
1694	 * rodevices. */
1695	if (ocfs2_is_hard_readonly(osb)) {
1696		if (ex)
1697			status = -EROFS;
1698		goto bail;
1699	}
1700
1701	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1702		wait_event(osb->recovery_event,
1703			   ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1704
1705	acquired = 0;
1706	lockres = &OCFS2_I(inode)->ip_meta_lockres;
1707	level = ex ? LKM_EXMODE : LKM_PRMODE;
1708	dlm_flags = 0;
1709	if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1710		dlm_flags |= LKM_NOQUEUE;
1711
1712	status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1713	if (status < 0) {
1714		if (status != -EAGAIN && status != -EIOCBRETRY)
1715			mlog_errno(status);
1716		goto bail;
1717	}
1718
1719	/* Notify the error cleanup path to drop the cluster lock. */
1720	acquired = 1;
1721
1722	/* We wait twice because a node may have died while we were in
1723	 * the lower dlm layers. The second time though, we've
1724	 * committed to owning this lock so we don't allow signals to
1725	 * abort the operation. */
1726	if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1727		wait_event(osb->recovery_event,
1728			   ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1729
1730	/* This is fun. The caller may want a bh back, or it may
1731	 * not. ocfs2_meta_lock_update definitely wants one in, but
1732	 * may or may not read one, depending on what's in the
1733	 * LVB. The result of all of this is that we've *only* gone to
1734	 * disk if we have to, so the complexity is worthwhile. */
1735	status = ocfs2_meta_lock_update(inode, &local_bh);
1736	if (status < 0) {
1737		if (status != -ENOENT)
1738			mlog_errno(status);
1739		goto bail;
1740	}
1741
1742	if (ret_bh) {
1743		status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1744		if (status < 0) {
1745			mlog_errno(status);
1746			goto bail;
1747		}
1748	}
1749
1750	if (handle) {
1751		status = ocfs2_handle_add_lock(handle, inode);
1752		if (status < 0)
1753			mlog_errno(status);
1754	}
1755
1756bail:
1757	if (status < 0) {
1758		if (ret_bh && (*ret_bh)) {
1759			brelse(*ret_bh);
1760			*ret_bh = NULL;
1761		}
1762		if (acquired)
1763			ocfs2_meta_unlock(inode, ex);
1764	}
1765
1766	if (local_bh)
1767		brelse(local_bh);
1768
1769	mlog_exit(status);
1770	return status;
1771}
1772
1773/*
1774 * This is working around a lock inversion between tasks acquiring DLM locks
1775 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1776 * while acquiring page locks.
1777 *
1778 * ** These _with_page variantes are only intended to be called from aop
1779 * methods that hold page locks and return a very specific *positive* error
1780 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1781 *
1782 * The DLM is called such that it returns -EAGAIN if it would have blocked
1783 * waiting for the vote thread.  In that case we unlock our page so the vote
1784 * thread can make progress.  Once we've done this we have to return
1785 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1786 * into the VFS who will then immediately retry the aop call.
1787 *
1788 * We do a blocking lock and immediate unlock before returning, though, so that
1789 * the lock has a great chance of being cached on this node by the time the VFS
1790 * calls back to retry the aop.    This has a potential to livelock as nodes
1791 * ping locks back and forth, but that's a risk we're willing to take to avoid
1792 * the lock inversion simply.
1793 */
1794int ocfs2_meta_lock_with_page(struct inode *inode,
1795			      struct ocfs2_journal_handle *handle,
1796			      struct buffer_head **ret_bh,
1797			      int ex,
1798			      struct page *page)
1799{
1800	int ret;
1801
1802	ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
1803				   OCFS2_LOCK_NONBLOCK);
1804	if (ret == -EAGAIN) {
1805		unlock_page(page);
1806		if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
1807			ocfs2_meta_unlock(inode, ex);
1808		ret = AOP_TRUNCATED_PAGE;
1809	}
1810
1811	return ret;
1812}
1813
1814void ocfs2_meta_unlock(struct inode *inode,
1815		       int ex)
1816{
1817	int level = ex ? LKM_EXMODE : LKM_PRMODE;
1818	struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1819
1820	mlog_entry_void();
1821
1822	mlog(0, "inode %llu drop %s META lock\n",
1823	     (unsigned long long)OCFS2_I(inode)->ip_blkno,
1824	     ex ? "EXMODE" : "PRMODE");
1825
1826	if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1827		ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1828
1829	mlog_exit_void();
1830}
1831
1832int ocfs2_super_lock(struct ocfs2_super *osb,
1833		     int ex)
1834{
1835	int status;
1836	int level = ex ? LKM_EXMODE : LKM_PRMODE;
1837	struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1838	struct buffer_head *bh;
1839	struct ocfs2_slot_info *si = osb->slot_info;
1840
1841	mlog_entry_void();
1842
1843	if (ocfs2_is_hard_readonly(osb))
1844		return -EROFS;
1845
1846	status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1847	if (status < 0) {
1848		mlog_errno(status);
1849		goto bail;
1850	}
1851
1852	/* The super block lock path is really in the best position to
1853	 * know when resources covered by the lock need to be
1854	 * refreshed, so we do it here. Of course, making sense of
1855	 * everything is up to the caller :) */
1856	status = ocfs2_should_refresh_lock_res(lockres);
1857	if (status < 0) {
1858		mlog_errno(status);
1859		goto bail;
1860	}
1861	if (status) {
1862		bh = si->si_bh;
1863		status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1864					  si->si_inode);
1865		if (status == 0)
1866			ocfs2_update_slot_info(si);
1867
1868		ocfs2_complete_lock_res_refresh(lockres, status);
1869
1870		if (status < 0)
1871			mlog_errno(status);
1872	}
1873bail:
1874	mlog_exit(status);
1875	return status;
1876}
1877
1878void ocfs2_super_unlock(struct ocfs2_super *osb,
1879			int ex)
1880{
1881	int level = ex ? LKM_EXMODE : LKM_PRMODE;
1882	struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1883
1884	ocfs2_cluster_unlock(osb, lockres, level);
1885}
1886
1887int ocfs2_rename_lock(struct ocfs2_super *osb)
1888{
1889	int status;
1890	struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1891
1892	if (ocfs2_is_hard_readonly(osb))
1893		return -EROFS;
1894
1895	status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1896	if (status < 0)
1897		mlog_errno(status);
1898
1899	return status;
1900}
1901
1902void ocfs2_rename_unlock(struct ocfs2_super *osb)
1903{
1904	struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1905
1906	ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1907}
1908
1909int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1910{
1911	int ret;
1912	int level = ex ? LKM_EXMODE : LKM_PRMODE;
1913	struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1914	struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1915
1916	BUG_ON(!dl);
1917
1918	if (ocfs2_is_hard_readonly(osb))
1919		return -EROFS;
1920
1921	ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1922	if (ret < 0)
1923		mlog_errno(ret);
1924
1925	return ret;
1926}
1927
1928void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1929{
1930	int level = ex ? LKM_EXMODE : LKM_PRMODE;
1931	struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1932	struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1933
1934	ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1935}
1936
1937/* Reference counting of the dlm debug structure. We want this because
1938 * open references on the debug inodes can live on after a mount, so
1939 * we can't rely on the ocfs2_super to always exist. */
1940static void ocfs2_dlm_debug_free(struct kref *kref)
1941{
1942	struct ocfs2_dlm_debug *dlm_debug;
1943
1944	dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1945
1946	kfree(dlm_debug);
1947}
1948
1949void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1950{
1951	if (dlm_debug)
1952		kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1953}
1954
1955static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1956{
1957	kref_get(&debug->d_refcnt);
1958}
1959
1960struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1961{
1962	struct ocfs2_dlm_debug *dlm_debug;
1963
1964	dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1965	if (!dlm_debug) {
1966		mlog_errno(-ENOMEM);
1967		goto out;
1968	}
1969
1970	kref_init(&dlm_debug->d_refcnt);
1971	INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1972	dlm_debug->d_locking_state = NULL;
1973out:
1974	return dlm_debug;
1975}
1976
1977/* Access to this is arbitrated for us via seq_file->sem. */
1978struct ocfs2_dlm_seq_priv {
1979	struct ocfs2_dlm_debug *p_dlm_debug;
1980	struct ocfs2_lock_res p_iter_res;
1981	struct ocfs2_lock_res p_tmp_res;
1982};
1983
1984static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1985						 struct ocfs2_dlm_seq_priv *priv)
1986{
1987	struct ocfs2_lock_res *iter, *ret = NULL;
1988	struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1989
1990	assert_spin_locked(&ocfs2_dlm_tracking_lock);
1991
1992	list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1993		/* discover the head of the list */
1994		if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1995			mlog(0, "End of list found, %p\n", ret);
1996			break;
1997		}
1998
1999		/* We track our "dummy" iteration lockres' by a NULL
2000		 * l_ops field. */
2001		if (iter->l_ops != NULL) {
2002			ret = iter;
2003			break;
2004		}
2005	}
2006
2007	return ret;
2008}
2009
2010static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2011{
2012	struct ocfs2_dlm_seq_priv *priv = m->private;
2013	struct ocfs2_lock_res *iter;
2014
2015	spin_lock(&ocfs2_dlm_tracking_lock);
2016	iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2017	if (iter) {
2018		/* Since lockres' have the lifetime of their container
2019		 * (which can be inodes, ocfs2_supers, etc) we want to
2020		 * copy this out to a temporary lockres while still
2021		 * under the spinlock. Obviously after this we can't
2022		 * trust any pointers on the copy returned, but that's
2023		 * ok as the information we want isn't typically held
2024		 * in them. */
2025		priv->p_tmp_res = *iter;
2026		iter = &priv->p_tmp_res;
2027	}
2028	spin_unlock(&ocfs2_dlm_tracking_lock);
2029
2030	return iter;
2031}
2032
2033static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2034{
2035}
2036
2037static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2038{
2039	struct ocfs2_dlm_seq_priv *priv = m->private;
2040	struct ocfs2_lock_res *iter = v;
2041	struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2042
2043	spin_lock(&ocfs2_dlm_tracking_lock);
2044	iter = ocfs2_dlm_next_res(iter, priv);
2045	list_del_init(&dummy->l_debug_list);
2046	if (iter) {
2047		list_add(&dummy->l_debug_list, &iter->l_debug_list);
2048		priv->p_tmp_res = *iter;
2049		iter = &priv->p_tmp_res;
2050	}
2051	spin_unlock(&ocfs2_dlm_tracking_lock);
2052
2053	return iter;
2054}
2055
2056/* So that debugfs.ocfs2 can determine which format is being used */
2057#define OCFS2_DLM_DEBUG_STR_VERSION 1
2058static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2059{
2060	int i;
2061	char *lvb;
2062	struct ocfs2_lock_res *lockres = v;
2063
2064	if (!lockres)
2065		return -EINVAL;
2066
2067	seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2068
2069	if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2070		seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2071			   lockres->l_name,
2072			   (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2073	else
2074		seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2075
2076	seq_printf(m, "%d\t"
2077		   "0x%lx\t"
2078		   "0x%x\t"
2079		   "0x%x\t"
2080		   "%u\t"
2081		   "%u\t"
2082		   "%d\t"
2083		   "%d\t",
2084		   lockres->l_level,
2085		   lockres->l_flags,
2086		   lockres->l_action,
2087		   lockres->l_unlock_action,
2088		   lockres->l_ro_holders,
2089		   lockres->l_ex_holders,
2090		   lockres->l_requested,
2091		   lockres->l_blocking);
2092
2093	/* Dump the raw LVB */
2094	lvb = lockres->l_lksb.lvb;
2095	for(i = 0; i < DLM_LVB_LEN; i++)
2096		seq_printf(m, "0x%x\t", lvb[i]);
2097
2098	/* End the line */
2099	seq_printf(m, "\n");
2100	return 0;
2101}
2102
2103static struct seq_operations ocfs2_dlm_seq_ops = {
2104	.start =	ocfs2_dlm_seq_start,
2105	.stop =		ocfs2_dlm_seq_stop,
2106	.next =		ocfs2_dlm_seq_next,
2107	.show =		ocfs2_dlm_seq_show,
2108};
2109
2110static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2111{
2112	struct seq_file *seq = (struct seq_file *) file->private_data;
2113	struct ocfs2_dlm_seq_priv *priv = seq->private;
2114	struct ocfs2_lock_res *res = &priv->p_iter_res;
2115
2116	ocfs2_remove_lockres_tracking(res);
2117	ocfs2_put_dlm_debug(priv->p_dlm_debug);
2118	return seq_release_private(inode, file);
2119}
2120
2121static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2122{
2123	int ret;
2124	struct ocfs2_dlm_seq_priv *priv;
2125	struct seq_file *seq;
2126	struct ocfs2_super *osb;
2127
2128	priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2129	if (!priv) {
2130		ret = -ENOMEM;
2131		mlog_errno(ret);
2132		goto out;
2133	}
2134	osb = (struct ocfs2_super *) inode->u.generic_ip;
2135	ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2136	priv->p_dlm_debug = osb->osb_dlm_debug;
2137	INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2138
2139	ret = seq_open(file, &ocfs2_dlm_seq_ops);
2140	if (ret) {
2141		kfree(priv);
2142		mlog_errno(ret);
2143		goto out;
2144	}
2145
2146	seq = (struct seq_file *) file->private_data;
2147	seq->private = priv;
2148
2149	ocfs2_add_lockres_tracking(&priv->p_iter_res,
2150				   priv->p_dlm_debug);
2151
2152out:
2153	return ret;
2154}
2155
2156static const struct file_operations ocfs2_dlm_debug_fops = {
2157	.open =		ocfs2_dlm_debug_open,
2158	.release =	ocfs2_dlm_debug_release,
2159	.read =		seq_read,
2160	.llseek =	seq_lseek,
2161};
2162
2163static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2164{
2165	int ret = 0;
2166	struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2167
2168	dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2169							 S_IFREG|S_IRUSR,
2170							 osb->osb_debug_root,
2171							 osb,
2172							 &ocfs2_dlm_debug_fops);
2173	if (!dlm_debug->d_locking_state) {
2174		ret = -EINVAL;
2175		mlog(ML_ERROR,
2176		     "Unable to create locking state debugfs file.\n");
2177		goto out;
2178	}
2179
2180	ocfs2_get_dlm_debug(dlm_debug);
2181out:
2182	return ret;
2183}
2184
2185static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2186{
2187	struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2188
2189	if (dlm_debug) {
2190		debugfs_remove(dlm_debug->d_locking_state);
2191		ocfs2_put_dlm_debug(dlm_debug);
2192	}
2193}
2194
2195int ocfs2_dlm_init(struct ocfs2_super *osb)
2196{
2197	int status;
2198	u32 dlm_key;
2199	struct dlm_ctxt *dlm;
2200
2201	mlog_entry_void();
2202
2203	status = ocfs2_dlm_init_debug(osb);
2204	if (status < 0) {
2205		mlog_errno(status);
2206		goto bail;
2207	}
2208
2209	/* launch vote thread */
2210	osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
2211	if (IS_ERR(osb->vote_task)) {
2212		status = PTR_ERR(osb->vote_task);
2213		osb->vote_task = NULL;
2214		mlog_errno(status);
2215		goto bail;
2216	}
2217
2218	/* used by the dlm code to make message headers unique, each
2219	 * node in this domain must agree on this. */
2220	dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2221
2222	/* for now, uuid == domain */
2223	dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2224	if (IS_ERR(dlm)) {
2225		status = PTR_ERR(dlm);
2226		mlog_errno(status);
2227		goto bail;
2228	}
2229
2230	ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2231	ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2232
2233	dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2234
2235	osb->dlm = dlm;
2236
2237	status = 0;
2238bail:
2239	if (status < 0) {
2240		ocfs2_dlm_shutdown_debug(osb);
2241		if (osb->vote_task)
2242			kthread_stop(osb->vote_task);
2243	}
2244
2245	mlog_exit(status);
2246	return status;
2247}
2248
2249void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2250{
2251	mlog_entry_void();
2252
2253	dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2254
2255	ocfs2_drop_osb_locks(osb);
2256
2257	if (osb->vote_task) {
2258		kthread_stop(osb->vote_task);
2259		osb->vote_task = NULL;
2260	}
2261
2262	ocfs2_lock_res_free(&osb->osb_super_lockres);
2263	ocfs2_lock_res_free(&osb->osb_rename_lockres);
2264
2265	dlm_unregister_domain(osb->dlm);
2266	osb->dlm = NULL;
2267
2268	ocfs2_dlm_shutdown_debug(osb);
2269
2270	mlog_exit_void();
2271}
2272
2273static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
2274{
2275	struct ocfs2_lock_res *lockres = opaque;
2276	unsigned long flags;
2277
2278	mlog_entry_void();
2279
2280	mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2281	     lockres->l_unlock_action);
2282
2283	spin_lock_irqsave(&lockres->l_lock, flags);
2284	/* We tried to cancel a convert request, but it was already
2285	 * granted. All we want to do here is clear our unlock
2286	 * state. The wake_up call done at the bottom is redundant
2287	 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2288	 * hurt anything anyway */
2289	if (status == DLM_CANCELGRANT &&
2290	    lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2291		mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2292
2293		/* We don't clear the busy flag in this case as it
2294		 * should have been cleared by the ast which the dlm
2295		 * has called. */
2296		goto complete_unlock;
2297	}
2298
2299	if (status != DLM_NORMAL) {
2300		mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2301		     "unlock_action %d\n", status, lockres->l_name,
2302		     lockres->l_unlock_action);
2303		spin_unlock_irqrestore(&lockres->l_lock, flags);
2304		return;
2305	}
2306
2307	switch(lockres->l_unlock_action) {
2308	case OCFS2_UNLOCK_CANCEL_CONVERT:
2309		mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2310		lockres->l_action = OCFS2_AST_INVALID;
2311		break;
2312	case OCFS2_UNLOCK_DROP_LOCK:
2313		lockres->l_level = LKM_IVMODE;
2314		break;
2315	default:
2316		BUG();
2317	}
2318
2319	lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2320complete_unlock:
2321	lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2322	spin_unlock_irqrestore(&lockres->l_lock, flags);
2323
2324	wake_up(&lockres->l_event);
2325
2326	mlog_exit_void();
2327}
2328
2329typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
2330
2331struct drop_lock_cb {
2332	ocfs2_pre_drop_cb_t	*drop_func;
2333	void			*drop_data;
2334};
2335
2336static int ocfs2_drop_lock(struct ocfs2_super *osb,
2337			   struct ocfs2_lock_res *lockres,
2338			   struct drop_lock_cb *dcb)
2339{
2340	enum dlm_status status;
2341	unsigned long flags;
2342
2343	/* We didn't get anywhere near actually using this lockres. */
2344	if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2345		goto out;
2346
2347	spin_lock_irqsave(&lockres->l_lock, flags);
2348
2349	mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2350			"lockres %s, flags 0x%lx\n",
2351			lockres->l_name, lockres->l_flags);
2352
2353	while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2354		mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2355		     "%u, unlock_action = %u\n",
2356		     lockres->l_name, lockres->l_flags, lockres->l_action,
2357		     lockres->l_unlock_action);
2358
2359		spin_unlock_irqrestore(&lockres->l_lock, flags);
2360
2361		/* XXX: Today we just wait on any busy
2362		 * locks... Perhaps we need to cancel converts in the
2363		 * future? */
2364		ocfs2_wait_on_busy_lock(lockres);
2365
2366		spin_lock_irqsave(&lockres->l_lock, flags);
2367	}
2368
2369	if (dcb)
2370		dcb->drop_func(lockres, dcb->drop_data);
2371
2372	if (lockres->l_flags & OCFS2_LOCK_BUSY)
2373		mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2374		     lockres->l_name);
2375	if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2376		mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2377
2378	if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2379		spin_unlock_irqrestore(&lockres->l_lock, flags);
2380		goto out;
2381	}
2382
2383	lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2384
2385	/* make sure we never get here while waiting for an ast to
2386	 * fire. */
2387	BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2388
2389	/* is this necessary? */
2390	lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2391	lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2392	spin_unlock_irqrestore(&lockres->l_lock, flags);
2393
2394	mlog(0, "lock %s\n", lockres->l_name);
2395
2396	status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
2397			   lockres->l_ops->unlock_ast, lockres);
2398	if (status != DLM_NORMAL) {
2399		ocfs2_log_dlm_error("dlmunlock", status, lockres);
2400		mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2401		dlm_print_one_lock(lockres->l_lksb.lockid);
2402		BUG();
2403	}
2404	mlog(0, "lock %s, successfull return from dlmunlock\n",
2405	     lockres->l_name);
2406
2407	ocfs2_wait_on_busy_lock(lockres);
2408out:
2409	mlog_exit(0);
2410	return 0;
2411}
2412
2413/* Mark the lockres as being dropped. It will no longer be
2414 * queued if blocking, but we still may have to wait on it
2415 * being dequeued from the vote thread before we can consider
2416 * it safe to drop.
2417 *
2418 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2419void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2420{
2421	int status;
2422	struct ocfs2_mask_waiter mw;
2423	unsigned long flags;
2424
2425	ocfs2_init_mask_waiter(&mw);
2426
2427	spin_lock_irqsave(&lockres->l_lock, flags);
2428	lockres->l_flags |= OCFS2_LOCK_FREEING;
2429	while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2430		lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2431		spin_unlock_irqrestore(&lockres->l_lock, flags);
2432
2433		mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2434
2435		status = ocfs2_wait_for_mask(&mw);
2436		if (status)
2437			mlog_errno(status);
2438
2439		spin_lock_irqsave(&lockres->l_lock, flags);
2440	}
2441	spin_unlock_irqrestore(&lockres->l_lock, flags);
2442}
2443
2444void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2445			       struct ocfs2_lock_res *lockres)
2446{
2447	int ret;
2448
2449	ocfs2_mark_lockres_freeing(lockres);
2450	ret = ocfs2_drop_lock(osb, lockres, NULL);
2451	if (ret)
2452		mlog_errno(ret);
2453}
2454
2455static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2456{
2457	ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2458	ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
2459}
2460
2461static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
2462{
2463	struct inode *inode = data;
2464
2465	/* the metadata lock requires a bit more work as we have an
2466	 * LVB to worry about. */
2467	if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2468	    lockres->l_level == LKM_EXMODE &&
2469	    !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2470		__ocfs2_stuff_meta_lvb(inode);
2471}
2472
2473int ocfs2_drop_inode_locks(struct inode *inode)
2474{
2475	int status, err;
2476	struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
2477
2478	mlog_entry_void();
2479
2480	/* No need to call ocfs2_mark_lockres_freeing here -
2481	 * ocfs2_clear_inode has done it for us. */
2482
2483	err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2484			      &OCFS2_I(inode)->ip_data_lockres,
2485			      NULL);
2486	if (err < 0)
2487		mlog_errno(err);
2488
2489	status = err;
2490
2491	err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2492			      &OCFS2_I(inode)->ip_meta_lockres,
2493			      &meta_dcb);
2494	if (err < 0)
2495		mlog_errno(err);
2496	if (err < 0 && !status)
2497		status = err;
2498
2499	err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
2500			      &OCFS2_I(inode)->ip_rw_lockres,
2501			      NULL);
2502	if (err < 0)
2503		mlog_errno(err);
2504	if (err < 0 && !status)
2505		status = err;
2506
2507	mlog_exit(status);
2508	return status;
2509}
2510
2511static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2512				      int new_level)
2513{
2514	assert_spin_locked(&lockres->l_lock);
2515
2516	BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2517
2518	if (lockres->l_level <= new_level) {
2519		mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2520		     lockres->l_level, new_level);
2521		BUG();
2522	}
2523
2524	mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2525	     lockres->l_name, new_level, lockres->l_blocking);
2526
2527	lockres->l_action = OCFS2_AST_DOWNCONVERT;
2528	lockres->l_requested = new_level;
2529	lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2530}
2531
2532static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2533				  struct ocfs2_lock_res *lockres,
2534				  int new_level,
2535				  int lvb)
2536{
2537	int ret, dlm_flags = LKM_CONVERT;
2538	enum dlm_status status;
2539
2540	mlog_entry_void();
2541
2542	if (lvb)
2543		dlm_flags |= LKM_VALBLK;
2544
2545	status = dlmlock(osb->dlm,
2546			 new_level,
2547			 &lockres->l_lksb,
2548			 dlm_flags,
2549			 lockres->l_name,
2550			 OCFS2_LOCK_ID_MAX_LEN - 1,
2551			 lockres->l_ops->ast,
2552			 lockres,
2553			 lockres->l_ops->bast);
2554	if (status != DLM_NORMAL) {
2555		ocfs2_log_dlm_error("dlmlock", status, lockres);
2556		ret = -EINVAL;
2557		ocfs2_recover_from_dlm_error(lockres, 1);
2558		goto bail;
2559	}
2560
2561	ret = 0;
2562bail:
2563	mlog_exit(ret);
2564	return ret;
2565}
2566
2567/* returns 1 when the caller should unlock and call dlmunlock */
2568static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2569				        struct ocfs2_lock_res *lockres)
2570{
2571	assert_spin_locked(&lockres->l_lock);
2572
2573	mlog_entry_void();
2574	mlog(0, "lock %s\n", lockres->l_name);
2575
2576	if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2577		/* If we're already trying to cancel a lock conversion
2578		 * then just drop the spinlock and allow the caller to
2579		 * requeue this lock. */
2580
2581		mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2582		return 0;
2583	}
2584
2585	/* were we in a convert when we got the bast fire? */
2586	BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2587	       lockres->l_action != OCFS2_AST_DOWNCONVERT);
2588	/* set things up for the unlockast to know to just
2589	 * clear out the ast_action and unset busy, etc. */
2590	lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2591
2592	mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2593			"lock %s, invalid flags: 0x%lx\n",
2594			lockres->l_name, lockres->l_flags);
2595
2596	return 1;
2597}
2598
2599static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2600				struct ocfs2_lock_res *lockres)
2601{
2602	int ret;
2603	enum dlm_status status;
2604
2605	mlog_entry_void();
2606	mlog(0, "lock %s\n", lockres->l_name);
2607
2608	ret = 0;
2609	status = dlmunlock(osb->dlm,
2610			   &lockres->l_lksb,
2611			   LKM_CANCEL,
2612			   lockres->l_ops->unlock_ast,
2613			   lockres);
2614	if (status != DLM_NORMAL) {
2615		ocfs2_log_dlm_error("dlmunlock", status, lockres);
2616		ret = -EINVAL;
2617		ocfs2_recover_from_dlm_error(lockres, 0);
2618	}
2619
2620	mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2621
2622	mlog_exit(ret);
2623	return ret;
2624}
2625
2626static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
2627						  struct ocfs2_lock_res *lockres,
2628						  int new_level)
2629{
2630	int ret;
2631
2632	mlog_entry_void();
2633
2634	BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2635
2636	if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2637		ret = 0;
2638		mlog(0, "lockres %s currently being refreshed -- backing "
2639		     "off!\n", lockres->l_name);
2640	} else if (new_level == LKM_PRMODE)
2641		ret = !lockres->l_ex_holders &&
2642			ocfs2_inode_fully_checkpointed(inode);
2643	else /* Must be NLMODE we're converting to. */
2644		ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
2645			ocfs2_inode_fully_checkpointed(inode);
2646
2647	mlog_exit(ret);
2648	return ret;
2649}
2650
2651static int ocfs2_do_unblock_meta(struct inode *inode,
2652				 int *requeue)
2653{
2654	int new_level;
2655	int set_lvb = 0;
2656	int ret = 0;
2657	struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
2658	unsigned long flags;
2659
2660	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2661
2662	mlog_entry_void();
2663
2664	spin_lock_irqsave(&lockres->l_lock, flags);
2665
2666	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2667
2668	mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
2669	     lockres->l_blocking);
2670
2671	BUG_ON(lockres->l_level != LKM_EXMODE &&
2672	       lockres->l_level != LKM_PRMODE);
2673
2674	if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2675		*requeue = 1;
2676		ret = ocfs2_prepare_cancel_convert(osb, lockres);
2677		spin_unlock_irqrestore(&lockres->l_lock, flags);
2678		if (ret) {
2679			ret = ocfs2_cancel_convert(osb, lockres);
2680			if (ret < 0)
2681				mlog_errno(ret);
2682		}
2683		goto leave;
2684	}
2685
2686	new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2687
2688	mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
2689	     lockres->l_level, lockres->l_blocking, new_level);
2690
2691	if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
2692		if (lockres->l_level == LKM_EXMODE)
2693			set_lvb = 1;
2694
2695		/* If the lock hasn't been refreshed yet (rare), then
2696		 * our memory inode values are old and we skip
2697		 * stuffing the lvb. There's no need to actually clear
2698		 * out the lvb here as it's value is still valid. */
2699		if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2700			if (set_lvb)
2701				__ocfs2_stuff_meta_lvb(inode);
2702		} else
2703			mlog(0, "lockres %s: downconverting stale lock!\n",
2704			     lockres->l_name);
2705
2706		mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
2707		     "l_blocking=%d, new_level=%d\n",
2708		     lockres->l_level, lockres->l_blocking, new_level);
2709
2710		ocfs2_prepare_downconvert(lockres, new_level);
2711		spin_unlock_irqrestore(&lockres->l_lock, flags);
2712		ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
2713		goto leave;
2714	}
2715	if (!ocfs2_inode_fully_checkpointed(inode))
2716		ocfs2_start_checkpoint(osb);
2717
2718	*requeue = 1;
2719	spin_unlock_irqrestore(&lockres->l_lock, flags);
2720	ret = 0;
2721leave:
2722	mlog_exit(ret);
2723	return ret;
2724}
2725
2726static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
2727				      struct ocfs2_lock_res *lockres,
2728				      struct ocfs2_unblock_ctl *ctl,
2729				      ocfs2_convert_worker_t *worker)
2730{
2731	unsigned long flags;
2732	int blocking;
2733	int new_level;
2734	int ret = 0;
2735
2736	mlog_entry_void();
2737
2738	spin_lock_irqsave(&lockres->l_lock, flags);
2739
2740	BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2741
2742recheck:
2743	if (lockres->l_flags & OCFS2_LOCK_BUSY) {
2744		ctl->requeue = 1;
2745		ret = ocfs2_prepare_cancel_convert(osb, lockres);
2746		spin_unlock_irqrestore(&lockres->l_lock, flags);
2747		if (ret) {
2748			ret = ocfs2_cancel_convert(osb, lockres);
2749			if (ret < 0)
2750				mlog_errno(ret);
2751		}
2752		goto leave;
2753	}
2754
2755	/* if we're blocking an exclusive and we have *any* holders,
2756	 * then requeue. */
2757	if ((lockres->l_blocking == LKM_EXMODE)
2758	    && (lockres->l_ex_holders || lockres->l_ro_holders)) {
2759		spin_unlock_irqrestore(&lockres->l_lock, flags);
2760		ctl->requeue = 1;
2761		ret = 0;
2762		goto leave;
2763	}
2764
2765	/* If it's a PR we're blocking, then only
2766	 * requeue if we've got any EX holders */
2767	if (lockres->l_blocking == LKM_PRMODE &&
2768	    lockres->l_ex_holders) {
2769		spin_unlock_irqrestore(&lockres->l_lock, flags);
2770		ctl->requeue = 1;
2771		ret = 0;
2772		goto leave;
2773	}
2774
2775	/* If we get here, then we know that there are no more
2776	 * incompatible holders (and anyone asking for an incompatible
2777	 * lock is blocked). We can now downconvert the lock */
2778	if (!worker)
2779		goto downconvert;
2780
2781	/* Some lockres types want to do a bit of work before
2782	 * downconverting a lock. Allow that here. The worker function
2783	 * may sleep, so we save off a copy of what we're blocking as
2784	 * it may change while we're not holding the spin lock. */
2785	blocking = lockres->l_blocking;
2786	spin_unlock_irqrestore(&lockres->l_lock, flags);
2787
2788	ctl->unblock_action = worker(lockres, blocking);
2789
2790	if (ctl->unblock_action == UNBLOCK_STOP_POST)
2791		goto leave;
2792
2793	spin_lock_irqsave(&lockres->l_lock, flags);
2794	if (blocking != lockres->l_blocking) {
2795		/* If this changed underneath us, then we can't drop
2796		 * it just yet. */
2797		goto recheck;
2798	}
2799
2800downconvert:
2801	ctl->requeue = 0;
2802	new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2803
2804	ocfs2_prepare_downconvert(lockres, new_level);
2805	spin_unlock_irqrestore(&lockres->l_lock, flags);
2806	ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
2807leave:
2808	mlog_exit(ret);
2809	return ret;
2810}
2811
2812static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2813				     int blocking)
2814{
2815	struct inode *inode;
2816	struct address_space *mapping;
2817
2818       	inode = ocfs2_lock_res_inode(lockres);
2819	mapping = inode->i_mapping;
2820
2821	if (filemap_fdatawrite(mapping)) {
2822		mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2823		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
2824	}
2825	sync_mapping_buffers(mapping);
2826	if (blocking == LKM_EXMODE) {
2827		truncate_inode_pages(mapping, 0);
2828		unmap_mapping_range(mapping, 0, 0, 0);
2829	} else {
2830		/* We only need to wait on the I/O if we're not also
2831		 * truncating pages because truncate_inode_pages waits
2832		 * for us above. We don't truncate pages if we're
2833		 * blocking anything < EXMODE because we want to keep
2834		 * them around in that case. */
2835		filemap_fdatawait(mapping);
2836	}
2837
2838	return UNBLOCK_CONTINUE;
2839}
2840
2841int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
2842		       struct ocfs2_unblock_ctl *ctl)
2843{
2844	int status;
2845	struct inode *inode;
2846	struct ocfs2_super *osb;
2847
2848	mlog_entry_void();
2849
2850	inode = ocfs2_lock_res_inode(lockres);
2851	osb = OCFS2_SB(inode->i_sb);
2852
2853	mlog(0, "unblock inode %llu\n",
2854	     (unsigned long long)OCFS2_I(inode)->ip_blkno);
2855
2856	status = ocfs2_generic_unblock_lock(osb, lockres, ctl,
2857					    ocfs2_data_convert_worker);
2858	if (status < 0)
2859		mlog_errno(status);
2860
2861	mlog(0, "inode %llu, requeue = %d\n",
2862	     (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2863
2864	mlog_exit(status);
2865	return status;
2866}
2867
2868static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
2869				    struct ocfs2_unblock_ctl *ctl)
2870{
2871	int status;
2872	struct inode *inode;
2873
2874	mlog_entry_void();
2875
2876	mlog(0, "Unblock lockres %s\n", lockres->l_name);
2877
2878	inode  = ocfs2_lock_res_inode(lockres);
2879
2880	status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
2881					    lockres, ctl, NULL);
2882	if (status < 0)
2883		mlog_errno(status);
2884
2885	mlog_exit(status);
2886	return status;
2887}
2888
2889static int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
2890			      struct ocfs2_unblock_ctl *ctl)
2891{
2892	int status;
2893	struct inode *inode;
2894
2895	mlog_entry_void();
2896
2897       	inode = ocfs2_lock_res_inode(lockres);
2898
2899	mlog(0, "unblock inode %llu\n",
2900	     (unsigned long long)OCFS2_I(inode)->ip_blkno);
2901
2902	status = ocfs2_do_unblock_meta(inode, &ctl->requeue);
2903	if (status < 0)
2904		mlog_errno(status);
2905
2906	mlog(0, "inode %llu, requeue = %d\n",
2907	     (unsigned long long)OCFS2_I(inode)->ip_blkno, ctl->requeue);
2908
2909	mlog_exit(status);
2910	return status;
2911}
2912
2913/*
2914 * Does the final reference drop on our dentry lock. Right now this
2915 * happens in the vote thread, but we could choose to simplify the
2916 * dlmglue API and push these off to the ocfs2_wq in the future.
2917 */
2918static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2919				     struct ocfs2_lock_res *lockres)
2920{
2921	struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2922	ocfs2_dentry_lock_put(osb, dl);
2923}
2924
2925/*
2926 * d_delete() matching dentries before the lock downconvert.
2927 *
2928 * At this point, any process waiting to destroy the
2929 * dentry_lock due to last ref count is stopped by the
2930 * OCFS2_LOCK_QUEUED flag.
2931 *
2932 * We have two potential problems
2933 *
2934 * 1) If we do the last reference drop on our dentry_lock (via dput)
2935 *    we'll wind up in ocfs2_release_dentry_lock(), waiting on
2936 *    the downconvert to finish. Instead we take an elevated
2937 *    reference and push the drop until after we've completed our
2938 *    unblock processing.
2939 *
2940 * 2) There might be another process with a final reference,
2941 *    waiting on us to finish processing. If this is the case, we
2942 *    detect it and exit out - there's no more dentries anyway.
2943 */
2944static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2945				       int blocking)
2946{
2947	struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2948	struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2949	struct dentry *dentry;
2950	unsigned long flags;
2951	int extra_ref = 0;
2952
2953	/*
2954	 * This node is blocking another node from getting a read
2955	 * lock. This happens when we've renamed within a
2956	 * directory. We've forced the other nodes to d_delete(), but
2957	 * we never actually dropped our lock because it's still
2958	 * valid. The downconvert code will retain a PR for this node,
2959	 * so there's no further work to do.
2960	 */
2961	if (blocking == LKM_PRMODE)
2962		return UNBLOCK_CONTINUE;
2963
2964	/*
2965	 * Mark this inode as potentially orphaned. The code in
2966	 * ocfs2_delete_inode() will figure out whether it actually
2967	 * needs to be freed or not.
2968	 */
2969	spin_lock(&oi->ip_lock);
2970	oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2971	spin_unlock(&oi->ip_lock);
2972
2973	/*
2974	 * Yuck. We need to make sure however that the check of
2975	 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2976	 * respect to a reference decrement or the setting of that
2977	 * flag.
2978	 */
2979	spin_lock_irqsave(&lockres->l_lock, flags);
2980	spin_lock(&dentry_attach_lock);
2981	if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2982	    && dl->dl_count) {
2983		dl->dl_count++;
2984		extra_ref = 1;
2985	}
2986	spin_unlock(&dentry_attach_lock);
2987	spin_unlock_irqrestore(&lockres->l_lock, flags);
2988
2989	mlog(0, "extra_ref = %d\n", extra_ref);
2990
2991	/*
2992	 * We have a process waiting on us in ocfs2_dentry_iput(),
2993	 * which means we can't have any more outstanding
2994	 * aliases. There's no need to do any more work.
2995	 */
2996	if (!extra_ref)
2997		return UNBLOCK_CONTINUE;
2998
2999	spin_lock(&dentry_attach_lock);
3000	while (1) {
3001		dentry = ocfs2_find_local_alias(dl->dl_inode,
3002						dl->dl_parent_blkno, 1);
3003		if (!dentry)
3004			break;
3005		spin_unlock(&dentry_attach_lock);
3006
3007		mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3008		     dentry->d_name.name);
3009
3010		/*
3011		 * The following dcache calls may do an
3012		 * iput(). Normally we don't want that from the
3013		 * downconverting thread, but in this case it's ok
3014		 * because the requesting node already has an
3015		 * exclusive lock on the inode, so it can't be queued
3016		 * for a downconvert.
3017		 */
3018		d_delete(dentry);
3019		dput(dentry);
3020
3021		spin_lock(&dentry_attach_lock);
3022	}
3023	spin_unlock(&dentry_attach_lock);
3024
3025	/*
3026	 * If we are the last holder of this dentry lock, there is no
3027	 * reason to downconvert so skip straight to the unlock.
3028	 */
3029	if (dl->dl_count == 1)
3030		return UNBLOCK_STOP_POST;
3031
3032	return UNBLOCK_CONTINUE_POST;
3033}
3034
3035static int ocfs2_unblock_dentry_lock(struct ocfs2_lock_res *lockres,
3036				     struct ocfs2_unblock_ctl *ctl)
3037{
3038	int ret;
3039	struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3040	struct ocfs2_super *osb = OCFS2_SB(dl->dl_inode->i_sb);
3041
3042	mlog(0, "unblock dentry lock: %llu\n",
3043	     (unsigned long long)OCFS2_I(dl->dl_inode)->ip_blkno);
3044
3045	ret = ocfs2_generic_unblock_lock(osb,
3046					 lockres,
3047					 ctl,
3048					 ocfs2_dentry_convert_worker);
3049	if (ret < 0)
3050		mlog_errno(ret);
3051
3052	mlog(0, "requeue = %d, post = %d\n", ctl->requeue, ctl->unblock_action);
3053
3054	return ret;
3055}
3056
3057/* Generic unblock function for any lockres whose private data is an
3058 * ocfs2_super pointer. */
3059static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
3060				  struct ocfs2_unblock_ctl *ctl)
3061{
3062	int status;
3063	struct ocfs2_super *osb;
3064
3065	mlog_entry_void();
3066
3067	mlog(0, "Unblock lockres %s\n", lockres->l_name);
3068
3069	osb = ocfs2_lock_res_super(lockres);
3070
3071	status = ocfs2_generic_unblock_lock(osb,
3072					    lockres,
3073					    ctl,
3074					    NULL);
3075	if (status < 0)
3076		mlog_errno(status);
3077
3078	mlog_exit(status);
3079	return status;
3080}
3081
3082void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3083				struct ocfs2_lock_res *lockres)
3084{
3085	int status;
3086	struct ocfs2_unblock_ctl ctl = {0, 0,};
3087	unsigned long flags;
3088
3089	/* Our reference to the lockres in this function can be
3090	 * considered valid until we remove the OCFS2_LOCK_QUEUED
3091	 * flag. */
3092
3093	mlog_entry_void();
3094
3095	BUG_ON(!lockres);
3096	BUG_ON(!lockres->l_ops);
3097	BUG_ON(!lockres->l_ops->unblock);
3098
3099	mlog(0, "lockres %s blocked.\n", lockres->l_name);
3100
3101	/* Detect whether a lock has been marked as going away while
3102	 * the vote thread was processing other things. A lock can
3103	 * still be marked with OCFS2_LOCK_FREEING after this check,
3104	 * but short circuiting here will still save us some
3105	 * performance. */
3106	spin_lock_irqsave(&lockres->l_lock, flags);
3107	if (lockres->l_flags & OCFS2_LOCK_FREEING)
3108		goto unqueue;
3109	spin_unlock_irqrestore(&lockres->l_lock, flags);
3110
3111	status = lockres->l_ops->unblock(lockres, &ctl);
3112	if (status < 0)
3113		mlog_errno(status);
3114
3115	spin_lock_irqsave(&lockres->l_lock, flags);
3116unqueue:
3117	if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
3118		lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3119	} else
3120		ocfs2_schedule_blocked_lock(osb, lockres);
3121
3122	mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
3123	     ctl.requeue ? "yes" : "no");
3124	spin_unlock_irqrestore(&lockres->l_lock, flags);
3125
3126	if (ctl.unblock_action != UNBLOCK_CONTINUE
3127	    && lockres->l_ops->post_unlock)
3128		lockres->l_ops->post_unlock(osb, lockres);
3129
3130	mlog_exit_void();
3131}
3132
3133static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3134					struct ocfs2_lock_res *lockres)
3135{
3136	mlog_entry_void();
3137
3138	assert_spin_locked(&lockres->l_lock);
3139
3140	if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3141		/* Do not schedule a lock for downconvert when it's on
3142		 * the way to destruction - any nodes wanting access
3143		 * to the resource will get it soon. */
3144		mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3145		     lockres->l_name, lockres->l_flags);
3146		return;
3147	}
3148
3149	lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3150
3151	spin_lock(&osb->vote_task_lock);
3152	if (list_empty(&lockres->l_blocked_list)) {
3153		list_add_tail(&lockres->l_blocked_list,
3154			      &osb->blocked_lock_list);
3155		osb->blocked_lock_count++;
3156	}
3157	spin_unlock(&osb->vote_task_lock);
3158
3159	mlog_exit_void();
3160}
3161
3162/* This aids in debugging situations where a bad LVB might be involved. */
3163void ocfs2_dump_meta_lvb_info(u64 level,
3164			      const char *function,
3165			      unsigned int line,
3166			      struct ocfs2_lock_res *lockres)
3167{
3168	struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
3169
3170	mlog(level, "LVB information for %s (called from %s:%u):\n",
3171	     lockres->l_name, function, line);
3172	mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
3173	     lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
3174	     be32_to_cpu(lvb->lvb_igeneration));
3175	mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
3176	     (unsigned long long)be64_to_cpu(lvb->lvb_isize),
3177	     be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
3178	     be16_to_cpu(lvb->lvb_imode));
3179	mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
3180	     "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
3181	     (long long)be64_to_cpu(lvb->lvb_iatime_packed),
3182	     (long long)be64_to_cpu(lvb->lvb_ictime_packed),
3183	     (long long)be64_to_cpu(lvb->lvb_imtime_packed),
3184	     be32_to_cpu(lvb->lvb_iattr));
3185}
3186