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