blob: 7484bb9a34388372fca70ede623738db9b397506 [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"
Tao Ma8dec98e2009-08-18 11:19:58 +080056#include "refcounttree.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080057
58#include "buffer_head_io.h"
59
60struct ocfs2_mask_waiter {
61 struct list_head mw_item;
62 int mw_status;
63 struct completion mw_complete;
64 unsigned long mw_mask;
65 unsigned long mw_goal;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070066#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -080067 ktime_t mw_lock_start;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -070068#endif
Mark Fashehccd979b2005-12-15 14:31:24 -080069};
70
Mark Fasheh54a7e752006-09-12 21:49:13 -070071static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
72static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080073static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Jan Kara9e33d692008-08-25 19:56:50 +020074static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080075
Mark Fashehd680efe2006-09-08 14:14:34 -070076/*
Mark Fashehcc567d82006-09-13 21:52:21 -070077 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070078 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070079 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070080 * and ocfs2_process_blocked_lock()
81 *
82 */
83enum ocfs2_unblock_action {
84 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
85 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
86 * ->post_unlock callback */
87 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
88 * ->post_unlock() callback. */
89};
90
91struct ocfs2_unblock_ctl {
92 int requeue;
93 enum ocfs2_unblock_action unblock_action;
94};
95
Jan Karacb257972009-06-04 15:26:50 +020096/* Lockdep class keys */
97struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
98
Mark Fasheh810d5ae2006-09-13 21:39:52 -070099static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
100 int new_level);
101static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
102
Mark Fashehcc567d82006-09-13 21:52:21 -0700103static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
104 int blocking);
105
Mark Fashehcc567d82006-09-13 21:52:21 -0700106static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
107 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700108
109static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
110 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800111
Jan Kara9e33d692008-08-25 19:56:50 +0200112static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700113
Tao Ma8dec98e2009-08-18 11:19:58 +0800114static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
115 int new_level);
116static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
117 int blocking);
118
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700119#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
120
121/* This aids in debugging situations where a bad LVB might be involved. */
122static void ocfs2_dump_meta_lvb_info(u64 level,
123 const char *function,
124 unsigned int line,
125 struct ocfs2_lock_res *lockres)
126{
Mark Fasheha641dc22008-12-24 16:03:48 -0800127 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700128
129 mlog(level, "LVB information for %s (called from %s:%u):\n",
130 lockres->l_name, function, line);
131 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
132 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
133 be32_to_cpu(lvb->lvb_igeneration));
134 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
135 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
136 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
137 be16_to_cpu(lvb->lvb_imode));
138 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
139 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
140 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
141 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
142 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
143 be32_to_cpu(lvb->lvb_iattr));
144}
145
146
Mark Fashehf625c972006-09-12 21:24:53 -0700147/*
148 * OCFS2 Lock Resource Operations
149 *
150 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700151 *
152 * The most basic of lock types can point ->l_priv to their respective
153 * struct ocfs2_super and allow the default actions to manage things.
154 *
155 * Right now, each lock type also needs to implement an init function,
156 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
157 * should be called when the lock is no longer needed (i.e., object
158 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700159 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800160struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700161 /*
162 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
163 * this callback if ->l_priv is not an ocfs2_super pointer
164 */
165 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700166
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700167 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700168 * Optionally called in the downconvert thread after a
169 * successful downconvert. The lockres will not be referenced
170 * after this callback is called, so it is safe to free
171 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700172 *
173 * The exact semantics of when this is called are controlled
174 * by ->downconvert_worker()
175 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700176 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700177
178 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700179 * Allow a lock type to add checks to determine whether it is
180 * safe to downconvert a lock. Return 0 to re-queue the
181 * downconvert at a later time, nonzero to continue.
182 *
183 * For most locks, the default checks that there are no
184 * incompatible holders are sufficient.
185 *
186 * Called with the lockres spinlock held.
187 */
188 int (*check_downconvert)(struct ocfs2_lock_res *, int);
189
190 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700191 * Allows a lock type to populate the lock value block. This
192 * is called on downconvert, and when we drop a lock.
193 *
194 * Locks that want to use this should set LOCK_TYPE_USES_LVB
195 * in the flags field.
196 *
197 * Called with the lockres spinlock held.
198 */
199 void (*set_lvb)(struct ocfs2_lock_res *);
200
201 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700202 * Called from the downconvert thread when it is determined
203 * that a lock will be downconverted. This is called without
204 * any locks held so the function can do work that might
205 * schedule (syncing out data, etc).
206 *
207 * This should return any one of the ocfs2_unblock_action
208 * values, depending on what it wants the thread to do.
209 */
210 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
211
212 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700213 * LOCK_TYPE_* flags which describe the specific requirements
214 * of a lock type. Descriptions of each individual flag follow.
215 */
216 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800217};
218
Mark Fashehf625c972006-09-12 21:24:53 -0700219/*
220 * Some locks want to "refresh" potentially stale data when a
221 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
222 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
223 * individual lockres l_flags member from the ast function. It is
224 * expected that the locking wrapper will clear the
225 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
226 */
227#define LOCK_TYPE_REQUIRES_REFRESH 0x1
228
Mark Fashehb80fc012006-09-12 22:08:14 -0700229/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700230 * Indicate that a lock type makes use of the lock value block. The
231 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700232 */
233#define LOCK_TYPE_USES_LVB 0x2
234
Mark Fashehccd979b2005-12-15 14:31:24 -0800235static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700236 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700237 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800238};
239
Mark Fashehe63aecb62007-10-18 15:30:42 -0700240static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700241 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700242 .check_downconvert = ocfs2_check_meta_downconvert,
243 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700244 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700245 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800246};
247
Mark Fashehccd979b2005-12-15 14:31:24 -0800248static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700249 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800250};
251
252static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700253 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800254};
255
wengang wang6ca497a2009-03-06 21:29:10 +0800256static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
257 .flags = 0,
258};
259
Srinivas Eeda83273932009-06-03 17:02:55 -0700260static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
261 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
262};
263
Mark Fashehd680efe2006-09-08 14:14:34 -0700264static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700265 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700266 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700267 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700268 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700269};
270
Tiger Yang50008632007-03-20 16:01:38 -0700271static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
272 .get_osb = ocfs2_get_inode_osb,
273 .flags = 0,
274};
275
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800276static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
277 .get_osb = ocfs2_get_file_osb,
278 .flags = 0,
279};
280
Jan Kara9e33d692008-08-25 19:56:50 +0200281static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
282 .set_lvb = ocfs2_set_qinfo_lvb,
283 .get_osb = ocfs2_get_qinfo_osb,
284 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
285};
286
Tao Ma8dec98e2009-08-18 11:19:58 +0800287static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
288 .check_downconvert = ocfs2_check_refcount_downconvert,
289 .downconvert_worker = ocfs2_refcount_convert_worker,
290 .flags = 0,
291};
292
Mark Fashehccd979b2005-12-15 14:31:24 -0800293static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
294{
295 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700296 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
297 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800298}
299
Joel Beckerc0e41332010-01-29 14:46:44 -0800300static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
Joel Beckera796d282010-01-28 19:22:39 -0800301{
302 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
303}
304
Mark Fashehccd979b2005-12-15 14:31:24 -0800305static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
306{
307 BUG_ON(!ocfs2_is_inode_lock(lockres));
308
309 return (struct inode *) lockres->l_priv;
310}
311
Mark Fashehd680efe2006-09-08 14:14:34 -0700312static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
313{
314 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
315
316 return (struct ocfs2_dentry_lock *)lockres->l_priv;
317}
318
Jan Kara9e33d692008-08-25 19:56:50 +0200319static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
320{
321 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
322
323 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
324}
325
Tao Ma8dec98e2009-08-18 11:19:58 +0800326static inline struct ocfs2_refcount_tree *
327ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
328{
329 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
330}
331
Mark Fasheh54a7e752006-09-12 21:49:13 -0700332static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
333{
334 if (lockres->l_ops->get_osb)
335 return lockres->l_ops->get_osb(lockres);
336
337 return (struct ocfs2_super *)lockres->l_priv;
338}
339
Mark Fashehccd979b2005-12-15 14:31:24 -0800340static int ocfs2_lock_create(struct ocfs2_super *osb,
341 struct ocfs2_lock_res *lockres,
342 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800343 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800344static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
345 int wanted);
Jan Karacb257972009-06-04 15:26:50 +0200346static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
347 struct ocfs2_lock_res *lockres,
348 int level, unsigned long caller_ip);
349static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
350 struct ocfs2_lock_res *lockres,
351 int level)
352{
353 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
354}
355
Mark Fashehccd979b2005-12-15 14:31:24 -0800356static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
357static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
358static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
359static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
360static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
361 struct ocfs2_lock_res *lockres);
362static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
363 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800364#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
365 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
366 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
367 _err, _func, _lockres->l_name); \
368 else \
369 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
370 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
371 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800372} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700373static int ocfs2_downconvert_thread(void *arg);
374static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
375 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700376static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 struct buffer_head **bh);
378static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
379static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800380static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
381 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800382static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
383 struct ocfs2_lock_res *lockres,
384 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800385 int lvb,
386 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800387static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
388 struct ocfs2_lock_res *lockres);
389static int ocfs2_cancel_convert(struct ocfs2_super *osb,
390 struct ocfs2_lock_res *lockres);
391
Mark Fashehccd979b2005-12-15 14:31:24 -0800392
Mark Fashehccd979b2005-12-15 14:31:24 -0800393static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
394 u64 blkno,
395 u32 generation,
396 char *name)
397{
398 int len;
399
400 mlog_entry_void();
401
402 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
403
Mark Fashehb06970532006-03-03 10:24:33 -0800404 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
405 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
406 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800407
408 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
409
410 mlog(0, "built lock resource with name: %s\n", name);
411
412 mlog_exit_void();
413}
414
Ingo Molnar34af9462006-06-27 02:53:55 -0700415static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800416
417static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
418 struct ocfs2_dlm_debug *dlm_debug)
419{
420 mlog(0, "Add tracking for lockres %s\n", res->l_name);
421
422 spin_lock(&ocfs2_dlm_tracking_lock);
423 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
424 spin_unlock(&ocfs2_dlm_tracking_lock);
425}
426
427static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
428{
429 spin_lock(&ocfs2_dlm_tracking_lock);
430 if (!list_empty(&res->l_debug_list))
431 list_del_init(&res->l_debug_list);
432 spin_unlock(&ocfs2_dlm_tracking_lock);
433}
434
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700435#ifdef CONFIG_OCFS2_FS_STATS
436static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
437{
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700438 res->l_lock_refresh = 0;
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800439 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
440 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700441}
442
443static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
444 struct ocfs2_mask_waiter *mw, int ret)
445{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800446 u32 usec;
447 ktime_t kt;
448 struct ocfs2_lock_stats *stats;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700449
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800450 if (level == LKM_PRMODE)
451 stats = &res->l_lock_prmode;
452 else if (level == LKM_EXMODE)
453 stats = &res->l_lock_exmode;
454 else
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700455 return;
456
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800457 kt = ktime_sub(ktime_get(), mw->mw_lock_start);
458 usec = ktime_to_us(kt);
459
460 stats->ls_gets++;
461 stats->ls_total += ktime_to_ns(kt);
462 /* overflow */
463 if (unlikely(stats->ls_gets) == 0) {
464 stats->ls_gets++;
465 stats->ls_total = ktime_to_ns(kt);
466 }
467
468 if (stats->ls_max < usec)
469 stats->ls_max = usec;
470
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700471 if (ret)
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800472 stats->ls_fail++;
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700473}
474
475static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
476{
477 lockres->l_lock_refresh++;
478}
479
480static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
481{
Sunil Mushran5bc970e2010-12-28 23:26:03 -0800482 mw->mw_lock_start = ktime_get();
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700483}
484#else
485static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
486{
487}
488static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
489 int level, struct ocfs2_mask_waiter *mw, int ret)
490{
491}
492static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
493{
494}
495static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
496{
497}
498#endif
499
Mark Fashehccd979b2005-12-15 14:31:24 -0800500static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
501 struct ocfs2_lock_res *res,
502 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800503 struct ocfs2_lock_res_ops *ops,
504 void *priv)
505{
Mark Fashehccd979b2005-12-15 14:31:24 -0800506 res->l_type = type;
507 res->l_ops = ops;
508 res->l_priv = priv;
509
Joel Beckerbd3e7612008-02-01 12:14:57 -0800510 res->l_level = DLM_LOCK_IV;
511 res->l_requested = DLM_LOCK_IV;
512 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 res->l_action = OCFS2_AST_INVALID;
514 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
515
516 res->l_flags = OCFS2_LOCK_INITIALIZED;
517
518 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700519
520 ocfs2_init_lock_stats(res);
Jan Karacb257972009-06-04 15:26:50 +0200521#ifdef CONFIG_DEBUG_LOCK_ALLOC
522 if (type != OCFS2_LOCK_TYPE_OPEN)
523 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
524 &lockdep_keys[type], 0);
525 else
526 res->l_lockdep_map.key = NULL;
527#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800528}
529
530void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
531{
532 /* This also clears out the lock status block */
533 memset(res, 0, sizeof(struct ocfs2_lock_res));
534 spin_lock_init(&res->l_lock);
535 init_waitqueue_head(&res->l_event);
536 INIT_LIST_HEAD(&res->l_blocked_list);
537 INIT_LIST_HEAD(&res->l_mask_waiters);
538}
539
540void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
541 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700542 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800543 struct inode *inode)
544{
545 struct ocfs2_lock_res_ops *ops;
546
547 switch(type) {
548 case OCFS2_LOCK_TYPE_RW:
549 ops = &ocfs2_inode_rw_lops;
550 break;
551 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700552 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800553 break;
Tiger Yang50008632007-03-20 16:01:38 -0700554 case OCFS2_LOCK_TYPE_OPEN:
555 ops = &ocfs2_inode_open_lops;
556 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800557 default:
558 mlog_bug_on_msg(1, "type: %d\n", type);
559 ops = NULL; /* thanks, gcc */
560 break;
561 };
562
Mark Fashehd680efe2006-09-08 14:14:34 -0700563 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700564 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700565 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
566}
567
Mark Fasheh54a7e752006-09-12 21:49:13 -0700568static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
569{
570 struct inode *inode = ocfs2_lock_res_inode(lockres);
571
572 return OCFS2_SB(inode->i_sb);
573}
574
Jan Kara9e33d692008-08-25 19:56:50 +0200575static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
576{
577 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
578
579 return OCFS2_SB(info->dqi_gi.dqi_sb);
580}
581
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800582static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
583{
584 struct ocfs2_file_private *fp = lockres->l_priv;
585
586 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
587}
588
Mark Fashehd680efe2006-09-08 14:14:34 -0700589static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
590{
591 __be64 inode_blkno_be;
592
593 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
594 sizeof(__be64));
595
596 return be64_to_cpu(inode_blkno_be);
597}
598
Mark Fasheh54a7e752006-09-12 21:49:13 -0700599static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
600{
601 struct ocfs2_dentry_lock *dl = lockres->l_priv;
602
603 return OCFS2_SB(dl->dl_inode->i_sb);
604}
605
Mark Fashehd680efe2006-09-08 14:14:34 -0700606void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
607 u64 parent, struct inode *inode)
608{
609 int len;
610 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
611 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
612 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
613
614 ocfs2_lock_res_init_once(lockres);
615
616 /*
617 * Unfortunately, the standard lock naming scheme won't work
618 * here because we have two 16 byte values to use. Instead,
619 * we'll stuff the inode number as a binary value. We still
620 * want error prints to show something without garbling the
621 * display, so drop a null byte in there before the inode
622 * number. A future version of OCFS2 will likely use all
623 * binary lock names. The stringified names have been a
624 * tremendous aid in debugging, but now that the debugfs
625 * interface exists, we can mangle things there if need be.
626 *
627 * NOTE: We also drop the standard "pad" value (the total lock
628 * name size stays the same though - the last part is all
629 * zeros due to the memset in ocfs2_lock_res_init_once()
630 */
631 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
632 "%c%016llx",
633 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
634 (long long)parent);
635
636 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
637
638 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
639 sizeof(__be64));
640
641 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
642 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
643 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800644}
645
646static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
647 struct ocfs2_super *osb)
648{
649 /* Superblock lockres doesn't come from a slab so we call init
650 * once on it manually. */
651 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700652 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
653 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800654 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800655 &ocfs2_super_lops, osb);
656}
657
658static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
659 struct ocfs2_super *osb)
660{
661 /* Rename lockres doesn't come from a slab so we call init
662 * once on it manually. */
663 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
665 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 &ocfs2_rename_lops, osb);
667}
668
wengang wang6ca497a2009-03-06 21:29:10 +0800669static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
670 struct ocfs2_super *osb)
671{
672 /* nfs_sync lockres doesn't come from a slab so we call init
673 * once on it manually. */
674 ocfs2_lock_res_init_once(res);
675 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
676 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
677 &ocfs2_nfs_sync_lops, osb);
678}
679
Srinivas Eeda83273932009-06-03 17:02:55 -0700680static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
681 struct ocfs2_super *osb)
682{
Srinivas Eeda83273932009-06-03 17:02:55 -0700683 ocfs2_lock_res_init_once(res);
684 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
685 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
686 &ocfs2_orphan_scan_lops, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -0700687}
688
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800689void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
690 struct ocfs2_file_private *fp)
691{
692 struct inode *inode = fp->fp_file->f_mapping->host;
693 struct ocfs2_inode_info *oi = OCFS2_I(inode);
694
695 ocfs2_lock_res_init_once(lockres);
696 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
697 inode->i_generation, lockres->l_name);
698 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
699 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
700 fp);
701 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
702}
703
Jan Kara9e33d692008-08-25 19:56:50 +0200704void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
705 struct ocfs2_mem_dqinfo *info)
706{
707 ocfs2_lock_res_init_once(lockres);
708 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
709 0, lockres->l_name);
710 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
711 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
712 info);
713}
714
Tao Ma8dec98e2009-08-18 11:19:58 +0800715void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
716 struct ocfs2_super *osb, u64 ref_blkno,
717 unsigned int generation)
718{
719 ocfs2_lock_res_init_once(lockres);
720 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
721 generation, lockres->l_name);
722 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
723 &ocfs2_refcount_block_lops, osb);
724}
725
Mark Fashehccd979b2005-12-15 14:31:24 -0800726void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
727{
728 mlog_entry_void();
729
730 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
731 return;
732
733 ocfs2_remove_lockres_tracking(res);
734
735 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
736 "Lockres %s is on the blocked list\n",
737 res->l_name);
738 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
739 "Lockres %s has mask waiters pending\n",
740 res->l_name);
741 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
742 "Lockres %s is locked\n",
743 res->l_name);
744 mlog_bug_on_msg(res->l_ro_holders,
745 "Lockres %s has %u ro holders\n",
746 res->l_name, res->l_ro_holders);
747 mlog_bug_on_msg(res->l_ex_holders,
748 "Lockres %s has %u ex holders\n",
749 res->l_name, res->l_ex_holders);
750
751 /* Need to clear out the lock status block for the dlm */
752 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
753
754 res->l_flags = 0UL;
755 mlog_exit_void();
756}
757
758static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
759 int level)
760{
761 mlog_entry_void();
762
763 BUG_ON(!lockres);
764
765 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800766 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800767 lockres->l_ex_holders++;
768 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800769 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800770 lockres->l_ro_holders++;
771 break;
772 default:
773 BUG();
774 }
775
776 mlog_exit_void();
777}
778
779static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
780 int level)
781{
782 mlog_entry_void();
783
784 BUG_ON(!lockres);
785
786 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800787 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 BUG_ON(!lockres->l_ex_holders);
789 lockres->l_ex_holders--;
790 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800791 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800792 BUG_ON(!lockres->l_ro_holders);
793 lockres->l_ro_holders--;
794 break;
795 default:
796 BUG();
797 }
798 mlog_exit_void();
799}
800
801/* WARNING: This function lives in a world where the only three lock
802 * levels are EX, PR, and NL. It *will* have to be adjusted when more
803 * lock types are added. */
804static inline int ocfs2_highest_compat_lock_level(int level)
805{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800806 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800807
Joel Beckerbd3e7612008-02-01 12:14:57 -0800808 if (level == DLM_LOCK_EX)
809 new_level = DLM_LOCK_NL;
810 else if (level == DLM_LOCK_PR)
811 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800812 return new_level;
813}
814
815static void lockres_set_flags(struct ocfs2_lock_res *lockres,
816 unsigned long newflags)
817{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200818 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800819
820 assert_spin_locked(&lockres->l_lock);
821
822 lockres->l_flags = newflags;
823
Christoph Hellwig800deef2007-05-17 16:03:13 +0200824 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
826 continue;
827
828 list_del_init(&mw->mw_item);
829 mw->mw_status = 0;
830 complete(&mw->mw_complete);
831 }
832}
833static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
834{
835 lockres_set_flags(lockres, lockres->l_flags | or);
836}
837static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
838 unsigned long clear)
839{
840 lockres_set_flags(lockres, lockres->l_flags & ~clear);
841}
842
843static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
844{
845 mlog_entry_void();
846
847 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
848 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
849 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800850 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800851
852 lockres->l_level = lockres->l_requested;
853 if (lockres->l_level <=
854 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800855 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800856 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
857 }
858 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
859
860 mlog_exit_void();
861}
862
863static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
864{
865 mlog_entry_void();
866
867 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
868 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
869
870 /* Convert from RO to EX doesn't really need anything as our
871 * information is already up to data. Convert from NL to
872 * *anything* however should mark ourselves as needing an
873 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800874 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700875 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800876 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
877
878 lockres->l_level = lockres->l_requested;
Sunil Mushrana1912822010-01-21 10:50:03 -0800879
880 /*
881 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
882 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
883 * downconverting the lock before the upconvert has fully completed.
884 */
885 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
886
Mark Fashehccd979b2005-12-15 14:31:24 -0800887 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
888
889 mlog_exit_void();
890}
891
892static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
893{
894 mlog_entry_void();
895
Roel Kluin3cf0c502007-10-27 00:20:36 +0200896 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800897 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
898
Joel Beckerbd3e7612008-02-01 12:14:57 -0800899 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700900 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
901 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
903
904 lockres->l_level = lockres->l_requested;
905 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
906 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
907
908 mlog_exit_void();
909}
910
Mark Fashehccd979b2005-12-15 14:31:24 -0800911static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
912 int level)
913{
914 int needs_downconvert = 0;
915 mlog_entry_void();
916
917 assert_spin_locked(&lockres->l_lock);
918
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 if (level > lockres->l_blocking) {
920 /* only schedule a downconvert if we haven't already scheduled
921 * one that goes low enough to satisfy the level we're
922 * blocking. this also catches the case where we get
923 * duplicate BASTs */
924 if (ocfs2_highest_compat_lock_level(level) <
925 ocfs2_highest_compat_lock_level(lockres->l_blocking))
926 needs_downconvert = 1;
927
928 lockres->l_blocking = level;
929 }
930
Sunil Mushran9b915182010-02-26 19:42:44 -0800931 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
932 lockres->l_name, level, lockres->l_level, lockres->l_blocking,
933 needs_downconvert);
934
Wengang Wang0b94a902010-01-21 10:50:02 -0800935 if (needs_downconvert)
936 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
937
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 mlog_exit(needs_downconvert);
939 return needs_downconvert;
940}
941
Joel Beckerde551242008-02-01 14:45:08 -0800942/*
943 * OCFS2_LOCK_PENDING and l_pending_gen.
944 *
945 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
946 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
947 * for more details on the race.
948 *
949 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
950 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
951 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
952 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
953 * the caller is going to try to clear PENDING again. If nothing else is
954 * happening, __lockres_clear_pending() sees PENDING is unset and does
955 * nothing.
956 *
957 * But what if another path (eg downconvert thread) has just started a
958 * new locking action? The other path has re-set PENDING. Our path
959 * cannot clear PENDING, because that will re-open the original race
960 * window.
961 *
962 * [Example]
963 *
964 * ocfs2_meta_lock()
965 * ocfs2_cluster_lock()
966 * set BUSY
967 * set PENDING
968 * drop l_lock
969 * ocfs2_dlm_lock()
970 * ocfs2_locking_ast() ocfs2_downconvert_thread()
971 * clear PENDING ocfs2_unblock_lock()
972 * take_l_lock
973 * !BUSY
974 * ocfs2_prepare_downconvert()
975 * set BUSY
976 * set PENDING
977 * drop l_lock
978 * take l_lock
979 * clear PENDING
980 * drop l_lock
981 * <window>
982 * ocfs2_dlm_lock()
983 *
984 * So as you can see, we now have a window where l_lock is not held,
985 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
986 *
987 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
988 * set by ocfs2_prepare_downconvert(). That wasn't nice.
989 *
990 * To solve this we introduce l_pending_gen. A call to
991 * lockres_clear_pending() will only do so when it is passed a generation
992 * number that matches the lockres. lockres_set_pending() will return the
993 * current generation number. When ocfs2_cluster_lock() goes to clear
994 * PENDING, it passes the generation it got from set_pending(). In our
995 * example above, the generation numbers will *not* match. Thus,
996 * ocfs2_cluster_lock() will not clear the PENDING set by
997 * ocfs2_prepare_downconvert().
998 */
999
1000/* Unlocked version for ocfs2_locking_ast() */
1001static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
1002 unsigned int generation,
1003 struct ocfs2_super *osb)
1004{
1005 assert_spin_locked(&lockres->l_lock);
1006
1007 /*
1008 * The ast and locking functions can race us here. The winner
1009 * will clear pending, the loser will not.
1010 */
1011 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
1012 (lockres->l_pending_gen != generation))
1013 return;
1014
1015 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1016 lockres->l_pending_gen++;
1017
1018 /*
1019 * The downconvert thread may have skipped us because we
1020 * were PENDING. Wake it up.
1021 */
1022 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1023 ocfs2_wake_downconvert_thread(osb);
1024}
1025
1026/* Locked version for callers of ocfs2_dlm_lock() */
1027static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1028 unsigned int generation,
1029 struct ocfs2_super *osb)
1030{
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&lockres->l_lock, flags);
1034 __lockres_clear_pending(lockres, generation, osb);
1035 spin_unlock_irqrestore(&lockres->l_lock, flags);
1036}
1037
1038static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1039{
1040 assert_spin_locked(&lockres->l_lock);
1041 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1042
1043 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1044
1045 return lockres->l_pending_gen;
1046}
1047
Joel Beckerc0e41332010-01-29 14:46:44 -08001048static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001049{
Joel Beckera796d282010-01-28 19:22:39 -08001050 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehaa2623a2006-09-12 21:58:23 -07001051 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 int needs_downconvert;
1053 unsigned long flags;
1054
Joel Beckerbd3e7612008-02-01 12:14:57 -08001055 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001056
Sunil Mushran9b915182010-02-26 19:42:44 -08001057 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1058 "type %s\n", lockres->l_name, level, lockres->l_level,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001059 ocfs2_lock_type_string(lockres->l_type));
1060
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001061 /*
1062 * We can skip the bast for locks which don't enable caching -
1063 * they'll be dropped at the earliest possible time anyway.
1064 */
1065 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1066 return;
1067
Mark Fashehccd979b2005-12-15 14:31:24 -08001068 spin_lock_irqsave(&lockres->l_lock, flags);
1069 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1070 if (needs_downconvert)
1071 ocfs2_schedule_blocked_lock(osb, lockres);
1072 spin_unlock_irqrestore(&lockres->l_lock, flags);
1073
Mark Fashehd680efe2006-09-08 14:14:34 -07001074 wake_up(&lockres->l_event);
1075
Mark Fasheh34d024f2007-09-24 15:56:19 -07001076 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001077}
1078
Joel Beckerc0e41332010-01-29 14:46:44 -08001079static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
Mark Fashehccd979b2005-12-15 14:31:24 -08001080{
Joel Beckera796d282010-01-28 19:22:39 -08001081 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Joel Beckerde551242008-02-01 14:45:08 -08001082 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001084 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001085
1086 spin_lock_irqsave(&lockres->l_lock, flags);
1087
David Teigland1693a5c2008-01-30 16:52:53 -08001088 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1089
1090 if (status == -EAGAIN) {
1091 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1092 goto out;
1093 }
1094
1095 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001096 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001097 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001098 spin_unlock_irqrestore(&lockres->l_lock, flags);
1099 return;
1100 }
1101
Sunil Mushran9b915182010-02-26 19:42:44 -08001102 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
1103 "level %d => %d\n", lockres->l_name, lockres->l_action,
1104 lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
1105
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 switch(lockres->l_action) {
1107 case OCFS2_AST_ATTACH:
1108 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001109 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001110 break;
1111 case OCFS2_AST_CONVERT:
1112 ocfs2_generic_handle_convert_action(lockres);
1113 break;
1114 case OCFS2_AST_DOWNCONVERT:
1115 ocfs2_generic_handle_downconvert_action(lockres);
1116 break;
1117 default:
Sunil Mushran9b915182010-02-26 19:42:44 -08001118 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1119 "flags 0x%lx, unlock: %u\n",
Mark Fashehe92d57d2006-09-12 21:34:35 -07001120 lockres->l_name, lockres->l_action, lockres->l_flags,
1121 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001122 BUG();
1123 }
David Teigland1693a5c2008-01-30 16:52:53 -08001124out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001125 /* set it to something invalid so if we get called again we
1126 * can catch it. */
1127 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001128
Joel Beckerde551242008-02-01 14:45:08 -08001129 /* Did we try to cancel this lock? Clear that state */
1130 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1131 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1132
1133 /*
1134 * We may have beaten the locking functions here. We certainly
1135 * know that dlm_lock() has been called :-)
1136 * Because we can't have two lock calls in flight at once, we
1137 * can use lockres->l_pending_gen.
1138 */
1139 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1140
Mark Fashehccd979b2005-12-15 14:31:24 -08001141 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001142 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001143}
1144
Joel Becker553b5eb2010-01-29 17:19:06 -08001145static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
1146{
1147 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1148 unsigned long flags;
1149
1150 mlog_entry_void();
1151
Sunil Mushran9b915182010-02-26 19:42:44 -08001152 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1153 lockres->l_name, lockres->l_unlock_action);
Joel Becker553b5eb2010-01-29 17:19:06 -08001154
1155 spin_lock_irqsave(&lockres->l_lock, flags);
1156 if (error) {
1157 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
1158 "unlock_action %d\n", error, lockres->l_name,
1159 lockres->l_unlock_action);
1160 spin_unlock_irqrestore(&lockres->l_lock, flags);
1161 mlog_exit_void();
1162 return;
1163 }
1164
1165 switch(lockres->l_unlock_action) {
1166 case OCFS2_UNLOCK_CANCEL_CONVERT:
1167 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
1168 lockres->l_action = OCFS2_AST_INVALID;
1169 /* Downconvert thread may have requeued this lock, we
1170 * need to wake it. */
1171 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1172 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
1173 break;
1174 case OCFS2_UNLOCK_DROP_LOCK:
1175 lockres->l_level = DLM_LOCK_IV;
1176 break;
1177 default:
1178 BUG();
1179 }
1180
1181 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1182 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1183 wake_up(&lockres->l_event);
1184 spin_unlock_irqrestore(&lockres->l_lock, flags);
1185
1186 mlog_exit_void();
1187}
1188
1189/*
1190 * This is the filesystem locking protocol. It provides the lock handling
1191 * hooks for the underlying DLM. It has a maximum version number.
1192 * The version number allows interoperability with systems running at
1193 * the same major number and an equal or smaller minor number.
1194 *
1195 * Whenever the filesystem does new things with locks (adds or removes a
1196 * lock, orders them differently, does different things underneath a lock),
1197 * the version must be changed. The protocol is negotiated when joining
1198 * the dlm domain. A node may join the domain if its major version is
1199 * identical to all other nodes and its minor version is greater than
1200 * or equal to all other nodes. When its minor version is greater than
1201 * the other nodes, it will run at the minor version specified by the
1202 * other nodes.
1203 *
1204 * If a locking change is made that will not be compatible with older
1205 * versions, the major number must be increased and the minor version set
1206 * to zero. If a change merely adds a behavior that can be disabled when
1207 * speaking to older versions, the minor version must be increased. If a
1208 * change adds a fully backwards compatible change (eg, LVB changes that
1209 * are just ignored by older versions), the version does not need to be
1210 * updated.
1211 */
1212static struct ocfs2_locking_protocol lproto = {
1213 .lp_max_version = {
1214 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
1215 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
1216 },
1217 .lp_lock_ast = ocfs2_locking_ast,
1218 .lp_blocking_ast = ocfs2_blocking_ast,
1219 .lp_unlock_ast = ocfs2_unlock_ast,
1220};
1221
1222void ocfs2_set_locking_protocol(void)
1223{
1224 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1225}
1226
Mark Fashehccd979b2005-12-15 14:31:24 -08001227static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1228 int convert)
1229{
1230 unsigned long flags;
1231
1232 mlog_entry_void();
1233 spin_lock_irqsave(&lockres->l_lock, flags);
1234 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Sunil Mushrana1912822010-01-21 10:50:03 -08001235 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
Mark Fashehccd979b2005-12-15 14:31:24 -08001236 if (convert)
1237 lockres->l_action = OCFS2_AST_INVALID;
1238 else
1239 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1240 spin_unlock_irqrestore(&lockres->l_lock, flags);
1241
1242 wake_up(&lockres->l_event);
1243 mlog_exit_void();
1244}
1245
1246/* Note: If we detect another process working on the lock (i.e.,
1247 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1248 * to do the right thing in that case.
1249 */
1250static int ocfs2_lock_create(struct ocfs2_super *osb,
1251 struct ocfs2_lock_res *lockres,
1252 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001253 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001254{
1255 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001256 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001257 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001258
1259 mlog_entry_void();
1260
Joel Beckerbd3e7612008-02-01 12:14:57 -08001261 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 dlm_flags);
1263
1264 spin_lock_irqsave(&lockres->l_lock, flags);
1265 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1266 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1267 spin_unlock_irqrestore(&lockres->l_lock, flags);
1268 goto bail;
1269 }
1270
1271 lockres->l_action = OCFS2_AST_ATTACH;
1272 lockres->l_requested = level;
1273 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001274 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 spin_unlock_irqrestore(&lockres->l_lock, flags);
1276
Joel Becker4670c462008-02-01 14:39:35 -08001277 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001278 level,
1279 &lockres->l_lksb,
1280 dlm_flags,
1281 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001282 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001283 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001284 if (ret) {
1285 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001286 ocfs2_recover_from_dlm_error(lockres, 1);
1287 }
1288
Joel Becker7431cd72008-02-01 12:15:37 -08001289 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001290
1291bail:
1292 mlog_exit(ret);
1293 return ret;
1294}
1295
1296static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1297 int flag)
1298{
1299 unsigned long flags;
1300 int ret;
1301
1302 spin_lock_irqsave(&lockres->l_lock, flags);
1303 ret = lockres->l_flags & flag;
1304 spin_unlock_irqrestore(&lockres->l_lock, flags);
1305
1306 return ret;
1307}
1308
1309static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1310
1311{
1312 wait_event(lockres->l_event,
1313 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1314}
1315
1316static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1317
1318{
1319 wait_event(lockres->l_event,
1320 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1321}
1322
1323/* predict what lock level we'll be dropping down to on behalf
1324 * of another node, and return true if the currently wanted
1325 * level will be compatible with it. */
1326static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1327 int wanted)
1328{
1329 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1330
1331 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1332}
1333
1334static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1335{
1336 INIT_LIST_HEAD(&mw->mw_item);
1337 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001338 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001339}
1340
1341static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1342{
1343 wait_for_completion(&mw->mw_complete);
1344 /* Re-arm the completion in case we want to wait on it again */
1345 INIT_COMPLETION(mw->mw_complete);
1346 return mw->mw_status;
1347}
1348
1349static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1350 struct ocfs2_mask_waiter *mw,
1351 unsigned long mask,
1352 unsigned long goal)
1353{
1354 BUG_ON(!list_empty(&mw->mw_item));
1355
1356 assert_spin_locked(&lockres->l_lock);
1357
1358 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1359 mw->mw_mask = mask;
1360 mw->mw_goal = goal;
1361}
1362
1363/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1364 * if the mask still hadn't reached its goal */
1365static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1366 struct ocfs2_mask_waiter *mw)
1367{
1368 unsigned long flags;
1369 int ret = 0;
1370
1371 spin_lock_irqsave(&lockres->l_lock, flags);
1372 if (!list_empty(&mw->mw_item)) {
1373 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1374 ret = -EBUSY;
1375
1376 list_del_init(&mw->mw_item);
1377 init_completion(&mw->mw_complete);
1378 }
1379 spin_unlock_irqrestore(&lockres->l_lock, flags);
1380
1381 return ret;
1382
1383}
1384
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001385static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1386 struct ocfs2_lock_res *lockres)
1387{
1388 int ret;
1389
1390 ret = wait_for_completion_interruptible(&mw->mw_complete);
1391 if (ret)
1392 lockres_remove_mask_waiter(lockres, mw);
1393 else
1394 ret = mw->mw_status;
1395 /* Re-arm the completion in case we want to wait on it again */
1396 INIT_COMPLETION(mw->mw_complete);
1397 return ret;
1398}
1399
Jan Karacb257972009-06-04 15:26:50 +02001400static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1401 struct ocfs2_lock_res *lockres,
1402 int level,
1403 u32 lkm_flags,
1404 int arg_flags,
1405 int l_subclass,
1406 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001407{
1408 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001409 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1410 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1411 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001412 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001413 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001414
1415 mlog_entry_void();
1416
1417 ocfs2_init_mask_waiter(&mw);
1418
Mark Fashehb80fc012006-09-12 22:08:14 -07001419 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001420 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001421
Mark Fashehccd979b2005-12-15 14:31:24 -08001422again:
1423 wait = 0;
1424
Sunil Mushrana1912822010-01-21 10:50:03 -08001425 spin_lock_irqsave(&lockres->l_lock, flags);
1426
Mark Fashehccd979b2005-12-15 14:31:24 -08001427 if (catch_signals && signal_pending(current)) {
1428 ret = -ERESTARTSYS;
Sunil Mushrana1912822010-01-21 10:50:03 -08001429 goto unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 }
1431
Mark Fashehccd979b2005-12-15 14:31:24 -08001432 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1433 "Cluster lock called on freeing lockres %s! flags "
1434 "0x%lx\n", lockres->l_name, lockres->l_flags);
1435
1436 /* We only compare against the currently granted level
1437 * here. If the lock is blocked waiting on a downconvert,
1438 * we'll get caught below. */
1439 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1440 level > lockres->l_level) {
1441 /* is someone sitting in dlm_lock? If so, wait on
1442 * them. */
1443 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1444 wait = 1;
1445 goto unlock;
1446 }
1447
Sunil Mushrana1912822010-01-21 10:50:03 -08001448 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1449 /*
1450 * We've upconverted. If the lock now has a level we can
1451 * work with, we take it. If, however, the lock is not at the
1452 * required level, we go thru the full cycle. One way this could
1453 * happen is if a process requesting an upconvert to PR is
1454 * closely followed by another requesting upconvert to an EX.
1455 * If the process requesting EX lands here, we want it to
1456 * continue attempting to upconvert and let the process
1457 * requesting PR take the lock.
1458 * If multiple processes request upconvert to PR, the first one
1459 * here will take the lock. The others will have to go thru the
1460 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1461 * downconvert request.
1462 */
1463 if (level <= lockres->l_level)
1464 goto update_holders;
1465 }
1466
Mark Fashehccd979b2005-12-15 14:31:24 -08001467 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1468 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1469 /* is the lock is currently blocked on behalf of
1470 * another node */
1471 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1472 wait = 1;
1473 goto unlock;
1474 }
1475
1476 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001477 if (noqueue_attempted > 0) {
1478 ret = -EAGAIN;
1479 goto unlock;
1480 }
1481 if (lkm_flags & DLM_LKF_NOQUEUE)
1482 noqueue_attempted = 1;
1483
Mark Fashehccd979b2005-12-15 14:31:24 -08001484 if (lockres->l_action != OCFS2_AST_INVALID)
1485 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1486 lockres->l_name, lockres->l_action);
1487
Mark Fasheh019d1b22007-10-05 12:09:05 -07001488 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1489 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001490 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001491 } else {
1492 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001493 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001494 }
1495
Mark Fashehccd979b2005-12-15 14:31:24 -08001496 lockres->l_requested = level;
1497 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001498 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 spin_unlock_irqrestore(&lockres->l_lock, flags);
1500
Joel Beckerbd3e7612008-02-01 12:14:57 -08001501 BUG_ON(level == DLM_LOCK_IV);
1502 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001503
Sunil Mushran9b915182010-02-26 19:42:44 -08001504 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 lockres->l_name, lockres->l_level, level);
1506
1507 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001508 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001509 level,
1510 &lockres->l_lksb,
1511 lkm_flags,
1512 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001513 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001514 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001515 if (ret) {
1516 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1517 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001518 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001519 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001520 }
1521 ocfs2_recover_from_dlm_error(lockres, 1);
1522 goto out;
1523 }
1524
Coly Li73ac36e2009-01-07 18:09:16 -08001525 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001526 lockres->l_name);
1527
1528 /* At this point we've gone inside the dlm and need to
1529 * complete our work regardless. */
1530 catch_signals = 0;
1531
1532 /* wait for busy to clear and carry on */
1533 goto again;
1534 }
1535
Sunil Mushrana1912822010-01-21 10:50:03 -08001536update_holders:
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 /* Ok, if we get here then we're good to go. */
1538 ocfs2_inc_holders(lockres, level);
1539
1540 ret = 0;
1541unlock:
Sunil Mushrana1912822010-01-21 10:50:03 -08001542 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1543
Mark Fashehccd979b2005-12-15 14:31:24 -08001544 spin_unlock_irqrestore(&lockres->l_lock, flags);
1545out:
1546 /*
1547 * This is helping work around a lock inversion between the page lock
1548 * and dlm locks. One path holds the page lock while calling aops
1549 * which block acquiring dlm locks. The voting thread holds dlm
1550 * locks while acquiring page locks while down converting data locks.
1551 * This block is helping an aop path notice the inversion and back
1552 * off to unlock its page lock before trying the dlm lock again.
1553 */
1554 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1555 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1556 wait = 0;
1557 if (lockres_remove_mask_waiter(lockres, &mw))
1558 ret = -EAGAIN;
1559 else
1560 goto again;
1561 }
1562 if (wait) {
1563 ret = ocfs2_wait_for_mask(&mw);
1564 if (ret == 0)
1565 goto again;
1566 mlog_errno(ret);
1567 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001568 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001569
Jan Karacb257972009-06-04 15:26:50 +02001570#ifdef CONFIG_DEBUG_LOCK_ALLOC
1571 if (!ret && lockres->l_lockdep_map.key != NULL) {
1572 if (level == DLM_LOCK_PR)
1573 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1574 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1575 caller_ip);
1576 else
1577 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1578 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1579 caller_ip);
1580 }
1581#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001582 mlog_exit(ret);
1583 return ret;
1584}
1585
Jan Karacb257972009-06-04 15:26:50 +02001586static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1587 struct ocfs2_lock_res *lockres,
1588 int level,
1589 u32 lkm_flags,
1590 int arg_flags)
1591{
1592 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1593 0, _RET_IP_);
1594}
1595
1596
1597static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1598 struct ocfs2_lock_res *lockres,
1599 int level,
1600 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001601{
1602 unsigned long flags;
1603
1604 mlog_entry_void();
1605 spin_lock_irqsave(&lockres->l_lock, flags);
1606 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001607 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001608 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001609#ifdef CONFIG_DEBUG_LOCK_ALLOC
1610 if (lockres->l_lockdep_map.key != NULL)
1611 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1612#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 mlog_exit_void();
1614}
1615
Adrian Bunkda661162006-11-20 03:24:28 +01001616static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1617 struct ocfs2_lock_res *lockres,
1618 int ex,
1619 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001620{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001621 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001622 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001623 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001624
1625 spin_lock_irqsave(&lockres->l_lock, flags);
1626 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1627 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1628 spin_unlock_irqrestore(&lockres->l_lock, flags);
1629
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001630 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001631}
1632
1633/* Grants us an EX lock on the data and metadata resources, skipping
1634 * the normal cluster directory lookup. Use this ONLY on newly created
1635 * inodes which other nodes can't possibly see, and which haven't been
1636 * hashed in the inode hash yet. This can give us a good performance
1637 * increase as it'll skip the network broadcast normally associated
1638 * with creating a new lock resource. */
1639int ocfs2_create_new_inode_locks(struct inode *inode)
1640{
1641 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001642 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001643
1644 BUG_ON(!inode);
1645 BUG_ON(!ocfs2_inode_is_new(inode));
1646
1647 mlog_entry_void();
1648
Mark Fashehb06970532006-03-03 10:24:33 -08001649 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001650
1651 /* NOTE: That we don't increment any of the holder counts, nor
1652 * do we add anything to a journal handle. Since this is
1653 * supposed to be a new inode which the cluster doesn't know
1654 * about yet, there is no need to. As far as the LVB handling
1655 * is concerned, this is basically like acquiring an EX lock
1656 * on a resource which has an invalid one -- we'll set it
1657 * valid when we release the EX. */
1658
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001659 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001660 if (ret) {
1661 mlog_errno(ret);
1662 goto bail;
1663 }
1664
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001665 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001666 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001667 * don't use a generation in their lock names.
1668 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001669 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001670 if (ret) {
1671 mlog_errno(ret);
1672 goto bail;
1673 }
1674
Tiger Yang50008632007-03-20 16:01:38 -07001675 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1676 if (ret) {
1677 mlog_errno(ret);
1678 goto bail;
1679 }
1680
Mark Fashehccd979b2005-12-15 14:31:24 -08001681bail:
1682 mlog_exit(ret);
1683 return ret;
1684}
1685
1686int ocfs2_rw_lock(struct inode *inode, int write)
1687{
1688 int status, level;
1689 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001690 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001691
1692 BUG_ON(!inode);
1693
1694 mlog_entry_void();
1695
Mark Fashehb06970532006-03-03 10:24:33 -08001696 mlog(0, "inode %llu take %s RW lock\n",
1697 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001698 write ? "EXMODE" : "PRMODE");
1699
Coly Lid92bc512009-08-28 19:03:18 +08001700 if (ocfs2_mount_local(osb)) {
1701 mlog_exit(0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001702 return 0;
Coly Lid92bc512009-08-28 19:03:18 +08001703 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001704
Mark Fashehccd979b2005-12-15 14:31:24 -08001705 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1706
Joel Beckerbd3e7612008-02-01 12:14:57 -08001707 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001708
1709 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1710 0);
1711 if (status < 0)
1712 mlog_errno(status);
1713
1714 mlog_exit(status);
1715 return status;
1716}
1717
1718void ocfs2_rw_unlock(struct inode *inode, int write)
1719{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001720 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001721 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001722 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001723
1724 mlog_entry_void();
1725
Mark Fashehb06970532006-03-03 10:24:33 -08001726 mlog(0, "inode %llu drop %s RW lock\n",
1727 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 write ? "EXMODE" : "PRMODE");
1729
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001730 if (!ocfs2_mount_local(osb))
1731 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001732
1733 mlog_exit_void();
1734}
1735
Tiger Yang50008632007-03-20 16:01:38 -07001736/*
1737 * ocfs2_open_lock always get PR mode lock.
1738 */
1739int ocfs2_open_lock(struct inode *inode)
1740{
1741 int status = 0;
1742 struct ocfs2_lock_res *lockres;
1743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1744
1745 BUG_ON(!inode);
1746
1747 mlog_entry_void();
1748
1749 mlog(0, "inode %llu take PRMODE open lock\n",
1750 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1751
1752 if (ocfs2_mount_local(osb))
1753 goto out;
1754
1755 lockres = &OCFS2_I(inode)->ip_open_lockres;
1756
1757 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001758 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001759 if (status < 0)
1760 mlog_errno(status);
1761
1762out:
1763 mlog_exit(status);
1764 return status;
1765}
1766
1767int ocfs2_try_open_lock(struct inode *inode, int write)
1768{
1769 int status = 0, level;
1770 struct ocfs2_lock_res *lockres;
1771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1772
1773 BUG_ON(!inode);
1774
1775 mlog_entry_void();
1776
1777 mlog(0, "inode %llu try to take %s open lock\n",
1778 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1779 write ? "EXMODE" : "PRMODE");
1780
1781 if (ocfs2_mount_local(osb))
1782 goto out;
1783
1784 lockres = &OCFS2_I(inode)->ip_open_lockres;
1785
Joel Beckerbd3e7612008-02-01 12:14:57 -08001786 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001787
1788 /*
1789 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001790 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001791 * other nodes and the -EAGAIN will indicate to the caller that
1792 * this inode is still in use.
1793 */
1794 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001795 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001796
1797out:
1798 mlog_exit(status);
1799 return status;
1800}
1801
1802/*
1803 * ocfs2_open_unlock unlock PR and EX mode open locks.
1804 */
1805void ocfs2_open_unlock(struct inode *inode)
1806{
1807 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1808 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1809
1810 mlog_entry_void();
1811
1812 mlog(0, "inode %llu drop open lock\n",
1813 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1814
1815 if (ocfs2_mount_local(osb))
1816 goto out;
1817
1818 if(lockres->l_ro_holders)
1819 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001820 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001821 if(lockres->l_ex_holders)
1822 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001823 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001824
1825out:
1826 mlog_exit_void();
1827}
1828
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001829static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1830 int level)
1831{
1832 int ret;
1833 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1834 unsigned long flags;
1835 struct ocfs2_mask_waiter mw;
1836
1837 ocfs2_init_mask_waiter(&mw);
1838
1839retry_cancel:
1840 spin_lock_irqsave(&lockres->l_lock, flags);
1841 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1842 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1843 if (ret) {
1844 spin_unlock_irqrestore(&lockres->l_lock, flags);
1845 ret = ocfs2_cancel_convert(osb, lockres);
1846 if (ret < 0) {
1847 mlog_errno(ret);
1848 goto out;
1849 }
1850 goto retry_cancel;
1851 }
1852 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1853 spin_unlock_irqrestore(&lockres->l_lock, flags);
1854
1855 ocfs2_wait_for_mask(&mw);
1856 goto retry_cancel;
1857 }
1858
1859 ret = -ERESTARTSYS;
1860 /*
1861 * We may still have gotten the lock, in which case there's no
1862 * point to restarting the syscall.
1863 */
1864 if (lockres->l_level == level)
1865 ret = 0;
1866
1867 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1868 lockres->l_flags, lockres->l_level, lockres->l_action);
1869
1870 spin_unlock_irqrestore(&lockres->l_lock, flags);
1871
1872out:
1873 return ret;
1874}
1875
1876/*
1877 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1878 * flock() calls. The locking approach this requires is sufficiently
1879 * different from all other cluster lock types that we implement a
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001880 * separate path to the "low-level" dlm calls. In particular:
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001881 *
1882 * - No optimization of lock levels is done - we take at exactly
1883 * what's been requested.
1884 *
1885 * - No lock caching is employed. We immediately downconvert to
1886 * no-lock at unlock time. This also means flock locks never go on
1887 * the blocking list).
1888 *
1889 * - Since userspace can trivially deadlock itself with flock, we make
1890 * sure to allow cancellation of a misbehaving applications flock()
1891 * request.
1892 *
1893 * - Access to any flock lockres doesn't require concurrency, so we
1894 * can simplify the code by requiring the caller to guarantee
1895 * serialization of dlmglue flock calls.
1896 */
1897int ocfs2_file_lock(struct file *file, int ex, int trylock)
1898{
Mark Fashehe988cf12008-07-10 09:25:39 -07001899 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1900 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001901 unsigned long flags;
1902 struct ocfs2_file_private *fp = file->private_data;
1903 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1904 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1905 struct ocfs2_mask_waiter mw;
1906
1907 ocfs2_init_mask_waiter(&mw);
1908
1909 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001910 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001911 mlog(ML_ERROR,
1912 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1913 "level: %u\n", lockres->l_name, lockres->l_flags,
1914 lockres->l_level);
1915 return -EINVAL;
1916 }
1917
1918 spin_lock_irqsave(&lockres->l_lock, flags);
1919 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1920 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1921 spin_unlock_irqrestore(&lockres->l_lock, flags);
1922
1923 /*
1924 * Get the lock at NLMODE to start - that way we
1925 * can cancel the upconvert request if need be.
1926 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001927 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001928 if (ret < 0) {
1929 mlog_errno(ret);
1930 goto out;
1931 }
1932
1933 ret = ocfs2_wait_for_mask(&mw);
1934 if (ret) {
1935 mlog_errno(ret);
1936 goto out;
1937 }
1938 spin_lock_irqsave(&lockres->l_lock, flags);
1939 }
1940
1941 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001942 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001943 lockres->l_requested = level;
1944 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1945
1946 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1947 spin_unlock_irqrestore(&lockres->l_lock, flags);
1948
Joel Becker4670c462008-02-01 14:39:35 -08001949 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Beckera796d282010-01-28 19:22:39 -08001950 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Becker7431cd72008-02-01 12:15:37 -08001951 if (ret) {
1952 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001953 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001954 ret = -EINVAL;
1955 }
1956
1957 ocfs2_recover_from_dlm_error(lockres, 1);
1958 lockres_remove_mask_waiter(lockres, &mw);
1959 goto out;
1960 }
1961
1962 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1963 if (ret == -ERESTARTSYS) {
1964 /*
1965 * Userspace can cause deadlock itself with
1966 * flock(). Current behavior locally is to allow the
1967 * deadlock, but abort the system call if a signal is
1968 * received. We follow this example, otherwise a
1969 * poorly written program could sit in kernel until
1970 * reboot.
1971 *
1972 * Handling this is a bit more complicated for Ocfs2
1973 * though. We can't exit this function with an
1974 * outstanding lock request, so a cancel convert is
1975 * required. We intentionally overwrite 'ret' - if the
1976 * cancel fails and the lock was granted, it's easier
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001977 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001978 */
1979 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001980 } else if (!ret && (level > lockres->l_level)) {
1981 /* Trylock failed asynchronously */
1982 BUG_ON(!trylock);
1983 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001984 }
1985
1986out:
1987
1988 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1989 lockres->l_name, ex, trylock, ret);
1990 return ret;
1991}
1992
1993void ocfs2_file_unlock(struct file *file)
1994{
1995 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001996 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001997 unsigned long flags;
1998 struct ocfs2_file_private *fp = file->private_data;
1999 struct ocfs2_lock_res *lockres = &fp->fp_flock;
2000 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
2001 struct ocfs2_mask_waiter mw;
2002
2003 ocfs2_init_mask_waiter(&mw);
2004
2005 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
2006 return;
2007
Mark Fashehe988cf12008-07-10 09:25:39 -07002008 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002009 return;
2010
2011 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
2012 lockres->l_name, lockres->l_flags, lockres->l_level,
2013 lockres->l_action);
2014
2015 spin_lock_irqsave(&lockres->l_lock, flags);
2016 /*
2017 * Fake a blocking ast for the downconvert code.
2018 */
2019 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08002020 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002021
Mark Fashehe988cf12008-07-10 09:25:39 -07002022 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002023 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2024 spin_unlock_irqrestore(&lockres->l_lock, flags);
2025
Mark Fashehe988cf12008-07-10 09:25:39 -07002026 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08002027 if (ret) {
2028 mlog_errno(ret);
2029 return;
2030 }
2031
2032 ret = ocfs2_wait_for_mask(&mw);
2033 if (ret)
2034 mlog_errno(ret);
2035}
2036
Mark Fasheh34d024f2007-09-24 15:56:19 -07002037static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
2038 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002039{
2040 int kick = 0;
2041
2042 mlog_entry_void();
2043
2044 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07002045 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08002046 * condition. */
2047 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
2048 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002049 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08002050 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
2051 kick = 1;
2052 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002053 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08002054 if (!lockres->l_ex_holders)
2055 kick = 1;
2056 break;
2057 default:
2058 BUG();
2059 }
2060 }
2061
2062 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07002063 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002064
2065 mlog_exit_void();
2066}
2067
Mark Fashehccd979b2005-12-15 14:31:24 -08002068#define OCFS2_SEC_BITS 34
2069#define OCFS2_SEC_SHIFT (64 - 34)
2070#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2071
2072/* LVB only has room for 64 bits of time here so we pack it for
2073 * now. */
2074static u64 ocfs2_pack_timespec(struct timespec *spec)
2075{
2076 u64 res;
2077 u64 sec = spec->tv_sec;
2078 u32 nsec = spec->tv_nsec;
2079
2080 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
2081
2082 return res;
2083}
2084
2085/* Call this with the lockres locked. I am reasonably sure we don't
2086 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07002087 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002088static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2089{
2090 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002091 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002092 struct ocfs2_meta_lvb *lvb;
2093
2094 mlog_entry_void();
2095
Mark Fasheha641dc22008-12-24 16:03:48 -08002096 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002097
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002098 /*
2099 * Invalidate the LVB of a deleted inode - this way other
2100 * nodes are forced to go to disk and discover the new inode
2101 * status.
2102 */
2103 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2104 lvb->lvb_version = 0;
2105 goto out;
2106 }
2107
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07002108 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08002109 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2110 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
2111 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
2112 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
2113 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2114 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2115 lvb->lvb_iatime_packed =
2116 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
2117 lvb->lvb_ictime_packed =
2118 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2119 lvb->lvb_imtime_packed =
2120 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002121 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002122 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002123 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002124
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002125out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002126 mlog_meta_lvb(0, lockres);
2127
2128 mlog_exit_void();
2129}
2130
2131static void ocfs2_unpack_timespec(struct timespec *spec,
2132 u64 packed_time)
2133{
2134 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2135 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2136}
2137
2138static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2139{
2140 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002141 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002142 struct ocfs2_meta_lvb *lvb;
2143
2144 mlog_entry_void();
2145
2146 mlog_meta_lvb(0, lockres);
2147
Mark Fasheha641dc22008-12-24 16:03:48 -08002148 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002149
2150 /* We're safe here without the lockres lock... */
2151 spin_lock(&oi->ip_lock);
2152 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2153 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2154
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002155 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002156 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002157 ocfs2_set_inode_flags(inode);
2158
Mark Fashehccd979b2005-12-15 14:31:24 -08002159 /* fast-symlinks are a special case */
2160 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2161 inode->i_blocks = 0;
2162 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002163 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002164
2165 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2166 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2167 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2168 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2169 ocfs2_unpack_timespec(&inode->i_atime,
2170 be64_to_cpu(lvb->lvb_iatime_packed));
2171 ocfs2_unpack_timespec(&inode->i_mtime,
2172 be64_to_cpu(lvb->lvb_imtime_packed));
2173 ocfs2_unpack_timespec(&inode->i_ctime,
2174 be64_to_cpu(lvb->lvb_ictime_packed));
2175 spin_unlock(&oi->ip_lock);
2176
2177 mlog_exit_void();
2178}
2179
Mark Fashehf9e2d822006-09-12 15:35:49 -07002180static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2181 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002182{
Mark Fasheha641dc22008-12-24 16:03:48 -08002183 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002184
Joel Becker1c520df2009-06-19 15:14:13 -07002185 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2186 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002187 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002188 return 1;
2189 return 0;
2190}
2191
2192/* Determine whether a lock resource needs to be refreshed, and
2193 * arbitrate who gets to refresh it.
2194 *
2195 * 0 means no refresh needed.
2196 *
2197 * > 0 means you need to refresh this and you MUST call
2198 * ocfs2_complete_lock_res_refresh afterwards. */
2199static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2200{
2201 unsigned long flags;
2202 int status = 0;
2203
2204 mlog_entry_void();
2205
2206refresh_check:
2207 spin_lock_irqsave(&lockres->l_lock, flags);
2208 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2209 spin_unlock_irqrestore(&lockres->l_lock, flags);
2210 goto bail;
2211 }
2212
2213 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2214 spin_unlock_irqrestore(&lockres->l_lock, flags);
2215
2216 ocfs2_wait_on_refreshing_lock(lockres);
2217 goto refresh_check;
2218 }
2219
2220 /* Ok, I'll be the one to refresh this lock. */
2221 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2222 spin_unlock_irqrestore(&lockres->l_lock, flags);
2223
2224 status = 1;
2225bail:
2226 mlog_exit(status);
2227 return status;
2228}
2229
2230/* If status is non zero, I'll mark it as not being in refresh
2231 * anymroe, but i won't clear the needs refresh flag. */
2232static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2233 int status)
2234{
2235 unsigned long flags;
2236 mlog_entry_void();
2237
2238 spin_lock_irqsave(&lockres->l_lock, flags);
2239 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2240 if (!status)
2241 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2242 spin_unlock_irqrestore(&lockres->l_lock, flags);
2243
2244 wake_up(&lockres->l_event);
2245
2246 mlog_exit_void();
2247}
2248
2249/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002250static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002251 struct buffer_head **bh)
2252{
2253 int status = 0;
2254 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002255 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002256 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002257 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002258
2259 mlog_entry_void();
2260
Mark Fashehbe9e9862007-04-18 15:22:08 -07002261 if (ocfs2_mount_local(osb))
2262 goto bail;
2263
Mark Fashehccd979b2005-12-15 14:31:24 -08002264 spin_lock(&oi->ip_lock);
2265 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002266 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002267 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002268 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002269 spin_unlock(&oi->ip_lock);
2270 status = -ENOENT;
2271 goto bail;
2272 }
2273 spin_unlock(&oi->ip_lock);
2274
Mark Fashehbe9e9862007-04-18 15:22:08 -07002275 if (!ocfs2_should_refresh_lock_res(lockres))
2276 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002277
2278 /* This will discard any caching information we might have had
2279 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002280 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002281
Mark Fasheh83418972007-04-23 18:53:12 -07002282 ocfs2_extent_map_trunc(inode, 0);
2283
Mark Fashehbe9e9862007-04-18 15:22:08 -07002284 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002285 mlog(0, "Trusting LVB on inode %llu\n",
2286 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002287 ocfs2_refresh_inode_from_lvb(inode);
2288 } else {
2289 /* Boo, we have to go to disk. */
2290 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002291 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002292 if (status < 0) {
2293 mlog_errno(status);
2294 goto bail_refresh;
2295 }
2296 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2297
2298 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002299 * locking an invalid object. ocfs2_read_inode_block()
2300 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002301 *
2302 * We bug on a stale inode here because we checked
2303 * above whether it was wiped from disk. The wiping
2304 * node provides a guarantee that we receive that
2305 * message and can mark the inode before dropping any
2306 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 mlog_bug_on_msg(inode->i_generation !=
2308 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002309 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002310 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002311 (unsigned long long)oi->ip_blkno,
2312 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002313 inode->i_generation);
2314 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2315 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002316 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2317 (unsigned long long)oi->ip_blkno,
2318 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002319 le32_to_cpu(fe->i_flags));
2320
2321 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002322 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002323 }
2324
2325 status = 0;
2326bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002327 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002328bail:
2329 mlog_exit(status);
2330 return status;
2331}
2332
2333static int ocfs2_assign_bh(struct inode *inode,
2334 struct buffer_head **ret_bh,
2335 struct buffer_head *passed_bh)
2336{
2337 int status;
2338
2339 if (passed_bh) {
2340 /* Ok, the update went to disk for us, use the
2341 * returned bh. */
2342 *ret_bh = passed_bh;
2343 get_bh(*ret_bh);
2344
2345 return 0;
2346 }
2347
Joel Beckerb657c952008-11-13 14:49:11 -08002348 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002349 if (status < 0)
2350 mlog_errno(status);
2351
2352 return status;
2353}
2354
2355/*
2356 * returns < 0 error if the callback will never be called, otherwise
2357 * the result of the lock will be communicated via the callback.
2358 */
Jan Karacb257972009-06-04 15:26:50 +02002359int ocfs2_inode_lock_full_nested(struct inode *inode,
2360 struct buffer_head **ret_bh,
2361 int ex,
2362 int arg_flags,
2363 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002364{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002365 int status, level, acquired;
2366 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002367 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002368 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2369 struct buffer_head *local_bh = NULL;
2370
2371 BUG_ON(!inode);
2372
2373 mlog_entry_void();
2374
Mark Fashehb06970532006-03-03 10:24:33 -08002375 mlog(0, "inode %llu, take %s META lock\n",
2376 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002377 ex ? "EXMODE" : "PRMODE");
2378
2379 status = 0;
2380 acquired = 0;
2381 /* We'll allow faking a readonly metadata lock for
2382 * rodevices. */
2383 if (ocfs2_is_hard_readonly(osb)) {
2384 if (ex)
2385 status = -EROFS;
2386 goto bail;
2387 }
2388
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002389 if (ocfs2_mount_local(osb))
2390 goto local;
2391
Mark Fashehccd979b2005-12-15 14:31:24 -08002392 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002393 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002394
Mark Fashehe63aecb62007-10-18 15:30:42 -07002395 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002396 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002397 dlm_flags = 0;
2398 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002399 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002400
Jan Karacb257972009-06-04 15:26:50 +02002401 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2402 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 if (status < 0) {
2404 if (status != -EAGAIN && status != -EIOCBRETRY)
2405 mlog_errno(status);
2406 goto bail;
2407 }
2408
2409 /* Notify the error cleanup path to drop the cluster lock. */
2410 acquired = 1;
2411
2412 /* We wait twice because a node may have died while we were in
2413 * the lower dlm layers. The second time though, we've
2414 * committed to owning this lock so we don't allow signals to
2415 * abort the operation. */
2416 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002417 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002418
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002419local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002420 /*
2421 * We only see this flag if we're being called from
2422 * ocfs2_read_locked_inode(). It means we're locking an inode
2423 * which hasn't been populated yet, so clear the refresh flag
2424 * and let the caller handle it.
2425 */
2426 if (inode->i_state & I_NEW) {
2427 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002428 if (lockres)
2429 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002430 goto bail;
2431 }
2432
Mark Fashehccd979b2005-12-15 14:31:24 -08002433 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002434 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002435 * may or may not read one, depending on what's in the
2436 * LVB. The result of all of this is that we've *only* gone to
2437 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002438 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002439 if (status < 0) {
2440 if (status != -ENOENT)
2441 mlog_errno(status);
2442 goto bail;
2443 }
2444
2445 if (ret_bh) {
2446 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2447 if (status < 0) {
2448 mlog_errno(status);
2449 goto bail;
2450 }
2451 }
2452
Mark Fashehccd979b2005-12-15 14:31:24 -08002453bail:
2454 if (status < 0) {
2455 if (ret_bh && (*ret_bh)) {
2456 brelse(*ret_bh);
2457 *ret_bh = NULL;
2458 }
2459 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002460 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002461 }
2462
2463 if (local_bh)
2464 brelse(local_bh);
2465
2466 mlog_exit(status);
2467 return status;
2468}
2469
2470/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002471 * This is working around a lock inversion between tasks acquiring DLM
2472 * locks while holding a page lock and the downconvert thread which
2473 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002474 *
2475 * ** These _with_page variantes are only intended to be called from aop
2476 * methods that hold page locks and return a very specific *positive* error
2477 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2478 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002479 * The DLM is called such that it returns -EAGAIN if it would have
2480 * blocked waiting for the downconvert thread. In that case we unlock
2481 * our page so the downconvert thread can make progress. Once we've
2482 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2483 * that called us can bubble that back up into the VFS who will then
2484 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002485 *
2486 * We do a blocking lock and immediate unlock before returning, though, so that
2487 * the lock has a great chance of being cached on this node by the time the VFS
2488 * calls back to retry the aop. This has a potential to livelock as nodes
2489 * ping locks back and forth, but that's a risk we're willing to take to avoid
2490 * the lock inversion simply.
2491 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002492int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002493 struct buffer_head **ret_bh,
2494 int ex,
2495 struct page *page)
2496{
2497 int ret;
2498
Mark Fashehe63aecb62007-10-18 15:30:42 -07002499 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002500 if (ret == -EAGAIN) {
2501 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002502 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2503 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002504 ret = AOP_TRUNCATED_PAGE;
2505 }
2506
2507 return ret;
2508}
2509
Mark Fashehe63aecb62007-10-18 15:30:42 -07002510int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002511 struct vfsmount *vfsmnt,
2512 int *level)
2513{
2514 int ret;
2515
2516 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002517 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002518 if (ret < 0) {
2519 mlog_errno(ret);
2520 return ret;
2521 }
2522
2523 /*
2524 * If we should update atime, we will get EX lock,
2525 * otherwise we just get PR lock.
2526 */
2527 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2528 struct buffer_head *bh = NULL;
2529
Mark Fashehe63aecb62007-10-18 15:30:42 -07002530 ocfs2_inode_unlock(inode, 0);
2531 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002532 if (ret < 0) {
2533 mlog_errno(ret);
2534 return ret;
2535 }
2536 *level = 1;
2537 if (ocfs2_should_update_atime(inode, vfsmnt))
2538 ocfs2_update_inode_atime(inode, bh);
2539 if (bh)
2540 brelse(bh);
2541 } else
2542 *level = 0;
2543
2544 mlog_exit(ret);
2545 return ret;
2546}
2547
Mark Fashehe63aecb62007-10-18 15:30:42 -07002548void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002549 int ex)
2550{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002551 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002552 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002553 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002554
2555 mlog_entry_void();
2556
Mark Fashehb06970532006-03-03 10:24:33 -08002557 mlog(0, "inode %llu drop %s META lock\n",
2558 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002559 ex ? "EXMODE" : "PRMODE");
2560
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002561 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2562 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002563 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2564
2565 mlog_exit_void();
2566}
2567
Sunil Mushrandf152c22009-06-22 11:40:07 -07002568int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002569{
2570 struct ocfs2_lock_res *lockres;
2571 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002572 int status = 0;
2573
Sunil Mushrandf152c22009-06-22 11:40:07 -07002574 if (ocfs2_is_hard_readonly(osb))
2575 return -EROFS;
2576
2577 if (ocfs2_mount_local(osb))
2578 return 0;
2579
Srinivas Eeda83273932009-06-03 17:02:55 -07002580 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002581 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002582 if (status < 0)
2583 return status;
2584
2585 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002586 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2587 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002588 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002589 else
2590 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2591
Srinivas Eeda83273932009-06-03 17:02:55 -07002592 return status;
2593}
2594
Sunil Mushrandf152c22009-06-22 11:40:07 -07002595void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002596{
2597 struct ocfs2_lock_res *lockres;
2598 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002599
Sunil Mushrandf152c22009-06-22 11:40:07 -07002600 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2601 lockres = &osb->osb_orphan_scan.os_lockres;
2602 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2603 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2604 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2605 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2606 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002607}
2608
Mark Fashehccd979b2005-12-15 14:31:24 -08002609int ocfs2_super_lock(struct ocfs2_super *osb,
2610 int ex)
2611{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002612 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002613 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002614 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002615
2616 mlog_entry_void();
2617
2618 if (ocfs2_is_hard_readonly(osb))
2619 return -EROFS;
2620
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002621 if (ocfs2_mount_local(osb))
2622 goto bail;
2623
Mark Fashehccd979b2005-12-15 14:31:24 -08002624 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2625 if (status < 0) {
2626 mlog_errno(status);
2627 goto bail;
2628 }
2629
2630 /* The super block lock path is really in the best position to
2631 * know when resources covered by the lock need to be
2632 * refreshed, so we do it here. Of course, making sense of
2633 * everything is up to the caller :) */
2634 status = ocfs2_should_refresh_lock_res(lockres);
2635 if (status < 0) {
2636 mlog_errno(status);
2637 goto bail;
2638 }
2639 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002640 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002641
2642 ocfs2_complete_lock_res_refresh(lockres, status);
2643
2644 if (status < 0)
2645 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002646 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002647 }
2648bail:
2649 mlog_exit(status);
2650 return status;
2651}
2652
2653void ocfs2_super_unlock(struct ocfs2_super *osb,
2654 int ex)
2655{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002656 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002657 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2658
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002659 if (!ocfs2_mount_local(osb))
2660 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002661}
2662
2663int ocfs2_rename_lock(struct ocfs2_super *osb)
2664{
2665 int status;
2666 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2667
2668 if (ocfs2_is_hard_readonly(osb))
2669 return -EROFS;
2670
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002671 if (ocfs2_mount_local(osb))
2672 return 0;
2673
Joel Beckerbd3e7612008-02-01 12:14:57 -08002674 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002675 if (status < 0)
2676 mlog_errno(status);
2677
2678 return status;
2679}
2680
2681void ocfs2_rename_unlock(struct ocfs2_super *osb)
2682{
2683 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2684
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002685 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002686 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002687}
2688
wengang wang6ca497a2009-03-06 21:29:10 +08002689int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2690{
2691 int status;
2692 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2693
2694 if (ocfs2_is_hard_readonly(osb))
2695 return -EROFS;
2696
2697 if (ocfs2_mount_local(osb))
2698 return 0;
2699
2700 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2701 0, 0);
2702 if (status < 0)
2703 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2704
2705 return status;
2706}
2707
2708void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2709{
2710 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2711
2712 if (!ocfs2_mount_local(osb))
2713 ocfs2_cluster_unlock(osb, lockres,
2714 ex ? LKM_EXMODE : LKM_PRMODE);
2715}
2716
Mark Fashehd680efe2006-09-08 14:14:34 -07002717int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2718{
2719 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002720 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002721 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2722 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2723
2724 BUG_ON(!dl);
2725
2726 if (ocfs2_is_hard_readonly(osb))
2727 return -EROFS;
2728
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002729 if (ocfs2_mount_local(osb))
2730 return 0;
2731
Mark Fashehd680efe2006-09-08 14:14:34 -07002732 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2733 if (ret < 0)
2734 mlog_errno(ret);
2735
2736 return ret;
2737}
2738
2739void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2740{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002741 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002742 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2743 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2744
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002745 if (!ocfs2_mount_local(osb))
2746 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002747}
2748
Mark Fashehccd979b2005-12-15 14:31:24 -08002749/* Reference counting of the dlm debug structure. We want this because
2750 * open references on the debug inodes can live on after a mount, so
2751 * we can't rely on the ocfs2_super to always exist. */
2752static void ocfs2_dlm_debug_free(struct kref *kref)
2753{
2754 struct ocfs2_dlm_debug *dlm_debug;
2755
2756 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2757
2758 kfree(dlm_debug);
2759}
2760
2761void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2762{
2763 if (dlm_debug)
2764 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2765}
2766
2767static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2768{
2769 kref_get(&debug->d_refcnt);
2770}
2771
2772struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2773{
2774 struct ocfs2_dlm_debug *dlm_debug;
2775
2776 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2777 if (!dlm_debug) {
2778 mlog_errno(-ENOMEM);
2779 goto out;
2780 }
2781
2782 kref_init(&dlm_debug->d_refcnt);
2783 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2784 dlm_debug->d_locking_state = NULL;
2785out:
2786 return dlm_debug;
2787}
2788
2789/* Access to this is arbitrated for us via seq_file->sem. */
2790struct ocfs2_dlm_seq_priv {
2791 struct ocfs2_dlm_debug *p_dlm_debug;
2792 struct ocfs2_lock_res p_iter_res;
2793 struct ocfs2_lock_res p_tmp_res;
2794};
2795
2796static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2797 struct ocfs2_dlm_seq_priv *priv)
2798{
2799 struct ocfs2_lock_res *iter, *ret = NULL;
2800 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2801
2802 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2803
2804 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2805 /* discover the head of the list */
2806 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2807 mlog(0, "End of list found, %p\n", ret);
2808 break;
2809 }
2810
2811 /* We track our "dummy" iteration lockres' by a NULL
2812 * l_ops field. */
2813 if (iter->l_ops != NULL) {
2814 ret = iter;
2815 break;
2816 }
2817 }
2818
2819 return ret;
2820}
2821
2822static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2823{
2824 struct ocfs2_dlm_seq_priv *priv = m->private;
2825 struct ocfs2_lock_res *iter;
2826
2827 spin_lock(&ocfs2_dlm_tracking_lock);
2828 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2829 if (iter) {
2830 /* Since lockres' have the lifetime of their container
2831 * (which can be inodes, ocfs2_supers, etc) we want to
2832 * copy this out to a temporary lockres while still
2833 * under the spinlock. Obviously after this we can't
2834 * trust any pointers on the copy returned, but that's
2835 * ok as the information we want isn't typically held
2836 * in them. */
2837 priv->p_tmp_res = *iter;
2838 iter = &priv->p_tmp_res;
2839 }
2840 spin_unlock(&ocfs2_dlm_tracking_lock);
2841
2842 return iter;
2843}
2844
2845static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2846{
2847}
2848
2849static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2850{
2851 struct ocfs2_dlm_seq_priv *priv = m->private;
2852 struct ocfs2_lock_res *iter = v;
2853 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2854
2855 spin_lock(&ocfs2_dlm_tracking_lock);
2856 iter = ocfs2_dlm_next_res(iter, priv);
2857 list_del_init(&dummy->l_debug_list);
2858 if (iter) {
2859 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2860 priv->p_tmp_res = *iter;
2861 iter = &priv->p_tmp_res;
2862 }
2863 spin_unlock(&ocfs2_dlm_tracking_lock);
2864
2865 return iter;
2866}
2867
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002868/*
2869 * Version is used by debugfs.ocfs2 to determine the format being used
2870 *
2871 * New in version 2
2872 * - Lock stats printed
2873 * New in version 3
2874 * - Max time in lock stats is in usecs (instead of nsecs)
2875 */
2876#define OCFS2_DLM_DEBUG_STR_VERSION 3
Mark Fashehccd979b2005-12-15 14:31:24 -08002877static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2878{
2879 int i;
2880 char *lvb;
2881 struct ocfs2_lock_res *lockres = v;
2882
2883 if (!lockres)
2884 return -EINVAL;
2885
Mark Fashehd680efe2006-09-08 14:14:34 -07002886 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2887
2888 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2889 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2890 lockres->l_name,
2891 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2892 else
2893 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2894
2895 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002896 "0x%lx\t"
2897 "0x%x\t"
2898 "0x%x\t"
2899 "%u\t"
2900 "%u\t"
2901 "%d\t"
2902 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002903 lockres->l_level,
2904 lockres->l_flags,
2905 lockres->l_action,
2906 lockres->l_unlock_action,
2907 lockres->l_ro_holders,
2908 lockres->l_ex_holders,
2909 lockres->l_requested,
2910 lockres->l_blocking);
2911
2912 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002913 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002914 for(i = 0; i < DLM_LVB_LEN; i++)
2915 seq_printf(m, "0x%x\t", lvb[i]);
2916
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002917#ifdef CONFIG_OCFS2_FS_STATS
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002918# define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
2919# define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
2920# define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
2921# define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
2922# define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
2923# define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
2924# define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
2925# define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
2926# define lock_refresh(_l) ((_l)->l_lock_refresh)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002927#else
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002928# define lock_num_prmode(_l) (0)
2929# define lock_num_exmode(_l) (0)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002930# define lock_num_prmode_failed(_l) (0)
2931# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002932# define lock_total_prmode(_l) (0ULL)
2933# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002934# define lock_max_prmode(_l) (0)
2935# define lock_max_exmode(_l) (0)
2936# define lock_refresh(_l) (0)
2937#endif
2938 /* The following seq_print was added in version 2 of this output */
Sunil Mushran5bc970e2010-12-28 23:26:03 -08002939 seq_printf(m, "%u\t"
2940 "%u\t"
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002941 "%u\t"
2942 "%u\t"
2943 "%llu\t"
2944 "%llu\t"
2945 "%u\t"
2946 "%u\t"
2947 "%u\t",
2948 lock_num_prmode(lockres),
2949 lock_num_exmode(lockres),
2950 lock_num_prmode_failed(lockres),
2951 lock_num_exmode_failed(lockres),
2952 lock_total_prmode(lockres),
2953 lock_total_exmode(lockres),
2954 lock_max_prmode(lockres),
2955 lock_max_exmode(lockres),
2956 lock_refresh(lockres));
2957
Mark Fashehccd979b2005-12-15 14:31:24 -08002958 /* End the line */
2959 seq_printf(m, "\n");
2960 return 0;
2961}
2962
Jan Engelhardt90d99772008-01-22 20:52:20 +01002963static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002964 .start = ocfs2_dlm_seq_start,
2965 .stop = ocfs2_dlm_seq_stop,
2966 .next = ocfs2_dlm_seq_next,
2967 .show = ocfs2_dlm_seq_show,
2968};
2969
2970static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2971{
Joe Perches33fa1d92010-07-12 13:50:19 -07002972 struct seq_file *seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002973 struct ocfs2_dlm_seq_priv *priv = seq->private;
2974 struct ocfs2_lock_res *res = &priv->p_iter_res;
2975
2976 ocfs2_remove_lockres_tracking(res);
2977 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2978 return seq_release_private(inode, file);
2979}
2980
2981static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2982{
2983 int ret;
2984 struct ocfs2_dlm_seq_priv *priv;
2985 struct seq_file *seq;
2986 struct ocfs2_super *osb;
2987
2988 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2989 if (!priv) {
2990 ret = -ENOMEM;
2991 mlog_errno(ret);
2992 goto out;
2993 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002994 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002995 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2996 priv->p_dlm_debug = osb->osb_dlm_debug;
2997 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2998
2999 ret = seq_open(file, &ocfs2_dlm_seq_ops);
3000 if (ret) {
3001 kfree(priv);
3002 mlog_errno(ret);
3003 goto out;
3004 }
3005
Joe Perches33fa1d92010-07-12 13:50:19 -07003006 seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08003007 seq->private = priv;
3008
3009 ocfs2_add_lockres_tracking(&priv->p_iter_res,
3010 priv->p_dlm_debug);
3011
3012out:
3013 return ret;
3014}
3015
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08003016static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08003017 .open = ocfs2_dlm_debug_open,
3018 .release = ocfs2_dlm_debug_release,
3019 .read = seq_read,
3020 .llseek = seq_lseek,
3021};
3022
3023static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
3024{
3025 int ret = 0;
3026 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3027
3028 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
3029 S_IFREG|S_IRUSR,
3030 osb->osb_debug_root,
3031 osb,
3032 &ocfs2_dlm_debug_fops);
3033 if (!dlm_debug->d_locking_state) {
3034 ret = -EINVAL;
3035 mlog(ML_ERROR,
3036 "Unable to create locking state debugfs file.\n");
3037 goto out;
3038 }
3039
3040 ocfs2_get_dlm_debug(dlm_debug);
3041out:
3042 return ret;
3043}
3044
3045static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
3046{
3047 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3048
3049 if (dlm_debug) {
3050 debugfs_remove(dlm_debug->d_locking_state);
3051 ocfs2_put_dlm_debug(dlm_debug);
3052 }
3053}
3054
3055int ocfs2_dlm_init(struct ocfs2_super *osb)
3056{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003057 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08003058 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003059
3060 mlog_entry_void();
3061
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003062 if (ocfs2_mount_local(osb)) {
3063 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003064 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003065 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003066
Mark Fashehccd979b2005-12-15 14:31:24 -08003067 status = ocfs2_dlm_init_debug(osb);
3068 if (status < 0) {
3069 mlog_errno(status);
3070 goto bail;
3071 }
3072
Mark Fasheh34d024f2007-09-24 15:56:19 -07003073 /* launch downconvert thread */
3074 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
3075 if (IS_ERR(osb->dc_task)) {
3076 status = PTR_ERR(osb->dc_task);
3077 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003078 mlog_errno(status);
3079 goto bail;
3080 }
3081
Mark Fashehccd979b2005-12-15 14:31:24 -08003082 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08003083 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
3084 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08003085 strlen(osb->uuid_str),
Joel Becker553b5eb2010-01-29 17:19:06 -08003086 &lproto, ocfs2_do_node_down, osb,
Joel Becker4670c462008-02-01 14:39:35 -08003087 &conn);
3088 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003089 mlog_errno(status);
3090 goto bail;
3091 }
3092
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003093 status = ocfs2_cluster_this_node(&osb->node_num);
3094 if (status < 0) {
3095 mlog_errno(status);
3096 mlog(ML_ERROR,
3097 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08003098 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003099 goto bail;
3100 }
3101
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003102local:
Mark Fashehccd979b2005-12-15 14:31:24 -08003103 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3104 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08003105 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07003106 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003107
Joel Becker4670c462008-02-01 14:39:35 -08003108 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08003109
3110 status = 0;
3111bail:
3112 if (status < 0) {
3113 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003114 if (osb->dc_task)
3115 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08003116 }
3117
3118 mlog_exit(status);
3119 return status;
3120}
3121
Joel Becker286eaa92008-02-01 15:03:57 -08003122void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3123 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08003124{
3125 mlog_entry_void();
3126
Mark Fashehccd979b2005-12-15 14:31:24 -08003127 ocfs2_drop_osb_locks(osb);
3128
Joel Becker4670c462008-02-01 14:39:35 -08003129 /*
3130 * Now that we have dropped all locks and ocfs2_dismount_volume()
3131 * has disabled recovery, the DLM won't be talking to us. It's
3132 * safe to tear things down before disconnecting the cluster.
3133 */
3134
Mark Fasheh34d024f2007-09-24 15:56:19 -07003135 if (osb->dc_task) {
3136 kthread_stop(osb->dc_task);
3137 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003138 }
3139
3140 ocfs2_lock_res_free(&osb->osb_super_lockres);
3141 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003142 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003143 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003144
Joel Becker286eaa92008-02-01 15:03:57 -08003145 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003146 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003147
3148 ocfs2_dlm_shutdown_debug(osb);
3149
3150 mlog_exit_void();
3151}
3152
Mark Fashehccd979b2005-12-15 14:31:24 -08003153static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003154 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003155{
Joel Becker7431cd72008-02-01 12:15:37 -08003156 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003157 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003158 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003159
3160 /* We didn't get anywhere near actually using this lockres. */
3161 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3162 goto out;
3163
Mark Fashehb80fc012006-09-12 22:08:14 -07003164 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003165 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003166
Mark Fashehccd979b2005-12-15 14:31:24 -08003167 spin_lock_irqsave(&lockres->l_lock, flags);
3168
3169 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3170 "lockres %s, flags 0x%lx\n",
3171 lockres->l_name, lockres->l_flags);
3172
3173 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3174 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3175 "%u, unlock_action = %u\n",
3176 lockres->l_name, lockres->l_flags, lockres->l_action,
3177 lockres->l_unlock_action);
3178
3179 spin_unlock_irqrestore(&lockres->l_lock, flags);
3180
3181 /* XXX: Today we just wait on any busy
3182 * locks... Perhaps we need to cancel converts in the
3183 * future? */
3184 ocfs2_wait_on_busy_lock(lockres);
3185
3186 spin_lock_irqsave(&lockres->l_lock, flags);
3187 }
3188
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003189 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3190 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003191 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003192 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3193 lockres->l_ops->set_lvb(lockres);
3194 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003195
3196 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3197 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3198 lockres->l_name);
3199 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3200 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3201
3202 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3203 spin_unlock_irqrestore(&lockres->l_lock, flags);
3204 goto out;
3205 }
3206
3207 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3208
3209 /* make sure we never get here while waiting for an ast to
3210 * fire. */
3211 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3212
3213 /* is this necessary? */
3214 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3215 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3216 spin_unlock_irqrestore(&lockres->l_lock, flags);
3217
3218 mlog(0, "lock %s\n", lockres->l_name);
3219
Joel Beckera796d282010-01-28 19:22:39 -08003220 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
Joel Becker7431cd72008-02-01 12:15:37 -08003221 if (ret) {
3222 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003223 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003224 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003225 BUG();
3226 }
Coly Li73ac36e2009-01-07 18:09:16 -08003227 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003228 lockres->l_name);
3229
3230 ocfs2_wait_on_busy_lock(lockres);
3231out:
3232 mlog_exit(0);
3233 return 0;
3234}
3235
3236/* Mark the lockres as being dropped. It will no longer be
3237 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003238 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003239 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003240 *
3241 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3242void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3243{
3244 int status;
3245 struct ocfs2_mask_waiter mw;
3246 unsigned long flags;
3247
3248 ocfs2_init_mask_waiter(&mw);
3249
3250 spin_lock_irqsave(&lockres->l_lock, flags);
3251 lockres->l_flags |= OCFS2_LOCK_FREEING;
3252 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3253 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3254 spin_unlock_irqrestore(&lockres->l_lock, flags);
3255
3256 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3257
3258 status = ocfs2_wait_for_mask(&mw);
3259 if (status)
3260 mlog_errno(status);
3261
3262 spin_lock_irqsave(&lockres->l_lock, flags);
3263 }
3264 spin_unlock_irqrestore(&lockres->l_lock, flags);
3265}
3266
Mark Fashehd680efe2006-09-08 14:14:34 -07003267void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3268 struct ocfs2_lock_res *lockres)
3269{
3270 int ret;
3271
3272 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003273 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003274 if (ret)
3275 mlog_errno(ret);
3276}
3277
Mark Fashehccd979b2005-12-15 14:31:24 -08003278static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3279{
Mark Fashehd680efe2006-09-08 14:14:34 -07003280 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3281 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003282 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003283 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003284}
3285
Mark Fashehccd979b2005-12-15 14:31:24 -08003286int ocfs2_drop_inode_locks(struct inode *inode)
3287{
3288 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003289
3290 mlog_entry_void();
3291
3292 /* No need to call ocfs2_mark_lockres_freeing here -
3293 * ocfs2_clear_inode has done it for us. */
3294
3295 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003296 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003297 if (err < 0)
3298 mlog_errno(err);
3299
3300 status = err;
3301
3302 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003303 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003304 if (err < 0)
3305 mlog_errno(err);
3306 if (err < 0 && !status)
3307 status = err;
3308
3309 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003310 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003311 if (err < 0)
3312 mlog_errno(err);
3313 if (err < 0 && !status)
3314 status = err;
3315
3316 mlog_exit(status);
3317 return status;
3318}
3319
Joel Beckerde551242008-02-01 14:45:08 -08003320static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3321 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003322{
3323 assert_spin_locked(&lockres->l_lock);
3324
Joel Beckerbd3e7612008-02-01 12:14:57 -08003325 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003326
3327 if (lockres->l_level <= new_level) {
Sunil Mushran9b915182010-02-26 19:42:44 -08003328 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3329 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3330 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3331 new_level, list_empty(&lockres->l_blocked_list),
3332 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3333 lockres->l_flags, lockres->l_ro_holders,
3334 lockres->l_ex_holders, lockres->l_action,
3335 lockres->l_unlock_action, lockres->l_requested,
3336 lockres->l_blocking, lockres->l_pending_gen);
Mark Fashehccd979b2005-12-15 14:31:24 -08003337 BUG();
3338 }
3339
Sunil Mushran9b915182010-02-26 19:42:44 -08003340 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3341 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
Mark Fashehccd979b2005-12-15 14:31:24 -08003342
3343 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3344 lockres->l_requested = new_level;
3345 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003346 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003347}
3348
3349static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3350 struct ocfs2_lock_res *lockres,
3351 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003352 int lvb,
3353 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003354{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003355 int ret;
3356 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003357
3358 mlog_entry_void();
3359
Sunil Mushran9b915182010-02-26 19:42:44 -08003360 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3361 lockres->l_level, new_level);
3362
Mark Fashehccd979b2005-12-15 14:31:24 -08003363 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003364 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003365
Joel Becker4670c462008-02-01 14:39:35 -08003366 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003367 new_level,
3368 &lockres->l_lksb,
3369 dlm_flags,
3370 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08003371 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08003372 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003373 if (ret) {
3374 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003375 ocfs2_recover_from_dlm_error(lockres, 1);
3376 goto bail;
3377 }
3378
3379 ret = 0;
3380bail:
3381 mlog_exit(ret);
3382 return ret;
3383}
3384
Joel Becker24ef1812008-01-29 17:37:32 -08003385/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003386static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3387 struct ocfs2_lock_res *lockres)
3388{
3389 assert_spin_locked(&lockres->l_lock);
3390
3391 mlog_entry_void();
Mark Fashehccd979b2005-12-15 14:31:24 -08003392
3393 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3394 /* If we're already trying to cancel a lock conversion
3395 * then just drop the spinlock and allow the caller to
3396 * requeue this lock. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003397 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003398 return 0;
3399 }
3400
3401 /* were we in a convert when we got the bast fire? */
3402 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3403 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3404 /* set things up for the unlockast to know to just
3405 * clear out the ast_action and unset busy, etc. */
3406 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3407
3408 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3409 "lock %s, invalid flags: 0x%lx\n",
3410 lockres->l_name, lockres->l_flags);
3411
Sunil Mushran9b915182010-02-26 19:42:44 -08003412 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3413
Mark Fashehccd979b2005-12-15 14:31:24 -08003414 return 1;
3415}
3416
3417static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3418 struct ocfs2_lock_res *lockres)
3419{
3420 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003421
3422 mlog_entry_void();
Mark Fashehccd979b2005-12-15 14:31:24 -08003423
Joel Becker4670c462008-02-01 14:39:35 -08003424 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Beckera796d282010-01-28 19:22:39 -08003425 DLM_LKF_CANCEL);
Joel Becker7431cd72008-02-01 12:15:37 -08003426 if (ret) {
3427 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003428 ocfs2_recover_from_dlm_error(lockres, 0);
3429 }
3430
Sunil Mushran9b915182010-02-26 19:42:44 -08003431 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003432
3433 mlog_exit(ret);
3434 return ret;
3435}
3436
Mark Fashehb5e500e2006-09-13 22:01:16 -07003437static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3438 struct ocfs2_lock_res *lockres,
3439 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003440{
3441 unsigned long flags;
3442 int blocking;
3443 int new_level;
Sunil Mushran079b8052010-02-03 10:16:54 -08003444 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003445 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003446 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003447 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003448
3449 mlog_entry_void();
3450
3451 spin_lock_irqsave(&lockres->l_lock, flags);
3452
Mark Fashehccd979b2005-12-15 14:31:24 -08003453recheck:
Sunil Mushrandb0f6ce2010-02-01 16:55:50 -08003454 /*
3455 * Is it still blocking? If not, we have no more work to do.
3456 */
3457 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3458 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3459 spin_unlock_irqrestore(&lockres->l_lock, flags);
3460 ret = 0;
3461 goto leave;
3462 }
3463
Mark Fashehccd979b2005-12-15 14:31:24 -08003464 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003465 /* XXX
3466 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3467 * exists entirely for one reason - another thread has set
3468 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3469 *
3470 * If we do ocfs2_cancel_convert() before the other thread
3471 * calls dlm_lock(), our cancel will do nothing. We will
3472 * get no ast, and we will have no way of knowing the
3473 * cancel failed. Meanwhile, the other thread will call
3474 * into dlm_lock() and wait...forever.
3475 *
3476 * Why forever? Because another node has asked for the
3477 * lock first; that's why we're here in unblock_lock().
3478 *
3479 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3480 * set, we just requeue the unblock. Only when the other
3481 * thread has called dlm_lock() and cleared PENDING will
3482 * we then cancel their request.
3483 *
3484 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3485 * at the same time they set OCFS2_DLM_BUSY. They must
3486 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3487 */
Sunil Mushran9b915182010-02-26 19:42:44 -08003488 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3489 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3490 lockres->l_name);
Joel Beckerde551242008-02-01 14:45:08 -08003491 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003492 }
Joel Beckerde551242008-02-01 14:45:08 -08003493
Mark Fashehd680efe2006-09-08 14:14:34 -07003494 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003495 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3496 spin_unlock_irqrestore(&lockres->l_lock, flags);
3497 if (ret) {
3498 ret = ocfs2_cancel_convert(osb, lockres);
3499 if (ret < 0)
3500 mlog_errno(ret);
3501 }
3502 goto leave;
3503 }
3504
Sunil Mushrana1912822010-01-21 10:50:03 -08003505 /*
3506 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3507 * set when the ast is received for an upconvert just before the
3508 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3509 * on the heels of the ast, we want to delay the downconvert just
3510 * enough to allow the up requestor to do its task. Because this
3511 * lock is in the blocked queue, the lock will be downconverted
3512 * as soon as the requestor is done with the lock.
3513 */
3514 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3515 goto leave_requeue;
3516
Sunil Mushran0d741252010-01-29 09:44:11 -08003517 /*
3518 * How can we block and yet be at NL? We were trying to upconvert
3519 * from NL and got canceled. The code comes back here, and now
3520 * we notice and clear BLOCKING.
3521 */
3522 if (lockres->l_level == DLM_LOCK_NL) {
3523 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
Sunil Mushran9b915182010-02-26 19:42:44 -08003524 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
Sunil Mushran0d741252010-01-29 09:44:11 -08003525 lockres->l_blocking = DLM_LOCK_NL;
3526 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3527 spin_unlock_irqrestore(&lockres->l_lock, flags);
3528 goto leave;
3529 }
3530
Mark Fashehccd979b2005-12-15 14:31:24 -08003531 /* if we're blocking an exclusive and we have *any* holders,
3532 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003533 if ((lockres->l_blocking == DLM_LOCK_EX)
Sunil Mushran9b915182010-02-26 19:42:44 -08003534 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3535 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3536 lockres->l_name, lockres->l_ex_holders,
3537 lockres->l_ro_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003538 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003539 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003540
3541 /* If it's a PR we're blocking, then only
3542 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003543 if (lockres->l_blocking == DLM_LOCK_PR &&
Sunil Mushran9b915182010-02-26 19:42:44 -08003544 lockres->l_ex_holders) {
3545 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3546 lockres->l_name, lockres->l_ex_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003547 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003548 }
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003549
3550 /*
3551 * Can we get a lock in this state if the holder counts are
3552 * zero? The meta data unblock code used to check this.
3553 */
3554 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Sunil Mushran9b915182010-02-26 19:42:44 -08003555 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3556 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3557 lockres->l_name);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003558 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003559 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003560
Mark Fasheh16d5b952006-09-13 21:10:12 -07003561 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3562
3563 if (lockres->l_ops->check_downconvert
Sunil Mushran9b915182010-02-26 19:42:44 -08003564 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3565 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3566 lockres->l_name);
Mark Fasheh16d5b952006-09-13 21:10:12 -07003567 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003568 }
Mark Fasheh16d5b952006-09-13 21:10:12 -07003569
Mark Fashehccd979b2005-12-15 14:31:24 -08003570 /* If we get here, then we know that there are no more
3571 * incompatible holders (and anyone asking for an incompatible
3572 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003573 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003574 goto downconvert;
3575
3576 /* Some lockres types want to do a bit of work before
3577 * downconverting a lock. Allow that here. The worker function
3578 * may sleep, so we save off a copy of what we're blocking as
3579 * it may change while we're not holding the spin lock. */
3580 blocking = lockres->l_blocking;
Sunil Mushran079b8052010-02-03 10:16:54 -08003581 level = lockres->l_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003582 spin_unlock_irqrestore(&lockres->l_lock, flags);
3583
Mark Fashehcc567d82006-09-13 21:52:21 -07003584 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003585
Sunil Mushran9b915182010-02-26 19:42:44 -08003586 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3587 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3588 lockres->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -07003589 goto leave;
Sunil Mushran9b915182010-02-26 19:42:44 -08003590 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003591
3592 spin_lock_irqsave(&lockres->l_lock, flags);
Sunil Mushran079b8052010-02-03 10:16:54 -08003593 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003594 /* If this changed underneath us, then we can't drop
3595 * it just yet. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003596 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3597 "Recheck\n", lockres->l_name, blocking,
3598 lockres->l_blocking, level, lockres->l_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003599 goto recheck;
3600 }
3601
3602downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003603 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003604
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003605 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003606 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003607 set_lvb = 1;
3608
3609 /*
3610 * We only set the lvb if the lock has been fully
3611 * refreshed - otherwise we risk setting stale
3612 * data. Otherwise, there's no need to actually clear
3613 * out the lvb here as it's value is still valid.
3614 */
3615 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3616 lockres->l_ops->set_lvb(lockres);
3617 }
3618
Joel Beckerde551242008-02-01 14:45:08 -08003619 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003620 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003621 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3622 gen);
3623
Mark Fashehccd979b2005-12-15 14:31:24 -08003624leave:
3625 mlog_exit(ret);
3626 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003627
3628leave_requeue:
3629 spin_unlock_irqrestore(&lockres->l_lock, flags);
3630 ctl->requeue = 1;
3631
3632 mlog_exit(0);
3633 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003634}
3635
Mark Fashehd680efe2006-09-08 14:14:34 -07003636static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3637 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003638{
3639 struct inode *inode;
3640 struct address_space *mapping;
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003641 struct ocfs2_inode_info *oi;
Mark Fashehccd979b2005-12-15 14:31:24 -08003642
Mark Fashehccd979b2005-12-15 14:31:24 -08003643 inode = ocfs2_lock_res_inode(lockres);
3644 mapping = inode->i_mapping;
3645
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003646 if (S_ISDIR(inode->i_mode)) {
3647 oi = OCFS2_I(inode);
3648 oi->ip_dir_lock_gen++;
3649 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3650 goto out;
3651 }
3652
Mark Fasheh1044e402008-02-28 17:16:03 -08003653 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003654 goto out;
3655
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003656 /*
3657 * We need this before the filemap_fdatawrite() so that it can
3658 * transfer the dirty bit from the PTE to the
3659 * page. Unfortunately this means that even for EX->PR
3660 * downconverts, we'll lose our mappings and have to build
3661 * them up again.
3662 */
3663 unmap_mapping_range(mapping, 0, 0, 0);
3664
Mark Fashehccd979b2005-12-15 14:31:24 -08003665 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003666 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3667 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003668 }
3669 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003670 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003671 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003672 } else {
3673 /* We only need to wait on the I/O if we're not also
3674 * truncating pages because truncate_inode_pages waits
3675 * for us above. We don't truncate pages if we're
3676 * blocking anything < EXMODE because we want to keep
3677 * them around in that case. */
3678 filemap_fdatawait(mapping);
3679 }
3680
Mark Fashehf1f54062007-10-18 15:13:59 -07003681out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003682 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003683}
3684
Tao Maa4338482009-08-18 11:19:29 +08003685static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3686 struct ocfs2_lock_res *lockres,
3687 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003688{
Tao Maa4338482009-08-18 11:19:29 +08003689 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003690
Joel Beckerbd3e7612008-02-01 12:14:57 -08003691 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3692 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003693
3694 if (checkpointed)
3695 return 1;
3696
Tao Maa4338482009-08-18 11:19:29 +08003697 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003698 return 0;
3699}
3700
Tao Maa4338482009-08-18 11:19:29 +08003701static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3702 int new_level)
3703{
3704 struct inode *inode = ocfs2_lock_res_inode(lockres);
3705
3706 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3707}
3708
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003709static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3710{
3711 struct inode *inode = ocfs2_lock_res_inode(lockres);
3712
3713 __ocfs2_stuff_meta_lvb(inode);
3714}
3715
Mark Fashehd680efe2006-09-08 14:14:34 -07003716/*
3717 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003718 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003719 * dlmglue API and push these off to the ocfs2_wq in the future.
3720 */
3721static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3722 struct ocfs2_lock_res *lockres)
3723{
3724 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3725 ocfs2_dentry_lock_put(osb, dl);
3726}
3727
3728/*
3729 * d_delete() matching dentries before the lock downconvert.
3730 *
3731 * At this point, any process waiting to destroy the
3732 * dentry_lock due to last ref count is stopped by the
3733 * OCFS2_LOCK_QUEUED flag.
3734 *
3735 * We have two potential problems
3736 *
3737 * 1) If we do the last reference drop on our dentry_lock (via dput)
3738 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3739 * the downconvert to finish. Instead we take an elevated
3740 * reference and push the drop until after we've completed our
3741 * unblock processing.
3742 *
3743 * 2) There might be another process with a final reference,
3744 * waiting on us to finish processing. If this is the case, we
3745 * detect it and exit out - there's no more dentries anyway.
3746 */
3747static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3748 int blocking)
3749{
3750 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3751 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3752 struct dentry *dentry;
3753 unsigned long flags;
3754 int extra_ref = 0;
3755
3756 /*
3757 * This node is blocking another node from getting a read
3758 * lock. This happens when we've renamed within a
3759 * directory. We've forced the other nodes to d_delete(), but
3760 * we never actually dropped our lock because it's still
3761 * valid. The downconvert code will retain a PR for this node,
3762 * so there's no further work to do.
3763 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003764 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003765 return UNBLOCK_CONTINUE;
3766
3767 /*
3768 * Mark this inode as potentially orphaned. The code in
3769 * ocfs2_delete_inode() will figure out whether it actually
3770 * needs to be freed or not.
3771 */
3772 spin_lock(&oi->ip_lock);
3773 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3774 spin_unlock(&oi->ip_lock);
3775
3776 /*
3777 * Yuck. We need to make sure however that the check of
3778 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3779 * respect to a reference decrement or the setting of that
3780 * flag.
3781 */
3782 spin_lock_irqsave(&lockres->l_lock, flags);
3783 spin_lock(&dentry_attach_lock);
3784 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3785 && dl->dl_count) {
3786 dl->dl_count++;
3787 extra_ref = 1;
3788 }
3789 spin_unlock(&dentry_attach_lock);
3790 spin_unlock_irqrestore(&lockres->l_lock, flags);
3791
3792 mlog(0, "extra_ref = %d\n", extra_ref);
3793
3794 /*
3795 * We have a process waiting on us in ocfs2_dentry_iput(),
3796 * which means we can't have any more outstanding
3797 * aliases. There's no need to do any more work.
3798 */
3799 if (!extra_ref)
3800 return UNBLOCK_CONTINUE;
3801
3802 spin_lock(&dentry_attach_lock);
3803 while (1) {
3804 dentry = ocfs2_find_local_alias(dl->dl_inode,
3805 dl->dl_parent_blkno, 1);
3806 if (!dentry)
3807 break;
3808 spin_unlock(&dentry_attach_lock);
3809
3810 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3811 dentry->d_name.name);
3812
3813 /*
3814 * The following dcache calls may do an
3815 * iput(). Normally we don't want that from the
3816 * downconverting thread, but in this case it's ok
3817 * because the requesting node already has an
3818 * exclusive lock on the inode, so it can't be queued
3819 * for a downconvert.
3820 */
3821 d_delete(dentry);
3822 dput(dentry);
3823
3824 spin_lock(&dentry_attach_lock);
3825 }
3826 spin_unlock(&dentry_attach_lock);
3827
3828 /*
3829 * If we are the last holder of this dentry lock, there is no
3830 * reason to downconvert so skip straight to the unlock.
3831 */
3832 if (dl->dl_count == 1)
3833 return UNBLOCK_STOP_POST;
3834
3835 return UNBLOCK_CONTINUE_POST;
3836}
3837
Tao Ma8dec98e2009-08-18 11:19:58 +08003838static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3839 int new_level)
3840{
3841 struct ocfs2_refcount_tree *tree =
3842 ocfs2_lock_res_refcount_tree(lockres);
3843
3844 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3845}
3846
3847static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3848 int blocking)
3849{
3850 struct ocfs2_refcount_tree *tree =
3851 ocfs2_lock_res_refcount_tree(lockres);
3852
3853 ocfs2_metadata_cache_purge(&tree->rf_ci);
3854
3855 return UNBLOCK_CONTINUE;
3856}
3857
Jan Kara9e33d692008-08-25 19:56:50 +02003858static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3859{
3860 struct ocfs2_qinfo_lvb *lvb;
3861 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3862 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3863 oinfo->dqi_gi.dqi_type);
3864
3865 mlog_entry_void();
3866
Mark Fasheha641dc22008-12-24 16:03:48 -08003867 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003868 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3869 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3870 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3871 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3872 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3873 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3874 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3875
3876 mlog_exit_void();
3877}
3878
3879void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3880{
3881 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3882 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3883 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3884
3885 mlog_entry_void();
3886 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3887 ocfs2_cluster_unlock(osb, lockres, level);
3888 mlog_exit_void();
3889}
3890
3891static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3892{
3893 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3894 oinfo->dqi_gi.dqi_type);
3895 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3896 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003897 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003898 struct ocfs2_global_disk_dqinfo *gdinfo;
3899 int status = 0;
3900
Joel Becker1c520df2009-06-19 15:14:13 -07003901 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3902 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003903 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3904 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3905 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3906 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3907 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3908 oinfo->dqi_gi.dqi_free_entry =
3909 be32_to_cpu(lvb->lvb_free_entry);
3910 } else {
Jan Karaae4f6ef2010-04-28 19:04:29 +02003911 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
3912 oinfo->dqi_giblk, &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +01003913 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003914 mlog_errno(status);
3915 goto bail;
3916 }
3917 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3918 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3919 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3920 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3921 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3922 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3923 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3924 oinfo->dqi_gi.dqi_free_entry =
3925 le32_to_cpu(gdinfo->dqi_free_entry);
3926 brelse(bh);
3927 ocfs2_track_lock_refresh(lockres);
3928 }
3929
3930bail:
3931 return status;
3932}
3933
3934/* Lock quota info, this function expects at least shared lock on the quota file
3935 * so that we can safely refresh quota info from disk. */
3936int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3937{
3938 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3939 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3940 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3941 int status = 0;
3942
3943 mlog_entry_void();
3944
3945 /* On RO devices, locking really isn't needed... */
3946 if (ocfs2_is_hard_readonly(osb)) {
3947 if (ex)
3948 status = -EROFS;
3949 goto bail;
3950 }
3951 if (ocfs2_mount_local(osb))
3952 goto bail;
3953
3954 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3955 if (status < 0) {
3956 mlog_errno(status);
3957 goto bail;
3958 }
3959 if (!ocfs2_should_refresh_lock_res(lockres))
3960 goto bail;
3961 /* OK, we have the lock but we need to refresh the quota info */
3962 status = ocfs2_refresh_qinfo(oinfo);
3963 if (status)
3964 ocfs2_qinfo_unlock(oinfo, ex);
3965 ocfs2_complete_lock_res_refresh(lockres, status);
3966bail:
3967 mlog_exit(status);
3968 return status;
3969}
3970
Tao Ma8dec98e2009-08-18 11:19:58 +08003971int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
3972{
3973 int status;
3974 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3975 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3976 struct ocfs2_super *osb = lockres->l_priv;
3977
3978
3979 if (ocfs2_is_hard_readonly(osb))
3980 return -EROFS;
3981
3982 if (ocfs2_mount_local(osb))
3983 return 0;
3984
3985 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3986 if (status < 0)
3987 mlog_errno(status);
3988
3989 return status;
3990}
3991
3992void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
3993{
3994 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3995 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3996 struct ocfs2_super *osb = lockres->l_priv;
3997
3998 if (!ocfs2_mount_local(osb))
3999 ocfs2_cluster_unlock(osb, lockres, level);
4000}
4001
Adrian Bunk00600052008-01-29 00:11:41 +02004002static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
4003 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08004004{
4005 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07004006 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08004007 unsigned long flags;
4008
4009 /* Our reference to the lockres in this function can be
4010 * considered valid until we remove the OCFS2_LOCK_QUEUED
4011 * flag. */
4012
4013 mlog_entry_void();
4014
4015 BUG_ON(!lockres);
4016 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08004017
Sunil Mushran9b915182010-02-26 19:42:44 -08004018 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08004019
4020 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07004021 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08004022 * still be marked with OCFS2_LOCK_FREEING after this check,
4023 * but short circuiting here will still save us some
4024 * performance. */
4025 spin_lock_irqsave(&lockres->l_lock, flags);
4026 if (lockres->l_flags & OCFS2_LOCK_FREEING)
4027 goto unqueue;
4028 spin_unlock_irqrestore(&lockres->l_lock, flags);
4029
Mark Fashehb5e500e2006-09-13 22:01:16 -07004030 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08004031 if (status < 0)
4032 mlog_errno(status);
4033
4034 spin_lock_irqsave(&lockres->l_lock, flags);
4035unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07004036 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004037 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
4038 } else
4039 ocfs2_schedule_blocked_lock(osb, lockres);
4040
Sunil Mushran9b915182010-02-26 19:42:44 -08004041 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07004042 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08004043 spin_unlock_irqrestore(&lockres->l_lock, flags);
4044
Mark Fashehd680efe2006-09-08 14:14:34 -07004045 if (ctl.unblock_action != UNBLOCK_CONTINUE
4046 && lockres->l_ops->post_unlock)
4047 lockres->l_ops->post_unlock(osb, lockres);
4048
Mark Fashehccd979b2005-12-15 14:31:24 -08004049 mlog_exit_void();
4050}
4051
4052static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4053 struct ocfs2_lock_res *lockres)
4054{
4055 mlog_entry_void();
4056
4057 assert_spin_locked(&lockres->l_lock);
4058
4059 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4060 /* Do not schedule a lock for downconvert when it's on
4061 * the way to destruction - any nodes wanting access
4062 * to the resource will get it soon. */
Sunil Mushran9b915182010-02-26 19:42:44 -08004063 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08004064 lockres->l_name, lockres->l_flags);
4065 return;
4066 }
4067
4068 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4069
Mark Fasheh34d024f2007-09-24 15:56:19 -07004070 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08004071 if (list_empty(&lockres->l_blocked_list)) {
4072 list_add_tail(&lockres->l_blocked_list,
4073 &osb->blocked_lock_list);
4074 osb->blocked_lock_count++;
4075 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07004076 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08004077
4078 mlog_exit_void();
4079}
Mark Fasheh34d024f2007-09-24 15:56:19 -07004080
4081static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4082{
4083 unsigned long processed;
4084 struct ocfs2_lock_res *lockres;
4085
4086 mlog_entry_void();
4087
4088 spin_lock(&osb->dc_task_lock);
4089 /* grab this early so we know to try again if a state change and
4090 * wake happens part-way through our work */
4091 osb->dc_work_sequence = osb->dc_wake_sequence;
4092
4093 processed = osb->blocked_lock_count;
4094 while (processed) {
4095 BUG_ON(list_empty(&osb->blocked_lock_list));
4096
4097 lockres = list_entry(osb->blocked_lock_list.next,
4098 struct ocfs2_lock_res, l_blocked_list);
4099 list_del_init(&lockres->l_blocked_list);
4100 osb->blocked_lock_count--;
4101 spin_unlock(&osb->dc_task_lock);
4102
4103 BUG_ON(!processed);
4104 processed--;
4105
4106 ocfs2_process_blocked_lock(osb, lockres);
4107
4108 spin_lock(&osb->dc_task_lock);
4109 }
4110 spin_unlock(&osb->dc_task_lock);
4111
4112 mlog_exit_void();
4113}
4114
4115static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4116{
4117 int empty = 0;
4118
4119 spin_lock(&osb->dc_task_lock);
4120 if (list_empty(&osb->blocked_lock_list))
4121 empty = 1;
4122
4123 spin_unlock(&osb->dc_task_lock);
4124 return empty;
4125}
4126
4127static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4128{
4129 int should_wake = 0;
4130
4131 spin_lock(&osb->dc_task_lock);
4132 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4133 should_wake = 1;
4134 spin_unlock(&osb->dc_task_lock);
4135
4136 return should_wake;
4137}
4138
Adrian Bunk200bfae2008-02-17 10:20:38 +02004139static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004140{
4141 int status = 0;
4142 struct ocfs2_super *osb = arg;
4143
4144 /* only quit once we've been asked to stop and there is no more
4145 * work available */
4146 while (!(kthread_should_stop() &&
4147 ocfs2_downconvert_thread_lists_empty(osb))) {
4148
4149 wait_event_interruptible(osb->dc_event,
4150 ocfs2_downconvert_thread_should_wake(osb) ||
4151 kthread_should_stop());
4152
4153 mlog(0, "downconvert_thread: awoken\n");
4154
4155 ocfs2_downconvert_thread_do_work(osb);
4156 }
4157
4158 osb->dc_task = NULL;
4159 return status;
4160}
4161
4162void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4163{
4164 spin_lock(&osb->dc_task_lock);
4165 /* make sure the voting thread gets a swipe at whatever changes
4166 * the caller may have made to the voting state */
4167 osb->dc_wake_sequence++;
4168 spin_unlock(&osb->dc_task_lock);
4169 wake_up(&osb->dc_event);
4170}