blob: c7653bb343e1a4d43d5efaacc1030d61b2d0c2d8 [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>
34
Mark Fashehccd979b2005-12-15 14:31:24 -080035#define MLOG_MASK_PREFIX ML_DLM_GLUE
36#include <cluster/masklog.h>
37
38#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080039#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080040
41#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070042#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080043#include "dlmglue.h"
44#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080045#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080046#include "heartbeat.h"
47#include "inode.h"
48#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080049#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080050#include "slot_map.h"
51#include "super.h"
52#include "uptodate.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053
54#include "buffer_head_io.h"
55
56struct ocfs2_mask_waiter {
57 struct list_head mw_item;
58 int mw_status;
59 struct completion mw_complete;
60 unsigned long mw_mask;
61 unsigned long mw_goal;
62};
63
Mark Fasheh54a7e752006-09-12 21:49:13 -070064static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
65static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080066static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080067
Mark Fashehd680efe2006-09-08 14:14:34 -070068/*
Mark Fashehcc567d82006-09-13 21:52:21 -070069 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070070 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070071 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070072 * and ocfs2_process_blocked_lock()
73 *
74 */
75enum ocfs2_unblock_action {
76 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
77 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
78 * ->post_unlock callback */
79 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
80 * ->post_unlock() callback. */
81};
82
83struct ocfs2_unblock_ctl {
84 int requeue;
85 enum ocfs2_unblock_action unblock_action;
86};
87
Mark Fasheh810d5ae2006-09-13 21:39:52 -070088static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
89 int new_level);
90static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
91
Mark Fashehcc567d82006-09-13 21:52:21 -070092static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
93 int blocking);
94
Mark Fashehcc567d82006-09-13 21:52:21 -070095static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
96 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -070097
98static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
99 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800100
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700101
102#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
103
104/* This aids in debugging situations where a bad LVB might be involved. */
105static void ocfs2_dump_meta_lvb_info(u64 level,
106 const char *function,
107 unsigned int line,
108 struct ocfs2_lock_res *lockres)
109{
Joel Becker8f2c9c12008-02-01 12:16:57 -0800110 struct ocfs2_meta_lvb *lvb =
111 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700112
113 mlog(level, "LVB information for %s (called from %s:%u):\n",
114 lockres->l_name, function, line);
115 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
116 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
117 be32_to_cpu(lvb->lvb_igeneration));
118 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
119 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
120 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
121 be16_to_cpu(lvb->lvb_imode));
122 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
123 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
124 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
125 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
126 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
127 be32_to_cpu(lvb->lvb_iattr));
128}
129
130
Mark Fashehf625c972006-09-12 21:24:53 -0700131/*
132 * OCFS2 Lock Resource Operations
133 *
134 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700135 *
136 * The most basic of lock types can point ->l_priv to their respective
137 * struct ocfs2_super and allow the default actions to manage things.
138 *
139 * Right now, each lock type also needs to implement an init function,
140 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
141 * should be called when the lock is no longer needed (i.e., object
142 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700143 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800144struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700145 /*
146 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
147 * this callback if ->l_priv is not an ocfs2_super pointer
148 */
149 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700150
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700151 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700152 * Optionally called in the downconvert thread after a
153 * successful downconvert. The lockres will not be referenced
154 * after this callback is called, so it is safe to free
155 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700156 *
157 * The exact semantics of when this is called are controlled
158 * by ->downconvert_worker()
159 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700160 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700161
162 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700163 * Allow a lock type to add checks to determine whether it is
164 * safe to downconvert a lock. Return 0 to re-queue the
165 * downconvert at a later time, nonzero to continue.
166 *
167 * For most locks, the default checks that there are no
168 * incompatible holders are sufficient.
169 *
170 * Called with the lockres spinlock held.
171 */
172 int (*check_downconvert)(struct ocfs2_lock_res *, int);
173
174 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700175 * Allows a lock type to populate the lock value block. This
176 * is called on downconvert, and when we drop a lock.
177 *
178 * Locks that want to use this should set LOCK_TYPE_USES_LVB
179 * in the flags field.
180 *
181 * Called with the lockres spinlock held.
182 */
183 void (*set_lvb)(struct ocfs2_lock_res *);
184
185 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700186 * Called from the downconvert thread when it is determined
187 * that a lock will be downconverted. This is called without
188 * any locks held so the function can do work that might
189 * schedule (syncing out data, etc).
190 *
191 * This should return any one of the ocfs2_unblock_action
192 * values, depending on what it wants the thread to do.
193 */
194 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
195
196 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700197 * LOCK_TYPE_* flags which describe the specific requirements
198 * of a lock type. Descriptions of each individual flag follow.
199 */
200 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800201};
202
Mark Fashehf625c972006-09-12 21:24:53 -0700203/*
204 * Some locks want to "refresh" potentially stale data when a
205 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
206 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
207 * individual lockres l_flags member from the ast function. It is
208 * expected that the locking wrapper will clear the
209 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
210 */
211#define LOCK_TYPE_REQUIRES_REFRESH 0x1
212
Mark Fashehb80fc012006-09-12 22:08:14 -0700213/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700214 * Indicate that a lock type makes use of the lock value block. The
215 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700216 */
217#define LOCK_TYPE_USES_LVB 0x2
218
Mark Fashehccd979b2005-12-15 14:31:24 -0800219static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700220 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700221 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800222};
223
Mark Fashehe63aecb62007-10-18 15:30:42 -0700224static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700225 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700226 .check_downconvert = ocfs2_check_meta_downconvert,
227 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700228 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700229 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800230};
231
Mark Fashehccd979b2005-12-15 14:31:24 -0800232static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700233 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800234};
235
236static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700237 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800238};
239
Mark Fashehd680efe2006-09-08 14:14:34 -0700240static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700241 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700242 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700243 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700244 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700245};
246
Tiger Yang50008632007-03-20 16:01:38 -0700247static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
248 .get_osb = ocfs2_get_inode_osb,
249 .flags = 0,
250};
251
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800252static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
253 .get_osb = ocfs2_get_file_osb,
254 .flags = 0,
255};
256
Mark Fashehccd979b2005-12-15 14:31:24 -0800257static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
258{
259 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700260 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
261 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800262}
263
Mark Fashehccd979b2005-12-15 14:31:24 -0800264static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
265{
266 BUG_ON(!ocfs2_is_inode_lock(lockres));
267
268 return (struct inode *) lockres->l_priv;
269}
270
Mark Fashehd680efe2006-09-08 14:14:34 -0700271static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
272{
273 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
274
275 return (struct ocfs2_dentry_lock *)lockres->l_priv;
276}
277
Mark Fasheh54a7e752006-09-12 21:49:13 -0700278static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
279{
280 if (lockres->l_ops->get_osb)
281 return lockres->l_ops->get_osb(lockres);
282
283 return (struct ocfs2_super *)lockres->l_priv;
284}
285
Mark Fashehccd979b2005-12-15 14:31:24 -0800286static int ocfs2_lock_create(struct ocfs2_super *osb,
287 struct ocfs2_lock_res *lockres,
288 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800289 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800290static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
291 int wanted);
292static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
293 struct ocfs2_lock_res *lockres,
294 int level);
295static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
296static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
297static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
298static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
299static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
300 struct ocfs2_lock_res *lockres);
301static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
302 int convert);
Joel Becker7431cd72008-02-01 12:15:37 -0800303#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
304 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
305 _err, _func, _lockres->l_name); \
Mark Fashehccd979b2005-12-15 14:31:24 -0800306} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700307static int ocfs2_downconvert_thread(void *arg);
308static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
309 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700310static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800311 struct buffer_head **bh);
312static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
313static inline int ocfs2_highest_compat_lock_level(int level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800314static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
315 int new_level);
316static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
317 struct ocfs2_lock_res *lockres,
318 int new_level,
319 int lvb);
320static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
321 struct ocfs2_lock_res *lockres);
322static int ocfs2_cancel_convert(struct ocfs2_super *osb,
323 struct ocfs2_lock_res *lockres);
324
Mark Fashehccd979b2005-12-15 14:31:24 -0800325
Mark Fashehccd979b2005-12-15 14:31:24 -0800326static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
327 u64 blkno,
328 u32 generation,
329 char *name)
330{
331 int len;
332
333 mlog_entry_void();
334
335 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
336
Mark Fashehb06970532006-03-03 10:24:33 -0800337 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
338 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
339 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800340
341 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
342
343 mlog(0, "built lock resource with name: %s\n", name);
344
345 mlog_exit_void();
346}
347
Ingo Molnar34af9462006-06-27 02:53:55 -0700348static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800349
350static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
351 struct ocfs2_dlm_debug *dlm_debug)
352{
353 mlog(0, "Add tracking for lockres %s\n", res->l_name);
354
355 spin_lock(&ocfs2_dlm_tracking_lock);
356 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
357 spin_unlock(&ocfs2_dlm_tracking_lock);
358}
359
360static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
361{
362 spin_lock(&ocfs2_dlm_tracking_lock);
363 if (!list_empty(&res->l_debug_list))
364 list_del_init(&res->l_debug_list);
365 spin_unlock(&ocfs2_dlm_tracking_lock);
366}
367
368static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *res,
370 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800371 struct ocfs2_lock_res_ops *ops,
372 void *priv)
373{
Mark Fashehccd979b2005-12-15 14:31:24 -0800374 res->l_type = type;
375 res->l_ops = ops;
376 res->l_priv = priv;
377
Joel Beckerbd3e7612008-02-01 12:14:57 -0800378 res->l_level = DLM_LOCK_IV;
379 res->l_requested = DLM_LOCK_IV;
380 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 res->l_action = OCFS2_AST_INVALID;
382 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
383
384 res->l_flags = OCFS2_LOCK_INITIALIZED;
385
386 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
387}
388
389void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
390{
391 /* This also clears out the lock status block */
392 memset(res, 0, sizeof(struct ocfs2_lock_res));
393 spin_lock_init(&res->l_lock);
394 init_waitqueue_head(&res->l_event);
395 INIT_LIST_HEAD(&res->l_blocked_list);
396 INIT_LIST_HEAD(&res->l_mask_waiters);
397}
398
399void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
400 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700401 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800402 struct inode *inode)
403{
404 struct ocfs2_lock_res_ops *ops;
405
406 switch(type) {
407 case OCFS2_LOCK_TYPE_RW:
408 ops = &ocfs2_inode_rw_lops;
409 break;
410 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700411 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800412 break;
Tiger Yang50008632007-03-20 16:01:38 -0700413 case OCFS2_LOCK_TYPE_OPEN:
414 ops = &ocfs2_inode_open_lops;
415 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 default:
417 mlog_bug_on_msg(1, "type: %d\n", type);
418 ops = NULL; /* thanks, gcc */
419 break;
420 };
421
Mark Fashehd680efe2006-09-08 14:14:34 -0700422 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700423 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700424 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
425}
426
Mark Fasheh54a7e752006-09-12 21:49:13 -0700427static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
428{
429 struct inode *inode = ocfs2_lock_res_inode(lockres);
430
431 return OCFS2_SB(inode->i_sb);
432}
433
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800434static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
435{
436 struct ocfs2_file_private *fp = lockres->l_priv;
437
438 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
439}
440
Mark Fashehd680efe2006-09-08 14:14:34 -0700441static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
442{
443 __be64 inode_blkno_be;
444
445 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
446 sizeof(__be64));
447
448 return be64_to_cpu(inode_blkno_be);
449}
450
Mark Fasheh54a7e752006-09-12 21:49:13 -0700451static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
452{
453 struct ocfs2_dentry_lock *dl = lockres->l_priv;
454
455 return OCFS2_SB(dl->dl_inode->i_sb);
456}
457
Mark Fashehd680efe2006-09-08 14:14:34 -0700458void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
459 u64 parent, struct inode *inode)
460{
461 int len;
462 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
463 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
464 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
465
466 ocfs2_lock_res_init_once(lockres);
467
468 /*
469 * Unfortunately, the standard lock naming scheme won't work
470 * here because we have two 16 byte values to use. Instead,
471 * we'll stuff the inode number as a binary value. We still
472 * want error prints to show something without garbling the
473 * display, so drop a null byte in there before the inode
474 * number. A future version of OCFS2 will likely use all
475 * binary lock names. The stringified names have been a
476 * tremendous aid in debugging, but now that the debugfs
477 * interface exists, we can mangle things there if need be.
478 *
479 * NOTE: We also drop the standard "pad" value (the total lock
480 * name size stays the same though - the last part is all
481 * zeros due to the memset in ocfs2_lock_res_init_once()
482 */
483 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
484 "%c%016llx",
485 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
486 (long long)parent);
487
488 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
489
490 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
491 sizeof(__be64));
492
493 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
494 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
495 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496}
497
498static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
499 struct ocfs2_super *osb)
500{
501 /* Superblock lockres doesn't come from a slab so we call init
502 * once on it manually. */
503 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700504 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
505 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800506 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800507 &ocfs2_super_lops, osb);
508}
509
510static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
511 struct ocfs2_super *osb)
512{
513 /* Rename lockres doesn't come from a slab so we call init
514 * once on it manually. */
515 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700516 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
517 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800518 &ocfs2_rename_lops, osb);
519}
520
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800521void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
522 struct ocfs2_file_private *fp)
523{
524 struct inode *inode = fp->fp_file->f_mapping->host;
525 struct ocfs2_inode_info *oi = OCFS2_I(inode);
526
527 ocfs2_lock_res_init_once(lockres);
528 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
529 inode->i_generation, lockres->l_name);
530 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
531 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
532 fp);
533 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
534}
535
Mark Fashehccd979b2005-12-15 14:31:24 -0800536void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
537{
538 mlog_entry_void();
539
540 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
541 return;
542
543 ocfs2_remove_lockres_tracking(res);
544
545 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
546 "Lockres %s is on the blocked list\n",
547 res->l_name);
548 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
549 "Lockres %s has mask waiters pending\n",
550 res->l_name);
551 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
552 "Lockres %s is locked\n",
553 res->l_name);
554 mlog_bug_on_msg(res->l_ro_holders,
555 "Lockres %s has %u ro holders\n",
556 res->l_name, res->l_ro_holders);
557 mlog_bug_on_msg(res->l_ex_holders,
558 "Lockres %s has %u ex holders\n",
559 res->l_name, res->l_ex_holders);
560
561 /* Need to clear out the lock status block for the dlm */
562 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
563
564 res->l_flags = 0UL;
565 mlog_exit_void();
566}
567
568static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
569 int level)
570{
571 mlog_entry_void();
572
573 BUG_ON(!lockres);
574
575 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800576 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800577 lockres->l_ex_holders++;
578 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800579 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800580 lockres->l_ro_holders++;
581 break;
582 default:
583 BUG();
584 }
585
586 mlog_exit_void();
587}
588
589static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
590 int level)
591{
592 mlog_entry_void();
593
594 BUG_ON(!lockres);
595
596 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800597 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800598 BUG_ON(!lockres->l_ex_holders);
599 lockres->l_ex_holders--;
600 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800601 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800602 BUG_ON(!lockres->l_ro_holders);
603 lockres->l_ro_holders--;
604 break;
605 default:
606 BUG();
607 }
608 mlog_exit_void();
609}
610
611/* WARNING: This function lives in a world where the only three lock
612 * levels are EX, PR, and NL. It *will* have to be adjusted when more
613 * lock types are added. */
614static inline int ocfs2_highest_compat_lock_level(int level)
615{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800616 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800617
Joel Beckerbd3e7612008-02-01 12:14:57 -0800618 if (level == DLM_LOCK_EX)
619 new_level = DLM_LOCK_NL;
620 else if (level == DLM_LOCK_PR)
621 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800622 return new_level;
623}
624
625static void lockres_set_flags(struct ocfs2_lock_res *lockres,
626 unsigned long newflags)
627{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200628 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800629
630 assert_spin_locked(&lockres->l_lock);
631
632 lockres->l_flags = newflags;
633
Christoph Hellwig800deef2007-05-17 16:03:13 +0200634 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800635 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
636 continue;
637
638 list_del_init(&mw->mw_item);
639 mw->mw_status = 0;
640 complete(&mw->mw_complete);
641 }
642}
643static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
644{
645 lockres_set_flags(lockres, lockres->l_flags | or);
646}
647static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
648 unsigned long clear)
649{
650 lockres_set_flags(lockres, lockres->l_flags & ~clear);
651}
652
653static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
654{
655 mlog_entry_void();
656
657 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
658 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
659 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800660 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800661
662 lockres->l_level = lockres->l_requested;
663 if (lockres->l_level <=
664 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800665 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
667 }
668 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
669
670 mlog_exit_void();
671}
672
673static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
674{
675 mlog_entry_void();
676
677 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
678 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
679
680 /* Convert from RO to EX doesn't really need anything as our
681 * information is already up to data. Convert from NL to
682 * *anything* however should mark ourselves as needing an
683 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800684 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700685 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800686 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
687
688 lockres->l_level = lockres->l_requested;
689 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
690
691 mlog_exit_void();
692}
693
694static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
695{
696 mlog_entry_void();
697
Roel Kluin3cf0c502007-10-27 00:20:36 +0200698 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800699 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
700
Joel Beckerbd3e7612008-02-01 12:14:57 -0800701 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700702 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
703 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
705
706 lockres->l_level = lockres->l_requested;
707 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
708 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
709
710 mlog_exit_void();
711}
712
Mark Fashehccd979b2005-12-15 14:31:24 -0800713static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
714 int level)
715{
716 int needs_downconvert = 0;
717 mlog_entry_void();
718
719 assert_spin_locked(&lockres->l_lock);
720
721 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
722
723 if (level > lockres->l_blocking) {
724 /* only schedule a downconvert if we haven't already scheduled
725 * one that goes low enough to satisfy the level we're
726 * blocking. this also catches the case where we get
727 * duplicate BASTs */
728 if (ocfs2_highest_compat_lock_level(level) <
729 ocfs2_highest_compat_lock_level(lockres->l_blocking))
730 needs_downconvert = 1;
731
732 lockres->l_blocking = level;
733 }
734
735 mlog_exit(needs_downconvert);
736 return needs_downconvert;
737}
738
Mark Fashehaa2623a2006-09-12 21:58:23 -0700739static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800740{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700741 struct ocfs2_lock_res *lockres = opaque;
742 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800743 int needs_downconvert;
744 unsigned long flags;
745
Joel Beckerbd3e7612008-02-01 12:14:57 -0800746 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800747
Mark Fashehaa2623a2006-09-12 21:58:23 -0700748 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
749 lockres->l_name, level, lockres->l_level,
750 ocfs2_lock_type_string(lockres->l_type));
751
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800752 /*
753 * We can skip the bast for locks which don't enable caching -
754 * they'll be dropped at the earliest possible time anyway.
755 */
756 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
757 return;
758
Mark Fashehccd979b2005-12-15 14:31:24 -0800759 spin_lock_irqsave(&lockres->l_lock, flags);
760 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
761 if (needs_downconvert)
762 ocfs2_schedule_blocked_lock(osb, lockres);
763 spin_unlock_irqrestore(&lockres->l_lock, flags);
764
Mark Fashehd680efe2006-09-08 14:14:34 -0700765 wake_up(&lockres->l_event);
766
Mark Fasheh34d024f2007-09-24 15:56:19 -0700767 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800768}
769
Mark Fashehe92d57d2006-09-12 21:34:35 -0700770static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800771{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700772 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 unsigned long flags;
774
775 spin_lock_irqsave(&lockres->l_lock, flags);
776
Joel Becker8f2c9c12008-02-01 12:16:57 -0800777 if (ocfs2_dlm_lock_status(&lockres->l_lksb)) {
778 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
779 lockres->l_name,
780 ocfs2_dlm_lock_status(&lockres->l_lksb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800781 spin_unlock_irqrestore(&lockres->l_lock, flags);
782 return;
783 }
784
785 switch(lockres->l_action) {
786 case OCFS2_AST_ATTACH:
787 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700788 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800789 break;
790 case OCFS2_AST_CONVERT:
791 ocfs2_generic_handle_convert_action(lockres);
792 break;
793 case OCFS2_AST_DOWNCONVERT:
794 ocfs2_generic_handle_downconvert_action(lockres);
795 break;
796 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700797 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
798 "lockres flags = 0x%lx, unlock action: %u\n",
799 lockres->l_name, lockres->l_action, lockres->l_flags,
800 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -0800801 BUG();
802 }
803
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 /* set it to something invalid so if we get called again we
805 * can catch it. */
806 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800807
808 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700809 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800810}
811
Mark Fashehccd979b2005-12-15 14:31:24 -0800812static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
813 int convert)
814{
815 unsigned long flags;
816
817 mlog_entry_void();
818 spin_lock_irqsave(&lockres->l_lock, flags);
819 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
820 if (convert)
821 lockres->l_action = OCFS2_AST_INVALID;
822 else
823 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
824 spin_unlock_irqrestore(&lockres->l_lock, flags);
825
826 wake_up(&lockres->l_event);
827 mlog_exit_void();
828}
829
830/* Note: If we detect another process working on the lock (i.e.,
831 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
832 * to do the right thing in that case.
833 */
834static int ocfs2_lock_create(struct ocfs2_super *osb,
835 struct ocfs2_lock_res *lockres,
836 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800837 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800838{
839 int ret = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800840 unsigned long flags;
841
842 mlog_entry_void();
843
Joel Beckerbd3e7612008-02-01 12:14:57 -0800844 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -0800845 dlm_flags);
846
847 spin_lock_irqsave(&lockres->l_lock, flags);
848 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
849 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
850 spin_unlock_irqrestore(&lockres->l_lock, flags);
851 goto bail;
852 }
853
854 lockres->l_action = OCFS2_AST_ATTACH;
855 lockres->l_requested = level;
856 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
857 spin_unlock_irqrestore(&lockres->l_lock, flags);
858
Joel Becker4670c462008-02-01 14:39:35 -0800859 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -0800860 level,
861 &lockres->l_lksb,
862 dlm_flags,
863 lockres->l_name,
864 OCFS2_LOCK_ID_MAX_LEN - 1,
865 lockres);
866 if (ret) {
867 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800868 ocfs2_recover_from_dlm_error(lockres, 1);
869 }
870
Joel Becker7431cd72008-02-01 12:15:37 -0800871 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800872
873bail:
874 mlog_exit(ret);
875 return ret;
876}
877
878static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
879 int flag)
880{
881 unsigned long flags;
882 int ret;
883
884 spin_lock_irqsave(&lockres->l_lock, flags);
885 ret = lockres->l_flags & flag;
886 spin_unlock_irqrestore(&lockres->l_lock, flags);
887
888 return ret;
889}
890
891static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
892
893{
894 wait_event(lockres->l_event,
895 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
896}
897
898static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
899
900{
901 wait_event(lockres->l_event,
902 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
903}
904
905/* predict what lock level we'll be dropping down to on behalf
906 * of another node, and return true if the currently wanted
907 * level will be compatible with it. */
908static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
909 int wanted)
910{
911 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
912
913 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
914}
915
916static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
917{
918 INIT_LIST_HEAD(&mw->mw_item);
919 init_completion(&mw->mw_complete);
920}
921
922static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
923{
924 wait_for_completion(&mw->mw_complete);
925 /* Re-arm the completion in case we want to wait on it again */
926 INIT_COMPLETION(mw->mw_complete);
927 return mw->mw_status;
928}
929
930static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
931 struct ocfs2_mask_waiter *mw,
932 unsigned long mask,
933 unsigned long goal)
934{
935 BUG_ON(!list_empty(&mw->mw_item));
936
937 assert_spin_locked(&lockres->l_lock);
938
939 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
940 mw->mw_mask = mask;
941 mw->mw_goal = goal;
942}
943
944/* returns 0 if the mw that was removed was already satisfied, -EBUSY
945 * if the mask still hadn't reached its goal */
946static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
947 struct ocfs2_mask_waiter *mw)
948{
949 unsigned long flags;
950 int ret = 0;
951
952 spin_lock_irqsave(&lockres->l_lock, flags);
953 if (!list_empty(&mw->mw_item)) {
954 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
955 ret = -EBUSY;
956
957 list_del_init(&mw->mw_item);
958 init_completion(&mw->mw_complete);
959 }
960 spin_unlock_irqrestore(&lockres->l_lock, flags);
961
962 return ret;
963
964}
965
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800966static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
967 struct ocfs2_lock_res *lockres)
968{
969 int ret;
970
971 ret = wait_for_completion_interruptible(&mw->mw_complete);
972 if (ret)
973 lockres_remove_mask_waiter(lockres, mw);
974 else
975 ret = mw->mw_status;
976 /* Re-arm the completion in case we want to wait on it again */
977 INIT_COMPLETION(mw->mw_complete);
978 return ret;
979}
980
Mark Fashehccd979b2005-12-15 14:31:24 -0800981static int ocfs2_cluster_lock(struct ocfs2_super *osb,
982 struct ocfs2_lock_res *lockres,
983 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800984 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 int arg_flags)
986{
987 struct ocfs2_mask_waiter mw;
Mark Fashehccd979b2005-12-15 14:31:24 -0800988 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
989 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
990 unsigned long flags;
991
992 mlog_entry_void();
993
994 ocfs2_init_mask_waiter(&mw);
995
Mark Fashehb80fc012006-09-12 22:08:14 -0700996 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -0800997 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -0700998
Mark Fashehccd979b2005-12-15 14:31:24 -0800999again:
1000 wait = 0;
1001
1002 if (catch_signals && signal_pending(current)) {
1003 ret = -ERESTARTSYS;
1004 goto out;
1005 }
1006
1007 spin_lock_irqsave(&lockres->l_lock, flags);
1008
1009 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1010 "Cluster lock called on freeing lockres %s! flags "
1011 "0x%lx\n", lockres->l_name, lockres->l_flags);
1012
1013 /* We only compare against the currently granted level
1014 * here. If the lock is blocked waiting on a downconvert,
1015 * we'll get caught below. */
1016 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1017 level > lockres->l_level) {
1018 /* is someone sitting in dlm_lock? If so, wait on
1019 * them. */
1020 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1021 wait = 1;
1022 goto unlock;
1023 }
1024
Mark Fashehccd979b2005-12-15 14:31:24 -08001025 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1026 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1027 /* is the lock is currently blocked on behalf of
1028 * another node */
1029 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1030 wait = 1;
1031 goto unlock;
1032 }
1033
1034 if (level > lockres->l_level) {
1035 if (lockres->l_action != OCFS2_AST_INVALID)
1036 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1037 lockres->l_name, lockres->l_action);
1038
Mark Fasheh019d1b22007-10-05 12:09:05 -07001039 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1040 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001041 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001042 } else {
1043 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001044 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001045 }
1046
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 lockres->l_requested = level;
1048 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1049 spin_unlock_irqrestore(&lockres->l_lock, flags);
1050
Joel Beckerbd3e7612008-02-01 12:14:57 -08001051 BUG_ON(level == DLM_LOCK_IV);
1052 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001053
1054 mlog(0, "lock %s, convert from %d to level = %d\n",
1055 lockres->l_name, lockres->l_level, level);
1056
1057 /* call dlm_lock to upgrade lock now */
Joel Becker4670c462008-02-01 14:39:35 -08001058 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08001059 level,
1060 &lockres->l_lksb,
1061 lkm_flags,
1062 lockres->l_name,
1063 OCFS2_LOCK_ID_MAX_LEN - 1,
1064 lockres);
1065 if (ret) {
1066 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1067 (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001068 ocfs2_log_dlm_error("ocfs2_dlm_lock",
Joel Becker7431cd72008-02-01 12:15:37 -08001069 ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 }
1071 ocfs2_recover_from_dlm_error(lockres, 1);
1072 goto out;
1073 }
1074
Joel Becker24ef1812008-01-29 17:37:32 -08001075 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001076 lockres->l_name);
1077
1078 /* At this point we've gone inside the dlm and need to
1079 * complete our work regardless. */
1080 catch_signals = 0;
1081
1082 /* wait for busy to clear and carry on */
1083 goto again;
1084 }
1085
1086 /* Ok, if we get here then we're good to go. */
1087 ocfs2_inc_holders(lockres, level);
1088
1089 ret = 0;
1090unlock:
1091 spin_unlock_irqrestore(&lockres->l_lock, flags);
1092out:
1093 /*
1094 * This is helping work around a lock inversion between the page lock
1095 * and dlm locks. One path holds the page lock while calling aops
1096 * which block acquiring dlm locks. The voting thread holds dlm
1097 * locks while acquiring page locks while down converting data locks.
1098 * This block is helping an aop path notice the inversion and back
1099 * off to unlock its page lock before trying the dlm lock again.
1100 */
1101 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1102 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1103 wait = 0;
1104 if (lockres_remove_mask_waiter(lockres, &mw))
1105 ret = -EAGAIN;
1106 else
1107 goto again;
1108 }
1109 if (wait) {
1110 ret = ocfs2_wait_for_mask(&mw);
1111 if (ret == 0)
1112 goto again;
1113 mlog_errno(ret);
1114 }
1115
1116 mlog_exit(ret);
1117 return ret;
1118}
1119
1120static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1121 struct ocfs2_lock_res *lockres,
1122 int level)
1123{
1124 unsigned long flags;
1125
1126 mlog_entry_void();
1127 spin_lock_irqsave(&lockres->l_lock, flags);
1128 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001129 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001130 spin_unlock_irqrestore(&lockres->l_lock, flags);
1131 mlog_exit_void();
1132}
1133
Adrian Bunkda661162006-11-20 03:24:28 +01001134static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1135 struct ocfs2_lock_res *lockres,
1136 int ex,
1137 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001138{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001139 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001140 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001141 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001142
1143 spin_lock_irqsave(&lockres->l_lock, flags);
1144 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1145 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1146 spin_unlock_irqrestore(&lockres->l_lock, flags);
1147
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001148 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001149}
1150
1151/* Grants us an EX lock on the data and metadata resources, skipping
1152 * the normal cluster directory lookup. Use this ONLY on newly created
1153 * inodes which other nodes can't possibly see, and which haven't been
1154 * hashed in the inode hash yet. This can give us a good performance
1155 * increase as it'll skip the network broadcast normally associated
1156 * with creating a new lock resource. */
1157int ocfs2_create_new_inode_locks(struct inode *inode)
1158{
1159 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001160 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001161
1162 BUG_ON(!inode);
1163 BUG_ON(!ocfs2_inode_is_new(inode));
1164
1165 mlog_entry_void();
1166
Mark Fashehb06970532006-03-03 10:24:33 -08001167 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168
1169 /* NOTE: That we don't increment any of the holder counts, nor
1170 * do we add anything to a journal handle. Since this is
1171 * supposed to be a new inode which the cluster doesn't know
1172 * about yet, there is no need to. As far as the LVB handling
1173 * is concerned, this is basically like acquiring an EX lock
1174 * on a resource which has an invalid one -- we'll set it
1175 * valid when we release the EX. */
1176
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001177 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001178 if (ret) {
1179 mlog_errno(ret);
1180 goto bail;
1181 }
1182
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001183 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001184 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001185 * don't use a generation in their lock names.
1186 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001187 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188 if (ret) {
1189 mlog_errno(ret);
1190 goto bail;
1191 }
1192
Tiger Yang50008632007-03-20 16:01:38 -07001193 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1194 if (ret) {
1195 mlog_errno(ret);
1196 goto bail;
1197 }
1198
Mark Fashehccd979b2005-12-15 14:31:24 -08001199bail:
1200 mlog_exit(ret);
1201 return ret;
1202}
1203
1204int ocfs2_rw_lock(struct inode *inode, int write)
1205{
1206 int status, level;
1207 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001209
1210 BUG_ON(!inode);
1211
1212 mlog_entry_void();
1213
Mark Fashehb06970532006-03-03 10:24:33 -08001214 mlog(0, "inode %llu take %s RW lock\n",
1215 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001216 write ? "EXMODE" : "PRMODE");
1217
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001218 if (ocfs2_mount_local(osb))
1219 return 0;
1220
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1222
Joel Beckerbd3e7612008-02-01 12:14:57 -08001223 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001224
1225 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1226 0);
1227 if (status < 0)
1228 mlog_errno(status);
1229
1230 mlog_exit(status);
1231 return status;
1232}
1233
1234void ocfs2_rw_unlock(struct inode *inode, int write)
1235{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001236 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001239
1240 mlog_entry_void();
1241
Mark Fashehb06970532006-03-03 10:24:33 -08001242 mlog(0, "inode %llu drop %s RW lock\n",
1243 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 write ? "EXMODE" : "PRMODE");
1245
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001246 if (!ocfs2_mount_local(osb))
1247 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001248
1249 mlog_exit_void();
1250}
1251
Tiger Yang50008632007-03-20 16:01:38 -07001252/*
1253 * ocfs2_open_lock always get PR mode lock.
1254 */
1255int ocfs2_open_lock(struct inode *inode)
1256{
1257 int status = 0;
1258 struct ocfs2_lock_res *lockres;
1259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1260
1261 BUG_ON(!inode);
1262
1263 mlog_entry_void();
1264
1265 mlog(0, "inode %llu take PRMODE open lock\n",
1266 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1267
1268 if (ocfs2_mount_local(osb))
1269 goto out;
1270
1271 lockres = &OCFS2_I(inode)->ip_open_lockres;
1272
1273 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001274 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001275 if (status < 0)
1276 mlog_errno(status);
1277
1278out:
1279 mlog_exit(status);
1280 return status;
1281}
1282
1283int ocfs2_try_open_lock(struct inode *inode, int write)
1284{
1285 int status = 0, level;
1286 struct ocfs2_lock_res *lockres;
1287 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1288
1289 BUG_ON(!inode);
1290
1291 mlog_entry_void();
1292
1293 mlog(0, "inode %llu try to take %s open lock\n",
1294 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1295 write ? "EXMODE" : "PRMODE");
1296
1297 if (ocfs2_mount_local(osb))
1298 goto out;
1299
1300 lockres = &OCFS2_I(inode)->ip_open_lockres;
1301
Joel Beckerbd3e7612008-02-01 12:14:57 -08001302 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001303
1304 /*
1305 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001306 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001307 * other nodes and the -EAGAIN will indicate to the caller that
1308 * this inode is still in use.
1309 */
1310 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001311 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001312
1313out:
1314 mlog_exit(status);
1315 return status;
1316}
1317
1318/*
1319 * ocfs2_open_unlock unlock PR and EX mode open locks.
1320 */
1321void ocfs2_open_unlock(struct inode *inode)
1322{
1323 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1325
1326 mlog_entry_void();
1327
1328 mlog(0, "inode %llu drop open lock\n",
1329 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1330
1331 if (ocfs2_mount_local(osb))
1332 goto out;
1333
1334 if(lockres->l_ro_holders)
1335 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001336 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001337 if(lockres->l_ex_holders)
1338 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001339 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001340
1341out:
1342 mlog_exit_void();
1343}
1344
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001345static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1346 int level)
1347{
1348 int ret;
1349 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1350 unsigned long flags;
1351 struct ocfs2_mask_waiter mw;
1352
1353 ocfs2_init_mask_waiter(&mw);
1354
1355retry_cancel:
1356 spin_lock_irqsave(&lockres->l_lock, flags);
1357 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1358 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1359 if (ret) {
1360 spin_unlock_irqrestore(&lockres->l_lock, flags);
1361 ret = ocfs2_cancel_convert(osb, lockres);
1362 if (ret < 0) {
1363 mlog_errno(ret);
1364 goto out;
1365 }
1366 goto retry_cancel;
1367 }
1368 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1369 spin_unlock_irqrestore(&lockres->l_lock, flags);
1370
1371 ocfs2_wait_for_mask(&mw);
1372 goto retry_cancel;
1373 }
1374
1375 ret = -ERESTARTSYS;
1376 /*
1377 * We may still have gotten the lock, in which case there's no
1378 * point to restarting the syscall.
1379 */
1380 if (lockres->l_level == level)
1381 ret = 0;
1382
1383 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1384 lockres->l_flags, lockres->l_level, lockres->l_action);
1385
1386 spin_unlock_irqrestore(&lockres->l_lock, flags);
1387
1388out:
1389 return ret;
1390}
1391
1392/*
1393 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1394 * flock() calls. The locking approach this requires is sufficiently
1395 * different from all other cluster lock types that we implement a
1396 * seperate path to the "low-level" dlm calls. In particular:
1397 *
1398 * - No optimization of lock levels is done - we take at exactly
1399 * what's been requested.
1400 *
1401 * - No lock caching is employed. We immediately downconvert to
1402 * no-lock at unlock time. This also means flock locks never go on
1403 * the blocking list).
1404 *
1405 * - Since userspace can trivially deadlock itself with flock, we make
1406 * sure to allow cancellation of a misbehaving applications flock()
1407 * request.
1408 *
1409 * - Access to any flock lockres doesn't require concurrency, so we
1410 * can simplify the code by requiring the caller to guarantee
1411 * serialization of dlmglue flock calls.
1412 */
1413int ocfs2_file_lock(struct file *file, int ex, int trylock)
1414{
1415 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1416 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1417 unsigned long flags;
1418 struct ocfs2_file_private *fp = file->private_data;
1419 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1420 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1421 struct ocfs2_mask_waiter mw;
1422
1423 ocfs2_init_mask_waiter(&mw);
1424
1425 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001426 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001427 mlog(ML_ERROR,
1428 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1429 "level: %u\n", lockres->l_name, lockres->l_flags,
1430 lockres->l_level);
1431 return -EINVAL;
1432 }
1433
1434 spin_lock_irqsave(&lockres->l_lock, flags);
1435 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1436 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1437 spin_unlock_irqrestore(&lockres->l_lock, flags);
1438
1439 /*
1440 * Get the lock at NLMODE to start - that way we
1441 * can cancel the upconvert request if need be.
1442 */
1443 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1444 if (ret < 0) {
1445 mlog_errno(ret);
1446 goto out;
1447 }
1448
1449 ret = ocfs2_wait_for_mask(&mw);
1450 if (ret) {
1451 mlog_errno(ret);
1452 goto out;
1453 }
1454 spin_lock_irqsave(&lockres->l_lock, flags);
1455 }
1456
1457 lockres->l_action = OCFS2_AST_CONVERT;
1458 lkm_flags |= LKM_CONVERT;
1459 lockres->l_requested = level;
1460 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1461
1462 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1463 spin_unlock_irqrestore(&lockres->l_lock, flags);
1464
Joel Becker4670c462008-02-01 14:39:35 -08001465 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
Joel Becker24ef1812008-01-29 17:37:32 -08001466 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1467 lockres);
Joel Becker7431cd72008-02-01 12:15:37 -08001468 if (ret) {
1469 if (!trylock || (ret != -EAGAIN)) {
Joel Becker24ef1812008-01-29 17:37:32 -08001470 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001471 ret = -EINVAL;
1472 }
1473
1474 ocfs2_recover_from_dlm_error(lockres, 1);
1475 lockres_remove_mask_waiter(lockres, &mw);
1476 goto out;
1477 }
1478
1479 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1480 if (ret == -ERESTARTSYS) {
1481 /*
1482 * Userspace can cause deadlock itself with
1483 * flock(). Current behavior locally is to allow the
1484 * deadlock, but abort the system call if a signal is
1485 * received. We follow this example, otherwise a
1486 * poorly written program could sit in kernel until
1487 * reboot.
1488 *
1489 * Handling this is a bit more complicated for Ocfs2
1490 * though. We can't exit this function with an
1491 * outstanding lock request, so a cancel convert is
1492 * required. We intentionally overwrite 'ret' - if the
1493 * cancel fails and the lock was granted, it's easier
1494 * to just bubble sucess back up to the user.
1495 */
1496 ret = ocfs2_flock_handle_signal(lockres, level);
1497 }
1498
1499out:
1500
1501 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1502 lockres->l_name, ex, trylock, ret);
1503 return ret;
1504}
1505
1506void ocfs2_file_unlock(struct file *file)
1507{
1508 int ret;
1509 unsigned long flags;
1510 struct ocfs2_file_private *fp = file->private_data;
1511 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1512 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1513 struct ocfs2_mask_waiter mw;
1514
1515 ocfs2_init_mask_waiter(&mw);
1516
1517 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1518 return;
1519
1520 if (lockres->l_level == LKM_NLMODE)
1521 return;
1522
1523 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1524 lockres->l_name, lockres->l_flags, lockres->l_level,
1525 lockres->l_action);
1526
1527 spin_lock_irqsave(&lockres->l_lock, flags);
1528 /*
1529 * Fake a blocking ast for the downconvert code.
1530 */
1531 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001532 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001533
1534 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1535 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1536 spin_unlock_irqrestore(&lockres->l_lock, flags);
1537
1538 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1539 if (ret) {
1540 mlog_errno(ret);
1541 return;
1542 }
1543
1544 ret = ocfs2_wait_for_mask(&mw);
1545 if (ret)
1546 mlog_errno(ret);
1547}
1548
Mark Fasheh34d024f2007-09-24 15:56:19 -07001549static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1550 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001551{
1552 int kick = 0;
1553
1554 mlog_entry_void();
1555
1556 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001557 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001558 * condition. */
1559 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1560 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001561 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001562 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1563 kick = 1;
1564 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001565 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001566 if (!lockres->l_ex_holders)
1567 kick = 1;
1568 break;
1569 default:
1570 BUG();
1571 }
1572 }
1573
1574 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001575 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001576
1577 mlog_exit_void();
1578}
1579
Mark Fashehccd979b2005-12-15 14:31:24 -08001580#define OCFS2_SEC_BITS 34
1581#define OCFS2_SEC_SHIFT (64 - 34)
1582#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1583
1584/* LVB only has room for 64 bits of time here so we pack it for
1585 * now. */
1586static u64 ocfs2_pack_timespec(struct timespec *spec)
1587{
1588 u64 res;
1589 u64 sec = spec->tv_sec;
1590 u32 nsec = spec->tv_nsec;
1591
1592 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1593
1594 return res;
1595}
1596
1597/* Call this with the lockres locked. I am reasonably sure we don't
1598 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001599 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001600static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1601{
1602 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001603 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001604 struct ocfs2_meta_lvb *lvb;
1605
1606 mlog_entry_void();
1607
Joel Becker8f2c9c12008-02-01 12:16:57 -08001608 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001609
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001610 /*
1611 * Invalidate the LVB of a deleted inode - this way other
1612 * nodes are forced to go to disk and discover the new inode
1613 * status.
1614 */
1615 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1616 lvb->lvb_version = 0;
1617 goto out;
1618 }
1619
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001620 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001621 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1622 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1623 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1624 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1625 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1626 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1627 lvb->lvb_iatime_packed =
1628 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1629 lvb->lvb_ictime_packed =
1630 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1631 lvb->lvb_imtime_packed =
1632 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001633 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001634 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001635 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001636
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001637out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001638 mlog_meta_lvb(0, lockres);
1639
1640 mlog_exit_void();
1641}
1642
1643static void ocfs2_unpack_timespec(struct timespec *spec,
1644 u64 packed_time)
1645{
1646 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1647 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1648}
1649
1650static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1651{
1652 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001653 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001654 struct ocfs2_meta_lvb *lvb;
1655
1656 mlog_entry_void();
1657
1658 mlog_meta_lvb(0, lockres);
1659
Joel Becker8f2c9c12008-02-01 12:16:57 -08001660 lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001661
1662 /* We're safe here without the lockres lock... */
1663 spin_lock(&oi->ip_lock);
1664 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1665 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1666
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001667 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001668 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001669 ocfs2_set_inode_flags(inode);
1670
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 /* fast-symlinks are a special case */
1672 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1673 inode->i_blocks = 0;
1674 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001675 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001676
1677 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1678 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1679 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1680 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1681 ocfs2_unpack_timespec(&inode->i_atime,
1682 be64_to_cpu(lvb->lvb_iatime_packed));
1683 ocfs2_unpack_timespec(&inode->i_mtime,
1684 be64_to_cpu(lvb->lvb_imtime_packed));
1685 ocfs2_unpack_timespec(&inode->i_ctime,
1686 be64_to_cpu(lvb->lvb_ictime_packed));
1687 spin_unlock(&oi->ip_lock);
1688
1689 mlog_exit_void();
1690}
1691
Mark Fashehf9e2d822006-09-12 15:35:49 -07001692static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1693 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001694{
Joel Becker8f2c9c12008-02-01 12:16:57 -08001695 struct ocfs2_meta_lvb *lvb =
1696 (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001697
Mark Fashehf9e2d822006-09-12 15:35:49 -07001698 if (lvb->lvb_version == OCFS2_LVB_VERSION
1699 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001700 return 1;
1701 return 0;
1702}
1703
1704/* Determine whether a lock resource needs to be refreshed, and
1705 * arbitrate who gets to refresh it.
1706 *
1707 * 0 means no refresh needed.
1708 *
1709 * > 0 means you need to refresh this and you MUST call
1710 * ocfs2_complete_lock_res_refresh afterwards. */
1711static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1712{
1713 unsigned long flags;
1714 int status = 0;
1715
1716 mlog_entry_void();
1717
1718refresh_check:
1719 spin_lock_irqsave(&lockres->l_lock, flags);
1720 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1721 spin_unlock_irqrestore(&lockres->l_lock, flags);
1722 goto bail;
1723 }
1724
1725 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1726 spin_unlock_irqrestore(&lockres->l_lock, flags);
1727
1728 ocfs2_wait_on_refreshing_lock(lockres);
1729 goto refresh_check;
1730 }
1731
1732 /* Ok, I'll be the one to refresh this lock. */
1733 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1734 spin_unlock_irqrestore(&lockres->l_lock, flags);
1735
1736 status = 1;
1737bail:
1738 mlog_exit(status);
1739 return status;
1740}
1741
1742/* If status is non zero, I'll mark it as not being in refresh
1743 * anymroe, but i won't clear the needs refresh flag. */
1744static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1745 int status)
1746{
1747 unsigned long flags;
1748 mlog_entry_void();
1749
1750 spin_lock_irqsave(&lockres->l_lock, flags);
1751 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1752 if (!status)
1753 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1754 spin_unlock_irqrestore(&lockres->l_lock, flags);
1755
1756 wake_up(&lockres->l_event);
1757
1758 mlog_exit_void();
1759}
1760
1761/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001762static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001763 struct buffer_head **bh)
1764{
1765 int status = 0;
1766 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001767 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001768 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001769 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001770
1771 mlog_entry_void();
1772
Mark Fashehbe9e9862007-04-18 15:22:08 -07001773 if (ocfs2_mount_local(osb))
1774 goto bail;
1775
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 spin_lock(&oi->ip_lock);
1777 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08001778 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001779 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001780 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001781 spin_unlock(&oi->ip_lock);
1782 status = -ENOENT;
1783 goto bail;
1784 }
1785 spin_unlock(&oi->ip_lock);
1786
Mark Fashehbe9e9862007-04-18 15:22:08 -07001787 if (!ocfs2_should_refresh_lock_res(lockres))
1788 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001789
1790 /* This will discard any caching information we might have had
1791 * for the inode metadata. */
1792 ocfs2_metadata_cache_purge(inode);
1793
Mark Fasheh83418972007-04-23 18:53:12 -07001794 ocfs2_extent_map_trunc(inode, 0);
1795
Mark Fashehbe9e9862007-04-18 15:22:08 -07001796 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001797 mlog(0, "Trusting LVB on inode %llu\n",
1798 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001799 ocfs2_refresh_inode_from_lvb(inode);
1800 } else {
1801 /* Boo, we have to go to disk. */
1802 /* read bh, cast, ocfs2_refresh_inode */
1803 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1804 bh, OCFS2_BH_CACHED, inode);
1805 if (status < 0) {
1806 mlog_errno(status);
1807 goto bail_refresh;
1808 }
1809 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1810
1811 /* This is a good chance to make sure we're not
1812 * locking an invalid object.
1813 *
1814 * We bug on a stale inode here because we checked
1815 * above whether it was wiped from disk. The wiping
1816 * node provides a guarantee that we receive that
1817 * message and can mark the inode before dropping any
1818 * locks associated with it. */
1819 if (!OCFS2_IS_VALID_DINODE(fe)) {
1820 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1821 status = -EIO;
1822 goto bail_refresh;
1823 }
1824 mlog_bug_on_msg(inode->i_generation !=
1825 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08001826 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001827 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001828 (unsigned long long)oi->ip_blkno,
1829 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001830 inode->i_generation);
1831 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1832 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08001833 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1834 (unsigned long long)oi->ip_blkno,
1835 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001836 le32_to_cpu(fe->i_flags));
1837
1838 ocfs2_refresh_inode(inode, fe);
1839 }
1840
1841 status = 0;
1842bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07001843 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001844bail:
1845 mlog_exit(status);
1846 return status;
1847}
1848
1849static int ocfs2_assign_bh(struct inode *inode,
1850 struct buffer_head **ret_bh,
1851 struct buffer_head *passed_bh)
1852{
1853 int status;
1854
1855 if (passed_bh) {
1856 /* Ok, the update went to disk for us, use the
1857 * returned bh. */
1858 *ret_bh = passed_bh;
1859 get_bh(*ret_bh);
1860
1861 return 0;
1862 }
1863
1864 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1865 OCFS2_I(inode)->ip_blkno,
1866 ret_bh,
1867 OCFS2_BH_CACHED,
1868 inode);
1869 if (status < 0)
1870 mlog_errno(status);
1871
1872 return status;
1873}
1874
1875/*
1876 * returns < 0 error if the callback will never be called, otherwise
1877 * the result of the lock will be communicated via the callback.
1878 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001879int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001880 struct buffer_head **ret_bh,
1881 int ex,
1882 int arg_flags)
1883{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001884 int status, level, acquired;
1885 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001886 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001887 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1888 struct buffer_head *local_bh = NULL;
1889
1890 BUG_ON(!inode);
1891
1892 mlog_entry_void();
1893
Mark Fashehb06970532006-03-03 10:24:33 -08001894 mlog(0, "inode %llu, take %s META lock\n",
1895 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001896 ex ? "EXMODE" : "PRMODE");
1897
1898 status = 0;
1899 acquired = 0;
1900 /* We'll allow faking a readonly metadata lock for
1901 * rodevices. */
1902 if (ocfs2_is_hard_readonly(osb)) {
1903 if (ex)
1904 status = -EROFS;
1905 goto bail;
1906 }
1907
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001908 if (ocfs2_mount_local(osb))
1909 goto local;
1910
Mark Fashehccd979b2005-12-15 14:31:24 -08001911 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001912 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001913
Mark Fashehe63aecb62007-10-18 15:30:42 -07001914 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001915 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001916 dlm_flags = 0;
1917 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001918 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001919
1920 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1921 if (status < 0) {
1922 if (status != -EAGAIN && status != -EIOCBRETRY)
1923 mlog_errno(status);
1924 goto bail;
1925 }
1926
1927 /* Notify the error cleanup path to drop the cluster lock. */
1928 acquired = 1;
1929
1930 /* We wait twice because a node may have died while we were in
1931 * the lower dlm layers. The second time though, we've
1932 * committed to owning this lock so we don't allow signals to
1933 * abort the operation. */
1934 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001935 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001936
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001937local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001938 /*
1939 * We only see this flag if we're being called from
1940 * ocfs2_read_locked_inode(). It means we're locking an inode
1941 * which hasn't been populated yet, so clear the refresh flag
1942 * and let the caller handle it.
1943 */
1944 if (inode->i_state & I_NEW) {
1945 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001946 if (lockres)
1947 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001948 goto bail;
1949 }
1950
Mark Fashehccd979b2005-12-15 14:31:24 -08001951 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07001952 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08001953 * may or may not read one, depending on what's in the
1954 * LVB. The result of all of this is that we've *only* gone to
1955 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001956 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001957 if (status < 0) {
1958 if (status != -ENOENT)
1959 mlog_errno(status);
1960 goto bail;
1961 }
1962
1963 if (ret_bh) {
1964 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
1965 if (status < 0) {
1966 mlog_errno(status);
1967 goto bail;
1968 }
1969 }
1970
Mark Fashehccd979b2005-12-15 14:31:24 -08001971bail:
1972 if (status < 0) {
1973 if (ret_bh && (*ret_bh)) {
1974 brelse(*ret_bh);
1975 *ret_bh = NULL;
1976 }
1977 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07001978 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08001979 }
1980
1981 if (local_bh)
1982 brelse(local_bh);
1983
1984 mlog_exit(status);
1985 return status;
1986}
1987
1988/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07001989 * This is working around a lock inversion between tasks acquiring DLM
1990 * locks while holding a page lock and the downconvert thread which
1991 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08001992 *
1993 * ** These _with_page variantes are only intended to be called from aop
1994 * methods that hold page locks and return a very specific *positive* error
1995 * code that aop methods pass up to the VFS -- test for errors with != 0. **
1996 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07001997 * The DLM is called such that it returns -EAGAIN if it would have
1998 * blocked waiting for the downconvert thread. In that case we unlock
1999 * our page so the downconvert thread can make progress. Once we've
2000 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2001 * that called us can bubble that back up into the VFS who will then
2002 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002003 *
2004 * We do a blocking lock and immediate unlock before returning, though, so that
2005 * the lock has a great chance of being cached on this node by the time the VFS
2006 * calls back to retry the aop. This has a potential to livelock as nodes
2007 * ping locks back and forth, but that's a risk we're willing to take to avoid
2008 * the lock inversion simply.
2009 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002010int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002011 struct buffer_head **ret_bh,
2012 int ex,
2013 struct page *page)
2014{
2015 int ret;
2016
Mark Fashehe63aecb62007-10-18 15:30:42 -07002017 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002018 if (ret == -EAGAIN) {
2019 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002020 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2021 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 ret = AOP_TRUNCATED_PAGE;
2023 }
2024
2025 return ret;
2026}
2027
Mark Fashehe63aecb62007-10-18 15:30:42 -07002028int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002029 struct vfsmount *vfsmnt,
2030 int *level)
2031{
2032 int ret;
2033
2034 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002035 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002036 if (ret < 0) {
2037 mlog_errno(ret);
2038 return ret;
2039 }
2040
2041 /*
2042 * If we should update atime, we will get EX lock,
2043 * otherwise we just get PR lock.
2044 */
2045 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2046 struct buffer_head *bh = NULL;
2047
Mark Fashehe63aecb62007-10-18 15:30:42 -07002048 ocfs2_inode_unlock(inode, 0);
2049 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002050 if (ret < 0) {
2051 mlog_errno(ret);
2052 return ret;
2053 }
2054 *level = 1;
2055 if (ocfs2_should_update_atime(inode, vfsmnt))
2056 ocfs2_update_inode_atime(inode, bh);
2057 if (bh)
2058 brelse(bh);
2059 } else
2060 *level = 0;
2061
2062 mlog_exit(ret);
2063 return ret;
2064}
2065
Mark Fashehe63aecb62007-10-18 15:30:42 -07002066void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002067 int ex)
2068{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002069 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002070 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002071 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002072
2073 mlog_entry_void();
2074
Mark Fashehb06970532006-03-03 10:24:33 -08002075 mlog(0, "inode %llu drop %s META lock\n",
2076 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002077 ex ? "EXMODE" : "PRMODE");
2078
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002079 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2080 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002081 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2082
2083 mlog_exit_void();
2084}
2085
2086int ocfs2_super_lock(struct ocfs2_super *osb,
2087 int ex)
2088{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002089 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002090 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002091 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002092
2093 mlog_entry_void();
2094
2095 if (ocfs2_is_hard_readonly(osb))
2096 return -EROFS;
2097
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002098 if (ocfs2_mount_local(osb))
2099 goto bail;
2100
Mark Fashehccd979b2005-12-15 14:31:24 -08002101 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2102 if (status < 0) {
2103 mlog_errno(status);
2104 goto bail;
2105 }
2106
2107 /* The super block lock path is really in the best position to
2108 * know when resources covered by the lock need to be
2109 * refreshed, so we do it here. Of course, making sense of
2110 * everything is up to the caller :) */
2111 status = ocfs2_should_refresh_lock_res(lockres);
2112 if (status < 0) {
2113 mlog_errno(status);
2114 goto bail;
2115 }
2116 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002117 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002118
2119 ocfs2_complete_lock_res_refresh(lockres, status);
2120
2121 if (status < 0)
2122 mlog_errno(status);
2123 }
2124bail:
2125 mlog_exit(status);
2126 return status;
2127}
2128
2129void ocfs2_super_unlock(struct ocfs2_super *osb,
2130 int ex)
2131{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002132 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002133 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2134
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002135 if (!ocfs2_mount_local(osb))
2136 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002137}
2138
2139int ocfs2_rename_lock(struct ocfs2_super *osb)
2140{
2141 int status;
2142 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2143
2144 if (ocfs2_is_hard_readonly(osb))
2145 return -EROFS;
2146
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002147 if (ocfs2_mount_local(osb))
2148 return 0;
2149
Joel Beckerbd3e7612008-02-01 12:14:57 -08002150 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002151 if (status < 0)
2152 mlog_errno(status);
2153
2154 return status;
2155}
2156
2157void ocfs2_rename_unlock(struct ocfs2_super *osb)
2158{
2159 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2160
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002161 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002162 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002163}
2164
Mark Fashehd680efe2006-09-08 14:14:34 -07002165int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2166{
2167 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002168 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002169 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2170 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2171
2172 BUG_ON(!dl);
2173
2174 if (ocfs2_is_hard_readonly(osb))
2175 return -EROFS;
2176
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002177 if (ocfs2_mount_local(osb))
2178 return 0;
2179
Mark Fashehd680efe2006-09-08 14:14:34 -07002180 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2181 if (ret < 0)
2182 mlog_errno(ret);
2183
2184 return ret;
2185}
2186
2187void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2188{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002189 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002190 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2191 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2192
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002193 if (!ocfs2_mount_local(osb))
2194 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002195}
2196
Mark Fashehccd979b2005-12-15 14:31:24 -08002197/* Reference counting of the dlm debug structure. We want this because
2198 * open references on the debug inodes can live on after a mount, so
2199 * we can't rely on the ocfs2_super to always exist. */
2200static void ocfs2_dlm_debug_free(struct kref *kref)
2201{
2202 struct ocfs2_dlm_debug *dlm_debug;
2203
2204 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2205
2206 kfree(dlm_debug);
2207}
2208
2209void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2210{
2211 if (dlm_debug)
2212 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2213}
2214
2215static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2216{
2217 kref_get(&debug->d_refcnt);
2218}
2219
2220struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2221{
2222 struct ocfs2_dlm_debug *dlm_debug;
2223
2224 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2225 if (!dlm_debug) {
2226 mlog_errno(-ENOMEM);
2227 goto out;
2228 }
2229
2230 kref_init(&dlm_debug->d_refcnt);
2231 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2232 dlm_debug->d_locking_state = NULL;
2233out:
2234 return dlm_debug;
2235}
2236
2237/* Access to this is arbitrated for us via seq_file->sem. */
2238struct ocfs2_dlm_seq_priv {
2239 struct ocfs2_dlm_debug *p_dlm_debug;
2240 struct ocfs2_lock_res p_iter_res;
2241 struct ocfs2_lock_res p_tmp_res;
2242};
2243
2244static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2245 struct ocfs2_dlm_seq_priv *priv)
2246{
2247 struct ocfs2_lock_res *iter, *ret = NULL;
2248 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2249
2250 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2251
2252 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2253 /* discover the head of the list */
2254 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2255 mlog(0, "End of list found, %p\n", ret);
2256 break;
2257 }
2258
2259 /* We track our "dummy" iteration lockres' by a NULL
2260 * l_ops field. */
2261 if (iter->l_ops != NULL) {
2262 ret = iter;
2263 break;
2264 }
2265 }
2266
2267 return ret;
2268}
2269
2270static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2271{
2272 struct ocfs2_dlm_seq_priv *priv = m->private;
2273 struct ocfs2_lock_res *iter;
2274
2275 spin_lock(&ocfs2_dlm_tracking_lock);
2276 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2277 if (iter) {
2278 /* Since lockres' have the lifetime of their container
2279 * (which can be inodes, ocfs2_supers, etc) we want to
2280 * copy this out to a temporary lockres while still
2281 * under the spinlock. Obviously after this we can't
2282 * trust any pointers on the copy returned, but that's
2283 * ok as the information we want isn't typically held
2284 * in them. */
2285 priv->p_tmp_res = *iter;
2286 iter = &priv->p_tmp_res;
2287 }
2288 spin_unlock(&ocfs2_dlm_tracking_lock);
2289
2290 return iter;
2291}
2292
2293static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2294{
2295}
2296
2297static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2298{
2299 struct ocfs2_dlm_seq_priv *priv = m->private;
2300 struct ocfs2_lock_res *iter = v;
2301 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2302
2303 spin_lock(&ocfs2_dlm_tracking_lock);
2304 iter = ocfs2_dlm_next_res(iter, priv);
2305 list_del_init(&dummy->l_debug_list);
2306 if (iter) {
2307 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2308 priv->p_tmp_res = *iter;
2309 iter = &priv->p_tmp_res;
2310 }
2311 spin_unlock(&ocfs2_dlm_tracking_lock);
2312
2313 return iter;
2314}
2315
2316/* So that debugfs.ocfs2 can determine which format is being used */
2317#define OCFS2_DLM_DEBUG_STR_VERSION 1
2318static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2319{
2320 int i;
2321 char *lvb;
2322 struct ocfs2_lock_res *lockres = v;
2323
2324 if (!lockres)
2325 return -EINVAL;
2326
Mark Fashehd680efe2006-09-08 14:14:34 -07002327 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2328
2329 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2330 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2331 lockres->l_name,
2332 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2333 else
2334 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2335
2336 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002337 "0x%lx\t"
2338 "0x%x\t"
2339 "0x%x\t"
2340 "%u\t"
2341 "%u\t"
2342 "%d\t"
2343 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002344 lockres->l_level,
2345 lockres->l_flags,
2346 lockres->l_action,
2347 lockres->l_unlock_action,
2348 lockres->l_ro_holders,
2349 lockres->l_ex_holders,
2350 lockres->l_requested,
2351 lockres->l_blocking);
2352
2353 /* Dump the raw LVB */
Joel Becker8f2c9c12008-02-01 12:16:57 -08002354 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002355 for(i = 0; i < DLM_LVB_LEN; i++)
2356 seq_printf(m, "0x%x\t", lvb[i]);
2357
2358 /* End the line */
2359 seq_printf(m, "\n");
2360 return 0;
2361}
2362
Jan Engelhardt90d99772008-01-22 20:52:20 +01002363static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 .start = ocfs2_dlm_seq_start,
2365 .stop = ocfs2_dlm_seq_stop,
2366 .next = ocfs2_dlm_seq_next,
2367 .show = ocfs2_dlm_seq_show,
2368};
2369
2370static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2371{
2372 struct seq_file *seq = (struct seq_file *) file->private_data;
2373 struct ocfs2_dlm_seq_priv *priv = seq->private;
2374 struct ocfs2_lock_res *res = &priv->p_iter_res;
2375
2376 ocfs2_remove_lockres_tracking(res);
2377 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2378 return seq_release_private(inode, file);
2379}
2380
2381static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2382{
2383 int ret;
2384 struct ocfs2_dlm_seq_priv *priv;
2385 struct seq_file *seq;
2386 struct ocfs2_super *osb;
2387
2388 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2389 if (!priv) {
2390 ret = -ENOMEM;
2391 mlog_errno(ret);
2392 goto out;
2393 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002394 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002395 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2396 priv->p_dlm_debug = osb->osb_dlm_debug;
2397 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2398
2399 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2400 if (ret) {
2401 kfree(priv);
2402 mlog_errno(ret);
2403 goto out;
2404 }
2405
2406 seq = (struct seq_file *) file->private_data;
2407 seq->private = priv;
2408
2409 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2410 priv->p_dlm_debug);
2411
2412out:
2413 return ret;
2414}
2415
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002416static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002417 .open = ocfs2_dlm_debug_open,
2418 .release = ocfs2_dlm_debug_release,
2419 .read = seq_read,
2420 .llseek = seq_lseek,
2421};
2422
2423static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2424{
2425 int ret = 0;
2426 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2427
2428 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2429 S_IFREG|S_IRUSR,
2430 osb->osb_debug_root,
2431 osb,
2432 &ocfs2_dlm_debug_fops);
2433 if (!dlm_debug->d_locking_state) {
2434 ret = -EINVAL;
2435 mlog(ML_ERROR,
2436 "Unable to create locking state debugfs file.\n");
2437 goto out;
2438 }
2439
2440 ocfs2_get_dlm_debug(dlm_debug);
2441out:
2442 return ret;
2443}
2444
2445static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2446{
2447 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2448
2449 if (dlm_debug) {
2450 debugfs_remove(dlm_debug->d_locking_state);
2451 ocfs2_put_dlm_debug(dlm_debug);
2452 }
2453}
2454
2455int ocfs2_dlm_init(struct ocfs2_super *osb)
2456{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002457 int status = 0;
Joel Becker4670c462008-02-01 14:39:35 -08002458 struct ocfs2_cluster_connection *conn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002459
2460 mlog_entry_void();
2461
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002462 if (ocfs2_mount_local(osb)) {
2463 osb->node_num = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002464 goto local;
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002465 }
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002466
Mark Fashehccd979b2005-12-15 14:31:24 -08002467 status = ocfs2_dlm_init_debug(osb);
2468 if (status < 0) {
2469 mlog_errno(status);
2470 goto bail;
2471 }
2472
Mark Fasheh34d024f2007-09-24 15:56:19 -07002473 /* launch downconvert thread */
2474 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2475 if (IS_ERR(osb->dc_task)) {
2476 status = PTR_ERR(osb->dc_task);
2477 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002478 mlog_errno(status);
2479 goto bail;
2480 }
2481
Mark Fashehccd979b2005-12-15 14:31:24 -08002482 /* for now, uuid == domain */
Joel Becker4670c462008-02-01 14:39:35 -08002483 status = ocfs2_cluster_connect(osb->uuid_str,
2484 strlen(osb->uuid_str),
2485 ocfs2_do_node_down, osb,
2486 &conn);
2487 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002488 mlog_errno(status);
2489 goto bail;
2490 }
2491
Mark Fasheh0abd6d12008-01-29 16:59:56 -08002492 status = ocfs2_cluster_this_node(&osb->node_num);
2493 if (status < 0) {
2494 mlog_errno(status);
2495 mlog(ML_ERROR,
2496 "could not find this host's node number\n");
2497 ocfs2_cluster_disconnect(conn);
2498 goto bail;
2499 }
2500
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002501local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002502 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2503 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2504
Joel Becker4670c462008-02-01 14:39:35 -08002505 osb->cconn = conn;
Mark Fashehccd979b2005-12-15 14:31:24 -08002506
2507 status = 0;
2508bail:
2509 if (status < 0) {
2510 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002511 if (osb->dc_task)
2512 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002513 }
2514
2515 mlog_exit(status);
2516 return status;
2517}
2518
2519void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2520{
2521 mlog_entry_void();
2522
Mark Fashehccd979b2005-12-15 14:31:24 -08002523 ocfs2_drop_osb_locks(osb);
2524
Joel Becker4670c462008-02-01 14:39:35 -08002525 /*
2526 * Now that we have dropped all locks and ocfs2_dismount_volume()
2527 * has disabled recovery, the DLM won't be talking to us. It's
2528 * safe to tear things down before disconnecting the cluster.
2529 */
2530
Mark Fasheh34d024f2007-09-24 15:56:19 -07002531 if (osb->dc_task) {
2532 kthread_stop(osb->dc_task);
2533 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002534 }
2535
2536 ocfs2_lock_res_free(&osb->osb_super_lockres);
2537 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2538
Joel Becker4670c462008-02-01 14:39:35 -08002539 ocfs2_cluster_disconnect(osb->cconn);
2540 osb->cconn = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002541
2542 ocfs2_dlm_shutdown_debug(osb);
2543
2544 mlog_exit_void();
2545}
2546
Joel Becker7431cd72008-02-01 12:15:37 -08002547static void ocfs2_unlock_ast(void *opaque, int error)
Mark Fashehccd979b2005-12-15 14:31:24 -08002548{
2549 struct ocfs2_lock_res *lockres = opaque;
2550 unsigned long flags;
2551
2552 mlog_entry_void();
2553
2554 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2555 lockres->l_unlock_action);
2556
2557 spin_lock_irqsave(&lockres->l_lock, flags);
2558 /* We tried to cancel a convert request, but it was already
2559 * granted. All we want to do here is clear our unlock
2560 * state. The wake_up call done at the bottom is redundant
2561 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2562 * hurt anything anyway */
Joel Becker7431cd72008-02-01 12:15:37 -08002563 if (error == -DLM_ECANCEL &&
Mark Fashehccd979b2005-12-15 14:31:24 -08002564 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2565 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2566
2567 /* We don't clear the busy flag in this case as it
2568 * should have been cleared by the ast which the dlm
2569 * has called. */
2570 goto complete_unlock;
2571 }
2572
Joel Becker7431cd72008-02-01 12:15:37 -08002573 /* DLM_EUNLOCK is the success code for unlock */
2574 if (error != -DLM_EUNLOCK) {
2575 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2576 "unlock_action %d\n", error, lockres->l_name,
Mark Fashehccd979b2005-12-15 14:31:24 -08002577 lockres->l_unlock_action);
2578 spin_unlock_irqrestore(&lockres->l_lock, flags);
2579 return;
2580 }
2581
2582 switch(lockres->l_unlock_action) {
2583 case OCFS2_UNLOCK_CANCEL_CONVERT:
2584 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2585 lockres->l_action = OCFS2_AST_INVALID;
2586 break;
2587 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002588 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002589 break;
2590 default:
2591 BUG();
2592 }
2593
2594 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2595complete_unlock:
2596 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2597 spin_unlock_irqrestore(&lockres->l_lock, flags);
2598
2599 wake_up(&lockres->l_event);
2600
2601 mlog_exit_void();
2602}
2603
Mark Fashehccd979b2005-12-15 14:31:24 -08002604static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002605 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002606{
Joel Becker7431cd72008-02-01 12:15:37 -08002607 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002608 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002609 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002610
2611 /* We didn't get anywhere near actually using this lockres. */
2612 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2613 goto out;
2614
Mark Fashehb80fc012006-09-12 22:08:14 -07002615 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002616 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002617
Mark Fashehccd979b2005-12-15 14:31:24 -08002618 spin_lock_irqsave(&lockres->l_lock, flags);
2619
2620 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2621 "lockres %s, flags 0x%lx\n",
2622 lockres->l_name, lockres->l_flags);
2623
2624 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2625 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2626 "%u, unlock_action = %u\n",
2627 lockres->l_name, lockres->l_flags, lockres->l_action,
2628 lockres->l_unlock_action);
2629
2630 spin_unlock_irqrestore(&lockres->l_lock, flags);
2631
2632 /* XXX: Today we just wait on any busy
2633 * locks... Perhaps we need to cancel converts in the
2634 * future? */
2635 ocfs2_wait_on_busy_lock(lockres);
2636
2637 spin_lock_irqsave(&lockres->l_lock, flags);
2638 }
2639
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002640 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2641 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08002642 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002643 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2644 lockres->l_ops->set_lvb(lockres);
2645 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002646
2647 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2648 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2649 lockres->l_name);
2650 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2651 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2652
2653 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2654 spin_unlock_irqrestore(&lockres->l_lock, flags);
2655 goto out;
2656 }
2657
2658 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2659
2660 /* make sure we never get here while waiting for an ast to
2661 * fire. */
2662 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2663
2664 /* is this necessary? */
2665 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2666 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2667 spin_unlock_irqrestore(&lockres->l_lock, flags);
2668
2669 mlog(0, "lock %s\n", lockres->l_name);
2670
Joel Becker4670c462008-02-01 14:39:35 -08002671 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
Joel Becker7431cd72008-02-01 12:15:37 -08002672 lockres);
2673 if (ret) {
2674 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002675 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
Joel Becker8f2c9c12008-02-01 12:16:57 -08002676 /* XXX Need to abstract this */
2677 dlm_print_one_lock(lockres->l_lksb.lksb_o2dlm.lockid);
Mark Fashehccd979b2005-12-15 14:31:24 -08002678 BUG();
2679 }
Joel Becker24ef1812008-01-29 17:37:32 -08002680 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002681 lockres->l_name);
2682
2683 ocfs2_wait_on_busy_lock(lockres);
2684out:
2685 mlog_exit(0);
2686 return 0;
2687}
2688
2689/* Mark the lockres as being dropped. It will no longer be
2690 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07002691 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08002692 * it safe to drop.
2693 *
2694 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2695void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2696{
2697 int status;
2698 struct ocfs2_mask_waiter mw;
2699 unsigned long flags;
2700
2701 ocfs2_init_mask_waiter(&mw);
2702
2703 spin_lock_irqsave(&lockres->l_lock, flags);
2704 lockres->l_flags |= OCFS2_LOCK_FREEING;
2705 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2706 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2707 spin_unlock_irqrestore(&lockres->l_lock, flags);
2708
2709 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2710
2711 status = ocfs2_wait_for_mask(&mw);
2712 if (status)
2713 mlog_errno(status);
2714
2715 spin_lock_irqsave(&lockres->l_lock, flags);
2716 }
2717 spin_unlock_irqrestore(&lockres->l_lock, flags);
2718}
2719
Mark Fashehd680efe2006-09-08 14:14:34 -07002720void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2721 struct ocfs2_lock_res *lockres)
2722{
2723 int ret;
2724
2725 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002726 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002727 if (ret)
2728 mlog_errno(ret);
2729}
2730
Mark Fashehccd979b2005-12-15 14:31:24 -08002731static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2732{
Mark Fashehd680efe2006-09-08 14:14:34 -07002733 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2734 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002735}
2736
Mark Fashehccd979b2005-12-15 14:31:24 -08002737int ocfs2_drop_inode_locks(struct inode *inode)
2738{
2739 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002740
2741 mlog_entry_void();
2742
2743 /* No need to call ocfs2_mark_lockres_freeing here -
2744 * ocfs2_clear_inode has done it for us. */
2745
2746 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002747 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002748 if (err < 0)
2749 mlog_errno(err);
2750
2751 status = err;
2752
2753 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07002754 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002755 if (err < 0)
2756 mlog_errno(err);
2757 if (err < 0 && !status)
2758 status = err;
2759
2760 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002761 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002762 if (err < 0)
2763 mlog_errno(err);
2764 if (err < 0 && !status)
2765 status = err;
2766
2767 mlog_exit(status);
2768 return status;
2769}
2770
2771static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2772 int new_level)
2773{
2774 assert_spin_locked(&lockres->l_lock);
2775
Joel Beckerbd3e7612008-02-01 12:14:57 -08002776 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002777
2778 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002779 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002780 lockres->l_level, new_level);
2781 BUG();
2782 }
2783
2784 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2785 lockres->l_name, new_level, lockres->l_blocking);
2786
2787 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2788 lockres->l_requested = new_level;
2789 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2790}
2791
2792static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2793 struct ocfs2_lock_res *lockres,
2794 int new_level,
2795 int lvb)
2796{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002797 int ret;
2798 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08002799
2800 mlog_entry_void();
2801
2802 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002803 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08002804
Joel Becker4670c462008-02-01 14:39:35 -08002805 ret = ocfs2_dlm_lock(osb->cconn,
Joel Becker7431cd72008-02-01 12:15:37 -08002806 new_level,
2807 &lockres->l_lksb,
2808 dlm_flags,
2809 lockres->l_name,
2810 OCFS2_LOCK_ID_MAX_LEN - 1,
2811 lockres);
2812 if (ret) {
2813 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002814 ocfs2_recover_from_dlm_error(lockres, 1);
2815 goto bail;
2816 }
2817
2818 ret = 0;
2819bail:
2820 mlog_exit(ret);
2821 return ret;
2822}
2823
Joel Becker24ef1812008-01-29 17:37:32 -08002824/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08002825static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2826 struct ocfs2_lock_res *lockres)
2827{
2828 assert_spin_locked(&lockres->l_lock);
2829
2830 mlog_entry_void();
2831 mlog(0, "lock %s\n", lockres->l_name);
2832
2833 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2834 /* If we're already trying to cancel a lock conversion
2835 * then just drop the spinlock and allow the caller to
2836 * requeue this lock. */
2837
2838 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2839 return 0;
2840 }
2841
2842 /* were we in a convert when we got the bast fire? */
2843 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2844 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2845 /* set things up for the unlockast to know to just
2846 * clear out the ast_action and unset busy, etc. */
2847 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2848
2849 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2850 "lock %s, invalid flags: 0x%lx\n",
2851 lockres->l_name, lockres->l_flags);
2852
2853 return 1;
2854}
2855
2856static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2857 struct ocfs2_lock_res *lockres)
2858{
2859 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08002860
2861 mlog_entry_void();
2862 mlog(0, "lock %s\n", lockres->l_name);
2863
Joel Becker4670c462008-02-01 14:39:35 -08002864 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
Joel Becker7431cd72008-02-01 12:15:37 -08002865 DLM_LKF_CANCEL, lockres);
2866 if (ret) {
2867 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002868 ocfs2_recover_from_dlm_error(lockres, 0);
2869 }
2870
Joel Becker24ef1812008-01-29 17:37:32 -08002871 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002872
2873 mlog_exit(ret);
2874 return ret;
2875}
2876
Mark Fashehb5e500e2006-09-13 22:01:16 -07002877static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2878 struct ocfs2_lock_res *lockres,
2879 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002880{
2881 unsigned long flags;
2882 int blocking;
2883 int new_level;
2884 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002885 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002886
2887 mlog_entry_void();
2888
2889 spin_lock_irqsave(&lockres->l_lock, flags);
2890
2891 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2892
2893recheck:
2894 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002895 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002896 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2897 spin_unlock_irqrestore(&lockres->l_lock, flags);
2898 if (ret) {
2899 ret = ocfs2_cancel_convert(osb, lockres);
2900 if (ret < 0)
2901 mlog_errno(ret);
2902 }
2903 goto leave;
2904 }
2905
2906 /* if we're blocking an exclusive and we have *any* holders,
2907 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002908 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002909 && (lockres->l_ex_holders || lockres->l_ro_holders))
2910 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002911
2912 /* If it's a PR we're blocking, then only
2913 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002914 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002915 lockres->l_ex_holders)
2916 goto leave_requeue;
2917
2918 /*
2919 * Can we get a lock in this state if the holder counts are
2920 * zero? The meta data unblock code used to check this.
2921 */
2922 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2923 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2924 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002925
Mark Fasheh16d5b952006-09-13 21:10:12 -07002926 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2927
2928 if (lockres->l_ops->check_downconvert
2929 && !lockres->l_ops->check_downconvert(lockres, new_level))
2930 goto leave_requeue;
2931
Mark Fashehccd979b2005-12-15 14:31:24 -08002932 /* If we get here, then we know that there are no more
2933 * incompatible holders (and anyone asking for an incompatible
2934 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002935 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002936 goto downconvert;
2937
2938 /* Some lockres types want to do a bit of work before
2939 * downconverting a lock. Allow that here. The worker function
2940 * may sleep, so we save off a copy of what we're blocking as
2941 * it may change while we're not holding the spin lock. */
2942 blocking = lockres->l_blocking;
2943 spin_unlock_irqrestore(&lockres->l_lock, flags);
2944
Mark Fashehcc567d82006-09-13 21:52:21 -07002945 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002946
2947 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2948 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002949
2950 spin_lock_irqsave(&lockres->l_lock, flags);
2951 if (blocking != lockres->l_blocking) {
2952 /* If this changed underneath us, then we can't drop
2953 * it just yet. */
2954 goto recheck;
2955 }
2956
2957downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002958 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002959
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002960 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002961 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002962 set_lvb = 1;
2963
2964 /*
2965 * We only set the lvb if the lock has been fully
2966 * refreshed - otherwise we risk setting stale
2967 * data. Otherwise, there's no need to actually clear
2968 * out the lvb here as it's value is still valid.
2969 */
2970 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2971 lockres->l_ops->set_lvb(lockres);
2972 }
2973
Mark Fashehccd979b2005-12-15 14:31:24 -08002974 ocfs2_prepare_downconvert(lockres, new_level);
2975 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002976 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002977leave:
2978 mlog_exit(ret);
2979 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002980
2981leave_requeue:
2982 spin_unlock_irqrestore(&lockres->l_lock, flags);
2983 ctl->requeue = 1;
2984
2985 mlog_exit(0);
2986 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002987}
2988
Mark Fashehd680efe2006-09-08 14:14:34 -07002989static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
2990 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08002991{
2992 struct inode *inode;
2993 struct address_space *mapping;
2994
Mark Fashehccd979b2005-12-15 14:31:24 -08002995 inode = ocfs2_lock_res_inode(lockres);
2996 mapping = inode->i_mapping;
2997
Mark Fasheh1044e402008-02-28 17:16:03 -08002998 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07002999 goto out;
3000
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003001 /*
3002 * We need this before the filemap_fdatawrite() so that it can
3003 * transfer the dirty bit from the PTE to the
3004 * page. Unfortunately this means that even for EX->PR
3005 * downconverts, we'll lose our mappings and have to build
3006 * them up again.
3007 */
3008 unmap_mapping_range(mapping, 0, 0, 0);
3009
Mark Fashehccd979b2005-12-15 14:31:24 -08003010 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003011 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3012 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003013 }
3014 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003015 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003016 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003017 } else {
3018 /* We only need to wait on the I/O if we're not also
3019 * truncating pages because truncate_inode_pages waits
3020 * for us above. We don't truncate pages if we're
3021 * blocking anything < EXMODE because we want to keep
3022 * them around in that case. */
3023 filemap_fdatawait(mapping);
3024 }
3025
Mark Fashehf1f54062007-10-18 15:13:59 -07003026out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003027 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003028}
3029
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003030static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3031 int new_level)
3032{
3033 struct inode *inode = ocfs2_lock_res_inode(lockres);
3034 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3035
Joel Beckerbd3e7612008-02-01 12:14:57 -08003036 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3037 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003038
3039 if (checkpointed)
3040 return 1;
3041
3042 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3043 return 0;
3044}
3045
3046static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3047{
3048 struct inode *inode = ocfs2_lock_res_inode(lockres);
3049
3050 __ocfs2_stuff_meta_lvb(inode);
3051}
3052
Mark Fashehd680efe2006-09-08 14:14:34 -07003053/*
3054 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003055 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003056 * dlmglue API and push these off to the ocfs2_wq in the future.
3057 */
3058static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3059 struct ocfs2_lock_res *lockres)
3060{
3061 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3062 ocfs2_dentry_lock_put(osb, dl);
3063}
3064
3065/*
3066 * d_delete() matching dentries before the lock downconvert.
3067 *
3068 * At this point, any process waiting to destroy the
3069 * dentry_lock due to last ref count is stopped by the
3070 * OCFS2_LOCK_QUEUED flag.
3071 *
3072 * We have two potential problems
3073 *
3074 * 1) If we do the last reference drop on our dentry_lock (via dput)
3075 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3076 * the downconvert to finish. Instead we take an elevated
3077 * reference and push the drop until after we've completed our
3078 * unblock processing.
3079 *
3080 * 2) There might be another process with a final reference,
3081 * waiting on us to finish processing. If this is the case, we
3082 * detect it and exit out - there's no more dentries anyway.
3083 */
3084static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3085 int blocking)
3086{
3087 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3088 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3089 struct dentry *dentry;
3090 unsigned long flags;
3091 int extra_ref = 0;
3092
3093 /*
3094 * This node is blocking another node from getting a read
3095 * lock. This happens when we've renamed within a
3096 * directory. We've forced the other nodes to d_delete(), but
3097 * we never actually dropped our lock because it's still
3098 * valid. The downconvert code will retain a PR for this node,
3099 * so there's no further work to do.
3100 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003101 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003102 return UNBLOCK_CONTINUE;
3103
3104 /*
3105 * Mark this inode as potentially orphaned. The code in
3106 * ocfs2_delete_inode() will figure out whether it actually
3107 * needs to be freed or not.
3108 */
3109 spin_lock(&oi->ip_lock);
3110 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3111 spin_unlock(&oi->ip_lock);
3112
3113 /*
3114 * Yuck. We need to make sure however that the check of
3115 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3116 * respect to a reference decrement or the setting of that
3117 * flag.
3118 */
3119 spin_lock_irqsave(&lockres->l_lock, flags);
3120 spin_lock(&dentry_attach_lock);
3121 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3122 && dl->dl_count) {
3123 dl->dl_count++;
3124 extra_ref = 1;
3125 }
3126 spin_unlock(&dentry_attach_lock);
3127 spin_unlock_irqrestore(&lockres->l_lock, flags);
3128
3129 mlog(0, "extra_ref = %d\n", extra_ref);
3130
3131 /*
3132 * We have a process waiting on us in ocfs2_dentry_iput(),
3133 * which means we can't have any more outstanding
3134 * aliases. There's no need to do any more work.
3135 */
3136 if (!extra_ref)
3137 return UNBLOCK_CONTINUE;
3138
3139 spin_lock(&dentry_attach_lock);
3140 while (1) {
3141 dentry = ocfs2_find_local_alias(dl->dl_inode,
3142 dl->dl_parent_blkno, 1);
3143 if (!dentry)
3144 break;
3145 spin_unlock(&dentry_attach_lock);
3146
3147 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3148 dentry->d_name.name);
3149
3150 /*
3151 * The following dcache calls may do an
3152 * iput(). Normally we don't want that from the
3153 * downconverting thread, but in this case it's ok
3154 * because the requesting node already has an
3155 * exclusive lock on the inode, so it can't be queued
3156 * for a downconvert.
3157 */
3158 d_delete(dentry);
3159 dput(dentry);
3160
3161 spin_lock(&dentry_attach_lock);
3162 }
3163 spin_unlock(&dentry_attach_lock);
3164
3165 /*
3166 * If we are the last holder of this dentry lock, there is no
3167 * reason to downconvert so skip straight to the unlock.
3168 */
3169 if (dl->dl_count == 1)
3170 return UNBLOCK_STOP_POST;
3171
3172 return UNBLOCK_CONTINUE_POST;
3173}
3174
Joel Becker4670c462008-02-01 14:39:35 -08003175/*
3176 * This is the filesystem locking protocol. It provides the lock handling
3177 * hooks for the underlying DLM. It has a maximum version number.
3178 * The version number allows interoperability with systems running at
3179 * the same major number and an equal or smaller minor number.
3180 *
3181 * Whenever the filesystem does new things with locks (adds or removes a
3182 * lock, orders them differently, does different things underneath a lock),
3183 * the version must be changed. The protocol is negotiated when joining
3184 * the dlm domain. A node may join the domain if its major version is
3185 * identical to all other nodes and its minor version is greater than
3186 * or equal to all other nodes. When its minor version is greater than
3187 * the other nodes, it will run at the minor version specified by the
3188 * other nodes.
3189 *
3190 * If a locking change is made that will not be compatible with older
3191 * versions, the major number must be increased and the minor version set
3192 * to zero. If a change merely adds a behavior that can be disabled when
3193 * speaking to older versions, the minor version must be increased. If a
3194 * change adds a fully backwards compatible change (eg, LVB changes that
3195 * are just ignored by older versions), the version does not need to be
3196 * updated.
3197 */
Joel Becker24ef1812008-01-29 17:37:32 -08003198static struct ocfs2_locking_protocol lproto = {
Joel Becker4670c462008-02-01 14:39:35 -08003199 .lp_max_version = {
3200 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3201 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3202 },
Joel Becker24ef1812008-01-29 17:37:32 -08003203 .lp_lock_ast = ocfs2_locking_ast,
3204 .lp_blocking_ast = ocfs2_blocking_ast,
3205 .lp_unlock_ast = ocfs2_unlock_ast,
3206};
3207
3208/* This interface isn't the final one, hence the less-than-perfect names */
3209void dlmglue_init_stack(void)
3210{
3211 o2cb_get_stack(&lproto);
3212}
3213
3214void dlmglue_exit_stack(void)
3215{
3216 o2cb_put_stack();
3217}
3218
Adrian Bunk00600052008-01-29 00:11:41 +02003219static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3220 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003221{
3222 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003223 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003224 unsigned long flags;
3225
3226 /* Our reference to the lockres in this function can be
3227 * considered valid until we remove the OCFS2_LOCK_QUEUED
3228 * flag. */
3229
3230 mlog_entry_void();
3231
3232 BUG_ON(!lockres);
3233 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003234
3235 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3236
3237 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003238 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003239 * still be marked with OCFS2_LOCK_FREEING after this check,
3240 * but short circuiting here will still save us some
3241 * performance. */
3242 spin_lock_irqsave(&lockres->l_lock, flags);
3243 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3244 goto unqueue;
3245 spin_unlock_irqrestore(&lockres->l_lock, flags);
3246
Mark Fashehb5e500e2006-09-13 22:01:16 -07003247 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003248 if (status < 0)
3249 mlog_errno(status);
3250
3251 spin_lock_irqsave(&lockres->l_lock, flags);
3252unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003253 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003254 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3255 } else
3256 ocfs2_schedule_blocked_lock(osb, lockres);
3257
3258 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003259 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003260 spin_unlock_irqrestore(&lockres->l_lock, flags);
3261
Mark Fashehd680efe2006-09-08 14:14:34 -07003262 if (ctl.unblock_action != UNBLOCK_CONTINUE
3263 && lockres->l_ops->post_unlock)
3264 lockres->l_ops->post_unlock(osb, lockres);
3265
Mark Fashehccd979b2005-12-15 14:31:24 -08003266 mlog_exit_void();
3267}
3268
3269static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3270 struct ocfs2_lock_res *lockres)
3271{
3272 mlog_entry_void();
3273
3274 assert_spin_locked(&lockres->l_lock);
3275
3276 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3277 /* Do not schedule a lock for downconvert when it's on
3278 * the way to destruction - any nodes wanting access
3279 * to the resource will get it soon. */
3280 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3281 lockres->l_name, lockres->l_flags);
3282 return;
3283 }
3284
3285 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3286
Mark Fasheh34d024f2007-09-24 15:56:19 -07003287 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003288 if (list_empty(&lockres->l_blocked_list)) {
3289 list_add_tail(&lockres->l_blocked_list,
3290 &osb->blocked_lock_list);
3291 osb->blocked_lock_count++;
3292 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003293 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003294
3295 mlog_exit_void();
3296}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003297
3298static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3299{
3300 unsigned long processed;
3301 struct ocfs2_lock_res *lockres;
3302
3303 mlog_entry_void();
3304
3305 spin_lock(&osb->dc_task_lock);
3306 /* grab this early so we know to try again if a state change and
3307 * wake happens part-way through our work */
3308 osb->dc_work_sequence = osb->dc_wake_sequence;
3309
3310 processed = osb->blocked_lock_count;
3311 while (processed) {
3312 BUG_ON(list_empty(&osb->blocked_lock_list));
3313
3314 lockres = list_entry(osb->blocked_lock_list.next,
3315 struct ocfs2_lock_res, l_blocked_list);
3316 list_del_init(&lockres->l_blocked_list);
3317 osb->blocked_lock_count--;
3318 spin_unlock(&osb->dc_task_lock);
3319
3320 BUG_ON(!processed);
3321 processed--;
3322
3323 ocfs2_process_blocked_lock(osb, lockres);
3324
3325 spin_lock(&osb->dc_task_lock);
3326 }
3327 spin_unlock(&osb->dc_task_lock);
3328
3329 mlog_exit_void();
3330}
3331
3332static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3333{
3334 int empty = 0;
3335
3336 spin_lock(&osb->dc_task_lock);
3337 if (list_empty(&osb->blocked_lock_list))
3338 empty = 1;
3339
3340 spin_unlock(&osb->dc_task_lock);
3341 return empty;
3342}
3343
3344static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3345{
3346 int should_wake = 0;
3347
3348 spin_lock(&osb->dc_task_lock);
3349 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3350 should_wake = 1;
3351 spin_unlock(&osb->dc_task_lock);
3352
3353 return should_wake;
3354}
3355
Adrian Bunk200bfae2008-02-17 10:20:38 +02003356static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003357{
3358 int status = 0;
3359 struct ocfs2_super *osb = arg;
3360
3361 /* only quit once we've been asked to stop and there is no more
3362 * work available */
3363 while (!(kthread_should_stop() &&
3364 ocfs2_downconvert_thread_lists_empty(osb))) {
3365
3366 wait_event_interruptible(osb->dc_event,
3367 ocfs2_downconvert_thread_should_wake(osb) ||
3368 kthread_should_stop());
3369
3370 mlog(0, "downconvert_thread: awoken\n");
3371
3372 ocfs2_downconvert_thread_do_work(osb);
3373 }
3374
3375 osb->dc_task = NULL;
3376 return status;
3377}
3378
3379void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3380{
3381 spin_lock(&osb->dc_task_lock);
3382 /* make sure the voting thread gets a swipe at whatever changes
3383 * the caller may have made to the voting state */
3384 osb->dc_wake_sequence++;
3385 spin_unlock(&osb->dc_task_lock);
3386 wake_up(&osb->dc_event);
3387}