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