blob: d3d1f9372f7e00a9ef4516ead4969d1d85308b5b [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
Srinivas Eeda83273932009-06-03 17:02:55 -07002369int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno, int ex)
2370{
2371 struct ocfs2_lock_res *lockres;
2372 struct ocfs2_orphan_scan_lvb *lvb;
2373 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2374 int status = 0;
2375
2376 lockres = &osb->osb_orphan_scan.os_lockres;
2377 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2378 if (status < 0)
2379 return status;
2380
2381 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002382 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2383 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002384 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002385 else
2386 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2387
Srinivas Eeda83273932009-06-03 17:02:55 -07002388 return status;
2389}
2390
2391void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno, int ex)
2392{
2393 struct ocfs2_lock_res *lockres;
2394 struct ocfs2_orphan_scan_lvb *lvb;
2395 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2396
2397 lockres = &osb->osb_orphan_scan.os_lockres;
2398 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2399 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2400 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2401 ocfs2_cluster_unlock(osb, lockres, level);
2402}
2403
Mark Fashehccd979b2005-12-15 14:31:24 -08002404int ocfs2_super_lock(struct ocfs2_super *osb,
2405 int ex)
2406{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002407 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002408 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002409 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002410
2411 mlog_entry_void();
2412
2413 if (ocfs2_is_hard_readonly(osb))
2414 return -EROFS;
2415
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002416 if (ocfs2_mount_local(osb))
2417 goto bail;
2418
Mark Fashehccd979b2005-12-15 14:31:24 -08002419 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2420 if (status < 0) {
2421 mlog_errno(status);
2422 goto bail;
2423 }
2424
2425 /* The super block lock path is really in the best position to
2426 * know when resources covered by the lock need to be
2427 * refreshed, so we do it here. Of course, making sense of
2428 * everything is up to the caller :) */
2429 status = ocfs2_should_refresh_lock_res(lockres);
2430 if (status < 0) {
2431 mlog_errno(status);
2432 goto bail;
2433 }
2434 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002435 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002436
2437 ocfs2_complete_lock_res_refresh(lockres, status);
2438
2439 if (status < 0)
2440 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002441 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002442 }
2443bail:
2444 mlog_exit(status);
2445 return status;
2446}
2447
2448void ocfs2_super_unlock(struct ocfs2_super *osb,
2449 int ex)
2450{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002451 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002452 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2453
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002454 if (!ocfs2_mount_local(osb))
2455 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002456}
2457
2458int ocfs2_rename_lock(struct ocfs2_super *osb)
2459{
2460 int status;
2461 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2462
2463 if (ocfs2_is_hard_readonly(osb))
2464 return -EROFS;
2465
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002466 if (ocfs2_mount_local(osb))
2467 return 0;
2468
Joel Beckerbd3e7612008-02-01 12:14:57 -08002469 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002470 if (status < 0)
2471 mlog_errno(status);
2472
2473 return status;
2474}
2475
2476void ocfs2_rename_unlock(struct ocfs2_super *osb)
2477{
2478 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2479
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002480 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002481 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002482}
2483
wengang wang6ca497a2009-03-06 21:29:10 +08002484int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2485{
2486 int status;
2487 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2488
2489 if (ocfs2_is_hard_readonly(osb))
2490 return -EROFS;
2491
2492 if (ocfs2_mount_local(osb))
2493 return 0;
2494
2495 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2496 0, 0);
2497 if (status < 0)
2498 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2499
2500 return status;
2501}
2502
2503void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2504{
2505 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2506
2507 if (!ocfs2_mount_local(osb))
2508 ocfs2_cluster_unlock(osb, lockres,
2509 ex ? LKM_EXMODE : LKM_PRMODE);
2510}
2511
Mark Fashehd680efe2006-09-08 14:14:34 -07002512int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2513{
2514 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002515 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002516 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2517 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2518
2519 BUG_ON(!dl);
2520
2521 if (ocfs2_is_hard_readonly(osb))
2522 return -EROFS;
2523
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002524 if (ocfs2_mount_local(osb))
2525 return 0;
2526
Mark Fashehd680efe2006-09-08 14:14:34 -07002527 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2528 if (ret < 0)
2529 mlog_errno(ret);
2530
2531 return ret;
2532}
2533
2534void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2535{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002536 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002537 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2538 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2539
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002540 if (!ocfs2_mount_local(osb))
2541 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002542}
2543
Mark Fashehccd979b2005-12-15 14:31:24 -08002544/* Reference counting of the dlm debug structure. We want this because
2545 * open references on the debug inodes can live on after a mount, so
2546 * we can't rely on the ocfs2_super to always exist. */
2547static void ocfs2_dlm_debug_free(struct kref *kref)
2548{
2549 struct ocfs2_dlm_debug *dlm_debug;
2550
2551 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2552
2553 kfree(dlm_debug);
2554}
2555
2556void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2557{
2558 if (dlm_debug)
2559 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2560}
2561
2562static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2563{
2564 kref_get(&debug->d_refcnt);
2565}
2566
2567struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2568{
2569 struct ocfs2_dlm_debug *dlm_debug;
2570
2571 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2572 if (!dlm_debug) {
2573 mlog_errno(-ENOMEM);
2574 goto out;
2575 }
2576
2577 kref_init(&dlm_debug->d_refcnt);
2578 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2579 dlm_debug->d_locking_state = NULL;
2580out:
2581 return dlm_debug;
2582}
2583
2584/* Access to this is arbitrated for us via seq_file->sem. */
2585struct ocfs2_dlm_seq_priv {
2586 struct ocfs2_dlm_debug *p_dlm_debug;
2587 struct ocfs2_lock_res p_iter_res;
2588 struct ocfs2_lock_res p_tmp_res;
2589};
2590
2591static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2592 struct ocfs2_dlm_seq_priv *priv)
2593{
2594 struct ocfs2_lock_res *iter, *ret = NULL;
2595 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2596
2597 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2598
2599 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2600 /* discover the head of the list */
2601 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2602 mlog(0, "End of list found, %p\n", ret);
2603 break;
2604 }
2605
2606 /* We track our "dummy" iteration lockres' by a NULL
2607 * l_ops field. */
2608 if (iter->l_ops != NULL) {
2609 ret = iter;
2610 break;
2611 }
2612 }
2613
2614 return ret;
2615}
2616
2617static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2618{
2619 struct ocfs2_dlm_seq_priv *priv = m->private;
2620 struct ocfs2_lock_res *iter;
2621
2622 spin_lock(&ocfs2_dlm_tracking_lock);
2623 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2624 if (iter) {
2625 /* Since lockres' have the lifetime of their container
2626 * (which can be inodes, ocfs2_supers, etc) we want to
2627 * copy this out to a temporary lockres while still
2628 * under the spinlock. Obviously after this we can't
2629 * trust any pointers on the copy returned, but that's
2630 * ok as the information we want isn't typically held
2631 * in them. */
2632 priv->p_tmp_res = *iter;
2633 iter = &priv->p_tmp_res;
2634 }
2635 spin_unlock(&ocfs2_dlm_tracking_lock);
2636
2637 return iter;
2638}
2639
2640static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2641{
2642}
2643
2644static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2645{
2646 struct ocfs2_dlm_seq_priv *priv = m->private;
2647 struct ocfs2_lock_res *iter = v;
2648 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2649
2650 spin_lock(&ocfs2_dlm_tracking_lock);
2651 iter = ocfs2_dlm_next_res(iter, priv);
2652 list_del_init(&dummy->l_debug_list);
2653 if (iter) {
2654 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2655 priv->p_tmp_res = *iter;
2656 iter = &priv->p_tmp_res;
2657 }
2658 spin_unlock(&ocfs2_dlm_tracking_lock);
2659
2660 return iter;
2661}
2662
2663/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002664#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002665static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2666{
2667 int i;
2668 char *lvb;
2669 struct ocfs2_lock_res *lockres = v;
2670
2671 if (!lockres)
2672 return -EINVAL;
2673
Mark Fashehd680efe2006-09-08 14:14:34 -07002674 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2675
2676 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2677 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2678 lockres->l_name,
2679 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2680 else
2681 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2682
2683 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002684 "0x%lx\t"
2685 "0x%x\t"
2686 "0x%x\t"
2687 "%u\t"
2688 "%u\t"
2689 "%d\t"
2690 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002691 lockres->l_level,
2692 lockres->l_flags,
2693 lockres->l_action,
2694 lockres->l_unlock_action,
2695 lockres->l_ro_holders,
2696 lockres->l_ex_holders,
2697 lockres->l_requested,
2698 lockres->l_blocking);
2699
2700 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002701 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002702 for(i = 0; i < DLM_LVB_LEN; i++)
2703 seq_printf(m, "0x%x\t", lvb[i]);
2704
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002705#ifdef CONFIG_OCFS2_FS_STATS
2706# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2707# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2708# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2709# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2710# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2711# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2712# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2713# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2714# define lock_refresh(_l) (_l)->l_lock_refresh
2715#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002716# define lock_num_prmode(_l) (0ULL)
2717# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002718# define lock_num_prmode_failed(_l) (0)
2719# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002720# define lock_total_prmode(_l) (0ULL)
2721# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002722# define lock_max_prmode(_l) (0)
2723# define lock_max_exmode(_l) (0)
2724# define lock_refresh(_l) (0)
2725#endif
2726 /* The following seq_print was added in version 2 of this output */
2727 seq_printf(m, "%llu\t"
2728 "%llu\t"
2729 "%u\t"
2730 "%u\t"
2731 "%llu\t"
2732 "%llu\t"
2733 "%u\t"
2734 "%u\t"
2735 "%u\t",
2736 lock_num_prmode(lockres),
2737 lock_num_exmode(lockres),
2738 lock_num_prmode_failed(lockres),
2739 lock_num_exmode_failed(lockres),
2740 lock_total_prmode(lockres),
2741 lock_total_exmode(lockres),
2742 lock_max_prmode(lockres),
2743 lock_max_exmode(lockres),
2744 lock_refresh(lockres));
2745
Mark Fashehccd979b2005-12-15 14:31:24 -08002746 /* End the line */
2747 seq_printf(m, "\n");
2748 return 0;
2749}
2750
Jan Engelhardt90d99772008-01-22 20:52:20 +01002751static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002752 .start = ocfs2_dlm_seq_start,
2753 .stop = ocfs2_dlm_seq_stop,
2754 .next = ocfs2_dlm_seq_next,
2755 .show = ocfs2_dlm_seq_show,
2756};
2757
2758static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2759{
2760 struct seq_file *seq = (struct seq_file *) file->private_data;
2761 struct ocfs2_dlm_seq_priv *priv = seq->private;
2762 struct ocfs2_lock_res *res = &priv->p_iter_res;
2763
2764 ocfs2_remove_lockres_tracking(res);
2765 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2766 return seq_release_private(inode, file);
2767}
2768
2769static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2770{
2771 int ret;
2772 struct ocfs2_dlm_seq_priv *priv;
2773 struct seq_file *seq;
2774 struct ocfs2_super *osb;
2775
2776 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2777 if (!priv) {
2778 ret = -ENOMEM;
2779 mlog_errno(ret);
2780 goto out;
2781 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002782 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002783 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2784 priv->p_dlm_debug = osb->osb_dlm_debug;
2785 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2786
2787 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2788 if (ret) {
2789 kfree(priv);
2790 mlog_errno(ret);
2791 goto out;
2792 }
2793
2794 seq = (struct seq_file *) file->private_data;
2795 seq->private = priv;
2796
2797 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2798 priv->p_dlm_debug);
2799
2800out:
2801 return ret;
2802}
2803
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002804static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002805 .open = ocfs2_dlm_debug_open,
2806 .release = ocfs2_dlm_debug_release,
2807 .read = seq_read,
2808 .llseek = seq_lseek,
2809};
2810
2811static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2812{
2813 int ret = 0;
2814 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2815
2816 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2817 S_IFREG|S_IRUSR,
2818 osb->osb_debug_root,
2819 osb,
2820 &ocfs2_dlm_debug_fops);
2821 if (!dlm_debug->d_locking_state) {
2822 ret = -EINVAL;
2823 mlog(ML_ERROR,
2824 "Unable to create locking state debugfs file.\n");
2825 goto out;
2826 }
2827
2828 ocfs2_get_dlm_debug(dlm_debug);
2829out:
2830 return ret;
2831}
2832
2833static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2834{
2835 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2836
2837 if (dlm_debug) {
2838 debugfs_remove(dlm_debug->d_locking_state);
2839 ocfs2_put_dlm_debug(dlm_debug);
2840 }
2841}
2842
2843int ocfs2_dlm_init(struct ocfs2_super *osb)
2844{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002845 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002846 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002847
2848 mlog_entry_void();
2849
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002850 if (ocfs2_mount_local(osb)) {
2851 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002852 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002853 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002854
Mark Fashehccd979b2005-12-15 14:31:24 -08002855 status = ocfs2_dlm_init_debug(osb);
2856 if (status < 0) {
2857 mlog_errno(status);
2858 goto bail;
2859 }
2860
Mark Fasheh34d024f2007-09-24 15:56:19 -07002861 /* launch downconvert thread */
2862 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2863 if (IS_ERR(osb->dc_task)) {
2864 status = PTR_ERR(osb->dc_task);
2865 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002866 mlog_errno(status);
2867 goto bail;
2868 }
2869
Mark Fashehccd979b2005-12-15 14:31:24 -08002870 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002871 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2872 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002873 strlen(osb->uuid_str),
2874 ocfs2_do_node_down, osb,
2875 &conn);
2876 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002877 mlog_errno(status);
2878 goto bail;
2879 }
2880
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002881 status = ocfs2_cluster_this_node(&osb->node_num);
2882 if (status < 0) {
2883 mlog_errno(status);
2884 mlog(ML_ERROR,
2885 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002886 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002887 goto bail;
2888 }
2889
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002890local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002891 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2892 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002893 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07002894 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002895
Joel Becker4670c462008-02-01 14:39:35 -08002896 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002897
2898 status = 0;
2899bail:
2900 if (status < 0) {
2901 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002902 if (osb->dc_task)
2903 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002904 }
2905
2906 mlog_exit(status);
2907 return status;
2908}
2909
Joel Becker286eaa92008-02-01 15:03:57 -08002910void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2911 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002912{
2913 mlog_entry_void();
2914
Mark Fashehccd979b2005-12-15 14:31:24 -08002915 ocfs2_drop_osb_locks(osb);
2916
Joel Becker4670c462008-02-01 14:39:35 -08002917 /*
2918 * Now that we have dropped all locks and ocfs2_dismount_volume()
2919 * has disabled recovery, the DLM won't be talking to us. It's
2920 * safe to tear things down before disconnecting the cluster.
2921 */
2922
Mark Fasheh34d024f2007-09-24 15:56:19 -07002923 if (osb->dc_task) {
2924 kthread_stop(osb->dc_task);
2925 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002926 }
2927
2928 ocfs2_lock_res_free(&osb->osb_super_lockres);
2929 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08002930 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07002931 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002932
Joel Becker286eaa92008-02-01 15:03:57 -08002933 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08002934 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002935
2936 ocfs2_dlm_shutdown_debug(osb);
2937
2938 mlog_exit_void();
2939}
2940
Joel Becker7431cd72008-02-01 12:15:37 -08002941static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002942{
2943 struct ocfs2_lock_res *lockres = opaque;
2944 unsigned long flags;
2945
2946 mlog_entry_void();
2947
2948 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2949 lockres->l_unlock_action);
2950
2951 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08002952 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08002953 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2954 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002955 lockres->l_unlock_action);
2956 spin_unlock_irqrestore(&lockres->l_lock, flags);
2957 return;
2958 }
2959
2960 switch(lockres->l_unlock_action) {
2961 case OCFS2_UNLOCK_CANCEL_CONVERT:
2962 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2963 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08002964 /* Downconvert thread may have requeued this lock, we
2965 * need to wake it. */
2966 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2967 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08002968 break;
2969 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002970 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002971 break;
2972 default:
2973 BUG();
2974 }
2975
2976 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08002977 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08002978 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06002979 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002980
2981 mlog_exit_void();
2982}
2983
Mark Fashehccd979b2005-12-15 14:31:24 -08002984static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002985 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002986{
Joel Becker7431cd72008-02-01 12:15:37 -08002987 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002988 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002989 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002990
2991 /* We didn't get anywhere near actually using this lockres. */
2992 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2993 goto out;
2994
Mark Fashehb80fc012006-09-12 22:08:14 -07002995 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002996 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002997
Mark Fashehccd979b2005-12-15 14:31:24 -08002998 spin_lock_irqsave(&lockres->l_lock, flags);
2999
3000 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3001 "lockres %s, flags 0x%lx\n",
3002 lockres->l_name, lockres->l_flags);
3003
3004 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3005 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3006 "%u, unlock_action = %u\n",
3007 lockres->l_name, lockres->l_flags, lockres->l_action,
3008 lockres->l_unlock_action);
3009
3010 spin_unlock_irqrestore(&lockres->l_lock, flags);
3011
3012 /* XXX: Today we just wait on any busy
3013 * locks... Perhaps we need to cancel converts in the
3014 * future? */
3015 ocfs2_wait_on_busy_lock(lockres);
3016
3017 spin_lock_irqsave(&lockres->l_lock, flags);
3018 }
3019
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003020 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3021 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003022 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003023 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3024 lockres->l_ops->set_lvb(lockres);
3025 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003026
3027 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3028 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3029 lockres->l_name);
3030 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3031 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3032
3033 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3034 spin_unlock_irqrestore(&lockres->l_lock, flags);
3035 goto out;
3036 }
3037
3038 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3039
3040 /* make sure we never get here while waiting for an ast to
3041 * fire. */
3042 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3043
3044 /* is this necessary? */
3045 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3046 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3047 spin_unlock_irqrestore(&lockres->l_lock, flags);
3048
3049 mlog(0, "lock %s\n", lockres->l_name);
3050
Joel Becker4670c462008-02-01 14:39:35 -08003051 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003052 lockres);
3053 if (ret) {
3054 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003055 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003056 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003057 BUG();
3058 }
Coly Li73ac36e2009-01-07 18:09:16 -08003059 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003060 lockres->l_name);
3061
3062 ocfs2_wait_on_busy_lock(lockres);
3063out:
3064 mlog_exit(0);
3065 return 0;
3066}
3067
3068/* Mark the lockres as being dropped. It will no longer be
3069 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003070 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08003071 * it safe to drop.
3072 *
3073 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3074void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3075{
3076 int status;
3077 struct ocfs2_mask_waiter mw;
3078 unsigned long flags;
3079
3080 ocfs2_init_mask_waiter(&mw);
3081
3082 spin_lock_irqsave(&lockres->l_lock, flags);
3083 lockres->l_flags |= OCFS2_LOCK_FREEING;
3084 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3085 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3086 spin_unlock_irqrestore(&lockres->l_lock, flags);
3087
3088 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3089
3090 status = ocfs2_wait_for_mask(&mw);
3091 if (status)
3092 mlog_errno(status);
3093
3094 spin_lock_irqsave(&lockres->l_lock, flags);
3095 }
3096 spin_unlock_irqrestore(&lockres->l_lock, flags);
3097}
3098
Mark Fashehd680efe2006-09-08 14:14:34 -07003099void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3100 struct ocfs2_lock_res *lockres)
3101{
3102 int ret;
3103
3104 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003105 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003106 if (ret)
3107 mlog_errno(ret);
3108}
3109
Mark Fashehccd979b2005-12-15 14:31:24 -08003110static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3111{
Mark Fashehd680efe2006-09-08 14:14:34 -07003112 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3113 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003114 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003115 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003116}
3117
Mark Fashehccd979b2005-12-15 14:31:24 -08003118int ocfs2_drop_inode_locks(struct inode *inode)
3119{
3120 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003121
3122 mlog_entry_void();
3123
3124 /* No need to call ocfs2_mark_lockres_freeing here -
3125 * ocfs2_clear_inode has done it for us. */
3126
3127 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003128 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003129 if (err < 0)
3130 mlog_errno(err);
3131
3132 status = err;
3133
3134 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003135 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003136 if (err < 0)
3137 mlog_errno(err);
3138 if (err < 0 && !status)
3139 status = err;
3140
3141 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003142 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003143 if (err < 0)
3144 mlog_errno(err);
3145 if (err < 0 && !status)
3146 status = err;
3147
3148 mlog_exit(status);
3149 return status;
3150}
3151
Joel Beckerde551242008-02-01 14:45:08 -08003152static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3153 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003154{
3155 assert_spin_locked(&lockres->l_lock);
3156
Joel Beckerbd3e7612008-02-01 12:14:57 -08003157 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003158
3159 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003160 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003161 lockres->l_level, new_level);
3162 BUG();
3163 }
3164
3165 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3166 lockres->l_name, new_level, lockres->l_blocking);
3167
3168 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3169 lockres->l_requested = new_level;
3170 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003171 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003172}
3173
3174static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3175 struct ocfs2_lock_res *lockres,
3176 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003177 int lvb,
3178 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003179{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003180 int ret;
3181 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003182
3183 mlog_entry_void();
3184
3185 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003186 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003187
Joel Becker4670c462008-02-01 14:39:35 -08003188 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003189 new_level,
3190 &lockres->l_lksb,
3191 dlm_flags,
3192 lockres->l_name,
3193 OCFS2_LOCK_ID_MAX_LEN - 1,
3194 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003195 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003196 if (ret) {
3197 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003198 ocfs2_recover_from_dlm_error(lockres, 1);
3199 goto bail;
3200 }
3201
3202 ret = 0;
3203bail:
3204 mlog_exit(ret);
3205 return ret;
3206}
3207
Joel Becker24ef1812008-01-29 17:37:32 -08003208/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003209static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3210 struct ocfs2_lock_res *lockres)
3211{
3212 assert_spin_locked(&lockres->l_lock);
3213
3214 mlog_entry_void();
3215 mlog(0, "lock %s\n", lockres->l_name);
3216
3217 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3218 /* If we're already trying to cancel a lock conversion
3219 * then just drop the spinlock and allow the caller to
3220 * requeue this lock. */
3221
3222 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3223 return 0;
3224 }
3225
3226 /* were we in a convert when we got the bast fire? */
3227 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3228 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3229 /* set things up for the unlockast to know to just
3230 * clear out the ast_action and unset busy, etc. */
3231 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3232
3233 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3234 "lock %s, invalid flags: 0x%lx\n",
3235 lockres->l_name, lockres->l_flags);
3236
3237 return 1;
3238}
3239
3240static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3241 struct ocfs2_lock_res *lockres)
3242{
3243 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003244
3245 mlog_entry_void();
3246 mlog(0, "lock %s\n", lockres->l_name);
3247
Joel Becker4670c462008-02-01 14:39:35 -08003248 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003249 DLM_LKF_CANCEL, lockres);
3250 if (ret) {
3251 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003252 ocfs2_recover_from_dlm_error(lockres, 0);
3253 }
3254
Joel Becker24ef1812008-01-29 17:37:32 -08003255 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003256
3257 mlog_exit(ret);
3258 return ret;
3259}
3260
Mark Fashehb5e500e2006-09-13 22:01:16 -07003261static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3262 struct ocfs2_lock_res *lockres,
3263 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003264{
3265 unsigned long flags;
3266 int blocking;
3267 int new_level;
3268 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003269 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003270 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003271
3272 mlog_entry_void();
3273
3274 spin_lock_irqsave(&lockres->l_lock, flags);
3275
3276 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3277
3278recheck:
3279 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003280 /* XXX
3281 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3282 * exists entirely for one reason - another thread has set
3283 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3284 *
3285 * If we do ocfs2_cancel_convert() before the other thread
3286 * calls dlm_lock(), our cancel will do nothing. We will
3287 * get no ast, and we will have no way of knowing the
3288 * cancel failed. Meanwhile, the other thread will call
3289 * into dlm_lock() and wait...forever.
3290 *
3291 * Why forever? Because another node has asked for the
3292 * lock first; that's why we're here in unblock_lock().
3293 *
3294 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3295 * set, we just requeue the unblock. Only when the other
3296 * thread has called dlm_lock() and cleared PENDING will
3297 * we then cancel their request.
3298 *
3299 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3300 * at the same time they set OCFS2_DLM_BUSY. They must
3301 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3302 */
3303 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3304 goto leave_requeue;
3305
Mark Fashehd680efe2006-09-08 14:14:34 -07003306 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003307 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3308 spin_unlock_irqrestore(&lockres->l_lock, flags);
3309 if (ret) {
3310 ret = ocfs2_cancel_convert(osb, lockres);
3311 if (ret < 0)
3312 mlog_errno(ret);
3313 }
3314 goto leave;
3315 }
3316
3317 /* if we're blocking an exclusive and we have *any* holders,
3318 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003319 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003320 && (lockres->l_ex_holders || lockres->l_ro_holders))
3321 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003322
3323 /* If it's a PR we're blocking, then only
3324 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003325 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003326 lockres->l_ex_holders)
3327 goto leave_requeue;
3328
3329 /*
3330 * Can we get a lock in this state if the holder counts are
3331 * zero? The meta data unblock code used to check this.
3332 */
3333 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3334 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3335 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003336
Mark Fasheh16d5b952006-09-13 21:10:12 -07003337 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3338
3339 if (lockres->l_ops->check_downconvert
3340 && !lockres->l_ops->check_downconvert(lockres, new_level))
3341 goto leave_requeue;
3342
Mark Fashehccd979b2005-12-15 14:31:24 -08003343 /* If we get here, then we know that there are no more
3344 * incompatible holders (and anyone asking for an incompatible
3345 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003346 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003347 goto downconvert;
3348
3349 /* Some lockres types want to do a bit of work before
3350 * downconverting a lock. Allow that here. The worker function
3351 * may sleep, so we save off a copy of what we're blocking as
3352 * it may change while we're not holding the spin lock. */
3353 blocking = lockres->l_blocking;
3354 spin_unlock_irqrestore(&lockres->l_lock, flags);
3355
Mark Fashehcc567d82006-09-13 21:52:21 -07003356 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003357
3358 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3359 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003360
3361 spin_lock_irqsave(&lockres->l_lock, flags);
3362 if (blocking != lockres->l_blocking) {
3363 /* If this changed underneath us, then we can't drop
3364 * it just yet. */
3365 goto recheck;
3366 }
3367
3368downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003369 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003370
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003371 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003372 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003373 set_lvb = 1;
3374
3375 /*
3376 * We only set the lvb if the lock has been fully
3377 * refreshed - otherwise we risk setting stale
3378 * data. Otherwise, there's no need to actually clear
3379 * out the lvb here as it's value is still valid.
3380 */
3381 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3382 lockres->l_ops->set_lvb(lockres);
3383 }
3384
Joel Beckerde551242008-02-01 14:45:08 -08003385 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003386 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003387 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3388 gen);
3389
Mark Fashehccd979b2005-12-15 14:31:24 -08003390leave:
3391 mlog_exit(ret);
3392 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003393
3394leave_requeue:
3395 spin_unlock_irqrestore(&lockres->l_lock, flags);
3396 ctl->requeue = 1;
3397
3398 mlog_exit(0);
3399 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003400}
3401
Mark Fashehd680efe2006-09-08 14:14:34 -07003402static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3403 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003404{
3405 struct inode *inode;
3406 struct address_space *mapping;
3407
Mark Fashehccd979b2005-12-15 14:31:24 -08003408 inode = ocfs2_lock_res_inode(lockres);
3409 mapping = inode->i_mapping;
3410
Mark Fasheh1044e402008-02-28 17:16:03 -08003411 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003412 goto out;
3413
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003414 /*
3415 * We need this before the filemap_fdatawrite() so that it can
3416 * transfer the dirty bit from the PTE to the
3417 * page. Unfortunately this means that even for EX->PR
3418 * downconverts, we'll lose our mappings and have to build
3419 * them up again.
3420 */
3421 unmap_mapping_range(mapping, 0, 0, 0);
3422
Mark Fashehccd979b2005-12-15 14:31:24 -08003423 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003424 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3425 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003426 }
3427 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003428 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003429 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003430 } else {
3431 /* We only need to wait on the I/O if we're not also
3432 * truncating pages because truncate_inode_pages waits
3433 * for us above. We don't truncate pages if we're
3434 * blocking anything < EXMODE because we want to keep
3435 * them around in that case. */
3436 filemap_fdatawait(mapping);
3437 }
3438
Mark Fashehf1f54062007-10-18 15:13:59 -07003439out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003440 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003441}
3442
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003443static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3444 int new_level)
3445{
3446 struct inode *inode = ocfs2_lock_res_inode(lockres);
3447 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3448
Joel Beckerbd3e7612008-02-01 12:14:57 -08003449 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3450 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003451
3452 if (checkpointed)
3453 return 1;
3454
3455 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3456 return 0;
3457}
3458
3459static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3460{
3461 struct inode *inode = ocfs2_lock_res_inode(lockres);
3462
3463 __ocfs2_stuff_meta_lvb(inode);
3464}
3465
Mark Fashehd680efe2006-09-08 14:14:34 -07003466/*
3467 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003468 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003469 * dlmglue API and push these off to the ocfs2_wq in the future.
3470 */
3471static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3472 struct ocfs2_lock_res *lockres)
3473{
3474 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3475 ocfs2_dentry_lock_put(osb, dl);
3476}
3477
3478/*
3479 * d_delete() matching dentries before the lock downconvert.
3480 *
3481 * At this point, any process waiting to destroy the
3482 * dentry_lock due to last ref count is stopped by the
3483 * OCFS2_LOCK_QUEUED flag.
3484 *
3485 * We have two potential problems
3486 *
3487 * 1) If we do the last reference drop on our dentry_lock (via dput)
3488 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3489 * the downconvert to finish. Instead we take an elevated
3490 * reference and push the drop until after we've completed our
3491 * unblock processing.
3492 *
3493 * 2) There might be another process with a final reference,
3494 * waiting on us to finish processing. If this is the case, we
3495 * detect it and exit out - there's no more dentries anyway.
3496 */
3497static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3498 int blocking)
3499{
3500 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3501 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3502 struct dentry *dentry;
3503 unsigned long flags;
3504 int extra_ref = 0;
3505
3506 /*
3507 * This node is blocking another node from getting a read
3508 * lock. This happens when we've renamed within a
3509 * directory. We've forced the other nodes to d_delete(), but
3510 * we never actually dropped our lock because it's still
3511 * valid. The downconvert code will retain a PR for this node,
3512 * so there's no further work to do.
3513 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003514 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003515 return UNBLOCK_CONTINUE;
3516
3517 /*
3518 * Mark this inode as potentially orphaned. The code in
3519 * ocfs2_delete_inode() will figure out whether it actually
3520 * needs to be freed or not.
3521 */
3522 spin_lock(&oi->ip_lock);
3523 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3524 spin_unlock(&oi->ip_lock);
3525
3526 /*
3527 * Yuck. We need to make sure however that the check of
3528 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3529 * respect to a reference decrement or the setting of that
3530 * flag.
3531 */
3532 spin_lock_irqsave(&lockres->l_lock, flags);
3533 spin_lock(&dentry_attach_lock);
3534 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3535 && dl->dl_count) {
3536 dl->dl_count++;
3537 extra_ref = 1;
3538 }
3539 spin_unlock(&dentry_attach_lock);
3540 spin_unlock_irqrestore(&lockres->l_lock, flags);
3541
3542 mlog(0, "extra_ref = %d\n", extra_ref);
3543
3544 /*
3545 * We have a process waiting on us in ocfs2_dentry_iput(),
3546 * which means we can't have any more outstanding
3547 * aliases. There's no need to do any more work.
3548 */
3549 if (!extra_ref)
3550 return UNBLOCK_CONTINUE;
3551
3552 spin_lock(&dentry_attach_lock);
3553 while (1) {
3554 dentry = ocfs2_find_local_alias(dl->dl_inode,
3555 dl->dl_parent_blkno, 1);
3556 if (!dentry)
3557 break;
3558 spin_unlock(&dentry_attach_lock);
3559
3560 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3561 dentry->d_name.name);
3562
3563 /*
3564 * The following dcache calls may do an
3565 * iput(). Normally we don't want that from the
3566 * downconverting thread, but in this case it's ok
3567 * because the requesting node already has an
3568 * exclusive lock on the inode, so it can't be queued
3569 * for a downconvert.
3570 */
3571 d_delete(dentry);
3572 dput(dentry);
3573
3574 spin_lock(&dentry_attach_lock);
3575 }
3576 spin_unlock(&dentry_attach_lock);
3577
3578 /*
3579 * If we are the last holder of this dentry lock, there is no
3580 * reason to downconvert so skip straight to the unlock.
3581 */
3582 if (dl->dl_count == 1)
3583 return UNBLOCK_STOP_POST;
3584
3585 return UNBLOCK_CONTINUE_POST;
3586}
3587
Jan Kara9e33d692008-08-25 19:56:50 +02003588static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3589{
3590 struct ocfs2_qinfo_lvb *lvb;
3591 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3592 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3593 oinfo->dqi_gi.dqi_type);
3594
3595 mlog_entry_void();
3596
Mark Fasheha641dc22008-12-24 16:03:48 -08003597 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003598 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3599 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3600 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3601 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3602 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3603 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3604 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3605
3606 mlog_exit_void();
3607}
3608
3609void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3610{
3611 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3612 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3613 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3614
3615 mlog_entry_void();
3616 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3617 ocfs2_cluster_unlock(osb, lockres, level);
3618 mlog_exit_void();
3619}
3620
3621static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3622{
3623 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3624 oinfo->dqi_gi.dqi_type);
3625 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3626 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003627 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003628 struct ocfs2_global_disk_dqinfo *gdinfo;
3629 int status = 0;
3630
Joel Becker1c520df2009-06-19 15:14:13 -07003631 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3632 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003633 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3634 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3635 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3636 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3637 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3638 oinfo->dqi_gi.dqi_free_entry =
3639 be32_to_cpu(lvb->lvb_free_entry);
3640 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003641 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3642 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003643 mlog_errno(status);
3644 goto bail;
3645 }
3646 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3647 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3648 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3649 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3650 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3651 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3652 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3653 oinfo->dqi_gi.dqi_free_entry =
3654 le32_to_cpu(gdinfo->dqi_free_entry);
3655 brelse(bh);
3656 ocfs2_track_lock_refresh(lockres);
3657 }
3658
3659bail:
3660 return status;
3661}
3662
3663/* Lock quota info, this function expects at least shared lock on the quota file
3664 * so that we can safely refresh quota info from disk. */
3665int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3666{
3667 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3668 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3669 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3670 int status = 0;
3671
3672 mlog_entry_void();
3673
3674 /* On RO devices, locking really isn't needed... */
3675 if (ocfs2_is_hard_readonly(osb)) {
3676 if (ex)
3677 status = -EROFS;
3678 goto bail;
3679 }
3680 if (ocfs2_mount_local(osb))
3681 goto bail;
3682
3683 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3684 if (status < 0) {
3685 mlog_errno(status);
3686 goto bail;
3687 }
3688 if (!ocfs2_should_refresh_lock_res(lockres))
3689 goto bail;
3690 /* OK, we have the lock but we need to refresh the quota info */
3691 status = ocfs2_refresh_qinfo(oinfo);
3692 if (status)
3693 ocfs2_qinfo_unlock(oinfo, ex);
3694 ocfs2_complete_lock_res_refresh(lockres, status);
3695bail:
3696 mlog_exit(status);
3697 return status;
3698}
3699
Joel Becker4670c462008-02-01 14:39:35 -08003700/*
3701 * This is the filesystem locking protocol. It provides the lock handling
3702 * hooks for the underlying DLM. It has a maximum version number.
3703 * The version number allows interoperability with systems running at
3704 * the same major number and an equal or smaller minor number.
3705 *
3706 * Whenever the filesystem does new things with locks (adds or removes a
3707 * lock, orders them differently, does different things underneath a lock),
3708 * the version must be changed. The protocol is negotiated when joining
3709 * the dlm domain. A node may join the domain if its major version is
3710 * identical to all other nodes and its minor version is greater than
3711 * or equal to all other nodes. When its minor version is greater than
3712 * the other nodes, it will run at the minor version specified by the
3713 * other nodes.
3714 *
3715 * If a locking change is made that will not be compatible with older
3716 * versions, the major number must be increased and the minor version set
3717 * to zero. If a change merely adds a behavior that can be disabled when
3718 * speaking to older versions, the minor version must be increased. If a
3719 * change adds a fully backwards compatible change (eg, LVB changes that
3720 * are just ignored by older versions), the version does not need to be
3721 * updated.
3722 */
Joel Becker24ef1812008-01-29 17:37:32 -08003723static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003724 .lp_max_version = {
3725 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3726 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3727 },
Joel Becker24ef1812008-01-29 17:37:32 -08003728 .lp_lock_ast = ocfs2_locking_ast,
3729 .lp_blocking_ast = ocfs2_blocking_ast,
3730 .lp_unlock_ast = ocfs2_unlock_ast,
3731};
3732
Joel Becker63e0c482008-01-30 16:58:36 -08003733void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003734{
Joel Becker63e0c482008-01-30 16:58:36 -08003735 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003736}
3737
Joel Becker24ef1812008-01-29 17:37:32 -08003738
Adrian Bunk00600052008-01-29 00:11:41 +02003739static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3740 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003741{
3742 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003743 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003744 unsigned long flags;
3745
3746 /* Our reference to the lockres in this function can be
3747 * considered valid until we remove the OCFS2_LOCK_QUEUED
3748 * flag. */
3749
3750 mlog_entry_void();
3751
3752 BUG_ON(!lockres);
3753 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003754
3755 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3756
3757 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003758 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003759 * still be marked with OCFS2_LOCK_FREEING after this check,
3760 * but short circuiting here will still save us some
3761 * performance. */
3762 spin_lock_irqsave(&lockres->l_lock, flags);
3763 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3764 goto unqueue;
3765 spin_unlock_irqrestore(&lockres->l_lock, flags);
3766
Mark Fashehb5e500e2006-09-13 22:01:16 -07003767 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003768 if (status < 0)
3769 mlog_errno(status);
3770
3771 spin_lock_irqsave(&lockres->l_lock, flags);
3772unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003773 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003774 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3775 } else
3776 ocfs2_schedule_blocked_lock(osb, lockres);
3777
3778 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003779 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003780 spin_unlock_irqrestore(&lockres->l_lock, flags);
3781
Mark Fashehd680efe2006-09-08 14:14:34 -07003782 if (ctl.unblock_action != UNBLOCK_CONTINUE
3783 && lockres->l_ops->post_unlock)
3784 lockres->l_ops->post_unlock(osb, lockres);
3785
Mark Fashehccd979b2005-12-15 14:31:24 -08003786 mlog_exit_void();
3787}
3788
3789static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3790 struct ocfs2_lock_res *lockres)
3791{
3792 mlog_entry_void();
3793
3794 assert_spin_locked(&lockres->l_lock);
3795
3796 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3797 /* Do not schedule a lock for downconvert when it's on
3798 * the way to destruction - any nodes wanting access
3799 * to the resource will get it soon. */
3800 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3801 lockres->l_name, lockres->l_flags);
3802 return;
3803 }
3804
3805 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3806
Mark Fasheh34d024f2007-09-24 15:56:19 -07003807 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003808 if (list_empty(&lockres->l_blocked_list)) {
3809 list_add_tail(&lockres->l_blocked_list,
3810 &osb->blocked_lock_list);
3811 osb->blocked_lock_count++;
3812 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003813 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003814
3815 mlog_exit_void();
3816}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003817
3818static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3819{
3820 unsigned long processed;
3821 struct ocfs2_lock_res *lockres;
3822
3823 mlog_entry_void();
3824
3825 spin_lock(&osb->dc_task_lock);
3826 /* grab this early so we know to try again if a state change and
3827 * wake happens part-way through our work */
3828 osb->dc_work_sequence = osb->dc_wake_sequence;
3829
3830 processed = osb->blocked_lock_count;
3831 while (processed) {
3832 BUG_ON(list_empty(&osb->blocked_lock_list));
3833
3834 lockres = list_entry(osb->blocked_lock_list.next,
3835 struct ocfs2_lock_res, l_blocked_list);
3836 list_del_init(&lockres->l_blocked_list);
3837 osb->blocked_lock_count--;
3838 spin_unlock(&osb->dc_task_lock);
3839
3840 BUG_ON(!processed);
3841 processed--;
3842
3843 ocfs2_process_blocked_lock(osb, lockres);
3844
3845 spin_lock(&osb->dc_task_lock);
3846 }
3847 spin_unlock(&osb->dc_task_lock);
3848
3849 mlog_exit_void();
3850}
3851
3852static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3853{
3854 int empty = 0;
3855
3856 spin_lock(&osb->dc_task_lock);
3857 if (list_empty(&osb->blocked_lock_list))
3858 empty = 1;
3859
3860 spin_unlock(&osb->dc_task_lock);
3861 return empty;
3862}
3863
3864static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3865{
3866 int should_wake = 0;
3867
3868 spin_lock(&osb->dc_task_lock);
3869 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3870 should_wake = 1;
3871 spin_unlock(&osb->dc_task_lock);
3872
3873 return should_wake;
3874}
3875
Adrian Bunk200bfae2008-02-17 10:20:38 +02003876static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003877{
3878 int status = 0;
3879 struct ocfs2_super *osb = arg;
3880
3881 /* only quit once we've been asked to stop and there is no more
3882 * work available */
3883 while (!(kthread_should_stop() &&
3884 ocfs2_downconvert_thread_lists_empty(osb))) {
3885
3886 wait_event_interruptible(osb->dc_event,
3887 ocfs2_downconvert_thread_should_wake(osb) ||
3888 kthread_should_stop());
3889
3890 mlog(0, "downconvert_thread: awoken\n");
3891
3892 ocfs2_downconvert_thread_do_work(osb);
3893 }
3894
3895 osb->dc_task = NULL;
3896 return status;
3897}
3898
3899void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3900{
3901 spin_lock(&osb->dc_task_lock);
3902 /* make sure the voting thread gets a swipe at whatever changes
3903 * the caller may have made to the voting state */
3904 osb->dc_wake_sequence++;
3905 spin_unlock(&osb->dc_task_lock);
3906 wake_up(&osb->dc_event);
3907}