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