blob: bb2fc6993e2a1d43debb7f55533df7f0b94d9940 [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
Mark Fashehccd979b2005-12-15 14:31:24 -0800300static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
301{
302 BUG_ON(!ocfs2_is_inode_lock(lockres));
303
304 return (struct inode *) lockres->l_priv;
305}
306
Mark Fashehd680efe2006-09-08 14:14:34 -0700307static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
308{
309 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
310
311 return (struct ocfs2_dentry_lock *)lockres->l_priv;
312}
313
Jan Kara9e33d692008-08-25 19:56:50 +0200314static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
315{
316 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
317
318 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
319}
320
Tao Ma8dec98e2009-08-18 11:19:58 +0800321static inline struct ocfs2_refcount_tree *
322ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
323{
324 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
325}
326
Mark Fasheh54a7e752006-09-12 21:49:13 -0700327static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
328{
329 if (lockres->l_ops->get_osb)
330 return lockres->l_ops->get_osb(lockres);
331
332 return (struct ocfs2_super *)lockres->l_priv;
333}
334
Mark Fashehccd979b2005-12-15 14:31:24 -0800335static int ocfs2_lock_create(struct ocfs2_super *osb,
336 struct ocfs2_lock_res *lockres,
337 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800338 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800339static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
340 int wanted);
Jan Karacb257972009-06-04 15:26:50 +0200341static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
342 struct ocfs2_lock_res *lockres,
343 int level, unsigned long caller_ip);
344static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
345 struct ocfs2_lock_res *lockres,
346 int level)
347{
348 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
349}
350
Mark Fashehccd979b2005-12-15 14:31:24 -0800351static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
352static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
353static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
354static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
355static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
356 struct ocfs2_lock_res *lockres);
357static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
358 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800359#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
360 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
361 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
362 _err, _func, _lockres->l_name); \
363 else \
364 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
365 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
366 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800367} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700368static int ocfs2_downconvert_thread(void *arg);
369static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
370 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700371static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 struct buffer_head **bh);
373static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
374static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800375static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
376 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800377static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
378 struct ocfs2_lock_res *lockres,
379 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800380 int lvb,
381 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800382static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
383 struct ocfs2_lock_res *lockres);
384static int ocfs2_cancel_convert(struct ocfs2_super *osb,
385 struct ocfs2_lock_res *lockres);
386
Mark Fashehccd979b2005-12-15 14:31:24 -0800387
Mark Fashehccd979b2005-12-15 14:31:24 -0800388static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
389 u64 blkno,
390 u32 generation,
391 char *name)
392{
393 int len;
394
395 mlog_entry_void();
396
397 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
398
Mark Fashehb06970532006-03-03 10:24:33 -0800399 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
400 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
401 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800402
403 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
404
405 mlog(0, "built lock resource with name: %s\n", name);
406
407 mlog_exit_void();
408}
409
Ingo Molnar34af9462006-06-27 02:53:55 -0700410static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800411
412static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
413 struct ocfs2_dlm_debug *dlm_debug)
414{
415 mlog(0, "Add tracking for lockres %s\n", res->l_name);
416
417 spin_lock(&ocfs2_dlm_tracking_lock);
418 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
419 spin_unlock(&ocfs2_dlm_tracking_lock);
420}
421
422static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
423{
424 spin_lock(&ocfs2_dlm_tracking_lock);
425 if (!list_empty(&res->l_debug_list))
426 list_del_init(&res->l_debug_list);
427 spin_unlock(&ocfs2_dlm_tracking_lock);
428}
429
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700430#ifdef CONFIG_OCFS2_FS_STATS
431static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
432{
433 res->l_lock_num_prmode = 0;
434 res->l_lock_num_prmode_failed = 0;
435 res->l_lock_total_prmode = 0;
436 res->l_lock_max_prmode = 0;
437 res->l_lock_num_exmode = 0;
438 res->l_lock_num_exmode_failed = 0;
439 res->l_lock_total_exmode = 0;
440 res->l_lock_max_exmode = 0;
441 res->l_lock_refresh = 0;
442}
443
444static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
445 struct ocfs2_mask_waiter *mw, int ret)
446{
447 unsigned long long *num, *sum;
448 unsigned int *max, *failed;
449 struct timespec ts = current_kernel_time();
450 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
451
452 if (level == LKM_PRMODE) {
453 num = &res->l_lock_num_prmode;
454 sum = &res->l_lock_total_prmode;
455 max = &res->l_lock_max_prmode;
456 failed = &res->l_lock_num_prmode_failed;
457 } else if (level == LKM_EXMODE) {
458 num = &res->l_lock_num_exmode;
459 sum = &res->l_lock_total_exmode;
460 max = &res->l_lock_max_exmode;
461 failed = &res->l_lock_num_exmode_failed;
462 } else
463 return;
464
465 (*num)++;
466 (*sum) += time;
467 if (time > *max)
468 *max = time;
469 if (ret)
470 (*failed)++;
471}
472
473static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
474{
475 lockres->l_lock_refresh++;
476}
477
478static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
479{
480 struct timespec ts = current_kernel_time();
481 mw->mw_lock_start = timespec_to_ns(&ts);
482}
483#else
484static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
485{
486}
487static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
488 int level, struct ocfs2_mask_waiter *mw, int ret)
489{
490}
491static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
492{
493}
494static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
495{
496}
497#endif
498
Mark Fashehccd979b2005-12-15 14:31:24 -0800499static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
500 struct ocfs2_lock_res *res,
501 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800502 struct ocfs2_lock_res_ops *ops,
503 void *priv)
504{
Mark Fashehccd979b2005-12-15 14:31:24 -0800505 res->l_type = type;
506 res->l_ops = ops;
507 res->l_priv = priv;
508
Joel Beckerbd3e7612008-02-01 12:14:57 -0800509 res->l_level = DLM_LOCK_IV;
510 res->l_requested = DLM_LOCK_IV;
511 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800512 res->l_action = OCFS2_AST_INVALID;
513 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
514
515 res->l_flags = OCFS2_LOCK_INITIALIZED;
516
517 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700518
519 ocfs2_init_lock_stats(res);
Jan Karacb257972009-06-04 15:26:50 +0200520#ifdef CONFIG_DEBUG_LOCK_ALLOC
521 if (type != OCFS2_LOCK_TYPE_OPEN)
522 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
523 &lockdep_keys[type], 0);
524 else
525 res->l_lockdep_map.key = NULL;
526#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800527}
528
529void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
530{
531 /* This also clears out the lock status block */
532 memset(res, 0, sizeof(struct ocfs2_lock_res));
533 spin_lock_init(&res->l_lock);
534 init_waitqueue_head(&res->l_event);
535 INIT_LIST_HEAD(&res->l_blocked_list);
536 INIT_LIST_HEAD(&res->l_mask_waiters);
537}
538
539void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
540 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700541 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800542 struct inode *inode)
543{
544 struct ocfs2_lock_res_ops *ops;
545
546 switch(type) {
547 case OCFS2_LOCK_TYPE_RW:
548 ops = &ocfs2_inode_rw_lops;
549 break;
550 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700551 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800552 break;
Tiger Yang50008632007-03-20 16:01:38 -0700553 case OCFS2_LOCK_TYPE_OPEN:
554 ops = &ocfs2_inode_open_lops;
555 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 default:
557 mlog_bug_on_msg(1, "type: %d\n", type);
558 ops = NULL; /* thanks, gcc */
559 break;
560 };
561
Mark Fashehd680efe2006-09-08 14:14:34 -0700562 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700563 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700564 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
565}
566
Mark Fasheh54a7e752006-09-12 21:49:13 -0700567static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
568{
569 struct inode *inode = ocfs2_lock_res_inode(lockres);
570
571 return OCFS2_SB(inode->i_sb);
572}
573
Jan Kara9e33d692008-08-25 19:56:50 +0200574static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
575{
576 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
577
578 return OCFS2_SB(info->dqi_gi.dqi_sb);
579}
580
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800581static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
582{
583 struct ocfs2_file_private *fp = lockres->l_priv;
584
585 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
586}
587
Mark Fashehd680efe2006-09-08 14:14:34 -0700588static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
589{
590 __be64 inode_blkno_be;
591
592 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
593 sizeof(__be64));
594
595 return be64_to_cpu(inode_blkno_be);
596}
597
Mark Fasheh54a7e752006-09-12 21:49:13 -0700598static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
599{
600 struct ocfs2_dentry_lock *dl = lockres->l_priv;
601
602 return OCFS2_SB(dl->dl_inode->i_sb);
603}
604
Mark Fashehd680efe2006-09-08 14:14:34 -0700605void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
606 u64 parent, struct inode *inode)
607{
608 int len;
609 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
610 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
611 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
612
613 ocfs2_lock_res_init_once(lockres);
614
615 /*
616 * Unfortunately, the standard lock naming scheme won't work
617 * here because we have two 16 byte values to use. Instead,
618 * we'll stuff the inode number as a binary value. We still
619 * want error prints to show something without garbling the
620 * display, so drop a null byte in there before the inode
621 * number. A future version of OCFS2 will likely use all
622 * binary lock names. The stringified names have been a
623 * tremendous aid in debugging, but now that the debugfs
624 * interface exists, we can mangle things there if need be.
625 *
626 * NOTE: We also drop the standard "pad" value (the total lock
627 * name size stays the same though - the last part is all
628 * zeros due to the memset in ocfs2_lock_res_init_once()
629 */
630 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
631 "%c%016llx",
632 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
633 (long long)parent);
634
635 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
636
637 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
638 sizeof(__be64));
639
640 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
641 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
642 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800643}
644
645static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
646 struct ocfs2_super *osb)
647{
648 /* Superblock lockres doesn't come from a slab so we call init
649 * once on it manually. */
650 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700651 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
652 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800654 &ocfs2_super_lops, osb);
655}
656
657static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
658 struct ocfs2_super *osb)
659{
660 /* Rename lockres doesn't come from a slab so we call init
661 * once on it manually. */
662 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700663 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
664 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 &ocfs2_rename_lops, osb);
666}
667
wengang wang6ca497a2009-03-06 21:29:10 +0800668static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
669 struct ocfs2_super *osb)
670{
671 /* nfs_sync lockres doesn't come from a slab so we call init
672 * once on it manually. */
673 ocfs2_lock_res_init_once(res);
674 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
675 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
676 &ocfs2_nfs_sync_lops, osb);
677}
678
Srinivas Eeda83273932009-06-03 17:02:55 -0700679static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
680 struct ocfs2_super *osb)
681{
Srinivas Eeda83273932009-06-03 17:02:55 -0700682 ocfs2_lock_res_init_once(res);
683 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
684 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
685 &ocfs2_orphan_scan_lops, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -0700686}
687
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800688void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
689 struct ocfs2_file_private *fp)
690{
691 struct inode *inode = fp->fp_file->f_mapping->host;
692 struct ocfs2_inode_info *oi = OCFS2_I(inode);
693
694 ocfs2_lock_res_init_once(lockres);
695 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
696 inode->i_generation, lockres->l_name);
697 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
698 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
699 fp);
700 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
701}
702
Jan Kara9e33d692008-08-25 19:56:50 +0200703void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
704 struct ocfs2_mem_dqinfo *info)
705{
706 ocfs2_lock_res_init_once(lockres);
707 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
708 0, lockres->l_name);
709 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
710 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
711 info);
712}
713
Tao Ma8dec98e2009-08-18 11:19:58 +0800714void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
715 struct ocfs2_super *osb, u64 ref_blkno,
716 unsigned int generation)
717{
718 ocfs2_lock_res_init_once(lockres);
719 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
720 generation, lockres->l_name);
721 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
722 &ocfs2_refcount_block_lops, osb);
723}
724
Mark Fashehccd979b2005-12-15 14:31:24 -0800725void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
726{
727 mlog_entry_void();
728
729 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
730 return;
731
732 ocfs2_remove_lockres_tracking(res);
733
734 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
735 "Lockres %s is on the blocked list\n",
736 res->l_name);
737 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
738 "Lockres %s has mask waiters pending\n",
739 res->l_name);
740 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
741 "Lockres %s is locked\n",
742 res->l_name);
743 mlog_bug_on_msg(res->l_ro_holders,
744 "Lockres %s has %u ro holders\n",
745 res->l_name, res->l_ro_holders);
746 mlog_bug_on_msg(res->l_ex_holders,
747 "Lockres %s has %u ex holders\n",
748 res->l_name, res->l_ex_holders);
749
750 /* Need to clear out the lock status block for the dlm */
751 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
752
753 res->l_flags = 0UL;
754 mlog_exit_void();
755}
756
757static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
758 int level)
759{
760 mlog_entry_void();
761
762 BUG_ON(!lockres);
763
764 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800765 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800766 lockres->l_ex_holders++;
767 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800768 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800769 lockres->l_ro_holders++;
770 break;
771 default:
772 BUG();
773 }
774
775 mlog_exit_void();
776}
777
778static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
779 int level)
780{
781 mlog_entry_void();
782
783 BUG_ON(!lockres);
784
785 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800786 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800787 BUG_ON(!lockres->l_ex_holders);
788 lockres->l_ex_holders--;
789 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800790 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 BUG_ON(!lockres->l_ro_holders);
792 lockres->l_ro_holders--;
793 break;
794 default:
795 BUG();
796 }
797 mlog_exit_void();
798}
799
800/* WARNING: This function lives in a world where the only three lock
801 * levels are EX, PR, and NL. It *will* have to be adjusted when more
802 * lock types are added. */
803static inline int ocfs2_highest_compat_lock_level(int level)
804{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800805 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800806
Joel Beckerbd3e7612008-02-01 12:14:57 -0800807 if (level == DLM_LOCK_EX)
808 new_level = DLM_LOCK_NL;
809 else if (level == DLM_LOCK_PR)
810 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811 return new_level;
812}
813
814static void lockres_set_flags(struct ocfs2_lock_res *lockres,
815 unsigned long newflags)
816{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200817 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800818
819 assert_spin_locked(&lockres->l_lock);
820
821 lockres->l_flags = newflags;
822
Christoph Hellwig800deef2007-05-17 16:03:13 +0200823 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800824 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
825 continue;
826
827 list_del_init(&mw->mw_item);
828 mw->mw_status = 0;
829 complete(&mw->mw_complete);
830 }
831}
832static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
833{
834 lockres_set_flags(lockres, lockres->l_flags | or);
835}
836static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
837 unsigned long clear)
838{
839 lockres_set_flags(lockres, lockres->l_flags & ~clear);
840}
841
842static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
843{
844 mlog_entry_void();
845
846 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
847 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
848 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800849 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800850
851 lockres->l_level = lockres->l_requested;
852 if (lockres->l_level <=
853 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800854 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800855 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
856 }
857 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
858
859 mlog_exit_void();
860}
861
862static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
863{
864 mlog_entry_void();
865
866 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
867 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
868
869 /* Convert from RO to EX doesn't really need anything as our
870 * information is already up to data. Convert from NL to
871 * *anything* however should mark ourselves as needing an
872 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800873 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700874 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800875 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
876
877 lockres->l_level = lockres->l_requested;
878 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
879
880 mlog_exit_void();
881}
882
883static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
884{
885 mlog_entry_void();
886
Roel Kluin3cf0c502007-10-27 00:20:36 +0200887 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800888 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
889
Joel Beckerbd3e7612008-02-01 12:14:57 -0800890 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700891 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
892 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
894
895 lockres->l_level = lockres->l_requested;
896 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
897 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
898
899 mlog_exit_void();
900}
901
Mark Fashehccd979b2005-12-15 14:31:24 -0800902static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
903 int level)
904{
905 int needs_downconvert = 0;
906 mlog_entry_void();
907
908 assert_spin_locked(&lockres->l_lock);
909
910 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
911
912 if (level > lockres->l_blocking) {
913 /* only schedule a downconvert if we haven't already scheduled
914 * one that goes low enough to satisfy the level we're
915 * blocking. this also catches the case where we get
916 * duplicate BASTs */
917 if (ocfs2_highest_compat_lock_level(level) <
918 ocfs2_highest_compat_lock_level(lockres->l_blocking))
919 needs_downconvert = 1;
920
921 lockres->l_blocking = level;
922 }
923
924 mlog_exit(needs_downconvert);
925 return needs_downconvert;
926}
927
Joel Beckerde551242008-02-01 14:45:08 -0800928/*
929 * OCFS2_LOCK_PENDING and l_pending_gen.
930 *
931 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
932 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
933 * for more details on the race.
934 *
935 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
936 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
937 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
938 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
939 * the caller is going to try to clear PENDING again. If nothing else is
940 * happening, __lockres_clear_pending() sees PENDING is unset and does
941 * nothing.
942 *
943 * But what if another path (eg downconvert thread) has just started a
944 * new locking action? The other path has re-set PENDING. Our path
945 * cannot clear PENDING, because that will re-open the original race
946 * window.
947 *
948 * [Example]
949 *
950 * ocfs2_meta_lock()
951 * ocfs2_cluster_lock()
952 * set BUSY
953 * set PENDING
954 * drop l_lock
955 * ocfs2_dlm_lock()
956 * ocfs2_locking_ast() ocfs2_downconvert_thread()
957 * clear PENDING ocfs2_unblock_lock()
958 * take_l_lock
959 * !BUSY
960 * ocfs2_prepare_downconvert()
961 * set BUSY
962 * set PENDING
963 * drop l_lock
964 * take l_lock
965 * clear PENDING
966 * drop l_lock
967 * <window>
968 * ocfs2_dlm_lock()
969 *
970 * So as you can see, we now have a window where l_lock is not held,
971 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
972 *
973 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
974 * set by ocfs2_prepare_downconvert(). That wasn't nice.
975 *
976 * To solve this we introduce l_pending_gen. A call to
977 * lockres_clear_pending() will only do so when it is passed a generation
978 * number that matches the lockres. lockres_set_pending() will return the
979 * current generation number. When ocfs2_cluster_lock() goes to clear
980 * PENDING, it passes the generation it got from set_pending(). In our
981 * example above, the generation numbers will *not* match. Thus,
982 * ocfs2_cluster_lock() will not clear the PENDING set by
983 * ocfs2_prepare_downconvert().
984 */
985
986/* Unlocked version for ocfs2_locking_ast() */
987static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
988 unsigned int generation,
989 struct ocfs2_super *osb)
990{
991 assert_spin_locked(&lockres->l_lock);
992
993 /*
994 * The ast and locking functions can race us here. The winner
995 * will clear pending, the loser will not.
996 */
997 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
998 (lockres->l_pending_gen != generation))
999 return;
1000
1001 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1002 lockres->l_pending_gen++;
1003
1004 /*
1005 * The downconvert thread may have skipped us because we
1006 * were PENDING. Wake it up.
1007 */
1008 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1009 ocfs2_wake_downconvert_thread(osb);
1010}
1011
1012/* Locked version for callers of ocfs2_dlm_lock() */
1013static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1014 unsigned int generation,
1015 struct ocfs2_super *osb)
1016{
1017 unsigned long flags;
1018
1019 spin_lock_irqsave(&lockres->l_lock, flags);
1020 __lockres_clear_pending(lockres, generation, osb);
1021 spin_unlock_irqrestore(&lockres->l_lock, flags);
1022}
1023
1024static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1025{
1026 assert_spin_locked(&lockres->l_lock);
1027 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1028
1029 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1030
1031 return lockres->l_pending_gen;
1032}
1033
1034
Mark Fashehaa2623a2006-09-12 21:58:23 -07001035static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001036{
Mark Fashehaa2623a2006-09-12 21:58:23 -07001037 struct ocfs2_lock_res *lockres = opaque;
1038 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 int needs_downconvert;
1040 unsigned long flags;
1041
Joel Beckerbd3e7612008-02-01 12:14:57 -08001042 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001043
Mark Fashehaa2623a2006-09-12 21:58:23 -07001044 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1045 lockres->l_name, level, lockres->l_level,
1046 ocfs2_lock_type_string(lockres->l_type));
1047
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001048 /*
1049 * We can skip the bast for locks which don't enable caching -
1050 * they'll be dropped at the earliest possible time anyway.
1051 */
1052 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1053 return;
1054
Mark Fashehccd979b2005-12-15 14:31:24 -08001055 spin_lock_irqsave(&lockres->l_lock, flags);
1056 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1057 if (needs_downconvert)
1058 ocfs2_schedule_blocked_lock(osb, lockres);
1059 spin_unlock_irqrestore(&lockres->l_lock, flags);
1060
Mark Fashehd680efe2006-09-08 14:14:34 -07001061 wake_up(&lockres->l_event);
1062
Mark Fasheh34d024f2007-09-24 15:56:19 -07001063 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001064}
1065
Mark Fashehe92d57d2006-09-12 21:34:35 -07001066static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -08001067{
Mark Fashehe92d57d2006-09-12 21:34:35 -07001068 struct ocfs2_lock_res *lockres = opaque;
Joel Beckerde551242008-02-01 14:45:08 -08001069 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001071 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001072
1073 spin_lock_irqsave(&lockres->l_lock, flags);
1074
David Teigland1693a5c2008-01-30 16:52:53 -08001075 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1076
1077 if (status == -EAGAIN) {
1078 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1079 goto out;
1080 }
1081
1082 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001083 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001084 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 spin_unlock_irqrestore(&lockres->l_lock, flags);
1086 return;
1087 }
1088
1089 switch(lockres->l_action) {
1090 case OCFS2_AST_ATTACH:
1091 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001092 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 break;
1094 case OCFS2_AST_CONVERT:
1095 ocfs2_generic_handle_convert_action(lockres);
1096 break;
1097 case OCFS2_AST_DOWNCONVERT:
1098 ocfs2_generic_handle_downconvert_action(lockres);
1099 break;
1100 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001101 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1102 "lockres flags = 0x%lx, unlock action: %u\n",
1103 lockres->l_name, lockres->l_action, lockres->l_flags,
1104 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001105 BUG();
1106 }
David Teigland1693a5c2008-01-30 16:52:53 -08001107out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 /* set it to something invalid so if we get called again we
1109 * can catch it. */
1110 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001111
Joel Beckerde551242008-02-01 14:45:08 -08001112 /* Did we try to cancel this lock? Clear that state */
1113 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1114 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1115
1116 /*
1117 * We may have beaten the locking functions here. We certainly
1118 * know that dlm_lock() has been called :-)
1119 * Because we can't have two lock calls in flight at once, we
1120 * can use lockres->l_pending_gen.
1121 */
1122 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1123
Mark Fashehccd979b2005-12-15 14:31:24 -08001124 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001125 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001126}
1127
Mark Fashehccd979b2005-12-15 14:31:24 -08001128static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1129 int convert)
1130{
1131 unsigned long flags;
1132
1133 mlog_entry_void();
1134 spin_lock_irqsave(&lockres->l_lock, flags);
1135 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1136 if (convert)
1137 lockres->l_action = OCFS2_AST_INVALID;
1138 else
1139 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1140 spin_unlock_irqrestore(&lockres->l_lock, flags);
1141
1142 wake_up(&lockres->l_event);
1143 mlog_exit_void();
1144}
1145
1146/* Note: If we detect another process working on the lock (i.e.,
1147 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1148 * to do the right thing in that case.
1149 */
1150static int ocfs2_lock_create(struct ocfs2_super *osb,
1151 struct ocfs2_lock_res *lockres,
1152 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001153 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001154{
1155 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001157 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001158
1159 mlog_entry_void();
1160
Joel Beckerbd3e7612008-02-01 12:14:57 -08001161 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 dlm_flags);
1163
1164 spin_lock_irqsave(&lockres->l_lock, flags);
1165 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1166 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1167 spin_unlock_irqrestore(&lockres->l_lock, flags);
1168 goto bail;
1169 }
1170
1171 lockres->l_action = OCFS2_AST_ATTACH;
1172 lockres->l_requested = level;
1173 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001174 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001175 spin_unlock_irqrestore(&lockres->l_lock, flags);
1176
Joel Becker4670c462008-02-01 14:39:35 -08001177 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001178 level,
1179 &lockres->l_lksb,
1180 dlm_flags,
1181 lockres->l_name,
1182 OCFS2_LOCK_ID_MAX_LEN - 1,
1183 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001184 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001185 if (ret) {
1186 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 ocfs2_recover_from_dlm_error(lockres, 1);
1188 }
1189
Joel Becker7431cd72008-02-01 12:15:37 -08001190 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001191
1192bail:
1193 mlog_exit(ret);
1194 return ret;
1195}
1196
1197static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1198 int flag)
1199{
1200 unsigned long flags;
1201 int ret;
1202
1203 spin_lock_irqsave(&lockres->l_lock, flags);
1204 ret = lockres->l_flags & flag;
1205 spin_unlock_irqrestore(&lockres->l_lock, flags);
1206
1207 return ret;
1208}
1209
1210static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1211
1212{
1213 wait_event(lockres->l_event,
1214 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1215}
1216
1217static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1218
1219{
1220 wait_event(lockres->l_event,
1221 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1222}
1223
1224/* predict what lock level we'll be dropping down to on behalf
1225 * of another node, and return true if the currently wanted
1226 * level will be compatible with it. */
1227static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1228 int wanted)
1229{
1230 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1231
1232 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1233}
1234
1235static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1236{
1237 INIT_LIST_HEAD(&mw->mw_item);
1238 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001239 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240}
1241
1242static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1243{
1244 wait_for_completion(&mw->mw_complete);
1245 /* Re-arm the completion in case we want to wait on it again */
1246 INIT_COMPLETION(mw->mw_complete);
1247 return mw->mw_status;
1248}
1249
1250static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1251 struct ocfs2_mask_waiter *mw,
1252 unsigned long mask,
1253 unsigned long goal)
1254{
1255 BUG_ON(!list_empty(&mw->mw_item));
1256
1257 assert_spin_locked(&lockres->l_lock);
1258
1259 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1260 mw->mw_mask = mask;
1261 mw->mw_goal = goal;
1262}
1263
1264/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1265 * if the mask still hadn't reached its goal */
1266static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1267 struct ocfs2_mask_waiter *mw)
1268{
1269 unsigned long flags;
1270 int ret = 0;
1271
1272 spin_lock_irqsave(&lockres->l_lock, flags);
1273 if (!list_empty(&mw->mw_item)) {
1274 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1275 ret = -EBUSY;
1276
1277 list_del_init(&mw->mw_item);
1278 init_completion(&mw->mw_complete);
1279 }
1280 spin_unlock_irqrestore(&lockres->l_lock, flags);
1281
1282 return ret;
1283
1284}
1285
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001286static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1287 struct ocfs2_lock_res *lockres)
1288{
1289 int ret;
1290
1291 ret = wait_for_completion_interruptible(&mw->mw_complete);
1292 if (ret)
1293 lockres_remove_mask_waiter(lockres, mw);
1294 else
1295 ret = mw->mw_status;
1296 /* Re-arm the completion in case we want to wait on it again */
1297 INIT_COMPLETION(mw->mw_complete);
1298 return ret;
1299}
1300
Jan Karacb257972009-06-04 15:26:50 +02001301static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1302 struct ocfs2_lock_res *lockres,
1303 int level,
1304 u32 lkm_flags,
1305 int arg_flags,
1306 int l_subclass,
1307 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001308{
1309 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1311 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1312 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001313 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001314 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001315
1316 mlog_entry_void();
1317
1318 ocfs2_init_mask_waiter(&mw);
1319
Mark Fashehb80fc012006-09-12 22:08:14 -07001320 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001321 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001322
Mark Fashehccd979b2005-12-15 14:31:24 -08001323again:
1324 wait = 0;
1325
1326 if (catch_signals && signal_pending(current)) {
1327 ret = -ERESTARTSYS;
1328 goto out;
1329 }
1330
1331 spin_lock_irqsave(&lockres->l_lock, flags);
1332
1333 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1334 "Cluster lock called on freeing lockres %s! flags "
1335 "0x%lx\n", lockres->l_name, lockres->l_flags);
1336
1337 /* We only compare against the currently granted level
1338 * here. If the lock is blocked waiting on a downconvert,
1339 * we'll get caught below. */
1340 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1341 level > lockres->l_level) {
1342 /* is someone sitting in dlm_lock? If so, wait on
1343 * them. */
1344 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1345 wait = 1;
1346 goto unlock;
1347 }
1348
Mark Fashehccd979b2005-12-15 14:31:24 -08001349 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1350 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1351 /* is the lock is currently blocked on behalf of
1352 * another node */
1353 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1354 wait = 1;
1355 goto unlock;
1356 }
1357
1358 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001359 if (noqueue_attempted > 0) {
1360 ret = -EAGAIN;
1361 goto unlock;
1362 }
1363 if (lkm_flags & DLM_LKF_NOQUEUE)
1364 noqueue_attempted = 1;
1365
Mark Fashehccd979b2005-12-15 14:31:24 -08001366 if (lockres->l_action != OCFS2_AST_INVALID)
1367 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1368 lockres->l_name, lockres->l_action);
1369
Mark Fasheh019d1b22007-10-05 12:09:05 -07001370 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1371 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001372 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001373 } else {
1374 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001375 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001376 }
1377
Mark Fashehccd979b2005-12-15 14:31:24 -08001378 lockres->l_requested = level;
1379 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001380 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 spin_unlock_irqrestore(&lockres->l_lock, flags);
1382
Joel Beckerbd3e7612008-02-01 12:14:57 -08001383 BUG_ON(level == DLM_LOCK_IV);
1384 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001385
1386 mlog(0, "lock %s, convert from %d to level = %d\n",
1387 lockres->l_name, lockres->l_level, level);
1388
1389 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001390 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001391 level,
1392 &lockres->l_lksb,
1393 lkm_flags,
1394 lockres->l_name,
1395 OCFS2_LOCK_ID_MAX_LEN - 1,
1396 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001397 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001398 if (ret) {
1399 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1400 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001401 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001402 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 }
1404 ocfs2_recover_from_dlm_error(lockres, 1);
1405 goto out;
1406 }
1407
Coly Li73ac36e2009-01-07 18:09:16 -08001408 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001409 lockres->l_name);
1410
1411 /* At this point we've gone inside the dlm and need to
1412 * complete our work regardless. */
1413 catch_signals = 0;
1414
1415 /* wait for busy to clear and carry on */
1416 goto again;
1417 }
1418
1419 /* Ok, if we get here then we're good to go. */
1420 ocfs2_inc_holders(lockres, level);
1421
1422 ret = 0;
1423unlock:
1424 spin_unlock_irqrestore(&lockres->l_lock, flags);
1425out:
1426 /*
1427 * This is helping work around a lock inversion between the page lock
1428 * and dlm locks. One path holds the page lock while calling aops
1429 * which block acquiring dlm locks. The voting thread holds dlm
1430 * locks while acquiring page locks while down converting data locks.
1431 * This block is helping an aop path notice the inversion and back
1432 * off to unlock its page lock before trying the dlm lock again.
1433 */
1434 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1435 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1436 wait = 0;
1437 if (lockres_remove_mask_waiter(lockres, &mw))
1438 ret = -EAGAIN;
1439 else
1440 goto again;
1441 }
1442 if (wait) {
1443 ret = ocfs2_wait_for_mask(&mw);
1444 if (ret == 0)
1445 goto again;
1446 mlog_errno(ret);
1447 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001448 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001449
Jan Karacb257972009-06-04 15:26:50 +02001450#ifdef CONFIG_DEBUG_LOCK_ALLOC
1451 if (!ret && lockres->l_lockdep_map.key != NULL) {
1452 if (level == DLM_LOCK_PR)
1453 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1454 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1455 caller_ip);
1456 else
1457 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1458 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1459 caller_ip);
1460 }
1461#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001462 mlog_exit(ret);
1463 return ret;
1464}
1465
Jan Karacb257972009-06-04 15:26:50 +02001466static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1467 struct ocfs2_lock_res *lockres,
1468 int level,
1469 u32 lkm_flags,
1470 int arg_flags)
1471{
1472 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1473 0, _RET_IP_);
1474}
1475
1476
1477static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1478 struct ocfs2_lock_res *lockres,
1479 int level,
1480 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001481{
1482 unsigned long flags;
1483
1484 mlog_entry_void();
1485 spin_lock_irqsave(&lockres->l_lock, flags);
1486 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001487 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001488 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001489#ifdef CONFIG_DEBUG_LOCK_ALLOC
1490 if (lockres->l_lockdep_map.key != NULL)
1491 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1492#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001493 mlog_exit_void();
1494}
1495
Adrian Bunkda661162006-11-20 03:24:28 +01001496static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1497 struct ocfs2_lock_res *lockres,
1498 int ex,
1499 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001500{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001501 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001502 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001503 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001504
1505 spin_lock_irqsave(&lockres->l_lock, flags);
1506 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1507 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1508 spin_unlock_irqrestore(&lockres->l_lock, flags);
1509
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001510 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001511}
1512
1513/* Grants us an EX lock on the data and metadata resources, skipping
1514 * the normal cluster directory lookup. Use this ONLY on newly created
1515 * inodes which other nodes can't possibly see, and which haven't been
1516 * hashed in the inode hash yet. This can give us a good performance
1517 * increase as it'll skip the network broadcast normally associated
1518 * with creating a new lock resource. */
1519int ocfs2_create_new_inode_locks(struct inode *inode)
1520{
1521 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001522 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001523
1524 BUG_ON(!inode);
1525 BUG_ON(!ocfs2_inode_is_new(inode));
1526
1527 mlog_entry_void();
1528
Mark Fashehb06970532006-03-03 10:24:33 -08001529 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001530
1531 /* NOTE: That we don't increment any of the holder counts, nor
1532 * do we add anything to a journal handle. Since this is
1533 * supposed to be a new inode which the cluster doesn't know
1534 * about yet, there is no need to. As far as the LVB handling
1535 * is concerned, this is basically like acquiring an EX lock
1536 * on a resource which has an invalid one -- we'll set it
1537 * valid when we release the EX. */
1538
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001539 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001540 if (ret) {
1541 mlog_errno(ret);
1542 goto bail;
1543 }
1544
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001545 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001546 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001547 * don't use a generation in their lock names.
1548 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001549 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001550 if (ret) {
1551 mlog_errno(ret);
1552 goto bail;
1553 }
1554
Tiger Yang50008632007-03-20 16:01:38 -07001555 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1556 if (ret) {
1557 mlog_errno(ret);
1558 goto bail;
1559 }
1560
Mark Fashehccd979b2005-12-15 14:31:24 -08001561bail:
1562 mlog_exit(ret);
1563 return ret;
1564}
1565
1566int ocfs2_rw_lock(struct inode *inode, int write)
1567{
1568 int status, level;
1569 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001570 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001571
1572 BUG_ON(!inode);
1573
1574 mlog_entry_void();
1575
Mark Fashehb06970532006-03-03 10:24:33 -08001576 mlog(0, "inode %llu take %s RW lock\n",
1577 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001578 write ? "EXMODE" : "PRMODE");
1579
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001580 if (ocfs2_mount_local(osb))
1581 return 0;
1582
Mark Fashehccd979b2005-12-15 14:31:24 -08001583 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1584
Joel Beckerbd3e7612008-02-01 12:14:57 -08001585 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001586
1587 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1588 0);
1589 if (status < 0)
1590 mlog_errno(status);
1591
1592 mlog_exit(status);
1593 return status;
1594}
1595
1596void ocfs2_rw_unlock(struct inode *inode, int write)
1597{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001598 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001599 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001600 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001601
1602 mlog_entry_void();
1603
Mark Fashehb06970532006-03-03 10:24:33 -08001604 mlog(0, "inode %llu drop %s RW lock\n",
1605 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001606 write ? "EXMODE" : "PRMODE");
1607
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001608 if (!ocfs2_mount_local(osb))
1609 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001610
1611 mlog_exit_void();
1612}
1613
Tiger Yang50008632007-03-20 16:01:38 -07001614/*
1615 * ocfs2_open_lock always get PR mode lock.
1616 */
1617int ocfs2_open_lock(struct inode *inode)
1618{
1619 int status = 0;
1620 struct ocfs2_lock_res *lockres;
1621 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1622
1623 BUG_ON(!inode);
1624
1625 mlog_entry_void();
1626
1627 mlog(0, "inode %llu take PRMODE open lock\n",
1628 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1629
1630 if (ocfs2_mount_local(osb))
1631 goto out;
1632
1633 lockres = &OCFS2_I(inode)->ip_open_lockres;
1634
1635 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001636 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001637 if (status < 0)
1638 mlog_errno(status);
1639
1640out:
1641 mlog_exit(status);
1642 return status;
1643}
1644
1645int ocfs2_try_open_lock(struct inode *inode, int write)
1646{
1647 int status = 0, level;
1648 struct ocfs2_lock_res *lockres;
1649 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1650
1651 BUG_ON(!inode);
1652
1653 mlog_entry_void();
1654
1655 mlog(0, "inode %llu try to take %s open lock\n",
1656 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1657 write ? "EXMODE" : "PRMODE");
1658
1659 if (ocfs2_mount_local(osb))
1660 goto out;
1661
1662 lockres = &OCFS2_I(inode)->ip_open_lockres;
1663
Joel Beckerbd3e7612008-02-01 12:14:57 -08001664 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001665
1666 /*
1667 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001668 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001669 * other nodes and the -EAGAIN will indicate to the caller that
1670 * this inode is still in use.
1671 */
1672 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001673 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001674
1675out:
1676 mlog_exit(status);
1677 return status;
1678}
1679
1680/*
1681 * ocfs2_open_unlock unlock PR and EX mode open locks.
1682 */
1683void ocfs2_open_unlock(struct inode *inode)
1684{
1685 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1686 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1687
1688 mlog_entry_void();
1689
1690 mlog(0, "inode %llu drop open lock\n",
1691 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1692
1693 if (ocfs2_mount_local(osb))
1694 goto out;
1695
1696 if(lockres->l_ro_holders)
1697 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001698 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001699 if(lockres->l_ex_holders)
1700 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001701 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001702
1703out:
1704 mlog_exit_void();
1705}
1706
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001707static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1708 int level)
1709{
1710 int ret;
1711 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1712 unsigned long flags;
1713 struct ocfs2_mask_waiter mw;
1714
1715 ocfs2_init_mask_waiter(&mw);
1716
1717retry_cancel:
1718 spin_lock_irqsave(&lockres->l_lock, flags);
1719 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1720 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1721 if (ret) {
1722 spin_unlock_irqrestore(&lockres->l_lock, flags);
1723 ret = ocfs2_cancel_convert(osb, lockres);
1724 if (ret < 0) {
1725 mlog_errno(ret);
1726 goto out;
1727 }
1728 goto retry_cancel;
1729 }
1730 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1731 spin_unlock_irqrestore(&lockres->l_lock, flags);
1732
1733 ocfs2_wait_for_mask(&mw);
1734 goto retry_cancel;
1735 }
1736
1737 ret = -ERESTARTSYS;
1738 /*
1739 * We may still have gotten the lock, in which case there's no
1740 * point to restarting the syscall.
1741 */
1742 if (lockres->l_level == level)
1743 ret = 0;
1744
1745 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1746 lockres->l_flags, lockres->l_level, lockres->l_action);
1747
1748 spin_unlock_irqrestore(&lockres->l_lock, flags);
1749
1750out:
1751 return ret;
1752}
1753
1754/*
1755 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1756 * flock() calls. The locking approach this requires is sufficiently
1757 * different from all other cluster lock types that we implement a
1758 * seperate path to the "low-level" dlm calls. In particular:
1759 *
1760 * - No optimization of lock levels is done - we take at exactly
1761 * what's been requested.
1762 *
1763 * - No lock caching is employed. We immediately downconvert to
1764 * no-lock at unlock time. This also means flock locks never go on
1765 * the blocking list).
1766 *
1767 * - Since userspace can trivially deadlock itself with flock, we make
1768 * sure to allow cancellation of a misbehaving applications flock()
1769 * request.
1770 *
1771 * - Access to any flock lockres doesn't require concurrency, so we
1772 * can simplify the code by requiring the caller to guarantee
1773 * serialization of dlmglue flock calls.
1774 */
1775int ocfs2_file_lock(struct file *file, int ex, int trylock)
1776{
Mark Fashehe988cf12008-07-10 09:25:39 -07001777 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1778 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001779 unsigned long flags;
1780 struct ocfs2_file_private *fp = file->private_data;
1781 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1782 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1783 struct ocfs2_mask_waiter mw;
1784
1785 ocfs2_init_mask_waiter(&mw);
1786
1787 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001788 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001789 mlog(ML_ERROR,
1790 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1791 "level: %u\n", lockres->l_name, lockres->l_flags,
1792 lockres->l_level);
1793 return -EINVAL;
1794 }
1795
1796 spin_lock_irqsave(&lockres->l_lock, flags);
1797 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1798 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1799 spin_unlock_irqrestore(&lockres->l_lock, flags);
1800
1801 /*
1802 * Get the lock at NLMODE to start - that way we
1803 * can cancel the upconvert request if need be.
1804 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001805 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001806 if (ret < 0) {
1807 mlog_errno(ret);
1808 goto out;
1809 }
1810
1811 ret = ocfs2_wait_for_mask(&mw);
1812 if (ret) {
1813 mlog_errno(ret);
1814 goto out;
1815 }
1816 spin_lock_irqsave(&lockres->l_lock, flags);
1817 }
1818
1819 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001820 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001821 lockres->l_requested = level;
1822 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1823
1824 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1825 spin_unlock_irqrestore(&lockres->l_lock, flags);
1826
Joel Becker4670c462008-02-01 14:39:35 -08001827 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001828 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1829 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001830 if (ret) {
1831 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001832 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001833 ret = -EINVAL;
1834 }
1835
1836 ocfs2_recover_from_dlm_error(lockres, 1);
1837 lockres_remove_mask_waiter(lockres, &mw);
1838 goto out;
1839 }
1840
1841 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1842 if (ret == -ERESTARTSYS) {
1843 /*
1844 * Userspace can cause deadlock itself with
1845 * flock(). Current behavior locally is to allow the
1846 * deadlock, but abort the system call if a signal is
1847 * received. We follow this example, otherwise a
1848 * poorly written program could sit in kernel until
1849 * reboot.
1850 *
1851 * Handling this is a bit more complicated for Ocfs2
1852 * though. We can't exit this function with an
1853 * outstanding lock request, so a cancel convert is
1854 * required. We intentionally overwrite 'ret' - if the
1855 * cancel fails and the lock was granted, it's easier
1856 * to just bubble sucess back up to the user.
1857 */
1858 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001859 } else if (!ret && (level > lockres->l_level)) {
1860 /* Trylock failed asynchronously */
1861 BUG_ON(!trylock);
1862 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001863 }
1864
1865out:
1866
1867 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1868 lockres->l_name, ex, trylock, ret);
1869 return ret;
1870}
1871
1872void ocfs2_file_unlock(struct file *file)
1873{
1874 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001875 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001876 unsigned long flags;
1877 struct ocfs2_file_private *fp = file->private_data;
1878 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1879 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1880 struct ocfs2_mask_waiter mw;
1881
1882 ocfs2_init_mask_waiter(&mw);
1883
1884 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1885 return;
1886
Mark Fashehe988cf12008-07-10 09:25:39 -07001887 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001888 return;
1889
1890 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1891 lockres->l_name, lockres->l_flags, lockres->l_level,
1892 lockres->l_action);
1893
1894 spin_lock_irqsave(&lockres->l_lock, flags);
1895 /*
1896 * Fake a blocking ast for the downconvert code.
1897 */
1898 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001899 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001900
Mark Fashehe988cf12008-07-10 09:25:39 -07001901 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001902 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1903 spin_unlock_irqrestore(&lockres->l_lock, flags);
1904
Mark Fashehe988cf12008-07-10 09:25:39 -07001905 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001906 if (ret) {
1907 mlog_errno(ret);
1908 return;
1909 }
1910
1911 ret = ocfs2_wait_for_mask(&mw);
1912 if (ret)
1913 mlog_errno(ret);
1914}
1915
Mark Fasheh34d024f2007-09-24 15:56:19 -07001916static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1917 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001918{
1919 int kick = 0;
1920
1921 mlog_entry_void();
1922
1923 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001924 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001925 * condition. */
1926 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1927 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001928 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001929 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1930 kick = 1;
1931 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001932 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 if (!lockres->l_ex_holders)
1934 kick = 1;
1935 break;
1936 default:
1937 BUG();
1938 }
1939 }
1940
1941 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001942 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001943
1944 mlog_exit_void();
1945}
1946
Mark Fashehccd979b2005-12-15 14:31:24 -08001947#define OCFS2_SEC_BITS 34
1948#define OCFS2_SEC_SHIFT (64 - 34)
1949#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1950
1951/* LVB only has room for 64 bits of time here so we pack it for
1952 * now. */
1953static u64 ocfs2_pack_timespec(struct timespec *spec)
1954{
1955 u64 res;
1956 u64 sec = spec->tv_sec;
1957 u32 nsec = spec->tv_nsec;
1958
1959 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1960
1961 return res;
1962}
1963
1964/* Call this with the lockres locked. I am reasonably sure we don't
1965 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001966 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001967static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1968{
1969 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001970 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001971 struct ocfs2_meta_lvb *lvb;
1972
1973 mlog_entry_void();
1974
Mark Fasheha641dc22008-12-24 16:03:48 -08001975 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001976
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001977 /*
1978 * Invalidate the LVB of a deleted inode - this way other
1979 * nodes are forced to go to disk and discover the new inode
1980 * status.
1981 */
1982 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1983 lvb->lvb_version = 0;
1984 goto out;
1985 }
1986
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001987 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001988 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1989 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1990 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1991 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1992 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1993 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1994 lvb->lvb_iatime_packed =
1995 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1996 lvb->lvb_ictime_packed =
1997 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1998 lvb->lvb_imtime_packed =
1999 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002000 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002001 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002002 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002003
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002004out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002005 mlog_meta_lvb(0, lockres);
2006
2007 mlog_exit_void();
2008}
2009
2010static void ocfs2_unpack_timespec(struct timespec *spec,
2011 u64 packed_time)
2012{
2013 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2014 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2015}
2016
2017static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2018{
2019 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002020 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002021 struct ocfs2_meta_lvb *lvb;
2022
2023 mlog_entry_void();
2024
2025 mlog_meta_lvb(0, lockres);
2026
Mark Fasheha641dc22008-12-24 16:03:48 -08002027 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002028
2029 /* We're safe here without the lockres lock... */
2030 spin_lock(&oi->ip_lock);
2031 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2032 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2033
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002034 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002035 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002036 ocfs2_set_inode_flags(inode);
2037
Mark Fashehccd979b2005-12-15 14:31:24 -08002038 /* fast-symlinks are a special case */
2039 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2040 inode->i_blocks = 0;
2041 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002042 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002043
2044 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2045 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2046 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2047 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2048 ocfs2_unpack_timespec(&inode->i_atime,
2049 be64_to_cpu(lvb->lvb_iatime_packed));
2050 ocfs2_unpack_timespec(&inode->i_mtime,
2051 be64_to_cpu(lvb->lvb_imtime_packed));
2052 ocfs2_unpack_timespec(&inode->i_ctime,
2053 be64_to_cpu(lvb->lvb_ictime_packed));
2054 spin_unlock(&oi->ip_lock);
2055
2056 mlog_exit_void();
2057}
2058
Mark Fashehf9e2d822006-09-12 15:35:49 -07002059static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2060 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002061{
Mark Fasheha641dc22008-12-24 16:03:48 -08002062 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002063
Joel Becker1c520df2009-06-19 15:14:13 -07002064 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2065 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002066 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002067 return 1;
2068 return 0;
2069}
2070
2071/* Determine whether a lock resource needs to be refreshed, and
2072 * arbitrate who gets to refresh it.
2073 *
2074 * 0 means no refresh needed.
2075 *
2076 * > 0 means you need to refresh this and you MUST call
2077 * ocfs2_complete_lock_res_refresh afterwards. */
2078static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2079{
2080 unsigned long flags;
2081 int status = 0;
2082
2083 mlog_entry_void();
2084
2085refresh_check:
2086 spin_lock_irqsave(&lockres->l_lock, flags);
2087 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2088 spin_unlock_irqrestore(&lockres->l_lock, flags);
2089 goto bail;
2090 }
2091
2092 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2093 spin_unlock_irqrestore(&lockres->l_lock, flags);
2094
2095 ocfs2_wait_on_refreshing_lock(lockres);
2096 goto refresh_check;
2097 }
2098
2099 /* Ok, I'll be the one to refresh this lock. */
2100 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2101 spin_unlock_irqrestore(&lockres->l_lock, flags);
2102
2103 status = 1;
2104bail:
2105 mlog_exit(status);
2106 return status;
2107}
2108
2109/* If status is non zero, I'll mark it as not being in refresh
2110 * anymroe, but i won't clear the needs refresh flag. */
2111static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2112 int status)
2113{
2114 unsigned long flags;
2115 mlog_entry_void();
2116
2117 spin_lock_irqsave(&lockres->l_lock, flags);
2118 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2119 if (!status)
2120 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2121 spin_unlock_irqrestore(&lockres->l_lock, flags);
2122
2123 wake_up(&lockres->l_event);
2124
2125 mlog_exit_void();
2126}
2127
2128/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002129static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002130 struct buffer_head **bh)
2131{
2132 int status = 0;
2133 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002134 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002135 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002136 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002137
2138 mlog_entry_void();
2139
Mark Fashehbe9e9862007-04-18 15:22:08 -07002140 if (ocfs2_mount_local(osb))
2141 goto bail;
2142
Mark Fashehccd979b2005-12-15 14:31:24 -08002143 spin_lock(&oi->ip_lock);
2144 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002145 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002147 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002148 spin_unlock(&oi->ip_lock);
2149 status = -ENOENT;
2150 goto bail;
2151 }
2152 spin_unlock(&oi->ip_lock);
2153
Mark Fashehbe9e9862007-04-18 15:22:08 -07002154 if (!ocfs2_should_refresh_lock_res(lockres))
2155 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002156
2157 /* This will discard any caching information we might have had
2158 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002159 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002160
Mark Fasheh83418972007-04-23 18:53:12 -07002161 ocfs2_extent_map_trunc(inode, 0);
2162
Mark Fashehbe9e9862007-04-18 15:22:08 -07002163 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002164 mlog(0, "Trusting LVB on inode %llu\n",
2165 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002166 ocfs2_refresh_inode_from_lvb(inode);
2167 } else {
2168 /* Boo, we have to go to disk. */
2169 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002170 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 if (status < 0) {
2172 mlog_errno(status);
2173 goto bail_refresh;
2174 }
2175 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2176
2177 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002178 * locking an invalid object. ocfs2_read_inode_block()
2179 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002180 *
2181 * We bug on a stale inode here because we checked
2182 * above whether it was wiped from disk. The wiping
2183 * node provides a guarantee that we receive that
2184 * message and can mark the inode before dropping any
2185 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002186 mlog_bug_on_msg(inode->i_generation !=
2187 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002188 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002190 (unsigned long long)oi->ip_blkno,
2191 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002192 inode->i_generation);
2193 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2194 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002195 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2196 (unsigned long long)oi->ip_blkno,
2197 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002198 le32_to_cpu(fe->i_flags));
2199
2200 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002201 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002202 }
2203
2204 status = 0;
2205bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002206 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002207bail:
2208 mlog_exit(status);
2209 return status;
2210}
2211
2212static int ocfs2_assign_bh(struct inode *inode,
2213 struct buffer_head **ret_bh,
2214 struct buffer_head *passed_bh)
2215{
2216 int status;
2217
2218 if (passed_bh) {
2219 /* Ok, the update went to disk for us, use the
2220 * returned bh. */
2221 *ret_bh = passed_bh;
2222 get_bh(*ret_bh);
2223
2224 return 0;
2225 }
2226
Joel Beckerb657c952008-11-13 14:49:11 -08002227 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002228 if (status < 0)
2229 mlog_errno(status);
2230
2231 return status;
2232}
2233
2234/*
2235 * returns < 0 error if the callback will never be called, otherwise
2236 * the result of the lock will be communicated via the callback.
2237 */
Jan Karacb257972009-06-04 15:26:50 +02002238int ocfs2_inode_lock_full_nested(struct inode *inode,
2239 struct buffer_head **ret_bh,
2240 int ex,
2241 int arg_flags,
2242 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002243{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002244 int status, level, acquired;
2245 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002246 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002247 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2248 struct buffer_head *local_bh = NULL;
2249
2250 BUG_ON(!inode);
2251
2252 mlog_entry_void();
2253
Mark Fashehb06970532006-03-03 10:24:33 -08002254 mlog(0, "inode %llu, take %s META lock\n",
2255 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002256 ex ? "EXMODE" : "PRMODE");
2257
2258 status = 0;
2259 acquired = 0;
2260 /* We'll allow faking a readonly metadata lock for
2261 * rodevices. */
2262 if (ocfs2_is_hard_readonly(osb)) {
2263 if (ex)
2264 status = -EROFS;
2265 goto bail;
2266 }
2267
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002268 if (ocfs2_mount_local(osb))
2269 goto local;
2270
Mark Fashehccd979b2005-12-15 14:31:24 -08002271 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002272 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002273
Mark Fashehe63aecb62007-10-18 15:30:42 -07002274 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002275 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002276 dlm_flags = 0;
2277 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002278 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002279
Jan Karacb257972009-06-04 15:26:50 +02002280 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2281 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002282 if (status < 0) {
2283 if (status != -EAGAIN && status != -EIOCBRETRY)
2284 mlog_errno(status);
2285 goto bail;
2286 }
2287
2288 /* Notify the error cleanup path to drop the cluster lock. */
2289 acquired = 1;
2290
2291 /* We wait twice because a node may have died while we were in
2292 * the lower dlm layers. The second time though, we've
2293 * committed to owning this lock so we don't allow signals to
2294 * abort the operation. */
2295 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002296 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002297
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002298local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002299 /*
2300 * We only see this flag if we're being called from
2301 * ocfs2_read_locked_inode(). It means we're locking an inode
2302 * which hasn't been populated yet, so clear the refresh flag
2303 * and let the caller handle it.
2304 */
2305 if (inode->i_state & I_NEW) {
2306 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002307 if (lockres)
2308 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002309 goto bail;
2310 }
2311
Mark Fashehccd979b2005-12-15 14:31:24 -08002312 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002313 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002314 * may or may not read one, depending on what's in the
2315 * LVB. The result of all of this is that we've *only* gone to
2316 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002317 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002318 if (status < 0) {
2319 if (status != -ENOENT)
2320 mlog_errno(status);
2321 goto bail;
2322 }
2323
2324 if (ret_bh) {
2325 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2326 if (status < 0) {
2327 mlog_errno(status);
2328 goto bail;
2329 }
2330 }
2331
Mark Fashehccd979b2005-12-15 14:31:24 -08002332bail:
2333 if (status < 0) {
2334 if (ret_bh && (*ret_bh)) {
2335 brelse(*ret_bh);
2336 *ret_bh = NULL;
2337 }
2338 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002339 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002340 }
2341
2342 if (local_bh)
2343 brelse(local_bh);
2344
2345 mlog_exit(status);
2346 return status;
2347}
2348
2349/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002350 * This is working around a lock inversion between tasks acquiring DLM
2351 * locks while holding a page lock and the downconvert thread which
2352 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002353 *
2354 * ** These _with_page variantes are only intended to be called from aop
2355 * methods that hold page locks and return a very specific *positive* error
2356 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2357 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002358 * The DLM is called such that it returns -EAGAIN if it would have
2359 * blocked waiting for the downconvert thread. In that case we unlock
2360 * our page so the downconvert thread can make progress. Once we've
2361 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2362 * that called us can bubble that back up into the VFS who will then
2363 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 *
2365 * We do a blocking lock and immediate unlock before returning, though, so that
2366 * the lock has a great chance of being cached on this node by the time the VFS
2367 * calls back to retry the aop. This has a potential to livelock as nodes
2368 * ping locks back and forth, but that's a risk we're willing to take to avoid
2369 * the lock inversion simply.
2370 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002371int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002372 struct buffer_head **ret_bh,
2373 int ex,
2374 struct page *page)
2375{
2376 int ret;
2377
Mark Fashehe63aecb62007-10-18 15:30:42 -07002378 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002379 if (ret == -EAGAIN) {
2380 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002381 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2382 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002383 ret = AOP_TRUNCATED_PAGE;
2384 }
2385
2386 return ret;
2387}
2388
Mark Fashehe63aecb62007-10-18 15:30:42 -07002389int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002390 struct vfsmount *vfsmnt,
2391 int *level)
2392{
2393 int ret;
2394
2395 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002396 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002397 if (ret < 0) {
2398 mlog_errno(ret);
2399 return ret;
2400 }
2401
2402 /*
2403 * If we should update atime, we will get EX lock,
2404 * otherwise we just get PR lock.
2405 */
2406 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2407 struct buffer_head *bh = NULL;
2408
Mark Fashehe63aecb62007-10-18 15:30:42 -07002409 ocfs2_inode_unlock(inode, 0);
2410 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002411 if (ret < 0) {
2412 mlog_errno(ret);
2413 return ret;
2414 }
2415 *level = 1;
2416 if (ocfs2_should_update_atime(inode, vfsmnt))
2417 ocfs2_update_inode_atime(inode, bh);
2418 if (bh)
2419 brelse(bh);
2420 } else
2421 *level = 0;
2422
2423 mlog_exit(ret);
2424 return ret;
2425}
2426
Mark Fashehe63aecb62007-10-18 15:30:42 -07002427void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002428 int ex)
2429{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002430 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002431 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002432 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002433
2434 mlog_entry_void();
2435
Mark Fashehb06970532006-03-03 10:24:33 -08002436 mlog(0, "inode %llu drop %s META lock\n",
2437 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002438 ex ? "EXMODE" : "PRMODE");
2439
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002440 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2441 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002442 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2443
2444 mlog_exit_void();
2445}
2446
Sunil Mushrandf152c22009-06-22 11:40:07 -07002447int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002448{
2449 struct ocfs2_lock_res *lockres;
2450 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002451 int status = 0;
2452
Sunil Mushrandf152c22009-06-22 11:40:07 -07002453 if (ocfs2_is_hard_readonly(osb))
2454 return -EROFS;
2455
2456 if (ocfs2_mount_local(osb))
2457 return 0;
2458
Srinivas Eeda83273932009-06-03 17:02:55 -07002459 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002460 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002461 if (status < 0)
2462 return status;
2463
2464 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002465 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2466 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002467 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002468 else
2469 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2470
Srinivas Eeda83273932009-06-03 17:02:55 -07002471 return status;
2472}
2473
Sunil Mushrandf152c22009-06-22 11:40:07 -07002474void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002475{
2476 struct ocfs2_lock_res *lockres;
2477 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002478
Sunil Mushrandf152c22009-06-22 11:40:07 -07002479 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2480 lockres = &osb->osb_orphan_scan.os_lockres;
2481 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2482 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2483 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2484 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2485 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002486}
2487
Mark Fashehccd979b2005-12-15 14:31:24 -08002488int ocfs2_super_lock(struct ocfs2_super *osb,
2489 int ex)
2490{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002491 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002492 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002493 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002494
2495 mlog_entry_void();
2496
2497 if (ocfs2_is_hard_readonly(osb))
2498 return -EROFS;
2499
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002500 if (ocfs2_mount_local(osb))
2501 goto bail;
2502
Mark Fashehccd979b2005-12-15 14:31:24 -08002503 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2504 if (status < 0) {
2505 mlog_errno(status);
2506 goto bail;
2507 }
2508
2509 /* The super block lock path is really in the best position to
2510 * know when resources covered by the lock need to be
2511 * refreshed, so we do it here. Of course, making sense of
2512 * everything is up to the caller :) */
2513 status = ocfs2_should_refresh_lock_res(lockres);
2514 if (status < 0) {
2515 mlog_errno(status);
2516 goto bail;
2517 }
2518 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002519 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002520
2521 ocfs2_complete_lock_res_refresh(lockres, status);
2522
2523 if (status < 0)
2524 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002525 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002526 }
2527bail:
2528 mlog_exit(status);
2529 return status;
2530}
2531
2532void ocfs2_super_unlock(struct ocfs2_super *osb,
2533 int ex)
2534{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002535 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002536 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2537
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002538 if (!ocfs2_mount_local(osb))
2539 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002540}
2541
2542int ocfs2_rename_lock(struct ocfs2_super *osb)
2543{
2544 int status;
2545 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2546
2547 if (ocfs2_is_hard_readonly(osb))
2548 return -EROFS;
2549
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002550 if (ocfs2_mount_local(osb))
2551 return 0;
2552
Joel Beckerbd3e7612008-02-01 12:14:57 -08002553 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002554 if (status < 0)
2555 mlog_errno(status);
2556
2557 return status;
2558}
2559
2560void ocfs2_rename_unlock(struct ocfs2_super *osb)
2561{
2562 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2563
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002564 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002565 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002566}
2567
wengang wang6ca497a2009-03-06 21:29:10 +08002568int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2569{
2570 int status;
2571 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2572
2573 if (ocfs2_is_hard_readonly(osb))
2574 return -EROFS;
2575
2576 if (ocfs2_mount_local(osb))
2577 return 0;
2578
2579 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2580 0, 0);
2581 if (status < 0)
2582 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2583
2584 return status;
2585}
2586
2587void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2588{
2589 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2590
2591 if (!ocfs2_mount_local(osb))
2592 ocfs2_cluster_unlock(osb, lockres,
2593 ex ? LKM_EXMODE : LKM_PRMODE);
2594}
2595
Mark Fashehd680efe2006-09-08 14:14:34 -07002596int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2597{
2598 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002599 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002600 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2601 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2602
2603 BUG_ON(!dl);
2604
2605 if (ocfs2_is_hard_readonly(osb))
2606 return -EROFS;
2607
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002608 if (ocfs2_mount_local(osb))
2609 return 0;
2610
Mark Fashehd680efe2006-09-08 14:14:34 -07002611 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2612 if (ret < 0)
2613 mlog_errno(ret);
2614
2615 return ret;
2616}
2617
2618void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2619{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002620 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002621 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2622 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2623
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002624 if (!ocfs2_mount_local(osb))
2625 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002626}
2627
Mark Fashehccd979b2005-12-15 14:31:24 -08002628/* Reference counting of the dlm debug structure. We want this because
2629 * open references on the debug inodes can live on after a mount, so
2630 * we can't rely on the ocfs2_super to always exist. */
2631static void ocfs2_dlm_debug_free(struct kref *kref)
2632{
2633 struct ocfs2_dlm_debug *dlm_debug;
2634
2635 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2636
2637 kfree(dlm_debug);
2638}
2639
2640void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2641{
2642 if (dlm_debug)
2643 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2644}
2645
2646static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2647{
2648 kref_get(&debug->d_refcnt);
2649}
2650
2651struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2652{
2653 struct ocfs2_dlm_debug *dlm_debug;
2654
2655 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2656 if (!dlm_debug) {
2657 mlog_errno(-ENOMEM);
2658 goto out;
2659 }
2660
2661 kref_init(&dlm_debug->d_refcnt);
2662 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2663 dlm_debug->d_locking_state = NULL;
2664out:
2665 return dlm_debug;
2666}
2667
2668/* Access to this is arbitrated for us via seq_file->sem. */
2669struct ocfs2_dlm_seq_priv {
2670 struct ocfs2_dlm_debug *p_dlm_debug;
2671 struct ocfs2_lock_res p_iter_res;
2672 struct ocfs2_lock_res p_tmp_res;
2673};
2674
2675static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2676 struct ocfs2_dlm_seq_priv *priv)
2677{
2678 struct ocfs2_lock_res *iter, *ret = NULL;
2679 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2680
2681 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2682
2683 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2684 /* discover the head of the list */
2685 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2686 mlog(0, "End of list found, %p\n", ret);
2687 break;
2688 }
2689
2690 /* We track our "dummy" iteration lockres' by a NULL
2691 * l_ops field. */
2692 if (iter->l_ops != NULL) {
2693 ret = iter;
2694 break;
2695 }
2696 }
2697
2698 return ret;
2699}
2700
2701static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2702{
2703 struct ocfs2_dlm_seq_priv *priv = m->private;
2704 struct ocfs2_lock_res *iter;
2705
2706 spin_lock(&ocfs2_dlm_tracking_lock);
2707 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2708 if (iter) {
2709 /* Since lockres' have the lifetime of their container
2710 * (which can be inodes, ocfs2_supers, etc) we want to
2711 * copy this out to a temporary lockres while still
2712 * under the spinlock. Obviously after this we can't
2713 * trust any pointers on the copy returned, but that's
2714 * ok as the information we want isn't typically held
2715 * in them. */
2716 priv->p_tmp_res = *iter;
2717 iter = &priv->p_tmp_res;
2718 }
2719 spin_unlock(&ocfs2_dlm_tracking_lock);
2720
2721 return iter;
2722}
2723
2724static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2725{
2726}
2727
2728static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2729{
2730 struct ocfs2_dlm_seq_priv *priv = m->private;
2731 struct ocfs2_lock_res *iter = v;
2732 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2733
2734 spin_lock(&ocfs2_dlm_tracking_lock);
2735 iter = ocfs2_dlm_next_res(iter, priv);
2736 list_del_init(&dummy->l_debug_list);
2737 if (iter) {
2738 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2739 priv->p_tmp_res = *iter;
2740 iter = &priv->p_tmp_res;
2741 }
2742 spin_unlock(&ocfs2_dlm_tracking_lock);
2743
2744 return iter;
2745}
2746
2747/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002748#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002749static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2750{
2751 int i;
2752 char *lvb;
2753 struct ocfs2_lock_res *lockres = v;
2754
2755 if (!lockres)
2756 return -EINVAL;
2757
Mark Fashehd680efe2006-09-08 14:14:34 -07002758 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2759
2760 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2761 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2762 lockres->l_name,
2763 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2764 else
2765 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2766
2767 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002768 "0x%lx\t"
2769 "0x%x\t"
2770 "0x%x\t"
2771 "%u\t"
2772 "%u\t"
2773 "%d\t"
2774 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002775 lockres->l_level,
2776 lockres->l_flags,
2777 lockres->l_action,
2778 lockres->l_unlock_action,
2779 lockres->l_ro_holders,
2780 lockres->l_ex_holders,
2781 lockres->l_requested,
2782 lockres->l_blocking);
2783
2784 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002785 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002786 for(i = 0; i < DLM_LVB_LEN; i++)
2787 seq_printf(m, "0x%x\t", lvb[i]);
2788
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002789#ifdef CONFIG_OCFS2_FS_STATS
2790# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2791# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2792# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2793# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2794# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2795# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2796# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2797# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2798# define lock_refresh(_l) (_l)->l_lock_refresh
2799#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002800# define lock_num_prmode(_l) (0ULL)
2801# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002802# define lock_num_prmode_failed(_l) (0)
2803# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002804# define lock_total_prmode(_l) (0ULL)
2805# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002806# define lock_max_prmode(_l) (0)
2807# define lock_max_exmode(_l) (0)
2808# define lock_refresh(_l) (0)
2809#endif
2810 /* The following seq_print was added in version 2 of this output */
2811 seq_printf(m, "%llu\t"
2812 "%llu\t"
2813 "%u\t"
2814 "%u\t"
2815 "%llu\t"
2816 "%llu\t"
2817 "%u\t"
2818 "%u\t"
2819 "%u\t",
2820 lock_num_prmode(lockres),
2821 lock_num_exmode(lockres),
2822 lock_num_prmode_failed(lockres),
2823 lock_num_exmode_failed(lockres),
2824 lock_total_prmode(lockres),
2825 lock_total_exmode(lockres),
2826 lock_max_prmode(lockres),
2827 lock_max_exmode(lockres),
2828 lock_refresh(lockres));
2829
Mark Fashehccd979b2005-12-15 14:31:24 -08002830 /* End the line */
2831 seq_printf(m, "\n");
2832 return 0;
2833}
2834
Jan Engelhardt90d99772008-01-22 20:52:20 +01002835static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002836 .start = ocfs2_dlm_seq_start,
2837 .stop = ocfs2_dlm_seq_stop,
2838 .next = ocfs2_dlm_seq_next,
2839 .show = ocfs2_dlm_seq_show,
2840};
2841
2842static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2843{
2844 struct seq_file *seq = (struct seq_file *) file->private_data;
2845 struct ocfs2_dlm_seq_priv *priv = seq->private;
2846 struct ocfs2_lock_res *res = &priv->p_iter_res;
2847
2848 ocfs2_remove_lockres_tracking(res);
2849 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2850 return seq_release_private(inode, file);
2851}
2852
2853static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2854{
2855 int ret;
2856 struct ocfs2_dlm_seq_priv *priv;
2857 struct seq_file *seq;
2858 struct ocfs2_super *osb;
2859
2860 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2861 if (!priv) {
2862 ret = -ENOMEM;
2863 mlog_errno(ret);
2864 goto out;
2865 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002866 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002867 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2868 priv->p_dlm_debug = osb->osb_dlm_debug;
2869 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2870
2871 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2872 if (ret) {
2873 kfree(priv);
2874 mlog_errno(ret);
2875 goto out;
2876 }
2877
2878 seq = (struct seq_file *) file->private_data;
2879 seq->private = priv;
2880
2881 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2882 priv->p_dlm_debug);
2883
2884out:
2885 return ret;
2886}
2887
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002888static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002889 .open = ocfs2_dlm_debug_open,
2890 .release = ocfs2_dlm_debug_release,
2891 .read = seq_read,
2892 .llseek = seq_lseek,
2893};
2894
2895static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2896{
2897 int ret = 0;
2898 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2899
2900 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2901 S_IFREG|S_IRUSR,
2902 osb->osb_debug_root,
2903 osb,
2904 &ocfs2_dlm_debug_fops);
2905 if (!dlm_debug->d_locking_state) {
2906 ret = -EINVAL;
2907 mlog(ML_ERROR,
2908 "Unable to create locking state debugfs file.\n");
2909 goto out;
2910 }
2911
2912 ocfs2_get_dlm_debug(dlm_debug);
2913out:
2914 return ret;
2915}
2916
2917static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2918{
2919 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2920
2921 if (dlm_debug) {
2922 debugfs_remove(dlm_debug->d_locking_state);
2923 ocfs2_put_dlm_debug(dlm_debug);
2924 }
2925}
2926
2927int ocfs2_dlm_init(struct ocfs2_super *osb)
2928{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002929 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002930 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002931
2932 mlog_entry_void();
2933
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002934 if (ocfs2_mount_local(osb)) {
2935 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002936 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002937 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002938
Mark Fashehccd979b2005-12-15 14:31:24 -08002939 status = ocfs2_dlm_init_debug(osb);
2940 if (status < 0) {
2941 mlog_errno(status);
2942 goto bail;
2943 }
2944
Mark Fasheh34d024f2007-09-24 15:56:19 -07002945 /* launch downconvert thread */
2946 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2947 if (IS_ERR(osb->dc_task)) {
2948 status = PTR_ERR(osb->dc_task);
2949 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002950 mlog_errno(status);
2951 goto bail;
2952 }
2953
Mark Fashehccd979b2005-12-15 14:31:24 -08002954 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002955 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2956 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002957 strlen(osb->uuid_str),
2958 ocfs2_do_node_down, osb,
2959 &conn);
2960 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002961 mlog_errno(status);
2962 goto bail;
2963 }
2964
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002965 status = ocfs2_cluster_this_node(&osb->node_num);
2966 if (status < 0) {
2967 mlog_errno(status);
2968 mlog(ML_ERROR,
2969 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002970 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002971 goto bail;
2972 }
2973
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002974local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002975 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2976 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002977 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07002978 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002979
Joel Becker4670c462008-02-01 14:39:35 -08002980 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002981
2982 status = 0;
2983bail:
2984 if (status < 0) {
2985 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002986 if (osb->dc_task)
2987 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002988 }
2989
2990 mlog_exit(status);
2991 return status;
2992}
2993
Joel Becker286eaa92008-02-01 15:03:57 -08002994void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2995 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002996{
2997 mlog_entry_void();
2998
Mark Fashehccd979b2005-12-15 14:31:24 -08002999 ocfs2_drop_osb_locks(osb);
3000
Joel Becker4670c462008-02-01 14:39:35 -08003001 /*
3002 * Now that we have dropped all locks and ocfs2_dismount_volume()
3003 * has disabled recovery, the DLM won't be talking to us. It's
3004 * safe to tear things down before disconnecting the cluster.
3005 */
3006
Mark Fasheh34d024f2007-09-24 15:56:19 -07003007 if (osb->dc_task) {
3008 kthread_stop(osb->dc_task);
3009 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003010 }
3011
3012 ocfs2_lock_res_free(&osb->osb_super_lockres);
3013 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003014 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003015 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003016
Joel Becker286eaa92008-02-01 15:03:57 -08003017 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003018 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003019
3020 ocfs2_dlm_shutdown_debug(osb);
3021
3022 mlog_exit_void();
3023}
3024
Joel Becker7431cd72008-02-01 12:15:37 -08003025static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08003026{
3027 struct ocfs2_lock_res *lockres = opaque;
3028 unsigned long flags;
3029
3030 mlog_entry_void();
3031
3032 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
3033 lockres->l_unlock_action);
3034
3035 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003036 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08003037 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
3038 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08003039 lockres->l_unlock_action);
3040 spin_unlock_irqrestore(&lockres->l_lock, flags);
3041 return;
3042 }
3043
3044 switch(lockres->l_unlock_action) {
3045 case OCFS2_UNLOCK_CANCEL_CONVERT:
3046 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
3047 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08003048 /* Downconvert thread may have requeued this lock, we
3049 * need to wake it. */
3050 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3051 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08003052 break;
3053 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08003054 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08003055 break;
3056 default:
3057 BUG();
3058 }
3059
3060 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08003061 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08003062 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06003063 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08003064
3065 mlog_exit_void();
3066}
3067
Mark Fashehccd979b2005-12-15 14:31:24 -08003068static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003069 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003070{
Joel Becker7431cd72008-02-01 12:15:37 -08003071 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003072 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003073 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003074
3075 /* We didn't get anywhere near actually using this lockres. */
3076 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3077 goto out;
3078
Mark Fashehb80fc012006-09-12 22:08:14 -07003079 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003080 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003081
Mark Fashehccd979b2005-12-15 14:31:24 -08003082 spin_lock_irqsave(&lockres->l_lock, flags);
3083
3084 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3085 "lockres %s, flags 0x%lx\n",
3086 lockres->l_name, lockres->l_flags);
3087
3088 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3089 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3090 "%u, unlock_action = %u\n",
3091 lockres->l_name, lockres->l_flags, lockres->l_action,
3092 lockres->l_unlock_action);
3093
3094 spin_unlock_irqrestore(&lockres->l_lock, flags);
3095
3096 /* XXX: Today we just wait on any busy
3097 * locks... Perhaps we need to cancel converts in the
3098 * future? */
3099 ocfs2_wait_on_busy_lock(lockres);
3100
3101 spin_lock_irqsave(&lockres->l_lock, flags);
3102 }
3103
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003104 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3105 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003106 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003107 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3108 lockres->l_ops->set_lvb(lockres);
3109 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003110
3111 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3112 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3113 lockres->l_name);
3114 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3115 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3116
3117 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3118 spin_unlock_irqrestore(&lockres->l_lock, flags);
3119 goto out;
3120 }
3121
3122 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3123
3124 /* make sure we never get here while waiting for an ast to
3125 * fire. */
3126 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3127
3128 /* is this necessary? */
3129 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3130 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3131 spin_unlock_irqrestore(&lockres->l_lock, flags);
3132
3133 mlog(0, "lock %s\n", lockres->l_name);
3134
Joel Becker4670c462008-02-01 14:39:35 -08003135 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003136 lockres);
3137 if (ret) {
3138 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003139 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003140 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003141 BUG();
3142 }
Coly Li73ac36e2009-01-07 18:09:16 -08003143 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003144 lockres->l_name);
3145
3146 ocfs2_wait_on_busy_lock(lockres);
3147out:
3148 mlog_exit(0);
3149 return 0;
3150}
3151
3152/* Mark the lockres as being dropped. It will no longer be
3153 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003154 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08003155 * it safe to drop.
3156 *
3157 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3158void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3159{
3160 int status;
3161 struct ocfs2_mask_waiter mw;
3162 unsigned long flags;
3163
3164 ocfs2_init_mask_waiter(&mw);
3165
3166 spin_lock_irqsave(&lockres->l_lock, flags);
3167 lockres->l_flags |= OCFS2_LOCK_FREEING;
3168 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3169 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3170 spin_unlock_irqrestore(&lockres->l_lock, flags);
3171
3172 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3173
3174 status = ocfs2_wait_for_mask(&mw);
3175 if (status)
3176 mlog_errno(status);
3177
3178 spin_lock_irqsave(&lockres->l_lock, flags);
3179 }
3180 spin_unlock_irqrestore(&lockres->l_lock, flags);
3181}
3182
Mark Fashehd680efe2006-09-08 14:14:34 -07003183void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3184 struct ocfs2_lock_res *lockres)
3185{
3186 int ret;
3187
3188 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003189 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003190 if (ret)
3191 mlog_errno(ret);
3192}
3193
Mark Fashehccd979b2005-12-15 14:31:24 -08003194static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3195{
Mark Fashehd680efe2006-09-08 14:14:34 -07003196 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3197 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003198 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003199 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003200}
3201
Mark Fashehccd979b2005-12-15 14:31:24 -08003202int ocfs2_drop_inode_locks(struct inode *inode)
3203{
3204 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003205
3206 mlog_entry_void();
3207
3208 /* No need to call ocfs2_mark_lockres_freeing here -
3209 * ocfs2_clear_inode has done it for us. */
3210
3211 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003212 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003213 if (err < 0)
3214 mlog_errno(err);
3215
3216 status = err;
3217
3218 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003219 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003220 if (err < 0)
3221 mlog_errno(err);
3222 if (err < 0 && !status)
3223 status = err;
3224
3225 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003226 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003227 if (err < 0)
3228 mlog_errno(err);
3229 if (err < 0 && !status)
3230 status = err;
3231
3232 mlog_exit(status);
3233 return status;
3234}
3235
Joel Beckerde551242008-02-01 14:45:08 -08003236static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3237 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003238{
3239 assert_spin_locked(&lockres->l_lock);
3240
Joel Beckerbd3e7612008-02-01 12:14:57 -08003241 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003242
3243 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003244 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003245 lockres->l_level, new_level);
3246 BUG();
3247 }
3248
3249 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3250 lockres->l_name, new_level, lockres->l_blocking);
3251
3252 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3253 lockres->l_requested = new_level;
3254 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003255 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003256}
3257
3258static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3259 struct ocfs2_lock_res *lockres,
3260 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003261 int lvb,
3262 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003263{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003264 int ret;
3265 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003266
3267 mlog_entry_void();
3268
3269 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003270 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003271
Joel Becker4670c462008-02-01 14:39:35 -08003272 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003273 new_level,
3274 &lockres->l_lksb,
3275 dlm_flags,
3276 lockres->l_name,
3277 OCFS2_LOCK_ID_MAX_LEN - 1,
3278 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003279 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003280 if (ret) {
3281 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003282 ocfs2_recover_from_dlm_error(lockres, 1);
3283 goto bail;
3284 }
3285
3286 ret = 0;
3287bail:
3288 mlog_exit(ret);
3289 return ret;
3290}
3291
Joel Becker24ef1812008-01-29 17:37:32 -08003292/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003293static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3294 struct ocfs2_lock_res *lockres)
3295{
3296 assert_spin_locked(&lockres->l_lock);
3297
3298 mlog_entry_void();
3299 mlog(0, "lock %s\n", lockres->l_name);
3300
3301 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3302 /* If we're already trying to cancel a lock conversion
3303 * then just drop the spinlock and allow the caller to
3304 * requeue this lock. */
3305
3306 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3307 return 0;
3308 }
3309
3310 /* were we in a convert when we got the bast fire? */
3311 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3312 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3313 /* set things up for the unlockast to know to just
3314 * clear out the ast_action and unset busy, etc. */
3315 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3316
3317 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3318 "lock %s, invalid flags: 0x%lx\n",
3319 lockres->l_name, lockres->l_flags);
3320
3321 return 1;
3322}
3323
3324static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3325 struct ocfs2_lock_res *lockres)
3326{
3327 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003328
3329 mlog_entry_void();
3330 mlog(0, "lock %s\n", lockres->l_name);
3331
Joel Becker4670c462008-02-01 14:39:35 -08003332 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003333 DLM_LKF_CANCEL, lockres);
3334 if (ret) {
3335 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003336 ocfs2_recover_from_dlm_error(lockres, 0);
3337 }
3338
Joel Becker24ef1812008-01-29 17:37:32 -08003339 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003340
3341 mlog_exit(ret);
3342 return ret;
3343}
3344
Mark Fashehb5e500e2006-09-13 22:01:16 -07003345static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3346 struct ocfs2_lock_res *lockres,
3347 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003348{
3349 unsigned long flags;
3350 int blocking;
3351 int new_level;
3352 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003353 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003354 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003355
3356 mlog_entry_void();
3357
3358 spin_lock_irqsave(&lockres->l_lock, flags);
3359
3360 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3361
3362recheck:
3363 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003364 /* XXX
3365 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3366 * exists entirely for one reason - another thread has set
3367 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3368 *
3369 * If we do ocfs2_cancel_convert() before the other thread
3370 * calls dlm_lock(), our cancel will do nothing. We will
3371 * get no ast, and we will have no way of knowing the
3372 * cancel failed. Meanwhile, the other thread will call
3373 * into dlm_lock() and wait...forever.
3374 *
3375 * Why forever? Because another node has asked for the
3376 * lock first; that's why we're here in unblock_lock().
3377 *
3378 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3379 * set, we just requeue the unblock. Only when the other
3380 * thread has called dlm_lock() and cleared PENDING will
3381 * we then cancel their request.
3382 *
3383 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3384 * at the same time they set OCFS2_DLM_BUSY. They must
3385 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3386 */
3387 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3388 goto leave_requeue;
3389
Mark Fashehd680efe2006-09-08 14:14:34 -07003390 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003391 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3392 spin_unlock_irqrestore(&lockres->l_lock, flags);
3393 if (ret) {
3394 ret = ocfs2_cancel_convert(osb, lockres);
3395 if (ret < 0)
3396 mlog_errno(ret);
3397 }
3398 goto leave;
3399 }
3400
3401 /* if we're blocking an exclusive and we have *any* holders,
3402 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003403 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003404 && (lockres->l_ex_holders || lockres->l_ro_holders))
3405 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003406
3407 /* If it's a PR we're blocking, then only
3408 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003409 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003410 lockres->l_ex_holders)
3411 goto leave_requeue;
3412
3413 /*
3414 * Can we get a lock in this state if the holder counts are
3415 * zero? The meta data unblock code used to check this.
3416 */
3417 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3418 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3419 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003420
Mark Fasheh16d5b952006-09-13 21:10:12 -07003421 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3422
3423 if (lockres->l_ops->check_downconvert
3424 && !lockres->l_ops->check_downconvert(lockres, new_level))
3425 goto leave_requeue;
3426
Mark Fashehccd979b2005-12-15 14:31:24 -08003427 /* If we get here, then we know that there are no more
3428 * incompatible holders (and anyone asking for an incompatible
3429 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003430 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003431 goto downconvert;
3432
3433 /* Some lockres types want to do a bit of work before
3434 * downconverting a lock. Allow that here. The worker function
3435 * may sleep, so we save off a copy of what we're blocking as
3436 * it may change while we're not holding the spin lock. */
3437 blocking = lockres->l_blocking;
3438 spin_unlock_irqrestore(&lockres->l_lock, flags);
3439
Mark Fashehcc567d82006-09-13 21:52:21 -07003440 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003441
3442 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3443 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003444
3445 spin_lock_irqsave(&lockres->l_lock, flags);
3446 if (blocking != lockres->l_blocking) {
3447 /* If this changed underneath us, then we can't drop
3448 * it just yet. */
3449 goto recheck;
3450 }
3451
3452downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003453 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003454
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003455 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003456 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003457 set_lvb = 1;
3458
3459 /*
3460 * We only set the lvb if the lock has been fully
3461 * refreshed - otherwise we risk setting stale
3462 * data. Otherwise, there's no need to actually clear
3463 * out the lvb here as it's value is still valid.
3464 */
3465 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3466 lockres->l_ops->set_lvb(lockres);
3467 }
3468
Joel Beckerde551242008-02-01 14:45:08 -08003469 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003470 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003471 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3472 gen);
3473
Mark Fashehccd979b2005-12-15 14:31:24 -08003474leave:
3475 mlog_exit(ret);
3476 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003477
3478leave_requeue:
3479 spin_unlock_irqrestore(&lockres->l_lock, flags);
3480 ctl->requeue = 1;
3481
3482 mlog_exit(0);
3483 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003484}
3485
Mark Fashehd680efe2006-09-08 14:14:34 -07003486static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3487 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003488{
3489 struct inode *inode;
3490 struct address_space *mapping;
3491
Mark Fashehccd979b2005-12-15 14:31:24 -08003492 inode = ocfs2_lock_res_inode(lockres);
3493 mapping = inode->i_mapping;
3494
Mark Fasheh1044e402008-02-28 17:16:03 -08003495 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003496 goto out;
3497
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003498 /*
3499 * We need this before the filemap_fdatawrite() so that it can
3500 * transfer the dirty bit from the PTE to the
3501 * page. Unfortunately this means that even for EX->PR
3502 * downconverts, we'll lose our mappings and have to build
3503 * them up again.
3504 */
3505 unmap_mapping_range(mapping, 0, 0, 0);
3506
Mark Fashehccd979b2005-12-15 14:31:24 -08003507 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003508 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3509 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003510 }
3511 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003512 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003513 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003514 } else {
3515 /* We only need to wait on the I/O if we're not also
3516 * truncating pages because truncate_inode_pages waits
3517 * for us above. We don't truncate pages if we're
3518 * blocking anything < EXMODE because we want to keep
3519 * them around in that case. */
3520 filemap_fdatawait(mapping);
3521 }
3522
Mark Fashehf1f54062007-10-18 15:13:59 -07003523out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003524 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003525}
3526
Tao Maa4338482009-08-18 11:19:29 +08003527static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3528 struct ocfs2_lock_res *lockres,
3529 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003530{
Tao Maa4338482009-08-18 11:19:29 +08003531 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003532
Joel Beckerbd3e7612008-02-01 12:14:57 -08003533 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3534 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003535
3536 if (checkpointed)
3537 return 1;
3538
Tao Maa4338482009-08-18 11:19:29 +08003539 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003540 return 0;
3541}
3542
Tao Maa4338482009-08-18 11:19:29 +08003543static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3544 int new_level)
3545{
3546 struct inode *inode = ocfs2_lock_res_inode(lockres);
3547
3548 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3549}
3550
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003551static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3552{
3553 struct inode *inode = ocfs2_lock_res_inode(lockres);
3554
3555 __ocfs2_stuff_meta_lvb(inode);
3556}
3557
Mark Fashehd680efe2006-09-08 14:14:34 -07003558/*
3559 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003560 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003561 * dlmglue API and push these off to the ocfs2_wq in the future.
3562 */
3563static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3564 struct ocfs2_lock_res *lockres)
3565{
3566 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3567 ocfs2_dentry_lock_put(osb, dl);
3568}
3569
3570/*
3571 * d_delete() matching dentries before the lock downconvert.
3572 *
3573 * At this point, any process waiting to destroy the
3574 * dentry_lock due to last ref count is stopped by the
3575 * OCFS2_LOCK_QUEUED flag.
3576 *
3577 * We have two potential problems
3578 *
3579 * 1) If we do the last reference drop on our dentry_lock (via dput)
3580 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3581 * the downconvert to finish. Instead we take an elevated
3582 * reference and push the drop until after we've completed our
3583 * unblock processing.
3584 *
3585 * 2) There might be another process with a final reference,
3586 * waiting on us to finish processing. If this is the case, we
3587 * detect it and exit out - there's no more dentries anyway.
3588 */
3589static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3590 int blocking)
3591{
3592 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3593 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3594 struct dentry *dentry;
3595 unsigned long flags;
3596 int extra_ref = 0;
3597
3598 /*
3599 * This node is blocking another node from getting a read
3600 * lock. This happens when we've renamed within a
3601 * directory. We've forced the other nodes to d_delete(), but
3602 * we never actually dropped our lock because it's still
3603 * valid. The downconvert code will retain a PR for this node,
3604 * so there's no further work to do.
3605 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003606 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003607 return UNBLOCK_CONTINUE;
3608
3609 /*
3610 * Mark this inode as potentially orphaned. The code in
3611 * ocfs2_delete_inode() will figure out whether it actually
3612 * needs to be freed or not.
3613 */
3614 spin_lock(&oi->ip_lock);
3615 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3616 spin_unlock(&oi->ip_lock);
3617
3618 /*
3619 * Yuck. We need to make sure however that the check of
3620 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3621 * respect to a reference decrement or the setting of that
3622 * flag.
3623 */
3624 spin_lock_irqsave(&lockres->l_lock, flags);
3625 spin_lock(&dentry_attach_lock);
3626 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3627 && dl->dl_count) {
3628 dl->dl_count++;
3629 extra_ref = 1;
3630 }
3631 spin_unlock(&dentry_attach_lock);
3632 spin_unlock_irqrestore(&lockres->l_lock, flags);
3633
3634 mlog(0, "extra_ref = %d\n", extra_ref);
3635
3636 /*
3637 * We have a process waiting on us in ocfs2_dentry_iput(),
3638 * which means we can't have any more outstanding
3639 * aliases. There's no need to do any more work.
3640 */
3641 if (!extra_ref)
3642 return UNBLOCK_CONTINUE;
3643
3644 spin_lock(&dentry_attach_lock);
3645 while (1) {
3646 dentry = ocfs2_find_local_alias(dl->dl_inode,
3647 dl->dl_parent_blkno, 1);
3648 if (!dentry)
3649 break;
3650 spin_unlock(&dentry_attach_lock);
3651
3652 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3653 dentry->d_name.name);
3654
3655 /*
3656 * The following dcache calls may do an
3657 * iput(). Normally we don't want that from the
3658 * downconverting thread, but in this case it's ok
3659 * because the requesting node already has an
3660 * exclusive lock on the inode, so it can't be queued
3661 * for a downconvert.
3662 */
3663 d_delete(dentry);
3664 dput(dentry);
3665
3666 spin_lock(&dentry_attach_lock);
3667 }
3668 spin_unlock(&dentry_attach_lock);
3669
3670 /*
3671 * If we are the last holder of this dentry lock, there is no
3672 * reason to downconvert so skip straight to the unlock.
3673 */
3674 if (dl->dl_count == 1)
3675 return UNBLOCK_STOP_POST;
3676
3677 return UNBLOCK_CONTINUE_POST;
3678}
3679
Tao Ma8dec98e2009-08-18 11:19:58 +08003680static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3681 int new_level)
3682{
3683 struct ocfs2_refcount_tree *tree =
3684 ocfs2_lock_res_refcount_tree(lockres);
3685
3686 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3687}
3688
3689static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3690 int blocking)
3691{
3692 struct ocfs2_refcount_tree *tree =
3693 ocfs2_lock_res_refcount_tree(lockres);
3694
3695 ocfs2_metadata_cache_purge(&tree->rf_ci);
3696
3697 return UNBLOCK_CONTINUE;
3698}
3699
Jan Kara9e33d692008-08-25 19:56:50 +02003700static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3701{
3702 struct ocfs2_qinfo_lvb *lvb;
3703 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3704 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3705 oinfo->dqi_gi.dqi_type);
3706
3707 mlog_entry_void();
3708
Mark Fasheha641dc22008-12-24 16:03:48 -08003709 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003710 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3711 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3712 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3713 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3714 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3715 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3716 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3717
3718 mlog_exit_void();
3719}
3720
3721void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3722{
3723 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3724 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3725 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3726
3727 mlog_entry_void();
3728 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3729 ocfs2_cluster_unlock(osb, lockres, level);
3730 mlog_exit_void();
3731}
3732
3733static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3734{
3735 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3736 oinfo->dqi_gi.dqi_type);
3737 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3738 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003739 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003740 struct ocfs2_global_disk_dqinfo *gdinfo;
3741 int status = 0;
3742
Joel Becker1c520df2009-06-19 15:14:13 -07003743 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3744 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003745 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3746 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3747 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3748 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3749 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3750 oinfo->dqi_gi.dqi_free_entry =
3751 be32_to_cpu(lvb->lvb_free_entry);
3752 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003753 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3754 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003755 mlog_errno(status);
3756 goto bail;
3757 }
3758 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3759 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3760 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3761 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3762 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3763 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3764 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3765 oinfo->dqi_gi.dqi_free_entry =
3766 le32_to_cpu(gdinfo->dqi_free_entry);
3767 brelse(bh);
3768 ocfs2_track_lock_refresh(lockres);
3769 }
3770
3771bail:
3772 return status;
3773}
3774
3775/* Lock quota info, this function expects at least shared lock on the quota file
3776 * so that we can safely refresh quota info from disk. */
3777int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3778{
3779 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3780 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3781 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3782 int status = 0;
3783
3784 mlog_entry_void();
3785
3786 /* On RO devices, locking really isn't needed... */
3787 if (ocfs2_is_hard_readonly(osb)) {
3788 if (ex)
3789 status = -EROFS;
3790 goto bail;
3791 }
3792 if (ocfs2_mount_local(osb))
3793 goto bail;
3794
3795 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3796 if (status < 0) {
3797 mlog_errno(status);
3798 goto bail;
3799 }
3800 if (!ocfs2_should_refresh_lock_res(lockres))
3801 goto bail;
3802 /* OK, we have the lock but we need to refresh the quota info */
3803 status = ocfs2_refresh_qinfo(oinfo);
3804 if (status)
3805 ocfs2_qinfo_unlock(oinfo, ex);
3806 ocfs2_complete_lock_res_refresh(lockres, status);
3807bail:
3808 mlog_exit(status);
3809 return status;
3810}
3811
Tao Ma8dec98e2009-08-18 11:19:58 +08003812int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
3813{
3814 int status;
3815 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3816 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3817 struct ocfs2_super *osb = lockres->l_priv;
3818
3819
3820 if (ocfs2_is_hard_readonly(osb))
3821 return -EROFS;
3822
3823 if (ocfs2_mount_local(osb))
3824 return 0;
3825
3826 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3827 if (status < 0)
3828 mlog_errno(status);
3829
3830 return status;
3831}
3832
3833void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
3834{
3835 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3836 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3837 struct ocfs2_super *osb = lockres->l_priv;
3838
3839 if (!ocfs2_mount_local(osb))
3840 ocfs2_cluster_unlock(osb, lockres, level);
3841}
3842
Joel Becker4670c462008-02-01 14:39:35 -08003843/*
3844 * This is the filesystem locking protocol. It provides the lock handling
3845 * hooks for the underlying DLM. It has a maximum version number.
3846 * The version number allows interoperability with systems running at
3847 * the same major number and an equal or smaller minor number.
3848 *
3849 * Whenever the filesystem does new things with locks (adds or removes a
3850 * lock, orders them differently, does different things underneath a lock),
3851 * the version must be changed. The protocol is negotiated when joining
3852 * the dlm domain. A node may join the domain if its major version is
3853 * identical to all other nodes and its minor version is greater than
3854 * or equal to all other nodes. When its minor version is greater than
3855 * the other nodes, it will run at the minor version specified by the
3856 * other nodes.
3857 *
3858 * If a locking change is made that will not be compatible with older
3859 * versions, the major number must be increased and the minor version set
3860 * to zero. If a change merely adds a behavior that can be disabled when
3861 * speaking to older versions, the minor version must be increased. If a
3862 * change adds a fully backwards compatible change (eg, LVB changes that
3863 * are just ignored by older versions), the version does not need to be
3864 * updated.
3865 */
Joel Becker24ef1812008-01-29 17:37:32 -08003866static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003867 .lp_max_version = {
3868 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3869 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3870 },
Joel Becker24ef1812008-01-29 17:37:32 -08003871 .lp_lock_ast = ocfs2_locking_ast,
3872 .lp_blocking_ast = ocfs2_blocking_ast,
3873 .lp_unlock_ast = ocfs2_unlock_ast,
3874};
3875
Joel Becker63e0c482008-01-30 16:58:36 -08003876void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003877{
Joel Becker63e0c482008-01-30 16:58:36 -08003878 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003879}
3880
Joel Becker24ef1812008-01-29 17:37:32 -08003881
Adrian Bunk00600052008-01-29 00:11:41 +02003882static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3883 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003884{
3885 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003886 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003887 unsigned long flags;
3888
3889 /* Our reference to the lockres in this function can be
3890 * considered valid until we remove the OCFS2_LOCK_QUEUED
3891 * flag. */
3892
3893 mlog_entry_void();
3894
3895 BUG_ON(!lockres);
3896 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003897
3898 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3899
3900 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003901 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003902 * still be marked with OCFS2_LOCK_FREEING after this check,
3903 * but short circuiting here will still save us some
3904 * performance. */
3905 spin_lock_irqsave(&lockres->l_lock, flags);
3906 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3907 goto unqueue;
3908 spin_unlock_irqrestore(&lockres->l_lock, flags);
3909
Mark Fashehb5e500e2006-09-13 22:01:16 -07003910 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003911 if (status < 0)
3912 mlog_errno(status);
3913
3914 spin_lock_irqsave(&lockres->l_lock, flags);
3915unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003916 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003917 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3918 } else
3919 ocfs2_schedule_blocked_lock(osb, lockres);
3920
3921 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003922 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003923 spin_unlock_irqrestore(&lockres->l_lock, flags);
3924
Mark Fashehd680efe2006-09-08 14:14:34 -07003925 if (ctl.unblock_action != UNBLOCK_CONTINUE
3926 && lockres->l_ops->post_unlock)
3927 lockres->l_ops->post_unlock(osb, lockres);
3928
Mark Fashehccd979b2005-12-15 14:31:24 -08003929 mlog_exit_void();
3930}
3931
3932static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3933 struct ocfs2_lock_res *lockres)
3934{
3935 mlog_entry_void();
3936
3937 assert_spin_locked(&lockres->l_lock);
3938
3939 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3940 /* Do not schedule a lock for downconvert when it's on
3941 * the way to destruction - any nodes wanting access
3942 * to the resource will get it soon. */
3943 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3944 lockres->l_name, lockres->l_flags);
3945 return;
3946 }
3947
3948 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3949
Mark Fasheh34d024f2007-09-24 15:56:19 -07003950 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003951 if (list_empty(&lockres->l_blocked_list)) {
3952 list_add_tail(&lockres->l_blocked_list,
3953 &osb->blocked_lock_list);
3954 osb->blocked_lock_count++;
3955 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003956 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003957
3958 mlog_exit_void();
3959}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003960
3961static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3962{
3963 unsigned long processed;
3964 struct ocfs2_lock_res *lockres;
3965
3966 mlog_entry_void();
3967
3968 spin_lock(&osb->dc_task_lock);
3969 /* grab this early so we know to try again if a state change and
3970 * wake happens part-way through our work */
3971 osb->dc_work_sequence = osb->dc_wake_sequence;
3972
3973 processed = osb->blocked_lock_count;
3974 while (processed) {
3975 BUG_ON(list_empty(&osb->blocked_lock_list));
3976
3977 lockres = list_entry(osb->blocked_lock_list.next,
3978 struct ocfs2_lock_res, l_blocked_list);
3979 list_del_init(&lockres->l_blocked_list);
3980 osb->blocked_lock_count--;
3981 spin_unlock(&osb->dc_task_lock);
3982
3983 BUG_ON(!processed);
3984 processed--;
3985
3986 ocfs2_process_blocked_lock(osb, lockres);
3987
3988 spin_lock(&osb->dc_task_lock);
3989 }
3990 spin_unlock(&osb->dc_task_lock);
3991
3992 mlog_exit_void();
3993}
3994
3995static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3996{
3997 int empty = 0;
3998
3999 spin_lock(&osb->dc_task_lock);
4000 if (list_empty(&osb->blocked_lock_list))
4001 empty = 1;
4002
4003 spin_unlock(&osb->dc_task_lock);
4004 return empty;
4005}
4006
4007static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4008{
4009 int should_wake = 0;
4010
4011 spin_lock(&osb->dc_task_lock);
4012 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4013 should_wake = 1;
4014 spin_unlock(&osb->dc_task_lock);
4015
4016 return should_wake;
4017}
4018
Adrian Bunk200bfae2008-02-17 10:20:38 +02004019static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004020{
4021 int status = 0;
4022 struct ocfs2_super *osb = arg;
4023
4024 /* only quit once we've been asked to stop and there is no more
4025 * work available */
4026 while (!(kthread_should_stop() &&
4027 ocfs2_downconvert_thread_lists_empty(osb))) {
4028
4029 wait_event_interruptible(osb->dc_event,
4030 ocfs2_downconvert_thread_should_wake(osb) ||
4031 kthread_should_stop());
4032
4033 mlog(0, "downconvert_thread: awoken\n");
4034
4035 ocfs2_downconvert_thread_do_work(osb);
4036 }
4037
4038 osb->dc_task = NULL;
4039 return status;
4040}
4041
4042void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4043{
4044 spin_lock(&osb->dc_task_lock);
4045 /* make sure the voting thread gets a swipe at whatever changes
4046 * the caller may have made to the voting state */
4047 osb->dc_wake_sequence++;
4048 spin_unlock(&osb->dc_task_lock);
4049 wake_up(&osb->dc_event);
4050}