blob: e15fc7d50827019fe974b9bb61b8f5dd7312665a [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>
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070034#include <linux/time.h>
Jan Kara9e33d692008-08-25 19:56:50 +020035#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080036
Mark Fashehccd979b2005-12-15 14:31:24 -080037#define MLOG_MASK_PREFIX ML_DLM_GLUE
38#include <cluster/masklog.h>
39
40#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080041#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080042
43#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070044#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045#include "dlmglue.h"
46#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080047#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080048#include "heartbeat.h"
49#include "inode.h"
50#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080051#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#include "slot_map.h"
53#include "super.h"
54#include "uptodate.h"
Jan Kara9e33d692008-08-25 19:56:50 +020055#include "quota.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080056
57#include "buffer_head_io.h"
58
59struct ocfs2_mask_waiter {
60 struct list_head mw_item;
61 int mw_status;
62 struct completion mw_complete;
63 unsigned long mw_mask;
64 unsigned long mw_goal;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070065#ifdef CONFIG_OCFS2_FS_STATS
66 unsigned long long mw_lock_start;
67#endif
Mark Fashehccd979b2005-12-15 14:31:24 -080068};
69
Mark Fasheh54a7e752006-09-12 21:49:13 -070070static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080072static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Jan Kara9e33d692008-08-25 19:56:50 +020073static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080074
Mark Fashehd680efe2006-09-08 14:14:34 -070075/*
Mark Fashehcc567d82006-09-13 21:52:21 -070076 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070077 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070078 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070079 * and ocfs2_process_blocked_lock()
80 *
81 */
82enum ocfs2_unblock_action {
83 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
84 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
85 * ->post_unlock callback */
86 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
87 * ->post_unlock() callback. */
88};
89
90struct ocfs2_unblock_ctl {
91 int requeue;
92 enum ocfs2_unblock_action unblock_action;
93};
94
Mark Fasheh810d5ae2006-09-13 21:39:52 -070095static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
96 int new_level);
97static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
98
Mark Fashehcc567d82006-09-13 21:52:21 -070099static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
100 int blocking);
101
Mark Fashehcc567d82006-09-13 21:52:21 -0700102static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
103 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700104
105static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
106 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800107
Jan Kara9e33d692008-08-25 19:56:50 +0200108static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700109
110#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
111
112/* This aids in debugging situations where a bad LVB might be involved. */
113static void ocfs2_dump_meta_lvb_info(u64 level,
114 const char *function,
115 unsigned int line,
116 struct ocfs2_lock_res *lockres)
117{
Mark Fasheha641dc22008-12-24 16:03:48 -0800118 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700119
120 mlog(level, "LVB information for %s (called from %s:%u):\n",
121 lockres->l_name, function, line);
122 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
123 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
124 be32_to_cpu(lvb->lvb_igeneration));
125 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
126 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
127 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
128 be16_to_cpu(lvb->lvb_imode));
129 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
130 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
131 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
132 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
133 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
134 be32_to_cpu(lvb->lvb_iattr));
135}
136
137
Mark Fashehf625c972006-09-12 21:24:53 -0700138/*
139 * OCFS2 Lock Resource Operations
140 *
141 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700142 *
143 * The most basic of lock types can point ->l_priv to their respective
144 * struct ocfs2_super and allow the default actions to manage things.
145 *
146 * Right now, each lock type also needs to implement an init function,
147 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
148 * should be called when the lock is no longer needed (i.e., object
149 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700150 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800151struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700152 /*
153 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
154 * this callback if ->l_priv is not an ocfs2_super pointer
155 */
156 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700157
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700158 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700159 * Optionally called in the downconvert thread after a
160 * successful downconvert. The lockres will not be referenced
161 * after this callback is called, so it is safe to free
162 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700163 *
164 * The exact semantics of when this is called are controlled
165 * by ->downconvert_worker()
166 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700167 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700168
169 /*
Mark Fasheh16d5b9562006-09-13 21:10:12 -0700170 * Allow a lock type to add checks to determine whether it is
171 * safe to downconvert a lock. Return 0 to re-queue the
172 * downconvert at a later time, nonzero to continue.
173 *
174 * For most locks, the default checks that there are no
175 * incompatible holders are sufficient.
176 *
177 * Called with the lockres spinlock held.
178 */
179 int (*check_downconvert)(struct ocfs2_lock_res *, int);
180
181 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700182 * Allows a lock type to populate the lock value block. This
183 * is called on downconvert, and when we drop a lock.
184 *
185 * Locks that want to use this should set LOCK_TYPE_USES_LVB
186 * in the flags field.
187 *
188 * Called with the lockres spinlock held.
189 */
190 void (*set_lvb)(struct ocfs2_lock_res *);
191
192 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700193 * Called from the downconvert thread when it is determined
194 * that a lock will be downconverted. This is called without
195 * any locks held so the function can do work that might
196 * schedule (syncing out data, etc).
197 *
198 * This should return any one of the ocfs2_unblock_action
199 * values, depending on what it wants the thread to do.
200 */
201 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
202
203 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700204 * LOCK_TYPE_* flags which describe the specific requirements
205 * of a lock type. Descriptions of each individual flag follow.
206 */
207 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800208};
209
Mark Fashehf625c972006-09-12 21:24:53 -0700210/*
211 * Some locks want to "refresh" potentially stale data when a
212 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
213 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
214 * individual lockres l_flags member from the ast function. It is
215 * expected that the locking wrapper will clear the
216 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
217 */
218#define LOCK_TYPE_REQUIRES_REFRESH 0x1
219
Mark Fashehb80fc012006-09-12 22:08:14 -0700220/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700221 * Indicate that a lock type makes use of the lock value block. The
222 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700223 */
224#define LOCK_TYPE_USES_LVB 0x2
225
Mark Fashehccd979b2005-12-15 14:31:24 -0800226static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700227 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700228 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800229};
230
Mark Fashehe63aecb62007-10-18 15:30:42 -0700231static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700232 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700233 .check_downconvert = ocfs2_check_meta_downconvert,
234 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700235 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700236 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800237};
238
Mark Fashehccd979b2005-12-15 14:31:24 -0800239static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700240 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800241};
242
243static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700244 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800245};
246
wengang wang6ca497a2009-03-06 21:29:10 +0800247static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
248 .flags = 0,
249};
250
Mark Fashehd680efe2006-09-08 14:14:34 -0700251static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700252 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700253 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700254 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700255 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700256};
257
Tiger Yang50008632007-03-20 16:01:38 -0700258static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
259 .get_osb = ocfs2_get_inode_osb,
260 .flags = 0,
261};
262
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800263static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
264 .get_osb = ocfs2_get_file_osb,
265 .flags = 0,
266};
267
Jan Kara9e33d692008-08-25 19:56:50 +0200268static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
269 .set_lvb = ocfs2_set_qinfo_lvb,
270 .get_osb = ocfs2_get_qinfo_osb,
271 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
272};
273
Mark Fashehccd979b2005-12-15 14:31:24 -0800274static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
275{
276 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700277 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
278 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800279}
280
Mark Fashehccd979b2005-12-15 14:31:24 -0800281static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
282{
283 BUG_ON(!ocfs2_is_inode_lock(lockres));
284
285 return (struct inode *) lockres->l_priv;
286}
287
Mark Fashehd680efe2006-09-08 14:14:34 -0700288static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
289{
290 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
291
292 return (struct ocfs2_dentry_lock *)lockres->l_priv;
293}
294
Jan Kara9e33d692008-08-25 19:56:50 +0200295static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
296{
297 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
298
299 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
300}
301
Mark Fasheh54a7e752006-09-12 21:49:13 -0700302static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
303{
304 if (lockres->l_ops->get_osb)
305 return lockres->l_ops->get_osb(lockres);
306
307 return (struct ocfs2_super *)lockres->l_priv;
308}
309
Mark Fashehccd979b2005-12-15 14:31:24 -0800310static int ocfs2_lock_create(struct ocfs2_super *osb,
311 struct ocfs2_lock_res *lockres,
312 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800313 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800314static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
315 int wanted);
316static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
317 struct ocfs2_lock_res *lockres,
318 int level);
319static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
320static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
321static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
322static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
323static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
324 struct ocfs2_lock_res *lockres);
325static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
326 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800327#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
328 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
329 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
330 _err, _func, _lockres->l_name); \
331 else \
332 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
333 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
334 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800335} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700336static int ocfs2_downconvert_thread(void *arg);
337static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
338 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700339static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800340 struct buffer_head **bh);
341static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
342static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800343static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
344 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800345static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
346 struct ocfs2_lock_res *lockres,
347 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800348 int lvb,
349 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800350static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
351 struct ocfs2_lock_res *lockres);
352static int ocfs2_cancel_convert(struct ocfs2_super *osb,
353 struct ocfs2_lock_res *lockres);
354
Mark Fashehccd979b2005-12-15 14:31:24 -0800355
Mark Fashehccd979b2005-12-15 14:31:24 -0800356static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
357 u64 blkno,
358 u32 generation,
359 char *name)
360{
361 int len;
362
363 mlog_entry_void();
364
365 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
366
Mark Fashehb0697052006-03-03 10:24:33 -0800367 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
368 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
369 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800370
371 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
372
373 mlog(0, "built lock resource with name: %s\n", name);
374
375 mlog_exit_void();
376}
377
Ingo Molnar34af9462006-06-27 02:53:55 -0700378static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800379
380static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
381 struct ocfs2_dlm_debug *dlm_debug)
382{
383 mlog(0, "Add tracking for lockres %s\n", res->l_name);
384
385 spin_lock(&ocfs2_dlm_tracking_lock);
386 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
387 spin_unlock(&ocfs2_dlm_tracking_lock);
388}
389
390static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
391{
392 spin_lock(&ocfs2_dlm_tracking_lock);
393 if (!list_empty(&res->l_debug_list))
394 list_del_init(&res->l_debug_list);
395 spin_unlock(&ocfs2_dlm_tracking_lock);
396}
397
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700398#ifdef CONFIG_OCFS2_FS_STATS
399static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
400{
401 res->l_lock_num_prmode = 0;
402 res->l_lock_num_prmode_failed = 0;
403 res->l_lock_total_prmode = 0;
404 res->l_lock_max_prmode = 0;
405 res->l_lock_num_exmode = 0;
406 res->l_lock_num_exmode_failed = 0;
407 res->l_lock_total_exmode = 0;
408 res->l_lock_max_exmode = 0;
409 res->l_lock_refresh = 0;
410}
411
412static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
413 struct ocfs2_mask_waiter *mw, int ret)
414{
415 unsigned long long *num, *sum;
416 unsigned int *max, *failed;
417 struct timespec ts = current_kernel_time();
418 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
419
420 if (level == LKM_PRMODE) {
421 num = &res->l_lock_num_prmode;
422 sum = &res->l_lock_total_prmode;
423 max = &res->l_lock_max_prmode;
424 failed = &res->l_lock_num_prmode_failed;
425 } else if (level == LKM_EXMODE) {
426 num = &res->l_lock_num_exmode;
427 sum = &res->l_lock_total_exmode;
428 max = &res->l_lock_max_exmode;
429 failed = &res->l_lock_num_exmode_failed;
430 } else
431 return;
432
433 (*num)++;
434 (*sum) += time;
435 if (time > *max)
436 *max = time;
437 if (ret)
438 (*failed)++;
439}
440
441static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
442{
443 lockres->l_lock_refresh++;
444}
445
446static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
447{
448 struct timespec ts = current_kernel_time();
449 mw->mw_lock_start = timespec_to_ns(&ts);
450}
451#else
452static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
453{
454}
455static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
456 int level, struct ocfs2_mask_waiter *mw, int ret)
457{
458}
459static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
460{
461}
462static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
463{
464}
465#endif
466
Mark Fashehccd979b2005-12-15 14:31:24 -0800467static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
468 struct ocfs2_lock_res *res,
469 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800470 struct ocfs2_lock_res_ops *ops,
471 void *priv)
472{
Mark Fashehccd979b2005-12-15 14:31:24 -0800473 res->l_type = type;
474 res->l_ops = ops;
475 res->l_priv = priv;
476
Joel Beckerbd3e7612008-02-01 12:14:57 -0800477 res->l_level = DLM_LOCK_IV;
478 res->l_requested = DLM_LOCK_IV;
479 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800480 res->l_action = OCFS2_AST_INVALID;
481 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
482
483 res->l_flags = OCFS2_LOCK_INITIALIZED;
484
485 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700486
487 ocfs2_init_lock_stats(res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800488}
489
490void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
491{
492 /* This also clears out the lock status block */
493 memset(res, 0, sizeof(struct ocfs2_lock_res));
494 spin_lock_init(&res->l_lock);
495 init_waitqueue_head(&res->l_event);
496 INIT_LIST_HEAD(&res->l_blocked_list);
497 INIT_LIST_HEAD(&res->l_mask_waiters);
498}
499
500void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
501 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700502 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800503 struct inode *inode)
504{
505 struct ocfs2_lock_res_ops *ops;
506
507 switch(type) {
508 case OCFS2_LOCK_TYPE_RW:
509 ops = &ocfs2_inode_rw_lops;
510 break;
511 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700512 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 break;
Tiger Yang50008632007-03-20 16:01:38 -0700514 case OCFS2_LOCK_TYPE_OPEN:
515 ops = &ocfs2_inode_open_lops;
516 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 default:
518 mlog_bug_on_msg(1, "type: %d\n", type);
519 ops = NULL; /* thanks, gcc */
520 break;
521 };
522
Mark Fashehd680efe2006-09-08 14:14:34 -0700523 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700524 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700525 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
526}
527
Mark Fasheh54a7e752006-09-12 21:49:13 -0700528static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
529{
530 struct inode *inode = ocfs2_lock_res_inode(lockres);
531
532 return OCFS2_SB(inode->i_sb);
533}
534
Jan Kara9e33d692008-08-25 19:56:50 +0200535static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
536{
537 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
538
539 return OCFS2_SB(info->dqi_gi.dqi_sb);
540}
541
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800542static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
543{
544 struct ocfs2_file_private *fp = lockres->l_priv;
545
546 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
547}
548
Mark Fashehd680efe2006-09-08 14:14:34 -0700549static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
550{
551 __be64 inode_blkno_be;
552
553 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
554 sizeof(__be64));
555
556 return be64_to_cpu(inode_blkno_be);
557}
558
Mark Fasheh54a7e752006-09-12 21:49:13 -0700559static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
560{
561 struct ocfs2_dentry_lock *dl = lockres->l_priv;
562
563 return OCFS2_SB(dl->dl_inode->i_sb);
564}
565
Mark Fashehd680efe2006-09-08 14:14:34 -0700566void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
567 u64 parent, struct inode *inode)
568{
569 int len;
570 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
571 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
572 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
573
574 ocfs2_lock_res_init_once(lockres);
575
576 /*
577 * Unfortunately, the standard lock naming scheme won't work
578 * here because we have two 16 byte values to use. Instead,
579 * we'll stuff the inode number as a binary value. We still
580 * want error prints to show something without garbling the
581 * display, so drop a null byte in there before the inode
582 * number. A future version of OCFS2 will likely use all
583 * binary lock names. The stringified names have been a
584 * tremendous aid in debugging, but now that the debugfs
585 * interface exists, we can mangle things there if need be.
586 *
587 * NOTE: We also drop the standard "pad" value (the total lock
588 * name size stays the same though - the last part is all
589 * zeros due to the memset in ocfs2_lock_res_init_once()
590 */
591 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
592 "%c%016llx",
593 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
594 (long long)parent);
595
596 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
597
598 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
599 sizeof(__be64));
600
601 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
602 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
603 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800604}
605
606static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
607 struct ocfs2_super *osb)
608{
609 /* Superblock lockres doesn't come from a slab so we call init
610 * once on it manually. */
611 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700612 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
613 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800614 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800615 &ocfs2_super_lops, osb);
616}
617
618static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
619 struct ocfs2_super *osb)
620{
621 /* Rename lockres doesn't come from a slab so we call init
622 * once on it manually. */
623 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700624 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
625 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800626 &ocfs2_rename_lops, osb);
627}
628
wengang wang6ca497a2009-03-06 21:29:10 +0800629static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
630 struct ocfs2_super *osb)
631{
632 /* nfs_sync lockres doesn't come from a slab so we call init
633 * once on it manually. */
634 ocfs2_lock_res_init_once(res);
635 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
636 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
637 &ocfs2_nfs_sync_lops, osb);
638}
639
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800640void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
641 struct ocfs2_file_private *fp)
642{
643 struct inode *inode = fp->fp_file->f_mapping->host;
644 struct ocfs2_inode_info *oi = OCFS2_I(inode);
645
646 ocfs2_lock_res_init_once(lockres);
647 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
648 inode->i_generation, lockres->l_name);
649 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
650 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
651 fp);
652 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
653}
654
Jan Kara9e33d692008-08-25 19:56:50 +0200655void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
656 struct ocfs2_mem_dqinfo *info)
657{
658 ocfs2_lock_res_init_once(lockres);
659 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
660 0, lockres->l_name);
661 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
662 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
663 info);
664}
665
Mark Fashehccd979b2005-12-15 14:31:24 -0800666void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
667{
668 mlog_entry_void();
669
670 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
671 return;
672
673 ocfs2_remove_lockres_tracking(res);
674
675 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
676 "Lockres %s is on the blocked list\n",
677 res->l_name);
678 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
679 "Lockres %s has mask waiters pending\n",
680 res->l_name);
681 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
682 "Lockres %s is locked\n",
683 res->l_name);
684 mlog_bug_on_msg(res->l_ro_holders,
685 "Lockres %s has %u ro holders\n",
686 res->l_name, res->l_ro_holders);
687 mlog_bug_on_msg(res->l_ex_holders,
688 "Lockres %s has %u ex holders\n",
689 res->l_name, res->l_ex_holders);
690
691 /* Need to clear out the lock status block for the dlm */
692 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
693
694 res->l_flags = 0UL;
695 mlog_exit_void();
696}
697
698static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
699 int level)
700{
701 mlog_entry_void();
702
703 BUG_ON(!lockres);
704
705 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800706 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800707 lockres->l_ex_holders++;
708 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800709 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 lockres->l_ro_holders++;
711 break;
712 default:
713 BUG();
714 }
715
716 mlog_exit_void();
717}
718
719static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
720 int level)
721{
722 mlog_entry_void();
723
724 BUG_ON(!lockres);
725
726 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800727 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 BUG_ON(!lockres->l_ex_holders);
729 lockres->l_ex_holders--;
730 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800731 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800732 BUG_ON(!lockres->l_ro_holders);
733 lockres->l_ro_holders--;
734 break;
735 default:
736 BUG();
737 }
738 mlog_exit_void();
739}
740
741/* WARNING: This function lives in a world where the only three lock
742 * levels are EX, PR, and NL. It *will* have to be adjusted when more
743 * lock types are added. */
744static inline int ocfs2_highest_compat_lock_level(int level)
745{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800746 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800747
Joel Beckerbd3e7612008-02-01 12:14:57 -0800748 if (level == DLM_LOCK_EX)
749 new_level = DLM_LOCK_NL;
750 else if (level == DLM_LOCK_PR)
751 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 return new_level;
753}
754
755static void lockres_set_flags(struct ocfs2_lock_res *lockres,
756 unsigned long newflags)
757{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200758 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800759
760 assert_spin_locked(&lockres->l_lock);
761
762 lockres->l_flags = newflags;
763
Christoph Hellwig800deef2007-05-17 16:03:13 +0200764 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800765 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
766 continue;
767
768 list_del_init(&mw->mw_item);
769 mw->mw_status = 0;
770 complete(&mw->mw_complete);
771 }
772}
773static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
774{
775 lockres_set_flags(lockres, lockres->l_flags | or);
776}
777static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
778 unsigned long clear)
779{
780 lockres_set_flags(lockres, lockres->l_flags & ~clear);
781}
782
783static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
784{
785 mlog_entry_void();
786
787 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
788 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
789 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800790 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800791
792 lockres->l_level = lockres->l_requested;
793 if (lockres->l_level <=
794 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800795 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800796 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
797 }
798 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
799
800 mlog_exit_void();
801}
802
803static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
804{
805 mlog_entry_void();
806
807 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
808 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
809
810 /* Convert from RO to EX doesn't really need anything as our
811 * information is already up to data. Convert from NL to
812 * *anything* however should mark ourselves as needing an
813 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800814 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700815 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
817
818 lockres->l_level = lockres->l_requested;
819 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
820
821 mlog_exit_void();
822}
823
824static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
825{
826 mlog_entry_void();
827
Roel Kluin3cf0c502007-10-27 00:20:36 +0200828 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
830
Joel Beckerbd3e7612008-02-01 12:14:57 -0800831 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700832 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
833 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800834 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
835
836 lockres->l_level = lockres->l_requested;
837 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
838 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
839
840 mlog_exit_void();
841}
842
Mark Fashehccd979b2005-12-15 14:31:24 -0800843static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
844 int level)
845{
846 int needs_downconvert = 0;
847 mlog_entry_void();
848
849 assert_spin_locked(&lockres->l_lock);
850
851 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
852
853 if (level > lockres->l_blocking) {
854 /* only schedule a downconvert if we haven't already scheduled
855 * one that goes low enough to satisfy the level we're
856 * blocking. this also catches the case where we get
857 * duplicate BASTs */
858 if (ocfs2_highest_compat_lock_level(level) <
859 ocfs2_highest_compat_lock_level(lockres->l_blocking))
860 needs_downconvert = 1;
861
862 lockres->l_blocking = level;
863 }
864
865 mlog_exit(needs_downconvert);
866 return needs_downconvert;
867}
868
Joel Beckerde551242008-02-01 14:45:08 -0800869/*
870 * OCFS2_LOCK_PENDING and l_pending_gen.
871 *
872 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
873 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
874 * for more details on the race.
875 *
876 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
877 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
878 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
879 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
880 * the caller is going to try to clear PENDING again. If nothing else is
881 * happening, __lockres_clear_pending() sees PENDING is unset and does
882 * nothing.
883 *
884 * But what if another path (eg downconvert thread) has just started a
885 * new locking action? The other path has re-set PENDING. Our path
886 * cannot clear PENDING, because that will re-open the original race
887 * window.
888 *
889 * [Example]
890 *
891 * ocfs2_meta_lock()
892 * ocfs2_cluster_lock()
893 * set BUSY
894 * set PENDING
895 * drop l_lock
896 * ocfs2_dlm_lock()
897 * ocfs2_locking_ast() ocfs2_downconvert_thread()
898 * clear PENDING ocfs2_unblock_lock()
899 * take_l_lock
900 * !BUSY
901 * ocfs2_prepare_downconvert()
902 * set BUSY
903 * set PENDING
904 * drop l_lock
905 * take l_lock
906 * clear PENDING
907 * drop l_lock
908 * <window>
909 * ocfs2_dlm_lock()
910 *
911 * So as you can see, we now have a window where l_lock is not held,
912 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
913 *
914 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
915 * set by ocfs2_prepare_downconvert(). That wasn't nice.
916 *
917 * To solve this we introduce l_pending_gen. A call to
918 * lockres_clear_pending() will only do so when it is passed a generation
919 * number that matches the lockres. lockres_set_pending() will return the
920 * current generation number. When ocfs2_cluster_lock() goes to clear
921 * PENDING, it passes the generation it got from set_pending(). In our
922 * example above, the generation numbers will *not* match. Thus,
923 * ocfs2_cluster_lock() will not clear the PENDING set by
924 * ocfs2_prepare_downconvert().
925 */
926
927/* Unlocked version for ocfs2_locking_ast() */
928static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
929 unsigned int generation,
930 struct ocfs2_super *osb)
931{
932 assert_spin_locked(&lockres->l_lock);
933
934 /*
935 * The ast and locking functions can race us here. The winner
936 * will clear pending, the loser will not.
937 */
938 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
939 (lockres->l_pending_gen != generation))
940 return;
941
942 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
943 lockres->l_pending_gen++;
944
945 /*
946 * The downconvert thread may have skipped us because we
947 * were PENDING. Wake it up.
948 */
949 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
950 ocfs2_wake_downconvert_thread(osb);
951}
952
953/* Locked version for callers of ocfs2_dlm_lock() */
954static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
955 unsigned int generation,
956 struct ocfs2_super *osb)
957{
958 unsigned long flags;
959
960 spin_lock_irqsave(&lockres->l_lock, flags);
961 __lockres_clear_pending(lockres, generation, osb);
962 spin_unlock_irqrestore(&lockres->l_lock, flags);
963}
964
965static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
966{
967 assert_spin_locked(&lockres->l_lock);
968 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
969
970 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
971
972 return lockres->l_pending_gen;
973}
974
975
Mark Fashehaa2623a2006-09-12 21:58:23 -0700976static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800977{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700978 struct ocfs2_lock_res *lockres = opaque;
979 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 int needs_downconvert;
981 unsigned long flags;
982
Joel Beckerbd3e7612008-02-01 12:14:57 -0800983 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800984
Mark Fashehaa2623a2006-09-12 21:58:23 -0700985 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
986 lockres->l_name, level, lockres->l_level,
987 ocfs2_lock_type_string(lockres->l_type));
988
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800989 /*
990 * We can skip the bast for locks which don't enable caching -
991 * they'll be dropped at the earliest possible time anyway.
992 */
993 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
994 return;
995
Mark Fashehccd979b2005-12-15 14:31:24 -0800996 spin_lock_irqsave(&lockres->l_lock, flags);
997 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
998 if (needs_downconvert)
999 ocfs2_schedule_blocked_lock(osb, lockres);
1000 spin_unlock_irqrestore(&lockres->l_lock, flags);
1001
Mark Fashehd680efe2006-09-08 14:14:34 -07001002 wake_up(&lockres->l_event);
1003
Mark Fasheh34d024f2007-09-24 15:56:19 -07001004 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001005}
1006
Mark Fashehe92d57d2006-09-12 21:34:35 -07001007static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -08001008{
Mark Fashehe92d57d2006-09-12 21:34:35 -07001009 struct ocfs2_lock_res *lockres = opaque;
Joel Beckerde551242008-02-01 14:45:08 -08001010 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001012 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001013
1014 spin_lock_irqsave(&lockres->l_lock, flags);
1015
David Teigland1693a5c2008-01-30 16:52:53 -08001016 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1017
1018 if (status == -EAGAIN) {
1019 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1020 goto out;
1021 }
1022
1023 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001024 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001025 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 spin_unlock_irqrestore(&lockres->l_lock, flags);
1027 return;
1028 }
1029
1030 switch(lockres->l_action) {
1031 case OCFS2_AST_ATTACH:
1032 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001033 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001034 break;
1035 case OCFS2_AST_CONVERT:
1036 ocfs2_generic_handle_convert_action(lockres);
1037 break;
1038 case OCFS2_AST_DOWNCONVERT:
1039 ocfs2_generic_handle_downconvert_action(lockres);
1040 break;
1041 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001042 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1043 "lockres flags = 0x%lx, unlock action: %u\n",
1044 lockres->l_name, lockres->l_action, lockres->l_flags,
1045 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 BUG();
1047 }
David Teigland1693a5c2008-01-30 16:52:53 -08001048out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001049 /* set it to something invalid so if we get called again we
1050 * can catch it. */
1051 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001052
Joel Beckerde551242008-02-01 14:45:08 -08001053 /* Did we try to cancel this lock? Clear that state */
1054 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1055 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1056
1057 /*
1058 * We may have beaten the locking functions here. We certainly
1059 * know that dlm_lock() has been called :-)
1060 * Because we can't have two lock calls in flight at once, we
1061 * can use lockres->l_pending_gen.
1062 */
1063 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1064
Mark Fashehccd979b2005-12-15 14:31:24 -08001065 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001066 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001067}
1068
Mark Fashehccd979b2005-12-15 14:31:24 -08001069static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1070 int convert)
1071{
1072 unsigned long flags;
1073
1074 mlog_entry_void();
1075 spin_lock_irqsave(&lockres->l_lock, flags);
1076 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1077 if (convert)
1078 lockres->l_action = OCFS2_AST_INVALID;
1079 else
1080 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1081 spin_unlock_irqrestore(&lockres->l_lock, flags);
1082
1083 wake_up(&lockres->l_event);
1084 mlog_exit_void();
1085}
1086
1087/* Note: If we detect another process working on the lock (i.e.,
1088 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1089 * to do the right thing in that case.
1090 */
1091static int ocfs2_lock_create(struct ocfs2_super *osb,
1092 struct ocfs2_lock_res *lockres,
1093 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001094 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001095{
1096 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001097 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001098 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001099
1100 mlog_entry_void();
1101
Joel Beckerbd3e7612008-02-01 12:14:57 -08001102 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 dlm_flags);
1104
1105 spin_lock_irqsave(&lockres->l_lock, flags);
1106 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1107 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1108 spin_unlock_irqrestore(&lockres->l_lock, flags);
1109 goto bail;
1110 }
1111
1112 lockres->l_action = OCFS2_AST_ATTACH;
1113 lockres->l_requested = level;
1114 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001115 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 spin_unlock_irqrestore(&lockres->l_lock, flags);
1117
Joel Becker4670c462008-02-01 14:39:35 -08001118 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001119 level,
1120 &lockres->l_lksb,
1121 dlm_flags,
1122 lockres->l_name,
1123 OCFS2_LOCK_ID_MAX_LEN - 1,
1124 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001125 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001126 if (ret) {
1127 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001128 ocfs2_recover_from_dlm_error(lockres, 1);
1129 }
1130
Joel Becker7431cd72008-02-01 12:15:37 -08001131 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132
1133bail:
1134 mlog_exit(ret);
1135 return ret;
1136}
1137
1138static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1139 int flag)
1140{
1141 unsigned long flags;
1142 int ret;
1143
1144 spin_lock_irqsave(&lockres->l_lock, flags);
1145 ret = lockres->l_flags & flag;
1146 spin_unlock_irqrestore(&lockres->l_lock, flags);
1147
1148 return ret;
1149}
1150
1151static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1152
1153{
1154 wait_event(lockres->l_event,
1155 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1156}
1157
1158static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1159
1160{
1161 wait_event(lockres->l_event,
1162 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1163}
1164
1165/* predict what lock level we'll be dropping down to on behalf
1166 * of another node, and return true if the currently wanted
1167 * level will be compatible with it. */
1168static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1169 int wanted)
1170{
1171 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1172
1173 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1174}
1175
1176static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1177{
1178 INIT_LIST_HEAD(&mw->mw_item);
1179 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001180 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001181}
1182
1183static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1184{
1185 wait_for_completion(&mw->mw_complete);
1186 /* Re-arm the completion in case we want to wait on it again */
1187 INIT_COMPLETION(mw->mw_complete);
1188 return mw->mw_status;
1189}
1190
1191static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1192 struct ocfs2_mask_waiter *mw,
1193 unsigned long mask,
1194 unsigned long goal)
1195{
1196 BUG_ON(!list_empty(&mw->mw_item));
1197
1198 assert_spin_locked(&lockres->l_lock);
1199
1200 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1201 mw->mw_mask = mask;
1202 mw->mw_goal = goal;
1203}
1204
1205/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1206 * if the mask still hadn't reached its goal */
1207static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1208 struct ocfs2_mask_waiter *mw)
1209{
1210 unsigned long flags;
1211 int ret = 0;
1212
1213 spin_lock_irqsave(&lockres->l_lock, flags);
1214 if (!list_empty(&mw->mw_item)) {
1215 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1216 ret = -EBUSY;
1217
1218 list_del_init(&mw->mw_item);
1219 init_completion(&mw->mw_complete);
1220 }
1221 spin_unlock_irqrestore(&lockres->l_lock, flags);
1222
1223 return ret;
1224
1225}
1226
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001227static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1228 struct ocfs2_lock_res *lockres)
1229{
1230 int ret;
1231
1232 ret = wait_for_completion_interruptible(&mw->mw_complete);
1233 if (ret)
1234 lockres_remove_mask_waiter(lockres, mw);
1235 else
1236 ret = mw->mw_status;
1237 /* Re-arm the completion in case we want to wait on it again */
1238 INIT_COMPLETION(mw->mw_complete);
1239 return ret;
1240}
1241
Mark Fashehccd979b2005-12-15 14:31:24 -08001242static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1243 struct ocfs2_lock_res *lockres,
1244 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001245 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 int arg_flags)
1247{
1248 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1250 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1251 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001252 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001253 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001254
1255 mlog_entry_void();
1256
1257 ocfs2_init_mask_waiter(&mw);
1258
Mark Fashehb80fc012006-09-12 22:08:14 -07001259 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001260 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001261
Mark Fashehccd979b2005-12-15 14:31:24 -08001262again:
1263 wait = 0;
1264
1265 if (catch_signals && signal_pending(current)) {
1266 ret = -ERESTARTSYS;
1267 goto out;
1268 }
1269
1270 spin_lock_irqsave(&lockres->l_lock, flags);
1271
1272 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1273 "Cluster lock called on freeing lockres %s! flags "
1274 "0x%lx\n", lockres->l_name, lockres->l_flags);
1275
1276 /* We only compare against the currently granted level
1277 * here. If the lock is blocked waiting on a downconvert,
1278 * we'll get caught below. */
1279 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1280 level > lockres->l_level) {
1281 /* is someone sitting in dlm_lock? If so, wait on
1282 * them. */
1283 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1284 wait = 1;
1285 goto unlock;
1286 }
1287
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1289 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1290 /* is the lock is currently blocked on behalf of
1291 * another node */
1292 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1293 wait = 1;
1294 goto unlock;
1295 }
1296
1297 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001298 if (noqueue_attempted > 0) {
1299 ret = -EAGAIN;
1300 goto unlock;
1301 }
1302 if (lkm_flags & DLM_LKF_NOQUEUE)
1303 noqueue_attempted = 1;
1304
Mark Fashehccd979b2005-12-15 14:31:24 -08001305 if (lockres->l_action != OCFS2_AST_INVALID)
1306 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1307 lockres->l_name, lockres->l_action);
1308
Mark Fasheh019d1b22007-10-05 12:09:05 -07001309 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1310 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001311 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001312 } else {
1313 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001314 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001315 }
1316
Mark Fashehccd979b2005-12-15 14:31:24 -08001317 lockres->l_requested = level;
1318 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001319 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001320 spin_unlock_irqrestore(&lockres->l_lock, flags);
1321
Joel Beckerbd3e7612008-02-01 12:14:57 -08001322 BUG_ON(level == DLM_LOCK_IV);
1323 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001324
1325 mlog(0, "lock %s, convert from %d to level = %d\n",
1326 lockres->l_name, lockres->l_level, level);
1327
1328 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001329 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001330 level,
1331 &lockres->l_lksb,
1332 lkm_flags,
1333 lockres->l_name,
1334 OCFS2_LOCK_ID_MAX_LEN - 1,
1335 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001336 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001337 if (ret) {
1338 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1339 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001340 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001341 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 }
1343 ocfs2_recover_from_dlm_error(lockres, 1);
1344 goto out;
1345 }
1346
Coly Li73ac36e2009-01-07 18:09:16 -08001347 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 lockres->l_name);
1349
1350 /* At this point we've gone inside the dlm and need to
1351 * complete our work regardless. */
1352 catch_signals = 0;
1353
1354 /* wait for busy to clear and carry on */
1355 goto again;
1356 }
1357
1358 /* Ok, if we get here then we're good to go. */
1359 ocfs2_inc_holders(lockres, level);
1360
1361 ret = 0;
1362unlock:
1363 spin_unlock_irqrestore(&lockres->l_lock, flags);
1364out:
1365 /*
1366 * This is helping work around a lock inversion between the page lock
1367 * and dlm locks. One path holds the page lock while calling aops
1368 * which block acquiring dlm locks. The voting thread holds dlm
1369 * locks while acquiring page locks while down converting data locks.
1370 * This block is helping an aop path notice the inversion and back
1371 * off to unlock its page lock before trying the dlm lock again.
1372 */
1373 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1374 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1375 wait = 0;
1376 if (lockres_remove_mask_waiter(lockres, &mw))
1377 ret = -EAGAIN;
1378 else
1379 goto again;
1380 }
1381 if (wait) {
1382 ret = ocfs2_wait_for_mask(&mw);
1383 if (ret == 0)
1384 goto again;
1385 mlog_errno(ret);
1386 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001387 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001388
1389 mlog_exit(ret);
1390 return ret;
1391}
1392
1393static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1394 struct ocfs2_lock_res *lockres,
1395 int level)
1396{
1397 unsigned long flags;
1398
1399 mlog_entry_void();
1400 spin_lock_irqsave(&lockres->l_lock, flags);
1401 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001402 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 spin_unlock_irqrestore(&lockres->l_lock, flags);
1404 mlog_exit_void();
1405}
1406
Adrian Bunkda661162006-11-20 03:24:28 +01001407static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1408 struct ocfs2_lock_res *lockres,
1409 int ex,
1410 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001411{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001412 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001413 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001414 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001415
1416 spin_lock_irqsave(&lockres->l_lock, flags);
1417 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1418 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1419 spin_unlock_irqrestore(&lockres->l_lock, flags);
1420
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001421 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001422}
1423
1424/* Grants us an EX lock on the data and metadata resources, skipping
1425 * the normal cluster directory lookup. Use this ONLY on newly created
1426 * inodes which other nodes can't possibly see, and which haven't been
1427 * hashed in the inode hash yet. This can give us a good performance
1428 * increase as it'll skip the network broadcast normally associated
1429 * with creating a new lock resource. */
1430int ocfs2_create_new_inode_locks(struct inode *inode)
1431{
1432 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001433 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001434
1435 BUG_ON(!inode);
1436 BUG_ON(!ocfs2_inode_is_new(inode));
1437
1438 mlog_entry_void();
1439
Mark Fashehb0697052006-03-03 10:24:33 -08001440 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001441
1442 /* NOTE: That we don't increment any of the holder counts, nor
1443 * do we add anything to a journal handle. Since this is
1444 * supposed to be a new inode which the cluster doesn't know
1445 * about yet, there is no need to. As far as the LVB handling
1446 * is concerned, this is basically like acquiring an EX lock
1447 * on a resource which has an invalid one -- we'll set it
1448 * valid when we release the EX. */
1449
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001450 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001451 if (ret) {
1452 mlog_errno(ret);
1453 goto bail;
1454 }
1455
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001456 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001457 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001458 * don't use a generation in their lock names.
1459 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001460 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001461 if (ret) {
1462 mlog_errno(ret);
1463 goto bail;
1464 }
1465
Tiger Yang50008632007-03-20 16:01:38 -07001466 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1467 if (ret) {
1468 mlog_errno(ret);
1469 goto bail;
1470 }
1471
Mark Fashehccd979b2005-12-15 14:31:24 -08001472bail:
1473 mlog_exit(ret);
1474 return ret;
1475}
1476
1477int ocfs2_rw_lock(struct inode *inode, int write)
1478{
1479 int status, level;
1480 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001481 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001482
1483 BUG_ON(!inode);
1484
1485 mlog_entry_void();
1486
Mark Fashehb0697052006-03-03 10:24:33 -08001487 mlog(0, "inode %llu take %s RW lock\n",
1488 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001489 write ? "EXMODE" : "PRMODE");
1490
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001491 if (ocfs2_mount_local(osb))
1492 return 0;
1493
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1495
Joel Beckerbd3e7612008-02-01 12:14:57 -08001496 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001497
1498 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1499 0);
1500 if (status < 0)
1501 mlog_errno(status);
1502
1503 mlog_exit(status);
1504 return status;
1505}
1506
1507void ocfs2_rw_unlock(struct inode *inode, int write)
1508{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001509 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001510 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001511 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001512
1513 mlog_entry_void();
1514
Mark Fashehb0697052006-03-03 10:24:33 -08001515 mlog(0, "inode %llu drop %s RW lock\n",
1516 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001517 write ? "EXMODE" : "PRMODE");
1518
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001519 if (!ocfs2_mount_local(osb))
1520 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001521
1522 mlog_exit_void();
1523}
1524
Tiger Yang50008632007-03-20 16:01:38 -07001525/*
1526 * ocfs2_open_lock always get PR mode lock.
1527 */
1528int ocfs2_open_lock(struct inode *inode)
1529{
1530 int status = 0;
1531 struct ocfs2_lock_res *lockres;
1532 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1533
1534 BUG_ON(!inode);
1535
1536 mlog_entry_void();
1537
1538 mlog(0, "inode %llu take PRMODE open lock\n",
1539 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1540
1541 if (ocfs2_mount_local(osb))
1542 goto out;
1543
1544 lockres = &OCFS2_I(inode)->ip_open_lockres;
1545
1546 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001547 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001548 if (status < 0)
1549 mlog_errno(status);
1550
1551out:
1552 mlog_exit(status);
1553 return status;
1554}
1555
1556int ocfs2_try_open_lock(struct inode *inode, int write)
1557{
1558 int status = 0, level;
1559 struct ocfs2_lock_res *lockres;
1560 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1561
1562 BUG_ON(!inode);
1563
1564 mlog_entry_void();
1565
1566 mlog(0, "inode %llu try to take %s open lock\n",
1567 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1568 write ? "EXMODE" : "PRMODE");
1569
1570 if (ocfs2_mount_local(osb))
1571 goto out;
1572
1573 lockres = &OCFS2_I(inode)->ip_open_lockres;
1574
Joel Beckerbd3e7612008-02-01 12:14:57 -08001575 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001576
1577 /*
1578 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001579 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001580 * other nodes and the -EAGAIN will indicate to the caller that
1581 * this inode is still in use.
1582 */
1583 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001584 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001585
1586out:
1587 mlog_exit(status);
1588 return status;
1589}
1590
1591/*
1592 * ocfs2_open_unlock unlock PR and EX mode open locks.
1593 */
1594void ocfs2_open_unlock(struct inode *inode)
1595{
1596 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1597 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1598
1599 mlog_entry_void();
1600
1601 mlog(0, "inode %llu drop open lock\n",
1602 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1603
1604 if (ocfs2_mount_local(osb))
1605 goto out;
1606
1607 if(lockres->l_ro_holders)
1608 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001609 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001610 if(lockres->l_ex_holders)
1611 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001612 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001613
1614out:
1615 mlog_exit_void();
1616}
1617
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001618static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1619 int level)
1620{
1621 int ret;
1622 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1623 unsigned long flags;
1624 struct ocfs2_mask_waiter mw;
1625
1626 ocfs2_init_mask_waiter(&mw);
1627
1628retry_cancel:
1629 spin_lock_irqsave(&lockres->l_lock, flags);
1630 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1631 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1632 if (ret) {
1633 spin_unlock_irqrestore(&lockres->l_lock, flags);
1634 ret = ocfs2_cancel_convert(osb, lockres);
1635 if (ret < 0) {
1636 mlog_errno(ret);
1637 goto out;
1638 }
1639 goto retry_cancel;
1640 }
1641 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1642 spin_unlock_irqrestore(&lockres->l_lock, flags);
1643
1644 ocfs2_wait_for_mask(&mw);
1645 goto retry_cancel;
1646 }
1647
1648 ret = -ERESTARTSYS;
1649 /*
1650 * We may still have gotten the lock, in which case there's no
1651 * point to restarting the syscall.
1652 */
1653 if (lockres->l_level == level)
1654 ret = 0;
1655
1656 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1657 lockres->l_flags, lockres->l_level, lockres->l_action);
1658
1659 spin_unlock_irqrestore(&lockres->l_lock, flags);
1660
1661out:
1662 return ret;
1663}
1664
1665/*
1666 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1667 * flock() calls. The locking approach this requires is sufficiently
1668 * different from all other cluster lock types that we implement a
1669 * seperate path to the "low-level" dlm calls. In particular:
1670 *
1671 * - No optimization of lock levels is done - we take at exactly
1672 * what's been requested.
1673 *
1674 * - No lock caching is employed. We immediately downconvert to
1675 * no-lock at unlock time. This also means flock locks never go on
1676 * the blocking list).
1677 *
1678 * - Since userspace can trivially deadlock itself with flock, we make
1679 * sure to allow cancellation of a misbehaving applications flock()
1680 * request.
1681 *
1682 * - Access to any flock lockres doesn't require concurrency, so we
1683 * can simplify the code by requiring the caller to guarantee
1684 * serialization of dlmglue flock calls.
1685 */
1686int ocfs2_file_lock(struct file *file, int ex, int trylock)
1687{
Mark Fashehe988cf12008-07-10 09:25:39 -07001688 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1689 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001690 unsigned long flags;
1691 struct ocfs2_file_private *fp = file->private_data;
1692 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1693 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1694 struct ocfs2_mask_waiter mw;
1695
1696 ocfs2_init_mask_waiter(&mw);
1697
1698 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001699 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001700 mlog(ML_ERROR,
1701 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1702 "level: %u\n", lockres->l_name, lockres->l_flags,
1703 lockres->l_level);
1704 return -EINVAL;
1705 }
1706
1707 spin_lock_irqsave(&lockres->l_lock, flags);
1708 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1709 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1710 spin_unlock_irqrestore(&lockres->l_lock, flags);
1711
1712 /*
1713 * Get the lock at NLMODE to start - that way we
1714 * can cancel the upconvert request if need be.
1715 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001716 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001717 if (ret < 0) {
1718 mlog_errno(ret);
1719 goto out;
1720 }
1721
1722 ret = ocfs2_wait_for_mask(&mw);
1723 if (ret) {
1724 mlog_errno(ret);
1725 goto out;
1726 }
1727 spin_lock_irqsave(&lockres->l_lock, flags);
1728 }
1729
1730 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001731 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001732 lockres->l_requested = level;
1733 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1734
1735 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1736 spin_unlock_irqrestore(&lockres->l_lock, flags);
1737
Joel Becker4670c462008-02-01 14:39:35 -08001738 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001739 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1740 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001741 if (ret) {
1742 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001743 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001744 ret = -EINVAL;
1745 }
1746
1747 ocfs2_recover_from_dlm_error(lockres, 1);
1748 lockres_remove_mask_waiter(lockres, &mw);
1749 goto out;
1750 }
1751
1752 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1753 if (ret == -ERESTARTSYS) {
1754 /*
1755 * Userspace can cause deadlock itself with
1756 * flock(). Current behavior locally is to allow the
1757 * deadlock, but abort the system call if a signal is
1758 * received. We follow this example, otherwise a
1759 * poorly written program could sit in kernel until
1760 * reboot.
1761 *
1762 * Handling this is a bit more complicated for Ocfs2
1763 * though. We can't exit this function with an
1764 * outstanding lock request, so a cancel convert is
1765 * required. We intentionally overwrite 'ret' - if the
1766 * cancel fails and the lock was granted, it's easier
1767 * to just bubble sucess back up to the user.
1768 */
1769 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001770 } else if (!ret && (level > lockres->l_level)) {
1771 /* Trylock failed asynchronously */
1772 BUG_ON(!trylock);
1773 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001774 }
1775
1776out:
1777
1778 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1779 lockres->l_name, ex, trylock, ret);
1780 return ret;
1781}
1782
1783void ocfs2_file_unlock(struct file *file)
1784{
1785 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001786 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001787 unsigned long flags;
1788 struct ocfs2_file_private *fp = file->private_data;
1789 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1790 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1791 struct ocfs2_mask_waiter mw;
1792
1793 ocfs2_init_mask_waiter(&mw);
1794
1795 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1796 return;
1797
Mark Fashehe988cf12008-07-10 09:25:39 -07001798 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001799 return;
1800
1801 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1802 lockres->l_name, lockres->l_flags, lockres->l_level,
1803 lockres->l_action);
1804
1805 spin_lock_irqsave(&lockres->l_lock, flags);
1806 /*
1807 * Fake a blocking ast for the downconvert code.
1808 */
1809 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001810 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001811
Mark Fashehe988cf12008-07-10 09:25:39 -07001812 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001813 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1814 spin_unlock_irqrestore(&lockres->l_lock, flags);
1815
Mark Fashehe988cf12008-07-10 09:25:39 -07001816 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001817 if (ret) {
1818 mlog_errno(ret);
1819 return;
1820 }
1821
1822 ret = ocfs2_wait_for_mask(&mw);
1823 if (ret)
1824 mlog_errno(ret);
1825}
1826
Mark Fasheh34d024f2007-09-24 15:56:19 -07001827static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1828 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001829{
1830 int kick = 0;
1831
1832 mlog_entry_void();
1833
1834 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001835 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001836 * condition. */
1837 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1838 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001839 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001840 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1841 kick = 1;
1842 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001843 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001844 if (!lockres->l_ex_holders)
1845 kick = 1;
1846 break;
1847 default:
1848 BUG();
1849 }
1850 }
1851
1852 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001853 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001854
1855 mlog_exit_void();
1856}
1857
Mark Fashehccd979b2005-12-15 14:31:24 -08001858#define OCFS2_SEC_BITS 34
1859#define OCFS2_SEC_SHIFT (64 - 34)
1860#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1861
1862/* LVB only has room for 64 bits of time here so we pack it for
1863 * now. */
1864static u64 ocfs2_pack_timespec(struct timespec *spec)
1865{
1866 u64 res;
1867 u64 sec = spec->tv_sec;
1868 u32 nsec = spec->tv_nsec;
1869
1870 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1871
1872 return res;
1873}
1874
1875/* Call this with the lockres locked. I am reasonably sure we don't
1876 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001877 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001878static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1879{
1880 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001881 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001882 struct ocfs2_meta_lvb *lvb;
1883
1884 mlog_entry_void();
1885
Mark Fasheha641dc22008-12-24 16:03:48 -08001886 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001887
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001888 /*
1889 * Invalidate the LVB of a deleted inode - this way other
1890 * nodes are forced to go to disk and discover the new inode
1891 * status.
1892 */
1893 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1894 lvb->lvb_version = 0;
1895 goto out;
1896 }
1897
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001898 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001899 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1900 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1901 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1902 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1903 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1904 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1905 lvb->lvb_iatime_packed =
1906 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1907 lvb->lvb_ictime_packed =
1908 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1909 lvb->lvb_imtime_packed =
1910 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001911 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001912 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001913 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001914
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001915out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001916 mlog_meta_lvb(0, lockres);
1917
1918 mlog_exit_void();
1919}
1920
1921static void ocfs2_unpack_timespec(struct timespec *spec,
1922 u64 packed_time)
1923{
1924 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1925 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1926}
1927
1928static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1929{
1930 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001931 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 struct ocfs2_meta_lvb *lvb;
1933
1934 mlog_entry_void();
1935
1936 mlog_meta_lvb(0, lockres);
1937
Mark Fasheha641dc22008-12-24 16:03:48 -08001938 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001939
1940 /* We're safe here without the lockres lock... */
1941 spin_lock(&oi->ip_lock);
1942 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1943 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1944
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001945 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001946 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001947 ocfs2_set_inode_flags(inode);
1948
Mark Fashehccd979b2005-12-15 14:31:24 -08001949 /* fast-symlinks are a special case */
1950 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1951 inode->i_blocks = 0;
1952 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001953 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001954
1955 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1956 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1957 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1958 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1959 ocfs2_unpack_timespec(&inode->i_atime,
1960 be64_to_cpu(lvb->lvb_iatime_packed));
1961 ocfs2_unpack_timespec(&inode->i_mtime,
1962 be64_to_cpu(lvb->lvb_imtime_packed));
1963 ocfs2_unpack_timespec(&inode->i_ctime,
1964 be64_to_cpu(lvb->lvb_ictime_packed));
1965 spin_unlock(&oi->ip_lock);
1966
1967 mlog_exit_void();
1968}
1969
Mark Fashehf9e2d822006-09-12 15:35:49 -07001970static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1971 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001972{
Mark Fasheha641dc22008-12-24 16:03:48 -08001973 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001974
Mark Fashehf9e2d822006-09-12 15:35:49 -07001975 if (lvb->lvb_version == OCFS2_LVB_VERSION
1976 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001977 return 1;
1978 return 0;
1979}
1980
1981/* Determine whether a lock resource needs to be refreshed, and
1982 * arbitrate who gets to refresh it.
1983 *
1984 * 0 means no refresh needed.
1985 *
1986 * > 0 means you need to refresh this and you MUST call
1987 * ocfs2_complete_lock_res_refresh afterwards. */
1988static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1989{
1990 unsigned long flags;
1991 int status = 0;
1992
1993 mlog_entry_void();
1994
1995refresh_check:
1996 spin_lock_irqsave(&lockres->l_lock, flags);
1997 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1998 spin_unlock_irqrestore(&lockres->l_lock, flags);
1999 goto bail;
2000 }
2001
2002 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2003 spin_unlock_irqrestore(&lockres->l_lock, flags);
2004
2005 ocfs2_wait_on_refreshing_lock(lockres);
2006 goto refresh_check;
2007 }
2008
2009 /* Ok, I'll be the one to refresh this lock. */
2010 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2011 spin_unlock_irqrestore(&lockres->l_lock, flags);
2012
2013 status = 1;
2014bail:
2015 mlog_exit(status);
2016 return status;
2017}
2018
2019/* If status is non zero, I'll mark it as not being in refresh
2020 * anymroe, but i won't clear the needs refresh flag. */
2021static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2022 int status)
2023{
2024 unsigned long flags;
2025 mlog_entry_void();
2026
2027 spin_lock_irqsave(&lockres->l_lock, flags);
2028 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2029 if (!status)
2030 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2031 spin_unlock_irqrestore(&lockres->l_lock, flags);
2032
2033 wake_up(&lockres->l_event);
2034
2035 mlog_exit_void();
2036}
2037
2038/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002039static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002040 struct buffer_head **bh)
2041{
2042 int status = 0;
2043 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002044 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002045 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002046 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002047
2048 mlog_entry_void();
2049
Mark Fashehbe9e9862007-04-18 15:22:08 -07002050 if (ocfs2_mount_local(osb))
2051 goto bail;
2052
Mark Fashehccd979b2005-12-15 14:31:24 -08002053 spin_lock(&oi->ip_lock);
2054 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb0697052006-03-03 10:24:33 -08002055 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002056 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002057 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002058 spin_unlock(&oi->ip_lock);
2059 status = -ENOENT;
2060 goto bail;
2061 }
2062 spin_unlock(&oi->ip_lock);
2063
Mark Fashehbe9e9862007-04-18 15:22:08 -07002064 if (!ocfs2_should_refresh_lock_res(lockres))
2065 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002066
2067 /* This will discard any caching information we might have had
2068 * for the inode metadata. */
2069 ocfs2_metadata_cache_purge(inode);
2070
Mark Fasheh83418972007-04-23 18:53:12 -07002071 ocfs2_extent_map_trunc(inode, 0);
2072
Mark Fashehbe9e9862007-04-18 15:22:08 -07002073 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb0697052006-03-03 10:24:33 -08002074 mlog(0, "Trusting LVB on inode %llu\n",
2075 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002076 ocfs2_refresh_inode_from_lvb(inode);
2077 } else {
2078 /* Boo, we have to go to disk. */
2079 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002080 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002081 if (status < 0) {
2082 mlog_errno(status);
2083 goto bail_refresh;
2084 }
2085 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2086
2087 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002088 * locking an invalid object. ocfs2_read_inode_block()
2089 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002090 *
2091 * We bug on a stale inode here because we checked
2092 * above whether it was wiped from disk. The wiping
2093 * node provides a guarantee that we receive that
2094 * message and can mark the inode before dropping any
2095 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002096 mlog_bug_on_msg(inode->i_generation !=
2097 le32_to_cpu(fe->i_generation),
Mark Fashehb0697052006-03-03 10:24:33 -08002098 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002099 "inode->i_generation: %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002100 (unsigned long long)oi->ip_blkno,
2101 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002102 inode->i_generation);
2103 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2104 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb0697052006-03-03 10:24:33 -08002105 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2106 (unsigned long long)oi->ip_blkno,
2107 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002108 le32_to_cpu(fe->i_flags));
2109
2110 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002111 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002112 }
2113
2114 status = 0;
2115bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002116 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002117bail:
2118 mlog_exit(status);
2119 return status;
2120}
2121
2122static int ocfs2_assign_bh(struct inode *inode,
2123 struct buffer_head **ret_bh,
2124 struct buffer_head *passed_bh)
2125{
2126 int status;
2127
2128 if (passed_bh) {
2129 /* Ok, the update went to disk for us, use the
2130 * returned bh. */
2131 *ret_bh = passed_bh;
2132 get_bh(*ret_bh);
2133
2134 return 0;
2135 }
2136
Joel Beckerb657c952008-11-13 14:49:11 -08002137 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 if (status < 0)
2139 mlog_errno(status);
2140
2141 return status;
2142}
2143
2144/*
2145 * returns < 0 error if the callback will never be called, otherwise
2146 * the result of the lock will be communicated via the callback.
2147 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002148int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002149 struct buffer_head **ret_bh,
2150 int ex,
2151 int arg_flags)
2152{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002153 int status, level, acquired;
2154 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002155 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2157 struct buffer_head *local_bh = NULL;
2158
2159 BUG_ON(!inode);
2160
2161 mlog_entry_void();
2162
Mark Fashehb0697052006-03-03 10:24:33 -08002163 mlog(0, "inode %llu, take %s META lock\n",
2164 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002165 ex ? "EXMODE" : "PRMODE");
2166
2167 status = 0;
2168 acquired = 0;
2169 /* We'll allow faking a readonly metadata lock for
2170 * rodevices. */
2171 if (ocfs2_is_hard_readonly(osb)) {
2172 if (ex)
2173 status = -EROFS;
2174 goto bail;
2175 }
2176
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002177 if (ocfs2_mount_local(osb))
2178 goto local;
2179
Mark Fashehccd979b2005-12-15 14:31:24 -08002180 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002181 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002182
Mark Fashehe63aecb62007-10-18 15:30:42 -07002183 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002184 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002185 dlm_flags = 0;
2186 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002187 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002188
2189 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2190 if (status < 0) {
2191 if (status != -EAGAIN && status != -EIOCBRETRY)
2192 mlog_errno(status);
2193 goto bail;
2194 }
2195
2196 /* Notify the error cleanup path to drop the cluster lock. */
2197 acquired = 1;
2198
2199 /* We wait twice because a node may have died while we were in
2200 * the lower dlm layers. The second time though, we've
2201 * committed to owning this lock so we don't allow signals to
2202 * abort the operation. */
2203 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002204 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002205
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002206local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002207 /*
2208 * We only see this flag if we're being called from
2209 * ocfs2_read_locked_inode(). It means we're locking an inode
2210 * which hasn't been populated yet, so clear the refresh flag
2211 * and let the caller handle it.
2212 */
2213 if (inode->i_state & I_NEW) {
2214 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002215 if (lockres)
2216 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002217 goto bail;
2218 }
2219
Mark Fashehccd979b2005-12-15 14:31:24 -08002220 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002221 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002222 * may or may not read one, depending on what's in the
2223 * LVB. The result of all of this is that we've *only* gone to
2224 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002225 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002226 if (status < 0) {
2227 if (status != -ENOENT)
2228 mlog_errno(status);
2229 goto bail;
2230 }
2231
2232 if (ret_bh) {
2233 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2234 if (status < 0) {
2235 mlog_errno(status);
2236 goto bail;
2237 }
2238 }
2239
Mark Fashehccd979b2005-12-15 14:31:24 -08002240bail:
2241 if (status < 0) {
2242 if (ret_bh && (*ret_bh)) {
2243 brelse(*ret_bh);
2244 *ret_bh = NULL;
2245 }
2246 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002247 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002248 }
2249
2250 if (local_bh)
2251 brelse(local_bh);
2252
2253 mlog_exit(status);
2254 return status;
2255}
2256
2257/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002258 * This is working around a lock inversion between tasks acquiring DLM
2259 * locks while holding a page lock and the downconvert thread which
2260 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 *
2262 * ** These _with_page variantes are only intended to be called from aop
2263 * methods that hold page locks and return a very specific *positive* error
2264 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2265 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002266 * The DLM is called such that it returns -EAGAIN if it would have
2267 * blocked waiting for the downconvert thread. In that case we unlock
2268 * our page so the downconvert thread can make progress. Once we've
2269 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2270 * that called us can bubble that back up into the VFS who will then
2271 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002272 *
2273 * We do a blocking lock and immediate unlock before returning, though, so that
2274 * the lock has a great chance of being cached on this node by the time the VFS
2275 * calls back to retry the aop. This has a potential to livelock as nodes
2276 * ping locks back and forth, but that's a risk we're willing to take to avoid
2277 * the lock inversion simply.
2278 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002279int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002280 struct buffer_head **ret_bh,
2281 int ex,
2282 struct page *page)
2283{
2284 int ret;
2285
Mark Fashehe63aecb62007-10-18 15:30:42 -07002286 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002287 if (ret == -EAGAIN) {
2288 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002289 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2290 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002291 ret = AOP_TRUNCATED_PAGE;
2292 }
2293
2294 return ret;
2295}
2296
Mark Fashehe63aecb62007-10-18 15:30:42 -07002297int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002298 struct vfsmount *vfsmnt,
2299 int *level)
2300{
2301 int ret;
2302
2303 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002304 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002305 if (ret < 0) {
2306 mlog_errno(ret);
2307 return ret;
2308 }
2309
2310 /*
2311 * If we should update atime, we will get EX lock,
2312 * otherwise we just get PR lock.
2313 */
2314 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2315 struct buffer_head *bh = NULL;
2316
Mark Fashehe63aecb62007-10-18 15:30:42 -07002317 ocfs2_inode_unlock(inode, 0);
2318 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002319 if (ret < 0) {
2320 mlog_errno(ret);
2321 return ret;
2322 }
2323 *level = 1;
2324 if (ocfs2_should_update_atime(inode, vfsmnt))
2325 ocfs2_update_inode_atime(inode, bh);
2326 if (bh)
2327 brelse(bh);
2328 } else
2329 *level = 0;
2330
2331 mlog_exit(ret);
2332 return ret;
2333}
2334
Mark Fashehe63aecb62007-10-18 15:30:42 -07002335void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002336 int ex)
2337{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002338 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002339 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002340 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002341
2342 mlog_entry_void();
2343
Mark Fashehb0697052006-03-03 10:24:33 -08002344 mlog(0, "inode %llu drop %s META lock\n",
2345 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002346 ex ? "EXMODE" : "PRMODE");
2347
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002348 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2349 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002350 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2351
2352 mlog_exit_void();
2353}
2354
2355int ocfs2_super_lock(struct ocfs2_super *osb,
2356 int ex)
2357{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002358 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002359 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002360 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002361
2362 mlog_entry_void();
2363
2364 if (ocfs2_is_hard_readonly(osb))
2365 return -EROFS;
2366
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002367 if (ocfs2_mount_local(osb))
2368 goto bail;
2369
Mark Fashehccd979b2005-12-15 14:31:24 -08002370 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2371 if (status < 0) {
2372 mlog_errno(status);
2373 goto bail;
2374 }
2375
2376 /* The super block lock path is really in the best position to
2377 * know when resources covered by the lock need to be
2378 * refreshed, so we do it here. Of course, making sense of
2379 * everything is up to the caller :) */
2380 status = ocfs2_should_refresh_lock_res(lockres);
2381 if (status < 0) {
2382 mlog_errno(status);
2383 goto bail;
2384 }
2385 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002386 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002387
2388 ocfs2_complete_lock_res_refresh(lockres, status);
2389
2390 if (status < 0)
2391 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002392 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002393 }
2394bail:
2395 mlog_exit(status);
2396 return status;
2397}
2398
2399void ocfs2_super_unlock(struct ocfs2_super *osb,
2400 int ex)
2401{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002402 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2404
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002405 if (!ocfs2_mount_local(osb))
2406 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002407}
2408
2409int ocfs2_rename_lock(struct ocfs2_super *osb)
2410{
2411 int status;
2412 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2413
2414 if (ocfs2_is_hard_readonly(osb))
2415 return -EROFS;
2416
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002417 if (ocfs2_mount_local(osb))
2418 return 0;
2419
Joel Beckerbd3e7612008-02-01 12:14:57 -08002420 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002421 if (status < 0)
2422 mlog_errno(status);
2423
2424 return status;
2425}
2426
2427void ocfs2_rename_unlock(struct ocfs2_super *osb)
2428{
2429 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2430
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002431 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002432 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002433}
2434
wengang wang6ca497a2009-03-06 21:29:10 +08002435int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2436{
2437 int status;
2438 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2439
2440 if (ocfs2_is_hard_readonly(osb))
2441 return -EROFS;
2442
2443 if (ocfs2_mount_local(osb))
2444 return 0;
2445
2446 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2447 0, 0);
2448 if (status < 0)
2449 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2450
2451 return status;
2452}
2453
2454void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2455{
2456 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2457
2458 if (!ocfs2_mount_local(osb))
2459 ocfs2_cluster_unlock(osb, lockres,
2460 ex ? LKM_EXMODE : LKM_PRMODE);
2461}
2462
Mark Fashehd680efe2006-09-08 14:14:34 -07002463int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2464{
2465 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002466 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002467 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2468 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2469
2470 BUG_ON(!dl);
2471
2472 if (ocfs2_is_hard_readonly(osb))
2473 return -EROFS;
2474
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002475 if (ocfs2_mount_local(osb))
2476 return 0;
2477
Mark Fashehd680efe2006-09-08 14:14:34 -07002478 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2479 if (ret < 0)
2480 mlog_errno(ret);
2481
2482 return ret;
2483}
2484
2485void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2486{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002487 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002488 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2489 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2490
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002491 if (!ocfs2_mount_local(osb))
2492 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002493}
2494
Mark Fashehccd979b2005-12-15 14:31:24 -08002495/* Reference counting of the dlm debug structure. We want this because
2496 * open references on the debug inodes can live on after a mount, so
2497 * we can't rely on the ocfs2_super to always exist. */
2498static void ocfs2_dlm_debug_free(struct kref *kref)
2499{
2500 struct ocfs2_dlm_debug *dlm_debug;
2501
2502 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2503
2504 kfree(dlm_debug);
2505}
2506
2507void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2508{
2509 if (dlm_debug)
2510 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2511}
2512
2513static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2514{
2515 kref_get(&debug->d_refcnt);
2516}
2517
2518struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2519{
2520 struct ocfs2_dlm_debug *dlm_debug;
2521
2522 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2523 if (!dlm_debug) {
2524 mlog_errno(-ENOMEM);
2525 goto out;
2526 }
2527
2528 kref_init(&dlm_debug->d_refcnt);
2529 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2530 dlm_debug->d_locking_state = NULL;
2531out:
2532 return dlm_debug;
2533}
2534
2535/* Access to this is arbitrated for us via seq_file->sem. */
2536struct ocfs2_dlm_seq_priv {
2537 struct ocfs2_dlm_debug *p_dlm_debug;
2538 struct ocfs2_lock_res p_iter_res;
2539 struct ocfs2_lock_res p_tmp_res;
2540};
2541
2542static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2543 struct ocfs2_dlm_seq_priv *priv)
2544{
2545 struct ocfs2_lock_res *iter, *ret = NULL;
2546 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2547
2548 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2549
2550 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2551 /* discover the head of the list */
2552 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2553 mlog(0, "End of list found, %p\n", ret);
2554 break;
2555 }
2556
2557 /* We track our "dummy" iteration lockres' by a NULL
2558 * l_ops field. */
2559 if (iter->l_ops != NULL) {
2560 ret = iter;
2561 break;
2562 }
2563 }
2564
2565 return ret;
2566}
2567
2568static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2569{
2570 struct ocfs2_dlm_seq_priv *priv = m->private;
2571 struct ocfs2_lock_res *iter;
2572
2573 spin_lock(&ocfs2_dlm_tracking_lock);
2574 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2575 if (iter) {
2576 /* Since lockres' have the lifetime of their container
2577 * (which can be inodes, ocfs2_supers, etc) we want to
2578 * copy this out to a temporary lockres while still
2579 * under the spinlock. Obviously after this we can't
2580 * trust any pointers on the copy returned, but that's
2581 * ok as the information we want isn't typically held
2582 * in them. */
2583 priv->p_tmp_res = *iter;
2584 iter = &priv->p_tmp_res;
2585 }
2586 spin_unlock(&ocfs2_dlm_tracking_lock);
2587
2588 return iter;
2589}
2590
2591static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2592{
2593}
2594
2595static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2596{
2597 struct ocfs2_dlm_seq_priv *priv = m->private;
2598 struct ocfs2_lock_res *iter = v;
2599 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2600
2601 spin_lock(&ocfs2_dlm_tracking_lock);
2602 iter = ocfs2_dlm_next_res(iter, priv);
2603 list_del_init(&dummy->l_debug_list);
2604 if (iter) {
2605 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2606 priv->p_tmp_res = *iter;
2607 iter = &priv->p_tmp_res;
2608 }
2609 spin_unlock(&ocfs2_dlm_tracking_lock);
2610
2611 return iter;
2612}
2613
2614/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002615#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002616static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2617{
2618 int i;
2619 char *lvb;
2620 struct ocfs2_lock_res *lockres = v;
2621
2622 if (!lockres)
2623 return -EINVAL;
2624
Mark Fashehd680efe2006-09-08 14:14:34 -07002625 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2626
2627 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2628 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2629 lockres->l_name,
2630 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2631 else
2632 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2633
2634 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002635 "0x%lx\t"
2636 "0x%x\t"
2637 "0x%x\t"
2638 "%u\t"
2639 "%u\t"
2640 "%d\t"
2641 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002642 lockres->l_level,
2643 lockres->l_flags,
2644 lockres->l_action,
2645 lockres->l_unlock_action,
2646 lockres->l_ro_holders,
2647 lockres->l_ex_holders,
2648 lockres->l_requested,
2649 lockres->l_blocking);
2650
2651 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002652 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002653 for(i = 0; i < DLM_LVB_LEN; i++)
2654 seq_printf(m, "0x%x\t", lvb[i]);
2655
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002656#ifdef CONFIG_OCFS2_FS_STATS
2657# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2658# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2659# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2660# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2661# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2662# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2663# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2664# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2665# define lock_refresh(_l) (_l)->l_lock_refresh
2666#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002667# define lock_num_prmode(_l) (0ULL)
2668# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002669# define lock_num_prmode_failed(_l) (0)
2670# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002671# define lock_total_prmode(_l) (0ULL)
2672# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002673# define lock_max_prmode(_l) (0)
2674# define lock_max_exmode(_l) (0)
2675# define lock_refresh(_l) (0)
2676#endif
2677 /* The following seq_print was added in version 2 of this output */
2678 seq_printf(m, "%llu\t"
2679 "%llu\t"
2680 "%u\t"
2681 "%u\t"
2682 "%llu\t"
2683 "%llu\t"
2684 "%u\t"
2685 "%u\t"
2686 "%u\t",
2687 lock_num_prmode(lockres),
2688 lock_num_exmode(lockres),
2689 lock_num_prmode_failed(lockres),
2690 lock_num_exmode_failed(lockres),
2691 lock_total_prmode(lockres),
2692 lock_total_exmode(lockres),
2693 lock_max_prmode(lockres),
2694 lock_max_exmode(lockres),
2695 lock_refresh(lockres));
2696
Mark Fashehccd979b2005-12-15 14:31:24 -08002697 /* End the line */
2698 seq_printf(m, "\n");
2699 return 0;
2700}
2701
Jan Engelhardt90d99772008-01-22 20:52:20 +01002702static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002703 .start = ocfs2_dlm_seq_start,
2704 .stop = ocfs2_dlm_seq_stop,
2705 .next = ocfs2_dlm_seq_next,
2706 .show = ocfs2_dlm_seq_show,
2707};
2708
2709static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2710{
2711 struct seq_file *seq = (struct seq_file *) file->private_data;
2712 struct ocfs2_dlm_seq_priv *priv = seq->private;
2713 struct ocfs2_lock_res *res = &priv->p_iter_res;
2714
2715 ocfs2_remove_lockres_tracking(res);
2716 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2717 return seq_release_private(inode, file);
2718}
2719
2720static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2721{
2722 int ret;
2723 struct ocfs2_dlm_seq_priv *priv;
2724 struct seq_file *seq;
2725 struct ocfs2_super *osb;
2726
2727 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2728 if (!priv) {
2729 ret = -ENOMEM;
2730 mlog_errno(ret);
2731 goto out;
2732 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002733 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002734 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2735 priv->p_dlm_debug = osb->osb_dlm_debug;
2736 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2737
2738 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2739 if (ret) {
2740 kfree(priv);
2741 mlog_errno(ret);
2742 goto out;
2743 }
2744
2745 seq = (struct seq_file *) file->private_data;
2746 seq->private = priv;
2747
2748 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2749 priv->p_dlm_debug);
2750
2751out:
2752 return ret;
2753}
2754
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002755static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002756 .open = ocfs2_dlm_debug_open,
2757 .release = ocfs2_dlm_debug_release,
2758 .read = seq_read,
2759 .llseek = seq_lseek,
2760};
2761
2762static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2763{
2764 int ret = 0;
2765 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2766
2767 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2768 S_IFREG|S_IRUSR,
2769 osb->osb_debug_root,
2770 osb,
2771 &ocfs2_dlm_debug_fops);
2772 if (!dlm_debug->d_locking_state) {
2773 ret = -EINVAL;
2774 mlog(ML_ERROR,
2775 "Unable to create locking state debugfs file.\n");
2776 goto out;
2777 }
2778
2779 ocfs2_get_dlm_debug(dlm_debug);
2780out:
2781 return ret;
2782}
2783
2784static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2785{
2786 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2787
2788 if (dlm_debug) {
2789 debugfs_remove(dlm_debug->d_locking_state);
2790 ocfs2_put_dlm_debug(dlm_debug);
2791 }
2792}
2793
2794int ocfs2_dlm_init(struct ocfs2_super *osb)
2795{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002796 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002797 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002798
2799 mlog_entry_void();
2800
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002801 if (ocfs2_mount_local(osb)) {
2802 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002803 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002804 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002805
Mark Fashehccd979b2005-12-15 14:31:24 -08002806 status = ocfs2_dlm_init_debug(osb);
2807 if (status < 0) {
2808 mlog_errno(status);
2809 goto bail;
2810 }
2811
Mark Fasheh34d024f2007-09-24 15:56:19 -07002812 /* launch downconvert thread */
2813 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2814 if (IS_ERR(osb->dc_task)) {
2815 status = PTR_ERR(osb->dc_task);
2816 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002817 mlog_errno(status);
2818 goto bail;
2819 }
2820
Mark Fashehccd979b2005-12-15 14:31:24 -08002821 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002822 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2823 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002824 strlen(osb->uuid_str),
2825 ocfs2_do_node_down, osb,
2826 &conn);
2827 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002828 mlog_errno(status);
2829 goto bail;
2830 }
2831
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002832 status = ocfs2_cluster_this_node(&osb->node_num);
2833 if (status < 0) {
2834 mlog_errno(status);
2835 mlog(ML_ERROR,
2836 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002837 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002838 goto bail;
2839 }
2840
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002841local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002842 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2843 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002844 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002845
Joel Becker4670c462008-02-01 14:39:35 -08002846 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002847
2848 status = 0;
2849bail:
2850 if (status < 0) {
2851 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002852 if (osb->dc_task)
2853 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002854 }
2855
2856 mlog_exit(status);
2857 return status;
2858}
2859
Joel Becker286eaa92008-02-01 15:03:57 -08002860void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2861 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002862{
2863 mlog_entry_void();
2864
Mark Fashehccd979b2005-12-15 14:31:24 -08002865 ocfs2_drop_osb_locks(osb);
2866
Joel Becker4670c462008-02-01 14:39:35 -08002867 /*
2868 * Now that we have dropped all locks and ocfs2_dismount_volume()
2869 * has disabled recovery, the DLM won't be talking to us. It's
2870 * safe to tear things down before disconnecting the cluster.
2871 */
2872
Mark Fasheh34d024f2007-09-24 15:56:19 -07002873 if (osb->dc_task) {
2874 kthread_stop(osb->dc_task);
2875 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002876 }
2877
2878 ocfs2_lock_res_free(&osb->osb_super_lockres);
2879 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08002880 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002881
Joel Becker286eaa92008-02-01 15:03:57 -08002882 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08002883 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002884
2885 ocfs2_dlm_shutdown_debug(osb);
2886
2887 mlog_exit_void();
2888}
2889
Joel Becker7431cd72008-02-01 12:15:37 -08002890static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002891{
2892 struct ocfs2_lock_res *lockres = opaque;
2893 unsigned long flags;
2894
2895 mlog_entry_void();
2896
2897 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2898 lockres->l_unlock_action);
2899
2900 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08002901 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08002902 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2903 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002904 lockres->l_unlock_action);
2905 spin_unlock_irqrestore(&lockres->l_lock, flags);
2906 return;
2907 }
2908
2909 switch(lockres->l_unlock_action) {
2910 case OCFS2_UNLOCK_CANCEL_CONVERT:
2911 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2912 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08002913 /* Downconvert thread may have requeued this lock, we
2914 * need to wake it. */
2915 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2916 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08002917 break;
2918 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002919 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002920 break;
2921 default:
2922 BUG();
2923 }
2924
2925 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08002926 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08002927 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06002928 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002929
2930 mlog_exit_void();
2931}
2932
Mark Fashehccd979b2005-12-15 14:31:24 -08002933static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002934 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002935{
Joel Becker7431cd72008-02-01 12:15:37 -08002936 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002937 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002938 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002939
2940 /* We didn't get anywhere near actually using this lockres. */
2941 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2942 goto out;
2943
Mark Fashehb80fc012006-09-12 22:08:14 -07002944 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002945 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002946
Mark Fashehccd979b2005-12-15 14:31:24 -08002947 spin_lock_irqsave(&lockres->l_lock, flags);
2948
2949 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2950 "lockres %s, flags 0x%lx\n",
2951 lockres->l_name, lockres->l_flags);
2952
2953 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2954 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2955 "%u, unlock_action = %u\n",
2956 lockres->l_name, lockres->l_flags, lockres->l_action,
2957 lockres->l_unlock_action);
2958
2959 spin_unlock_irqrestore(&lockres->l_lock, flags);
2960
2961 /* XXX: Today we just wait on any busy
2962 * locks... Perhaps we need to cancel converts in the
2963 * future? */
2964 ocfs2_wait_on_busy_lock(lockres);
2965
2966 spin_lock_irqsave(&lockres->l_lock, flags);
2967 }
2968
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002969 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2970 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08002971 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002972 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2973 lockres->l_ops->set_lvb(lockres);
2974 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002975
2976 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2977 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2978 lockres->l_name);
2979 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2980 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2981
2982 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2983 spin_unlock_irqrestore(&lockres->l_lock, flags);
2984 goto out;
2985 }
2986
2987 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2988
2989 /* make sure we never get here while waiting for an ast to
2990 * fire. */
2991 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2992
2993 /* is this necessary? */
2994 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2995 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2996 spin_unlock_irqrestore(&lockres->l_lock, flags);
2997
2998 mlog(0, "lock %s\n", lockres->l_name);
2999
Joel Becker4670c462008-02-01 14:39:35 -08003000 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003001 lockres);
3002 if (ret) {
3003 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003004 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003005 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003006 BUG();
3007 }
Coly Li73ac36e2009-01-07 18:09:16 -08003008 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003009 lockres->l_name);
3010
3011 ocfs2_wait_on_busy_lock(lockres);
3012out:
3013 mlog_exit(0);
3014 return 0;
3015}
3016
3017/* Mark the lockres as being dropped. It will no longer be
3018 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003019 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08003020 * it safe to drop.
3021 *
3022 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3023void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3024{
3025 int status;
3026 struct ocfs2_mask_waiter mw;
3027 unsigned long flags;
3028
3029 ocfs2_init_mask_waiter(&mw);
3030
3031 spin_lock_irqsave(&lockres->l_lock, flags);
3032 lockres->l_flags |= OCFS2_LOCK_FREEING;
3033 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3034 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3035 spin_unlock_irqrestore(&lockres->l_lock, flags);
3036
3037 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3038
3039 status = ocfs2_wait_for_mask(&mw);
3040 if (status)
3041 mlog_errno(status);
3042
3043 spin_lock_irqsave(&lockres->l_lock, flags);
3044 }
3045 spin_unlock_irqrestore(&lockres->l_lock, flags);
3046}
3047
Mark Fashehd680efe2006-09-08 14:14:34 -07003048void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3049 struct ocfs2_lock_res *lockres)
3050{
3051 int ret;
3052
3053 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003054 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003055 if (ret)
3056 mlog_errno(ret);
3057}
3058
Mark Fashehccd979b2005-12-15 14:31:24 -08003059static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3060{
Mark Fashehd680efe2006-09-08 14:14:34 -07003061 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3062 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003063 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003064}
3065
Mark Fashehccd979b2005-12-15 14:31:24 -08003066int ocfs2_drop_inode_locks(struct inode *inode)
3067{
3068 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003069
3070 mlog_entry_void();
3071
3072 /* No need to call ocfs2_mark_lockres_freeing here -
3073 * ocfs2_clear_inode has done it for us. */
3074
3075 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003076 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003077 if (err < 0)
3078 mlog_errno(err);
3079
3080 status = err;
3081
3082 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003083 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003084 if (err < 0)
3085 mlog_errno(err);
3086 if (err < 0 && !status)
3087 status = err;
3088
3089 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003090 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003091 if (err < 0)
3092 mlog_errno(err);
3093 if (err < 0 && !status)
3094 status = err;
3095
3096 mlog_exit(status);
3097 return status;
3098}
3099
Joel Beckerde551242008-02-01 14:45:08 -08003100static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3101 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003102{
3103 assert_spin_locked(&lockres->l_lock);
3104
Joel Beckerbd3e7612008-02-01 12:14:57 -08003105 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003106
3107 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003108 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003109 lockres->l_level, new_level);
3110 BUG();
3111 }
3112
3113 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3114 lockres->l_name, new_level, lockres->l_blocking);
3115
3116 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3117 lockres->l_requested = new_level;
3118 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003119 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003120}
3121
3122static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3123 struct ocfs2_lock_res *lockres,
3124 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003125 int lvb,
3126 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003127{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003128 int ret;
3129 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003130
3131 mlog_entry_void();
3132
3133 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003134 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003135
Joel Becker4670c462008-02-01 14:39:35 -08003136 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003137 new_level,
3138 &lockres->l_lksb,
3139 dlm_flags,
3140 lockres->l_name,
3141 OCFS2_LOCK_ID_MAX_LEN - 1,
3142 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003143 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003144 if (ret) {
3145 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003146 ocfs2_recover_from_dlm_error(lockres, 1);
3147 goto bail;
3148 }
3149
3150 ret = 0;
3151bail:
3152 mlog_exit(ret);
3153 return ret;
3154}
3155
Joel Becker24ef1812008-01-29 17:37:32 -08003156/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003157static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3158 struct ocfs2_lock_res *lockres)
3159{
3160 assert_spin_locked(&lockres->l_lock);
3161
3162 mlog_entry_void();
3163 mlog(0, "lock %s\n", lockres->l_name);
3164
3165 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3166 /* If we're already trying to cancel a lock conversion
3167 * then just drop the spinlock and allow the caller to
3168 * requeue this lock. */
3169
3170 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3171 return 0;
3172 }
3173
3174 /* were we in a convert when we got the bast fire? */
3175 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3176 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3177 /* set things up for the unlockast to know to just
3178 * clear out the ast_action and unset busy, etc. */
3179 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3180
3181 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3182 "lock %s, invalid flags: 0x%lx\n",
3183 lockres->l_name, lockres->l_flags);
3184
3185 return 1;
3186}
3187
3188static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3189 struct ocfs2_lock_res *lockres)
3190{
3191 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003192
3193 mlog_entry_void();
3194 mlog(0, "lock %s\n", lockres->l_name);
3195
Joel Becker4670c462008-02-01 14:39:35 -08003196 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003197 DLM_LKF_CANCEL, lockres);
3198 if (ret) {
3199 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003200 ocfs2_recover_from_dlm_error(lockres, 0);
3201 }
3202
Joel Becker24ef1812008-01-29 17:37:32 -08003203 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003204
3205 mlog_exit(ret);
3206 return ret;
3207}
3208
Mark Fashehb5e500e2006-09-13 22:01:16 -07003209static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3210 struct ocfs2_lock_res *lockres,
3211 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003212{
3213 unsigned long flags;
3214 int blocking;
3215 int new_level;
3216 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003217 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003218 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003219
3220 mlog_entry_void();
3221
3222 spin_lock_irqsave(&lockres->l_lock, flags);
3223
3224 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3225
3226recheck:
3227 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003228 /* XXX
3229 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3230 * exists entirely for one reason - another thread has set
3231 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3232 *
3233 * If we do ocfs2_cancel_convert() before the other thread
3234 * calls dlm_lock(), our cancel will do nothing. We will
3235 * get no ast, and we will have no way of knowing the
3236 * cancel failed. Meanwhile, the other thread will call
3237 * into dlm_lock() and wait...forever.
3238 *
3239 * Why forever? Because another node has asked for the
3240 * lock first; that's why we're here in unblock_lock().
3241 *
3242 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3243 * set, we just requeue the unblock. Only when the other
3244 * thread has called dlm_lock() and cleared PENDING will
3245 * we then cancel their request.
3246 *
3247 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3248 * at the same time they set OCFS2_DLM_BUSY. They must
3249 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3250 */
3251 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3252 goto leave_requeue;
3253
Mark Fashehd680efe2006-09-08 14:14:34 -07003254 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003255 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3256 spin_unlock_irqrestore(&lockres->l_lock, flags);
3257 if (ret) {
3258 ret = ocfs2_cancel_convert(osb, lockres);
3259 if (ret < 0)
3260 mlog_errno(ret);
3261 }
3262 goto leave;
3263 }
3264
3265 /* if we're blocking an exclusive and we have *any* holders,
3266 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003267 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003268 && (lockres->l_ex_holders || lockres->l_ro_holders))
3269 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003270
3271 /* If it's a PR we're blocking, then only
3272 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003273 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003274 lockres->l_ex_holders)
3275 goto leave_requeue;
3276
3277 /*
3278 * Can we get a lock in this state if the holder counts are
3279 * zero? The meta data unblock code used to check this.
3280 */
3281 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3282 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3283 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003284
Mark Fasheh16d5b9562006-09-13 21:10:12 -07003285 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3286
3287 if (lockres->l_ops->check_downconvert
3288 && !lockres->l_ops->check_downconvert(lockres, new_level))
3289 goto leave_requeue;
3290
Mark Fashehccd979b2005-12-15 14:31:24 -08003291 /* If we get here, then we know that there are no more
3292 * incompatible holders (and anyone asking for an incompatible
3293 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003294 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003295 goto downconvert;
3296
3297 /* Some lockres types want to do a bit of work before
3298 * downconverting a lock. Allow that here. The worker function
3299 * may sleep, so we save off a copy of what we're blocking as
3300 * it may change while we're not holding the spin lock. */
3301 blocking = lockres->l_blocking;
3302 spin_unlock_irqrestore(&lockres->l_lock, flags);
3303
Mark Fashehcc567d82006-09-13 21:52:21 -07003304 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003305
3306 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3307 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003308
3309 spin_lock_irqsave(&lockres->l_lock, flags);
3310 if (blocking != lockres->l_blocking) {
3311 /* If this changed underneath us, then we can't drop
3312 * it just yet. */
3313 goto recheck;
3314 }
3315
3316downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003317 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003318
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003319 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003320 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003321 set_lvb = 1;
3322
3323 /*
3324 * We only set the lvb if the lock has been fully
3325 * refreshed - otherwise we risk setting stale
3326 * data. Otherwise, there's no need to actually clear
3327 * out the lvb here as it's value is still valid.
3328 */
3329 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3330 lockres->l_ops->set_lvb(lockres);
3331 }
3332
Joel Beckerde551242008-02-01 14:45:08 -08003333 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003334 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003335 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3336 gen);
3337
Mark Fashehccd979b2005-12-15 14:31:24 -08003338leave:
3339 mlog_exit(ret);
3340 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003341
3342leave_requeue:
3343 spin_unlock_irqrestore(&lockres->l_lock, flags);
3344 ctl->requeue = 1;
3345
3346 mlog_exit(0);
3347 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003348}
3349
Mark Fashehd680efe2006-09-08 14:14:34 -07003350static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3351 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003352{
3353 struct inode *inode;
3354 struct address_space *mapping;
3355
Mark Fashehccd979b2005-12-15 14:31:24 -08003356 inode = ocfs2_lock_res_inode(lockres);
3357 mapping = inode->i_mapping;
3358
Mark Fasheh1044e402008-02-28 17:16:03 -08003359 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003360 goto out;
3361
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003362 /*
3363 * We need this before the filemap_fdatawrite() so that it can
3364 * transfer the dirty bit from the PTE to the
3365 * page. Unfortunately this means that even for EX->PR
3366 * downconverts, we'll lose our mappings and have to build
3367 * them up again.
3368 */
3369 unmap_mapping_range(mapping, 0, 0, 0);
3370
Mark Fashehccd979b2005-12-15 14:31:24 -08003371 if (filemap_fdatawrite(mapping)) {
Mark Fashehb0697052006-03-03 10:24:33 -08003372 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3373 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003374 }
3375 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003376 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003377 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003378 } else {
3379 /* We only need to wait on the I/O if we're not also
3380 * truncating pages because truncate_inode_pages waits
3381 * for us above. We don't truncate pages if we're
3382 * blocking anything < EXMODE because we want to keep
3383 * them around in that case. */
3384 filemap_fdatawait(mapping);
3385 }
3386
Mark Fashehf1f54062007-10-18 15:13:59 -07003387out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003388 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003389}
3390
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003391static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3392 int new_level)
3393{
3394 struct inode *inode = ocfs2_lock_res_inode(lockres);
3395 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3396
Joel Beckerbd3e7612008-02-01 12:14:57 -08003397 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3398 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003399
3400 if (checkpointed)
3401 return 1;
3402
3403 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3404 return 0;
3405}
3406
3407static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3408{
3409 struct inode *inode = ocfs2_lock_res_inode(lockres);
3410
3411 __ocfs2_stuff_meta_lvb(inode);
3412}
3413
Mark Fashehd680efe2006-09-08 14:14:34 -07003414/*
3415 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003416 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003417 * dlmglue API and push these off to the ocfs2_wq in the future.
3418 */
3419static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3420 struct ocfs2_lock_res *lockres)
3421{
3422 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3423 ocfs2_dentry_lock_put(osb, dl);
3424}
3425
3426/*
3427 * d_delete() matching dentries before the lock downconvert.
3428 *
3429 * At this point, any process waiting to destroy the
3430 * dentry_lock due to last ref count is stopped by the
3431 * OCFS2_LOCK_QUEUED flag.
3432 *
3433 * We have two potential problems
3434 *
3435 * 1) If we do the last reference drop on our dentry_lock (via dput)
3436 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3437 * the downconvert to finish. Instead we take an elevated
3438 * reference and push the drop until after we've completed our
3439 * unblock processing.
3440 *
3441 * 2) There might be another process with a final reference,
3442 * waiting on us to finish processing. If this is the case, we
3443 * detect it and exit out - there's no more dentries anyway.
3444 */
3445static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3446 int blocking)
3447{
3448 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3449 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3450 struct dentry *dentry;
3451 unsigned long flags;
3452 int extra_ref = 0;
3453
3454 /*
3455 * This node is blocking another node from getting a read
3456 * lock. This happens when we've renamed within a
3457 * directory. We've forced the other nodes to d_delete(), but
3458 * we never actually dropped our lock because it's still
3459 * valid. The downconvert code will retain a PR for this node,
3460 * so there's no further work to do.
3461 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003462 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003463 return UNBLOCK_CONTINUE;
3464
3465 /*
3466 * Mark this inode as potentially orphaned. The code in
3467 * ocfs2_delete_inode() will figure out whether it actually
3468 * needs to be freed or not.
3469 */
3470 spin_lock(&oi->ip_lock);
3471 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3472 spin_unlock(&oi->ip_lock);
3473
3474 /*
3475 * Yuck. We need to make sure however that the check of
3476 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3477 * respect to a reference decrement or the setting of that
3478 * flag.
3479 */
3480 spin_lock_irqsave(&lockres->l_lock, flags);
3481 spin_lock(&dentry_attach_lock);
3482 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3483 && dl->dl_count) {
3484 dl->dl_count++;
3485 extra_ref = 1;
3486 }
3487 spin_unlock(&dentry_attach_lock);
3488 spin_unlock_irqrestore(&lockres->l_lock, flags);
3489
3490 mlog(0, "extra_ref = %d\n", extra_ref);
3491
3492 /*
3493 * We have a process waiting on us in ocfs2_dentry_iput(),
3494 * which means we can't have any more outstanding
3495 * aliases. There's no need to do any more work.
3496 */
3497 if (!extra_ref)
3498 return UNBLOCK_CONTINUE;
3499
3500 spin_lock(&dentry_attach_lock);
3501 while (1) {
3502 dentry = ocfs2_find_local_alias(dl->dl_inode,
3503 dl->dl_parent_blkno, 1);
3504 if (!dentry)
3505 break;
3506 spin_unlock(&dentry_attach_lock);
3507
3508 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3509 dentry->d_name.name);
3510
3511 /*
3512 * The following dcache calls may do an
3513 * iput(). Normally we don't want that from the
3514 * downconverting thread, but in this case it's ok
3515 * because the requesting node already has an
3516 * exclusive lock on the inode, so it can't be queued
3517 * for a downconvert.
3518 */
3519 d_delete(dentry);
3520 dput(dentry);
3521
3522 spin_lock(&dentry_attach_lock);
3523 }
3524 spin_unlock(&dentry_attach_lock);
3525
3526 /*
3527 * If we are the last holder of this dentry lock, there is no
3528 * reason to downconvert so skip straight to the unlock.
3529 */
3530 if (dl->dl_count == 1)
3531 return UNBLOCK_STOP_POST;
3532
3533 return UNBLOCK_CONTINUE_POST;
3534}
3535
Jan Kara9e33d692008-08-25 19:56:50 +02003536static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3537{
3538 struct ocfs2_qinfo_lvb *lvb;
3539 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3540 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3541 oinfo->dqi_gi.dqi_type);
3542
3543 mlog_entry_void();
3544
Mark Fasheha641dc22008-12-24 16:03:48 -08003545 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003546 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3547 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3548 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3549 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3550 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3551 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3552 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3553
3554 mlog_exit_void();
3555}
3556
3557void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3558{
3559 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3560 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3561 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3562
3563 mlog_entry_void();
3564 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3565 ocfs2_cluster_unlock(osb, lockres, level);
3566 mlog_exit_void();
3567}
3568
3569static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3570{
3571 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3572 oinfo->dqi_gi.dqi_type);
3573 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3574 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003575 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003576 struct ocfs2_global_disk_dqinfo *gdinfo;
3577 int status = 0;
3578
3579 if (lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3580 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3581 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3582 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3583 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3584 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3585 oinfo->dqi_gi.dqi_free_entry =
3586 be32_to_cpu(lvb->lvb_free_entry);
3587 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003588 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3589 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003590 mlog_errno(status);
3591 goto bail;
3592 }
3593 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3594 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3595 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3596 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3597 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3598 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3599 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3600 oinfo->dqi_gi.dqi_free_entry =
3601 le32_to_cpu(gdinfo->dqi_free_entry);
3602 brelse(bh);
3603 ocfs2_track_lock_refresh(lockres);
3604 }
3605
3606bail:
3607 return status;
3608}
3609
3610/* Lock quota info, this function expects at least shared lock on the quota file
3611 * so that we can safely refresh quota info from disk. */
3612int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3613{
3614 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3615 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3616 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3617 int status = 0;
3618
3619 mlog_entry_void();
3620
3621 /* On RO devices, locking really isn't needed... */
3622 if (ocfs2_is_hard_readonly(osb)) {
3623 if (ex)
3624 status = -EROFS;
3625 goto bail;
3626 }
3627 if (ocfs2_mount_local(osb))
3628 goto bail;
3629
3630 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3631 if (status < 0) {
3632 mlog_errno(status);
3633 goto bail;
3634 }
3635 if (!ocfs2_should_refresh_lock_res(lockres))
3636 goto bail;
3637 /* OK, we have the lock but we need to refresh the quota info */
3638 status = ocfs2_refresh_qinfo(oinfo);
3639 if (status)
3640 ocfs2_qinfo_unlock(oinfo, ex);
3641 ocfs2_complete_lock_res_refresh(lockres, status);
3642bail:
3643 mlog_exit(status);
3644 return status;
3645}
3646
Joel Becker4670c462008-02-01 14:39:35 -08003647/*
3648 * This is the filesystem locking protocol. It provides the lock handling
3649 * hooks for the underlying DLM. It has a maximum version number.
3650 * The version number allows interoperability with systems running at
3651 * the same major number and an equal or smaller minor number.
3652 *
3653 * Whenever the filesystem does new things with locks (adds or removes a
3654 * lock, orders them differently, does different things underneath a lock),
3655 * the version must be changed. The protocol is negotiated when joining
3656 * the dlm domain. A node may join the domain if its major version is
3657 * identical to all other nodes and its minor version is greater than
3658 * or equal to all other nodes. When its minor version is greater than
3659 * the other nodes, it will run at the minor version specified by the
3660 * other nodes.
3661 *
3662 * If a locking change is made that will not be compatible with older
3663 * versions, the major number must be increased and the minor version set
3664 * to zero. If a change merely adds a behavior that can be disabled when
3665 * speaking to older versions, the minor version must be increased. If a
3666 * change adds a fully backwards compatible change (eg, LVB changes that
3667 * are just ignored by older versions), the version does not need to be
3668 * updated.
3669 */
Joel Becker24ef1812008-01-29 17:37:32 -08003670static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003671 .lp_max_version = {
3672 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3673 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3674 },
Joel Becker24ef1812008-01-29 17:37:32 -08003675 .lp_lock_ast = ocfs2_locking_ast,
3676 .lp_blocking_ast = ocfs2_blocking_ast,
3677 .lp_unlock_ast = ocfs2_unlock_ast,
3678};
3679
Joel Becker63e0c482008-01-30 16:58:36 -08003680void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003681{
Joel Becker63e0c482008-01-30 16:58:36 -08003682 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003683}
3684
Joel Becker24ef1812008-01-29 17:37:32 -08003685
Adrian Bunk00600052008-01-29 00:11:41 +02003686static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3687 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003688{
3689 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003690 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003691 unsigned long flags;
3692
3693 /* Our reference to the lockres in this function can be
3694 * considered valid until we remove the OCFS2_LOCK_QUEUED
3695 * flag. */
3696
3697 mlog_entry_void();
3698
3699 BUG_ON(!lockres);
3700 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003701
3702 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3703
3704 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003705 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003706 * still be marked with OCFS2_LOCK_FREEING after this check,
3707 * but short circuiting here will still save us some
3708 * performance. */
3709 spin_lock_irqsave(&lockres->l_lock, flags);
3710 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3711 goto unqueue;
3712 spin_unlock_irqrestore(&lockres->l_lock, flags);
3713
Mark Fashehb5e500e2006-09-13 22:01:16 -07003714 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003715 if (status < 0)
3716 mlog_errno(status);
3717
3718 spin_lock_irqsave(&lockres->l_lock, flags);
3719unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003720 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003721 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3722 } else
3723 ocfs2_schedule_blocked_lock(osb, lockres);
3724
3725 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003726 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003727 spin_unlock_irqrestore(&lockres->l_lock, flags);
3728
Mark Fashehd680efe2006-09-08 14:14:34 -07003729 if (ctl.unblock_action != UNBLOCK_CONTINUE
3730 && lockres->l_ops->post_unlock)
3731 lockres->l_ops->post_unlock(osb, lockres);
3732
Mark Fashehccd979b2005-12-15 14:31:24 -08003733 mlog_exit_void();
3734}
3735
3736static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3737 struct ocfs2_lock_res *lockres)
3738{
3739 mlog_entry_void();
3740
3741 assert_spin_locked(&lockres->l_lock);
3742
3743 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3744 /* Do not schedule a lock for downconvert when it's on
3745 * the way to destruction - any nodes wanting access
3746 * to the resource will get it soon. */
3747 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3748 lockres->l_name, lockres->l_flags);
3749 return;
3750 }
3751
3752 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3753
Mark Fasheh34d024f2007-09-24 15:56:19 -07003754 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003755 if (list_empty(&lockres->l_blocked_list)) {
3756 list_add_tail(&lockres->l_blocked_list,
3757 &osb->blocked_lock_list);
3758 osb->blocked_lock_count++;
3759 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003760 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003761
3762 mlog_exit_void();
3763}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003764
3765static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3766{
3767 unsigned long processed;
3768 struct ocfs2_lock_res *lockres;
3769
3770 mlog_entry_void();
3771
3772 spin_lock(&osb->dc_task_lock);
3773 /* grab this early so we know to try again if a state change and
3774 * wake happens part-way through our work */
3775 osb->dc_work_sequence = osb->dc_wake_sequence;
3776
3777 processed = osb->blocked_lock_count;
3778 while (processed) {
3779 BUG_ON(list_empty(&osb->blocked_lock_list));
3780
3781 lockres = list_entry(osb->blocked_lock_list.next,
3782 struct ocfs2_lock_res, l_blocked_list);
3783 list_del_init(&lockres->l_blocked_list);
3784 osb->blocked_lock_count--;
3785 spin_unlock(&osb->dc_task_lock);
3786
3787 BUG_ON(!processed);
3788 processed--;
3789
3790 ocfs2_process_blocked_lock(osb, lockres);
3791
3792 spin_lock(&osb->dc_task_lock);
3793 }
3794 spin_unlock(&osb->dc_task_lock);
3795
3796 mlog_exit_void();
3797}
3798
3799static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3800{
3801 int empty = 0;
3802
3803 spin_lock(&osb->dc_task_lock);
3804 if (list_empty(&osb->blocked_lock_list))
3805 empty = 1;
3806
3807 spin_unlock(&osb->dc_task_lock);
3808 return empty;
3809}
3810
3811static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3812{
3813 int should_wake = 0;
3814
3815 spin_lock(&osb->dc_task_lock);
3816 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3817 should_wake = 1;
3818 spin_unlock(&osb->dc_task_lock);
3819
3820 return should_wake;
3821}
3822
Adrian Bunk200bfae2008-02-17 10:20:38 +02003823static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003824{
3825 int status = 0;
3826 struct ocfs2_super *osb = arg;
3827
3828 /* only quit once we've been asked to stop and there is no more
3829 * work available */
3830 while (!(kthread_should_stop() &&
3831 ocfs2_downconvert_thread_lists_empty(osb))) {
3832
3833 wait_event_interruptible(osb->dc_event,
3834 ocfs2_downconvert_thread_should_wake(osb) ||
3835 kthread_should_stop());
3836
3837 mlog(0, "downconvert_thread: awoken\n");
3838
3839 ocfs2_downconvert_thread_do_work(osb);
3840 }
3841
3842 osb->dc_task = NULL;
3843 return status;
3844}
3845
3846void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3847{
3848 spin_lock(&osb->dc_task_lock);
3849 /* make sure the voting thread gets a swipe at whatever changes
3850 * the caller may have made to the voting state */
3851 osb->dc_wake_sequence++;
3852 spin_unlock(&osb->dc_task_lock);
3853 wake_up(&osb->dc_event);
3854}