blob: b8ac903ef8ef429fae515580e7e52daa010e3058 [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/crc32.h>
31#include <linux/kthread.h>
32#include <linux/pagemap.h>
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
35
36#include <cluster/heartbeat.h>
37#include <cluster/nodemanager.h>
38#include <cluster/tcp.h>
39
Mark Fashehccd979b2005-12-15 14:31:24 -080040#define MLOG_MASK_PREFIX ML_DLM_GLUE
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080044#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070047#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080048#include "dlmglue.h"
49#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080050#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080051#include "heartbeat.h"
52#include "inode.h"
53#include "journal.h"
Joel Becker24ef1812008-01-29 17:37:32 -080054#include "stackglue.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080055#include "slot_map.h"
56#include "super.h"
57#include "uptodate.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080058
59#include "buffer_head_io.h"
60
61struct ocfs2_mask_waiter {
62 struct list_head mw_item;
63 int mw_status;
64 struct completion mw_complete;
65 unsigned long mw_mask;
66 unsigned long mw_goal;
67};
68
Mark Fasheh54a7e752006-09-12 21:49:13 -070069static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
70static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080071static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080072
Mark Fashehd680efe2006-09-08 14:14:34 -070073/*
Mark Fashehcc567d82006-09-13 21:52:21 -070074 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070075 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070076 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070077 * and ocfs2_process_blocked_lock()
78 *
79 */
80enum ocfs2_unblock_action {
81 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
82 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
83 * ->post_unlock callback */
84 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
85 * ->post_unlock() callback. */
86};
87
88struct ocfs2_unblock_ctl {
89 int requeue;
90 enum ocfs2_unblock_action unblock_action;
91};
92
Mark Fasheh810d5ae2006-09-13 21:39:52 -070093static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
94 int new_level);
95static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
96
Mark Fashehcc567d82006-09-13 21:52:21 -070097static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
98 int blocking);
99
Mark Fashehcc567d82006-09-13 21:52:21 -0700100static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
101 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700102
103static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
104 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800105
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700106
107#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
108
109/* This aids in debugging situations where a bad LVB might be involved. */
110static void ocfs2_dump_meta_lvb_info(u64 level,
111 const char *function,
112 unsigned int line,
113 struct ocfs2_lock_res *lockres)
114{
115 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
116
117 mlog(level, "LVB information for %s (called from %s:%u):\n",
118 lockres->l_name, function, line);
119 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
120 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
121 be32_to_cpu(lvb->lvb_igeneration));
122 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
123 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
124 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
125 be16_to_cpu(lvb->lvb_imode));
126 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
127 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
128 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
129 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
130 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
131 be32_to_cpu(lvb->lvb_iattr));
132}
133
134
Mark Fashehf625c972006-09-12 21:24:53 -0700135/*
136 * OCFS2 Lock Resource Operations
137 *
138 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700139 *
140 * The most basic of lock types can point ->l_priv to their respective
141 * struct ocfs2_super and allow the default actions to manage things.
142 *
143 * Right now, each lock type also needs to implement an init function,
144 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
145 * should be called when the lock is no longer needed (i.e., object
146 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700147 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800148struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700149 /*
150 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
151 * this callback if ->l_priv is not an ocfs2_super pointer
152 */
153 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700154
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700155 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700156 * Optionally called in the downconvert thread after a
157 * successful downconvert. The lockres will not be referenced
158 * after this callback is called, so it is safe to free
159 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700160 *
161 * The exact semantics of when this is called are controlled
162 * by ->downconvert_worker()
163 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700164 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700165
166 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700167 * Allow a lock type to add checks to determine whether it is
168 * safe to downconvert a lock. Return 0 to re-queue the
169 * downconvert at a later time, nonzero to continue.
170 *
171 * For most locks, the default checks that there are no
172 * incompatible holders are sufficient.
173 *
174 * Called with the lockres spinlock held.
175 */
176 int (*check_downconvert)(struct ocfs2_lock_res *, int);
177
178 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700179 * Allows a lock type to populate the lock value block. This
180 * is called on downconvert, and when we drop a lock.
181 *
182 * Locks that want to use this should set LOCK_TYPE_USES_LVB
183 * in the flags field.
184 *
185 * Called with the lockres spinlock held.
186 */
187 void (*set_lvb)(struct ocfs2_lock_res *);
188
189 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700190 * Called from the downconvert thread when it is determined
191 * that a lock will be downconverted. This is called without
192 * any locks held so the function can do work that might
193 * schedule (syncing out data, etc).
194 *
195 * This should return any one of the ocfs2_unblock_action
196 * values, depending on what it wants the thread to do.
197 */
198 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
199
200 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700201 * LOCK_TYPE_* flags which describe the specific requirements
202 * of a lock type. Descriptions of each individual flag follow.
203 */
204 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800205};
206
Mark Fashehf625c972006-09-12 21:24:53 -0700207/*
208 * Some locks want to "refresh" potentially stale data when a
209 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
210 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
211 * individual lockres l_flags member from the ast function. It is
212 * expected that the locking wrapper will clear the
213 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
214 */
215#define LOCK_TYPE_REQUIRES_REFRESH 0x1
216
Mark Fashehb80fc012006-09-12 22:08:14 -0700217/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700218 * Indicate that a lock type makes use of the lock value block. The
219 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700220 */
221#define LOCK_TYPE_USES_LVB 0x2
222
Mark Fashehccd979b2005-12-15 14:31:24 -0800223static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700224 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700225 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800226};
227
Mark Fashehe63aecb62007-10-18 15:30:42 -0700228static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700229 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700230 .check_downconvert = ocfs2_check_meta_downconvert,
231 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700232 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700233 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800234};
235
Mark Fashehccd979b2005-12-15 14:31:24 -0800236static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700237 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800238};
239
240static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700241 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800242};
243
Mark Fashehd680efe2006-09-08 14:14:34 -0700244static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700245 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700246 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700247 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700248 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700249};
250
Tiger Yang50008632007-03-20 16:01:38 -0700251static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
252 .get_osb = ocfs2_get_inode_osb,
253 .flags = 0,
254};
255
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800256static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
257 .get_osb = ocfs2_get_file_osb,
258 .flags = 0,
259};
260
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800261/*
262 * This is the filesystem locking protocol version.
263 *
264 * Whenever the filesystem does new things with locks (adds or removes a
265 * lock, orders them differently, does different things underneath a lock),
266 * the version must be changed. The protocol is negotiated when joining
267 * the dlm domain. A node may join the domain if its major version is
268 * identical to all other nodes and its minor version is greater than
269 * or equal to all other nodes. When its minor version is greater than
270 * the other nodes, it will run at the minor version specified by the
271 * other nodes.
272 *
273 * If a locking change is made that will not be compatible with older
274 * versions, the major number must be increased and the minor version set
275 * to zero. If a change merely adds a behavior that can be disabled when
276 * speaking to older versions, the minor version must be increased. If a
277 * change adds a fully backwards compatible change (eg, LVB changes that
278 * are just ignored by older versions), the version does not need to be
279 * updated.
280 */
281const struct dlm_protocol_version ocfs2_locking_protocol = {
282 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
283 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
284};
285
Mark Fashehccd979b2005-12-15 14:31:24 -0800286static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
287{
288 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700289 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
290 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800291}
292
Mark Fashehccd979b2005-12-15 14:31:24 -0800293static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
294{
295 BUG_ON(!ocfs2_is_inode_lock(lockres));
296
297 return (struct inode *) lockres->l_priv;
298}
299
Mark Fashehd680efe2006-09-08 14:14:34 -0700300static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
301{
302 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
303
304 return (struct ocfs2_dentry_lock *)lockres->l_priv;
305}
306
Mark Fasheh54a7e752006-09-12 21:49:13 -0700307static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
308{
309 if (lockres->l_ops->get_osb)
310 return lockres->l_ops->get_osb(lockres);
311
312 return (struct ocfs2_super *)lockres->l_priv;
313}
314
Mark Fashehccd979b2005-12-15 14:31:24 -0800315static int ocfs2_lock_create(struct ocfs2_super *osb,
316 struct ocfs2_lock_res *lockres,
317 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800318 u32 dlm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800319static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
320 int wanted);
321static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
322 struct ocfs2_lock_res *lockres,
323 int level);
324static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
325static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
326static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
327static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
328static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
329 struct ocfs2_lock_res *lockres);
330static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
331 int convert);
332#define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
333 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
334 "resource %s: %s\n", dlm_errname(_stat), _func, \
335 _lockres->l_name, dlm_errmsg(_stat)); \
336} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700337static int ocfs2_downconvert_thread(void *arg);
338static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
339 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700340static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800341 struct buffer_head **bh);
342static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
343static inline int ocfs2_highest_compat_lock_level(int level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800344static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
345 int new_level);
346static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
347 struct ocfs2_lock_res *lockres,
348 int new_level,
349 int lvb);
350static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
351 struct ocfs2_lock_res *lockres);
352static int ocfs2_cancel_convert(struct ocfs2_super *osb,
353 struct ocfs2_lock_res *lockres);
354
Mark Fashehccd979b2005-12-15 14:31:24 -0800355
Mark Fashehccd979b2005-12-15 14:31:24 -0800356static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
357 u64 blkno,
358 u32 generation,
359 char *name)
360{
361 int len;
362
363 mlog_entry_void();
364
365 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
366
Mark Fashehb06970532006-03-03 10:24:33 -0800367 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
368 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
369 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800370
371 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
372
373 mlog(0, "built lock resource with name: %s\n", name);
374
375 mlog_exit_void();
376}
377
Ingo Molnar34af9462006-06-27 02:53:55 -0700378static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800379
380static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
381 struct ocfs2_dlm_debug *dlm_debug)
382{
383 mlog(0, "Add tracking for lockres %s\n", res->l_name);
384
385 spin_lock(&ocfs2_dlm_tracking_lock);
386 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
387 spin_unlock(&ocfs2_dlm_tracking_lock);
388}
389
390static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
391{
392 spin_lock(&ocfs2_dlm_tracking_lock);
393 if (!list_empty(&res->l_debug_list))
394 list_del_init(&res->l_debug_list);
395 spin_unlock(&ocfs2_dlm_tracking_lock);
396}
397
398static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
399 struct ocfs2_lock_res *res,
400 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 struct ocfs2_lock_res_ops *ops,
402 void *priv)
403{
Mark Fashehccd979b2005-12-15 14:31:24 -0800404 res->l_type = type;
405 res->l_ops = ops;
406 res->l_priv = priv;
407
Joel Beckerbd3e7612008-02-01 12:14:57 -0800408 res->l_level = DLM_LOCK_IV;
409 res->l_requested = DLM_LOCK_IV;
410 res->l_blocking = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -0800411 res->l_action = OCFS2_AST_INVALID;
412 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
413
414 res->l_flags = OCFS2_LOCK_INITIALIZED;
415
416 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
417}
418
419void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
420{
421 /* This also clears out the lock status block */
422 memset(res, 0, sizeof(struct ocfs2_lock_res));
423 spin_lock_init(&res->l_lock);
424 init_waitqueue_head(&res->l_event);
425 INIT_LIST_HEAD(&res->l_blocked_list);
426 INIT_LIST_HEAD(&res->l_mask_waiters);
427}
428
429void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
430 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700431 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800432 struct inode *inode)
433{
434 struct ocfs2_lock_res_ops *ops;
435
436 switch(type) {
437 case OCFS2_LOCK_TYPE_RW:
438 ops = &ocfs2_inode_rw_lops;
439 break;
440 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700441 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 break;
Tiger Yang50008632007-03-20 16:01:38 -0700443 case OCFS2_LOCK_TYPE_OPEN:
444 ops = &ocfs2_inode_open_lops;
445 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800446 default:
447 mlog_bug_on_msg(1, "type: %d\n", type);
448 ops = NULL; /* thanks, gcc */
449 break;
450 };
451
Mark Fashehd680efe2006-09-08 14:14:34 -0700452 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700453 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700454 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
455}
456
Mark Fasheh54a7e752006-09-12 21:49:13 -0700457static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
458{
459 struct inode *inode = ocfs2_lock_res_inode(lockres);
460
461 return OCFS2_SB(inode->i_sb);
462}
463
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800464static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
465{
466 struct ocfs2_file_private *fp = lockres->l_priv;
467
468 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
469}
470
Mark Fashehd680efe2006-09-08 14:14:34 -0700471static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
472{
473 __be64 inode_blkno_be;
474
475 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
476 sizeof(__be64));
477
478 return be64_to_cpu(inode_blkno_be);
479}
480
Mark Fasheh54a7e752006-09-12 21:49:13 -0700481static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
482{
483 struct ocfs2_dentry_lock *dl = lockres->l_priv;
484
485 return OCFS2_SB(dl->dl_inode->i_sb);
486}
487
Mark Fashehd680efe2006-09-08 14:14:34 -0700488void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
489 u64 parent, struct inode *inode)
490{
491 int len;
492 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
493 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
494 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
495
496 ocfs2_lock_res_init_once(lockres);
497
498 /*
499 * Unfortunately, the standard lock naming scheme won't work
500 * here because we have two 16 byte values to use. Instead,
501 * we'll stuff the inode number as a binary value. We still
502 * want error prints to show something without garbling the
503 * display, so drop a null byte in there before the inode
504 * number. A future version of OCFS2 will likely use all
505 * binary lock names. The stringified names have been a
506 * tremendous aid in debugging, but now that the debugfs
507 * interface exists, we can mangle things there if need be.
508 *
509 * NOTE: We also drop the standard "pad" value (the total lock
510 * name size stays the same though - the last part is all
511 * zeros due to the memset in ocfs2_lock_res_init_once()
512 */
513 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
514 "%c%016llx",
515 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
516 (long long)parent);
517
518 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
519
520 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
521 sizeof(__be64));
522
523 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
524 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
525 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526}
527
528static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
529 struct ocfs2_super *osb)
530{
531 /* Superblock lockres doesn't come from a slab so we call init
532 * once on it manually. */
533 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700534 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
535 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800536 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 &ocfs2_super_lops, osb);
538}
539
540static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
541 struct ocfs2_super *osb)
542{
543 /* Rename lockres doesn't come from a slab so we call init
544 * once on it manually. */
545 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700546 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
547 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800548 &ocfs2_rename_lops, osb);
549}
550
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800551void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
552 struct ocfs2_file_private *fp)
553{
554 struct inode *inode = fp->fp_file->f_mapping->host;
555 struct ocfs2_inode_info *oi = OCFS2_I(inode);
556
557 ocfs2_lock_res_init_once(lockres);
558 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
559 inode->i_generation, lockres->l_name);
560 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
561 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
562 fp);
563 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
564}
565
Mark Fashehccd979b2005-12-15 14:31:24 -0800566void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
567{
568 mlog_entry_void();
569
570 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
571 return;
572
573 ocfs2_remove_lockres_tracking(res);
574
575 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
576 "Lockres %s is on the blocked list\n",
577 res->l_name);
578 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
579 "Lockres %s has mask waiters pending\n",
580 res->l_name);
581 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
582 "Lockres %s is locked\n",
583 res->l_name);
584 mlog_bug_on_msg(res->l_ro_holders,
585 "Lockres %s has %u ro holders\n",
586 res->l_name, res->l_ro_holders);
587 mlog_bug_on_msg(res->l_ex_holders,
588 "Lockres %s has %u ex holders\n",
589 res->l_name, res->l_ex_holders);
590
591 /* Need to clear out the lock status block for the dlm */
592 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
593
594 res->l_flags = 0UL;
595 mlog_exit_void();
596}
597
598static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
599 int level)
600{
601 mlog_entry_void();
602
603 BUG_ON(!lockres);
604
605 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800606 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800607 lockres->l_ex_holders++;
608 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800609 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 lockres->l_ro_holders++;
611 break;
612 default:
613 BUG();
614 }
615
616 mlog_exit_void();
617}
618
619static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
620 int level)
621{
622 mlog_entry_void();
623
624 BUG_ON(!lockres);
625
626 switch(level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800627 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -0800628 BUG_ON(!lockres->l_ex_holders);
629 lockres->l_ex_holders--;
630 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -0800631 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -0800632 BUG_ON(!lockres->l_ro_holders);
633 lockres->l_ro_holders--;
634 break;
635 default:
636 BUG();
637 }
638 mlog_exit_void();
639}
640
641/* WARNING: This function lives in a world where the only three lock
642 * levels are EX, PR, and NL. It *will* have to be adjusted when more
643 * lock types are added. */
644static inline int ocfs2_highest_compat_lock_level(int level)
645{
Joel Beckerbd3e7612008-02-01 12:14:57 -0800646 int new_level = DLM_LOCK_EX;
Mark Fashehccd979b2005-12-15 14:31:24 -0800647
Joel Beckerbd3e7612008-02-01 12:14:57 -0800648 if (level == DLM_LOCK_EX)
649 new_level = DLM_LOCK_NL;
650 else if (level == DLM_LOCK_PR)
651 new_level = DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -0800652 return new_level;
653}
654
655static void lockres_set_flags(struct ocfs2_lock_res *lockres,
656 unsigned long newflags)
657{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200658 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800659
660 assert_spin_locked(&lockres->l_lock);
661
662 lockres->l_flags = newflags;
663
Christoph Hellwig800deef2007-05-17 16:03:13 +0200664 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
666 continue;
667
668 list_del_init(&mw->mw_item);
669 mw->mw_status = 0;
670 complete(&mw->mw_complete);
671 }
672}
673static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
674{
675 lockres_set_flags(lockres, lockres->l_flags | or);
676}
677static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
678 unsigned long clear)
679{
680 lockres_set_flags(lockres, lockres->l_flags & ~clear);
681}
682
683static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
684{
685 mlog_entry_void();
686
687 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
688 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
689 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
Joel Beckerbd3e7612008-02-01 12:14:57 -0800690 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800691
692 lockres->l_level = lockres->l_requested;
693 if (lockres->l_level <=
694 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
Joel Beckerbd3e7612008-02-01 12:14:57 -0800695 lockres->l_blocking = DLM_LOCK_NL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800696 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
697 }
698 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
699
700 mlog_exit_void();
701}
702
703static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
704{
705 mlog_entry_void();
706
707 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
708 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
709
710 /* Convert from RO to EX doesn't really need anything as our
711 * information is already up to data. Convert from NL to
712 * *anything* however should mark ourselves as needing an
713 * update */
Joel Beckerbd3e7612008-02-01 12:14:57 -0800714 if (lockres->l_level == DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700715 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800716 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
717
718 lockres->l_level = lockres->l_requested;
719 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
720
721 mlog_exit_void();
722}
723
724static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
725{
726 mlog_entry_void();
727
Roel Kluin3cf0c502007-10-27 00:20:36 +0200728 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800729 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
730
Joel Beckerbd3e7612008-02-01 12:14:57 -0800731 if (lockres->l_requested > DLM_LOCK_NL &&
Mark Fashehf625c972006-09-12 21:24:53 -0700732 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
733 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
735
736 lockres->l_level = lockres->l_requested;
737 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
738 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
739
740 mlog_exit_void();
741}
742
Mark Fashehccd979b2005-12-15 14:31:24 -0800743static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
744 int level)
745{
746 int needs_downconvert = 0;
747 mlog_entry_void();
748
749 assert_spin_locked(&lockres->l_lock);
750
751 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
752
753 if (level > lockres->l_blocking) {
754 /* only schedule a downconvert if we haven't already scheduled
755 * one that goes low enough to satisfy the level we're
756 * blocking. this also catches the case where we get
757 * duplicate BASTs */
758 if (ocfs2_highest_compat_lock_level(level) <
759 ocfs2_highest_compat_lock_level(lockres->l_blocking))
760 needs_downconvert = 1;
761
762 lockres->l_blocking = level;
763 }
764
765 mlog_exit(needs_downconvert);
766 return needs_downconvert;
767}
768
Mark Fashehaa2623a2006-09-12 21:58:23 -0700769static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800770{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700771 struct ocfs2_lock_res *lockres = opaque;
772 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 int needs_downconvert;
774 unsigned long flags;
775
Joel Beckerbd3e7612008-02-01 12:14:57 -0800776 BUG_ON(level <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777
Mark Fashehaa2623a2006-09-12 21:58:23 -0700778 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
779 lockres->l_name, level, lockres->l_level,
780 ocfs2_lock_type_string(lockres->l_type));
781
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800782 /*
783 * We can skip the bast for locks which don't enable caching -
784 * they'll be dropped at the earliest possible time anyway.
785 */
786 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
787 return;
788
Mark Fashehccd979b2005-12-15 14:31:24 -0800789 spin_lock_irqsave(&lockres->l_lock, flags);
790 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
791 if (needs_downconvert)
792 ocfs2_schedule_blocked_lock(osb, lockres);
793 spin_unlock_irqrestore(&lockres->l_lock, flags);
794
Mark Fashehd680efe2006-09-08 14:14:34 -0700795 wake_up(&lockres->l_event);
796
Mark Fasheh34d024f2007-09-24 15:56:19 -0700797 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800798}
799
Mark Fashehe92d57d2006-09-12 21:34:35 -0700800static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800801{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700802 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800803 struct dlm_lockstatus *lksb = &lockres->l_lksb;
804 unsigned long flags;
805
806 spin_lock_irqsave(&lockres->l_lock, flags);
807
808 if (lksb->status != DLM_NORMAL) {
809 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
810 lockres->l_name, lksb->status);
811 spin_unlock_irqrestore(&lockres->l_lock, flags);
812 return;
813 }
814
815 switch(lockres->l_action) {
816 case OCFS2_AST_ATTACH:
817 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700818 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800819 break;
820 case OCFS2_AST_CONVERT:
821 ocfs2_generic_handle_convert_action(lockres);
822 break;
823 case OCFS2_AST_DOWNCONVERT:
824 ocfs2_generic_handle_downconvert_action(lockres);
825 break;
826 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700827 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
828 "lockres flags = 0x%lx, unlock action: %u\n",
829 lockres->l_name, lockres->l_action, lockres->l_flags,
830 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -0800831 BUG();
832 }
833
Mark Fashehccd979b2005-12-15 14:31:24 -0800834 /* set it to something invalid so if we get called again we
835 * can catch it. */
836 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800837
838 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700839 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800840}
841
Mark Fashehccd979b2005-12-15 14:31:24 -0800842static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
843 int convert)
844{
845 unsigned long flags;
846
847 mlog_entry_void();
848 spin_lock_irqsave(&lockres->l_lock, flags);
849 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
850 if (convert)
851 lockres->l_action = OCFS2_AST_INVALID;
852 else
853 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
854 spin_unlock_irqrestore(&lockres->l_lock, flags);
855
856 wake_up(&lockres->l_event);
857 mlog_exit_void();
858}
859
860/* Note: If we detect another process working on the lock (i.e.,
861 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
862 * to do the right thing in that case.
863 */
864static int ocfs2_lock_create(struct ocfs2_super *osb,
865 struct ocfs2_lock_res *lockres,
866 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -0800867 u32 dlm_flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800868{
869 int ret = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800870 enum dlm_status status = DLM_NORMAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800871 unsigned long flags;
872
873 mlog_entry_void();
874
Joel Beckerbd3e7612008-02-01 12:14:57 -0800875 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
Mark Fashehccd979b2005-12-15 14:31:24 -0800876 dlm_flags);
877
878 spin_lock_irqsave(&lockres->l_lock, flags);
879 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
880 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
881 spin_unlock_irqrestore(&lockres->l_lock, flags);
882 goto bail;
883 }
884
885 lockres->l_action = OCFS2_AST_ATTACH;
886 lockres->l_requested = level;
887 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
888 spin_unlock_irqrestore(&lockres->l_lock, flags);
889
Joel Becker24ef1812008-01-29 17:37:32 -0800890 status = ocfs2_dlm_lock(osb->dlm,
891 level,
892 &lockres->l_lksb,
893 dlm_flags,
894 lockres->l_name,
895 OCFS2_LOCK_ID_MAX_LEN - 1,
896 lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800897 if (status != DLM_NORMAL) {
Joel Becker24ef1812008-01-29 17:37:32 -0800898 ocfs2_log_dlm_error("ocfs2_dlm_lock", status, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800899 ret = -EINVAL;
900 ocfs2_recover_from_dlm_error(lockres, 1);
901 }
902
Joel Becker24ef1812008-01-29 17:37:32 -0800903 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
904 lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800905
906bail:
907 mlog_exit(ret);
908 return ret;
909}
910
911static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
912 int flag)
913{
914 unsigned long flags;
915 int ret;
916
917 spin_lock_irqsave(&lockres->l_lock, flags);
918 ret = lockres->l_flags & flag;
919 spin_unlock_irqrestore(&lockres->l_lock, flags);
920
921 return ret;
922}
923
924static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
925
926{
927 wait_event(lockres->l_event,
928 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
929}
930
931static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
932
933{
934 wait_event(lockres->l_event,
935 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
936}
937
938/* predict what lock level we'll be dropping down to on behalf
939 * of another node, and return true if the currently wanted
940 * level will be compatible with it. */
941static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
942 int wanted)
943{
944 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
945
946 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
947}
948
949static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
950{
951 INIT_LIST_HEAD(&mw->mw_item);
952 init_completion(&mw->mw_complete);
953}
954
955static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
956{
957 wait_for_completion(&mw->mw_complete);
958 /* Re-arm the completion in case we want to wait on it again */
959 INIT_COMPLETION(mw->mw_complete);
960 return mw->mw_status;
961}
962
963static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
964 struct ocfs2_mask_waiter *mw,
965 unsigned long mask,
966 unsigned long goal)
967{
968 BUG_ON(!list_empty(&mw->mw_item));
969
970 assert_spin_locked(&lockres->l_lock);
971
972 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
973 mw->mw_mask = mask;
974 mw->mw_goal = goal;
975}
976
977/* returns 0 if the mw that was removed was already satisfied, -EBUSY
978 * if the mask still hadn't reached its goal */
979static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
980 struct ocfs2_mask_waiter *mw)
981{
982 unsigned long flags;
983 int ret = 0;
984
985 spin_lock_irqsave(&lockres->l_lock, flags);
986 if (!list_empty(&mw->mw_item)) {
987 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
988 ret = -EBUSY;
989
990 list_del_init(&mw->mw_item);
991 init_completion(&mw->mw_complete);
992 }
993 spin_unlock_irqrestore(&lockres->l_lock, flags);
994
995 return ret;
996
997}
998
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800999static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1000 struct ocfs2_lock_res *lockres)
1001{
1002 int ret;
1003
1004 ret = wait_for_completion_interruptible(&mw->mw_complete);
1005 if (ret)
1006 lockres_remove_mask_waiter(lockres, mw);
1007 else
1008 ret = mw->mw_status;
1009 /* Re-arm the completion in case we want to wait on it again */
1010 INIT_COMPLETION(mw->mw_complete);
1011 return ret;
1012}
1013
Mark Fashehccd979b2005-12-15 14:31:24 -08001014static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1015 struct ocfs2_lock_res *lockres,
1016 int level,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001017 u32 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001018 int arg_flags)
1019{
1020 struct ocfs2_mask_waiter mw;
1021 enum dlm_status status;
1022 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1023 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1024 unsigned long flags;
1025
1026 mlog_entry_void();
1027
1028 ocfs2_init_mask_waiter(&mw);
1029
Mark Fashehb80fc012006-09-12 22:08:14 -07001030 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001031 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07001032
Mark Fashehccd979b2005-12-15 14:31:24 -08001033again:
1034 wait = 0;
1035
1036 if (catch_signals && signal_pending(current)) {
1037 ret = -ERESTARTSYS;
1038 goto out;
1039 }
1040
1041 spin_lock_irqsave(&lockres->l_lock, flags);
1042
1043 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1044 "Cluster lock called on freeing lockres %s! flags "
1045 "0x%lx\n", lockres->l_name, lockres->l_flags);
1046
1047 /* We only compare against the currently granted level
1048 * here. If the lock is blocked waiting on a downconvert,
1049 * we'll get caught below. */
1050 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1051 level > lockres->l_level) {
1052 /* is someone sitting in dlm_lock? If so, wait on
1053 * them. */
1054 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1055 wait = 1;
1056 goto unlock;
1057 }
1058
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1060 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1061 /* is the lock is currently blocked on behalf of
1062 * another node */
1063 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1064 wait = 1;
1065 goto unlock;
1066 }
1067
1068 if (level > lockres->l_level) {
1069 if (lockres->l_action != OCFS2_AST_INVALID)
1070 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1071 lockres->l_name, lockres->l_action);
1072
Mark Fasheh019d1b22007-10-05 12:09:05 -07001073 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1074 lockres->l_action = OCFS2_AST_ATTACH;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001075 lkm_flags &= ~DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001076 } else {
1077 lockres->l_action = OCFS2_AST_CONVERT;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001078 lkm_flags |= DLM_LKF_CONVERT;
Mark Fasheh019d1b22007-10-05 12:09:05 -07001079 }
1080
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 lockres->l_requested = level;
1082 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1083 spin_unlock_irqrestore(&lockres->l_lock, flags);
1084
Joel Beckerbd3e7612008-02-01 12:14:57 -08001085 BUG_ON(level == DLM_LOCK_IV);
1086 BUG_ON(level == DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001087
1088 mlog(0, "lock %s, convert from %d to level = %d\n",
1089 lockres->l_name, lockres->l_level, level);
1090
1091 /* call dlm_lock to upgrade lock now */
Joel Becker24ef1812008-01-29 17:37:32 -08001092 status = ocfs2_dlm_lock(osb->dlm,
1093 level,
1094 &lockres->l_lksb,
1095 lkm_flags,
1096 lockres->l_name,
1097 OCFS2_LOCK_ID_MAX_LEN - 1,
1098 lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 if (status != DLM_NORMAL) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001100 if ((lkm_flags & DLM_LKF_NOQUEUE) &&
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 (status == DLM_NOTQUEUED))
1102 ret = -EAGAIN;
1103 else {
Joel Becker24ef1812008-01-29 17:37:32 -08001104 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1105 status, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 ret = -EINVAL;
1107 }
1108 ocfs2_recover_from_dlm_error(lockres, 1);
1109 goto out;
1110 }
1111
Joel Becker24ef1812008-01-29 17:37:32 -08001112 mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08001113 lockres->l_name);
1114
1115 /* At this point we've gone inside the dlm and need to
1116 * complete our work regardless. */
1117 catch_signals = 0;
1118
1119 /* wait for busy to clear and carry on */
1120 goto again;
1121 }
1122
1123 /* Ok, if we get here then we're good to go. */
1124 ocfs2_inc_holders(lockres, level);
1125
1126 ret = 0;
1127unlock:
1128 spin_unlock_irqrestore(&lockres->l_lock, flags);
1129out:
1130 /*
1131 * This is helping work around a lock inversion between the page lock
1132 * and dlm locks. One path holds the page lock while calling aops
1133 * which block acquiring dlm locks. The voting thread holds dlm
1134 * locks while acquiring page locks while down converting data locks.
1135 * This block is helping an aop path notice the inversion and back
1136 * off to unlock its page lock before trying the dlm lock again.
1137 */
1138 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1139 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1140 wait = 0;
1141 if (lockres_remove_mask_waiter(lockres, &mw))
1142 ret = -EAGAIN;
1143 else
1144 goto again;
1145 }
1146 if (wait) {
1147 ret = ocfs2_wait_for_mask(&mw);
1148 if (ret == 0)
1149 goto again;
1150 mlog_errno(ret);
1151 }
1152
1153 mlog_exit(ret);
1154 return ret;
1155}
1156
1157static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1158 struct ocfs2_lock_res *lockres,
1159 int level)
1160{
1161 unsigned long flags;
1162
1163 mlog_entry_void();
1164 spin_lock_irqsave(&lockres->l_lock, flags);
1165 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001166 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 spin_unlock_irqrestore(&lockres->l_lock, flags);
1168 mlog_exit_void();
1169}
1170
Adrian Bunkda661162006-11-20 03:24:28 +01001171static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1172 struct ocfs2_lock_res *lockres,
1173 int ex,
1174 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001175{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001176 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001177 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001178 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
1180 spin_lock_irqsave(&lockres->l_lock, flags);
1181 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1182 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1183 spin_unlock_irqrestore(&lockres->l_lock, flags);
1184
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001185 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001186}
1187
1188/* Grants us an EX lock on the data and metadata resources, skipping
1189 * the normal cluster directory lookup. Use this ONLY on newly created
1190 * inodes which other nodes can't possibly see, and which haven't been
1191 * hashed in the inode hash yet. This can give us a good performance
1192 * increase as it'll skip the network broadcast normally associated
1193 * with creating a new lock resource. */
1194int ocfs2_create_new_inode_locks(struct inode *inode)
1195{
1196 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001197 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198
1199 BUG_ON(!inode);
1200 BUG_ON(!ocfs2_inode_is_new(inode));
1201
1202 mlog_entry_void();
1203
Mark Fashehb06970532006-03-03 10:24:33 -08001204 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001205
1206 /* NOTE: That we don't increment any of the holder counts, nor
1207 * do we add anything to a journal handle. Since this is
1208 * supposed to be a new inode which the cluster doesn't know
1209 * about yet, there is no need to. As far as the LVB handling
1210 * is concerned, this is basically like acquiring an EX lock
1211 * on a resource which has an invalid one -- we'll set it
1212 * valid when we release the EX. */
1213
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001214 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001215 if (ret) {
1216 mlog_errno(ret);
1217 goto bail;
1218 }
1219
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001220 /*
Joel Beckerbd3e7612008-02-01 12:14:57 -08001221 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001222 * don't use a generation in their lock names.
1223 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001224 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 if (ret) {
1226 mlog_errno(ret);
1227 goto bail;
1228 }
1229
Tiger Yang50008632007-03-20 16:01:38 -07001230 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1231 if (ret) {
1232 mlog_errno(ret);
1233 goto bail;
1234 }
1235
Mark Fashehccd979b2005-12-15 14:31:24 -08001236bail:
1237 mlog_exit(ret);
1238 return ret;
1239}
1240
1241int ocfs2_rw_lock(struct inode *inode, int write)
1242{
1243 int status, level;
1244 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001245 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001246
1247 BUG_ON(!inode);
1248
1249 mlog_entry_void();
1250
Mark Fashehb06970532006-03-03 10:24:33 -08001251 mlog(0, "inode %llu take %s RW lock\n",
1252 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001253 write ? "EXMODE" : "PRMODE");
1254
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001255 if (ocfs2_mount_local(osb))
1256 return 0;
1257
Mark Fashehccd979b2005-12-15 14:31:24 -08001258 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1259
Joel Beckerbd3e7612008-02-01 12:14:57 -08001260 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001261
1262 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1263 0);
1264 if (status < 0)
1265 mlog_errno(status);
1266
1267 mlog_exit(status);
1268 return status;
1269}
1270
1271void ocfs2_rw_unlock(struct inode *inode, int write)
1272{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001273 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001275 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001276
1277 mlog_entry_void();
1278
Mark Fashehb06970532006-03-03 10:24:33 -08001279 mlog(0, "inode %llu drop %s RW lock\n",
1280 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 write ? "EXMODE" : "PRMODE");
1282
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001283 if (!ocfs2_mount_local(osb))
1284 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001285
1286 mlog_exit_void();
1287}
1288
Tiger Yang50008632007-03-20 16:01:38 -07001289/*
1290 * ocfs2_open_lock always get PR mode lock.
1291 */
1292int ocfs2_open_lock(struct inode *inode)
1293{
1294 int status = 0;
1295 struct ocfs2_lock_res *lockres;
1296 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1297
1298 BUG_ON(!inode);
1299
1300 mlog_entry_void();
1301
1302 mlog(0, "inode %llu take PRMODE open lock\n",
1303 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1304
1305 if (ocfs2_mount_local(osb))
1306 goto out;
1307
1308 lockres = &OCFS2_I(inode)->ip_open_lockres;
1309
1310 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001311 DLM_LOCK_PR, 0, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001312 if (status < 0)
1313 mlog_errno(status);
1314
1315out:
1316 mlog_exit(status);
1317 return status;
1318}
1319
1320int ocfs2_try_open_lock(struct inode *inode, int write)
1321{
1322 int status = 0, level;
1323 struct ocfs2_lock_res *lockres;
1324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1325
1326 BUG_ON(!inode);
1327
1328 mlog_entry_void();
1329
1330 mlog(0, "inode %llu try to take %s open lock\n",
1331 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1332 write ? "EXMODE" : "PRMODE");
1333
1334 if (ocfs2_mount_local(osb))
1335 goto out;
1336
1337 lockres = &OCFS2_I(inode)->ip_open_lockres;
1338
Joel Beckerbd3e7612008-02-01 12:14:57 -08001339 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
Tiger Yang50008632007-03-20 16:01:38 -07001340
1341 /*
1342 * The file system may already holding a PRMODE/EXMODE open lock.
Joel Beckerbd3e7612008-02-01 12:14:57 -08001343 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
Tiger Yang50008632007-03-20 16:01:38 -07001344 * other nodes and the -EAGAIN will indicate to the caller that
1345 * this inode is still in use.
1346 */
1347 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001348 level, DLM_LKF_NOQUEUE, 0);
Tiger Yang50008632007-03-20 16:01:38 -07001349
1350out:
1351 mlog_exit(status);
1352 return status;
1353}
1354
1355/*
1356 * ocfs2_open_unlock unlock PR and EX mode open locks.
1357 */
1358void ocfs2_open_unlock(struct inode *inode)
1359{
1360 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1362
1363 mlog_entry_void();
1364
1365 mlog(0, "inode %llu drop open lock\n",
1366 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1367
1368 if (ocfs2_mount_local(osb))
1369 goto out;
1370
1371 if(lockres->l_ro_holders)
1372 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001373 DLM_LOCK_PR);
Tiger Yang50008632007-03-20 16:01:38 -07001374 if(lockres->l_ex_holders)
1375 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
Joel Beckerbd3e7612008-02-01 12:14:57 -08001376 DLM_LOCK_EX);
Tiger Yang50008632007-03-20 16:01:38 -07001377
1378out:
1379 mlog_exit_void();
1380}
1381
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001382static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1383 int level)
1384{
1385 int ret;
1386 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1387 unsigned long flags;
1388 struct ocfs2_mask_waiter mw;
1389
1390 ocfs2_init_mask_waiter(&mw);
1391
1392retry_cancel:
1393 spin_lock_irqsave(&lockres->l_lock, flags);
1394 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1395 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1396 if (ret) {
1397 spin_unlock_irqrestore(&lockres->l_lock, flags);
1398 ret = ocfs2_cancel_convert(osb, lockres);
1399 if (ret < 0) {
1400 mlog_errno(ret);
1401 goto out;
1402 }
1403 goto retry_cancel;
1404 }
1405 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1406 spin_unlock_irqrestore(&lockres->l_lock, flags);
1407
1408 ocfs2_wait_for_mask(&mw);
1409 goto retry_cancel;
1410 }
1411
1412 ret = -ERESTARTSYS;
1413 /*
1414 * We may still have gotten the lock, in which case there's no
1415 * point to restarting the syscall.
1416 */
1417 if (lockres->l_level == level)
1418 ret = 0;
1419
1420 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1421 lockres->l_flags, lockres->l_level, lockres->l_action);
1422
1423 spin_unlock_irqrestore(&lockres->l_lock, flags);
1424
1425out:
1426 return ret;
1427}
1428
1429/*
1430 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1431 * flock() calls. The locking approach this requires is sufficiently
1432 * different from all other cluster lock types that we implement a
1433 * seperate path to the "low-level" dlm calls. In particular:
1434 *
1435 * - No optimization of lock levels is done - we take at exactly
1436 * what's been requested.
1437 *
1438 * - No lock caching is employed. We immediately downconvert to
1439 * no-lock at unlock time. This also means flock locks never go on
1440 * the blocking list).
1441 *
1442 * - Since userspace can trivially deadlock itself with flock, we make
1443 * sure to allow cancellation of a misbehaving applications flock()
1444 * request.
1445 *
1446 * - Access to any flock lockres doesn't require concurrency, so we
1447 * can simplify the code by requiring the caller to guarantee
1448 * serialization of dlmglue flock calls.
1449 */
1450int ocfs2_file_lock(struct file *file, int ex, int trylock)
1451{
1452 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1453 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1454 unsigned long flags;
1455 struct ocfs2_file_private *fp = file->private_data;
1456 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1457 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1458 struct ocfs2_mask_waiter mw;
1459
1460 ocfs2_init_mask_waiter(&mw);
1461
1462 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
Joel Beckerbd3e7612008-02-01 12:14:57 -08001463 (lockres->l_level > DLM_LOCK_NL)) {
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001464 mlog(ML_ERROR,
1465 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1466 "level: %u\n", lockres->l_name, lockres->l_flags,
1467 lockres->l_level);
1468 return -EINVAL;
1469 }
1470
1471 spin_lock_irqsave(&lockres->l_lock, flags);
1472 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1473 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1474 spin_unlock_irqrestore(&lockres->l_lock, flags);
1475
1476 /*
1477 * Get the lock at NLMODE to start - that way we
1478 * can cancel the upconvert request if need be.
1479 */
1480 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1481 if (ret < 0) {
1482 mlog_errno(ret);
1483 goto out;
1484 }
1485
1486 ret = ocfs2_wait_for_mask(&mw);
1487 if (ret) {
1488 mlog_errno(ret);
1489 goto out;
1490 }
1491 spin_lock_irqsave(&lockres->l_lock, flags);
1492 }
1493
1494 lockres->l_action = OCFS2_AST_CONVERT;
1495 lkm_flags |= LKM_CONVERT;
1496 lockres->l_requested = level;
1497 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1498
1499 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1500 spin_unlock_irqrestore(&lockres->l_lock, flags);
1501
Joel Becker24ef1812008-01-29 17:37:32 -08001502 ret = ocfs2_dlm_lock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1503 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1504 lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001505 if (ret != DLM_NORMAL) {
1506 if (trylock && ret == DLM_NOTQUEUED)
1507 ret = -EAGAIN;
1508 else {
Joel Becker24ef1812008-01-29 17:37:32 -08001509 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001510 ret = -EINVAL;
1511 }
1512
1513 ocfs2_recover_from_dlm_error(lockres, 1);
1514 lockres_remove_mask_waiter(lockres, &mw);
1515 goto out;
1516 }
1517
1518 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1519 if (ret == -ERESTARTSYS) {
1520 /*
1521 * Userspace can cause deadlock itself with
1522 * flock(). Current behavior locally is to allow the
1523 * deadlock, but abort the system call if a signal is
1524 * received. We follow this example, otherwise a
1525 * poorly written program could sit in kernel until
1526 * reboot.
1527 *
1528 * Handling this is a bit more complicated for Ocfs2
1529 * though. We can't exit this function with an
1530 * outstanding lock request, so a cancel convert is
1531 * required. We intentionally overwrite 'ret' - if the
1532 * cancel fails and the lock was granted, it's easier
1533 * to just bubble sucess back up to the user.
1534 */
1535 ret = ocfs2_flock_handle_signal(lockres, level);
1536 }
1537
1538out:
1539
1540 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1541 lockres->l_name, ex, trylock, ret);
1542 return ret;
1543}
1544
1545void ocfs2_file_unlock(struct file *file)
1546{
1547 int ret;
1548 unsigned long flags;
1549 struct ocfs2_file_private *fp = file->private_data;
1550 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1551 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1552 struct ocfs2_mask_waiter mw;
1553
1554 ocfs2_init_mask_waiter(&mw);
1555
1556 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1557 return;
1558
1559 if (lockres->l_level == LKM_NLMODE)
1560 return;
1561
1562 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1563 lockres->l_name, lockres->l_flags, lockres->l_level,
1564 lockres->l_action);
1565
1566 spin_lock_irqsave(&lockres->l_lock, flags);
1567 /*
1568 * Fake a blocking ast for the downconvert code.
1569 */
1570 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
Joel Beckerbd3e7612008-02-01 12:14:57 -08001571 lockres->l_blocking = DLM_LOCK_EX;
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001572
1573 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1574 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1575 spin_unlock_irqrestore(&lockres->l_lock, flags);
1576
1577 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1578 if (ret) {
1579 mlog_errno(ret);
1580 return;
1581 }
1582
1583 ret = ocfs2_wait_for_mask(&mw);
1584 if (ret)
1585 mlog_errno(ret);
1586}
1587
Mark Fasheh34d024f2007-09-24 15:56:19 -07001588static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1589 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001590{
1591 int kick = 0;
1592
1593 mlog_entry_void();
1594
1595 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001596 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001597 * condition. */
1598 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1599 switch(lockres->l_blocking) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08001600 case DLM_LOCK_EX:
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1602 kick = 1;
1603 break;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001604 case DLM_LOCK_PR:
Mark Fashehccd979b2005-12-15 14:31:24 -08001605 if (!lockres->l_ex_holders)
1606 kick = 1;
1607 break;
1608 default:
1609 BUG();
1610 }
1611 }
1612
1613 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001614 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001615
1616 mlog_exit_void();
1617}
1618
Mark Fashehccd979b2005-12-15 14:31:24 -08001619#define OCFS2_SEC_BITS 34
1620#define OCFS2_SEC_SHIFT (64 - 34)
1621#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1622
1623/* LVB only has room for 64 bits of time here so we pack it for
1624 * now. */
1625static u64 ocfs2_pack_timespec(struct timespec *spec)
1626{
1627 u64 res;
1628 u64 sec = spec->tv_sec;
1629 u32 nsec = spec->tv_nsec;
1630
1631 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1632
1633 return res;
1634}
1635
1636/* Call this with the lockres locked. I am reasonably sure we don't
1637 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001638 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001639static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1640{
1641 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001642 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001643 struct ocfs2_meta_lvb *lvb;
1644
1645 mlog_entry_void();
1646
1647 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1648
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001649 /*
1650 * Invalidate the LVB of a deleted inode - this way other
1651 * nodes are forced to go to disk and discover the new inode
1652 * status.
1653 */
1654 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1655 lvb->lvb_version = 0;
1656 goto out;
1657 }
1658
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001659 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001660 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1661 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1662 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1663 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1664 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1665 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1666 lvb->lvb_iatime_packed =
1667 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1668 lvb->lvb_ictime_packed =
1669 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1670 lvb->lvb_imtime_packed =
1671 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001672 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001673 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001674 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001675
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001676out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001677 mlog_meta_lvb(0, lockres);
1678
1679 mlog_exit_void();
1680}
1681
1682static void ocfs2_unpack_timespec(struct timespec *spec,
1683 u64 packed_time)
1684{
1685 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1686 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1687}
1688
1689static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1690{
1691 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001692 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001693 struct ocfs2_meta_lvb *lvb;
1694
1695 mlog_entry_void();
1696
1697 mlog_meta_lvb(0, lockres);
1698
1699 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1700
1701 /* We're safe here without the lockres lock... */
1702 spin_lock(&oi->ip_lock);
1703 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1704 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1705
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001706 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001707 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001708 ocfs2_set_inode_flags(inode);
1709
Mark Fashehccd979b2005-12-15 14:31:24 -08001710 /* fast-symlinks are a special case */
1711 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1712 inode->i_blocks = 0;
1713 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001714 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001715
1716 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1717 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1718 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1719 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1720 ocfs2_unpack_timespec(&inode->i_atime,
1721 be64_to_cpu(lvb->lvb_iatime_packed));
1722 ocfs2_unpack_timespec(&inode->i_mtime,
1723 be64_to_cpu(lvb->lvb_imtime_packed));
1724 ocfs2_unpack_timespec(&inode->i_ctime,
1725 be64_to_cpu(lvb->lvb_ictime_packed));
1726 spin_unlock(&oi->ip_lock);
1727
1728 mlog_exit_void();
1729}
1730
Mark Fashehf9e2d822006-09-12 15:35:49 -07001731static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1732 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001733{
1734 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1735
Mark Fashehf9e2d822006-09-12 15:35:49 -07001736 if (lvb->lvb_version == OCFS2_LVB_VERSION
1737 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 return 1;
1739 return 0;
1740}
1741
1742/* Determine whether a lock resource needs to be refreshed, and
1743 * arbitrate who gets to refresh it.
1744 *
1745 * 0 means no refresh needed.
1746 *
1747 * > 0 means you need to refresh this and you MUST call
1748 * ocfs2_complete_lock_res_refresh afterwards. */
1749static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1750{
1751 unsigned long flags;
1752 int status = 0;
1753
1754 mlog_entry_void();
1755
1756refresh_check:
1757 spin_lock_irqsave(&lockres->l_lock, flags);
1758 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1759 spin_unlock_irqrestore(&lockres->l_lock, flags);
1760 goto bail;
1761 }
1762
1763 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1764 spin_unlock_irqrestore(&lockres->l_lock, flags);
1765
1766 ocfs2_wait_on_refreshing_lock(lockres);
1767 goto refresh_check;
1768 }
1769
1770 /* Ok, I'll be the one to refresh this lock. */
1771 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1772 spin_unlock_irqrestore(&lockres->l_lock, flags);
1773
1774 status = 1;
1775bail:
1776 mlog_exit(status);
1777 return status;
1778}
1779
1780/* If status is non zero, I'll mark it as not being in refresh
1781 * anymroe, but i won't clear the needs refresh flag. */
1782static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1783 int status)
1784{
1785 unsigned long flags;
1786 mlog_entry_void();
1787
1788 spin_lock_irqsave(&lockres->l_lock, flags);
1789 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1790 if (!status)
1791 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1792 spin_unlock_irqrestore(&lockres->l_lock, flags);
1793
1794 wake_up(&lockres->l_event);
1795
1796 mlog_exit_void();
1797}
1798
1799/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001800static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001801 struct buffer_head **bh)
1802{
1803 int status = 0;
1804 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001805 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001806 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001808
1809 mlog_entry_void();
1810
Mark Fashehbe9e9862007-04-18 15:22:08 -07001811 if (ocfs2_mount_local(osb))
1812 goto bail;
1813
Mark Fashehccd979b2005-12-15 14:31:24 -08001814 spin_lock(&oi->ip_lock);
1815 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb06970532006-03-03 10:24:33 -08001816 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001817 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001818 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001819 spin_unlock(&oi->ip_lock);
1820 status = -ENOENT;
1821 goto bail;
1822 }
1823 spin_unlock(&oi->ip_lock);
1824
Mark Fashehbe9e9862007-04-18 15:22:08 -07001825 if (!ocfs2_should_refresh_lock_res(lockres))
1826 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001827
1828 /* This will discard any caching information we might have had
1829 * for the inode metadata. */
1830 ocfs2_metadata_cache_purge(inode);
1831
Mark Fasheh83418972007-04-23 18:53:12 -07001832 ocfs2_extent_map_trunc(inode, 0);
1833
Mark Fashehbe9e9862007-04-18 15:22:08 -07001834 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001835 mlog(0, "Trusting LVB on inode %llu\n",
1836 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001837 ocfs2_refresh_inode_from_lvb(inode);
1838 } else {
1839 /* Boo, we have to go to disk. */
1840 /* read bh, cast, ocfs2_refresh_inode */
1841 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1842 bh, OCFS2_BH_CACHED, inode);
1843 if (status < 0) {
1844 mlog_errno(status);
1845 goto bail_refresh;
1846 }
1847 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1848
1849 /* This is a good chance to make sure we're not
1850 * locking an invalid object.
1851 *
1852 * We bug on a stale inode here because we checked
1853 * above whether it was wiped from disk. The wiping
1854 * node provides a guarantee that we receive that
1855 * message and can mark the inode before dropping any
1856 * locks associated with it. */
1857 if (!OCFS2_IS_VALID_DINODE(fe)) {
1858 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1859 status = -EIO;
1860 goto bail_refresh;
1861 }
1862 mlog_bug_on_msg(inode->i_generation !=
1863 le32_to_cpu(fe->i_generation),
Mark Fashehb06970532006-03-03 10:24:33 -08001864 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001865 "inode->i_generation: %u\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001866 (unsigned long long)oi->ip_blkno,
1867 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 inode->i_generation);
1869 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1870 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb06970532006-03-03 10:24:33 -08001871 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1872 (unsigned long long)oi->ip_blkno,
1873 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001874 le32_to_cpu(fe->i_flags));
1875
1876 ocfs2_refresh_inode(inode, fe);
1877 }
1878
1879 status = 0;
1880bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07001881 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001882bail:
1883 mlog_exit(status);
1884 return status;
1885}
1886
1887static int ocfs2_assign_bh(struct inode *inode,
1888 struct buffer_head **ret_bh,
1889 struct buffer_head *passed_bh)
1890{
1891 int status;
1892
1893 if (passed_bh) {
1894 /* Ok, the update went to disk for us, use the
1895 * returned bh. */
1896 *ret_bh = passed_bh;
1897 get_bh(*ret_bh);
1898
1899 return 0;
1900 }
1901
1902 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1903 OCFS2_I(inode)->ip_blkno,
1904 ret_bh,
1905 OCFS2_BH_CACHED,
1906 inode);
1907 if (status < 0)
1908 mlog_errno(status);
1909
1910 return status;
1911}
1912
1913/*
1914 * returns < 0 error if the callback will never be called, otherwise
1915 * the result of the lock will be communicated via the callback.
1916 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001917int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001918 struct buffer_head **ret_bh,
1919 int ex,
1920 int arg_flags)
1921{
Joel Beckerbd3e7612008-02-01 12:14:57 -08001922 int status, level, acquired;
1923 u32 dlm_flags;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001924 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001925 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1926 struct buffer_head *local_bh = NULL;
1927
1928 BUG_ON(!inode);
1929
1930 mlog_entry_void();
1931
Mark Fashehb06970532006-03-03 10:24:33 -08001932 mlog(0, "inode %llu, take %s META lock\n",
1933 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001934 ex ? "EXMODE" : "PRMODE");
1935
1936 status = 0;
1937 acquired = 0;
1938 /* We'll allow faking a readonly metadata lock for
1939 * rodevices. */
1940 if (ocfs2_is_hard_readonly(osb)) {
1941 if (ex)
1942 status = -EROFS;
1943 goto bail;
1944 }
1945
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001946 if (ocfs2_mount_local(osb))
1947 goto local;
1948
Mark Fashehccd979b2005-12-15 14:31:24 -08001949 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001950 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001951
Mark Fashehe63aecb62007-10-18 15:30:42 -07001952 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Joel Beckerbd3e7612008-02-01 12:14:57 -08001953 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08001954 dlm_flags = 0;
1955 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
Joel Beckerbd3e7612008-02-01 12:14:57 -08001956 dlm_flags |= DLM_LKF_NOQUEUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001957
1958 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1959 if (status < 0) {
1960 if (status != -EAGAIN && status != -EIOCBRETRY)
1961 mlog_errno(status);
1962 goto bail;
1963 }
1964
1965 /* Notify the error cleanup path to drop the cluster lock. */
1966 acquired = 1;
1967
1968 /* We wait twice because a node may have died while we were in
1969 * the lower dlm layers. The second time though, we've
1970 * committed to owning this lock so we don't allow signals to
1971 * abort the operation. */
1972 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
Joel Becker553abd02008-02-01 12:03:57 -08001973 ocfs2_wait_for_recovery(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001974
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001975local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001976 /*
1977 * We only see this flag if we're being called from
1978 * ocfs2_read_locked_inode(). It means we're locking an inode
1979 * which hasn't been populated yet, so clear the refresh flag
1980 * and let the caller handle it.
1981 */
1982 if (inode->i_state & I_NEW) {
1983 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001984 if (lockres)
1985 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001986 goto bail;
1987 }
1988
Mark Fashehccd979b2005-12-15 14:31:24 -08001989 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07001990 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08001991 * may or may not read one, depending on what's in the
1992 * LVB. The result of all of this is that we've *only* gone to
1993 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001994 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001995 if (status < 0) {
1996 if (status != -ENOENT)
1997 mlog_errno(status);
1998 goto bail;
1999 }
2000
2001 if (ret_bh) {
2002 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2003 if (status < 0) {
2004 mlog_errno(status);
2005 goto bail;
2006 }
2007 }
2008
Mark Fashehccd979b2005-12-15 14:31:24 -08002009bail:
2010 if (status < 0) {
2011 if (ret_bh && (*ret_bh)) {
2012 brelse(*ret_bh);
2013 *ret_bh = NULL;
2014 }
2015 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002016 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002017 }
2018
2019 if (local_bh)
2020 brelse(local_bh);
2021
2022 mlog_exit(status);
2023 return status;
2024}
2025
2026/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002027 * This is working around a lock inversion between tasks acquiring DLM
2028 * locks while holding a page lock and the downconvert thread which
2029 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002030 *
2031 * ** These _with_page variantes are only intended to be called from aop
2032 * methods that hold page locks and return a very specific *positive* error
2033 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2034 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002035 * The DLM is called such that it returns -EAGAIN if it would have
2036 * blocked waiting for the downconvert thread. In that case we unlock
2037 * our page so the downconvert thread can make progress. Once we've
2038 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2039 * that called us can bubble that back up into the VFS who will then
2040 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002041 *
2042 * We do a blocking lock and immediate unlock before returning, though, so that
2043 * the lock has a great chance of being cached on this node by the time the VFS
2044 * calls back to retry the aop. This has a potential to livelock as nodes
2045 * ping locks back and forth, but that's a risk we're willing to take to avoid
2046 * the lock inversion simply.
2047 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002048int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002049 struct buffer_head **ret_bh,
2050 int ex,
2051 struct page *page)
2052{
2053 int ret;
2054
Mark Fashehe63aecb62007-10-18 15:30:42 -07002055 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002056 if (ret == -EAGAIN) {
2057 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002058 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2059 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002060 ret = AOP_TRUNCATED_PAGE;
2061 }
2062
2063 return ret;
2064}
2065
Mark Fashehe63aecb62007-10-18 15:30:42 -07002066int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002067 struct vfsmount *vfsmnt,
2068 int *level)
2069{
2070 int ret;
2071
2072 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002073 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002074 if (ret < 0) {
2075 mlog_errno(ret);
2076 return ret;
2077 }
2078
2079 /*
2080 * If we should update atime, we will get EX lock,
2081 * otherwise we just get PR lock.
2082 */
2083 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2084 struct buffer_head *bh = NULL;
2085
Mark Fashehe63aecb62007-10-18 15:30:42 -07002086 ocfs2_inode_unlock(inode, 0);
2087 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002088 if (ret < 0) {
2089 mlog_errno(ret);
2090 return ret;
2091 }
2092 *level = 1;
2093 if (ocfs2_should_update_atime(inode, vfsmnt))
2094 ocfs2_update_inode_atime(inode, bh);
2095 if (bh)
2096 brelse(bh);
2097 } else
2098 *level = 0;
2099
2100 mlog_exit(ret);
2101 return ret;
2102}
2103
Mark Fashehe63aecb62007-10-18 15:30:42 -07002104void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002105 int ex)
2106{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002107 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002108 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002109 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002110
2111 mlog_entry_void();
2112
Mark Fashehb06970532006-03-03 10:24:33 -08002113 mlog(0, "inode %llu drop %s META lock\n",
2114 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002115 ex ? "EXMODE" : "PRMODE");
2116
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002117 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2118 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002119 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2120
2121 mlog_exit_void();
2122}
2123
2124int ocfs2_super_lock(struct ocfs2_super *osb,
2125 int ex)
2126{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002127 int status = 0;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002128 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002129 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002130
2131 mlog_entry_void();
2132
2133 if (ocfs2_is_hard_readonly(osb))
2134 return -EROFS;
2135
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002136 if (ocfs2_mount_local(osb))
2137 goto bail;
2138
Mark Fashehccd979b2005-12-15 14:31:24 -08002139 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2140 if (status < 0) {
2141 mlog_errno(status);
2142 goto bail;
2143 }
2144
2145 /* The super block lock path is really in the best position to
2146 * know when resources covered by the lock need to be
2147 * refreshed, so we do it here. Of course, making sense of
2148 * everything is up to the caller :) */
2149 status = ocfs2_should_refresh_lock_res(lockres);
2150 if (status < 0) {
2151 mlog_errno(status);
2152 goto bail;
2153 }
2154 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002155 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156
2157 ocfs2_complete_lock_res_refresh(lockres, status);
2158
2159 if (status < 0)
2160 mlog_errno(status);
2161 }
2162bail:
2163 mlog_exit(status);
2164 return status;
2165}
2166
2167void ocfs2_super_unlock(struct ocfs2_super *osb,
2168 int ex)
2169{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002170 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2172
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002173 if (!ocfs2_mount_local(osb))
2174 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002175}
2176
2177int ocfs2_rename_lock(struct ocfs2_super *osb)
2178{
2179 int status;
2180 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2181
2182 if (ocfs2_is_hard_readonly(osb))
2183 return -EROFS;
2184
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002185 if (ocfs2_mount_local(osb))
2186 return 0;
2187
Joel Beckerbd3e7612008-02-01 12:14:57 -08002188 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 if (status < 0)
2190 mlog_errno(status);
2191
2192 return status;
2193}
2194
2195void ocfs2_rename_unlock(struct ocfs2_super *osb)
2196{
2197 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2198
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002199 if (!ocfs2_mount_local(osb))
Joel Beckerbd3e7612008-02-01 12:14:57 -08002200 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
Mark Fashehccd979b2005-12-15 14:31:24 -08002201}
2202
Mark Fashehd680efe2006-09-08 14:14:34 -07002203int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2204{
2205 int ret;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002206 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002207 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2208 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2209
2210 BUG_ON(!dl);
2211
2212 if (ocfs2_is_hard_readonly(osb))
2213 return -EROFS;
2214
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002215 if (ocfs2_mount_local(osb))
2216 return 0;
2217
Mark Fashehd680efe2006-09-08 14:14:34 -07002218 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2219 if (ret < 0)
2220 mlog_errno(ret);
2221
2222 return ret;
2223}
2224
2225void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2226{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002227 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
Mark Fashehd680efe2006-09-08 14:14:34 -07002228 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2229 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2230
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002231 if (!ocfs2_mount_local(osb))
2232 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002233}
2234
Mark Fashehccd979b2005-12-15 14:31:24 -08002235/* Reference counting of the dlm debug structure. We want this because
2236 * open references on the debug inodes can live on after a mount, so
2237 * we can't rely on the ocfs2_super to always exist. */
2238static void ocfs2_dlm_debug_free(struct kref *kref)
2239{
2240 struct ocfs2_dlm_debug *dlm_debug;
2241
2242 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2243
2244 kfree(dlm_debug);
2245}
2246
2247void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2248{
2249 if (dlm_debug)
2250 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2251}
2252
2253static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2254{
2255 kref_get(&debug->d_refcnt);
2256}
2257
2258struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2259{
2260 struct ocfs2_dlm_debug *dlm_debug;
2261
2262 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2263 if (!dlm_debug) {
2264 mlog_errno(-ENOMEM);
2265 goto out;
2266 }
2267
2268 kref_init(&dlm_debug->d_refcnt);
2269 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2270 dlm_debug->d_locking_state = NULL;
2271out:
2272 return dlm_debug;
2273}
2274
2275/* Access to this is arbitrated for us via seq_file->sem. */
2276struct ocfs2_dlm_seq_priv {
2277 struct ocfs2_dlm_debug *p_dlm_debug;
2278 struct ocfs2_lock_res p_iter_res;
2279 struct ocfs2_lock_res p_tmp_res;
2280};
2281
2282static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2283 struct ocfs2_dlm_seq_priv *priv)
2284{
2285 struct ocfs2_lock_res *iter, *ret = NULL;
2286 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2287
2288 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2289
2290 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2291 /* discover the head of the list */
2292 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2293 mlog(0, "End of list found, %p\n", ret);
2294 break;
2295 }
2296
2297 /* We track our "dummy" iteration lockres' by a NULL
2298 * l_ops field. */
2299 if (iter->l_ops != NULL) {
2300 ret = iter;
2301 break;
2302 }
2303 }
2304
2305 return ret;
2306}
2307
2308static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2309{
2310 struct ocfs2_dlm_seq_priv *priv = m->private;
2311 struct ocfs2_lock_res *iter;
2312
2313 spin_lock(&ocfs2_dlm_tracking_lock);
2314 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2315 if (iter) {
2316 /* Since lockres' have the lifetime of their container
2317 * (which can be inodes, ocfs2_supers, etc) we want to
2318 * copy this out to a temporary lockres while still
2319 * under the spinlock. Obviously after this we can't
2320 * trust any pointers on the copy returned, but that's
2321 * ok as the information we want isn't typically held
2322 * in them. */
2323 priv->p_tmp_res = *iter;
2324 iter = &priv->p_tmp_res;
2325 }
2326 spin_unlock(&ocfs2_dlm_tracking_lock);
2327
2328 return iter;
2329}
2330
2331static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2332{
2333}
2334
2335static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2336{
2337 struct ocfs2_dlm_seq_priv *priv = m->private;
2338 struct ocfs2_lock_res *iter = v;
2339 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2340
2341 spin_lock(&ocfs2_dlm_tracking_lock);
2342 iter = ocfs2_dlm_next_res(iter, priv);
2343 list_del_init(&dummy->l_debug_list);
2344 if (iter) {
2345 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2346 priv->p_tmp_res = *iter;
2347 iter = &priv->p_tmp_res;
2348 }
2349 spin_unlock(&ocfs2_dlm_tracking_lock);
2350
2351 return iter;
2352}
2353
2354/* So that debugfs.ocfs2 can determine which format is being used */
2355#define OCFS2_DLM_DEBUG_STR_VERSION 1
2356static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2357{
2358 int i;
2359 char *lvb;
2360 struct ocfs2_lock_res *lockres = v;
2361
2362 if (!lockres)
2363 return -EINVAL;
2364
Mark Fashehd680efe2006-09-08 14:14:34 -07002365 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2366
2367 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2368 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2369 lockres->l_name,
2370 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2371 else
2372 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2373
2374 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002375 "0x%lx\t"
2376 "0x%x\t"
2377 "0x%x\t"
2378 "%u\t"
2379 "%u\t"
2380 "%d\t"
2381 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002382 lockres->l_level,
2383 lockres->l_flags,
2384 lockres->l_action,
2385 lockres->l_unlock_action,
2386 lockres->l_ro_holders,
2387 lockres->l_ex_holders,
2388 lockres->l_requested,
2389 lockres->l_blocking);
2390
2391 /* Dump the raw LVB */
2392 lvb = lockres->l_lksb.lvb;
2393 for(i = 0; i < DLM_LVB_LEN; i++)
2394 seq_printf(m, "0x%x\t", lvb[i]);
2395
2396 /* End the line */
2397 seq_printf(m, "\n");
2398 return 0;
2399}
2400
Jan Engelhardt90d99772008-01-22 20:52:20 +01002401static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002402 .start = ocfs2_dlm_seq_start,
2403 .stop = ocfs2_dlm_seq_stop,
2404 .next = ocfs2_dlm_seq_next,
2405 .show = ocfs2_dlm_seq_show,
2406};
2407
2408static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2409{
2410 struct seq_file *seq = (struct seq_file *) file->private_data;
2411 struct ocfs2_dlm_seq_priv *priv = seq->private;
2412 struct ocfs2_lock_res *res = &priv->p_iter_res;
2413
2414 ocfs2_remove_lockres_tracking(res);
2415 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2416 return seq_release_private(inode, file);
2417}
2418
2419static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2420{
2421 int ret;
2422 struct ocfs2_dlm_seq_priv *priv;
2423 struct seq_file *seq;
2424 struct ocfs2_super *osb;
2425
2426 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2427 if (!priv) {
2428 ret = -ENOMEM;
2429 mlog_errno(ret);
2430 goto out;
2431 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002432 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002433 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2434 priv->p_dlm_debug = osb->osb_dlm_debug;
2435 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2436
2437 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2438 if (ret) {
2439 kfree(priv);
2440 mlog_errno(ret);
2441 goto out;
2442 }
2443
2444 seq = (struct seq_file *) file->private_data;
2445 seq->private = priv;
2446
2447 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2448 priv->p_dlm_debug);
2449
2450out:
2451 return ret;
2452}
2453
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002454static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002455 .open = ocfs2_dlm_debug_open,
2456 .release = ocfs2_dlm_debug_release,
2457 .read = seq_read,
2458 .llseek = seq_lseek,
2459};
2460
2461static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2462{
2463 int ret = 0;
2464 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2465
2466 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2467 S_IFREG|S_IRUSR,
2468 osb->osb_debug_root,
2469 osb,
2470 &ocfs2_dlm_debug_fops);
2471 if (!dlm_debug->d_locking_state) {
2472 ret = -EINVAL;
2473 mlog(ML_ERROR,
2474 "Unable to create locking state debugfs file.\n");
2475 goto out;
2476 }
2477
2478 ocfs2_get_dlm_debug(dlm_debug);
2479out:
2480 return ret;
2481}
2482
2483static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2484{
2485 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2486
2487 if (dlm_debug) {
2488 debugfs_remove(dlm_debug->d_locking_state);
2489 ocfs2_put_dlm_debug(dlm_debug);
2490 }
2491}
2492
2493int ocfs2_dlm_init(struct ocfs2_super *osb)
2494{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002495 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002496 u32 dlm_key;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002497 struct dlm_ctxt *dlm = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002498
2499 mlog_entry_void();
2500
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002501 if (ocfs2_mount_local(osb))
2502 goto local;
2503
Mark Fashehccd979b2005-12-15 14:31:24 -08002504 status = ocfs2_dlm_init_debug(osb);
2505 if (status < 0) {
2506 mlog_errno(status);
2507 goto bail;
2508 }
2509
Mark Fasheh34d024f2007-09-24 15:56:19 -07002510 /* launch downconvert thread */
2511 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2512 if (IS_ERR(osb->dc_task)) {
2513 status = PTR_ERR(osb->dc_task);
2514 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002515 mlog_errno(status);
2516 goto bail;
2517 }
2518
2519 /* used by the dlm code to make message headers unique, each
2520 * node in this domain must agree on this. */
2521 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2522
2523 /* for now, uuid == domain */
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002524 dlm = dlm_register_domain(osb->uuid_str, dlm_key,
2525 &osb->osb_locking_proto);
Mark Fashehccd979b2005-12-15 14:31:24 -08002526 if (IS_ERR(dlm)) {
2527 status = PTR_ERR(dlm);
2528 mlog_errno(status);
2529 goto bail;
2530 }
2531
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002532 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2533
2534local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002535 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2536 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2537
Mark Fashehccd979b2005-12-15 14:31:24 -08002538 osb->dlm = dlm;
2539
2540 status = 0;
2541bail:
2542 if (status < 0) {
2543 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002544 if (osb->dc_task)
2545 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002546 }
2547
2548 mlog_exit(status);
2549 return status;
2550}
2551
2552void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2553{
2554 mlog_entry_void();
2555
2556 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2557
2558 ocfs2_drop_osb_locks(osb);
2559
Mark Fasheh34d024f2007-09-24 15:56:19 -07002560 if (osb->dc_task) {
2561 kthread_stop(osb->dc_task);
2562 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002563 }
2564
2565 ocfs2_lock_res_free(&osb->osb_super_lockres);
2566 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2567
2568 dlm_unregister_domain(osb->dlm);
2569 osb->dlm = NULL;
2570
2571 ocfs2_dlm_shutdown_debug(osb);
2572
2573 mlog_exit_void();
2574}
2575
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002576static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002577{
2578 struct ocfs2_lock_res *lockres = opaque;
2579 unsigned long flags;
2580
2581 mlog_entry_void();
2582
2583 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2584 lockres->l_unlock_action);
2585
2586 spin_lock_irqsave(&lockres->l_lock, flags);
2587 /* We tried to cancel a convert request, but it was already
2588 * granted. All we want to do here is clear our unlock
2589 * state. The wake_up call done at the bottom is redundant
2590 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2591 * hurt anything anyway */
2592 if (status == DLM_CANCELGRANT &&
2593 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2594 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2595
2596 /* We don't clear the busy flag in this case as it
2597 * should have been cleared by the ast which the dlm
2598 * has called. */
2599 goto complete_unlock;
2600 }
2601
2602 if (status != DLM_NORMAL) {
2603 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2604 "unlock_action %d\n", status, lockres->l_name,
2605 lockres->l_unlock_action);
2606 spin_unlock_irqrestore(&lockres->l_lock, flags);
2607 return;
2608 }
2609
2610 switch(lockres->l_unlock_action) {
2611 case OCFS2_UNLOCK_CANCEL_CONVERT:
2612 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2613 lockres->l_action = OCFS2_AST_INVALID;
2614 break;
2615 case OCFS2_UNLOCK_DROP_LOCK:
Joel Beckerbd3e7612008-02-01 12:14:57 -08002616 lockres->l_level = DLM_LOCK_IV;
Mark Fashehccd979b2005-12-15 14:31:24 -08002617 break;
2618 default:
2619 BUG();
2620 }
2621
2622 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2623complete_unlock:
2624 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2625 spin_unlock_irqrestore(&lockres->l_lock, flags);
2626
2627 wake_up(&lockres->l_event);
2628
2629 mlog_exit_void();
2630}
2631
Mark Fashehccd979b2005-12-15 14:31:24 -08002632static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002633 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002634{
2635 enum dlm_status status;
2636 unsigned long flags;
Joel Beckerbd3e7612008-02-01 12:14:57 -08002637 u32 lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002638
2639 /* We didn't get anywhere near actually using this lockres. */
2640 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2641 goto out;
2642
Mark Fashehb80fc012006-09-12 22:08:14 -07002643 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002644 lkm_flags |= DLM_LKF_VALBLK;
Mark Fashehb80fc012006-09-12 22:08:14 -07002645
Mark Fashehccd979b2005-12-15 14:31:24 -08002646 spin_lock_irqsave(&lockres->l_lock, flags);
2647
2648 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2649 "lockres %s, flags 0x%lx\n",
2650 lockres->l_name, lockres->l_flags);
2651
2652 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2653 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2654 "%u, unlock_action = %u\n",
2655 lockres->l_name, lockres->l_flags, lockres->l_action,
2656 lockres->l_unlock_action);
2657
2658 spin_unlock_irqrestore(&lockres->l_lock, flags);
2659
2660 /* XXX: Today we just wait on any busy
2661 * locks... Perhaps we need to cancel converts in the
2662 * future? */
2663 ocfs2_wait_on_busy_lock(lockres);
2664
2665 spin_lock_irqsave(&lockres->l_lock, flags);
2666 }
2667
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002668 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2669 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
Joel Beckerbd3e7612008-02-01 12:14:57 -08002670 lockres->l_level == DLM_LOCK_EX &&
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002671 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2672 lockres->l_ops->set_lvb(lockres);
2673 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002674
2675 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2676 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2677 lockres->l_name);
2678 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2679 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2680
2681 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2682 spin_unlock_irqrestore(&lockres->l_lock, flags);
2683 goto out;
2684 }
2685
2686 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2687
2688 /* make sure we never get here while waiting for an ast to
2689 * fire. */
2690 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2691
2692 /* is this necessary? */
2693 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2694 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2695 spin_unlock_irqrestore(&lockres->l_lock, flags);
2696
2697 mlog(0, "lock %s\n", lockres->l_name);
2698
Joel Becker24ef1812008-01-29 17:37:32 -08002699 status = ocfs2_dlm_unlock(osb->dlm, &lockres->l_lksb, lkm_flags,
2700 lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002701 if (status != DLM_NORMAL) {
Joel Becker24ef1812008-01-29 17:37:32 -08002702 ocfs2_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002703 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2704 dlm_print_one_lock(lockres->l_lksb.lockid);
2705 BUG();
2706 }
Joel Becker24ef1812008-01-29 17:37:32 -08002707 mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002708 lockres->l_name);
2709
2710 ocfs2_wait_on_busy_lock(lockres);
2711out:
2712 mlog_exit(0);
2713 return 0;
2714}
2715
2716/* Mark the lockres as being dropped. It will no longer be
2717 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07002718 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08002719 * it safe to drop.
2720 *
2721 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2722void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2723{
2724 int status;
2725 struct ocfs2_mask_waiter mw;
2726 unsigned long flags;
2727
2728 ocfs2_init_mask_waiter(&mw);
2729
2730 spin_lock_irqsave(&lockres->l_lock, flags);
2731 lockres->l_flags |= OCFS2_LOCK_FREEING;
2732 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2733 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2734 spin_unlock_irqrestore(&lockres->l_lock, flags);
2735
2736 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2737
2738 status = ocfs2_wait_for_mask(&mw);
2739 if (status)
2740 mlog_errno(status);
2741
2742 spin_lock_irqsave(&lockres->l_lock, flags);
2743 }
2744 spin_unlock_irqrestore(&lockres->l_lock, flags);
2745}
2746
Mark Fashehd680efe2006-09-08 14:14:34 -07002747void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2748 struct ocfs2_lock_res *lockres)
2749{
2750 int ret;
2751
2752 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002753 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002754 if (ret)
2755 mlog_errno(ret);
2756}
2757
Mark Fashehccd979b2005-12-15 14:31:24 -08002758static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2759{
Mark Fashehd680efe2006-09-08 14:14:34 -07002760 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2761 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002762}
2763
Mark Fashehccd979b2005-12-15 14:31:24 -08002764int ocfs2_drop_inode_locks(struct inode *inode)
2765{
2766 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002767
2768 mlog_entry_void();
2769
2770 /* No need to call ocfs2_mark_lockres_freeing here -
2771 * ocfs2_clear_inode has done it for us. */
2772
2773 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002774 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002775 if (err < 0)
2776 mlog_errno(err);
2777
2778 status = err;
2779
2780 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07002781 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002782 if (err < 0)
2783 mlog_errno(err);
2784 if (err < 0 && !status)
2785 status = err;
2786
2787 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002788 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002789 if (err < 0)
2790 mlog_errno(err);
2791 if (err < 0 && !status)
2792 status = err;
2793
2794 mlog_exit(status);
2795 return status;
2796}
2797
2798static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2799 int new_level)
2800{
2801 assert_spin_locked(&lockres->l_lock);
2802
Joel Beckerbd3e7612008-02-01 12:14:57 -08002803 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002804
2805 if (lockres->l_level <= new_level) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002806 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
Mark Fashehccd979b2005-12-15 14:31:24 -08002807 lockres->l_level, new_level);
2808 BUG();
2809 }
2810
2811 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2812 lockres->l_name, new_level, lockres->l_blocking);
2813
2814 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2815 lockres->l_requested = new_level;
2816 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2817}
2818
2819static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2820 struct ocfs2_lock_res *lockres,
2821 int new_level,
2822 int lvb)
2823{
Joel Beckerbd3e7612008-02-01 12:14:57 -08002824 int ret;
2825 u32 dlm_flags = DLM_LKF_CONVERT;
Mark Fashehccd979b2005-12-15 14:31:24 -08002826 enum dlm_status status;
2827
2828 mlog_entry_void();
2829
2830 if (lvb)
Joel Beckerbd3e7612008-02-01 12:14:57 -08002831 dlm_flags |= DLM_LKF_VALBLK;
Mark Fashehccd979b2005-12-15 14:31:24 -08002832
Joel Becker24ef1812008-01-29 17:37:32 -08002833 status = ocfs2_dlm_lock(osb->dlm,
2834 new_level,
2835 &lockres->l_lksb,
2836 dlm_flags,
2837 lockres->l_name,
2838 OCFS2_LOCK_ID_MAX_LEN - 1,
2839 lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002840 if (status != DLM_NORMAL) {
Joel Becker24ef1812008-01-29 17:37:32 -08002841 ocfs2_log_dlm_error("ocfs2_dlm_lock", status, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002842 ret = -EINVAL;
2843 ocfs2_recover_from_dlm_error(lockres, 1);
2844 goto bail;
2845 }
2846
2847 ret = 0;
2848bail:
2849 mlog_exit(ret);
2850 return ret;
2851}
2852
Joel Becker24ef1812008-01-29 17:37:32 -08002853/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
Mark Fashehccd979b2005-12-15 14:31:24 -08002854static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2855 struct ocfs2_lock_res *lockres)
2856{
2857 assert_spin_locked(&lockres->l_lock);
2858
2859 mlog_entry_void();
2860 mlog(0, "lock %s\n", lockres->l_name);
2861
2862 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2863 /* If we're already trying to cancel a lock conversion
2864 * then just drop the spinlock and allow the caller to
2865 * requeue this lock. */
2866
2867 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2868 return 0;
2869 }
2870
2871 /* were we in a convert when we got the bast fire? */
2872 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2873 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2874 /* set things up for the unlockast to know to just
2875 * clear out the ast_action and unset busy, etc. */
2876 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2877
2878 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2879 "lock %s, invalid flags: 0x%lx\n",
2880 lockres->l_name, lockres->l_flags);
2881
2882 return 1;
2883}
2884
2885static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2886 struct ocfs2_lock_res *lockres)
2887{
2888 int ret;
2889 enum dlm_status status;
2890
2891 mlog_entry_void();
2892 mlog(0, "lock %s\n", lockres->l_name);
2893
2894 ret = 0;
Joel Becker24ef1812008-01-29 17:37:32 -08002895 status = ocfs2_dlm_unlock(osb->dlm,
2896 &lockres->l_lksb,
Joel Beckerbd3e7612008-02-01 12:14:57 -08002897 DLM_LKF_CANCEL,
Joel Becker24ef1812008-01-29 17:37:32 -08002898 lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002899 if (status != DLM_NORMAL) {
Joel Becker24ef1812008-01-29 17:37:32 -08002900 ocfs2_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002901 ret = -EINVAL;
2902 ocfs2_recover_from_dlm_error(lockres, 0);
2903 }
2904
Joel Becker24ef1812008-01-29 17:37:32 -08002905 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002906
2907 mlog_exit(ret);
2908 return ret;
2909}
2910
Mark Fashehb5e500e2006-09-13 22:01:16 -07002911static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2912 struct ocfs2_lock_res *lockres,
2913 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002914{
2915 unsigned long flags;
2916 int blocking;
2917 int new_level;
2918 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002919 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002920
2921 mlog_entry_void();
2922
2923 spin_lock_irqsave(&lockres->l_lock, flags);
2924
2925 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2926
2927recheck:
2928 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002929 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002930 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2931 spin_unlock_irqrestore(&lockres->l_lock, flags);
2932 if (ret) {
2933 ret = ocfs2_cancel_convert(osb, lockres);
2934 if (ret < 0)
2935 mlog_errno(ret);
2936 }
2937 goto leave;
2938 }
2939
2940 /* if we're blocking an exclusive and we have *any* holders,
2941 * then requeue. */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002942 if ((lockres->l_blocking == DLM_LOCK_EX)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002943 && (lockres->l_ex_holders || lockres->l_ro_holders))
2944 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002945
2946 /* If it's a PR we're blocking, then only
2947 * requeue if we've got any EX holders */
Joel Beckerbd3e7612008-02-01 12:14:57 -08002948 if (lockres->l_blocking == DLM_LOCK_PR &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002949 lockres->l_ex_holders)
2950 goto leave_requeue;
2951
2952 /*
2953 * Can we get a lock in this state if the holder counts are
2954 * zero? The meta data unblock code used to check this.
2955 */
2956 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2957 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2958 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002959
Mark Fasheh16d5b952006-09-13 21:10:12 -07002960 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2961
2962 if (lockres->l_ops->check_downconvert
2963 && !lockres->l_ops->check_downconvert(lockres, new_level))
2964 goto leave_requeue;
2965
Mark Fashehccd979b2005-12-15 14:31:24 -08002966 /* If we get here, then we know that there are no more
2967 * incompatible holders (and anyone asking for an incompatible
2968 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002969 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002970 goto downconvert;
2971
2972 /* Some lockres types want to do a bit of work before
2973 * downconverting a lock. Allow that here. The worker function
2974 * may sleep, so we save off a copy of what we're blocking as
2975 * it may change while we're not holding the spin lock. */
2976 blocking = lockres->l_blocking;
2977 spin_unlock_irqrestore(&lockres->l_lock, flags);
2978
Mark Fashehcc567d82006-09-13 21:52:21 -07002979 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002980
2981 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2982 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002983
2984 spin_lock_irqsave(&lockres->l_lock, flags);
2985 if (blocking != lockres->l_blocking) {
2986 /* If this changed underneath us, then we can't drop
2987 * it just yet. */
2988 goto recheck;
2989 }
2990
2991downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002992 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002993
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002994 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
Joel Beckerbd3e7612008-02-01 12:14:57 -08002995 if (lockres->l_level == DLM_LOCK_EX)
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002996 set_lvb = 1;
2997
2998 /*
2999 * We only set the lvb if the lock has been fully
3000 * refreshed - otherwise we risk setting stale
3001 * data. Otherwise, there's no need to actually clear
3002 * out the lvb here as it's value is still valid.
3003 */
3004 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3005 lockres->l_ops->set_lvb(lockres);
3006 }
3007
Mark Fashehccd979b2005-12-15 14:31:24 -08003008 ocfs2_prepare_downconvert(lockres, new_level);
3009 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003010 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003011leave:
3012 mlog_exit(ret);
3013 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003014
3015leave_requeue:
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
3017 ctl->requeue = 1;
3018
3019 mlog_exit(0);
3020 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003021}
3022
Mark Fashehd680efe2006-09-08 14:14:34 -07003023static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3024 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003025{
3026 struct inode *inode;
3027 struct address_space *mapping;
3028
Mark Fashehccd979b2005-12-15 14:31:24 -08003029 inode = ocfs2_lock_res_inode(lockres);
3030 mapping = inode->i_mapping;
3031
Mark Fasheh1044e402008-02-28 17:16:03 -08003032 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003033 goto out;
3034
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003035 /*
3036 * We need this before the filemap_fdatawrite() so that it can
3037 * transfer the dirty bit from the PTE to the
3038 * page. Unfortunately this means that even for EX->PR
3039 * downconverts, we'll lose our mappings and have to build
3040 * them up again.
3041 */
3042 unmap_mapping_range(mapping, 0, 0, 0);
3043
Mark Fashehccd979b2005-12-15 14:31:24 -08003044 if (filemap_fdatawrite(mapping)) {
Mark Fashehb06970532006-03-03 10:24:33 -08003045 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3046 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003047 }
3048 sync_mapping_buffers(mapping);
Joel Beckerbd3e7612008-02-01 12:14:57 -08003049 if (blocking == DLM_LOCK_EX) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003050 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003051 } else {
3052 /* We only need to wait on the I/O if we're not also
3053 * truncating pages because truncate_inode_pages waits
3054 * for us above. We don't truncate pages if we're
3055 * blocking anything < EXMODE because we want to keep
3056 * them around in that case. */
3057 filemap_fdatawait(mapping);
3058 }
3059
Mark Fashehf1f54062007-10-18 15:13:59 -07003060out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003061 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003062}
3063
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003064static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3065 int new_level)
3066{
3067 struct inode *inode = ocfs2_lock_res_inode(lockres);
3068 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3069
Joel Beckerbd3e7612008-02-01 12:14:57 -08003070 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3071 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003072
3073 if (checkpointed)
3074 return 1;
3075
3076 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3077 return 0;
3078}
3079
3080static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3081{
3082 struct inode *inode = ocfs2_lock_res_inode(lockres);
3083
3084 __ocfs2_stuff_meta_lvb(inode);
3085}
3086
Mark Fashehd680efe2006-09-08 14:14:34 -07003087/*
3088 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003089 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003090 * dlmglue API and push these off to the ocfs2_wq in the future.
3091 */
3092static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3093 struct ocfs2_lock_res *lockres)
3094{
3095 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3096 ocfs2_dentry_lock_put(osb, dl);
3097}
3098
3099/*
3100 * d_delete() matching dentries before the lock downconvert.
3101 *
3102 * At this point, any process waiting to destroy the
3103 * dentry_lock due to last ref count is stopped by the
3104 * OCFS2_LOCK_QUEUED flag.
3105 *
3106 * We have two potential problems
3107 *
3108 * 1) If we do the last reference drop on our dentry_lock (via dput)
3109 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3110 * the downconvert to finish. Instead we take an elevated
3111 * reference and push the drop until after we've completed our
3112 * unblock processing.
3113 *
3114 * 2) There might be another process with a final reference,
3115 * waiting on us to finish processing. If this is the case, we
3116 * detect it and exit out - there's no more dentries anyway.
3117 */
3118static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3119 int blocking)
3120{
3121 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3122 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3123 struct dentry *dentry;
3124 unsigned long flags;
3125 int extra_ref = 0;
3126
3127 /*
3128 * This node is blocking another node from getting a read
3129 * lock. This happens when we've renamed within a
3130 * directory. We've forced the other nodes to d_delete(), but
3131 * we never actually dropped our lock because it's still
3132 * valid. The downconvert code will retain a PR for this node,
3133 * so there's no further work to do.
3134 */
Joel Beckerbd3e7612008-02-01 12:14:57 -08003135 if (blocking == DLM_LOCK_PR)
Mark Fashehd680efe2006-09-08 14:14:34 -07003136 return UNBLOCK_CONTINUE;
3137
3138 /*
3139 * Mark this inode as potentially orphaned. The code in
3140 * ocfs2_delete_inode() will figure out whether it actually
3141 * needs to be freed or not.
3142 */
3143 spin_lock(&oi->ip_lock);
3144 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3145 spin_unlock(&oi->ip_lock);
3146
3147 /*
3148 * Yuck. We need to make sure however that the check of
3149 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3150 * respect to a reference decrement or the setting of that
3151 * flag.
3152 */
3153 spin_lock_irqsave(&lockres->l_lock, flags);
3154 spin_lock(&dentry_attach_lock);
3155 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3156 && dl->dl_count) {
3157 dl->dl_count++;
3158 extra_ref = 1;
3159 }
3160 spin_unlock(&dentry_attach_lock);
3161 spin_unlock_irqrestore(&lockres->l_lock, flags);
3162
3163 mlog(0, "extra_ref = %d\n", extra_ref);
3164
3165 /*
3166 * We have a process waiting on us in ocfs2_dentry_iput(),
3167 * which means we can't have any more outstanding
3168 * aliases. There's no need to do any more work.
3169 */
3170 if (!extra_ref)
3171 return UNBLOCK_CONTINUE;
3172
3173 spin_lock(&dentry_attach_lock);
3174 while (1) {
3175 dentry = ocfs2_find_local_alias(dl->dl_inode,
3176 dl->dl_parent_blkno, 1);
3177 if (!dentry)
3178 break;
3179 spin_unlock(&dentry_attach_lock);
3180
3181 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3182 dentry->d_name.name);
3183
3184 /*
3185 * The following dcache calls may do an
3186 * iput(). Normally we don't want that from the
3187 * downconverting thread, but in this case it's ok
3188 * because the requesting node already has an
3189 * exclusive lock on the inode, so it can't be queued
3190 * for a downconvert.
3191 */
3192 d_delete(dentry);
3193 dput(dentry);
3194
3195 spin_lock(&dentry_attach_lock);
3196 }
3197 spin_unlock(&dentry_attach_lock);
3198
3199 /*
3200 * If we are the last holder of this dentry lock, there is no
3201 * reason to downconvert so skip straight to the unlock.
3202 */
3203 if (dl->dl_count == 1)
3204 return UNBLOCK_STOP_POST;
3205
3206 return UNBLOCK_CONTINUE_POST;
3207}
3208
Joel Becker24ef1812008-01-29 17:37:32 -08003209static struct ocfs2_locking_protocol lproto = {
3210 .lp_lock_ast = ocfs2_locking_ast,
3211 .lp_blocking_ast = ocfs2_blocking_ast,
3212 .lp_unlock_ast = ocfs2_unlock_ast,
3213};
3214
3215/* This interface isn't the final one, hence the less-than-perfect names */
3216void dlmglue_init_stack(void)
3217{
3218 o2cb_get_stack(&lproto);
3219}
3220
3221void dlmglue_exit_stack(void)
3222{
3223 o2cb_put_stack();
3224}
3225
Adrian Bunk00600052008-01-29 00:11:41 +02003226static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3227 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003228{
3229 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003230 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003231 unsigned long flags;
3232
3233 /* Our reference to the lockres in this function can be
3234 * considered valid until we remove the OCFS2_LOCK_QUEUED
3235 * flag. */
3236
3237 mlog_entry_void();
3238
3239 BUG_ON(!lockres);
3240 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003241
3242 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3243
3244 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003245 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003246 * still be marked with OCFS2_LOCK_FREEING after this check,
3247 * but short circuiting here will still save us some
3248 * performance. */
3249 spin_lock_irqsave(&lockres->l_lock, flags);
3250 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3251 goto unqueue;
3252 spin_unlock_irqrestore(&lockres->l_lock, flags);
3253
Mark Fashehb5e500e2006-09-13 22:01:16 -07003254 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003255 if (status < 0)
3256 mlog_errno(status);
3257
3258 spin_lock_irqsave(&lockres->l_lock, flags);
3259unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003260 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003261 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3262 } else
3263 ocfs2_schedule_blocked_lock(osb, lockres);
3264
3265 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003266 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003267 spin_unlock_irqrestore(&lockres->l_lock, flags);
3268
Mark Fashehd680efe2006-09-08 14:14:34 -07003269 if (ctl.unblock_action != UNBLOCK_CONTINUE
3270 && lockres->l_ops->post_unlock)
3271 lockres->l_ops->post_unlock(osb, lockres);
3272
Mark Fashehccd979b2005-12-15 14:31:24 -08003273 mlog_exit_void();
3274}
3275
3276static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3277 struct ocfs2_lock_res *lockres)
3278{
3279 mlog_entry_void();
3280
3281 assert_spin_locked(&lockres->l_lock);
3282
3283 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3284 /* Do not schedule a lock for downconvert when it's on
3285 * the way to destruction - any nodes wanting access
3286 * to the resource will get it soon. */
3287 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3288 lockres->l_name, lockres->l_flags);
3289 return;
3290 }
3291
3292 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3293
Mark Fasheh34d024f2007-09-24 15:56:19 -07003294 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003295 if (list_empty(&lockres->l_blocked_list)) {
3296 list_add_tail(&lockres->l_blocked_list,
3297 &osb->blocked_lock_list);
3298 osb->blocked_lock_count++;
3299 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003300 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003301
3302 mlog_exit_void();
3303}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003304
3305static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3306{
3307 unsigned long processed;
3308 struct ocfs2_lock_res *lockres;
3309
3310 mlog_entry_void();
3311
3312 spin_lock(&osb->dc_task_lock);
3313 /* grab this early so we know to try again if a state change and
3314 * wake happens part-way through our work */
3315 osb->dc_work_sequence = osb->dc_wake_sequence;
3316
3317 processed = osb->blocked_lock_count;
3318 while (processed) {
3319 BUG_ON(list_empty(&osb->blocked_lock_list));
3320
3321 lockres = list_entry(osb->blocked_lock_list.next,
3322 struct ocfs2_lock_res, l_blocked_list);
3323 list_del_init(&lockres->l_blocked_list);
3324 osb->blocked_lock_count--;
3325 spin_unlock(&osb->dc_task_lock);
3326
3327 BUG_ON(!processed);
3328 processed--;
3329
3330 ocfs2_process_blocked_lock(osb, lockres);
3331
3332 spin_lock(&osb->dc_task_lock);
3333 }
3334 spin_unlock(&osb->dc_task_lock);
3335
3336 mlog_exit_void();
3337}
3338
3339static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3340{
3341 int empty = 0;
3342
3343 spin_lock(&osb->dc_task_lock);
3344 if (list_empty(&osb->blocked_lock_list))
3345 empty = 1;
3346
3347 spin_unlock(&osb->dc_task_lock);
3348 return empty;
3349}
3350
3351static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3352{
3353 int should_wake = 0;
3354
3355 spin_lock(&osb->dc_task_lock);
3356 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3357 should_wake = 1;
3358 spin_unlock(&osb->dc_task_lock);
3359
3360 return should_wake;
3361}
3362
Adrian Bunk200bfae2008-02-17 10:20:38 +02003363static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003364{
3365 int status = 0;
3366 struct ocfs2_super *osb = arg;
3367
3368 /* only quit once we've been asked to stop and there is no more
3369 * work available */
3370 while (!(kthread_should_stop() &&
3371 ocfs2_downconvert_thread_lists_empty(osb))) {
3372
3373 wait_event_interruptible(osb->dc_event,
3374 ocfs2_downconvert_thread_should_wake(osb) ||
3375 kthread_should_stop());
3376
3377 mlog(0, "downconvert_thread: awoken\n");
3378
3379 ocfs2_downconvert_thread_do_work(osb);
3380 }
3381
3382 osb->dc_task = NULL;
3383 return status;
3384}
3385
3386void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3387{
3388 spin_lock(&osb->dc_task_lock);
3389 /* make sure the voting thread gets a swipe at whatever changes
3390 * the caller may have made to the voting state */
3391 osb->dc_wake_sequence++;
3392 spin_unlock(&osb->dc_task_lock);
3393 wake_up(&osb->dc_event);
3394}