blob: 0cdf63042b76de1b89fb9fd01ba735322717acc5 [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
Mark Fashehccd979b2005-12-15 14:31:24 -0800910 if (level > lockres->l_blocking) {
911 /* only schedule a downconvert if we haven't already scheduled
912 * one that goes low enough to satisfy the level we're
913 * blocking. this also catches the case where we get
914 * duplicate BASTs */
915 if (ocfs2_highest_compat_lock_level(level) <
916 ocfs2_highest_compat_lock_level(lockres->l_blocking))
917 needs_downconvert = 1;
918
919 lockres->l_blocking = level;
920 }
921
Wengang Wang0b94a902010-01-21 10:50:02 -0800922 if (needs_downconvert)
923 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
924
Mark Fashehccd979b2005-12-15 14:31:24 -0800925 mlog_exit(needs_downconvert);
926 return needs_downconvert;
927}
928
Joel Beckerde551242008-02-01 14:45:08 -0800929/*
930 * OCFS2_LOCK_PENDING and l_pending_gen.
931 *
932 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
933 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
934 * for more details on the race.
935 *
936 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
937 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
938 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
939 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
940 * the caller is going to try to clear PENDING again. If nothing else is
941 * happening, __lockres_clear_pending() sees PENDING is unset and does
942 * nothing.
943 *
944 * But what if another path (eg downconvert thread) has just started a
945 * new locking action? The other path has re-set PENDING. Our path
946 * cannot clear PENDING, because that will re-open the original race
947 * window.
948 *
949 * [Example]
950 *
951 * ocfs2_meta_lock()
952 * ocfs2_cluster_lock()
953 * set BUSY
954 * set PENDING
955 * drop l_lock
956 * ocfs2_dlm_lock()
957 * ocfs2_locking_ast() ocfs2_downconvert_thread()
958 * clear PENDING ocfs2_unblock_lock()
959 * take_l_lock
960 * !BUSY
961 * ocfs2_prepare_downconvert()
962 * set BUSY
963 * set PENDING
964 * drop l_lock
965 * take l_lock
966 * clear PENDING
967 * drop l_lock
968 * <window>
969 * ocfs2_dlm_lock()
970 *
971 * So as you can see, we now have a window where l_lock is not held,
972 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
973 *
974 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
975 * set by ocfs2_prepare_downconvert(). That wasn't nice.
976 *
977 * To solve this we introduce l_pending_gen. A call to
978 * lockres_clear_pending() will only do so when it is passed a generation
979 * number that matches the lockres. lockres_set_pending() will return the
980 * current generation number. When ocfs2_cluster_lock() goes to clear
981 * PENDING, it passes the generation it got from set_pending(). In our
982 * example above, the generation numbers will *not* match. Thus,
983 * ocfs2_cluster_lock() will not clear the PENDING set by
984 * ocfs2_prepare_downconvert().
985 */
986
987/* Unlocked version for ocfs2_locking_ast() */
988static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
989 unsigned int generation,
990 struct ocfs2_super *osb)
991{
992 assert_spin_locked(&lockres->l_lock);
993
994 /*
995 * The ast and locking functions can race us here. The winner
996 * will clear pending, the loser will not.
997 */
998 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
999 (lockres->l_pending_gen != generation))
1000 return;
1001
1002 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1003 lockres->l_pending_gen++;
1004
1005 /*
1006 * The downconvert thread may have skipped us because we
1007 * were PENDING. Wake it up.
1008 */
1009 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1010 ocfs2_wake_downconvert_thread(osb);
1011}
1012
1013/* Locked version for callers of ocfs2_dlm_lock() */
1014static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1015 unsigned int generation,
1016 struct ocfs2_super *osb)
1017{
1018 unsigned long flags;
1019
1020 spin_lock_irqsave(&lockres->l_lock, flags);
1021 __lockres_clear_pending(lockres, generation, osb);
1022 spin_unlock_irqrestore(&lockres->l_lock, flags);
1023}
1024
1025static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1026{
1027 assert_spin_locked(&lockres->l_lock);
1028 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1029
1030 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1031
1032 return lockres->l_pending_gen;
1033}
1034
1035
Mark Fashehaa2623a2006-09-12 21:58:23 -07001036static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001037{
Mark Fashehaa2623a2006-09-12 21:58:23 -07001038 struct ocfs2_lock_res *lockres = opaque;
1039 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 int needs_downconvert;
1041 unsigned long flags;
1042
Joel Beckerbd3e7612008-02-01 12:14:57 -08001043 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001044
Mark Fashehaa2623a2006-09-12 21:58:23 -07001045 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1046 lockres->l_name, level, lockres->l_level,
1047 ocfs2_lock_type_string(lockres->l_type));
1048
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001049 /*
1050 * We can skip the bast for locks which don't enable caching -
1051 * they'll be dropped at the earliest possible time anyway.
1052 */
1053 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1054 return;
1055
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 spin_lock_irqsave(&lockres->l_lock, flags);
1057 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1058 if (needs_downconvert)
1059 ocfs2_schedule_blocked_lock(osb, lockres);
1060 spin_unlock_irqrestore(&lockres->l_lock, flags);
1061
Mark Fashehd680efe2006-09-08 14:14:34 -07001062 wake_up(&lockres->l_event);
1063
Mark Fasheh34d024f2007-09-24 15:56:19 -07001064 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001065}
1066
Mark Fashehe92d57d2006-09-12 21:34:35 -07001067static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -08001068{
Mark Fashehe92d57d2006-09-12 21:34:35 -07001069 struct ocfs2_lock_res *lockres = opaque;
Joel Beckerde551242008-02-01 14:45:08 -08001070 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001072 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001073
1074 spin_lock_irqsave(&lockres->l_lock, flags);
1075
David Teigland1693a5c2008-01-30 16:52:53 -08001076 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1077
1078 if (status == -EAGAIN) {
1079 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1080 goto out;
1081 }
1082
1083 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001084 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001085 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 spin_unlock_irqrestore(&lockres->l_lock, flags);
1087 return;
1088 }
1089
1090 switch(lockres->l_action) {
1091 case OCFS2_AST_ATTACH:
1092 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001093 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 break;
1095 case OCFS2_AST_CONVERT:
1096 ocfs2_generic_handle_convert_action(lockres);
1097 break;
1098 case OCFS2_AST_DOWNCONVERT:
1099 ocfs2_generic_handle_downconvert_action(lockres);
1100 break;
1101 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001102 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1103 "lockres flags = 0x%lx, unlock action: %u\n",
1104 lockres->l_name, lockres->l_action, lockres->l_flags,
1105 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 BUG();
1107 }
David Teigland1693a5c2008-01-30 16:52:53 -08001108out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001109 /* set it to something invalid so if we get called again we
1110 * can catch it. */
1111 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001112
Joel Beckerde551242008-02-01 14:45:08 -08001113 /* Did we try to cancel this lock? Clear that state */
1114 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1115 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1116
1117 /*
1118 * We may have beaten the locking functions here. We certainly
1119 * know that dlm_lock() has been called :-)
1120 * Because we can't have two lock calls in flight at once, we
1121 * can use lockres->l_pending_gen.
1122 */
1123 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1124
Mark Fashehccd979b2005-12-15 14:31:24 -08001125 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001126 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001127}
1128
Mark Fashehccd979b2005-12-15 14:31:24 -08001129static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1130 int convert)
1131{
1132 unsigned long flags;
1133
1134 mlog_entry_void();
1135 spin_lock_irqsave(&lockres->l_lock, flags);
1136 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1137 if (convert)
1138 lockres->l_action = OCFS2_AST_INVALID;
1139 else
1140 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1141 spin_unlock_irqrestore(&lockres->l_lock, flags);
1142
1143 wake_up(&lockres->l_event);
1144 mlog_exit_void();
1145}
1146
1147/* Note: If we detect another process working on the lock (i.e.,
1148 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1149 * to do the right thing in that case.
1150 */
1151static int ocfs2_lock_create(struct ocfs2_super *osb,
1152 struct ocfs2_lock_res *lockres,
1153 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001154 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001155{
1156 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001157 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001158 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001159
1160 mlog_entry_void();
1161
Joel Beckerbd3e7612008-02-01 12:14:57 -08001162 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 dlm_flags);
1164
1165 spin_lock_irqsave(&lockres->l_lock, flags);
1166 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1167 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1168 spin_unlock_irqrestore(&lockres->l_lock, flags);
1169 goto bail;
1170 }
1171
1172 lockres->l_action = OCFS2_AST_ATTACH;
1173 lockres->l_requested = level;
1174 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001175 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001176 spin_unlock_irqrestore(&lockres->l_lock, flags);
1177
Joel Becker4670c462008-02-01 14:39:35 -08001178 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001179 level,
1180 &lockres->l_lksb,
1181 dlm_flags,
1182 lockres->l_name,
1183 OCFS2_LOCK_ID_MAX_LEN - 1,
1184 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001185 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001186 if (ret) {
1187 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188 ocfs2_recover_from_dlm_error(lockres, 1);
1189 }
1190
Joel Becker7431cd72008-02-01 12:15:37 -08001191 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193bail:
1194 mlog_exit(ret);
1195 return ret;
1196}
1197
1198static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1199 int flag)
1200{
1201 unsigned long flags;
1202 int ret;
1203
1204 spin_lock_irqsave(&lockres->l_lock, flags);
1205 ret = lockres->l_flags & flag;
1206 spin_unlock_irqrestore(&lockres->l_lock, flags);
1207
1208 return ret;
1209}
1210
1211static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1212
1213{
1214 wait_event(lockres->l_event,
1215 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1216}
1217
1218static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1219
1220{
1221 wait_event(lockres->l_event,
1222 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1223}
1224
1225/* predict what lock level we'll be dropping down to on behalf
1226 * of another node, and return true if the currently wanted
1227 * level will be compatible with it. */
1228static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1229 int wanted)
1230{
1231 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1232
1233 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1234}
1235
1236static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1237{
1238 INIT_LIST_HEAD(&mw->mw_item);
1239 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001240 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001241}
1242
1243static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1244{
1245 wait_for_completion(&mw->mw_complete);
1246 /* Re-arm the completion in case we want to wait on it again */
1247 INIT_COMPLETION(mw->mw_complete);
1248 return mw->mw_status;
1249}
1250
1251static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1252 struct ocfs2_mask_waiter *mw,
1253 unsigned long mask,
1254 unsigned long goal)
1255{
1256 BUG_ON(!list_empty(&mw->mw_item));
1257
1258 assert_spin_locked(&lockres->l_lock);
1259
1260 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1261 mw->mw_mask = mask;
1262 mw->mw_goal = goal;
1263}
1264
1265/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1266 * if the mask still hadn't reached its goal */
1267static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1268 struct ocfs2_mask_waiter *mw)
1269{
1270 unsigned long flags;
1271 int ret = 0;
1272
1273 spin_lock_irqsave(&lockres->l_lock, flags);
1274 if (!list_empty(&mw->mw_item)) {
1275 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1276 ret = -EBUSY;
1277
1278 list_del_init(&mw->mw_item);
1279 init_completion(&mw->mw_complete);
1280 }
1281 spin_unlock_irqrestore(&lockres->l_lock, flags);
1282
1283 return ret;
1284
1285}
1286
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001287static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1288 struct ocfs2_lock_res *lockres)
1289{
1290 int ret;
1291
1292 ret = wait_for_completion_interruptible(&mw->mw_complete);
1293 if (ret)
1294 lockres_remove_mask_waiter(lockres, mw);
1295 else
1296 ret = mw->mw_status;
1297 /* Re-arm the completion in case we want to wait on it again */
1298 INIT_COMPLETION(mw->mw_complete);
1299 return ret;
1300}
1301
Jan Karacb257972009-06-04 15:26:50 +02001302static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1303 struct ocfs2_lock_res *lockres,
1304 int level,
1305 u32 lkm_flags,
1306 int arg_flags,
1307 int l_subclass,
1308 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001309{
1310 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1312 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1313 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001314 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001315 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001316
1317 mlog_entry_void();
1318
1319 ocfs2_init_mask_waiter(&mw);
1320
Mark Fashehb80fc012006-09-12 22:08:14 -07001321 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001322 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001323
Mark Fashehccd979b2005-12-15 14:31:24 -08001324again:
1325 wait = 0;
1326
1327 if (catch_signals && signal_pending(current)) {
1328 ret = -ERESTARTSYS;
1329 goto out;
1330 }
1331
1332 spin_lock_irqsave(&lockres->l_lock, flags);
1333
1334 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1335 "Cluster lock called on freeing lockres %s! flags "
1336 "0x%lx\n", lockres->l_name, lockres->l_flags);
1337
1338 /* We only compare against the currently granted level
1339 * here. If the lock is blocked waiting on a downconvert,
1340 * we'll get caught below. */
1341 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1342 level > lockres->l_level) {
1343 /* is someone sitting in dlm_lock? If so, wait on
1344 * them. */
1345 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1346 wait = 1;
1347 goto unlock;
1348 }
1349
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1351 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1352 /* is the lock is currently blocked on behalf of
1353 * another node */
1354 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1355 wait = 1;
1356 goto unlock;
1357 }
1358
1359 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001360 if (noqueue_attempted > 0) {
1361 ret = -EAGAIN;
1362 goto unlock;
1363 }
1364 if (lkm_flags & DLM_LKF_NOQUEUE)
1365 noqueue_attempted = 1;
1366
Mark Fashehccd979b2005-12-15 14:31:24 -08001367 if (lockres->l_action != OCFS2_AST_INVALID)
1368 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1369 lockres->l_name, lockres->l_action);
1370
Mark Fasheh019d1b22007-10-05 12:09:05 -07001371 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1372 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001373 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001374 } else {
1375 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001376 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001377 }
1378
Mark Fashehccd979b2005-12-15 14:31:24 -08001379 lockres->l_requested = level;
1380 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001381 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001382 spin_unlock_irqrestore(&lockres->l_lock, flags);
1383
Joel Beckerbd3e7612008-02-01 12:14:57 -08001384 BUG_ON(level == DLM_LOCK_IV);
1385 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001386
1387 mlog(0, "lock %s, convert from %d to level = %d\n",
1388 lockres->l_name, lockres->l_level, level);
1389
1390 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001391 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001392 level,
1393 &lockres->l_lksb,
1394 lkm_flags,
1395 lockres->l_name,
1396 OCFS2_LOCK_ID_MAX_LEN - 1,
1397 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08001398 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001399 if (ret) {
1400 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1401 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001402 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001403 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001404 }
1405 ocfs2_recover_from_dlm_error(lockres, 1);
1406 goto out;
1407 }
1408
Coly Li73ac36e2009-01-07 18:09:16 -08001409 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 lockres->l_name);
1411
1412 /* At this point we've gone inside the dlm and need to
1413 * complete our work regardless. */
1414 catch_signals = 0;
1415
1416 /* wait for busy to clear and carry on */
1417 goto again;
1418 }
1419
1420 /* Ok, if we get here then we're good to go. */
1421 ocfs2_inc_holders(lockres, level);
1422
1423 ret = 0;
1424unlock:
1425 spin_unlock_irqrestore(&lockres->l_lock, flags);
1426out:
1427 /*
1428 * This is helping work around a lock inversion between the page lock
1429 * and dlm locks. One path holds the page lock while calling aops
1430 * which block acquiring dlm locks. The voting thread holds dlm
1431 * locks while acquiring page locks while down converting data locks.
1432 * This block is helping an aop path notice the inversion and back
1433 * off to unlock its page lock before trying the dlm lock again.
1434 */
1435 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1436 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1437 wait = 0;
1438 if (lockres_remove_mask_waiter(lockres, &mw))
1439 ret = -EAGAIN;
1440 else
1441 goto again;
1442 }
1443 if (wait) {
1444 ret = ocfs2_wait_for_mask(&mw);
1445 if (ret == 0)
1446 goto again;
1447 mlog_errno(ret);
1448 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001449 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001450
Jan Karacb257972009-06-04 15:26:50 +02001451#ifdef CONFIG_DEBUG_LOCK_ALLOC
1452 if (!ret && lockres->l_lockdep_map.key != NULL) {
1453 if (level == DLM_LOCK_PR)
1454 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1455 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1456 caller_ip);
1457 else
1458 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1459 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1460 caller_ip);
1461 }
1462#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001463 mlog_exit(ret);
1464 return ret;
1465}
1466
Jan Karacb257972009-06-04 15:26:50 +02001467static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1468 struct ocfs2_lock_res *lockres,
1469 int level,
1470 u32 lkm_flags,
1471 int arg_flags)
1472{
1473 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1474 0, _RET_IP_);
1475}
1476
1477
1478static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1479 struct ocfs2_lock_res *lockres,
1480 int level,
1481 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001482{
1483 unsigned long flags;
1484
1485 mlog_entry_void();
1486 spin_lock_irqsave(&lockres->l_lock, flags);
1487 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001488 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001489 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001490#ifdef CONFIG_DEBUG_LOCK_ALLOC
1491 if (lockres->l_lockdep_map.key != NULL)
1492 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1493#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 mlog_exit_void();
1495}
1496
Adrian Bunkda661162006-11-20 03:24:28 +01001497static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1498 struct ocfs2_lock_res *lockres,
1499 int ex,
1500 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001501{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001502 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001503 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001504 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001505
1506 spin_lock_irqsave(&lockres->l_lock, flags);
1507 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1508 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1509 spin_unlock_irqrestore(&lockres->l_lock, flags);
1510
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001511 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001512}
1513
1514/* Grants us an EX lock on the data and metadata resources, skipping
1515 * the normal cluster directory lookup. Use this ONLY on newly created
1516 * inodes which other nodes can't possibly see, and which haven't been
1517 * hashed in the inode hash yet. This can give us a good performance
1518 * increase as it'll skip the network broadcast normally associated
1519 * with creating a new lock resource. */
1520int ocfs2_create_new_inode_locks(struct inode *inode)
1521{
1522 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001523 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001524
1525 BUG_ON(!inode);
1526 BUG_ON(!ocfs2_inode_is_new(inode));
1527
1528 mlog_entry_void();
1529
Mark Fashehb06970532006-03-03 10:24:33 -08001530 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001531
1532 /* NOTE: That we don't increment any of the holder counts, nor
1533 * do we add anything to a journal handle. Since this is
1534 * supposed to be a new inode which the cluster doesn't know
1535 * about yet, there is no need to. As far as the LVB handling
1536 * is concerned, this is basically like acquiring an EX lock
1537 * on a resource which has an invalid one -- we'll set it
1538 * valid when we release the EX. */
1539
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001540 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001541 if (ret) {
1542 mlog_errno(ret);
1543 goto bail;
1544 }
1545
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001546 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001547 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001548 * don't use a generation in their lock names.
1549 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001550 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001551 if (ret) {
1552 mlog_errno(ret);
1553 goto bail;
1554 }
1555
Tiger Yang50008632007-03-20 16:01:38 -07001556 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1557 if (ret) {
1558 mlog_errno(ret);
1559 goto bail;
1560 }
1561
Mark Fashehccd979b2005-12-15 14:31:24 -08001562bail:
1563 mlog_exit(ret);
1564 return ret;
1565}
1566
1567int ocfs2_rw_lock(struct inode *inode, int write)
1568{
1569 int status, level;
1570 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001571 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001572
1573 BUG_ON(!inode);
1574
1575 mlog_entry_void();
1576
Mark Fashehb06970532006-03-03 10:24:33 -08001577 mlog(0, "inode %llu take %s RW lock\n",
1578 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001579 write ? "EXMODE" : "PRMODE");
1580
Coly Lid92bc512009-08-28 19:03:18 +08001581 if (ocfs2_mount_local(osb)) {
1582 mlog_exit(0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001583 return 0;
Coly Lid92bc512009-08-28 19:03:18 +08001584 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001585
Mark Fashehccd979b2005-12-15 14:31:24 -08001586 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1587
Joel Beckerbd3e7612008-02-01 12:14:57 -08001588 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001589
1590 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1591 0);
1592 if (status < 0)
1593 mlog_errno(status);
1594
1595 mlog_exit(status);
1596 return status;
1597}
1598
1599void ocfs2_rw_unlock(struct inode *inode, int write)
1600{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001601 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001602 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001603 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001604
1605 mlog_entry_void();
1606
Mark Fashehb06970532006-03-03 10:24:33 -08001607 mlog(0, "inode %llu drop %s RW lock\n",
1608 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001609 write ? "EXMODE" : "PRMODE");
1610
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001611 if (!ocfs2_mount_local(osb))
1612 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613
1614 mlog_exit_void();
1615}
1616
Tiger Yang50008632007-03-20 16:01:38 -07001617/*
1618 * ocfs2_open_lock always get PR mode lock.
1619 */
1620int ocfs2_open_lock(struct inode *inode)
1621{
1622 int status = 0;
1623 struct ocfs2_lock_res *lockres;
1624 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1625
1626 BUG_ON(!inode);
1627
1628 mlog_entry_void();
1629
1630 mlog(0, "inode %llu take PRMODE open lock\n",
1631 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1632
1633 if (ocfs2_mount_local(osb))
1634 goto out;
1635
1636 lockres = &OCFS2_I(inode)->ip_open_lockres;
1637
1638 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001639 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001640 if (status < 0)
1641 mlog_errno(status);
1642
1643out:
1644 mlog_exit(status);
1645 return status;
1646}
1647
1648int ocfs2_try_open_lock(struct inode *inode, int write)
1649{
1650 int status = 0, level;
1651 struct ocfs2_lock_res *lockres;
1652 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1653
1654 BUG_ON(!inode);
1655
1656 mlog_entry_void();
1657
1658 mlog(0, "inode %llu try to take %s open lock\n",
1659 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1660 write ? "EXMODE" : "PRMODE");
1661
1662 if (ocfs2_mount_local(osb))
1663 goto out;
1664
1665 lockres = &OCFS2_I(inode)->ip_open_lockres;
1666
Joel Beckerbd3e7612008-02-01 12:14:57 -08001667 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001668
1669 /*
1670 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001671 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001672 * other nodes and the -EAGAIN will indicate to the caller that
1673 * this inode is still in use.
1674 */
1675 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001676 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001677
1678out:
1679 mlog_exit(status);
1680 return status;
1681}
1682
1683/*
1684 * ocfs2_open_unlock unlock PR and EX mode open locks.
1685 */
1686void ocfs2_open_unlock(struct inode *inode)
1687{
1688 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1689 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1690
1691 mlog_entry_void();
1692
1693 mlog(0, "inode %llu drop open lock\n",
1694 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1695
1696 if (ocfs2_mount_local(osb))
1697 goto out;
1698
1699 if(lockres->l_ro_holders)
1700 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001701 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001702 if(lockres->l_ex_holders)
1703 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001704 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001705
1706out:
1707 mlog_exit_void();
1708}
1709
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001710static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1711 int level)
1712{
1713 int ret;
1714 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1715 unsigned long flags;
1716 struct ocfs2_mask_waiter mw;
1717
1718 ocfs2_init_mask_waiter(&mw);
1719
1720retry_cancel:
1721 spin_lock_irqsave(&lockres->l_lock, flags);
1722 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1723 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1724 if (ret) {
1725 spin_unlock_irqrestore(&lockres->l_lock, flags);
1726 ret = ocfs2_cancel_convert(osb, lockres);
1727 if (ret < 0) {
1728 mlog_errno(ret);
1729 goto out;
1730 }
1731 goto retry_cancel;
1732 }
1733 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1734 spin_unlock_irqrestore(&lockres->l_lock, flags);
1735
1736 ocfs2_wait_for_mask(&mw);
1737 goto retry_cancel;
1738 }
1739
1740 ret = -ERESTARTSYS;
1741 /*
1742 * We may still have gotten the lock, in which case there's no
1743 * point to restarting the syscall.
1744 */
1745 if (lockres->l_level == level)
1746 ret = 0;
1747
1748 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1749 lockres->l_flags, lockres->l_level, lockres->l_action);
1750
1751 spin_unlock_irqrestore(&lockres->l_lock, flags);
1752
1753out:
1754 return ret;
1755}
1756
1757/*
1758 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1759 * flock() calls. The locking approach this requires is sufficiently
1760 * different from all other cluster lock types that we implement a
1761 * seperate path to the "low-level" dlm calls. In particular:
1762 *
1763 * - No optimization of lock levels is done - we take at exactly
1764 * what's been requested.
1765 *
1766 * - No lock caching is employed. We immediately downconvert to
1767 * no-lock at unlock time. This also means flock locks never go on
1768 * the blocking list).
1769 *
1770 * - Since userspace can trivially deadlock itself with flock, we make
1771 * sure to allow cancellation of a misbehaving applications flock()
1772 * request.
1773 *
1774 * - Access to any flock lockres doesn't require concurrency, so we
1775 * can simplify the code by requiring the caller to guarantee
1776 * serialization of dlmglue flock calls.
1777 */
1778int ocfs2_file_lock(struct file *file, int ex, int trylock)
1779{
Mark Fashehe988cf12008-07-10 09:25:39 -07001780 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1781 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001782 unsigned long flags;
1783 struct ocfs2_file_private *fp = file->private_data;
1784 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1785 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1786 struct ocfs2_mask_waiter mw;
1787
1788 ocfs2_init_mask_waiter(&mw);
1789
1790 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001791 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001792 mlog(ML_ERROR,
1793 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1794 "level: %u\n", lockres->l_name, lockres->l_flags,
1795 lockres->l_level);
1796 return -EINVAL;
1797 }
1798
1799 spin_lock_irqsave(&lockres->l_lock, flags);
1800 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1801 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1802 spin_unlock_irqrestore(&lockres->l_lock, flags);
1803
1804 /*
1805 * Get the lock at NLMODE to start - that way we
1806 * can cancel the upconvert request if need be.
1807 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001808 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001809 if (ret < 0) {
1810 mlog_errno(ret);
1811 goto out;
1812 }
1813
1814 ret = ocfs2_wait_for_mask(&mw);
1815 if (ret) {
1816 mlog_errno(ret);
1817 goto out;
1818 }
1819 spin_lock_irqsave(&lockres->l_lock, flags);
1820 }
1821
1822 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001823 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001824 lockres->l_requested = level;
1825 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1826
1827 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1828 spin_unlock_irqrestore(&lockres->l_lock, flags);
1829
Joel Becker4670c462008-02-01 14:39:35 -08001830 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001831 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1832 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001833 if (ret) {
1834 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001835 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001836 ret = -EINVAL;
1837 }
1838
1839 ocfs2_recover_from_dlm_error(lockres, 1);
1840 lockres_remove_mask_waiter(lockres, &mw);
1841 goto out;
1842 }
1843
1844 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1845 if (ret == -ERESTARTSYS) {
1846 /*
1847 * Userspace can cause deadlock itself with
1848 * flock(). Current behavior locally is to allow the
1849 * deadlock, but abort the system call if a signal is
1850 * received. We follow this example, otherwise a
1851 * poorly written program could sit in kernel until
1852 * reboot.
1853 *
1854 * Handling this is a bit more complicated for Ocfs2
1855 * though. We can't exit this function with an
1856 * outstanding lock request, so a cancel convert is
1857 * required. We intentionally overwrite 'ret' - if the
1858 * cancel fails and the lock was granted, it's easier
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001859 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001860 */
1861 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001862 } else if (!ret && (level > lockres->l_level)) {
1863 /* Trylock failed asynchronously */
1864 BUG_ON(!trylock);
1865 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001866 }
1867
1868out:
1869
1870 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1871 lockres->l_name, ex, trylock, ret);
1872 return ret;
1873}
1874
1875void ocfs2_file_unlock(struct file *file)
1876{
1877 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001878 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001879 unsigned long flags;
1880 struct ocfs2_file_private *fp = file->private_data;
1881 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1882 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1883 struct ocfs2_mask_waiter mw;
1884
1885 ocfs2_init_mask_waiter(&mw);
1886
1887 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1888 return;
1889
Mark Fashehe988cf12008-07-10 09:25:39 -07001890 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001891 return;
1892
1893 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1894 lockres->l_name, lockres->l_flags, lockres->l_level,
1895 lockres->l_action);
1896
1897 spin_lock_irqsave(&lockres->l_lock, flags);
1898 /*
1899 * Fake a blocking ast for the downconvert code.
1900 */
1901 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001902 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001903
Mark Fashehe988cf12008-07-10 09:25:39 -07001904 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001905 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1906 spin_unlock_irqrestore(&lockres->l_lock, flags);
1907
Mark Fashehe988cf12008-07-10 09:25:39 -07001908 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001909 if (ret) {
1910 mlog_errno(ret);
1911 return;
1912 }
1913
1914 ret = ocfs2_wait_for_mask(&mw);
1915 if (ret)
1916 mlog_errno(ret);
1917}
1918
Mark Fasheh34d024f2007-09-24 15:56:19 -07001919static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1920 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001921{
1922 int kick = 0;
1923
1924 mlog_entry_void();
1925
1926 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001927 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 * condition. */
1929 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1930 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001931 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1933 kick = 1;
1934 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001935 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001936 if (!lockres->l_ex_holders)
1937 kick = 1;
1938 break;
1939 default:
1940 BUG();
1941 }
1942 }
1943
1944 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001945 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001946
1947 mlog_exit_void();
1948}
1949
Mark Fashehccd979b2005-12-15 14:31:24 -08001950#define OCFS2_SEC_BITS 34
1951#define OCFS2_SEC_SHIFT (64 - 34)
1952#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1953
1954/* LVB only has room for 64 bits of time here so we pack it for
1955 * now. */
1956static u64 ocfs2_pack_timespec(struct timespec *spec)
1957{
1958 u64 res;
1959 u64 sec = spec->tv_sec;
1960 u32 nsec = spec->tv_nsec;
1961
1962 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1963
1964 return res;
1965}
1966
1967/* Call this with the lockres locked. I am reasonably sure we don't
1968 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001969 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001970static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1971{
1972 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001973 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001974 struct ocfs2_meta_lvb *lvb;
1975
1976 mlog_entry_void();
1977
Mark Fasheha641dc22008-12-24 16:03:48 -08001978 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001979
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001980 /*
1981 * Invalidate the LVB of a deleted inode - this way other
1982 * nodes are forced to go to disk and discover the new inode
1983 * status.
1984 */
1985 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1986 lvb->lvb_version = 0;
1987 goto out;
1988 }
1989
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001990 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1992 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1993 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1994 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1995 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1996 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1997 lvb->lvb_iatime_packed =
1998 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1999 lvb->lvb_ictime_packed =
2000 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2001 lvb->lvb_imtime_packed =
2002 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002003 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002004 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002005 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002006
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002007out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002008 mlog_meta_lvb(0, lockres);
2009
2010 mlog_exit_void();
2011}
2012
2013static void ocfs2_unpack_timespec(struct timespec *spec,
2014 u64 packed_time)
2015{
2016 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2017 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2018}
2019
2020static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2021{
2022 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002023 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 struct ocfs2_meta_lvb *lvb;
2025
2026 mlog_entry_void();
2027
2028 mlog_meta_lvb(0, lockres);
2029
Mark Fasheha641dc22008-12-24 16:03:48 -08002030 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002031
2032 /* We're safe here without the lockres lock... */
2033 spin_lock(&oi->ip_lock);
2034 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2035 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2036
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002037 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002038 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002039 ocfs2_set_inode_flags(inode);
2040
Mark Fashehccd979b2005-12-15 14:31:24 -08002041 /* fast-symlinks are a special case */
2042 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2043 inode->i_blocks = 0;
2044 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002045 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002046
2047 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2048 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2049 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2050 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2051 ocfs2_unpack_timespec(&inode->i_atime,
2052 be64_to_cpu(lvb->lvb_iatime_packed));
2053 ocfs2_unpack_timespec(&inode->i_mtime,
2054 be64_to_cpu(lvb->lvb_imtime_packed));
2055 ocfs2_unpack_timespec(&inode->i_ctime,
2056 be64_to_cpu(lvb->lvb_ictime_packed));
2057 spin_unlock(&oi->ip_lock);
2058
2059 mlog_exit_void();
2060}
2061
Mark Fashehf9e2d822006-09-12 15:35:49 -07002062static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2063 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002064{
Mark Fasheha641dc22008-12-24 16:03:48 -08002065 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002066
Joel Becker1c520df2009-06-19 15:14:13 -07002067 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2068 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002069 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002070 return 1;
2071 return 0;
2072}
2073
2074/* Determine whether a lock resource needs to be refreshed, and
2075 * arbitrate who gets to refresh it.
2076 *
2077 * 0 means no refresh needed.
2078 *
2079 * > 0 means you need to refresh this and you MUST call
2080 * ocfs2_complete_lock_res_refresh afterwards. */
2081static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2082{
2083 unsigned long flags;
2084 int status = 0;
2085
2086 mlog_entry_void();
2087
2088refresh_check:
2089 spin_lock_irqsave(&lockres->l_lock, flags);
2090 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2091 spin_unlock_irqrestore(&lockres->l_lock, flags);
2092 goto bail;
2093 }
2094
2095 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2096 spin_unlock_irqrestore(&lockres->l_lock, flags);
2097
2098 ocfs2_wait_on_refreshing_lock(lockres);
2099 goto refresh_check;
2100 }
2101
2102 /* Ok, I'll be the one to refresh this lock. */
2103 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2104 spin_unlock_irqrestore(&lockres->l_lock, flags);
2105
2106 status = 1;
2107bail:
2108 mlog_exit(status);
2109 return status;
2110}
2111
2112/* If status is non zero, I'll mark it as not being in refresh
2113 * anymroe, but i won't clear the needs refresh flag. */
2114static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2115 int status)
2116{
2117 unsigned long flags;
2118 mlog_entry_void();
2119
2120 spin_lock_irqsave(&lockres->l_lock, flags);
2121 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2122 if (!status)
2123 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2124 spin_unlock_irqrestore(&lockres->l_lock, flags);
2125
2126 wake_up(&lockres->l_event);
2127
2128 mlog_exit_void();
2129}
2130
2131/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002132static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002133 struct buffer_head **bh)
2134{
2135 int status = 0;
2136 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002137 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002139 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002140
2141 mlog_entry_void();
2142
Mark Fashehbe9e9862007-04-18 15:22:08 -07002143 if (ocfs2_mount_local(osb))
2144 goto bail;
2145
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 spin_lock(&oi->ip_lock);
2147 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08002148 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002149 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002150 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002151 spin_unlock(&oi->ip_lock);
2152 status = -ENOENT;
2153 goto bail;
2154 }
2155 spin_unlock(&oi->ip_lock);
2156
Mark Fashehbe9e9862007-04-18 15:22:08 -07002157 if (!ocfs2_should_refresh_lock_res(lockres))
2158 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002159
2160 /* This will discard any caching information we might have had
2161 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002162 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002163
Mark Fasheh83418972007-04-23 18:53:12 -07002164 ocfs2_extent_map_trunc(inode, 0);
2165
Mark Fashehbe9e9862007-04-18 15:22:08 -07002166 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08002167 mlog(0, "Trusting LVB on inode %llu\n",
2168 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002169 ocfs2_refresh_inode_from_lvb(inode);
2170 } else {
2171 /* Boo, we have to go to disk. */
2172 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002173 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002174 if (status < 0) {
2175 mlog_errno(status);
2176 goto bail_refresh;
2177 }
2178 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2179
2180 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002181 * locking an invalid object. ocfs2_read_inode_block()
2182 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002183 *
2184 * We bug on a stale inode here because we checked
2185 * above whether it was wiped from disk. The wiping
2186 * node provides a guarantee that we receive that
2187 * message and can mark the inode before dropping any
2188 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 mlog_bug_on_msg(inode->i_generation !=
2190 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08002191 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002192 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002193 (unsigned long long)oi->ip_blkno,
2194 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 inode->i_generation);
2196 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2197 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08002198 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2199 (unsigned long long)oi->ip_blkno,
2200 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002201 le32_to_cpu(fe->i_flags));
2202
2203 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002204 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002205 }
2206
2207 status = 0;
2208bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002209 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002210bail:
2211 mlog_exit(status);
2212 return status;
2213}
2214
2215static int ocfs2_assign_bh(struct inode *inode,
2216 struct buffer_head **ret_bh,
2217 struct buffer_head *passed_bh)
2218{
2219 int status;
2220
2221 if (passed_bh) {
2222 /* Ok, the update went to disk for us, use the
2223 * returned bh. */
2224 *ret_bh = passed_bh;
2225 get_bh(*ret_bh);
2226
2227 return 0;
2228 }
2229
Joel Beckerb657c952008-11-13 14:49:11 -08002230 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 if (status < 0)
2232 mlog_errno(status);
2233
2234 return status;
2235}
2236
2237/*
2238 * returns < 0 error if the callback will never be called, otherwise
2239 * the result of the lock will be communicated via the callback.
2240 */
Jan Karacb257972009-06-04 15:26:50 +02002241int ocfs2_inode_lock_full_nested(struct inode *inode,
2242 struct buffer_head **ret_bh,
2243 int ex,
2244 int arg_flags,
2245 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002246{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002247 int status, level, acquired;
2248 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002249 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002250 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2251 struct buffer_head *local_bh = NULL;
2252
2253 BUG_ON(!inode);
2254
2255 mlog_entry_void();
2256
Mark Fashehb06970532006-03-03 10:24:33 -08002257 mlog(0, "inode %llu, take %s META lock\n",
2258 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002259 ex ? "EXMODE" : "PRMODE");
2260
2261 status = 0;
2262 acquired = 0;
2263 /* We'll allow faking a readonly metadata lock for
2264 * rodevices. */
2265 if (ocfs2_is_hard_readonly(osb)) {
2266 if (ex)
2267 status = -EROFS;
2268 goto bail;
2269 }
2270
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002271 if (ocfs2_mount_local(osb))
2272 goto local;
2273
Mark Fashehccd979b2005-12-15 14:31:24 -08002274 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002275 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002276
Mark Fashehe63aecb62007-10-18 15:30:42 -07002277 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002278 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002279 dlm_flags = 0;
2280 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002281 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002282
Jan Karacb257972009-06-04 15:26:50 +02002283 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2284 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002285 if (status < 0) {
2286 if (status != -EAGAIN && status != -EIOCBRETRY)
2287 mlog_errno(status);
2288 goto bail;
2289 }
2290
2291 /* Notify the error cleanup path to drop the cluster lock. */
2292 acquired = 1;
2293
2294 /* We wait twice because a node may have died while we were in
2295 * the lower dlm layers. The second time though, we've
2296 * committed to owning this lock so we don't allow signals to
2297 * abort the operation. */
2298 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002299 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002300
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002301local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002302 /*
2303 * We only see this flag if we're being called from
2304 * ocfs2_read_locked_inode(). It means we're locking an inode
2305 * which hasn't been populated yet, so clear the refresh flag
2306 * and let the caller handle it.
2307 */
2308 if (inode->i_state & I_NEW) {
2309 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002310 if (lockres)
2311 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002312 goto bail;
2313 }
2314
Mark Fashehccd979b2005-12-15 14:31:24 -08002315 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002316 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002317 * may or may not read one, depending on what's in the
2318 * LVB. The result of all of this is that we've *only* gone to
2319 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002320 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002321 if (status < 0) {
2322 if (status != -ENOENT)
2323 mlog_errno(status);
2324 goto bail;
2325 }
2326
2327 if (ret_bh) {
2328 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2329 if (status < 0) {
2330 mlog_errno(status);
2331 goto bail;
2332 }
2333 }
2334
Mark Fashehccd979b2005-12-15 14:31:24 -08002335bail:
2336 if (status < 0) {
2337 if (ret_bh && (*ret_bh)) {
2338 brelse(*ret_bh);
2339 *ret_bh = NULL;
2340 }
2341 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002342 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002343 }
2344
2345 if (local_bh)
2346 brelse(local_bh);
2347
2348 mlog_exit(status);
2349 return status;
2350}
2351
2352/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002353 * This is working around a lock inversion between tasks acquiring DLM
2354 * locks while holding a page lock and the downconvert thread which
2355 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002356 *
2357 * ** These _with_page variantes are only intended to be called from aop
2358 * methods that hold page locks and return a very specific *positive* error
2359 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2360 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002361 * The DLM is called such that it returns -EAGAIN if it would have
2362 * blocked waiting for the downconvert thread. In that case we unlock
2363 * our page so the downconvert thread can make progress. Once we've
2364 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2365 * that called us can bubble that back up into the VFS who will then
2366 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002367 *
2368 * We do a blocking lock and immediate unlock before returning, though, so that
2369 * the lock has a great chance of being cached on this node by the time the VFS
2370 * calls back to retry the aop. This has a potential to livelock as nodes
2371 * ping locks back and forth, but that's a risk we're willing to take to avoid
2372 * the lock inversion simply.
2373 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002374int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002375 struct buffer_head **ret_bh,
2376 int ex,
2377 struct page *page)
2378{
2379 int ret;
2380
Mark Fashehe63aecb62007-10-18 15:30:42 -07002381 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002382 if (ret == -EAGAIN) {
2383 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002384 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2385 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002386 ret = AOP_TRUNCATED_PAGE;
2387 }
2388
2389 return ret;
2390}
2391
Mark Fashehe63aecb62007-10-18 15:30:42 -07002392int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002393 struct vfsmount *vfsmnt,
2394 int *level)
2395{
2396 int ret;
2397
2398 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002399 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002400 if (ret < 0) {
2401 mlog_errno(ret);
2402 return ret;
2403 }
2404
2405 /*
2406 * If we should update atime, we will get EX lock,
2407 * otherwise we just get PR lock.
2408 */
2409 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2410 struct buffer_head *bh = NULL;
2411
Mark Fashehe63aecb62007-10-18 15:30:42 -07002412 ocfs2_inode_unlock(inode, 0);
2413 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002414 if (ret < 0) {
2415 mlog_errno(ret);
2416 return ret;
2417 }
2418 *level = 1;
2419 if (ocfs2_should_update_atime(inode, vfsmnt))
2420 ocfs2_update_inode_atime(inode, bh);
2421 if (bh)
2422 brelse(bh);
2423 } else
2424 *level = 0;
2425
2426 mlog_exit(ret);
2427 return ret;
2428}
2429
Mark Fashehe63aecb62007-10-18 15:30:42 -07002430void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002431 int ex)
2432{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002433 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002434 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002435 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002436
2437 mlog_entry_void();
2438
Mark Fashehb06970532006-03-03 10:24:33 -08002439 mlog(0, "inode %llu drop %s META lock\n",
2440 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002441 ex ? "EXMODE" : "PRMODE");
2442
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002443 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2444 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002445 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2446
2447 mlog_exit_void();
2448}
2449
Sunil Mushrandf152c22009-06-22 11:40:07 -07002450int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002451{
2452 struct ocfs2_lock_res *lockres;
2453 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002454 int status = 0;
2455
Sunil Mushrandf152c22009-06-22 11:40:07 -07002456 if (ocfs2_is_hard_readonly(osb))
2457 return -EROFS;
2458
2459 if (ocfs2_mount_local(osb))
2460 return 0;
2461
Srinivas Eeda83273932009-06-03 17:02:55 -07002462 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002463 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002464 if (status < 0)
2465 return status;
2466
2467 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002468 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2469 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002470 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002471 else
2472 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2473
Srinivas Eeda83273932009-06-03 17:02:55 -07002474 return status;
2475}
2476
Sunil Mushrandf152c22009-06-22 11:40:07 -07002477void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002478{
2479 struct ocfs2_lock_res *lockres;
2480 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002481
Sunil Mushrandf152c22009-06-22 11:40:07 -07002482 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2483 lockres = &osb->osb_orphan_scan.os_lockres;
2484 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2485 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2486 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2487 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2488 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002489}
2490
Mark Fashehccd979b2005-12-15 14:31:24 -08002491int ocfs2_super_lock(struct ocfs2_super *osb,
2492 int ex)
2493{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002494 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002495 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002496 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002497
2498 mlog_entry_void();
2499
2500 if (ocfs2_is_hard_readonly(osb))
2501 return -EROFS;
2502
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002503 if (ocfs2_mount_local(osb))
2504 goto bail;
2505
Mark Fashehccd979b2005-12-15 14:31:24 -08002506 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2507 if (status < 0) {
2508 mlog_errno(status);
2509 goto bail;
2510 }
2511
2512 /* The super block lock path is really in the best position to
2513 * know when resources covered by the lock need to be
2514 * refreshed, so we do it here. Of course, making sense of
2515 * everything is up to the caller :) */
2516 status = ocfs2_should_refresh_lock_res(lockres);
2517 if (status < 0) {
2518 mlog_errno(status);
2519 goto bail;
2520 }
2521 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002522 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002523
2524 ocfs2_complete_lock_res_refresh(lockres, status);
2525
2526 if (status < 0)
2527 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002528 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002529 }
2530bail:
2531 mlog_exit(status);
2532 return status;
2533}
2534
2535void ocfs2_super_unlock(struct ocfs2_super *osb,
2536 int ex)
2537{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002538 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002539 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2540
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002541 if (!ocfs2_mount_local(osb))
2542 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002543}
2544
2545int ocfs2_rename_lock(struct ocfs2_super *osb)
2546{
2547 int status;
2548 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2549
2550 if (ocfs2_is_hard_readonly(osb))
2551 return -EROFS;
2552
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002553 if (ocfs2_mount_local(osb))
2554 return 0;
2555
Joel Beckerbd3e7612008-02-01 12:14:57 -08002556 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002557 if (status < 0)
2558 mlog_errno(status);
2559
2560 return status;
2561}
2562
2563void ocfs2_rename_unlock(struct ocfs2_super *osb)
2564{
2565 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2566
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002567 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002568 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002569}
2570
wengang wang6ca497a2009-03-06 21:29:10 +08002571int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2572{
2573 int status;
2574 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2575
2576 if (ocfs2_is_hard_readonly(osb))
2577 return -EROFS;
2578
2579 if (ocfs2_mount_local(osb))
2580 return 0;
2581
2582 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2583 0, 0);
2584 if (status < 0)
2585 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2586
2587 return status;
2588}
2589
2590void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2591{
2592 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2593
2594 if (!ocfs2_mount_local(osb))
2595 ocfs2_cluster_unlock(osb, lockres,
2596 ex ? LKM_EXMODE : LKM_PRMODE);
2597}
2598
Mark Fashehd680efe2006-09-08 14:14:34 -07002599int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2600{
2601 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002602 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002603 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2604 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2605
2606 BUG_ON(!dl);
2607
2608 if (ocfs2_is_hard_readonly(osb))
2609 return -EROFS;
2610
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002611 if (ocfs2_mount_local(osb))
2612 return 0;
2613
Mark Fashehd680efe2006-09-08 14:14:34 -07002614 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2615 if (ret < 0)
2616 mlog_errno(ret);
2617
2618 return ret;
2619}
2620
2621void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2622{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002623 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002624 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2625 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2626
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002627 if (!ocfs2_mount_local(osb))
2628 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002629}
2630
Mark Fashehccd979b2005-12-15 14:31:24 -08002631/* Reference counting of the dlm debug structure. We want this because
2632 * open references on the debug inodes can live on after a mount, so
2633 * we can't rely on the ocfs2_super to always exist. */
2634static void ocfs2_dlm_debug_free(struct kref *kref)
2635{
2636 struct ocfs2_dlm_debug *dlm_debug;
2637
2638 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2639
2640 kfree(dlm_debug);
2641}
2642
2643void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2644{
2645 if (dlm_debug)
2646 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2647}
2648
2649static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2650{
2651 kref_get(&debug->d_refcnt);
2652}
2653
2654struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2655{
2656 struct ocfs2_dlm_debug *dlm_debug;
2657
2658 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2659 if (!dlm_debug) {
2660 mlog_errno(-ENOMEM);
2661 goto out;
2662 }
2663
2664 kref_init(&dlm_debug->d_refcnt);
2665 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2666 dlm_debug->d_locking_state = NULL;
2667out:
2668 return dlm_debug;
2669}
2670
2671/* Access to this is arbitrated for us via seq_file->sem. */
2672struct ocfs2_dlm_seq_priv {
2673 struct ocfs2_dlm_debug *p_dlm_debug;
2674 struct ocfs2_lock_res p_iter_res;
2675 struct ocfs2_lock_res p_tmp_res;
2676};
2677
2678static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2679 struct ocfs2_dlm_seq_priv *priv)
2680{
2681 struct ocfs2_lock_res *iter, *ret = NULL;
2682 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2683
2684 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2685
2686 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2687 /* discover the head of the list */
2688 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2689 mlog(0, "End of list found, %p\n", ret);
2690 break;
2691 }
2692
2693 /* We track our "dummy" iteration lockres' by a NULL
2694 * l_ops field. */
2695 if (iter->l_ops != NULL) {
2696 ret = iter;
2697 break;
2698 }
2699 }
2700
2701 return ret;
2702}
2703
2704static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2705{
2706 struct ocfs2_dlm_seq_priv *priv = m->private;
2707 struct ocfs2_lock_res *iter;
2708
2709 spin_lock(&ocfs2_dlm_tracking_lock);
2710 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2711 if (iter) {
2712 /* Since lockres' have the lifetime of their container
2713 * (which can be inodes, ocfs2_supers, etc) we want to
2714 * copy this out to a temporary lockres while still
2715 * under the spinlock. Obviously after this we can't
2716 * trust any pointers on the copy returned, but that's
2717 * ok as the information we want isn't typically held
2718 * in them. */
2719 priv->p_tmp_res = *iter;
2720 iter = &priv->p_tmp_res;
2721 }
2722 spin_unlock(&ocfs2_dlm_tracking_lock);
2723
2724 return iter;
2725}
2726
2727static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2728{
2729}
2730
2731static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2732{
2733 struct ocfs2_dlm_seq_priv *priv = m->private;
2734 struct ocfs2_lock_res *iter = v;
2735 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2736
2737 spin_lock(&ocfs2_dlm_tracking_lock);
2738 iter = ocfs2_dlm_next_res(iter, priv);
2739 list_del_init(&dummy->l_debug_list);
2740 if (iter) {
2741 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2742 priv->p_tmp_res = *iter;
2743 iter = &priv->p_tmp_res;
2744 }
2745 spin_unlock(&ocfs2_dlm_tracking_lock);
2746
2747 return iter;
2748}
2749
2750/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002751#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002752static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2753{
2754 int i;
2755 char *lvb;
2756 struct ocfs2_lock_res *lockres = v;
2757
2758 if (!lockres)
2759 return -EINVAL;
2760
Mark Fashehd680efe2006-09-08 14:14:34 -07002761 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2762
2763 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2764 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2765 lockres->l_name,
2766 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2767 else
2768 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2769
2770 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002771 "0x%lx\t"
2772 "0x%x\t"
2773 "0x%x\t"
2774 "%u\t"
2775 "%u\t"
2776 "%d\t"
2777 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002778 lockres->l_level,
2779 lockres->l_flags,
2780 lockres->l_action,
2781 lockres->l_unlock_action,
2782 lockres->l_ro_holders,
2783 lockres->l_ex_holders,
2784 lockres->l_requested,
2785 lockres->l_blocking);
2786
2787 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002788 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002789 for(i = 0; i < DLM_LVB_LEN; i++)
2790 seq_printf(m, "0x%x\t", lvb[i]);
2791
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002792#ifdef CONFIG_OCFS2_FS_STATS
2793# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2794# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2795# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2796# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2797# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2798# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2799# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2800# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2801# define lock_refresh(_l) (_l)->l_lock_refresh
2802#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002803# define lock_num_prmode(_l) (0ULL)
2804# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002805# define lock_num_prmode_failed(_l) (0)
2806# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002807# define lock_total_prmode(_l) (0ULL)
2808# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002809# define lock_max_prmode(_l) (0)
2810# define lock_max_exmode(_l) (0)
2811# define lock_refresh(_l) (0)
2812#endif
2813 /* The following seq_print was added in version 2 of this output */
2814 seq_printf(m, "%llu\t"
2815 "%llu\t"
2816 "%u\t"
2817 "%u\t"
2818 "%llu\t"
2819 "%llu\t"
2820 "%u\t"
2821 "%u\t"
2822 "%u\t",
2823 lock_num_prmode(lockres),
2824 lock_num_exmode(lockres),
2825 lock_num_prmode_failed(lockres),
2826 lock_num_exmode_failed(lockres),
2827 lock_total_prmode(lockres),
2828 lock_total_exmode(lockres),
2829 lock_max_prmode(lockres),
2830 lock_max_exmode(lockres),
2831 lock_refresh(lockres));
2832
Mark Fashehccd979b2005-12-15 14:31:24 -08002833 /* End the line */
2834 seq_printf(m, "\n");
2835 return 0;
2836}
2837
Jan Engelhardt90d99772008-01-22 20:52:20 +01002838static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002839 .start = ocfs2_dlm_seq_start,
2840 .stop = ocfs2_dlm_seq_stop,
2841 .next = ocfs2_dlm_seq_next,
2842 .show = ocfs2_dlm_seq_show,
2843};
2844
2845static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2846{
2847 struct seq_file *seq = (struct seq_file *) file->private_data;
2848 struct ocfs2_dlm_seq_priv *priv = seq->private;
2849 struct ocfs2_lock_res *res = &priv->p_iter_res;
2850
2851 ocfs2_remove_lockres_tracking(res);
2852 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2853 return seq_release_private(inode, file);
2854}
2855
2856static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2857{
2858 int ret;
2859 struct ocfs2_dlm_seq_priv *priv;
2860 struct seq_file *seq;
2861 struct ocfs2_super *osb;
2862
2863 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2864 if (!priv) {
2865 ret = -ENOMEM;
2866 mlog_errno(ret);
2867 goto out;
2868 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002869 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002870 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2871 priv->p_dlm_debug = osb->osb_dlm_debug;
2872 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2873
2874 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2875 if (ret) {
2876 kfree(priv);
2877 mlog_errno(ret);
2878 goto out;
2879 }
2880
2881 seq = (struct seq_file *) file->private_data;
2882 seq->private = priv;
2883
2884 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2885 priv->p_dlm_debug);
2886
2887out:
2888 return ret;
2889}
2890
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002891static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002892 .open = ocfs2_dlm_debug_open,
2893 .release = ocfs2_dlm_debug_release,
2894 .read = seq_read,
2895 .llseek = seq_lseek,
2896};
2897
2898static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2899{
2900 int ret = 0;
2901 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2902
2903 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2904 S_IFREG|S_IRUSR,
2905 osb->osb_debug_root,
2906 osb,
2907 &ocfs2_dlm_debug_fops);
2908 if (!dlm_debug->d_locking_state) {
2909 ret = -EINVAL;
2910 mlog(ML_ERROR,
2911 "Unable to create locking state debugfs file.\n");
2912 goto out;
2913 }
2914
2915 ocfs2_get_dlm_debug(dlm_debug);
2916out:
2917 return ret;
2918}
2919
2920static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2921{
2922 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2923
2924 if (dlm_debug) {
2925 debugfs_remove(dlm_debug->d_locking_state);
2926 ocfs2_put_dlm_debug(dlm_debug);
2927 }
2928}
2929
2930int ocfs2_dlm_init(struct ocfs2_super *osb)
2931{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002932 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002933 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002934
2935 mlog_entry_void();
2936
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002937 if (ocfs2_mount_local(osb)) {
2938 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002939 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002940 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002941
Mark Fashehccd979b2005-12-15 14:31:24 -08002942 status = ocfs2_dlm_init_debug(osb);
2943 if (status < 0) {
2944 mlog_errno(status);
2945 goto bail;
2946 }
2947
Mark Fasheh34d024f2007-09-24 15:56:19 -07002948 /* launch downconvert thread */
2949 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2950 if (IS_ERR(osb->dc_task)) {
2951 status = PTR_ERR(osb->dc_task);
2952 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002953 mlog_errno(status);
2954 goto bail;
2955 }
2956
Mark Fashehccd979b2005-12-15 14:31:24 -08002957 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002958 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2959 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002960 strlen(osb->uuid_str),
2961 ocfs2_do_node_down, osb,
2962 &conn);
2963 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002964 mlog_errno(status);
2965 goto bail;
2966 }
2967
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002968 status = ocfs2_cluster_this_node(&osb->node_num);
2969 if (status < 0) {
2970 mlog_errno(status);
2971 mlog(ML_ERROR,
2972 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08002973 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002974 goto bail;
2975 }
2976
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002977local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002978 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2979 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08002980 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07002981 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002982
Joel Becker4670c462008-02-01 14:39:35 -08002983 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002984
2985 status = 0;
2986bail:
2987 if (status < 0) {
2988 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002989 if (osb->dc_task)
2990 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002991 }
2992
2993 mlog_exit(status);
2994 return status;
2995}
2996
Joel Becker286eaa92008-02-01 15:03:57 -08002997void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2998 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08002999{
3000 mlog_entry_void();
3001
Mark Fashehccd979b2005-12-15 14:31:24 -08003002 ocfs2_drop_osb_locks(osb);
3003
Joel Becker4670c462008-02-01 14:39:35 -08003004 /*
3005 * Now that we have dropped all locks and ocfs2_dismount_volume()
3006 * has disabled recovery, the DLM won't be talking to us. It's
3007 * safe to tear things down before disconnecting the cluster.
3008 */
3009
Mark Fasheh34d024f2007-09-24 15:56:19 -07003010 if (osb->dc_task) {
3011 kthread_stop(osb->dc_task);
3012 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003013 }
3014
3015 ocfs2_lock_res_free(&osb->osb_super_lockres);
3016 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003017 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003018 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003019
Joel Becker286eaa92008-02-01 15:03:57 -08003020 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003021 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003022
3023 ocfs2_dlm_shutdown_debug(osb);
3024
3025 mlog_exit_void();
3026}
3027
Joel Becker7431cd72008-02-01 12:15:37 -08003028static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08003029{
3030 struct ocfs2_lock_res *lockres = opaque;
3031 unsigned long flags;
3032
3033 mlog_entry_void();
3034
3035 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
3036 lockres->l_unlock_action);
3037
3038 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003039 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08003040 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
3041 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08003042 lockres->l_unlock_action);
3043 spin_unlock_irqrestore(&lockres->l_lock, flags);
Coly Lid92bc512009-08-28 19:03:18 +08003044 mlog_exit_void();
Mark Fashehccd979b2005-12-15 14:31:24 -08003045 return;
3046 }
3047
3048 switch(lockres->l_unlock_action) {
3049 case OCFS2_UNLOCK_CANCEL_CONVERT:
3050 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
3051 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08003052 /* Downconvert thread may have requeued this lock, we
3053 * need to wake it. */
3054 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3055 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08003056 break;
3057 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08003058 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08003059 break;
3060 default:
3061 BUG();
3062 }
3063
3064 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08003065 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08003066 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06003067 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08003068
3069 mlog_exit_void();
3070}
3071
Mark Fashehccd979b2005-12-15 14:31:24 -08003072static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003073 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003074{
Joel Becker7431cd72008-02-01 12:15:37 -08003075 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003076 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003077 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003078
3079 /* We didn't get anywhere near actually using this lockres. */
3080 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3081 goto out;
3082
Mark Fashehb80fc012006-09-12 22:08:14 -07003083 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003084 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003085
Mark Fashehccd979b2005-12-15 14:31:24 -08003086 spin_lock_irqsave(&lockres->l_lock, flags);
3087
3088 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3089 "lockres %s, flags 0x%lx\n",
3090 lockres->l_name, lockres->l_flags);
3091
3092 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3093 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3094 "%u, unlock_action = %u\n",
3095 lockres->l_name, lockres->l_flags, lockres->l_action,
3096 lockres->l_unlock_action);
3097
3098 spin_unlock_irqrestore(&lockres->l_lock, flags);
3099
3100 /* XXX: Today we just wait on any busy
3101 * locks... Perhaps we need to cancel converts in the
3102 * future? */
3103 ocfs2_wait_on_busy_lock(lockres);
3104
3105 spin_lock_irqsave(&lockres->l_lock, flags);
3106 }
3107
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003108 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3109 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003110 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003111 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3112 lockres->l_ops->set_lvb(lockres);
3113 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003114
3115 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3116 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3117 lockres->l_name);
3118 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3119 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3120
3121 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3122 spin_unlock_irqrestore(&lockres->l_lock, flags);
3123 goto out;
3124 }
3125
3126 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3127
3128 /* make sure we never get here while waiting for an ast to
3129 * fire. */
3130 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3131
3132 /* is this necessary? */
3133 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3134 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3135 spin_unlock_irqrestore(&lockres->l_lock, flags);
3136
3137 mlog(0, "lock %s\n", lockres->l_name);
3138
Joel Becker4670c462008-02-01 14:39:35 -08003139 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08003140 lockres);
3141 if (ret) {
3142 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003143 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003144 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003145 BUG();
3146 }
Coly Li73ac36e2009-01-07 18:09:16 -08003147 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003148 lockres->l_name);
3149
3150 ocfs2_wait_on_busy_lock(lockres);
3151out:
3152 mlog_exit(0);
3153 return 0;
3154}
3155
3156/* Mark the lockres as being dropped. It will no longer be
3157 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003158 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003159 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003160 *
3161 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3162void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3163{
3164 int status;
3165 struct ocfs2_mask_waiter mw;
3166 unsigned long flags;
3167
3168 ocfs2_init_mask_waiter(&mw);
3169
3170 spin_lock_irqsave(&lockres->l_lock, flags);
3171 lockres->l_flags |= OCFS2_LOCK_FREEING;
3172 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3173 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3174 spin_unlock_irqrestore(&lockres->l_lock, flags);
3175
3176 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3177
3178 status = ocfs2_wait_for_mask(&mw);
3179 if (status)
3180 mlog_errno(status);
3181
3182 spin_lock_irqsave(&lockres->l_lock, flags);
3183 }
3184 spin_unlock_irqrestore(&lockres->l_lock, flags);
3185}
3186
Mark Fashehd680efe2006-09-08 14:14:34 -07003187void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3188 struct ocfs2_lock_res *lockres)
3189{
3190 int ret;
3191
3192 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003193 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003194 if (ret)
3195 mlog_errno(ret);
3196}
3197
Mark Fashehccd979b2005-12-15 14:31:24 -08003198static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3199{
Mark Fashehd680efe2006-09-08 14:14:34 -07003200 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3201 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003202 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003203 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003204}
3205
Mark Fashehccd979b2005-12-15 14:31:24 -08003206int ocfs2_drop_inode_locks(struct inode *inode)
3207{
3208 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003209
3210 mlog_entry_void();
3211
3212 /* No need to call ocfs2_mark_lockres_freeing here -
3213 * ocfs2_clear_inode has done it for us. */
3214
3215 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003216 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003217 if (err < 0)
3218 mlog_errno(err);
3219
3220 status = err;
3221
3222 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003223 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003224 if (err < 0)
3225 mlog_errno(err);
3226 if (err < 0 && !status)
3227 status = err;
3228
3229 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003230 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003231 if (err < 0)
3232 mlog_errno(err);
3233 if (err < 0 && !status)
3234 status = err;
3235
3236 mlog_exit(status);
3237 return status;
3238}
3239
Joel Beckerde551242008-02-01 14:45:08 -08003240static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3241 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003242{
3243 assert_spin_locked(&lockres->l_lock);
3244
Joel Beckerbd3e7612008-02-01 12:14:57 -08003245 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003246
3247 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003248 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003249 lockres->l_level, new_level);
3250 BUG();
3251 }
3252
3253 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3254 lockres->l_name, new_level, lockres->l_blocking);
3255
3256 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3257 lockres->l_requested = new_level;
3258 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003259 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003260}
3261
3262static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3263 struct ocfs2_lock_res *lockres,
3264 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003265 int lvb,
3266 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003267{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003268 int ret;
3269 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003270
3271 mlog_entry_void();
3272
3273 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003274 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003275
Joel Becker4670c462008-02-01 14:39:35 -08003276 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003277 new_level,
3278 &lockres->l_lksb,
3279 dlm_flags,
3280 lockres->l_name,
3281 OCFS2_LOCK_ID_MAX_LEN - 1,
3282 lockres);
Joel Beckerde551242008-02-01 14:45:08 -08003283 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003284 if (ret) {
3285 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003286 ocfs2_recover_from_dlm_error(lockres, 1);
3287 goto bail;
3288 }
3289
3290 ret = 0;
3291bail:
3292 mlog_exit(ret);
3293 return ret;
3294}
3295
Joel Becker24ef1812008-01-29 17:37:32 -08003296/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003297static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3298 struct ocfs2_lock_res *lockres)
3299{
3300 assert_spin_locked(&lockres->l_lock);
3301
3302 mlog_entry_void();
3303 mlog(0, "lock %s\n", lockres->l_name);
3304
3305 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3306 /* If we're already trying to cancel a lock conversion
3307 * then just drop the spinlock and allow the caller to
3308 * requeue this lock. */
3309
3310 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3311 return 0;
3312 }
3313
3314 /* were we in a convert when we got the bast fire? */
3315 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3316 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3317 /* set things up for the unlockast to know to just
3318 * clear out the ast_action and unset busy, etc. */
3319 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3320
3321 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3322 "lock %s, invalid flags: 0x%lx\n",
3323 lockres->l_name, lockres->l_flags);
3324
3325 return 1;
3326}
3327
3328static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3329 struct ocfs2_lock_res *lockres)
3330{
3331 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003332
3333 mlog_entry_void();
3334 mlog(0, "lock %s\n", lockres->l_name);
3335
Joel Becker4670c462008-02-01 14:39:35 -08003336 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08003337 DLM_LKF_CANCEL, lockres);
3338 if (ret) {
3339 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003340 ocfs2_recover_from_dlm_error(lockres, 0);
3341 }
3342
Joel Becker24ef1812008-01-29 17:37:32 -08003343 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003344
3345 mlog_exit(ret);
3346 return ret;
3347}
3348
Mark Fashehb5e500e2006-09-13 22:01:16 -07003349static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3350 struct ocfs2_lock_res *lockres,
3351 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003352{
3353 unsigned long flags;
3354 int blocking;
3355 int new_level;
3356 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003357 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003358 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003359
3360 mlog_entry_void();
3361
3362 spin_lock_irqsave(&lockres->l_lock, flags);
3363
3364 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3365
3366recheck:
3367 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003368 /* XXX
3369 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3370 * exists entirely for one reason - another thread has set
3371 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3372 *
3373 * If we do ocfs2_cancel_convert() before the other thread
3374 * calls dlm_lock(), our cancel will do nothing. We will
3375 * get no ast, and we will have no way of knowing the
3376 * cancel failed. Meanwhile, the other thread will call
3377 * into dlm_lock() and wait...forever.
3378 *
3379 * Why forever? Because another node has asked for the
3380 * lock first; that's why we're here in unblock_lock().
3381 *
3382 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3383 * set, we just requeue the unblock. Only when the other
3384 * thread has called dlm_lock() and cleared PENDING will
3385 * we then cancel their request.
3386 *
3387 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3388 * at the same time they set OCFS2_DLM_BUSY. They must
3389 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3390 */
3391 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3392 goto leave_requeue;
3393
Mark Fashehd680efe2006-09-08 14:14:34 -07003394 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003395 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3396 spin_unlock_irqrestore(&lockres->l_lock, flags);
3397 if (ret) {
3398 ret = ocfs2_cancel_convert(osb, lockres);
3399 if (ret < 0)
3400 mlog_errno(ret);
3401 }
3402 goto leave;
3403 }
3404
3405 /* if we're blocking an exclusive and we have *any* holders,
3406 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003407 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003408 && (lockres->l_ex_holders || lockres->l_ro_holders))
3409 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003410
3411 /* If it's a PR we're blocking, then only
3412 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003413 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003414 lockres->l_ex_holders)
3415 goto leave_requeue;
3416
3417 /*
3418 * Can we get a lock in this state if the holder counts are
3419 * zero? The meta data unblock code used to check this.
3420 */
3421 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3422 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3423 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003424
Mark Fasheh16d5b952006-09-13 21:10:12 -07003425 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3426
3427 if (lockres->l_ops->check_downconvert
3428 && !lockres->l_ops->check_downconvert(lockres, new_level))
3429 goto leave_requeue;
3430
Mark Fashehccd979b2005-12-15 14:31:24 -08003431 /* If we get here, then we know that there are no more
3432 * incompatible holders (and anyone asking for an incompatible
3433 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003434 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003435 goto downconvert;
3436
3437 /* Some lockres types want to do a bit of work before
3438 * downconverting a lock. Allow that here. The worker function
3439 * may sleep, so we save off a copy of what we're blocking as
3440 * it may change while we're not holding the spin lock. */
3441 blocking = lockres->l_blocking;
3442 spin_unlock_irqrestore(&lockres->l_lock, flags);
3443
Mark Fashehcc567d82006-09-13 21:52:21 -07003444 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003445
3446 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3447 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003448
3449 spin_lock_irqsave(&lockres->l_lock, flags);
3450 if (blocking != lockres->l_blocking) {
3451 /* If this changed underneath us, then we can't drop
3452 * it just yet. */
3453 goto recheck;
3454 }
3455
3456downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003457 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003458
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003459 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003460 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003461 set_lvb = 1;
3462
3463 /*
3464 * We only set the lvb if the lock has been fully
3465 * refreshed - otherwise we risk setting stale
3466 * data. Otherwise, there's no need to actually clear
3467 * out the lvb here as it's value is still valid.
3468 */
3469 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3470 lockres->l_ops->set_lvb(lockres);
3471 }
3472
Joel Beckerde551242008-02-01 14:45:08 -08003473 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003474 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003475 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3476 gen);
3477
Mark Fashehccd979b2005-12-15 14:31:24 -08003478leave:
3479 mlog_exit(ret);
3480 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003481
3482leave_requeue:
3483 spin_unlock_irqrestore(&lockres->l_lock, flags);
3484 ctl->requeue = 1;
3485
3486 mlog_exit(0);
3487 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003488}
3489
Mark Fashehd680efe2006-09-08 14:14:34 -07003490static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3491 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003492{
3493 struct inode *inode;
3494 struct address_space *mapping;
3495
Mark Fashehccd979b2005-12-15 14:31:24 -08003496 inode = ocfs2_lock_res_inode(lockres);
3497 mapping = inode->i_mapping;
3498
Mark Fasheh1044e402008-02-28 17:16:03 -08003499 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003500 goto out;
3501
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003502 /*
3503 * We need this before the filemap_fdatawrite() so that it can
3504 * transfer the dirty bit from the PTE to the
3505 * page. Unfortunately this means that even for EX->PR
3506 * downconverts, we'll lose our mappings and have to build
3507 * them up again.
3508 */
3509 unmap_mapping_range(mapping, 0, 0, 0);
3510
Mark Fashehccd979b2005-12-15 14:31:24 -08003511 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003512 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3513 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003514 }
3515 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003516 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003517 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003518 } else {
3519 /* We only need to wait on the I/O if we're not also
3520 * truncating pages because truncate_inode_pages waits
3521 * for us above. We don't truncate pages if we're
3522 * blocking anything < EXMODE because we want to keep
3523 * them around in that case. */
3524 filemap_fdatawait(mapping);
3525 }
3526
Mark Fashehf1f54062007-10-18 15:13:59 -07003527out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003528 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003529}
3530
Tao Maa4338482009-08-18 11:19:29 +08003531static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3532 struct ocfs2_lock_res *lockres,
3533 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003534{
Tao Maa4338482009-08-18 11:19:29 +08003535 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003536
Joel Beckerbd3e7612008-02-01 12:14:57 -08003537 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3538 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003539
3540 if (checkpointed)
3541 return 1;
3542
Tao Maa4338482009-08-18 11:19:29 +08003543 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003544 return 0;
3545}
3546
Tao Maa4338482009-08-18 11:19:29 +08003547static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3548 int new_level)
3549{
3550 struct inode *inode = ocfs2_lock_res_inode(lockres);
3551
3552 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3553}
3554
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003555static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3556{
3557 struct inode *inode = ocfs2_lock_res_inode(lockres);
3558
3559 __ocfs2_stuff_meta_lvb(inode);
3560}
3561
Mark Fashehd680efe2006-09-08 14:14:34 -07003562/*
3563 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003564 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003565 * dlmglue API and push these off to the ocfs2_wq in the future.
3566 */
3567static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3568 struct ocfs2_lock_res *lockres)
3569{
3570 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3571 ocfs2_dentry_lock_put(osb, dl);
3572}
3573
3574/*
3575 * d_delete() matching dentries before the lock downconvert.
3576 *
3577 * At this point, any process waiting to destroy the
3578 * dentry_lock due to last ref count is stopped by the
3579 * OCFS2_LOCK_QUEUED flag.
3580 *
3581 * We have two potential problems
3582 *
3583 * 1) If we do the last reference drop on our dentry_lock (via dput)
3584 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3585 * the downconvert to finish. Instead we take an elevated
3586 * reference and push the drop until after we've completed our
3587 * unblock processing.
3588 *
3589 * 2) There might be another process with a final reference,
3590 * waiting on us to finish processing. If this is the case, we
3591 * detect it and exit out - there's no more dentries anyway.
3592 */
3593static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3594 int blocking)
3595{
3596 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3597 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3598 struct dentry *dentry;
3599 unsigned long flags;
3600 int extra_ref = 0;
3601
3602 /*
3603 * This node is blocking another node from getting a read
3604 * lock. This happens when we've renamed within a
3605 * directory. We've forced the other nodes to d_delete(), but
3606 * we never actually dropped our lock because it's still
3607 * valid. The downconvert code will retain a PR for this node,
3608 * so there's no further work to do.
3609 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003610 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003611 return UNBLOCK_CONTINUE;
3612
3613 /*
3614 * Mark this inode as potentially orphaned. The code in
3615 * ocfs2_delete_inode() will figure out whether it actually
3616 * needs to be freed or not.
3617 */
3618 spin_lock(&oi->ip_lock);
3619 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3620 spin_unlock(&oi->ip_lock);
3621
3622 /*
3623 * Yuck. We need to make sure however that the check of
3624 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3625 * respect to a reference decrement or the setting of that
3626 * flag.
3627 */
3628 spin_lock_irqsave(&lockres->l_lock, flags);
3629 spin_lock(&dentry_attach_lock);
3630 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3631 && dl->dl_count) {
3632 dl->dl_count++;
3633 extra_ref = 1;
3634 }
3635 spin_unlock(&dentry_attach_lock);
3636 spin_unlock_irqrestore(&lockres->l_lock, flags);
3637
3638 mlog(0, "extra_ref = %d\n", extra_ref);
3639
3640 /*
3641 * We have a process waiting on us in ocfs2_dentry_iput(),
3642 * which means we can't have any more outstanding
3643 * aliases. There's no need to do any more work.
3644 */
3645 if (!extra_ref)
3646 return UNBLOCK_CONTINUE;
3647
3648 spin_lock(&dentry_attach_lock);
3649 while (1) {
3650 dentry = ocfs2_find_local_alias(dl->dl_inode,
3651 dl->dl_parent_blkno, 1);
3652 if (!dentry)
3653 break;
3654 spin_unlock(&dentry_attach_lock);
3655
3656 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3657 dentry->d_name.name);
3658
3659 /*
3660 * The following dcache calls may do an
3661 * iput(). Normally we don't want that from the
3662 * downconverting thread, but in this case it's ok
3663 * because the requesting node already has an
3664 * exclusive lock on the inode, so it can't be queued
3665 * for a downconvert.
3666 */
3667 d_delete(dentry);
3668 dput(dentry);
3669
3670 spin_lock(&dentry_attach_lock);
3671 }
3672 spin_unlock(&dentry_attach_lock);
3673
3674 /*
3675 * If we are the last holder of this dentry lock, there is no
3676 * reason to downconvert so skip straight to the unlock.
3677 */
3678 if (dl->dl_count == 1)
3679 return UNBLOCK_STOP_POST;
3680
3681 return UNBLOCK_CONTINUE_POST;
3682}
3683
Tao Ma8dec98e2009-08-18 11:19:58 +08003684static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3685 int new_level)
3686{
3687 struct ocfs2_refcount_tree *tree =
3688 ocfs2_lock_res_refcount_tree(lockres);
3689
3690 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3691}
3692
3693static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3694 int blocking)
3695{
3696 struct ocfs2_refcount_tree *tree =
3697 ocfs2_lock_res_refcount_tree(lockres);
3698
3699 ocfs2_metadata_cache_purge(&tree->rf_ci);
3700
3701 return UNBLOCK_CONTINUE;
3702}
3703
Jan Kara9e33d692008-08-25 19:56:50 +02003704static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3705{
3706 struct ocfs2_qinfo_lvb *lvb;
3707 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3708 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3709 oinfo->dqi_gi.dqi_type);
3710
3711 mlog_entry_void();
3712
Mark Fasheha641dc22008-12-24 16:03:48 -08003713 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003714 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3715 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3716 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3717 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3718 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3719 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3720 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3721
3722 mlog_exit_void();
3723}
3724
3725void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3726{
3727 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3728 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3729 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3730
3731 mlog_entry_void();
3732 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3733 ocfs2_cluster_unlock(osb, lockres, level);
3734 mlog_exit_void();
3735}
3736
3737static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3738{
3739 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3740 oinfo->dqi_gi.dqi_type);
3741 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3742 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003743 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003744 struct ocfs2_global_disk_dqinfo *gdinfo;
3745 int status = 0;
3746
Joel Becker1c520df2009-06-19 15:14:13 -07003747 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3748 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003749 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3750 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3751 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3752 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3753 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3754 oinfo->dqi_gi.dqi_free_entry =
3755 be32_to_cpu(lvb->lvb_free_entry);
3756 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003757 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3758 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003759 mlog_errno(status);
3760 goto bail;
3761 }
3762 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3763 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3764 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3765 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3766 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3767 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3768 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3769 oinfo->dqi_gi.dqi_free_entry =
3770 le32_to_cpu(gdinfo->dqi_free_entry);
3771 brelse(bh);
3772 ocfs2_track_lock_refresh(lockres);
3773 }
3774
3775bail:
3776 return status;
3777}
3778
3779/* Lock quota info, this function expects at least shared lock on the quota file
3780 * so that we can safely refresh quota info from disk. */
3781int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3782{
3783 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3784 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3785 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3786 int status = 0;
3787
3788 mlog_entry_void();
3789
3790 /* On RO devices, locking really isn't needed... */
3791 if (ocfs2_is_hard_readonly(osb)) {
3792 if (ex)
3793 status = -EROFS;
3794 goto bail;
3795 }
3796 if (ocfs2_mount_local(osb))
3797 goto bail;
3798
3799 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3800 if (status < 0) {
3801 mlog_errno(status);
3802 goto bail;
3803 }
3804 if (!ocfs2_should_refresh_lock_res(lockres))
3805 goto bail;
3806 /* OK, we have the lock but we need to refresh the quota info */
3807 status = ocfs2_refresh_qinfo(oinfo);
3808 if (status)
3809 ocfs2_qinfo_unlock(oinfo, ex);
3810 ocfs2_complete_lock_res_refresh(lockres, status);
3811bail:
3812 mlog_exit(status);
3813 return status;
3814}
3815
Tao Ma8dec98e2009-08-18 11:19:58 +08003816int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
3817{
3818 int status;
3819 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3820 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3821 struct ocfs2_super *osb = lockres->l_priv;
3822
3823
3824 if (ocfs2_is_hard_readonly(osb))
3825 return -EROFS;
3826
3827 if (ocfs2_mount_local(osb))
3828 return 0;
3829
3830 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3831 if (status < 0)
3832 mlog_errno(status);
3833
3834 return status;
3835}
3836
3837void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
3838{
3839 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3840 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3841 struct ocfs2_super *osb = lockres->l_priv;
3842
3843 if (!ocfs2_mount_local(osb))
3844 ocfs2_cluster_unlock(osb, lockres, level);
3845}
3846
Joel Becker4670c462008-02-01 14:39:35 -08003847/*
3848 * This is the filesystem locking protocol. It provides the lock handling
3849 * hooks for the underlying DLM. It has a maximum version number.
3850 * The version number allows interoperability with systems running at
3851 * the same major number and an equal or smaller minor number.
3852 *
3853 * Whenever the filesystem does new things with locks (adds or removes a
3854 * lock, orders them differently, does different things underneath a lock),
3855 * the version must be changed. The protocol is negotiated when joining
3856 * the dlm domain. A node may join the domain if its major version is
3857 * identical to all other nodes and its minor version is greater than
3858 * or equal to all other nodes. When its minor version is greater than
3859 * the other nodes, it will run at the minor version specified by the
3860 * other nodes.
3861 *
3862 * If a locking change is made that will not be compatible with older
3863 * versions, the major number must be increased and the minor version set
3864 * to zero. If a change merely adds a behavior that can be disabled when
3865 * speaking to older versions, the minor version must be increased. If a
3866 * change adds a fully backwards compatible change (eg, LVB changes that
3867 * are just ignored by older versions), the version does not need to be
3868 * updated.
3869 */
Joel Becker24ef1812008-01-29 17:37:32 -08003870static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003871 .lp_max_version = {
3872 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3873 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3874 },
Joel Becker24ef1812008-01-29 17:37:32 -08003875 .lp_lock_ast = ocfs2_locking_ast,
3876 .lp_blocking_ast = ocfs2_blocking_ast,
3877 .lp_unlock_ast = ocfs2_unlock_ast,
3878};
3879
Joel Becker63e0c482008-01-30 16:58:36 -08003880void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003881{
Joel Becker63e0c482008-01-30 16:58:36 -08003882 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003883}
3884
Joel Becker24ef1812008-01-29 17:37:32 -08003885
Adrian Bunk00600052008-01-29 00:11:41 +02003886static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3887 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003888{
3889 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003890 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003891 unsigned long flags;
3892
3893 /* Our reference to the lockres in this function can be
3894 * considered valid until we remove the OCFS2_LOCK_QUEUED
3895 * flag. */
3896
3897 mlog_entry_void();
3898
3899 BUG_ON(!lockres);
3900 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003901
3902 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3903
3904 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003905 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003906 * still be marked with OCFS2_LOCK_FREEING after this check,
3907 * but short circuiting here will still save us some
3908 * performance. */
3909 spin_lock_irqsave(&lockres->l_lock, flags);
3910 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3911 goto unqueue;
3912 spin_unlock_irqrestore(&lockres->l_lock, flags);
3913
Mark Fashehb5e500e2006-09-13 22:01:16 -07003914 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003915 if (status < 0)
3916 mlog_errno(status);
3917
3918 spin_lock_irqsave(&lockres->l_lock, flags);
3919unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003920 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003921 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3922 } else
3923 ocfs2_schedule_blocked_lock(osb, lockres);
3924
3925 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003926 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003927 spin_unlock_irqrestore(&lockres->l_lock, flags);
3928
Mark Fashehd680efe2006-09-08 14:14:34 -07003929 if (ctl.unblock_action != UNBLOCK_CONTINUE
3930 && lockres->l_ops->post_unlock)
3931 lockres->l_ops->post_unlock(osb, lockres);
3932
Mark Fashehccd979b2005-12-15 14:31:24 -08003933 mlog_exit_void();
3934}
3935
3936static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3937 struct ocfs2_lock_res *lockres)
3938{
3939 mlog_entry_void();
3940
3941 assert_spin_locked(&lockres->l_lock);
3942
3943 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3944 /* Do not schedule a lock for downconvert when it's on
3945 * the way to destruction - any nodes wanting access
3946 * to the resource will get it soon. */
3947 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3948 lockres->l_name, lockres->l_flags);
3949 return;
3950 }
3951
3952 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3953
Mark Fasheh34d024f2007-09-24 15:56:19 -07003954 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003955 if (list_empty(&lockres->l_blocked_list)) {
3956 list_add_tail(&lockres->l_blocked_list,
3957 &osb->blocked_lock_list);
3958 osb->blocked_lock_count++;
3959 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003960 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003961
3962 mlog_exit_void();
3963}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003964
3965static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3966{
3967 unsigned long processed;
3968 struct ocfs2_lock_res *lockres;
3969
3970 mlog_entry_void();
3971
3972 spin_lock(&osb->dc_task_lock);
3973 /* grab this early so we know to try again if a state change and
3974 * wake happens part-way through our work */
3975 osb->dc_work_sequence = osb->dc_wake_sequence;
3976
3977 processed = osb->blocked_lock_count;
3978 while (processed) {
3979 BUG_ON(list_empty(&osb->blocked_lock_list));
3980
3981 lockres = list_entry(osb->blocked_lock_list.next,
3982 struct ocfs2_lock_res, l_blocked_list);
3983 list_del_init(&lockres->l_blocked_list);
3984 osb->blocked_lock_count--;
3985 spin_unlock(&osb->dc_task_lock);
3986
3987 BUG_ON(!processed);
3988 processed--;
3989
3990 ocfs2_process_blocked_lock(osb, lockres);
3991
3992 spin_lock(&osb->dc_task_lock);
3993 }
3994 spin_unlock(&osb->dc_task_lock);
3995
3996 mlog_exit_void();
3997}
3998
3999static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4000{
4001 int empty = 0;
4002
4003 spin_lock(&osb->dc_task_lock);
4004 if (list_empty(&osb->blocked_lock_list))
4005 empty = 1;
4006
4007 spin_unlock(&osb->dc_task_lock);
4008 return empty;
4009}
4010
4011static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4012{
4013 int should_wake = 0;
4014
4015 spin_lock(&osb->dc_task_lock);
4016 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4017 should_wake = 1;
4018 spin_unlock(&osb->dc_task_lock);
4019
4020 return should_wake;
4021}
4022
Adrian Bunk200bfae2008-02-17 10:20:38 +02004023static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004024{
4025 int status = 0;
4026 struct ocfs2_super *osb = arg;
4027
4028 /* only quit once we've been asked to stop and there is no more
4029 * work available */
4030 while (!(kthread_should_stop() &&
4031 ocfs2_downconvert_thread_lists_empty(osb))) {
4032
4033 wait_event_interruptible(osb->dc_event,
4034 ocfs2_downconvert_thread_should_wake(osb) ||
4035 kthread_should_stop());
4036
4037 mlog(0, "downconvert_thread: awoken\n");
4038
4039 ocfs2_downconvert_thread_do_work(osb);
4040 }
4041
4042 osb->dc_task = NULL;
4043 return status;
4044}
4045
4046void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4047{
4048 spin_lock(&osb->dc_task_lock);
4049 /* make sure the voting thread gets a swipe at whatever changes
4050 * the caller may have made to the voting state */
4051 osb->dc_wake_sequence++;
4052 spin_unlock(&osb->dc_task_lock);
4053 wake_up(&osb->dc_event);
4054}