blob: 1841bbb49cb63b9ce7625cfc68e0c17f2d6e6caa [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 Fasheh16d5b952006-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 Fashehb06970532006-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{
Srinivas Eeda83273932009-06-03 17:02:55 -0700647 ocfs2_lock_res_init_once(res);
648 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
649 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
650 &ocfs2_orphan_scan_lops, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -0700651}
652
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800653void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
654 struct ocfs2_file_private *fp)
655{
656 struct inode *inode = fp->fp_file->f_mapping->host;
657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
658
659 ocfs2_lock_res_init_once(lockres);
660 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
661 inode->i_generation, lockres->l_name);
662 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
663 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
664 fp);
665 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
666}
667
Jan Kara9e33d692008-08-25 19:56:50 +0200668void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
669 struct ocfs2_mem_dqinfo *info)
670{
671 ocfs2_lock_res_init_once(lockres);
672 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
673 0, lockres->l_name);
674 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
675 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
676 info);
677}
678
Mark Fashehccd979b2005-12-15 14:31:24 -0800679void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
680{
681 mlog_entry_void();
682
683 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
684 return;
685
686 ocfs2_remove_lockres_tracking(res);
687
688 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
689 "Lockres %s is on the blocked list\n",
690 res->l_name);
691 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
692 "Lockres %s has mask waiters pending\n",
693 res->l_name);
694 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
695 "Lockres %s is locked\n",
696 res->l_name);
697 mlog_bug_on_msg(res->l_ro_holders,
698 "Lockres %s has %u ro holders\n",
699 res->l_name, res->l_ro_holders);
700 mlog_bug_on_msg(res->l_ex_holders,
701 "Lockres %s has %u ex holders\n",
702 res->l_name, res->l_ex_holders);
703
704 /* Need to clear out the lock status block for the dlm */
705 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
706
707 res->l_flags = 0UL;
708 mlog_exit_void();
709}
710
711static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
712 int level)
713{
714 mlog_entry_void();
715
716 BUG_ON(!lockres);
717
718 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800719 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 lockres->l_ex_holders++;
721 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800722 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800723 lockres->l_ro_holders++;
724 break;
725 default:
726 BUG();
727 }
728
729 mlog_exit_void();
730}
731
732static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
733 int level)
734{
735 mlog_entry_void();
736
737 BUG_ON(!lockres);
738
739 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800740 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 BUG_ON(!lockres->l_ex_holders);
742 lockres->l_ex_holders--;
743 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800744 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 BUG_ON(!lockres->l_ro_holders);
746 lockres->l_ro_holders--;
747 break;
748 default:
749 BUG();
750 }
751 mlog_exit_void();
752}
753
754/* WARNING: This function lives in a world where the only three lock
755 * levels are EX, PR, and NL. It *will* have to be adjusted when more
756 * lock types are added. */
757static inline int ocfs2_highest_compat_lock_level(int level)
758{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800759 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800760
Joel Beckerbd3e7612008-02-01 12:14:57 -0800761 if (level == DLM_LOCK_EX)
762 new_level = DLM_LOCK_NL;
763 else if (level == DLM_LOCK_PR)
764 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800765 return new_level;
766}
767
768static void lockres_set_flags(struct ocfs2_lock_res *lockres,
769 unsigned long newflags)
770{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200771 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800772
773 assert_spin_locked(&lockres->l_lock);
774
775 lockres->l_flags = newflags;
776
Christoph Hellwig800deef2007-05-17 16:03:13 +0200777 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800778 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
779 continue;
780
781 list_del_init(&mw->mw_item);
782 mw->mw_status = 0;
783 complete(&mw->mw_complete);
784 }
785}
786static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
787{
788 lockres_set_flags(lockres, lockres->l_flags | or);
789}
790static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
791 unsigned long clear)
792{
793 lockres_set_flags(lockres, lockres->l_flags & ~clear);
794}
795
796static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
797{
798 mlog_entry_void();
799
800 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
801 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
802 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800803 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800804
805 lockres->l_level = lockres->l_requested;
806 if (lockres->l_level <=
807 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800808 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
810 }
811 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
812
813 mlog_exit_void();
814}
815
816static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
817{
818 mlog_entry_void();
819
820 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
821 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
822
823 /* Convert from RO to EX doesn't really need anything as our
824 * information is already up to data. Convert from NL to
825 * *anything* however should mark ourselves as needing an
826 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800827 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700828 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
830
831 lockres->l_level = lockres->l_requested;
832 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
833
834 mlog_exit_void();
835}
836
837static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
838{
839 mlog_entry_void();
840
Roel Kluin3cf0c502007-10-27 00:20:36 +0200841 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800842 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
843
Joel Beckerbd3e7612008-02-01 12:14:57 -0800844 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700845 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
846 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800847 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
848
849 lockres->l_level = lockres->l_requested;
850 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
851 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
852
853 mlog_exit_void();
854}
855
Mark Fashehccd979b2005-12-15 14:31:24 -0800856static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
857 int level)
858{
859 int needs_downconvert = 0;
860 mlog_entry_void();
861
862 assert_spin_locked(&lockres->l_lock);
863
864 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
865
866 if (level > lockres->l_blocking) {
867 /* only schedule a downconvert if we haven't already scheduled
868 * one that goes low enough to satisfy the level we're
869 * blocking. this also catches the case where we get
870 * duplicate BASTs */
871 if (ocfs2_highest_compat_lock_level(level) <
872 ocfs2_highest_compat_lock_level(lockres->l_blocking))
873 needs_downconvert = 1;
874
875 lockres->l_blocking = level;
876 }
877
878 mlog_exit(needs_downconvert);
879 return needs_downconvert;
880}
881
Joel Beckerde551242008-02-01 14:45:08 -0800882/*
883 * OCFS2_LOCK_PENDING and l_pending_gen.
884 *
885 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
886 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
887 * for more details on the race.
888 *
889 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
890 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
891 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
892 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
893 * the caller is going to try to clear PENDING again. If nothing else is
894 * happening, __lockres_clear_pending() sees PENDING is unset and does
895 * nothing.
896 *
897 * But what if another path (eg downconvert thread) has just started a
898 * new locking action? The other path has re-set PENDING. Our path
899 * cannot clear PENDING, because that will re-open the original race
900 * window.
901 *
902 * [Example]
903 *
904 * ocfs2_meta_lock()
905 * ocfs2_cluster_lock()
906 * set BUSY
907 * set PENDING
908 * drop l_lock
909 * ocfs2_dlm_lock()
910 * ocfs2_locking_ast() ocfs2_downconvert_thread()
911 * clear PENDING ocfs2_unblock_lock()
912 * take_l_lock
913 * !BUSY
914 * ocfs2_prepare_downconvert()
915 * set BUSY
916 * set PENDING
917 * drop l_lock
918 * take l_lock
919 * clear PENDING
920 * drop l_lock
921 * <window>
922 * ocfs2_dlm_lock()
923 *
924 * So as you can see, we now have a window where l_lock is not held,
925 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
926 *
927 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
928 * set by ocfs2_prepare_downconvert(). That wasn't nice.
929 *
930 * To solve this we introduce l_pending_gen. A call to
931 * lockres_clear_pending() will only do so when it is passed a generation
932 * number that matches the lockres. lockres_set_pending() will return the
933 * current generation number. When ocfs2_cluster_lock() goes to clear
934 * PENDING, it passes the generation it got from set_pending(). In our
935 * example above, the generation numbers will *not* match. Thus,
936 * ocfs2_cluster_lock() will not clear the PENDING set by
937 * ocfs2_prepare_downconvert().
938 */
939
940/* Unlocked version for ocfs2_locking_ast() */
941static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
942 unsigned int generation,
943 struct ocfs2_super *osb)
944{
945 assert_spin_locked(&lockres->l_lock);
946
947 /*
948 * The ast and locking functions can race us here. The winner
949 * will clear pending, the loser will not.
950 */
951 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
952 (lockres->l_pending_gen != generation))
953 return;
954
955 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
956 lockres->l_pending_gen++;
957
958 /*
959 * The downconvert thread may have skipped us because we
960 * were PENDING. Wake it up.
961 */
962 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
963 ocfs2_wake_downconvert_thread(osb);
964}
965
966/* Locked version for callers of ocfs2_dlm_lock() */
967static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
968 unsigned int generation,
969 struct ocfs2_super *osb)
970{
971 unsigned long flags;
972
973 spin_lock_irqsave(&lockres->l_lock, flags);
974 __lockres_clear_pending(lockres, generation, osb);
975 spin_unlock_irqrestore(&lockres->l_lock, flags);
976}
977
978static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
979{
980 assert_spin_locked(&lockres->l_lock);
981 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
982
983 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
984
985 return lockres->l_pending_gen;
986}
987
988
Mark Fashehaa2623a2006-09-12 21:58:23 -0700989static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800990{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700991 struct ocfs2_lock_res *lockres = opaque;
992 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 int needs_downconvert;
994 unsigned long flags;
995
Joel Beckerbd3e7612008-02-01 12:14:57 -0800996 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800997
Mark Fashehaa2623a2006-09-12 21:58:23 -0700998 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
999 lockres->l_name, level, lockres->l_level,
1000 ocfs2_lock_type_string(lockres->l_type));
1001
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001002 /*
1003 * We can skip the bast for locks which don't enable caching -
1004 * they'll be dropped at the earliest possible time anyway.
1005 */
1006 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1007 return;
1008
Mark Fashehccd979b2005-12-15 14:31:24 -08001009 spin_lock_irqsave(&lockres->l_lock, flags);
1010 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1011 if (needs_downconvert)
1012 ocfs2_schedule_blocked_lock(osb, lockres);
1013 spin_unlock_irqrestore(&lockres->l_lock, flags);
1014
Mark Fashehd680efe2006-09-08 14:14:34 -07001015 wake_up(&lockres->l_event);
1016
Mark Fasheh34d024f2007-09-24 15:56:19 -07001017 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001018}
1019
Mark Fashehe92d57d2006-09-12 21:34:35 -07001020static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -08001021{
Mark Fashehe92d57d2006-09-12 21:34:35 -07001022 struct ocfs2_lock_res *lockres = opaque;
Joel Beckerde551242008-02-01 14:45:08 -08001023 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001024 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001025 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001026
1027 spin_lock_irqsave(&lockres->l_lock, flags);
1028
David Teigland1693a5c2008-01-30 16:52:53 -08001029 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1030
1031 if (status == -EAGAIN) {
1032 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1033 goto out;
1034 }
1035
1036 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001037 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001038 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 spin_unlock_irqrestore(&lockres->l_lock, flags);
1040 return;
1041 }
1042
1043 switch(lockres->l_action) {
1044 case OCFS2_AST_ATTACH:
1045 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001046 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 break;
1048 case OCFS2_AST_CONVERT:
1049 ocfs2_generic_handle_convert_action(lockres);
1050 break;
1051 case OCFS2_AST_DOWNCONVERT:
1052 ocfs2_generic_handle_downconvert_action(lockres);
1053 break;
1054 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001055 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1056 "lockres flags = 0x%lx, unlock action: %u\n",
1057 lockres->l_name, lockres->l_action, lockres->l_flags,
1058 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 BUG();
1060 }
David Teigland1693a5c2008-01-30 16:52:53 -08001061out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 /* set it to something invalid so if we get called again we
1063 * can catch it. */
1064 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001065
Joel Beckerde551242008-02-01 14:45:08 -08001066 /* Did we try to cancel this lock? Clear that state */
1067 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1068 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1069
1070 /*
1071 * We may have beaten the locking functions here. We certainly
1072 * know that dlm_lock() has been called :-)
1073 * Because we can't have two lock calls in flight at once, we
1074 * can use lockres->l_pending_gen.
1075 */
1076 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1077
Mark Fashehccd979b2005-12-15 14:31:24 -08001078 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001079 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001080}
1081
Mark Fashehccd979b2005-12-15 14:31:24 -08001082static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1083 int convert)
1084{
1085 unsigned long flags;
1086
1087 mlog_entry_void();
1088 spin_lock_irqsave(&lockres->l_lock, flags);
1089 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1090 if (convert)
1091 lockres->l_action = OCFS2_AST_INVALID;
1092 else
1093 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1094 spin_unlock_irqrestore(&lockres->l_lock, flags);
1095
1096 wake_up(&lockres->l_event);
1097 mlog_exit_void();
1098}
1099
1100/* Note: If we detect another process working on the lock (i.e.,
1101 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1102 * to do the right thing in that case.
1103 */
1104static int ocfs2_lock_create(struct ocfs2_super *osb,
1105 struct ocfs2_lock_res *lockres,
1106 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001107 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001108{
1109 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001110 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001111 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001112
1113 mlog_entry_void();
1114
Joel Beckerbd3e7612008-02-01 12:14:57 -08001115 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 dlm_flags);
1117
1118 spin_lock_irqsave(&lockres->l_lock, flags);
1119 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1120 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1121 spin_unlock_irqrestore(&lockres->l_lock, flags);
1122 goto bail;
1123 }
1124
1125 lockres->l_action = OCFS2_AST_ATTACH;
1126 lockres->l_requested = level;
1127 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001128 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001129 spin_unlock_irqrestore(&lockres->l_lock, flags);
1130
Joel Becker4670c462008-02-01 14:39:35 -08001131 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001132 level,
1133 &lockres->l_lksb,
1134 dlm_flags,
1135 lockres->l_name,
1136 OCFS2_LOCK_ID_MAX_LEN - 1,
1137 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001138 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001139 if (ret) {
1140 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001141 ocfs2_recover_from_dlm_error(lockres, 1);
1142 }
1143
Joel Becker7431cd72008-02-01 12:15:37 -08001144 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001145
1146bail:
1147 mlog_exit(ret);
1148 return ret;
1149}
1150
1151static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1152 int flag)
1153{
1154 unsigned long flags;
1155 int ret;
1156
1157 spin_lock_irqsave(&lockres->l_lock, flags);
1158 ret = lockres->l_flags & flag;
1159 spin_unlock_irqrestore(&lockres->l_lock, flags);
1160
1161 return ret;
1162}
1163
1164static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1165
1166{
1167 wait_event(lockres->l_event,
1168 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1169}
1170
1171static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1172
1173{
1174 wait_event(lockres->l_event,
1175 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1176}
1177
1178/* predict what lock level we'll be dropping down to on behalf
1179 * of another node, and return true if the currently wanted
1180 * level will be compatible with it. */
1181static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1182 int wanted)
1183{
1184 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1185
1186 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1187}
1188
1189static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1190{
1191 INIT_LIST_HEAD(&mw->mw_item);
1192 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001193 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001194}
1195
1196static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1197{
1198 wait_for_completion(&mw->mw_complete);
1199 /* Re-arm the completion in case we want to wait on it again */
1200 INIT_COMPLETION(mw->mw_complete);
1201 return mw->mw_status;
1202}
1203
1204static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1205 struct ocfs2_mask_waiter *mw,
1206 unsigned long mask,
1207 unsigned long goal)
1208{
1209 BUG_ON(!list_empty(&mw->mw_item));
1210
1211 assert_spin_locked(&lockres->l_lock);
1212
1213 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1214 mw->mw_mask = mask;
1215 mw->mw_goal = goal;
1216}
1217
1218/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1219 * if the mask still hadn't reached its goal */
1220static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1221 struct ocfs2_mask_waiter *mw)
1222{
1223 unsigned long flags;
1224 int ret = 0;
1225
1226 spin_lock_irqsave(&lockres->l_lock, flags);
1227 if (!list_empty(&mw->mw_item)) {
1228 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1229 ret = -EBUSY;
1230
1231 list_del_init(&mw->mw_item);
1232 init_completion(&mw->mw_complete);
1233 }
1234 spin_unlock_irqrestore(&lockres->l_lock, flags);
1235
1236 return ret;
1237
1238}
1239
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001240static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1241 struct ocfs2_lock_res *lockres)
1242{
1243 int ret;
1244
1245 ret = wait_for_completion_interruptible(&mw->mw_complete);
1246 if (ret)
1247 lockres_remove_mask_waiter(lockres, mw);
1248 else
1249 ret = mw->mw_status;
1250 /* Re-arm the completion in case we want to wait on it again */
1251 INIT_COMPLETION(mw->mw_complete);
1252 return ret;
1253}
1254
Mark Fashehccd979b2005-12-15 14:31:24 -08001255static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1256 struct ocfs2_lock_res *lockres,
1257 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001258 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 int arg_flags)
1260{
1261 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1263 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1264 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001265 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001266 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001267
1268 mlog_entry_void();
1269
1270 ocfs2_init_mask_waiter(&mw);
1271
Mark Fashehb80fc012006-09-12 22:08:14 -07001272 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001273 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001274
Mark Fashehccd979b2005-12-15 14:31:24 -08001275again:
1276 wait = 0;
1277
1278 if (catch_signals && signal_pending(current)) {
1279 ret = -ERESTARTSYS;
1280 goto out;
1281 }
1282
1283 spin_lock_irqsave(&lockres->l_lock, flags);
1284
1285 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1286 "Cluster lock called on freeing lockres %s! flags "
1287 "0x%lx\n", lockres->l_name, lockres->l_flags);
1288
1289 /* We only compare against the currently granted level
1290 * here. If the lock is blocked waiting on a downconvert,
1291 * we'll get caught below. */
1292 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1293 level > lockres->l_level) {
1294 /* is someone sitting in dlm_lock? If so, wait on
1295 * them. */
1296 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1297 wait = 1;
1298 goto unlock;
1299 }
1300
Mark Fashehccd979b2005-12-15 14:31:24 -08001301 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1302 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1303 /* is the lock is currently blocked on behalf of
1304 * another node */
1305 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1306 wait = 1;
1307 goto unlock;
1308 }
1309
1310 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001311 if (noqueue_attempted > 0) {
1312 ret = -EAGAIN;
1313 goto unlock;
1314 }
1315 if (lkm_flags & DLM_LKF_NOQUEUE)
1316 noqueue_attempted = 1;
1317
Mark Fashehccd979b2005-12-15 14:31:24 -08001318 if (lockres->l_action != OCFS2_AST_INVALID)
1319 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1320 lockres->l_name, lockres->l_action);
1321
Mark Fasheh019d1b22007-10-05 12:09:05 -07001322 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1323 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001324 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001325 } else {
1326 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001327 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001328 }
1329
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 lockres->l_requested = level;
1331 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001332 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001333 spin_unlock_irqrestore(&lockres->l_lock, flags);
1334
Joel Beckerbd3e7612008-02-01 12:14:57 -08001335 BUG_ON(level == DLM_LOCK_IV);
1336 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001337
1338 mlog(0, "lock %s, convert from %d to level = %d\n",
1339 lockres->l_name, lockres->l_level, level);
1340
1341 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001342 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001343 level,
1344 &lockres->l_lksb,
1345 lkm_flags,
1346 lockres->l_name,
1347 OCFS2_LOCK_ID_MAX_LEN - 1,
1348 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001349 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001350 if (ret) {
1351 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1352 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001353 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001354 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001355 }
1356 ocfs2_recover_from_dlm_error(lockres, 1);
1357 goto out;
1358 }
1359
Coly Li73ac36e2009-01-07 18:09:16 -08001360 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001361 lockres->l_name);
1362
1363 /* At this point we've gone inside the dlm and need to
1364 * complete our work regardless. */
1365 catch_signals = 0;
1366
1367 /* wait for busy to clear and carry on */
1368 goto again;
1369 }
1370
1371 /* Ok, if we get here then we're good to go. */
1372 ocfs2_inc_holders(lockres, level);
1373
1374 ret = 0;
1375unlock:
1376 spin_unlock_irqrestore(&lockres->l_lock, flags);
1377out:
1378 /*
1379 * This is helping work around a lock inversion between the page lock
1380 * and dlm locks. One path holds the page lock while calling aops
1381 * which block acquiring dlm locks. The voting thread holds dlm
1382 * locks while acquiring page locks while down converting data locks.
1383 * This block is helping an aop path notice the inversion and back
1384 * off to unlock its page lock before trying the dlm lock again.
1385 */
1386 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1387 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1388 wait = 0;
1389 if (lockres_remove_mask_waiter(lockres, &mw))
1390 ret = -EAGAIN;
1391 else
1392 goto again;
1393 }
1394 if (wait) {
1395 ret = ocfs2_wait_for_mask(&mw);
1396 if (ret == 0)
1397 goto again;
1398 mlog_errno(ret);
1399 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001400 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
1402 mlog_exit(ret);
1403 return ret;
1404}
1405
1406static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1407 struct ocfs2_lock_res *lockres,
1408 int level)
1409{
1410 unsigned long flags;
1411
1412 mlog_entry_void();
1413 spin_lock_irqsave(&lockres->l_lock, flags);
1414 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001415 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001416 spin_unlock_irqrestore(&lockres->l_lock, flags);
1417 mlog_exit_void();
1418}
1419
Adrian Bunkda661162006-11-20 03:24:28 +01001420static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1421 struct ocfs2_lock_res *lockres,
1422 int ex,
1423 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001424{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001425 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001426 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001427 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001428
1429 spin_lock_irqsave(&lockres->l_lock, flags);
1430 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1431 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1432 spin_unlock_irqrestore(&lockres->l_lock, flags);
1433
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001434 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001435}
1436
1437/* Grants us an EX lock on the data and metadata resources, skipping
1438 * the normal cluster directory lookup. Use this ONLY on newly created
1439 * inodes which other nodes can't possibly see, and which haven't been
1440 * hashed in the inode hash yet. This can give us a good performance
1441 * increase as it'll skip the network broadcast normally associated
1442 * with creating a new lock resource. */
1443int ocfs2_create_new_inode_locks(struct inode *inode)
1444{
1445 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001446 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001447
1448 BUG_ON(!inode);
1449 BUG_ON(!ocfs2_inode_is_new(inode));
1450
1451 mlog_entry_void();
1452
Mark Fashehb06970532006-03-03 10:24:33 -08001453 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001454
1455 /* NOTE: That we don't increment any of the holder counts, nor
1456 * do we add anything to a journal handle. Since this is
1457 * supposed to be a new inode which the cluster doesn't know
1458 * about yet, there is no need to. As far as the LVB handling
1459 * is concerned, this is basically like acquiring an EX lock
1460 * on a resource which has an invalid one -- we'll set it
1461 * valid when we release the EX. */
1462
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001463 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001464 if (ret) {
1465 mlog_errno(ret);
1466 goto bail;
1467 }
1468
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001469 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001470 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001471 * don't use a generation in their lock names.
1472 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001473 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001474 if (ret) {
1475 mlog_errno(ret);
1476 goto bail;
1477 }
1478
Tiger Yang50008632007-03-20 16:01:38 -07001479 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1480 if (ret) {
1481 mlog_errno(ret);
1482 goto bail;
1483 }
1484
Mark Fashehccd979b2005-12-15 14:31:24 -08001485bail:
1486 mlog_exit(ret);
1487 return ret;
1488}
1489
1490int ocfs2_rw_lock(struct inode *inode, int write)
1491{
1492 int status, level;
1493 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001494 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001495
1496 BUG_ON(!inode);
1497
1498 mlog_entry_void();
1499
Mark Fashehb06970532006-03-03 10:24:33 -08001500 mlog(0, "inode %llu take %s RW lock\n",
1501 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001502 write ? "EXMODE" : "PRMODE");
1503
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001504 if (ocfs2_mount_local(osb))
1505 return 0;
1506
Mark Fashehccd979b2005-12-15 14:31:24 -08001507 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1508
Joel Beckerbd3e7612008-02-01 12:14:57 -08001509 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001510
1511 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1512 0);
1513 if (status < 0)
1514 mlog_errno(status);
1515
1516 mlog_exit(status);
1517 return status;
1518}
1519
1520void ocfs2_rw_unlock(struct inode *inode, int write)
1521{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001522 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001523 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001524 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001525
1526 mlog_entry_void();
1527
Mark Fashehb06970532006-03-03 10:24:33 -08001528 mlog(0, "inode %llu drop %s RW lock\n",
1529 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001530 write ? "EXMODE" : "PRMODE");
1531
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001532 if (!ocfs2_mount_local(osb))
1533 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001534
1535 mlog_exit_void();
1536}
1537
Tiger Yang50008632007-03-20 16:01:38 -07001538/*
1539 * ocfs2_open_lock always get PR mode lock.
1540 */
1541int ocfs2_open_lock(struct inode *inode)
1542{
1543 int status = 0;
1544 struct ocfs2_lock_res *lockres;
1545 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1546
1547 BUG_ON(!inode);
1548
1549 mlog_entry_void();
1550
1551 mlog(0, "inode %llu take PRMODE open lock\n",
1552 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1553
1554 if (ocfs2_mount_local(osb))
1555 goto out;
1556
1557 lockres = &OCFS2_I(inode)->ip_open_lockres;
1558
1559 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001560 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001561 if (status < 0)
1562 mlog_errno(status);
1563
1564out:
1565 mlog_exit(status);
1566 return status;
1567}
1568
1569int ocfs2_try_open_lock(struct inode *inode, int write)
1570{
1571 int status = 0, level;
1572 struct ocfs2_lock_res *lockres;
1573 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1574
1575 BUG_ON(!inode);
1576
1577 mlog_entry_void();
1578
1579 mlog(0, "inode %llu try to take %s open lock\n",
1580 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1581 write ? "EXMODE" : "PRMODE");
1582
1583 if (ocfs2_mount_local(osb))
1584 goto out;
1585
1586 lockres = &OCFS2_I(inode)->ip_open_lockres;
1587
Joel Beckerbd3e7612008-02-01 12:14:57 -08001588 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001589
1590 /*
1591 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001592 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001593 * other nodes and the -EAGAIN will indicate to the caller that
1594 * this inode is still in use.
1595 */
1596 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001597 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001598
1599out:
1600 mlog_exit(status);
1601 return status;
1602}
1603
1604/*
1605 * ocfs2_open_unlock unlock PR and EX mode open locks.
1606 */
1607void ocfs2_open_unlock(struct inode *inode)
1608{
1609 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1610 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1611
1612 mlog_entry_void();
1613
1614 mlog(0, "inode %llu drop open lock\n",
1615 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1616
1617 if (ocfs2_mount_local(osb))
1618 goto out;
1619
1620 if(lockres->l_ro_holders)
1621 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001622 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001623 if(lockres->l_ex_holders)
1624 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001625 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001626
1627out:
1628 mlog_exit_void();
1629}
1630
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001631static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1632 int level)
1633{
1634 int ret;
1635 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1636 unsigned long flags;
1637 struct ocfs2_mask_waiter mw;
1638
1639 ocfs2_init_mask_waiter(&mw);
1640
1641retry_cancel:
1642 spin_lock_irqsave(&lockres->l_lock, flags);
1643 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1644 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1645 if (ret) {
1646 spin_unlock_irqrestore(&lockres->l_lock, flags);
1647 ret = ocfs2_cancel_convert(osb, lockres);
1648 if (ret < 0) {
1649 mlog_errno(ret);
1650 goto out;
1651 }
1652 goto retry_cancel;
1653 }
1654 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1655 spin_unlock_irqrestore(&lockres->l_lock, flags);
1656
1657 ocfs2_wait_for_mask(&mw);
1658 goto retry_cancel;
1659 }
1660
1661 ret = -ERESTARTSYS;
1662 /*
1663 * We may still have gotten the lock, in which case there's no
1664 * point to restarting the syscall.
1665 */
1666 if (lockres->l_level == level)
1667 ret = 0;
1668
1669 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1670 lockres->l_flags, lockres->l_level, lockres->l_action);
1671
1672 spin_unlock_irqrestore(&lockres->l_lock, flags);
1673
1674out:
1675 return ret;
1676}
1677
1678/*
1679 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1680 * flock() calls. The locking approach this requires is sufficiently
1681 * different from all other cluster lock types that we implement a
1682 * seperate path to the "low-level" dlm calls. In particular:
1683 *
1684 * - No optimization of lock levels is done - we take at exactly
1685 * what's been requested.
1686 *
1687 * - No lock caching is employed. We immediately downconvert to
1688 * no-lock at unlock time. This also means flock locks never go on
1689 * the blocking list).
1690 *
1691 * - Since userspace can trivially deadlock itself with flock, we make
1692 * sure to allow cancellation of a misbehaving applications flock()
1693 * request.
1694 *
1695 * - Access to any flock lockres doesn't require concurrency, so we
1696 * can simplify the code by requiring the caller to guarantee
1697 * serialization of dlmglue flock calls.
1698 */
1699int ocfs2_file_lock(struct file *file, int ex, int trylock)
1700{
Mark Fashehe988cf12008-07-10 09:25:39 -07001701 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1702 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001703 unsigned long flags;
1704 struct ocfs2_file_private *fp = file->private_data;
1705 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1706 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1707 struct ocfs2_mask_waiter mw;
1708
1709 ocfs2_init_mask_waiter(&mw);
1710
1711 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001712 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001713 mlog(ML_ERROR,
1714 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1715 "level: %u\n", lockres->l_name, lockres->l_flags,
1716 lockres->l_level);
1717 return -EINVAL;
1718 }
1719
1720 spin_lock_irqsave(&lockres->l_lock, flags);
1721 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1722 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1723 spin_unlock_irqrestore(&lockres->l_lock, flags);
1724
1725 /*
1726 * Get the lock at NLMODE to start - that way we
1727 * can cancel the upconvert request if need be.
1728 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001729 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001730 if (ret < 0) {
1731 mlog_errno(ret);
1732 goto out;
1733 }
1734
1735 ret = ocfs2_wait_for_mask(&mw);
1736 if (ret) {
1737 mlog_errno(ret);
1738 goto out;
1739 }
1740 spin_lock_irqsave(&lockres->l_lock, flags);
1741 }
1742
1743 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001744 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001745 lockres->l_requested = level;
1746 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1747
1748 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1749 spin_unlock_irqrestore(&lockres->l_lock, flags);
1750
Joel Becker4670c462008-02-01 14:39:35 -08001751 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001752 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1753 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001754 if (ret) {
1755 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001756 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001757 ret = -EINVAL;
1758 }
1759
1760 ocfs2_recover_from_dlm_error(lockres, 1);
1761 lockres_remove_mask_waiter(lockres, &mw);
1762 goto out;
1763 }
1764
1765 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1766 if (ret == -ERESTARTSYS) {
1767 /*
1768 * Userspace can cause deadlock itself with
1769 * flock(). Current behavior locally is to allow the
1770 * deadlock, but abort the system call if a signal is
1771 * received. We follow this example, otherwise a
1772 * poorly written program could sit in kernel until
1773 * reboot.
1774 *
1775 * Handling this is a bit more complicated for Ocfs2
1776 * though. We can't exit this function with an
1777 * outstanding lock request, so a cancel convert is
1778 * required. We intentionally overwrite 'ret' - if the
1779 * cancel fails and the lock was granted, it's easier
1780 * to just bubble sucess back up to the user.
1781 */
1782 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001783 } else if (!ret && (level > lockres->l_level)) {
1784 /* Trylock failed asynchronously */
1785 BUG_ON(!trylock);
1786 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001787 }
1788
1789out:
1790
1791 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1792 lockres->l_name, ex, trylock, ret);
1793 return ret;
1794}
1795
1796void ocfs2_file_unlock(struct file *file)
1797{
1798 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001799 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001800 unsigned long flags;
1801 struct ocfs2_file_private *fp = file->private_data;
1802 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1803 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1804 struct ocfs2_mask_waiter mw;
1805
1806 ocfs2_init_mask_waiter(&mw);
1807
1808 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1809 return;
1810
Mark Fashehe988cf12008-07-10 09:25:39 -07001811 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001812 return;
1813
1814 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1815 lockres->l_name, lockres->l_flags, lockres->l_level,
1816 lockres->l_action);
1817
1818 spin_lock_irqsave(&lockres->l_lock, flags);
1819 /*
1820 * Fake a blocking ast for the downconvert code.
1821 */
1822 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001823 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001824
Mark Fashehe988cf12008-07-10 09:25:39 -07001825 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001826 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1827 spin_unlock_irqrestore(&lockres->l_lock, flags);
1828
Mark Fashehe988cf12008-07-10 09:25:39 -07001829 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001830 if (ret) {
1831 mlog_errno(ret);
1832 return;
1833 }
1834
1835 ret = ocfs2_wait_for_mask(&mw);
1836 if (ret)
1837 mlog_errno(ret);
1838}
1839
Mark Fasheh34d024f2007-09-24 15:56:19 -07001840static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1841 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001842{
1843 int kick = 0;
1844
1845 mlog_entry_void();
1846
1847 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001848 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001849 * condition. */
1850 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1851 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001852 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001853 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1854 kick = 1;
1855 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001856 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001857 if (!lockres->l_ex_holders)
1858 kick = 1;
1859 break;
1860 default:
1861 BUG();
1862 }
1863 }
1864
1865 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001866 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001867
1868 mlog_exit_void();
1869}
1870
Mark Fashehccd979b2005-12-15 14:31:24 -08001871#define OCFS2_SEC_BITS 34
1872#define OCFS2_SEC_SHIFT (64 - 34)
1873#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1874
1875/* LVB only has room for 64 bits of time here so we pack it for
1876 * now. */
1877static u64 ocfs2_pack_timespec(struct timespec *spec)
1878{
1879 u64 res;
1880 u64 sec = spec->tv_sec;
1881 u32 nsec = spec->tv_nsec;
1882
1883 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1884
1885 return res;
1886}
1887
1888/* Call this with the lockres locked. I am reasonably sure we don't
1889 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001890 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001891static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1892{
1893 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001894 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001895 struct ocfs2_meta_lvb *lvb;
1896
1897 mlog_entry_void();
1898
Mark Fasheha641dc22008-12-24 16:03:48 -08001899 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001900
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001901 /*
1902 * Invalidate the LVB of a deleted inode - this way other
1903 * nodes are forced to go to disk and discover the new inode
1904 * status.
1905 */
1906 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1907 lvb->lvb_version = 0;
1908 goto out;
1909 }
1910
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001911 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001912 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1913 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1914 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1915 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1916 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1917 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1918 lvb->lvb_iatime_packed =
1919 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1920 lvb->lvb_ictime_packed =
1921 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1922 lvb->lvb_imtime_packed =
1923 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001924 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001925 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001926 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001927
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001928out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001929 mlog_meta_lvb(0, lockres);
1930
1931 mlog_exit_void();
1932}
1933
1934static void ocfs2_unpack_timespec(struct timespec *spec,
1935 u64 packed_time)
1936{
1937 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1938 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1939}
1940
1941static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1942{
1943 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001944 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001945 struct ocfs2_meta_lvb *lvb;
1946
1947 mlog_entry_void();
1948
1949 mlog_meta_lvb(0, lockres);
1950
Mark Fasheha641dc22008-12-24 16:03:48 -08001951 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001952
1953 /* We're safe here without the lockres lock... */
1954 spin_lock(&oi->ip_lock);
1955 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1956 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1957
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001958 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001959 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001960 ocfs2_set_inode_flags(inode);
1961
Mark Fashehccd979b2005-12-15 14:31:24 -08001962 /* fast-symlinks are a special case */
1963 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1964 inode->i_blocks = 0;
1965 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001966 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001967
1968 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1969 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1970 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1971 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1972 ocfs2_unpack_timespec(&inode->i_atime,
1973 be64_to_cpu(lvb->lvb_iatime_packed));
1974 ocfs2_unpack_timespec(&inode->i_mtime,
1975 be64_to_cpu(lvb->lvb_imtime_packed));
1976 ocfs2_unpack_timespec(&inode->i_ctime,
1977 be64_to_cpu(lvb->lvb_ictime_packed));
1978 spin_unlock(&oi->ip_lock);
1979
1980 mlog_exit_void();
1981}
1982
Mark Fashehf9e2d822006-09-12 15:35:49 -07001983static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1984 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001985{
Mark Fasheha641dc22008-12-24 16:03:48 -08001986 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001987
Joel Becker1c520df2009-06-19 15:14:13 -07001988 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
1989 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07001990 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 return 1;
1992 return 0;
1993}
1994
1995/* Determine whether a lock resource needs to be refreshed, and
1996 * arbitrate who gets to refresh it.
1997 *
1998 * 0 means no refresh needed.
1999 *
2000 * > 0 means you need to refresh this and you MUST call
2001 * ocfs2_complete_lock_res_refresh afterwards. */
2002static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2003{
2004 unsigned long flags;
2005 int status = 0;
2006
2007 mlog_entry_void();
2008
2009refresh_check:
2010 spin_lock_irqsave(&lockres->l_lock, flags);
2011 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2012 spin_unlock_irqrestore(&lockres->l_lock, flags);
2013 goto bail;
2014 }
2015
2016 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2017 spin_unlock_irqrestore(&lockres->l_lock, flags);
2018
2019 ocfs2_wait_on_refreshing_lock(lockres);
2020 goto refresh_check;
2021 }
2022
2023 /* Ok, I'll be the one to refresh this lock. */
2024 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2025 spin_unlock_irqrestore(&lockres->l_lock, flags);
2026
2027 status = 1;
2028bail:
2029 mlog_exit(status);
2030 return status;
2031}
2032
2033/* If status is non zero, I'll mark it as not being in refresh
2034 * anymroe, but i won't clear the needs refresh flag. */
2035static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2036 int status)
2037{
2038 unsigned long flags;
2039 mlog_entry_void();
2040
2041 spin_lock_irqsave(&lockres->l_lock, flags);
2042 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2043 if (!status)
2044 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2045 spin_unlock_irqrestore(&lockres->l_lock, flags);
2046
2047 wake_up(&lockres->l_event);
2048
2049 mlog_exit_void();
2050}
2051
2052/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002053static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002054 struct buffer_head **bh)
2055{
2056 int status = 0;
2057 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002058 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002059 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002060 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002061
2062 mlog_entry_void();
2063
Mark Fashehbe9e9862007-04-18 15:22:08 -07002064 if (ocfs2_mount_local(osb))
2065 goto bail;
2066
Mark Fashehccd979b2005-12-15 14:31:24 -08002067 spin_lock(&oi->ip_lock);
2068 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002069 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002070 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002071 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002072 spin_unlock(&oi->ip_lock);
2073 status = -ENOENT;
2074 goto bail;
2075 }
2076 spin_unlock(&oi->ip_lock);
2077
Mark Fashehbe9e9862007-04-18 15:22:08 -07002078 if (!ocfs2_should_refresh_lock_res(lockres))
2079 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002080
2081 /* This will discard any caching information we might have had
2082 * for the inode metadata. */
2083 ocfs2_metadata_cache_purge(inode);
2084
Mark Fasheh83418972007-04-23 18:53:12 -07002085 ocfs2_extent_map_trunc(inode, 0);
2086
Mark Fashehbe9e9862007-04-18 15:22:08 -07002087 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002088 mlog(0, "Trusting LVB on inode %llu\n",
2089 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002090 ocfs2_refresh_inode_from_lvb(inode);
2091 } else {
2092 /* Boo, we have to go to disk. */
2093 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002094 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002095 if (status < 0) {
2096 mlog_errno(status);
2097 goto bail_refresh;
2098 }
2099 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2100
2101 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002102 * locking an invalid object. ocfs2_read_inode_block()
2103 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002104 *
2105 * We bug on a stale inode here because we checked
2106 * above whether it was wiped from disk. The wiping
2107 * node provides a guarantee that we receive that
2108 * message and can mark the inode before dropping any
2109 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002110 mlog_bug_on_msg(inode->i_generation !=
2111 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002112 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002113 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002114 (unsigned long long)oi->ip_blkno,
2115 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002116 inode->i_generation);
2117 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2118 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002119 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2120 (unsigned long long)oi->ip_blkno,
2121 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002122 le32_to_cpu(fe->i_flags));
2123
2124 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002125 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002126 }
2127
2128 status = 0;
2129bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002130 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002131bail:
2132 mlog_exit(status);
2133 return status;
2134}
2135
2136static int ocfs2_assign_bh(struct inode *inode,
2137 struct buffer_head **ret_bh,
2138 struct buffer_head *passed_bh)
2139{
2140 int status;
2141
2142 if (passed_bh) {
2143 /* Ok, the update went to disk for us, use the
2144 * returned bh. */
2145 *ret_bh = passed_bh;
2146 get_bh(*ret_bh);
2147
2148 return 0;
2149 }
2150
Joel Beckerb657c952008-11-13 14:49:11 -08002151 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002152 if (status < 0)
2153 mlog_errno(status);
2154
2155 return status;
2156}
2157
2158/*
2159 * returns < 0 error if the callback will never be called, otherwise
2160 * the result of the lock will be communicated via the callback.
2161 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002162int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002163 struct buffer_head **ret_bh,
2164 int ex,
2165 int arg_flags)
2166{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002167 int status, level, acquired;
2168 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002169 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002170 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2171 struct buffer_head *local_bh = NULL;
2172
2173 BUG_ON(!inode);
2174
2175 mlog_entry_void();
2176
Mark Fashehb06970532006-03-03 10:24:33 -08002177 mlog(0, "inode %llu, take %s META lock\n",
2178 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002179 ex ? "EXMODE" : "PRMODE");
2180
2181 status = 0;
2182 acquired = 0;
2183 /* We'll allow faking a readonly metadata lock for
2184 * rodevices. */
2185 if (ocfs2_is_hard_readonly(osb)) {
2186 if (ex)
2187 status = -EROFS;
2188 goto bail;
2189 }
2190
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002191 if (ocfs2_mount_local(osb))
2192 goto local;
2193
Mark Fashehccd979b2005-12-15 14:31:24 -08002194 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002195 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002196
Mark Fashehe63aecb62007-10-18 15:30:42 -07002197 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002198 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002199 dlm_flags = 0;
2200 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002201 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002202
2203 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2204 if (status < 0) {
2205 if (status != -EAGAIN && status != -EIOCBRETRY)
2206 mlog_errno(status);
2207 goto bail;
2208 }
2209
2210 /* Notify the error cleanup path to drop the cluster lock. */
2211 acquired = 1;
2212
2213 /* We wait twice because a node may have died while we were in
2214 * the lower dlm layers. The second time though, we've
2215 * committed to owning this lock so we don't allow signals to
2216 * abort the operation. */
2217 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002218 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002219
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002220local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002221 /*
2222 * We only see this flag if we're being called from
2223 * ocfs2_read_locked_inode(). It means we're locking an inode
2224 * which hasn't been populated yet, so clear the refresh flag
2225 * and let the caller handle it.
2226 */
2227 if (inode->i_state & I_NEW) {
2228 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002229 if (lockres)
2230 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002231 goto bail;
2232 }
2233
Mark Fashehccd979b2005-12-15 14:31:24 -08002234 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002235 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002236 * may or may not read one, depending on what's in the
2237 * LVB. The result of all of this is that we've *only* gone to
2238 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002239 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002240 if (status < 0) {
2241 if (status != -ENOENT)
2242 mlog_errno(status);
2243 goto bail;
2244 }
2245
2246 if (ret_bh) {
2247 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2248 if (status < 0) {
2249 mlog_errno(status);
2250 goto bail;
2251 }
2252 }
2253
Mark Fashehccd979b2005-12-15 14:31:24 -08002254bail:
2255 if (status < 0) {
2256 if (ret_bh && (*ret_bh)) {
2257 brelse(*ret_bh);
2258 *ret_bh = NULL;
2259 }
2260 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002261 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002262 }
2263
2264 if (local_bh)
2265 brelse(local_bh);
2266
2267 mlog_exit(status);
2268 return status;
2269}
2270
2271/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002272 * This is working around a lock inversion between tasks acquiring DLM
2273 * locks while holding a page lock and the downconvert thread which
2274 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002275 *
2276 * ** These _with_page variantes are only intended to be called from aop
2277 * methods that hold page locks and return a very specific *positive* error
2278 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2279 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002280 * The DLM is called such that it returns -EAGAIN if it would have
2281 * blocked waiting for the downconvert thread. In that case we unlock
2282 * our page so the downconvert thread can make progress. Once we've
2283 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2284 * that called us can bubble that back up into the VFS who will then
2285 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002286 *
2287 * We do a blocking lock and immediate unlock before returning, though, so that
2288 * the lock has a great chance of being cached on this node by the time the VFS
2289 * calls back to retry the aop. This has a potential to livelock as nodes
2290 * ping locks back and forth, but that's a risk we're willing to take to avoid
2291 * the lock inversion simply.
2292 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002293int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002294 struct buffer_head **ret_bh,
2295 int ex,
2296 struct page *page)
2297{
2298 int ret;
2299
Mark Fashehe63aecb62007-10-18 15:30:42 -07002300 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002301 if (ret == -EAGAIN) {
2302 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002303 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2304 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002305 ret = AOP_TRUNCATED_PAGE;
2306 }
2307
2308 return ret;
2309}
2310
Mark Fashehe63aecb62007-10-18 15:30:42 -07002311int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002312 struct vfsmount *vfsmnt,
2313 int *level)
2314{
2315 int ret;
2316
2317 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002318 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002319 if (ret < 0) {
2320 mlog_errno(ret);
2321 return ret;
2322 }
2323
2324 /*
2325 * If we should update atime, we will get EX lock,
2326 * otherwise we just get PR lock.
2327 */
2328 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2329 struct buffer_head *bh = NULL;
2330
Mark Fashehe63aecb62007-10-18 15:30:42 -07002331 ocfs2_inode_unlock(inode, 0);
2332 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002333 if (ret < 0) {
2334 mlog_errno(ret);
2335 return ret;
2336 }
2337 *level = 1;
2338 if (ocfs2_should_update_atime(inode, vfsmnt))
2339 ocfs2_update_inode_atime(inode, bh);
2340 if (bh)
2341 brelse(bh);
2342 } else
2343 *level = 0;
2344
2345 mlog_exit(ret);
2346 return ret;
2347}
2348
Mark Fashehe63aecb62007-10-18 15:30:42 -07002349void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002350 int ex)
2351{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002352 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002353 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002355
2356 mlog_entry_void();
2357
Mark Fashehb06970532006-03-03 10:24:33 -08002358 mlog(0, "inode %llu drop %s META lock\n",
2359 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002360 ex ? "EXMODE" : "PRMODE");
2361
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002362 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2363 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2365
2366 mlog_exit_void();
2367}
2368
Sunil Mushrandf152c22009-06-22 11:40:07 -07002369int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002370{
2371 struct ocfs2_lock_res *lockres;
2372 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002373 int status = 0;
2374
Sunil Mushrandf152c22009-06-22 11:40:07 -07002375 if (ocfs2_is_hard_readonly(osb))
2376 return -EROFS;
2377
2378 if (ocfs2_mount_local(osb))
2379 return 0;
2380
Srinivas Eeda83273932009-06-03 17:02:55 -07002381 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002382 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002383 if (status < 0)
2384 return status;
2385
2386 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002387 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2388 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002389 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002390 else
2391 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2392
Srinivas Eeda83273932009-06-03 17:02:55 -07002393 return status;
2394}
2395
Sunil Mushrandf152c22009-06-22 11:40:07 -07002396void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002397{
2398 struct ocfs2_lock_res *lockres;
2399 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002400
Sunil Mushrandf152c22009-06-22 11:40:07 -07002401 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2402 lockres = &osb->osb_orphan_scan.os_lockres;
2403 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2404 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2405 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2406 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2407 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002408}
2409
Mark Fashehccd979b2005-12-15 14:31:24 -08002410int ocfs2_super_lock(struct ocfs2_super *osb,
2411 int ex)
2412{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002413 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002414 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002415 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002416
2417 mlog_entry_void();
2418
2419 if (ocfs2_is_hard_readonly(osb))
2420 return -EROFS;
2421
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002422 if (ocfs2_mount_local(osb))
2423 goto bail;
2424
Mark Fashehccd979b2005-12-15 14:31:24 -08002425 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2426 if (status < 0) {
2427 mlog_errno(status);
2428 goto bail;
2429 }
2430
2431 /* The super block lock path is really in the best position to
2432 * know when resources covered by the lock need to be
2433 * refreshed, so we do it here. Of course, making sense of
2434 * everything is up to the caller :) */
2435 status = ocfs2_should_refresh_lock_res(lockres);
2436 if (status < 0) {
2437 mlog_errno(status);
2438 goto bail;
2439 }
2440 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002441 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002442
2443 ocfs2_complete_lock_res_refresh(lockres, status);
2444
2445 if (status < 0)
2446 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002447 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002448 }
2449bail:
2450 mlog_exit(status);
2451 return status;
2452}
2453
2454void ocfs2_super_unlock(struct ocfs2_super *osb,
2455 int ex)
2456{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002457 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002458 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2459
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002460 if (!ocfs2_mount_local(osb))
2461 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002462}
2463
2464int ocfs2_rename_lock(struct ocfs2_super *osb)
2465{
2466 int status;
2467 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2468
2469 if (ocfs2_is_hard_readonly(osb))
2470 return -EROFS;
2471
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002472 if (ocfs2_mount_local(osb))
2473 return 0;
2474
Joel Beckerbd3e7612008-02-01 12:14:57 -08002475 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002476 if (status < 0)
2477 mlog_errno(status);
2478
2479 return status;
2480}
2481
2482void ocfs2_rename_unlock(struct ocfs2_super *osb)
2483{
2484 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2485
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002486 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002487 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002488}
2489
wengang wang6ca497a2009-03-06 21:29:10 +08002490int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2491{
2492 int status;
2493 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2494
2495 if (ocfs2_is_hard_readonly(osb))
2496 return -EROFS;
2497
2498 if (ocfs2_mount_local(osb))
2499 return 0;
2500
2501 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2502 0, 0);
2503 if (status < 0)
2504 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2505
2506 return status;
2507}
2508
2509void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2510{
2511 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2512
2513 if (!ocfs2_mount_local(osb))
2514 ocfs2_cluster_unlock(osb, lockres,
2515 ex ? LKM_EXMODE : LKM_PRMODE);
2516}
2517
Mark Fashehd680efe2006-09-08 14:14:34 -07002518int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2519{
2520 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002521 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002522 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2523 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2524
2525 BUG_ON(!dl);
2526
2527 if (ocfs2_is_hard_readonly(osb))
2528 return -EROFS;
2529
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002530 if (ocfs2_mount_local(osb))
2531 return 0;
2532
Mark Fashehd680efe2006-09-08 14:14:34 -07002533 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2534 if (ret < 0)
2535 mlog_errno(ret);
2536
2537 return ret;
2538}
2539
2540void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2541{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002542 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002543 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2544 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2545
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002546 if (!ocfs2_mount_local(osb))
2547 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002548}
2549
Mark Fashehccd979b2005-12-15 14:31:24 -08002550/* Reference counting of the dlm debug structure. We want this because
2551 * open references on the debug inodes can live on after a mount, so
2552 * we can't rely on the ocfs2_super to always exist. */
2553static void ocfs2_dlm_debug_free(struct kref *kref)
2554{
2555 struct ocfs2_dlm_debug *dlm_debug;
2556
2557 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2558
2559 kfree(dlm_debug);
2560}
2561
2562void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2563{
2564 if (dlm_debug)
2565 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2566}
2567
2568static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2569{
2570 kref_get(&debug->d_refcnt);
2571}
2572
2573struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2574{
2575 struct ocfs2_dlm_debug *dlm_debug;
2576
2577 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2578 if (!dlm_debug) {
2579 mlog_errno(-ENOMEM);
2580 goto out;
2581 }
2582
2583 kref_init(&dlm_debug->d_refcnt);
2584 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2585 dlm_debug->d_locking_state = NULL;
2586out:
2587 return dlm_debug;
2588}
2589
2590/* Access to this is arbitrated for us via seq_file->sem. */
2591struct ocfs2_dlm_seq_priv {
2592 struct ocfs2_dlm_debug *p_dlm_debug;
2593 struct ocfs2_lock_res p_iter_res;
2594 struct ocfs2_lock_res p_tmp_res;
2595};
2596
2597static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2598 struct ocfs2_dlm_seq_priv *priv)
2599{
2600 struct ocfs2_lock_res *iter, *ret = NULL;
2601 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2602
2603 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2604
2605 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2606 /* discover the head of the list */
2607 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2608 mlog(0, "End of list found, %p\n", ret);
2609 break;
2610 }
2611
2612 /* We track our "dummy" iteration lockres' by a NULL
2613 * l_ops field. */
2614 if (iter->l_ops != NULL) {
2615 ret = iter;
2616 break;
2617 }
2618 }
2619
2620 return ret;
2621}
2622
2623static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2624{
2625 struct ocfs2_dlm_seq_priv *priv = m->private;
2626 struct ocfs2_lock_res *iter;
2627
2628 spin_lock(&ocfs2_dlm_tracking_lock);
2629 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2630 if (iter) {
2631 /* Since lockres' have the lifetime of their container
2632 * (which can be inodes, ocfs2_supers, etc) we want to
2633 * copy this out to a temporary lockres while still
2634 * under the spinlock. Obviously after this we can't
2635 * trust any pointers on the copy returned, but that's
2636 * ok as the information we want isn't typically held
2637 * in them. */
2638 priv->p_tmp_res = *iter;
2639 iter = &priv->p_tmp_res;
2640 }
2641 spin_unlock(&ocfs2_dlm_tracking_lock);
2642
2643 return iter;
2644}
2645
2646static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2647{
2648}
2649
2650static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2651{
2652 struct ocfs2_dlm_seq_priv *priv = m->private;
2653 struct ocfs2_lock_res *iter = v;
2654 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2655
2656 spin_lock(&ocfs2_dlm_tracking_lock);
2657 iter = ocfs2_dlm_next_res(iter, priv);
2658 list_del_init(&dummy->l_debug_list);
2659 if (iter) {
2660 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2661 priv->p_tmp_res = *iter;
2662 iter = &priv->p_tmp_res;
2663 }
2664 spin_unlock(&ocfs2_dlm_tracking_lock);
2665
2666 return iter;
2667}
2668
2669/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002670#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002671static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2672{
2673 int i;
2674 char *lvb;
2675 struct ocfs2_lock_res *lockres = v;
2676
2677 if (!lockres)
2678 return -EINVAL;
2679
Mark Fashehd680efe2006-09-08 14:14:34 -07002680 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2681
2682 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2683 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2684 lockres->l_name,
2685 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2686 else
2687 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2688
2689 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002690 "0x%lx\t"
2691 "0x%x\t"
2692 "0x%x\t"
2693 "%u\t"
2694 "%u\t"
2695 "%d\t"
2696 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002697 lockres->l_level,
2698 lockres->l_flags,
2699 lockres->l_action,
2700 lockres->l_unlock_action,
2701 lockres->l_ro_holders,
2702 lockres->l_ex_holders,
2703 lockres->l_requested,
2704 lockres->l_blocking);
2705
2706 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002707 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002708 for(i = 0; i < DLM_LVB_LEN; i++)
2709 seq_printf(m, "0x%x\t", lvb[i]);
2710
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002711#ifdef CONFIG_OCFS2_FS_STATS
2712# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2713# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2714# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2715# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2716# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2717# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2718# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2719# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2720# define lock_refresh(_l) (_l)->l_lock_refresh
2721#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002722# define lock_num_prmode(_l) (0ULL)
2723# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002724# define lock_num_prmode_failed(_l) (0)
2725# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002726# define lock_total_prmode(_l) (0ULL)
2727# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002728# define lock_max_prmode(_l) (0)
2729# define lock_max_exmode(_l) (0)
2730# define lock_refresh(_l) (0)
2731#endif
2732 /* The following seq_print was added in version 2 of this output */
2733 seq_printf(m, "%llu\t"
2734 "%llu\t"
2735 "%u\t"
2736 "%u\t"
2737 "%llu\t"
2738 "%llu\t"
2739 "%u\t"
2740 "%u\t"
2741 "%u\t",
2742 lock_num_prmode(lockres),
2743 lock_num_exmode(lockres),
2744 lock_num_prmode_failed(lockres),
2745 lock_num_exmode_failed(lockres),
2746 lock_total_prmode(lockres),
2747 lock_total_exmode(lockres),
2748 lock_max_prmode(lockres),
2749 lock_max_exmode(lockres),
2750 lock_refresh(lockres));
2751
Mark Fashehccd979b2005-12-15 14:31:24 -08002752 /* End the line */
2753 seq_printf(m, "\n");
2754 return 0;
2755}
2756
Jan Engelhardt90d99772008-01-22 20:52:20 +01002757static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002758 .start = ocfs2_dlm_seq_start,
2759 .stop = ocfs2_dlm_seq_stop,
2760 .next = ocfs2_dlm_seq_next,
2761 .show = ocfs2_dlm_seq_show,
2762};
2763
2764static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2765{
2766 struct seq_file *seq = (struct seq_file *) file->private_data;
2767 struct ocfs2_dlm_seq_priv *priv = seq->private;
2768 struct ocfs2_lock_res *res = &priv->p_iter_res;
2769
2770 ocfs2_remove_lockres_tracking(res);
2771 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2772 return seq_release_private(inode, file);
2773}
2774
2775static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2776{
2777 int ret;
2778 struct ocfs2_dlm_seq_priv *priv;
2779 struct seq_file *seq;
2780 struct ocfs2_super *osb;
2781
2782 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2783 if (!priv) {
2784 ret = -ENOMEM;
2785 mlog_errno(ret);
2786 goto out;
2787 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002788 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002789 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2790 priv->p_dlm_debug = osb->osb_dlm_debug;
2791 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2792
2793 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2794 if (ret) {
2795 kfree(priv);
2796 mlog_errno(ret);
2797 goto out;
2798 }
2799
2800 seq = (struct seq_file *) file->private_data;
2801 seq->private = priv;
2802
2803 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2804 priv->p_dlm_debug);
2805
2806out:
2807 return ret;
2808}
2809
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002810static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002811 .open = ocfs2_dlm_debug_open,
2812 .release = ocfs2_dlm_debug_release,
2813 .read = seq_read,
2814 .llseek = seq_lseek,
2815};
2816
2817static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2818{
2819 int ret = 0;
2820 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2821
2822 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2823 S_IFREG|S_IRUSR,
2824 osb->osb_debug_root,
2825 osb,
2826 &ocfs2_dlm_debug_fops);
2827 if (!dlm_debug->d_locking_state) {
2828 ret = -EINVAL;
2829 mlog(ML_ERROR,
2830 "Unable to create locking state debugfs file.\n");
2831 goto out;
2832 }
2833
2834 ocfs2_get_dlm_debug(dlm_debug);
2835out:
2836 return ret;
2837}
2838
2839static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2840{
2841 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2842
2843 if (dlm_debug) {
2844 debugfs_remove(dlm_debug->d_locking_state);
2845 ocfs2_put_dlm_debug(dlm_debug);
2846 }
2847}
2848
2849int ocfs2_dlm_init(struct ocfs2_super *osb)
2850{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002851 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002852 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002853
2854 mlog_entry_void();
2855
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002856 if (ocfs2_mount_local(osb)) {
2857 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002858 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002859 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002860
Mark Fashehccd979b2005-12-15 14:31:24 -08002861 status = ocfs2_dlm_init_debug(osb);
2862 if (status < 0) {
2863 mlog_errno(status);
2864 goto bail;
2865 }
2866
Mark Fasheh34d024f2007-09-24 15:56:19 -07002867 /* launch downconvert thread */
2868 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2869 if (IS_ERR(osb->dc_task)) {
2870 status = PTR_ERR(osb->dc_task);
2871 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002872 mlog_errno(status);
2873 goto bail;
2874 }
2875
Mark Fashehccd979b2005-12-15 14:31:24 -08002876 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002877 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2878 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002879 strlen(osb->uuid_str),
2880 ocfs2_do_node_down, osb,
2881 &conn);
2882 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002883 mlog_errno(status);
2884 goto bail;
2885 }
2886
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002887 status = ocfs2_cluster_this_node(&osb->node_num);
2888 if (status < 0) {
2889 mlog_errno(status);
2890 mlog(ML_ERROR,
2891 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002892 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002893 goto bail;
2894 }
2895
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002896local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002897 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2898 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002899 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07002900 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002901
Joel Becker4670c462008-02-01 14:39:35 -08002902 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002903
2904 status = 0;
2905bail:
2906 if (status < 0) {
2907 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002908 if (osb->dc_task)
2909 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002910 }
2911
2912 mlog_exit(status);
2913 return status;
2914}
2915
Joel Becker286eaa92008-02-01 15:03:57 -08002916void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2917 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002918{
2919 mlog_entry_void();
2920
Mark Fashehccd979b2005-12-15 14:31:24 -08002921 ocfs2_drop_osb_locks(osb);
2922
Joel Becker4670c462008-02-01 14:39:35 -08002923 /*
2924 * Now that we have dropped all locks and ocfs2_dismount_volume()
2925 * has disabled recovery, the DLM won't be talking to us. It's
2926 * safe to tear things down before disconnecting the cluster.
2927 */
2928
Mark Fasheh34d024f2007-09-24 15:56:19 -07002929 if (osb->dc_task) {
2930 kthread_stop(osb->dc_task);
2931 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002932 }
2933
2934 ocfs2_lock_res_free(&osb->osb_super_lockres);
2935 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08002936 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07002937 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002938
Joel Becker286eaa92008-02-01 15:03:57 -08002939 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08002940 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002941
2942 ocfs2_dlm_shutdown_debug(osb);
2943
2944 mlog_exit_void();
2945}
2946
Joel Becker7431cd72008-02-01 12:15:37 -08002947static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002948{
2949 struct ocfs2_lock_res *lockres = opaque;
2950 unsigned long flags;
2951
2952 mlog_entry_void();
2953
2954 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2955 lockres->l_unlock_action);
2956
2957 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08002958 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08002959 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2960 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002961 lockres->l_unlock_action);
2962 spin_unlock_irqrestore(&lockres->l_lock, flags);
2963 return;
2964 }
2965
2966 switch(lockres->l_unlock_action) {
2967 case OCFS2_UNLOCK_CANCEL_CONVERT:
2968 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2969 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08002970 /* Downconvert thread may have requeued this lock, we
2971 * need to wake it. */
2972 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2973 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08002974 break;
2975 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002976 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002977 break;
2978 default:
2979 BUG();
2980 }
2981
2982 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08002983 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08002984 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06002985 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002986
2987 mlog_exit_void();
2988}
2989
Mark Fashehccd979b2005-12-15 14:31:24 -08002990static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002991 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002992{
Joel Becker7431cd72008-02-01 12:15:37 -08002993 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002994 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002995 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002996
2997 /* We didn't get anywhere near actually using this lockres. */
2998 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2999 goto out;
3000
Mark Fashehb80fc012006-09-12 22:08:14 -07003001 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003002 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003003
Mark Fashehccd979b2005-12-15 14:31:24 -08003004 spin_lock_irqsave(&lockres->l_lock, flags);
3005
3006 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3007 "lockres %s, flags 0x%lx\n",
3008 lockres->l_name, lockres->l_flags);
3009
3010 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3011 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3012 "%u, unlock_action = %u\n",
3013 lockres->l_name, lockres->l_flags, lockres->l_action,
3014 lockres->l_unlock_action);
3015
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
3017
3018 /* XXX: Today we just wait on any busy
3019 * locks... Perhaps we need to cancel converts in the
3020 * future? */
3021 ocfs2_wait_on_busy_lock(lockres);
3022
3023 spin_lock_irqsave(&lockres->l_lock, flags);
3024 }
3025
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003026 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3027 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003028 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003029 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3030 lockres->l_ops->set_lvb(lockres);
3031 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003032
3033 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3034 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3035 lockres->l_name);
3036 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3037 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3038
3039 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3040 spin_unlock_irqrestore(&lockres->l_lock, flags);
3041 goto out;
3042 }
3043
3044 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3045
3046 /* make sure we never get here while waiting for an ast to
3047 * fire. */
3048 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3049
3050 /* is this necessary? */
3051 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3052 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3053 spin_unlock_irqrestore(&lockres->l_lock, flags);
3054
3055 mlog(0, "lock %s\n", lockres->l_name);
3056
Joel Becker4670c462008-02-01 14:39:35 -08003057 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003058 lockres);
3059 if (ret) {
3060 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003061 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003062 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003063 BUG();
3064 }
Coly Li73ac36e2009-01-07 18:09:16 -08003065 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003066 lockres->l_name);
3067
3068 ocfs2_wait_on_busy_lock(lockres);
3069out:
3070 mlog_exit(0);
3071 return 0;
3072}
3073
3074/* Mark the lockres as being dropped. It will no longer be
3075 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003076 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08003077 * it safe to drop.
3078 *
3079 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3080void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3081{
3082 int status;
3083 struct ocfs2_mask_waiter mw;
3084 unsigned long flags;
3085
3086 ocfs2_init_mask_waiter(&mw);
3087
3088 spin_lock_irqsave(&lockres->l_lock, flags);
3089 lockres->l_flags |= OCFS2_LOCK_FREEING;
3090 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3091 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3092 spin_unlock_irqrestore(&lockres->l_lock, flags);
3093
3094 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3095
3096 status = ocfs2_wait_for_mask(&mw);
3097 if (status)
3098 mlog_errno(status);
3099
3100 spin_lock_irqsave(&lockres->l_lock, flags);
3101 }
3102 spin_unlock_irqrestore(&lockres->l_lock, flags);
3103}
3104
Mark Fashehd680efe2006-09-08 14:14:34 -07003105void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3106 struct ocfs2_lock_res *lockres)
3107{
3108 int ret;
3109
3110 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003111 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003112 if (ret)
3113 mlog_errno(ret);
3114}
3115
Mark Fashehccd979b2005-12-15 14:31:24 -08003116static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3117{
Mark Fashehd680efe2006-09-08 14:14:34 -07003118 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3119 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003120 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003121 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003122}
3123
Mark Fashehccd979b2005-12-15 14:31:24 -08003124int ocfs2_drop_inode_locks(struct inode *inode)
3125{
3126 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003127
3128 mlog_entry_void();
3129
3130 /* No need to call ocfs2_mark_lockres_freeing here -
3131 * ocfs2_clear_inode has done it for us. */
3132
3133 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003134 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003135 if (err < 0)
3136 mlog_errno(err);
3137
3138 status = err;
3139
3140 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003141 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003142 if (err < 0)
3143 mlog_errno(err);
3144 if (err < 0 && !status)
3145 status = err;
3146
3147 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003148 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003149 if (err < 0)
3150 mlog_errno(err);
3151 if (err < 0 && !status)
3152 status = err;
3153
3154 mlog_exit(status);
3155 return status;
3156}
3157
Joel Beckerde551242008-02-01 14:45:08 -08003158static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3159 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003160{
3161 assert_spin_locked(&lockres->l_lock);
3162
Joel Beckerbd3e7612008-02-01 12:14:57 -08003163 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003164
3165 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003166 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003167 lockres->l_level, new_level);
3168 BUG();
3169 }
3170
3171 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3172 lockres->l_name, new_level, lockres->l_blocking);
3173
3174 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3175 lockres->l_requested = new_level;
3176 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003177 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003178}
3179
3180static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3181 struct ocfs2_lock_res *lockres,
3182 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003183 int lvb,
3184 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003185{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003186 int ret;
3187 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003188
3189 mlog_entry_void();
3190
3191 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003192 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003193
Joel Becker4670c462008-02-01 14:39:35 -08003194 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003195 new_level,
3196 &lockres->l_lksb,
3197 dlm_flags,
3198 lockres->l_name,
3199 OCFS2_LOCK_ID_MAX_LEN - 1,
3200 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003201 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003202 if (ret) {
3203 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003204 ocfs2_recover_from_dlm_error(lockres, 1);
3205 goto bail;
3206 }
3207
3208 ret = 0;
3209bail:
3210 mlog_exit(ret);
3211 return ret;
3212}
3213
Joel Becker24ef1812008-01-29 17:37:32 -08003214/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003215static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3216 struct ocfs2_lock_res *lockres)
3217{
3218 assert_spin_locked(&lockres->l_lock);
3219
3220 mlog_entry_void();
3221 mlog(0, "lock %s\n", lockres->l_name);
3222
3223 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3224 /* If we're already trying to cancel a lock conversion
3225 * then just drop the spinlock and allow the caller to
3226 * requeue this lock. */
3227
3228 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3229 return 0;
3230 }
3231
3232 /* were we in a convert when we got the bast fire? */
3233 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3234 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3235 /* set things up for the unlockast to know to just
3236 * clear out the ast_action and unset busy, etc. */
3237 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3238
3239 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3240 "lock %s, invalid flags: 0x%lx\n",
3241 lockres->l_name, lockres->l_flags);
3242
3243 return 1;
3244}
3245
3246static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3247 struct ocfs2_lock_res *lockres)
3248{
3249 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003250
3251 mlog_entry_void();
3252 mlog(0, "lock %s\n", lockres->l_name);
3253
Joel Becker4670c462008-02-01 14:39:35 -08003254 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003255 DLM_LKF_CANCEL, lockres);
3256 if (ret) {
3257 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003258 ocfs2_recover_from_dlm_error(lockres, 0);
3259 }
3260
Joel Becker24ef1812008-01-29 17:37:32 -08003261 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003262
3263 mlog_exit(ret);
3264 return ret;
3265}
3266
Mark Fashehb5e500e2006-09-13 22:01:16 -07003267static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3268 struct ocfs2_lock_res *lockres,
3269 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003270{
3271 unsigned long flags;
3272 int blocking;
3273 int new_level;
3274 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003275 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003276 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003277
3278 mlog_entry_void();
3279
3280 spin_lock_irqsave(&lockres->l_lock, flags);
3281
3282 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3283
3284recheck:
3285 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003286 /* XXX
3287 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3288 * exists entirely for one reason - another thread has set
3289 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3290 *
3291 * If we do ocfs2_cancel_convert() before the other thread
3292 * calls dlm_lock(), our cancel will do nothing. We will
3293 * get no ast, and we will have no way of knowing the
3294 * cancel failed. Meanwhile, the other thread will call
3295 * into dlm_lock() and wait...forever.
3296 *
3297 * Why forever? Because another node has asked for the
3298 * lock first; that's why we're here in unblock_lock().
3299 *
3300 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3301 * set, we just requeue the unblock. Only when the other
3302 * thread has called dlm_lock() and cleared PENDING will
3303 * we then cancel their request.
3304 *
3305 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3306 * at the same time they set OCFS2_DLM_BUSY. They must
3307 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3308 */
3309 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3310 goto leave_requeue;
3311
Mark Fashehd680efe2006-09-08 14:14:34 -07003312 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003313 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3314 spin_unlock_irqrestore(&lockres->l_lock, flags);
3315 if (ret) {
3316 ret = ocfs2_cancel_convert(osb, lockres);
3317 if (ret < 0)
3318 mlog_errno(ret);
3319 }
3320 goto leave;
3321 }
3322
3323 /* if we're blocking an exclusive and we have *any* holders,
3324 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003325 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003326 && (lockres->l_ex_holders || lockres->l_ro_holders))
3327 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003328
3329 /* If it's a PR we're blocking, then only
3330 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003331 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003332 lockres->l_ex_holders)
3333 goto leave_requeue;
3334
3335 /*
3336 * Can we get a lock in this state if the holder counts are
3337 * zero? The meta data unblock code used to check this.
3338 */
3339 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3340 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3341 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003342
Mark Fasheh16d5b952006-09-13 21:10:12 -07003343 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3344
3345 if (lockres->l_ops->check_downconvert
3346 && !lockres->l_ops->check_downconvert(lockres, new_level))
3347 goto leave_requeue;
3348
Mark Fashehccd979b2005-12-15 14:31:24 -08003349 /* If we get here, then we know that there are no more
3350 * incompatible holders (and anyone asking for an incompatible
3351 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003352 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003353 goto downconvert;
3354
3355 /* Some lockres types want to do a bit of work before
3356 * downconverting a lock. Allow that here. The worker function
3357 * may sleep, so we save off a copy of what we're blocking as
3358 * it may change while we're not holding the spin lock. */
3359 blocking = lockres->l_blocking;
3360 spin_unlock_irqrestore(&lockres->l_lock, flags);
3361
Mark Fashehcc567d82006-09-13 21:52:21 -07003362 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003363
3364 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3365 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003366
3367 spin_lock_irqsave(&lockres->l_lock, flags);
3368 if (blocking != lockres->l_blocking) {
3369 /* If this changed underneath us, then we can't drop
3370 * it just yet. */
3371 goto recheck;
3372 }
3373
3374downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003375 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003376
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003377 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003378 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003379 set_lvb = 1;
3380
3381 /*
3382 * We only set the lvb if the lock has been fully
3383 * refreshed - otherwise we risk setting stale
3384 * data. Otherwise, there's no need to actually clear
3385 * out the lvb here as it's value is still valid.
3386 */
3387 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3388 lockres->l_ops->set_lvb(lockres);
3389 }
3390
Joel Beckerde551242008-02-01 14:45:08 -08003391 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003392 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003393 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3394 gen);
3395
Mark Fashehccd979b2005-12-15 14:31:24 -08003396leave:
3397 mlog_exit(ret);
3398 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003399
3400leave_requeue:
3401 spin_unlock_irqrestore(&lockres->l_lock, flags);
3402 ctl->requeue = 1;
3403
3404 mlog_exit(0);
3405 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003406}
3407
Mark Fashehd680efe2006-09-08 14:14:34 -07003408static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3409 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003410{
3411 struct inode *inode;
3412 struct address_space *mapping;
3413
Mark Fashehccd979b2005-12-15 14:31:24 -08003414 inode = ocfs2_lock_res_inode(lockres);
3415 mapping = inode->i_mapping;
3416
Mark Fasheh1044e402008-02-28 17:16:03 -08003417 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003418 goto out;
3419
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003420 /*
3421 * We need this before the filemap_fdatawrite() so that it can
3422 * transfer the dirty bit from the PTE to the
3423 * page. Unfortunately this means that even for EX->PR
3424 * downconverts, we'll lose our mappings and have to build
3425 * them up again.
3426 */
3427 unmap_mapping_range(mapping, 0, 0, 0);
3428
Mark Fashehccd979b2005-12-15 14:31:24 -08003429 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003430 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3431 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003432 }
3433 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003434 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003435 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003436 } else {
3437 /* We only need to wait on the I/O if we're not also
3438 * truncating pages because truncate_inode_pages waits
3439 * for us above. We don't truncate pages if we're
3440 * blocking anything < EXMODE because we want to keep
3441 * them around in that case. */
3442 filemap_fdatawait(mapping);
3443 }
3444
Mark Fashehf1f54062007-10-18 15:13:59 -07003445out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003446 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003447}
3448
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003449static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3450 int new_level)
3451{
3452 struct inode *inode = ocfs2_lock_res_inode(lockres);
3453 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3454
Joel Beckerbd3e7612008-02-01 12:14:57 -08003455 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3456 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003457
3458 if (checkpointed)
3459 return 1;
3460
3461 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3462 return 0;
3463}
3464
3465static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3466{
3467 struct inode *inode = ocfs2_lock_res_inode(lockres);
3468
3469 __ocfs2_stuff_meta_lvb(inode);
3470}
3471
Mark Fashehd680efe2006-09-08 14:14:34 -07003472/*
3473 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003474 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003475 * dlmglue API and push these off to the ocfs2_wq in the future.
3476 */
3477static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3478 struct ocfs2_lock_res *lockres)
3479{
3480 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3481 ocfs2_dentry_lock_put(osb, dl);
3482}
3483
3484/*
3485 * d_delete() matching dentries before the lock downconvert.
3486 *
3487 * At this point, any process waiting to destroy the
3488 * dentry_lock due to last ref count is stopped by the
3489 * OCFS2_LOCK_QUEUED flag.
3490 *
3491 * We have two potential problems
3492 *
3493 * 1) If we do the last reference drop on our dentry_lock (via dput)
3494 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3495 * the downconvert to finish. Instead we take an elevated
3496 * reference and push the drop until after we've completed our
3497 * unblock processing.
3498 *
3499 * 2) There might be another process with a final reference,
3500 * waiting on us to finish processing. If this is the case, we
3501 * detect it and exit out - there's no more dentries anyway.
3502 */
3503static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3504 int blocking)
3505{
3506 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3507 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3508 struct dentry *dentry;
3509 unsigned long flags;
3510 int extra_ref = 0;
3511
3512 /*
3513 * This node is blocking another node from getting a read
3514 * lock. This happens when we've renamed within a
3515 * directory. We've forced the other nodes to d_delete(), but
3516 * we never actually dropped our lock because it's still
3517 * valid. The downconvert code will retain a PR for this node,
3518 * so there's no further work to do.
3519 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003520 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003521 return UNBLOCK_CONTINUE;
3522
3523 /*
3524 * Mark this inode as potentially orphaned. The code in
3525 * ocfs2_delete_inode() will figure out whether it actually
3526 * needs to be freed or not.
3527 */
3528 spin_lock(&oi->ip_lock);
3529 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3530 spin_unlock(&oi->ip_lock);
3531
3532 /*
3533 * Yuck. We need to make sure however that the check of
3534 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3535 * respect to a reference decrement or the setting of that
3536 * flag.
3537 */
3538 spin_lock_irqsave(&lockres->l_lock, flags);
3539 spin_lock(&dentry_attach_lock);
3540 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3541 && dl->dl_count) {
3542 dl->dl_count++;
3543 extra_ref = 1;
3544 }
3545 spin_unlock(&dentry_attach_lock);
3546 spin_unlock_irqrestore(&lockres->l_lock, flags);
3547
3548 mlog(0, "extra_ref = %d\n", extra_ref);
3549
3550 /*
3551 * We have a process waiting on us in ocfs2_dentry_iput(),
3552 * which means we can't have any more outstanding
3553 * aliases. There's no need to do any more work.
3554 */
3555 if (!extra_ref)
3556 return UNBLOCK_CONTINUE;
3557
3558 spin_lock(&dentry_attach_lock);
3559 while (1) {
3560 dentry = ocfs2_find_local_alias(dl->dl_inode,
3561 dl->dl_parent_blkno, 1);
3562 if (!dentry)
3563 break;
3564 spin_unlock(&dentry_attach_lock);
3565
3566 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3567 dentry->d_name.name);
3568
3569 /*
3570 * The following dcache calls may do an
3571 * iput(). Normally we don't want that from the
3572 * downconverting thread, but in this case it's ok
3573 * because the requesting node already has an
3574 * exclusive lock on the inode, so it can't be queued
3575 * for a downconvert.
3576 */
3577 d_delete(dentry);
3578 dput(dentry);
3579
3580 spin_lock(&dentry_attach_lock);
3581 }
3582 spin_unlock(&dentry_attach_lock);
3583
3584 /*
3585 * If we are the last holder of this dentry lock, there is no
3586 * reason to downconvert so skip straight to the unlock.
3587 */
3588 if (dl->dl_count == 1)
3589 return UNBLOCK_STOP_POST;
3590
3591 return UNBLOCK_CONTINUE_POST;
3592}
3593
Jan Kara9e33d692008-08-25 19:56:50 +02003594static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3595{
3596 struct ocfs2_qinfo_lvb *lvb;
3597 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3598 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3599 oinfo->dqi_gi.dqi_type);
3600
3601 mlog_entry_void();
3602
Mark Fasheha641dc22008-12-24 16:03:48 -08003603 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003604 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3605 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3606 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3607 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3608 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3609 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3610 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3611
3612 mlog_exit_void();
3613}
3614
3615void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3616{
3617 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3618 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3619 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3620
3621 mlog_entry_void();
3622 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3623 ocfs2_cluster_unlock(osb, lockres, level);
3624 mlog_exit_void();
3625}
3626
3627static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3628{
3629 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3630 oinfo->dqi_gi.dqi_type);
3631 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3632 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003633 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003634 struct ocfs2_global_disk_dqinfo *gdinfo;
3635 int status = 0;
3636
Joel Becker1c520df2009-06-19 15:14:13 -07003637 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3638 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003639 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3640 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3641 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3642 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3643 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3644 oinfo->dqi_gi.dqi_free_entry =
3645 be32_to_cpu(lvb->lvb_free_entry);
3646 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003647 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3648 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003649 mlog_errno(status);
3650 goto bail;
3651 }
3652 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3653 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3654 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3655 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3656 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3657 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3658 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3659 oinfo->dqi_gi.dqi_free_entry =
3660 le32_to_cpu(gdinfo->dqi_free_entry);
3661 brelse(bh);
3662 ocfs2_track_lock_refresh(lockres);
3663 }
3664
3665bail:
3666 return status;
3667}
3668
3669/* Lock quota info, this function expects at least shared lock on the quota file
3670 * so that we can safely refresh quota info from disk. */
3671int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3672{
3673 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3674 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3675 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3676 int status = 0;
3677
3678 mlog_entry_void();
3679
3680 /* On RO devices, locking really isn't needed... */
3681 if (ocfs2_is_hard_readonly(osb)) {
3682 if (ex)
3683 status = -EROFS;
3684 goto bail;
3685 }
3686 if (ocfs2_mount_local(osb))
3687 goto bail;
3688
3689 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3690 if (status < 0) {
3691 mlog_errno(status);
3692 goto bail;
3693 }
3694 if (!ocfs2_should_refresh_lock_res(lockres))
3695 goto bail;
3696 /* OK, we have the lock but we need to refresh the quota info */
3697 status = ocfs2_refresh_qinfo(oinfo);
3698 if (status)
3699 ocfs2_qinfo_unlock(oinfo, ex);
3700 ocfs2_complete_lock_res_refresh(lockres, status);
3701bail:
3702 mlog_exit(status);
3703 return status;
3704}
3705
Joel Becker4670c462008-02-01 14:39:35 -08003706/*
3707 * This is the filesystem locking protocol. It provides the lock handling
3708 * hooks for the underlying DLM. It has a maximum version number.
3709 * The version number allows interoperability with systems running at
3710 * the same major number and an equal or smaller minor number.
3711 *
3712 * Whenever the filesystem does new things with locks (adds or removes a
3713 * lock, orders them differently, does different things underneath a lock),
3714 * the version must be changed. The protocol is negotiated when joining
3715 * the dlm domain. A node may join the domain if its major version is
3716 * identical to all other nodes and its minor version is greater than
3717 * or equal to all other nodes. When its minor version is greater than
3718 * the other nodes, it will run at the minor version specified by the
3719 * other nodes.
3720 *
3721 * If a locking change is made that will not be compatible with older
3722 * versions, the major number must be increased and the minor version set
3723 * to zero. If a change merely adds a behavior that can be disabled when
3724 * speaking to older versions, the minor version must be increased. If a
3725 * change adds a fully backwards compatible change (eg, LVB changes that
3726 * are just ignored by older versions), the version does not need to be
3727 * updated.
3728 */
Joel Becker24ef1812008-01-29 17:37:32 -08003729static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003730 .lp_max_version = {
3731 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3732 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3733 },
Joel Becker24ef1812008-01-29 17:37:32 -08003734 .lp_lock_ast = ocfs2_locking_ast,
3735 .lp_blocking_ast = ocfs2_blocking_ast,
3736 .lp_unlock_ast = ocfs2_unlock_ast,
3737};
3738
Joel Becker63e0c482008-01-30 16:58:36 -08003739void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003740{
Joel Becker63e0c482008-01-30 16:58:36 -08003741 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003742}
3743
Joel Becker24ef1812008-01-29 17:37:32 -08003744
Adrian Bunk00600052008-01-29 00:11:41 +02003745static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3746 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003747{
3748 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003749 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003750 unsigned long flags;
3751
3752 /* Our reference to the lockres in this function can be
3753 * considered valid until we remove the OCFS2_LOCK_QUEUED
3754 * flag. */
3755
3756 mlog_entry_void();
3757
3758 BUG_ON(!lockres);
3759 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003760
3761 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3762
3763 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003764 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003765 * still be marked with OCFS2_LOCK_FREEING after this check,
3766 * but short circuiting here will still save us some
3767 * performance. */
3768 spin_lock_irqsave(&lockres->l_lock, flags);
3769 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3770 goto unqueue;
3771 spin_unlock_irqrestore(&lockres->l_lock, flags);
3772
Mark Fashehb5e500e2006-09-13 22:01:16 -07003773 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003774 if (status < 0)
3775 mlog_errno(status);
3776
3777 spin_lock_irqsave(&lockres->l_lock, flags);
3778unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003779 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003780 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3781 } else
3782 ocfs2_schedule_blocked_lock(osb, lockres);
3783
3784 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003785 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003786 spin_unlock_irqrestore(&lockres->l_lock, flags);
3787
Mark Fashehd680efe2006-09-08 14:14:34 -07003788 if (ctl.unblock_action != UNBLOCK_CONTINUE
3789 && lockres->l_ops->post_unlock)
3790 lockres->l_ops->post_unlock(osb, lockres);
3791
Mark Fashehccd979b2005-12-15 14:31:24 -08003792 mlog_exit_void();
3793}
3794
3795static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3796 struct ocfs2_lock_res *lockres)
3797{
3798 mlog_entry_void();
3799
3800 assert_spin_locked(&lockres->l_lock);
3801
3802 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3803 /* Do not schedule a lock for downconvert when it's on
3804 * the way to destruction - any nodes wanting access
3805 * to the resource will get it soon. */
3806 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3807 lockres->l_name, lockres->l_flags);
3808 return;
3809 }
3810
3811 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3812
Mark Fasheh34d024f2007-09-24 15:56:19 -07003813 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003814 if (list_empty(&lockres->l_blocked_list)) {
3815 list_add_tail(&lockres->l_blocked_list,
3816 &osb->blocked_lock_list);
3817 osb->blocked_lock_count++;
3818 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003819 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003820
3821 mlog_exit_void();
3822}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003823
3824static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3825{
3826 unsigned long processed;
3827 struct ocfs2_lock_res *lockres;
3828
3829 mlog_entry_void();
3830
3831 spin_lock(&osb->dc_task_lock);
3832 /* grab this early so we know to try again if a state change and
3833 * wake happens part-way through our work */
3834 osb->dc_work_sequence = osb->dc_wake_sequence;
3835
3836 processed = osb->blocked_lock_count;
3837 while (processed) {
3838 BUG_ON(list_empty(&osb->blocked_lock_list));
3839
3840 lockres = list_entry(osb->blocked_lock_list.next,
3841 struct ocfs2_lock_res, l_blocked_list);
3842 list_del_init(&lockres->l_blocked_list);
3843 osb->blocked_lock_count--;
3844 spin_unlock(&osb->dc_task_lock);
3845
3846 BUG_ON(!processed);
3847 processed--;
3848
3849 ocfs2_process_blocked_lock(osb, lockres);
3850
3851 spin_lock(&osb->dc_task_lock);
3852 }
3853 spin_unlock(&osb->dc_task_lock);
3854
3855 mlog_exit_void();
3856}
3857
3858static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3859{
3860 int empty = 0;
3861
3862 spin_lock(&osb->dc_task_lock);
3863 if (list_empty(&osb->blocked_lock_list))
3864 empty = 1;
3865
3866 spin_unlock(&osb->dc_task_lock);
3867 return empty;
3868}
3869
3870static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3871{
3872 int should_wake = 0;
3873
3874 spin_lock(&osb->dc_task_lock);
3875 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3876 should_wake = 1;
3877 spin_unlock(&osb->dc_task_lock);
3878
3879 return should_wake;
3880}
3881
Adrian Bunk200bfae2008-02-17 10:20:38 +02003882static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003883{
3884 int status = 0;
3885 struct ocfs2_super *osb = arg;
3886
3887 /* only quit once we've been asked to stop and there is no more
3888 * work available */
3889 while (!(kthread_should_stop() &&
3890 ocfs2_downconvert_thread_lists_empty(osb))) {
3891
3892 wait_event_interruptible(osb->dc_event,
3893 ocfs2_downconvert_thread_should_wake(osb) ||
3894 kthread_should_stop());
3895
3896 mlog(0, "downconvert_thread: awoken\n");
3897
3898 ocfs2_downconvert_thread_do_work(osb);
3899 }
3900
3901 osb->dc_task = NULL;
3902 return status;
3903}
3904
3905void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3906{
3907 spin_lock(&osb->dc_task_lock);
3908 /* make sure the voting thread gets a swipe at whatever changes
3909 * the caller may have made to the voting state */
3910 osb->dc_wake_sequence++;
3911 spin_unlock(&osb->dc_task_lock);
3912 wake_up(&osb->dc_event);
3913}