blob: 7e36abea8f40e056bc226309b8462cfbb88a0125 [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>
Mark Fashehccd979b2005-12-15 14:31:24 -080030#include <linux/crc32.h>
31#include <linux/kthread.h>
32#include <linux/pagemap.h>
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
35
36#include <cluster/heartbeat.h>
37#include <cluster/nodemanager.h>
38#include <cluster/tcp.h>
39
40#include <dlm/dlmapi.h>
41
42#define MLOG_MASK_PREFIX ML_DLM_GLUE
43#include <cluster/masklog.h>
44
45#include "ocfs2.h"
46
47#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070048#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080049#include "dlmglue.h"
50#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080051#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#include "heartbeat.h"
53#include "inode.h"
54#include "journal.h"
55#include "slot_map.h"
56#include "super.h"
57#include "uptodate.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080058
59#include "buffer_head_io.h"
60
61struct ocfs2_mask_waiter {
62 struct list_head mw_item;
63 int mw_status;
64 struct completion mw_complete;
65 unsigned long mw_mask;
66 unsigned long mw_goal;
67};
68
Mark Fasheh54a7e752006-09-12 21:49:13 -070069static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
70static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080071
Mark Fashehd680efe2006-09-08 14:14:34 -070072/*
Mark Fashehcc567d82006-09-13 21:52:21 -070073 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070074 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070075 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070076 * and ocfs2_process_blocked_lock()
77 *
78 */
79enum ocfs2_unblock_action {
80 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
81 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
82 * ->post_unlock callback */
83 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
84 * ->post_unlock() callback. */
85};
86
87struct ocfs2_unblock_ctl {
88 int requeue;
89 enum ocfs2_unblock_action unblock_action;
90};
91
Mark Fasheh810d5ae2006-09-13 21:39:52 -070092static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
93 int new_level);
94static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
95
Mark Fashehcc567d82006-09-13 21:52:21 -070096static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
97 int blocking);
98
Mark Fashehcc567d82006-09-13 21:52:21 -070099static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
100 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700101
102static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
103 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800104
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700105
106#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
107
108/* This aids in debugging situations where a bad LVB might be involved. */
109static void ocfs2_dump_meta_lvb_info(u64 level,
110 const char *function,
111 unsigned int line,
112 struct ocfs2_lock_res *lockres)
113{
114 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
115
116 mlog(level, "LVB information for %s (called from %s:%u):\n",
117 lockres->l_name, function, line);
118 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
119 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
120 be32_to_cpu(lvb->lvb_igeneration));
121 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
122 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
123 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
124 be16_to_cpu(lvb->lvb_imode));
125 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
126 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
127 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
128 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
129 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
130 be32_to_cpu(lvb->lvb_iattr));
131}
132
133
Mark Fashehf625c972006-09-12 21:24:53 -0700134/*
135 * OCFS2 Lock Resource Operations
136 *
137 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700138 *
139 * The most basic of lock types can point ->l_priv to their respective
140 * struct ocfs2_super and allow the default actions to manage things.
141 *
142 * Right now, each lock type also needs to implement an init function,
143 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
144 * should be called when the lock is no longer needed (i.e., object
145 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700146 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800147struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700148 /*
149 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
150 * this callback if ->l_priv is not an ocfs2_super pointer
151 */
152 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700153
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700154 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700155 * Optionally called in the downconvert thread after a
156 * successful downconvert. The lockres will not be referenced
157 * after this callback is called, so it is safe to free
158 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700159 *
160 * The exact semantics of when this is called are controlled
161 * by ->downconvert_worker()
162 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700163 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700164
165 /*
Mark Fasheh16d5b9562006-09-13 21:10:12 -0700166 * Allow a lock type to add checks to determine whether it is
167 * safe to downconvert a lock. Return 0 to re-queue the
168 * downconvert at a later time, nonzero to continue.
169 *
170 * For most locks, the default checks that there are no
171 * incompatible holders are sufficient.
172 *
173 * Called with the lockres spinlock held.
174 */
175 int (*check_downconvert)(struct ocfs2_lock_res *, int);
176
177 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700178 * Allows a lock type to populate the lock value block. This
179 * is called on downconvert, and when we drop a lock.
180 *
181 * Locks that want to use this should set LOCK_TYPE_USES_LVB
182 * in the flags field.
183 *
184 * Called with the lockres spinlock held.
185 */
186 void (*set_lvb)(struct ocfs2_lock_res *);
187
188 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700189 * Called from the downconvert thread when it is determined
190 * that a lock will be downconverted. This is called without
191 * any locks held so the function can do work that might
192 * schedule (syncing out data, etc).
193 *
194 * This should return any one of the ocfs2_unblock_action
195 * values, depending on what it wants the thread to do.
196 */
197 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
198
199 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700200 * LOCK_TYPE_* flags which describe the specific requirements
201 * of a lock type. Descriptions of each individual flag follow.
202 */
203 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800204};
205
Mark Fashehf625c972006-09-12 21:24:53 -0700206/*
207 * Some locks want to "refresh" potentially stale data when a
208 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
209 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
210 * individual lockres l_flags member from the ast function. It is
211 * expected that the locking wrapper will clear the
212 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
213 */
214#define LOCK_TYPE_REQUIRES_REFRESH 0x1
215
Mark Fashehb80fc012006-09-12 22:08:14 -0700216/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700217 * Indicate that a lock type makes use of the lock value block. The
218 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700219 */
220#define LOCK_TYPE_USES_LVB 0x2
221
Mark Fashehccd979b2005-12-15 14:31:24 -0800222static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700223 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700224 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800225};
226
227static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700228 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700229 .check_downconvert = ocfs2_check_meta_downconvert,
230 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700231 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700232 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800233};
234
Mark Fashehccd979b2005-12-15 14:31:24 -0800235static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700236 .get_osb = ocfs2_get_inode_osb,
Mark Fashehcc567d82006-09-13 21:52:21 -0700237 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700238 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800239};
240
241static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700242 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800243};
244
245static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700246 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800247};
248
Mark Fashehd680efe2006-09-08 14:14:34 -0700249static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700250 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700251 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700252 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700253 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700254};
255
Tiger Yang50008632007-03-20 16:01:38 -0700256static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
257 .get_osb = ocfs2_get_inode_osb,
258 .flags = 0,
259};
260
Mark Fashehccd979b2005-12-15 14:31:24 -0800261static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
262{
263 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
264 lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
Tiger Yang50008632007-03-20 16:01:38 -0700265 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
266 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800267}
268
Mark Fashehccd979b2005-12-15 14:31:24 -0800269static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
270{
271 BUG_ON(!ocfs2_is_inode_lock(lockres));
272
273 return (struct inode *) lockres->l_priv;
274}
275
Mark Fashehd680efe2006-09-08 14:14:34 -0700276static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
277{
278 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
279
280 return (struct ocfs2_dentry_lock *)lockres->l_priv;
281}
282
Mark Fasheh54a7e752006-09-12 21:49:13 -0700283static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
284{
285 if (lockres->l_ops->get_osb)
286 return lockres->l_ops->get_osb(lockres);
287
288 return (struct ocfs2_super *)lockres->l_priv;
289}
290
Mark Fashehccd979b2005-12-15 14:31:24 -0800291static int ocfs2_lock_create(struct ocfs2_super *osb,
292 struct ocfs2_lock_res *lockres,
293 int level,
294 int dlm_flags);
295static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
296 int wanted);
297static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
298 struct ocfs2_lock_res *lockres,
299 int level);
300static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
301static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
302static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
303static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
304static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
305 struct ocfs2_lock_res *lockres);
306static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
307 int convert);
308#define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
309 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
310 "resource %s: %s\n", dlm_errname(_stat), _func, \
311 _lockres->l_name, dlm_errmsg(_stat)); \
312} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700313static int ocfs2_downconvert_thread(void *arg);
314static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
315 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800316static int ocfs2_meta_lock_update(struct inode *inode,
317 struct buffer_head **bh);
318static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
319static inline int ocfs2_highest_compat_lock_level(int level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800320
Mark Fashehccd979b2005-12-15 14:31:24 -0800321static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
322 u64 blkno,
323 u32 generation,
324 char *name)
325{
326 int len;
327
328 mlog_entry_void();
329
330 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
331
Mark Fashehb0697052006-03-03 10:24:33 -0800332 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
333 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
334 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800335
336 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
337
338 mlog(0, "built lock resource with name: %s\n", name);
339
340 mlog_exit_void();
341}
342
Ingo Molnar34af9462006-06-27 02:53:55 -0700343static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800344
345static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
346 struct ocfs2_dlm_debug *dlm_debug)
347{
348 mlog(0, "Add tracking for lockres %s\n", res->l_name);
349
350 spin_lock(&ocfs2_dlm_tracking_lock);
351 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
352 spin_unlock(&ocfs2_dlm_tracking_lock);
353}
354
355static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
356{
357 spin_lock(&ocfs2_dlm_tracking_lock);
358 if (!list_empty(&res->l_debug_list))
359 list_del_init(&res->l_debug_list);
360 spin_unlock(&ocfs2_dlm_tracking_lock);
361}
362
363static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
364 struct ocfs2_lock_res *res,
365 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800366 struct ocfs2_lock_res_ops *ops,
367 void *priv)
368{
Mark Fashehccd979b2005-12-15 14:31:24 -0800369 res->l_type = type;
370 res->l_ops = ops;
371 res->l_priv = priv;
372
373 res->l_level = LKM_IVMODE;
374 res->l_requested = LKM_IVMODE;
375 res->l_blocking = LKM_IVMODE;
376 res->l_action = OCFS2_AST_INVALID;
377 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
378
379 res->l_flags = OCFS2_LOCK_INITIALIZED;
380
381 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
382}
383
384void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
385{
386 /* This also clears out the lock status block */
387 memset(res, 0, sizeof(struct ocfs2_lock_res));
388 spin_lock_init(&res->l_lock);
389 init_waitqueue_head(&res->l_event);
390 INIT_LIST_HEAD(&res->l_blocked_list);
391 INIT_LIST_HEAD(&res->l_mask_waiters);
392}
393
394void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
395 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700396 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800397 struct inode *inode)
398{
399 struct ocfs2_lock_res_ops *ops;
400
401 switch(type) {
402 case OCFS2_LOCK_TYPE_RW:
403 ops = &ocfs2_inode_rw_lops;
404 break;
405 case OCFS2_LOCK_TYPE_META:
406 ops = &ocfs2_inode_meta_lops;
407 break;
408 case OCFS2_LOCK_TYPE_DATA:
409 ops = &ocfs2_inode_data_lops;
410 break;
Tiger Yang50008632007-03-20 16:01:38 -0700411 case OCFS2_LOCK_TYPE_OPEN:
412 ops = &ocfs2_inode_open_lops;
413 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800414 default:
415 mlog_bug_on_msg(1, "type: %d\n", type);
416 ops = NULL; /* thanks, gcc */
417 break;
418 };
419
Mark Fashehd680efe2006-09-08 14:14:34 -0700420 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700421 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700422 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
423}
424
Mark Fasheh54a7e752006-09-12 21:49:13 -0700425static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
426{
427 struct inode *inode = ocfs2_lock_res_inode(lockres);
428
429 return OCFS2_SB(inode->i_sb);
430}
431
Mark Fashehd680efe2006-09-08 14:14:34 -0700432static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
433{
434 __be64 inode_blkno_be;
435
436 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
437 sizeof(__be64));
438
439 return be64_to_cpu(inode_blkno_be);
440}
441
Mark Fasheh54a7e752006-09-12 21:49:13 -0700442static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
443{
444 struct ocfs2_dentry_lock *dl = lockres->l_priv;
445
446 return OCFS2_SB(dl->dl_inode->i_sb);
447}
448
Mark Fashehd680efe2006-09-08 14:14:34 -0700449void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
450 u64 parent, struct inode *inode)
451{
452 int len;
453 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
454 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
455 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
456
457 ocfs2_lock_res_init_once(lockres);
458
459 /*
460 * Unfortunately, the standard lock naming scheme won't work
461 * here because we have two 16 byte values to use. Instead,
462 * we'll stuff the inode number as a binary value. We still
463 * want error prints to show something without garbling the
464 * display, so drop a null byte in there before the inode
465 * number. A future version of OCFS2 will likely use all
466 * binary lock names. The stringified names have been a
467 * tremendous aid in debugging, but now that the debugfs
468 * interface exists, we can mangle things there if need be.
469 *
470 * NOTE: We also drop the standard "pad" value (the total lock
471 * name size stays the same though - the last part is all
472 * zeros due to the memset in ocfs2_lock_res_init_once()
473 */
474 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
475 "%c%016llx",
476 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
477 (long long)parent);
478
479 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
480
481 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
482 sizeof(__be64));
483
484 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
485 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
486 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800487}
488
489static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
490 struct ocfs2_super *osb)
491{
492 /* Superblock lockres doesn't come from a slab so we call init
493 * once on it manually. */
494 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700495 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
496 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800498 &ocfs2_super_lops, osb);
499}
500
501static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
502 struct ocfs2_super *osb)
503{
504 /* Rename lockres doesn't come from a slab so we call init
505 * once on it manually. */
506 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700507 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
508 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800509 &ocfs2_rename_lops, osb);
510}
511
512void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
513{
514 mlog_entry_void();
515
516 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
517 return;
518
519 ocfs2_remove_lockres_tracking(res);
520
521 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
522 "Lockres %s is on the blocked list\n",
523 res->l_name);
524 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
525 "Lockres %s has mask waiters pending\n",
526 res->l_name);
527 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
528 "Lockres %s is locked\n",
529 res->l_name);
530 mlog_bug_on_msg(res->l_ro_holders,
531 "Lockres %s has %u ro holders\n",
532 res->l_name, res->l_ro_holders);
533 mlog_bug_on_msg(res->l_ex_holders,
534 "Lockres %s has %u ex holders\n",
535 res->l_name, res->l_ex_holders);
536
537 /* Need to clear out the lock status block for the dlm */
538 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
539
540 res->l_flags = 0UL;
541 mlog_exit_void();
542}
543
544static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
545 int level)
546{
547 mlog_entry_void();
548
549 BUG_ON(!lockres);
550
551 switch(level) {
552 case LKM_EXMODE:
553 lockres->l_ex_holders++;
554 break;
555 case LKM_PRMODE:
556 lockres->l_ro_holders++;
557 break;
558 default:
559 BUG();
560 }
561
562 mlog_exit_void();
563}
564
565static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
566 int level)
567{
568 mlog_entry_void();
569
570 BUG_ON(!lockres);
571
572 switch(level) {
573 case LKM_EXMODE:
574 BUG_ON(!lockres->l_ex_holders);
575 lockres->l_ex_holders--;
576 break;
577 case LKM_PRMODE:
578 BUG_ON(!lockres->l_ro_holders);
579 lockres->l_ro_holders--;
580 break;
581 default:
582 BUG();
583 }
584 mlog_exit_void();
585}
586
587/* WARNING: This function lives in a world where the only three lock
588 * levels are EX, PR, and NL. It *will* have to be adjusted when more
589 * lock types are added. */
590static inline int ocfs2_highest_compat_lock_level(int level)
591{
592 int new_level = LKM_EXMODE;
593
594 if (level == LKM_EXMODE)
595 new_level = LKM_NLMODE;
596 else if (level == LKM_PRMODE)
597 new_level = LKM_PRMODE;
598 return new_level;
599}
600
601static void lockres_set_flags(struct ocfs2_lock_res *lockres,
602 unsigned long newflags)
603{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200604 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800605
606 assert_spin_locked(&lockres->l_lock);
607
608 lockres->l_flags = newflags;
609
Christoph Hellwig800deef2007-05-17 16:03:13 +0200610 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800611 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
612 continue;
613
614 list_del_init(&mw->mw_item);
615 mw->mw_status = 0;
616 complete(&mw->mw_complete);
617 }
618}
619static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
620{
621 lockres_set_flags(lockres, lockres->l_flags | or);
622}
623static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
624 unsigned long clear)
625{
626 lockres_set_flags(lockres, lockres->l_flags & ~clear);
627}
628
629static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
630{
631 mlog_entry_void();
632
633 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
634 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
635 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
636 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
637
638 lockres->l_level = lockres->l_requested;
639 if (lockres->l_level <=
640 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
641 lockres->l_blocking = LKM_NLMODE;
642 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
643 }
644 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
645
646 mlog_exit_void();
647}
648
649static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
650{
651 mlog_entry_void();
652
653 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
654 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
655
656 /* Convert from RO to EX doesn't really need anything as our
657 * information is already up to data. Convert from NL to
658 * *anything* however should mark ourselves as needing an
659 * update */
Mark Fashehf625c972006-09-12 21:24:53 -0700660 if (lockres->l_level == LKM_NLMODE &&
661 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800662 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
663
664 lockres->l_level = lockres->l_requested;
665 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
666
667 mlog_exit_void();
668}
669
670static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
671{
672 mlog_entry_void();
673
Roel Kluin3cf0c502007-10-27 00:20:36 +0200674 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800675 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
676
677 if (lockres->l_requested > LKM_NLMODE &&
Mark Fashehf625c972006-09-12 21:24:53 -0700678 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
679 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800680 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
681
682 lockres->l_level = lockres->l_requested;
683 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
684 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
685
686 mlog_exit_void();
687}
688
Mark Fashehccd979b2005-12-15 14:31:24 -0800689static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
690 int level)
691{
692 int needs_downconvert = 0;
693 mlog_entry_void();
694
695 assert_spin_locked(&lockres->l_lock);
696
697 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
698
699 if (level > lockres->l_blocking) {
700 /* only schedule a downconvert if we haven't already scheduled
701 * one that goes low enough to satisfy the level we're
702 * blocking. this also catches the case where we get
703 * duplicate BASTs */
704 if (ocfs2_highest_compat_lock_level(level) <
705 ocfs2_highest_compat_lock_level(lockres->l_blocking))
706 needs_downconvert = 1;
707
708 lockres->l_blocking = level;
709 }
710
711 mlog_exit(needs_downconvert);
712 return needs_downconvert;
713}
714
Mark Fashehaa2623a2006-09-12 21:58:23 -0700715static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800716{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700717 struct ocfs2_lock_res *lockres = opaque;
718 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800719 int needs_downconvert;
720 unsigned long flags;
721
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 BUG_ON(level <= LKM_NLMODE);
723
Mark Fashehaa2623a2006-09-12 21:58:23 -0700724 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
725 lockres->l_name, level, lockres->l_level,
726 ocfs2_lock_type_string(lockres->l_type));
727
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 spin_lock_irqsave(&lockres->l_lock, flags);
729 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
730 if (needs_downconvert)
731 ocfs2_schedule_blocked_lock(osb, lockres);
732 spin_unlock_irqrestore(&lockres->l_lock, flags);
733
Mark Fashehd680efe2006-09-08 14:14:34 -0700734 wake_up(&lockres->l_event);
735
Mark Fasheh34d024f2007-09-24 15:56:19 -0700736 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800737}
738
Mark Fashehe92d57d2006-09-12 21:34:35 -0700739static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800740{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700741 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800742 struct dlm_lockstatus *lksb = &lockres->l_lksb;
743 unsigned long flags;
744
745 spin_lock_irqsave(&lockres->l_lock, flags);
746
747 if (lksb->status != DLM_NORMAL) {
748 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
749 lockres->l_name, lksb->status);
750 spin_unlock_irqrestore(&lockres->l_lock, flags);
751 return;
752 }
753
754 switch(lockres->l_action) {
755 case OCFS2_AST_ATTACH:
756 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700757 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800758 break;
759 case OCFS2_AST_CONVERT:
760 ocfs2_generic_handle_convert_action(lockres);
761 break;
762 case OCFS2_AST_DOWNCONVERT:
763 ocfs2_generic_handle_downconvert_action(lockres);
764 break;
765 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700766 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
767 "lockres flags = 0x%lx, unlock action: %u\n",
768 lockres->l_name, lockres->l_action, lockres->l_flags,
769 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -0800770 BUG();
771 }
772
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 /* set it to something invalid so if we get called again we
774 * can catch it. */
775 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800776
777 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700778 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800779}
780
Mark Fashehccd979b2005-12-15 14:31:24 -0800781static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
782 int convert)
783{
784 unsigned long flags;
785
786 mlog_entry_void();
787 spin_lock_irqsave(&lockres->l_lock, flags);
788 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
789 if (convert)
790 lockres->l_action = OCFS2_AST_INVALID;
791 else
792 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
793 spin_unlock_irqrestore(&lockres->l_lock, flags);
794
795 wake_up(&lockres->l_event);
796 mlog_exit_void();
797}
798
799/* Note: If we detect another process working on the lock (i.e.,
800 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
801 * to do the right thing in that case.
802 */
803static int ocfs2_lock_create(struct ocfs2_super *osb,
804 struct ocfs2_lock_res *lockres,
805 int level,
806 int dlm_flags)
807{
808 int ret = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800809 enum dlm_status status = DLM_NORMAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800810 unsigned long flags;
811
812 mlog_entry_void();
813
814 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
815 dlm_flags);
816
817 spin_lock_irqsave(&lockres->l_lock, flags);
818 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
819 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
820 spin_unlock_irqrestore(&lockres->l_lock, flags);
821 goto bail;
822 }
823
824 lockres->l_action = OCFS2_AST_ATTACH;
825 lockres->l_requested = level;
826 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
827 spin_unlock_irqrestore(&lockres->l_lock, flags);
828
829 status = dlmlock(osb->dlm,
830 level,
831 &lockres->l_lksb,
832 dlm_flags,
833 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -0700834 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -0700835 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -0700837 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -0800838 if (status != DLM_NORMAL) {
839 ocfs2_log_dlm_error("dlmlock", status, lockres);
840 ret = -EINVAL;
841 ocfs2_recover_from_dlm_error(lockres, 1);
842 }
843
844 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
845
846bail:
847 mlog_exit(ret);
848 return ret;
849}
850
851static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
852 int flag)
853{
854 unsigned long flags;
855 int ret;
856
857 spin_lock_irqsave(&lockres->l_lock, flags);
858 ret = lockres->l_flags & flag;
859 spin_unlock_irqrestore(&lockres->l_lock, flags);
860
861 return ret;
862}
863
864static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
865
866{
867 wait_event(lockres->l_event,
868 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
869}
870
871static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
872
873{
874 wait_event(lockres->l_event,
875 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
876}
877
878/* predict what lock level we'll be dropping down to on behalf
879 * of another node, and return true if the currently wanted
880 * level will be compatible with it. */
881static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
882 int wanted)
883{
884 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
885
886 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
887}
888
889static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
890{
891 INIT_LIST_HEAD(&mw->mw_item);
892 init_completion(&mw->mw_complete);
893}
894
895static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
896{
897 wait_for_completion(&mw->mw_complete);
898 /* Re-arm the completion in case we want to wait on it again */
899 INIT_COMPLETION(mw->mw_complete);
900 return mw->mw_status;
901}
902
903static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
904 struct ocfs2_mask_waiter *mw,
905 unsigned long mask,
906 unsigned long goal)
907{
908 BUG_ON(!list_empty(&mw->mw_item));
909
910 assert_spin_locked(&lockres->l_lock);
911
912 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
913 mw->mw_mask = mask;
914 mw->mw_goal = goal;
915}
916
917/* returns 0 if the mw that was removed was already satisfied, -EBUSY
918 * if the mask still hadn't reached its goal */
919static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
920 struct ocfs2_mask_waiter *mw)
921{
922 unsigned long flags;
923 int ret = 0;
924
925 spin_lock_irqsave(&lockres->l_lock, flags);
926 if (!list_empty(&mw->mw_item)) {
927 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
928 ret = -EBUSY;
929
930 list_del_init(&mw->mw_item);
931 init_completion(&mw->mw_complete);
932 }
933 spin_unlock_irqrestore(&lockres->l_lock, flags);
934
935 return ret;
936
937}
938
939static int ocfs2_cluster_lock(struct ocfs2_super *osb,
940 struct ocfs2_lock_res *lockres,
941 int level,
942 int lkm_flags,
943 int arg_flags)
944{
945 struct ocfs2_mask_waiter mw;
946 enum dlm_status status;
947 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
948 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
949 unsigned long flags;
950
951 mlog_entry_void();
952
953 ocfs2_init_mask_waiter(&mw);
954
Mark Fashehb80fc012006-09-12 22:08:14 -0700955 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
956 lkm_flags |= LKM_VALBLK;
957
Mark Fashehccd979b2005-12-15 14:31:24 -0800958again:
959 wait = 0;
960
961 if (catch_signals && signal_pending(current)) {
962 ret = -ERESTARTSYS;
963 goto out;
964 }
965
966 spin_lock_irqsave(&lockres->l_lock, flags);
967
968 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
969 "Cluster lock called on freeing lockres %s! flags "
970 "0x%lx\n", lockres->l_name, lockres->l_flags);
971
972 /* We only compare against the currently granted level
973 * here. If the lock is blocked waiting on a downconvert,
974 * we'll get caught below. */
975 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
976 level > lockres->l_level) {
977 /* is someone sitting in dlm_lock? If so, wait on
978 * them. */
979 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
980 wait = 1;
981 goto unlock;
982 }
983
Mark Fashehccd979b2005-12-15 14:31:24 -0800984 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
985 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
986 /* is the lock is currently blocked on behalf of
987 * another node */
988 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
989 wait = 1;
990 goto unlock;
991 }
992
993 if (level > lockres->l_level) {
994 if (lockres->l_action != OCFS2_AST_INVALID)
995 mlog(ML_ERROR, "lockres %s has action %u pending\n",
996 lockres->l_name, lockres->l_action);
997
Mark Fasheh019d1b22007-10-05 12:09:05 -0700998 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
999 lockres->l_action = OCFS2_AST_ATTACH;
1000 lkm_flags &= ~LKM_CONVERT;
1001 } else {
1002 lockres->l_action = OCFS2_AST_CONVERT;
1003 lkm_flags |= LKM_CONVERT;
1004 }
1005
Mark Fashehccd979b2005-12-15 14:31:24 -08001006 lockres->l_requested = level;
1007 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1008 spin_unlock_irqrestore(&lockres->l_lock, flags);
1009
1010 BUG_ON(level == LKM_IVMODE);
1011 BUG_ON(level == LKM_NLMODE);
1012
1013 mlog(0, "lock %s, convert from %d to level = %d\n",
1014 lockres->l_name, lockres->l_level, level);
1015
1016 /* call dlm_lock to upgrade lock now */
1017 status = dlmlock(osb->dlm,
1018 level,
1019 &lockres->l_lksb,
Mark Fasheh019d1b22007-10-05 12:09:05 -07001020 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001021 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -07001022 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -07001023 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001025 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 if (status != DLM_NORMAL) {
1027 if ((lkm_flags & LKM_NOQUEUE) &&
1028 (status == DLM_NOTQUEUED))
1029 ret = -EAGAIN;
1030 else {
1031 ocfs2_log_dlm_error("dlmlock", status,
1032 lockres);
1033 ret = -EINVAL;
1034 }
1035 ocfs2_recover_from_dlm_error(lockres, 1);
1036 goto out;
1037 }
1038
1039 mlog(0, "lock %s, successfull return from dlmlock\n",
1040 lockres->l_name);
1041
1042 /* At this point we've gone inside the dlm and need to
1043 * complete our work regardless. */
1044 catch_signals = 0;
1045
1046 /* wait for busy to clear and carry on */
1047 goto again;
1048 }
1049
1050 /* Ok, if we get here then we're good to go. */
1051 ocfs2_inc_holders(lockres, level);
1052
1053 ret = 0;
1054unlock:
1055 spin_unlock_irqrestore(&lockres->l_lock, flags);
1056out:
1057 /*
1058 * This is helping work around a lock inversion between the page lock
1059 * and dlm locks. One path holds the page lock while calling aops
1060 * which block acquiring dlm locks. The voting thread holds dlm
1061 * locks while acquiring page locks while down converting data locks.
1062 * This block is helping an aop path notice the inversion and back
1063 * off to unlock its page lock before trying the dlm lock again.
1064 */
1065 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1066 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1067 wait = 0;
1068 if (lockres_remove_mask_waiter(lockres, &mw))
1069 ret = -EAGAIN;
1070 else
1071 goto again;
1072 }
1073 if (wait) {
1074 ret = ocfs2_wait_for_mask(&mw);
1075 if (ret == 0)
1076 goto again;
1077 mlog_errno(ret);
1078 }
1079
1080 mlog_exit(ret);
1081 return ret;
1082}
1083
1084static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1085 struct ocfs2_lock_res *lockres,
1086 int level)
1087{
1088 unsigned long flags;
1089
1090 mlog_entry_void();
1091 spin_lock_irqsave(&lockres->l_lock, flags);
1092 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001093 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 spin_unlock_irqrestore(&lockres->l_lock, flags);
1095 mlog_exit_void();
1096}
1097
Adrian Bunkda661162006-11-20 03:24:28 +01001098static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1099 struct ocfs2_lock_res *lockres,
1100 int ex,
1101 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001102{
Mark Fashehd680efe2006-09-08 14:14:34 -07001103 int level = ex ? LKM_EXMODE : LKM_PRMODE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 unsigned long flags;
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001105 int lkm_flags = local ? LKM_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001106
1107 spin_lock_irqsave(&lockres->l_lock, flags);
1108 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1109 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1110 spin_unlock_irqrestore(&lockres->l_lock, flags);
1111
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001112 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001113}
1114
1115/* Grants us an EX lock on the data and metadata resources, skipping
1116 * the normal cluster directory lookup. Use this ONLY on newly created
1117 * inodes which other nodes can't possibly see, and which haven't been
1118 * hashed in the inode hash yet. This can give us a good performance
1119 * increase as it'll skip the network broadcast normally associated
1120 * with creating a new lock resource. */
1121int ocfs2_create_new_inode_locks(struct inode *inode)
1122{
1123 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001124 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001125
1126 BUG_ON(!inode);
1127 BUG_ON(!ocfs2_inode_is_new(inode));
1128
1129 mlog_entry_void();
1130
Mark Fashehb0697052006-03-03 10:24:33 -08001131 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132
1133 /* NOTE: That we don't increment any of the holder counts, nor
1134 * do we add anything to a journal handle. Since this is
1135 * supposed to be a new inode which the cluster doesn't know
1136 * about yet, there is no need to. As far as the LVB handling
1137 * is concerned, this is basically like acquiring an EX lock
1138 * on a resource which has an invalid one -- we'll set it
1139 * valid when we release the EX. */
1140
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001141 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 if (ret) {
1143 mlog_errno(ret);
1144 goto bail;
1145 }
1146
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001147 /*
1148 * We don't want to use LKM_LOCAL on a meta data lock as they
1149 * don't use a generation in their lock names.
1150 */
1151 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 if (ret) {
1153 mlog_errno(ret);
1154 goto bail;
1155 }
1156
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001157 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001158 if (ret) {
1159 mlog_errno(ret);
1160 goto bail;
1161 }
1162
Tiger Yang50008632007-03-20 16:01:38 -07001163 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1164 if (ret) {
1165 mlog_errno(ret);
1166 goto bail;
1167 }
1168
Mark Fashehccd979b2005-12-15 14:31:24 -08001169bail:
1170 mlog_exit(ret);
1171 return ret;
1172}
1173
1174int ocfs2_rw_lock(struct inode *inode, int write)
1175{
1176 int status, level;
1177 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001178 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
1180 BUG_ON(!inode);
1181
1182 mlog_entry_void();
1183
Mark Fashehb0697052006-03-03 10:24:33 -08001184 mlog(0, "inode %llu take %s RW lock\n",
1185 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 write ? "EXMODE" : "PRMODE");
1187
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001188 if (ocfs2_mount_local(osb))
1189 return 0;
1190
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1192
1193 level = write ? LKM_EXMODE : LKM_PRMODE;
1194
1195 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1196 0);
1197 if (status < 0)
1198 mlog_errno(status);
1199
1200 mlog_exit(status);
1201 return status;
1202}
1203
1204void ocfs2_rw_unlock(struct inode *inode, int write)
1205{
1206 int level = write ? LKM_EXMODE : LKM_PRMODE;
1207 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001209
1210 mlog_entry_void();
1211
Mark Fashehb0697052006-03-03 10:24:33 -08001212 mlog(0, "inode %llu drop %s RW lock\n",
1213 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 write ? "EXMODE" : "PRMODE");
1215
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001216 if (!ocfs2_mount_local(osb))
1217 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001218
1219 mlog_exit_void();
1220}
1221
Tiger Yang50008632007-03-20 16:01:38 -07001222/*
1223 * ocfs2_open_lock always get PR mode lock.
1224 */
1225int ocfs2_open_lock(struct inode *inode)
1226{
1227 int status = 0;
1228 struct ocfs2_lock_res *lockres;
1229 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1230
1231 BUG_ON(!inode);
1232
1233 mlog_entry_void();
1234
1235 mlog(0, "inode %llu take PRMODE open lock\n",
1236 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1237
1238 if (ocfs2_mount_local(osb))
1239 goto out;
1240
1241 lockres = &OCFS2_I(inode)->ip_open_lockres;
1242
1243 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1244 LKM_PRMODE, 0, 0);
1245 if (status < 0)
1246 mlog_errno(status);
1247
1248out:
1249 mlog_exit(status);
1250 return status;
1251}
1252
1253int ocfs2_try_open_lock(struct inode *inode, int write)
1254{
1255 int status = 0, level;
1256 struct ocfs2_lock_res *lockres;
1257 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1258
1259 BUG_ON(!inode);
1260
1261 mlog_entry_void();
1262
1263 mlog(0, "inode %llu try to take %s open lock\n",
1264 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1265 write ? "EXMODE" : "PRMODE");
1266
1267 if (ocfs2_mount_local(osb))
1268 goto out;
1269
1270 lockres = &OCFS2_I(inode)->ip_open_lockres;
1271
1272 level = write ? LKM_EXMODE : LKM_PRMODE;
1273
1274 /*
1275 * The file system may already holding a PRMODE/EXMODE open lock.
1276 * Since we pass LKM_NOQUEUE, the request won't block waiting on
1277 * other nodes and the -EAGAIN will indicate to the caller that
1278 * this inode is still in use.
1279 */
1280 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1281 level, LKM_NOQUEUE, 0);
1282
1283out:
1284 mlog_exit(status);
1285 return status;
1286}
1287
1288/*
1289 * ocfs2_open_unlock unlock PR and EX mode open locks.
1290 */
1291void ocfs2_open_unlock(struct inode *inode)
1292{
1293 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1294 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1295
1296 mlog_entry_void();
1297
1298 mlog(0, "inode %llu drop open lock\n",
1299 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1300
1301 if (ocfs2_mount_local(osb))
1302 goto out;
1303
1304 if(lockres->l_ro_holders)
1305 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1306 LKM_PRMODE);
1307 if(lockres->l_ex_holders)
1308 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1309 LKM_EXMODE);
1310
1311out:
1312 mlog_exit_void();
1313}
1314
Mark Fashehccd979b2005-12-15 14:31:24 -08001315int ocfs2_data_lock_full(struct inode *inode,
1316 int write,
1317 int arg_flags)
1318{
1319 int status = 0, level;
1320 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001321 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001322
1323 BUG_ON(!inode);
1324
1325 mlog_entry_void();
1326
Mark Fashehb0697052006-03-03 10:24:33 -08001327 mlog(0, "inode %llu take %s DATA lock\n",
1328 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001329 write ? "EXMODE" : "PRMODE");
1330
1331 /* We'll allow faking a readonly data lock for
1332 * rodevices. */
1333 if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
1334 if (write) {
1335 status = -EROFS;
1336 mlog_errno(status);
1337 }
1338 goto out;
1339 }
1340
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001341 if (ocfs2_mount_local(osb))
1342 goto out;
1343
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 lockres = &OCFS2_I(inode)->ip_data_lockres;
1345
1346 level = write ? LKM_EXMODE : LKM_PRMODE;
1347
1348 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
1349 0, arg_flags);
1350 if (status < 0 && status != -EAGAIN)
1351 mlog_errno(status);
1352
1353out:
1354 mlog_exit(status);
1355 return status;
1356}
1357
1358/* see ocfs2_meta_lock_with_page() */
1359int ocfs2_data_lock_with_page(struct inode *inode,
1360 int write,
1361 struct page *page)
1362{
1363 int ret;
1364
1365 ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
1366 if (ret == -EAGAIN) {
1367 unlock_page(page);
1368 if (ocfs2_data_lock(inode, write) == 0)
1369 ocfs2_data_unlock(inode, write);
1370 ret = AOP_TRUNCATED_PAGE;
1371 }
1372
1373 return ret;
1374}
1375
Mark Fasheh34d024f2007-09-24 15:56:19 -07001376static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1377 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001378{
1379 int kick = 0;
1380
1381 mlog_entry_void();
1382
1383 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001384 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001385 * condition. */
1386 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1387 switch(lockres->l_blocking) {
1388 case LKM_EXMODE:
1389 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1390 kick = 1;
1391 break;
1392 case LKM_PRMODE:
1393 if (!lockres->l_ex_holders)
1394 kick = 1;
1395 break;
1396 default:
1397 BUG();
1398 }
1399 }
1400
1401 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001402 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403
1404 mlog_exit_void();
1405}
1406
1407void ocfs2_data_unlock(struct inode *inode,
1408 int write)
1409{
1410 int level = write ? LKM_EXMODE : LKM_PRMODE;
1411 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001412 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001413
1414 mlog_entry_void();
1415
Mark Fashehb0697052006-03-03 10:24:33 -08001416 mlog(0, "inode %llu drop %s DATA lock\n",
1417 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001418 write ? "EXMODE" : "PRMODE");
1419
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001420 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1421 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1423
1424 mlog_exit_void();
1425}
1426
1427#define OCFS2_SEC_BITS 34
1428#define OCFS2_SEC_SHIFT (64 - 34)
1429#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1430
1431/* LVB only has room for 64 bits of time here so we pack it for
1432 * now. */
1433static u64 ocfs2_pack_timespec(struct timespec *spec)
1434{
1435 u64 res;
1436 u64 sec = spec->tv_sec;
1437 u32 nsec = spec->tv_nsec;
1438
1439 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1440
1441 return res;
1442}
1443
1444/* Call this with the lockres locked. I am reasonably sure we don't
1445 * need ip_lock in this function as anyone who would be changing those
1446 * values is supposed to be blocked in ocfs2_meta_lock right now. */
1447static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1448{
1449 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1450 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1451 struct ocfs2_meta_lvb *lvb;
1452
1453 mlog_entry_void();
1454
1455 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1456
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001457 /*
1458 * Invalidate the LVB of a deleted inode - this way other
1459 * nodes are forced to go to disk and discover the new inode
1460 * status.
1461 */
1462 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1463 lvb->lvb_version = 0;
1464 goto out;
1465 }
1466
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001467 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1469 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1470 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1471 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1472 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1473 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1474 lvb->lvb_iatime_packed =
1475 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1476 lvb->lvb_ictime_packed =
1477 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1478 lvb->lvb_imtime_packed =
1479 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001480 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001481 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001482 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001483
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001484out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 mlog_meta_lvb(0, lockres);
1486
1487 mlog_exit_void();
1488}
1489
1490static void ocfs2_unpack_timespec(struct timespec *spec,
1491 u64 packed_time)
1492{
1493 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1494 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1495}
1496
1497static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1498{
1499 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1500 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
1501 struct ocfs2_meta_lvb *lvb;
1502
1503 mlog_entry_void();
1504
1505 mlog_meta_lvb(0, lockres);
1506
1507 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1508
1509 /* We're safe here without the lockres lock... */
1510 spin_lock(&oi->ip_lock);
1511 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1512 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1513
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001514 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001515 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001516 ocfs2_set_inode_flags(inode);
1517
Mark Fashehccd979b2005-12-15 14:31:24 -08001518 /* fast-symlinks are a special case */
1519 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1520 inode->i_blocks = 0;
1521 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001522 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001523
1524 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1525 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1526 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1527 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1528 ocfs2_unpack_timespec(&inode->i_atime,
1529 be64_to_cpu(lvb->lvb_iatime_packed));
1530 ocfs2_unpack_timespec(&inode->i_mtime,
1531 be64_to_cpu(lvb->lvb_imtime_packed));
1532 ocfs2_unpack_timespec(&inode->i_ctime,
1533 be64_to_cpu(lvb->lvb_ictime_packed));
1534 spin_unlock(&oi->ip_lock);
1535
1536 mlog_exit_void();
1537}
1538
Mark Fashehf9e2d822006-09-12 15:35:49 -07001539static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1540 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001541{
1542 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1543
Mark Fashehf9e2d822006-09-12 15:35:49 -07001544 if (lvb->lvb_version == OCFS2_LVB_VERSION
1545 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001546 return 1;
1547 return 0;
1548}
1549
1550/* Determine whether a lock resource needs to be refreshed, and
1551 * arbitrate who gets to refresh it.
1552 *
1553 * 0 means no refresh needed.
1554 *
1555 * > 0 means you need to refresh this and you MUST call
1556 * ocfs2_complete_lock_res_refresh afterwards. */
1557static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1558{
1559 unsigned long flags;
1560 int status = 0;
1561
1562 mlog_entry_void();
1563
1564refresh_check:
1565 spin_lock_irqsave(&lockres->l_lock, flags);
1566 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1567 spin_unlock_irqrestore(&lockres->l_lock, flags);
1568 goto bail;
1569 }
1570
1571 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1572 spin_unlock_irqrestore(&lockres->l_lock, flags);
1573
1574 ocfs2_wait_on_refreshing_lock(lockres);
1575 goto refresh_check;
1576 }
1577
1578 /* Ok, I'll be the one to refresh this lock. */
1579 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1580 spin_unlock_irqrestore(&lockres->l_lock, flags);
1581
1582 status = 1;
1583bail:
1584 mlog_exit(status);
1585 return status;
1586}
1587
1588/* If status is non zero, I'll mark it as not being in refresh
1589 * anymroe, but i won't clear the needs refresh flag. */
1590static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1591 int status)
1592{
1593 unsigned long flags;
1594 mlog_entry_void();
1595
1596 spin_lock_irqsave(&lockres->l_lock, flags);
1597 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1598 if (!status)
1599 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1600 spin_unlock_irqrestore(&lockres->l_lock, flags);
1601
1602 wake_up(&lockres->l_event);
1603
1604 mlog_exit_void();
1605}
1606
1607/* may or may not return a bh if it went to disk. */
1608static int ocfs2_meta_lock_update(struct inode *inode,
1609 struct buffer_head **bh)
1610{
1611 int status = 0;
1612 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehbe9e9862007-04-18 15:22:08 -07001613 struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001614 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001615 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001616
1617 mlog_entry_void();
1618
Mark Fashehbe9e9862007-04-18 15:22:08 -07001619 if (ocfs2_mount_local(osb))
1620 goto bail;
1621
Mark Fashehccd979b2005-12-15 14:31:24 -08001622 spin_lock(&oi->ip_lock);
1623 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb0697052006-03-03 10:24:33 -08001624 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001625 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001626 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001627 spin_unlock(&oi->ip_lock);
1628 status = -ENOENT;
1629 goto bail;
1630 }
1631 spin_unlock(&oi->ip_lock);
1632
Mark Fashehbe9e9862007-04-18 15:22:08 -07001633 if (!ocfs2_should_refresh_lock_res(lockres))
1634 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001635
1636 /* This will discard any caching information we might have had
1637 * for the inode metadata. */
1638 ocfs2_metadata_cache_purge(inode);
1639
Mark Fasheh83418972007-04-23 18:53:12 -07001640 ocfs2_extent_map_trunc(inode, 0);
1641
Mark Fashehbe9e9862007-04-18 15:22:08 -07001642 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb0697052006-03-03 10:24:33 -08001643 mlog(0, "Trusting LVB on inode %llu\n",
1644 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001645 ocfs2_refresh_inode_from_lvb(inode);
1646 } else {
1647 /* Boo, we have to go to disk. */
1648 /* read bh, cast, ocfs2_refresh_inode */
1649 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1650 bh, OCFS2_BH_CACHED, inode);
1651 if (status < 0) {
1652 mlog_errno(status);
1653 goto bail_refresh;
1654 }
1655 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1656
1657 /* This is a good chance to make sure we're not
1658 * locking an invalid object.
1659 *
1660 * We bug on a stale inode here because we checked
1661 * above whether it was wiped from disk. The wiping
1662 * node provides a guarantee that we receive that
1663 * message and can mark the inode before dropping any
1664 * locks associated with it. */
1665 if (!OCFS2_IS_VALID_DINODE(fe)) {
1666 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1667 status = -EIO;
1668 goto bail_refresh;
1669 }
1670 mlog_bug_on_msg(inode->i_generation !=
1671 le32_to_cpu(fe->i_generation),
Mark Fashehb0697052006-03-03 10:24:33 -08001672 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 "inode->i_generation: %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001674 (unsigned long long)oi->ip_blkno,
1675 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001676 inode->i_generation);
1677 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1678 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb0697052006-03-03 10:24:33 -08001679 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1680 (unsigned long long)oi->ip_blkno,
1681 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001682 le32_to_cpu(fe->i_flags));
1683
1684 ocfs2_refresh_inode(inode, fe);
1685 }
1686
1687 status = 0;
1688bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07001689 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001690bail:
1691 mlog_exit(status);
1692 return status;
1693}
1694
1695static int ocfs2_assign_bh(struct inode *inode,
1696 struct buffer_head **ret_bh,
1697 struct buffer_head *passed_bh)
1698{
1699 int status;
1700
1701 if (passed_bh) {
1702 /* Ok, the update went to disk for us, use the
1703 * returned bh. */
1704 *ret_bh = passed_bh;
1705 get_bh(*ret_bh);
1706
1707 return 0;
1708 }
1709
1710 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1711 OCFS2_I(inode)->ip_blkno,
1712 ret_bh,
1713 OCFS2_BH_CACHED,
1714 inode);
1715 if (status < 0)
1716 mlog_errno(status);
1717
1718 return status;
1719}
1720
1721/*
1722 * returns < 0 error if the callback will never be called, otherwise
1723 * the result of the lock will be communicated via the callback.
1724 */
1725int ocfs2_meta_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001726 struct buffer_head **ret_bh,
1727 int ex,
1728 int arg_flags)
1729{
1730 int status, level, dlm_flags, acquired;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001731 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001732 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1733 struct buffer_head *local_bh = NULL;
1734
1735 BUG_ON(!inode);
1736
1737 mlog_entry_void();
1738
Mark Fashehb0697052006-03-03 10:24:33 -08001739 mlog(0, "inode %llu, take %s META lock\n",
1740 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001741 ex ? "EXMODE" : "PRMODE");
1742
1743 status = 0;
1744 acquired = 0;
1745 /* We'll allow faking a readonly metadata lock for
1746 * rodevices. */
1747 if (ocfs2_is_hard_readonly(osb)) {
1748 if (ex)
1749 status = -EROFS;
1750 goto bail;
1751 }
1752
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001753 if (ocfs2_mount_local(osb))
1754 goto local;
1755
Mark Fashehccd979b2005-12-15 14:31:24 -08001756 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1757 wait_event(osb->recovery_event,
1758 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1759
Mark Fashehccd979b2005-12-15 14:31:24 -08001760 lockres = &OCFS2_I(inode)->ip_meta_lockres;
1761 level = ex ? LKM_EXMODE : LKM_PRMODE;
1762 dlm_flags = 0;
1763 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1764 dlm_flags |= LKM_NOQUEUE;
1765
1766 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1767 if (status < 0) {
1768 if (status != -EAGAIN && status != -EIOCBRETRY)
1769 mlog_errno(status);
1770 goto bail;
1771 }
1772
1773 /* Notify the error cleanup path to drop the cluster lock. */
1774 acquired = 1;
1775
1776 /* We wait twice because a node may have died while we were in
1777 * the lower dlm layers. The second time though, we've
1778 * committed to owning this lock so we don't allow signals to
1779 * abort the operation. */
1780 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1781 wait_event(osb->recovery_event,
1782 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1783
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001784local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001785 /*
1786 * We only see this flag if we're being called from
1787 * ocfs2_read_locked_inode(). It means we're locking an inode
1788 * which hasn't been populated yet, so clear the refresh flag
1789 * and let the caller handle it.
1790 */
1791 if (inode->i_state & I_NEW) {
1792 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001793 if (lockres)
1794 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001795 goto bail;
1796 }
1797
Mark Fashehccd979b2005-12-15 14:31:24 -08001798 /* This is fun. The caller may want a bh back, or it may
1799 * not. ocfs2_meta_lock_update definitely wants one in, but
1800 * may or may not read one, depending on what's in the
1801 * LVB. The result of all of this is that we've *only* gone to
1802 * disk if we have to, so the complexity is worthwhile. */
1803 status = ocfs2_meta_lock_update(inode, &local_bh);
1804 if (status < 0) {
1805 if (status != -ENOENT)
1806 mlog_errno(status);
1807 goto bail;
1808 }
1809
1810 if (ret_bh) {
1811 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1812 if (status < 0) {
1813 mlog_errno(status);
1814 goto bail;
1815 }
1816 }
1817
Mark Fashehccd979b2005-12-15 14:31:24 -08001818bail:
1819 if (status < 0) {
1820 if (ret_bh && (*ret_bh)) {
1821 brelse(*ret_bh);
1822 *ret_bh = NULL;
1823 }
1824 if (acquired)
1825 ocfs2_meta_unlock(inode, ex);
1826 }
1827
1828 if (local_bh)
1829 brelse(local_bh);
1830
1831 mlog_exit(status);
1832 return status;
1833}
1834
1835/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07001836 * This is working around a lock inversion between tasks acquiring DLM
1837 * locks while holding a page lock and the downconvert thread which
1838 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08001839 *
1840 * ** These _with_page variantes are only intended to be called from aop
1841 * methods that hold page locks and return a very specific *positive* error
1842 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1843 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07001844 * The DLM is called such that it returns -EAGAIN if it would have
1845 * blocked waiting for the downconvert thread. In that case we unlock
1846 * our page so the downconvert thread can make progress. Once we've
1847 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
1848 * that called us can bubble that back up into the VFS who will then
1849 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 *
1851 * We do a blocking lock and immediate unlock before returning, though, so that
1852 * the lock has a great chance of being cached on this node by the time the VFS
1853 * calls back to retry the aop. This has a potential to livelock as nodes
1854 * ping locks back and forth, but that's a risk we're willing to take to avoid
1855 * the lock inversion simply.
1856 */
1857int ocfs2_meta_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001858 struct buffer_head **ret_bh,
1859 int ex,
1860 struct page *page)
1861{
1862 int ret;
1863
Mark Fasheh4bcec182006-10-09 16:02:40 -07001864 ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08001865 if (ret == -EAGAIN) {
1866 unlock_page(page);
Mark Fasheh4bcec182006-10-09 16:02:40 -07001867 if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 ocfs2_meta_unlock(inode, ex);
1869 ret = AOP_TRUNCATED_PAGE;
1870 }
1871
1872 return ret;
1873}
1874
Tiger Yang7f1a37e2006-11-15 15:48:42 +08001875int ocfs2_meta_lock_atime(struct inode *inode,
1876 struct vfsmount *vfsmnt,
1877 int *level)
1878{
1879 int ret;
1880
1881 mlog_entry_void();
1882 ret = ocfs2_meta_lock(inode, NULL, 0);
1883 if (ret < 0) {
1884 mlog_errno(ret);
1885 return ret;
1886 }
1887
1888 /*
1889 * If we should update atime, we will get EX lock,
1890 * otherwise we just get PR lock.
1891 */
1892 if (ocfs2_should_update_atime(inode, vfsmnt)) {
1893 struct buffer_head *bh = NULL;
1894
1895 ocfs2_meta_unlock(inode, 0);
1896 ret = ocfs2_meta_lock(inode, &bh, 1);
1897 if (ret < 0) {
1898 mlog_errno(ret);
1899 return ret;
1900 }
1901 *level = 1;
1902 if (ocfs2_should_update_atime(inode, vfsmnt))
1903 ocfs2_update_inode_atime(inode, bh);
1904 if (bh)
1905 brelse(bh);
1906 } else
1907 *level = 0;
1908
1909 mlog_exit(ret);
1910 return ret;
1911}
1912
Mark Fashehccd979b2005-12-15 14:31:24 -08001913void ocfs2_meta_unlock(struct inode *inode,
1914 int ex)
1915{
1916 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1917 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001918 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001919
1920 mlog_entry_void();
1921
Mark Fashehb0697052006-03-03 10:24:33 -08001922 mlog(0, "inode %llu drop %s META lock\n",
1923 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 ex ? "EXMODE" : "PRMODE");
1925
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001926 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
1927 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
1929
1930 mlog_exit_void();
1931}
1932
1933int ocfs2_super_lock(struct ocfs2_super *osb,
1934 int ex)
1935{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001936 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001937 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1938 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1939 struct buffer_head *bh;
1940 struct ocfs2_slot_info *si = osb->slot_info;
1941
1942 mlog_entry_void();
1943
1944 if (ocfs2_is_hard_readonly(osb))
1945 return -EROFS;
1946
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001947 if (ocfs2_mount_local(osb))
1948 goto bail;
1949
Mark Fashehccd979b2005-12-15 14:31:24 -08001950 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1951 if (status < 0) {
1952 mlog_errno(status);
1953 goto bail;
1954 }
1955
1956 /* The super block lock path is really in the best position to
1957 * know when resources covered by the lock need to be
1958 * refreshed, so we do it here. Of course, making sense of
1959 * everything is up to the caller :) */
1960 status = ocfs2_should_refresh_lock_res(lockres);
1961 if (status < 0) {
1962 mlog_errno(status);
1963 goto bail;
1964 }
1965 if (status) {
1966 bh = si->si_bh;
1967 status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
1968 si->si_inode);
1969 if (status == 0)
1970 ocfs2_update_slot_info(si);
1971
1972 ocfs2_complete_lock_res_refresh(lockres, status);
1973
1974 if (status < 0)
1975 mlog_errno(status);
1976 }
1977bail:
1978 mlog_exit(status);
1979 return status;
1980}
1981
1982void ocfs2_super_unlock(struct ocfs2_super *osb,
1983 int ex)
1984{
1985 int level = ex ? LKM_EXMODE : LKM_PRMODE;
1986 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
1987
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001988 if (!ocfs2_mount_local(osb))
1989 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001990}
1991
1992int ocfs2_rename_lock(struct ocfs2_super *osb)
1993{
1994 int status;
1995 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
1996
1997 if (ocfs2_is_hard_readonly(osb))
1998 return -EROFS;
1999
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002000 if (ocfs2_mount_local(osb))
2001 return 0;
2002
Mark Fashehccd979b2005-12-15 14:31:24 -08002003 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
2004 if (status < 0)
2005 mlog_errno(status);
2006
2007 return status;
2008}
2009
2010void ocfs2_rename_unlock(struct ocfs2_super *osb)
2011{
2012 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2013
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002014 if (!ocfs2_mount_local(osb))
2015 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002016}
2017
Mark Fashehd680efe2006-09-08 14:14:34 -07002018int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2019{
2020 int ret;
2021 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2022 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2023 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2024
2025 BUG_ON(!dl);
2026
2027 if (ocfs2_is_hard_readonly(osb))
2028 return -EROFS;
2029
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002030 if (ocfs2_mount_local(osb))
2031 return 0;
2032
Mark Fashehd680efe2006-09-08 14:14:34 -07002033 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2034 if (ret < 0)
2035 mlog_errno(ret);
2036
2037 return ret;
2038}
2039
2040void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2041{
2042 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2043 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2044 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2045
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002046 if (!ocfs2_mount_local(osb))
2047 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002048}
2049
Mark Fashehccd979b2005-12-15 14:31:24 -08002050/* Reference counting of the dlm debug structure. We want this because
2051 * open references on the debug inodes can live on after a mount, so
2052 * we can't rely on the ocfs2_super to always exist. */
2053static void ocfs2_dlm_debug_free(struct kref *kref)
2054{
2055 struct ocfs2_dlm_debug *dlm_debug;
2056
2057 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2058
2059 kfree(dlm_debug);
2060}
2061
2062void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2063{
2064 if (dlm_debug)
2065 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2066}
2067
2068static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2069{
2070 kref_get(&debug->d_refcnt);
2071}
2072
2073struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2074{
2075 struct ocfs2_dlm_debug *dlm_debug;
2076
2077 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2078 if (!dlm_debug) {
2079 mlog_errno(-ENOMEM);
2080 goto out;
2081 }
2082
2083 kref_init(&dlm_debug->d_refcnt);
2084 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2085 dlm_debug->d_locking_state = NULL;
2086out:
2087 return dlm_debug;
2088}
2089
2090/* Access to this is arbitrated for us via seq_file->sem. */
2091struct ocfs2_dlm_seq_priv {
2092 struct ocfs2_dlm_debug *p_dlm_debug;
2093 struct ocfs2_lock_res p_iter_res;
2094 struct ocfs2_lock_res p_tmp_res;
2095};
2096
2097static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2098 struct ocfs2_dlm_seq_priv *priv)
2099{
2100 struct ocfs2_lock_res *iter, *ret = NULL;
2101 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2102
2103 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2104
2105 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2106 /* discover the head of the list */
2107 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2108 mlog(0, "End of list found, %p\n", ret);
2109 break;
2110 }
2111
2112 /* We track our "dummy" iteration lockres' by a NULL
2113 * l_ops field. */
2114 if (iter->l_ops != NULL) {
2115 ret = iter;
2116 break;
2117 }
2118 }
2119
2120 return ret;
2121}
2122
2123static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2124{
2125 struct ocfs2_dlm_seq_priv *priv = m->private;
2126 struct ocfs2_lock_res *iter;
2127
2128 spin_lock(&ocfs2_dlm_tracking_lock);
2129 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2130 if (iter) {
2131 /* Since lockres' have the lifetime of their container
2132 * (which can be inodes, ocfs2_supers, etc) we want to
2133 * copy this out to a temporary lockres while still
2134 * under the spinlock. Obviously after this we can't
2135 * trust any pointers on the copy returned, but that's
2136 * ok as the information we want isn't typically held
2137 * in them. */
2138 priv->p_tmp_res = *iter;
2139 iter = &priv->p_tmp_res;
2140 }
2141 spin_unlock(&ocfs2_dlm_tracking_lock);
2142
2143 return iter;
2144}
2145
2146static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2147{
2148}
2149
2150static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2151{
2152 struct ocfs2_dlm_seq_priv *priv = m->private;
2153 struct ocfs2_lock_res *iter = v;
2154 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2155
2156 spin_lock(&ocfs2_dlm_tracking_lock);
2157 iter = ocfs2_dlm_next_res(iter, priv);
2158 list_del_init(&dummy->l_debug_list);
2159 if (iter) {
2160 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2161 priv->p_tmp_res = *iter;
2162 iter = &priv->p_tmp_res;
2163 }
2164 spin_unlock(&ocfs2_dlm_tracking_lock);
2165
2166 return iter;
2167}
2168
2169/* So that debugfs.ocfs2 can determine which format is being used */
2170#define OCFS2_DLM_DEBUG_STR_VERSION 1
2171static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2172{
2173 int i;
2174 char *lvb;
2175 struct ocfs2_lock_res *lockres = v;
2176
2177 if (!lockres)
2178 return -EINVAL;
2179
Mark Fashehd680efe2006-09-08 14:14:34 -07002180 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2181
2182 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2183 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2184 lockres->l_name,
2185 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2186 else
2187 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2188
2189 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002190 "0x%lx\t"
2191 "0x%x\t"
2192 "0x%x\t"
2193 "%u\t"
2194 "%u\t"
2195 "%d\t"
2196 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002197 lockres->l_level,
2198 lockres->l_flags,
2199 lockres->l_action,
2200 lockres->l_unlock_action,
2201 lockres->l_ro_holders,
2202 lockres->l_ex_holders,
2203 lockres->l_requested,
2204 lockres->l_blocking);
2205
2206 /* Dump the raw LVB */
2207 lvb = lockres->l_lksb.lvb;
2208 for(i = 0; i < DLM_LVB_LEN; i++)
2209 seq_printf(m, "0x%x\t", lvb[i]);
2210
2211 /* End the line */
2212 seq_printf(m, "\n");
2213 return 0;
2214}
2215
2216static struct seq_operations ocfs2_dlm_seq_ops = {
2217 .start = ocfs2_dlm_seq_start,
2218 .stop = ocfs2_dlm_seq_stop,
2219 .next = ocfs2_dlm_seq_next,
2220 .show = ocfs2_dlm_seq_show,
2221};
2222
2223static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2224{
2225 struct seq_file *seq = (struct seq_file *) file->private_data;
2226 struct ocfs2_dlm_seq_priv *priv = seq->private;
2227 struct ocfs2_lock_res *res = &priv->p_iter_res;
2228
2229 ocfs2_remove_lockres_tracking(res);
2230 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2231 return seq_release_private(inode, file);
2232}
2233
2234static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2235{
2236 int ret;
2237 struct ocfs2_dlm_seq_priv *priv;
2238 struct seq_file *seq;
2239 struct ocfs2_super *osb;
2240
2241 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2242 if (!priv) {
2243 ret = -ENOMEM;
2244 mlog_errno(ret);
2245 goto out;
2246 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002247 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002248 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2249 priv->p_dlm_debug = osb->osb_dlm_debug;
2250 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2251
2252 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2253 if (ret) {
2254 kfree(priv);
2255 mlog_errno(ret);
2256 goto out;
2257 }
2258
2259 seq = (struct seq_file *) file->private_data;
2260 seq->private = priv;
2261
2262 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2263 priv->p_dlm_debug);
2264
2265out:
2266 return ret;
2267}
2268
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002269static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002270 .open = ocfs2_dlm_debug_open,
2271 .release = ocfs2_dlm_debug_release,
2272 .read = seq_read,
2273 .llseek = seq_lseek,
2274};
2275
2276static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2277{
2278 int ret = 0;
2279 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2280
2281 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2282 S_IFREG|S_IRUSR,
2283 osb->osb_debug_root,
2284 osb,
2285 &ocfs2_dlm_debug_fops);
2286 if (!dlm_debug->d_locking_state) {
2287 ret = -EINVAL;
2288 mlog(ML_ERROR,
2289 "Unable to create locking state debugfs file.\n");
2290 goto out;
2291 }
2292
2293 ocfs2_get_dlm_debug(dlm_debug);
2294out:
2295 return ret;
2296}
2297
2298static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2299{
2300 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2301
2302 if (dlm_debug) {
2303 debugfs_remove(dlm_debug->d_locking_state);
2304 ocfs2_put_dlm_debug(dlm_debug);
2305 }
2306}
2307
2308int ocfs2_dlm_init(struct ocfs2_super *osb)
2309{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002310 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002311 u32 dlm_key;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002312 struct dlm_ctxt *dlm = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002313
2314 mlog_entry_void();
2315
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002316 if (ocfs2_mount_local(osb))
2317 goto local;
2318
Mark Fashehccd979b2005-12-15 14:31:24 -08002319 status = ocfs2_dlm_init_debug(osb);
2320 if (status < 0) {
2321 mlog_errno(status);
2322 goto bail;
2323 }
2324
Mark Fasheh34d024f2007-09-24 15:56:19 -07002325 /* launch downconvert thread */
2326 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2327 if (IS_ERR(osb->dc_task)) {
2328 status = PTR_ERR(osb->dc_task);
2329 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002330 mlog_errno(status);
2331 goto bail;
2332 }
2333
2334 /* used by the dlm code to make message headers unique, each
2335 * node in this domain must agree on this. */
2336 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2337
2338 /* for now, uuid == domain */
2339 dlm = dlm_register_domain(osb->uuid_str, dlm_key);
2340 if (IS_ERR(dlm)) {
2341 status = PTR_ERR(dlm);
2342 mlog_errno(status);
2343 goto bail;
2344 }
2345
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002346 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2347
2348local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002349 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2350 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2351
Mark Fashehccd979b2005-12-15 14:31:24 -08002352 osb->dlm = dlm;
2353
2354 status = 0;
2355bail:
2356 if (status < 0) {
2357 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002358 if (osb->dc_task)
2359 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002360 }
2361
2362 mlog_exit(status);
2363 return status;
2364}
2365
2366void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2367{
2368 mlog_entry_void();
2369
2370 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2371
2372 ocfs2_drop_osb_locks(osb);
2373
Mark Fasheh34d024f2007-09-24 15:56:19 -07002374 if (osb->dc_task) {
2375 kthread_stop(osb->dc_task);
2376 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002377 }
2378
2379 ocfs2_lock_res_free(&osb->osb_super_lockres);
2380 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2381
2382 dlm_unregister_domain(osb->dlm);
2383 osb->dlm = NULL;
2384
2385 ocfs2_dlm_shutdown_debug(osb);
2386
2387 mlog_exit_void();
2388}
2389
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002390static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002391{
2392 struct ocfs2_lock_res *lockres = opaque;
2393 unsigned long flags;
2394
2395 mlog_entry_void();
2396
2397 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2398 lockres->l_unlock_action);
2399
2400 spin_lock_irqsave(&lockres->l_lock, flags);
2401 /* We tried to cancel a convert request, but it was already
2402 * granted. All we want to do here is clear our unlock
2403 * state. The wake_up call done at the bottom is redundant
2404 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2405 * hurt anything anyway */
2406 if (status == DLM_CANCELGRANT &&
2407 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2408 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2409
2410 /* We don't clear the busy flag in this case as it
2411 * should have been cleared by the ast which the dlm
2412 * has called. */
2413 goto complete_unlock;
2414 }
2415
2416 if (status != DLM_NORMAL) {
2417 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2418 "unlock_action %d\n", status, lockres->l_name,
2419 lockres->l_unlock_action);
2420 spin_unlock_irqrestore(&lockres->l_lock, flags);
2421 return;
2422 }
2423
2424 switch(lockres->l_unlock_action) {
2425 case OCFS2_UNLOCK_CANCEL_CONVERT:
2426 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2427 lockres->l_action = OCFS2_AST_INVALID;
2428 break;
2429 case OCFS2_UNLOCK_DROP_LOCK:
2430 lockres->l_level = LKM_IVMODE;
2431 break;
2432 default:
2433 BUG();
2434 }
2435
2436 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2437complete_unlock:
2438 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2439 spin_unlock_irqrestore(&lockres->l_lock, flags);
2440
2441 wake_up(&lockres->l_event);
2442
2443 mlog_exit_void();
2444}
2445
Mark Fashehccd979b2005-12-15 14:31:24 -08002446static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002447 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002448{
2449 enum dlm_status status;
2450 unsigned long flags;
Mark Fashehb80fc012006-09-12 22:08:14 -07002451 int lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002452
2453 /* We didn't get anywhere near actually using this lockres. */
2454 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2455 goto out;
2456
Mark Fashehb80fc012006-09-12 22:08:14 -07002457 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2458 lkm_flags |= LKM_VALBLK;
2459
Mark Fashehccd979b2005-12-15 14:31:24 -08002460 spin_lock_irqsave(&lockres->l_lock, flags);
2461
2462 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2463 "lockres %s, flags 0x%lx\n",
2464 lockres->l_name, lockres->l_flags);
2465
2466 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2467 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2468 "%u, unlock_action = %u\n",
2469 lockres->l_name, lockres->l_flags, lockres->l_action,
2470 lockres->l_unlock_action);
2471
2472 spin_unlock_irqrestore(&lockres->l_lock, flags);
2473
2474 /* XXX: Today we just wait on any busy
2475 * locks... Perhaps we need to cancel converts in the
2476 * future? */
2477 ocfs2_wait_on_busy_lock(lockres);
2478
2479 spin_lock_irqsave(&lockres->l_lock, flags);
2480 }
2481
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002482 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2483 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2484 lockres->l_level == LKM_EXMODE &&
2485 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2486 lockres->l_ops->set_lvb(lockres);
2487 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002488
2489 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2490 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2491 lockres->l_name);
2492 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2493 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2494
2495 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2496 spin_unlock_irqrestore(&lockres->l_lock, flags);
2497 goto out;
2498 }
2499
2500 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2501
2502 /* make sure we never get here while waiting for an ast to
2503 * fire. */
2504 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2505
2506 /* is this necessary? */
2507 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2508 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2509 spin_unlock_irqrestore(&lockres->l_lock, flags);
2510
2511 mlog(0, "lock %s\n", lockres->l_name);
2512
Mark Fashehb80fc012006-09-12 22:08:14 -07002513 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002514 ocfs2_unlock_ast, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002515 if (status != DLM_NORMAL) {
2516 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2517 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2518 dlm_print_one_lock(lockres->l_lksb.lockid);
2519 BUG();
2520 }
2521 mlog(0, "lock %s, successfull return from dlmunlock\n",
2522 lockres->l_name);
2523
2524 ocfs2_wait_on_busy_lock(lockres);
2525out:
2526 mlog_exit(0);
2527 return 0;
2528}
2529
2530/* Mark the lockres as being dropped. It will no longer be
2531 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07002532 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08002533 * it safe to drop.
2534 *
2535 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2536void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2537{
2538 int status;
2539 struct ocfs2_mask_waiter mw;
2540 unsigned long flags;
2541
2542 ocfs2_init_mask_waiter(&mw);
2543
2544 spin_lock_irqsave(&lockres->l_lock, flags);
2545 lockres->l_flags |= OCFS2_LOCK_FREEING;
2546 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2547 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2548 spin_unlock_irqrestore(&lockres->l_lock, flags);
2549
2550 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2551
2552 status = ocfs2_wait_for_mask(&mw);
2553 if (status)
2554 mlog_errno(status);
2555
2556 spin_lock_irqsave(&lockres->l_lock, flags);
2557 }
2558 spin_unlock_irqrestore(&lockres->l_lock, flags);
2559}
2560
Mark Fashehd680efe2006-09-08 14:14:34 -07002561void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2562 struct ocfs2_lock_res *lockres)
2563{
2564 int ret;
2565
2566 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002567 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002568 if (ret)
2569 mlog_errno(ret);
2570}
2571
Mark Fashehccd979b2005-12-15 14:31:24 -08002572static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2573{
Mark Fashehd680efe2006-09-08 14:14:34 -07002574 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2575 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002576}
2577
Mark Fashehccd979b2005-12-15 14:31:24 -08002578int ocfs2_drop_inode_locks(struct inode *inode)
2579{
2580 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002581
2582 mlog_entry_void();
2583
2584 /* No need to call ocfs2_mark_lockres_freeing here -
2585 * ocfs2_clear_inode has done it for us. */
2586
2587 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002588 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002589 if (err < 0)
2590 mlog_errno(err);
2591
2592 status = err;
2593
2594 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002595 &OCFS2_I(inode)->ip_data_lockres);
2596 if (err < 0)
2597 mlog_errno(err);
2598 if (err < 0 && !status)
2599 status = err;
2600
2601 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002602 &OCFS2_I(inode)->ip_meta_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002603 if (err < 0)
2604 mlog_errno(err);
2605 if (err < 0 && !status)
2606 status = err;
2607
2608 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002609 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002610 if (err < 0)
2611 mlog_errno(err);
2612 if (err < 0 && !status)
2613 status = err;
2614
2615 mlog_exit(status);
2616 return status;
2617}
2618
2619static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2620 int new_level)
2621{
2622 assert_spin_locked(&lockres->l_lock);
2623
2624 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2625
2626 if (lockres->l_level <= new_level) {
2627 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2628 lockres->l_level, new_level);
2629 BUG();
2630 }
2631
2632 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2633 lockres->l_name, new_level, lockres->l_blocking);
2634
2635 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2636 lockres->l_requested = new_level;
2637 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2638}
2639
2640static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2641 struct ocfs2_lock_res *lockres,
2642 int new_level,
2643 int lvb)
2644{
2645 int ret, dlm_flags = LKM_CONVERT;
2646 enum dlm_status status;
2647
2648 mlog_entry_void();
2649
2650 if (lvb)
2651 dlm_flags |= LKM_VALBLK;
2652
2653 status = dlmlock(osb->dlm,
2654 new_level,
2655 &lockres->l_lksb,
2656 dlm_flags,
2657 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -07002658 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -07002659 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002660 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -07002661 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -08002662 if (status != DLM_NORMAL) {
2663 ocfs2_log_dlm_error("dlmlock", status, lockres);
2664 ret = -EINVAL;
2665 ocfs2_recover_from_dlm_error(lockres, 1);
2666 goto bail;
2667 }
2668
2669 ret = 0;
2670bail:
2671 mlog_exit(ret);
2672 return ret;
2673}
2674
2675/* returns 1 when the caller should unlock and call dlmunlock */
2676static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2677 struct ocfs2_lock_res *lockres)
2678{
2679 assert_spin_locked(&lockres->l_lock);
2680
2681 mlog_entry_void();
2682 mlog(0, "lock %s\n", lockres->l_name);
2683
2684 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2685 /* If we're already trying to cancel a lock conversion
2686 * then just drop the spinlock and allow the caller to
2687 * requeue this lock. */
2688
2689 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2690 return 0;
2691 }
2692
2693 /* were we in a convert when we got the bast fire? */
2694 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2695 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2696 /* set things up for the unlockast to know to just
2697 * clear out the ast_action and unset busy, etc. */
2698 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2699
2700 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2701 "lock %s, invalid flags: 0x%lx\n",
2702 lockres->l_name, lockres->l_flags);
2703
2704 return 1;
2705}
2706
2707static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2708 struct ocfs2_lock_res *lockres)
2709{
2710 int ret;
2711 enum dlm_status status;
2712
2713 mlog_entry_void();
2714 mlog(0, "lock %s\n", lockres->l_name);
2715
2716 ret = 0;
2717 status = dlmunlock(osb->dlm,
2718 &lockres->l_lksb,
2719 LKM_CANCEL,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002720 ocfs2_unlock_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002721 lockres);
2722 if (status != DLM_NORMAL) {
2723 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2724 ret = -EINVAL;
2725 ocfs2_recover_from_dlm_error(lockres, 0);
2726 }
2727
2728 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2729
2730 mlog_exit(ret);
2731 return ret;
2732}
2733
Mark Fashehb5e500e2006-09-13 22:01:16 -07002734static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2735 struct ocfs2_lock_res *lockres,
2736 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002737{
2738 unsigned long flags;
2739 int blocking;
2740 int new_level;
2741 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002742 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002743
2744 mlog_entry_void();
2745
2746 spin_lock_irqsave(&lockres->l_lock, flags);
2747
2748 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2749
2750recheck:
2751 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002752 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002753 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2754 spin_unlock_irqrestore(&lockres->l_lock, flags);
2755 if (ret) {
2756 ret = ocfs2_cancel_convert(osb, lockres);
2757 if (ret < 0)
2758 mlog_errno(ret);
2759 }
2760 goto leave;
2761 }
2762
2763 /* if we're blocking an exclusive and we have *any* holders,
2764 * then requeue. */
2765 if ((lockres->l_blocking == LKM_EXMODE)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002766 && (lockres->l_ex_holders || lockres->l_ro_holders))
2767 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002768
2769 /* If it's a PR we're blocking, then only
2770 * requeue if we've got any EX holders */
2771 if (lockres->l_blocking == LKM_PRMODE &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002772 lockres->l_ex_holders)
2773 goto leave_requeue;
2774
2775 /*
2776 * Can we get a lock in this state if the holder counts are
2777 * zero? The meta data unblock code used to check this.
2778 */
2779 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2780 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2781 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002782
Mark Fasheh16d5b9562006-09-13 21:10:12 -07002783 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2784
2785 if (lockres->l_ops->check_downconvert
2786 && !lockres->l_ops->check_downconvert(lockres, new_level))
2787 goto leave_requeue;
2788
Mark Fashehccd979b2005-12-15 14:31:24 -08002789 /* If we get here, then we know that there are no more
2790 * incompatible holders (and anyone asking for an incompatible
2791 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002792 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002793 goto downconvert;
2794
2795 /* Some lockres types want to do a bit of work before
2796 * downconverting a lock. Allow that here. The worker function
2797 * may sleep, so we save off a copy of what we're blocking as
2798 * it may change while we're not holding the spin lock. */
2799 blocking = lockres->l_blocking;
2800 spin_unlock_irqrestore(&lockres->l_lock, flags);
2801
Mark Fashehcc567d82006-09-13 21:52:21 -07002802 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002803
2804 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2805 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002806
2807 spin_lock_irqsave(&lockres->l_lock, flags);
2808 if (blocking != lockres->l_blocking) {
2809 /* If this changed underneath us, then we can't drop
2810 * it just yet. */
2811 goto recheck;
2812 }
2813
2814downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002815 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002816
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002817 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2818 if (lockres->l_level == LKM_EXMODE)
2819 set_lvb = 1;
2820
2821 /*
2822 * We only set the lvb if the lock has been fully
2823 * refreshed - otherwise we risk setting stale
2824 * data. Otherwise, there's no need to actually clear
2825 * out the lvb here as it's value is still valid.
2826 */
2827 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2828 lockres->l_ops->set_lvb(lockres);
2829 }
2830
Mark Fashehccd979b2005-12-15 14:31:24 -08002831 ocfs2_prepare_downconvert(lockres, new_level);
2832 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002833 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002834leave:
2835 mlog_exit(ret);
2836 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002837
2838leave_requeue:
2839 spin_unlock_irqrestore(&lockres->l_lock, flags);
2840 ctl->requeue = 1;
2841
2842 mlog_exit(0);
2843 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002844}
2845
Mark Fashehd680efe2006-09-08 14:14:34 -07002846static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2847 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08002848{
2849 struct inode *inode;
2850 struct address_space *mapping;
2851
Mark Fashehccd979b2005-12-15 14:31:24 -08002852 inode = ocfs2_lock_res_inode(lockres);
2853 mapping = inode->i_mapping;
2854
Mark Fashehf1f54062007-10-18 15:13:59 -07002855 if (S_ISREG(inode->i_mode))
2856 goto out;
2857
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08002858 /*
2859 * We need this before the filemap_fdatawrite() so that it can
2860 * transfer the dirty bit from the PTE to the
2861 * page. Unfortunately this means that even for EX->PR
2862 * downconverts, we'll lose our mappings and have to build
2863 * them up again.
2864 */
2865 unmap_mapping_range(mapping, 0, 0, 0);
2866
Mark Fashehccd979b2005-12-15 14:31:24 -08002867 if (filemap_fdatawrite(mapping)) {
Mark Fashehb0697052006-03-03 10:24:33 -08002868 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
2869 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002870 }
2871 sync_mapping_buffers(mapping);
2872 if (blocking == LKM_EXMODE) {
2873 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002874 } else {
2875 /* We only need to wait on the I/O if we're not also
2876 * truncating pages because truncate_inode_pages waits
2877 * for us above. We don't truncate pages if we're
2878 * blocking anything < EXMODE because we want to keep
2879 * them around in that case. */
2880 filemap_fdatawait(mapping);
2881 }
2882
Mark Fashehf1f54062007-10-18 15:13:59 -07002883out:
Mark Fashehd680efe2006-09-08 14:14:34 -07002884 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002885}
2886
Mark Fasheh810d5ae2006-09-13 21:39:52 -07002887static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
2888 int new_level)
2889{
2890 struct inode *inode = ocfs2_lock_res_inode(lockres);
2891 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
2892
2893 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
2894 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
2895
2896 if (checkpointed)
2897 return 1;
2898
2899 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
2900 return 0;
2901}
2902
2903static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
2904{
2905 struct inode *inode = ocfs2_lock_res_inode(lockres);
2906
2907 __ocfs2_stuff_meta_lvb(inode);
2908}
2909
Mark Fashehd680efe2006-09-08 14:14:34 -07002910/*
2911 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07002912 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07002913 * dlmglue API and push these off to the ocfs2_wq in the future.
2914 */
2915static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
2916 struct ocfs2_lock_res *lockres)
2917{
2918 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2919 ocfs2_dentry_lock_put(osb, dl);
2920}
2921
2922/*
2923 * d_delete() matching dentries before the lock downconvert.
2924 *
2925 * At this point, any process waiting to destroy the
2926 * dentry_lock due to last ref count is stopped by the
2927 * OCFS2_LOCK_QUEUED flag.
2928 *
2929 * We have two potential problems
2930 *
2931 * 1) If we do the last reference drop on our dentry_lock (via dput)
2932 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
2933 * the downconvert to finish. Instead we take an elevated
2934 * reference and push the drop until after we've completed our
2935 * unblock processing.
2936 *
2937 * 2) There might be another process with a final reference,
2938 * waiting on us to finish processing. If this is the case, we
2939 * detect it and exit out - there's no more dentries anyway.
2940 */
2941static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
2942 int blocking)
2943{
2944 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
2945 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
2946 struct dentry *dentry;
2947 unsigned long flags;
2948 int extra_ref = 0;
2949
2950 /*
2951 * This node is blocking another node from getting a read
2952 * lock. This happens when we've renamed within a
2953 * directory. We've forced the other nodes to d_delete(), but
2954 * we never actually dropped our lock because it's still
2955 * valid. The downconvert code will retain a PR for this node,
2956 * so there's no further work to do.
2957 */
2958 if (blocking == LKM_PRMODE)
2959 return UNBLOCK_CONTINUE;
2960
2961 /*
2962 * Mark this inode as potentially orphaned. The code in
2963 * ocfs2_delete_inode() will figure out whether it actually
2964 * needs to be freed or not.
2965 */
2966 spin_lock(&oi->ip_lock);
2967 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
2968 spin_unlock(&oi->ip_lock);
2969
2970 /*
2971 * Yuck. We need to make sure however that the check of
2972 * OCFS2_LOCK_FREEING and the extra reference are atomic with
2973 * respect to a reference decrement or the setting of that
2974 * flag.
2975 */
2976 spin_lock_irqsave(&lockres->l_lock, flags);
2977 spin_lock(&dentry_attach_lock);
2978 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
2979 && dl->dl_count) {
2980 dl->dl_count++;
2981 extra_ref = 1;
2982 }
2983 spin_unlock(&dentry_attach_lock);
2984 spin_unlock_irqrestore(&lockres->l_lock, flags);
2985
2986 mlog(0, "extra_ref = %d\n", extra_ref);
2987
2988 /*
2989 * We have a process waiting on us in ocfs2_dentry_iput(),
2990 * which means we can't have any more outstanding
2991 * aliases. There's no need to do any more work.
2992 */
2993 if (!extra_ref)
2994 return UNBLOCK_CONTINUE;
2995
2996 spin_lock(&dentry_attach_lock);
2997 while (1) {
2998 dentry = ocfs2_find_local_alias(dl->dl_inode,
2999 dl->dl_parent_blkno, 1);
3000 if (!dentry)
3001 break;
3002 spin_unlock(&dentry_attach_lock);
3003
3004 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3005 dentry->d_name.name);
3006
3007 /*
3008 * The following dcache calls may do an
3009 * iput(). Normally we don't want that from the
3010 * downconverting thread, but in this case it's ok
3011 * because the requesting node already has an
3012 * exclusive lock on the inode, so it can't be queued
3013 * for a downconvert.
3014 */
3015 d_delete(dentry);
3016 dput(dentry);
3017
3018 spin_lock(&dentry_attach_lock);
3019 }
3020 spin_unlock(&dentry_attach_lock);
3021
3022 /*
3023 * If we are the last holder of this dentry lock, there is no
3024 * reason to downconvert so skip straight to the unlock.
3025 */
3026 if (dl->dl_count == 1)
3027 return UNBLOCK_STOP_POST;
3028
3029 return UNBLOCK_CONTINUE_POST;
3030}
3031
Mark Fashehccd979b2005-12-15 14:31:24 -08003032void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3033 struct ocfs2_lock_res *lockres)
3034{
3035 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003036 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003037 unsigned long flags;
3038
3039 /* Our reference to the lockres in this function can be
3040 * considered valid until we remove the OCFS2_LOCK_QUEUED
3041 * flag. */
3042
3043 mlog_entry_void();
3044
3045 BUG_ON(!lockres);
3046 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003047
3048 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3049
3050 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003051 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003052 * still be marked with OCFS2_LOCK_FREEING after this check,
3053 * but short circuiting here will still save us some
3054 * performance. */
3055 spin_lock_irqsave(&lockres->l_lock, flags);
3056 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3057 goto unqueue;
3058 spin_unlock_irqrestore(&lockres->l_lock, flags);
3059
Mark Fashehb5e500e2006-09-13 22:01:16 -07003060 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003061 if (status < 0)
3062 mlog_errno(status);
3063
3064 spin_lock_irqsave(&lockres->l_lock, flags);
3065unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003066 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003067 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3068 } else
3069 ocfs2_schedule_blocked_lock(osb, lockres);
3070
3071 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003072 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003073 spin_unlock_irqrestore(&lockres->l_lock, flags);
3074
Mark Fashehd680efe2006-09-08 14:14:34 -07003075 if (ctl.unblock_action != UNBLOCK_CONTINUE
3076 && lockres->l_ops->post_unlock)
3077 lockres->l_ops->post_unlock(osb, lockres);
3078
Mark Fashehccd979b2005-12-15 14:31:24 -08003079 mlog_exit_void();
3080}
3081
3082static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3083 struct ocfs2_lock_res *lockres)
3084{
3085 mlog_entry_void();
3086
3087 assert_spin_locked(&lockres->l_lock);
3088
3089 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3090 /* Do not schedule a lock for downconvert when it's on
3091 * the way to destruction - any nodes wanting access
3092 * to the resource will get it soon. */
3093 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3094 lockres->l_name, lockres->l_flags);
3095 return;
3096 }
3097
3098 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3099
Mark Fasheh34d024f2007-09-24 15:56:19 -07003100 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003101 if (list_empty(&lockres->l_blocked_list)) {
3102 list_add_tail(&lockres->l_blocked_list,
3103 &osb->blocked_lock_list);
3104 osb->blocked_lock_count++;
3105 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003106 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003107
3108 mlog_exit_void();
3109}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003110
3111static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3112{
3113 unsigned long processed;
3114 struct ocfs2_lock_res *lockres;
3115
3116 mlog_entry_void();
3117
3118 spin_lock(&osb->dc_task_lock);
3119 /* grab this early so we know to try again if a state change and
3120 * wake happens part-way through our work */
3121 osb->dc_work_sequence = osb->dc_wake_sequence;
3122
3123 processed = osb->blocked_lock_count;
3124 while (processed) {
3125 BUG_ON(list_empty(&osb->blocked_lock_list));
3126
3127 lockres = list_entry(osb->blocked_lock_list.next,
3128 struct ocfs2_lock_res, l_blocked_list);
3129 list_del_init(&lockres->l_blocked_list);
3130 osb->blocked_lock_count--;
3131 spin_unlock(&osb->dc_task_lock);
3132
3133 BUG_ON(!processed);
3134 processed--;
3135
3136 ocfs2_process_blocked_lock(osb, lockres);
3137
3138 spin_lock(&osb->dc_task_lock);
3139 }
3140 spin_unlock(&osb->dc_task_lock);
3141
3142 mlog_exit_void();
3143}
3144
3145static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3146{
3147 int empty = 0;
3148
3149 spin_lock(&osb->dc_task_lock);
3150 if (list_empty(&osb->blocked_lock_list))
3151 empty = 1;
3152
3153 spin_unlock(&osb->dc_task_lock);
3154 return empty;
3155}
3156
3157static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3158{
3159 int should_wake = 0;
3160
3161 spin_lock(&osb->dc_task_lock);
3162 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3163 should_wake = 1;
3164 spin_unlock(&osb->dc_task_lock);
3165
3166 return should_wake;
3167}
3168
3169int ocfs2_downconvert_thread(void *arg)
3170{
3171 int status = 0;
3172 struct ocfs2_super *osb = arg;
3173
3174 /* only quit once we've been asked to stop and there is no more
3175 * work available */
3176 while (!(kthread_should_stop() &&
3177 ocfs2_downconvert_thread_lists_empty(osb))) {
3178
3179 wait_event_interruptible(osb->dc_event,
3180 ocfs2_downconvert_thread_should_wake(osb) ||
3181 kthread_should_stop());
3182
3183 mlog(0, "downconvert_thread: awoken\n");
3184
3185 ocfs2_downconvert_thread_do_work(osb);
3186 }
3187
3188 osb->dc_task = NULL;
3189 return status;
3190}
3191
3192void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3193{
3194 spin_lock(&osb->dc_task_lock);
3195 /* make sure the voting thread gets a swipe at whatever changes
3196 * the caller may have made to the voting state */
3197 osb->dc_wake_sequence++;
3198 spin_unlock(&osb->dc_task_lock);
3199 wake_up(&osb->dc_event);
3200}