Merge branch 'kmemleak' of git://linux-arm.org/linux-2.6
[deliverable/linux.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
f9e2d82e
MF
1992 if (lvb->lvb_version == OCFS2_LVB_VERSION
1993 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
ccd979bd
MF
1994 return 1;
1995 return 0;
1996}
1997
1998/* Determine whether a lock resource needs to be refreshed, and
1999 * arbitrate who gets to refresh it.
2000 *
2001 * 0 means no refresh needed.
2002 *
2003 * > 0 means you need to refresh this and you MUST call
2004 * ocfs2_complete_lock_res_refresh afterwards. */
2005static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2006{
2007 unsigned long flags;
2008 int status = 0;
2009
2010 mlog_entry_void();
2011
2012refresh_check:
2013 spin_lock_irqsave(&lockres->l_lock, flags);
2014 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2015 spin_unlock_irqrestore(&lockres->l_lock, flags);
2016 goto bail;
2017 }
2018
2019 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2020 spin_unlock_irqrestore(&lockres->l_lock, flags);
2021
2022 ocfs2_wait_on_refreshing_lock(lockres);
2023 goto refresh_check;
2024 }
2025
2026 /* Ok, I'll be the one to refresh this lock. */
2027 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2028 spin_unlock_irqrestore(&lockres->l_lock, flags);
2029
2030 status = 1;
2031bail:
2032 mlog_exit(status);
2033 return status;
2034}
2035
2036/* If status is non zero, I'll mark it as not being in refresh
2037 * anymroe, but i won't clear the needs refresh flag. */
2038static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2039 int status)
2040{
2041 unsigned long flags;
2042 mlog_entry_void();
2043
2044 spin_lock_irqsave(&lockres->l_lock, flags);
2045 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2046 if (!status)
2047 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2048 spin_unlock_irqrestore(&lockres->l_lock, flags);
2049
2050 wake_up(&lockres->l_event);
2051
2052 mlog_exit_void();
2053}
2054
2055/* may or may not return a bh if it went to disk. */
e63aecb6 2056static int ocfs2_inode_lock_update(struct inode *inode,
ccd979bd
MF
2057 struct buffer_head **bh)
2058{
2059 int status = 0;
2060 struct ocfs2_inode_info *oi = OCFS2_I(inode);
e63aecb6 2061 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
ccd979bd 2062 struct ocfs2_dinode *fe;
c271c5c2 2063 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bd
MF
2064
2065 mlog_entry_void();
2066
be9e986b
MF
2067 if (ocfs2_mount_local(osb))
2068 goto bail;
2069
ccd979bd
MF
2070 spin_lock(&oi->ip_lock);
2071 if (oi->ip_flags & OCFS2_INODE_DELETED) {
b0697053 2072 mlog(0, "Orphaned inode %llu was deleted while we "
ccd979bd 2073 "were waiting on a lock. ip_flags = 0x%x\n",
b0697053 2074 (unsigned long long)oi->ip_blkno, oi->ip_flags);
ccd979bd
MF
2075 spin_unlock(&oi->ip_lock);
2076 status = -ENOENT;
2077 goto bail;
2078 }
2079 spin_unlock(&oi->ip_lock);
2080
be9e986b
MF
2081 if (!ocfs2_should_refresh_lock_res(lockres))
2082 goto bail;
ccd979bd
MF
2083
2084 /* This will discard any caching information we might have had
2085 * for the inode metadata. */
2086 ocfs2_metadata_cache_purge(inode);
2087
83418978
MF
2088 ocfs2_extent_map_trunc(inode, 0);
2089
be9e986b 2090 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
b0697053
MF
2091 mlog(0, "Trusting LVB on inode %llu\n",
2092 (unsigned long long)oi->ip_blkno);
ccd979bd
MF
2093 ocfs2_refresh_inode_from_lvb(inode);
2094 } else {
2095 /* Boo, we have to go to disk. */
2096 /* read bh, cast, ocfs2_refresh_inode */
b657c95c 2097 status = ocfs2_read_inode_block(inode, bh);
ccd979bd
MF
2098 if (status < 0) {
2099 mlog_errno(status);
2100 goto bail_refresh;
2101 }
2102 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2103
2104 /* This is a good chance to make sure we're not
b657c95c
JB
2105 * locking an invalid object. ocfs2_read_inode_block()
2106 * already checked that the inode block is sane.
ccd979bd
MF
2107 *
2108 * We bug on a stale inode here because we checked
2109 * above whether it was wiped from disk. The wiping
2110 * node provides a guarantee that we receive that
2111 * message and can mark the inode before dropping any
2112 * locks associated with it. */
ccd979bd
MF
2113 mlog_bug_on_msg(inode->i_generation !=
2114 le32_to_cpu(fe->i_generation),
b0697053 2115 "Invalid dinode %llu disk generation: %u "
ccd979bd 2116 "inode->i_generation: %u\n",
b0697053
MF
2117 (unsigned long long)oi->ip_blkno,
2118 le32_to_cpu(fe->i_generation),
ccd979bd
MF
2119 inode->i_generation);
2120 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2121 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
b0697053
MF
2122 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2123 (unsigned long long)oi->ip_blkno,
2124 (unsigned long long)le64_to_cpu(fe->i_dtime),
ccd979bd
MF
2125 le32_to_cpu(fe->i_flags));
2126
2127 ocfs2_refresh_inode(inode, fe);
8ddb7b00 2128 ocfs2_track_lock_refresh(lockres);
ccd979bd
MF
2129 }
2130
2131 status = 0;
2132bail_refresh:
be9e986b 2133 ocfs2_complete_lock_res_refresh(lockres, status);
ccd979bd
MF
2134bail:
2135 mlog_exit(status);
2136 return status;
2137}
2138
2139static int ocfs2_assign_bh(struct inode *inode,
2140 struct buffer_head **ret_bh,
2141 struct buffer_head *passed_bh)
2142{
2143 int status;
2144
2145 if (passed_bh) {
2146 /* Ok, the update went to disk for us, use the
2147 * returned bh. */
2148 *ret_bh = passed_bh;
2149 get_bh(*ret_bh);
2150
2151 return 0;
2152 }
2153
b657c95c 2154 status = ocfs2_read_inode_block(inode, ret_bh);
ccd979bd
MF
2155 if (status < 0)
2156 mlog_errno(status);
2157
2158 return status;
2159}
2160
2161/*
2162 * returns < 0 error if the callback will never be called, otherwise
2163 * the result of the lock will be communicated via the callback.
2164 */
e63aecb6 2165int ocfs2_inode_lock_full(struct inode *inode,
ccd979bd
MF
2166 struct buffer_head **ret_bh,
2167 int ex,
2168 int arg_flags)
2169{
bd3e7610
JB
2170 int status, level, acquired;
2171 u32 dlm_flags;
c271c5c2 2172 struct ocfs2_lock_res *lockres = NULL;
ccd979bd
MF
2173 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2174 struct buffer_head *local_bh = NULL;
2175
2176 BUG_ON(!inode);
2177
2178 mlog_entry_void();
2179
b0697053
MF
2180 mlog(0, "inode %llu, take %s META lock\n",
2181 (unsigned long long)OCFS2_I(inode)->ip_blkno,
ccd979bd
MF
2182 ex ? "EXMODE" : "PRMODE");
2183
2184 status = 0;
2185 acquired = 0;
2186 /* We'll allow faking a readonly metadata lock for
2187 * rodevices. */
2188 if (ocfs2_is_hard_readonly(osb)) {
2189 if (ex)
2190 status = -EROFS;
2191 goto bail;
2192 }
2193
c271c5c2
SM
2194 if (ocfs2_mount_local(osb))
2195 goto local;
2196
ccd979bd 2197 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
553abd04 2198 ocfs2_wait_for_recovery(osb);
ccd979bd 2199
e63aecb6 2200 lockres = &OCFS2_I(inode)->ip_inode_lockres;
bd3e7610 2201 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
ccd979bd
MF
2202 dlm_flags = 0;
2203 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
bd3e7610 2204 dlm_flags |= DLM_LKF_NOQUEUE;
ccd979bd
MF
2205
2206 status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
2207 if (status < 0) {
2208 if (status != -EAGAIN && status != -EIOCBRETRY)
2209 mlog_errno(status);
2210 goto bail;
2211 }
2212
2213 /* Notify the error cleanup path to drop the cluster lock. */
2214 acquired = 1;
2215
2216 /* We wait twice because a node may have died while we were in
2217 * the lower dlm layers. The second time though, we've
2218 * committed to owning this lock so we don't allow signals to
2219 * abort the operation. */
2220 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
553abd04 2221 ocfs2_wait_for_recovery(osb);
ccd979bd 2222
c271c5c2 2223local:
24c19ef4
MF
2224 /*
2225 * We only see this flag if we're being called from
2226 * ocfs2_read_locked_inode(). It means we're locking an inode
2227 * which hasn't been populated yet, so clear the refresh flag
2228 * and let the caller handle it.
2229 */
2230 if (inode->i_state & I_NEW) {
2231 status = 0;
c271c5c2
SM
2232 if (lockres)
2233 ocfs2_complete_lock_res_refresh(lockres, 0);
24c19ef4
MF
2234 goto bail;
2235 }
2236
ccd979bd 2237 /* This is fun. The caller may want a bh back, or it may
e63aecb6 2238 * not. ocfs2_inode_lock_update definitely wants one in, but
ccd979bd
MF
2239 * may or may not read one, depending on what's in the
2240 * LVB. The result of all of this is that we've *only* gone to
2241 * disk if we have to, so the complexity is worthwhile. */
e63aecb6 2242 status = ocfs2_inode_lock_update(inode, &local_bh);
ccd979bd
MF
2243 if (status < 0) {
2244 if (status != -ENOENT)
2245 mlog_errno(status);
2246 goto bail;
2247 }
2248
2249 if (ret_bh) {
2250 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2251 if (status < 0) {
2252 mlog_errno(status);
2253 goto bail;
2254 }
2255 }
2256
ccd979bd
MF
2257bail:
2258 if (status < 0) {
2259 if (ret_bh && (*ret_bh)) {
2260 brelse(*ret_bh);
2261 *ret_bh = NULL;
2262 }
2263 if (acquired)
e63aecb6 2264 ocfs2_inode_unlock(inode, ex);
ccd979bd
MF
2265 }
2266
2267 if (local_bh)
2268 brelse(local_bh);
2269
2270 mlog_exit(status);
2271 return status;
2272}
2273
2274/*
34d024f8
MF
2275 * This is working around a lock inversion between tasks acquiring DLM
2276 * locks while holding a page lock and the downconvert thread which
2277 * blocks dlm lock acquiry while acquiring page locks.
ccd979bd
MF
2278 *
2279 * ** These _with_page variantes are only intended to be called from aop
2280 * methods that hold page locks and return a very specific *positive* error
2281 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2282 *
34d024f8
MF
2283 * The DLM is called such that it returns -EAGAIN if it would have
2284 * blocked waiting for the downconvert thread. In that case we unlock
2285 * our page so the downconvert thread can make progress. Once we've
2286 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2287 * that called us can bubble that back up into the VFS who will then
2288 * immediately retry the aop call.
ccd979bd
MF
2289 *
2290 * We do a blocking lock and immediate unlock before returning, though, so that
2291 * the lock has a great chance of being cached on this node by the time the VFS
2292 * calls back to retry the aop. This has a potential to livelock as nodes
2293 * ping locks back and forth, but that's a risk we're willing to take to avoid
2294 * the lock inversion simply.
2295 */
e63aecb6 2296int ocfs2_inode_lock_with_page(struct inode *inode,
ccd979bd
MF
2297 struct buffer_head **ret_bh,
2298 int ex,
2299 struct page *page)
2300{
2301 int ret;
2302
e63aecb6 2303 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
ccd979bd
MF
2304 if (ret == -EAGAIN) {
2305 unlock_page(page);
e63aecb6
MF
2306 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2307 ocfs2_inode_unlock(inode, ex);
ccd979bd
MF
2308 ret = AOP_TRUNCATED_PAGE;
2309 }
2310
2311 return ret;
2312}
2313
e63aecb6 2314int ocfs2_inode_lock_atime(struct inode *inode,
7f1a37e3
TY
2315 struct vfsmount *vfsmnt,
2316 int *level)
2317{
2318 int ret;
2319
2320 mlog_entry_void();
e63aecb6 2321 ret = ocfs2_inode_lock(inode, NULL, 0);
7f1a37e3
TY
2322 if (ret < 0) {
2323 mlog_errno(ret);
2324 return ret;
2325 }
2326
2327 /*
2328 * If we should update atime, we will get EX lock,
2329 * otherwise we just get PR lock.
2330 */
2331 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2332 struct buffer_head *bh = NULL;
2333
e63aecb6
MF
2334 ocfs2_inode_unlock(inode, 0);
2335 ret = ocfs2_inode_lock(inode, &bh, 1);
7f1a37e3
TY
2336 if (ret < 0) {
2337 mlog_errno(ret);
2338 return ret;
2339 }
2340 *level = 1;
2341 if (ocfs2_should_update_atime(inode, vfsmnt))
2342 ocfs2_update_inode_atime(inode, bh);
2343 if (bh)
2344 brelse(bh);
2345 } else
2346 *level = 0;
2347
2348 mlog_exit(ret);
2349 return ret;
2350}
2351
e63aecb6 2352void ocfs2_inode_unlock(struct inode *inode,
ccd979bd
MF
2353 int ex)
2354{
bd3e7610 2355 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
e63aecb6 2356 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
c271c5c2 2357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bd
MF
2358
2359 mlog_entry_void();
2360
b0697053
MF
2361 mlog(0, "inode %llu drop %s META lock\n",
2362 (unsigned long long)OCFS2_I(inode)->ip_blkno,
ccd979bd
MF
2363 ex ? "EXMODE" : "PRMODE");
2364
c271c5c2
SM
2365 if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
2366 !ocfs2_mount_local(osb))
ccd979bd
MF
2367 ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
2368
2369 mlog_exit_void();
2370}
2371
83273932
SE
2372int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno, int ex)
2373{
2374 struct ocfs2_lock_res *lockres;
2375 struct ocfs2_orphan_scan_lvb *lvb;
2376 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2377 int status = 0;
2378
2379 lockres = &osb->osb_orphan_scan.os_lockres;
2380 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2381 if (status < 0)
2382 return status;
2383
2384 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2385 if (lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2386 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2387 return status;
2388}
2389
2390void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno, int ex)
2391{
2392 struct ocfs2_lock_res *lockres;
2393 struct ocfs2_orphan_scan_lvb *lvb;
2394 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2395
2396 lockres = &osb->osb_orphan_scan.os_lockres;
2397 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2398 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2399 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2400 ocfs2_cluster_unlock(osb, lockres, level);
2401}
2402
ccd979bd
MF
2403int ocfs2_super_lock(struct ocfs2_super *osb,
2404 int ex)
2405{
c271c5c2 2406 int status = 0;
bd3e7610 2407 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
ccd979bd 2408 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
ccd979bd
MF
2409
2410 mlog_entry_void();
2411
2412 if (ocfs2_is_hard_readonly(osb))
2413 return -EROFS;
2414
c271c5c2
SM
2415 if (ocfs2_mount_local(osb))
2416 goto bail;
2417
ccd979bd
MF
2418 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2419 if (status < 0) {
2420 mlog_errno(status);
2421 goto bail;
2422 }
2423
2424 /* The super block lock path is really in the best position to
2425 * know when resources covered by the lock need to be
2426 * refreshed, so we do it here. Of course, making sense of
2427 * everything is up to the caller :) */
2428 status = ocfs2_should_refresh_lock_res(lockres);
2429 if (status < 0) {
2430 mlog_errno(status);
2431 goto bail;
2432 }
2433 if (status) {
8e8a4603 2434 status = ocfs2_refresh_slot_info(osb);
ccd979bd
MF
2435
2436 ocfs2_complete_lock_res_refresh(lockres, status);
2437
2438 if (status < 0)
2439 mlog_errno(status);
8ddb7b00 2440 ocfs2_track_lock_refresh(lockres);
ccd979bd
MF
2441 }
2442bail:
2443 mlog_exit(status);
2444 return status;
2445}
2446
2447void ocfs2_super_unlock(struct ocfs2_super *osb,
2448 int ex)
2449{
bd3e7610 2450 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
ccd979bd
MF
2451 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2452
c271c5c2
SM
2453 if (!ocfs2_mount_local(osb))
2454 ocfs2_cluster_unlock(osb, lockres, level);
ccd979bd
MF
2455}
2456
2457int ocfs2_rename_lock(struct ocfs2_super *osb)
2458{
2459 int status;
2460 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2461
2462 if (ocfs2_is_hard_readonly(osb))
2463 return -EROFS;
2464
c271c5c2
SM
2465 if (ocfs2_mount_local(osb))
2466 return 0;
2467
bd3e7610 2468 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
ccd979bd
MF
2469 if (status < 0)
2470 mlog_errno(status);
2471
2472 return status;
2473}
2474
2475void ocfs2_rename_unlock(struct ocfs2_super *osb)
2476{
2477 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2478
c271c5c2 2479 if (!ocfs2_mount_local(osb))
bd3e7610 2480 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
ccd979bd
MF
2481}
2482
6ca497a8 2483int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2484{
2485 int status;
2486 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2487
2488 if (ocfs2_is_hard_readonly(osb))
2489 return -EROFS;
2490
2491 if (ocfs2_mount_local(osb))
2492 return 0;
2493
2494 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2495 0, 0);
2496 if (status < 0)
2497 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2498
2499 return status;
2500}
2501
2502void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2503{
2504 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2505
2506 if (!ocfs2_mount_local(osb))
2507 ocfs2_cluster_unlock(osb, lockres,
2508 ex ? LKM_EXMODE : LKM_PRMODE);
2509}
2510
d680efe9
MF
2511int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2512{
2513 int ret;
bd3e7610 2514 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
d680efe9
MF
2515 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2516 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2517
2518 BUG_ON(!dl);
2519
2520 if (ocfs2_is_hard_readonly(osb))
2521 return -EROFS;
2522
c271c5c2
SM
2523 if (ocfs2_mount_local(osb))
2524 return 0;
2525
d680efe9
MF
2526 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2527 if (ret < 0)
2528 mlog_errno(ret);
2529
2530 return ret;
2531}
2532
2533void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2534{
bd3e7610 2535 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
d680efe9
MF
2536 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2537 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2538
c271c5c2
SM
2539 if (!ocfs2_mount_local(osb))
2540 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
d680efe9
MF
2541}
2542
ccd979bd
MF
2543/* Reference counting of the dlm debug structure. We want this because
2544 * open references on the debug inodes can live on after a mount, so
2545 * we can't rely on the ocfs2_super to always exist. */
2546static void ocfs2_dlm_debug_free(struct kref *kref)
2547{
2548 struct ocfs2_dlm_debug *dlm_debug;
2549
2550 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2551
2552 kfree(dlm_debug);
2553}
2554
2555void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2556{
2557 if (dlm_debug)
2558 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2559}
2560
2561static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2562{
2563 kref_get(&debug->d_refcnt);
2564}
2565
2566struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
2567{
2568 struct ocfs2_dlm_debug *dlm_debug;
2569
2570 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
2571 if (!dlm_debug) {
2572 mlog_errno(-ENOMEM);
2573 goto out;
2574 }
2575
2576 kref_init(&dlm_debug->d_refcnt);
2577 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
2578 dlm_debug->d_locking_state = NULL;
2579out:
2580 return dlm_debug;
2581}
2582
2583/* Access to this is arbitrated for us via seq_file->sem. */
2584struct ocfs2_dlm_seq_priv {
2585 struct ocfs2_dlm_debug *p_dlm_debug;
2586 struct ocfs2_lock_res p_iter_res;
2587 struct ocfs2_lock_res p_tmp_res;
2588};
2589
2590static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
2591 struct ocfs2_dlm_seq_priv *priv)
2592{
2593 struct ocfs2_lock_res *iter, *ret = NULL;
2594 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
2595
2596 assert_spin_locked(&ocfs2_dlm_tracking_lock);
2597
2598 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
2599 /* discover the head of the list */
2600 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
2601 mlog(0, "End of list found, %p\n", ret);
2602 break;
2603 }
2604
2605 /* We track our "dummy" iteration lockres' by a NULL
2606 * l_ops field. */
2607 if (iter->l_ops != NULL) {
2608 ret = iter;
2609 break;
2610 }
2611 }
2612
2613 return ret;
2614}
2615
2616static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
2617{
2618 struct ocfs2_dlm_seq_priv *priv = m->private;
2619 struct ocfs2_lock_res *iter;
2620
2621 spin_lock(&ocfs2_dlm_tracking_lock);
2622 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
2623 if (iter) {
2624 /* Since lockres' have the lifetime of their container
2625 * (which can be inodes, ocfs2_supers, etc) we want to
2626 * copy this out to a temporary lockres while still
2627 * under the spinlock. Obviously after this we can't
2628 * trust any pointers on the copy returned, but that's
2629 * ok as the information we want isn't typically held
2630 * in them. */
2631 priv->p_tmp_res = *iter;
2632 iter = &priv->p_tmp_res;
2633 }
2634 spin_unlock(&ocfs2_dlm_tracking_lock);
2635
2636 return iter;
2637}
2638
2639static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
2640{
2641}
2642
2643static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
2644{
2645 struct ocfs2_dlm_seq_priv *priv = m->private;
2646 struct ocfs2_lock_res *iter = v;
2647 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
2648
2649 spin_lock(&ocfs2_dlm_tracking_lock);
2650 iter = ocfs2_dlm_next_res(iter, priv);
2651 list_del_init(&dummy->l_debug_list);
2652 if (iter) {
2653 list_add(&dummy->l_debug_list, &iter->l_debug_list);
2654 priv->p_tmp_res = *iter;
2655 iter = &priv->p_tmp_res;
2656 }
2657 spin_unlock(&ocfs2_dlm_tracking_lock);
2658
2659 return iter;
2660}
2661
2662/* So that debugfs.ocfs2 can determine which format is being used */
8ddb7b00 2663#define OCFS2_DLM_DEBUG_STR_VERSION 2
ccd979bd
MF
2664static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
2665{
2666 int i;
2667 char *lvb;
2668 struct ocfs2_lock_res *lockres = v;
2669
2670 if (!lockres)
2671 return -EINVAL;
2672
d680efe9
MF
2673 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
2674
2675 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
2676 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
2677 lockres->l_name,
2678 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
2679 else
2680 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
2681
2682 seq_printf(m, "%d\t"
ccd979bd
MF
2683 "0x%lx\t"
2684 "0x%x\t"
2685 "0x%x\t"
2686 "%u\t"
2687 "%u\t"
2688 "%d\t"
2689 "%d\t",
ccd979bd
MF
2690 lockres->l_level,
2691 lockres->l_flags,
2692 lockres->l_action,
2693 lockres->l_unlock_action,
2694 lockres->l_ro_holders,
2695 lockres->l_ex_holders,
2696 lockres->l_requested,
2697 lockres->l_blocking);
2698
2699 /* Dump the raw LVB */
8f2c9c1b 2700 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
ccd979bd
MF
2701 for(i = 0; i < DLM_LVB_LEN; i++)
2702 seq_printf(m, "0x%x\t", lvb[i]);
2703
8ddb7b00
SM
2704#ifdef CONFIG_OCFS2_FS_STATS
2705# define lock_num_prmode(_l) (_l)->l_lock_num_prmode
2706# define lock_num_exmode(_l) (_l)->l_lock_num_exmode
2707# define lock_num_prmode_failed(_l) (_l)->l_lock_num_prmode_failed
2708# define lock_num_exmode_failed(_l) (_l)->l_lock_num_exmode_failed
2709# define lock_total_prmode(_l) (_l)->l_lock_total_prmode
2710# define lock_total_exmode(_l) (_l)->l_lock_total_exmode
2711# define lock_max_prmode(_l) (_l)->l_lock_max_prmode
2712# define lock_max_exmode(_l) (_l)->l_lock_max_exmode
2713# define lock_refresh(_l) (_l)->l_lock_refresh
2714#else
dd25e55e
RD
2715# define lock_num_prmode(_l) (0ULL)
2716# define lock_num_exmode(_l) (0ULL)
8ddb7b00
SM
2717# define lock_num_prmode_failed(_l) (0)
2718# define lock_num_exmode_failed(_l) (0)
dd25e55e
RD
2719# define lock_total_prmode(_l) (0ULL)
2720# define lock_total_exmode(_l) (0ULL)
8ddb7b00
SM
2721# define lock_max_prmode(_l) (0)
2722# define lock_max_exmode(_l) (0)
2723# define lock_refresh(_l) (0)
2724#endif
2725 /* The following seq_print was added in version 2 of this output */
2726 seq_printf(m, "%llu\t"
2727 "%llu\t"
2728 "%u\t"
2729 "%u\t"
2730 "%llu\t"
2731 "%llu\t"
2732 "%u\t"
2733 "%u\t"
2734 "%u\t",
2735 lock_num_prmode(lockres),
2736 lock_num_exmode(lockres),
2737 lock_num_prmode_failed(lockres),
2738 lock_num_exmode_failed(lockres),
2739 lock_total_prmode(lockres),
2740 lock_total_exmode(lockres),
2741 lock_max_prmode(lockres),
2742 lock_max_exmode(lockres),
2743 lock_refresh(lockres));
2744
ccd979bd
MF
2745 /* End the line */
2746 seq_printf(m, "\n");
2747 return 0;
2748}
2749
90d99779 2750static const struct seq_operations ocfs2_dlm_seq_ops = {
ccd979bd
MF
2751 .start = ocfs2_dlm_seq_start,
2752 .stop = ocfs2_dlm_seq_stop,
2753 .next = ocfs2_dlm_seq_next,
2754 .show = ocfs2_dlm_seq_show,
2755};
2756
2757static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
2758{
2759 struct seq_file *seq = (struct seq_file *) file->private_data;
2760 struct ocfs2_dlm_seq_priv *priv = seq->private;
2761 struct ocfs2_lock_res *res = &priv->p_iter_res;
2762
2763 ocfs2_remove_lockres_tracking(res);
2764 ocfs2_put_dlm_debug(priv->p_dlm_debug);
2765 return seq_release_private(inode, file);
2766}
2767
2768static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
2769{
2770 int ret;
2771 struct ocfs2_dlm_seq_priv *priv;
2772 struct seq_file *seq;
2773 struct ocfs2_super *osb;
2774
2775 priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
2776 if (!priv) {
2777 ret = -ENOMEM;
2778 mlog_errno(ret);
2779 goto out;
2780 }
8e18e294 2781 osb = inode->i_private;
ccd979bd
MF
2782 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
2783 priv->p_dlm_debug = osb->osb_dlm_debug;
2784 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
2785
2786 ret = seq_open(file, &ocfs2_dlm_seq_ops);
2787 if (ret) {
2788 kfree(priv);
2789 mlog_errno(ret);
2790 goto out;
2791 }
2792
2793 seq = (struct seq_file *) file->private_data;
2794 seq->private = priv;
2795
2796 ocfs2_add_lockres_tracking(&priv->p_iter_res,
2797 priv->p_dlm_debug);
2798
2799out:
2800 return ret;
2801}
2802
4b6f5d20 2803static const struct file_operations ocfs2_dlm_debug_fops = {
ccd979bd
MF
2804 .open = ocfs2_dlm_debug_open,
2805 .release = ocfs2_dlm_debug_release,
2806 .read = seq_read,
2807 .llseek = seq_lseek,
2808};
2809
2810static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
2811{
2812 int ret = 0;
2813 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2814
2815 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
2816 S_IFREG|S_IRUSR,
2817 osb->osb_debug_root,
2818 osb,
2819 &ocfs2_dlm_debug_fops);
2820 if (!dlm_debug->d_locking_state) {
2821 ret = -EINVAL;
2822 mlog(ML_ERROR,
2823 "Unable to create locking state debugfs file.\n");
2824 goto out;
2825 }
2826
2827 ocfs2_get_dlm_debug(dlm_debug);
2828out:
2829 return ret;
2830}
2831
2832static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
2833{
2834 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
2835
2836 if (dlm_debug) {
2837 debugfs_remove(dlm_debug->d_locking_state);
2838 ocfs2_put_dlm_debug(dlm_debug);
2839 }
2840}
2841
2842int ocfs2_dlm_init(struct ocfs2_super *osb)
2843{
c271c5c2 2844 int status = 0;
4670c46d 2845 struct ocfs2_cluster_connection *conn = NULL;
ccd979bd
MF
2846
2847 mlog_entry_void();
2848
0abd6d18
MF
2849 if (ocfs2_mount_local(osb)) {
2850 osb->node_num = 0;
c271c5c2 2851 goto local;
0abd6d18 2852 }
c271c5c2 2853
ccd979bd
MF
2854 status = ocfs2_dlm_init_debug(osb);
2855 if (status < 0) {
2856 mlog_errno(status);
2857 goto bail;
2858 }
2859
34d024f8
MF
2860 /* launch downconvert thread */
2861 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
2862 if (IS_ERR(osb->dc_task)) {
2863 status = PTR_ERR(osb->dc_task);
2864 osb->dc_task = NULL;
ccd979bd
MF
2865 mlog_errno(status);
2866 goto bail;
2867 }
2868
ccd979bd 2869 /* for now, uuid == domain */
9c6c877c
JB
2870 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
2871 osb->uuid_str,
4670c46d
JB
2872 strlen(osb->uuid_str),
2873 ocfs2_do_node_down, osb,
2874 &conn);
2875 if (status) {
ccd979bd
MF
2876 mlog_errno(status);
2877 goto bail;
2878 }
2879
0abd6d18
MF
2880 status = ocfs2_cluster_this_node(&osb->node_num);
2881 if (status < 0) {
2882 mlog_errno(status);
2883 mlog(ML_ERROR,
2884 "could not find this host's node number\n");
286eaa95 2885 ocfs2_cluster_disconnect(conn, 0);
0abd6d18
MF
2886 goto bail;
2887 }
2888
c271c5c2 2889local:
ccd979bd
MF
2890 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
2891 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
6ca497a8 2892 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
83273932 2893 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
ccd979bd 2894
4670c46d 2895 osb->cconn = conn;
ccd979bd
MF
2896
2897 status = 0;
2898bail:
2899 if (status < 0) {
2900 ocfs2_dlm_shutdown_debug(osb);
34d024f8
MF
2901 if (osb->dc_task)
2902 kthread_stop(osb->dc_task);
ccd979bd
MF
2903 }
2904
2905 mlog_exit(status);
2906 return status;
2907}
2908
286eaa95
JB
2909void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
2910 int hangup_pending)
ccd979bd
MF
2911{
2912 mlog_entry_void();
2913
ccd979bd
MF
2914 ocfs2_drop_osb_locks(osb);
2915
4670c46d
JB
2916 /*
2917 * Now that we have dropped all locks and ocfs2_dismount_volume()
2918 * has disabled recovery, the DLM won't be talking to us. It's
2919 * safe to tear things down before disconnecting the cluster.
2920 */
2921
34d024f8
MF
2922 if (osb->dc_task) {
2923 kthread_stop(osb->dc_task);
2924 osb->dc_task = NULL;
ccd979bd
MF
2925 }
2926
2927 ocfs2_lock_res_free(&osb->osb_super_lockres);
2928 ocfs2_lock_res_free(&osb->osb_rename_lockres);
6ca497a8 2929 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
83273932 2930 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
ccd979bd 2931
286eaa95 2932 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
4670c46d 2933 osb->cconn = NULL;
ccd979bd
MF
2934
2935 ocfs2_dlm_shutdown_debug(osb);
2936
2937 mlog_exit_void();
2938}
2939
7431cd7e 2940static void ocfs2_unlock_ast(void *opaque, int error)
ccd979bd
MF
2941{
2942 struct ocfs2_lock_res *lockres = opaque;
2943 unsigned long flags;
2944
2945 mlog_entry_void();
2946
2947 mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
2948 lockres->l_unlock_action);
2949
2950 spin_lock_irqsave(&lockres->l_lock, flags);
de551246 2951 if (error) {
7431cd7e
JB
2952 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
2953 "unlock_action %d\n", error, lockres->l_name,
ccd979bd
MF
2954 lockres->l_unlock_action);
2955 spin_unlock_irqrestore(&lockres->l_lock, flags);
2956 return;
2957 }
2958
2959 switch(lockres->l_unlock_action) {
2960 case OCFS2_UNLOCK_CANCEL_CONVERT:
2961 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
2962 lockres->l_action = OCFS2_AST_INVALID;
a4b91965
SM
2963 /* Downconvert thread may have requeued this lock, we
2964 * need to wake it. */
2965 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
2966 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
ccd979bd
MF
2967 break;
2968 case OCFS2_UNLOCK_DROP_LOCK:
bd3e7610 2969 lockres->l_level = DLM_LOCK_IV;
ccd979bd
MF
2970 break;
2971 default:
2972 BUG();
2973 }
2974
2975 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
ccd979bd 2976 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
ccd979bd 2977 wake_up(&lockres->l_event);
07f9eebc 2978 spin_unlock_irqrestore(&lockres->l_lock, flags);
ccd979bd
MF
2979
2980 mlog_exit_void();
2981}
2982
ccd979bd 2983static int ocfs2_drop_lock(struct ocfs2_super *osb,
0d5dc6c2 2984 struct ocfs2_lock_res *lockres)
ccd979bd 2985{
7431cd7e 2986 int ret;
ccd979bd 2987 unsigned long flags;
bd3e7610 2988 u32 lkm_flags = 0;
ccd979bd
MF
2989
2990 /* We didn't get anywhere near actually using this lockres. */
2991 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
2992 goto out;
2993
b80fc012 2994 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
bd3e7610 2995 lkm_flags |= DLM_LKF_VALBLK;
b80fc012 2996
ccd979bd
MF
2997 spin_lock_irqsave(&lockres->l_lock, flags);
2998
2999 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3000 "lockres %s, flags 0x%lx\n",
3001 lockres->l_name, lockres->l_flags);
3002
3003 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3004 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3005 "%u, unlock_action = %u\n",
3006 lockres->l_name, lockres->l_flags, lockres->l_action,
3007 lockres->l_unlock_action);
3008
3009 spin_unlock_irqrestore(&lockres->l_lock, flags);
3010
3011 /* XXX: Today we just wait on any busy
3012 * locks... Perhaps we need to cancel converts in the
3013 * future? */
3014 ocfs2_wait_on_busy_lock(lockres);
3015
3016 spin_lock_irqsave(&lockres->l_lock, flags);
3017 }
3018
0d5dc6c2
MF
3019 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3020 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
bd3e7610 3021 lockres->l_level == DLM_LOCK_EX &&
0d5dc6c2
MF
3022 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3023 lockres->l_ops->set_lvb(lockres);
3024 }
ccd979bd
MF
3025
3026 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3027 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3028 lockres->l_name);
3029 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3030 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3031
3032 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3033 spin_unlock_irqrestore(&lockres->l_lock, flags);
3034 goto out;
3035 }
3036
3037 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3038
3039 /* make sure we never get here while waiting for an ast to
3040 * fire. */
3041 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3042
3043 /* is this necessary? */
3044 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3045 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3046 spin_unlock_irqrestore(&lockres->l_lock, flags);
3047
3048 mlog(0, "lock %s\n", lockres->l_name);
3049
4670c46d 3050 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
7431cd7e
JB
3051 lockres);
3052 if (ret) {
3053 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
ccd979bd 3054 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
cf0acdcd 3055 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
ccd979bd
MF
3056 BUG();
3057 }
73ac36ea 3058 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
ccd979bd
MF
3059 lockres->l_name);
3060
3061 ocfs2_wait_on_busy_lock(lockres);
3062out:
3063 mlog_exit(0);
3064 return 0;
3065}
3066
3067/* Mark the lockres as being dropped. It will no longer be
3068 * queued if blocking, but we still may have to wait on it
34d024f8 3069 * being dequeued from the downconvert thread before we can consider
ccd979bd
MF
3070 * it safe to drop.
3071 *
3072 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3073void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
3074{
3075 int status;
3076 struct ocfs2_mask_waiter mw;
3077 unsigned long flags;
3078
3079 ocfs2_init_mask_waiter(&mw);
3080
3081 spin_lock_irqsave(&lockres->l_lock, flags);
3082 lockres->l_flags |= OCFS2_LOCK_FREEING;
3083 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3084 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3085 spin_unlock_irqrestore(&lockres->l_lock, flags);
3086
3087 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3088
3089 status = ocfs2_wait_for_mask(&mw);
3090 if (status)
3091 mlog_errno(status);
3092
3093 spin_lock_irqsave(&lockres->l_lock, flags);
3094 }
3095 spin_unlock_irqrestore(&lockres->l_lock, flags);
3096}
3097
d680efe9
MF
3098void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3099 struct ocfs2_lock_res *lockres)
ccd979bd 3100{
d680efe9 3101 int ret;
ccd979bd 3102
d680efe9 3103 ocfs2_mark_lockres_freeing(lockres);
0d5dc6c2 3104 ret = ocfs2_drop_lock(osb, lockres);
d680efe9
MF
3105 if (ret)
3106 mlog_errno(ret);
3107}
ccd979bd 3108
d680efe9
MF
3109static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3110{
3111 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3112 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
6ca497a8 3113 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
83273932 3114 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
ccd979bd
MF
3115}
3116
ccd979bd
MF
3117int ocfs2_drop_inode_locks(struct inode *inode)
3118{
3119 int status, err;
ccd979bd
MF
3120
3121 mlog_entry_void();
3122
3123 /* No need to call ocfs2_mark_lockres_freeing here -
3124 * ocfs2_clear_inode has done it for us. */
3125
3126 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
50008630 3127 &OCFS2_I(inode)->ip_open_lockres);
ccd979bd
MF
3128 if (err < 0)
3129 mlog_errno(err);
3130
3131 status = err;
3132
3133 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
e63aecb6 3134 &OCFS2_I(inode)->ip_inode_lockres);
ccd979bd
MF
3135 if (err < 0)
3136 mlog_errno(err);
3137 if (err < 0 && !status)
3138 status = err;
3139
3140 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
0d5dc6c2 3141 &OCFS2_I(inode)->ip_rw_lockres);
ccd979bd
MF
3142 if (err < 0)
3143 mlog_errno(err);
3144 if (err < 0 && !status)
3145 status = err;
3146
3147 mlog_exit(status);
3148 return status;
3149}
3150
de551246
JB
3151static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3152 int new_level)
ccd979bd
MF
3153{
3154 assert_spin_locked(&lockres->l_lock);
3155
bd3e7610 3156 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
ccd979bd
MF
3157
3158 if (lockres->l_level <= new_level) {
bd3e7610 3159 mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
ccd979bd
MF
3160 lockres->l_level, new_level);
3161 BUG();
3162 }
3163
3164 mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
3165 lockres->l_name, new_level, lockres->l_blocking);
3166
3167 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3168 lockres->l_requested = new_level;
3169 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
de551246 3170 return lockres_set_pending(lockres);
ccd979bd
MF
3171}
3172
3173static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3174 struct ocfs2_lock_res *lockres,
3175 int new_level,
de551246
JB
3176 int lvb,
3177 unsigned int generation)
ccd979bd 3178{
bd3e7610
JB
3179 int ret;
3180 u32 dlm_flags = DLM_LKF_CONVERT;
ccd979bd
MF
3181
3182 mlog_entry_void();
3183
3184 if (lvb)
bd3e7610 3185 dlm_flags |= DLM_LKF_VALBLK;
ccd979bd 3186
4670c46d 3187 ret = ocfs2_dlm_lock(osb->cconn,
7431cd7e
JB
3188 new_level,
3189 &lockres->l_lksb,
3190 dlm_flags,
3191 lockres->l_name,
3192 OCFS2_LOCK_ID_MAX_LEN - 1,
3193 lockres);
de551246 3194 lockres_clear_pending(lockres, generation, osb);
7431cd7e
JB
3195 if (ret) {
3196 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
ccd979bd
MF
3197 ocfs2_recover_from_dlm_error(lockres, 1);
3198 goto bail;
3199 }
3200
3201 ret = 0;
3202bail:
3203 mlog_exit(ret);
3204 return ret;
3205}
3206
24ef1815 3207/* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
ccd979bd
MF
3208static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3209 struct ocfs2_lock_res *lockres)
3210{
3211 assert_spin_locked(&lockres->l_lock);
3212
3213 mlog_entry_void();
3214 mlog(0, "lock %s\n", lockres->l_name);
3215
3216 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3217 /* If we're already trying to cancel a lock conversion
3218 * then just drop the spinlock and allow the caller to
3219 * requeue this lock. */
3220
3221 mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
3222 return 0;
3223 }
3224
3225 /* were we in a convert when we got the bast fire? */
3226 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3227 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3228 /* set things up for the unlockast to know to just
3229 * clear out the ast_action and unset busy, etc. */
3230 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3231
3232 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3233 "lock %s, invalid flags: 0x%lx\n",
3234 lockres->l_name, lockres->l_flags);
3235
3236 return 1;
3237}
3238
3239static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3240 struct ocfs2_lock_res *lockres)
3241{
3242 int ret;
ccd979bd
MF
3243
3244 mlog_entry_void();
3245 mlog(0, "lock %s\n", lockres->l_name);
3246
4670c46d 3247 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
7431cd7e
JB
3248 DLM_LKF_CANCEL, lockres);
3249 if (ret) {
3250 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
ccd979bd
MF
3251 ocfs2_recover_from_dlm_error(lockres, 0);
3252 }
3253
24ef1815 3254 mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
ccd979bd 3255
ccd979bd
MF
3256 mlog_exit(ret);
3257 return ret;
3258}
3259
b5e500e2
MF
3260static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3261 struct ocfs2_lock_res *lockres,
3262 struct ocfs2_unblock_ctl *ctl)
ccd979bd
MF
3263{
3264 unsigned long flags;
3265 int blocking;
3266 int new_level;
3267 int ret = 0;
5ef0d4ea 3268 int set_lvb = 0;
de551246 3269 unsigned int gen;
ccd979bd
MF
3270
3271 mlog_entry_void();
3272
3273 spin_lock_irqsave(&lockres->l_lock, flags);
3274
3275 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
3276
3277recheck:
3278 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
de551246
JB
3279 /* XXX
3280 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3281 * exists entirely for one reason - another thread has set
3282 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3283 *
3284 * If we do ocfs2_cancel_convert() before the other thread
3285 * calls dlm_lock(), our cancel will do nothing. We will
3286 * get no ast, and we will have no way of knowing the
3287 * cancel failed. Meanwhile, the other thread will call
3288 * into dlm_lock() and wait...forever.
3289 *
3290 * Why forever? Because another node has asked for the
3291 * lock first; that's why we're here in unblock_lock().
3292 *
3293 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3294 * set, we just requeue the unblock. Only when the other
3295 * thread has called dlm_lock() and cleared PENDING will
3296 * we then cancel their request.
3297 *
3298 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3299 * at the same time they set OCFS2_DLM_BUSY. They must
3300 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3301 */
3302 if (lockres->l_flags & OCFS2_LOCK_PENDING)
3303 goto leave_requeue;
3304
d680efe9 3305 ctl->requeue = 1;
ccd979bd
MF
3306 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3307 spin_unlock_irqrestore(&lockres->l_lock, flags);
3308 if (ret) {
3309 ret = ocfs2_cancel_convert(osb, lockres);
3310 if (ret < 0)
3311 mlog_errno(ret);
3312 }
3313 goto leave;
3314 }
3315
3316 /* if we're blocking an exclusive and we have *any* holders,
3317 * then requeue. */
bd3e7610 3318 if ((lockres->l_blocking == DLM_LOCK_EX)
f7fbfdd1
MF
3319 && (lockres->l_ex_holders || lockres->l_ro_holders))
3320 goto leave_requeue;
ccd979bd
MF
3321
3322 /* If it's a PR we're blocking, then only
3323 * requeue if we've got any EX holders */
bd3e7610 3324 if (lockres->l_blocking == DLM_LOCK_PR &&
f7fbfdd1
MF
3325 lockres->l_ex_holders)
3326 goto leave_requeue;
3327
3328 /*
3329 * Can we get a lock in this state if the holder counts are
3330 * zero? The meta data unblock code used to check this.
3331 */
3332 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3333 && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
3334 goto leave_requeue;
ccd979bd 3335
16d5b956
MF
3336 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3337
3338 if (lockres->l_ops->check_downconvert
3339 && !lockres->l_ops->check_downconvert(lockres, new_level))
3340 goto leave_requeue;
3341
ccd979bd
MF
3342 /* If we get here, then we know that there are no more
3343 * incompatible holders (and anyone asking for an incompatible
3344 * lock is blocked). We can now downconvert the lock */
cc567d89 3345 if (!lockres->l_ops->downconvert_worker)
ccd979bd
MF
3346 goto downconvert;
3347
3348 /* Some lockres types want to do a bit of work before
3349 * downconverting a lock. Allow that here. The worker function
3350 * may sleep, so we save off a copy of what we're blocking as
3351 * it may change while we're not holding the spin lock. */
3352 blocking = lockres->l_blocking;
3353 spin_unlock_irqrestore(&lockres->l_lock, flags);
3354
cc567d89 3355 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
d680efe9
MF
3356
3357 if (ctl->unblock_action == UNBLOCK_STOP_POST)
3358 goto leave;
ccd979bd
MF
3359
3360 spin_lock_irqsave(&lockres->l_lock, flags);
3361 if (blocking != lockres->l_blocking) {
3362 /* If this changed underneath us, then we can't drop
3363 * it just yet. */
3364 goto recheck;
3365 }
3366
3367downconvert:
d680efe9 3368 ctl->requeue = 0;
ccd979bd 3369
5ef0d4ea 3370 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
bd3e7610 3371 if (lockres->l_level == DLM_LOCK_EX)
5ef0d4ea
MF
3372 set_lvb = 1;
3373
3374 /*
3375 * We only set the lvb if the lock has been fully
3376 * refreshed - otherwise we risk setting stale
3377 * data. Otherwise, there's no need to actually clear
3378 * out the lvb here as it's value is still valid.
3379 */
3380 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3381 lockres->l_ops->set_lvb(lockres);
3382 }
3383
de551246 3384 gen = ocfs2_prepare_downconvert(lockres, new_level);
ccd979bd 3385 spin_unlock_irqrestore(&lockres->l_lock, flags);
de551246
JB
3386 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3387 gen);
3388
ccd979bd
MF
3389leave:
3390 mlog_exit(ret);
3391 return ret;
f7fbfdd1
MF
3392
3393leave_requeue:
3394 spin_unlock_irqrestore(&lockres->l_lock, flags);
3395 ctl->requeue = 1;
3396
3397 mlog_exit(0);
3398 return 0;
ccd979bd
MF
3399}
3400
d680efe9
MF
3401static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3402 int blocking)
ccd979bd
MF
3403{
3404 struct inode *inode;
3405 struct address_space *mapping;
3406
ccd979bd
MF
3407 inode = ocfs2_lock_res_inode(lockres);
3408 mapping = inode->i_mapping;
3409
1044e401 3410 if (!S_ISREG(inode->i_mode))
f1f54068
MF
3411 goto out;
3412
7f4a2a97
MF
3413 /*
3414 * We need this before the filemap_fdatawrite() so that it can
3415 * transfer the dirty bit from the PTE to the
3416 * page. Unfortunately this means that even for EX->PR
3417 * downconverts, we'll lose our mappings and have to build
3418 * them up again.
3419 */
3420 unmap_mapping_range(mapping, 0, 0, 0);
3421
ccd979bd 3422 if (filemap_fdatawrite(mapping)) {
b0697053
MF
3423 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3424 (unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bd
MF
3425 }
3426 sync_mapping_buffers(mapping);
bd3e7610 3427 if (blocking == DLM_LOCK_EX) {
ccd979bd 3428 truncate_inode_pages(mapping, 0);
ccd979bd
MF
3429 } else {
3430 /* We only need to wait on the I/O if we're not also
3431 * truncating pages because truncate_inode_pages waits
3432 * for us above. We don't truncate pages if we're
3433 * blocking anything < EXMODE because we want to keep
3434 * them around in that case. */
3435 filemap_fdatawait(mapping);
3436 }
3437
f1f54068 3438out:
d680efe9 3439 return UNBLOCK_CONTINUE;
ccd979bd
MF
3440}
3441
810d5aeb
MF
3442static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3443 int new_level)
3444{
3445 struct inode *inode = ocfs2_lock_res_inode(lockres);
3446 int checkpointed = ocfs2_inode_fully_checkpointed(inode);
3447
bd3e7610
JB
3448 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3449 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
810d5aeb
MF
3450
3451 if (checkpointed)
3452 return 1;
3453
3454 ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
3455 return 0;
3456}
3457
3458static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3459{
3460 struct inode *inode = ocfs2_lock_res_inode(lockres);
3461
3462 __ocfs2_stuff_meta_lvb(inode);
3463}
3464
d680efe9
MF
3465/*
3466 * Does the final reference drop on our dentry lock. Right now this
34d024f8 3467 * happens in the downconvert thread, but we could choose to simplify the
d680efe9
MF
3468 * dlmglue API and push these off to the ocfs2_wq in the future.
3469 */
3470static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3471 struct ocfs2_lock_res *lockres)
3472{
3473 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3474 ocfs2_dentry_lock_put(osb, dl);
3475}
3476
3477/*
3478 * d_delete() matching dentries before the lock downconvert.
3479 *
3480 * At this point, any process waiting to destroy the
3481 * dentry_lock due to last ref count is stopped by the
3482 * OCFS2_LOCK_QUEUED flag.
3483 *
3484 * We have two potential problems
3485 *
3486 * 1) If we do the last reference drop on our dentry_lock (via dput)
3487 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3488 * the downconvert to finish. Instead we take an elevated
3489 * reference and push the drop until after we've completed our
3490 * unblock processing.
3491 *
3492 * 2) There might be another process with a final reference,
3493 * waiting on us to finish processing. If this is the case, we
3494 * detect it and exit out - there's no more dentries anyway.
3495 */
3496static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
3497 int blocking)
3498{
3499 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3500 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
3501 struct dentry *dentry;
3502 unsigned long flags;
3503 int extra_ref = 0;
3504
3505 /*
3506 * This node is blocking another node from getting a read
3507 * lock. This happens when we've renamed within a
3508 * directory. We've forced the other nodes to d_delete(), but
3509 * we never actually dropped our lock because it's still
3510 * valid. The downconvert code will retain a PR for this node,
3511 * so there's no further work to do.
3512 */
bd3e7610 3513 if (blocking == DLM_LOCK_PR)
d680efe9
MF
3514 return UNBLOCK_CONTINUE;
3515
3516 /*
3517 * Mark this inode as potentially orphaned. The code in
3518 * ocfs2_delete_inode() will figure out whether it actually
3519 * needs to be freed or not.
3520 */
3521 spin_lock(&oi->ip_lock);
3522 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
3523 spin_unlock(&oi->ip_lock);
3524
3525 /*
3526 * Yuck. We need to make sure however that the check of
3527 * OCFS2_LOCK_FREEING and the extra reference are atomic with
3528 * respect to a reference decrement or the setting of that
3529 * flag.
3530 */
3531 spin_lock_irqsave(&lockres->l_lock, flags);
3532 spin_lock(&dentry_attach_lock);
3533 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
3534 && dl->dl_count) {
3535 dl->dl_count++;
3536 extra_ref = 1;
3537 }
3538 spin_unlock(&dentry_attach_lock);
3539 spin_unlock_irqrestore(&lockres->l_lock, flags);
3540
3541 mlog(0, "extra_ref = %d\n", extra_ref);
3542
3543 /*
3544 * We have a process waiting on us in ocfs2_dentry_iput(),
3545 * which means we can't have any more outstanding
3546 * aliases. There's no need to do any more work.
3547 */
3548 if (!extra_ref)
3549 return UNBLOCK_CONTINUE;
3550
3551 spin_lock(&dentry_attach_lock);
3552 while (1) {
3553 dentry = ocfs2_find_local_alias(dl->dl_inode,
3554 dl->dl_parent_blkno, 1);
3555 if (!dentry)
3556 break;
3557 spin_unlock(&dentry_attach_lock);
3558
3559 mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
3560 dentry->d_name.name);
3561
3562 /*
3563 * The following dcache calls may do an
3564 * iput(). Normally we don't want that from the
3565 * downconverting thread, but in this case it's ok
3566 * because the requesting node already has an
3567 * exclusive lock on the inode, so it can't be queued
3568 * for a downconvert.
3569 */
3570 d_delete(dentry);
3571 dput(dentry);
3572
3573 spin_lock(&dentry_attach_lock);
3574 }
3575 spin_unlock(&dentry_attach_lock);
3576
3577 /*
3578 * If we are the last holder of this dentry lock, there is no
3579 * reason to downconvert so skip straight to the unlock.
3580 */
3581 if (dl->dl_count == 1)
3582 return UNBLOCK_STOP_POST;
3583
3584 return UNBLOCK_CONTINUE_POST;
3585}
3586
9e33d69f
JK
3587static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
3588{
3589 struct ocfs2_qinfo_lvb *lvb;
3590 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
3591 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3592 oinfo->dqi_gi.dqi_type);
3593
3594 mlog_entry_void();
3595
a641dc2a 3596 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
9e33d69f
JK
3597 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
3598 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
3599 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
3600 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
3601 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
3602 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
3603 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
3604
3605 mlog_exit_void();
3606}
3607
3608void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3609{
3610 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3611 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3612 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3613
3614 mlog_entry_void();
3615 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
3616 ocfs2_cluster_unlock(osb, lockres, level);
3617 mlog_exit_void();
3618}
3619
3620static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
3621{
3622 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
3623 oinfo->dqi_gi.dqi_type);
3624 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3625 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
85eb8b73 3626 struct buffer_head *bh = NULL;
9e33d69f
JK
3627 struct ocfs2_global_disk_dqinfo *gdinfo;
3628 int status = 0;
3629
3630 if (lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
3631 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
3632 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
3633 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
3634 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
3635 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
3636 oinfo->dqi_gi.dqi_free_entry =
3637 be32_to_cpu(lvb->lvb_free_entry);
3638 } else {
85eb8b73
JB
3639 status = ocfs2_read_quota_block(oinfo->dqi_gqinode, 0, &bh);
3640 if (status) {
9e33d69f
JK
3641 mlog_errno(status);
3642 goto bail;
3643 }
3644 gdinfo = (struct ocfs2_global_disk_dqinfo *)
3645 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
3646 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
3647 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
3648 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
3649 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
3650 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
3651 oinfo->dqi_gi.dqi_free_entry =
3652 le32_to_cpu(gdinfo->dqi_free_entry);
3653 brelse(bh);
3654 ocfs2_track_lock_refresh(lockres);
3655 }
3656
3657bail:
3658 return status;
3659}
3660
3661/* Lock quota info, this function expects at least shared lock on the quota file
3662 * so that we can safely refresh quota info from disk. */
3663int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
3664{
3665 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
3666 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
3667 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
3668 int status = 0;
3669
3670 mlog_entry_void();
3671
3672 /* On RO devices, locking really isn't needed... */
3673 if (ocfs2_is_hard_readonly(osb)) {
3674 if (ex)
3675 status = -EROFS;
3676 goto bail;
3677 }
3678 if (ocfs2_mount_local(osb))
3679 goto bail;
3680
3681 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
3682 if (status < 0) {
3683 mlog_errno(status);
3684 goto bail;
3685 }
3686 if (!ocfs2_should_refresh_lock_res(lockres))
3687 goto bail;
3688 /* OK, we have the lock but we need to refresh the quota info */
3689 status = ocfs2_refresh_qinfo(oinfo);
3690 if (status)
3691 ocfs2_qinfo_unlock(oinfo, ex);
3692 ocfs2_complete_lock_res_refresh(lockres, status);
3693bail:
3694 mlog_exit(status);
3695 return status;
3696}
3697
4670c46d
JB
3698/*
3699 * This is the filesystem locking protocol. It provides the lock handling
3700 * hooks for the underlying DLM. It has a maximum version number.
3701 * The version number allows interoperability with systems running at
3702 * the same major number and an equal or smaller minor number.
3703 *
3704 * Whenever the filesystem does new things with locks (adds or removes a
3705 * lock, orders them differently, does different things underneath a lock),
3706 * the version must be changed. The protocol is negotiated when joining
3707 * the dlm domain. A node may join the domain if its major version is
3708 * identical to all other nodes and its minor version is greater than
3709 * or equal to all other nodes. When its minor version is greater than
3710 * the other nodes, it will run at the minor version specified by the
3711 * other nodes.
3712 *
3713 * If a locking change is made that will not be compatible with older
3714 * versions, the major number must be increased and the minor version set
3715 * to zero. If a change merely adds a behavior that can be disabled when
3716 * speaking to older versions, the minor version must be increased. If a
3717 * change adds a fully backwards compatible change (eg, LVB changes that
3718 * are just ignored by older versions), the version does not need to be
3719 * updated.
3720 */
24ef1815 3721static struct ocfs2_locking_protocol lproto = {
4670c46d
JB
3722 .lp_max_version = {
3723 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
3724 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
3725 },
24ef1815
JB
3726 .lp_lock_ast = ocfs2_locking_ast,
3727 .lp_blocking_ast = ocfs2_blocking_ast,
3728 .lp_unlock_ast = ocfs2_unlock_ast,
3729};
3730
63e0c48a 3731void ocfs2_set_locking_protocol(void)
24ef1815 3732{
63e0c48a 3733 ocfs2_stack_glue_set_locking_protocol(&lproto);
24ef1815
JB
3734}
3735
24ef1815 3736
00600056
AB
3737static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3738 struct ocfs2_lock_res *lockres)
ccd979bd
MF
3739{
3740 int status;
d680efe9 3741 struct ocfs2_unblock_ctl ctl = {0, 0,};
ccd979bd
MF
3742 unsigned long flags;
3743
3744 /* Our reference to the lockres in this function can be
3745 * considered valid until we remove the OCFS2_LOCK_QUEUED
3746 * flag. */
3747
3748 mlog_entry_void();
3749
3750 BUG_ON(!lockres);
3751 BUG_ON(!lockres->l_ops);
ccd979bd
MF
3752
3753 mlog(0, "lockres %s blocked.\n", lockres->l_name);
3754
3755 /* Detect whether a lock has been marked as going away while
34d024f8 3756 * the downconvert thread was processing other things. A lock can
ccd979bd
MF
3757 * still be marked with OCFS2_LOCK_FREEING after this check,
3758 * but short circuiting here will still save us some
3759 * performance. */
3760 spin_lock_irqsave(&lockres->l_lock, flags);
3761 if (lockres->l_flags & OCFS2_LOCK_FREEING)
3762 goto unqueue;
3763 spin_unlock_irqrestore(&lockres->l_lock, flags);
3764
b5e500e2 3765 status = ocfs2_unblock_lock(osb, lockres, &ctl);
ccd979bd
MF
3766 if (status < 0)
3767 mlog_errno(status);
3768
3769 spin_lock_irqsave(&lockres->l_lock, flags);
3770unqueue:
d680efe9 3771 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
ccd979bd
MF
3772 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
3773 } else
3774 ocfs2_schedule_blocked_lock(osb, lockres);
3775
3776 mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
d680efe9 3777 ctl.requeue ? "yes" : "no");
ccd979bd
MF
3778 spin_unlock_irqrestore(&lockres->l_lock, flags);
3779
d680efe9
MF
3780 if (ctl.unblock_action != UNBLOCK_CONTINUE
3781 && lockres->l_ops->post_unlock)
3782 lockres->l_ops->post_unlock(osb, lockres);
3783
ccd979bd
MF
3784 mlog_exit_void();
3785}
3786
3787static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
3788 struct ocfs2_lock_res *lockres)
3789{
3790 mlog_entry_void();
3791
3792 assert_spin_locked(&lockres->l_lock);
3793
3794 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
3795 /* Do not schedule a lock for downconvert when it's on
3796 * the way to destruction - any nodes wanting access
3797 * to the resource will get it soon. */
3798 mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
3799 lockres->l_name, lockres->l_flags);
3800 return;
3801 }
3802
3803 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
3804
34d024f8 3805 spin_lock(&osb->dc_task_lock);
ccd979bd
MF
3806 if (list_empty(&lockres->l_blocked_list)) {
3807 list_add_tail(&lockres->l_blocked_list,
3808 &osb->blocked_lock_list);
3809 osb->blocked_lock_count++;
3810 }
34d024f8 3811 spin_unlock(&osb->dc_task_lock);
ccd979bd
MF
3812
3813 mlog_exit_void();
3814}
34d024f8
MF
3815
3816static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
3817{
3818 unsigned long processed;
3819 struct ocfs2_lock_res *lockres;
3820
3821 mlog_entry_void();
3822
3823 spin_lock(&osb->dc_task_lock);
3824 /* grab this early so we know to try again if a state change and
3825 * wake happens part-way through our work */
3826 osb->dc_work_sequence = osb->dc_wake_sequence;
3827
3828 processed = osb->blocked_lock_count;
3829 while (processed) {
3830 BUG_ON(list_empty(&osb->blocked_lock_list));
3831
3832 lockres = list_entry(osb->blocked_lock_list.next,
3833 struct ocfs2_lock_res, l_blocked_list);
3834 list_del_init(&lockres->l_blocked_list);
3835 osb->blocked_lock_count--;
3836 spin_unlock(&osb->dc_task_lock);
3837
3838 BUG_ON(!processed);
3839 processed--;
3840
3841 ocfs2_process_blocked_lock(osb, lockres);
3842
3843 spin_lock(&osb->dc_task_lock);
3844 }
3845 spin_unlock(&osb->dc_task_lock);
3846
3847 mlog_exit_void();
3848}
3849
3850static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
3851{
3852 int empty = 0;
3853
3854 spin_lock(&osb->dc_task_lock);
3855 if (list_empty(&osb->blocked_lock_list))
3856 empty = 1;
3857
3858 spin_unlock(&osb->dc_task_lock);
3859 return empty;
3860}
3861
3862static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
3863{
3864 int should_wake = 0;
3865
3866 spin_lock(&osb->dc_task_lock);
3867 if (osb->dc_work_sequence != osb->dc_wake_sequence)
3868 should_wake = 1;
3869 spin_unlock(&osb->dc_task_lock);
3870
3871 return should_wake;
3872}
3873
200bfae3 3874static int ocfs2_downconvert_thread(void *arg)
34d024f8
MF
3875{
3876 int status = 0;
3877 struct ocfs2_super *osb = arg;
3878
3879 /* only quit once we've been asked to stop and there is no more
3880 * work available */
3881 while (!(kthread_should_stop() &&
3882 ocfs2_downconvert_thread_lists_empty(osb))) {
3883
3884 wait_event_interruptible(osb->dc_event,
3885 ocfs2_downconvert_thread_should_wake(osb) ||
3886 kthread_should_stop());
3887
3888 mlog(0, "downconvert_thread: awoken\n");
3889
3890 ocfs2_downconvert_thread_do_work(osb);
3891 }
3892
3893 osb->dc_task = NULL;
3894 return status;
3895}
3896
3897void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
3898{
3899 spin_lock(&osb->dc_task_lock);
3900 /* make sure the voting thread gets a swipe at whatever changes
3901 * the caller may have made to the voting state */
3902 osb->dc_wake_sequence++;
3903 spin_unlock(&osb->dc_task_lock);
3904 wake_up(&osb->dc_event);
3905}
This page took 0.549399 seconds and 5 git commands to generate.