blob: 6652a48cbe0fc0da97da83aa6c2e3c6334a7ab81 [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/kthread.h>
31#include <linux/pagemap.h>
32#include <linux/debugfs.h>
33#include <linux/seq_file.h>
34
35#include <cluster/heartbeat.h>
36#include <cluster/nodemanager.h>
37#include <cluster/tcp.h>
38
Mark Fashehccd979b2005-12-15 14:31:24 -080039#define MLOG_MASK_PREFIX ML_DLM_GLUE
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080043#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080044
45#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070046#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080047#include "dlmglue.h"
48#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080049#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080050#include "heartbeat.h"
51#include "inode.h"
52#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080053#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080054#include "slot_map.h"
55#include "super.h"
56#include "uptodate.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080057
58#include "buffer_head_io.h"
59
60struct ocfs2_mask_waiter {
61 struct list_head mw_item;
62 int mw_status;
63 struct completion mw_complete;
64 unsigned long mw_mask;
65 unsigned long mw_goal;
66};
67
Mark Fasheh54a7e752006-09-12 21:49:13 -070068static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
69static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080070static struct ocfs2_super *ocfs2_get_file_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{
Joel Becker8f2c9c12008-02-01 12:16:57 -0800114 struct ocfs2_meta_lvb *lvb =
115 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700116
117 mlog(level, "LVB information for %s (called from %s:%u):\n",
118 lockres->l_name, function, line);
119 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
120 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
121 be32_to_cpu(lvb->lvb_igeneration));
122 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
123 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
124 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
125 be16_to_cpu(lvb->lvb_imode));
126 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
127 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
128 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
129 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
130 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
131 be32_to_cpu(lvb->lvb_iattr));
132}
133
134
Mark Fashehf625c972006-09-12 21:24:53 -0700135/*
136 * OCFS2 Lock Resource Operations
137 *
138 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700139 *
140 * The most basic of lock types can point ->l_priv to their respective
141 * struct ocfs2_super and allow the default actions to manage things.
142 *
143 * Right now, each lock type also needs to implement an init function,
144 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
145 * should be called when the lock is no longer needed (i.e., object
146 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700147 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800148struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700149 /*
150 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
151 * this callback if ->l_priv is not an ocfs2_super pointer
152 */
153 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700154
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700155 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700156 * Optionally called in the downconvert thread after a
157 * successful downconvert. The lockres will not be referenced
158 * after this callback is called, so it is safe to free
159 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700160 *
161 * The exact semantics of when this is called are controlled
162 * by ->downconvert_worker()
163 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700164 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700165
166 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700167 * Allow a lock type to add checks to determine whether it is
168 * safe to downconvert a lock. Return 0 to re-queue the
169 * downconvert at a later time, nonzero to continue.
170 *
171 * For most locks, the default checks that there are no
172 * incompatible holders are sufficient.
173 *
174 * Called with the lockres spinlock held.
175 */
176 int (*check_downconvert)(struct ocfs2_lock_res *, int);
177
178 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700179 * Allows a lock type to populate the lock value block. This
180 * is called on downconvert, and when we drop a lock.
181 *
182 * Locks that want to use this should set LOCK_TYPE_USES_LVB
183 * in the flags field.
184 *
185 * Called with the lockres spinlock held.
186 */
187 void (*set_lvb)(struct ocfs2_lock_res *);
188
189 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700190 * Called from the downconvert thread when it is determined
191 * that a lock will be downconverted. This is called without
192 * any locks held so the function can do work that might
193 * schedule (syncing out data, etc).
194 *
195 * This should return any one of the ocfs2_unblock_action
196 * values, depending on what it wants the thread to do.
197 */
198 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
199
200 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700201 * LOCK_TYPE_* flags which describe the specific requirements
202 * of a lock type. Descriptions of each individual flag follow.
203 */
204 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800205};
206
Mark Fashehf625c972006-09-12 21:24:53 -0700207/*
208 * Some locks want to "refresh" potentially stale data when a
209 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
210 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
211 * individual lockres l_flags member from the ast function. It is
212 * expected that the locking wrapper will clear the
213 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
214 */
215#define LOCK_TYPE_REQUIRES_REFRESH 0x1
216
Mark Fashehb80fc012006-09-12 22:08:14 -0700217/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700218 * Indicate that a lock type makes use of the lock value block. The
219 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700220 */
221#define LOCK_TYPE_USES_LVB 0x2
222
Mark Fashehccd979b2005-12-15 14:31:24 -0800223static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700224 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700225 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800226};
227
Mark Fashehe63aecb62007-10-18 15:30:42 -0700228static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700229 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700230 .check_downconvert = ocfs2_check_meta_downconvert,
231 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700232 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700233 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800234};
235
Mark Fashehccd979b2005-12-15 14:31:24 -0800236static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700237 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800238};
239
240static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700241 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800242};
243
Mark Fashehd680efe2006-09-08 14:14:34 -0700244static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700245 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700246 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700247 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700248 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700249};
250
Tiger Yang50008632007-03-20 16:01:38 -0700251static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
252 .get_osb = ocfs2_get_inode_osb,
253 .flags = 0,
254};
255
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800256static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
257 .get_osb = ocfs2_get_file_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 ||
Tiger Yang50008632007-03-20 16:01:38 -0700264 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
265 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800266}
267
Mark Fashehccd979b2005-12-15 14:31:24 -0800268static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
269{
270 BUG_ON(!ocfs2_is_inode_lock(lockres));
271
272 return (struct inode *) lockres->l_priv;
273}
274
Mark Fashehd680efe2006-09-08 14:14:34 -0700275static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
276{
277 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
278
279 return (struct ocfs2_dentry_lock *)lockres->l_priv;
280}
281
Mark Fasheh54a7e752006-09-12 21:49:13 -0700282static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
283{
284 if (lockres->l_ops->get_osb)
285 return lockres->l_ops->get_osb(lockres);
286
287 return (struct ocfs2_super *)lockres->l_priv;
288}
289
Mark Fashehccd979b2005-12-15 14:31:24 -0800290static int ocfs2_lock_create(struct ocfs2_super *osb,
291 struct ocfs2_lock_res *lockres,
292 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800293 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800294static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
295 int wanted);
296static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
297 struct ocfs2_lock_res *lockres,
298 int level);
299static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
300static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
301static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
302static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
303static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
304 struct ocfs2_lock_res *lockres);
305static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
306 int convert);
Joel Becker7431cd72008-02-01 12:15:37 -0800307#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
308 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
309 _err, _func, _lockres->l_name); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800310} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700311static int ocfs2_downconvert_thread(void *arg);
312static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
313 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700314static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800315 struct buffer_head **bh);
316static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
317static inline int ocfs2_highest_compat_lock_level(int level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800318static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
319 int new_level);
320static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres,
322 int new_level,
323 int lvb);
324static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
325 struct ocfs2_lock_res *lockres);
326static int ocfs2_cancel_convert(struct ocfs2_super *osb,
327 struct ocfs2_lock_res *lockres);
328
Mark Fashehccd979b2005-12-15 14:31:24 -0800329
Mark Fashehccd979b2005-12-15 14:31:24 -0800330static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
331 u64 blkno,
332 u32 generation,
333 char *name)
334{
335 int len;
336
337 mlog_entry_void();
338
339 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
340
Mark Fashehb06970532006-03-03 10:24:33 -0800341 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
342 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
343 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800344
345 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
346
347 mlog(0, "built lock resource with name: %s\n", name);
348
349 mlog_exit_void();
350}
351
Ingo Molnar34af9462006-06-27 02:53:55 -0700352static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800353
354static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
355 struct ocfs2_dlm_debug *dlm_debug)
356{
357 mlog(0, "Add tracking for lockres %s\n", res->l_name);
358
359 spin_lock(&ocfs2_dlm_tracking_lock);
360 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
361 spin_unlock(&ocfs2_dlm_tracking_lock);
362}
363
364static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
365{
366 spin_lock(&ocfs2_dlm_tracking_lock);
367 if (!list_empty(&res->l_debug_list))
368 list_del_init(&res->l_debug_list);
369 spin_unlock(&ocfs2_dlm_tracking_lock);
370}
371
372static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
373 struct ocfs2_lock_res *res,
374 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 struct ocfs2_lock_res_ops *ops,
376 void *priv)
377{
Mark Fashehccd979b2005-12-15 14:31:24 -0800378 res->l_type = type;
379 res->l_ops = ops;
380 res->l_priv = priv;
381
Joel Beckerbd3e7612008-02-01 12:14:57 -0800382 res->l_level = DLM_LOCK_IV;
383 res->l_requested = DLM_LOCK_IV;
384 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800385 res->l_action = OCFS2_AST_INVALID;
386 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
387
388 res->l_flags = OCFS2_LOCK_INITIALIZED;
389
390 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
391}
392
393void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
394{
395 /* This also clears out the lock status block */
396 memset(res, 0, sizeof(struct ocfs2_lock_res));
397 spin_lock_init(&res->l_lock);
398 init_waitqueue_head(&res->l_event);
399 INIT_LIST_HEAD(&res->l_blocked_list);
400 INIT_LIST_HEAD(&res->l_mask_waiters);
401}
402
403void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
404 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700405 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800406 struct inode *inode)
407{
408 struct ocfs2_lock_res_ops *ops;
409
410 switch(type) {
411 case OCFS2_LOCK_TYPE_RW:
412 ops = &ocfs2_inode_rw_lops;
413 break;
414 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700415 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 break;
Tiger Yang50008632007-03-20 16:01:38 -0700417 case OCFS2_LOCK_TYPE_OPEN:
418 ops = &ocfs2_inode_open_lops;
419 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800420 default:
421 mlog_bug_on_msg(1, "type: %d\n", type);
422 ops = NULL; /* thanks, gcc */
423 break;
424 };
425
Mark Fashehd680efe2006-09-08 14:14:34 -0700426 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700427 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700428 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
429}
430
Mark Fasheh54a7e752006-09-12 21:49:13 -0700431static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
432{
433 struct inode *inode = ocfs2_lock_res_inode(lockres);
434
435 return OCFS2_SB(inode->i_sb);
436}
437
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800438static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
439{
440 struct ocfs2_file_private *fp = lockres->l_priv;
441
442 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
443}
444
Mark Fashehd680efe2006-09-08 14:14:34 -0700445static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
446{
447 __be64 inode_blkno_be;
448
449 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
450 sizeof(__be64));
451
452 return be64_to_cpu(inode_blkno_be);
453}
454
Mark Fasheh54a7e752006-09-12 21:49:13 -0700455static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
456{
457 struct ocfs2_dentry_lock *dl = lockres->l_priv;
458
459 return OCFS2_SB(dl->dl_inode->i_sb);
460}
461
Mark Fashehd680efe2006-09-08 14:14:34 -0700462void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
463 u64 parent, struct inode *inode)
464{
465 int len;
466 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
467 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
468 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
469
470 ocfs2_lock_res_init_once(lockres);
471
472 /*
473 * Unfortunately, the standard lock naming scheme won't work
474 * here because we have two 16 byte values to use. Instead,
475 * we'll stuff the inode number as a binary value. We still
476 * want error prints to show something without garbling the
477 * display, so drop a null byte in there before the inode
478 * number. A future version of OCFS2 will likely use all
479 * binary lock names. The stringified names have been a
480 * tremendous aid in debugging, but now that the debugfs
481 * interface exists, we can mangle things there if need be.
482 *
483 * NOTE: We also drop the standard "pad" value (the total lock
484 * name size stays the same though - the last part is all
485 * zeros due to the memset in ocfs2_lock_res_init_once()
486 */
487 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
488 "%c%016llx",
489 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
490 (long long)parent);
491
492 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
493
494 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
495 sizeof(__be64));
496
497 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
498 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
499 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800500}
501
502static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
503 struct ocfs2_super *osb)
504{
505 /* Superblock lockres doesn't come from a slab so we call init
506 * once on it manually. */
507 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700508 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
509 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800511 &ocfs2_super_lops, osb);
512}
513
514static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
515 struct ocfs2_super *osb)
516{
517 /* Rename lockres doesn't come from a slab so we call init
518 * once on it manually. */
519 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700520 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
521 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800522 &ocfs2_rename_lops, osb);
523}
524
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800525void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
526 struct ocfs2_file_private *fp)
527{
528 struct inode *inode = fp->fp_file->f_mapping->host;
529 struct ocfs2_inode_info *oi = OCFS2_I(inode);
530
531 ocfs2_lock_res_init_once(lockres);
532 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
533 inode->i_generation, lockres->l_name);
534 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
535 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
536 fp);
537 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
538}
539
Mark Fashehccd979b2005-12-15 14:31:24 -0800540void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
541{
542 mlog_entry_void();
543
544 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
545 return;
546
547 ocfs2_remove_lockres_tracking(res);
548
549 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
550 "Lockres %s is on the blocked list\n",
551 res->l_name);
552 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
553 "Lockres %s has mask waiters pending\n",
554 res->l_name);
555 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
556 "Lockres %s is locked\n",
557 res->l_name);
558 mlog_bug_on_msg(res->l_ro_holders,
559 "Lockres %s has %u ro holders\n",
560 res->l_name, res->l_ro_holders);
561 mlog_bug_on_msg(res->l_ex_holders,
562 "Lockres %s has %u ex holders\n",
563 res->l_name, res->l_ex_holders);
564
565 /* Need to clear out the lock status block for the dlm */
566 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
567
568 res->l_flags = 0UL;
569 mlog_exit_void();
570}
571
572static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
573 int level)
574{
575 mlog_entry_void();
576
577 BUG_ON(!lockres);
578
579 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800580 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800581 lockres->l_ex_holders++;
582 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800583 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800584 lockres->l_ro_holders++;
585 break;
586 default:
587 BUG();
588 }
589
590 mlog_exit_void();
591}
592
593static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
594 int level)
595{
596 mlog_entry_void();
597
598 BUG_ON(!lockres);
599
600 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800601 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800602 BUG_ON(!lockres->l_ex_holders);
603 lockres->l_ex_holders--;
604 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800605 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800606 BUG_ON(!lockres->l_ro_holders);
607 lockres->l_ro_holders--;
608 break;
609 default:
610 BUG();
611 }
612 mlog_exit_void();
613}
614
615/* WARNING: This function lives in a world where the only three lock
616 * levels are EX, PR, and NL. It *will* have to be adjusted when more
617 * lock types are added. */
618static inline int ocfs2_highest_compat_lock_level(int level)
619{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800620 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800621
Joel Beckerbd3e7612008-02-01 12:14:57 -0800622 if (level == DLM_LOCK_EX)
623 new_level = DLM_LOCK_NL;
624 else if (level == DLM_LOCK_PR)
625 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 return new_level;
627}
628
629static void lockres_set_flags(struct ocfs2_lock_res *lockres,
630 unsigned long newflags)
631{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200632 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800633
634 assert_spin_locked(&lockres->l_lock);
635
636 lockres->l_flags = newflags;
637
Christoph Hellwig800deef2007-05-17 16:03:13 +0200638 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
640 continue;
641
642 list_del_init(&mw->mw_item);
643 mw->mw_status = 0;
644 complete(&mw->mw_complete);
645 }
646}
647static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
648{
649 lockres_set_flags(lockres, lockres->l_flags | or);
650}
651static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
652 unsigned long clear)
653{
654 lockres_set_flags(lockres, lockres->l_flags & ~clear);
655}
656
657static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
658{
659 mlog_entry_void();
660
661 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
662 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
663 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800664 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800665
666 lockres->l_level = lockres->l_requested;
667 if (lockres->l_level <=
668 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800669 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
671 }
672 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
673
674 mlog_exit_void();
675}
676
677static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
678{
679 mlog_entry_void();
680
681 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
682 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
683
684 /* Convert from RO to EX doesn't really need anything as our
685 * information is already up to data. Convert from NL to
686 * *anything* however should mark ourselves as needing an
687 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800688 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700689 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800690 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
691
692 lockres->l_level = lockres->l_requested;
693 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
694
695 mlog_exit_void();
696}
697
698static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
699{
700 mlog_entry_void();
701
Roel Kluin3cf0c502007-10-27 00:20:36 +0200702 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800703 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
704
Joel Beckerbd3e7612008-02-01 12:14:57 -0800705 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700706 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
707 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800708 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
709
710 lockres->l_level = lockres->l_requested;
711 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
712 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
713
714 mlog_exit_void();
715}
716
Mark Fashehccd979b2005-12-15 14:31:24 -0800717static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
718 int level)
719{
720 int needs_downconvert = 0;
721 mlog_entry_void();
722
723 assert_spin_locked(&lockres->l_lock);
724
725 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
726
727 if (level > lockres->l_blocking) {
728 /* only schedule a downconvert if we haven't already scheduled
729 * one that goes low enough to satisfy the level we're
730 * blocking. this also catches the case where we get
731 * duplicate BASTs */
732 if (ocfs2_highest_compat_lock_level(level) <
733 ocfs2_highest_compat_lock_level(lockres->l_blocking))
734 needs_downconvert = 1;
735
736 lockres->l_blocking = level;
737 }
738
739 mlog_exit(needs_downconvert);
740 return needs_downconvert;
741}
742
Mark Fashehaa2623a2006-09-12 21:58:23 -0700743static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800744{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700745 struct ocfs2_lock_res *lockres = opaque;
746 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800747 int needs_downconvert;
748 unsigned long flags;
749
Joel Beckerbd3e7612008-02-01 12:14:57 -0800750 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800751
Mark Fashehaa2623a2006-09-12 21:58:23 -0700752 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
753 lockres->l_name, level, lockres->l_level,
754 ocfs2_lock_type_string(lockres->l_type));
755
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800756 /*
757 * We can skip the bast for locks which don't enable caching -
758 * they'll be dropped at the earliest possible time anyway.
759 */
760 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
761 return;
762
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 spin_lock_irqsave(&lockres->l_lock, flags);
764 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
765 if (needs_downconvert)
766 ocfs2_schedule_blocked_lock(osb, lockres);
767 spin_unlock_irqrestore(&lockres->l_lock, flags);
768
Mark Fashehd680efe2006-09-08 14:14:34 -0700769 wake_up(&lockres->l_event);
770
Mark Fasheh34d024f2007-09-24 15:56:19 -0700771 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800772}
773
Mark Fashehe92d57d2006-09-12 21:34:35 -0700774static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800775{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700776 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 unsigned long flags;
778
779 spin_lock_irqsave(&lockres->l_lock, flags);
780
Joel Becker8f2c9c12008-02-01 12:16:57 -0800781 if (ocfs2_dlm_lock_status(&lockres->l_lksb)) {
782 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
783 lockres->l_name,
784 ocfs2_dlm_lock_status(&lockres->l_lksb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 spin_unlock_irqrestore(&lockres->l_lock, flags);
786 return;
787 }
788
789 switch(lockres->l_action) {
790 case OCFS2_AST_ATTACH:
791 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700792 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800793 break;
794 case OCFS2_AST_CONVERT:
795 ocfs2_generic_handle_convert_action(lockres);
796 break;
797 case OCFS2_AST_DOWNCONVERT:
798 ocfs2_generic_handle_downconvert_action(lockres);
799 break;
800 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700801 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
802 "lockres flags = 0x%lx, unlock action: %u\n",
803 lockres->l_name, lockres->l_action, lockres->l_flags,
804 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -0800805 BUG();
806 }
807
Mark Fashehccd979b2005-12-15 14:31:24 -0800808 /* set it to something invalid so if we get called again we
809 * can catch it. */
810 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811
812 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700813 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800814}
815
Mark Fashehccd979b2005-12-15 14:31:24 -0800816static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
817 int convert)
818{
819 unsigned long flags;
820
821 mlog_entry_void();
822 spin_lock_irqsave(&lockres->l_lock, flags);
823 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
824 if (convert)
825 lockres->l_action = OCFS2_AST_INVALID;
826 else
827 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
828 spin_unlock_irqrestore(&lockres->l_lock, flags);
829
830 wake_up(&lockres->l_event);
831 mlog_exit_void();
832}
833
834/* Note: If we detect another process working on the lock (i.e.,
835 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
836 * to do the right thing in that case.
837 */
838static int ocfs2_lock_create(struct ocfs2_super *osb,
839 struct ocfs2_lock_res *lockres,
840 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800841 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800842{
843 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800844 unsigned long flags;
845
846 mlog_entry_void();
847
Joel Beckerbd3e7612008-02-01 12:14:57 -0800848 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 dlm_flags);
850
851 spin_lock_irqsave(&lockres->l_lock, flags);
852 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
853 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
854 spin_unlock_irqrestore(&lockres->l_lock, flags);
855 goto bail;
856 }
857
858 lockres->l_action = OCFS2_AST_ATTACH;
859 lockres->l_requested = level;
860 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
861 spin_unlock_irqrestore(&lockres->l_lock, flags);
862
Joel Becker4670c462008-02-01 14:39:35 -0800863 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -0800864 level,
865 &lockres->l_lksb,
866 dlm_flags,
867 lockres->l_name,
868 OCFS2_LOCK_ID_MAX_LEN - 1,
869 lockres);
870 if (ret) {
871 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 ocfs2_recover_from_dlm_error(lockres, 1);
873 }
874
Joel Becker7431cd72008-02-01 12:15:37 -0800875 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800876
877bail:
878 mlog_exit(ret);
879 return ret;
880}
881
882static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
883 int flag)
884{
885 unsigned long flags;
886 int ret;
887
888 spin_lock_irqsave(&lockres->l_lock, flags);
889 ret = lockres->l_flags & flag;
890 spin_unlock_irqrestore(&lockres->l_lock, flags);
891
892 return ret;
893}
894
895static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
896
897{
898 wait_event(lockres->l_event,
899 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
900}
901
902static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
903
904{
905 wait_event(lockres->l_event,
906 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
907}
908
909/* predict what lock level we'll be dropping down to on behalf
910 * of another node, and return true if the currently wanted
911 * level will be compatible with it. */
912static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
913 int wanted)
914{
915 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
916
917 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
918}
919
920static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
921{
922 INIT_LIST_HEAD(&mw->mw_item);
923 init_completion(&mw->mw_complete);
924}
925
926static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
927{
928 wait_for_completion(&mw->mw_complete);
929 /* Re-arm the completion in case we want to wait on it again */
930 INIT_COMPLETION(mw->mw_complete);
931 return mw->mw_status;
932}
933
934static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
935 struct ocfs2_mask_waiter *mw,
936 unsigned long mask,
937 unsigned long goal)
938{
939 BUG_ON(!list_empty(&mw->mw_item));
940
941 assert_spin_locked(&lockres->l_lock);
942
943 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
944 mw->mw_mask = mask;
945 mw->mw_goal = goal;
946}
947
948/* returns 0 if the mw that was removed was already satisfied, -EBUSY
949 * if the mask still hadn't reached its goal */
950static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
951 struct ocfs2_mask_waiter *mw)
952{
953 unsigned long flags;
954 int ret = 0;
955
956 spin_lock_irqsave(&lockres->l_lock, flags);
957 if (!list_empty(&mw->mw_item)) {
958 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
959 ret = -EBUSY;
960
961 list_del_init(&mw->mw_item);
962 init_completion(&mw->mw_complete);
963 }
964 spin_unlock_irqrestore(&lockres->l_lock, flags);
965
966 return ret;
967
968}
969
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800970static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
971 struct ocfs2_lock_res *lockres)
972{
973 int ret;
974
975 ret = wait_for_completion_interruptible(&mw->mw_complete);
976 if (ret)
977 lockres_remove_mask_waiter(lockres, mw);
978 else
979 ret = mw->mw_status;
980 /* Re-arm the completion in case we want to wait on it again */
981 INIT_COMPLETION(mw->mw_complete);
982 return ret;
983}
984
Mark Fashehccd979b2005-12-15 14:31:24 -0800985static int ocfs2_cluster_lock(struct ocfs2_super *osb,
986 struct ocfs2_lock_res *lockres,
987 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800988 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 int arg_flags)
990{
991 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
993 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
994 unsigned long flags;
995
996 mlog_entry_void();
997
998 ocfs2_init_mask_waiter(&mw);
999
Mark Fashehb80fc012006-09-12 22:08:14 -07001000 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001001 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001002
Mark Fashehccd979b2005-12-15 14:31:24 -08001003again:
1004 wait = 0;
1005
1006 if (catch_signals && signal_pending(current)) {
1007 ret = -ERESTARTSYS;
1008 goto out;
1009 }
1010
1011 spin_lock_irqsave(&lockres->l_lock, flags);
1012
1013 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1014 "Cluster lock called on freeing lockres %s! flags "
1015 "0x%lx\n", lockres->l_name, lockres->l_flags);
1016
1017 /* We only compare against the currently granted level
1018 * here. If the lock is blocked waiting on a downconvert,
1019 * we'll get caught below. */
1020 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1021 level > lockres->l_level) {
1022 /* is someone sitting in dlm_lock? If so, wait on
1023 * them. */
1024 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1025 wait = 1;
1026 goto unlock;
1027 }
1028
Mark Fashehccd979b2005-12-15 14:31:24 -08001029 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1030 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1031 /* is the lock is currently blocked on behalf of
1032 * another node */
1033 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1034 wait = 1;
1035 goto unlock;
1036 }
1037
1038 if (level > lockres->l_level) {
1039 if (lockres->l_action != OCFS2_AST_INVALID)
1040 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1041 lockres->l_name, lockres->l_action);
1042
Mark Fasheh019d1b22007-10-05 12:09:05 -07001043 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1044 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001045 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001046 } else {
1047 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001048 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001049 }
1050
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 lockres->l_requested = level;
1052 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1053 spin_unlock_irqrestore(&lockres->l_lock, flags);
1054
Joel Beckerbd3e7612008-02-01 12:14:57 -08001055 BUG_ON(level == DLM_LOCK_IV);
1056 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001057
1058 mlog(0, "lock %s, convert from %d to level = %d\n",
1059 lockres->l_name, lockres->l_level, level);
1060
1061 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001062 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001063 level,
1064 &lockres->l_lksb,
1065 lkm_flags,
1066 lockres->l_name,
1067 OCFS2_LOCK_ID_MAX_LEN - 1,
1068 lockres);
1069 if (ret) {
1070 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1071 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001072 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001073 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 }
1075 ocfs2_recover_from_dlm_error(lockres, 1);
1076 goto out;
1077 }
1078
Joel Becker24ef1812008-01-29 17:37:32 -08001079 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 lockres->l_name);
1081
1082 /* At this point we've gone inside the dlm and need to
1083 * complete our work regardless. */
1084 catch_signals = 0;
1085
1086 /* wait for busy to clear and carry on */
1087 goto again;
1088 }
1089
1090 /* Ok, if we get here then we're good to go. */
1091 ocfs2_inc_holders(lockres, level);
1092
1093 ret = 0;
1094unlock:
1095 spin_unlock_irqrestore(&lockres->l_lock, flags);
1096out:
1097 /*
1098 * This is helping work around a lock inversion between the page lock
1099 * and dlm locks. One path holds the page lock while calling aops
1100 * which block acquiring dlm locks. The voting thread holds dlm
1101 * locks while acquiring page locks while down converting data locks.
1102 * This block is helping an aop path notice the inversion and back
1103 * off to unlock its page lock before trying the dlm lock again.
1104 */
1105 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1106 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1107 wait = 0;
1108 if (lockres_remove_mask_waiter(lockres, &mw))
1109 ret = -EAGAIN;
1110 else
1111 goto again;
1112 }
1113 if (wait) {
1114 ret = ocfs2_wait_for_mask(&mw);
1115 if (ret == 0)
1116 goto again;
1117 mlog_errno(ret);
1118 }
1119
1120 mlog_exit(ret);
1121 return ret;
1122}
1123
1124static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1125 struct ocfs2_lock_res *lockres,
1126 int level)
1127{
1128 unsigned long flags;
1129
1130 mlog_entry_void();
1131 spin_lock_irqsave(&lockres->l_lock, flags);
1132 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001133 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001134 spin_unlock_irqrestore(&lockres->l_lock, flags);
1135 mlog_exit_void();
1136}
1137
Adrian Bunkda66116e2006-11-20 03:24:28 +01001138static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1139 struct ocfs2_lock_res *lockres,
1140 int ex,
1141 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001142{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001143 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001145 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001146
1147 spin_lock_irqsave(&lockres->l_lock, flags);
1148 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1149 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1150 spin_unlock_irqrestore(&lockres->l_lock, flags);
1151
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001152 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001153}
1154
1155/* Grants us an EX lock on the data and metadata resources, skipping
1156 * the normal cluster directory lookup. Use this ONLY on newly created
1157 * inodes which other nodes can't possibly see, and which haven't been
1158 * hashed in the inode hash yet. This can give us a good performance
1159 * increase as it'll skip the network broadcast normally associated
1160 * with creating a new lock resource. */
1161int ocfs2_create_new_inode_locks(struct inode *inode)
1162{
1163 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001164 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001165
1166 BUG_ON(!inode);
1167 BUG_ON(!ocfs2_inode_is_new(inode));
1168
1169 mlog_entry_void();
1170
Mark Fashehb06970532006-03-03 10:24:33 -08001171 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001172
1173 /* NOTE: That we don't increment any of the holder counts, nor
1174 * do we add anything to a journal handle. Since this is
1175 * supposed to be a new inode which the cluster doesn't know
1176 * about yet, there is no need to. As far as the LVB handling
1177 * is concerned, this is basically like acquiring an EX lock
1178 * on a resource which has an invalid one -- we'll set it
1179 * valid when we release the EX. */
1180
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001181 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 if (ret) {
1183 mlog_errno(ret);
1184 goto bail;
1185 }
1186
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001187 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001188 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001189 * don't use a generation in their lock names.
1190 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001191 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 if (ret) {
1193 mlog_errno(ret);
1194 goto bail;
1195 }
1196
Tiger Yang50008632007-03-20 16:01:38 -07001197 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1198 if (ret) {
1199 mlog_errno(ret);
1200 goto bail;
1201 }
1202
Mark Fashehccd979b2005-12-15 14:31:24 -08001203bail:
1204 mlog_exit(ret);
1205 return ret;
1206}
1207
1208int ocfs2_rw_lock(struct inode *inode, int write)
1209{
1210 int status, level;
1211 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001212 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001213
1214 BUG_ON(!inode);
1215
1216 mlog_entry_void();
1217
Mark Fashehb06970532006-03-03 10:24:33 -08001218 mlog(0, "inode %llu take %s RW lock\n",
1219 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 write ? "EXMODE" : "PRMODE");
1221
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001222 if (ocfs2_mount_local(osb))
1223 return 0;
1224
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1226
Joel Beckerbd3e7612008-02-01 12:14:57 -08001227 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001228
1229 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1230 0);
1231 if (status < 0)
1232 mlog_errno(status);
1233
1234 mlog_exit(status);
1235 return status;
1236}
1237
1238void ocfs2_rw_unlock(struct inode *inode, int write)
1239{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001240 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001242 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001243
1244 mlog_entry_void();
1245
Mark Fashehb06970532006-03-03 10:24:33 -08001246 mlog(0, "inode %llu drop %s RW lock\n",
1247 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 write ? "EXMODE" : "PRMODE");
1249
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001250 if (!ocfs2_mount_local(osb))
1251 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001252
1253 mlog_exit_void();
1254}
1255
Tiger Yang50008632007-03-20 16:01:38 -07001256/*
1257 * ocfs2_open_lock always get PR mode lock.
1258 */
1259int ocfs2_open_lock(struct inode *inode)
1260{
1261 int status = 0;
1262 struct ocfs2_lock_res *lockres;
1263 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1264
1265 BUG_ON(!inode);
1266
1267 mlog_entry_void();
1268
1269 mlog(0, "inode %llu take PRMODE open lock\n",
1270 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1271
1272 if (ocfs2_mount_local(osb))
1273 goto out;
1274
1275 lockres = &OCFS2_I(inode)->ip_open_lockres;
1276
1277 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001278 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001279 if (status < 0)
1280 mlog_errno(status);
1281
1282out:
1283 mlog_exit(status);
1284 return status;
1285}
1286
1287int ocfs2_try_open_lock(struct inode *inode, int write)
1288{
1289 int status = 0, level;
1290 struct ocfs2_lock_res *lockres;
1291 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1292
1293 BUG_ON(!inode);
1294
1295 mlog_entry_void();
1296
1297 mlog(0, "inode %llu try to take %s open lock\n",
1298 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1299 write ? "EXMODE" : "PRMODE");
1300
1301 if (ocfs2_mount_local(osb))
1302 goto out;
1303
1304 lockres = &OCFS2_I(inode)->ip_open_lockres;
1305
Joel Beckerbd3e7612008-02-01 12:14:57 -08001306 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001307
1308 /*
1309 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001310 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001311 * other nodes and the -EAGAIN will indicate to the caller that
1312 * this inode is still in use.
1313 */
1314 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001315 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001316
1317out:
1318 mlog_exit(status);
1319 return status;
1320}
1321
1322/*
1323 * ocfs2_open_unlock unlock PR and EX mode open locks.
1324 */
1325void ocfs2_open_unlock(struct inode *inode)
1326{
1327 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1328 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1329
1330 mlog_entry_void();
1331
1332 mlog(0, "inode %llu drop open lock\n",
1333 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1334
1335 if (ocfs2_mount_local(osb))
1336 goto out;
1337
1338 if(lockres->l_ro_holders)
1339 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001340 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001341 if(lockres->l_ex_holders)
1342 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001343 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001344
1345out:
1346 mlog_exit_void();
1347}
1348
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001349static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1350 int level)
1351{
1352 int ret;
1353 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1354 unsigned long flags;
1355 struct ocfs2_mask_waiter mw;
1356
1357 ocfs2_init_mask_waiter(&mw);
1358
1359retry_cancel:
1360 spin_lock_irqsave(&lockres->l_lock, flags);
1361 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1362 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1363 if (ret) {
1364 spin_unlock_irqrestore(&lockres->l_lock, flags);
1365 ret = ocfs2_cancel_convert(osb, lockres);
1366 if (ret < 0) {
1367 mlog_errno(ret);
1368 goto out;
1369 }
1370 goto retry_cancel;
1371 }
1372 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1373 spin_unlock_irqrestore(&lockres->l_lock, flags);
1374
1375 ocfs2_wait_for_mask(&mw);
1376 goto retry_cancel;
1377 }
1378
1379 ret = -ERESTARTSYS;
1380 /*
1381 * We may still have gotten the lock, in which case there's no
1382 * point to restarting the syscall.
1383 */
1384 if (lockres->l_level == level)
1385 ret = 0;
1386
1387 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1388 lockres->l_flags, lockres->l_level, lockres->l_action);
1389
1390 spin_unlock_irqrestore(&lockres->l_lock, flags);
1391
1392out:
1393 return ret;
1394}
1395
1396/*
1397 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1398 * flock() calls. The locking approach this requires is sufficiently
1399 * different from all other cluster lock types that we implement a
1400 * seperate path to the "low-level" dlm calls. In particular:
1401 *
1402 * - No optimization of lock levels is done - we take at exactly
1403 * what's been requested.
1404 *
1405 * - No lock caching is employed. We immediately downconvert to
1406 * no-lock at unlock time. This also means flock locks never go on
1407 * the blocking list).
1408 *
1409 * - Since userspace can trivially deadlock itself with flock, we make
1410 * sure to allow cancellation of a misbehaving applications flock()
1411 * request.
1412 *
1413 * - Access to any flock lockres doesn't require concurrency, so we
1414 * can simplify the code by requiring the caller to guarantee
1415 * serialization of dlmglue flock calls.
1416 */
1417int ocfs2_file_lock(struct file *file, int ex, int trylock)
1418{
1419 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1420 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1421 unsigned long flags;
1422 struct ocfs2_file_private *fp = file->private_data;
1423 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1424 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1425 struct ocfs2_mask_waiter mw;
1426
1427 ocfs2_init_mask_waiter(&mw);
1428
1429 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001430 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001431 mlog(ML_ERROR,
1432 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1433 "level: %u\n", lockres->l_name, lockres->l_flags,
1434 lockres->l_level);
1435 return -EINVAL;
1436 }
1437
1438 spin_lock_irqsave(&lockres->l_lock, flags);
1439 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1440 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1441 spin_unlock_irqrestore(&lockres->l_lock, flags);
1442
1443 /*
1444 * Get the lock at NLMODE to start - that way we
1445 * can cancel the upconvert request if need be.
1446 */
1447 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1448 if (ret < 0) {
1449 mlog_errno(ret);
1450 goto out;
1451 }
1452
1453 ret = ocfs2_wait_for_mask(&mw);
1454 if (ret) {
1455 mlog_errno(ret);
1456 goto out;
1457 }
1458 spin_lock_irqsave(&lockres->l_lock, flags);
1459 }
1460
1461 lockres->l_action = OCFS2_AST_CONVERT;
1462 lkm_flags |= LKM_CONVERT;
1463 lockres->l_requested = level;
1464 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1465
1466 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1467 spin_unlock_irqrestore(&lockres->l_lock, flags);
1468
Joel Becker4670c462008-02-01 14:39:35 -08001469 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001470 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1471 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001472 if (ret) {
1473 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001474 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001475 ret = -EINVAL;
1476 }
1477
1478 ocfs2_recover_from_dlm_error(lockres, 1);
1479 lockres_remove_mask_waiter(lockres, &mw);
1480 goto out;
1481 }
1482
1483 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1484 if (ret == -ERESTARTSYS) {
1485 /*
1486 * Userspace can cause deadlock itself with
1487 * flock(). Current behavior locally is to allow the
1488 * deadlock, but abort the system call if a signal is
1489 * received. We follow this example, otherwise a
1490 * poorly written program could sit in kernel until
1491 * reboot.
1492 *
1493 * Handling this is a bit more complicated for Ocfs2
1494 * though. We can't exit this function with an
1495 * outstanding lock request, so a cancel convert is
1496 * required. We intentionally overwrite 'ret' - if the
1497 * cancel fails and the lock was granted, it's easier
1498 * to just bubble sucess back up to the user.
1499 */
1500 ret = ocfs2_flock_handle_signal(lockres, level);
1501 }
1502
1503out:
1504
1505 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1506 lockres->l_name, ex, trylock, ret);
1507 return ret;
1508}
1509
1510void ocfs2_file_unlock(struct file *file)
1511{
1512 int ret;
1513 unsigned long flags;
1514 struct ocfs2_file_private *fp = file->private_data;
1515 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1516 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1517 struct ocfs2_mask_waiter mw;
1518
1519 ocfs2_init_mask_waiter(&mw);
1520
1521 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1522 return;
1523
1524 if (lockres->l_level == LKM_NLMODE)
1525 return;
1526
1527 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1528 lockres->l_name, lockres->l_flags, lockres->l_level,
1529 lockres->l_action);
1530
1531 spin_lock_irqsave(&lockres->l_lock, flags);
1532 /*
1533 * Fake a blocking ast for the downconvert code.
1534 */
1535 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001536 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001537
1538 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1539 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1540 spin_unlock_irqrestore(&lockres->l_lock, flags);
1541
1542 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1543 if (ret) {
1544 mlog_errno(ret);
1545 return;
1546 }
1547
1548 ret = ocfs2_wait_for_mask(&mw);
1549 if (ret)
1550 mlog_errno(ret);
1551}
1552
Mark Fasheh34d024f2007-09-24 15:56:19 -07001553static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1554 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001555{
1556 int kick = 0;
1557
1558 mlog_entry_void();
1559
1560 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001561 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 * condition. */
1563 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1564 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001565 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001566 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1567 kick = 1;
1568 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001569 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 if (!lockres->l_ex_holders)
1571 kick = 1;
1572 break;
1573 default:
1574 BUG();
1575 }
1576 }
1577
1578 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001579 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001580
1581 mlog_exit_void();
1582}
1583
Mark Fashehccd979b2005-12-15 14:31:24 -08001584#define OCFS2_SEC_BITS 34
1585#define OCFS2_SEC_SHIFT (64 - 34)
1586#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1587
1588/* LVB only has room for 64 bits of time here so we pack it for
1589 * now. */
1590static u64 ocfs2_pack_timespec(struct timespec *spec)
1591{
1592 u64 res;
1593 u64 sec = spec->tv_sec;
1594 u32 nsec = spec->tv_nsec;
1595
1596 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1597
1598 return res;
1599}
1600
1601/* Call this with the lockres locked. I am reasonably sure we don't
1602 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001603 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001604static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1605{
1606 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001607 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001608 struct ocfs2_meta_lvb *lvb;
1609
1610 mlog_entry_void();
1611
Joel Becker8f2c9c12008-02-01 12:16:57 -08001612 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001614 /*
1615 * Invalidate the LVB of a deleted inode - this way other
1616 * nodes are forced to go to disk and discover the new inode
1617 * status.
1618 */
1619 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1620 lvb->lvb_version = 0;
1621 goto out;
1622 }
1623
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001624 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001625 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1626 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1627 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1628 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1629 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1630 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1631 lvb->lvb_iatime_packed =
1632 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1633 lvb->lvb_ictime_packed =
1634 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1635 lvb->lvb_imtime_packed =
1636 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001637 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001638 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001639 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001640
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001641out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001642 mlog_meta_lvb(0, lockres);
1643
1644 mlog_exit_void();
1645}
1646
1647static void ocfs2_unpack_timespec(struct timespec *spec,
1648 u64 packed_time)
1649{
1650 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1651 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1652}
1653
1654static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1655{
1656 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001657 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001658 struct ocfs2_meta_lvb *lvb;
1659
1660 mlog_entry_void();
1661
1662 mlog_meta_lvb(0, lockres);
1663
Joel Becker8f2c9c12008-02-01 12:16:57 -08001664 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001665
1666 /* We're safe here without the lockres lock... */
1667 spin_lock(&oi->ip_lock);
1668 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1669 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1670
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001671 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001672 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001673 ocfs2_set_inode_flags(inode);
1674
Mark Fashehccd979b2005-12-15 14:31:24 -08001675 /* fast-symlinks are a special case */
1676 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1677 inode->i_blocks = 0;
1678 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001679 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001680
1681 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1682 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1683 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1684 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1685 ocfs2_unpack_timespec(&inode->i_atime,
1686 be64_to_cpu(lvb->lvb_iatime_packed));
1687 ocfs2_unpack_timespec(&inode->i_mtime,
1688 be64_to_cpu(lvb->lvb_imtime_packed));
1689 ocfs2_unpack_timespec(&inode->i_ctime,
1690 be64_to_cpu(lvb->lvb_ictime_packed));
1691 spin_unlock(&oi->ip_lock);
1692
1693 mlog_exit_void();
1694}
1695
Mark Fashehf9e2d822006-09-12 15:35:49 -07001696static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1697 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001698{
Joel Becker8f2c9c12008-02-01 12:16:57 -08001699 struct ocfs2_meta_lvb *lvb =
1700 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001701
Mark Fashehf9e2d822006-09-12 15:35:49 -07001702 if (lvb->lvb_version == OCFS2_LVB_VERSION
1703 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001704 return 1;
1705 return 0;
1706}
1707
1708/* Determine whether a lock resource needs to be refreshed, and
1709 * arbitrate who gets to refresh it.
1710 *
1711 * 0 means no refresh needed.
1712 *
1713 * > 0 means you need to refresh this and you MUST call
1714 * ocfs2_complete_lock_res_refresh afterwards. */
1715static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1716{
1717 unsigned long flags;
1718 int status = 0;
1719
1720 mlog_entry_void();
1721
1722refresh_check:
1723 spin_lock_irqsave(&lockres->l_lock, flags);
1724 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1725 spin_unlock_irqrestore(&lockres->l_lock, flags);
1726 goto bail;
1727 }
1728
1729 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1730 spin_unlock_irqrestore(&lockres->l_lock, flags);
1731
1732 ocfs2_wait_on_refreshing_lock(lockres);
1733 goto refresh_check;
1734 }
1735
1736 /* Ok, I'll be the one to refresh this lock. */
1737 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1738 spin_unlock_irqrestore(&lockres->l_lock, flags);
1739
1740 status = 1;
1741bail:
1742 mlog_exit(status);
1743 return status;
1744}
1745
1746/* If status is non zero, I'll mark it as not being in refresh
1747 * anymroe, but i won't clear the needs refresh flag. */
1748static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1749 int status)
1750{
1751 unsigned long flags;
1752 mlog_entry_void();
1753
1754 spin_lock_irqsave(&lockres->l_lock, flags);
1755 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1756 if (!status)
1757 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1758 spin_unlock_irqrestore(&lockres->l_lock, flags);
1759
1760 wake_up(&lockres->l_event);
1761
1762 mlog_exit_void();
1763}
1764
1765/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001766static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001767 struct buffer_head **bh)
1768{
1769 int status = 0;
1770 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001771 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001772 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001773 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001774
1775 mlog_entry_void();
1776
Mark Fashehbe9e9862007-04-18 15:22:08 -07001777 if (ocfs2_mount_local(osb))
1778 goto bail;
1779
Mark Fashehccd979b2005-12-15 14:31:24 -08001780 spin_lock(&oi->ip_lock);
1781 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08001782 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001783 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001784 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001785 spin_unlock(&oi->ip_lock);
1786 status = -ENOENT;
1787 goto bail;
1788 }
1789 spin_unlock(&oi->ip_lock);
1790
Mark Fashehbe9e9862007-04-18 15:22:08 -07001791 if (!ocfs2_should_refresh_lock_res(lockres))
1792 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001793
1794 /* This will discard any caching information we might have had
1795 * for the inode metadata. */
1796 ocfs2_metadata_cache_purge(inode);
1797
Mark Fasheh83418972007-04-23 18:53:12 -07001798 ocfs2_extent_map_trunc(inode, 0);
1799
Mark Fashehbe9e9862007-04-18 15:22:08 -07001800 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001801 mlog(0, "Trusting LVB on inode %llu\n",
1802 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001803 ocfs2_refresh_inode_from_lvb(inode);
1804 } else {
1805 /* Boo, we have to go to disk. */
1806 /* read bh, cast, ocfs2_refresh_inode */
1807 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1808 bh, OCFS2_BH_CACHED, inode);
1809 if (status < 0) {
1810 mlog_errno(status);
1811 goto bail_refresh;
1812 }
1813 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1814
1815 /* This is a good chance to make sure we're not
1816 * locking an invalid object.
1817 *
1818 * We bug on a stale inode here because we checked
1819 * above whether it was wiped from disk. The wiping
1820 * node provides a guarantee that we receive that
1821 * message and can mark the inode before dropping any
1822 * locks associated with it. */
1823 if (!OCFS2_IS_VALID_DINODE(fe)) {
1824 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1825 status = -EIO;
1826 goto bail_refresh;
1827 }
1828 mlog_bug_on_msg(inode->i_generation !=
1829 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08001830 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001831 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001832 (unsigned long long)oi->ip_blkno,
1833 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001834 inode->i_generation);
1835 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1836 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08001837 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1838 (unsigned long long)oi->ip_blkno,
1839 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001840 le32_to_cpu(fe->i_flags));
1841
1842 ocfs2_refresh_inode(inode, fe);
1843 }
1844
1845 status = 0;
1846bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07001847 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001848bail:
1849 mlog_exit(status);
1850 return status;
1851}
1852
1853static int ocfs2_assign_bh(struct inode *inode,
1854 struct buffer_head **ret_bh,
1855 struct buffer_head *passed_bh)
1856{
1857 int status;
1858
1859 if (passed_bh) {
1860 /* Ok, the update went to disk for us, use the
1861 * returned bh. */
1862 *ret_bh = passed_bh;
1863 get_bh(*ret_bh);
1864
1865 return 0;
1866 }
1867
1868 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1869 OCFS2_I(inode)->ip_blkno,
1870 ret_bh,
1871 OCFS2_BH_CACHED,
1872 inode);
1873 if (status < 0)
1874 mlog_errno(status);
1875
1876 return status;
1877}
1878
1879/*
1880 * returns < 0 error if the callback will never be called, otherwise
1881 * the result of the lock will be communicated via the callback.
1882 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001883int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001884 struct buffer_head **ret_bh,
1885 int ex,
1886 int arg_flags)
1887{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001888 int status, level, acquired;
1889 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001890 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1892 struct buffer_head *local_bh = NULL;
1893
1894 BUG_ON(!inode);
1895
1896 mlog_entry_void();
1897
Mark Fashehb06970532006-03-03 10:24:33 -08001898 mlog(0, "inode %llu, take %s META lock\n",
1899 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 ex ? "EXMODE" : "PRMODE");
1901
1902 status = 0;
1903 acquired = 0;
1904 /* We'll allow faking a readonly metadata lock for
1905 * rodevices. */
1906 if (ocfs2_is_hard_readonly(osb)) {
1907 if (ex)
1908 status = -EROFS;
1909 goto bail;
1910 }
1911
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001912 if (ocfs2_mount_local(osb))
1913 goto local;
1914
Mark Fashehccd979b2005-12-15 14:31:24 -08001915 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001916 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001917
Mark Fashehe63aecb62007-10-18 15:30:42 -07001918 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001919 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001920 dlm_flags = 0;
1921 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001922 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001923
1924 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1925 if (status < 0) {
1926 if (status != -EAGAIN && status != -EIOCBRETRY)
1927 mlog_errno(status);
1928 goto bail;
1929 }
1930
1931 /* Notify the error cleanup path to drop the cluster lock. */
1932 acquired = 1;
1933
1934 /* We wait twice because a node may have died while we were in
1935 * the lower dlm layers. The second time though, we've
1936 * committed to owning this lock so we don't allow signals to
1937 * abort the operation. */
1938 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001939 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001940
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001941local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001942 /*
1943 * We only see this flag if we're being called from
1944 * ocfs2_read_locked_inode(). It means we're locking an inode
1945 * which hasn't been populated yet, so clear the refresh flag
1946 * and let the caller handle it.
1947 */
1948 if (inode->i_state & I_NEW) {
1949 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001950 if (lockres)
1951 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001952 goto bail;
1953 }
1954
Mark Fashehccd979b2005-12-15 14:31:24 -08001955 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07001956 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08001957 * may or may not read one, depending on what's in the
1958 * LVB. The result of all of this is that we've *only* gone to
1959 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001960 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001961 if (status < 0) {
1962 if (status != -ENOENT)
1963 mlog_errno(status);
1964 goto bail;
1965 }
1966
1967 if (ret_bh) {
1968 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1969 if (status < 0) {
1970 mlog_errno(status);
1971 goto bail;
1972 }
1973 }
1974
Mark Fashehccd979b2005-12-15 14:31:24 -08001975bail:
1976 if (status < 0) {
1977 if (ret_bh && (*ret_bh)) {
1978 brelse(*ret_bh);
1979 *ret_bh = NULL;
1980 }
1981 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001982 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 }
1984
1985 if (local_bh)
1986 brelse(local_bh);
1987
1988 mlog_exit(status);
1989 return status;
1990}
1991
1992/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07001993 * This is working around a lock inversion between tasks acquiring DLM
1994 * locks while holding a page lock and the downconvert thread which
1995 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08001996 *
1997 * ** These _with_page variantes are only intended to be called from aop
1998 * methods that hold page locks and return a very specific *positive* error
1999 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2000 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002001 * The DLM is called such that it returns -EAGAIN if it would have
2002 * blocked waiting for the downconvert thread. In that case we unlock
2003 * our page so the downconvert thread can make progress. Once we've
2004 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2005 * that called us can bubble that back up into the VFS who will then
2006 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002007 *
2008 * We do a blocking lock and immediate unlock before returning, though, so that
2009 * the lock has a great chance of being cached on this node by the time the VFS
2010 * calls back to retry the aop. This has a potential to livelock as nodes
2011 * ping locks back and forth, but that's a risk we're willing to take to avoid
2012 * the lock inversion simply.
2013 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002014int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002015 struct buffer_head **ret_bh,
2016 int ex,
2017 struct page *page)
2018{
2019 int ret;
2020
Mark Fashehe63aecb62007-10-18 15:30:42 -07002021 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 if (ret == -EAGAIN) {
2023 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002024 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2025 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002026 ret = AOP_TRUNCATED_PAGE;
2027 }
2028
2029 return ret;
2030}
2031
Mark Fashehe63aecb62007-10-18 15:30:42 -07002032int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002033 struct vfsmount *vfsmnt,
2034 int *level)
2035{
2036 int ret;
2037
2038 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002039 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002040 if (ret < 0) {
2041 mlog_errno(ret);
2042 return ret;
2043 }
2044
2045 /*
2046 * If we should update atime, we will get EX lock,
2047 * otherwise we just get PR lock.
2048 */
2049 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2050 struct buffer_head *bh = NULL;
2051
Mark Fashehe63aecb62007-10-18 15:30:42 -07002052 ocfs2_inode_unlock(inode, 0);
2053 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002054 if (ret < 0) {
2055 mlog_errno(ret);
2056 return ret;
2057 }
2058 *level = 1;
2059 if (ocfs2_should_update_atime(inode, vfsmnt))
2060 ocfs2_update_inode_atime(inode, bh);
2061 if (bh)
2062 brelse(bh);
2063 } else
2064 *level = 0;
2065
2066 mlog_exit(ret);
2067 return ret;
2068}
2069
Mark Fashehe63aecb62007-10-18 15:30:42 -07002070void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002071 int ex)
2072{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002073 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002074 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002075 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002076
2077 mlog_entry_void();
2078
Mark Fashehb06970532006-03-03 10:24:33 -08002079 mlog(0, "inode %llu drop %s META lock\n",
2080 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002081 ex ? "EXMODE" : "PRMODE");
2082
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002083 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2084 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002085 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2086
2087 mlog_exit_void();
2088}
2089
2090int ocfs2_super_lock(struct ocfs2_super *osb,
2091 int ex)
2092{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002093 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002094 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002095 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002096
2097 mlog_entry_void();
2098
2099 if (ocfs2_is_hard_readonly(osb))
2100 return -EROFS;
2101
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002102 if (ocfs2_mount_local(osb))
2103 goto bail;
2104
Mark Fashehccd979b2005-12-15 14:31:24 -08002105 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2106 if (status < 0) {
2107 mlog_errno(status);
2108 goto bail;
2109 }
2110
2111 /* The super block lock path is really in the best position to
2112 * know when resources covered by the lock need to be
2113 * refreshed, so we do it here. Of course, making sense of
2114 * everything is up to the caller :) */
2115 status = ocfs2_should_refresh_lock_res(lockres);
2116 if (status < 0) {
2117 mlog_errno(status);
2118 goto bail;
2119 }
2120 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002121 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002122
2123 ocfs2_complete_lock_res_refresh(lockres, status);
2124
2125 if (status < 0)
2126 mlog_errno(status);
2127 }
2128bail:
2129 mlog_exit(status);
2130 return status;
2131}
2132
2133void ocfs2_super_unlock(struct ocfs2_super *osb,
2134 int ex)
2135{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002136 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002137 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2138
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002139 if (!ocfs2_mount_local(osb))
2140 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002141}
2142
2143int ocfs2_rename_lock(struct ocfs2_super *osb)
2144{
2145 int status;
2146 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2147
2148 if (ocfs2_is_hard_readonly(osb))
2149 return -EROFS;
2150
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002151 if (ocfs2_mount_local(osb))
2152 return 0;
2153
Joel Beckerbd3e7612008-02-01 12:14:57 -08002154 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002155 if (status < 0)
2156 mlog_errno(status);
2157
2158 return status;
2159}
2160
2161void ocfs2_rename_unlock(struct ocfs2_super *osb)
2162{
2163 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2164
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002165 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002166 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002167}
2168
Mark Fashehd680efe2006-09-08 14:14:34 -07002169int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2170{
2171 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002172 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002173 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2174 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2175
2176 BUG_ON(!dl);
2177
2178 if (ocfs2_is_hard_readonly(osb))
2179 return -EROFS;
2180
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002181 if (ocfs2_mount_local(osb))
2182 return 0;
2183
Mark Fashehd680efe2006-09-08 14:14:34 -07002184 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2185 if (ret < 0)
2186 mlog_errno(ret);
2187
2188 return ret;
2189}
2190
2191void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2192{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002193 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002194 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2195 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2196
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002197 if (!ocfs2_mount_local(osb))
2198 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002199}
2200
Mark Fashehccd979b2005-12-15 14:31:24 -08002201/* Reference counting of the dlm debug structure. We want this because
2202 * open references on the debug inodes can live on after a mount, so
2203 * we can't rely on the ocfs2_super to always exist. */
2204static void ocfs2_dlm_debug_free(struct kref *kref)
2205{
2206 struct ocfs2_dlm_debug *dlm_debug;
2207
2208 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2209
2210 kfree(dlm_debug);
2211}
2212
2213void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2214{
2215 if (dlm_debug)
2216 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2217}
2218
2219static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2220{
2221 kref_get(&debug->d_refcnt);
2222}
2223
2224struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2225{
2226 struct ocfs2_dlm_debug *dlm_debug;
2227
2228 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2229 if (!dlm_debug) {
2230 mlog_errno(-ENOMEM);
2231 goto out;
2232 }
2233
2234 kref_init(&dlm_debug->d_refcnt);
2235 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2236 dlm_debug->d_locking_state = NULL;
2237out:
2238 return dlm_debug;
2239}
2240
2241/* Access to this is arbitrated for us via seq_file->sem. */
2242struct ocfs2_dlm_seq_priv {
2243 struct ocfs2_dlm_debug *p_dlm_debug;
2244 struct ocfs2_lock_res p_iter_res;
2245 struct ocfs2_lock_res p_tmp_res;
2246};
2247
2248static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2249 struct ocfs2_dlm_seq_priv *priv)
2250{
2251 struct ocfs2_lock_res *iter, *ret = NULL;
2252 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2253
2254 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2255
2256 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2257 /* discover the head of the list */
2258 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2259 mlog(0, "End of list found, %p\n", ret);
2260 break;
2261 }
2262
2263 /* We track our "dummy" iteration lockres' by a NULL
2264 * l_ops field. */
2265 if (iter->l_ops != NULL) {
2266 ret = iter;
2267 break;
2268 }
2269 }
2270
2271 return ret;
2272}
2273
2274static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2275{
2276 struct ocfs2_dlm_seq_priv *priv = m->private;
2277 struct ocfs2_lock_res *iter;
2278
2279 spin_lock(&ocfs2_dlm_tracking_lock);
2280 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2281 if (iter) {
2282 /* Since lockres' have the lifetime of their container
2283 * (which can be inodes, ocfs2_supers, etc) we want to
2284 * copy this out to a temporary lockres while still
2285 * under the spinlock. Obviously after this we can't
2286 * trust any pointers on the copy returned, but that's
2287 * ok as the information we want isn't typically held
2288 * in them. */
2289 priv->p_tmp_res = *iter;
2290 iter = &priv->p_tmp_res;
2291 }
2292 spin_unlock(&ocfs2_dlm_tracking_lock);
2293
2294 return iter;
2295}
2296
2297static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2298{
2299}
2300
2301static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2302{
2303 struct ocfs2_dlm_seq_priv *priv = m->private;
2304 struct ocfs2_lock_res *iter = v;
2305 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2306
2307 spin_lock(&ocfs2_dlm_tracking_lock);
2308 iter = ocfs2_dlm_next_res(iter, priv);
2309 list_del_init(&dummy->l_debug_list);
2310 if (iter) {
2311 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2312 priv->p_tmp_res = *iter;
2313 iter = &priv->p_tmp_res;
2314 }
2315 spin_unlock(&ocfs2_dlm_tracking_lock);
2316
2317 return iter;
2318}
2319
2320/* So that debugfs.ocfs2 can determine which format is being used */
2321#define OCFS2_DLM_DEBUG_STR_VERSION 1
2322static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2323{
2324 int i;
2325 char *lvb;
2326 struct ocfs2_lock_res *lockres = v;
2327
2328 if (!lockres)
2329 return -EINVAL;
2330
Mark Fashehd680efe2006-09-08 14:14:34 -07002331 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2332
2333 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2334 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2335 lockres->l_name,
2336 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2337 else
2338 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2339
2340 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002341 "0x%lx\t"
2342 "0x%x\t"
2343 "0x%x\t"
2344 "%u\t"
2345 "%u\t"
2346 "%d\t"
2347 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002348 lockres->l_level,
2349 lockres->l_flags,
2350 lockres->l_action,
2351 lockres->l_unlock_action,
2352 lockres->l_ro_holders,
2353 lockres->l_ex_holders,
2354 lockres->l_requested,
2355 lockres->l_blocking);
2356
2357 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002358 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002359 for(i = 0; i < DLM_LVB_LEN; i++)
2360 seq_printf(m, "0x%x\t", lvb[i]);
2361
2362 /* End the line */
2363 seq_printf(m, "\n");
2364 return 0;
2365}
2366
Jan Engelhardt90d99772008-01-22 20:52:20 +01002367static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002368 .start = ocfs2_dlm_seq_start,
2369 .stop = ocfs2_dlm_seq_stop,
2370 .next = ocfs2_dlm_seq_next,
2371 .show = ocfs2_dlm_seq_show,
2372};
2373
2374static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2375{
2376 struct seq_file *seq = (struct seq_file *) file->private_data;
2377 struct ocfs2_dlm_seq_priv *priv = seq->private;
2378 struct ocfs2_lock_res *res = &priv->p_iter_res;
2379
2380 ocfs2_remove_lockres_tracking(res);
2381 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2382 return seq_release_private(inode, file);
2383}
2384
2385static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2386{
2387 int ret;
2388 struct ocfs2_dlm_seq_priv *priv;
2389 struct seq_file *seq;
2390 struct ocfs2_super *osb;
2391
2392 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2393 if (!priv) {
2394 ret = -ENOMEM;
2395 mlog_errno(ret);
2396 goto out;
2397 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002398 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002399 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2400 priv->p_dlm_debug = osb->osb_dlm_debug;
2401 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2402
2403 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2404 if (ret) {
2405 kfree(priv);
2406 mlog_errno(ret);
2407 goto out;
2408 }
2409
2410 seq = (struct seq_file *) file->private_data;
2411 seq->private = priv;
2412
2413 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2414 priv->p_dlm_debug);
2415
2416out:
2417 return ret;
2418}
2419
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002420static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002421 .open = ocfs2_dlm_debug_open,
2422 .release = ocfs2_dlm_debug_release,
2423 .read = seq_read,
2424 .llseek = seq_lseek,
2425};
2426
2427static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2428{
2429 int ret = 0;
2430 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2431
2432 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2433 S_IFREG|S_IRUSR,
2434 osb->osb_debug_root,
2435 osb,
2436 &ocfs2_dlm_debug_fops);
2437 if (!dlm_debug->d_locking_state) {
2438 ret = -EINVAL;
2439 mlog(ML_ERROR,
2440 "Unable to create locking state debugfs file.\n");
2441 goto out;
2442 }
2443
2444 ocfs2_get_dlm_debug(dlm_debug);
2445out:
2446 return ret;
2447}
2448
2449static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2450{
2451 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2452
2453 if (dlm_debug) {
2454 debugfs_remove(dlm_debug->d_locking_state);
2455 ocfs2_put_dlm_debug(dlm_debug);
2456 }
2457}
2458
2459int ocfs2_dlm_init(struct ocfs2_super *osb)
2460{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002461 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002462 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002463
2464 mlog_entry_void();
2465
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002466 if (ocfs2_mount_local(osb))
2467 goto local;
2468
Mark Fashehccd979b2005-12-15 14:31:24 -08002469 status = ocfs2_dlm_init_debug(osb);
2470 if (status < 0) {
2471 mlog_errno(status);
2472 goto bail;
2473 }
2474
Mark Fasheh34d024f2007-09-24 15:56:19 -07002475 /* launch downconvert thread */
2476 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2477 if (IS_ERR(osb->dc_task)) {
2478 status = PTR_ERR(osb->dc_task);
2479 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002480 mlog_errno(status);
2481 goto bail;
2482 }
2483
Mark Fashehccd979b2005-12-15 14:31:24 -08002484 /* for now, uuid == domain */
Joel Becker4670c462008-02-01 14:39:35 -08002485 status = ocfs2_cluster_connect(osb->uuid_str,
2486 strlen(osb->uuid_str),
2487 ocfs2_do_node_down, osb,
2488 &conn);
2489 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002490 mlog_errno(status);
2491 goto bail;
2492 }
2493
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002494local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002495 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2496 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2497
Joel Becker4670c462008-02-01 14:39:35 -08002498 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002499
2500 status = 0;
2501bail:
2502 if (status < 0) {
2503 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002504 if (osb->dc_task)
2505 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002506 }
2507
2508 mlog_exit(status);
2509 return status;
2510}
2511
2512void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2513{
2514 mlog_entry_void();
2515
Mark Fashehccd979b2005-12-15 14:31:24 -08002516 ocfs2_drop_osb_locks(osb);
2517
Joel Becker4670c462008-02-01 14:39:35 -08002518 /*
2519 * Now that we have dropped all locks and ocfs2_dismount_volume()
2520 * has disabled recovery, the DLM won't be talking to us. It's
2521 * safe to tear things down before disconnecting the cluster.
2522 */
2523
Mark Fasheh34d024f2007-09-24 15:56:19 -07002524 if (osb->dc_task) {
2525 kthread_stop(osb->dc_task);
2526 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002527 }
2528
2529 ocfs2_lock_res_free(&osb->osb_super_lockres);
2530 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2531
Joel Becker4670c462008-02-01 14:39:35 -08002532 ocfs2_cluster_disconnect(osb->cconn);
2533 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002534
2535 ocfs2_dlm_shutdown_debug(osb);
2536
2537 mlog_exit_void();
2538}
2539
Joel Becker7431cd72008-02-01 12:15:37 -08002540static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002541{
2542 struct ocfs2_lock_res *lockres = opaque;
2543 unsigned long flags;
2544
2545 mlog_entry_void();
2546
2547 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2548 lockres->l_unlock_action);
2549
2550 spin_lock_irqsave(&lockres->l_lock, flags);
2551 /* We tried to cancel a convert request, but it was already
2552 * granted. All we want to do here is clear our unlock
2553 * state. The wake_up call done at the bottom is redundant
2554 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2555 * hurt anything anyway */
Joel Becker7431cd72008-02-01 12:15:37 -08002556 if (error == -DLM_ECANCEL &&
Mark Fashehccd979b2005-12-15 14:31:24 -08002557 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2558 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2559
2560 /* We don't clear the busy flag in this case as it
2561 * should have been cleared by the ast which the dlm
2562 * has called. */
2563 goto complete_unlock;
2564 }
2565
Joel Becker7431cd72008-02-01 12:15:37 -08002566 /* DLM_EUNLOCK is the success code for unlock */
2567 if (error != -DLM_EUNLOCK) {
2568 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2569 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002570 lockres->l_unlock_action);
2571 spin_unlock_irqrestore(&lockres->l_lock, flags);
2572 return;
2573 }
2574
2575 switch(lockres->l_unlock_action) {
2576 case OCFS2_UNLOCK_CANCEL_CONVERT:
2577 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2578 lockres->l_action = OCFS2_AST_INVALID;
2579 break;
2580 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002581 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002582 break;
2583 default:
2584 BUG();
2585 }
2586
2587 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2588complete_unlock:
2589 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2590 spin_unlock_irqrestore(&lockres->l_lock, flags);
2591
2592 wake_up(&lockres->l_event);
2593
2594 mlog_exit_void();
2595}
2596
Mark Fashehccd979b2005-12-15 14:31:24 -08002597static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002598 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002599{
Joel Becker7431cd72008-02-01 12:15:37 -08002600 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002601 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002602 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002603
2604 /* We didn't get anywhere near actually using this lockres. */
2605 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2606 goto out;
2607
Mark Fashehb80fc012006-09-12 22:08:14 -07002608 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002609 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002610
Mark Fashehccd979b2005-12-15 14:31:24 -08002611 spin_lock_irqsave(&lockres->l_lock, flags);
2612
2613 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2614 "lockres %s, flags 0x%lx\n",
2615 lockres->l_name, lockres->l_flags);
2616
2617 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2618 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2619 "%u, unlock_action = %u\n",
2620 lockres->l_name, lockres->l_flags, lockres->l_action,
2621 lockres->l_unlock_action);
2622
2623 spin_unlock_irqrestore(&lockres->l_lock, flags);
2624
2625 /* XXX: Today we just wait on any busy
2626 * locks... Perhaps we need to cancel converts in the
2627 * future? */
2628 ocfs2_wait_on_busy_lock(lockres);
2629
2630 spin_lock_irqsave(&lockres->l_lock, flags);
2631 }
2632
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002633 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2634 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08002635 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002636 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2637 lockres->l_ops->set_lvb(lockres);
2638 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002639
2640 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2641 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2642 lockres->l_name);
2643 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2644 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2645
2646 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2647 spin_unlock_irqrestore(&lockres->l_lock, flags);
2648 goto out;
2649 }
2650
2651 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2652
2653 /* make sure we never get here while waiting for an ast to
2654 * fire. */
2655 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2656
2657 /* is this necessary? */
2658 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2659 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2660 spin_unlock_irqrestore(&lockres->l_lock, flags);
2661
2662 mlog(0, "lock %s\n", lockres->l_name);
2663
Joel Becker4670c462008-02-01 14:39:35 -08002664 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08002665 lockres);
2666 if (ret) {
2667 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002668 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Becker8f2c9c12008-02-01 12:16:57 -08002669 /* XXX Need to abstract this */
2670 dlm_print_one_lock(lockres->l_lksb.lksb_o2dlm.lockid);
Mark Fashehccd979b2005-12-15 14:31:24 -08002671 BUG();
2672 }
Joel Becker24ef1812008-01-29 17:37:32 -08002673 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002674 lockres->l_name);
2675
2676 ocfs2_wait_on_busy_lock(lockres);
2677out:
2678 mlog_exit(0);
2679 return 0;
2680}
2681
2682/* Mark the lockres as being dropped. It will no longer be
2683 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07002684 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08002685 * it safe to drop.
2686 *
2687 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2688void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2689{
2690 int status;
2691 struct ocfs2_mask_waiter mw;
2692 unsigned long flags;
2693
2694 ocfs2_init_mask_waiter(&mw);
2695
2696 spin_lock_irqsave(&lockres->l_lock, flags);
2697 lockres->l_flags |= OCFS2_LOCK_FREEING;
2698 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2699 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2700 spin_unlock_irqrestore(&lockres->l_lock, flags);
2701
2702 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2703
2704 status = ocfs2_wait_for_mask(&mw);
2705 if (status)
2706 mlog_errno(status);
2707
2708 spin_lock_irqsave(&lockres->l_lock, flags);
2709 }
2710 spin_unlock_irqrestore(&lockres->l_lock, flags);
2711}
2712
Mark Fashehd680efe2006-09-08 14:14:34 -07002713void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2714 struct ocfs2_lock_res *lockres)
2715{
2716 int ret;
2717
2718 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002719 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002720 if (ret)
2721 mlog_errno(ret);
2722}
2723
Mark Fashehccd979b2005-12-15 14:31:24 -08002724static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2725{
Mark Fashehd680efe2006-09-08 14:14:34 -07002726 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2727 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002728}
2729
Mark Fashehccd979b2005-12-15 14:31:24 -08002730int ocfs2_drop_inode_locks(struct inode *inode)
2731{
2732 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002733
2734 mlog_entry_void();
2735
2736 /* No need to call ocfs2_mark_lockres_freeing here -
2737 * ocfs2_clear_inode has done it for us. */
2738
2739 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002740 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002741 if (err < 0)
2742 mlog_errno(err);
2743
2744 status = err;
2745
2746 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07002747 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002748 if (err < 0)
2749 mlog_errno(err);
2750 if (err < 0 && !status)
2751 status = err;
2752
2753 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002754 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002755 if (err < 0)
2756 mlog_errno(err);
2757 if (err < 0 && !status)
2758 status = err;
2759
2760 mlog_exit(status);
2761 return status;
2762}
2763
2764static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2765 int new_level)
2766{
2767 assert_spin_locked(&lockres->l_lock);
2768
Joel Beckerbd3e7612008-02-01 12:14:57 -08002769 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002770
2771 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002772 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002773 lockres->l_level, new_level);
2774 BUG();
2775 }
2776
2777 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2778 lockres->l_name, new_level, lockres->l_blocking);
2779
2780 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2781 lockres->l_requested = new_level;
2782 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2783}
2784
2785static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2786 struct ocfs2_lock_res *lockres,
2787 int new_level,
2788 int lvb)
2789{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002790 int ret;
2791 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08002792
2793 mlog_entry_void();
2794
2795 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002796 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08002797
Joel Becker4670c462008-02-01 14:39:35 -08002798 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08002799 new_level,
2800 &lockres->l_lksb,
2801 dlm_flags,
2802 lockres->l_name,
2803 OCFS2_LOCK_ID_MAX_LEN - 1,
2804 lockres);
2805 if (ret) {
2806 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002807 ocfs2_recover_from_dlm_error(lockres, 1);
2808 goto bail;
2809 }
2810
2811 ret = 0;
2812bail:
2813 mlog_exit(ret);
2814 return ret;
2815}
2816
Joel Becker24ef1812008-01-29 17:37:32 -08002817/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08002818static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2819 struct ocfs2_lock_res *lockres)
2820{
2821 assert_spin_locked(&lockres->l_lock);
2822
2823 mlog_entry_void();
2824 mlog(0, "lock %s\n", lockres->l_name);
2825
2826 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2827 /* If we're already trying to cancel a lock conversion
2828 * then just drop the spinlock and allow the caller to
2829 * requeue this lock. */
2830
2831 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2832 return 0;
2833 }
2834
2835 /* were we in a convert when we got the bast fire? */
2836 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2837 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2838 /* set things up for the unlockast to know to just
2839 * clear out the ast_action and unset busy, etc. */
2840 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2841
2842 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2843 "lock %s, invalid flags: 0x%lx\n",
2844 lockres->l_name, lockres->l_flags);
2845
2846 return 1;
2847}
2848
2849static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2850 struct ocfs2_lock_res *lockres)
2851{
2852 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002853
2854 mlog_entry_void();
2855 mlog(0, "lock %s\n", lockres->l_name);
2856
Joel Becker4670c462008-02-01 14:39:35 -08002857 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08002858 DLM_LKF_CANCEL, lockres);
2859 if (ret) {
2860 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002861 ocfs2_recover_from_dlm_error(lockres, 0);
2862 }
2863
Joel Becker24ef1812008-01-29 17:37:32 -08002864 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002865
2866 mlog_exit(ret);
2867 return ret;
2868}
2869
Mark Fashehb5e500e2006-09-13 22:01:16 -07002870static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2871 struct ocfs2_lock_res *lockres,
2872 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002873{
2874 unsigned long flags;
2875 int blocking;
2876 int new_level;
2877 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002878 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002879
2880 mlog_entry_void();
2881
2882 spin_lock_irqsave(&lockres->l_lock, flags);
2883
2884 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2885
2886recheck:
2887 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002888 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002889 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2890 spin_unlock_irqrestore(&lockres->l_lock, flags);
2891 if (ret) {
2892 ret = ocfs2_cancel_convert(osb, lockres);
2893 if (ret < 0)
2894 mlog_errno(ret);
2895 }
2896 goto leave;
2897 }
2898
2899 /* if we're blocking an exclusive and we have *any* holders,
2900 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002901 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002902 && (lockres->l_ex_holders || lockres->l_ro_holders))
2903 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002904
2905 /* If it's a PR we're blocking, then only
2906 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002907 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002908 lockres->l_ex_holders)
2909 goto leave_requeue;
2910
2911 /*
2912 * Can we get a lock in this state if the holder counts are
2913 * zero? The meta data unblock code used to check this.
2914 */
2915 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2916 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2917 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002918
Mark Fasheh16d5b952006-09-13 21:10:12 -07002919 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2920
2921 if (lockres->l_ops->check_downconvert
2922 && !lockres->l_ops->check_downconvert(lockres, new_level))
2923 goto leave_requeue;
2924
Mark Fashehccd979b2005-12-15 14:31:24 -08002925 /* If we get here, then we know that there are no more
2926 * incompatible holders (and anyone asking for an incompatible
2927 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002928 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002929 goto downconvert;
2930
2931 /* Some lockres types want to do a bit of work before
2932 * downconverting a lock. Allow that here. The worker function
2933 * may sleep, so we save off a copy of what we're blocking as
2934 * it may change while we're not holding the spin lock. */
2935 blocking = lockres->l_blocking;
2936 spin_unlock_irqrestore(&lockres->l_lock, flags);
2937
Mark Fashehcc567d82006-09-13 21:52:21 -07002938 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002939
2940 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2941 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002942
2943 spin_lock_irqsave(&lockres->l_lock, flags);
2944 if (blocking != lockres->l_blocking) {
2945 /* If this changed underneath us, then we can't drop
2946 * it just yet. */
2947 goto recheck;
2948 }
2949
2950downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002951 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002952
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002953 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002954 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002955 set_lvb = 1;
2956
2957 /*
2958 * We only set the lvb if the lock has been fully
2959 * refreshed - otherwise we risk setting stale
2960 * data. Otherwise, there's no need to actually clear
2961 * out the lvb here as it's value is still valid.
2962 */
2963 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2964 lockres->l_ops->set_lvb(lockres);
2965 }
2966
Mark Fashehccd979b2005-12-15 14:31:24 -08002967 ocfs2_prepare_downconvert(lockres, new_level);
2968 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002969 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002970leave:
2971 mlog_exit(ret);
2972 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002973
2974leave_requeue:
2975 spin_unlock_irqrestore(&lockres->l_lock, flags);
2976 ctl->requeue = 1;
2977
2978 mlog_exit(0);
2979 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002980}
2981
Mark Fashehd680efe2006-09-08 14:14:34 -07002982static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2983 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08002984{
2985 struct inode *inode;
2986 struct address_space *mapping;
2987
Mark Fashehccd979b2005-12-15 14:31:24 -08002988 inode = ocfs2_lock_res_inode(lockres);
2989 mapping = inode->i_mapping;
2990
Mark Fasheh1044e402008-02-28 17:16:03 -08002991 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07002992 goto out;
2993
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08002994 /*
2995 * We need this before the filemap_fdatawrite() so that it can
2996 * transfer the dirty bit from the PTE to the
2997 * page. Unfortunately this means that even for EX->PR
2998 * downconverts, we'll lose our mappings and have to build
2999 * them up again.
3000 */
3001 unmap_mapping_range(mapping, 0, 0, 0);
3002
Mark Fashehccd979b2005-12-15 14:31:24 -08003003 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003004 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3005 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003006 }
3007 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003008 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003009 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003010 } else {
3011 /* We only need to wait on the I/O if we're not also
3012 * truncating pages because truncate_inode_pages waits
3013 * for us above. We don't truncate pages if we're
3014 * blocking anything < EXMODE because we want to keep
3015 * them around in that case. */
3016 filemap_fdatawait(mapping);
3017 }
3018
Mark Fashehf1f54062007-10-18 15:13:59 -07003019out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003020 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003021}
3022
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003023static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3024 int new_level)
3025{
3026 struct inode *inode = ocfs2_lock_res_inode(lockres);
3027 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3028
Joel Beckerbd3e7612008-02-01 12:14:57 -08003029 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3030 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003031
3032 if (checkpointed)
3033 return 1;
3034
3035 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3036 return 0;
3037}
3038
3039static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3040{
3041 struct inode *inode = ocfs2_lock_res_inode(lockres);
3042
3043 __ocfs2_stuff_meta_lvb(inode);
3044}
3045
Mark Fashehd680efe2006-09-08 14:14:34 -07003046/*
3047 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003048 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003049 * dlmglue API and push these off to the ocfs2_wq in the future.
3050 */
3051static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3052 struct ocfs2_lock_res *lockres)
3053{
3054 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3055 ocfs2_dentry_lock_put(osb, dl);
3056}
3057
3058/*
3059 * d_delete() matching dentries before the lock downconvert.
3060 *
3061 * At this point, any process waiting to destroy the
3062 * dentry_lock due to last ref count is stopped by the
3063 * OCFS2_LOCK_QUEUED flag.
3064 *
3065 * We have two potential problems
3066 *
3067 * 1) If we do the last reference drop on our dentry_lock (via dput)
3068 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3069 * the downconvert to finish. Instead we take an elevated
3070 * reference and push the drop until after we've completed our
3071 * unblock processing.
3072 *
3073 * 2) There might be another process with a final reference,
3074 * waiting on us to finish processing. If this is the case, we
3075 * detect it and exit out - there's no more dentries anyway.
3076 */
3077static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3078 int blocking)
3079{
3080 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3081 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3082 struct dentry *dentry;
3083 unsigned long flags;
3084 int extra_ref = 0;
3085
3086 /*
3087 * This node is blocking another node from getting a read
3088 * lock. This happens when we've renamed within a
3089 * directory. We've forced the other nodes to d_delete(), but
3090 * we never actually dropped our lock because it's still
3091 * valid. The downconvert code will retain a PR for this node,
3092 * so there's no further work to do.
3093 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003094 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003095 return UNBLOCK_CONTINUE;
3096
3097 /*
3098 * Mark this inode as potentially orphaned. The code in
3099 * ocfs2_delete_inode() will figure out whether it actually
3100 * needs to be freed or not.
3101 */
3102 spin_lock(&oi->ip_lock);
3103 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3104 spin_unlock(&oi->ip_lock);
3105
3106 /*
3107 * Yuck. We need to make sure however that the check of
3108 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3109 * respect to a reference decrement or the setting of that
3110 * flag.
3111 */
3112 spin_lock_irqsave(&lockres->l_lock, flags);
3113 spin_lock(&dentry_attach_lock);
3114 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3115 && dl->dl_count) {
3116 dl->dl_count++;
3117 extra_ref = 1;
3118 }
3119 spin_unlock(&dentry_attach_lock);
3120 spin_unlock_irqrestore(&lockres->l_lock, flags);
3121
3122 mlog(0, "extra_ref = %d\n", extra_ref);
3123
3124 /*
3125 * We have a process waiting on us in ocfs2_dentry_iput(),
3126 * which means we can't have any more outstanding
3127 * aliases. There's no need to do any more work.
3128 */
3129 if (!extra_ref)
3130 return UNBLOCK_CONTINUE;
3131
3132 spin_lock(&dentry_attach_lock);
3133 while (1) {
3134 dentry = ocfs2_find_local_alias(dl->dl_inode,
3135 dl->dl_parent_blkno, 1);
3136 if (!dentry)
3137 break;
3138 spin_unlock(&dentry_attach_lock);
3139
3140 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3141 dentry->d_name.name);
3142
3143 /*
3144 * The following dcache calls may do an
3145 * iput(). Normally we don't want that from the
3146 * downconverting thread, but in this case it's ok
3147 * because the requesting node already has an
3148 * exclusive lock on the inode, so it can't be queued
3149 * for a downconvert.
3150 */
3151 d_delete(dentry);
3152 dput(dentry);
3153
3154 spin_lock(&dentry_attach_lock);
3155 }
3156 spin_unlock(&dentry_attach_lock);
3157
3158 /*
3159 * If we are the last holder of this dentry lock, there is no
3160 * reason to downconvert so skip straight to the unlock.
3161 */
3162 if (dl->dl_count == 1)
3163 return UNBLOCK_STOP_POST;
3164
3165 return UNBLOCK_CONTINUE_POST;
3166}
3167
Joel Becker4670c462008-02-01 14:39:35 -08003168/*
3169 * This is the filesystem locking protocol. It provides the lock handling
3170 * hooks for the underlying DLM. It has a maximum version number.
3171 * The version number allows interoperability with systems running at
3172 * the same major number and an equal or smaller minor number.
3173 *
3174 * Whenever the filesystem does new things with locks (adds or removes a
3175 * lock, orders them differently, does different things underneath a lock),
3176 * the version must be changed. The protocol is negotiated when joining
3177 * the dlm domain. A node may join the domain if its major version is
3178 * identical to all other nodes and its minor version is greater than
3179 * or equal to all other nodes. When its minor version is greater than
3180 * the other nodes, it will run at the minor version specified by the
3181 * other nodes.
3182 *
3183 * If a locking change is made that will not be compatible with older
3184 * versions, the major number must be increased and the minor version set
3185 * to zero. If a change merely adds a behavior that can be disabled when
3186 * speaking to older versions, the minor version must be increased. If a
3187 * change adds a fully backwards compatible change (eg, LVB changes that
3188 * are just ignored by older versions), the version does not need to be
3189 * updated.
3190 */
Joel Becker24ef1812008-01-29 17:37:32 -08003191static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003192 .lp_max_version = {
3193 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3194 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3195 },
Joel Becker24ef1812008-01-29 17:37:32 -08003196 .lp_lock_ast = ocfs2_locking_ast,
3197 .lp_blocking_ast = ocfs2_blocking_ast,
3198 .lp_unlock_ast = ocfs2_unlock_ast,
3199};
3200
3201/* This interface isn't the final one, hence the less-than-perfect names */
3202void dlmglue_init_stack(void)
3203{
3204 o2cb_get_stack(&lproto);
3205}
3206
3207void dlmglue_exit_stack(void)
3208{
3209 o2cb_put_stack();
3210}
3211
Adrian Bunk00600052008-01-29 00:11:41 +02003212static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3213 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003214{
3215 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003216 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003217 unsigned long flags;
3218
3219 /* Our reference to the lockres in this function can be
3220 * considered valid until we remove the OCFS2_LOCK_QUEUED
3221 * flag. */
3222
3223 mlog_entry_void();
3224
3225 BUG_ON(!lockres);
3226 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003227
3228 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3229
3230 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003231 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003232 * still be marked with OCFS2_LOCK_FREEING after this check,
3233 * but short circuiting here will still save us some
3234 * performance. */
3235 spin_lock_irqsave(&lockres->l_lock, flags);
3236 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3237 goto unqueue;
3238 spin_unlock_irqrestore(&lockres->l_lock, flags);
3239
Mark Fashehb5e500e2006-09-13 22:01:16 -07003240 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003241 if (status < 0)
3242 mlog_errno(status);
3243
3244 spin_lock_irqsave(&lockres->l_lock, flags);
3245unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003246 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003247 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3248 } else
3249 ocfs2_schedule_blocked_lock(osb, lockres);
3250
3251 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003252 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003253 spin_unlock_irqrestore(&lockres->l_lock, flags);
3254
Mark Fashehd680efe2006-09-08 14:14:34 -07003255 if (ctl.unblock_action != UNBLOCK_CONTINUE
3256 && lockres->l_ops->post_unlock)
3257 lockres->l_ops->post_unlock(osb, lockres);
3258
Mark Fashehccd979b2005-12-15 14:31:24 -08003259 mlog_exit_void();
3260}
3261
3262static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3263 struct ocfs2_lock_res *lockres)
3264{
3265 mlog_entry_void();
3266
3267 assert_spin_locked(&lockres->l_lock);
3268
3269 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3270 /* Do not schedule a lock for downconvert when it's on
3271 * the way to destruction - any nodes wanting access
3272 * to the resource will get it soon. */
3273 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3274 lockres->l_name, lockres->l_flags);
3275 return;
3276 }
3277
3278 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3279
Mark Fasheh34d024f2007-09-24 15:56:19 -07003280 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003281 if (list_empty(&lockres->l_blocked_list)) {
3282 list_add_tail(&lockres->l_blocked_list,
3283 &osb->blocked_lock_list);
3284 osb->blocked_lock_count++;
3285 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003286 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003287
3288 mlog_exit_void();
3289}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003290
3291static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3292{
3293 unsigned long processed;
3294 struct ocfs2_lock_res *lockres;
3295
3296 mlog_entry_void();
3297
3298 spin_lock(&osb->dc_task_lock);
3299 /* grab this early so we know to try again if a state change and
3300 * wake happens part-way through our work */
3301 osb->dc_work_sequence = osb->dc_wake_sequence;
3302
3303 processed = osb->blocked_lock_count;
3304 while (processed) {
3305 BUG_ON(list_empty(&osb->blocked_lock_list));
3306
3307 lockres = list_entry(osb->blocked_lock_list.next,
3308 struct ocfs2_lock_res, l_blocked_list);
3309 list_del_init(&lockres->l_blocked_list);
3310 osb->blocked_lock_count--;
3311 spin_unlock(&osb->dc_task_lock);
3312
3313 BUG_ON(!processed);
3314 processed--;
3315
3316 ocfs2_process_blocked_lock(osb, lockres);
3317
3318 spin_lock(&osb->dc_task_lock);
3319 }
3320 spin_unlock(&osb->dc_task_lock);
3321
3322 mlog_exit_void();
3323}
3324
3325static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3326{
3327 int empty = 0;
3328
3329 spin_lock(&osb->dc_task_lock);
3330 if (list_empty(&osb->blocked_lock_list))
3331 empty = 1;
3332
3333 spin_unlock(&osb->dc_task_lock);
3334 return empty;
3335}
3336
3337static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3338{
3339 int should_wake = 0;
3340
3341 spin_lock(&osb->dc_task_lock);
3342 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3343 should_wake = 1;
3344 spin_unlock(&osb->dc_task_lock);
3345
3346 return should_wake;
3347}
3348
Adrian Bunk200bfae2008-02-17 10:20:38 +02003349static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003350{
3351 int status = 0;
3352 struct ocfs2_super *osb = arg;
3353
3354 /* only quit once we've been asked to stop and there is no more
3355 * work available */
3356 while (!(kthread_should_stop() &&
3357 ocfs2_downconvert_thread_lists_empty(osb))) {
3358
3359 wait_event_interruptible(osb->dc_event,
3360 ocfs2_downconvert_thread_should_wake(osb) ||
3361 kthread_should_stop());
3362
3363 mlog(0, "downconvert_thread: awoken\n");
3364
3365 ocfs2_downconvert_thread_do_work(osb);
3366 }
3367
3368 osb->dc_task = NULL;
3369 return status;
3370}
3371
3372void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3373{
3374 spin_lock(&osb->dc_task_lock);
3375 /* make sure the voting thread gets a swipe at whatever changes
3376 * the caller may have made to the voting state */
3377 osb->dc_wake_sequence++;
3378 spin_unlock(&osb->dc_task_lock);
3379 wake_up(&osb->dc_event);
3380}