blob: 7eb4b8da5cbc542e253560f6a65923730582255c [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
67 unsigned long long mw_lock_start;
68#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
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
401
Mark Fashehb06970532006-03-03 10:24:33 -0800402 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
403 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
404 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800405
406 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
407
408 mlog(0, "built lock resource with name: %s\n", name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800409}
410
Ingo Molnar34af9462006-06-27 02:53:55 -0700411static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800412
413static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
414 struct ocfs2_dlm_debug *dlm_debug)
415{
416 mlog(0, "Add tracking for lockres %s\n", res->l_name);
417
418 spin_lock(&ocfs2_dlm_tracking_lock);
419 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
420 spin_unlock(&ocfs2_dlm_tracking_lock);
421}
422
423static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
424{
425 spin_lock(&ocfs2_dlm_tracking_lock);
426 if (!list_empty(&res->l_debug_list))
427 list_del_init(&res->l_debug_list);
428 spin_unlock(&ocfs2_dlm_tracking_lock);
429}
430
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700431#ifdef CONFIG_OCFS2_FS_STATS
432static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
433{
434 res->l_lock_num_prmode = 0;
435 res->l_lock_num_prmode_failed = 0;
436 res->l_lock_total_prmode = 0;
437 res->l_lock_max_prmode = 0;
438 res->l_lock_num_exmode = 0;
439 res->l_lock_num_exmode_failed = 0;
440 res->l_lock_total_exmode = 0;
441 res->l_lock_max_exmode = 0;
442 res->l_lock_refresh = 0;
443}
444
445static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
446 struct ocfs2_mask_waiter *mw, int ret)
447{
448 unsigned long long *num, *sum;
449 unsigned int *max, *failed;
450 struct timespec ts = current_kernel_time();
451 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
452
453 if (level == LKM_PRMODE) {
454 num = &res->l_lock_num_prmode;
455 sum = &res->l_lock_total_prmode;
456 max = &res->l_lock_max_prmode;
457 failed = &res->l_lock_num_prmode_failed;
458 } else if (level == LKM_EXMODE) {
459 num = &res->l_lock_num_exmode;
460 sum = &res->l_lock_total_exmode;
461 max = &res->l_lock_max_exmode;
462 failed = &res->l_lock_num_exmode_failed;
463 } else
464 return;
465
466 (*num)++;
467 (*sum) += time;
468 if (time > *max)
469 *max = time;
470 if (ret)
471 (*failed)++;
472}
473
474static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
475{
476 lockres->l_lock_refresh++;
477}
478
479static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
480{
481 struct timespec ts = current_kernel_time();
482 mw->mw_lock_start = timespec_to_ns(&ts);
483}
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{
Mark Fashehccd979b2005-12-15 14:31:24 -0800728 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
729 return;
730
731 ocfs2_remove_lockres_tracking(res);
732
733 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
734 "Lockres %s is on the blocked list\n",
735 res->l_name);
736 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
737 "Lockres %s has mask waiters pending\n",
738 res->l_name);
739 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
740 "Lockres %s is locked\n",
741 res->l_name);
742 mlog_bug_on_msg(res->l_ro_holders,
743 "Lockres %s has %u ro holders\n",
744 res->l_name, res->l_ro_holders);
745 mlog_bug_on_msg(res->l_ex_holders,
746 "Lockres %s has %u ex holders\n",
747 res->l_name, res->l_ex_holders);
748
749 /* Need to clear out the lock status block for the dlm */
750 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
751
752 res->l_flags = 0UL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800753}
754
755static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
756 int level)
757{
Mark Fashehccd979b2005-12-15 14:31:24 -0800758 BUG_ON(!lockres);
759
760 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800761 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800762 lockres->l_ex_holders++;
763 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800764 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800765 lockres->l_ro_holders++;
766 break;
767 default:
768 BUG();
769 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800770}
771
772static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
773 int level)
774{
Mark Fashehccd979b2005-12-15 14:31:24 -0800775 BUG_ON(!lockres);
776
777 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800778 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 BUG_ON(!lockres->l_ex_holders);
780 lockres->l_ex_holders--;
781 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800782 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 BUG_ON(!lockres->l_ro_holders);
784 lockres->l_ro_holders--;
785 break;
786 default:
787 BUG();
788 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800789}
790
791/* WARNING: This function lives in a world where the only three lock
792 * levels are EX, PR, and NL. It *will* have to be adjusted when more
793 * lock types are added. */
794static inline int ocfs2_highest_compat_lock_level(int level)
795{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800796 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800797
Joel Beckerbd3e7612008-02-01 12:14:57 -0800798 if (level == DLM_LOCK_EX)
799 new_level = DLM_LOCK_NL;
800 else if (level == DLM_LOCK_PR)
801 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 return new_level;
803}
804
805static void lockres_set_flags(struct ocfs2_lock_res *lockres,
806 unsigned long newflags)
807{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200808 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800809
810 assert_spin_locked(&lockres->l_lock);
811
812 lockres->l_flags = newflags;
813
Christoph Hellwig800deef2007-05-17 16:03:13 +0200814 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800815 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
816 continue;
817
818 list_del_init(&mw->mw_item);
819 mw->mw_status = 0;
820 complete(&mw->mw_complete);
821 }
822}
823static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
824{
825 lockres_set_flags(lockres, lockres->l_flags | or);
826}
827static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
828 unsigned long clear)
829{
830 lockres_set_flags(lockres, lockres->l_flags & ~clear);
831}
832
833static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
834{
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
836 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
837 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800838 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800839
840 lockres->l_level = lockres->l_requested;
841 if (lockres->l_level <=
842 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800843 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800844 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
845 }
846 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800847}
848
849static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
850{
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
852 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
853
854 /* Convert from RO to EX doesn't really need anything as our
855 * information is already up to data. Convert from NL to
856 * *anything* however should mark ourselves as needing an
857 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800858 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700859 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800860 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
861
862 lockres->l_level = lockres->l_requested;
Sunil Mushrana1912822010-01-21 10:50:03 -0800863
864 /*
865 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
866 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
867 * downconverting the lock before the upconvert has fully completed.
868 */
869 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
870
Mark Fashehccd979b2005-12-15 14:31:24 -0800871 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800872}
873
874static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
875{
Roel Kluin3cf0c502007-10-27 00:20:36 +0200876 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
878
Joel Beckerbd3e7612008-02-01 12:14:57 -0800879 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700880 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
881 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
883
884 lockres->l_level = lockres->l_requested;
885 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
886 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -0800887}
888
Mark Fashehccd979b2005-12-15 14:31:24 -0800889static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
890 int level)
891{
892 int needs_downconvert = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800893
894 assert_spin_locked(&lockres->l_lock);
895
Mark Fashehccd979b2005-12-15 14:31:24 -0800896 if (level > lockres->l_blocking) {
897 /* only schedule a downconvert if we haven't already scheduled
898 * one that goes low enough to satisfy the level we're
899 * blocking. this also catches the case where we get
900 * duplicate BASTs */
901 if (ocfs2_highest_compat_lock_level(level) <
902 ocfs2_highest_compat_lock_level(lockres->l_blocking))
903 needs_downconvert = 1;
904
905 lockres->l_blocking = level;
906 }
907
Sunil Mushran9b915182010-02-26 19:42:44 -0800908 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
909 lockres->l_name, level, lockres->l_level, lockres->l_blocking,
910 needs_downconvert);
911
Wengang Wang0b94a902010-01-21 10:50:02 -0800912 if (needs_downconvert)
913 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Tao Mac1e8d352011-03-07 16:43:21 +0800914 mlog(0, "needs_downconvert = %d\n", needs_downconvert);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915 return needs_downconvert;
916}
917
Joel Beckerde551242008-02-01 14:45:08 -0800918/*
919 * OCFS2_LOCK_PENDING and l_pending_gen.
920 *
921 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
922 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
923 * for more details on the race.
924 *
925 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
926 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
927 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
928 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
929 * the caller is going to try to clear PENDING again. If nothing else is
930 * happening, __lockres_clear_pending() sees PENDING is unset and does
931 * nothing.
932 *
933 * But what if another path (eg downconvert thread) has just started a
934 * new locking action? The other path has re-set PENDING. Our path
935 * cannot clear PENDING, because that will re-open the original race
936 * window.
937 *
938 * [Example]
939 *
940 * ocfs2_meta_lock()
941 * ocfs2_cluster_lock()
942 * set BUSY
943 * set PENDING
944 * drop l_lock
945 * ocfs2_dlm_lock()
946 * ocfs2_locking_ast() ocfs2_downconvert_thread()
947 * clear PENDING ocfs2_unblock_lock()
948 * take_l_lock
949 * !BUSY
950 * ocfs2_prepare_downconvert()
951 * set BUSY
952 * set PENDING
953 * drop l_lock
954 * take l_lock
955 * clear PENDING
956 * drop l_lock
957 * <window>
958 * ocfs2_dlm_lock()
959 *
960 * So as you can see, we now have a window where l_lock is not held,
961 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
962 *
963 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
964 * set by ocfs2_prepare_downconvert(). That wasn't nice.
965 *
966 * To solve this we introduce l_pending_gen. A call to
967 * lockres_clear_pending() will only do so when it is passed a generation
968 * number that matches the lockres. lockres_set_pending() will return the
969 * current generation number. When ocfs2_cluster_lock() goes to clear
970 * PENDING, it passes the generation it got from set_pending(). In our
971 * example above, the generation numbers will *not* match. Thus,
972 * ocfs2_cluster_lock() will not clear the PENDING set by
973 * ocfs2_prepare_downconvert().
974 */
975
976/* Unlocked version for ocfs2_locking_ast() */
977static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
978 unsigned int generation,
979 struct ocfs2_super *osb)
980{
981 assert_spin_locked(&lockres->l_lock);
982
983 /*
984 * The ast and locking functions can race us here. The winner
985 * will clear pending, the loser will not.
986 */
987 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
988 (lockres->l_pending_gen != generation))
989 return;
990
991 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
992 lockres->l_pending_gen++;
993
994 /*
995 * The downconvert thread may have skipped us because we
996 * were PENDING. Wake it up.
997 */
998 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
999 ocfs2_wake_downconvert_thread(osb);
1000}
1001
1002/* Locked version for callers of ocfs2_dlm_lock() */
1003static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1004 unsigned int generation,
1005 struct ocfs2_super *osb)
1006{
1007 unsigned long flags;
1008
1009 spin_lock_irqsave(&lockres->l_lock, flags);
1010 __lockres_clear_pending(lockres, generation, osb);
1011 spin_unlock_irqrestore(&lockres->l_lock, flags);
1012}
1013
1014static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1015{
1016 assert_spin_locked(&lockres->l_lock);
1017 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1018
1019 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1020
1021 return lockres->l_pending_gen;
1022}
1023
Joel Beckerc0e41332010-01-29 14:46:44 -08001024static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001025{
Joel Beckera796d282010-01-28 19:22:39 -08001026 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehaa2623a2006-09-12 21:58:23 -07001027 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 int needs_downconvert;
1029 unsigned long flags;
1030
Joel Beckerbd3e7612008-02-01 12:14:57 -08001031 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001032
Sunil Mushran9b915182010-02-26 19:42:44 -08001033 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1034 "type %s\n", lockres->l_name, level, lockres->l_level,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001035 ocfs2_lock_type_string(lockres->l_type));
1036
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001037 /*
1038 * We can skip the bast for locks which don't enable caching -
1039 * they'll be dropped at the earliest possible time anyway.
1040 */
1041 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1042 return;
1043
Mark Fashehccd979b2005-12-15 14:31:24 -08001044 spin_lock_irqsave(&lockres->l_lock, flags);
1045 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1046 if (needs_downconvert)
1047 ocfs2_schedule_blocked_lock(osb, lockres);
1048 spin_unlock_irqrestore(&lockres->l_lock, flags);
1049
Mark Fashehd680efe2006-09-08 14:14:34 -07001050 wake_up(&lockres->l_event);
1051
Mark Fasheh34d024f2007-09-24 15:56:19 -07001052 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001053}
1054
Joel Beckerc0e41332010-01-29 14:46:44 -08001055static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
Mark Fashehccd979b2005-12-15 14:31:24 -08001056{
Joel Beckera796d282010-01-28 19:22:39 -08001057 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Joel Beckerde551242008-02-01 14:45:08 -08001058 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001060 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001061
1062 spin_lock_irqsave(&lockres->l_lock, flags);
1063
David Teigland1693a5c2008-01-30 16:52:53 -08001064 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1065
1066 if (status == -EAGAIN) {
1067 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1068 goto out;
1069 }
1070
1071 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001072 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001073 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 spin_unlock_irqrestore(&lockres->l_lock, flags);
1075 return;
1076 }
1077
Sunil Mushran9b915182010-02-26 19:42:44 -08001078 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
1079 "level %d => %d\n", lockres->l_name, lockres->l_action,
1080 lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
1081
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 switch(lockres->l_action) {
1083 case OCFS2_AST_ATTACH:
1084 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001085 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 break;
1087 case OCFS2_AST_CONVERT:
1088 ocfs2_generic_handle_convert_action(lockres);
1089 break;
1090 case OCFS2_AST_DOWNCONVERT:
1091 ocfs2_generic_handle_downconvert_action(lockres);
1092 break;
1093 default:
Sunil Mushran9b915182010-02-26 19:42:44 -08001094 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1095 "flags 0x%lx, unlock: %u\n",
Mark Fashehe92d57d2006-09-12 21:34:35 -07001096 lockres->l_name, lockres->l_action, lockres->l_flags,
1097 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001098 BUG();
1099 }
David Teigland1693a5c2008-01-30 16:52:53 -08001100out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 /* set it to something invalid so if we get called again we
1102 * can catch it. */
1103 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001104
Joel Beckerde551242008-02-01 14:45:08 -08001105 /* Did we try to cancel this lock? Clear that state */
1106 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1107 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1108
1109 /*
1110 * We may have beaten the locking functions here. We certainly
1111 * know that dlm_lock() has been called :-)
1112 * Because we can't have two lock calls in flight at once, we
1113 * can use lockres->l_pending_gen.
1114 */
1115 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1116
Mark Fashehccd979b2005-12-15 14:31:24 -08001117 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001118 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001119}
1120
Joel Becker553b5eb2010-01-29 17:19:06 -08001121static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
1122{
1123 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1124 unsigned long flags;
1125
Sunil Mushran9b915182010-02-26 19:42:44 -08001126 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1127 lockres->l_name, lockres->l_unlock_action);
Joel Becker553b5eb2010-01-29 17:19:06 -08001128
1129 spin_lock_irqsave(&lockres->l_lock, flags);
1130 if (error) {
1131 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
1132 "unlock_action %d\n", error, lockres->l_name,
1133 lockres->l_unlock_action);
1134 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Becker553b5eb2010-01-29 17:19:06 -08001135 return;
1136 }
1137
1138 switch(lockres->l_unlock_action) {
1139 case OCFS2_UNLOCK_CANCEL_CONVERT:
1140 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
1141 lockres->l_action = OCFS2_AST_INVALID;
1142 /* Downconvert thread may have requeued this lock, we
1143 * need to wake it. */
1144 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1145 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
1146 break;
1147 case OCFS2_UNLOCK_DROP_LOCK:
1148 lockres->l_level = DLM_LOCK_IV;
1149 break;
1150 default:
1151 BUG();
1152 }
1153
1154 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1155 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1156 wake_up(&lockres->l_event);
1157 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Becker553b5eb2010-01-29 17:19:06 -08001158}
1159
1160/*
1161 * This is the filesystem locking protocol. It provides the lock handling
1162 * hooks for the underlying DLM. It has a maximum version number.
1163 * The version number allows interoperability with systems running at
1164 * the same major number and an equal or smaller minor number.
1165 *
1166 * Whenever the filesystem does new things with locks (adds or removes a
1167 * lock, orders them differently, does different things underneath a lock),
1168 * the version must be changed. The protocol is negotiated when joining
1169 * the dlm domain. A node may join the domain if its major version is
1170 * identical to all other nodes and its minor version is greater than
1171 * or equal to all other nodes. When its minor version is greater than
1172 * the other nodes, it will run at the minor version specified by the
1173 * other nodes.
1174 *
1175 * If a locking change is made that will not be compatible with older
1176 * versions, the major number must be increased and the minor version set
1177 * to zero. If a change merely adds a behavior that can be disabled when
1178 * speaking to older versions, the minor version must be increased. If a
1179 * change adds a fully backwards compatible change (eg, LVB changes that
1180 * are just ignored by older versions), the version does not need to be
1181 * updated.
1182 */
1183static struct ocfs2_locking_protocol lproto = {
1184 .lp_max_version = {
1185 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
1186 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
1187 },
1188 .lp_lock_ast = ocfs2_locking_ast,
1189 .lp_blocking_ast = ocfs2_blocking_ast,
1190 .lp_unlock_ast = ocfs2_unlock_ast,
1191};
1192
1193void ocfs2_set_locking_protocol(void)
1194{
1195 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1196}
1197
Mark Fashehccd979b2005-12-15 14:31:24 -08001198static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1199 int convert)
1200{
1201 unsigned long flags;
1202
Mark Fashehccd979b2005-12-15 14:31:24 -08001203 spin_lock_irqsave(&lockres->l_lock, flags);
1204 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Sunil Mushrana1912822010-01-21 10:50:03 -08001205 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 if (convert)
1207 lockres->l_action = OCFS2_AST_INVALID;
1208 else
1209 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1210 spin_unlock_irqrestore(&lockres->l_lock, flags);
1211
1212 wake_up(&lockres->l_event);
Mark Fashehccd979b2005-12-15 14:31:24 -08001213}
1214
1215/* Note: If we detect another process working on the lock (i.e.,
1216 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1217 * to do the right thing in that case.
1218 */
1219static int ocfs2_lock_create(struct ocfs2_super *osb,
1220 struct ocfs2_lock_res *lockres,
1221 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001222 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001223{
1224 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001226 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001227
Joel Beckerbd3e7612008-02-01 12:14:57 -08001228 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 dlm_flags);
1230
1231 spin_lock_irqsave(&lockres->l_lock, flags);
1232 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1233 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1234 spin_unlock_irqrestore(&lockres->l_lock, flags);
1235 goto bail;
1236 }
1237
1238 lockres->l_action = OCFS2_AST_ATTACH;
1239 lockres->l_requested = level;
1240 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001241 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 spin_unlock_irqrestore(&lockres->l_lock, flags);
1243
Joel Becker4670c462008-02-01 14:39:35 -08001244 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001245 level,
1246 &lockres->l_lksb,
1247 dlm_flags,
1248 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001249 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001250 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001251 if (ret) {
1252 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001253 ocfs2_recover_from_dlm_error(lockres, 1);
1254 }
1255
Joel Becker7431cd72008-02-01 12:15:37 -08001256 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001257
1258bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 return ret;
1260}
1261
1262static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1263 int flag)
1264{
1265 unsigned long flags;
1266 int ret;
1267
1268 spin_lock_irqsave(&lockres->l_lock, flags);
1269 ret = lockres->l_flags & flag;
1270 spin_unlock_irqrestore(&lockres->l_lock, flags);
1271
1272 return ret;
1273}
1274
1275static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1276
1277{
1278 wait_event(lockres->l_event,
1279 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1280}
1281
1282static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1283
1284{
1285 wait_event(lockres->l_event,
1286 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1287}
1288
1289/* predict what lock level we'll be dropping down to on behalf
1290 * of another node, and return true if the currently wanted
1291 * level will be compatible with it. */
1292static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1293 int wanted)
1294{
1295 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1296
1297 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1298}
1299
1300static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1301{
1302 INIT_LIST_HEAD(&mw->mw_item);
1303 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001304 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001305}
1306
1307static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1308{
1309 wait_for_completion(&mw->mw_complete);
1310 /* Re-arm the completion in case we want to wait on it again */
1311 INIT_COMPLETION(mw->mw_complete);
1312 return mw->mw_status;
1313}
1314
1315static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1316 struct ocfs2_mask_waiter *mw,
1317 unsigned long mask,
1318 unsigned long goal)
1319{
1320 BUG_ON(!list_empty(&mw->mw_item));
1321
1322 assert_spin_locked(&lockres->l_lock);
1323
1324 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1325 mw->mw_mask = mask;
1326 mw->mw_goal = goal;
1327}
1328
1329/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1330 * if the mask still hadn't reached its goal */
1331static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1332 struct ocfs2_mask_waiter *mw)
1333{
1334 unsigned long flags;
1335 int ret = 0;
1336
1337 spin_lock_irqsave(&lockres->l_lock, flags);
1338 if (!list_empty(&mw->mw_item)) {
1339 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1340 ret = -EBUSY;
1341
1342 list_del_init(&mw->mw_item);
1343 init_completion(&mw->mw_complete);
1344 }
1345 spin_unlock_irqrestore(&lockres->l_lock, flags);
1346
1347 return ret;
1348
1349}
1350
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001351static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1352 struct ocfs2_lock_res *lockres)
1353{
1354 int ret;
1355
1356 ret = wait_for_completion_interruptible(&mw->mw_complete);
1357 if (ret)
1358 lockres_remove_mask_waiter(lockres, mw);
1359 else
1360 ret = mw->mw_status;
1361 /* Re-arm the completion in case we want to wait on it again */
1362 INIT_COMPLETION(mw->mw_complete);
1363 return ret;
1364}
1365
Jan Karacb257972009-06-04 15:26:50 +02001366static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1367 struct ocfs2_lock_res *lockres,
1368 int level,
1369 u32 lkm_flags,
1370 int arg_flags,
1371 int l_subclass,
1372 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001373{
1374 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1376 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1377 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001378 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001379 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001380
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 ocfs2_init_mask_waiter(&mw);
1382
Mark Fashehb80fc012006-09-12 22:08:14 -07001383 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001384 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001385
Mark Fashehccd979b2005-12-15 14:31:24 -08001386again:
1387 wait = 0;
1388
Sunil Mushrana1912822010-01-21 10:50:03 -08001389 spin_lock_irqsave(&lockres->l_lock, flags);
1390
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 if (catch_signals && signal_pending(current)) {
1392 ret = -ERESTARTSYS;
Sunil Mushrana1912822010-01-21 10:50:03 -08001393 goto unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001394 }
1395
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1397 "Cluster lock called on freeing lockres %s! flags "
1398 "0x%lx\n", lockres->l_name, lockres->l_flags);
1399
1400 /* We only compare against the currently granted level
1401 * here. If the lock is blocked waiting on a downconvert,
1402 * we'll get caught below. */
1403 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1404 level > lockres->l_level) {
1405 /* is someone sitting in dlm_lock? If so, wait on
1406 * them. */
1407 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1408 wait = 1;
1409 goto unlock;
1410 }
1411
Sunil Mushrana1912822010-01-21 10:50:03 -08001412 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1413 /*
1414 * We've upconverted. If the lock now has a level we can
1415 * work with, we take it. If, however, the lock is not at the
1416 * required level, we go thru the full cycle. One way this could
1417 * happen is if a process requesting an upconvert to PR is
1418 * closely followed by another requesting upconvert to an EX.
1419 * If the process requesting EX lands here, we want it to
1420 * continue attempting to upconvert and let the process
1421 * requesting PR take the lock.
1422 * If multiple processes request upconvert to PR, the first one
1423 * here will take the lock. The others will have to go thru the
1424 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1425 * downconvert request.
1426 */
1427 if (level <= lockres->l_level)
1428 goto update_holders;
1429 }
1430
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1432 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1433 /* is the lock is currently blocked on behalf of
1434 * another node */
1435 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1436 wait = 1;
1437 goto unlock;
1438 }
1439
1440 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001441 if (noqueue_attempted > 0) {
1442 ret = -EAGAIN;
1443 goto unlock;
1444 }
1445 if (lkm_flags & DLM_LKF_NOQUEUE)
1446 noqueue_attempted = 1;
1447
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 if (lockres->l_action != OCFS2_AST_INVALID)
1449 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1450 lockres->l_name, lockres->l_action);
1451
Mark Fasheh019d1b22007-10-05 12:09:05 -07001452 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1453 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001454 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001455 } else {
1456 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001457 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001458 }
1459
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 lockres->l_requested = level;
1461 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001462 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001463 spin_unlock_irqrestore(&lockres->l_lock, flags);
1464
Joel Beckerbd3e7612008-02-01 12:14:57 -08001465 BUG_ON(level == DLM_LOCK_IV);
1466 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001467
Sunil Mushran9b915182010-02-26 19:42:44 -08001468 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 lockres->l_name, lockres->l_level, level);
1470
1471 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001472 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001473 level,
1474 &lockres->l_lksb,
1475 lkm_flags,
1476 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001477 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001478 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001479 if (ret) {
1480 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1481 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001482 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001483 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001484 }
1485 ocfs2_recover_from_dlm_error(lockres, 1);
1486 goto out;
1487 }
1488
Coly Li73ac36e2009-01-07 18:09:16 -08001489 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 lockres->l_name);
1491
1492 /* At this point we've gone inside the dlm and need to
1493 * complete our work regardless. */
1494 catch_signals = 0;
1495
1496 /* wait for busy to clear and carry on */
1497 goto again;
1498 }
1499
Sunil Mushrana1912822010-01-21 10:50:03 -08001500update_holders:
Mark Fashehccd979b2005-12-15 14:31:24 -08001501 /* Ok, if we get here then we're good to go. */
1502 ocfs2_inc_holders(lockres, level);
1503
1504 ret = 0;
1505unlock:
Sunil Mushrana1912822010-01-21 10:50:03 -08001506 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1507
Mark Fashehccd979b2005-12-15 14:31:24 -08001508 spin_unlock_irqrestore(&lockres->l_lock, flags);
1509out:
1510 /*
1511 * This is helping work around a lock inversion between the page lock
1512 * and dlm locks. One path holds the page lock while calling aops
1513 * which block acquiring dlm locks. The voting thread holds dlm
1514 * locks while acquiring page locks while down converting data locks.
1515 * This block is helping an aop path notice the inversion and back
1516 * off to unlock its page lock before trying the dlm lock again.
1517 */
1518 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1519 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1520 wait = 0;
1521 if (lockres_remove_mask_waiter(lockres, &mw))
1522 ret = -EAGAIN;
1523 else
1524 goto again;
1525 }
1526 if (wait) {
1527 ret = ocfs2_wait_for_mask(&mw);
1528 if (ret == 0)
1529 goto again;
1530 mlog_errno(ret);
1531 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001532 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533
Jan Karacb257972009-06-04 15:26:50 +02001534#ifdef CONFIG_DEBUG_LOCK_ALLOC
1535 if (!ret && lockres->l_lockdep_map.key != NULL) {
1536 if (level == DLM_LOCK_PR)
1537 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1538 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1539 caller_ip);
1540 else
1541 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1542 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1543 caller_ip);
1544 }
1545#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001546 return ret;
1547}
1548
Jan Karacb257972009-06-04 15:26:50 +02001549static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1550 struct ocfs2_lock_res *lockres,
1551 int level,
1552 u32 lkm_flags,
1553 int arg_flags)
1554{
1555 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1556 0, _RET_IP_);
1557}
1558
1559
1560static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1561 struct ocfs2_lock_res *lockres,
1562 int level,
1563 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001564{
1565 unsigned long flags;
1566
Mark Fashehccd979b2005-12-15 14:31:24 -08001567 spin_lock_irqsave(&lockres->l_lock, flags);
1568 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001569 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001570 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001571#ifdef CONFIG_DEBUG_LOCK_ALLOC
1572 if (lockres->l_lockdep_map.key != NULL)
1573 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1574#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001575}
1576
Adrian Bunkda66116e2006-11-20 03:24:28 +01001577static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1578 struct ocfs2_lock_res *lockres,
1579 int ex,
1580 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001581{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001582 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001583 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001584 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001585
1586 spin_lock_irqsave(&lockres->l_lock, flags);
1587 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1588 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1589 spin_unlock_irqrestore(&lockres->l_lock, flags);
1590
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001591 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001592}
1593
1594/* Grants us an EX lock on the data and metadata resources, skipping
1595 * the normal cluster directory lookup. Use this ONLY on newly created
1596 * inodes which other nodes can't possibly see, and which haven't been
1597 * hashed in the inode hash yet. This can give us a good performance
1598 * increase as it'll skip the network broadcast normally associated
1599 * with creating a new lock resource. */
1600int ocfs2_create_new_inode_locks(struct inode *inode)
1601{
1602 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001603 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001604
1605 BUG_ON(!inode);
1606 BUG_ON(!ocfs2_inode_is_new(inode));
1607
Mark Fashehb06970532006-03-03 10:24:33 -08001608 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001609
1610 /* NOTE: That we don't increment any of the holder counts, nor
1611 * do we add anything to a journal handle. Since this is
1612 * supposed to be a new inode which the cluster doesn't know
1613 * about yet, there is no need to. As far as the LVB handling
1614 * is concerned, this is basically like acquiring an EX lock
1615 * on a resource which has an invalid one -- we'll set it
1616 * valid when we release the EX. */
1617
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001618 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001619 if (ret) {
1620 mlog_errno(ret);
1621 goto bail;
1622 }
1623
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001624 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001625 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001626 * don't use a generation in their lock names.
1627 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001628 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001629 if (ret) {
1630 mlog_errno(ret);
1631 goto bail;
1632 }
1633
Tiger Yang50008632007-03-20 16:01:38 -07001634 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1635 if (ret) {
1636 mlog_errno(ret);
1637 goto bail;
1638 }
1639
Mark Fashehccd979b2005-12-15 14:31:24 -08001640bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001641 return ret;
1642}
1643
1644int ocfs2_rw_lock(struct inode *inode, int write)
1645{
1646 int status, level;
1647 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001648 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001649
1650 BUG_ON(!inode);
1651
Mark Fashehb06970532006-03-03 10:24:33 -08001652 mlog(0, "inode %llu take %s RW lock\n",
1653 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001654 write ? "EXMODE" : "PRMODE");
1655
Tao Mac1e8d352011-03-07 16:43:21 +08001656 if (ocfs2_mount_local(osb))
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001657 return 0;
1658
Mark Fashehccd979b2005-12-15 14:31:24 -08001659 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1660
Joel Beckerbd3e7612008-02-01 12:14:57 -08001661 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001662
1663 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1664 0);
1665 if (status < 0)
1666 mlog_errno(status);
1667
Mark Fashehccd979b2005-12-15 14:31:24 -08001668 return status;
1669}
1670
1671void ocfs2_rw_unlock(struct inode *inode, int write)
1672{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001673 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001674 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001675 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001676
Mark Fashehb06970532006-03-03 10:24:33 -08001677 mlog(0, "inode %llu drop %s RW lock\n",
1678 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001679 write ? "EXMODE" : "PRMODE");
1680
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001681 if (!ocfs2_mount_local(osb))
1682 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001683}
1684
Tiger Yang50008632007-03-20 16:01:38 -07001685/*
1686 * ocfs2_open_lock always get PR mode lock.
1687 */
1688int ocfs2_open_lock(struct inode *inode)
1689{
1690 int status = 0;
1691 struct ocfs2_lock_res *lockres;
1692 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1693
1694 BUG_ON(!inode);
1695
Tiger Yang50008632007-03-20 16:01:38 -07001696 mlog(0, "inode %llu take PRMODE open lock\n",
1697 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1698
1699 if (ocfs2_mount_local(osb))
1700 goto out;
1701
1702 lockres = &OCFS2_I(inode)->ip_open_lockres;
1703
1704 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001705 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001706 if (status < 0)
1707 mlog_errno(status);
1708
1709out:
Tiger Yang50008632007-03-20 16:01:38 -07001710 return status;
1711}
1712
1713int ocfs2_try_open_lock(struct inode *inode, int write)
1714{
1715 int status = 0, level;
1716 struct ocfs2_lock_res *lockres;
1717 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1718
1719 BUG_ON(!inode);
1720
Tiger Yang50008632007-03-20 16:01:38 -07001721 mlog(0, "inode %llu try to take %s open lock\n",
1722 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1723 write ? "EXMODE" : "PRMODE");
1724
1725 if (ocfs2_mount_local(osb))
1726 goto out;
1727
1728 lockres = &OCFS2_I(inode)->ip_open_lockres;
1729
Joel Beckerbd3e7612008-02-01 12:14:57 -08001730 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001731
1732 /*
1733 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001734 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001735 * other nodes and the -EAGAIN will indicate to the caller that
1736 * this inode is still in use.
1737 */
1738 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001739 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001740
1741out:
Tiger Yang50008632007-03-20 16:01:38 -07001742 return status;
1743}
1744
1745/*
1746 * ocfs2_open_unlock unlock PR and EX mode open locks.
1747 */
1748void ocfs2_open_unlock(struct inode *inode)
1749{
1750 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1751 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1752
Tiger Yang50008632007-03-20 16:01:38 -07001753 mlog(0, "inode %llu drop open lock\n",
1754 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1755
1756 if (ocfs2_mount_local(osb))
1757 goto out;
1758
1759 if(lockres->l_ro_holders)
1760 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001761 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001762 if(lockres->l_ex_holders)
1763 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001764 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001765
1766out:
Tao Mac1e8d352011-03-07 16:43:21 +08001767 return;
Tiger Yang50008632007-03-20 16:01:38 -07001768}
1769
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001770static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1771 int level)
1772{
1773 int ret;
1774 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1775 unsigned long flags;
1776 struct ocfs2_mask_waiter mw;
1777
1778 ocfs2_init_mask_waiter(&mw);
1779
1780retry_cancel:
1781 spin_lock_irqsave(&lockres->l_lock, flags);
1782 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1783 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1784 if (ret) {
1785 spin_unlock_irqrestore(&lockres->l_lock, flags);
1786 ret = ocfs2_cancel_convert(osb, lockres);
1787 if (ret < 0) {
1788 mlog_errno(ret);
1789 goto out;
1790 }
1791 goto retry_cancel;
1792 }
1793 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1794 spin_unlock_irqrestore(&lockres->l_lock, flags);
1795
1796 ocfs2_wait_for_mask(&mw);
1797 goto retry_cancel;
1798 }
1799
1800 ret = -ERESTARTSYS;
1801 /*
1802 * We may still have gotten the lock, in which case there's no
1803 * point to restarting the syscall.
1804 */
1805 if (lockres->l_level == level)
1806 ret = 0;
1807
1808 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1809 lockres->l_flags, lockres->l_level, lockres->l_action);
1810
1811 spin_unlock_irqrestore(&lockres->l_lock, flags);
1812
1813out:
1814 return ret;
1815}
1816
1817/*
1818 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1819 * flock() calls. The locking approach this requires is sufficiently
1820 * different from all other cluster lock types that we implement a
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001821 * separate path to the "low-level" dlm calls. In particular:
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001822 *
1823 * - No optimization of lock levels is done - we take at exactly
1824 * what's been requested.
1825 *
1826 * - No lock caching is employed. We immediately downconvert to
1827 * no-lock at unlock time. This also means flock locks never go on
1828 * the blocking list).
1829 *
1830 * - Since userspace can trivially deadlock itself with flock, we make
1831 * sure to allow cancellation of a misbehaving applications flock()
1832 * request.
1833 *
1834 * - Access to any flock lockres doesn't require concurrency, so we
1835 * can simplify the code by requiring the caller to guarantee
1836 * serialization of dlmglue flock calls.
1837 */
1838int ocfs2_file_lock(struct file *file, int ex, int trylock)
1839{
Mark Fashehe988cf12008-07-10 09:25:39 -07001840 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1841 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001842 unsigned long flags;
1843 struct ocfs2_file_private *fp = file->private_data;
1844 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1845 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1846 struct ocfs2_mask_waiter mw;
1847
1848 ocfs2_init_mask_waiter(&mw);
1849
1850 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001851 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001852 mlog(ML_ERROR,
1853 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1854 "level: %u\n", lockres->l_name, lockres->l_flags,
1855 lockres->l_level);
1856 return -EINVAL;
1857 }
1858
1859 spin_lock_irqsave(&lockres->l_lock, flags);
1860 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1861 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1862 spin_unlock_irqrestore(&lockres->l_lock, flags);
1863
1864 /*
1865 * Get the lock at NLMODE to start - that way we
1866 * can cancel the upconvert request if need be.
1867 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001868 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001869 if (ret < 0) {
1870 mlog_errno(ret);
1871 goto out;
1872 }
1873
1874 ret = ocfs2_wait_for_mask(&mw);
1875 if (ret) {
1876 mlog_errno(ret);
1877 goto out;
1878 }
1879 spin_lock_irqsave(&lockres->l_lock, flags);
1880 }
1881
1882 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001883 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001884 lockres->l_requested = level;
1885 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1886
1887 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1888 spin_unlock_irqrestore(&lockres->l_lock, flags);
1889
Joel Becker4670c462008-02-01 14:39:35 -08001890 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Beckera796d282010-01-28 19:22:39 -08001891 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Becker7431cd72008-02-01 12:15:37 -08001892 if (ret) {
1893 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001894 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001895 ret = -EINVAL;
1896 }
1897
1898 ocfs2_recover_from_dlm_error(lockres, 1);
1899 lockres_remove_mask_waiter(lockres, &mw);
1900 goto out;
1901 }
1902
1903 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1904 if (ret == -ERESTARTSYS) {
1905 /*
1906 * Userspace can cause deadlock itself with
1907 * flock(). Current behavior locally is to allow the
1908 * deadlock, but abort the system call if a signal is
1909 * received. We follow this example, otherwise a
1910 * poorly written program could sit in kernel until
1911 * reboot.
1912 *
1913 * Handling this is a bit more complicated for Ocfs2
1914 * though. We can't exit this function with an
1915 * outstanding lock request, so a cancel convert is
1916 * required. We intentionally overwrite 'ret' - if the
1917 * cancel fails and the lock was granted, it's easier
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001918 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001919 */
1920 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001921 } else if (!ret && (level > lockres->l_level)) {
1922 /* Trylock failed asynchronously */
1923 BUG_ON(!trylock);
1924 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001925 }
1926
1927out:
1928
1929 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1930 lockres->l_name, ex, trylock, ret);
1931 return ret;
1932}
1933
1934void ocfs2_file_unlock(struct file *file)
1935{
1936 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001937 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001938 unsigned long flags;
1939 struct ocfs2_file_private *fp = file->private_data;
1940 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1941 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1942 struct ocfs2_mask_waiter mw;
1943
1944 ocfs2_init_mask_waiter(&mw);
1945
1946 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1947 return;
1948
Mark Fashehe988cf12008-07-10 09:25:39 -07001949 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001950 return;
1951
1952 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1953 lockres->l_name, lockres->l_flags, lockres->l_level,
1954 lockres->l_action);
1955
1956 spin_lock_irqsave(&lockres->l_lock, flags);
1957 /*
1958 * Fake a blocking ast for the downconvert code.
1959 */
1960 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001961 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001962
Mark Fashehe988cf12008-07-10 09:25:39 -07001963 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001964 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1965 spin_unlock_irqrestore(&lockres->l_lock, flags);
1966
Mark Fashehe988cf12008-07-10 09:25:39 -07001967 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001968 if (ret) {
1969 mlog_errno(ret);
1970 return;
1971 }
1972
1973 ret = ocfs2_wait_for_mask(&mw);
1974 if (ret)
1975 mlog_errno(ret);
1976}
1977
Mark Fasheh34d024f2007-09-24 15:56:19 -07001978static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1979 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001980{
1981 int kick = 0;
1982
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001984 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001985 * condition. */
1986 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1987 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001988 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001989 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1990 kick = 1;
1991 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001992 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001993 if (!lockres->l_ex_holders)
1994 kick = 1;
1995 break;
1996 default:
1997 BUG();
1998 }
1999 }
2000
2001 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07002002 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002003}
2004
Mark Fashehccd979b2005-12-15 14:31:24 -08002005#define OCFS2_SEC_BITS 34
2006#define OCFS2_SEC_SHIFT (64 - 34)
2007#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2008
2009/* LVB only has room for 64 bits of time here so we pack it for
2010 * now. */
2011static u64 ocfs2_pack_timespec(struct timespec *spec)
2012{
2013 u64 res;
2014 u64 sec = spec->tv_sec;
2015 u32 nsec = spec->tv_nsec;
2016
2017 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
2018
2019 return res;
2020}
2021
2022/* Call this with the lockres locked. I am reasonably sure we don't
2023 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07002024 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002025static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2026{
2027 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002028 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002029 struct ocfs2_meta_lvb *lvb;
2030
Mark Fasheha641dc22008-12-24 16:03:48 -08002031 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002032
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002033 /*
2034 * Invalidate the LVB of a deleted inode - this way other
2035 * nodes are forced to go to disk and discover the new inode
2036 * status.
2037 */
2038 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2039 lvb->lvb_version = 0;
2040 goto out;
2041 }
2042
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07002043 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08002044 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2045 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
2046 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
2047 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
2048 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2049 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2050 lvb->lvb_iatime_packed =
2051 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
2052 lvb->lvb_ictime_packed =
2053 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2054 lvb->lvb_imtime_packed =
2055 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002056 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002057 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002058 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002059
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002060out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002061 mlog_meta_lvb(0, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002062}
2063
2064static void ocfs2_unpack_timespec(struct timespec *spec,
2065 u64 packed_time)
2066{
2067 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2068 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2069}
2070
2071static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2072{
2073 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002074 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002075 struct ocfs2_meta_lvb *lvb;
2076
Mark Fashehccd979b2005-12-15 14:31:24 -08002077 mlog_meta_lvb(0, lockres);
2078
Mark Fasheha641dc22008-12-24 16:03:48 -08002079 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002080
2081 /* We're safe here without the lockres lock... */
2082 spin_lock(&oi->ip_lock);
2083 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2084 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2085
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002086 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002087 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002088 ocfs2_set_inode_flags(inode);
2089
Mark Fashehccd979b2005-12-15 14:31:24 -08002090 /* fast-symlinks are a special case */
2091 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2092 inode->i_blocks = 0;
2093 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002094 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002095
2096 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2097 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2098 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2099 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2100 ocfs2_unpack_timespec(&inode->i_atime,
2101 be64_to_cpu(lvb->lvb_iatime_packed));
2102 ocfs2_unpack_timespec(&inode->i_mtime,
2103 be64_to_cpu(lvb->lvb_imtime_packed));
2104 ocfs2_unpack_timespec(&inode->i_ctime,
2105 be64_to_cpu(lvb->lvb_ictime_packed));
2106 spin_unlock(&oi->ip_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08002107}
2108
Mark Fashehf9e2d822006-09-12 15:35:49 -07002109static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2110 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002111{
Mark Fasheha641dc22008-12-24 16:03:48 -08002112 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002113
Joel Becker1c520df2009-06-19 15:14:13 -07002114 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2115 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002116 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002117 return 1;
2118 return 0;
2119}
2120
2121/* Determine whether a lock resource needs to be refreshed, and
2122 * arbitrate who gets to refresh it.
2123 *
2124 * 0 means no refresh needed.
2125 *
2126 * > 0 means you need to refresh this and you MUST call
2127 * ocfs2_complete_lock_res_refresh afterwards. */
2128static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2129{
2130 unsigned long flags;
2131 int status = 0;
2132
Mark Fashehccd979b2005-12-15 14:31:24 -08002133refresh_check:
2134 spin_lock_irqsave(&lockres->l_lock, flags);
2135 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2136 spin_unlock_irqrestore(&lockres->l_lock, flags);
2137 goto bail;
2138 }
2139
2140 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2141 spin_unlock_irqrestore(&lockres->l_lock, flags);
2142
2143 ocfs2_wait_on_refreshing_lock(lockres);
2144 goto refresh_check;
2145 }
2146
2147 /* Ok, I'll be the one to refresh this lock. */
2148 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2149 spin_unlock_irqrestore(&lockres->l_lock, flags);
2150
2151 status = 1;
2152bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002153 mlog(0, "status %d\n", status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002154 return status;
2155}
2156
2157/* If status is non zero, I'll mark it as not being in refresh
2158 * anymroe, but i won't clear the needs refresh flag. */
2159static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2160 int status)
2161{
2162 unsigned long flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08002163
2164 spin_lock_irqsave(&lockres->l_lock, flags);
2165 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2166 if (!status)
2167 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2168 spin_unlock_irqrestore(&lockres->l_lock, flags);
2169
2170 wake_up(&lockres->l_event);
Mark Fashehccd979b2005-12-15 14:31:24 -08002171}
2172
2173/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002174static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002175 struct buffer_head **bh)
2176{
2177 int status = 0;
2178 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002179 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002180 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002181 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002182
Mark Fashehbe9e9862007-04-18 15:22:08 -07002183 if (ocfs2_mount_local(osb))
2184 goto bail;
2185
Mark Fashehccd979b2005-12-15 14:31:24 -08002186 spin_lock(&oi->ip_lock);
2187 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002188 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002190 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002191 spin_unlock(&oi->ip_lock);
2192 status = -ENOENT;
2193 goto bail;
2194 }
2195 spin_unlock(&oi->ip_lock);
2196
Mark Fashehbe9e9862007-04-18 15:22:08 -07002197 if (!ocfs2_should_refresh_lock_res(lockres))
2198 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002199
2200 /* This will discard any caching information we might have had
2201 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002202 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002203
Mark Fasheh83418972007-04-23 18:53:12 -07002204 ocfs2_extent_map_trunc(inode, 0);
2205
Mark Fashehbe9e9862007-04-18 15:22:08 -07002206 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002207 mlog(0, "Trusting LVB on inode %llu\n",
2208 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002209 ocfs2_refresh_inode_from_lvb(inode);
2210 } else {
2211 /* Boo, we have to go to disk. */
2212 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002213 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002214 if (status < 0) {
2215 mlog_errno(status);
2216 goto bail_refresh;
2217 }
2218 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2219
2220 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002221 * locking an invalid object. ocfs2_read_inode_block()
2222 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002223 *
2224 * We bug on a stale inode here because we checked
2225 * above whether it was wiped from disk. The wiping
2226 * node provides a guarantee that we receive that
2227 * message and can mark the inode before dropping any
2228 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002229 mlog_bug_on_msg(inode->i_generation !=
2230 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002231 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002232 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002233 (unsigned long long)oi->ip_blkno,
2234 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002235 inode->i_generation);
2236 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2237 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002238 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2239 (unsigned long long)oi->ip_blkno,
2240 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002241 le32_to_cpu(fe->i_flags));
2242
2243 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002244 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002245 }
2246
2247 status = 0;
2248bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002249 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002250bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002251 return status;
2252}
2253
2254static int ocfs2_assign_bh(struct inode *inode,
2255 struct buffer_head **ret_bh,
2256 struct buffer_head *passed_bh)
2257{
2258 int status;
2259
2260 if (passed_bh) {
2261 /* Ok, the update went to disk for us, use the
2262 * returned bh. */
2263 *ret_bh = passed_bh;
2264 get_bh(*ret_bh);
2265
2266 return 0;
2267 }
2268
Joel Beckerb657c952008-11-13 14:49:11 -08002269 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002270 if (status < 0)
2271 mlog_errno(status);
2272
2273 return status;
2274}
2275
2276/*
2277 * returns < 0 error if the callback will never be called, otherwise
2278 * the result of the lock will be communicated via the callback.
2279 */
Jan Karacb257972009-06-04 15:26:50 +02002280int ocfs2_inode_lock_full_nested(struct inode *inode,
2281 struct buffer_head **ret_bh,
2282 int ex,
2283 int arg_flags,
2284 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002285{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002286 int status, level, acquired;
2287 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002288 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002289 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2290 struct buffer_head *local_bh = NULL;
2291
2292 BUG_ON(!inode);
2293
Mark Fashehb06970532006-03-03 10:24:33 -08002294 mlog(0, "inode %llu, take %s META lock\n",
2295 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002296 ex ? "EXMODE" : "PRMODE");
2297
2298 status = 0;
2299 acquired = 0;
2300 /* We'll allow faking a readonly metadata lock for
2301 * rodevices. */
2302 if (ocfs2_is_hard_readonly(osb)) {
2303 if (ex)
2304 status = -EROFS;
2305 goto bail;
2306 }
2307
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002308 if (ocfs2_mount_local(osb))
2309 goto local;
2310
Mark Fashehccd979b2005-12-15 14:31:24 -08002311 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002312 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002313
Mark Fashehe63aecb62007-10-18 15:30:42 -07002314 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002315 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 dlm_flags = 0;
2317 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002318 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002319
Jan Karacb257972009-06-04 15:26:50 +02002320 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2321 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002322 if (status < 0) {
2323 if (status != -EAGAIN && status != -EIOCBRETRY)
2324 mlog_errno(status);
2325 goto bail;
2326 }
2327
2328 /* Notify the error cleanup path to drop the cluster lock. */
2329 acquired = 1;
2330
2331 /* We wait twice because a node may have died while we were in
2332 * the lower dlm layers. The second time though, we've
2333 * committed to owning this lock so we don't allow signals to
2334 * abort the operation. */
2335 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002336 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002337
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002338local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002339 /*
2340 * We only see this flag if we're being called from
2341 * ocfs2_read_locked_inode(). It means we're locking an inode
2342 * which hasn't been populated yet, so clear the refresh flag
2343 * and let the caller handle it.
2344 */
2345 if (inode->i_state & I_NEW) {
2346 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002347 if (lockres)
2348 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002349 goto bail;
2350 }
2351
Mark Fashehccd979b2005-12-15 14:31:24 -08002352 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002353 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002354 * may or may not read one, depending on what's in the
2355 * LVB. The result of all of this is that we've *only* gone to
2356 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002357 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002358 if (status < 0) {
2359 if (status != -ENOENT)
2360 mlog_errno(status);
2361 goto bail;
2362 }
2363
2364 if (ret_bh) {
2365 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2366 if (status < 0) {
2367 mlog_errno(status);
2368 goto bail;
2369 }
2370 }
2371
Mark Fashehccd979b2005-12-15 14:31:24 -08002372bail:
2373 if (status < 0) {
2374 if (ret_bh && (*ret_bh)) {
2375 brelse(*ret_bh);
2376 *ret_bh = NULL;
2377 }
2378 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002379 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002380 }
2381
2382 if (local_bh)
2383 brelse(local_bh);
2384
Mark Fashehccd979b2005-12-15 14:31:24 -08002385 return status;
2386}
2387
2388/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002389 * This is working around a lock inversion between tasks acquiring DLM
2390 * locks while holding a page lock and the downconvert thread which
2391 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002392 *
2393 * ** These _with_page variantes are only intended to be called from aop
2394 * methods that hold page locks and return a very specific *positive* error
2395 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2396 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002397 * The DLM is called such that it returns -EAGAIN if it would have
2398 * blocked waiting for the downconvert thread. In that case we unlock
2399 * our page so the downconvert thread can make progress. Once we've
2400 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2401 * that called us can bubble that back up into the VFS who will then
2402 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 *
2404 * We do a blocking lock and immediate unlock before returning, though, so that
2405 * the lock has a great chance of being cached on this node by the time the VFS
2406 * calls back to retry the aop. This has a potential to livelock as nodes
2407 * ping locks back and forth, but that's a risk we're willing to take to avoid
2408 * the lock inversion simply.
2409 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002410int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002411 struct buffer_head **ret_bh,
2412 int ex,
2413 struct page *page)
2414{
2415 int ret;
2416
Mark Fashehe63aecb62007-10-18 15:30:42 -07002417 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002418 if (ret == -EAGAIN) {
2419 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002420 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2421 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002422 ret = AOP_TRUNCATED_PAGE;
2423 }
2424
2425 return ret;
2426}
2427
Mark Fashehe63aecb62007-10-18 15:30:42 -07002428int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002429 struct vfsmount *vfsmnt,
2430 int *level)
2431{
2432 int ret;
2433
Mark Fashehe63aecb62007-10-18 15:30:42 -07002434 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002435 if (ret < 0) {
2436 mlog_errno(ret);
2437 return ret;
2438 }
2439
2440 /*
2441 * If we should update atime, we will get EX lock,
2442 * otherwise we just get PR lock.
2443 */
2444 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2445 struct buffer_head *bh = NULL;
2446
Mark Fashehe63aecb62007-10-18 15:30:42 -07002447 ocfs2_inode_unlock(inode, 0);
2448 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002449 if (ret < 0) {
2450 mlog_errno(ret);
2451 return ret;
2452 }
2453 *level = 1;
2454 if (ocfs2_should_update_atime(inode, vfsmnt))
2455 ocfs2_update_inode_atime(inode, bh);
2456 if (bh)
2457 brelse(bh);
2458 } else
2459 *level = 0;
2460
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002461 return ret;
2462}
2463
Mark Fashehe63aecb62007-10-18 15:30:42 -07002464void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002465 int ex)
2466{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002467 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002468 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002470
Mark Fashehb06970532006-03-03 10:24:33 -08002471 mlog(0, "inode %llu drop %s META lock\n",
2472 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002473 ex ? "EXMODE" : "PRMODE");
2474
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002475 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2476 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002477 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002478}
2479
Sunil Mushrandf152c22009-06-22 11:40:07 -07002480int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002481{
2482 struct ocfs2_lock_res *lockres;
2483 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002484 int status = 0;
2485
Sunil Mushrandf152c22009-06-22 11:40:07 -07002486 if (ocfs2_is_hard_readonly(osb))
2487 return -EROFS;
2488
2489 if (ocfs2_mount_local(osb))
2490 return 0;
2491
Srinivas Eeda83273932009-06-03 17:02:55 -07002492 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002493 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002494 if (status < 0)
2495 return status;
2496
2497 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002498 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2499 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002500 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002501 else
2502 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2503
Srinivas Eeda83273932009-06-03 17:02:55 -07002504 return status;
2505}
2506
Sunil Mushrandf152c22009-06-22 11:40:07 -07002507void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002508{
2509 struct ocfs2_lock_res *lockres;
2510 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002511
Sunil Mushrandf152c22009-06-22 11:40:07 -07002512 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2513 lockres = &osb->osb_orphan_scan.os_lockres;
2514 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2515 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2516 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2517 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2518 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002519}
2520
Mark Fashehccd979b2005-12-15 14:31:24 -08002521int ocfs2_super_lock(struct ocfs2_super *osb,
2522 int ex)
2523{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002524 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002525 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002526 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002527
Mark Fashehccd979b2005-12-15 14:31:24 -08002528 if (ocfs2_is_hard_readonly(osb))
2529 return -EROFS;
2530
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002531 if (ocfs2_mount_local(osb))
2532 goto bail;
2533
Mark Fashehccd979b2005-12-15 14:31:24 -08002534 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2535 if (status < 0) {
2536 mlog_errno(status);
2537 goto bail;
2538 }
2539
2540 /* The super block lock path is really in the best position to
2541 * know when resources covered by the lock need to be
2542 * refreshed, so we do it here. Of course, making sense of
2543 * everything is up to the caller :) */
2544 status = ocfs2_should_refresh_lock_res(lockres);
2545 if (status < 0) {
2546 mlog_errno(status);
2547 goto bail;
2548 }
2549 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002550 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002551
2552 ocfs2_complete_lock_res_refresh(lockres, status);
2553
2554 if (status < 0)
2555 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002556 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002557 }
2558bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08002559 return status;
2560}
2561
2562void ocfs2_super_unlock(struct ocfs2_super *osb,
2563 int ex)
2564{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002565 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002566 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2567
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002568 if (!ocfs2_mount_local(osb))
2569 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002570}
2571
2572int ocfs2_rename_lock(struct ocfs2_super *osb)
2573{
2574 int status;
2575 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2576
2577 if (ocfs2_is_hard_readonly(osb))
2578 return -EROFS;
2579
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002580 if (ocfs2_mount_local(osb))
2581 return 0;
2582
Joel Beckerbd3e7612008-02-01 12:14:57 -08002583 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002584 if (status < 0)
2585 mlog_errno(status);
2586
2587 return status;
2588}
2589
2590void ocfs2_rename_unlock(struct ocfs2_super *osb)
2591{
2592 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2593
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002594 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002595 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002596}
2597
wengang wang6ca497a2009-03-06 21:29:10 +08002598int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2599{
2600 int status;
2601 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2602
2603 if (ocfs2_is_hard_readonly(osb))
2604 return -EROFS;
2605
2606 if (ocfs2_mount_local(osb))
2607 return 0;
2608
2609 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2610 0, 0);
2611 if (status < 0)
2612 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2613
2614 return status;
2615}
2616
2617void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2618{
2619 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2620
2621 if (!ocfs2_mount_local(osb))
2622 ocfs2_cluster_unlock(osb, lockres,
2623 ex ? LKM_EXMODE : LKM_PRMODE);
2624}
2625
Mark Fashehd680efe2006-09-08 14:14:34 -07002626int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2627{
2628 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002629 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002630 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2631 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2632
2633 BUG_ON(!dl);
2634
2635 if (ocfs2_is_hard_readonly(osb))
2636 return -EROFS;
2637
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002638 if (ocfs2_mount_local(osb))
2639 return 0;
2640
Mark Fashehd680efe2006-09-08 14:14:34 -07002641 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2642 if (ret < 0)
2643 mlog_errno(ret);
2644
2645 return ret;
2646}
2647
2648void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2649{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002650 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002651 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2652 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2653
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002654 if (!ocfs2_mount_local(osb))
2655 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002656}
2657
Mark Fashehccd979b2005-12-15 14:31:24 -08002658/* Reference counting of the dlm debug structure. We want this because
2659 * open references on the debug inodes can live on after a mount, so
2660 * we can't rely on the ocfs2_super to always exist. */
2661static void ocfs2_dlm_debug_free(struct kref *kref)
2662{
2663 struct ocfs2_dlm_debug *dlm_debug;
2664
2665 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2666
2667 kfree(dlm_debug);
2668}
2669
2670void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2671{
2672 if (dlm_debug)
2673 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2674}
2675
2676static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2677{
2678 kref_get(&debug->d_refcnt);
2679}
2680
2681struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2682{
2683 struct ocfs2_dlm_debug *dlm_debug;
2684
2685 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2686 if (!dlm_debug) {
2687 mlog_errno(-ENOMEM);
2688 goto out;
2689 }
2690
2691 kref_init(&dlm_debug->d_refcnt);
2692 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2693 dlm_debug->d_locking_state = NULL;
2694out:
2695 return dlm_debug;
2696}
2697
2698/* Access to this is arbitrated for us via seq_file->sem. */
2699struct ocfs2_dlm_seq_priv {
2700 struct ocfs2_dlm_debug *p_dlm_debug;
2701 struct ocfs2_lock_res p_iter_res;
2702 struct ocfs2_lock_res p_tmp_res;
2703};
2704
2705static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2706 struct ocfs2_dlm_seq_priv *priv)
2707{
2708 struct ocfs2_lock_res *iter, *ret = NULL;
2709 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2710
2711 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2712
2713 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2714 /* discover the head of the list */
2715 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2716 mlog(0, "End of list found, %p\n", ret);
2717 break;
2718 }
2719
2720 /* We track our "dummy" iteration lockres' by a NULL
2721 * l_ops field. */
2722 if (iter->l_ops != NULL) {
2723 ret = iter;
2724 break;
2725 }
2726 }
2727
2728 return ret;
2729}
2730
2731static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2732{
2733 struct ocfs2_dlm_seq_priv *priv = m->private;
2734 struct ocfs2_lock_res *iter;
2735
2736 spin_lock(&ocfs2_dlm_tracking_lock);
2737 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2738 if (iter) {
2739 /* Since lockres' have the lifetime of their container
2740 * (which can be inodes, ocfs2_supers, etc) we want to
2741 * copy this out to a temporary lockres while still
2742 * under the spinlock. Obviously after this we can't
2743 * trust any pointers on the copy returned, but that's
2744 * ok as the information we want isn't typically held
2745 * in them. */
2746 priv->p_tmp_res = *iter;
2747 iter = &priv->p_tmp_res;
2748 }
2749 spin_unlock(&ocfs2_dlm_tracking_lock);
2750
2751 return iter;
2752}
2753
2754static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2755{
2756}
2757
2758static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2759{
2760 struct ocfs2_dlm_seq_priv *priv = m->private;
2761 struct ocfs2_lock_res *iter = v;
2762 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2763
2764 spin_lock(&ocfs2_dlm_tracking_lock);
2765 iter = ocfs2_dlm_next_res(iter, priv);
2766 list_del_init(&dummy->l_debug_list);
2767 if (iter) {
2768 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2769 priv->p_tmp_res = *iter;
2770 iter = &priv->p_tmp_res;
2771 }
2772 spin_unlock(&ocfs2_dlm_tracking_lock);
2773
2774 return iter;
2775}
2776
2777/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002778#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002779static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2780{
2781 int i;
2782 char *lvb;
2783 struct ocfs2_lock_res *lockres = v;
2784
2785 if (!lockres)
2786 return -EINVAL;
2787
Mark Fashehd680efe2006-09-08 14:14:34 -07002788 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2789
2790 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2791 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2792 lockres->l_name,
2793 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2794 else
2795 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2796
2797 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002798 "0x%lx\t"
2799 "0x%x\t"
2800 "0x%x\t"
2801 "%u\t"
2802 "%u\t"
2803 "%d\t"
2804 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002805 lockres->l_level,
2806 lockres->l_flags,
2807 lockres->l_action,
2808 lockres->l_unlock_action,
2809 lockres->l_ro_holders,
2810 lockres->l_ex_holders,
2811 lockres->l_requested,
2812 lockres->l_blocking);
2813
2814 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002815 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002816 for(i = 0; i < DLM_LVB_LEN; i++)
2817 seq_printf(m, "0x%x\t", lvb[i]);
2818
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002819#ifdef CONFIG_OCFS2_FS_STATS
2820# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2821# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2822# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2823# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2824# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2825# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2826# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2827# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2828# define lock_refresh(_l) (_l)->l_lock_refresh
2829#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002830# define lock_num_prmode(_l) (0ULL)
2831# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002832# define lock_num_prmode_failed(_l) (0)
2833# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002834# define lock_total_prmode(_l) (0ULL)
2835# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002836# define lock_max_prmode(_l) (0)
2837# define lock_max_exmode(_l) (0)
2838# define lock_refresh(_l) (0)
2839#endif
2840 /* The following seq_print was added in version 2 of this output */
2841 seq_printf(m, "%llu\t"
2842 "%llu\t"
2843 "%u\t"
2844 "%u\t"
2845 "%llu\t"
2846 "%llu\t"
2847 "%u\t"
2848 "%u\t"
2849 "%u\t",
2850 lock_num_prmode(lockres),
2851 lock_num_exmode(lockres),
2852 lock_num_prmode_failed(lockres),
2853 lock_num_exmode_failed(lockres),
2854 lock_total_prmode(lockres),
2855 lock_total_exmode(lockres),
2856 lock_max_prmode(lockres),
2857 lock_max_exmode(lockres),
2858 lock_refresh(lockres));
2859
Mark Fashehccd979b2005-12-15 14:31:24 -08002860 /* End the line */
2861 seq_printf(m, "\n");
2862 return 0;
2863}
2864
Jan Engelhardt90d99772008-01-22 20:52:20 +01002865static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002866 .start = ocfs2_dlm_seq_start,
2867 .stop = ocfs2_dlm_seq_stop,
2868 .next = ocfs2_dlm_seq_next,
2869 .show = ocfs2_dlm_seq_show,
2870};
2871
2872static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2873{
Joe Perches33fa1d92010-07-12 13:50:19 -07002874 struct seq_file *seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002875 struct ocfs2_dlm_seq_priv *priv = seq->private;
2876 struct ocfs2_lock_res *res = &priv->p_iter_res;
2877
2878 ocfs2_remove_lockres_tracking(res);
2879 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2880 return seq_release_private(inode, file);
2881}
2882
2883static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2884{
2885 int ret;
2886 struct ocfs2_dlm_seq_priv *priv;
2887 struct seq_file *seq;
2888 struct ocfs2_super *osb;
2889
2890 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2891 if (!priv) {
2892 ret = -ENOMEM;
2893 mlog_errno(ret);
2894 goto out;
2895 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002896 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002897 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2898 priv->p_dlm_debug = osb->osb_dlm_debug;
2899 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2900
2901 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2902 if (ret) {
2903 kfree(priv);
2904 mlog_errno(ret);
2905 goto out;
2906 }
2907
Joe Perches33fa1d92010-07-12 13:50:19 -07002908 seq = file->private_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08002909 seq->private = priv;
2910
2911 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2912 priv->p_dlm_debug);
2913
2914out:
2915 return ret;
2916}
2917
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002918static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002919 .open = ocfs2_dlm_debug_open,
2920 .release = ocfs2_dlm_debug_release,
2921 .read = seq_read,
2922 .llseek = seq_lseek,
2923};
2924
2925static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2926{
2927 int ret = 0;
2928 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2929
2930 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2931 S_IFREG|S_IRUSR,
2932 osb->osb_debug_root,
2933 osb,
2934 &ocfs2_dlm_debug_fops);
2935 if (!dlm_debug->d_locking_state) {
2936 ret = -EINVAL;
2937 mlog(ML_ERROR,
2938 "Unable to create locking state debugfs file.\n");
2939 goto out;
2940 }
2941
2942 ocfs2_get_dlm_debug(dlm_debug);
2943out:
2944 return ret;
2945}
2946
2947static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2948{
2949 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2950
2951 if (dlm_debug) {
2952 debugfs_remove(dlm_debug->d_locking_state);
2953 ocfs2_put_dlm_debug(dlm_debug);
2954 }
2955}
2956
2957int ocfs2_dlm_init(struct ocfs2_super *osb)
2958{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002959 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002960 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002961
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002962 if (ocfs2_mount_local(osb)) {
2963 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002964 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002965 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002966
Mark Fashehccd979b2005-12-15 14:31:24 -08002967 status = ocfs2_dlm_init_debug(osb);
2968 if (status < 0) {
2969 mlog_errno(status);
2970 goto bail;
2971 }
2972
Mark Fasheh34d024f2007-09-24 15:56:19 -07002973 /* launch downconvert thread */
2974 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2975 if (IS_ERR(osb->dc_task)) {
2976 status = PTR_ERR(osb->dc_task);
2977 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002978 mlog_errno(status);
2979 goto bail;
2980 }
2981
Mark Fashehccd979b2005-12-15 14:31:24 -08002982 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002983 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2984 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002985 strlen(osb->uuid_str),
Joel Becker553b5eb2010-01-29 17:19:06 -08002986 &lproto, ocfs2_do_node_down, osb,
Joel Becker4670c462008-02-01 14:39:35 -08002987 &conn);
2988 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002989 mlog_errno(status);
2990 goto bail;
2991 }
2992
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002993 status = ocfs2_cluster_this_node(&osb->node_num);
2994 if (status < 0) {
2995 mlog_errno(status);
2996 mlog(ML_ERROR,
2997 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002998 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002999 goto bail;
3000 }
3001
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003002local:
Mark Fashehccd979b2005-12-15 14:31:24 -08003003 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3004 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08003005 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07003006 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003007
Joel Becker4670c462008-02-01 14:39:35 -08003008 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08003009
3010 status = 0;
3011bail:
3012 if (status < 0) {
3013 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003014 if (osb->dc_task)
3015 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08003016 }
3017
Mark Fashehccd979b2005-12-15 14:31:24 -08003018 return status;
3019}
3020
Joel Becker286eaa92008-02-01 15:03:57 -08003021void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3022 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08003023{
Mark Fashehccd979b2005-12-15 14:31:24 -08003024 ocfs2_drop_osb_locks(osb);
3025
Joel Becker4670c462008-02-01 14:39:35 -08003026 /*
3027 * Now that we have dropped all locks and ocfs2_dismount_volume()
3028 * has disabled recovery, the DLM won't be talking to us. It's
3029 * safe to tear things down before disconnecting the cluster.
3030 */
3031
Mark Fasheh34d024f2007-09-24 15:56:19 -07003032 if (osb->dc_task) {
3033 kthread_stop(osb->dc_task);
3034 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003035 }
3036
3037 ocfs2_lock_res_free(&osb->osb_super_lockres);
3038 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003039 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003040 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003041
Joel Becker286eaa92008-02-01 15:03:57 -08003042 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003043 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003044
3045 ocfs2_dlm_shutdown_debug(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003046}
3047
Mark Fashehccd979b2005-12-15 14:31:24 -08003048static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003049 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003050{
Joel Becker7431cd72008-02-01 12:15:37 -08003051 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003052 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003053 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003054
3055 /* We didn't get anywhere near actually using this lockres. */
3056 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3057 goto out;
3058
Mark Fashehb80fc012006-09-12 22:08:14 -07003059 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003060 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003061
Mark Fashehccd979b2005-12-15 14:31:24 -08003062 spin_lock_irqsave(&lockres->l_lock, flags);
3063
3064 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3065 "lockres %s, flags 0x%lx\n",
3066 lockres->l_name, lockres->l_flags);
3067
3068 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3069 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3070 "%u, unlock_action = %u\n",
3071 lockres->l_name, lockres->l_flags, lockres->l_action,
3072 lockres->l_unlock_action);
3073
3074 spin_unlock_irqrestore(&lockres->l_lock, flags);
3075
3076 /* XXX: Today we just wait on any busy
3077 * locks... Perhaps we need to cancel converts in the
3078 * future? */
3079 ocfs2_wait_on_busy_lock(lockres);
3080
3081 spin_lock_irqsave(&lockres->l_lock, flags);
3082 }
3083
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003084 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3085 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003086 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003087 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3088 lockres->l_ops->set_lvb(lockres);
3089 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003090
3091 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3092 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3093 lockres->l_name);
3094 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3095 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3096
3097 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3098 spin_unlock_irqrestore(&lockres->l_lock, flags);
3099 goto out;
3100 }
3101
3102 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3103
3104 /* make sure we never get here while waiting for an ast to
3105 * fire. */
3106 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3107
3108 /* is this necessary? */
3109 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3110 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3111 spin_unlock_irqrestore(&lockres->l_lock, flags);
3112
3113 mlog(0, "lock %s\n", lockres->l_name);
3114
Joel Beckera796d282010-01-28 19:22:39 -08003115 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
Joel Becker7431cd72008-02-01 12:15:37 -08003116 if (ret) {
3117 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003118 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003119 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003120 BUG();
3121 }
Coly Li73ac36e2009-01-07 18:09:16 -08003122 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003123 lockres->l_name);
3124
3125 ocfs2_wait_on_busy_lock(lockres);
3126out:
Mark Fashehccd979b2005-12-15 14:31:24 -08003127 return 0;
3128}
3129
3130/* Mark the lockres as being dropped. It will no longer be
3131 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003132 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003133 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003134 *
3135 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3136void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3137{
3138 int status;
3139 struct ocfs2_mask_waiter mw;
3140 unsigned long flags;
3141
3142 ocfs2_init_mask_waiter(&mw);
3143
3144 spin_lock_irqsave(&lockres->l_lock, flags);
3145 lockres->l_flags |= OCFS2_LOCK_FREEING;
3146 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3147 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3148 spin_unlock_irqrestore(&lockres->l_lock, flags);
3149
3150 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3151
3152 status = ocfs2_wait_for_mask(&mw);
3153 if (status)
3154 mlog_errno(status);
3155
3156 spin_lock_irqsave(&lockres->l_lock, flags);
3157 }
3158 spin_unlock_irqrestore(&lockres->l_lock, flags);
3159}
3160
Mark Fashehd680efe2006-09-08 14:14:34 -07003161void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3162 struct ocfs2_lock_res *lockres)
3163{
3164 int ret;
3165
3166 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003167 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003168 if (ret)
3169 mlog_errno(ret);
3170}
3171
Mark Fashehccd979b2005-12-15 14:31:24 -08003172static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3173{
Mark Fashehd680efe2006-09-08 14:14:34 -07003174 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3175 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003176 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003177 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003178}
3179
Mark Fashehccd979b2005-12-15 14:31:24 -08003180int ocfs2_drop_inode_locks(struct inode *inode)
3181{
3182 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003183
Mark Fashehccd979b2005-12-15 14:31:24 -08003184 /* No need to call ocfs2_mark_lockres_freeing here -
3185 * ocfs2_clear_inode has done it for us. */
3186
3187 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003188 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003189 if (err < 0)
3190 mlog_errno(err);
3191
3192 status = err;
3193
3194 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003195 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003196 if (err < 0)
3197 mlog_errno(err);
3198 if (err < 0 && !status)
3199 status = err;
3200
3201 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003202 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003203 if (err < 0)
3204 mlog_errno(err);
3205 if (err < 0 && !status)
3206 status = err;
3207
Mark Fashehccd979b2005-12-15 14:31:24 -08003208 return status;
3209}
3210
Joel Beckerde551242008-02-01 14:45:08 -08003211static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3212 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003213{
3214 assert_spin_locked(&lockres->l_lock);
3215
Joel Beckerbd3e7612008-02-01 12:14:57 -08003216 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003217
3218 if (lockres->l_level <= new_level) {
Sunil Mushran9b915182010-02-26 19:42:44 -08003219 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3220 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3221 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3222 new_level, list_empty(&lockres->l_blocked_list),
3223 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3224 lockres->l_flags, lockres->l_ro_holders,
3225 lockres->l_ex_holders, lockres->l_action,
3226 lockres->l_unlock_action, lockres->l_requested,
3227 lockres->l_blocking, lockres->l_pending_gen);
Mark Fashehccd979b2005-12-15 14:31:24 -08003228 BUG();
3229 }
3230
Sunil Mushran9b915182010-02-26 19:42:44 -08003231 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3232 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
Mark Fashehccd979b2005-12-15 14:31:24 -08003233
3234 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3235 lockres->l_requested = new_level;
3236 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003237 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003238}
3239
3240static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3241 struct ocfs2_lock_res *lockres,
3242 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003243 int lvb,
3244 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003245{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003246 int ret;
3247 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003248
Sunil Mushran9b915182010-02-26 19:42:44 -08003249 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3250 lockres->l_level, new_level);
3251
Mark Fashehccd979b2005-12-15 14:31:24 -08003252 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003253 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003254
Joel Becker4670c462008-02-01 14:39:35 -08003255 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003256 new_level,
3257 &lockres->l_lksb,
3258 dlm_flags,
3259 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08003260 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08003261 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003262 if (ret) {
3263 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003264 ocfs2_recover_from_dlm_error(lockres, 1);
3265 goto bail;
3266 }
3267
3268 ret = 0;
3269bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08003270 return ret;
3271}
3272
Joel Becker24ef1812008-01-29 17:37:32 -08003273/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003274static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3275 struct ocfs2_lock_res *lockres)
3276{
3277 assert_spin_locked(&lockres->l_lock);
3278
Mark Fashehccd979b2005-12-15 14:31:24 -08003279 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3280 /* If we're already trying to cancel a lock conversion
3281 * then just drop the spinlock and allow the caller to
3282 * requeue this lock. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003283 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003284 return 0;
3285 }
3286
3287 /* were we in a convert when we got the bast fire? */
3288 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3289 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3290 /* set things up for the unlockast to know to just
3291 * clear out the ast_action and unset busy, etc. */
3292 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3293
3294 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3295 "lock %s, invalid flags: 0x%lx\n",
3296 lockres->l_name, lockres->l_flags);
3297
Sunil Mushran9b915182010-02-26 19:42:44 -08003298 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3299
Mark Fashehccd979b2005-12-15 14:31:24 -08003300 return 1;
3301}
3302
3303static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3304 struct ocfs2_lock_res *lockres)
3305{
3306 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003307
Joel Becker4670c462008-02-01 14:39:35 -08003308 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Beckera796d282010-01-28 19:22:39 -08003309 DLM_LKF_CANCEL);
Joel Becker7431cd72008-02-01 12:15:37 -08003310 if (ret) {
3311 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003312 ocfs2_recover_from_dlm_error(lockres, 0);
3313 }
3314
Sunil Mushran9b915182010-02-26 19:42:44 -08003315 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003316
Mark Fashehccd979b2005-12-15 14:31:24 -08003317 return ret;
3318}
3319
Mark Fashehb5e500e2006-09-13 22:01:16 -07003320static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3321 struct ocfs2_lock_res *lockres,
3322 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003323{
3324 unsigned long flags;
3325 int blocking;
3326 int new_level;
Sunil Mushran079b8052010-02-03 10:16:54 -08003327 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003328 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003329 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003330 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003331
Mark Fashehccd979b2005-12-15 14:31:24 -08003332 spin_lock_irqsave(&lockres->l_lock, flags);
3333
Mark Fashehccd979b2005-12-15 14:31:24 -08003334recheck:
Sunil Mushrandb0f6ce2010-02-01 16:55:50 -08003335 /*
3336 * Is it still blocking? If not, we have no more work to do.
3337 */
3338 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3339 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3340 spin_unlock_irqrestore(&lockres->l_lock, flags);
3341 ret = 0;
3342 goto leave;
3343 }
3344
Mark Fashehccd979b2005-12-15 14:31:24 -08003345 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003346 /* XXX
3347 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3348 * exists entirely for one reason - another thread has set
3349 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3350 *
3351 * If we do ocfs2_cancel_convert() before the other thread
3352 * calls dlm_lock(), our cancel will do nothing. We will
3353 * get no ast, and we will have no way of knowing the
3354 * cancel failed. Meanwhile, the other thread will call
3355 * into dlm_lock() and wait...forever.
3356 *
3357 * Why forever? Because another node has asked for the
3358 * lock first; that's why we're here in unblock_lock().
3359 *
3360 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3361 * set, we just requeue the unblock. Only when the other
3362 * thread has called dlm_lock() and cleared PENDING will
3363 * we then cancel their request.
3364 *
3365 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3366 * at the same time they set OCFS2_DLM_BUSY. They must
3367 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3368 */
Sunil Mushran9b915182010-02-26 19:42:44 -08003369 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3370 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3371 lockres->l_name);
Joel Beckerde551242008-02-01 14:45:08 -08003372 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003373 }
Joel Beckerde551242008-02-01 14:45:08 -08003374
Mark Fashehd680efe2006-09-08 14:14:34 -07003375 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003376 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3377 spin_unlock_irqrestore(&lockres->l_lock, flags);
3378 if (ret) {
3379 ret = ocfs2_cancel_convert(osb, lockres);
3380 if (ret < 0)
3381 mlog_errno(ret);
3382 }
3383 goto leave;
3384 }
3385
Sunil Mushrana1912822010-01-21 10:50:03 -08003386 /*
3387 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3388 * set when the ast is received for an upconvert just before the
3389 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3390 * on the heels of the ast, we want to delay the downconvert just
3391 * enough to allow the up requestor to do its task. Because this
3392 * lock is in the blocked queue, the lock will be downconverted
3393 * as soon as the requestor is done with the lock.
3394 */
3395 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3396 goto leave_requeue;
3397
Sunil Mushran0d741252010-01-29 09:44:11 -08003398 /*
3399 * How can we block and yet be at NL? We were trying to upconvert
3400 * from NL and got canceled. The code comes back here, and now
3401 * we notice and clear BLOCKING.
3402 */
3403 if (lockres->l_level == DLM_LOCK_NL) {
3404 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
Sunil Mushran9b915182010-02-26 19:42:44 -08003405 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
Sunil Mushran0d741252010-01-29 09:44:11 -08003406 lockres->l_blocking = DLM_LOCK_NL;
3407 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3408 spin_unlock_irqrestore(&lockres->l_lock, flags);
3409 goto leave;
3410 }
3411
Mark Fashehccd979b2005-12-15 14:31:24 -08003412 /* if we're blocking an exclusive and we have *any* holders,
3413 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003414 if ((lockres->l_blocking == DLM_LOCK_EX)
Sunil Mushran9b915182010-02-26 19:42:44 -08003415 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3416 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3417 lockres->l_name, lockres->l_ex_holders,
3418 lockres->l_ro_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003419 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003420 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003421
3422 /* If it's a PR we're blocking, then only
3423 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003424 if (lockres->l_blocking == DLM_LOCK_PR &&
Sunil Mushran9b915182010-02-26 19:42:44 -08003425 lockres->l_ex_holders) {
3426 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3427 lockres->l_name, lockres->l_ex_holders);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003428 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003429 }
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003430
3431 /*
3432 * Can we get a lock in this state if the holder counts are
3433 * zero? The meta data unblock code used to check this.
3434 */
3435 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Sunil Mushran9b915182010-02-26 19:42:44 -08003436 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3437 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3438 lockres->l_name);
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003439 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003440 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003441
Mark Fasheh16d5b952006-09-13 21:10:12 -07003442 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3443
3444 if (lockres->l_ops->check_downconvert
Sunil Mushran9b915182010-02-26 19:42:44 -08003445 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3446 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3447 lockres->l_name);
Mark Fasheh16d5b952006-09-13 21:10:12 -07003448 goto leave_requeue;
Sunil Mushran9b915182010-02-26 19:42:44 -08003449 }
Mark Fasheh16d5b952006-09-13 21:10:12 -07003450
Mark Fashehccd979b2005-12-15 14:31:24 -08003451 /* If we get here, then we know that there are no more
3452 * incompatible holders (and anyone asking for an incompatible
3453 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003454 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003455 goto downconvert;
3456
3457 /* Some lockres types want to do a bit of work before
3458 * downconverting a lock. Allow that here. The worker function
3459 * may sleep, so we save off a copy of what we're blocking as
3460 * it may change while we're not holding the spin lock. */
3461 blocking = lockres->l_blocking;
Sunil Mushran079b8052010-02-03 10:16:54 -08003462 level = lockres->l_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003463 spin_unlock_irqrestore(&lockres->l_lock, flags);
3464
Mark Fashehcc567d82006-09-13 21:52:21 -07003465 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003466
Sunil Mushran9b915182010-02-26 19:42:44 -08003467 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3468 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3469 lockres->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -07003470 goto leave;
Sunil Mushran9b915182010-02-26 19:42:44 -08003471 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003472
3473 spin_lock_irqsave(&lockres->l_lock, flags);
Sunil Mushran079b8052010-02-03 10:16:54 -08003474 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003475 /* If this changed underneath us, then we can't drop
3476 * it just yet. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003477 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3478 "Recheck\n", lockres->l_name, blocking,
3479 lockres->l_blocking, level, lockres->l_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003480 goto recheck;
3481 }
3482
3483downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003484 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003485
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003486 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003487 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003488 set_lvb = 1;
3489
3490 /*
3491 * We only set the lvb if the lock has been fully
3492 * refreshed - otherwise we risk setting stale
3493 * data. Otherwise, there's no need to actually clear
3494 * out the lvb here as it's value is still valid.
3495 */
3496 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3497 lockres->l_ops->set_lvb(lockres);
3498 }
3499
Joel Beckerde551242008-02-01 14:45:08 -08003500 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003501 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003502 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3503 gen);
3504
Mark Fashehccd979b2005-12-15 14:31:24 -08003505leave:
Tao Mac1e8d352011-03-07 16:43:21 +08003506 if (ret)
3507 mlog_errno(ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08003508 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003509
3510leave_requeue:
3511 spin_unlock_irqrestore(&lockres->l_lock, flags);
3512 ctl->requeue = 1;
3513
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003514 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003515}
3516
Mark Fashehd680efe2006-09-08 14:14:34 -07003517static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3518 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003519{
3520 struct inode *inode;
3521 struct address_space *mapping;
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003522 struct ocfs2_inode_info *oi;
Mark Fashehccd979b2005-12-15 14:31:24 -08003523
Mark Fashehccd979b2005-12-15 14:31:24 -08003524 inode = ocfs2_lock_res_inode(lockres);
3525 mapping = inode->i_mapping;
3526
Goldwyn Rodrigues5e98d492010-06-28 10:04:32 -05003527 if (S_ISDIR(inode->i_mode)) {
3528 oi = OCFS2_I(inode);
3529 oi->ip_dir_lock_gen++;
3530 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3531 goto out;
3532 }
3533
Mark Fasheh1044e402008-02-28 17:16:03 -08003534 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003535 goto out;
3536
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003537 /*
3538 * We need this before the filemap_fdatawrite() so that it can
3539 * transfer the dirty bit from the PTE to the
3540 * page. Unfortunately this means that even for EX->PR
3541 * downconverts, we'll lose our mappings and have to build
3542 * them up again.
3543 */
3544 unmap_mapping_range(mapping, 0, 0, 0);
3545
Mark Fashehccd979b2005-12-15 14:31:24 -08003546 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003547 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3548 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003549 }
3550 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003551 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003552 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003553 } else {
3554 /* We only need to wait on the I/O if we're not also
3555 * truncating pages because truncate_inode_pages waits
3556 * for us above. We don't truncate pages if we're
3557 * blocking anything < EXMODE because we want to keep
3558 * them around in that case. */
3559 filemap_fdatawait(mapping);
3560 }
3561
Mark Fashehf1f54062007-10-18 15:13:59 -07003562out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003563 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003564}
3565
Tao Maa4338482009-08-18 11:19:29 +08003566static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3567 struct ocfs2_lock_res *lockres,
3568 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003569{
Tao Maa4338482009-08-18 11:19:29 +08003570 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003571
Joel Beckerbd3e7612008-02-01 12:14:57 -08003572 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3573 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003574
3575 if (checkpointed)
3576 return 1;
3577
Tao Maa4338482009-08-18 11:19:29 +08003578 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003579 return 0;
3580}
3581
Tao Maa4338482009-08-18 11:19:29 +08003582static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3583 int new_level)
3584{
3585 struct inode *inode = ocfs2_lock_res_inode(lockres);
3586
3587 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3588}
3589
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003590static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3591{
3592 struct inode *inode = ocfs2_lock_res_inode(lockres);
3593
3594 __ocfs2_stuff_meta_lvb(inode);
3595}
3596
Mark Fashehd680efe2006-09-08 14:14:34 -07003597/*
3598 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003599 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003600 * dlmglue API and push these off to the ocfs2_wq in the future.
3601 */
3602static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3603 struct ocfs2_lock_res *lockres)
3604{
3605 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3606 ocfs2_dentry_lock_put(osb, dl);
3607}
3608
3609/*
3610 * d_delete() matching dentries before the lock downconvert.
3611 *
3612 * At this point, any process waiting to destroy the
3613 * dentry_lock due to last ref count is stopped by the
3614 * OCFS2_LOCK_QUEUED flag.
3615 *
3616 * We have two potential problems
3617 *
3618 * 1) If we do the last reference drop on our dentry_lock (via dput)
3619 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3620 * the downconvert to finish. Instead we take an elevated
3621 * reference and push the drop until after we've completed our
3622 * unblock processing.
3623 *
3624 * 2) There might be another process with a final reference,
3625 * waiting on us to finish processing. If this is the case, we
3626 * detect it and exit out - there's no more dentries anyway.
3627 */
3628static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3629 int blocking)
3630{
3631 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3632 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3633 struct dentry *dentry;
3634 unsigned long flags;
3635 int extra_ref = 0;
3636
3637 /*
3638 * This node is blocking another node from getting a read
3639 * lock. This happens when we've renamed within a
3640 * directory. We've forced the other nodes to d_delete(), but
3641 * we never actually dropped our lock because it's still
3642 * valid. The downconvert code will retain a PR for this node,
3643 * so there's no further work to do.
3644 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003645 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003646 return UNBLOCK_CONTINUE;
3647
3648 /*
3649 * Mark this inode as potentially orphaned. The code in
3650 * ocfs2_delete_inode() will figure out whether it actually
3651 * needs to be freed or not.
3652 */
3653 spin_lock(&oi->ip_lock);
3654 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3655 spin_unlock(&oi->ip_lock);
3656
3657 /*
3658 * Yuck. We need to make sure however that the check of
3659 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3660 * respect to a reference decrement or the setting of that
3661 * flag.
3662 */
3663 spin_lock_irqsave(&lockres->l_lock, flags);
3664 spin_lock(&dentry_attach_lock);
3665 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3666 && dl->dl_count) {
3667 dl->dl_count++;
3668 extra_ref = 1;
3669 }
3670 spin_unlock(&dentry_attach_lock);
3671 spin_unlock_irqrestore(&lockres->l_lock, flags);
3672
3673 mlog(0, "extra_ref = %d\n", extra_ref);
3674
3675 /*
3676 * We have a process waiting on us in ocfs2_dentry_iput(),
3677 * which means we can't have any more outstanding
3678 * aliases. There's no need to do any more work.
3679 */
3680 if (!extra_ref)
3681 return UNBLOCK_CONTINUE;
3682
3683 spin_lock(&dentry_attach_lock);
3684 while (1) {
3685 dentry = ocfs2_find_local_alias(dl->dl_inode,
3686 dl->dl_parent_blkno, 1);
3687 if (!dentry)
3688 break;
3689 spin_unlock(&dentry_attach_lock);
3690
3691 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3692 dentry->d_name.name);
3693
3694 /*
3695 * The following dcache calls may do an
3696 * iput(). Normally we don't want that from the
3697 * downconverting thread, but in this case it's ok
3698 * because the requesting node already has an
3699 * exclusive lock on the inode, so it can't be queued
3700 * for a downconvert.
3701 */
3702 d_delete(dentry);
3703 dput(dentry);
3704
3705 spin_lock(&dentry_attach_lock);
3706 }
3707 spin_unlock(&dentry_attach_lock);
3708
3709 /*
3710 * If we are the last holder of this dentry lock, there is no
3711 * reason to downconvert so skip straight to the unlock.
3712 */
3713 if (dl->dl_count == 1)
3714 return UNBLOCK_STOP_POST;
3715
3716 return UNBLOCK_CONTINUE_POST;
3717}
3718
Tao Ma8dec98e2009-08-18 11:19:58 +08003719static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3720 int new_level)
3721{
3722 struct ocfs2_refcount_tree *tree =
3723 ocfs2_lock_res_refcount_tree(lockres);
3724
3725 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3726}
3727
3728static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3729 int blocking)
3730{
3731 struct ocfs2_refcount_tree *tree =
3732 ocfs2_lock_res_refcount_tree(lockres);
3733
3734 ocfs2_metadata_cache_purge(&tree->rf_ci);
3735
3736 return UNBLOCK_CONTINUE;
3737}
3738
Jan Kara9e33d692008-08-25 19:56:50 +02003739static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3740{
3741 struct ocfs2_qinfo_lvb *lvb;
3742 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3743 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3744 oinfo->dqi_gi.dqi_type);
3745
Mark Fasheha641dc22008-12-24 16:03:48 -08003746 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003747 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3748 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3749 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3750 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3751 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3752 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3753 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
Jan Kara9e33d692008-08-25 19:56:50 +02003754}
3755
3756void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3757{
3758 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3759 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3760 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3761
Jan Kara9e33d692008-08-25 19:56:50 +02003762 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3763 ocfs2_cluster_unlock(osb, lockres, level);
Jan Kara9e33d692008-08-25 19:56:50 +02003764}
3765
3766static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3767{
3768 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3769 oinfo->dqi_gi.dqi_type);
3770 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3771 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003772 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003773 struct ocfs2_global_disk_dqinfo *gdinfo;
3774 int status = 0;
3775
Joel Becker1c520df2009-06-19 15:14:13 -07003776 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3777 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003778 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3779 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3780 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3781 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3782 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3783 oinfo->dqi_gi.dqi_free_entry =
3784 be32_to_cpu(lvb->lvb_free_entry);
3785 } else {
Jan Karaae4f6ef2010-04-28 19:04:29 +02003786 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
3787 oinfo->dqi_giblk, &bh);
Joel Becker85eb8b72008-11-25 15:31:27 +01003788 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003789 mlog_errno(status);
3790 goto bail;
3791 }
3792 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3793 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3794 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3795 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3796 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3797 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3798 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3799 oinfo->dqi_gi.dqi_free_entry =
3800 le32_to_cpu(gdinfo->dqi_free_entry);
3801 brelse(bh);
3802 ocfs2_track_lock_refresh(lockres);
3803 }
3804
3805bail:
3806 return status;
3807}
3808
3809/* Lock quota info, this function expects at least shared lock on the quota file
3810 * so that we can safely refresh quota info from disk. */
3811int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3812{
3813 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3814 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3815 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3816 int status = 0;
3817
Jan Kara9e33d692008-08-25 19:56:50 +02003818 /* On RO devices, locking really isn't needed... */
3819 if (ocfs2_is_hard_readonly(osb)) {
3820 if (ex)
3821 status = -EROFS;
3822 goto bail;
3823 }
3824 if (ocfs2_mount_local(osb))
3825 goto bail;
3826
3827 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3828 if (status < 0) {
3829 mlog_errno(status);
3830 goto bail;
3831 }
3832 if (!ocfs2_should_refresh_lock_res(lockres))
3833 goto bail;
3834 /* OK, we have the lock but we need to refresh the quota info */
3835 status = ocfs2_refresh_qinfo(oinfo);
3836 if (status)
3837 ocfs2_qinfo_unlock(oinfo, ex);
3838 ocfs2_complete_lock_res_refresh(lockres, status);
3839bail:
Jan Kara9e33d692008-08-25 19:56:50 +02003840 return status;
3841}
3842
Tao Ma8dec98e2009-08-18 11:19:58 +08003843int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
3844{
3845 int status;
3846 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3847 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3848 struct ocfs2_super *osb = lockres->l_priv;
3849
3850
3851 if (ocfs2_is_hard_readonly(osb))
3852 return -EROFS;
3853
3854 if (ocfs2_mount_local(osb))
3855 return 0;
3856
3857 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3858 if (status < 0)
3859 mlog_errno(status);
3860
3861 return status;
3862}
3863
3864void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
3865{
3866 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3867 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3868 struct ocfs2_super *osb = lockres->l_priv;
3869
3870 if (!ocfs2_mount_local(osb))
3871 ocfs2_cluster_unlock(osb, lockres, level);
3872}
3873
Adrian Bunk00600052008-01-29 00:11:41 +02003874static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3875 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003876{
3877 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003878 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003879 unsigned long flags;
3880
3881 /* Our reference to the lockres in this function can be
3882 * considered valid until we remove the OCFS2_LOCK_QUEUED
3883 * flag. */
3884
Mark Fashehccd979b2005-12-15 14:31:24 -08003885 BUG_ON(!lockres);
3886 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003887
Sunil Mushran9b915182010-02-26 19:42:44 -08003888 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003889
3890 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003891 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003892 * still be marked with OCFS2_LOCK_FREEING after this check,
3893 * but short circuiting here will still save us some
3894 * performance. */
3895 spin_lock_irqsave(&lockres->l_lock, flags);
3896 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3897 goto unqueue;
3898 spin_unlock_irqrestore(&lockres->l_lock, flags);
3899
Mark Fashehb5e500e2006-09-13 22:01:16 -07003900 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003901 if (status < 0)
3902 mlog_errno(status);
3903
3904 spin_lock_irqsave(&lockres->l_lock, flags);
3905unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003906 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003907 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3908 } else
3909 ocfs2_schedule_blocked_lock(osb, lockres);
3910
Sunil Mushran9b915182010-02-26 19:42:44 -08003911 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003912 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003913 spin_unlock_irqrestore(&lockres->l_lock, flags);
3914
Mark Fashehd680efe2006-09-08 14:14:34 -07003915 if (ctl.unblock_action != UNBLOCK_CONTINUE
3916 && lockres->l_ops->post_unlock)
3917 lockres->l_ops->post_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003918}
3919
3920static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3921 struct ocfs2_lock_res *lockres)
3922{
Mark Fashehccd979b2005-12-15 14:31:24 -08003923 assert_spin_locked(&lockres->l_lock);
3924
3925 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3926 /* Do not schedule a lock for downconvert when it's on
3927 * the way to destruction - any nodes wanting access
3928 * to the resource will get it soon. */
Sunil Mushran9b915182010-02-26 19:42:44 -08003929 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003930 lockres->l_name, lockres->l_flags);
3931 return;
3932 }
3933
3934 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3935
Mark Fasheh34d024f2007-09-24 15:56:19 -07003936 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003937 if (list_empty(&lockres->l_blocked_list)) {
3938 list_add_tail(&lockres->l_blocked_list,
3939 &osb->blocked_lock_list);
3940 osb->blocked_lock_count++;
3941 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003942 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003943}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003944
3945static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3946{
3947 unsigned long processed;
3948 struct ocfs2_lock_res *lockres;
3949
Mark Fasheh34d024f2007-09-24 15:56:19 -07003950 spin_lock(&osb->dc_task_lock);
3951 /* grab this early so we know to try again if a state change and
3952 * wake happens part-way through our work */
3953 osb->dc_work_sequence = osb->dc_wake_sequence;
3954
3955 processed = osb->blocked_lock_count;
3956 while (processed) {
3957 BUG_ON(list_empty(&osb->blocked_lock_list));
3958
3959 lockres = list_entry(osb->blocked_lock_list.next,
3960 struct ocfs2_lock_res, l_blocked_list);
3961 list_del_init(&lockres->l_blocked_list);
3962 osb->blocked_lock_count--;
3963 spin_unlock(&osb->dc_task_lock);
3964
3965 BUG_ON(!processed);
3966 processed--;
3967
3968 ocfs2_process_blocked_lock(osb, lockres);
3969
3970 spin_lock(&osb->dc_task_lock);
3971 }
3972 spin_unlock(&osb->dc_task_lock);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003973}
3974
3975static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3976{
3977 int empty = 0;
3978
3979 spin_lock(&osb->dc_task_lock);
3980 if (list_empty(&osb->blocked_lock_list))
3981 empty = 1;
3982
3983 spin_unlock(&osb->dc_task_lock);
3984 return empty;
3985}
3986
3987static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3988{
3989 int should_wake = 0;
3990
3991 spin_lock(&osb->dc_task_lock);
3992 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3993 should_wake = 1;
3994 spin_unlock(&osb->dc_task_lock);
3995
3996 return should_wake;
3997}
3998
Adrian Bunk200bfae2008-02-17 10:20:38 +02003999static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004000{
4001 int status = 0;
4002 struct ocfs2_super *osb = arg;
4003
4004 /* only quit once we've been asked to stop and there is no more
4005 * work available */
4006 while (!(kthread_should_stop() &&
4007 ocfs2_downconvert_thread_lists_empty(osb))) {
4008
4009 wait_event_interruptible(osb->dc_event,
4010 ocfs2_downconvert_thread_should_wake(osb) ||
4011 kthread_should_stop());
4012
4013 mlog(0, "downconvert_thread: awoken\n");
4014
4015 ocfs2_downconvert_thread_do_work(osb);
4016 }
4017
4018 osb->dc_task = NULL;
4019 return status;
4020}
4021
4022void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4023{
4024 spin_lock(&osb->dc_task_lock);
4025 /* make sure the voting thread gets a swipe at whatever changes
4026 * the caller may have made to the voting state */
4027 osb->dc_wake_sequence++;
4028 spin_unlock(&osb->dc_task_lock);
4029 wake_up(&osb->dc_event);
4030}