blob: 83d2ddb271864b3b56a7844305a6654eda3c31fc [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
Srinivas Eeda83273932009-06-03 17:02:55 -0700251static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
252 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
253};
254
Mark Fashehd680efe2006-09-08 14:14:34 -0700255static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700256 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700257 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700258 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700259 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700260};
261
Tiger Yang50008632007-03-20 16:01:38 -0700262static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
263 .get_osb = ocfs2_get_inode_osb,
264 .flags = 0,
265};
266
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800267static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
268 .get_osb = ocfs2_get_file_osb,
269 .flags = 0,
270};
271
Jan Kara9e33d692008-08-25 19:56:50 +0200272static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
273 .set_lvb = ocfs2_set_qinfo_lvb,
274 .get_osb = ocfs2_get_qinfo_osb,
275 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
276};
277
Mark Fashehccd979b2005-12-15 14:31:24 -0800278static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
279{
280 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700281 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
282 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800283}
284
Mark Fashehccd979b2005-12-15 14:31:24 -0800285static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
286{
287 BUG_ON(!ocfs2_is_inode_lock(lockres));
288
289 return (struct inode *) lockres->l_priv;
290}
291
Mark Fashehd680efe2006-09-08 14:14:34 -0700292static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
293{
294 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
295
296 return (struct ocfs2_dentry_lock *)lockres->l_priv;
297}
298
Jan Kara9e33d692008-08-25 19:56:50 +0200299static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
300{
301 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
302
303 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
304}
305
Mark Fasheh54a7e752006-09-12 21:49:13 -0700306static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
307{
308 if (lockres->l_ops->get_osb)
309 return lockres->l_ops->get_osb(lockres);
310
311 return (struct ocfs2_super *)lockres->l_priv;
312}
313
Mark Fashehccd979b2005-12-15 14:31:24 -0800314static int ocfs2_lock_create(struct ocfs2_super *osb,
315 struct ocfs2_lock_res *lockres,
316 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800317 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800318static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
319 int wanted);
320static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres,
322 int level);
323static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
324static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
325static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
326static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
327static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
328 struct ocfs2_lock_res *lockres);
329static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
330 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800331#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
332 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
333 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
334 _err, _func, _lockres->l_name); \
335 else \
336 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
337 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
338 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800339} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700340static int ocfs2_downconvert_thread(void *arg);
341static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
342 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700343static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800344 struct buffer_head **bh);
345static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
346static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800347static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
348 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800349static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
350 struct ocfs2_lock_res *lockres,
351 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800352 int lvb,
353 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800354static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres);
356static int ocfs2_cancel_convert(struct ocfs2_super *osb,
357 struct ocfs2_lock_res *lockres);
358
Mark Fashehccd979b2005-12-15 14:31:24 -0800359
Mark Fashehccd979b2005-12-15 14:31:24 -0800360static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
361 u64 blkno,
362 u32 generation,
363 char *name)
364{
365 int len;
366
367 mlog_entry_void();
368
369 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
370
Mark Fashehb0697052006-03-03 10:24:33 -0800371 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
372 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
373 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800374
375 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
376
377 mlog(0, "built lock resource with name: %s\n", name);
378
379 mlog_exit_void();
380}
381
Ingo Molnar34af9462006-06-27 02:53:55 -0700382static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800383
384static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
385 struct ocfs2_dlm_debug *dlm_debug)
386{
387 mlog(0, "Add tracking for lockres %s\n", res->l_name);
388
389 spin_lock(&ocfs2_dlm_tracking_lock);
390 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
391 spin_unlock(&ocfs2_dlm_tracking_lock);
392}
393
394static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
395{
396 spin_lock(&ocfs2_dlm_tracking_lock);
397 if (!list_empty(&res->l_debug_list))
398 list_del_init(&res->l_debug_list);
399 spin_unlock(&ocfs2_dlm_tracking_lock);
400}
401
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700402#ifdef CONFIG_OCFS2_FS_STATS
403static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
404{
405 res->l_lock_num_prmode = 0;
406 res->l_lock_num_prmode_failed = 0;
407 res->l_lock_total_prmode = 0;
408 res->l_lock_max_prmode = 0;
409 res->l_lock_num_exmode = 0;
410 res->l_lock_num_exmode_failed = 0;
411 res->l_lock_total_exmode = 0;
412 res->l_lock_max_exmode = 0;
413 res->l_lock_refresh = 0;
414}
415
416static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
417 struct ocfs2_mask_waiter *mw, int ret)
418{
419 unsigned long long *num, *sum;
420 unsigned int *max, *failed;
421 struct timespec ts = current_kernel_time();
422 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
423
424 if (level == LKM_PRMODE) {
425 num = &res->l_lock_num_prmode;
426 sum = &res->l_lock_total_prmode;
427 max = &res->l_lock_max_prmode;
428 failed = &res->l_lock_num_prmode_failed;
429 } else if (level == LKM_EXMODE) {
430 num = &res->l_lock_num_exmode;
431 sum = &res->l_lock_total_exmode;
432 max = &res->l_lock_max_exmode;
433 failed = &res->l_lock_num_exmode_failed;
434 } else
435 return;
436
437 (*num)++;
438 (*sum) += time;
439 if (time > *max)
440 *max = time;
441 if (ret)
442 (*failed)++;
443}
444
445static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
446{
447 lockres->l_lock_refresh++;
448}
449
450static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
451{
452 struct timespec ts = current_kernel_time();
453 mw->mw_lock_start = timespec_to_ns(&ts);
454}
455#else
456static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
457{
458}
459static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
460 int level, struct ocfs2_mask_waiter *mw, int ret)
461{
462}
463static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
464{
465}
466static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
467{
468}
469#endif
470
Mark Fashehccd979b2005-12-15 14:31:24 -0800471static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
472 struct ocfs2_lock_res *res,
473 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800474 struct ocfs2_lock_res_ops *ops,
475 void *priv)
476{
Mark Fashehccd979b2005-12-15 14:31:24 -0800477 res->l_type = type;
478 res->l_ops = ops;
479 res->l_priv = priv;
480
Joel Beckerbd3e7612008-02-01 12:14:57 -0800481 res->l_level = DLM_LOCK_IV;
482 res->l_requested = DLM_LOCK_IV;
483 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800484 res->l_action = OCFS2_AST_INVALID;
485 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
486
487 res->l_flags = OCFS2_LOCK_INITIALIZED;
488
489 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700490
491 ocfs2_init_lock_stats(res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800492}
493
494void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
495{
496 /* This also clears out the lock status block */
497 memset(res, 0, sizeof(struct ocfs2_lock_res));
498 spin_lock_init(&res->l_lock);
499 init_waitqueue_head(&res->l_event);
500 INIT_LIST_HEAD(&res->l_blocked_list);
501 INIT_LIST_HEAD(&res->l_mask_waiters);
502}
503
504void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
505 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700506 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 struct inode *inode)
508{
509 struct ocfs2_lock_res_ops *ops;
510
511 switch(type) {
512 case OCFS2_LOCK_TYPE_RW:
513 ops = &ocfs2_inode_rw_lops;
514 break;
515 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700516 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 break;
Tiger Yang50008632007-03-20 16:01:38 -0700518 case OCFS2_LOCK_TYPE_OPEN:
519 ops = &ocfs2_inode_open_lops;
520 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800521 default:
522 mlog_bug_on_msg(1, "type: %d\n", type);
523 ops = NULL; /* thanks, gcc */
524 break;
525 };
526
Mark Fashehd680efe2006-09-08 14:14:34 -0700527 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700528 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700529 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
530}
531
Mark Fasheh54a7e752006-09-12 21:49:13 -0700532static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
533{
534 struct inode *inode = ocfs2_lock_res_inode(lockres);
535
536 return OCFS2_SB(inode->i_sb);
537}
538
Jan Kara9e33d692008-08-25 19:56:50 +0200539static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
540{
541 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
542
543 return OCFS2_SB(info->dqi_gi.dqi_sb);
544}
545
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800546static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
547{
548 struct ocfs2_file_private *fp = lockres->l_priv;
549
550 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
551}
552
Mark Fashehd680efe2006-09-08 14:14:34 -0700553static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
554{
555 __be64 inode_blkno_be;
556
557 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
558 sizeof(__be64));
559
560 return be64_to_cpu(inode_blkno_be);
561}
562
Mark Fasheh54a7e752006-09-12 21:49:13 -0700563static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
564{
565 struct ocfs2_dentry_lock *dl = lockres->l_priv;
566
567 return OCFS2_SB(dl->dl_inode->i_sb);
568}
569
Mark Fashehd680efe2006-09-08 14:14:34 -0700570void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
571 u64 parent, struct inode *inode)
572{
573 int len;
574 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
575 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
576 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
577
578 ocfs2_lock_res_init_once(lockres);
579
580 /*
581 * Unfortunately, the standard lock naming scheme won't work
582 * here because we have two 16 byte values to use. Instead,
583 * we'll stuff the inode number as a binary value. We still
584 * want error prints to show something without garbling the
585 * display, so drop a null byte in there before the inode
586 * number. A future version of OCFS2 will likely use all
587 * binary lock names. The stringified names have been a
588 * tremendous aid in debugging, but now that the debugfs
589 * interface exists, we can mangle things there if need be.
590 *
591 * NOTE: We also drop the standard "pad" value (the total lock
592 * name size stays the same though - the last part is all
593 * zeros due to the memset in ocfs2_lock_res_init_once()
594 */
595 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
596 "%c%016llx",
597 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
598 (long long)parent);
599
600 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
601
602 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
603 sizeof(__be64));
604
605 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
606 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
607 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800608}
609
610static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
611 struct ocfs2_super *osb)
612{
613 /* Superblock lockres doesn't come from a slab so we call init
614 * once on it manually. */
615 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700616 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
617 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800619 &ocfs2_super_lops, osb);
620}
621
622static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
623 struct ocfs2_super *osb)
624{
625 /* Rename lockres doesn't come from a slab so we call init
626 * once on it manually. */
627 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700628 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
629 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 &ocfs2_rename_lops, osb);
631}
632
wengang wang6ca497a2009-03-06 21:29:10 +0800633static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
634 struct ocfs2_super *osb)
635{
636 /* nfs_sync lockres doesn't come from a slab so we call init
637 * once on it manually. */
638 ocfs2_lock_res_init_once(res);
639 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
640 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
641 &ocfs2_nfs_sync_lops, osb);
642}
643
Srinivas Eeda83273932009-06-03 17:02:55 -0700644static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
645 struct ocfs2_super *osb)
646{
647 struct ocfs2_orphan_scan_lvb *lvb;
648
649 ocfs2_lock_res_init_once(res);
650 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
651 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
652 &ocfs2_orphan_scan_lops, osb);
653 lvb = ocfs2_dlm_lvb(&res->l_lksb);
654 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
655}
656
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800657void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
658 struct ocfs2_file_private *fp)
659{
660 struct inode *inode = fp->fp_file->f_mapping->host;
661 struct ocfs2_inode_info *oi = OCFS2_I(inode);
662
663 ocfs2_lock_res_init_once(lockres);
664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
665 inode->i_generation, lockres->l_name);
666 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
667 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
668 fp);
669 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
670}
671
Jan Kara9e33d692008-08-25 19:56:50 +0200672void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
673 struct ocfs2_mem_dqinfo *info)
674{
675 ocfs2_lock_res_init_once(lockres);
676 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
677 0, lockres->l_name);
678 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
679 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
680 info);
681}
682
Mark Fashehccd979b2005-12-15 14:31:24 -0800683void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
684{
685 mlog_entry_void();
686
687 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
688 return;
689
690 ocfs2_remove_lockres_tracking(res);
691
692 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
693 "Lockres %s is on the blocked list\n",
694 res->l_name);
695 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
696 "Lockres %s has mask waiters pending\n",
697 res->l_name);
698 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
699 "Lockres %s is locked\n",
700 res->l_name);
701 mlog_bug_on_msg(res->l_ro_holders,
702 "Lockres %s has %u ro holders\n",
703 res->l_name, res->l_ro_holders);
704 mlog_bug_on_msg(res->l_ex_holders,
705 "Lockres %s has %u ex holders\n",
706 res->l_name, res->l_ex_holders);
707
708 /* Need to clear out the lock status block for the dlm */
709 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
710
711 res->l_flags = 0UL;
712 mlog_exit_void();
713}
714
715static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
716 int level)
717{
718 mlog_entry_void();
719
720 BUG_ON(!lockres);
721
722 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800723 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800724 lockres->l_ex_holders++;
725 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800726 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800727 lockres->l_ro_holders++;
728 break;
729 default:
730 BUG();
731 }
732
733 mlog_exit_void();
734}
735
736static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
737 int level)
738{
739 mlog_entry_void();
740
741 BUG_ON(!lockres);
742
743 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800744 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 BUG_ON(!lockres->l_ex_holders);
746 lockres->l_ex_holders--;
747 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800748 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 BUG_ON(!lockres->l_ro_holders);
750 lockres->l_ro_holders--;
751 break;
752 default:
753 BUG();
754 }
755 mlog_exit_void();
756}
757
758/* WARNING: This function lives in a world where the only three lock
759 * levels are EX, PR, and NL. It *will* have to be adjusted when more
760 * lock types are added. */
761static inline int ocfs2_highest_compat_lock_level(int level)
762{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800763 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800764
Joel Beckerbd3e7612008-02-01 12:14:57 -0800765 if (level == DLM_LOCK_EX)
766 new_level = DLM_LOCK_NL;
767 else if (level == DLM_LOCK_PR)
768 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 return new_level;
770}
771
772static void lockres_set_flags(struct ocfs2_lock_res *lockres,
773 unsigned long newflags)
774{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200775 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800776
777 assert_spin_locked(&lockres->l_lock);
778
779 lockres->l_flags = newflags;
780
Christoph Hellwig800deef2007-05-17 16:03:13 +0200781 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800782 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
783 continue;
784
785 list_del_init(&mw->mw_item);
786 mw->mw_status = 0;
787 complete(&mw->mw_complete);
788 }
789}
790static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
791{
792 lockres_set_flags(lockres, lockres->l_flags | or);
793}
794static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
795 unsigned long clear)
796{
797 lockres_set_flags(lockres, lockres->l_flags & ~clear);
798}
799
800static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
801{
802 mlog_entry_void();
803
804 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
805 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
806 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800807 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800808
809 lockres->l_level = lockres->l_requested;
810 if (lockres->l_level <=
811 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800812 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800813 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
814 }
815 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
816
817 mlog_exit_void();
818}
819
820static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
821{
822 mlog_entry_void();
823
824 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
825 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
826
827 /* Convert from RO to EX doesn't really need anything as our
828 * information is already up to data. Convert from NL to
829 * *anything* however should mark ourselves as needing an
830 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800831 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700832 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800833 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
834
835 lockres->l_level = lockres->l_requested;
836 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
837
838 mlog_exit_void();
839}
840
841static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
842{
843 mlog_entry_void();
844
Roel Kluin3cf0c502007-10-27 00:20:36 +0200845 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800846 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
847
Joel Beckerbd3e7612008-02-01 12:14:57 -0800848 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700849 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
850 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
852
853 lockres->l_level = lockres->l_requested;
854 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
855 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
856
857 mlog_exit_void();
858}
859
Mark Fashehccd979b2005-12-15 14:31:24 -0800860static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
861 int level)
862{
863 int needs_downconvert = 0;
864 mlog_entry_void();
865
866 assert_spin_locked(&lockres->l_lock);
867
868 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
869
870 if (level > lockres->l_blocking) {
871 /* only schedule a downconvert if we haven't already scheduled
872 * one that goes low enough to satisfy the level we're
873 * blocking. this also catches the case where we get
874 * duplicate BASTs */
875 if (ocfs2_highest_compat_lock_level(level) <
876 ocfs2_highest_compat_lock_level(lockres->l_blocking))
877 needs_downconvert = 1;
878
879 lockres->l_blocking = level;
880 }
881
882 mlog_exit(needs_downconvert);
883 return needs_downconvert;
884}
885
Joel Beckerde551242008-02-01 14:45:08 -0800886/*
887 * OCFS2_LOCK_PENDING and l_pending_gen.
888 *
889 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
890 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
891 * for more details on the race.
892 *
893 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
894 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
895 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
896 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
897 * the caller is going to try to clear PENDING again. If nothing else is
898 * happening, __lockres_clear_pending() sees PENDING is unset and does
899 * nothing.
900 *
901 * But what if another path (eg downconvert thread) has just started a
902 * new locking action? The other path has re-set PENDING. Our path
903 * cannot clear PENDING, because that will re-open the original race
904 * window.
905 *
906 * [Example]
907 *
908 * ocfs2_meta_lock()
909 * ocfs2_cluster_lock()
910 * set BUSY
911 * set PENDING
912 * drop l_lock
913 * ocfs2_dlm_lock()
914 * ocfs2_locking_ast() ocfs2_downconvert_thread()
915 * clear PENDING ocfs2_unblock_lock()
916 * take_l_lock
917 * !BUSY
918 * ocfs2_prepare_downconvert()
919 * set BUSY
920 * set PENDING
921 * drop l_lock
922 * take l_lock
923 * clear PENDING
924 * drop l_lock
925 * <window>
926 * ocfs2_dlm_lock()
927 *
928 * So as you can see, we now have a window where l_lock is not held,
929 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
930 *
931 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
932 * set by ocfs2_prepare_downconvert(). That wasn't nice.
933 *
934 * To solve this we introduce l_pending_gen. A call to
935 * lockres_clear_pending() will only do so when it is passed a generation
936 * number that matches the lockres. lockres_set_pending() will return the
937 * current generation number. When ocfs2_cluster_lock() goes to clear
938 * PENDING, it passes the generation it got from set_pending(). In our
939 * example above, the generation numbers will *not* match. Thus,
940 * ocfs2_cluster_lock() will not clear the PENDING set by
941 * ocfs2_prepare_downconvert().
942 */
943
944/* Unlocked version for ocfs2_locking_ast() */
945static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
946 unsigned int generation,
947 struct ocfs2_super *osb)
948{
949 assert_spin_locked(&lockres->l_lock);
950
951 /*
952 * The ast and locking functions can race us here. The winner
953 * will clear pending, the loser will not.
954 */
955 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
956 (lockres->l_pending_gen != generation))
957 return;
958
959 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
960 lockres->l_pending_gen++;
961
962 /*
963 * The downconvert thread may have skipped us because we
964 * were PENDING. Wake it up.
965 */
966 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
967 ocfs2_wake_downconvert_thread(osb);
968}
969
970/* Locked version for callers of ocfs2_dlm_lock() */
971static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
972 unsigned int generation,
973 struct ocfs2_super *osb)
974{
975 unsigned long flags;
976
977 spin_lock_irqsave(&lockres->l_lock, flags);
978 __lockres_clear_pending(lockres, generation, osb);
979 spin_unlock_irqrestore(&lockres->l_lock, flags);
980}
981
982static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
983{
984 assert_spin_locked(&lockres->l_lock);
985 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
986
987 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
988
989 return lockres->l_pending_gen;
990}
991
992
Mark Fashehaa2623a2006-09-12 21:58:23 -0700993static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800994{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700995 struct ocfs2_lock_res *lockres = opaque;
996 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 int needs_downconvert;
998 unsigned long flags;
999
Joel Beckerbd3e7612008-02-01 12:14:57 -08001000 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001001
Mark Fashehaa2623a2006-09-12 21:58:23 -07001002 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1003 lockres->l_name, level, lockres->l_level,
1004 ocfs2_lock_type_string(lockres->l_type));
1005
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001006 /*
1007 * We can skip the bast for locks which don't enable caching -
1008 * they'll be dropped at the earliest possible time anyway.
1009 */
1010 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1011 return;
1012
Mark Fashehccd979b2005-12-15 14:31:24 -08001013 spin_lock_irqsave(&lockres->l_lock, flags);
1014 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1015 if (needs_downconvert)
1016 ocfs2_schedule_blocked_lock(osb, lockres);
1017 spin_unlock_irqrestore(&lockres->l_lock, flags);
1018
Mark Fashehd680efe2006-09-08 14:14:34 -07001019 wake_up(&lockres->l_event);
1020
Mark Fasheh34d024f2007-09-24 15:56:19 -07001021 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001022}
1023
Mark Fashehe92d57d2006-09-12 21:34:35 -07001024static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -08001025{
Mark Fashehe92d57d2006-09-12 21:34:35 -07001026 struct ocfs2_lock_res *lockres = opaque;
Joel Beckerde551242008-02-01 14:45:08 -08001027 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001029 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001030
1031 spin_lock_irqsave(&lockres->l_lock, flags);
1032
David Teigland1693a5c2008-01-30 16:52:53 -08001033 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1034
1035 if (status == -EAGAIN) {
1036 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1037 goto out;
1038 }
1039
1040 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001041 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001042 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 spin_unlock_irqrestore(&lockres->l_lock, flags);
1044 return;
1045 }
1046
1047 switch(lockres->l_action) {
1048 case OCFS2_AST_ATTACH:
1049 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001050 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 break;
1052 case OCFS2_AST_CONVERT:
1053 ocfs2_generic_handle_convert_action(lockres);
1054 break;
1055 case OCFS2_AST_DOWNCONVERT:
1056 ocfs2_generic_handle_downconvert_action(lockres);
1057 break;
1058 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001059 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1060 "lockres flags = 0x%lx, unlock action: %u\n",
1061 lockres->l_name, lockres->l_action, lockres->l_flags,
1062 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 BUG();
1064 }
David Teigland1693a5c2008-01-30 16:52:53 -08001065out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 /* set it to something invalid so if we get called again we
1067 * can catch it. */
1068 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001069
Joel Beckerde551242008-02-01 14:45:08 -08001070 /* Did we try to cancel this lock? Clear that state */
1071 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1072 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1073
1074 /*
1075 * We may have beaten the locking functions here. We certainly
1076 * know that dlm_lock() has been called :-)
1077 * Because we can't have two lock calls in flight at once, we
1078 * can use lockres->l_pending_gen.
1079 */
1080 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1081
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001083 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001084}
1085
Mark Fashehccd979b2005-12-15 14:31:24 -08001086static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1087 int convert)
1088{
1089 unsigned long flags;
1090
1091 mlog_entry_void();
1092 spin_lock_irqsave(&lockres->l_lock, flags);
1093 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1094 if (convert)
1095 lockres->l_action = OCFS2_AST_INVALID;
1096 else
1097 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1098 spin_unlock_irqrestore(&lockres->l_lock, flags);
1099
1100 wake_up(&lockres->l_event);
1101 mlog_exit_void();
1102}
1103
1104/* Note: If we detect another process working on the lock (i.e.,
1105 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1106 * to do the right thing in that case.
1107 */
1108static int ocfs2_lock_create(struct ocfs2_super *osb,
1109 struct ocfs2_lock_res *lockres,
1110 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001111 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001112{
1113 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001114 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001115 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001116
1117 mlog_entry_void();
1118
Joel Beckerbd3e7612008-02-01 12:14:57 -08001119 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 dlm_flags);
1121
1122 spin_lock_irqsave(&lockres->l_lock, flags);
1123 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1124 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1125 spin_unlock_irqrestore(&lockres->l_lock, flags);
1126 goto bail;
1127 }
1128
1129 lockres->l_action = OCFS2_AST_ATTACH;
1130 lockres->l_requested = level;
1131 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001132 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 spin_unlock_irqrestore(&lockres->l_lock, flags);
1134
Joel Becker4670c462008-02-01 14:39:35 -08001135 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001136 level,
1137 &lockres->l_lksb,
1138 dlm_flags,
1139 lockres->l_name,
1140 OCFS2_LOCK_ID_MAX_LEN - 1,
1141 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001142 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001143 if (ret) {
1144 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 ocfs2_recover_from_dlm_error(lockres, 1);
1146 }
1147
Joel Becker7431cd72008-02-01 12:15:37 -08001148 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001149
1150bail:
1151 mlog_exit(ret);
1152 return ret;
1153}
1154
1155static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1156 int flag)
1157{
1158 unsigned long flags;
1159 int ret;
1160
1161 spin_lock_irqsave(&lockres->l_lock, flags);
1162 ret = lockres->l_flags & flag;
1163 spin_unlock_irqrestore(&lockres->l_lock, flags);
1164
1165 return ret;
1166}
1167
1168static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1169
1170{
1171 wait_event(lockres->l_event,
1172 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1173}
1174
1175static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1176
1177{
1178 wait_event(lockres->l_event,
1179 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1180}
1181
1182/* predict what lock level we'll be dropping down to on behalf
1183 * of another node, and return true if the currently wanted
1184 * level will be compatible with it. */
1185static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1186 int wanted)
1187{
1188 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1189
1190 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1191}
1192
1193static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1194{
1195 INIT_LIST_HEAD(&mw->mw_item);
1196 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001197 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198}
1199
1200static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1201{
1202 wait_for_completion(&mw->mw_complete);
1203 /* Re-arm the completion in case we want to wait on it again */
1204 INIT_COMPLETION(mw->mw_complete);
1205 return mw->mw_status;
1206}
1207
1208static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1209 struct ocfs2_mask_waiter *mw,
1210 unsigned long mask,
1211 unsigned long goal)
1212{
1213 BUG_ON(!list_empty(&mw->mw_item));
1214
1215 assert_spin_locked(&lockres->l_lock);
1216
1217 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1218 mw->mw_mask = mask;
1219 mw->mw_goal = goal;
1220}
1221
1222/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1223 * if the mask still hadn't reached its goal */
1224static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1225 struct ocfs2_mask_waiter *mw)
1226{
1227 unsigned long flags;
1228 int ret = 0;
1229
1230 spin_lock_irqsave(&lockres->l_lock, flags);
1231 if (!list_empty(&mw->mw_item)) {
1232 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1233 ret = -EBUSY;
1234
1235 list_del_init(&mw->mw_item);
1236 init_completion(&mw->mw_complete);
1237 }
1238 spin_unlock_irqrestore(&lockres->l_lock, flags);
1239
1240 return ret;
1241
1242}
1243
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001244static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1245 struct ocfs2_lock_res *lockres)
1246{
1247 int ret;
1248
1249 ret = wait_for_completion_interruptible(&mw->mw_complete);
1250 if (ret)
1251 lockres_remove_mask_waiter(lockres, mw);
1252 else
1253 ret = mw->mw_status;
1254 /* Re-arm the completion in case we want to wait on it again */
1255 INIT_COMPLETION(mw->mw_complete);
1256 return ret;
1257}
1258
Mark Fashehccd979b2005-12-15 14:31:24 -08001259static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1260 struct ocfs2_lock_res *lockres,
1261 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001262 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 int arg_flags)
1264{
1265 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1267 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1268 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001269 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001270 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001271
1272 mlog_entry_void();
1273
1274 ocfs2_init_mask_waiter(&mw);
1275
Mark Fashehb80fc012006-09-12 22:08:14 -07001276 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001277 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001278
Mark Fashehccd979b2005-12-15 14:31:24 -08001279again:
1280 wait = 0;
1281
1282 if (catch_signals && signal_pending(current)) {
1283 ret = -ERESTARTSYS;
1284 goto out;
1285 }
1286
1287 spin_lock_irqsave(&lockres->l_lock, flags);
1288
1289 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1290 "Cluster lock called on freeing lockres %s! flags "
1291 "0x%lx\n", lockres->l_name, lockres->l_flags);
1292
1293 /* We only compare against the currently granted level
1294 * here. If the lock is blocked waiting on a downconvert,
1295 * we'll get caught below. */
1296 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1297 level > lockres->l_level) {
1298 /* is someone sitting in dlm_lock? If so, wait on
1299 * them. */
1300 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1301 wait = 1;
1302 goto unlock;
1303 }
1304
Mark Fashehccd979b2005-12-15 14:31:24 -08001305 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1306 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1307 /* is the lock is currently blocked on behalf of
1308 * another node */
1309 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1310 wait = 1;
1311 goto unlock;
1312 }
1313
1314 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001315 if (noqueue_attempted > 0) {
1316 ret = -EAGAIN;
1317 goto unlock;
1318 }
1319 if (lkm_flags & DLM_LKF_NOQUEUE)
1320 noqueue_attempted = 1;
1321
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 if (lockres->l_action != OCFS2_AST_INVALID)
1323 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1324 lockres->l_name, lockres->l_action);
1325
Mark Fasheh019d1b22007-10-05 12:09:05 -07001326 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1327 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001328 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001329 } else {
1330 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001331 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001332 }
1333
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 lockres->l_requested = level;
1335 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001336 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001337 spin_unlock_irqrestore(&lockres->l_lock, flags);
1338
Joel Beckerbd3e7612008-02-01 12:14:57 -08001339 BUG_ON(level == DLM_LOCK_IV);
1340 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341
1342 mlog(0, "lock %s, convert from %d to level = %d\n",
1343 lockres->l_name, lockres->l_level, level);
1344
1345 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001346 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001347 level,
1348 &lockres->l_lksb,
1349 lkm_flags,
1350 lockres->l_name,
1351 OCFS2_LOCK_ID_MAX_LEN - 1,
1352 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001353 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001354 if (ret) {
1355 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1356 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001357 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001358 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001359 }
1360 ocfs2_recover_from_dlm_error(lockres, 1);
1361 goto out;
1362 }
1363
Coly Li73ac36e2009-01-07 18:09:16 -08001364 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001365 lockres->l_name);
1366
1367 /* At this point we've gone inside the dlm and need to
1368 * complete our work regardless. */
1369 catch_signals = 0;
1370
1371 /* wait for busy to clear and carry on */
1372 goto again;
1373 }
1374
1375 /* Ok, if we get here then we're good to go. */
1376 ocfs2_inc_holders(lockres, level);
1377
1378 ret = 0;
1379unlock:
1380 spin_unlock_irqrestore(&lockres->l_lock, flags);
1381out:
1382 /*
1383 * This is helping work around a lock inversion between the page lock
1384 * and dlm locks. One path holds the page lock while calling aops
1385 * which block acquiring dlm locks. The voting thread holds dlm
1386 * locks while acquiring page locks while down converting data locks.
1387 * This block is helping an aop path notice the inversion and back
1388 * off to unlock its page lock before trying the dlm lock again.
1389 */
1390 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1391 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1392 wait = 0;
1393 if (lockres_remove_mask_waiter(lockres, &mw))
1394 ret = -EAGAIN;
1395 else
1396 goto again;
1397 }
1398 if (wait) {
1399 ret = ocfs2_wait_for_mask(&mw);
1400 if (ret == 0)
1401 goto again;
1402 mlog_errno(ret);
1403 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001404 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001405
1406 mlog_exit(ret);
1407 return ret;
1408}
1409
1410static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1411 struct ocfs2_lock_res *lockres,
1412 int level)
1413{
1414 unsigned long flags;
1415
1416 mlog_entry_void();
1417 spin_lock_irqsave(&lockres->l_lock, flags);
1418 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001419 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001420 spin_unlock_irqrestore(&lockres->l_lock, flags);
1421 mlog_exit_void();
1422}
1423
Adrian Bunkda661162006-11-20 03:24:28 +01001424static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1425 struct ocfs2_lock_res *lockres,
1426 int ex,
1427 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001428{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001429 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001431 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001432
1433 spin_lock_irqsave(&lockres->l_lock, flags);
1434 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1435 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1436 spin_unlock_irqrestore(&lockres->l_lock, flags);
1437
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001438 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001439}
1440
1441/* Grants us an EX lock on the data and metadata resources, skipping
1442 * the normal cluster directory lookup. Use this ONLY on newly created
1443 * inodes which other nodes can't possibly see, and which haven't been
1444 * hashed in the inode hash yet. This can give us a good performance
1445 * increase as it'll skip the network broadcast normally associated
1446 * with creating a new lock resource. */
1447int ocfs2_create_new_inode_locks(struct inode *inode)
1448{
1449 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001450 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001451
1452 BUG_ON(!inode);
1453 BUG_ON(!ocfs2_inode_is_new(inode));
1454
1455 mlog_entry_void();
1456
Mark Fashehb0697052006-03-03 10:24:33 -08001457 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001458
1459 /* NOTE: That we don't increment any of the holder counts, nor
1460 * do we add anything to a journal handle. Since this is
1461 * supposed to be a new inode which the cluster doesn't know
1462 * about yet, there is no need to. As far as the LVB handling
1463 * is concerned, this is basically like acquiring an EX lock
1464 * on a resource which has an invalid one -- we'll set it
1465 * valid when we release the EX. */
1466
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001467 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 if (ret) {
1469 mlog_errno(ret);
1470 goto bail;
1471 }
1472
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001473 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001474 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001475 * don't use a generation in their lock names.
1476 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001477 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001478 if (ret) {
1479 mlog_errno(ret);
1480 goto bail;
1481 }
1482
Tiger Yang50008632007-03-20 16:01:38 -07001483 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1484 if (ret) {
1485 mlog_errno(ret);
1486 goto bail;
1487 }
1488
Mark Fashehccd979b2005-12-15 14:31:24 -08001489bail:
1490 mlog_exit(ret);
1491 return ret;
1492}
1493
1494int ocfs2_rw_lock(struct inode *inode, int write)
1495{
1496 int status, level;
1497 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001498 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001499
1500 BUG_ON(!inode);
1501
1502 mlog_entry_void();
1503
Mark Fashehb0697052006-03-03 10:24:33 -08001504 mlog(0, "inode %llu take %s RW lock\n",
1505 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001506 write ? "EXMODE" : "PRMODE");
1507
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001508 if (ocfs2_mount_local(osb))
1509 return 0;
1510
Mark Fashehccd979b2005-12-15 14:31:24 -08001511 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1512
Joel Beckerbd3e7612008-02-01 12:14:57 -08001513 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001514
1515 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1516 0);
1517 if (status < 0)
1518 mlog_errno(status);
1519
1520 mlog_exit(status);
1521 return status;
1522}
1523
1524void ocfs2_rw_unlock(struct inode *inode, int write)
1525{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001526 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001527 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001528 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001529
1530 mlog_entry_void();
1531
Mark Fashehb0697052006-03-03 10:24:33 -08001532 mlog(0, "inode %llu drop %s RW lock\n",
1533 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 write ? "EXMODE" : "PRMODE");
1535
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001536 if (!ocfs2_mount_local(osb))
1537 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001538
1539 mlog_exit_void();
1540}
1541
Tiger Yang50008632007-03-20 16:01:38 -07001542/*
1543 * ocfs2_open_lock always get PR mode lock.
1544 */
1545int ocfs2_open_lock(struct inode *inode)
1546{
1547 int status = 0;
1548 struct ocfs2_lock_res *lockres;
1549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1550
1551 BUG_ON(!inode);
1552
1553 mlog_entry_void();
1554
1555 mlog(0, "inode %llu take PRMODE open lock\n",
1556 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1557
1558 if (ocfs2_mount_local(osb))
1559 goto out;
1560
1561 lockres = &OCFS2_I(inode)->ip_open_lockres;
1562
1563 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001564 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001565 if (status < 0)
1566 mlog_errno(status);
1567
1568out:
1569 mlog_exit(status);
1570 return status;
1571}
1572
1573int ocfs2_try_open_lock(struct inode *inode, int write)
1574{
1575 int status = 0, level;
1576 struct ocfs2_lock_res *lockres;
1577 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1578
1579 BUG_ON(!inode);
1580
1581 mlog_entry_void();
1582
1583 mlog(0, "inode %llu try to take %s open lock\n",
1584 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1585 write ? "EXMODE" : "PRMODE");
1586
1587 if (ocfs2_mount_local(osb))
1588 goto out;
1589
1590 lockres = &OCFS2_I(inode)->ip_open_lockres;
1591
Joel Beckerbd3e7612008-02-01 12:14:57 -08001592 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001593
1594 /*
1595 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001596 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001597 * other nodes and the -EAGAIN will indicate to the caller that
1598 * this inode is still in use.
1599 */
1600 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001601 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001602
1603out:
1604 mlog_exit(status);
1605 return status;
1606}
1607
1608/*
1609 * ocfs2_open_unlock unlock PR and EX mode open locks.
1610 */
1611void ocfs2_open_unlock(struct inode *inode)
1612{
1613 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1614 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1615
1616 mlog_entry_void();
1617
1618 mlog(0, "inode %llu drop open lock\n",
1619 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1620
1621 if (ocfs2_mount_local(osb))
1622 goto out;
1623
1624 if(lockres->l_ro_holders)
1625 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001626 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001627 if(lockres->l_ex_holders)
1628 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001629 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001630
1631out:
1632 mlog_exit_void();
1633}
1634
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001635static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1636 int level)
1637{
1638 int ret;
1639 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1640 unsigned long flags;
1641 struct ocfs2_mask_waiter mw;
1642
1643 ocfs2_init_mask_waiter(&mw);
1644
1645retry_cancel:
1646 spin_lock_irqsave(&lockres->l_lock, flags);
1647 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1648 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1649 if (ret) {
1650 spin_unlock_irqrestore(&lockres->l_lock, flags);
1651 ret = ocfs2_cancel_convert(osb, lockres);
1652 if (ret < 0) {
1653 mlog_errno(ret);
1654 goto out;
1655 }
1656 goto retry_cancel;
1657 }
1658 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1659 spin_unlock_irqrestore(&lockres->l_lock, flags);
1660
1661 ocfs2_wait_for_mask(&mw);
1662 goto retry_cancel;
1663 }
1664
1665 ret = -ERESTARTSYS;
1666 /*
1667 * We may still have gotten the lock, in which case there's no
1668 * point to restarting the syscall.
1669 */
1670 if (lockres->l_level == level)
1671 ret = 0;
1672
1673 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1674 lockres->l_flags, lockres->l_level, lockres->l_action);
1675
1676 spin_unlock_irqrestore(&lockres->l_lock, flags);
1677
1678out:
1679 return ret;
1680}
1681
1682/*
1683 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1684 * flock() calls. The locking approach this requires is sufficiently
1685 * different from all other cluster lock types that we implement a
1686 * seperate path to the "low-level" dlm calls. In particular:
1687 *
1688 * - No optimization of lock levels is done - we take at exactly
1689 * what's been requested.
1690 *
1691 * - No lock caching is employed. We immediately downconvert to
1692 * no-lock at unlock time. This also means flock locks never go on
1693 * the blocking list).
1694 *
1695 * - Since userspace can trivially deadlock itself with flock, we make
1696 * sure to allow cancellation of a misbehaving applications flock()
1697 * request.
1698 *
1699 * - Access to any flock lockres doesn't require concurrency, so we
1700 * can simplify the code by requiring the caller to guarantee
1701 * serialization of dlmglue flock calls.
1702 */
1703int ocfs2_file_lock(struct file *file, int ex, int trylock)
1704{
Mark Fashehe988cf12008-07-10 09:25:39 -07001705 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1706 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001707 unsigned long flags;
1708 struct ocfs2_file_private *fp = file->private_data;
1709 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1710 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1711 struct ocfs2_mask_waiter mw;
1712
1713 ocfs2_init_mask_waiter(&mw);
1714
1715 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001716 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001717 mlog(ML_ERROR,
1718 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1719 "level: %u\n", lockres->l_name, lockres->l_flags,
1720 lockres->l_level);
1721 return -EINVAL;
1722 }
1723
1724 spin_lock_irqsave(&lockres->l_lock, flags);
1725 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1726 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1727 spin_unlock_irqrestore(&lockres->l_lock, flags);
1728
1729 /*
1730 * Get the lock at NLMODE to start - that way we
1731 * can cancel the upconvert request if need be.
1732 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001733 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001734 if (ret < 0) {
1735 mlog_errno(ret);
1736 goto out;
1737 }
1738
1739 ret = ocfs2_wait_for_mask(&mw);
1740 if (ret) {
1741 mlog_errno(ret);
1742 goto out;
1743 }
1744 spin_lock_irqsave(&lockres->l_lock, flags);
1745 }
1746
1747 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001748 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001749 lockres->l_requested = level;
1750 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1751
1752 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1753 spin_unlock_irqrestore(&lockres->l_lock, flags);
1754
Joel Becker4670c462008-02-01 14:39:35 -08001755 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001756 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1757 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001758 if (ret) {
1759 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001760 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001761 ret = -EINVAL;
1762 }
1763
1764 ocfs2_recover_from_dlm_error(lockres, 1);
1765 lockres_remove_mask_waiter(lockres, &mw);
1766 goto out;
1767 }
1768
1769 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1770 if (ret == -ERESTARTSYS) {
1771 /*
1772 * Userspace can cause deadlock itself with
1773 * flock(). Current behavior locally is to allow the
1774 * deadlock, but abort the system call if a signal is
1775 * received. We follow this example, otherwise a
1776 * poorly written program could sit in kernel until
1777 * reboot.
1778 *
1779 * Handling this is a bit more complicated for Ocfs2
1780 * though. We can't exit this function with an
1781 * outstanding lock request, so a cancel convert is
1782 * required. We intentionally overwrite 'ret' - if the
1783 * cancel fails and the lock was granted, it's easier
1784 * to just bubble sucess back up to the user.
1785 */
1786 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001787 } else if (!ret && (level > lockres->l_level)) {
1788 /* Trylock failed asynchronously */
1789 BUG_ON(!trylock);
1790 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001791 }
1792
1793out:
1794
1795 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1796 lockres->l_name, ex, trylock, ret);
1797 return ret;
1798}
1799
1800void ocfs2_file_unlock(struct file *file)
1801{
1802 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001803 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001804 unsigned long flags;
1805 struct ocfs2_file_private *fp = file->private_data;
1806 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1807 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1808 struct ocfs2_mask_waiter mw;
1809
1810 ocfs2_init_mask_waiter(&mw);
1811
1812 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1813 return;
1814
Mark Fashehe988cf12008-07-10 09:25:39 -07001815 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001816 return;
1817
1818 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1819 lockres->l_name, lockres->l_flags, lockres->l_level,
1820 lockres->l_action);
1821
1822 spin_lock_irqsave(&lockres->l_lock, flags);
1823 /*
1824 * Fake a blocking ast for the downconvert code.
1825 */
1826 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001827 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001828
Mark Fashehe988cf12008-07-10 09:25:39 -07001829 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001830 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1831 spin_unlock_irqrestore(&lockres->l_lock, flags);
1832
Mark Fashehe988cf12008-07-10 09:25:39 -07001833 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001834 if (ret) {
1835 mlog_errno(ret);
1836 return;
1837 }
1838
1839 ret = ocfs2_wait_for_mask(&mw);
1840 if (ret)
1841 mlog_errno(ret);
1842}
1843
Mark Fasheh34d024f2007-09-24 15:56:19 -07001844static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1845 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001846{
1847 int kick = 0;
1848
1849 mlog_entry_void();
1850
1851 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001852 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001853 * condition. */
1854 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1855 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001856 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001857 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1858 kick = 1;
1859 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001860 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001861 if (!lockres->l_ex_holders)
1862 kick = 1;
1863 break;
1864 default:
1865 BUG();
1866 }
1867 }
1868
1869 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001870 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001871
1872 mlog_exit_void();
1873}
1874
Mark Fashehccd979b2005-12-15 14:31:24 -08001875#define OCFS2_SEC_BITS 34
1876#define OCFS2_SEC_SHIFT (64 - 34)
1877#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1878
1879/* LVB only has room for 64 bits of time here so we pack it for
1880 * now. */
1881static u64 ocfs2_pack_timespec(struct timespec *spec)
1882{
1883 u64 res;
1884 u64 sec = spec->tv_sec;
1885 u32 nsec = spec->tv_nsec;
1886
1887 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1888
1889 return res;
1890}
1891
1892/* Call this with the lockres locked. I am reasonably sure we don't
1893 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001894 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001895static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1896{
1897 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001898 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001899 struct ocfs2_meta_lvb *lvb;
1900
1901 mlog_entry_void();
1902
Mark Fasheha641dc22008-12-24 16:03:48 -08001903 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001904
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001905 /*
1906 * Invalidate the LVB of a deleted inode - this way other
1907 * nodes are forced to go to disk and discover the new inode
1908 * status.
1909 */
1910 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1911 lvb->lvb_version = 0;
1912 goto out;
1913 }
1914
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001915 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001916 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1917 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1918 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1919 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1920 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1921 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1922 lvb->lvb_iatime_packed =
1923 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1924 lvb->lvb_ictime_packed =
1925 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1926 lvb->lvb_imtime_packed =
1927 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001928 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001929 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001930 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001931
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001932out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 mlog_meta_lvb(0, lockres);
1934
1935 mlog_exit_void();
1936}
1937
1938static void ocfs2_unpack_timespec(struct timespec *spec,
1939 u64 packed_time)
1940{
1941 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1942 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1943}
1944
1945static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1946{
1947 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001948 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001949 struct ocfs2_meta_lvb *lvb;
1950
1951 mlog_entry_void();
1952
1953 mlog_meta_lvb(0, lockres);
1954
Mark Fasheha641dc22008-12-24 16:03:48 -08001955 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001956
1957 /* We're safe here without the lockres lock... */
1958 spin_lock(&oi->ip_lock);
1959 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1960 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1961
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001962 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001963 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001964 ocfs2_set_inode_flags(inode);
1965
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 /* fast-symlinks are a special case */
1967 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1968 inode->i_blocks = 0;
1969 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001970 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001971
1972 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1973 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1974 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1975 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1976 ocfs2_unpack_timespec(&inode->i_atime,
1977 be64_to_cpu(lvb->lvb_iatime_packed));
1978 ocfs2_unpack_timespec(&inode->i_mtime,
1979 be64_to_cpu(lvb->lvb_imtime_packed));
1980 ocfs2_unpack_timespec(&inode->i_ctime,
1981 be64_to_cpu(lvb->lvb_ictime_packed));
1982 spin_unlock(&oi->ip_lock);
1983
1984 mlog_exit_void();
1985}
1986
Mark Fashehf9e2d822006-09-12 15:35:49 -07001987static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1988 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001989{
Mark Fasheha641dc22008-12-24 16:03:48 -08001990 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001991
Joel Becker1c520df2009-06-19 15:14:13 -07001992 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
1993 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07001994 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001995 return 1;
1996 return 0;
1997}
1998
1999/* Determine whether a lock resource needs to be refreshed, and
2000 * arbitrate who gets to refresh it.
2001 *
2002 * 0 means no refresh needed.
2003 *
2004 * > 0 means you need to refresh this and you MUST call
2005 * ocfs2_complete_lock_res_refresh afterwards. */
2006static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2007{
2008 unsigned long flags;
2009 int status = 0;
2010
2011 mlog_entry_void();
2012
2013refresh_check:
2014 spin_lock_irqsave(&lockres->l_lock, flags);
2015 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2016 spin_unlock_irqrestore(&lockres->l_lock, flags);
2017 goto bail;
2018 }
2019
2020 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2021 spin_unlock_irqrestore(&lockres->l_lock, flags);
2022
2023 ocfs2_wait_on_refreshing_lock(lockres);
2024 goto refresh_check;
2025 }
2026
2027 /* Ok, I'll be the one to refresh this lock. */
2028 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2029 spin_unlock_irqrestore(&lockres->l_lock, flags);
2030
2031 status = 1;
2032bail:
2033 mlog_exit(status);
2034 return status;
2035}
2036
2037/* If status is non zero, I'll mark it as not being in refresh
2038 * anymroe, but i won't clear the needs refresh flag. */
2039static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2040 int status)
2041{
2042 unsigned long flags;
2043 mlog_entry_void();
2044
2045 spin_lock_irqsave(&lockres->l_lock, flags);
2046 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2047 if (!status)
2048 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2049 spin_unlock_irqrestore(&lockres->l_lock, flags);
2050
2051 wake_up(&lockres->l_event);
2052
2053 mlog_exit_void();
2054}
2055
2056/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002057static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002058 struct buffer_head **bh)
2059{
2060 int status = 0;
2061 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002062 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002063 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002064 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002065
2066 mlog_entry_void();
2067
Mark Fashehbe9e9862007-04-18 15:22:08 -07002068 if (ocfs2_mount_local(osb))
2069 goto bail;
2070
Mark Fashehccd979b2005-12-15 14:31:24 -08002071 spin_lock(&oi->ip_lock);
2072 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb0697052006-03-03 10:24:33 -08002073 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002074 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002075 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002076 spin_unlock(&oi->ip_lock);
2077 status = -ENOENT;
2078 goto bail;
2079 }
2080 spin_unlock(&oi->ip_lock);
2081
Mark Fashehbe9e9862007-04-18 15:22:08 -07002082 if (!ocfs2_should_refresh_lock_res(lockres))
2083 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002084
2085 /* This will discard any caching information we might have had
2086 * for the inode metadata. */
2087 ocfs2_metadata_cache_purge(inode);
2088
Mark Fasheh83418972007-04-23 18:53:12 -07002089 ocfs2_extent_map_trunc(inode, 0);
2090
Mark Fashehbe9e9862007-04-18 15:22:08 -07002091 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb0697052006-03-03 10:24:33 -08002092 mlog(0, "Trusting LVB on inode %llu\n",
2093 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002094 ocfs2_refresh_inode_from_lvb(inode);
2095 } else {
2096 /* Boo, we have to go to disk. */
2097 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002098 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002099 if (status < 0) {
2100 mlog_errno(status);
2101 goto bail_refresh;
2102 }
2103 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2104
2105 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002106 * locking an invalid object. ocfs2_read_inode_block()
2107 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002108 *
2109 * We bug on a stale inode here because we checked
2110 * above whether it was wiped from disk. The wiping
2111 * node provides a guarantee that we receive that
2112 * message and can mark the inode before dropping any
2113 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002114 mlog_bug_on_msg(inode->i_generation !=
2115 le32_to_cpu(fe->i_generation),
Mark Fashehb0697052006-03-03 10:24:33 -08002116 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002117 "inode->i_generation: %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002118 (unsigned long long)oi->ip_blkno,
2119 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002120 inode->i_generation);
2121 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2122 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb0697052006-03-03 10:24:33 -08002123 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2124 (unsigned long long)oi->ip_blkno,
2125 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002126 le32_to_cpu(fe->i_flags));
2127
2128 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002129 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002130 }
2131
2132 status = 0;
2133bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002134 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002135bail:
2136 mlog_exit(status);
2137 return status;
2138}
2139
2140static int ocfs2_assign_bh(struct inode *inode,
2141 struct buffer_head **ret_bh,
2142 struct buffer_head *passed_bh)
2143{
2144 int status;
2145
2146 if (passed_bh) {
2147 /* Ok, the update went to disk for us, use the
2148 * returned bh. */
2149 *ret_bh = passed_bh;
2150 get_bh(*ret_bh);
2151
2152 return 0;
2153 }
2154
Joel Beckerb657c952008-11-13 14:49:11 -08002155 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 if (status < 0)
2157 mlog_errno(status);
2158
2159 return status;
2160}
2161
2162/*
2163 * returns < 0 error if the callback will never be called, otherwise
2164 * the result of the lock will be communicated via the callback.
2165 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002166int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002167 struct buffer_head **ret_bh,
2168 int ex,
2169 int arg_flags)
2170{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002171 int status, level, acquired;
2172 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002173 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2175 struct buffer_head *local_bh = NULL;
2176
2177 BUG_ON(!inode);
2178
2179 mlog_entry_void();
2180
Mark Fashehb0697052006-03-03 10:24:33 -08002181 mlog(0, "inode %llu, take %s META lock\n",
2182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002183 ex ? "EXMODE" : "PRMODE");
2184
2185 status = 0;
2186 acquired = 0;
2187 /* We'll allow faking a readonly metadata lock for
2188 * rodevices. */
2189 if (ocfs2_is_hard_readonly(osb)) {
2190 if (ex)
2191 status = -EROFS;
2192 goto bail;
2193 }
2194
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002195 if (ocfs2_mount_local(osb))
2196 goto local;
2197
Mark Fashehccd979b2005-12-15 14:31:24 -08002198 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002199 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002200
Mark Fashehe63aecb62007-10-18 15:30:42 -07002201 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002202 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002203 dlm_flags = 0;
2204 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002205 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002206
2207 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2208 if (status < 0) {
2209 if (status != -EAGAIN && status != -EIOCBRETRY)
2210 mlog_errno(status);
2211 goto bail;
2212 }
2213
2214 /* Notify the error cleanup path to drop the cluster lock. */
2215 acquired = 1;
2216
2217 /* We wait twice because a node may have died while we were in
2218 * the lower dlm layers. The second time though, we've
2219 * committed to owning this lock so we don't allow signals to
2220 * abort the operation. */
2221 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002222 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002223
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002224local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002225 /*
2226 * We only see this flag if we're being called from
2227 * ocfs2_read_locked_inode(). It means we're locking an inode
2228 * which hasn't been populated yet, so clear the refresh flag
2229 * and let the caller handle it.
2230 */
2231 if (inode->i_state & I_NEW) {
2232 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002233 if (lockres)
2234 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002235 goto bail;
2236 }
2237
Mark Fashehccd979b2005-12-15 14:31:24 -08002238 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002239 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002240 * may or may not read one, depending on what's in the
2241 * LVB. The result of all of this is that we've *only* gone to
2242 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002243 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002244 if (status < 0) {
2245 if (status != -ENOENT)
2246 mlog_errno(status);
2247 goto bail;
2248 }
2249
2250 if (ret_bh) {
2251 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2252 if (status < 0) {
2253 mlog_errno(status);
2254 goto bail;
2255 }
2256 }
2257
Mark Fashehccd979b2005-12-15 14:31:24 -08002258bail:
2259 if (status < 0) {
2260 if (ret_bh && (*ret_bh)) {
2261 brelse(*ret_bh);
2262 *ret_bh = NULL;
2263 }
2264 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002265 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002266 }
2267
2268 if (local_bh)
2269 brelse(local_bh);
2270
2271 mlog_exit(status);
2272 return status;
2273}
2274
2275/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002276 * This is working around a lock inversion between tasks acquiring DLM
2277 * locks while holding a page lock and the downconvert thread which
2278 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002279 *
2280 * ** These _with_page variantes are only intended to be called from aop
2281 * methods that hold page locks and return a very specific *positive* error
2282 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2283 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002284 * The DLM is called such that it returns -EAGAIN if it would have
2285 * blocked waiting for the downconvert thread. In that case we unlock
2286 * our page so the downconvert thread can make progress. Once we've
2287 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2288 * that called us can bubble that back up into the VFS who will then
2289 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002290 *
2291 * We do a blocking lock and immediate unlock before returning, though, so that
2292 * the lock has a great chance of being cached on this node by the time the VFS
2293 * calls back to retry the aop. This has a potential to livelock as nodes
2294 * ping locks back and forth, but that's a risk we're willing to take to avoid
2295 * the lock inversion simply.
2296 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002297int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002298 struct buffer_head **ret_bh,
2299 int ex,
2300 struct page *page)
2301{
2302 int ret;
2303
Mark Fashehe63aecb62007-10-18 15:30:42 -07002304 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002305 if (ret == -EAGAIN) {
2306 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002307 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2308 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002309 ret = AOP_TRUNCATED_PAGE;
2310 }
2311
2312 return ret;
2313}
2314
Mark Fashehe63aecb62007-10-18 15:30:42 -07002315int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002316 struct vfsmount *vfsmnt,
2317 int *level)
2318{
2319 int ret;
2320
2321 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002322 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002323 if (ret < 0) {
2324 mlog_errno(ret);
2325 return ret;
2326 }
2327
2328 /*
2329 * If we should update atime, we will get EX lock,
2330 * otherwise we just get PR lock.
2331 */
2332 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2333 struct buffer_head *bh = NULL;
2334
Mark Fashehe63aecb62007-10-18 15:30:42 -07002335 ocfs2_inode_unlock(inode, 0);
2336 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002337 if (ret < 0) {
2338 mlog_errno(ret);
2339 return ret;
2340 }
2341 *level = 1;
2342 if (ocfs2_should_update_atime(inode, vfsmnt))
2343 ocfs2_update_inode_atime(inode, bh);
2344 if (bh)
2345 brelse(bh);
2346 } else
2347 *level = 0;
2348
2349 mlog_exit(ret);
2350 return ret;
2351}
2352
Mark Fashehe63aecb62007-10-18 15:30:42 -07002353void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002354 int ex)
2355{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002356 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002357 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002358 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002359
2360 mlog_entry_void();
2361
Mark Fashehb0697052006-03-03 10:24:33 -08002362 mlog(0, "inode %llu drop %s META lock\n",
2363 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 ex ? "EXMODE" : "PRMODE");
2365
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002366 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2367 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002368 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2369
2370 mlog_exit_void();
2371}
2372
Srinivas Eeda83273932009-06-03 17:02:55 -07002373int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno, int ex)
2374{
2375 struct ocfs2_lock_res *lockres;
2376 struct ocfs2_orphan_scan_lvb *lvb;
2377 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2378 int status = 0;
2379
2380 lockres = &osb->osb_orphan_scan.os_lockres;
2381 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2382 if (status < 0)
2383 return status;
2384
2385 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002386 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2387 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002388 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2389 return status;
2390}
2391
2392void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno, int ex)
2393{
2394 struct ocfs2_lock_res *lockres;
2395 struct ocfs2_orphan_scan_lvb *lvb;
2396 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2397
2398 lockres = &osb->osb_orphan_scan.os_lockres;
2399 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2400 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2401 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2402 ocfs2_cluster_unlock(osb, lockres, level);
2403}
2404
Mark Fashehccd979b2005-12-15 14:31:24 -08002405int ocfs2_super_lock(struct ocfs2_super *osb,
2406 int ex)
2407{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002408 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002409 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002410 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002411
2412 mlog_entry_void();
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 goto bail;
2419
Mark Fashehccd979b2005-12-15 14:31:24 -08002420 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2421 if (status < 0) {
2422 mlog_errno(status);
2423 goto bail;
2424 }
2425
2426 /* The super block lock path is really in the best position to
2427 * know when resources covered by the lock need to be
2428 * refreshed, so we do it here. Of course, making sense of
2429 * everything is up to the caller :) */
2430 status = ocfs2_should_refresh_lock_res(lockres);
2431 if (status < 0) {
2432 mlog_errno(status);
2433 goto bail;
2434 }
2435 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002436 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002437
2438 ocfs2_complete_lock_res_refresh(lockres, status);
2439
2440 if (status < 0)
2441 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002442 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002443 }
2444bail:
2445 mlog_exit(status);
2446 return status;
2447}
2448
2449void ocfs2_super_unlock(struct ocfs2_super *osb,
2450 int ex)
2451{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002452 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002453 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2454
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002455 if (!ocfs2_mount_local(osb))
2456 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002457}
2458
2459int ocfs2_rename_lock(struct ocfs2_super *osb)
2460{
2461 int status;
2462 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2463
2464 if (ocfs2_is_hard_readonly(osb))
2465 return -EROFS;
2466
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002467 if (ocfs2_mount_local(osb))
2468 return 0;
2469
Joel Beckerbd3e7612008-02-01 12:14:57 -08002470 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002471 if (status < 0)
2472 mlog_errno(status);
2473
2474 return status;
2475}
2476
2477void ocfs2_rename_unlock(struct ocfs2_super *osb)
2478{
2479 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2480
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002481 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002482 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002483}
2484
wengang wang6ca497a2009-03-06 21:29:10 +08002485int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2486{
2487 int status;
2488 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2489
2490 if (ocfs2_is_hard_readonly(osb))
2491 return -EROFS;
2492
2493 if (ocfs2_mount_local(osb))
2494 return 0;
2495
2496 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2497 0, 0);
2498 if (status < 0)
2499 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2500
2501 return status;
2502}
2503
2504void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2505{
2506 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2507
2508 if (!ocfs2_mount_local(osb))
2509 ocfs2_cluster_unlock(osb, lockres,
2510 ex ? LKM_EXMODE : LKM_PRMODE);
2511}
2512
Mark Fashehd680efe2006-09-08 14:14:34 -07002513int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2514{
2515 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002516 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002517 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2518 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2519
2520 BUG_ON(!dl);
2521
2522 if (ocfs2_is_hard_readonly(osb))
2523 return -EROFS;
2524
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002525 if (ocfs2_mount_local(osb))
2526 return 0;
2527
Mark Fashehd680efe2006-09-08 14:14:34 -07002528 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2529 if (ret < 0)
2530 mlog_errno(ret);
2531
2532 return ret;
2533}
2534
2535void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2536{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002537 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002538 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2539 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2540
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002541 if (!ocfs2_mount_local(osb))
2542 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002543}
2544
Mark Fashehccd979b2005-12-15 14:31:24 -08002545/* Reference counting of the dlm debug structure. We want this because
2546 * open references on the debug inodes can live on after a mount, so
2547 * we can't rely on the ocfs2_super to always exist. */
2548static void ocfs2_dlm_debug_free(struct kref *kref)
2549{
2550 struct ocfs2_dlm_debug *dlm_debug;
2551
2552 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2553
2554 kfree(dlm_debug);
2555}
2556
2557void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2558{
2559 if (dlm_debug)
2560 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2561}
2562
2563static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2564{
2565 kref_get(&debug->d_refcnt);
2566}
2567
2568struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2569{
2570 struct ocfs2_dlm_debug *dlm_debug;
2571
2572 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2573 if (!dlm_debug) {
2574 mlog_errno(-ENOMEM);
2575 goto out;
2576 }
2577
2578 kref_init(&dlm_debug->d_refcnt);
2579 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2580 dlm_debug->d_locking_state = NULL;
2581out:
2582 return dlm_debug;
2583}
2584
2585/* Access to this is arbitrated for us via seq_file->sem. */
2586struct ocfs2_dlm_seq_priv {
2587 struct ocfs2_dlm_debug *p_dlm_debug;
2588 struct ocfs2_lock_res p_iter_res;
2589 struct ocfs2_lock_res p_tmp_res;
2590};
2591
2592static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2593 struct ocfs2_dlm_seq_priv *priv)
2594{
2595 struct ocfs2_lock_res *iter, *ret = NULL;
2596 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2597
2598 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2599
2600 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2601 /* discover the head of the list */
2602 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2603 mlog(0, "End of list found, %p\n", ret);
2604 break;
2605 }
2606
2607 /* We track our "dummy" iteration lockres' by a NULL
2608 * l_ops field. */
2609 if (iter->l_ops != NULL) {
2610 ret = iter;
2611 break;
2612 }
2613 }
2614
2615 return ret;
2616}
2617
2618static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2619{
2620 struct ocfs2_dlm_seq_priv *priv = m->private;
2621 struct ocfs2_lock_res *iter;
2622
2623 spin_lock(&ocfs2_dlm_tracking_lock);
2624 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2625 if (iter) {
2626 /* Since lockres' have the lifetime of their container
2627 * (which can be inodes, ocfs2_supers, etc) we want to
2628 * copy this out to a temporary lockres while still
2629 * under the spinlock. Obviously after this we can't
2630 * trust any pointers on the copy returned, but that's
2631 * ok as the information we want isn't typically held
2632 * in them. */
2633 priv->p_tmp_res = *iter;
2634 iter = &priv->p_tmp_res;
2635 }
2636 spin_unlock(&ocfs2_dlm_tracking_lock);
2637
2638 return iter;
2639}
2640
2641static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2642{
2643}
2644
2645static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2646{
2647 struct ocfs2_dlm_seq_priv *priv = m->private;
2648 struct ocfs2_lock_res *iter = v;
2649 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2650
2651 spin_lock(&ocfs2_dlm_tracking_lock);
2652 iter = ocfs2_dlm_next_res(iter, priv);
2653 list_del_init(&dummy->l_debug_list);
2654 if (iter) {
2655 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2656 priv->p_tmp_res = *iter;
2657 iter = &priv->p_tmp_res;
2658 }
2659 spin_unlock(&ocfs2_dlm_tracking_lock);
2660
2661 return iter;
2662}
2663
2664/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002665#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002666static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2667{
2668 int i;
2669 char *lvb;
2670 struct ocfs2_lock_res *lockres = v;
2671
2672 if (!lockres)
2673 return -EINVAL;
2674
Mark Fashehd680efe2006-09-08 14:14:34 -07002675 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2676
2677 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2678 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2679 lockres->l_name,
2680 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2681 else
2682 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2683
2684 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002685 "0x%lx\t"
2686 "0x%x\t"
2687 "0x%x\t"
2688 "%u\t"
2689 "%u\t"
2690 "%d\t"
2691 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002692 lockres->l_level,
2693 lockres->l_flags,
2694 lockres->l_action,
2695 lockres->l_unlock_action,
2696 lockres->l_ro_holders,
2697 lockres->l_ex_holders,
2698 lockres->l_requested,
2699 lockres->l_blocking);
2700
2701 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002702 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002703 for(i = 0; i < DLM_LVB_LEN; i++)
2704 seq_printf(m, "0x%x\t", lvb[i]);
2705
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002706#ifdef CONFIG_OCFS2_FS_STATS
2707# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2708# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2709# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2710# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2711# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2712# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2713# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2714# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2715# define lock_refresh(_l) (_l)->l_lock_refresh
2716#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002717# define lock_num_prmode(_l) (0ULL)
2718# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002719# define lock_num_prmode_failed(_l) (0)
2720# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002721# define lock_total_prmode(_l) (0ULL)
2722# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002723# define lock_max_prmode(_l) (0)
2724# define lock_max_exmode(_l) (0)
2725# define lock_refresh(_l) (0)
2726#endif
2727 /* The following seq_print was added in version 2 of this output */
2728 seq_printf(m, "%llu\t"
2729 "%llu\t"
2730 "%u\t"
2731 "%u\t"
2732 "%llu\t"
2733 "%llu\t"
2734 "%u\t"
2735 "%u\t"
2736 "%u\t",
2737 lock_num_prmode(lockres),
2738 lock_num_exmode(lockres),
2739 lock_num_prmode_failed(lockres),
2740 lock_num_exmode_failed(lockres),
2741 lock_total_prmode(lockres),
2742 lock_total_exmode(lockres),
2743 lock_max_prmode(lockres),
2744 lock_max_exmode(lockres),
2745 lock_refresh(lockres));
2746
Mark Fashehccd979b2005-12-15 14:31:24 -08002747 /* End the line */
2748 seq_printf(m, "\n");
2749 return 0;
2750}
2751
Jan Engelhardt90d99772008-01-22 20:52:20 +01002752static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002753 .start = ocfs2_dlm_seq_start,
2754 .stop = ocfs2_dlm_seq_stop,
2755 .next = ocfs2_dlm_seq_next,
2756 .show = ocfs2_dlm_seq_show,
2757};
2758
2759static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2760{
2761 struct seq_file *seq = (struct seq_file *) file->private_data;
2762 struct ocfs2_dlm_seq_priv *priv = seq->private;
2763 struct ocfs2_lock_res *res = &priv->p_iter_res;
2764
2765 ocfs2_remove_lockres_tracking(res);
2766 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2767 return seq_release_private(inode, file);
2768}
2769
2770static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2771{
2772 int ret;
2773 struct ocfs2_dlm_seq_priv *priv;
2774 struct seq_file *seq;
2775 struct ocfs2_super *osb;
2776
2777 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2778 if (!priv) {
2779 ret = -ENOMEM;
2780 mlog_errno(ret);
2781 goto out;
2782 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002783 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002784 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2785 priv->p_dlm_debug = osb->osb_dlm_debug;
2786 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2787
2788 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2789 if (ret) {
2790 kfree(priv);
2791 mlog_errno(ret);
2792 goto out;
2793 }
2794
2795 seq = (struct seq_file *) file->private_data;
2796 seq->private = priv;
2797
2798 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2799 priv->p_dlm_debug);
2800
2801out:
2802 return ret;
2803}
2804
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002805static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002806 .open = ocfs2_dlm_debug_open,
2807 .release = ocfs2_dlm_debug_release,
2808 .read = seq_read,
2809 .llseek = seq_lseek,
2810};
2811
2812static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2813{
2814 int ret = 0;
2815 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2816
2817 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2818 S_IFREG|S_IRUSR,
2819 osb->osb_debug_root,
2820 osb,
2821 &ocfs2_dlm_debug_fops);
2822 if (!dlm_debug->d_locking_state) {
2823 ret = -EINVAL;
2824 mlog(ML_ERROR,
2825 "Unable to create locking state debugfs file.\n");
2826 goto out;
2827 }
2828
2829 ocfs2_get_dlm_debug(dlm_debug);
2830out:
2831 return ret;
2832}
2833
2834static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2835{
2836 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2837
2838 if (dlm_debug) {
2839 debugfs_remove(dlm_debug->d_locking_state);
2840 ocfs2_put_dlm_debug(dlm_debug);
2841 }
2842}
2843
2844int ocfs2_dlm_init(struct ocfs2_super *osb)
2845{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002846 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002847 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002848
2849 mlog_entry_void();
2850
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002851 if (ocfs2_mount_local(osb)) {
2852 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002853 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002854 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002855
Mark Fashehccd979b2005-12-15 14:31:24 -08002856 status = ocfs2_dlm_init_debug(osb);
2857 if (status < 0) {
2858 mlog_errno(status);
2859 goto bail;
2860 }
2861
Mark Fasheh34d024f2007-09-24 15:56:19 -07002862 /* launch downconvert thread */
2863 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2864 if (IS_ERR(osb->dc_task)) {
2865 status = PTR_ERR(osb->dc_task);
2866 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002867 mlog_errno(status);
2868 goto bail;
2869 }
2870
Mark Fashehccd979b2005-12-15 14:31:24 -08002871 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002872 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2873 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002874 strlen(osb->uuid_str),
2875 ocfs2_do_node_down, osb,
2876 &conn);
2877 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002878 mlog_errno(status);
2879 goto bail;
2880 }
2881
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002882 status = ocfs2_cluster_this_node(&osb->node_num);
2883 if (status < 0) {
2884 mlog_errno(status);
2885 mlog(ML_ERROR,
2886 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002887 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002888 goto bail;
2889 }
2890
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002891local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002892 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2893 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002894 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07002895 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002896
Joel Becker4670c462008-02-01 14:39:35 -08002897 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002898
2899 status = 0;
2900bail:
2901 if (status < 0) {
2902 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002903 if (osb->dc_task)
2904 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002905 }
2906
2907 mlog_exit(status);
2908 return status;
2909}
2910
Joel Becker286eaa92008-02-01 15:03:57 -08002911void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2912 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002913{
2914 mlog_entry_void();
2915
Mark Fashehccd979b2005-12-15 14:31:24 -08002916 ocfs2_drop_osb_locks(osb);
2917
Joel Becker4670c462008-02-01 14:39:35 -08002918 /*
2919 * Now that we have dropped all locks and ocfs2_dismount_volume()
2920 * has disabled recovery, the DLM won't be talking to us. It's
2921 * safe to tear things down before disconnecting the cluster.
2922 */
2923
Mark Fasheh34d024f2007-09-24 15:56:19 -07002924 if (osb->dc_task) {
2925 kthread_stop(osb->dc_task);
2926 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002927 }
2928
2929 ocfs2_lock_res_free(&osb->osb_super_lockres);
2930 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08002931 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07002932 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002933
Joel Becker286eaa92008-02-01 15:03:57 -08002934 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08002935 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002936
2937 ocfs2_dlm_shutdown_debug(osb);
2938
2939 mlog_exit_void();
2940}
2941
Joel Becker7431cd72008-02-01 12:15:37 -08002942static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002943{
2944 struct ocfs2_lock_res *lockres = opaque;
2945 unsigned long flags;
2946
2947 mlog_entry_void();
2948
2949 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2950 lockres->l_unlock_action);
2951
2952 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08002953 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08002954 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2955 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002956 lockres->l_unlock_action);
2957 spin_unlock_irqrestore(&lockres->l_lock, flags);
2958 return;
2959 }
2960
2961 switch(lockres->l_unlock_action) {
2962 case OCFS2_UNLOCK_CANCEL_CONVERT:
2963 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2964 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08002965 /* Downconvert thread may have requeued this lock, we
2966 * need to wake it. */
2967 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2968 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08002969 break;
2970 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002971 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002972 break;
2973 default:
2974 BUG();
2975 }
2976
2977 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08002978 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08002979 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06002980 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002981
2982 mlog_exit_void();
2983}
2984
Mark Fashehccd979b2005-12-15 14:31:24 -08002985static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002986 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002987{
Joel Becker7431cd72008-02-01 12:15:37 -08002988 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002989 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002990 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002991
2992 /* We didn't get anywhere near actually using this lockres. */
2993 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2994 goto out;
2995
Mark Fashehb80fc012006-09-12 22:08:14 -07002996 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002997 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002998
Mark Fashehccd979b2005-12-15 14:31:24 -08002999 spin_lock_irqsave(&lockres->l_lock, flags);
3000
3001 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3002 "lockres %s, flags 0x%lx\n",
3003 lockres->l_name, lockres->l_flags);
3004
3005 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3006 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3007 "%u, unlock_action = %u\n",
3008 lockres->l_name, lockres->l_flags, lockres->l_action,
3009 lockres->l_unlock_action);
3010
3011 spin_unlock_irqrestore(&lockres->l_lock, flags);
3012
3013 /* XXX: Today we just wait on any busy
3014 * locks... Perhaps we need to cancel converts in the
3015 * future? */
3016 ocfs2_wait_on_busy_lock(lockres);
3017
3018 spin_lock_irqsave(&lockres->l_lock, flags);
3019 }
3020
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003021 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3022 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003023 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003024 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3025 lockres->l_ops->set_lvb(lockres);
3026 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003027
3028 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3029 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3030 lockres->l_name);
3031 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3032 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3033
3034 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3035 spin_unlock_irqrestore(&lockres->l_lock, flags);
3036 goto out;
3037 }
3038
3039 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3040
3041 /* make sure we never get here while waiting for an ast to
3042 * fire. */
3043 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3044
3045 /* is this necessary? */
3046 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3047 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3048 spin_unlock_irqrestore(&lockres->l_lock, flags);
3049
3050 mlog(0, "lock %s\n", lockres->l_name);
3051
Joel Becker4670c462008-02-01 14:39:35 -08003052 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003053 lockres);
3054 if (ret) {
3055 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003056 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003057 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003058 BUG();
3059 }
Coly Li73ac36e2009-01-07 18:09:16 -08003060 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003061 lockres->l_name);
3062
3063 ocfs2_wait_on_busy_lock(lockres);
3064out:
3065 mlog_exit(0);
3066 return 0;
3067}
3068
3069/* Mark the lockres as being dropped. It will no longer be
3070 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003071 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08003072 * it safe to drop.
3073 *
3074 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3075void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3076{
3077 int status;
3078 struct ocfs2_mask_waiter mw;
3079 unsigned long flags;
3080
3081 ocfs2_init_mask_waiter(&mw);
3082
3083 spin_lock_irqsave(&lockres->l_lock, flags);
3084 lockres->l_flags |= OCFS2_LOCK_FREEING;
3085 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3086 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3087 spin_unlock_irqrestore(&lockres->l_lock, flags);
3088
3089 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3090
3091 status = ocfs2_wait_for_mask(&mw);
3092 if (status)
3093 mlog_errno(status);
3094
3095 spin_lock_irqsave(&lockres->l_lock, flags);
3096 }
3097 spin_unlock_irqrestore(&lockres->l_lock, flags);
3098}
3099
Mark Fashehd680efe2006-09-08 14:14:34 -07003100void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3101 struct ocfs2_lock_res *lockres)
3102{
3103 int ret;
3104
3105 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003106 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003107 if (ret)
3108 mlog_errno(ret);
3109}
3110
Mark Fashehccd979b2005-12-15 14:31:24 -08003111static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3112{
Mark Fashehd680efe2006-09-08 14:14:34 -07003113 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3114 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003115 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003116 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003117}
3118
Mark Fashehccd979b2005-12-15 14:31:24 -08003119int ocfs2_drop_inode_locks(struct inode *inode)
3120{
3121 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003122
3123 mlog_entry_void();
3124
3125 /* No need to call ocfs2_mark_lockres_freeing here -
3126 * ocfs2_clear_inode has done it for us. */
3127
3128 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003129 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003130 if (err < 0)
3131 mlog_errno(err);
3132
3133 status = err;
3134
3135 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003136 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003137 if (err < 0)
3138 mlog_errno(err);
3139 if (err < 0 && !status)
3140 status = err;
3141
3142 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003143 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003144 if (err < 0)
3145 mlog_errno(err);
3146 if (err < 0 && !status)
3147 status = err;
3148
3149 mlog_exit(status);
3150 return status;
3151}
3152
Joel Beckerde551242008-02-01 14:45:08 -08003153static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3154 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003155{
3156 assert_spin_locked(&lockres->l_lock);
3157
Joel Beckerbd3e7612008-02-01 12:14:57 -08003158 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003159
3160 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003161 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003162 lockres->l_level, new_level);
3163 BUG();
3164 }
3165
3166 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3167 lockres->l_name, new_level, lockres->l_blocking);
3168
3169 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3170 lockres->l_requested = new_level;
3171 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003172 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003173}
3174
3175static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3176 struct ocfs2_lock_res *lockres,
3177 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003178 int lvb,
3179 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003180{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003181 int ret;
3182 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003183
3184 mlog_entry_void();
3185
3186 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003187 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003188
Joel Becker4670c462008-02-01 14:39:35 -08003189 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003190 new_level,
3191 &lockres->l_lksb,
3192 dlm_flags,
3193 lockres->l_name,
3194 OCFS2_LOCK_ID_MAX_LEN - 1,
3195 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003196 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003197 if (ret) {
3198 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003199 ocfs2_recover_from_dlm_error(lockres, 1);
3200 goto bail;
3201 }
3202
3203 ret = 0;
3204bail:
3205 mlog_exit(ret);
3206 return ret;
3207}
3208
Joel Becker24ef1812008-01-29 17:37:32 -08003209/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003210static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3211 struct ocfs2_lock_res *lockres)
3212{
3213 assert_spin_locked(&lockres->l_lock);
3214
3215 mlog_entry_void();
3216 mlog(0, "lock %s\n", lockres->l_name);
3217
3218 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3219 /* If we're already trying to cancel a lock conversion
3220 * then just drop the spinlock and allow the caller to
3221 * requeue this lock. */
3222
3223 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3224 return 0;
3225 }
3226
3227 /* were we in a convert when we got the bast fire? */
3228 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3229 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3230 /* set things up for the unlockast to know to just
3231 * clear out the ast_action and unset busy, etc. */
3232 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3233
3234 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3235 "lock %s, invalid flags: 0x%lx\n",
3236 lockres->l_name, lockres->l_flags);
3237
3238 return 1;
3239}
3240
3241static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3242 struct ocfs2_lock_res *lockres)
3243{
3244 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003245
3246 mlog_entry_void();
3247 mlog(0, "lock %s\n", lockres->l_name);
3248
Joel Becker4670c462008-02-01 14:39:35 -08003249 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003250 DLM_LKF_CANCEL, lockres);
3251 if (ret) {
3252 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003253 ocfs2_recover_from_dlm_error(lockres, 0);
3254 }
3255
Joel Becker24ef1812008-01-29 17:37:32 -08003256 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003257
3258 mlog_exit(ret);
3259 return ret;
3260}
3261
Mark Fashehb5e500e2006-09-13 22:01:16 -07003262static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3263 struct ocfs2_lock_res *lockres,
3264 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003265{
3266 unsigned long flags;
3267 int blocking;
3268 int new_level;
3269 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003270 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003271 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003272
3273 mlog_entry_void();
3274
3275 spin_lock_irqsave(&lockres->l_lock, flags);
3276
3277 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3278
3279recheck:
3280 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003281 /* XXX
3282 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3283 * exists entirely for one reason - another thread has set
3284 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3285 *
3286 * If we do ocfs2_cancel_convert() before the other thread
3287 * calls dlm_lock(), our cancel will do nothing. We will
3288 * get no ast, and we will have no way of knowing the
3289 * cancel failed. Meanwhile, the other thread will call
3290 * into dlm_lock() and wait...forever.
3291 *
3292 * Why forever? Because another node has asked for the
3293 * lock first; that's why we're here in unblock_lock().
3294 *
3295 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3296 * set, we just requeue the unblock. Only when the other
3297 * thread has called dlm_lock() and cleared PENDING will
3298 * we then cancel their request.
3299 *
3300 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3301 * at the same time they set OCFS2_DLM_BUSY. They must
3302 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3303 */
3304 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3305 goto leave_requeue;
3306
Mark Fashehd680efe2006-09-08 14:14:34 -07003307 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003308 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3309 spin_unlock_irqrestore(&lockres->l_lock, flags);
3310 if (ret) {
3311 ret = ocfs2_cancel_convert(osb, lockres);
3312 if (ret < 0)
3313 mlog_errno(ret);
3314 }
3315 goto leave;
3316 }
3317
3318 /* if we're blocking an exclusive and we have *any* holders,
3319 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003320 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003321 && (lockres->l_ex_holders || lockres->l_ro_holders))
3322 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003323
3324 /* If it's a PR we're blocking, then only
3325 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003326 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003327 lockres->l_ex_holders)
3328 goto leave_requeue;
3329
3330 /*
3331 * Can we get a lock in this state if the holder counts are
3332 * zero? The meta data unblock code used to check this.
3333 */
3334 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3335 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3336 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003337
Mark Fasheh16d5b9562006-09-13 21:10:12 -07003338 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3339
3340 if (lockres->l_ops->check_downconvert
3341 && !lockres->l_ops->check_downconvert(lockres, new_level))
3342 goto leave_requeue;
3343
Mark Fashehccd979b2005-12-15 14:31:24 -08003344 /* If we get here, then we know that there are no more
3345 * incompatible holders (and anyone asking for an incompatible
3346 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003347 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003348 goto downconvert;
3349
3350 /* Some lockres types want to do a bit of work before
3351 * downconverting a lock. Allow that here. The worker function
3352 * may sleep, so we save off a copy of what we're blocking as
3353 * it may change while we're not holding the spin lock. */
3354 blocking = lockres->l_blocking;
3355 spin_unlock_irqrestore(&lockres->l_lock, flags);
3356
Mark Fashehcc567d82006-09-13 21:52:21 -07003357 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003358
3359 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3360 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003361
3362 spin_lock_irqsave(&lockres->l_lock, flags);
3363 if (blocking != lockres->l_blocking) {
3364 /* If this changed underneath us, then we can't drop
3365 * it just yet. */
3366 goto recheck;
3367 }
3368
3369downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003370 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003371
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003372 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003373 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003374 set_lvb = 1;
3375
3376 /*
3377 * We only set the lvb if the lock has been fully
3378 * refreshed - otherwise we risk setting stale
3379 * data. Otherwise, there's no need to actually clear
3380 * out the lvb here as it's value is still valid.
3381 */
3382 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3383 lockres->l_ops->set_lvb(lockres);
3384 }
3385
Joel Beckerde551242008-02-01 14:45:08 -08003386 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003387 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003388 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3389 gen);
3390
Mark Fashehccd979b2005-12-15 14:31:24 -08003391leave:
3392 mlog_exit(ret);
3393 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003394
3395leave_requeue:
3396 spin_unlock_irqrestore(&lockres->l_lock, flags);
3397 ctl->requeue = 1;
3398
3399 mlog_exit(0);
3400 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003401}
3402
Mark Fashehd680efe2006-09-08 14:14:34 -07003403static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3404 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003405{
3406 struct inode *inode;
3407 struct address_space *mapping;
3408
Mark Fashehccd979b2005-12-15 14:31:24 -08003409 inode = ocfs2_lock_res_inode(lockres);
3410 mapping = inode->i_mapping;
3411
Mark Fasheh1044e402008-02-28 17:16:03 -08003412 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003413 goto out;
3414
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003415 /*
3416 * We need this before the filemap_fdatawrite() so that it can
3417 * transfer the dirty bit from the PTE to the
3418 * page. Unfortunately this means that even for EX->PR
3419 * downconverts, we'll lose our mappings and have to build
3420 * them up again.
3421 */
3422 unmap_mapping_range(mapping, 0, 0, 0);
3423
Mark Fashehccd979b2005-12-15 14:31:24 -08003424 if (filemap_fdatawrite(mapping)) {
Mark Fashehb0697052006-03-03 10:24:33 -08003425 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3426 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003427 }
3428 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003429 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003430 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003431 } else {
3432 /* We only need to wait on the I/O if we're not also
3433 * truncating pages because truncate_inode_pages waits
3434 * for us above. We don't truncate pages if we're
3435 * blocking anything < EXMODE because we want to keep
3436 * them around in that case. */
3437 filemap_fdatawait(mapping);
3438 }
3439
Mark Fashehf1f54062007-10-18 15:13:59 -07003440out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003441 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003442}
3443
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003444static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3445 int new_level)
3446{
3447 struct inode *inode = ocfs2_lock_res_inode(lockres);
3448 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3449
Joel Beckerbd3e7612008-02-01 12:14:57 -08003450 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3451 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003452
3453 if (checkpointed)
3454 return 1;
3455
3456 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3457 return 0;
3458}
3459
3460static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3461{
3462 struct inode *inode = ocfs2_lock_res_inode(lockres);
3463
3464 __ocfs2_stuff_meta_lvb(inode);
3465}
3466
Mark Fashehd680efe2006-09-08 14:14:34 -07003467/*
3468 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003469 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003470 * dlmglue API and push these off to the ocfs2_wq in the future.
3471 */
3472static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3473 struct ocfs2_lock_res *lockres)
3474{
3475 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3476 ocfs2_dentry_lock_put(osb, dl);
3477}
3478
3479/*
3480 * d_delete() matching dentries before the lock downconvert.
3481 *
3482 * At this point, any process waiting to destroy the
3483 * dentry_lock due to last ref count is stopped by the
3484 * OCFS2_LOCK_QUEUED flag.
3485 *
3486 * We have two potential problems
3487 *
3488 * 1) If we do the last reference drop on our dentry_lock (via dput)
3489 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3490 * the downconvert to finish. Instead we take an elevated
3491 * reference and push the drop until after we've completed our
3492 * unblock processing.
3493 *
3494 * 2) There might be another process with a final reference,
3495 * waiting on us to finish processing. If this is the case, we
3496 * detect it and exit out - there's no more dentries anyway.
3497 */
3498static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3499 int blocking)
3500{
3501 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3502 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3503 struct dentry *dentry;
3504 unsigned long flags;
3505 int extra_ref = 0;
3506
3507 /*
3508 * This node is blocking another node from getting a read
3509 * lock. This happens when we've renamed within a
3510 * directory. We've forced the other nodes to d_delete(), but
3511 * we never actually dropped our lock because it's still
3512 * valid. The downconvert code will retain a PR for this node,
3513 * so there's no further work to do.
3514 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003515 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003516 return UNBLOCK_CONTINUE;
3517
3518 /*
3519 * Mark this inode as potentially orphaned. The code in
3520 * ocfs2_delete_inode() will figure out whether it actually
3521 * needs to be freed or not.
3522 */
3523 spin_lock(&oi->ip_lock);
3524 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3525 spin_unlock(&oi->ip_lock);
3526
3527 /*
3528 * Yuck. We need to make sure however that the check of
3529 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3530 * respect to a reference decrement or the setting of that
3531 * flag.
3532 */
3533 spin_lock_irqsave(&lockres->l_lock, flags);
3534 spin_lock(&dentry_attach_lock);
3535 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3536 && dl->dl_count) {
3537 dl->dl_count++;
3538 extra_ref = 1;
3539 }
3540 spin_unlock(&dentry_attach_lock);
3541 spin_unlock_irqrestore(&lockres->l_lock, flags);
3542
3543 mlog(0, "extra_ref = %d\n", extra_ref);
3544
3545 /*
3546 * We have a process waiting on us in ocfs2_dentry_iput(),
3547 * which means we can't have any more outstanding
3548 * aliases. There's no need to do any more work.
3549 */
3550 if (!extra_ref)
3551 return UNBLOCK_CONTINUE;
3552
3553 spin_lock(&dentry_attach_lock);
3554 while (1) {
3555 dentry = ocfs2_find_local_alias(dl->dl_inode,
3556 dl->dl_parent_blkno, 1);
3557 if (!dentry)
3558 break;
3559 spin_unlock(&dentry_attach_lock);
3560
3561 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3562 dentry->d_name.name);
3563
3564 /*
3565 * The following dcache calls may do an
3566 * iput(). Normally we don't want that from the
3567 * downconverting thread, but in this case it's ok
3568 * because the requesting node already has an
3569 * exclusive lock on the inode, so it can't be queued
3570 * for a downconvert.
3571 */
3572 d_delete(dentry);
3573 dput(dentry);
3574
3575 spin_lock(&dentry_attach_lock);
3576 }
3577 spin_unlock(&dentry_attach_lock);
3578
3579 /*
3580 * If we are the last holder of this dentry lock, there is no
3581 * reason to downconvert so skip straight to the unlock.
3582 */
3583 if (dl->dl_count == 1)
3584 return UNBLOCK_STOP_POST;
3585
3586 return UNBLOCK_CONTINUE_POST;
3587}
3588
Jan Kara9e33d692008-08-25 19:56:50 +02003589static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3590{
3591 struct ocfs2_qinfo_lvb *lvb;
3592 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3593 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3594 oinfo->dqi_gi.dqi_type);
3595
3596 mlog_entry_void();
3597
Mark Fasheha641dc22008-12-24 16:03:48 -08003598 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003599 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3600 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3601 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3602 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3603 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3604 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3605 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3606
3607 mlog_exit_void();
3608}
3609
3610void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3611{
3612 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3613 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3614 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3615
3616 mlog_entry_void();
3617 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3618 ocfs2_cluster_unlock(osb, lockres, level);
3619 mlog_exit_void();
3620}
3621
3622static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3623{
3624 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3625 oinfo->dqi_gi.dqi_type);
3626 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3627 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003628 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003629 struct ocfs2_global_disk_dqinfo *gdinfo;
3630 int status = 0;
3631
Joel Becker1c520df2009-06-19 15:14:13 -07003632 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3633 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003634 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3635 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3636 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3637 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3638 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3639 oinfo->dqi_gi.dqi_free_entry =
3640 be32_to_cpu(lvb->lvb_free_entry);
3641 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003642 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3643 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003644 mlog_errno(status);
3645 goto bail;
3646 }
3647 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3648 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3649 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3650 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3651 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3652 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3653 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3654 oinfo->dqi_gi.dqi_free_entry =
3655 le32_to_cpu(gdinfo->dqi_free_entry);
3656 brelse(bh);
3657 ocfs2_track_lock_refresh(lockres);
3658 }
3659
3660bail:
3661 return status;
3662}
3663
3664/* Lock quota info, this function expects at least shared lock on the quota file
3665 * so that we can safely refresh quota info from disk. */
3666int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3667{
3668 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3669 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3670 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3671 int status = 0;
3672
3673 mlog_entry_void();
3674
3675 /* On RO devices, locking really isn't needed... */
3676 if (ocfs2_is_hard_readonly(osb)) {
3677 if (ex)
3678 status = -EROFS;
3679 goto bail;
3680 }
3681 if (ocfs2_mount_local(osb))
3682 goto bail;
3683
3684 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3685 if (status < 0) {
3686 mlog_errno(status);
3687 goto bail;
3688 }
3689 if (!ocfs2_should_refresh_lock_res(lockres))
3690 goto bail;
3691 /* OK, we have the lock but we need to refresh the quota info */
3692 status = ocfs2_refresh_qinfo(oinfo);
3693 if (status)
3694 ocfs2_qinfo_unlock(oinfo, ex);
3695 ocfs2_complete_lock_res_refresh(lockres, status);
3696bail:
3697 mlog_exit(status);
3698 return status;
3699}
3700
Joel Becker4670c462008-02-01 14:39:35 -08003701/*
3702 * This is the filesystem locking protocol. It provides the lock handling
3703 * hooks for the underlying DLM. It has a maximum version number.
3704 * The version number allows interoperability with systems running at
3705 * the same major number and an equal or smaller minor number.
3706 *
3707 * Whenever the filesystem does new things with locks (adds or removes a
3708 * lock, orders them differently, does different things underneath a lock),
3709 * the version must be changed. The protocol is negotiated when joining
3710 * the dlm domain. A node may join the domain if its major version is
3711 * identical to all other nodes and its minor version is greater than
3712 * or equal to all other nodes. When its minor version is greater than
3713 * the other nodes, it will run at the minor version specified by the
3714 * other nodes.
3715 *
3716 * If a locking change is made that will not be compatible with older
3717 * versions, the major number must be increased and the minor version set
3718 * to zero. If a change merely adds a behavior that can be disabled when
3719 * speaking to older versions, the minor version must be increased. If a
3720 * change adds a fully backwards compatible change (eg, LVB changes that
3721 * are just ignored by older versions), the version does not need to be
3722 * updated.
3723 */
Joel Becker24ef1812008-01-29 17:37:32 -08003724static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003725 .lp_max_version = {
3726 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3727 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3728 },
Joel Becker24ef1812008-01-29 17:37:32 -08003729 .lp_lock_ast = ocfs2_locking_ast,
3730 .lp_blocking_ast = ocfs2_blocking_ast,
3731 .lp_unlock_ast = ocfs2_unlock_ast,
3732};
3733
Joel Becker63e0c482008-01-30 16:58:36 -08003734void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003735{
Joel Becker63e0c482008-01-30 16:58:36 -08003736 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003737}
3738
Joel Becker24ef1812008-01-29 17:37:32 -08003739
Adrian Bunk00600052008-01-29 00:11:41 +02003740static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3741 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003742{
3743 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003744 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003745 unsigned long flags;
3746
3747 /* Our reference to the lockres in this function can be
3748 * considered valid until we remove the OCFS2_LOCK_QUEUED
3749 * flag. */
3750
3751 mlog_entry_void();
3752
3753 BUG_ON(!lockres);
3754 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003755
3756 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3757
3758 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003759 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003760 * still be marked with OCFS2_LOCK_FREEING after this check,
3761 * but short circuiting here will still save us some
3762 * performance. */
3763 spin_lock_irqsave(&lockres->l_lock, flags);
3764 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3765 goto unqueue;
3766 spin_unlock_irqrestore(&lockres->l_lock, flags);
3767
Mark Fashehb5e500e2006-09-13 22:01:16 -07003768 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003769 if (status < 0)
3770 mlog_errno(status);
3771
3772 spin_lock_irqsave(&lockres->l_lock, flags);
3773unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003774 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003775 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3776 } else
3777 ocfs2_schedule_blocked_lock(osb, lockres);
3778
3779 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003780 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003781 spin_unlock_irqrestore(&lockres->l_lock, flags);
3782
Mark Fashehd680efe2006-09-08 14:14:34 -07003783 if (ctl.unblock_action != UNBLOCK_CONTINUE
3784 && lockres->l_ops->post_unlock)
3785 lockres->l_ops->post_unlock(osb, lockres);
3786
Mark Fashehccd979b2005-12-15 14:31:24 -08003787 mlog_exit_void();
3788}
3789
3790static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3791 struct ocfs2_lock_res *lockres)
3792{
3793 mlog_entry_void();
3794
3795 assert_spin_locked(&lockres->l_lock);
3796
3797 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3798 /* Do not schedule a lock for downconvert when it's on
3799 * the way to destruction - any nodes wanting access
3800 * to the resource will get it soon. */
3801 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3802 lockres->l_name, lockres->l_flags);
3803 return;
3804 }
3805
3806 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3807
Mark Fasheh34d024f2007-09-24 15:56:19 -07003808 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003809 if (list_empty(&lockres->l_blocked_list)) {
3810 list_add_tail(&lockres->l_blocked_list,
3811 &osb->blocked_lock_list);
3812 osb->blocked_lock_count++;
3813 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003814 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003815
3816 mlog_exit_void();
3817}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003818
3819static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3820{
3821 unsigned long processed;
3822 struct ocfs2_lock_res *lockres;
3823
3824 mlog_entry_void();
3825
3826 spin_lock(&osb->dc_task_lock);
3827 /* grab this early so we know to try again if a state change and
3828 * wake happens part-way through our work */
3829 osb->dc_work_sequence = osb->dc_wake_sequence;
3830
3831 processed = osb->blocked_lock_count;
3832 while (processed) {
3833 BUG_ON(list_empty(&osb->blocked_lock_list));
3834
3835 lockres = list_entry(osb->blocked_lock_list.next,
3836 struct ocfs2_lock_res, l_blocked_list);
3837 list_del_init(&lockres->l_blocked_list);
3838 osb->blocked_lock_count--;
3839 spin_unlock(&osb->dc_task_lock);
3840
3841 BUG_ON(!processed);
3842 processed--;
3843
3844 ocfs2_process_blocked_lock(osb, lockres);
3845
3846 spin_lock(&osb->dc_task_lock);
3847 }
3848 spin_unlock(&osb->dc_task_lock);
3849
3850 mlog_exit_void();
3851}
3852
3853static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3854{
3855 int empty = 0;
3856
3857 spin_lock(&osb->dc_task_lock);
3858 if (list_empty(&osb->blocked_lock_list))
3859 empty = 1;
3860
3861 spin_unlock(&osb->dc_task_lock);
3862 return empty;
3863}
3864
3865static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3866{
3867 int should_wake = 0;
3868
3869 spin_lock(&osb->dc_task_lock);
3870 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3871 should_wake = 1;
3872 spin_unlock(&osb->dc_task_lock);
3873
3874 return should_wake;
3875}
3876
Adrian Bunk200bfae2008-02-17 10:20:38 +02003877static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003878{
3879 int status = 0;
3880 struct ocfs2_super *osb = arg;
3881
3882 /* only quit once we've been asked to stop and there is no more
3883 * work available */
3884 while (!(kthread_should_stop() &&
3885 ocfs2_downconvert_thread_lists_empty(osb))) {
3886
3887 wait_event_interruptible(osb->dc_event,
3888 ocfs2_downconvert_thread_should_wake(osb) ||
3889 kthread_should_stop());
3890
3891 mlog(0, "downconvert_thread: awoken\n");
3892
3893 ocfs2_downconvert_thread_do_work(osb);
3894 }
3895
3896 osb->dc_task = NULL;
3897 return status;
3898}
3899
3900void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3901{
3902 spin_lock(&osb->dc_task_lock);
3903 /* make sure the voting thread gets a swipe at whatever changes
3904 * the caller may have made to the voting state */
3905 osb->dc_wake_sequence++;
3906 spin_unlock(&osb->dc_task_lock);
3907 wake_up(&osb->dc_event);
3908}