blob: 1ba67df0092469f56d553b9685a272e4c75e86e6 [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 Fasheh16d5b9562006-09-13 21:10:12 -0700179 * Allow a lock type to add checks to determine whether it is
180 * safe to downconvert a lock. Return 0 to re-queue the
181 * downconvert at a later time, nonzero to continue.
182 *
183 * For most locks, the default checks that there are no
184 * incompatible holders are sufficient.
185 *
186 * Called with the lockres spinlock held.
187 */
188 int (*check_downconvert)(struct ocfs2_lock_res *, int);
189
190 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700191 * Allows a lock type to populate the lock value block. This
192 * is called on downconvert, and when we drop a lock.
193 *
194 * Locks that want to use this should set LOCK_TYPE_USES_LVB
195 * in the flags field.
196 *
197 * Called with the lockres spinlock held.
198 */
199 void (*set_lvb)(struct ocfs2_lock_res *);
200
201 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700202 * Called from the downconvert thread when it is determined
203 * that a lock will be downconverted. This is called without
204 * any locks held so the function can do work that might
205 * schedule (syncing out data, etc).
206 *
207 * This should return any one of the ocfs2_unblock_action
208 * values, depending on what it wants the thread to do.
209 */
210 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
211
212 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700213 * LOCK_TYPE_* flags which describe the specific requirements
214 * of a lock type. Descriptions of each individual flag follow.
215 */
216 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800217};
218
Mark Fashehf625c972006-09-12 21:24:53 -0700219/*
220 * Some locks want to "refresh" potentially stale data when a
221 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
222 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
223 * individual lockres l_flags member from the ast function. It is
224 * expected that the locking wrapper will clear the
225 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
226 */
227#define LOCK_TYPE_REQUIRES_REFRESH 0x1
228
Mark Fashehb80fc012006-09-12 22:08:14 -0700229/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700230 * Indicate that a lock type makes use of the lock value block. The
231 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700232 */
233#define LOCK_TYPE_USES_LVB 0x2
234
Mark Fashehccd979b2005-12-15 14:31:24 -0800235static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700236 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700237 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800238};
239
Mark Fashehe63aecb62007-10-18 15:30:42 -0700240static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700241 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700242 .check_downconvert = ocfs2_check_meta_downconvert,
243 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700244 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700245 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800246};
247
Mark Fashehccd979b2005-12-15 14:31:24 -0800248static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700249 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800250};
251
252static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700253 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800254};
255
wengang wang6ca497a2009-03-06 21:29:10 +0800256static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
257 .flags = 0,
258};
259
Srinivas Eeda83273932009-06-03 17:02:55 -0700260static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
261 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
262};
263
Mark Fashehd680efe2006-09-08 14:14:34 -0700264static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700265 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700266 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700267 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700268 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700269};
270
Tiger Yang50008632007-03-20 16:01:38 -0700271static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
272 .get_osb = ocfs2_get_inode_osb,
273 .flags = 0,
274};
275
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800276static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
277 .get_osb = ocfs2_get_file_osb,
278 .flags = 0,
279};
280
Jan Kara9e33d692008-08-25 19:56:50 +0200281static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
282 .set_lvb = ocfs2_set_qinfo_lvb,
283 .get_osb = ocfs2_get_qinfo_osb,
284 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
285};
286
Tao Ma8dec98e2009-08-18 11:19:58 +0800287static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
288 .check_downconvert = ocfs2_check_refcount_downconvert,
289 .downconvert_worker = ocfs2_refcount_convert_worker,
290 .flags = 0,
291};
292
Mark Fashehccd979b2005-12-15 14:31:24 -0800293static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
294{
295 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700296 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
297 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800298}
299
Joel Beckera796d282010-01-28 19:22:39 -0800300static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(union ocfs2_dlm_lksb *lksb)
301{
302 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
303}
304
Mark Fashehccd979b2005-12-15 14:31:24 -0800305static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
306{
307 BUG_ON(!ocfs2_is_inode_lock(lockres));
308
309 return (struct inode *) lockres->l_priv;
310}
311
Mark Fashehd680efe2006-09-08 14:14:34 -0700312static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
313{
314 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
315
316 return (struct ocfs2_dentry_lock *)lockres->l_priv;
317}
318
Jan Kara9e33d692008-08-25 19:56:50 +0200319static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
320{
321 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
322
323 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
324}
325
Tao Ma8dec98e2009-08-18 11:19:58 +0800326static inline struct ocfs2_refcount_tree *
327ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
328{
329 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
330}
331
Mark Fasheh54a7e752006-09-12 21:49:13 -0700332static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
333{
334 if (lockres->l_ops->get_osb)
335 return lockres->l_ops->get_osb(lockres);
336
337 return (struct ocfs2_super *)lockres->l_priv;
338}
339
Mark Fashehccd979b2005-12-15 14:31:24 -0800340static int ocfs2_lock_create(struct ocfs2_super *osb,
341 struct ocfs2_lock_res *lockres,
342 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800343 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800344static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
345 int wanted);
Jan Karacb257972009-06-04 15:26:50 +0200346static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
347 struct ocfs2_lock_res *lockres,
348 int level, unsigned long caller_ip);
349static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
350 struct ocfs2_lock_res *lockres,
351 int level)
352{
353 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
354}
355
Mark Fashehccd979b2005-12-15 14:31:24 -0800356static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
357static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
358static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
359static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
360static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
361 struct ocfs2_lock_res *lockres);
362static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
363 int convert);
Sunil Mushranc74ff8b2009-02-03 12:37:14 -0800364#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
365 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
366 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
367 _err, _func, _lockres->l_name); \
368 else \
369 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
370 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
371 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800372} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700373static int ocfs2_downconvert_thread(void *arg);
374static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
375 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700376static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 struct buffer_head **bh);
378static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
379static inline int ocfs2_highest_compat_lock_level(int level);
Joel Beckerde551242008-02-01 14:45:08 -0800380static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
381 int new_level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800382static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
383 struct ocfs2_lock_res *lockres,
384 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -0800385 int lvb,
386 unsigned int generation);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800387static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
388 struct ocfs2_lock_res *lockres);
389static int ocfs2_cancel_convert(struct ocfs2_super *osb,
390 struct ocfs2_lock_res *lockres);
391
Mark Fashehccd979b2005-12-15 14:31:24 -0800392
Mark Fashehccd979b2005-12-15 14:31:24 -0800393static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
394 u64 blkno,
395 u32 generation,
396 char *name)
397{
398 int len;
399
400 mlog_entry_void();
401
402 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
403
Mark Fashehb0697052006-03-03 10:24:33 -0800404 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
405 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
406 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800407
408 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
409
410 mlog(0, "built lock resource with name: %s\n", name);
411
412 mlog_exit_void();
413}
414
Ingo Molnar34af9462006-06-27 02:53:55 -0700415static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800416
417static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
418 struct ocfs2_dlm_debug *dlm_debug)
419{
420 mlog(0, "Add tracking for lockres %s\n", res->l_name);
421
422 spin_lock(&ocfs2_dlm_tracking_lock);
423 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
424 spin_unlock(&ocfs2_dlm_tracking_lock);
425}
426
427static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
428{
429 spin_lock(&ocfs2_dlm_tracking_lock);
430 if (!list_empty(&res->l_debug_list))
431 list_del_init(&res->l_debug_list);
432 spin_unlock(&ocfs2_dlm_tracking_lock);
433}
434
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700435#ifdef CONFIG_OCFS2_FS_STATS
436static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
437{
438 res->l_lock_num_prmode = 0;
439 res->l_lock_num_prmode_failed = 0;
440 res->l_lock_total_prmode = 0;
441 res->l_lock_max_prmode = 0;
442 res->l_lock_num_exmode = 0;
443 res->l_lock_num_exmode_failed = 0;
444 res->l_lock_total_exmode = 0;
445 res->l_lock_max_exmode = 0;
446 res->l_lock_refresh = 0;
447}
448
449static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
450 struct ocfs2_mask_waiter *mw, int ret)
451{
452 unsigned long long *num, *sum;
453 unsigned int *max, *failed;
454 struct timespec ts = current_kernel_time();
455 unsigned long long time = timespec_to_ns(&ts) - mw->mw_lock_start;
456
457 if (level == LKM_PRMODE) {
458 num = &res->l_lock_num_prmode;
459 sum = &res->l_lock_total_prmode;
460 max = &res->l_lock_max_prmode;
461 failed = &res->l_lock_num_prmode_failed;
462 } else if (level == LKM_EXMODE) {
463 num = &res->l_lock_num_exmode;
464 sum = &res->l_lock_total_exmode;
465 max = &res->l_lock_max_exmode;
466 failed = &res->l_lock_num_exmode_failed;
467 } else
468 return;
469
470 (*num)++;
471 (*sum) += time;
472 if (time > *max)
473 *max = time;
474 if (ret)
475 (*failed)++;
476}
477
478static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
479{
480 lockres->l_lock_refresh++;
481}
482
483static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
484{
485 struct timespec ts = current_kernel_time();
486 mw->mw_lock_start = timespec_to_ns(&ts);
487}
488#else
489static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
490{
491}
492static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
493 int level, struct ocfs2_mask_waiter *mw, int ret)
494{
495}
496static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
497{
498}
499static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
500{
501}
502#endif
503
Mark Fashehccd979b2005-12-15 14:31:24 -0800504static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
505 struct ocfs2_lock_res *res,
506 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 struct ocfs2_lock_res_ops *ops,
508 void *priv)
509{
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 res->l_type = type;
511 res->l_ops = ops;
512 res->l_priv = priv;
513
Joel Beckerbd3e7612008-02-01 12:14:57 -0800514 res->l_level = DLM_LOCK_IV;
515 res->l_requested = DLM_LOCK_IV;
516 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517 res->l_action = OCFS2_AST_INVALID;
518 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
519
520 res->l_flags = OCFS2_LOCK_INITIALIZED;
521
522 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -0700523
524 ocfs2_init_lock_stats(res);
Jan Karacb257972009-06-04 15:26:50 +0200525#ifdef CONFIG_DEBUG_LOCK_ALLOC
526 if (type != OCFS2_LOCK_TYPE_OPEN)
527 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
528 &lockdep_keys[type], 0);
529 else
530 res->l_lockdep_map.key = NULL;
531#endif
Mark Fashehccd979b2005-12-15 14:31:24 -0800532}
533
534void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
535{
536 /* This also clears out the lock status block */
537 memset(res, 0, sizeof(struct ocfs2_lock_res));
538 spin_lock_init(&res->l_lock);
539 init_waitqueue_head(&res->l_event);
540 INIT_LIST_HEAD(&res->l_blocked_list);
541 INIT_LIST_HEAD(&res->l_mask_waiters);
542}
543
544void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
545 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700546 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800547 struct inode *inode)
548{
549 struct ocfs2_lock_res_ops *ops;
550
551 switch(type) {
552 case OCFS2_LOCK_TYPE_RW:
553 ops = &ocfs2_inode_rw_lops;
554 break;
555 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700556 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800557 break;
Tiger Yang50008632007-03-20 16:01:38 -0700558 case OCFS2_LOCK_TYPE_OPEN:
559 ops = &ocfs2_inode_open_lops;
560 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800561 default:
562 mlog_bug_on_msg(1, "type: %d\n", type);
563 ops = NULL; /* thanks, gcc */
564 break;
565 };
566
Mark Fashehd680efe2006-09-08 14:14:34 -0700567 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700568 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700569 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
570}
571
Mark Fasheh54a7e752006-09-12 21:49:13 -0700572static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
573{
574 struct inode *inode = ocfs2_lock_res_inode(lockres);
575
576 return OCFS2_SB(inode->i_sb);
577}
578
Jan Kara9e33d692008-08-25 19:56:50 +0200579static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
580{
581 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
582
583 return OCFS2_SB(info->dqi_gi.dqi_sb);
584}
585
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800586static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
587{
588 struct ocfs2_file_private *fp = lockres->l_priv;
589
590 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
591}
592
Mark Fashehd680efe2006-09-08 14:14:34 -0700593static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
594{
595 __be64 inode_blkno_be;
596
597 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
598 sizeof(__be64));
599
600 return be64_to_cpu(inode_blkno_be);
601}
602
Mark Fasheh54a7e752006-09-12 21:49:13 -0700603static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
604{
605 struct ocfs2_dentry_lock *dl = lockres->l_priv;
606
607 return OCFS2_SB(dl->dl_inode->i_sb);
608}
609
Mark Fashehd680efe2006-09-08 14:14:34 -0700610void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
611 u64 parent, struct inode *inode)
612{
613 int len;
614 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
615 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
616 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
617
618 ocfs2_lock_res_init_once(lockres);
619
620 /*
621 * Unfortunately, the standard lock naming scheme won't work
622 * here because we have two 16 byte values to use. Instead,
623 * we'll stuff the inode number as a binary value. We still
624 * want error prints to show something without garbling the
625 * display, so drop a null byte in there before the inode
626 * number. A future version of OCFS2 will likely use all
627 * binary lock names. The stringified names have been a
628 * tremendous aid in debugging, but now that the debugfs
629 * interface exists, we can mangle things there if need be.
630 *
631 * NOTE: We also drop the standard "pad" value (the total lock
632 * name size stays the same though - the last part is all
633 * zeros due to the memset in ocfs2_lock_res_init_once()
634 */
635 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
636 "%c%016llx",
637 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
638 (long long)parent);
639
640 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
641
642 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
643 sizeof(__be64));
644
645 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
646 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
647 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800648}
649
650static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
651 struct ocfs2_super *osb)
652{
653 /* Superblock lockres doesn't come from a slab so we call init
654 * once on it manually. */
655 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700656 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
657 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800658 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800659 &ocfs2_super_lops, osb);
660}
661
662static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
663 struct ocfs2_super *osb)
664{
665 /* Rename lockres doesn't come from a slab so we call init
666 * once on it manually. */
667 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700668 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
669 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800670 &ocfs2_rename_lops, osb);
671}
672
wengang wang6ca497a2009-03-06 21:29:10 +0800673static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
674 struct ocfs2_super *osb)
675{
676 /* nfs_sync lockres doesn't come from a slab so we call init
677 * once on it manually. */
678 ocfs2_lock_res_init_once(res);
679 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
680 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
681 &ocfs2_nfs_sync_lops, osb);
682}
683
Srinivas Eeda83273932009-06-03 17:02:55 -0700684static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
685 struct ocfs2_super *osb)
686{
Srinivas Eeda83273932009-06-03 17:02:55 -0700687 ocfs2_lock_res_init_once(res);
688 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
689 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
690 &ocfs2_orphan_scan_lops, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -0700691}
692
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800693void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
694 struct ocfs2_file_private *fp)
695{
696 struct inode *inode = fp->fp_file->f_mapping->host;
697 struct ocfs2_inode_info *oi = OCFS2_I(inode);
698
699 ocfs2_lock_res_init_once(lockres);
700 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
701 inode->i_generation, lockres->l_name);
702 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
703 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
704 fp);
705 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
706}
707
Jan Kara9e33d692008-08-25 19:56:50 +0200708void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
709 struct ocfs2_mem_dqinfo *info)
710{
711 ocfs2_lock_res_init_once(lockres);
712 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
713 0, lockres->l_name);
714 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
715 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
716 info);
717}
718
Tao Ma8dec98e2009-08-18 11:19:58 +0800719void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
720 struct ocfs2_super *osb, u64 ref_blkno,
721 unsigned int generation)
722{
723 ocfs2_lock_res_init_once(lockres);
724 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
725 generation, lockres->l_name);
726 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
727 &ocfs2_refcount_block_lops, osb);
728}
729
Mark Fashehccd979b2005-12-15 14:31:24 -0800730void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
731{
732 mlog_entry_void();
733
734 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
735 return;
736
737 ocfs2_remove_lockres_tracking(res);
738
739 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
740 "Lockres %s is on the blocked list\n",
741 res->l_name);
742 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
743 "Lockres %s has mask waiters pending\n",
744 res->l_name);
745 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
746 "Lockres %s is locked\n",
747 res->l_name);
748 mlog_bug_on_msg(res->l_ro_holders,
749 "Lockres %s has %u ro holders\n",
750 res->l_name, res->l_ro_holders);
751 mlog_bug_on_msg(res->l_ex_holders,
752 "Lockres %s has %u ex holders\n",
753 res->l_name, res->l_ex_holders);
754
755 /* Need to clear out the lock status block for the dlm */
756 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
757
758 res->l_flags = 0UL;
759 mlog_exit_void();
760}
761
762static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
763 int level)
764{
765 mlog_entry_void();
766
767 BUG_ON(!lockres);
768
769 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800770 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800771 lockres->l_ex_holders++;
772 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800773 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800774 lockres->l_ro_holders++;
775 break;
776 default:
777 BUG();
778 }
779
780 mlog_exit_void();
781}
782
783static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
784 int level)
785{
786 mlog_entry_void();
787
788 BUG_ON(!lockres);
789
790 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800791 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800792 BUG_ON(!lockres->l_ex_holders);
793 lockres->l_ex_holders--;
794 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800795 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800796 BUG_ON(!lockres->l_ro_holders);
797 lockres->l_ro_holders--;
798 break;
799 default:
800 BUG();
801 }
802 mlog_exit_void();
803}
804
805/* WARNING: This function lives in a world where the only three lock
806 * levels are EX, PR, and NL. It *will* have to be adjusted when more
807 * lock types are added. */
808static inline int ocfs2_highest_compat_lock_level(int level)
809{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800810 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811
Joel Beckerbd3e7612008-02-01 12:14:57 -0800812 if (level == DLM_LOCK_EX)
813 new_level = DLM_LOCK_NL;
814 else if (level == DLM_LOCK_PR)
815 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 return new_level;
817}
818
819static void lockres_set_flags(struct ocfs2_lock_res *lockres,
820 unsigned long newflags)
821{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200822 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800823
824 assert_spin_locked(&lockres->l_lock);
825
826 lockres->l_flags = newflags;
827
Christoph Hellwig800deef2007-05-17 16:03:13 +0200828 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
830 continue;
831
832 list_del_init(&mw->mw_item);
833 mw->mw_status = 0;
834 complete(&mw->mw_complete);
835 }
836}
837static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
838{
839 lockres_set_flags(lockres, lockres->l_flags | or);
840}
841static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
842 unsigned long clear)
843{
844 lockres_set_flags(lockres, lockres->l_flags & ~clear);
845}
846
847static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
848{
849 mlog_entry_void();
850
851 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
852 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
853 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800854 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800855
856 lockres->l_level = lockres->l_requested;
857 if (lockres->l_level <=
858 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800859 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800860 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
861 }
862 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
863
864 mlog_exit_void();
865}
866
867static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
868{
869 mlog_entry_void();
870
871 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
872 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
873
874 /* Convert from RO to EX doesn't really need anything as our
875 * information is already up to data. Convert from NL to
876 * *anything* however should mark ourselves as needing an
877 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800878 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700879 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800880 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
881
882 lockres->l_level = lockres->l_requested;
Sunil Mushrana1912822010-01-21 10:50:03 -0800883
884 /*
885 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
886 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
887 * downconverting the lock before the upconvert has fully completed.
888 */
889 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
890
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
892
893 mlog_exit_void();
894}
895
896static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
897{
898 mlog_entry_void();
899
Roel Kluin3cf0c502007-10-27 00:20:36 +0200900 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800901 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
902
Joel Beckerbd3e7612008-02-01 12:14:57 -0800903 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700904 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
905 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800906 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
907
908 lockres->l_level = lockres->l_requested;
909 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
910 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
911
912 mlog_exit_void();
913}
914
Mark Fashehccd979b2005-12-15 14:31:24 -0800915static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
916 int level)
917{
918 int needs_downconvert = 0;
919 mlog_entry_void();
920
921 assert_spin_locked(&lockres->l_lock);
922
Mark Fashehccd979b2005-12-15 14:31:24 -0800923 if (level > lockres->l_blocking) {
924 /* only schedule a downconvert if we haven't already scheduled
925 * one that goes low enough to satisfy the level we're
926 * blocking. this also catches the case where we get
927 * duplicate BASTs */
928 if (ocfs2_highest_compat_lock_level(level) <
929 ocfs2_highest_compat_lock_level(lockres->l_blocking))
930 needs_downconvert = 1;
931
932 lockres->l_blocking = level;
933 }
934
Wengang Wang0b94a902010-01-21 10:50:02 -0800935 if (needs_downconvert)
936 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
937
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 mlog_exit(needs_downconvert);
939 return needs_downconvert;
940}
941
Joel Beckerde551242008-02-01 14:45:08 -0800942/*
943 * OCFS2_LOCK_PENDING and l_pending_gen.
944 *
945 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
946 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
947 * for more details on the race.
948 *
949 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
950 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
951 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
952 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
953 * the caller is going to try to clear PENDING again. If nothing else is
954 * happening, __lockres_clear_pending() sees PENDING is unset and does
955 * nothing.
956 *
957 * But what if another path (eg downconvert thread) has just started a
958 * new locking action? The other path has re-set PENDING. Our path
959 * cannot clear PENDING, because that will re-open the original race
960 * window.
961 *
962 * [Example]
963 *
964 * ocfs2_meta_lock()
965 * ocfs2_cluster_lock()
966 * set BUSY
967 * set PENDING
968 * drop l_lock
969 * ocfs2_dlm_lock()
970 * ocfs2_locking_ast() ocfs2_downconvert_thread()
971 * clear PENDING ocfs2_unblock_lock()
972 * take_l_lock
973 * !BUSY
974 * ocfs2_prepare_downconvert()
975 * set BUSY
976 * set PENDING
977 * drop l_lock
978 * take l_lock
979 * clear PENDING
980 * drop l_lock
981 * <window>
982 * ocfs2_dlm_lock()
983 *
984 * So as you can see, we now have a window where l_lock is not held,
985 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
986 *
987 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
988 * set by ocfs2_prepare_downconvert(). That wasn't nice.
989 *
990 * To solve this we introduce l_pending_gen. A call to
991 * lockres_clear_pending() will only do so when it is passed a generation
992 * number that matches the lockres. lockres_set_pending() will return the
993 * current generation number. When ocfs2_cluster_lock() goes to clear
994 * PENDING, it passes the generation it got from set_pending(). In our
995 * example above, the generation numbers will *not* match. Thus,
996 * ocfs2_cluster_lock() will not clear the PENDING set by
997 * ocfs2_prepare_downconvert().
998 */
999
1000/* Unlocked version for ocfs2_locking_ast() */
1001static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
1002 unsigned int generation,
1003 struct ocfs2_super *osb)
1004{
1005 assert_spin_locked(&lockres->l_lock);
1006
1007 /*
1008 * The ast and locking functions can race us here. The winner
1009 * will clear pending, the loser will not.
1010 */
1011 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
1012 (lockres->l_pending_gen != generation))
1013 return;
1014
1015 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1016 lockres->l_pending_gen++;
1017
1018 /*
1019 * The downconvert thread may have skipped us because we
1020 * were PENDING. Wake it up.
1021 */
1022 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1023 ocfs2_wake_downconvert_thread(osb);
1024}
1025
1026/* Locked version for callers of ocfs2_dlm_lock() */
1027static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1028 unsigned int generation,
1029 struct ocfs2_super *osb)
1030{
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&lockres->l_lock, flags);
1034 __lockres_clear_pending(lockres, generation, osb);
1035 spin_unlock_irqrestore(&lockres->l_lock, flags);
1036}
1037
1038static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1039{
1040 assert_spin_locked(&lockres->l_lock);
1041 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1042
1043 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1044
1045 return lockres->l_pending_gen;
1046}
1047
1048
Joel Beckera796d282010-01-28 19:22:39 -08001049static void ocfs2_blocking_ast(union ocfs2_dlm_lksb *lksb, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -08001050{
Joel Beckera796d282010-01-28 19:22:39 -08001051 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehaa2623a2006-09-12 21:58:23 -07001052 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001053 int needs_downconvert;
1054 unsigned long flags;
1055
Joel Beckerbd3e7612008-02-01 12:14:57 -08001056 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001057
Mark Fashehaa2623a2006-09-12 21:58:23 -07001058 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
1059 lockres->l_name, level, lockres->l_level,
1060 ocfs2_lock_type_string(lockres->l_type));
1061
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001062 /*
1063 * We can skip the bast for locks which don't enable caching -
1064 * they'll be dropped at the earliest possible time anyway.
1065 */
1066 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1067 return;
1068
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 spin_lock_irqsave(&lockres->l_lock, flags);
1070 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1071 if (needs_downconvert)
1072 ocfs2_schedule_blocked_lock(osb, lockres);
1073 spin_unlock_irqrestore(&lockres->l_lock, flags);
1074
Mark Fashehd680efe2006-09-08 14:14:34 -07001075 wake_up(&lockres->l_event);
1076
Mark Fasheh34d024f2007-09-24 15:56:19 -07001077 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001078}
1079
Joel Beckera796d282010-01-28 19:22:39 -08001080static void ocfs2_locking_ast(union ocfs2_dlm_lksb *lksb)
Mark Fashehccd979b2005-12-15 14:31:24 -08001081{
Joel Beckera796d282010-01-28 19:22:39 -08001082 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Joel Beckerde551242008-02-01 14:45:08 -08001083 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 unsigned long flags;
David Teigland1693a5c2008-01-30 16:52:53 -08001085 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08001086
1087 spin_lock_irqsave(&lockres->l_lock, flags);
1088
David Teigland1693a5c2008-01-30 16:52:53 -08001089 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1090
1091 if (status == -EAGAIN) {
1092 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1093 goto out;
1094 }
1095
1096 if (status) {
Joel Becker8f2c9c12008-02-01 12:16:57 -08001097 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
David Teigland1693a5c2008-01-30 16:52:53 -08001098 lockres->l_name, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 spin_unlock_irqrestore(&lockres->l_lock, flags);
1100 return;
1101 }
1102
1103 switch(lockres->l_action) {
1104 case OCFS2_AST_ATTACH:
1105 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -07001106 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001107 break;
1108 case OCFS2_AST_CONVERT:
1109 ocfs2_generic_handle_convert_action(lockres);
1110 break;
1111 case OCFS2_AST_DOWNCONVERT:
1112 ocfs2_generic_handle_downconvert_action(lockres);
1113 break;
1114 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -07001115 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
1116 "lockres flags = 0x%lx, unlock action: %u\n",
1117 lockres->l_name, lockres->l_action, lockres->l_flags,
1118 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -08001119 BUG();
1120 }
David Teigland1693a5c2008-01-30 16:52:53 -08001121out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001122 /* set it to something invalid so if we get called again we
1123 * can catch it. */
1124 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08001125
Joel Beckerde551242008-02-01 14:45:08 -08001126 /* Did we try to cancel this lock? Clear that state */
1127 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1128 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1129
1130 /*
1131 * We may have beaten the locking functions here. We certainly
1132 * know that dlm_lock() has been called :-)
1133 * Because we can't have two lock calls in flight at once, we
1134 * can use lockres->l_pending_gen.
1135 */
1136 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1137
Mark Fashehccd979b2005-12-15 14:31:24 -08001138 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -07001139 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001140}
1141
Mark Fashehccd979b2005-12-15 14:31:24 -08001142static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1143 int convert)
1144{
1145 unsigned long flags;
1146
1147 mlog_entry_void();
1148 spin_lock_irqsave(&lockres->l_lock, flags);
1149 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Sunil Mushrana1912822010-01-21 10:50:03 -08001150 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
Mark Fashehccd979b2005-12-15 14:31:24 -08001151 if (convert)
1152 lockres->l_action = OCFS2_AST_INVALID;
1153 else
1154 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1155 spin_unlock_irqrestore(&lockres->l_lock, flags);
1156
1157 wake_up(&lockres->l_event);
1158 mlog_exit_void();
1159}
1160
1161/* Note: If we detect another process working on the lock (i.e.,
1162 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1163 * to do the right thing in that case.
1164 */
1165static int ocfs2_lock_create(struct ocfs2_super *osb,
1166 struct ocfs2_lock_res *lockres,
1167 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001168 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -08001169{
1170 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001172 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08001173
1174 mlog_entry_void();
1175
Joel Beckerbd3e7612008-02-01 12:14:57 -08001176 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -08001177 dlm_flags);
1178
1179 spin_lock_irqsave(&lockres->l_lock, flags);
1180 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1181 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1182 spin_unlock_irqrestore(&lockres->l_lock, flags);
1183 goto bail;
1184 }
1185
1186 lockres->l_action = OCFS2_AST_ATTACH;
1187 lockres->l_requested = level;
1188 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001189 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001190 spin_unlock_irqrestore(&lockres->l_lock, flags);
1191
Joel Becker4670c462008-02-01 14:39:35 -08001192 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001193 level,
1194 &lockres->l_lksb,
1195 dlm_flags,
1196 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001197 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001198 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001199 if (ret) {
1200 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 ocfs2_recover_from_dlm_error(lockres, 1);
1202 }
1203
Joel Becker7431cd72008-02-01 12:15:37 -08001204 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001205
1206bail:
1207 mlog_exit(ret);
1208 return ret;
1209}
1210
1211static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1212 int flag)
1213{
1214 unsigned long flags;
1215 int ret;
1216
1217 spin_lock_irqsave(&lockres->l_lock, flags);
1218 ret = lockres->l_flags & flag;
1219 spin_unlock_irqrestore(&lockres->l_lock, flags);
1220
1221 return ret;
1222}
1223
1224static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1225
1226{
1227 wait_event(lockres->l_event,
1228 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1229}
1230
1231static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1232
1233{
1234 wait_event(lockres->l_event,
1235 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1236}
1237
1238/* predict what lock level we'll be dropping down to on behalf
1239 * of another node, and return true if the currently wanted
1240 * level will be compatible with it. */
1241static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1242 int wanted)
1243{
1244 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1245
1246 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1247}
1248
1249static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1250{
1251 INIT_LIST_HEAD(&mw->mw_item);
1252 init_completion(&mw->mw_complete);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001253 ocfs2_init_start_time(mw);
Mark Fashehccd979b2005-12-15 14:31:24 -08001254}
1255
1256static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1257{
1258 wait_for_completion(&mw->mw_complete);
1259 /* Re-arm the completion in case we want to wait on it again */
1260 INIT_COMPLETION(mw->mw_complete);
1261 return mw->mw_status;
1262}
1263
1264static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1265 struct ocfs2_mask_waiter *mw,
1266 unsigned long mask,
1267 unsigned long goal)
1268{
1269 BUG_ON(!list_empty(&mw->mw_item));
1270
1271 assert_spin_locked(&lockres->l_lock);
1272
1273 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1274 mw->mw_mask = mask;
1275 mw->mw_goal = goal;
1276}
1277
1278/* returns 0 if the mw that was removed was already satisfied, -EBUSY
1279 * if the mask still hadn't reached its goal */
1280static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1281 struct ocfs2_mask_waiter *mw)
1282{
1283 unsigned long flags;
1284 int ret = 0;
1285
1286 spin_lock_irqsave(&lockres->l_lock, flags);
1287 if (!list_empty(&mw->mw_item)) {
1288 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1289 ret = -EBUSY;
1290
1291 list_del_init(&mw->mw_item);
1292 init_completion(&mw->mw_complete);
1293 }
1294 spin_unlock_irqrestore(&lockres->l_lock, flags);
1295
1296 return ret;
1297
1298}
1299
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001300static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1301 struct ocfs2_lock_res *lockres)
1302{
1303 int ret;
1304
1305 ret = wait_for_completion_interruptible(&mw->mw_complete);
1306 if (ret)
1307 lockres_remove_mask_waiter(lockres, mw);
1308 else
1309 ret = mw->mw_status;
1310 /* Re-arm the completion in case we want to wait on it again */
1311 INIT_COMPLETION(mw->mw_complete);
1312 return ret;
1313}
1314
Jan Karacb257972009-06-04 15:26:50 +02001315static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1316 struct ocfs2_lock_res *lockres,
1317 int level,
1318 u32 lkm_flags,
1319 int arg_flags,
1320 int l_subclass,
1321 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001322{
1323 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -08001324 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1325 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1326 unsigned long flags;
Joel Beckerde551242008-02-01 14:45:08 -08001327 unsigned int gen;
David Teigland1693a5c2008-01-30 16:52:53 -08001328 int noqueue_attempted = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001329
1330 mlog_entry_void();
1331
1332 ocfs2_init_mask_waiter(&mw);
1333
Mark Fashehb80fc012006-09-12 22:08:14 -07001334 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001335 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001336
Mark Fashehccd979b2005-12-15 14:31:24 -08001337again:
1338 wait = 0;
1339
Sunil Mushrana1912822010-01-21 10:50:03 -08001340 spin_lock_irqsave(&lockres->l_lock, flags);
1341
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 if (catch_signals && signal_pending(current)) {
1343 ret = -ERESTARTSYS;
Sunil Mushrana1912822010-01-21 10:50:03 -08001344 goto unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 }
1346
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1348 "Cluster lock called on freeing lockres %s! flags "
1349 "0x%lx\n", lockres->l_name, lockres->l_flags);
1350
1351 /* We only compare against the currently granted level
1352 * here. If the lock is blocked waiting on a downconvert,
1353 * we'll get caught below. */
1354 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1355 level > lockres->l_level) {
1356 /* is someone sitting in dlm_lock? If so, wait on
1357 * them. */
1358 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1359 wait = 1;
1360 goto unlock;
1361 }
1362
Sunil Mushrana1912822010-01-21 10:50:03 -08001363 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1364 /*
1365 * We've upconverted. If the lock now has a level we can
1366 * work with, we take it. If, however, the lock is not at the
1367 * required level, we go thru the full cycle. One way this could
1368 * happen is if a process requesting an upconvert to PR is
1369 * closely followed by another requesting upconvert to an EX.
1370 * If the process requesting EX lands here, we want it to
1371 * continue attempting to upconvert and let the process
1372 * requesting PR take the lock.
1373 * If multiple processes request upconvert to PR, the first one
1374 * here will take the lock. The others will have to go thru the
1375 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1376 * downconvert request.
1377 */
1378 if (level <= lockres->l_level)
1379 goto update_holders;
1380 }
1381
Mark Fashehccd979b2005-12-15 14:31:24 -08001382 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1383 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1384 /* is the lock is currently blocked on behalf of
1385 * another node */
1386 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1387 wait = 1;
1388 goto unlock;
1389 }
1390
1391 if (level > lockres->l_level) {
David Teigland1693a5c2008-01-30 16:52:53 -08001392 if (noqueue_attempted > 0) {
1393 ret = -EAGAIN;
1394 goto unlock;
1395 }
1396 if (lkm_flags & DLM_LKF_NOQUEUE)
1397 noqueue_attempted = 1;
1398
Mark Fashehccd979b2005-12-15 14:31:24 -08001399 if (lockres->l_action != OCFS2_AST_INVALID)
1400 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1401 lockres->l_name, lockres->l_action);
1402
Mark Fasheh019d1b22007-10-05 12:09:05 -07001403 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1404 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001405 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001406 } else {
1407 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001408 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001409 }
1410
Mark Fashehccd979b2005-12-15 14:31:24 -08001411 lockres->l_requested = level;
1412 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08001413 gen = lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001414 spin_unlock_irqrestore(&lockres->l_lock, flags);
1415
Joel Beckerbd3e7612008-02-01 12:14:57 -08001416 BUG_ON(level == DLM_LOCK_IV);
1417 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001418
1419 mlog(0, "lock %s, convert from %d to level = %d\n",
1420 lockres->l_name, lockres->l_level, level);
1421
1422 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001423 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001424 level,
1425 &lockres->l_lksb,
1426 lkm_flags,
1427 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08001428 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08001429 lockres_clear_pending(lockres, gen, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08001430 if (ret) {
1431 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1432 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001433 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001434 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001435 }
1436 ocfs2_recover_from_dlm_error(lockres, 1);
1437 goto out;
1438 }
1439
Coly Li73ac36e2009-01-07 18:09:16 -08001440 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001441 lockres->l_name);
1442
1443 /* At this point we've gone inside the dlm and need to
1444 * complete our work regardless. */
1445 catch_signals = 0;
1446
1447 /* wait for busy to clear and carry on */
1448 goto again;
1449 }
1450
Sunil Mushrana1912822010-01-21 10:50:03 -08001451update_holders:
Mark Fashehccd979b2005-12-15 14:31:24 -08001452 /* Ok, if we get here then we're good to go. */
1453 ocfs2_inc_holders(lockres, level);
1454
1455 ret = 0;
1456unlock:
Sunil Mushrana1912822010-01-21 10:50:03 -08001457 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1458
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 spin_unlock_irqrestore(&lockres->l_lock, flags);
1460out:
1461 /*
1462 * This is helping work around a lock inversion between the page lock
1463 * and dlm locks. One path holds the page lock while calling aops
1464 * which block acquiring dlm locks. The voting thread holds dlm
1465 * locks while acquiring page locks while down converting data locks.
1466 * This block is helping an aop path notice the inversion and back
1467 * off to unlock its page lock before trying the dlm lock again.
1468 */
1469 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1470 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1471 wait = 0;
1472 if (lockres_remove_mask_waiter(lockres, &mw))
1473 ret = -EAGAIN;
1474 else
1475 goto again;
1476 }
1477 if (wait) {
1478 ret = ocfs2_wait_for_mask(&mw);
1479 if (ret == 0)
1480 goto again;
1481 mlog_errno(ret);
1482 }
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07001483 ocfs2_update_lock_stats(lockres, level, &mw, ret);
Mark Fashehccd979b2005-12-15 14:31:24 -08001484
Jan Karacb257972009-06-04 15:26:50 +02001485#ifdef CONFIG_DEBUG_LOCK_ALLOC
1486 if (!ret && lockres->l_lockdep_map.key != NULL) {
1487 if (level == DLM_LOCK_PR)
1488 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1489 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1490 caller_ip);
1491 else
1492 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1493 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1494 caller_ip);
1495 }
1496#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 mlog_exit(ret);
1498 return ret;
1499}
1500
Jan Karacb257972009-06-04 15:26:50 +02001501static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1502 struct ocfs2_lock_res *lockres,
1503 int level,
1504 u32 lkm_flags,
1505 int arg_flags)
1506{
1507 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1508 0, _RET_IP_);
1509}
1510
1511
1512static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1513 struct ocfs2_lock_res *lockres,
1514 int level,
1515 unsigned long caller_ip)
Mark Fashehccd979b2005-12-15 14:31:24 -08001516{
1517 unsigned long flags;
1518
1519 mlog_entry_void();
1520 spin_lock_irqsave(&lockres->l_lock, flags);
1521 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001522 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001523 spin_unlock_irqrestore(&lockres->l_lock, flags);
Jan Karacb257972009-06-04 15:26:50 +02001524#ifdef CONFIG_DEBUG_LOCK_ALLOC
1525 if (lockres->l_lockdep_map.key != NULL)
1526 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1527#endif
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 mlog_exit_void();
1529}
1530
Adrian Bunkda661162006-11-20 03:24:28 +01001531static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1532 struct ocfs2_lock_res *lockres,
1533 int ex,
1534 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001535{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001536 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001538 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001539
1540 spin_lock_irqsave(&lockres->l_lock, flags);
1541 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1542 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1543 spin_unlock_irqrestore(&lockres->l_lock, flags);
1544
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001545 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001546}
1547
1548/* Grants us an EX lock on the data and metadata resources, skipping
1549 * the normal cluster directory lookup. Use this ONLY on newly created
1550 * inodes which other nodes can't possibly see, and which haven't been
1551 * hashed in the inode hash yet. This can give us a good performance
1552 * increase as it'll skip the network broadcast normally associated
1553 * with creating a new lock resource. */
1554int ocfs2_create_new_inode_locks(struct inode *inode)
1555{
1556 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001557 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001558
1559 BUG_ON(!inode);
1560 BUG_ON(!ocfs2_inode_is_new(inode));
1561
1562 mlog_entry_void();
1563
Mark Fashehb0697052006-03-03 10:24:33 -08001564 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001565
1566 /* NOTE: That we don't increment any of the holder counts, nor
1567 * do we add anything to a journal handle. Since this is
1568 * supposed to be a new inode which the cluster doesn't know
1569 * about yet, there is no need to. As far as the LVB handling
1570 * is concerned, this is basically like acquiring an EX lock
1571 * on a resource which has an invalid one -- we'll set it
1572 * valid when we release the EX. */
1573
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001574 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001575 if (ret) {
1576 mlog_errno(ret);
1577 goto bail;
1578 }
1579
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001580 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001581 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001582 * don't use a generation in their lock names.
1583 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001584 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001585 if (ret) {
1586 mlog_errno(ret);
1587 goto bail;
1588 }
1589
Tiger Yang50008632007-03-20 16:01:38 -07001590 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1591 if (ret) {
1592 mlog_errno(ret);
1593 goto bail;
1594 }
1595
Mark Fashehccd979b2005-12-15 14:31:24 -08001596bail:
1597 mlog_exit(ret);
1598 return ret;
1599}
1600
1601int ocfs2_rw_lock(struct inode *inode, int write)
1602{
1603 int status, level;
1604 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001605 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001606
1607 BUG_ON(!inode);
1608
1609 mlog_entry_void();
1610
Mark Fashehb0697052006-03-03 10:24:33 -08001611 mlog(0, "inode %llu take %s RW lock\n",
1612 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 write ? "EXMODE" : "PRMODE");
1614
Coly Lid92bc512009-08-28 19:03:18 +08001615 if (ocfs2_mount_local(osb)) {
1616 mlog_exit(0);
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001617 return 0;
Coly Lid92bc512009-08-28 19:03:18 +08001618 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001619
Mark Fashehccd979b2005-12-15 14:31:24 -08001620 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1621
Joel Beckerbd3e7612008-02-01 12:14:57 -08001622 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001623
1624 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1625 0);
1626 if (status < 0)
1627 mlog_errno(status);
1628
1629 mlog_exit(status);
1630 return status;
1631}
1632
1633void ocfs2_rw_unlock(struct inode *inode, int write)
1634{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001635 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001636 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001637 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001638
1639 mlog_entry_void();
1640
Mark Fashehb0697052006-03-03 10:24:33 -08001641 mlog(0, "inode %llu drop %s RW lock\n",
1642 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001643 write ? "EXMODE" : "PRMODE");
1644
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001645 if (!ocfs2_mount_local(osb))
1646 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001647
1648 mlog_exit_void();
1649}
1650
Tiger Yang50008632007-03-20 16:01:38 -07001651/*
1652 * ocfs2_open_lock always get PR mode lock.
1653 */
1654int ocfs2_open_lock(struct inode *inode)
1655{
1656 int status = 0;
1657 struct ocfs2_lock_res *lockres;
1658 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1659
1660 BUG_ON(!inode);
1661
1662 mlog_entry_void();
1663
1664 mlog(0, "inode %llu take PRMODE open lock\n",
1665 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1666
1667 if (ocfs2_mount_local(osb))
1668 goto out;
1669
1670 lockres = &OCFS2_I(inode)->ip_open_lockres;
1671
1672 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001673 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001674 if (status < 0)
1675 mlog_errno(status);
1676
1677out:
1678 mlog_exit(status);
1679 return status;
1680}
1681
1682int ocfs2_try_open_lock(struct inode *inode, int write)
1683{
1684 int status = 0, level;
1685 struct ocfs2_lock_res *lockres;
1686 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1687
1688 BUG_ON(!inode);
1689
1690 mlog_entry_void();
1691
1692 mlog(0, "inode %llu try to take %s open lock\n",
1693 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1694 write ? "EXMODE" : "PRMODE");
1695
1696 if (ocfs2_mount_local(osb))
1697 goto out;
1698
1699 lockres = &OCFS2_I(inode)->ip_open_lockres;
1700
Joel Beckerbd3e7612008-02-01 12:14:57 -08001701 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001702
1703 /*
1704 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001705 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001706 * other nodes and the -EAGAIN will indicate to the caller that
1707 * this inode is still in use.
1708 */
1709 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001710 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001711
1712out:
1713 mlog_exit(status);
1714 return status;
1715}
1716
1717/*
1718 * ocfs2_open_unlock unlock PR and EX mode open locks.
1719 */
1720void ocfs2_open_unlock(struct inode *inode)
1721{
1722 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1723 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1724
1725 mlog_entry_void();
1726
1727 mlog(0, "inode %llu drop open lock\n",
1728 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1729
1730 if (ocfs2_mount_local(osb))
1731 goto out;
1732
1733 if(lockres->l_ro_holders)
1734 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001735 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001736 if(lockres->l_ex_holders)
1737 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001738 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001739
1740out:
1741 mlog_exit_void();
1742}
1743
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001744static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1745 int level)
1746{
1747 int ret;
1748 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1749 unsigned long flags;
1750 struct ocfs2_mask_waiter mw;
1751
1752 ocfs2_init_mask_waiter(&mw);
1753
1754retry_cancel:
1755 spin_lock_irqsave(&lockres->l_lock, flags);
1756 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1757 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1758 if (ret) {
1759 spin_unlock_irqrestore(&lockres->l_lock, flags);
1760 ret = ocfs2_cancel_convert(osb, lockres);
1761 if (ret < 0) {
1762 mlog_errno(ret);
1763 goto out;
1764 }
1765 goto retry_cancel;
1766 }
1767 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1768 spin_unlock_irqrestore(&lockres->l_lock, flags);
1769
1770 ocfs2_wait_for_mask(&mw);
1771 goto retry_cancel;
1772 }
1773
1774 ret = -ERESTARTSYS;
1775 /*
1776 * We may still have gotten the lock, in which case there's no
1777 * point to restarting the syscall.
1778 */
1779 if (lockres->l_level == level)
1780 ret = 0;
1781
1782 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1783 lockres->l_flags, lockres->l_level, lockres->l_action);
1784
1785 spin_unlock_irqrestore(&lockres->l_lock, flags);
1786
1787out:
1788 return ret;
1789}
1790
1791/*
1792 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1793 * flock() calls. The locking approach this requires is sufficiently
1794 * different from all other cluster lock types that we implement a
1795 * seperate path to the "low-level" dlm calls. In particular:
1796 *
1797 * - No optimization of lock levels is done - we take at exactly
1798 * what's been requested.
1799 *
1800 * - No lock caching is employed. We immediately downconvert to
1801 * no-lock at unlock time. This also means flock locks never go on
1802 * the blocking list).
1803 *
1804 * - Since userspace can trivially deadlock itself with flock, we make
1805 * sure to allow cancellation of a misbehaving applications flock()
1806 * request.
1807 *
1808 * - Access to any flock lockres doesn't require concurrency, so we
1809 * can simplify the code by requiring the caller to guarantee
1810 * serialization of dlmglue flock calls.
1811 */
1812int ocfs2_file_lock(struct file *file, int ex, int trylock)
1813{
Mark Fashehe988cf12008-07-10 09:25:39 -07001814 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1815 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001816 unsigned long flags;
1817 struct ocfs2_file_private *fp = file->private_data;
1818 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1819 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1820 struct ocfs2_mask_waiter mw;
1821
1822 ocfs2_init_mask_waiter(&mw);
1823
1824 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001825 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001826 mlog(ML_ERROR,
1827 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1828 "level: %u\n", lockres->l_name, lockres->l_flags,
1829 lockres->l_level);
1830 return -EINVAL;
1831 }
1832
1833 spin_lock_irqsave(&lockres->l_lock, flags);
1834 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1835 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1836 spin_unlock_irqrestore(&lockres->l_lock, flags);
1837
1838 /*
1839 * Get the lock at NLMODE to start - that way we
1840 * can cancel the upconvert request if need be.
1841 */
Mark Fashehe988cf12008-07-10 09:25:39 -07001842 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001843 if (ret < 0) {
1844 mlog_errno(ret);
1845 goto out;
1846 }
1847
1848 ret = ocfs2_wait_for_mask(&mw);
1849 if (ret) {
1850 mlog_errno(ret);
1851 goto out;
1852 }
1853 spin_lock_irqsave(&lockres->l_lock, flags);
1854 }
1855
1856 lockres->l_action = OCFS2_AST_CONVERT;
Mark Fashehe988cf12008-07-10 09:25:39 -07001857 lkm_flags |= DLM_LKF_CONVERT;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001858 lockres->l_requested = level;
1859 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1860
1861 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1862 spin_unlock_irqrestore(&lockres->l_lock, flags);
1863
Joel Becker4670c462008-02-01 14:39:35 -08001864 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Beckera796d282010-01-28 19:22:39 -08001865 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Becker7431cd72008-02-01 12:15:37 -08001866 if (ret) {
1867 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001868 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001869 ret = -EINVAL;
1870 }
1871
1872 ocfs2_recover_from_dlm_error(lockres, 1);
1873 lockres_remove_mask_waiter(lockres, &mw);
1874 goto out;
1875 }
1876
1877 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1878 if (ret == -ERESTARTSYS) {
1879 /*
1880 * Userspace can cause deadlock itself with
1881 * flock(). Current behavior locally is to allow the
1882 * deadlock, but abort the system call if a signal is
1883 * received. We follow this example, otherwise a
1884 * poorly written program could sit in kernel until
1885 * reboot.
1886 *
1887 * Handling this is a bit more complicated for Ocfs2
1888 * though. We can't exit this function with an
1889 * outstanding lock request, so a cancel convert is
1890 * required. We intentionally overwrite 'ret' - if the
1891 * cancel fails and the lock was granted, it's easier
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001892 * to just bubble success back up to the user.
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001893 */
1894 ret = ocfs2_flock_handle_signal(lockres, level);
David Teigland1693a5c2008-01-30 16:52:53 -08001895 } else if (!ret && (level > lockres->l_level)) {
1896 /* Trylock failed asynchronously */
1897 BUG_ON(!trylock);
1898 ret = -EAGAIN;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001899 }
1900
1901out:
1902
1903 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1904 lockres->l_name, ex, trylock, ret);
1905 return ret;
1906}
1907
1908void ocfs2_file_unlock(struct file *file)
1909{
1910 int ret;
Joel Beckerde551242008-02-01 14:45:08 -08001911 unsigned int gen;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001912 unsigned long flags;
1913 struct ocfs2_file_private *fp = file->private_data;
1914 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1915 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1916 struct ocfs2_mask_waiter mw;
1917
1918 ocfs2_init_mask_waiter(&mw);
1919
1920 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1921 return;
1922
Mark Fashehe988cf12008-07-10 09:25:39 -07001923 if (lockres->l_level == DLM_LOCK_NL)
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001924 return;
1925
1926 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1927 lockres->l_name, lockres->l_flags, lockres->l_level,
1928 lockres->l_action);
1929
1930 spin_lock_irqsave(&lockres->l_lock, flags);
1931 /*
1932 * Fake a blocking ast for the downconvert code.
1933 */
1934 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001935 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001936
Mark Fashehe988cf12008-07-10 09:25:39 -07001937 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001938 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1939 spin_unlock_irqrestore(&lockres->l_lock, flags);
1940
Mark Fashehe988cf12008-07-10 09:25:39 -07001941 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001942 if (ret) {
1943 mlog_errno(ret);
1944 return;
1945 }
1946
1947 ret = ocfs2_wait_for_mask(&mw);
1948 if (ret)
1949 mlog_errno(ret);
1950}
1951
Mark Fasheh34d024f2007-09-24 15:56:19 -07001952static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1953 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001954{
1955 int kick = 0;
1956
1957 mlog_entry_void();
1958
1959 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001960 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001961 * condition. */
1962 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1963 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001964 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001965 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1966 kick = 1;
1967 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001968 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001969 if (!lockres->l_ex_holders)
1970 kick = 1;
1971 break;
1972 default:
1973 BUG();
1974 }
1975 }
1976
1977 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001978 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001979
1980 mlog_exit_void();
1981}
1982
Mark Fashehccd979b2005-12-15 14:31:24 -08001983#define OCFS2_SEC_BITS 34
1984#define OCFS2_SEC_SHIFT (64 - 34)
1985#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1986
1987/* LVB only has room for 64 bits of time here so we pack it for
1988 * now. */
1989static u64 ocfs2_pack_timespec(struct timespec *spec)
1990{
1991 u64 res;
1992 u64 sec = spec->tv_sec;
1993 u32 nsec = spec->tv_nsec;
1994
1995 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1996
1997 return res;
1998}
1999
2000/* Call this with the lockres locked. I am reasonably sure we don't
2001 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07002002 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002003static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2004{
2005 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002006 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002007 struct ocfs2_meta_lvb *lvb;
2008
2009 mlog_entry_void();
2010
Mark Fasheha641dc22008-12-24 16:03:48 -08002011 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002012
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002013 /*
2014 * Invalidate the LVB of a deleted inode - this way other
2015 * nodes are forced to go to disk and discover the new inode
2016 * status.
2017 */
2018 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2019 lvb->lvb_version = 0;
2020 goto out;
2021 }
2022
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07002023 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2025 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
2026 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
2027 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
2028 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2029 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2030 lvb->lvb_iatime_packed =
2031 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
2032 lvb->lvb_ictime_packed =
2033 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2034 lvb->lvb_imtime_packed =
2035 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002036 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002037 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07002038 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08002039
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002040out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002041 mlog_meta_lvb(0, lockres);
2042
2043 mlog_exit_void();
2044}
2045
2046static void ocfs2_unpack_timespec(struct timespec *spec,
2047 u64 packed_time)
2048{
2049 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2050 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2051}
2052
2053static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2054{
2055 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002056 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 struct ocfs2_meta_lvb *lvb;
2058
2059 mlog_entry_void();
2060
2061 mlog_meta_lvb(0, lockres);
2062
Mark Fasheha641dc22008-12-24 16:03:48 -08002063 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002064
2065 /* We're safe here without the lockres lock... */
2066 spin_lock(&oi->ip_lock);
2067 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2068 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2069
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002070 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07002071 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07002072 ocfs2_set_inode_flags(inode);
2073
Mark Fashehccd979b2005-12-15 14:31:24 -08002074 /* fast-symlinks are a special case */
2075 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2076 inode->i_blocks = 0;
2077 else
Mark Fasheh8110b072007-03-22 16:53:23 -07002078 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002079
2080 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
2081 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
2082 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2083 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
2084 ocfs2_unpack_timespec(&inode->i_atime,
2085 be64_to_cpu(lvb->lvb_iatime_packed));
2086 ocfs2_unpack_timespec(&inode->i_mtime,
2087 be64_to_cpu(lvb->lvb_imtime_packed));
2088 ocfs2_unpack_timespec(&inode->i_ctime,
2089 be64_to_cpu(lvb->lvb_ictime_packed));
2090 spin_unlock(&oi->ip_lock);
2091
2092 mlog_exit_void();
2093}
2094
Mark Fashehf9e2d822006-09-12 15:35:49 -07002095static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2096 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002097{
Mark Fasheha641dc22008-12-24 16:03:48 -08002098 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002099
Joel Becker1c520df2009-06-19 15:14:13 -07002100 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2101 && lvb->lvb_version == OCFS2_LVB_VERSION
Mark Fashehf9e2d822006-09-12 15:35:49 -07002102 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08002103 return 1;
2104 return 0;
2105}
2106
2107/* Determine whether a lock resource needs to be refreshed, and
2108 * arbitrate who gets to refresh it.
2109 *
2110 * 0 means no refresh needed.
2111 *
2112 * > 0 means you need to refresh this and you MUST call
2113 * ocfs2_complete_lock_res_refresh afterwards. */
2114static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2115{
2116 unsigned long flags;
2117 int status = 0;
2118
2119 mlog_entry_void();
2120
2121refresh_check:
2122 spin_lock_irqsave(&lockres->l_lock, flags);
2123 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2124 spin_unlock_irqrestore(&lockres->l_lock, flags);
2125 goto bail;
2126 }
2127
2128 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2129 spin_unlock_irqrestore(&lockres->l_lock, flags);
2130
2131 ocfs2_wait_on_refreshing_lock(lockres);
2132 goto refresh_check;
2133 }
2134
2135 /* Ok, I'll be the one to refresh this lock. */
2136 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2137 spin_unlock_irqrestore(&lockres->l_lock, flags);
2138
2139 status = 1;
2140bail:
2141 mlog_exit(status);
2142 return status;
2143}
2144
2145/* If status is non zero, I'll mark it as not being in refresh
2146 * anymroe, but i won't clear the needs refresh flag. */
2147static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2148 int status)
2149{
2150 unsigned long flags;
2151 mlog_entry_void();
2152
2153 spin_lock_irqsave(&lockres->l_lock, flags);
2154 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2155 if (!status)
2156 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2157 spin_unlock_irqrestore(&lockres->l_lock, flags);
2158
2159 wake_up(&lockres->l_event);
2160
2161 mlog_exit_void();
2162}
2163
2164/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002165static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002166 struct buffer_head **bh)
2167{
2168 int status = 0;
2169 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002170 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002173
2174 mlog_entry_void();
2175
Mark Fashehbe9e9862007-04-18 15:22:08 -07002176 if (ocfs2_mount_local(osb))
2177 goto bail;
2178
Mark Fashehccd979b2005-12-15 14:31:24 -08002179 spin_lock(&oi->ip_lock);
2180 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb0697052006-03-03 10:24:33 -08002181 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08002182 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002183 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08002184 spin_unlock(&oi->ip_lock);
2185 status = -ENOENT;
2186 goto bail;
2187 }
2188 spin_unlock(&oi->ip_lock);
2189
Mark Fashehbe9e9862007-04-18 15:22:08 -07002190 if (!ocfs2_should_refresh_lock_res(lockres))
2191 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08002192
2193 /* This will discard any caching information we might have had
2194 * for the inode metadata. */
Joel Becker8cb471e2009-02-10 20:00:41 -08002195 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08002196
Mark Fasheh83418972007-04-23 18:53:12 -07002197 ocfs2_extent_map_trunc(inode, 0);
2198
Mark Fashehbe9e9862007-04-18 15:22:08 -07002199 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb0697052006-03-03 10:24:33 -08002200 mlog(0, "Trusting LVB on inode %llu\n",
2201 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002202 ocfs2_refresh_inode_from_lvb(inode);
2203 } else {
2204 /* Boo, we have to go to disk. */
2205 /* read bh, cast, ocfs2_refresh_inode */
Joel Beckerb657c952008-11-13 14:49:11 -08002206 status = ocfs2_read_inode_block(inode, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002207 if (status < 0) {
2208 mlog_errno(status);
2209 goto bail_refresh;
2210 }
2211 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2212
2213 /* This is a good chance to make sure we're not
Joel Beckerb657c952008-11-13 14:49:11 -08002214 * locking an invalid object. ocfs2_read_inode_block()
2215 * already checked that the inode block is sane.
Mark Fashehccd979b2005-12-15 14:31:24 -08002216 *
2217 * We bug on a stale inode here because we checked
2218 * above whether it was wiped from disk. The wiping
2219 * node provides a guarantee that we receive that
2220 * message and can mark the inode before dropping any
2221 * locks associated with it. */
Mark Fashehccd979b2005-12-15 14:31:24 -08002222 mlog_bug_on_msg(inode->i_generation !=
2223 le32_to_cpu(fe->i_generation),
Mark Fashehb0697052006-03-03 10:24:33 -08002224 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08002225 "inode->i_generation: %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -08002226 (unsigned long long)oi->ip_blkno,
2227 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08002228 inode->i_generation);
2229 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2230 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb0697052006-03-03 10:24:33 -08002231 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2232 (unsigned long long)oi->ip_blkno,
2233 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08002234 le32_to_cpu(fe->i_flags));
2235
2236 ocfs2_refresh_inode(inode, fe);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002237 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002238 }
2239
2240 status = 0;
2241bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07002242 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002243bail:
2244 mlog_exit(status);
2245 return status;
2246}
2247
2248static int ocfs2_assign_bh(struct inode *inode,
2249 struct buffer_head **ret_bh,
2250 struct buffer_head *passed_bh)
2251{
2252 int status;
2253
2254 if (passed_bh) {
2255 /* Ok, the update went to disk for us, use the
2256 * returned bh. */
2257 *ret_bh = passed_bh;
2258 get_bh(*ret_bh);
2259
2260 return 0;
2261 }
2262
Joel Beckerb657c952008-11-13 14:49:11 -08002263 status = ocfs2_read_inode_block(inode, ret_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002264 if (status < 0)
2265 mlog_errno(status);
2266
2267 return status;
2268}
2269
2270/*
2271 * returns < 0 error if the callback will never be called, otherwise
2272 * the result of the lock will be communicated via the callback.
2273 */
Jan Karacb257972009-06-04 15:26:50 +02002274int ocfs2_inode_lock_full_nested(struct inode *inode,
2275 struct buffer_head **ret_bh,
2276 int ex,
2277 int arg_flags,
2278 int subclass)
Mark Fashehccd979b2005-12-15 14:31:24 -08002279{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002280 int status, level, acquired;
2281 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002282 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2284 struct buffer_head *local_bh = NULL;
2285
2286 BUG_ON(!inode);
2287
2288 mlog_entry_void();
2289
Mark Fashehb0697052006-03-03 10:24:33 -08002290 mlog(0, "inode %llu, take %s META lock\n",
2291 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002292 ex ? "EXMODE" : "PRMODE");
2293
2294 status = 0;
2295 acquired = 0;
2296 /* We'll allow faking a readonly metadata lock for
2297 * rodevices. */
2298 if (ocfs2_is_hard_readonly(osb)) {
2299 if (ex)
2300 status = -EROFS;
2301 goto bail;
2302 }
2303
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002304 if (ocfs2_mount_local(osb))
2305 goto local;
2306
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002308 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002309
Mark Fashehe63aecb62007-10-18 15:30:42 -07002310 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002311 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002312 dlm_flags = 0;
2313 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002314 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08002315
Jan Karacb257972009-06-04 15:26:50 +02002316 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2317 arg_flags, subclass, _RET_IP_);
Mark Fashehccd979b2005-12-15 14:31:24 -08002318 if (status < 0) {
2319 if (status != -EAGAIN && status != -EIOCBRETRY)
2320 mlog_errno(status);
2321 goto bail;
2322 }
2323
2324 /* Notify the error cleanup path to drop the cluster lock. */
2325 acquired = 1;
2326
2327 /* We wait twice because a node may have died while we were in
2328 * the lower dlm layers. The second time though, we've
2329 * committed to owning this lock so we don't allow signals to
2330 * abort the operation. */
2331 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08002332 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002333
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002334local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002335 /*
2336 * We only see this flag if we're being called from
2337 * ocfs2_read_locked_inode(). It means we're locking an inode
2338 * which hasn't been populated yet, so clear the refresh flag
2339 * and let the caller handle it.
2340 */
2341 if (inode->i_state & I_NEW) {
2342 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002343 if (lockres)
2344 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07002345 goto bail;
2346 }
2347
Mark Fashehccd979b2005-12-15 14:31:24 -08002348 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07002349 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08002350 * may or may not read one, depending on what's in the
2351 * LVB. The result of all of this is that we've *only* gone to
2352 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002353 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002354 if (status < 0) {
2355 if (status != -ENOENT)
2356 mlog_errno(status);
2357 goto bail;
2358 }
2359
2360 if (ret_bh) {
2361 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2362 if (status < 0) {
2363 mlog_errno(status);
2364 goto bail;
2365 }
2366 }
2367
Mark Fashehccd979b2005-12-15 14:31:24 -08002368bail:
2369 if (status < 0) {
2370 if (ret_bh && (*ret_bh)) {
2371 brelse(*ret_bh);
2372 *ret_bh = NULL;
2373 }
2374 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002375 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002376 }
2377
2378 if (local_bh)
2379 brelse(local_bh);
2380
2381 mlog_exit(status);
2382 return status;
2383}
2384
2385/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002386 * This is working around a lock inversion between tasks acquiring DLM
2387 * locks while holding a page lock and the downconvert thread which
2388 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002389 *
2390 * ** These _with_page variantes are only intended to be called from aop
2391 * methods that hold page locks and return a very specific *positive* error
2392 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2393 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002394 * The DLM is called such that it returns -EAGAIN if it would have
2395 * blocked waiting for the downconvert thread. In that case we unlock
2396 * our page so the downconvert thread can make progress. Once we've
2397 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2398 * that called us can bubble that back up into the VFS who will then
2399 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002400 *
2401 * We do a blocking lock and immediate unlock before returning, though, so that
2402 * the lock has a great chance of being cached on this node by the time the VFS
2403 * calls back to retry the aop. This has a potential to livelock as nodes
2404 * ping locks back and forth, but that's a risk we're willing to take to avoid
2405 * the lock inversion simply.
2406 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002407int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002408 struct buffer_head **ret_bh,
2409 int ex,
2410 struct page *page)
2411{
2412 int ret;
2413
Mark Fashehe63aecb62007-10-18 15:30:42 -07002414 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002415 if (ret == -EAGAIN) {
2416 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002417 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2418 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002419 ret = AOP_TRUNCATED_PAGE;
2420 }
2421
2422 return ret;
2423}
2424
Mark Fashehe63aecb62007-10-18 15:30:42 -07002425int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002426 struct vfsmount *vfsmnt,
2427 int *level)
2428{
2429 int ret;
2430
2431 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002432 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002433 if (ret < 0) {
2434 mlog_errno(ret);
2435 return ret;
2436 }
2437
2438 /*
2439 * If we should update atime, we will get EX lock,
2440 * otherwise we just get PR lock.
2441 */
2442 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2443 struct buffer_head *bh = NULL;
2444
Mark Fashehe63aecb62007-10-18 15:30:42 -07002445 ocfs2_inode_unlock(inode, 0);
2446 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002447 if (ret < 0) {
2448 mlog_errno(ret);
2449 return ret;
2450 }
2451 *level = 1;
2452 if (ocfs2_should_update_atime(inode, vfsmnt))
2453 ocfs2_update_inode_atime(inode, bh);
2454 if (bh)
2455 brelse(bh);
2456 } else
2457 *level = 0;
2458
2459 mlog_exit(ret);
2460 return ret;
2461}
2462
Mark Fashehe63aecb62007-10-18 15:30:42 -07002463void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002464 int ex)
2465{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002466 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002467 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002468 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002469
2470 mlog_entry_void();
2471
Mark Fashehb0697052006-03-03 10:24:33 -08002472 mlog(0, "inode %llu drop %s META lock\n",
2473 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002474 ex ? "EXMODE" : "PRMODE");
2475
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002476 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2477 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002478 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2479
2480 mlog_exit_void();
2481}
2482
Sunil Mushrandf152c22009-06-22 11:40:07 -07002483int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002484{
2485 struct ocfs2_lock_res *lockres;
2486 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002487 int status = 0;
2488
Sunil Mushrandf152c22009-06-22 11:40:07 -07002489 if (ocfs2_is_hard_readonly(osb))
2490 return -EROFS;
2491
2492 if (ocfs2_mount_local(osb))
2493 return 0;
2494
Srinivas Eeda83273932009-06-03 17:02:55 -07002495 lockres = &osb->osb_orphan_scan.os_lockres;
Sunil Mushrandf152c22009-06-22 11:40:07 -07002496 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Srinivas Eeda83273932009-06-03 17:02:55 -07002497 if (status < 0)
2498 return status;
2499
2500 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker1c520df2009-06-19 15:14:13 -07002501 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2502 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
Srinivas Eeda83273932009-06-03 17:02:55 -07002503 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
Sunil Mushran32119492009-06-19 16:53:18 -07002504 else
2505 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2506
Srinivas Eeda83273932009-06-03 17:02:55 -07002507 return status;
2508}
2509
Sunil Mushrandf152c22009-06-22 11:40:07 -07002510void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
Srinivas Eeda83273932009-06-03 17:02:55 -07002511{
2512 struct ocfs2_lock_res *lockres;
2513 struct ocfs2_orphan_scan_lvb *lvb;
Srinivas Eeda83273932009-06-03 17:02:55 -07002514
Sunil Mushrandf152c22009-06-22 11:40:07 -07002515 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2516 lockres = &osb->osb_orphan_scan.os_lockres;
2517 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2518 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2519 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2520 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2521 }
Srinivas Eeda83273932009-06-03 17:02:55 -07002522}
2523
Mark Fashehccd979b2005-12-15 14:31:24 -08002524int ocfs2_super_lock(struct ocfs2_super *osb,
2525 int ex)
2526{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002527 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002528 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002529 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002530
2531 mlog_entry_void();
2532
2533 if (ocfs2_is_hard_readonly(osb))
2534 return -EROFS;
2535
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002536 if (ocfs2_mount_local(osb))
2537 goto bail;
2538
Mark Fashehccd979b2005-12-15 14:31:24 -08002539 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2540 if (status < 0) {
2541 mlog_errno(status);
2542 goto bail;
2543 }
2544
2545 /* The super block lock path is really in the best position to
2546 * know when resources covered by the lock need to be
2547 * refreshed, so we do it here. Of course, making sense of
2548 * everything is up to the caller :) */
2549 status = ocfs2_should_refresh_lock_res(lockres);
2550 if (status < 0) {
2551 mlog_errno(status);
2552 goto bail;
2553 }
2554 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002555 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002556
2557 ocfs2_complete_lock_res_refresh(lockres, status);
2558
2559 if (status < 0)
2560 mlog_errno(status);
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002561 ocfs2_track_lock_refresh(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002562 }
2563bail:
2564 mlog_exit(status);
2565 return status;
2566}
2567
2568void ocfs2_super_unlock(struct ocfs2_super *osb,
2569 int ex)
2570{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002571 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002572 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2573
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002574 if (!ocfs2_mount_local(osb))
2575 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002576}
2577
2578int ocfs2_rename_lock(struct ocfs2_super *osb)
2579{
2580 int status;
2581 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2582
2583 if (ocfs2_is_hard_readonly(osb))
2584 return -EROFS;
2585
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002586 if (ocfs2_mount_local(osb))
2587 return 0;
2588
Joel Beckerbd3e7612008-02-01 12:14:57 -08002589 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002590 if (status < 0)
2591 mlog_errno(status);
2592
2593 return status;
2594}
2595
2596void ocfs2_rename_unlock(struct ocfs2_super *osb)
2597{
2598 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2599
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002600 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002601 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002602}
2603
wengang wang6ca497a2009-03-06 21:29:10 +08002604int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2605{
2606 int status;
2607 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2608
2609 if (ocfs2_is_hard_readonly(osb))
2610 return -EROFS;
2611
2612 if (ocfs2_mount_local(osb))
2613 return 0;
2614
2615 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2616 0, 0);
2617 if (status < 0)
2618 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2619
2620 return status;
2621}
2622
2623void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2624{
2625 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2626
2627 if (!ocfs2_mount_local(osb))
2628 ocfs2_cluster_unlock(osb, lockres,
2629 ex ? LKM_EXMODE : LKM_PRMODE);
2630}
2631
Mark Fashehd680efe2006-09-08 14:14:34 -07002632int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2633{
2634 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002635 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002636 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2637 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2638
2639 BUG_ON(!dl);
2640
2641 if (ocfs2_is_hard_readonly(osb))
2642 return -EROFS;
2643
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002644 if (ocfs2_mount_local(osb))
2645 return 0;
2646
Mark Fashehd680efe2006-09-08 14:14:34 -07002647 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2648 if (ret < 0)
2649 mlog_errno(ret);
2650
2651 return ret;
2652}
2653
2654void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2655{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002656 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002657 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2658 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2659
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002660 if (!ocfs2_mount_local(osb))
2661 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002662}
2663
Mark Fashehccd979b2005-12-15 14:31:24 -08002664/* Reference counting of the dlm debug structure. We want this because
2665 * open references on the debug inodes can live on after a mount, so
2666 * we can't rely on the ocfs2_super to always exist. */
2667static void ocfs2_dlm_debug_free(struct kref *kref)
2668{
2669 struct ocfs2_dlm_debug *dlm_debug;
2670
2671 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2672
2673 kfree(dlm_debug);
2674}
2675
2676void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2677{
2678 if (dlm_debug)
2679 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2680}
2681
2682static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2683{
2684 kref_get(&debug->d_refcnt);
2685}
2686
2687struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2688{
2689 struct ocfs2_dlm_debug *dlm_debug;
2690
2691 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2692 if (!dlm_debug) {
2693 mlog_errno(-ENOMEM);
2694 goto out;
2695 }
2696
2697 kref_init(&dlm_debug->d_refcnt);
2698 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2699 dlm_debug->d_locking_state = NULL;
2700out:
2701 return dlm_debug;
2702}
2703
2704/* Access to this is arbitrated for us via seq_file->sem. */
2705struct ocfs2_dlm_seq_priv {
2706 struct ocfs2_dlm_debug *p_dlm_debug;
2707 struct ocfs2_lock_res p_iter_res;
2708 struct ocfs2_lock_res p_tmp_res;
2709};
2710
2711static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2712 struct ocfs2_dlm_seq_priv *priv)
2713{
2714 struct ocfs2_lock_res *iter, *ret = NULL;
2715 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2716
2717 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2718
2719 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2720 /* discover the head of the list */
2721 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2722 mlog(0, "End of list found, %p\n", ret);
2723 break;
2724 }
2725
2726 /* We track our "dummy" iteration lockres' by a NULL
2727 * l_ops field. */
2728 if (iter->l_ops != NULL) {
2729 ret = iter;
2730 break;
2731 }
2732 }
2733
2734 return ret;
2735}
2736
2737static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2738{
2739 struct ocfs2_dlm_seq_priv *priv = m->private;
2740 struct ocfs2_lock_res *iter;
2741
2742 spin_lock(&ocfs2_dlm_tracking_lock);
2743 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2744 if (iter) {
2745 /* Since lockres' have the lifetime of their container
2746 * (which can be inodes, ocfs2_supers, etc) we want to
2747 * copy this out to a temporary lockres while still
2748 * under the spinlock. Obviously after this we can't
2749 * trust any pointers on the copy returned, but that's
2750 * ok as the information we want isn't typically held
2751 * in them. */
2752 priv->p_tmp_res = *iter;
2753 iter = &priv->p_tmp_res;
2754 }
2755 spin_unlock(&ocfs2_dlm_tracking_lock);
2756
2757 return iter;
2758}
2759
2760static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2761{
2762}
2763
2764static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2765{
2766 struct ocfs2_dlm_seq_priv *priv = m->private;
2767 struct ocfs2_lock_res *iter = v;
2768 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2769
2770 spin_lock(&ocfs2_dlm_tracking_lock);
2771 iter = ocfs2_dlm_next_res(iter, priv);
2772 list_del_init(&dummy->l_debug_list);
2773 if (iter) {
2774 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2775 priv->p_tmp_res = *iter;
2776 iter = &priv->p_tmp_res;
2777 }
2778 spin_unlock(&ocfs2_dlm_tracking_lock);
2779
2780 return iter;
2781}
2782
2783/* So that debugfs.ocfs2 can determine which format is being used */
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002784#define OCFS2_DLM_DEBUG_STR_VERSION 2
Mark Fashehccd979b2005-12-15 14:31:24 -08002785static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2786{
2787 int i;
2788 char *lvb;
2789 struct ocfs2_lock_res *lockres = v;
2790
2791 if (!lockres)
2792 return -EINVAL;
2793
Mark Fashehd680efe2006-09-08 14:14:34 -07002794 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2795
2796 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2797 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2798 lockres->l_name,
2799 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2800 else
2801 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2802
2803 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002804 "0x%lx\t"
2805 "0x%x\t"
2806 "0x%x\t"
2807 "%u\t"
2808 "%u\t"
2809 "%d\t"
2810 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002811 lockres->l_level,
2812 lockres->l_flags,
2813 lockres->l_action,
2814 lockres->l_unlock_action,
2815 lockres->l_ro_holders,
2816 lockres->l_ex_holders,
2817 lockres->l_requested,
2818 lockres->l_blocking);
2819
2820 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002821 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002822 for(i = 0; i < DLM_LVB_LEN; i++)
2823 seq_printf(m, "0x%x\t", lvb[i]);
2824
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002825#ifdef CONFIG_OCFS2_FS_STATS
2826# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2827# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2828# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2829# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2830# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2831# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2832# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2833# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2834# define lock_refresh(_l) (_l)->l_lock_refresh
2835#else
Randy Dunlapdd25e552008-05-28 14:41:00 -07002836# define lock_num_prmode(_l) (0ULL)
2837# define lock_num_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002838# define lock_num_prmode_failed(_l) (0)
2839# define lock_num_exmode_failed(_l) (0)
Randy Dunlapdd25e552008-05-28 14:41:00 -07002840# define lock_total_prmode(_l) (0ULL)
2841# define lock_total_exmode(_l) (0ULL)
Sunil Mushran8ddb7b02008-05-13 13:45:15 -07002842# define lock_max_prmode(_l) (0)
2843# define lock_max_exmode(_l) (0)
2844# define lock_refresh(_l) (0)
2845#endif
2846 /* The following seq_print was added in version 2 of this output */
2847 seq_printf(m, "%llu\t"
2848 "%llu\t"
2849 "%u\t"
2850 "%u\t"
2851 "%llu\t"
2852 "%llu\t"
2853 "%u\t"
2854 "%u\t"
2855 "%u\t",
2856 lock_num_prmode(lockres),
2857 lock_num_exmode(lockres),
2858 lock_num_prmode_failed(lockres),
2859 lock_num_exmode_failed(lockres),
2860 lock_total_prmode(lockres),
2861 lock_total_exmode(lockres),
2862 lock_max_prmode(lockres),
2863 lock_max_exmode(lockres),
2864 lock_refresh(lockres));
2865
Mark Fashehccd979b2005-12-15 14:31:24 -08002866 /* End the line */
2867 seq_printf(m, "\n");
2868 return 0;
2869}
2870
Jan Engelhardt90d99772008-01-22 20:52:20 +01002871static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002872 .start = ocfs2_dlm_seq_start,
2873 .stop = ocfs2_dlm_seq_stop,
2874 .next = ocfs2_dlm_seq_next,
2875 .show = ocfs2_dlm_seq_show,
2876};
2877
2878static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2879{
2880 struct seq_file *seq = (struct seq_file *) file->private_data;
2881 struct ocfs2_dlm_seq_priv *priv = seq->private;
2882 struct ocfs2_lock_res *res = &priv->p_iter_res;
2883
2884 ocfs2_remove_lockres_tracking(res);
2885 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2886 return seq_release_private(inode, file);
2887}
2888
2889static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2890{
2891 int ret;
2892 struct ocfs2_dlm_seq_priv *priv;
2893 struct seq_file *seq;
2894 struct ocfs2_super *osb;
2895
2896 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2897 if (!priv) {
2898 ret = -ENOMEM;
2899 mlog_errno(ret);
2900 goto out;
2901 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002902 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002903 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2904 priv->p_dlm_debug = osb->osb_dlm_debug;
2905 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2906
2907 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2908 if (ret) {
2909 kfree(priv);
2910 mlog_errno(ret);
2911 goto out;
2912 }
2913
2914 seq = (struct seq_file *) file->private_data;
2915 seq->private = priv;
2916
2917 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2918 priv->p_dlm_debug);
2919
2920out:
2921 return ret;
2922}
2923
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002924static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002925 .open = ocfs2_dlm_debug_open,
2926 .release = ocfs2_dlm_debug_release,
2927 .read = seq_read,
2928 .llseek = seq_lseek,
2929};
2930
2931static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2932{
2933 int ret = 0;
2934 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2935
2936 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2937 S_IFREG|S_IRUSR,
2938 osb->osb_debug_root,
2939 osb,
2940 &ocfs2_dlm_debug_fops);
2941 if (!dlm_debug->d_locking_state) {
2942 ret = -EINVAL;
2943 mlog(ML_ERROR,
2944 "Unable to create locking state debugfs file.\n");
2945 goto out;
2946 }
2947
2948 ocfs2_get_dlm_debug(dlm_debug);
2949out:
2950 return ret;
2951}
2952
2953static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2954{
2955 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2956
2957 if (dlm_debug) {
2958 debugfs_remove(dlm_debug->d_locking_state);
2959 ocfs2_put_dlm_debug(dlm_debug);
2960 }
2961}
2962
2963int ocfs2_dlm_init(struct ocfs2_super *osb)
2964{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002965 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002966 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002967
2968 mlog_entry_void();
2969
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002970 if (ocfs2_mount_local(osb)) {
2971 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002972 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002973 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002974
Mark Fashehccd979b2005-12-15 14:31:24 -08002975 status = ocfs2_dlm_init_debug(osb);
2976 if (status < 0) {
2977 mlog_errno(status);
2978 goto bail;
2979 }
2980
Mark Fasheh34d024f2007-09-24 15:56:19 -07002981 /* launch downconvert thread */
2982 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2983 if (IS_ERR(osb->dc_task)) {
2984 status = PTR_ERR(osb->dc_task);
2985 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002986 mlog_errno(status);
2987 goto bail;
2988 }
2989
Mark Fashehccd979b2005-12-15 14:31:24 -08002990 /* for now, uuid == domain */
Joel Becker9c6c8772008-02-01 15:17:30 -08002991 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2992 osb->uuid_str,
Joel Becker4670c462008-02-01 14:39:35 -08002993 strlen(osb->uuid_str),
2994 ocfs2_do_node_down, osb,
2995 &conn);
2996 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002997 mlog_errno(status);
2998 goto bail;
2999 }
3000
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003001 status = ocfs2_cluster_this_node(&osb->node_num);
3002 if (status < 0) {
3003 mlog_errno(status);
3004 mlog(ML_ERROR,
3005 "could not find this host's node number\n");
Joel Becker286eaa92008-02-01 15:03:57 -08003006 ocfs2_cluster_disconnect(conn, 0);
Mark Fasheh0abd6d12008-01-29 16:59:56 -08003007 goto bail;
3008 }
3009
Sunil Mushranc271c5c2006-12-05 17:56:35 -08003010local:
Mark Fashehccd979b2005-12-15 14:31:24 -08003011 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3012 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
wengang wang6ca497a2009-03-06 21:29:10 +08003013 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
Srinivas Eeda83273932009-06-03 17:02:55 -07003014 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003015
Joel Becker4670c462008-02-01 14:39:35 -08003016 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08003017
3018 status = 0;
3019bail:
3020 if (status < 0) {
3021 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07003022 if (osb->dc_task)
3023 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08003024 }
3025
3026 mlog_exit(status);
3027 return status;
3028}
3029
Joel Becker286eaa92008-02-01 15:03:57 -08003030void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3031 int hangup_pending)
Mark Fashehccd979b2005-12-15 14:31:24 -08003032{
3033 mlog_entry_void();
3034
Mark Fashehccd979b2005-12-15 14:31:24 -08003035 ocfs2_drop_osb_locks(osb);
3036
Joel Becker4670c462008-02-01 14:39:35 -08003037 /*
3038 * Now that we have dropped all locks and ocfs2_dismount_volume()
3039 * has disabled recovery, the DLM won't be talking to us. It's
3040 * safe to tear things down before disconnecting the cluster.
3041 */
3042
Mark Fasheh34d024f2007-09-24 15:56:19 -07003043 if (osb->dc_task) {
3044 kthread_stop(osb->dc_task);
3045 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003046 }
3047
3048 ocfs2_lock_res_free(&osb->osb_super_lockres);
3049 ocfs2_lock_res_free(&osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003050 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003051 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003052
Joel Becker286eaa92008-02-01 15:03:57 -08003053 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
Joel Becker4670c462008-02-01 14:39:35 -08003054 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003055
3056 ocfs2_dlm_shutdown_debug(osb);
3057
3058 mlog_exit_void();
3059}
3060
Joel Beckera796d282010-01-28 19:22:39 -08003061static void ocfs2_unlock_ast(union ocfs2_dlm_lksb *lksb, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08003062{
Joel Beckera796d282010-01-28 19:22:39 -08003063 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003064 unsigned long flags;
3065
3066 mlog_entry_void();
3067
3068 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
3069 lockres->l_unlock_action);
3070
3071 spin_lock_irqsave(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003072 if (error) {
Joel Becker7431cd72008-02-01 12:15:37 -08003073 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
3074 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08003075 lockres->l_unlock_action);
3076 spin_unlock_irqrestore(&lockres->l_lock, flags);
Coly Lid92bc512009-08-28 19:03:18 +08003077 mlog_exit_void();
Mark Fashehccd979b2005-12-15 14:31:24 -08003078 return;
3079 }
3080
3081 switch(lockres->l_unlock_action) {
3082 case OCFS2_UNLOCK_CANCEL_CONVERT:
3083 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
3084 lockres->l_action = OCFS2_AST_INVALID;
Sunil Mushrana4b91962009-01-29 17:12:31 -08003085 /* Downconvert thread may have requeued this lock, we
3086 * need to wake it. */
3087 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3088 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
Mark Fashehccd979b2005-12-15 14:31:24 -08003089 break;
3090 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08003091 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08003092 break;
3093 default:
3094 BUG();
3095 }
3096
3097 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
Mark Fashehccd979b2005-12-15 14:31:24 -08003098 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -08003099 wake_up(&lockres->l_event);
David Teigland07f9eeb2008-11-17 12:28:48 -06003100 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08003101
3102 mlog_exit_void();
3103}
3104
Mark Fashehccd979b2005-12-15 14:31:24 -08003105static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003106 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003107{
Joel Becker7431cd72008-02-01 12:15:37 -08003108 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003109 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08003110 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003111
3112 /* We didn't get anywhere near actually using this lockres. */
3113 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3114 goto out;
3115
Mark Fashehb80fc012006-09-12 22:08:14 -07003116 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003117 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07003118
Mark Fashehccd979b2005-12-15 14:31:24 -08003119 spin_lock_irqsave(&lockres->l_lock, flags);
3120
3121 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3122 "lockres %s, flags 0x%lx\n",
3123 lockres->l_name, lockres->l_flags);
3124
3125 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3126 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3127 "%u, unlock_action = %u\n",
3128 lockres->l_name, lockres->l_flags, lockres->l_action,
3129 lockres->l_unlock_action);
3130
3131 spin_unlock_irqrestore(&lockres->l_lock, flags);
3132
3133 /* XXX: Today we just wait on any busy
3134 * locks... Perhaps we need to cancel converts in the
3135 * future? */
3136 ocfs2_wait_on_busy_lock(lockres);
3137
3138 spin_lock_irqsave(&lockres->l_lock, flags);
3139 }
3140
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003141 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3142 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08003143 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003144 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3145 lockres->l_ops->set_lvb(lockres);
3146 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003147
3148 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3149 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3150 lockres->l_name);
3151 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3152 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3153
3154 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3155 spin_unlock_irqrestore(&lockres->l_lock, flags);
3156 goto out;
3157 }
3158
3159 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3160
3161 /* make sure we never get here while waiting for an ast to
3162 * fire. */
3163 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3164
3165 /* is this necessary? */
3166 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3167 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3168 spin_unlock_irqrestore(&lockres->l_lock, flags);
3169
3170 mlog(0, "lock %s\n", lockres->l_name);
3171
Joel Beckera796d282010-01-28 19:22:39 -08003172 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
Joel Becker7431cd72008-02-01 12:15:37 -08003173 if (ret) {
3174 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003175 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Beckercf0acdc2008-01-29 16:59:55 -08003176 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003177 BUG();
3178 }
Coly Li73ac36e2009-01-07 18:09:16 -08003179 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003180 lockres->l_name);
3181
3182 ocfs2_wait_on_busy_lock(lockres);
3183out:
3184 mlog_exit(0);
3185 return 0;
3186}
3187
3188/* Mark the lockres as being dropped. It will no longer be
3189 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07003190 * being dequeued from the downconvert thread before we can consider
Sunil Mushran2bd63212010-01-25 16:57:38 -08003191 * it safe to drop.
Mark Fashehccd979b2005-12-15 14:31:24 -08003192 *
3193 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3194void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3195{
3196 int status;
3197 struct ocfs2_mask_waiter mw;
3198 unsigned long flags;
3199
3200 ocfs2_init_mask_waiter(&mw);
3201
3202 spin_lock_irqsave(&lockres->l_lock, flags);
3203 lockres->l_flags |= OCFS2_LOCK_FREEING;
3204 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3205 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3206 spin_unlock_irqrestore(&lockres->l_lock, flags);
3207
3208 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3209
3210 status = ocfs2_wait_for_mask(&mw);
3211 if (status)
3212 mlog_errno(status);
3213
3214 spin_lock_irqsave(&lockres->l_lock, flags);
3215 }
3216 spin_unlock_irqrestore(&lockres->l_lock, flags);
3217}
3218
Mark Fashehd680efe2006-09-08 14:14:34 -07003219void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3220 struct ocfs2_lock_res *lockres)
3221{
3222 int ret;
3223
3224 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003225 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07003226 if (ret)
3227 mlog_errno(ret);
3228}
3229
Mark Fashehccd979b2005-12-15 14:31:24 -08003230static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3231{
Mark Fashehd680efe2006-09-08 14:14:34 -07003232 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3233 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
wengang wang6ca497a2009-03-06 21:29:10 +08003234 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
Srinivas Eeda83273932009-06-03 17:02:55 -07003235 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003236}
3237
Mark Fashehccd979b2005-12-15 14:31:24 -08003238int ocfs2_drop_inode_locks(struct inode *inode)
3239{
3240 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08003241
3242 mlog_entry_void();
3243
3244 /* No need to call ocfs2_mark_lockres_freeing here -
3245 * ocfs2_clear_inode has done it for us. */
3246
3247 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07003248 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003249 if (err < 0)
3250 mlog_errno(err);
3251
3252 status = err;
3253
3254 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07003255 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003256 if (err < 0)
3257 mlog_errno(err);
3258 if (err < 0 && !status)
3259 status = err;
3260
3261 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07003262 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003263 if (err < 0)
3264 mlog_errno(err);
3265 if (err < 0 && !status)
3266 status = err;
3267
3268 mlog_exit(status);
3269 return status;
3270}
3271
Joel Beckerde551242008-02-01 14:45:08 -08003272static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3273 int new_level)
Mark Fashehccd979b2005-12-15 14:31:24 -08003274{
3275 assert_spin_locked(&lockres->l_lock);
3276
Joel Beckerbd3e7612008-02-01 12:14:57 -08003277 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003278
3279 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003280 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08003281 lockres->l_level, new_level);
3282 BUG();
3283 }
3284
3285 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3286 lockres->l_name, new_level, lockres->l_blocking);
3287
3288 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3289 lockres->l_requested = new_level;
3290 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
Joel Beckerde551242008-02-01 14:45:08 -08003291 return lockres_set_pending(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003292}
3293
3294static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3295 struct ocfs2_lock_res *lockres,
3296 int new_level,
Joel Beckerde551242008-02-01 14:45:08 -08003297 int lvb,
3298 unsigned int generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08003299{
Joel Beckerbd3e7612008-02-01 12:14:57 -08003300 int ret;
3301 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08003302
3303 mlog_entry_void();
3304
3305 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08003306 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08003307
Joel Becker4670c462008-02-01 14:39:35 -08003308 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08003309 new_level,
3310 &lockres->l_lksb,
3311 dlm_flags,
3312 lockres->l_name,
Joel Beckera796d282010-01-28 19:22:39 -08003313 OCFS2_LOCK_ID_MAX_LEN - 1);
Joel Beckerde551242008-02-01 14:45:08 -08003314 lockres_clear_pending(lockres, generation, osb);
Joel Becker7431cd72008-02-01 12:15:37 -08003315 if (ret) {
3316 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003317 ocfs2_recover_from_dlm_error(lockres, 1);
3318 goto bail;
3319 }
3320
3321 ret = 0;
3322bail:
3323 mlog_exit(ret);
3324 return ret;
3325}
3326
Joel Becker24ef1812008-01-29 17:37:32 -08003327/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08003328static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3329 struct ocfs2_lock_res *lockres)
3330{
3331 assert_spin_locked(&lockres->l_lock);
3332
3333 mlog_entry_void();
3334 mlog(0, "lock %s\n", lockres->l_name);
3335
3336 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3337 /* If we're already trying to cancel a lock conversion
3338 * then just drop the spinlock and allow the caller to
3339 * requeue this lock. */
3340
3341 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3342 return 0;
3343 }
3344
3345 /* were we in a convert when we got the bast fire? */
3346 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3347 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3348 /* set things up for the unlockast to know to just
3349 * clear out the ast_action and unset busy, etc. */
3350 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3351
3352 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3353 "lock %s, invalid flags: 0x%lx\n",
3354 lockres->l_name, lockres->l_flags);
3355
3356 return 1;
3357}
3358
3359static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3360 struct ocfs2_lock_res *lockres)
3361{
3362 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08003363
3364 mlog_entry_void();
3365 mlog(0, "lock %s\n", lockres->l_name);
3366
Joel Becker4670c462008-02-01 14:39:35 -08003367 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Beckera796d282010-01-28 19:22:39 -08003368 DLM_LKF_CANCEL);
Joel Becker7431cd72008-02-01 12:15:37 -08003369 if (ret) {
3370 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08003371 ocfs2_recover_from_dlm_error(lockres, 0);
3372 }
3373
Joel Becker24ef1812008-01-29 17:37:32 -08003374 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08003375
3376 mlog_exit(ret);
3377 return ret;
3378}
3379
Mark Fashehb5e500e2006-09-13 22:01:16 -07003380static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3381 struct ocfs2_lock_res *lockres,
3382 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08003383{
3384 unsigned long flags;
3385 int blocking;
3386 int new_level;
Sunil Mushran079b8052010-02-03 10:16:54 -08003387 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003388 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003389 int set_lvb = 0;
Joel Beckerde551242008-02-01 14:45:08 -08003390 unsigned int gen;
Mark Fashehccd979b2005-12-15 14:31:24 -08003391
3392 mlog_entry_void();
3393
3394 spin_lock_irqsave(&lockres->l_lock, flags);
3395
Mark Fashehccd979b2005-12-15 14:31:24 -08003396recheck:
Sunil Mushrandb0f6ce2010-02-01 16:55:50 -08003397 /*
3398 * Is it still blocking? If not, we have no more work to do.
3399 */
3400 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3401 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3402 spin_unlock_irqrestore(&lockres->l_lock, flags);
3403 ret = 0;
3404 goto leave;
3405 }
3406
Mark Fashehccd979b2005-12-15 14:31:24 -08003407 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Joel Beckerde551242008-02-01 14:45:08 -08003408 /* XXX
3409 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3410 * exists entirely for one reason - another thread has set
3411 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3412 *
3413 * If we do ocfs2_cancel_convert() before the other thread
3414 * calls dlm_lock(), our cancel will do nothing. We will
3415 * get no ast, and we will have no way of knowing the
3416 * cancel failed. Meanwhile, the other thread will call
3417 * into dlm_lock() and wait...forever.
3418 *
3419 * Why forever? Because another node has asked for the
3420 * lock first; that's why we're here in unblock_lock().
3421 *
3422 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3423 * set, we just requeue the unblock. Only when the other
3424 * thread has called dlm_lock() and cleared PENDING will
3425 * we then cancel their request.
3426 *
3427 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3428 * at the same time they set OCFS2_DLM_BUSY. They must
3429 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3430 */
3431 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3432 goto leave_requeue;
3433
Mark Fashehd680efe2006-09-08 14:14:34 -07003434 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003435 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3436 spin_unlock_irqrestore(&lockres->l_lock, flags);
3437 if (ret) {
3438 ret = ocfs2_cancel_convert(osb, lockres);
3439 if (ret < 0)
3440 mlog_errno(ret);
3441 }
3442 goto leave;
3443 }
3444
Sunil Mushrana1912822010-01-21 10:50:03 -08003445 /*
3446 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3447 * set when the ast is received for an upconvert just before the
3448 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3449 * on the heels of the ast, we want to delay the downconvert just
3450 * enough to allow the up requestor to do its task. Because this
3451 * lock is in the blocked queue, the lock will be downconverted
3452 * as soon as the requestor is done with the lock.
3453 */
3454 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3455 goto leave_requeue;
3456
Sunil Mushran0d741252010-01-29 09:44:11 -08003457 /*
3458 * How can we block and yet be at NL? We were trying to upconvert
3459 * from NL and got canceled. The code comes back here, and now
3460 * we notice and clear BLOCKING.
3461 */
3462 if (lockres->l_level == DLM_LOCK_NL) {
3463 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
3464 lockres->l_blocking = DLM_LOCK_NL;
3465 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3466 spin_unlock_irqrestore(&lockres->l_lock, flags);
3467 goto leave;
3468 }
3469
Mark Fashehccd979b2005-12-15 14:31:24 -08003470 /* if we're blocking an exclusive and we have *any* holders,
3471 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003472 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003473 && (lockres->l_ex_holders || lockres->l_ro_holders))
3474 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003475
3476 /* If it's a PR we're blocking, then only
3477 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003478 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003479 lockres->l_ex_holders)
3480 goto leave_requeue;
3481
3482 /*
3483 * Can we get a lock in this state if the holder counts are
3484 * zero? The meta data unblock code used to check this.
3485 */
3486 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3487 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3488 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08003489
Mark Fasheh16d5b9562006-09-13 21:10:12 -07003490 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3491
3492 if (lockres->l_ops->check_downconvert
3493 && !lockres->l_ops->check_downconvert(lockres, new_level))
3494 goto leave_requeue;
3495
Mark Fashehccd979b2005-12-15 14:31:24 -08003496 /* If we get here, then we know that there are no more
3497 * incompatible holders (and anyone asking for an incompatible
3498 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07003499 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08003500 goto downconvert;
3501
3502 /* Some lockres types want to do a bit of work before
3503 * downconverting a lock. Allow that here. The worker function
3504 * may sleep, so we save off a copy of what we're blocking as
3505 * it may change while we're not holding the spin lock. */
3506 blocking = lockres->l_blocking;
Sunil Mushran079b8052010-02-03 10:16:54 -08003507 level = lockres->l_level;
Mark Fashehccd979b2005-12-15 14:31:24 -08003508 spin_unlock_irqrestore(&lockres->l_lock, flags);
3509
Mark Fashehcc567d82006-09-13 21:52:21 -07003510 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07003511
3512 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3513 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08003514
3515 spin_lock_irqsave(&lockres->l_lock, flags);
Sunil Mushran079b8052010-02-03 10:16:54 -08003516 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003517 /* If this changed underneath us, then we can't drop
3518 * it just yet. */
3519 goto recheck;
3520 }
3521
3522downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07003523 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003524
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003525 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08003526 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003527 set_lvb = 1;
3528
3529 /*
3530 * We only set the lvb if the lock has been fully
3531 * refreshed - otherwise we risk setting stale
3532 * data. Otherwise, there's no need to actually clear
3533 * out the lvb here as it's value is still valid.
3534 */
3535 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3536 lockres->l_ops->set_lvb(lockres);
3537 }
3538
Joel Beckerde551242008-02-01 14:45:08 -08003539 gen = ocfs2_prepare_downconvert(lockres, new_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08003540 spin_unlock_irqrestore(&lockres->l_lock, flags);
Joel Beckerde551242008-02-01 14:45:08 -08003541 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3542 gen);
3543
Mark Fashehccd979b2005-12-15 14:31:24 -08003544leave:
3545 mlog_exit(ret);
3546 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003547
3548leave_requeue:
3549 spin_unlock_irqrestore(&lockres->l_lock, flags);
3550 ctl->requeue = 1;
3551
3552 mlog_exit(0);
3553 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003554}
3555
Mark Fashehd680efe2006-09-08 14:14:34 -07003556static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3557 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003558{
3559 struct inode *inode;
3560 struct address_space *mapping;
3561
Mark Fashehccd979b2005-12-15 14:31:24 -08003562 inode = ocfs2_lock_res_inode(lockres);
3563 mapping = inode->i_mapping;
3564
Mark Fasheh1044e402008-02-28 17:16:03 -08003565 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003566 goto out;
3567
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003568 /*
3569 * We need this before the filemap_fdatawrite() so that it can
3570 * transfer the dirty bit from the PTE to the
3571 * page. Unfortunately this means that even for EX->PR
3572 * downconverts, we'll lose our mappings and have to build
3573 * them up again.
3574 */
3575 unmap_mapping_range(mapping, 0, 0, 0);
3576
Mark Fashehccd979b2005-12-15 14:31:24 -08003577 if (filemap_fdatawrite(mapping)) {
Mark Fashehb0697052006-03-03 10:24:33 -08003578 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3579 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003580 }
3581 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003582 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003583 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003584 } else {
3585 /* We only need to wait on the I/O if we're not also
3586 * truncating pages because truncate_inode_pages waits
3587 * for us above. We don't truncate pages if we're
3588 * blocking anything < EXMODE because we want to keep
3589 * them around in that case. */
3590 filemap_fdatawait(mapping);
3591 }
3592
Mark Fashehf1f54062007-10-18 15:13:59 -07003593out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003594 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003595}
3596
Tao Maa4338482009-08-18 11:19:29 +08003597static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3598 struct ocfs2_lock_res *lockres,
3599 int new_level)
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003600{
Tao Maa4338482009-08-18 11:19:29 +08003601 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003602
Joel Beckerbd3e7612008-02-01 12:14:57 -08003603 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3604 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003605
3606 if (checkpointed)
3607 return 1;
3608
Tao Maa4338482009-08-18 11:19:29 +08003609 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003610 return 0;
3611}
3612
Tao Maa4338482009-08-18 11:19:29 +08003613static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3614 int new_level)
3615{
3616 struct inode *inode = ocfs2_lock_res_inode(lockres);
3617
3618 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3619}
3620
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003621static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3622{
3623 struct inode *inode = ocfs2_lock_res_inode(lockres);
3624
3625 __ocfs2_stuff_meta_lvb(inode);
3626}
3627
Mark Fashehd680efe2006-09-08 14:14:34 -07003628/*
3629 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003630 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003631 * dlmglue API and push these off to the ocfs2_wq in the future.
3632 */
3633static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3634 struct ocfs2_lock_res *lockres)
3635{
3636 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3637 ocfs2_dentry_lock_put(osb, dl);
3638}
3639
3640/*
3641 * d_delete() matching dentries before the lock downconvert.
3642 *
3643 * At this point, any process waiting to destroy the
3644 * dentry_lock due to last ref count is stopped by the
3645 * OCFS2_LOCK_QUEUED flag.
3646 *
3647 * We have two potential problems
3648 *
3649 * 1) If we do the last reference drop on our dentry_lock (via dput)
3650 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3651 * the downconvert to finish. Instead we take an elevated
3652 * reference and push the drop until after we've completed our
3653 * unblock processing.
3654 *
3655 * 2) There might be another process with a final reference,
3656 * waiting on us to finish processing. If this is the case, we
3657 * detect it and exit out - there's no more dentries anyway.
3658 */
3659static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3660 int blocking)
3661{
3662 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3663 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3664 struct dentry *dentry;
3665 unsigned long flags;
3666 int extra_ref = 0;
3667
3668 /*
3669 * This node is blocking another node from getting a read
3670 * lock. This happens when we've renamed within a
3671 * directory. We've forced the other nodes to d_delete(), but
3672 * we never actually dropped our lock because it's still
3673 * valid. The downconvert code will retain a PR for this node,
3674 * so there's no further work to do.
3675 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003676 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003677 return UNBLOCK_CONTINUE;
3678
3679 /*
3680 * Mark this inode as potentially orphaned. The code in
3681 * ocfs2_delete_inode() will figure out whether it actually
3682 * needs to be freed or not.
3683 */
3684 spin_lock(&oi->ip_lock);
3685 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3686 spin_unlock(&oi->ip_lock);
3687
3688 /*
3689 * Yuck. We need to make sure however that the check of
3690 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3691 * respect to a reference decrement or the setting of that
3692 * flag.
3693 */
3694 spin_lock_irqsave(&lockres->l_lock, flags);
3695 spin_lock(&dentry_attach_lock);
3696 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3697 && dl->dl_count) {
3698 dl->dl_count++;
3699 extra_ref = 1;
3700 }
3701 spin_unlock(&dentry_attach_lock);
3702 spin_unlock_irqrestore(&lockres->l_lock, flags);
3703
3704 mlog(0, "extra_ref = %d\n", extra_ref);
3705
3706 /*
3707 * We have a process waiting on us in ocfs2_dentry_iput(),
3708 * which means we can't have any more outstanding
3709 * aliases. There's no need to do any more work.
3710 */
3711 if (!extra_ref)
3712 return UNBLOCK_CONTINUE;
3713
3714 spin_lock(&dentry_attach_lock);
3715 while (1) {
3716 dentry = ocfs2_find_local_alias(dl->dl_inode,
3717 dl->dl_parent_blkno, 1);
3718 if (!dentry)
3719 break;
3720 spin_unlock(&dentry_attach_lock);
3721
3722 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3723 dentry->d_name.name);
3724
3725 /*
3726 * The following dcache calls may do an
3727 * iput(). Normally we don't want that from the
3728 * downconverting thread, but in this case it's ok
3729 * because the requesting node already has an
3730 * exclusive lock on the inode, so it can't be queued
3731 * for a downconvert.
3732 */
3733 d_delete(dentry);
3734 dput(dentry);
3735
3736 spin_lock(&dentry_attach_lock);
3737 }
3738 spin_unlock(&dentry_attach_lock);
3739
3740 /*
3741 * If we are the last holder of this dentry lock, there is no
3742 * reason to downconvert so skip straight to the unlock.
3743 */
3744 if (dl->dl_count == 1)
3745 return UNBLOCK_STOP_POST;
3746
3747 return UNBLOCK_CONTINUE_POST;
3748}
3749
Tao Ma8dec98e2009-08-18 11:19:58 +08003750static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
3751 int new_level)
3752{
3753 struct ocfs2_refcount_tree *tree =
3754 ocfs2_lock_res_refcount_tree(lockres);
3755
3756 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
3757}
3758
3759static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
3760 int blocking)
3761{
3762 struct ocfs2_refcount_tree *tree =
3763 ocfs2_lock_res_refcount_tree(lockres);
3764
3765 ocfs2_metadata_cache_purge(&tree->rf_ci);
3766
3767 return UNBLOCK_CONTINUE;
3768}
3769
Jan Kara9e33d692008-08-25 19:56:50 +02003770static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3771{
3772 struct ocfs2_qinfo_lvb *lvb;
3773 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3774 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3775 oinfo->dqi_gi.dqi_type);
3776
3777 mlog_entry_void();
3778
Mark Fasheha641dc22008-12-24 16:03:48 -08003779 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Jan Kara9e33d692008-08-25 19:56:50 +02003780 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3781 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3782 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3783 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3784 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3785 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3786 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3787
3788 mlog_exit_void();
3789}
3790
3791void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3792{
3793 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3794 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3795 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3796
3797 mlog_entry_void();
3798 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3799 ocfs2_cluster_unlock(osb, lockres, level);
3800 mlog_exit_void();
3801}
3802
3803static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3804{
3805 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3806 oinfo->dqi_gi.dqi_type);
3807 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3808 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Joel Becker85eb8b72008-11-25 15:31:27 +01003809 struct buffer_head *bh = NULL;
Jan Kara9e33d692008-08-25 19:56:50 +02003810 struct ocfs2_global_disk_dqinfo *gdinfo;
3811 int status = 0;
3812
Joel Becker1c520df2009-06-19 15:14:13 -07003813 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
3814 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
Jan Kara9e33d692008-08-25 19:56:50 +02003815 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3816 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3817 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3818 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3819 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3820 oinfo->dqi_gi.dqi_free_entry =
3821 be32_to_cpu(lvb->lvb_free_entry);
3822 } else {
Joel Becker85eb8b72008-11-25 15:31:27 +01003823 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3824 if (status) {
Jan Kara9e33d692008-08-25 19:56:50 +02003825 mlog_errno(status);
3826 goto bail;
3827 }
3828 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3829 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3830 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3831 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3832 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3833 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3834 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3835 oinfo->dqi_gi.dqi_free_entry =
3836 le32_to_cpu(gdinfo->dqi_free_entry);
3837 brelse(bh);
3838 ocfs2_track_lock_refresh(lockres);
3839 }
3840
3841bail:
3842 return status;
3843}
3844
3845/* Lock quota info, this function expects at least shared lock on the quota file
3846 * so that we can safely refresh quota info from disk. */
3847int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3848{
3849 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3850 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3851 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3852 int status = 0;
3853
3854 mlog_entry_void();
3855
3856 /* On RO devices, locking really isn't needed... */
3857 if (ocfs2_is_hard_readonly(osb)) {
3858 if (ex)
3859 status = -EROFS;
3860 goto bail;
3861 }
3862 if (ocfs2_mount_local(osb))
3863 goto bail;
3864
3865 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3866 if (status < 0) {
3867 mlog_errno(status);
3868 goto bail;
3869 }
3870 if (!ocfs2_should_refresh_lock_res(lockres))
3871 goto bail;
3872 /* OK, we have the lock but we need to refresh the quota info */
3873 status = ocfs2_refresh_qinfo(oinfo);
3874 if (status)
3875 ocfs2_qinfo_unlock(oinfo, ex);
3876 ocfs2_complete_lock_res_refresh(lockres, status);
3877bail:
3878 mlog_exit(status);
3879 return status;
3880}
3881
Tao Ma8dec98e2009-08-18 11:19:58 +08003882int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
3883{
3884 int status;
3885 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3886 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3887 struct ocfs2_super *osb = lockres->l_priv;
3888
3889
3890 if (ocfs2_is_hard_readonly(osb))
3891 return -EROFS;
3892
3893 if (ocfs2_mount_local(osb))
3894 return 0;
3895
3896 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3897 if (status < 0)
3898 mlog_errno(status);
3899
3900 return status;
3901}
3902
3903void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
3904{
3905 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3906 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
3907 struct ocfs2_super *osb = lockres->l_priv;
3908
3909 if (!ocfs2_mount_local(osb))
3910 ocfs2_cluster_unlock(osb, lockres, level);
3911}
3912
Joel Becker4670c462008-02-01 14:39:35 -08003913/*
3914 * This is the filesystem locking protocol. It provides the lock handling
3915 * hooks for the underlying DLM. It has a maximum version number.
3916 * The version number allows interoperability with systems running at
3917 * the same major number and an equal or smaller minor number.
3918 *
3919 * Whenever the filesystem does new things with locks (adds or removes a
3920 * lock, orders them differently, does different things underneath a lock),
3921 * the version must be changed. The protocol is negotiated when joining
3922 * the dlm domain. A node may join the domain if its major version is
3923 * identical to all other nodes and its minor version is greater than
3924 * or equal to all other nodes. When its minor version is greater than
3925 * the other nodes, it will run at the minor version specified by the
3926 * other nodes.
3927 *
3928 * If a locking change is made that will not be compatible with older
3929 * versions, the major number must be increased and the minor version set
3930 * to zero. If a change merely adds a behavior that can be disabled when
3931 * speaking to older versions, the minor version must be increased. If a
3932 * change adds a fully backwards compatible change (eg, LVB changes that
3933 * are just ignored by older versions), the version does not need to be
3934 * updated.
3935 */
Joel Becker24ef1812008-01-29 17:37:32 -08003936static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003937 .lp_max_version = {
3938 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3939 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3940 },
Joel Becker24ef1812008-01-29 17:37:32 -08003941 .lp_lock_ast = ocfs2_locking_ast,
3942 .lp_blocking_ast = ocfs2_blocking_ast,
3943 .lp_unlock_ast = ocfs2_unlock_ast,
3944};
3945
Joel Becker63e0c482008-01-30 16:58:36 -08003946void ocfs2_set_locking_protocol(void)
Joel Becker24ef1812008-01-29 17:37:32 -08003947{
Joel Becker63e0c482008-01-30 16:58:36 -08003948 ocfs2_stack_glue_set_locking_protocol(&lproto);
Joel Becker24ef1812008-01-29 17:37:32 -08003949}
3950
Joel Becker24ef1812008-01-29 17:37:32 -08003951
Adrian Bunk00600052008-01-29 00:11:41 +02003952static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3953 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003954{
3955 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003956 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003957 unsigned long flags;
3958
3959 /* Our reference to the lockres in this function can be
3960 * considered valid until we remove the OCFS2_LOCK_QUEUED
3961 * flag. */
3962
3963 mlog_entry_void();
3964
3965 BUG_ON(!lockres);
3966 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003967
3968 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3969
3970 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003971 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003972 * still be marked with OCFS2_LOCK_FREEING after this check,
3973 * but short circuiting here will still save us some
3974 * performance. */
3975 spin_lock_irqsave(&lockres->l_lock, flags);
3976 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3977 goto unqueue;
3978 spin_unlock_irqrestore(&lockres->l_lock, flags);
3979
Mark Fashehb5e500e2006-09-13 22:01:16 -07003980 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003981 if (status < 0)
3982 mlog_errno(status);
3983
3984 spin_lock_irqsave(&lockres->l_lock, flags);
3985unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003986 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003987 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3988 } else
3989 ocfs2_schedule_blocked_lock(osb, lockres);
3990
3991 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003992 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003993 spin_unlock_irqrestore(&lockres->l_lock, flags);
3994
Mark Fashehd680efe2006-09-08 14:14:34 -07003995 if (ctl.unblock_action != UNBLOCK_CONTINUE
3996 && lockres->l_ops->post_unlock)
3997 lockres->l_ops->post_unlock(osb, lockres);
3998
Mark Fashehccd979b2005-12-15 14:31:24 -08003999 mlog_exit_void();
4000}
4001
4002static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4003 struct ocfs2_lock_res *lockres)
4004{
4005 mlog_entry_void();
4006
4007 assert_spin_locked(&lockres->l_lock);
4008
4009 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4010 /* Do not schedule a lock for downconvert when it's on
4011 * the way to destruction - any nodes wanting access
4012 * to the resource will get it soon. */
4013 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
4014 lockres->l_name, lockres->l_flags);
4015 return;
4016 }
4017
4018 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4019
Mark Fasheh34d024f2007-09-24 15:56:19 -07004020 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08004021 if (list_empty(&lockres->l_blocked_list)) {
4022 list_add_tail(&lockres->l_blocked_list,
4023 &osb->blocked_lock_list);
4024 osb->blocked_lock_count++;
4025 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07004026 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08004027
4028 mlog_exit_void();
4029}
Mark Fasheh34d024f2007-09-24 15:56:19 -07004030
4031static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4032{
4033 unsigned long processed;
4034 struct ocfs2_lock_res *lockres;
4035
4036 mlog_entry_void();
4037
4038 spin_lock(&osb->dc_task_lock);
4039 /* grab this early so we know to try again if a state change and
4040 * wake happens part-way through our work */
4041 osb->dc_work_sequence = osb->dc_wake_sequence;
4042
4043 processed = osb->blocked_lock_count;
4044 while (processed) {
4045 BUG_ON(list_empty(&osb->blocked_lock_list));
4046
4047 lockres = list_entry(osb->blocked_lock_list.next,
4048 struct ocfs2_lock_res, l_blocked_list);
4049 list_del_init(&lockres->l_blocked_list);
4050 osb->blocked_lock_count--;
4051 spin_unlock(&osb->dc_task_lock);
4052
4053 BUG_ON(!processed);
4054 processed--;
4055
4056 ocfs2_process_blocked_lock(osb, lockres);
4057
4058 spin_lock(&osb->dc_task_lock);
4059 }
4060 spin_unlock(&osb->dc_task_lock);
4061
4062 mlog_exit_void();
4063}
4064
4065static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4066{
4067 int empty = 0;
4068
4069 spin_lock(&osb->dc_task_lock);
4070 if (list_empty(&osb->blocked_lock_list))
4071 empty = 1;
4072
4073 spin_unlock(&osb->dc_task_lock);
4074 return empty;
4075}
4076
4077static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4078{
4079 int should_wake = 0;
4080
4081 spin_lock(&osb->dc_task_lock);
4082 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4083 should_wake = 1;
4084 spin_unlock(&osb->dc_task_lock);
4085
4086 return should_wake;
4087}
4088
Adrian Bunk200bfae2008-02-17 10:20:38 +02004089static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07004090{
4091 int status = 0;
4092 struct ocfs2_super *osb = arg;
4093
4094 /* only quit once we've been asked to stop and there is no more
4095 * work available */
4096 while (!(kthread_should_stop() &&
4097 ocfs2_downconvert_thread_lists_empty(osb))) {
4098
4099 wait_event_interruptible(osb->dc_event,
4100 ocfs2_downconvert_thread_should_wake(osb) ||
4101 kthread_should_stop());
4102
4103 mlog(0, "downconvert_thread: awoken\n");
4104
4105 ocfs2_downconvert_thread_do_work(osb);
4106 }
4107
4108 osb->dc_task = NULL;
4109 return status;
4110}
4111
4112void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4113{
4114 spin_lock(&osb->dc_task_lock);
4115 /* make sure the voting thread gets a swipe at whatever changes
4116 * the caller may have made to the voting state */
4117 osb->dc_wake_sequence++;
4118 spin_unlock(&osb->dc_task_lock);
4119 wake_up(&osb->dc_event);
4120}