blob: 69fba16efbd1e08241ebe46551d12b506a442b15 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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"
Mark Fashehd680efe2006-09-08 14:14:34 -070049#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080050#include "dlmglue.h"
51#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080052#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053#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
Mark Fasheh54a7e752006-09-12 21:49:13 -070071static 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);
Mark Fashehccd979b2005-12-15 14:31:24 -080073
Mark Fashehd680efe2006-09-08 14:14:34 -070074/*
Mark Fashehcc567d82006-09-13 21:52:21 -070075 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070076 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070077 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070078 * 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
Mark Fasheh810d5ae2006-09-13 21:39:52 -070094static 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
Mark Fashehcc567d82006-09-13 21:52:21 -070098static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
99 int blocking);
100
Mark Fashehcc567d82006-09-13 21:52:21 -0700101static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
102 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700103
104static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800106
Mark Fashehf625c972006-09-12 21:24:53 -0700107/*
108 * OCFS2 Lock Resource Operations
109 *
110 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700111 *
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).
Mark Fashehf625c972006-09-12 21:24:53 -0700119 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800120struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700121 /*
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 *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700126
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700127 /*
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 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700136 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700137
138 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700139 * 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
150 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700151 * 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
161 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700162 * 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
172 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700173 * LOCK_TYPE_* flags which describe the specific requirements
174 * of a lock type. Descriptions of each individual flag follow.
175 */
176 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800177};
178
Mark Fashehf625c972006-09-12 21:24:53 -0700179/*
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
Mark Fashehb80fc012006-09-12 22:08:14 -0700189/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700190 * Indicate that a lock type makes use of the lock value block. The
191 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700192 */
193#define LOCK_TYPE_USES_LVB 0x2
194
Mark Fashehccd979b2005-12-15 14:31:24 -0800195static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700196 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700197 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800198};
199
200static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700201 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700202 .check_downconvert = ocfs2_check_meta_downconvert,
203 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehb80fc012006-09-12 22:08:14 -0700204 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800205};
206
Mark Fashehccd979b2005-12-15 14:31:24 -0800207static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700208 .get_osb = ocfs2_get_inode_osb,
Mark Fashehcc567d82006-09-13 21:52:21 -0700209 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700210 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800211};
212
213static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700214 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800215};
216
217static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700218 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800219};
220
Mark Fashehd680efe2006-09-08 14:14:34 -0700221static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700222 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700223 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700224 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700225 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700226};
227
Mark Fashehccd979b2005-12-15 14:31:24 -0800228static 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800235static 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
Mark Fashehd680efe2006-09-08 14:14:34 -0700242static 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
Mark Fasheh54a7e752006-09-12 21:49:13 -0700249static 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800257static 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800285
Mark Fashehccd979b2005-12-15 14:31:24 -0800286static 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
Mark Fashehb06970532006-03-03 10:24:33 -0800297 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800300
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
Ingo Molnar34af9462006-06-27 02:53:55 -0700308static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800309
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,
Mark Fashehccd979b2005-12-15 14:31:24 -0800331 struct ocfs2_lock_res_ops *ops,
332 void *priv)
333{
Mark Fashehccd979b2005-12-15 14:31:24 -0800334 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,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700361 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800362 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
Mark Fashehd680efe2006-09-08 14:14:34 -0700382 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700383 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700384 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
385}
386
Mark Fasheh54a7e752006-09-12 21:49:13 -0700387static 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
Mark Fashehd680efe2006-09-08 14:14:34 -0700394static __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
Mark Fasheh54a7e752006-09-12 21:49:13 -0700404static 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
Mark Fashehd680efe2006-09-08 14:14:34 -0700411void 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800449}
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);
Mark Fashehd680efe2006-09-08 14:14:34 -0700457 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
458 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800459 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800460 &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);
Mark Fashehd680efe2006-09-08 14:14:34 -0700469 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,
Mark Fashehccd979b2005-12-15 14:31:24 -0800471 &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 */
Mark Fashehf625c972006-09-12 21:24:53 -0700624 if (lockres->l_level == LKM_NLMODE &&
625 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 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 &&
Mark Fashehf625c972006-09-12 21:24:53 -0700642 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
643 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800653static 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
Mark Fashehaa2623a2006-09-12 21:58:23 -0700679static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800680{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700681 struct ocfs2_lock_res *lockres = opaque;
682 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800683 int needs_downconvert;
684 unsigned long flags;
685
Mark Fashehccd979b2005-12-15 14:31:24 -0800686 BUG_ON(level <= LKM_NLMODE);
687
Mark Fashehaa2623a2006-09-12 21:58:23 -0700688 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800692 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
Mark Fashehd680efe2006-09-08 14:14:34 -0700698 wake_up(&lockres->l_event);
699
Mark Fashehccd979b2005-12-15 14:31:24 -0800700 ocfs2_kick_vote_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800701}
702
Mark Fashehe92d57d2006-09-12 21:34:35 -0700703static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800704{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700705 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 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);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700721 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 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:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700730 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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 BUG();
735 }
736
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 /* set it to something invalid so if we get called again we
738 * can catch it. */
739 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800740
741 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700742 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800743}
744
Mark Fashehccd979b2005-12-15 14:31:24 -0800745static 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;
773 enum dlm_status status;
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,
Mark Fashehf0681062006-09-08 11:40:10 -0700798 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -0700799 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -0800800 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -0700801 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 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
Mark Fashehb80fc012006-09-12 22:08:14 -0700919 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
920 lkm_flags |= LKM_VALBLK;
921
Mark Fashehccd979b2005-12-15 14:31:24 -0800922again:
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,
Mark Fashehb80fc012006-09-12 22:08:14 -0700989 lkm_flags|LKM_CONVERT,
Mark Fashehccd979b2005-12-15 14:31:24 -0800990 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -0700991 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -0700992 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -0700994 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -0800995 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
Adrian Bunkda661162006-11-20 03:24:28 +01001067static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1068 struct ocfs2_lock_res *lockres,
1069 int ex,
1070 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001071{
Mark Fashehd680efe2006-09-08 14:14:34 -07001072 int level = ex ? LKM_EXMODE : LKM_PRMODE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001073 unsigned long flags;
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001074 int lkm_flags = local ? LKM_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001075
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
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001081 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001082}
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;
Mark Fashehd680efe2006-09-08 14:14:34 -07001093 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001094
1095 BUG_ON(!inode);
1096 BUG_ON(!ocfs2_inode_is_new(inode));
1097
1098 mlog_entry_void();
1099
Mark Fashehb06970532006-03-03 10:24:33 -08001100 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001101
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
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001110 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 if (ret) {
1112 mlog_errno(ret);
1113 goto bail;
1114 }
1115
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001116 /*
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);
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 if (ret) {
1122 mlog_errno(ret);
1123 goto bail;
1124 }
1125
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001126 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001127 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;
1141
1142 BUG_ON(!inode);
1143
1144 mlog_entry_void();
1145
Mark Fashehb06970532006-03-03 10:24:33 -08001146 mlog(0, "inode %llu take %s RW lock\n",
1147 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 write ? "EXMODE" : "PRMODE");
1149
1150 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1151
1152 level = write ? LKM_EXMODE : LKM_PRMODE;
1153
1154 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1155 0);
1156 if (status < 0)
1157 mlog_errno(status);
1158
1159 mlog_exit(status);
1160 return status;
1161}
1162
1163void ocfs2_rw_unlock(struct inode *inode, int write)
1164{
1165 int level = write ? LKM_EXMODE : LKM_PRMODE;
1166 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1167
1168 mlog_entry_void();
1169
Mark Fashehb06970532006-03-03 10:24:33 -08001170 mlog(0, "inode %llu drop %s RW lock\n",
1171 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 write ? "EXMODE" : "PRMODE");
1173
1174 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1175
1176 mlog_exit_void();
1177}
1178
1179int ocfs2_data_lock_full(struct inode *inode,
1180 int write,
1181 int arg_flags)
1182{
1183 int status = 0, level;
1184 struct ocfs2_lock_res *lockres;
1185
1186 BUG_ON(!inode);
1187
1188 mlog_entry_void();
1189
Mark Fashehb06970532006-03-03 10:24:33 -08001190 mlog(0, "inode %llu take %s DATA lock\n",
1191 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 write ? "EXMODE" : "PRMODE");
1193
1194 /* We'll allow faking a readonly data lock for
1195 * rodevices. */
1196 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1197 if (write) {
1198 status = -EROFS;
1199 mlog_errno(status);
1200 }
1201 goto out;
1202 }
1203
1204 lockres = &OCFS2_I(inode)->ip_data_lockres;
1205
1206 level = write ? LKM_EXMODE : LKM_PRMODE;
1207
1208 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1209 0, arg_flags);
1210 if (status < 0 && status != -EAGAIN)
1211 mlog_errno(status);
1212
1213out:
1214 mlog_exit(status);
1215 return status;
1216}
1217
1218/* see ocfs2_meta_lock_with_page() */
1219int ocfs2_data_lock_with_page(struct inode *inode,
1220 int write,
1221 struct page *page)
1222{
1223 int ret;
1224
1225 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1226 if (ret == -EAGAIN) {
1227 unlock_page(page);
1228 if (ocfs2_data_lock(inode, write) == 0)
1229 ocfs2_data_unlock(inode, write);
1230 ret = AOP_TRUNCATED_PAGE;
1231 }
1232
1233 return ret;
1234}
1235
1236static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
1237 struct ocfs2_lock_res *lockres)
1238{
1239 int kick = 0;
1240
1241 mlog_entry_void();
1242
1243 /* If we know that another node is waiting on our lock, kick
1244 * the vote thread * pre-emptively when we reach a release
1245 * condition. */
1246 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1247 switch(lockres->l_blocking) {
1248 case LKM_EXMODE:
1249 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1250 kick = 1;
1251 break;
1252 case LKM_PRMODE:
1253 if (!lockres->l_ex_holders)
1254 kick = 1;
1255 break;
1256 default:
1257 BUG();
1258 }
1259 }
1260
1261 if (kick)
1262 ocfs2_kick_vote_thread(osb);
1263
1264 mlog_exit_void();
1265}
1266
1267void ocfs2_data_unlock(struct inode *inode,
1268 int write)
1269{
1270 int level = write ? LKM_EXMODE : LKM_PRMODE;
1271 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
1272
1273 mlog_entry_void();
1274
Mark Fashehb06970532006-03-03 10:24:33 -08001275 mlog(0, "inode %llu drop %s DATA lock\n",
1276 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 write ? "EXMODE" : "PRMODE");
1278
1279 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1280 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1281
1282 mlog_exit_void();
1283}
1284
1285#define OCFS2_SEC_BITS 34
1286#define OCFS2_SEC_SHIFT (64 - 34)
1287#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1288
1289/* LVB only has room for 64 bits of time here so we pack it for
1290 * now. */
1291static u64 ocfs2_pack_timespec(struct timespec *spec)
1292{
1293 u64 res;
1294 u64 sec = spec->tv_sec;
1295 u32 nsec = spec->tv_nsec;
1296
1297 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1298
1299 return res;
1300}
1301
1302/* Call this with the lockres locked. I am reasonably sure we don't
1303 * need ip_lock in this function as anyone who would be changing those
1304 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1305static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1306{
1307 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1308 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1309 struct ocfs2_meta_lvb *lvb;
1310
1311 mlog_entry_void();
1312
1313 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1314
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001315 /*
1316 * Invalidate the LVB of a deleted inode - this way other
1317 * nodes are forced to go to disk and discover the new inode
1318 * status.
1319 */
1320 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1321 lvb->lvb_version = 0;
1322 goto out;
1323 }
1324
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001325 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1327 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1328 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1329 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1330 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1331 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1332 lvb->lvb_iatime_packed =
1333 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1334 lvb->lvb_ictime_packed =
1335 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1336 lvb->lvb_imtime_packed =
1337 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001338 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001339 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001340
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001341out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 mlog_meta_lvb(0, lockres);
1343
1344 mlog_exit_void();
1345}
1346
1347static void ocfs2_unpack_timespec(struct timespec *spec,
1348 u64 packed_time)
1349{
1350 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1351 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1352}
1353
1354static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1355{
1356 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1357 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1358 struct ocfs2_meta_lvb *lvb;
1359
1360 mlog_entry_void();
1361
1362 mlog_meta_lvb(0, lockres);
1363
1364 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1365
1366 /* We're safe here without the lockres lock... */
1367 spin_lock(&oi->ip_lock);
1368 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1369 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1370
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001371 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
1372 ocfs2_set_inode_flags(inode);
1373
Mark Fashehccd979b2005-12-15 14:31:24 -08001374 /* fast-symlinks are a special case */
1375 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1376 inode->i_blocks = 0;
1377 else
1378 inode->i_blocks =
1379 ocfs2_align_bytes_to_sectors(i_size_read(inode));
1380
1381 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1382 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1383 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1384 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1385 ocfs2_unpack_timespec(&inode->i_atime,
1386 be64_to_cpu(lvb->lvb_iatime_packed));
1387 ocfs2_unpack_timespec(&inode->i_mtime,
1388 be64_to_cpu(lvb->lvb_imtime_packed));
1389 ocfs2_unpack_timespec(&inode->i_ctime,
1390 be64_to_cpu(lvb->lvb_ictime_packed));
1391 spin_unlock(&oi->ip_lock);
1392
1393 mlog_exit_void();
1394}
1395
Mark Fashehf9e2d822006-09-12 15:35:49 -07001396static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1397 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001398{
1399 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1400
Mark Fashehf9e2d822006-09-12 15:35:49 -07001401 if (lvb->lvb_version == OCFS2_LVB_VERSION
1402 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 return 1;
1404 return 0;
1405}
1406
1407/* Determine whether a lock resource needs to be refreshed, and
1408 * arbitrate who gets to refresh it.
1409 *
1410 * 0 means no refresh needed.
1411 *
1412 * > 0 means you need to refresh this and you MUST call
1413 * ocfs2_complete_lock_res_refresh afterwards. */
1414static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1415{
1416 unsigned long flags;
1417 int status = 0;
1418
1419 mlog_entry_void();
1420
1421refresh_check:
1422 spin_lock_irqsave(&lockres->l_lock, flags);
1423 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1424 spin_unlock_irqrestore(&lockres->l_lock, flags);
1425 goto bail;
1426 }
1427
1428 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1429 spin_unlock_irqrestore(&lockres->l_lock, flags);
1430
1431 ocfs2_wait_on_refreshing_lock(lockres);
1432 goto refresh_check;
1433 }
1434
1435 /* Ok, I'll be the one to refresh this lock. */
1436 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1437 spin_unlock_irqrestore(&lockres->l_lock, flags);
1438
1439 status = 1;
1440bail:
1441 mlog_exit(status);
1442 return status;
1443}
1444
1445/* If status is non zero, I'll mark it as not being in refresh
1446 * anymroe, but i won't clear the needs refresh flag. */
1447static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1448 int status)
1449{
1450 unsigned long flags;
1451 mlog_entry_void();
1452
1453 spin_lock_irqsave(&lockres->l_lock, flags);
1454 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1455 if (!status)
1456 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1457 spin_unlock_irqrestore(&lockres->l_lock, flags);
1458
1459 wake_up(&lockres->l_event);
1460
1461 mlog_exit_void();
1462}
1463
1464/* may or may not return a bh if it went to disk. */
1465static int ocfs2_meta_lock_update(struct inode *inode,
1466 struct buffer_head **bh)
1467{
1468 int status = 0;
1469 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1470 struct ocfs2_lock_res *lockres;
1471 struct ocfs2_dinode *fe;
1472
1473 mlog_entry_void();
1474
1475 spin_lock(&oi->ip_lock);
1476 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08001477 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001478 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001479 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001480 spin_unlock(&oi->ip_lock);
1481 status = -ENOENT;
1482 goto bail;
1483 }
1484 spin_unlock(&oi->ip_lock);
1485
1486 lockres = &oi->ip_meta_lockres;
1487
1488 if (!ocfs2_should_refresh_lock_res(lockres))
1489 goto bail;
1490
1491 /* This will discard any caching information we might have had
1492 * for the inode metadata. */
1493 ocfs2_metadata_cache_purge(inode);
1494
1495 /* will do nothing for inode types that don't use the extent
1496 * map (directories, bitmap files, etc) */
1497 ocfs2_extent_map_trunc(inode, 0);
1498
Mark Fashehf9e2d822006-09-12 15:35:49 -07001499 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001500 mlog(0, "Trusting LVB on inode %llu\n",
1501 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001502 ocfs2_refresh_inode_from_lvb(inode);
1503 } else {
1504 /* Boo, we have to go to disk. */
1505 /* read bh, cast, ocfs2_refresh_inode */
1506 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1507 bh, OCFS2_BH_CACHED, inode);
1508 if (status < 0) {
1509 mlog_errno(status);
1510 goto bail_refresh;
1511 }
1512 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1513
1514 /* This is a good chance to make sure we're not
1515 * locking an invalid object.
1516 *
1517 * We bug on a stale inode here because we checked
1518 * above whether it was wiped from disk. The wiping
1519 * node provides a guarantee that we receive that
1520 * message and can mark the inode before dropping any
1521 * locks associated with it. */
1522 if (!OCFS2_IS_VALID_DINODE(fe)) {
1523 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1524 status = -EIO;
1525 goto bail_refresh;
1526 }
1527 mlog_bug_on_msg(inode->i_generation !=
1528 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08001529 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001530 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001531 (unsigned long long)oi->ip_blkno,
1532 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 inode->i_generation);
1534 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1535 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08001536 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1537 (unsigned long long)oi->ip_blkno,
1538 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 le32_to_cpu(fe->i_flags));
1540
1541 ocfs2_refresh_inode(inode, fe);
1542 }
1543
1544 status = 0;
1545bail_refresh:
1546 ocfs2_complete_lock_res_refresh(lockres, status);
1547bail:
1548 mlog_exit(status);
1549 return status;
1550}
1551
1552static int ocfs2_assign_bh(struct inode *inode,
1553 struct buffer_head **ret_bh,
1554 struct buffer_head *passed_bh)
1555{
1556 int status;
1557
1558 if (passed_bh) {
1559 /* Ok, the update went to disk for us, use the
1560 * returned bh. */
1561 *ret_bh = passed_bh;
1562 get_bh(*ret_bh);
1563
1564 return 0;
1565 }
1566
1567 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1568 OCFS2_I(inode)->ip_blkno,
1569 ret_bh,
1570 OCFS2_BH_CACHED,
1571 inode);
1572 if (status < 0)
1573 mlog_errno(status);
1574
1575 return status;
1576}
1577
1578/*
1579 * returns < 0 error if the callback will never be called, otherwise
1580 * the result of the lock will be communicated via the callback.
1581 */
1582int ocfs2_meta_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001583 struct buffer_head **ret_bh,
1584 int ex,
1585 int arg_flags)
1586{
1587 int status, level, dlm_flags, acquired;
1588 struct ocfs2_lock_res *lockres;
1589 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1590 struct buffer_head *local_bh = NULL;
1591
1592 BUG_ON(!inode);
1593
1594 mlog_entry_void();
1595
Mark Fashehb06970532006-03-03 10:24:33 -08001596 mlog(0, "inode %llu, take %s META lock\n",
1597 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001598 ex ? "EXMODE" : "PRMODE");
1599
1600 status = 0;
1601 acquired = 0;
1602 /* We'll allow faking a readonly metadata lock for
1603 * rodevices. */
1604 if (ocfs2_is_hard_readonly(osb)) {
1605 if (ex)
1606 status = -EROFS;
1607 goto bail;
1608 }
1609
1610 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1611 wait_event(osb->recovery_event,
1612 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1613
1614 acquired = 0;
1615 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1616 level = ex ? LKM_EXMODE : LKM_PRMODE;
1617 dlm_flags = 0;
1618 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1619 dlm_flags |= LKM_NOQUEUE;
1620
1621 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1622 if (status < 0) {
1623 if (status != -EAGAIN && status != -EIOCBRETRY)
1624 mlog_errno(status);
1625 goto bail;
1626 }
1627
1628 /* Notify the error cleanup path to drop the cluster lock. */
1629 acquired = 1;
1630
1631 /* We wait twice because a node may have died while we were in
1632 * the lower dlm layers. The second time though, we've
1633 * committed to owning this lock so we don't allow signals to
1634 * abort the operation. */
1635 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1636 wait_event(osb->recovery_event,
1637 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1638
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001639 /*
1640 * We only see this flag if we're being called from
1641 * ocfs2_read_locked_inode(). It means we're locking an inode
1642 * which hasn't been populated yet, so clear the refresh flag
1643 * and let the caller handle it.
1644 */
1645 if (inode->i_state & I_NEW) {
1646 status = 0;
1647 ocfs2_complete_lock_res_refresh(lockres, 0);
1648 goto bail;
1649 }
1650
Mark Fashehccd979b2005-12-15 14:31:24 -08001651 /* This is fun. The caller may want a bh back, or it may
1652 * not. ocfs2_meta_lock_update definitely wants one in, but
1653 * may or may not read one, depending on what's in the
1654 * LVB. The result of all of this is that we've *only* gone to
1655 * disk if we have to, so the complexity is worthwhile. */
1656 status = ocfs2_meta_lock_update(inode, &local_bh);
1657 if (status < 0) {
1658 if (status != -ENOENT)
1659 mlog_errno(status);
1660 goto bail;
1661 }
1662
1663 if (ret_bh) {
1664 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1665 if (status < 0) {
1666 mlog_errno(status);
1667 goto bail;
1668 }
1669 }
1670
Mark Fashehccd979b2005-12-15 14:31:24 -08001671bail:
1672 if (status < 0) {
1673 if (ret_bh && (*ret_bh)) {
1674 brelse(*ret_bh);
1675 *ret_bh = NULL;
1676 }
1677 if (acquired)
1678 ocfs2_meta_unlock(inode, ex);
1679 }
1680
1681 if (local_bh)
1682 brelse(local_bh);
1683
1684 mlog_exit(status);
1685 return status;
1686}
1687
1688/*
1689 * This is working around a lock inversion between tasks acquiring DLM locks
1690 * while holding a page lock and the vote thread which blocks dlm lock acquiry
1691 * while acquiring page locks.
1692 *
1693 * ** These _with_page variantes are only intended to be called from aop
1694 * methods that hold page locks and return a very specific *positive* error
1695 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1696 *
1697 * The DLM is called such that it returns -EAGAIN if it would have blocked
1698 * waiting for the vote thread. In that case we unlock our page so the vote
1699 * thread can make progress. Once we've done this we have to return
1700 * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
1701 * into the VFS who will then immediately retry the aop call.
1702 *
1703 * We do a blocking lock and immediate unlock before returning, though, so that
1704 * the lock has a great chance of being cached on this node by the time the VFS
1705 * calls back to retry the aop. This has a potential to livelock as nodes
1706 * ping locks back and forth, but that's a risk we're willing to take to avoid
1707 * the lock inversion simply.
1708 */
1709int ocfs2_meta_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001710 struct buffer_head **ret_bh,
1711 int ex,
1712 struct page *page)
1713{
1714 int ret;
1715
Mark Fasheh4bcec182006-10-09 16:02:40 -07001716 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08001717 if (ret == -EAGAIN) {
1718 unlock_page(page);
Mark Fasheh4bcec182006-10-09 16:02:40 -07001719 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 ocfs2_meta_unlock(inode, ex);
1721 ret = AOP_TRUNCATED_PAGE;
1722 }
1723
1724 return ret;
1725}
1726
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001727int ocfs2_meta_lock_atime(struct inode *inode,
1728 struct vfsmount *vfsmnt,
1729 int *level)
1730{
1731 int ret;
1732
1733 mlog_entry_void();
1734 ret = ocfs2_meta_lock(inode, NULL, 0);
1735 if (ret < 0) {
1736 mlog_errno(ret);
1737 return ret;
1738 }
1739
1740 /*
1741 * If we should update atime, we will get EX lock,
1742 * otherwise we just get PR lock.
1743 */
1744 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1745 struct buffer_head *bh = NULL;
1746
1747 ocfs2_meta_unlock(inode, 0);
1748 ret = ocfs2_meta_lock(inode, &bh, 1);
1749 if (ret < 0) {
1750 mlog_errno(ret);
1751 return ret;
1752 }
1753 *level = 1;
1754 if (ocfs2_should_update_atime(inode, vfsmnt))
1755 ocfs2_update_inode_atime(inode, bh);
1756 if (bh)
1757 brelse(bh);
1758 } else
1759 *level = 0;
1760
1761 mlog_exit(ret);
1762 return ret;
1763}
1764
Mark Fashehccd979b2005-12-15 14:31:24 -08001765void ocfs2_meta_unlock(struct inode *inode,
1766 int ex)
1767{
1768 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1769 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
1770
1771 mlog_entry_void();
1772
Mark Fashehb06970532006-03-03 10:24:33 -08001773 mlog(0, "inode %llu drop %s META lock\n",
1774 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001775 ex ? "EXMODE" : "PRMODE");
1776
1777 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
1778 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1779
1780 mlog_exit_void();
1781}
1782
1783int ocfs2_super_lock(struct ocfs2_super *osb,
1784 int ex)
1785{
1786 int status;
1787 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1788 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1789 struct buffer_head *bh;
1790 struct ocfs2_slot_info *si = osb->slot_info;
1791
1792 mlog_entry_void();
1793
1794 if (ocfs2_is_hard_readonly(osb))
1795 return -EROFS;
1796
1797 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1798 if (status < 0) {
1799 mlog_errno(status);
1800 goto bail;
1801 }
1802
1803 /* The super block lock path is really in the best position to
1804 * know when resources covered by the lock need to be
1805 * refreshed, so we do it here. Of course, making sense of
1806 * everything is up to the caller :) */
1807 status = ocfs2_should_refresh_lock_res(lockres);
1808 if (status < 0) {
1809 mlog_errno(status);
1810 goto bail;
1811 }
1812 if (status) {
1813 bh = si->si_bh;
1814 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1815 si->si_inode);
1816 if (status == 0)
1817 ocfs2_update_slot_info(si);
1818
1819 ocfs2_complete_lock_res_refresh(lockres, status);
1820
1821 if (status < 0)
1822 mlog_errno(status);
1823 }
1824bail:
1825 mlog_exit(status);
1826 return status;
1827}
1828
1829void ocfs2_super_unlock(struct ocfs2_super *osb,
1830 int ex)
1831{
1832 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1833 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1834
1835 ocfs2_cluster_unlock(osb, lockres, level);
1836}
1837
1838int ocfs2_rename_lock(struct ocfs2_super *osb)
1839{
1840 int status;
1841 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1842
1843 if (ocfs2_is_hard_readonly(osb))
1844 return -EROFS;
1845
1846 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
1847 if (status < 0)
1848 mlog_errno(status);
1849
1850 return status;
1851}
1852
1853void ocfs2_rename_unlock(struct ocfs2_super *osb)
1854{
1855 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1856
1857 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
1858}
1859
Mark Fashehd680efe2006-09-08 14:14:34 -07001860int ocfs2_dentry_lock(struct dentry *dentry, int ex)
1861{
1862 int ret;
1863 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1864 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1865 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1866
1867 BUG_ON(!dl);
1868
1869 if (ocfs2_is_hard_readonly(osb))
1870 return -EROFS;
1871
1872 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
1873 if (ret < 0)
1874 mlog_errno(ret);
1875
1876 return ret;
1877}
1878
1879void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
1880{
1881 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1882 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
1883 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
1884
1885 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
1886}
1887
Mark Fashehccd979b2005-12-15 14:31:24 -08001888/* Reference counting of the dlm debug structure. We want this because
1889 * open references on the debug inodes can live on after a mount, so
1890 * we can't rely on the ocfs2_super to always exist. */
1891static void ocfs2_dlm_debug_free(struct kref *kref)
1892{
1893 struct ocfs2_dlm_debug *dlm_debug;
1894
1895 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
1896
1897 kfree(dlm_debug);
1898}
1899
1900void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
1901{
1902 if (dlm_debug)
1903 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
1904}
1905
1906static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
1907{
1908 kref_get(&debug->d_refcnt);
1909}
1910
1911struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
1912{
1913 struct ocfs2_dlm_debug *dlm_debug;
1914
1915 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
1916 if (!dlm_debug) {
1917 mlog_errno(-ENOMEM);
1918 goto out;
1919 }
1920
1921 kref_init(&dlm_debug->d_refcnt);
1922 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
1923 dlm_debug->d_locking_state = NULL;
1924out:
1925 return dlm_debug;
1926}
1927
1928/* Access to this is arbitrated for us via seq_file->sem. */
1929struct ocfs2_dlm_seq_priv {
1930 struct ocfs2_dlm_debug *p_dlm_debug;
1931 struct ocfs2_lock_res p_iter_res;
1932 struct ocfs2_lock_res p_tmp_res;
1933};
1934
1935static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
1936 struct ocfs2_dlm_seq_priv *priv)
1937{
1938 struct ocfs2_lock_res *iter, *ret = NULL;
1939 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
1940
1941 assert_spin_locked(&ocfs2_dlm_tracking_lock);
1942
1943 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
1944 /* discover the head of the list */
1945 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
1946 mlog(0, "End of list found, %p\n", ret);
1947 break;
1948 }
1949
1950 /* We track our "dummy" iteration lockres' by a NULL
1951 * l_ops field. */
1952 if (iter->l_ops != NULL) {
1953 ret = iter;
1954 break;
1955 }
1956 }
1957
1958 return ret;
1959}
1960
1961static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
1962{
1963 struct ocfs2_dlm_seq_priv *priv = m->private;
1964 struct ocfs2_lock_res *iter;
1965
1966 spin_lock(&ocfs2_dlm_tracking_lock);
1967 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
1968 if (iter) {
1969 /* Since lockres' have the lifetime of their container
1970 * (which can be inodes, ocfs2_supers, etc) we want to
1971 * copy this out to a temporary lockres while still
1972 * under the spinlock. Obviously after this we can't
1973 * trust any pointers on the copy returned, but that's
1974 * ok as the information we want isn't typically held
1975 * in them. */
1976 priv->p_tmp_res = *iter;
1977 iter = &priv->p_tmp_res;
1978 }
1979 spin_unlock(&ocfs2_dlm_tracking_lock);
1980
1981 return iter;
1982}
1983
1984static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
1985{
1986}
1987
1988static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
1989{
1990 struct ocfs2_dlm_seq_priv *priv = m->private;
1991 struct ocfs2_lock_res *iter = v;
1992 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
1993
1994 spin_lock(&ocfs2_dlm_tracking_lock);
1995 iter = ocfs2_dlm_next_res(iter, priv);
1996 list_del_init(&dummy->l_debug_list);
1997 if (iter) {
1998 list_add(&dummy->l_debug_list, &iter->l_debug_list);
1999 priv->p_tmp_res = *iter;
2000 iter = &priv->p_tmp_res;
2001 }
2002 spin_unlock(&ocfs2_dlm_tracking_lock);
2003
2004 return iter;
2005}
2006
2007/* So that debugfs.ocfs2 can determine which format is being used */
2008#define OCFS2_DLM_DEBUG_STR_VERSION 1
2009static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2010{
2011 int i;
2012 char *lvb;
2013 struct ocfs2_lock_res *lockres = v;
2014
2015 if (!lockres)
2016 return -EINVAL;
2017
Mark Fashehd680efe2006-09-08 14:14:34 -07002018 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2019
2020 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2021 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2022 lockres->l_name,
2023 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2024 else
2025 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2026
2027 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002028 "0x%lx\t"
2029 "0x%x\t"
2030 "0x%x\t"
2031 "%u\t"
2032 "%u\t"
2033 "%d\t"
2034 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002035 lockres->l_level,
2036 lockres->l_flags,
2037 lockres->l_action,
2038 lockres->l_unlock_action,
2039 lockres->l_ro_holders,
2040 lockres->l_ex_holders,
2041 lockres->l_requested,
2042 lockres->l_blocking);
2043
2044 /* Dump the raw LVB */
2045 lvb = lockres->l_lksb.lvb;
2046 for(i = 0; i < DLM_LVB_LEN; i++)
2047 seq_printf(m, "0x%x\t", lvb[i]);
2048
2049 /* End the line */
2050 seq_printf(m, "\n");
2051 return 0;
2052}
2053
2054static struct seq_operations ocfs2_dlm_seq_ops = {
2055 .start = ocfs2_dlm_seq_start,
2056 .stop = ocfs2_dlm_seq_stop,
2057 .next = ocfs2_dlm_seq_next,
2058 .show = ocfs2_dlm_seq_show,
2059};
2060
2061static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2062{
2063 struct seq_file *seq = (struct seq_file *) file->private_data;
2064 struct ocfs2_dlm_seq_priv *priv = seq->private;
2065 struct ocfs2_lock_res *res = &priv->p_iter_res;
2066
2067 ocfs2_remove_lockres_tracking(res);
2068 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2069 return seq_release_private(inode, file);
2070}
2071
2072static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2073{
2074 int ret;
2075 struct ocfs2_dlm_seq_priv *priv;
2076 struct seq_file *seq;
2077 struct ocfs2_super *osb;
2078
2079 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2080 if (!priv) {
2081 ret = -ENOMEM;
2082 mlog_errno(ret);
2083 goto out;
2084 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002085 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002086 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2087 priv->p_dlm_debug = osb->osb_dlm_debug;
2088 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2089
2090 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2091 if (ret) {
2092 kfree(priv);
2093 mlog_errno(ret);
2094 goto out;
2095 }
2096
2097 seq = (struct seq_file *) file->private_data;
2098 seq->private = priv;
2099
2100 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2101 priv->p_dlm_debug);
2102
2103out:
2104 return ret;
2105}
2106
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002107static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002108 .open = ocfs2_dlm_debug_open,
2109 .release = ocfs2_dlm_debug_release,
2110 .read = seq_read,
2111 .llseek = seq_lseek,
2112};
2113
2114static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2115{
2116 int ret = 0;
2117 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2118
2119 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2120 S_IFREG|S_IRUSR,
2121 osb->osb_debug_root,
2122 osb,
2123 &ocfs2_dlm_debug_fops);
2124 if (!dlm_debug->d_locking_state) {
2125 ret = -EINVAL;
2126 mlog(ML_ERROR,
2127 "Unable to create locking state debugfs file.\n");
2128 goto out;
2129 }
2130
2131 ocfs2_get_dlm_debug(dlm_debug);
2132out:
2133 return ret;
2134}
2135
2136static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2137{
2138 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2139
2140 if (dlm_debug) {
2141 debugfs_remove(dlm_debug->d_locking_state);
2142 ocfs2_put_dlm_debug(dlm_debug);
2143 }
2144}
2145
2146int ocfs2_dlm_init(struct ocfs2_super *osb)
2147{
2148 int status;
2149 u32 dlm_key;
2150 struct dlm_ctxt *dlm;
2151
2152 mlog_entry_void();
2153
2154 status = ocfs2_dlm_init_debug(osb);
2155 if (status < 0) {
2156 mlog_errno(status);
2157 goto bail;
2158 }
2159
2160 /* launch vote thread */
Mark Fasheh78427042006-05-04 12:03:26 -07002161 osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
Mark Fashehccd979b2005-12-15 14:31:24 -08002162 if (IS_ERR(osb->vote_task)) {
2163 status = PTR_ERR(osb->vote_task);
2164 osb->vote_task = NULL;
2165 mlog_errno(status);
2166 goto bail;
2167 }
2168
2169 /* used by the dlm code to make message headers unique, each
2170 * node in this domain must agree on this. */
2171 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2172
2173 /* for now, uuid == domain */
2174 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2175 if (IS_ERR(dlm)) {
2176 status = PTR_ERR(dlm);
2177 mlog_errno(status);
2178 goto bail;
2179 }
2180
2181 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2182 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2183
2184 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2185
2186 osb->dlm = dlm;
2187
2188 status = 0;
2189bail:
2190 if (status < 0) {
2191 ocfs2_dlm_shutdown_debug(osb);
2192 if (osb->vote_task)
2193 kthread_stop(osb->vote_task);
2194 }
2195
2196 mlog_exit(status);
2197 return status;
2198}
2199
2200void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2201{
2202 mlog_entry_void();
2203
2204 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2205
2206 ocfs2_drop_osb_locks(osb);
2207
2208 if (osb->vote_task) {
2209 kthread_stop(osb->vote_task);
2210 osb->vote_task = NULL;
2211 }
2212
2213 ocfs2_lock_res_free(&osb->osb_super_lockres);
2214 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2215
2216 dlm_unregister_domain(osb->dlm);
2217 osb->dlm = NULL;
2218
2219 ocfs2_dlm_shutdown_debug(osb);
2220
2221 mlog_exit_void();
2222}
2223
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002224static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002225{
2226 struct ocfs2_lock_res *lockres = opaque;
2227 unsigned long flags;
2228
2229 mlog_entry_void();
2230
2231 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2232 lockres->l_unlock_action);
2233
2234 spin_lock_irqsave(&lockres->l_lock, flags);
2235 /* We tried to cancel a convert request, but it was already
2236 * granted. All we want to do here is clear our unlock
2237 * state. The wake_up call done at the bottom is redundant
2238 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2239 * hurt anything anyway */
2240 if (status == DLM_CANCELGRANT &&
2241 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2242 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2243
2244 /* We don't clear the busy flag in this case as it
2245 * should have been cleared by the ast which the dlm
2246 * has called. */
2247 goto complete_unlock;
2248 }
2249
2250 if (status != DLM_NORMAL) {
2251 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2252 "unlock_action %d\n", status, lockres->l_name,
2253 lockres->l_unlock_action);
2254 spin_unlock_irqrestore(&lockres->l_lock, flags);
2255 return;
2256 }
2257
2258 switch(lockres->l_unlock_action) {
2259 case OCFS2_UNLOCK_CANCEL_CONVERT:
2260 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2261 lockres->l_action = OCFS2_AST_INVALID;
2262 break;
2263 case OCFS2_UNLOCK_DROP_LOCK:
2264 lockres->l_level = LKM_IVMODE;
2265 break;
2266 default:
2267 BUG();
2268 }
2269
2270 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2271complete_unlock:
2272 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2273 spin_unlock_irqrestore(&lockres->l_lock, flags);
2274
2275 wake_up(&lockres->l_event);
2276
2277 mlog_exit_void();
2278}
2279
Mark Fashehccd979b2005-12-15 14:31:24 -08002280static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002281 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002282{
2283 enum dlm_status status;
2284 unsigned long flags;
Mark Fashehb80fc012006-09-12 22:08:14 -07002285 int lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002286
2287 /* We didn't get anywhere near actually using this lockres. */
2288 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2289 goto out;
2290
Mark Fashehb80fc012006-09-12 22:08:14 -07002291 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2292 lkm_flags |= LKM_VALBLK;
2293
Mark Fashehccd979b2005-12-15 14:31:24 -08002294 spin_lock_irqsave(&lockres->l_lock, flags);
2295
2296 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2297 "lockres %s, flags 0x%lx\n",
2298 lockres->l_name, lockres->l_flags);
2299
2300 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2301 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2302 "%u, unlock_action = %u\n",
2303 lockres->l_name, lockres->l_flags, lockres->l_action,
2304 lockres->l_unlock_action);
2305
2306 spin_unlock_irqrestore(&lockres->l_lock, flags);
2307
2308 /* XXX: Today we just wait on any busy
2309 * locks... Perhaps we need to cancel converts in the
2310 * future? */
2311 ocfs2_wait_on_busy_lock(lockres);
2312
2313 spin_lock_irqsave(&lockres->l_lock, flags);
2314 }
2315
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002316 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2317 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2318 lockres->l_level == LKM_EXMODE &&
2319 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2320 lockres->l_ops->set_lvb(lockres);
2321 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002322
2323 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2324 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2325 lockres->l_name);
2326 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2327 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2328
2329 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2330 spin_unlock_irqrestore(&lockres->l_lock, flags);
2331 goto out;
2332 }
2333
2334 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2335
2336 /* make sure we never get here while waiting for an ast to
2337 * fire. */
2338 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2339
2340 /* is this necessary? */
2341 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2342 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2343 spin_unlock_irqrestore(&lockres->l_lock, flags);
2344
2345 mlog(0, "lock %s\n", lockres->l_name);
2346
Mark Fashehb80fc012006-09-12 22:08:14 -07002347 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002348 ocfs2_unlock_ast, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002349 if (status != DLM_NORMAL) {
2350 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2351 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2352 dlm_print_one_lock(lockres->l_lksb.lockid);
2353 BUG();
2354 }
2355 mlog(0, "lock %s, successfull return from dlmunlock\n",
2356 lockres->l_name);
2357
2358 ocfs2_wait_on_busy_lock(lockres);
2359out:
2360 mlog_exit(0);
2361 return 0;
2362}
2363
2364/* Mark the lockres as being dropped. It will no longer be
2365 * queued if blocking, but we still may have to wait on it
2366 * being dequeued from the vote thread before we can consider
2367 * it safe to drop.
2368 *
2369 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2370void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2371{
2372 int status;
2373 struct ocfs2_mask_waiter mw;
2374 unsigned long flags;
2375
2376 ocfs2_init_mask_waiter(&mw);
2377
2378 spin_lock_irqsave(&lockres->l_lock, flags);
2379 lockres->l_flags |= OCFS2_LOCK_FREEING;
2380 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2381 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2382 spin_unlock_irqrestore(&lockres->l_lock, flags);
2383
2384 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2385
2386 status = ocfs2_wait_for_mask(&mw);
2387 if (status)
2388 mlog_errno(status);
2389
2390 spin_lock_irqsave(&lockres->l_lock, flags);
2391 }
2392 spin_unlock_irqrestore(&lockres->l_lock, flags);
2393}
2394
Mark Fashehd680efe2006-09-08 14:14:34 -07002395void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2396 struct ocfs2_lock_res *lockres)
2397{
2398 int ret;
2399
2400 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002401 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002402 if (ret)
2403 mlog_errno(ret);
2404}
2405
Mark Fashehccd979b2005-12-15 14:31:24 -08002406static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2407{
Mark Fashehd680efe2006-09-08 14:14:34 -07002408 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2409 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002410}
2411
Mark Fashehccd979b2005-12-15 14:31:24 -08002412int ocfs2_drop_inode_locks(struct inode *inode)
2413{
2414 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002415
2416 mlog_entry_void();
2417
2418 /* No need to call ocfs2_mark_lockres_freeing here -
2419 * ocfs2_clear_inode has done it for us. */
2420
2421 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002422 &OCFS2_I(inode)->ip_data_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002423 if (err < 0)
2424 mlog_errno(err);
2425
2426 status = err;
2427
2428 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002429 &OCFS2_I(inode)->ip_meta_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002430 if (err < 0)
2431 mlog_errno(err);
2432 if (err < 0 && !status)
2433 status = err;
2434
2435 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002436 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002437 if (err < 0)
2438 mlog_errno(err);
2439 if (err < 0 && !status)
2440 status = err;
2441
2442 mlog_exit(status);
2443 return status;
2444}
2445
2446static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2447 int new_level)
2448{
2449 assert_spin_locked(&lockres->l_lock);
2450
2451 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2452
2453 if (lockres->l_level <= new_level) {
2454 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2455 lockres->l_level, new_level);
2456 BUG();
2457 }
2458
2459 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2460 lockres->l_name, new_level, lockres->l_blocking);
2461
2462 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2463 lockres->l_requested = new_level;
2464 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2465}
2466
2467static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2468 struct ocfs2_lock_res *lockres,
2469 int new_level,
2470 int lvb)
2471{
2472 int ret, dlm_flags = LKM_CONVERT;
2473 enum dlm_status status;
2474
2475 mlog_entry_void();
2476
2477 if (lvb)
2478 dlm_flags |= LKM_VALBLK;
2479
2480 status = dlmlock(osb->dlm,
2481 new_level,
2482 &lockres->l_lksb,
2483 dlm_flags,
2484 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -07002485 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -07002486 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002487 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -07002488 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -08002489 if (status != DLM_NORMAL) {
2490 ocfs2_log_dlm_error("dlmlock", status, lockres);
2491 ret = -EINVAL;
2492 ocfs2_recover_from_dlm_error(lockres, 1);
2493 goto bail;
2494 }
2495
2496 ret = 0;
2497bail:
2498 mlog_exit(ret);
2499 return ret;
2500}
2501
2502/* returns 1 when the caller should unlock and call dlmunlock */
2503static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2504 struct ocfs2_lock_res *lockres)
2505{
2506 assert_spin_locked(&lockres->l_lock);
2507
2508 mlog_entry_void();
2509 mlog(0, "lock %s\n", lockres->l_name);
2510
2511 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2512 /* If we're already trying to cancel a lock conversion
2513 * then just drop the spinlock and allow the caller to
2514 * requeue this lock. */
2515
2516 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2517 return 0;
2518 }
2519
2520 /* were we in a convert when we got the bast fire? */
2521 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2522 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2523 /* set things up for the unlockast to know to just
2524 * clear out the ast_action and unset busy, etc. */
2525 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2526
2527 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2528 "lock %s, invalid flags: 0x%lx\n",
2529 lockres->l_name, lockres->l_flags);
2530
2531 return 1;
2532}
2533
2534static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2535 struct ocfs2_lock_res *lockres)
2536{
2537 int ret;
2538 enum dlm_status status;
2539
2540 mlog_entry_void();
2541 mlog(0, "lock %s\n", lockres->l_name);
2542
2543 ret = 0;
2544 status = dlmunlock(osb->dlm,
2545 &lockres->l_lksb,
2546 LKM_CANCEL,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002547 ocfs2_unlock_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002548 lockres);
2549 if (status != DLM_NORMAL) {
2550 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2551 ret = -EINVAL;
2552 ocfs2_recover_from_dlm_error(lockres, 0);
2553 }
2554
2555 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2556
2557 mlog_exit(ret);
2558 return ret;
2559}
2560
Mark Fashehb5e500e2006-09-13 22:01:16 -07002561static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2562 struct ocfs2_lock_res *lockres,
2563 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002564{
2565 unsigned long flags;
2566 int blocking;
2567 int new_level;
2568 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002569 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002570
2571 mlog_entry_void();
2572
2573 spin_lock_irqsave(&lockres->l_lock, flags);
2574
2575 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2576
2577recheck:
2578 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002579 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002580 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2581 spin_unlock_irqrestore(&lockres->l_lock, flags);
2582 if (ret) {
2583 ret = ocfs2_cancel_convert(osb, lockres);
2584 if (ret < 0)
2585 mlog_errno(ret);
2586 }
2587 goto leave;
2588 }
2589
2590 /* if we're blocking an exclusive and we have *any* holders,
2591 * then requeue. */
2592 if ((lockres->l_blocking == LKM_EXMODE)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002593 && (lockres->l_ex_holders || lockres->l_ro_holders))
2594 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002595
2596 /* If it's a PR we're blocking, then only
2597 * requeue if we've got any EX holders */
2598 if (lockres->l_blocking == LKM_PRMODE &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002599 lockres->l_ex_holders)
2600 goto leave_requeue;
2601
2602 /*
2603 * Can we get a lock in this state if the holder counts are
2604 * zero? The meta data unblock code used to check this.
2605 */
2606 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2607 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2608 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002609
Mark Fasheh16d5b952006-09-13 21:10:12 -07002610 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2611
2612 if (lockres->l_ops->check_downconvert
2613 && !lockres->l_ops->check_downconvert(lockres, new_level))
2614 goto leave_requeue;
2615
Mark Fashehccd979b2005-12-15 14:31:24 -08002616 /* If we get here, then we know that there are no more
2617 * incompatible holders (and anyone asking for an incompatible
2618 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002619 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002620 goto downconvert;
2621
2622 /* Some lockres types want to do a bit of work before
2623 * downconverting a lock. Allow that here. The worker function
2624 * may sleep, so we save off a copy of what we're blocking as
2625 * it may change while we're not holding the spin lock. */
2626 blocking = lockres->l_blocking;
2627 spin_unlock_irqrestore(&lockres->l_lock, flags);
2628
Mark Fashehcc567d82006-09-13 21:52:21 -07002629 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002630
2631 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2632 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002633
2634 spin_lock_irqsave(&lockres->l_lock, flags);
2635 if (blocking != lockres->l_blocking) {
2636 /* If this changed underneath us, then we can't drop
2637 * it just yet. */
2638 goto recheck;
2639 }
2640
2641downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002642 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002643
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002644 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2645 if (lockres->l_level == LKM_EXMODE)
2646 set_lvb = 1;
2647
2648 /*
2649 * We only set the lvb if the lock has been fully
2650 * refreshed - otherwise we risk setting stale
2651 * data. Otherwise, there's no need to actually clear
2652 * out the lvb here as it's value is still valid.
2653 */
2654 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2655 lockres->l_ops->set_lvb(lockres);
2656 }
2657
Mark Fashehccd979b2005-12-15 14:31:24 -08002658 ocfs2_prepare_downconvert(lockres, new_level);
2659 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002660 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002661leave:
2662 mlog_exit(ret);
2663 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002664
2665leave_requeue:
2666 spin_unlock_irqrestore(&lockres->l_lock, flags);
2667 ctl->requeue = 1;
2668
2669 mlog_exit(0);
2670 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002671}
2672
Mark Fashehd680efe2006-09-08 14:14:34 -07002673static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2674 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08002675{
2676 struct inode *inode;
2677 struct address_space *mapping;
2678
Mark Fashehccd979b2005-12-15 14:31:24 -08002679 inode = ocfs2_lock_res_inode(lockres);
2680 mapping = inode->i_mapping;
2681
2682 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002683 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2684 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002685 }
2686 sync_mapping_buffers(mapping);
2687 if (blocking == LKM_EXMODE) {
2688 truncate_inode_pages(mapping, 0);
2689 unmap_mapping_range(mapping, 0, 0, 0);
2690 } else {
2691 /* We only need to wait on the I/O if we're not also
2692 * truncating pages because truncate_inode_pages waits
2693 * for us above. We don't truncate pages if we're
2694 * blocking anything < EXMODE because we want to keep
2695 * them around in that case. */
2696 filemap_fdatawait(mapping);
2697 }
2698
Mark Fashehd680efe2006-09-08 14:14:34 -07002699 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002700}
2701
Mark Fasheh810d5ae2006-09-13 21:39:52 -07002702static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2703 int new_level)
2704{
2705 struct inode *inode = ocfs2_lock_res_inode(lockres);
2706 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2707
2708 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2709 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2710
2711 if (checkpointed)
2712 return 1;
2713
2714 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2715 return 0;
2716}
2717
2718static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2719{
2720 struct inode *inode = ocfs2_lock_res_inode(lockres);
2721
2722 __ocfs2_stuff_meta_lvb(inode);
2723}
2724
Mark Fashehd680efe2006-09-08 14:14:34 -07002725/*
2726 * Does the final reference drop on our dentry lock. Right now this
2727 * happens in the vote thread, but we could choose to simplify the
2728 * dlmglue API and push these off to the ocfs2_wq in the future.
2729 */
2730static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2731 struct ocfs2_lock_res *lockres)
2732{
2733 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2734 ocfs2_dentry_lock_put(osb, dl);
2735}
2736
2737/*
2738 * d_delete() matching dentries before the lock downconvert.
2739 *
2740 * At this point, any process waiting to destroy the
2741 * dentry_lock due to last ref count is stopped by the
2742 * OCFS2_LOCK_QUEUED flag.
2743 *
2744 * We have two potential problems
2745 *
2746 * 1) If we do the last reference drop on our dentry_lock (via dput)
2747 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2748 * the downconvert to finish. Instead we take an elevated
2749 * reference and push the drop until after we've completed our
2750 * unblock processing.
2751 *
2752 * 2) There might be another process with a final reference,
2753 * waiting on us to finish processing. If this is the case, we
2754 * detect it and exit out - there's no more dentries anyway.
2755 */
2756static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2757 int blocking)
2758{
2759 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2760 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2761 struct dentry *dentry;
2762 unsigned long flags;
2763 int extra_ref = 0;
2764
2765 /*
2766 * This node is blocking another node from getting a read
2767 * lock. This happens when we've renamed within a
2768 * directory. We've forced the other nodes to d_delete(), but
2769 * we never actually dropped our lock because it's still
2770 * valid. The downconvert code will retain a PR for this node,
2771 * so there's no further work to do.
2772 */
2773 if (blocking == LKM_PRMODE)
2774 return UNBLOCK_CONTINUE;
2775
2776 /*
2777 * Mark this inode as potentially orphaned. The code in
2778 * ocfs2_delete_inode() will figure out whether it actually
2779 * needs to be freed or not.
2780 */
2781 spin_lock(&oi->ip_lock);
2782 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2783 spin_unlock(&oi->ip_lock);
2784
2785 /*
2786 * Yuck. We need to make sure however that the check of
2787 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2788 * respect to a reference decrement or the setting of that
2789 * flag.
2790 */
2791 spin_lock_irqsave(&lockres->l_lock, flags);
2792 spin_lock(&dentry_attach_lock);
2793 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2794 && dl->dl_count) {
2795 dl->dl_count++;
2796 extra_ref = 1;
2797 }
2798 spin_unlock(&dentry_attach_lock);
2799 spin_unlock_irqrestore(&lockres->l_lock, flags);
2800
2801 mlog(0, "extra_ref = %d\n", extra_ref);
2802
2803 /*
2804 * We have a process waiting on us in ocfs2_dentry_iput(),
2805 * which means we can't have any more outstanding
2806 * aliases. There's no need to do any more work.
2807 */
2808 if (!extra_ref)
2809 return UNBLOCK_CONTINUE;
2810
2811 spin_lock(&dentry_attach_lock);
2812 while (1) {
2813 dentry = ocfs2_find_local_alias(dl->dl_inode,
2814 dl->dl_parent_blkno, 1);
2815 if (!dentry)
2816 break;
2817 spin_unlock(&dentry_attach_lock);
2818
2819 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
2820 dentry->d_name.name);
2821
2822 /*
2823 * The following dcache calls may do an
2824 * iput(). Normally we don't want that from the
2825 * downconverting thread, but in this case it's ok
2826 * because the requesting node already has an
2827 * exclusive lock on the inode, so it can't be queued
2828 * for a downconvert.
2829 */
2830 d_delete(dentry);
2831 dput(dentry);
2832
2833 spin_lock(&dentry_attach_lock);
2834 }
2835 spin_unlock(&dentry_attach_lock);
2836
2837 /*
2838 * If we are the last holder of this dentry lock, there is no
2839 * reason to downconvert so skip straight to the unlock.
2840 */
2841 if (dl->dl_count == 1)
2842 return UNBLOCK_STOP_POST;
2843
2844 return UNBLOCK_CONTINUE_POST;
2845}
2846
Mark Fashehccd979b2005-12-15 14:31:24 -08002847void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
2848 struct ocfs2_lock_res *lockres)
2849{
2850 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07002851 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08002852 unsigned long flags;
2853
2854 /* Our reference to the lockres in this function can be
2855 * considered valid until we remove the OCFS2_LOCK_QUEUED
2856 * flag. */
2857
2858 mlog_entry_void();
2859
2860 BUG_ON(!lockres);
2861 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08002862
2863 mlog(0, "lockres %s blocked.\n", lockres->l_name);
2864
2865 /* Detect whether a lock has been marked as going away while
2866 * the vote thread was processing other things. A lock can
2867 * still be marked with OCFS2_LOCK_FREEING after this check,
2868 * but short circuiting here will still save us some
2869 * performance. */
2870 spin_lock_irqsave(&lockres->l_lock, flags);
2871 if (lockres->l_flags & OCFS2_LOCK_FREEING)
2872 goto unqueue;
2873 spin_unlock_irqrestore(&lockres->l_lock, flags);
2874
Mark Fashehb5e500e2006-09-13 22:01:16 -07002875 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08002876 if (status < 0)
2877 mlog_errno(status);
2878
2879 spin_lock_irqsave(&lockres->l_lock, flags);
2880unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07002881 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002882 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
2883 } else
2884 ocfs2_schedule_blocked_lock(osb, lockres);
2885
2886 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07002887 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08002888 spin_unlock_irqrestore(&lockres->l_lock, flags);
2889
Mark Fashehd680efe2006-09-08 14:14:34 -07002890 if (ctl.unblock_action != UNBLOCK_CONTINUE
2891 && lockres->l_ops->post_unlock)
2892 lockres->l_ops->post_unlock(osb, lockres);
2893
Mark Fashehccd979b2005-12-15 14:31:24 -08002894 mlog_exit_void();
2895}
2896
2897static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
2898 struct ocfs2_lock_res *lockres)
2899{
2900 mlog_entry_void();
2901
2902 assert_spin_locked(&lockres->l_lock);
2903
2904 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
2905 /* Do not schedule a lock for downconvert when it's on
2906 * the way to destruction - any nodes wanting access
2907 * to the resource will get it soon. */
2908 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
2909 lockres->l_name, lockres->l_flags);
2910 return;
2911 }
2912
2913 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
2914
2915 spin_lock(&osb->vote_task_lock);
2916 if (list_empty(&lockres->l_blocked_list)) {
2917 list_add_tail(&lockres->l_blocked_list,
2918 &osb->blocked_lock_list);
2919 osb->blocked_lock_count++;
2920 }
2921 spin_unlock(&osb->vote_task_lock);
2922
2923 mlog_exit_void();
2924}
2925
2926/* This aids in debugging situations where a bad LVB might be involved. */
2927void ocfs2_dump_meta_lvb_info(u64 level,
2928 const char *function,
2929 unsigned int line,
2930 struct ocfs2_lock_res *lockres)
2931{
2932 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
2933
2934 mlog(level, "LVB information for %s (called from %s:%u):\n",
2935 lockres->l_name, function, line);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002936 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
2937 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
2938 be32_to_cpu(lvb->lvb_igeneration));
Mark Fashehb06970532006-03-03 10:24:33 -08002939 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
2940 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
2941 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
2942 be16_to_cpu(lvb->lvb_imode));
2943 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002944 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
Mark Fashehb06970532006-03-03 10:24:33 -08002945 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
2946 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002947 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
2948 be32_to_cpu(lvb->lvb_iattr));
Mark Fashehccd979b2005-12-15 14:31:24 -08002949}