blob: 1a80fa9e7c9abfab4ad1d4beaba2abbdc244bece [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
40#include <dlm/dlmapi.h>
41
42#define MLOG_MASK_PREFIX ML_DLM_GLUE
43#include <cluster/masklog.h>
44
45#include "ocfs2.h"
Joel Beckerd24fbcd2008-01-25 17:02:21 -080046#include "ocfs2_lockingver.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080047
48#include "alloc.h"
Mark Fashehd680efe2006-09-08 14:14:34 -070049#include "dcache.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080050#include "dlmglue.h"
51#include "extent_map.h"
Tiger Yang7f1a37e2006-11-15 15:48:42 +080052#include "file.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053#include "heartbeat.h"
54#include "inode.h"
55#include "journal.h"
56#include "slot_map.h"
57#include "super.h"
58#include "uptodate.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080059
60#include "buffer_head_io.h"
61
62struct ocfs2_mask_waiter {
63 struct list_head mw_item;
64 int mw_status;
65 struct completion mw_complete;
66 unsigned long mw_mask;
67 unsigned long mw_goal;
68};
69
Mark Fasheh54a7e752006-09-12 21:49:13 -070070static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
71static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
Mark Fashehcf8e06f2007-12-20 16:43:10 -080072static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -080073
Mark Fashehd680efe2006-09-08 14:14:34 -070074/*
Mark Fashehcc567d82006-09-13 21:52:21 -070075 * Return value from ->downconvert_worker functions.
Mark Fashehd680efe2006-09-08 14:14:34 -070076 *
Mark Fashehb5e500e2006-09-13 22:01:16 -070077 * These control the precise actions of ocfs2_unblock_lock()
Mark Fashehd680efe2006-09-08 14:14:34 -070078 * and ocfs2_process_blocked_lock()
79 *
80 */
81enum ocfs2_unblock_action {
82 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
83 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
84 * ->post_unlock callback */
85 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
86 * ->post_unlock() callback. */
87};
88
89struct ocfs2_unblock_ctl {
90 int requeue;
91 enum ocfs2_unblock_action unblock_action;
92};
93
Mark Fasheh810d5ae2006-09-13 21:39:52 -070094static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
95 int new_level);
96static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
97
Mark Fashehcc567d82006-09-13 21:52:21 -070098static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
99 int blocking);
100
Mark Fashehcc567d82006-09-13 21:52:21 -0700101static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
102 int blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -0700103
104static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
105 struct ocfs2_lock_res *lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800106
Adrian Bunk6cb129f2007-04-26 00:29:35 -0700107
108#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
109
110/* This aids in debugging situations where a bad LVB might be involved. */
111static void ocfs2_dump_meta_lvb_info(u64 level,
112 const char *function,
113 unsigned int line,
114 struct ocfs2_lock_res *lockres)
115{
116 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
117
118 mlog(level, "LVB information for %s (called from %s:%u):\n",
119 lockres->l_name, function, line);
120 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
121 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
122 be32_to_cpu(lvb->lvb_igeneration));
123 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
124 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
125 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
126 be16_to_cpu(lvb->lvb_imode));
127 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
128 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
129 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
130 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
131 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
132 be32_to_cpu(lvb->lvb_iattr));
133}
134
135
Mark Fashehf625c972006-09-12 21:24:53 -0700136/*
137 * OCFS2 Lock Resource Operations
138 *
139 * These fine tune the behavior of the generic dlmglue locking infrastructure.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700140 *
141 * The most basic of lock types can point ->l_priv to their respective
142 * struct ocfs2_super and allow the default actions to manage things.
143 *
144 * Right now, each lock type also needs to implement an init function,
145 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
146 * should be called when the lock is no longer needed (i.e., object
147 * destruction time).
Mark Fashehf625c972006-09-12 21:24:53 -0700148 */
Mark Fashehccd979b2005-12-15 14:31:24 -0800149struct ocfs2_lock_res_ops {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700150 /*
151 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
152 * this callback if ->l_priv is not an ocfs2_super pointer
153 */
154 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
Mark Fashehb5e500e2006-09-13 22:01:16 -0700155
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700156 /*
Mark Fasheh34d024f2007-09-24 15:56:19 -0700157 * Optionally called in the downconvert thread after a
158 * successful downconvert. The lockres will not be referenced
159 * after this callback is called, so it is safe to free
160 * memory, etc.
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -0700161 *
162 * The exact semantics of when this is called are controlled
163 * by ->downconvert_worker()
164 */
Mark Fashehd680efe2006-09-08 14:14:34 -0700165 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
Mark Fashehf625c972006-09-12 21:24:53 -0700166
167 /*
Mark Fasheh16d5b952006-09-13 21:10:12 -0700168 * Allow a lock type to add checks to determine whether it is
169 * safe to downconvert a lock. Return 0 to re-queue the
170 * downconvert at a later time, nonzero to continue.
171 *
172 * For most locks, the default checks that there are no
173 * incompatible holders are sufficient.
174 *
175 * Called with the lockres spinlock held.
176 */
177 int (*check_downconvert)(struct ocfs2_lock_res *, int);
178
179 /*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700180 * Allows a lock type to populate the lock value block. This
181 * is called on downconvert, and when we drop a lock.
182 *
183 * Locks that want to use this should set LOCK_TYPE_USES_LVB
184 * in the flags field.
185 *
186 * Called with the lockres spinlock held.
187 */
188 void (*set_lvb)(struct ocfs2_lock_res *);
189
190 /*
Mark Fashehcc567d82006-09-13 21:52:21 -0700191 * Called from the downconvert thread when it is determined
192 * that a lock will be downconverted. This is called without
193 * any locks held so the function can do work that might
194 * schedule (syncing out data, etc).
195 *
196 * This should return any one of the ocfs2_unblock_action
197 * values, depending on what it wants the thread to do.
198 */
199 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
200
201 /*
Mark Fashehf625c972006-09-12 21:24:53 -0700202 * LOCK_TYPE_* flags which describe the specific requirements
203 * of a lock type. Descriptions of each individual flag follow.
204 */
205 int flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800206};
207
Mark Fashehf625c972006-09-12 21:24:53 -0700208/*
209 * Some locks want to "refresh" potentially stale data when a
210 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
211 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
212 * individual lockres l_flags member from the ast function. It is
213 * expected that the locking wrapper will clear the
214 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
215 */
216#define LOCK_TYPE_REQUIRES_REFRESH 0x1
217
Mark Fashehb80fc012006-09-12 22:08:14 -0700218/*
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -0700219 * Indicate that a lock type makes use of the lock value block. The
220 * ->set_lvb lock type callback must be defined.
Mark Fashehb80fc012006-09-12 22:08:14 -0700221 */
222#define LOCK_TYPE_USES_LVB 0x2
223
Mark Fashehccd979b2005-12-15 14:31:24 -0800224static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700225 .get_osb = ocfs2_get_inode_osb,
Mark Fashehf625c972006-09-12 21:24:53 -0700226 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800227};
228
Mark Fashehe63aecb62007-10-18 15:30:42 -0700229static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700230 .get_osb = ocfs2_get_inode_osb,
Mark Fasheh810d5ae2006-09-13 21:39:52 -0700231 .check_downconvert = ocfs2_check_meta_downconvert,
232 .set_lvb = ocfs2_set_meta_lvb,
Mark Fashehf1f54062007-10-18 15:13:59 -0700233 .downconvert_worker = ocfs2_data_convert_worker,
Mark Fashehb80fc012006-09-12 22:08:14 -0700234 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
Mark Fashehccd979b2005-12-15 14:31:24 -0800235};
236
Mark Fashehccd979b2005-12-15 14:31:24 -0800237static struct ocfs2_lock_res_ops ocfs2_super_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700238 .flags = LOCK_TYPE_REQUIRES_REFRESH,
Mark Fashehccd979b2005-12-15 14:31:24 -0800239};
240
241static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
Mark Fashehf625c972006-09-12 21:24:53 -0700242 .flags = 0,
Mark Fashehccd979b2005-12-15 14:31:24 -0800243};
244
Mark Fashehd680efe2006-09-08 14:14:34 -0700245static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
Mark Fasheh54a7e752006-09-12 21:49:13 -0700246 .get_osb = ocfs2_get_dentry_osb,
Mark Fashehd680efe2006-09-08 14:14:34 -0700247 .post_unlock = ocfs2_dentry_post_unlock,
Mark Fashehcc567d82006-09-13 21:52:21 -0700248 .downconvert_worker = ocfs2_dentry_convert_worker,
Mark Fashehf625c972006-09-12 21:24:53 -0700249 .flags = 0,
Mark Fashehd680efe2006-09-08 14:14:34 -0700250};
251
Tiger Yang50008632007-03-20 16:01:38 -0700252static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
253 .get_osb = ocfs2_get_inode_osb,
254 .flags = 0,
255};
256
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800257static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
258 .get_osb = ocfs2_get_file_osb,
259 .flags = 0,
260};
261
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800262/*
263 * This is the filesystem locking protocol version.
264 *
265 * Whenever the filesystem does new things with locks (adds or removes a
266 * lock, orders them differently, does different things underneath a lock),
267 * the version must be changed. The protocol is negotiated when joining
268 * the dlm domain. A node may join the domain if its major version is
269 * identical to all other nodes and its minor version is greater than
270 * or equal to all other nodes. When its minor version is greater than
271 * the other nodes, it will run at the minor version specified by the
272 * other nodes.
273 *
274 * If a locking change is made that will not be compatible with older
275 * versions, the major number must be increased and the minor version set
276 * to zero. If a change merely adds a behavior that can be disabled when
277 * speaking to older versions, the minor version must be increased. If a
278 * change adds a fully backwards compatible change (eg, LVB changes that
279 * are just ignored by older versions), the version does not need to be
280 * updated.
281 */
282const struct dlm_protocol_version ocfs2_locking_protocol = {
283 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
284 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
285};
286
Mark Fashehccd979b2005-12-15 14:31:24 -0800287static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
288{
289 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
Tiger Yang50008632007-03-20 16:01:38 -0700290 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
291 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
Mark Fashehccd979b2005-12-15 14:31:24 -0800292}
293
Mark Fashehccd979b2005-12-15 14:31:24 -0800294static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
295{
296 BUG_ON(!ocfs2_is_inode_lock(lockres));
297
298 return (struct inode *) lockres->l_priv;
299}
300
Mark Fashehd680efe2006-09-08 14:14:34 -0700301static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
302{
303 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
304
305 return (struct ocfs2_dentry_lock *)lockres->l_priv;
306}
307
Mark Fasheh54a7e752006-09-12 21:49:13 -0700308static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
309{
310 if (lockres->l_ops->get_osb)
311 return lockres->l_ops->get_osb(lockres);
312
313 return (struct ocfs2_super *)lockres->l_priv;
314}
315
Mark Fashehccd979b2005-12-15 14:31:24 -0800316static int ocfs2_lock_create(struct ocfs2_super *osb,
317 struct ocfs2_lock_res *lockres,
318 int level,
319 int dlm_flags);
320static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
321 int wanted);
322static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
323 struct ocfs2_lock_res *lockres,
324 int level);
325static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
326static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
327static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
328static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
329static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
330 struct ocfs2_lock_res *lockres);
331static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
332 int convert);
333#define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
334 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
335 "resource %s: %s\n", dlm_errname(_stat), _func, \
336 _lockres->l_name, dlm_errmsg(_stat)); \
337} while (0)
Mark Fasheh34d024f2007-09-24 15:56:19 -0700338static int ocfs2_downconvert_thread(void *arg);
339static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
340 struct ocfs2_lock_res *lockres);
Mark Fashehe63aecb62007-10-18 15:30:42 -0700341static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800342 struct buffer_head **bh);
343static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
344static inline int ocfs2_highest_compat_lock_level(int level);
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800345static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
346 int new_level);
347static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
348 struct ocfs2_lock_res *lockres,
349 int new_level,
350 int lvb);
351static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
352 struct ocfs2_lock_res *lockres);
353static int ocfs2_cancel_convert(struct ocfs2_super *osb,
354 struct ocfs2_lock_res *lockres);
355
Mark Fashehccd979b2005-12-15 14:31:24 -0800356
Mark Fashehccd979b2005-12-15 14:31:24 -0800357static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
358 u64 blkno,
359 u32 generation,
360 char *name)
361{
362 int len;
363
364 mlog_entry_void();
365
366 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
367
Mark Fashehb0697052006-03-03 10:24:33 -0800368 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
369 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
370 (long long)blkno, generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800371
372 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
373
374 mlog(0, "built lock resource with name: %s\n", name);
375
376 mlog_exit_void();
377}
378
Ingo Molnar34af9462006-06-27 02:53:55 -0700379static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380
381static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
382 struct ocfs2_dlm_debug *dlm_debug)
383{
384 mlog(0, "Add tracking for lockres %s\n", res->l_name);
385
386 spin_lock(&ocfs2_dlm_tracking_lock);
387 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
388 spin_unlock(&ocfs2_dlm_tracking_lock);
389}
390
391static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
392{
393 spin_lock(&ocfs2_dlm_tracking_lock);
394 if (!list_empty(&res->l_debug_list))
395 list_del_init(&res->l_debug_list);
396 spin_unlock(&ocfs2_dlm_tracking_lock);
397}
398
399static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
400 struct ocfs2_lock_res *res,
401 enum ocfs2_lock_type type,
Mark Fashehccd979b2005-12-15 14:31:24 -0800402 struct ocfs2_lock_res_ops *ops,
403 void *priv)
404{
Mark Fashehccd979b2005-12-15 14:31:24 -0800405 res->l_type = type;
406 res->l_ops = ops;
407 res->l_priv = priv;
408
409 res->l_level = LKM_IVMODE;
410 res->l_requested = LKM_IVMODE;
411 res->l_blocking = LKM_IVMODE;
412 res->l_action = OCFS2_AST_INVALID;
413 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
414
415 res->l_flags = OCFS2_LOCK_INITIALIZED;
416
417 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
418}
419
420void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
421{
422 /* This also clears out the lock status block */
423 memset(res, 0, sizeof(struct ocfs2_lock_res));
424 spin_lock_init(&res->l_lock);
425 init_waitqueue_head(&res->l_event);
426 INIT_LIST_HEAD(&res->l_blocked_list);
427 INIT_LIST_HEAD(&res->l_mask_waiters);
428}
429
430void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
431 enum ocfs2_lock_type type,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700432 unsigned int generation,
Mark Fashehccd979b2005-12-15 14:31:24 -0800433 struct inode *inode)
434{
435 struct ocfs2_lock_res_ops *ops;
436
437 switch(type) {
438 case OCFS2_LOCK_TYPE_RW:
439 ops = &ocfs2_inode_rw_lops;
440 break;
441 case OCFS2_LOCK_TYPE_META:
Mark Fashehe63aecb62007-10-18 15:30:42 -0700442 ops = &ocfs2_inode_inode_lops;
Mark Fashehccd979b2005-12-15 14:31:24 -0800443 break;
Tiger Yang50008632007-03-20 16:01:38 -0700444 case OCFS2_LOCK_TYPE_OPEN:
445 ops = &ocfs2_inode_open_lops;
446 break;
Mark Fashehccd979b2005-12-15 14:31:24 -0800447 default:
448 mlog_bug_on_msg(1, "type: %d\n", type);
449 ops = NULL; /* thanks, gcc */
450 break;
451 };
452
Mark Fashehd680efe2006-09-08 14:14:34 -0700453 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
Mark Fasheh24c19ef2006-09-22 17:28:19 -0700454 generation, res->l_name);
Mark Fashehd680efe2006-09-08 14:14:34 -0700455 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
456}
457
Mark Fasheh54a7e752006-09-12 21:49:13 -0700458static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
459{
460 struct inode *inode = ocfs2_lock_res_inode(lockres);
461
462 return OCFS2_SB(inode->i_sb);
463}
464
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800465static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
466{
467 struct ocfs2_file_private *fp = lockres->l_priv;
468
469 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
470}
471
Mark Fashehd680efe2006-09-08 14:14:34 -0700472static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
473{
474 __be64 inode_blkno_be;
475
476 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
477 sizeof(__be64));
478
479 return be64_to_cpu(inode_blkno_be);
480}
481
Mark Fasheh54a7e752006-09-12 21:49:13 -0700482static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
483{
484 struct ocfs2_dentry_lock *dl = lockres->l_priv;
485
486 return OCFS2_SB(dl->dl_inode->i_sb);
487}
488
Mark Fashehd680efe2006-09-08 14:14:34 -0700489void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
490 u64 parent, struct inode *inode)
491{
492 int len;
493 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
494 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
495 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
496
497 ocfs2_lock_res_init_once(lockres);
498
499 /*
500 * Unfortunately, the standard lock naming scheme won't work
501 * here because we have two 16 byte values to use. Instead,
502 * we'll stuff the inode number as a binary value. We still
503 * want error prints to show something without garbling the
504 * display, so drop a null byte in there before the inode
505 * number. A future version of OCFS2 will likely use all
506 * binary lock names. The stringified names have been a
507 * tremendous aid in debugging, but now that the debugfs
508 * interface exists, we can mangle things there if need be.
509 *
510 * NOTE: We also drop the standard "pad" value (the total lock
511 * name size stays the same though - the last part is all
512 * zeros due to the memset in ocfs2_lock_res_init_once()
513 */
514 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
515 "%c%016llx",
516 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
517 (long long)parent);
518
519 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
520
521 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
522 sizeof(__be64));
523
524 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
525 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
526 dl);
Mark Fashehccd979b2005-12-15 14:31:24 -0800527}
528
529static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
530 struct ocfs2_super *osb)
531{
532 /* Superblock lockres doesn't come from a slab so we call init
533 * once on it manually. */
534 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700535 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
536 0, res->l_name);
Mark Fashehccd979b2005-12-15 14:31:24 -0800537 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
Mark Fashehccd979b2005-12-15 14:31:24 -0800538 &ocfs2_super_lops, osb);
539}
540
541static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
542 struct ocfs2_super *osb)
543{
544 /* Rename lockres doesn't come from a slab so we call init
545 * once on it manually. */
546 ocfs2_lock_res_init_once(res);
Mark Fashehd680efe2006-09-08 14:14:34 -0700547 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
548 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
Mark Fashehccd979b2005-12-15 14:31:24 -0800549 &ocfs2_rename_lops, osb);
550}
551
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800552void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
553 struct ocfs2_file_private *fp)
554{
555 struct inode *inode = fp->fp_file->f_mapping->host;
556 struct ocfs2_inode_info *oi = OCFS2_I(inode);
557
558 ocfs2_lock_res_init_once(lockres);
559 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
560 inode->i_generation, lockres->l_name);
561 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
562 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
563 fp);
564 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
565}
566
Mark Fashehccd979b2005-12-15 14:31:24 -0800567void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
568{
569 mlog_entry_void();
570
571 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
572 return;
573
574 ocfs2_remove_lockres_tracking(res);
575
576 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
577 "Lockres %s is on the blocked list\n",
578 res->l_name);
579 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
580 "Lockres %s has mask waiters pending\n",
581 res->l_name);
582 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
583 "Lockres %s is locked\n",
584 res->l_name);
585 mlog_bug_on_msg(res->l_ro_holders,
586 "Lockres %s has %u ro holders\n",
587 res->l_name, res->l_ro_holders);
588 mlog_bug_on_msg(res->l_ex_holders,
589 "Lockres %s has %u ex holders\n",
590 res->l_name, res->l_ex_holders);
591
592 /* Need to clear out the lock status block for the dlm */
593 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
594
595 res->l_flags = 0UL;
596 mlog_exit_void();
597}
598
599static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
600 int level)
601{
602 mlog_entry_void();
603
604 BUG_ON(!lockres);
605
606 switch(level) {
607 case LKM_EXMODE:
608 lockres->l_ex_holders++;
609 break;
610 case LKM_PRMODE:
611 lockres->l_ro_holders++;
612 break;
613 default:
614 BUG();
615 }
616
617 mlog_exit_void();
618}
619
620static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
621 int level)
622{
623 mlog_entry_void();
624
625 BUG_ON(!lockres);
626
627 switch(level) {
628 case LKM_EXMODE:
629 BUG_ON(!lockres->l_ex_holders);
630 lockres->l_ex_holders--;
631 break;
632 case LKM_PRMODE:
633 BUG_ON(!lockres->l_ro_holders);
634 lockres->l_ro_holders--;
635 break;
636 default:
637 BUG();
638 }
639 mlog_exit_void();
640}
641
642/* WARNING: This function lives in a world where the only three lock
643 * levels are EX, PR, and NL. It *will* have to be adjusted when more
644 * lock types are added. */
645static inline int ocfs2_highest_compat_lock_level(int level)
646{
647 int new_level = LKM_EXMODE;
648
649 if (level == LKM_EXMODE)
650 new_level = LKM_NLMODE;
651 else if (level == LKM_PRMODE)
652 new_level = LKM_PRMODE;
653 return new_level;
654}
655
656static void lockres_set_flags(struct ocfs2_lock_res *lockres,
657 unsigned long newflags)
658{
Christoph Hellwig800deef2007-05-17 16:03:13 +0200659 struct ocfs2_mask_waiter *mw, *tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -0800660
661 assert_spin_locked(&lockres->l_lock);
662
663 lockres->l_flags = newflags;
664
Christoph Hellwig800deef2007-05-17 16:03:13 +0200665 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800666 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
667 continue;
668
669 list_del_init(&mw->mw_item);
670 mw->mw_status = 0;
671 complete(&mw->mw_complete);
672 }
673}
674static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
675{
676 lockres_set_flags(lockres, lockres->l_flags | or);
677}
678static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
679 unsigned long clear)
680{
681 lockres_set_flags(lockres, lockres->l_flags & ~clear);
682}
683
684static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
685{
686 mlog_entry_void();
687
688 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
689 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
690 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
691 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
692
693 lockres->l_level = lockres->l_requested;
694 if (lockres->l_level <=
695 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
696 lockres->l_blocking = LKM_NLMODE;
697 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
698 }
699 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
700
701 mlog_exit_void();
702}
703
704static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
705{
706 mlog_entry_void();
707
708 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
709 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
710
711 /* Convert from RO to EX doesn't really need anything as our
712 * information is already up to data. Convert from NL to
713 * *anything* however should mark ourselves as needing an
714 * update */
Mark Fashehf625c972006-09-12 21:24:53 -0700715 if (lockres->l_level == LKM_NLMODE &&
716 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800717 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
718
719 lockres->l_level = lockres->l_requested;
720 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
721
722 mlog_exit_void();
723}
724
725static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
726{
727 mlog_entry_void();
728
Roel Kluin3cf0c502007-10-27 00:20:36 +0200729 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
Mark Fashehccd979b2005-12-15 14:31:24 -0800730 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
731
732 if (lockres->l_requested > LKM_NLMODE &&
Mark Fashehf625c972006-09-12 21:24:53 -0700733 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
734 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
736
737 lockres->l_level = lockres->l_requested;
738 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
739 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
740
741 mlog_exit_void();
742}
743
Mark Fashehccd979b2005-12-15 14:31:24 -0800744static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
745 int level)
746{
747 int needs_downconvert = 0;
748 mlog_entry_void();
749
750 assert_spin_locked(&lockres->l_lock);
751
752 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
753
754 if (level > lockres->l_blocking) {
755 /* only schedule a downconvert if we haven't already scheduled
756 * one that goes low enough to satisfy the level we're
757 * blocking. this also catches the case where we get
758 * duplicate BASTs */
759 if (ocfs2_highest_compat_lock_level(level) <
760 ocfs2_highest_compat_lock_level(lockres->l_blocking))
761 needs_downconvert = 1;
762
763 lockres->l_blocking = level;
764 }
765
766 mlog_exit(needs_downconvert);
767 return needs_downconvert;
768}
769
Mark Fashehaa2623a2006-09-12 21:58:23 -0700770static void ocfs2_blocking_ast(void *opaque, int level)
Mark Fashehccd979b2005-12-15 14:31:24 -0800771{
Mark Fashehaa2623a2006-09-12 21:58:23 -0700772 struct ocfs2_lock_res *lockres = opaque;
773 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -0800774 int needs_downconvert;
775 unsigned long flags;
776
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 BUG_ON(level <= LKM_NLMODE);
778
Mark Fashehaa2623a2006-09-12 21:58:23 -0700779 mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
780 lockres->l_name, level, lockres->l_level,
781 ocfs2_lock_type_string(lockres->l_type));
782
Mark Fashehcf8e06f2007-12-20 16:43:10 -0800783 /*
784 * We can skip the bast for locks which don't enable caching -
785 * they'll be dropped at the earliest possible time anyway.
786 */
787 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
788 return;
789
Mark Fashehccd979b2005-12-15 14:31:24 -0800790 spin_lock_irqsave(&lockres->l_lock, flags);
791 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
792 if (needs_downconvert)
793 ocfs2_schedule_blocked_lock(osb, lockres);
794 spin_unlock_irqrestore(&lockres->l_lock, flags);
795
Mark Fashehd680efe2006-09-08 14:14:34 -0700796 wake_up(&lockres->l_event);
797
Mark Fasheh34d024f2007-09-24 15:56:19 -0700798 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800799}
800
Mark Fashehe92d57d2006-09-12 21:34:35 -0700801static void ocfs2_locking_ast(void *opaque)
Mark Fashehccd979b2005-12-15 14:31:24 -0800802{
Mark Fashehe92d57d2006-09-12 21:34:35 -0700803 struct ocfs2_lock_res *lockres = opaque;
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 struct dlm_lockstatus *lksb = &lockres->l_lksb;
805 unsigned long flags;
806
807 spin_lock_irqsave(&lockres->l_lock, flags);
808
809 if (lksb->status != DLM_NORMAL) {
810 mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
811 lockres->l_name, lksb->status);
812 spin_unlock_irqrestore(&lockres->l_lock, flags);
813 return;
814 }
815
816 switch(lockres->l_action) {
817 case OCFS2_AST_ATTACH:
818 ocfs2_generic_handle_attach_action(lockres);
Mark Fashehe92d57d2006-09-12 21:34:35 -0700819 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 break;
821 case OCFS2_AST_CONVERT:
822 ocfs2_generic_handle_convert_action(lockres);
823 break;
824 case OCFS2_AST_DOWNCONVERT:
825 ocfs2_generic_handle_downconvert_action(lockres);
826 break;
827 default:
Mark Fashehe92d57d2006-09-12 21:34:35 -0700828 mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
829 "lockres flags = 0x%lx, unlock action: %u\n",
830 lockres->l_name, lockres->l_action, lockres->l_flags,
831 lockres->l_unlock_action);
Mark Fashehccd979b2005-12-15 14:31:24 -0800832 BUG();
833 }
834
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 /* set it to something invalid so if we get called again we
836 * can catch it. */
837 lockres->l_action = OCFS2_AST_INVALID;
Mark Fashehccd979b2005-12-15 14:31:24 -0800838
839 wake_up(&lockres->l_event);
Mark Fashehd680efe2006-09-08 14:14:34 -0700840 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800841}
842
Mark Fashehccd979b2005-12-15 14:31:24 -0800843static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
844 int convert)
845{
846 unsigned long flags;
847
848 mlog_entry_void();
849 spin_lock_irqsave(&lockres->l_lock, flags);
850 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
851 if (convert)
852 lockres->l_action = OCFS2_AST_INVALID;
853 else
854 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
855 spin_unlock_irqrestore(&lockres->l_lock, flags);
856
857 wake_up(&lockres->l_event);
858 mlog_exit_void();
859}
860
861/* Note: If we detect another process working on the lock (i.e.,
862 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
863 * to do the right thing in that case.
864 */
865static int ocfs2_lock_create(struct ocfs2_super *osb,
866 struct ocfs2_lock_res *lockres,
867 int level,
868 int dlm_flags)
869{
870 int ret = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -0800871 enum dlm_status status = DLM_NORMAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 unsigned long flags;
873
874 mlog_entry_void();
875
876 mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
877 dlm_flags);
878
879 spin_lock_irqsave(&lockres->l_lock, flags);
880 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
881 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
882 spin_unlock_irqrestore(&lockres->l_lock, flags);
883 goto bail;
884 }
885
886 lockres->l_action = OCFS2_AST_ATTACH;
887 lockres->l_requested = level;
888 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
889 spin_unlock_irqrestore(&lockres->l_lock, flags);
890
891 status = dlmlock(osb->dlm,
892 level,
893 &lockres->l_lksb,
894 dlm_flags,
895 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -0700896 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -0700897 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -0700899 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 if (status != DLM_NORMAL) {
901 ocfs2_log_dlm_error("dlmlock", status, lockres);
902 ret = -EINVAL;
903 ocfs2_recover_from_dlm_error(lockres, 1);
904 }
905
906 mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
907
908bail:
909 mlog_exit(ret);
910 return ret;
911}
912
913static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
914 int flag)
915{
916 unsigned long flags;
917 int ret;
918
919 spin_lock_irqsave(&lockres->l_lock, flags);
920 ret = lockres->l_flags & flag;
921 spin_unlock_irqrestore(&lockres->l_lock, flags);
922
923 return ret;
924}
925
926static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
927
928{
929 wait_event(lockres->l_event,
930 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
931}
932
933static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
934
935{
936 wait_event(lockres->l_event,
937 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
938}
939
940/* predict what lock level we'll be dropping down to on behalf
941 * of another node, and return true if the currently wanted
942 * level will be compatible with it. */
943static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
944 int wanted)
945{
946 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
947
948 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
949}
950
951static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
952{
953 INIT_LIST_HEAD(&mw->mw_item);
954 init_completion(&mw->mw_complete);
955}
956
957static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
958{
959 wait_for_completion(&mw->mw_complete);
960 /* Re-arm the completion in case we want to wait on it again */
961 INIT_COMPLETION(mw->mw_complete);
962 return mw->mw_status;
963}
964
965static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
966 struct ocfs2_mask_waiter *mw,
967 unsigned long mask,
968 unsigned long goal)
969{
970 BUG_ON(!list_empty(&mw->mw_item));
971
972 assert_spin_locked(&lockres->l_lock);
973
974 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
975 mw->mw_mask = mask;
976 mw->mw_goal = goal;
977}
978
979/* returns 0 if the mw that was removed was already satisfied, -EBUSY
980 * if the mask still hadn't reached its goal */
981static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
982 struct ocfs2_mask_waiter *mw)
983{
984 unsigned long flags;
985 int ret = 0;
986
987 spin_lock_irqsave(&lockres->l_lock, flags);
988 if (!list_empty(&mw->mw_item)) {
989 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
990 ret = -EBUSY;
991
992 list_del_init(&mw->mw_item);
993 init_completion(&mw->mw_complete);
994 }
995 spin_unlock_irqrestore(&lockres->l_lock, flags);
996
997 return ret;
998
999}
1000
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001001static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1002 struct ocfs2_lock_res *lockres)
1003{
1004 int ret;
1005
1006 ret = wait_for_completion_interruptible(&mw->mw_complete);
1007 if (ret)
1008 lockres_remove_mask_waiter(lockres, mw);
1009 else
1010 ret = mw->mw_status;
1011 /* Re-arm the completion in case we want to wait on it again */
1012 INIT_COMPLETION(mw->mw_complete);
1013 return ret;
1014}
1015
Mark Fashehccd979b2005-12-15 14:31:24 -08001016static int ocfs2_cluster_lock(struct ocfs2_super *osb,
1017 struct ocfs2_lock_res *lockres,
1018 int level,
1019 int lkm_flags,
1020 int arg_flags)
1021{
1022 struct ocfs2_mask_waiter mw;
1023 enum dlm_status status;
1024 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1025 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1026 unsigned long flags;
1027
1028 mlog_entry_void();
1029
1030 ocfs2_init_mask_waiter(&mw);
1031
Mark Fashehb80fc012006-09-12 22:08:14 -07001032 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1033 lkm_flags |= LKM_VALBLK;
1034
Mark Fashehccd979b2005-12-15 14:31:24 -08001035again:
1036 wait = 0;
1037
1038 if (catch_signals && signal_pending(current)) {
1039 ret = -ERESTARTSYS;
1040 goto out;
1041 }
1042
1043 spin_lock_irqsave(&lockres->l_lock, flags);
1044
1045 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1046 "Cluster lock called on freeing lockres %s! flags "
1047 "0x%lx\n", lockres->l_name, lockres->l_flags);
1048
1049 /* We only compare against the currently granted level
1050 * here. If the lock is blocked waiting on a downconvert,
1051 * we'll get caught below. */
1052 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1053 level > lockres->l_level) {
1054 /* is someone sitting in dlm_lock? If so, wait on
1055 * them. */
1056 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1057 wait = 1;
1058 goto unlock;
1059 }
1060
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1062 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1063 /* is the lock is currently blocked on behalf of
1064 * another node */
1065 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1066 wait = 1;
1067 goto unlock;
1068 }
1069
1070 if (level > lockres->l_level) {
1071 if (lockres->l_action != OCFS2_AST_INVALID)
1072 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1073 lockres->l_name, lockres->l_action);
1074
Mark Fasheh019d1b22007-10-05 12:09:05 -07001075 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1076 lockres->l_action = OCFS2_AST_ATTACH;
1077 lkm_flags &= ~LKM_CONVERT;
1078 } else {
1079 lockres->l_action = OCFS2_AST_CONVERT;
1080 lkm_flags |= LKM_CONVERT;
1081 }
1082
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 lockres->l_requested = level;
1084 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1085 spin_unlock_irqrestore(&lockres->l_lock, flags);
1086
1087 BUG_ON(level == LKM_IVMODE);
1088 BUG_ON(level == LKM_NLMODE);
1089
1090 mlog(0, "lock %s, convert from %d to level = %d\n",
1091 lockres->l_name, lockres->l_level, level);
1092
1093 /* call dlm_lock to upgrade lock now */
1094 status = dlmlock(osb->dlm,
1095 level,
1096 &lockres->l_lksb,
Mark Fasheh019d1b22007-10-05 12:09:05 -07001097 lkm_flags,
Mark Fashehccd979b2005-12-15 14:31:24 -08001098 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -07001099 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -07001100 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -07001102 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 if (status != DLM_NORMAL) {
1104 if ((lkm_flags & LKM_NOQUEUE) &&
1105 (status == DLM_NOTQUEUED))
1106 ret = -EAGAIN;
1107 else {
1108 ocfs2_log_dlm_error("dlmlock", status,
1109 lockres);
1110 ret = -EINVAL;
1111 }
1112 ocfs2_recover_from_dlm_error(lockres, 1);
1113 goto out;
1114 }
1115
1116 mlog(0, "lock %s, successfull return from dlmlock\n",
1117 lockres->l_name);
1118
1119 /* At this point we've gone inside the dlm and need to
1120 * complete our work regardless. */
1121 catch_signals = 0;
1122
1123 /* wait for busy to clear and carry on */
1124 goto again;
1125 }
1126
1127 /* Ok, if we get here then we're good to go. */
1128 ocfs2_inc_holders(lockres, level);
1129
1130 ret = 0;
1131unlock:
1132 spin_unlock_irqrestore(&lockres->l_lock, flags);
1133out:
1134 /*
1135 * This is helping work around a lock inversion between the page lock
1136 * and dlm locks. One path holds the page lock while calling aops
1137 * which block acquiring dlm locks. The voting thread holds dlm
1138 * locks while acquiring page locks while down converting data locks.
1139 * This block is helping an aop path notice the inversion and back
1140 * off to unlock its page lock before trying the dlm lock again.
1141 */
1142 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1143 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1144 wait = 0;
1145 if (lockres_remove_mask_waiter(lockres, &mw))
1146 ret = -EAGAIN;
1147 else
1148 goto again;
1149 }
1150 if (wait) {
1151 ret = ocfs2_wait_for_mask(&mw);
1152 if (ret == 0)
1153 goto again;
1154 mlog_errno(ret);
1155 }
1156
1157 mlog_exit(ret);
1158 return ret;
1159}
1160
1161static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
1162 struct ocfs2_lock_res *lockres,
1163 int level)
1164{
1165 unsigned long flags;
1166
1167 mlog_entry_void();
1168 spin_lock_irqsave(&lockres->l_lock, flags);
1169 ocfs2_dec_holders(lockres, level);
Mark Fasheh34d024f2007-09-24 15:56:19 -07001170 ocfs2_downconvert_on_unlock(osb, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 spin_unlock_irqrestore(&lockres->l_lock, flags);
1172 mlog_exit_void();
1173}
1174
Adrian Bunkda661162006-11-20 03:24:28 +01001175static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1176 struct ocfs2_lock_res *lockres,
1177 int ex,
1178 int local)
Mark Fashehccd979b2005-12-15 14:31:24 -08001179{
Mark Fashehd680efe2006-09-08 14:14:34 -07001180 int level = ex ? LKM_EXMODE : LKM_PRMODE;
Mark Fashehccd979b2005-12-15 14:31:24 -08001181 unsigned long flags;
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001182 int lkm_flags = local ? LKM_LOCAL : 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001183
1184 spin_lock_irqsave(&lockres->l_lock, flags);
1185 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1186 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1187 spin_unlock_irqrestore(&lockres->l_lock, flags);
1188
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001189 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001190}
1191
1192/* Grants us an EX lock on the data and metadata resources, skipping
1193 * the normal cluster directory lookup. Use this ONLY on newly created
1194 * inodes which other nodes can't possibly see, and which haven't been
1195 * hashed in the inode hash yet. This can give us a good performance
1196 * increase as it'll skip the network broadcast normally associated
1197 * with creating a new lock resource. */
1198int ocfs2_create_new_inode_locks(struct inode *inode)
1199{
1200 int ret;
Mark Fashehd680efe2006-09-08 14:14:34 -07001201 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001202
1203 BUG_ON(!inode);
1204 BUG_ON(!ocfs2_inode_is_new(inode));
1205
1206 mlog_entry_void();
1207
Mark Fashehb0697052006-03-03 10:24:33 -08001208 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001209
1210 /* NOTE: That we don't increment any of the holder counts, nor
1211 * do we add anything to a journal handle. Since this is
1212 * supposed to be a new inode which the cluster doesn't know
1213 * about yet, there is no need to. As far as the LVB handling
1214 * is concerned, this is basically like acquiring an EX lock
1215 * on a resource which has an invalid one -- we'll set it
1216 * valid when we release the EX. */
1217
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001218 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001219 if (ret) {
1220 mlog_errno(ret);
1221 goto bail;
1222 }
1223
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001224 /*
1225 * We don't want to use LKM_LOCAL on a meta data lock as they
1226 * don't use a generation in their lock names.
1227 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001228 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 if (ret) {
1230 mlog_errno(ret);
1231 goto bail;
1232 }
1233
Tiger Yang50008632007-03-20 16:01:38 -07001234 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1235 if (ret) {
1236 mlog_errno(ret);
1237 goto bail;
1238 }
1239
Mark Fashehccd979b2005-12-15 14:31:24 -08001240bail:
1241 mlog_exit(ret);
1242 return ret;
1243}
1244
1245int ocfs2_rw_lock(struct inode *inode, int write)
1246{
1247 int status, level;
1248 struct ocfs2_lock_res *lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001249 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001250
1251 BUG_ON(!inode);
1252
1253 mlog_entry_void();
1254
Mark Fashehb0697052006-03-03 10:24:33 -08001255 mlog(0, "inode %llu take %s RW lock\n",
1256 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 write ? "EXMODE" : "PRMODE");
1258
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001259 if (ocfs2_mount_local(osb))
1260 return 0;
1261
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1263
1264 level = write ? LKM_EXMODE : LKM_PRMODE;
1265
1266 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
1267 0);
1268 if (status < 0)
1269 mlog_errno(status);
1270
1271 mlog_exit(status);
1272 return status;
1273}
1274
1275void ocfs2_rw_unlock(struct inode *inode, int write)
1276{
1277 int level = write ? LKM_EXMODE : LKM_PRMODE;
1278 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001279 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001280
1281 mlog_entry_void();
1282
Mark Fashehb0697052006-03-03 10:24:33 -08001283 mlog(0, "inode %llu drop %s RW lock\n",
1284 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001285 write ? "EXMODE" : "PRMODE");
1286
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001287 if (!ocfs2_mount_local(osb))
1288 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001289
1290 mlog_exit_void();
1291}
1292
Tiger Yang50008632007-03-20 16:01:38 -07001293/*
1294 * ocfs2_open_lock always get PR mode lock.
1295 */
1296int ocfs2_open_lock(struct inode *inode)
1297{
1298 int status = 0;
1299 struct ocfs2_lock_res *lockres;
1300 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1301
1302 BUG_ON(!inode);
1303
1304 mlog_entry_void();
1305
1306 mlog(0, "inode %llu take PRMODE open lock\n",
1307 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1308
1309 if (ocfs2_mount_local(osb))
1310 goto out;
1311
1312 lockres = &OCFS2_I(inode)->ip_open_lockres;
1313
1314 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1315 LKM_PRMODE, 0, 0);
1316 if (status < 0)
1317 mlog_errno(status);
1318
1319out:
1320 mlog_exit(status);
1321 return status;
1322}
1323
1324int ocfs2_try_open_lock(struct inode *inode, int write)
1325{
1326 int status = 0, level;
1327 struct ocfs2_lock_res *lockres;
1328 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1329
1330 BUG_ON(!inode);
1331
1332 mlog_entry_void();
1333
1334 mlog(0, "inode %llu try to take %s open lock\n",
1335 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1336 write ? "EXMODE" : "PRMODE");
1337
1338 if (ocfs2_mount_local(osb))
1339 goto out;
1340
1341 lockres = &OCFS2_I(inode)->ip_open_lockres;
1342
1343 level = write ? LKM_EXMODE : LKM_PRMODE;
1344
1345 /*
1346 * The file system may already holding a PRMODE/EXMODE open lock.
1347 * Since we pass LKM_NOQUEUE, the request won't block waiting on
1348 * other nodes and the -EAGAIN will indicate to the caller that
1349 * this inode is still in use.
1350 */
1351 status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
1352 level, LKM_NOQUEUE, 0);
1353
1354out:
1355 mlog_exit(status);
1356 return status;
1357}
1358
1359/*
1360 * ocfs2_open_unlock unlock PR and EX mode open locks.
1361 */
1362void ocfs2_open_unlock(struct inode *inode)
1363{
1364 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1365 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1366
1367 mlog_entry_void();
1368
1369 mlog(0, "inode %llu drop open lock\n",
1370 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1371
1372 if (ocfs2_mount_local(osb))
1373 goto out;
1374
1375 if(lockres->l_ro_holders)
1376 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1377 LKM_PRMODE);
1378 if(lockres->l_ex_holders)
1379 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
1380 LKM_EXMODE);
1381
1382out:
1383 mlog_exit_void();
1384}
1385
Mark Fashehcf8e06f2007-12-20 16:43:10 -08001386static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1387 int level)
1388{
1389 int ret;
1390 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1391 unsigned long flags;
1392 struct ocfs2_mask_waiter mw;
1393
1394 ocfs2_init_mask_waiter(&mw);
1395
1396retry_cancel:
1397 spin_lock_irqsave(&lockres->l_lock, flags);
1398 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1399 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1400 if (ret) {
1401 spin_unlock_irqrestore(&lockres->l_lock, flags);
1402 ret = ocfs2_cancel_convert(osb, lockres);
1403 if (ret < 0) {
1404 mlog_errno(ret);
1405 goto out;
1406 }
1407 goto retry_cancel;
1408 }
1409 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1410 spin_unlock_irqrestore(&lockres->l_lock, flags);
1411
1412 ocfs2_wait_for_mask(&mw);
1413 goto retry_cancel;
1414 }
1415
1416 ret = -ERESTARTSYS;
1417 /*
1418 * We may still have gotten the lock, in which case there's no
1419 * point to restarting the syscall.
1420 */
1421 if (lockres->l_level == level)
1422 ret = 0;
1423
1424 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1425 lockres->l_flags, lockres->l_level, lockres->l_action);
1426
1427 spin_unlock_irqrestore(&lockres->l_lock, flags);
1428
1429out:
1430 return ret;
1431}
1432
1433/*
1434 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1435 * flock() calls. The locking approach this requires is sufficiently
1436 * different from all other cluster lock types that we implement a
1437 * seperate path to the "low-level" dlm calls. In particular:
1438 *
1439 * - No optimization of lock levels is done - we take at exactly
1440 * what's been requested.
1441 *
1442 * - No lock caching is employed. We immediately downconvert to
1443 * no-lock at unlock time. This also means flock locks never go on
1444 * the blocking list).
1445 *
1446 * - Since userspace can trivially deadlock itself with flock, we make
1447 * sure to allow cancellation of a misbehaving applications flock()
1448 * request.
1449 *
1450 * - Access to any flock lockres doesn't require concurrency, so we
1451 * can simplify the code by requiring the caller to guarantee
1452 * serialization of dlmglue flock calls.
1453 */
1454int ocfs2_file_lock(struct file *file, int ex, int trylock)
1455{
1456 int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
1457 unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
1458 unsigned long flags;
1459 struct ocfs2_file_private *fp = file->private_data;
1460 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1461 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1462 struct ocfs2_mask_waiter mw;
1463
1464 ocfs2_init_mask_waiter(&mw);
1465
1466 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1467 (lockres->l_level > LKM_NLMODE)) {
1468 mlog(ML_ERROR,
1469 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1470 "level: %u\n", lockres->l_name, lockres->l_flags,
1471 lockres->l_level);
1472 return -EINVAL;
1473 }
1474
1475 spin_lock_irqsave(&lockres->l_lock, flags);
1476 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1477 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1478 spin_unlock_irqrestore(&lockres->l_lock, flags);
1479
1480 /*
1481 * Get the lock at NLMODE to start - that way we
1482 * can cancel the upconvert request if need be.
1483 */
1484 ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
1485 if (ret < 0) {
1486 mlog_errno(ret);
1487 goto out;
1488 }
1489
1490 ret = ocfs2_wait_for_mask(&mw);
1491 if (ret) {
1492 mlog_errno(ret);
1493 goto out;
1494 }
1495 spin_lock_irqsave(&lockres->l_lock, flags);
1496 }
1497
1498 lockres->l_action = OCFS2_AST_CONVERT;
1499 lkm_flags |= LKM_CONVERT;
1500 lockres->l_requested = level;
1501 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1502
1503 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1504 spin_unlock_irqrestore(&lockres->l_lock, flags);
1505
1506 ret = dlmlock(osb->dlm, level, &lockres->l_lksb, lkm_flags,
1507 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
1508 ocfs2_locking_ast, lockres, ocfs2_blocking_ast);
1509 if (ret != DLM_NORMAL) {
1510 if (trylock && ret == DLM_NOTQUEUED)
1511 ret = -EAGAIN;
1512 else {
1513 ocfs2_log_dlm_error("dlmlock", ret, lockres);
1514 ret = -EINVAL;
1515 }
1516
1517 ocfs2_recover_from_dlm_error(lockres, 1);
1518 lockres_remove_mask_waiter(lockres, &mw);
1519 goto out;
1520 }
1521
1522 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
1523 if (ret == -ERESTARTSYS) {
1524 /*
1525 * Userspace can cause deadlock itself with
1526 * flock(). Current behavior locally is to allow the
1527 * deadlock, but abort the system call if a signal is
1528 * received. We follow this example, otherwise a
1529 * poorly written program could sit in kernel until
1530 * reboot.
1531 *
1532 * Handling this is a bit more complicated for Ocfs2
1533 * though. We can't exit this function with an
1534 * outstanding lock request, so a cancel convert is
1535 * required. We intentionally overwrite 'ret' - if the
1536 * cancel fails and the lock was granted, it's easier
1537 * to just bubble sucess back up to the user.
1538 */
1539 ret = ocfs2_flock_handle_signal(lockres, level);
1540 }
1541
1542out:
1543
1544 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
1545 lockres->l_name, ex, trylock, ret);
1546 return ret;
1547}
1548
1549void ocfs2_file_unlock(struct file *file)
1550{
1551 int ret;
1552 unsigned long flags;
1553 struct ocfs2_file_private *fp = file->private_data;
1554 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1555 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1556 struct ocfs2_mask_waiter mw;
1557
1558 ocfs2_init_mask_waiter(&mw);
1559
1560 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
1561 return;
1562
1563 if (lockres->l_level == LKM_NLMODE)
1564 return;
1565
1566 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
1567 lockres->l_name, lockres->l_flags, lockres->l_level,
1568 lockres->l_action);
1569
1570 spin_lock_irqsave(&lockres->l_lock, flags);
1571 /*
1572 * Fake a blocking ast for the downconvert code.
1573 */
1574 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
1575 lockres->l_blocking = LKM_EXMODE;
1576
1577 ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
1578 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1579 spin_unlock_irqrestore(&lockres->l_lock, flags);
1580
1581 ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0);
1582 if (ret) {
1583 mlog_errno(ret);
1584 return;
1585 }
1586
1587 ret = ocfs2_wait_for_mask(&mw);
1588 if (ret)
1589 mlog_errno(ret);
1590}
1591
Mark Fasheh34d024f2007-09-24 15:56:19 -07001592static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
1593 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001594{
1595 int kick = 0;
1596
1597 mlog_entry_void();
1598
1599 /* If we know that another node is waiting on our lock, kick
Mark Fasheh34d024f2007-09-24 15:56:19 -07001600 * the downconvert thread * pre-emptively when we reach a release
Mark Fashehccd979b2005-12-15 14:31:24 -08001601 * condition. */
1602 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
1603 switch(lockres->l_blocking) {
1604 case LKM_EXMODE:
1605 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
1606 kick = 1;
1607 break;
1608 case LKM_PRMODE:
1609 if (!lockres->l_ex_holders)
1610 kick = 1;
1611 break;
1612 default:
1613 BUG();
1614 }
1615 }
1616
1617 if (kick)
Mark Fasheh34d024f2007-09-24 15:56:19 -07001618 ocfs2_wake_downconvert_thread(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001619
1620 mlog_exit_void();
1621}
1622
Mark Fashehccd979b2005-12-15 14:31:24 -08001623#define OCFS2_SEC_BITS 34
1624#define OCFS2_SEC_SHIFT (64 - 34)
1625#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
1626
1627/* LVB only has room for 64 bits of time here so we pack it for
1628 * now. */
1629static u64 ocfs2_pack_timespec(struct timespec *spec)
1630{
1631 u64 res;
1632 u64 sec = spec->tv_sec;
1633 u32 nsec = spec->tv_nsec;
1634
1635 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
1636
1637 return res;
1638}
1639
1640/* Call this with the lockres locked. I am reasonably sure we don't
1641 * need ip_lock in this function as anyone who would be changing those
Mark Fashehe63aecb62007-10-18 15:30:42 -07001642 * values is supposed to be blocked in ocfs2_inode_lock right now. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001643static void __ocfs2_stuff_meta_lvb(struct inode *inode)
1644{
1645 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001646 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001647 struct ocfs2_meta_lvb *lvb;
1648
1649 mlog_entry_void();
1650
1651 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1652
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001653 /*
1654 * Invalidate the LVB of a deleted inode - this way other
1655 * nodes are forced to go to disk and discover the new inode
1656 * status.
1657 */
1658 if (oi->ip_flags & OCFS2_INODE_DELETED) {
1659 lvb->lvb_version = 0;
1660 goto out;
1661 }
1662
Mark Fasheh4d3b83f2006-09-12 15:22:18 -07001663 lvb->lvb_version = OCFS2_LVB_VERSION;
Mark Fashehccd979b2005-12-15 14:31:24 -08001664 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
1665 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
1666 lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
1667 lvb->lvb_igid = cpu_to_be32(inode->i_gid);
1668 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
1669 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
1670 lvb->lvb_iatime_packed =
1671 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
1672 lvb->lvb_ictime_packed =
1673 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
1674 lvb->lvb_imtime_packed =
1675 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001676 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001677 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
Mark Fashehf9e2d822006-09-12 15:35:49 -07001678 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001679
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001680out:
Mark Fashehccd979b2005-12-15 14:31:24 -08001681 mlog_meta_lvb(0, lockres);
1682
1683 mlog_exit_void();
1684}
1685
1686static void ocfs2_unpack_timespec(struct timespec *spec,
1687 u64 packed_time)
1688{
1689 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
1690 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
1691}
1692
1693static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
1694{
1695 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001696 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001697 struct ocfs2_meta_lvb *lvb;
1698
1699 mlog_entry_void();
1700
1701 mlog_meta_lvb(0, lockres);
1702
1703 lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1704
1705 /* We're safe here without the lockres lock... */
1706 spin_lock(&oi->ip_lock);
1707 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
1708 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
1709
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001710 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
Mark Fasheh15b1e362007-09-07 13:58:15 -07001711 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
Herbert Poetzlca4d1472006-07-03 17:27:12 -07001712 ocfs2_set_inode_flags(inode);
1713
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 /* fast-symlinks are a special case */
1715 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
1716 inode->i_blocks = 0;
1717 else
Mark Fasheh8110b072007-03-22 16:53:23 -07001718 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001719
1720 inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
1721 inode->i_gid = be32_to_cpu(lvb->lvb_igid);
1722 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
1723 inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
1724 ocfs2_unpack_timespec(&inode->i_atime,
1725 be64_to_cpu(lvb->lvb_iatime_packed));
1726 ocfs2_unpack_timespec(&inode->i_mtime,
1727 be64_to_cpu(lvb->lvb_imtime_packed));
1728 ocfs2_unpack_timespec(&inode->i_ctime,
1729 be64_to_cpu(lvb->lvb_ictime_packed));
1730 spin_unlock(&oi->ip_lock);
1731
1732 mlog_exit_void();
1733}
1734
Mark Fashehf9e2d822006-09-12 15:35:49 -07001735static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
1736 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08001737{
1738 struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
1739
Mark Fashehf9e2d822006-09-12 15:35:49 -07001740 if (lvb->lvb_version == OCFS2_LVB_VERSION
1741 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
Mark Fashehccd979b2005-12-15 14:31:24 -08001742 return 1;
1743 return 0;
1744}
1745
1746/* Determine whether a lock resource needs to be refreshed, and
1747 * arbitrate who gets to refresh it.
1748 *
1749 * 0 means no refresh needed.
1750 *
1751 * > 0 means you need to refresh this and you MUST call
1752 * ocfs2_complete_lock_res_refresh afterwards. */
1753static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
1754{
1755 unsigned long flags;
1756 int status = 0;
1757
1758 mlog_entry_void();
1759
1760refresh_check:
1761 spin_lock_irqsave(&lockres->l_lock, flags);
1762 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
1763 spin_unlock_irqrestore(&lockres->l_lock, flags);
1764 goto bail;
1765 }
1766
1767 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
1768 spin_unlock_irqrestore(&lockres->l_lock, flags);
1769
1770 ocfs2_wait_on_refreshing_lock(lockres);
1771 goto refresh_check;
1772 }
1773
1774 /* Ok, I'll be the one to refresh this lock. */
1775 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
1776 spin_unlock_irqrestore(&lockres->l_lock, flags);
1777
1778 status = 1;
1779bail:
1780 mlog_exit(status);
1781 return status;
1782}
1783
1784/* If status is non zero, I'll mark it as not being in refresh
1785 * anymroe, but i won't clear the needs refresh flag. */
1786static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
1787 int status)
1788{
1789 unsigned long flags;
1790 mlog_entry_void();
1791
1792 spin_lock_irqsave(&lockres->l_lock, flags);
1793 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
1794 if (!status)
1795 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
1796 spin_unlock_irqrestore(&lockres->l_lock, flags);
1797
1798 wake_up(&lockres->l_event);
1799
1800 mlog_exit_void();
1801}
1802
1803/* may or may not return a bh if it went to disk. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001804static int ocfs2_inode_lock_update(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001805 struct buffer_head **bh)
1806{
1807 int status = 0;
1808 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehe63aecb62007-10-18 15:30:42 -07001809 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001810 struct ocfs2_dinode *fe;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001811 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001812
1813 mlog_entry_void();
1814
Mark Fashehbe9e9862007-04-18 15:22:08 -07001815 if (ocfs2_mount_local(osb))
1816 goto bail;
1817
Mark Fashehccd979b2005-12-15 14:31:24 -08001818 spin_lock(&oi->ip_lock);
1819 if (oi->ip_flags & OCFS2_INODE_DELETED) {
Mark Fashehb0697052006-03-03 10:24:33 -08001820 mlog(0, "Orphaned inode %llu was deleted while we "
Mark Fashehccd979b2005-12-15 14:31:24 -08001821 "were waiting on a lock. ip_flags = 0x%x\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001822 (unsigned long long)oi->ip_blkno, oi->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -08001823 spin_unlock(&oi->ip_lock);
1824 status = -ENOENT;
1825 goto bail;
1826 }
1827 spin_unlock(&oi->ip_lock);
1828
Mark Fashehbe9e9862007-04-18 15:22:08 -07001829 if (!ocfs2_should_refresh_lock_res(lockres))
1830 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001831
1832 /* This will discard any caching information we might have had
1833 * for the inode metadata. */
1834 ocfs2_metadata_cache_purge(inode);
1835
Mark Fasheh83418972007-04-23 18:53:12 -07001836 ocfs2_extent_map_trunc(inode, 0);
1837
Mark Fashehbe9e9862007-04-18 15:22:08 -07001838 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
Mark Fashehb0697052006-03-03 10:24:33 -08001839 mlog(0, "Trusting LVB on inode %llu\n",
1840 (unsigned long long)oi->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001841 ocfs2_refresh_inode_from_lvb(inode);
1842 } else {
1843 /* Boo, we have to go to disk. */
1844 /* read bh, cast, ocfs2_refresh_inode */
1845 status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
1846 bh, OCFS2_BH_CACHED, inode);
1847 if (status < 0) {
1848 mlog_errno(status);
1849 goto bail_refresh;
1850 }
1851 fe = (struct ocfs2_dinode *) (*bh)->b_data;
1852
1853 /* This is a good chance to make sure we're not
1854 * locking an invalid object.
1855 *
1856 * We bug on a stale inode here because we checked
1857 * above whether it was wiped from disk. The wiping
1858 * node provides a guarantee that we receive that
1859 * message and can mark the inode before dropping any
1860 * locks associated with it. */
1861 if (!OCFS2_IS_VALID_DINODE(fe)) {
1862 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
1863 status = -EIO;
1864 goto bail_refresh;
1865 }
1866 mlog_bug_on_msg(inode->i_generation !=
1867 le32_to_cpu(fe->i_generation),
Mark Fashehb0697052006-03-03 10:24:33 -08001868 "Invalid dinode %llu disk generation: %u "
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 "inode->i_generation: %u\n",
Mark Fashehb0697052006-03-03 10:24:33 -08001870 (unsigned long long)oi->ip_blkno,
1871 le32_to_cpu(fe->i_generation),
Mark Fashehccd979b2005-12-15 14:31:24 -08001872 inode->i_generation);
1873 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
1874 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
Mark Fashehb0697052006-03-03 10:24:33 -08001875 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
1876 (unsigned long long)oi->ip_blkno,
1877 (unsigned long long)le64_to_cpu(fe->i_dtime),
Mark Fashehccd979b2005-12-15 14:31:24 -08001878 le32_to_cpu(fe->i_flags));
1879
1880 ocfs2_refresh_inode(inode, fe);
1881 }
1882
1883 status = 0;
1884bail_refresh:
Mark Fashehbe9e9862007-04-18 15:22:08 -07001885 ocfs2_complete_lock_res_refresh(lockres, status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001886bail:
1887 mlog_exit(status);
1888 return status;
1889}
1890
1891static int ocfs2_assign_bh(struct inode *inode,
1892 struct buffer_head **ret_bh,
1893 struct buffer_head *passed_bh)
1894{
1895 int status;
1896
1897 if (passed_bh) {
1898 /* Ok, the update went to disk for us, use the
1899 * returned bh. */
1900 *ret_bh = passed_bh;
1901 get_bh(*ret_bh);
1902
1903 return 0;
1904 }
1905
1906 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1907 OCFS2_I(inode)->ip_blkno,
1908 ret_bh,
1909 OCFS2_BH_CACHED,
1910 inode);
1911 if (status < 0)
1912 mlog_errno(status);
1913
1914 return status;
1915}
1916
1917/*
1918 * returns < 0 error if the callback will never be called, otherwise
1919 * the result of the lock will be communicated via the callback.
1920 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001921int ocfs2_inode_lock_full(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08001922 struct buffer_head **ret_bh,
1923 int ex,
1924 int arg_flags)
1925{
1926 int status, level, dlm_flags, acquired;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001927 struct ocfs2_lock_res *lockres = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1929 struct buffer_head *local_bh = NULL;
1930
1931 BUG_ON(!inode);
1932
1933 mlog_entry_void();
1934
Mark Fashehb0697052006-03-03 10:24:33 -08001935 mlog(0, "inode %llu, take %s META lock\n",
1936 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08001937 ex ? "EXMODE" : "PRMODE");
1938
1939 status = 0;
1940 acquired = 0;
1941 /* We'll allow faking a readonly metadata lock for
1942 * rodevices. */
1943 if (ocfs2_is_hard_readonly(osb)) {
1944 if (ex)
1945 status = -EROFS;
1946 goto bail;
1947 }
1948
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001949 if (ocfs2_mount_local(osb))
1950 goto local;
1951
Mark Fashehccd979b2005-12-15 14:31:24 -08001952 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1953 wait_event(osb->recovery_event,
1954 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1955
Mark Fashehe63aecb62007-10-18 15:30:42 -07001956 lockres = &OCFS2_I(inode)->ip_inode_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08001957 level = ex ? LKM_EXMODE : LKM_PRMODE;
1958 dlm_flags = 0;
1959 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
1960 dlm_flags |= LKM_NOQUEUE;
1961
1962 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
1963 if (status < 0) {
1964 if (status != -EAGAIN && status != -EIOCBRETRY)
1965 mlog_errno(status);
1966 goto bail;
1967 }
1968
1969 /* Notify the error cleanup path to drop the cluster lock. */
1970 acquired = 1;
1971
1972 /* We wait twice because a node may have died while we were in
1973 * the lower dlm layers. The second time though, we've
1974 * committed to owning this lock so we don't allow signals to
1975 * abort the operation. */
1976 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
1977 wait_event(osb->recovery_event,
1978 ocfs2_node_map_is_empty(osb, &osb->recovery_map));
1979
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001980local:
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001981 /*
1982 * We only see this flag if we're being called from
1983 * ocfs2_read_locked_inode(). It means we're locking an inode
1984 * which hasn't been populated yet, so clear the refresh flag
1985 * and let the caller handle it.
1986 */
1987 if (inode->i_state & I_NEW) {
1988 status = 0;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08001989 if (lockres)
1990 ocfs2_complete_lock_res_refresh(lockres, 0);
Mark Fasheh24c19ef2006-09-22 17:28:19 -07001991 goto bail;
1992 }
1993
Mark Fashehccd979b2005-12-15 14:31:24 -08001994 /* This is fun. The caller may want a bh back, or it may
Mark Fashehe63aecb62007-10-18 15:30:42 -07001995 * not. ocfs2_inode_lock_update definitely wants one in, but
Mark Fashehccd979b2005-12-15 14:31:24 -08001996 * may or may not read one, depending on what's in the
1997 * LVB. The result of all of this is that we've *only* gone to
1998 * disk if we have to, so the complexity is worthwhile. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001999 status = ocfs2_inode_lock_update(inode, &local_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002000 if (status < 0) {
2001 if (status != -ENOENT)
2002 mlog_errno(status);
2003 goto bail;
2004 }
2005
2006 if (ret_bh) {
2007 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2008 if (status < 0) {
2009 mlog_errno(status);
2010 goto bail;
2011 }
2012 }
2013
Mark Fashehccd979b2005-12-15 14:31:24 -08002014bail:
2015 if (status < 0) {
2016 if (ret_bh && (*ret_bh)) {
2017 brelse(*ret_bh);
2018 *ret_bh = NULL;
2019 }
2020 if (acquired)
Mark Fashehe63aecb62007-10-18 15:30:42 -07002021 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 }
2023
2024 if (local_bh)
2025 brelse(local_bh);
2026
2027 mlog_exit(status);
2028 return status;
2029}
2030
2031/*
Mark Fasheh34d024f2007-09-24 15:56:19 -07002032 * This is working around a lock inversion between tasks acquiring DLM
2033 * locks while holding a page lock and the downconvert thread which
2034 * blocks dlm lock acquiry while acquiring page locks.
Mark Fashehccd979b2005-12-15 14:31:24 -08002035 *
2036 * ** These _with_page variantes are only intended to be called from aop
2037 * methods that hold page locks and return a very specific *positive* error
2038 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2039 *
Mark Fasheh34d024f2007-09-24 15:56:19 -07002040 * The DLM is called such that it returns -EAGAIN if it would have
2041 * blocked waiting for the downconvert thread. In that case we unlock
2042 * our page so the downconvert thread can make progress. Once we've
2043 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2044 * that called us can bubble that back up into the VFS who will then
2045 * immediately retry the aop call.
Mark Fashehccd979b2005-12-15 14:31:24 -08002046 *
2047 * We do a blocking lock and immediate unlock before returning, though, so that
2048 * the lock has a great chance of being cached on this node by the time the VFS
2049 * calls back to retry the aop. This has a potential to livelock as nodes
2050 * ping locks back and forth, but that's a risk we're willing to take to avoid
2051 * the lock inversion simply.
2052 */
Mark Fashehe63aecb62007-10-18 15:30:42 -07002053int ocfs2_inode_lock_with_page(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002054 struct buffer_head **ret_bh,
2055 int ex,
2056 struct page *page)
2057{
2058 int ret;
2059
Mark Fashehe63aecb62007-10-18 15:30:42 -07002060 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
Mark Fashehccd979b2005-12-15 14:31:24 -08002061 if (ret == -EAGAIN) {
2062 unlock_page(page);
Mark Fashehe63aecb62007-10-18 15:30:42 -07002063 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2064 ocfs2_inode_unlock(inode, ex);
Mark Fashehccd979b2005-12-15 14:31:24 -08002065 ret = AOP_TRUNCATED_PAGE;
2066 }
2067
2068 return ret;
2069}
2070
Mark Fashehe63aecb62007-10-18 15:30:42 -07002071int ocfs2_inode_lock_atime(struct inode *inode,
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002072 struct vfsmount *vfsmnt,
2073 int *level)
2074{
2075 int ret;
2076
2077 mlog_entry_void();
Mark Fashehe63aecb62007-10-18 15:30:42 -07002078 ret = ocfs2_inode_lock(inode, NULL, 0);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002079 if (ret < 0) {
2080 mlog_errno(ret);
2081 return ret;
2082 }
2083
2084 /*
2085 * If we should update atime, we will get EX lock,
2086 * otherwise we just get PR lock.
2087 */
2088 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2089 struct buffer_head *bh = NULL;
2090
Mark Fashehe63aecb62007-10-18 15:30:42 -07002091 ocfs2_inode_unlock(inode, 0);
2092 ret = ocfs2_inode_lock(inode, &bh, 1);
Tiger Yang7f1a37e2006-11-15 15:48:42 +08002093 if (ret < 0) {
2094 mlog_errno(ret);
2095 return ret;
2096 }
2097 *level = 1;
2098 if (ocfs2_should_update_atime(inode, vfsmnt))
2099 ocfs2_update_inode_atime(inode, bh);
2100 if (bh)
2101 brelse(bh);
2102 } else
2103 *level = 0;
2104
2105 mlog_exit(ret);
2106 return ret;
2107}
2108
Mark Fashehe63aecb62007-10-18 15:30:42 -07002109void ocfs2_inode_unlock(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08002110 int ex)
2111{
2112 int level = ex ? LKM_EXMODE : LKM_PRMODE;
Mark Fashehe63aecb62007-10-18 15:30:42 -07002113 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002114 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002115
2116 mlog_entry_void();
2117
Mark Fashehb0697052006-03-03 10:24:33 -08002118 mlog(0, "inode %llu drop %s META lock\n",
2119 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08002120 ex ? "EXMODE" : "PRMODE");
2121
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002122 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2123 !ocfs2_mount_local(osb))
Mark Fashehccd979b2005-12-15 14:31:24 -08002124 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2125
2126 mlog_exit_void();
2127}
2128
2129int ocfs2_super_lock(struct ocfs2_super *osb,
2130 int ex)
2131{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002132 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002133 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2134 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
Mark Fashehccd979b2005-12-15 14:31:24 -08002135
2136 mlog_entry_void();
2137
2138 if (ocfs2_is_hard_readonly(osb))
2139 return -EROFS;
2140
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002141 if (ocfs2_mount_local(osb))
2142 goto bail;
2143
Mark Fashehccd979b2005-12-15 14:31:24 -08002144 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2145 if (status < 0) {
2146 mlog_errno(status);
2147 goto bail;
2148 }
2149
2150 /* The super block lock path is really in the best position to
2151 * know when resources covered by the lock need to be
2152 * refreshed, so we do it here. Of course, making sense of
2153 * everything is up to the caller :) */
2154 status = ocfs2_should_refresh_lock_res(lockres);
2155 if (status < 0) {
2156 mlog_errno(status);
2157 goto bail;
2158 }
2159 if (status) {
Mark Fasheh8e8a4602008-02-01 11:59:09 -08002160 status = ocfs2_refresh_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002161
2162 ocfs2_complete_lock_res_refresh(lockres, status);
2163
2164 if (status < 0)
2165 mlog_errno(status);
2166 }
2167bail:
2168 mlog_exit(status);
2169 return status;
2170}
2171
2172void ocfs2_super_unlock(struct ocfs2_super *osb,
2173 int ex)
2174{
2175 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2176 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2177
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002178 if (!ocfs2_mount_local(osb))
2179 ocfs2_cluster_unlock(osb, lockres, level);
Mark Fashehccd979b2005-12-15 14:31:24 -08002180}
2181
2182int ocfs2_rename_lock(struct ocfs2_super *osb)
2183{
2184 int status;
2185 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2186
2187 if (ocfs2_is_hard_readonly(osb))
2188 return -EROFS;
2189
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002190 if (ocfs2_mount_local(osb))
2191 return 0;
2192
Mark Fashehccd979b2005-12-15 14:31:24 -08002193 status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
2194 if (status < 0)
2195 mlog_errno(status);
2196
2197 return status;
2198}
2199
2200void ocfs2_rename_unlock(struct ocfs2_super *osb)
2201{
2202 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2203
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002204 if (!ocfs2_mount_local(osb))
2205 ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002206}
2207
Mark Fashehd680efe2006-09-08 14:14:34 -07002208int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2209{
2210 int ret;
2211 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2212 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2213 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2214
2215 BUG_ON(!dl);
2216
2217 if (ocfs2_is_hard_readonly(osb))
2218 return -EROFS;
2219
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002220 if (ocfs2_mount_local(osb))
2221 return 0;
2222
Mark Fashehd680efe2006-09-08 14:14:34 -07002223 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2224 if (ret < 0)
2225 mlog_errno(ret);
2226
2227 return ret;
2228}
2229
2230void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2231{
2232 int level = ex ? LKM_EXMODE : LKM_PRMODE;
2233 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2234 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2235
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002236 if (!ocfs2_mount_local(osb))
2237 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
Mark Fashehd680efe2006-09-08 14:14:34 -07002238}
2239
Mark Fashehccd979b2005-12-15 14:31:24 -08002240/* Reference counting of the dlm debug structure. We want this because
2241 * open references on the debug inodes can live on after a mount, so
2242 * we can't rely on the ocfs2_super to always exist. */
2243static void ocfs2_dlm_debug_free(struct kref *kref)
2244{
2245 struct ocfs2_dlm_debug *dlm_debug;
2246
2247 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2248
2249 kfree(dlm_debug);
2250}
2251
2252void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2253{
2254 if (dlm_debug)
2255 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2256}
2257
2258static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2259{
2260 kref_get(&debug->d_refcnt);
2261}
2262
2263struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2264{
2265 struct ocfs2_dlm_debug *dlm_debug;
2266
2267 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2268 if (!dlm_debug) {
2269 mlog_errno(-ENOMEM);
2270 goto out;
2271 }
2272
2273 kref_init(&dlm_debug->d_refcnt);
2274 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2275 dlm_debug->d_locking_state = NULL;
2276out:
2277 return dlm_debug;
2278}
2279
2280/* Access to this is arbitrated for us via seq_file->sem. */
2281struct ocfs2_dlm_seq_priv {
2282 struct ocfs2_dlm_debug *p_dlm_debug;
2283 struct ocfs2_lock_res p_iter_res;
2284 struct ocfs2_lock_res p_tmp_res;
2285};
2286
2287static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2288 struct ocfs2_dlm_seq_priv *priv)
2289{
2290 struct ocfs2_lock_res *iter, *ret = NULL;
2291 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2292
2293 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2294
2295 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2296 /* discover the head of the list */
2297 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2298 mlog(0, "End of list found, %p\n", ret);
2299 break;
2300 }
2301
2302 /* We track our "dummy" iteration lockres' by a NULL
2303 * l_ops field. */
2304 if (iter->l_ops != NULL) {
2305 ret = iter;
2306 break;
2307 }
2308 }
2309
2310 return ret;
2311}
2312
2313static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2314{
2315 struct ocfs2_dlm_seq_priv *priv = m->private;
2316 struct ocfs2_lock_res *iter;
2317
2318 spin_lock(&ocfs2_dlm_tracking_lock);
2319 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2320 if (iter) {
2321 /* Since lockres' have the lifetime of their container
2322 * (which can be inodes, ocfs2_supers, etc) we want to
2323 * copy this out to a temporary lockres while still
2324 * under the spinlock. Obviously after this we can't
2325 * trust any pointers on the copy returned, but that's
2326 * ok as the information we want isn't typically held
2327 * in them. */
2328 priv->p_tmp_res = *iter;
2329 iter = &priv->p_tmp_res;
2330 }
2331 spin_unlock(&ocfs2_dlm_tracking_lock);
2332
2333 return iter;
2334}
2335
2336static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2337{
2338}
2339
2340static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2341{
2342 struct ocfs2_dlm_seq_priv *priv = m->private;
2343 struct ocfs2_lock_res *iter = v;
2344 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2345
2346 spin_lock(&ocfs2_dlm_tracking_lock);
2347 iter = ocfs2_dlm_next_res(iter, priv);
2348 list_del_init(&dummy->l_debug_list);
2349 if (iter) {
2350 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2351 priv->p_tmp_res = *iter;
2352 iter = &priv->p_tmp_res;
2353 }
2354 spin_unlock(&ocfs2_dlm_tracking_lock);
2355
2356 return iter;
2357}
2358
2359/* So that debugfs.ocfs2 can determine which format is being used */
2360#define OCFS2_DLM_DEBUG_STR_VERSION 1
2361static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2362{
2363 int i;
2364 char *lvb;
2365 struct ocfs2_lock_res *lockres = v;
2366
2367 if (!lockres)
2368 return -EINVAL;
2369
Mark Fashehd680efe2006-09-08 14:14:34 -07002370 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2371
2372 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2373 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2374 lockres->l_name,
2375 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2376 else
2377 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2378
2379 seq_printf(m, "%d\t"
Mark Fashehccd979b2005-12-15 14:31:24 -08002380 "0x%lx\t"
2381 "0x%x\t"
2382 "0x%x\t"
2383 "%u\t"
2384 "%u\t"
2385 "%d\t"
2386 "%d\t",
Mark Fashehccd979b2005-12-15 14:31:24 -08002387 lockres->l_level,
2388 lockres->l_flags,
2389 lockres->l_action,
2390 lockres->l_unlock_action,
2391 lockres->l_ro_holders,
2392 lockres->l_ex_holders,
2393 lockres->l_requested,
2394 lockres->l_blocking);
2395
2396 /* Dump the raw LVB */
2397 lvb = lockres->l_lksb.lvb;
2398 for(i = 0; i < DLM_LVB_LEN; i++)
2399 seq_printf(m, "0x%x\t", lvb[i]);
2400
2401 /* End the line */
2402 seq_printf(m, "\n");
2403 return 0;
2404}
2405
Jan Engelhardt90d99772008-01-22 20:52:20 +01002406static const struct seq_operations ocfs2_dlm_seq_ops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002407 .start = ocfs2_dlm_seq_start,
2408 .stop = ocfs2_dlm_seq_stop,
2409 .next = ocfs2_dlm_seq_next,
2410 .show = ocfs2_dlm_seq_show,
2411};
2412
2413static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2414{
2415 struct seq_file *seq = (struct seq_file *) file->private_data;
2416 struct ocfs2_dlm_seq_priv *priv = seq->private;
2417 struct ocfs2_lock_res *res = &priv->p_iter_res;
2418
2419 ocfs2_remove_lockres_tracking(res);
2420 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2421 return seq_release_private(inode, file);
2422}
2423
2424static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2425{
2426 int ret;
2427 struct ocfs2_dlm_seq_priv *priv;
2428 struct seq_file *seq;
2429 struct ocfs2_super *osb;
2430
2431 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2432 if (!priv) {
2433 ret = -ENOMEM;
2434 mlog_errno(ret);
2435 goto out;
2436 }
Theodore Ts'o8e18e292006-09-27 01:50:46 -07002437 osb = inode->i_private;
Mark Fashehccd979b2005-12-15 14:31:24 -08002438 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2439 priv->p_dlm_debug = osb->osb_dlm_debug;
2440 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2441
2442 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2443 if (ret) {
2444 kfree(priv);
2445 mlog_errno(ret);
2446 goto out;
2447 }
2448
2449 seq = (struct seq_file *) file->private_data;
2450 seq->private = priv;
2451
2452 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2453 priv->p_dlm_debug);
2454
2455out:
2456 return ret;
2457}
2458
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002459static const struct file_operations ocfs2_dlm_debug_fops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08002460 .open = ocfs2_dlm_debug_open,
2461 .release = ocfs2_dlm_debug_release,
2462 .read = seq_read,
2463 .llseek = seq_lseek,
2464};
2465
2466static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2467{
2468 int ret = 0;
2469 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2470
2471 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2472 S_IFREG|S_IRUSR,
2473 osb->osb_debug_root,
2474 osb,
2475 &ocfs2_dlm_debug_fops);
2476 if (!dlm_debug->d_locking_state) {
2477 ret = -EINVAL;
2478 mlog(ML_ERROR,
2479 "Unable to create locking state debugfs file.\n");
2480 goto out;
2481 }
2482
2483 ocfs2_get_dlm_debug(dlm_debug);
2484out:
2485 return ret;
2486}
2487
2488static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2489{
2490 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2491
2492 if (dlm_debug) {
2493 debugfs_remove(dlm_debug->d_locking_state);
2494 ocfs2_put_dlm_debug(dlm_debug);
2495 }
2496}
2497
2498int ocfs2_dlm_init(struct ocfs2_super *osb)
2499{
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002500 int status = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002501 u32 dlm_key;
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002502 struct dlm_ctxt *dlm = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002503
2504 mlog_entry_void();
2505
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002506 if (ocfs2_mount_local(osb))
2507 goto local;
2508
Mark Fashehccd979b2005-12-15 14:31:24 -08002509 status = ocfs2_dlm_init_debug(osb);
2510 if (status < 0) {
2511 mlog_errno(status);
2512 goto bail;
2513 }
2514
Mark Fasheh34d024f2007-09-24 15:56:19 -07002515 /* launch downconvert thread */
2516 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2517 if (IS_ERR(osb->dc_task)) {
2518 status = PTR_ERR(osb->dc_task);
2519 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002520 mlog_errno(status);
2521 goto bail;
2522 }
2523
2524 /* used by the dlm code to make message headers unique, each
2525 * node in this domain must agree on this. */
2526 dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
2527
2528 /* for now, uuid == domain */
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002529 dlm = dlm_register_domain(osb->uuid_str, dlm_key,
2530 &osb->osb_locking_proto);
Mark Fashehccd979b2005-12-15 14:31:24 -08002531 if (IS_ERR(dlm)) {
2532 status = PTR_ERR(dlm);
2533 mlog_errno(status);
2534 goto bail;
2535 }
2536
Sunil Mushranc271c5c2006-12-05 17:56:35 -08002537 dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
2538
2539local:
Mark Fashehccd979b2005-12-15 14:31:24 -08002540 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2541 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
2542
Mark Fashehccd979b2005-12-15 14:31:24 -08002543 osb->dlm = dlm;
2544
2545 status = 0;
2546bail:
2547 if (status < 0) {
2548 ocfs2_dlm_shutdown_debug(osb);
Mark Fasheh34d024f2007-09-24 15:56:19 -07002549 if (osb->dc_task)
2550 kthread_stop(osb->dc_task);
Mark Fashehccd979b2005-12-15 14:31:24 -08002551 }
2552
2553 mlog_exit(status);
2554 return status;
2555}
2556
2557void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
2558{
2559 mlog_entry_void();
2560
2561 dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
2562
2563 ocfs2_drop_osb_locks(osb);
2564
Mark Fasheh34d024f2007-09-24 15:56:19 -07002565 if (osb->dc_task) {
2566 kthread_stop(osb->dc_task);
2567 osb->dc_task = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002568 }
2569
2570 ocfs2_lock_res_free(&osb->osb_super_lockres);
2571 ocfs2_lock_res_free(&osb->osb_rename_lockres);
2572
2573 dlm_unregister_domain(osb->dlm);
2574 osb->dlm = NULL;
2575
2576 ocfs2_dlm_shutdown_debug(osb);
2577
2578 mlog_exit_void();
2579}
2580
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002581static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002582{
2583 struct ocfs2_lock_res *lockres = opaque;
2584 unsigned long flags;
2585
2586 mlog_entry_void();
2587
2588 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2589 lockres->l_unlock_action);
2590
2591 spin_lock_irqsave(&lockres->l_lock, flags);
2592 /* We tried to cancel a convert request, but it was already
2593 * granted. All we want to do here is clear our unlock
2594 * state. The wake_up call done at the bottom is redundant
2595 * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
2596 * hurt anything anyway */
2597 if (status == DLM_CANCELGRANT &&
2598 lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2599 mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
2600
2601 /* We don't clear the busy flag in this case as it
2602 * should have been cleared by the ast which the dlm
2603 * has called. */
2604 goto complete_unlock;
2605 }
2606
2607 if (status != DLM_NORMAL) {
2608 mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
2609 "unlock_action %d\n", status, lockres->l_name,
2610 lockres->l_unlock_action);
2611 spin_unlock_irqrestore(&lockres->l_lock, flags);
2612 return;
2613 }
2614
2615 switch(lockres->l_unlock_action) {
2616 case OCFS2_UNLOCK_CANCEL_CONVERT:
2617 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2618 lockres->l_action = OCFS2_AST_INVALID;
2619 break;
2620 case OCFS2_UNLOCK_DROP_LOCK:
2621 lockres->l_level = LKM_IVMODE;
2622 break;
2623 default:
2624 BUG();
2625 }
2626
2627 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
2628complete_unlock:
2629 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
2630 spin_unlock_irqrestore(&lockres->l_lock, flags);
2631
2632 wake_up(&lockres->l_event);
2633
2634 mlog_exit_void();
2635}
2636
Mark Fashehccd979b2005-12-15 14:31:24 -08002637static int ocfs2_drop_lock(struct ocfs2_super *osb,
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002638 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08002639{
2640 enum dlm_status status;
2641 unsigned long flags;
Mark Fashehb80fc012006-09-12 22:08:14 -07002642 int lkm_flags = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002643
2644 /* We didn't get anywhere near actually using this lockres. */
2645 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2646 goto out;
2647
Mark Fashehb80fc012006-09-12 22:08:14 -07002648 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
2649 lkm_flags |= LKM_VALBLK;
2650
Mark Fashehccd979b2005-12-15 14:31:24 -08002651 spin_lock_irqsave(&lockres->l_lock, flags);
2652
2653 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
2654 "lockres %s, flags 0x%lx\n",
2655 lockres->l_name, lockres->l_flags);
2656
2657 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
2658 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
2659 "%u, unlock_action = %u\n",
2660 lockres->l_name, lockres->l_flags, lockres->l_action,
2661 lockres->l_unlock_action);
2662
2663 spin_unlock_irqrestore(&lockres->l_lock, flags);
2664
2665 /* XXX: Today we just wait on any busy
2666 * locks... Perhaps we need to cancel converts in the
2667 * future? */
2668 ocfs2_wait_on_busy_lock(lockres);
2669
2670 spin_lock_irqsave(&lockres->l_lock, flags);
2671 }
2672
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002673 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
2674 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
2675 lockres->l_level == LKM_EXMODE &&
2676 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
2677 lockres->l_ops->set_lvb(lockres);
2678 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002679
2680 if (lockres->l_flags & OCFS2_LOCK_BUSY)
2681 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
2682 lockres->l_name);
2683 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2684 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
2685
2686 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
2687 spin_unlock_irqrestore(&lockres->l_lock, flags);
2688 goto out;
2689 }
2690
2691 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
2692
2693 /* make sure we never get here while waiting for an ast to
2694 * fire. */
2695 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
2696
2697 /* is this necessary? */
2698 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2699 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
2700 spin_unlock_irqrestore(&lockres->l_lock, flags);
2701
2702 mlog(0, "lock %s\n", lockres->l_name);
2703
Mark Fashehb80fc012006-09-12 22:08:14 -07002704 status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002705 ocfs2_unlock_ast, lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002706 if (status != DLM_NORMAL) {
2707 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2708 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
2709 dlm_print_one_lock(lockres->l_lksb.lockid);
2710 BUG();
2711 }
2712 mlog(0, "lock %s, successfull return from dlmunlock\n",
2713 lockres->l_name);
2714
2715 ocfs2_wait_on_busy_lock(lockres);
2716out:
2717 mlog_exit(0);
2718 return 0;
2719}
2720
2721/* Mark the lockres as being dropped. It will no longer be
2722 * queued if blocking, but we still may have to wait on it
Mark Fasheh34d024f2007-09-24 15:56:19 -07002723 * being dequeued from the downconvert thread before we can consider
Mark Fashehccd979b2005-12-15 14:31:24 -08002724 * it safe to drop.
2725 *
2726 * You can *not* attempt to call cluster_lock on this lockres anymore. */
2727void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
2728{
2729 int status;
2730 struct ocfs2_mask_waiter mw;
2731 unsigned long flags;
2732
2733 ocfs2_init_mask_waiter(&mw);
2734
2735 spin_lock_irqsave(&lockres->l_lock, flags);
2736 lockres->l_flags |= OCFS2_LOCK_FREEING;
2737 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
2738 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
2739 spin_unlock_irqrestore(&lockres->l_lock, flags);
2740
2741 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
2742
2743 status = ocfs2_wait_for_mask(&mw);
2744 if (status)
2745 mlog_errno(status);
2746
2747 spin_lock_irqsave(&lockres->l_lock, flags);
2748 }
2749 spin_unlock_irqrestore(&lockres->l_lock, flags);
2750}
2751
Mark Fashehd680efe2006-09-08 14:14:34 -07002752void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
2753 struct ocfs2_lock_res *lockres)
2754{
2755 int ret;
2756
2757 ocfs2_mark_lockres_freeing(lockres);
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002758 ret = ocfs2_drop_lock(osb, lockres);
Mark Fashehd680efe2006-09-08 14:14:34 -07002759 if (ret)
2760 mlog_errno(ret);
2761}
2762
Mark Fashehccd979b2005-12-15 14:31:24 -08002763static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
2764{
Mark Fashehd680efe2006-09-08 14:14:34 -07002765 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
2766 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002767}
2768
Mark Fashehccd979b2005-12-15 14:31:24 -08002769int ocfs2_drop_inode_locks(struct inode *inode)
2770{
2771 int status, err;
Mark Fashehccd979b2005-12-15 14:31:24 -08002772
2773 mlog_entry_void();
2774
2775 /* No need to call ocfs2_mark_lockres_freeing here -
2776 * ocfs2_clear_inode has done it for us. */
2777
2778 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Tiger Yang50008632007-03-20 16:01:38 -07002779 &OCFS2_I(inode)->ip_open_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002780 if (err < 0)
2781 mlog_errno(err);
2782
2783 status = err;
2784
2785 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fashehe63aecb62007-10-18 15:30:42 -07002786 &OCFS2_I(inode)->ip_inode_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002787 if (err < 0)
2788 mlog_errno(err);
2789 if (err < 0 && !status)
2790 status = err;
2791
2792 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
Mark Fasheh0d5dc6c2006-09-14 14:44:51 -07002793 &OCFS2_I(inode)->ip_rw_lockres);
Mark Fashehccd979b2005-12-15 14:31:24 -08002794 if (err < 0)
2795 mlog_errno(err);
2796 if (err < 0 && !status)
2797 status = err;
2798
2799 mlog_exit(status);
2800 return status;
2801}
2802
2803static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
2804 int new_level)
2805{
2806 assert_spin_locked(&lockres->l_lock);
2807
2808 BUG_ON(lockres->l_blocking <= LKM_NLMODE);
2809
2810 if (lockres->l_level <= new_level) {
2811 mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
2812 lockres->l_level, new_level);
2813 BUG();
2814 }
2815
2816 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
2817 lockres->l_name, new_level, lockres->l_blocking);
2818
2819 lockres->l_action = OCFS2_AST_DOWNCONVERT;
2820 lockres->l_requested = new_level;
2821 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2822}
2823
2824static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
2825 struct ocfs2_lock_res *lockres,
2826 int new_level,
2827 int lvb)
2828{
2829 int ret, dlm_flags = LKM_CONVERT;
2830 enum dlm_status status;
2831
2832 mlog_entry_void();
2833
2834 if (lvb)
2835 dlm_flags |= LKM_VALBLK;
2836
2837 status = dlmlock(osb->dlm,
2838 new_level,
2839 &lockres->l_lksb,
2840 dlm_flags,
2841 lockres->l_name,
Mark Fashehf0681062006-09-08 11:40:10 -07002842 OCFS2_LOCK_ID_MAX_LEN - 1,
Mark Fashehe92d57d2006-09-12 21:34:35 -07002843 ocfs2_locking_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002844 lockres,
Mark Fashehaa2623a2006-09-12 21:58:23 -07002845 ocfs2_blocking_ast);
Mark Fashehccd979b2005-12-15 14:31:24 -08002846 if (status != DLM_NORMAL) {
2847 ocfs2_log_dlm_error("dlmlock", status, lockres);
2848 ret = -EINVAL;
2849 ocfs2_recover_from_dlm_error(lockres, 1);
2850 goto bail;
2851 }
2852
2853 ret = 0;
2854bail:
2855 mlog_exit(ret);
2856 return ret;
2857}
2858
2859/* returns 1 when the caller should unlock and call dlmunlock */
2860static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
2861 struct ocfs2_lock_res *lockres)
2862{
2863 assert_spin_locked(&lockres->l_lock);
2864
2865 mlog_entry_void();
2866 mlog(0, "lock %s\n", lockres->l_name);
2867
2868 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
2869 /* If we're already trying to cancel a lock conversion
2870 * then just drop the spinlock and allow the caller to
2871 * requeue this lock. */
2872
2873 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
2874 return 0;
2875 }
2876
2877 /* were we in a convert when we got the bast fire? */
2878 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
2879 lockres->l_action != OCFS2_AST_DOWNCONVERT);
2880 /* set things up for the unlockast to know to just
2881 * clear out the ast_action and unset busy, etc. */
2882 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
2883
2884 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
2885 "lock %s, invalid flags: 0x%lx\n",
2886 lockres->l_name, lockres->l_flags);
2887
2888 return 1;
2889}
2890
2891static int ocfs2_cancel_convert(struct ocfs2_super *osb,
2892 struct ocfs2_lock_res *lockres)
2893{
2894 int ret;
2895 enum dlm_status status;
2896
2897 mlog_entry_void();
2898 mlog(0, "lock %s\n", lockres->l_name);
2899
2900 ret = 0;
2901 status = dlmunlock(osb->dlm,
2902 &lockres->l_lksb,
2903 LKM_CANCEL,
Mark Fasheh2a45f2d2006-09-12 21:36:58 -07002904 ocfs2_unlock_ast,
Mark Fashehccd979b2005-12-15 14:31:24 -08002905 lockres);
2906 if (status != DLM_NORMAL) {
2907 ocfs2_log_dlm_error("dlmunlock", status, lockres);
2908 ret = -EINVAL;
2909 ocfs2_recover_from_dlm_error(lockres, 0);
2910 }
2911
2912 mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
2913
2914 mlog_exit(ret);
2915 return ret;
2916}
2917
Mark Fashehb5e500e2006-09-13 22:01:16 -07002918static int ocfs2_unblock_lock(struct ocfs2_super *osb,
2919 struct ocfs2_lock_res *lockres,
2920 struct ocfs2_unblock_ctl *ctl)
Mark Fashehccd979b2005-12-15 14:31:24 -08002921{
2922 unsigned long flags;
2923 int blocking;
2924 int new_level;
2925 int ret = 0;
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07002926 int set_lvb = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002927
2928 mlog_entry_void();
2929
2930 spin_lock_irqsave(&lockres->l_lock, flags);
2931
2932 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
2933
2934recheck:
2935 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
Mark Fashehd680efe2006-09-08 14:14:34 -07002936 ctl->requeue = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08002937 ret = ocfs2_prepare_cancel_convert(osb, lockres);
2938 spin_unlock_irqrestore(&lockres->l_lock, flags);
2939 if (ret) {
2940 ret = ocfs2_cancel_convert(osb, lockres);
2941 if (ret < 0)
2942 mlog_errno(ret);
2943 }
2944 goto leave;
2945 }
2946
2947 /* if we're blocking an exclusive and we have *any* holders,
2948 * then requeue. */
2949 if ((lockres->l_blocking == LKM_EXMODE)
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002950 && (lockres->l_ex_holders || lockres->l_ro_holders))
2951 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002952
2953 /* If it's a PR we're blocking, then only
2954 * requeue if we've got any EX holders */
2955 if (lockres->l_blocking == LKM_PRMODE &&
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07002956 lockres->l_ex_holders)
2957 goto leave_requeue;
2958
2959 /*
2960 * Can we get a lock in this state if the holder counts are
2961 * zero? The meta data unblock code used to check this.
2962 */
2963 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
2964 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
2965 goto leave_requeue;
Mark Fashehccd979b2005-12-15 14:31:24 -08002966
Mark Fasheh16d5b952006-09-13 21:10:12 -07002967 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
2968
2969 if (lockres->l_ops->check_downconvert
2970 && !lockres->l_ops->check_downconvert(lockres, new_level))
2971 goto leave_requeue;
2972
Mark Fashehccd979b2005-12-15 14:31:24 -08002973 /* If we get here, then we know that there are no more
2974 * incompatible holders (and anyone asking for an incompatible
2975 * lock is blocked). We can now downconvert the lock */
Mark Fashehcc567d82006-09-13 21:52:21 -07002976 if (!lockres->l_ops->downconvert_worker)
Mark Fashehccd979b2005-12-15 14:31:24 -08002977 goto downconvert;
2978
2979 /* Some lockres types want to do a bit of work before
2980 * downconverting a lock. Allow that here. The worker function
2981 * may sleep, so we save off a copy of what we're blocking as
2982 * it may change while we're not holding the spin lock. */
2983 blocking = lockres->l_blocking;
2984 spin_unlock_irqrestore(&lockres->l_lock, flags);
2985
Mark Fashehcc567d82006-09-13 21:52:21 -07002986 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
Mark Fashehd680efe2006-09-08 14:14:34 -07002987
2988 if (ctl->unblock_action == UNBLOCK_STOP_POST)
2989 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002990
2991 spin_lock_irqsave(&lockres->l_lock, flags);
2992 if (blocking != lockres->l_blocking) {
2993 /* If this changed underneath us, then we can't drop
2994 * it just yet. */
2995 goto recheck;
2996 }
2997
2998downconvert:
Mark Fashehd680efe2006-09-08 14:14:34 -07002999 ctl->requeue = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003000
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003001 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3002 if (lockres->l_level == LKM_EXMODE)
3003 set_lvb = 1;
3004
3005 /*
3006 * We only set the lvb if the lock has been fully
3007 * refreshed - otherwise we risk setting stale
3008 * data. Otherwise, there's no need to actually clear
3009 * out the lvb here as it's value is still valid.
3010 */
3011 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3012 lockres->l_ops->set_lvb(lockres);
3013 }
3014
Mark Fashehccd979b2005-12-15 14:31:24 -08003015 ocfs2_prepare_downconvert(lockres, new_level);
3016 spin_unlock_irqrestore(&lockres->l_lock, flags);
Mark Fasheh5ef0d4e2006-09-13 21:21:52 -07003017 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
Mark Fashehccd979b2005-12-15 14:31:24 -08003018leave:
3019 mlog_exit(ret);
3020 return ret;
Mark Fashehf7fbfdd2006-09-13 21:02:29 -07003021
3022leave_requeue:
3023 spin_unlock_irqrestore(&lockres->l_lock, flags);
3024 ctl->requeue = 1;
3025
3026 mlog_exit(0);
3027 return 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003028}
3029
Mark Fashehd680efe2006-09-08 14:14:34 -07003030static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3031 int blocking)
Mark Fashehccd979b2005-12-15 14:31:24 -08003032{
3033 struct inode *inode;
3034 struct address_space *mapping;
3035
Mark Fashehccd979b2005-12-15 14:31:24 -08003036 inode = ocfs2_lock_res_inode(lockres);
3037 mapping = inode->i_mapping;
3038
Mark Fasheh1044e402008-02-28 17:16:03 -08003039 if (!S_ISREG(inode->i_mode))
Mark Fashehf1f54062007-10-18 15:13:59 -07003040 goto out;
3041
Mark Fasheh7f4a2a92006-12-11 11:06:36 -08003042 /*
3043 * We need this before the filemap_fdatawrite() so that it can
3044 * transfer the dirty bit from the PTE to the
3045 * page. Unfortunately this means that even for EX->PR
3046 * downconverts, we'll lose our mappings and have to build
3047 * them up again.
3048 */
3049 unmap_mapping_range(mapping, 0, 0, 0);
3050
Mark Fashehccd979b2005-12-15 14:31:24 -08003051 if (filemap_fdatawrite(mapping)) {
Mark Fashehb0697052006-03-03 10:24:33 -08003052 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3053 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08003054 }
3055 sync_mapping_buffers(mapping);
3056 if (blocking == LKM_EXMODE) {
3057 truncate_inode_pages(mapping, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08003058 } else {
3059 /* We only need to wait on the I/O if we're not also
3060 * truncating pages because truncate_inode_pages waits
3061 * for us above. We don't truncate pages if we're
3062 * blocking anything < EXMODE because we want to keep
3063 * them around in that case. */
3064 filemap_fdatawait(mapping);
3065 }
3066
Mark Fashehf1f54062007-10-18 15:13:59 -07003067out:
Mark Fashehd680efe2006-09-08 14:14:34 -07003068 return UNBLOCK_CONTINUE;
Mark Fashehccd979b2005-12-15 14:31:24 -08003069}
3070
Mark Fasheh810d5ae2006-09-13 21:39:52 -07003071static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3072 int new_level)
3073{
3074 struct inode *inode = ocfs2_lock_res_inode(lockres);
3075 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3076
3077 BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
3078 BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
3079
3080 if (checkpointed)
3081 return 1;
3082
3083 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3084 return 0;
3085}
3086
3087static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3088{
3089 struct inode *inode = ocfs2_lock_res_inode(lockres);
3090
3091 __ocfs2_stuff_meta_lvb(inode);
3092}
3093
Mark Fashehd680efe2006-09-08 14:14:34 -07003094/*
3095 * Does the final reference drop on our dentry lock. Right now this
Mark Fasheh34d024f2007-09-24 15:56:19 -07003096 * happens in the downconvert thread, but we could choose to simplify the
Mark Fashehd680efe2006-09-08 14:14:34 -07003097 * dlmglue API and push these off to the ocfs2_wq in the future.
3098 */
3099static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3100 struct ocfs2_lock_res *lockres)
3101{
3102 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3103 ocfs2_dentry_lock_put(osb, dl);
3104}
3105
3106/*
3107 * d_delete() matching dentries before the lock downconvert.
3108 *
3109 * At this point, any process waiting to destroy the
3110 * dentry_lock due to last ref count is stopped by the
3111 * OCFS2_LOCK_QUEUED flag.
3112 *
3113 * We have two potential problems
3114 *
3115 * 1) If we do the last reference drop on our dentry_lock (via dput)
3116 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3117 * the downconvert to finish. Instead we take an elevated
3118 * reference and push the drop until after we've completed our
3119 * unblock processing.
3120 *
3121 * 2) There might be another process with a final reference,
3122 * waiting on us to finish processing. If this is the case, we
3123 * detect it and exit out - there's no more dentries anyway.
3124 */
3125static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3126 int blocking)
3127{
3128 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3129 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3130 struct dentry *dentry;
3131 unsigned long flags;
3132 int extra_ref = 0;
3133
3134 /*
3135 * This node is blocking another node from getting a read
3136 * lock. This happens when we've renamed within a
3137 * directory. We've forced the other nodes to d_delete(), but
3138 * we never actually dropped our lock because it's still
3139 * valid. The downconvert code will retain a PR for this node,
3140 * so there's no further work to do.
3141 */
3142 if (blocking == LKM_PRMODE)
3143 return UNBLOCK_CONTINUE;
3144
3145 /*
3146 * Mark this inode as potentially orphaned. The code in
3147 * ocfs2_delete_inode() will figure out whether it actually
3148 * needs to be freed or not.
3149 */
3150 spin_lock(&oi->ip_lock);
3151 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3152 spin_unlock(&oi->ip_lock);
3153
3154 /*
3155 * Yuck. We need to make sure however that the check of
3156 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3157 * respect to a reference decrement or the setting of that
3158 * flag.
3159 */
3160 spin_lock_irqsave(&lockres->l_lock, flags);
3161 spin_lock(&dentry_attach_lock);
3162 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3163 && dl->dl_count) {
3164 dl->dl_count++;
3165 extra_ref = 1;
3166 }
3167 spin_unlock(&dentry_attach_lock);
3168 spin_unlock_irqrestore(&lockres->l_lock, flags);
3169
3170 mlog(0, "extra_ref = %d\n", extra_ref);
3171
3172 /*
3173 * We have a process waiting on us in ocfs2_dentry_iput(),
3174 * which means we can't have any more outstanding
3175 * aliases. There's no need to do any more work.
3176 */
3177 if (!extra_ref)
3178 return UNBLOCK_CONTINUE;
3179
3180 spin_lock(&dentry_attach_lock);
3181 while (1) {
3182 dentry = ocfs2_find_local_alias(dl->dl_inode,
3183 dl->dl_parent_blkno, 1);
3184 if (!dentry)
3185 break;
3186 spin_unlock(&dentry_attach_lock);
3187
3188 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3189 dentry->d_name.name);
3190
3191 /*
3192 * The following dcache calls may do an
3193 * iput(). Normally we don't want that from the
3194 * downconverting thread, but in this case it's ok
3195 * because the requesting node already has an
3196 * exclusive lock on the inode, so it can't be queued
3197 * for a downconvert.
3198 */
3199 d_delete(dentry);
3200 dput(dentry);
3201
3202 spin_lock(&dentry_attach_lock);
3203 }
3204 spin_unlock(&dentry_attach_lock);
3205
3206 /*
3207 * If we are the last holder of this dentry lock, there is no
3208 * reason to downconvert so skip straight to the unlock.
3209 */
3210 if (dl->dl_count == 1)
3211 return UNBLOCK_STOP_POST;
3212
3213 return UNBLOCK_CONTINUE_POST;
3214}
3215
Adrian Bunk00600052008-01-29 00:11:41 +02003216static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3217 struct ocfs2_lock_res *lockres)
Mark Fashehccd979b2005-12-15 14:31:24 -08003218{
3219 int status;
Mark Fashehd680efe2006-09-08 14:14:34 -07003220 struct ocfs2_unblock_ctl ctl = {0, 0,};
Mark Fashehccd979b2005-12-15 14:31:24 -08003221 unsigned long flags;
3222
3223 /* Our reference to the lockres in this function can be
3224 * considered valid until we remove the OCFS2_LOCK_QUEUED
3225 * flag. */
3226
3227 mlog_entry_void();
3228
3229 BUG_ON(!lockres);
3230 BUG_ON(!lockres->l_ops);
Mark Fashehccd979b2005-12-15 14:31:24 -08003231
3232 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3233
3234 /* Detect whether a lock has been marked as going away while
Mark Fasheh34d024f2007-09-24 15:56:19 -07003235 * the downconvert thread was processing other things. A lock can
Mark Fashehccd979b2005-12-15 14:31:24 -08003236 * still be marked with OCFS2_LOCK_FREEING after this check,
3237 * but short circuiting here will still save us some
3238 * performance. */
3239 spin_lock_irqsave(&lockres->l_lock, flags);
3240 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3241 goto unqueue;
3242 spin_unlock_irqrestore(&lockres->l_lock, flags);
3243
Mark Fashehb5e500e2006-09-13 22:01:16 -07003244 status = ocfs2_unblock_lock(osb, lockres, &ctl);
Mark Fashehccd979b2005-12-15 14:31:24 -08003245 if (status < 0)
3246 mlog_errno(status);
3247
3248 spin_lock_irqsave(&lockres->l_lock, flags);
3249unqueue:
Mark Fashehd680efe2006-09-08 14:14:34 -07003250 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003251 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3252 } else
3253 ocfs2_schedule_blocked_lock(osb, lockres);
3254
3255 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
Mark Fashehd680efe2006-09-08 14:14:34 -07003256 ctl.requeue ? "yes" : "no");
Mark Fashehccd979b2005-12-15 14:31:24 -08003257 spin_unlock_irqrestore(&lockres->l_lock, flags);
3258
Mark Fashehd680efe2006-09-08 14:14:34 -07003259 if (ctl.unblock_action != UNBLOCK_CONTINUE
3260 && lockres->l_ops->post_unlock)
3261 lockres->l_ops->post_unlock(osb, lockres);
3262
Mark Fashehccd979b2005-12-15 14:31:24 -08003263 mlog_exit_void();
3264}
3265
3266static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3267 struct ocfs2_lock_res *lockres)
3268{
3269 mlog_entry_void();
3270
3271 assert_spin_locked(&lockres->l_lock);
3272
3273 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3274 /* Do not schedule a lock for downconvert when it's on
3275 * the way to destruction - any nodes wanting access
3276 * to the resource will get it soon. */
3277 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3278 lockres->l_name, lockres->l_flags);
3279 return;
3280 }
3281
3282 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3283
Mark Fasheh34d024f2007-09-24 15:56:19 -07003284 spin_lock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003285 if (list_empty(&lockres->l_blocked_list)) {
3286 list_add_tail(&lockres->l_blocked_list,
3287 &osb->blocked_lock_list);
3288 osb->blocked_lock_count++;
3289 }
Mark Fasheh34d024f2007-09-24 15:56:19 -07003290 spin_unlock(&osb->dc_task_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -08003291
3292 mlog_exit_void();
3293}
Mark Fasheh34d024f2007-09-24 15:56:19 -07003294
3295static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3296{
3297 unsigned long processed;
3298 struct ocfs2_lock_res *lockres;
3299
3300 mlog_entry_void();
3301
3302 spin_lock(&osb->dc_task_lock);
3303 /* grab this early so we know to try again if a state change and
3304 * wake happens part-way through our work */
3305 osb->dc_work_sequence = osb->dc_wake_sequence;
3306
3307 processed = osb->blocked_lock_count;
3308 while (processed) {
3309 BUG_ON(list_empty(&osb->blocked_lock_list));
3310
3311 lockres = list_entry(osb->blocked_lock_list.next,
3312 struct ocfs2_lock_res, l_blocked_list);
3313 list_del_init(&lockres->l_blocked_list);
3314 osb->blocked_lock_count--;
3315 spin_unlock(&osb->dc_task_lock);
3316
3317 BUG_ON(!processed);
3318 processed--;
3319
3320 ocfs2_process_blocked_lock(osb, lockres);
3321
3322 spin_lock(&osb->dc_task_lock);
3323 }
3324 spin_unlock(&osb->dc_task_lock);
3325
3326 mlog_exit_void();
3327}
3328
3329static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3330{
3331 int empty = 0;
3332
3333 spin_lock(&osb->dc_task_lock);
3334 if (list_empty(&osb->blocked_lock_list))
3335 empty = 1;
3336
3337 spin_unlock(&osb->dc_task_lock);
3338 return empty;
3339}
3340
3341static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3342{
3343 int should_wake = 0;
3344
3345 spin_lock(&osb->dc_task_lock);
3346 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3347 should_wake = 1;
3348 spin_unlock(&osb->dc_task_lock);
3349
3350 return should_wake;
3351}
3352
Adrian Bunk200bfae2008-02-17 10:20:38 +02003353static int ocfs2_downconvert_thread(void *arg)
Mark Fasheh34d024f2007-09-24 15:56:19 -07003354{
3355 int status = 0;
3356 struct ocfs2_super *osb = arg;
3357
3358 /* only quit once we've been asked to stop and there is no more
3359 * work available */
3360 while (!(kthread_should_stop() &&
3361 ocfs2_downconvert_thread_lists_empty(osb))) {
3362
3363 wait_event_interruptible(osb->dc_event,
3364 ocfs2_downconvert_thread_should_wake(osb) ||
3365 kthread_should_stop());
3366
3367 mlog(0, "downconvert_thread: awoken\n");
3368
3369 ocfs2_downconvert_thread_do_work(osb);
3370 }
3371
3372 osb->dc_task = NULL;
3373 return status;
3374}
3375
3376void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3377{
3378 spin_lock(&osb->dc_task_lock);
3379 /* make sure the voting thread gets a swipe at whatever changes
3380 * the caller may have made to the voting state */
3381 osb->dc_wake_sequence++;
3382 spin_unlock(&osb->dc_task_lock);
3383 wake_up(&osb->dc_event);
3384}