Linux 4.19.133
[linux/fpc-iii.git] / fs / ocfs2 / dlmglue.c
blobc141b06811a6c2bed176be8185f3242235ae1b3c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmglue.c
6 * Code which implements an OCFS2 specific interface to our DLM.
8 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
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.
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.
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.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/mm.h>
30 #include <linux/kthread.h>
31 #include <linux/pagemap.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/time.h>
35 #include <linux/quotaops.h>
36 #include <linux/sched/signal.h>
38 #define MLOG_MASK_PREFIX ML_DLM_GLUE
39 #include <cluster/masklog.h>
41 #include "ocfs2.h"
42 #include "ocfs2_lockingver.h"
44 #include "alloc.h"
45 #include "dcache.h"
46 #include "dlmglue.h"
47 #include "extent_map.h"
48 #include "file.h"
49 #include "heartbeat.h"
50 #include "inode.h"
51 #include "journal.h"
52 #include "stackglue.h"
53 #include "slot_map.h"
54 #include "super.h"
55 #include "uptodate.h"
56 #include "quota.h"
57 #include "refcounttree.h"
58 #include "acl.h"
60 #include "buffer_head_io.h"
62 struct ocfs2_mask_waiter {
63 struct list_head mw_item;
64 int mw_status;
65 struct completion mw_complete;
66 unsigned long mw_mask;
67 unsigned long mw_goal;
68 #ifdef CONFIG_OCFS2_FS_STATS
69 ktime_t mw_lock_start;
70 #endif
73 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
74 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
75 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
76 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
79 * Return value from ->downconvert_worker functions.
81 * These control the precise actions of ocfs2_unblock_lock()
82 * and ocfs2_process_blocked_lock()
85 enum ocfs2_unblock_action {
86 UNBLOCK_CONTINUE = 0, /* Continue downconvert */
87 UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
88 * ->post_unlock callback */
89 UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
90 * ->post_unlock() callback. */
93 struct ocfs2_unblock_ctl {
94 int requeue;
95 enum ocfs2_unblock_action unblock_action;
98 /* Lockdep class keys */
99 #ifdef CONFIG_DEBUG_LOCK_ALLOC
100 static struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
101 #endif
103 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
104 int new_level);
105 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
107 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
108 int blocking);
110 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
111 int blocking);
113 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
114 struct ocfs2_lock_res *lockres);
116 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
118 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
119 int new_level);
120 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
121 int blocking);
123 #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
125 /* This aids in debugging situations where a bad LVB might be involved. */
126 static void ocfs2_dump_meta_lvb_info(u64 level,
127 const char *function,
128 unsigned int line,
129 struct ocfs2_lock_res *lockres)
131 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
133 mlog(level, "LVB information for %s (called from %s:%u):\n",
134 lockres->l_name, function, line);
135 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
136 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
137 be32_to_cpu(lvb->lvb_igeneration));
138 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
139 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
140 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
141 be16_to_cpu(lvb->lvb_imode));
142 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
143 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
144 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
145 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
146 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
147 be32_to_cpu(lvb->lvb_iattr));
152 * OCFS2 Lock Resource Operations
154 * These fine tune the behavior of the generic dlmglue locking infrastructure.
156 * The most basic of lock types can point ->l_priv to their respective
157 * struct ocfs2_super and allow the default actions to manage things.
159 * Right now, each lock type also needs to implement an init function,
160 * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
161 * should be called when the lock is no longer needed (i.e., object
162 * destruction time).
164 struct ocfs2_lock_res_ops {
166 * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
167 * this callback if ->l_priv is not an ocfs2_super pointer
169 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
172 * Optionally called in the downconvert thread after a
173 * successful downconvert. The lockres will not be referenced
174 * after this callback is called, so it is safe to free
175 * memory, etc.
177 * The exact semantics of when this is called are controlled
178 * by ->downconvert_worker()
180 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
183 * Allow a lock type to add checks to determine whether it is
184 * safe to downconvert a lock. Return 0 to re-queue the
185 * downconvert at a later time, nonzero to continue.
187 * For most locks, the default checks that there are no
188 * incompatible holders are sufficient.
190 * Called with the lockres spinlock held.
192 int (*check_downconvert)(struct ocfs2_lock_res *, int);
195 * Allows a lock type to populate the lock value block. This
196 * is called on downconvert, and when we drop a lock.
198 * Locks that want to use this should set LOCK_TYPE_USES_LVB
199 * in the flags field.
201 * Called with the lockres spinlock held.
203 void (*set_lvb)(struct ocfs2_lock_res *);
206 * Called from the downconvert thread when it is determined
207 * that a lock will be downconverted. This is called without
208 * any locks held so the function can do work that might
209 * schedule (syncing out data, etc).
211 * This should return any one of the ocfs2_unblock_action
212 * values, depending on what it wants the thread to do.
214 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
217 * LOCK_TYPE_* flags which describe the specific requirements
218 * of a lock type. Descriptions of each individual flag follow.
220 int flags;
224 * Some locks want to "refresh" potentially stale data when a
225 * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
226 * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
227 * individual lockres l_flags member from the ast function. It is
228 * expected that the locking wrapper will clear the
229 * OCFS2_LOCK_NEEDS_REFRESH flag when done.
231 #define LOCK_TYPE_REQUIRES_REFRESH 0x1
234 * Indicate that a lock type makes use of the lock value block. The
235 * ->set_lvb lock type callback must be defined.
237 #define LOCK_TYPE_USES_LVB 0x2
239 static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
240 .get_osb = ocfs2_get_inode_osb,
241 .flags = 0,
244 static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
245 .get_osb = ocfs2_get_inode_osb,
246 .check_downconvert = ocfs2_check_meta_downconvert,
247 .set_lvb = ocfs2_set_meta_lvb,
248 .downconvert_worker = ocfs2_data_convert_worker,
249 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
252 static struct ocfs2_lock_res_ops ocfs2_super_lops = {
253 .flags = LOCK_TYPE_REQUIRES_REFRESH,
256 static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
257 .flags = 0,
260 static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
261 .flags = 0,
264 static struct ocfs2_lock_res_ops ocfs2_trim_fs_lops = {
265 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
268 static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
269 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
272 static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
273 .get_osb = ocfs2_get_dentry_osb,
274 .post_unlock = ocfs2_dentry_post_unlock,
275 .downconvert_worker = ocfs2_dentry_convert_worker,
276 .flags = 0,
279 static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
280 .get_osb = ocfs2_get_inode_osb,
281 .flags = 0,
284 static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
285 .get_osb = ocfs2_get_file_osb,
286 .flags = 0,
289 static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
290 .set_lvb = ocfs2_set_qinfo_lvb,
291 .get_osb = ocfs2_get_qinfo_osb,
292 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
295 static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
296 .check_downconvert = ocfs2_check_refcount_downconvert,
297 .downconvert_worker = ocfs2_refcount_convert_worker,
298 .flags = 0,
301 static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
303 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
304 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
305 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
308 static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
310 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
313 static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
315 BUG_ON(!ocfs2_is_inode_lock(lockres));
317 return (struct inode *) lockres->l_priv;
320 static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
322 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
324 return (struct ocfs2_dentry_lock *)lockres->l_priv;
327 static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
329 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
331 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
334 static inline struct ocfs2_refcount_tree *
335 ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
337 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
340 static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
342 if (lockres->l_ops->get_osb)
343 return lockres->l_ops->get_osb(lockres);
345 return (struct ocfs2_super *)lockres->l_priv;
348 static int ocfs2_lock_create(struct ocfs2_super *osb,
349 struct ocfs2_lock_res *lockres,
350 int level,
351 u32 dlm_flags);
352 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
353 int wanted);
354 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres,
356 int level, unsigned long caller_ip);
357 static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
358 struct ocfs2_lock_res *lockres,
359 int level)
361 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
364 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
365 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
366 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
367 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
368 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *lockres);
370 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
371 int convert);
372 #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
373 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
374 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
375 _err, _func, _lockres->l_name); \
376 else \
377 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
378 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
379 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
380 } while (0)
381 static int ocfs2_downconvert_thread(void *arg);
382 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
383 struct ocfs2_lock_res *lockres);
384 static int ocfs2_inode_lock_update(struct inode *inode,
385 struct buffer_head **bh);
386 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
387 static inline int ocfs2_highest_compat_lock_level(int level);
388 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
389 int new_level);
390 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
391 struct ocfs2_lock_res *lockres,
392 int new_level,
393 int lvb,
394 unsigned int generation);
395 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
396 struct ocfs2_lock_res *lockres);
397 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
398 struct ocfs2_lock_res *lockres);
401 static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
402 u64 blkno,
403 u32 generation,
404 char *name)
406 int len;
408 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
410 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
411 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
412 (long long)blkno, generation);
414 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
416 mlog(0, "built lock resource with name: %s\n", name);
419 static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
421 static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
422 struct ocfs2_dlm_debug *dlm_debug)
424 mlog(0, "Add tracking for lockres %s\n", res->l_name);
426 spin_lock(&ocfs2_dlm_tracking_lock);
427 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
428 spin_unlock(&ocfs2_dlm_tracking_lock);
431 static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
433 spin_lock(&ocfs2_dlm_tracking_lock);
434 if (!list_empty(&res->l_debug_list))
435 list_del_init(&res->l_debug_list);
436 spin_unlock(&ocfs2_dlm_tracking_lock);
439 #ifdef CONFIG_OCFS2_FS_STATS
440 static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
442 res->l_lock_refresh = 0;
443 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
444 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
447 static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
448 struct ocfs2_mask_waiter *mw, int ret)
450 u32 usec;
451 ktime_t kt;
452 struct ocfs2_lock_stats *stats;
454 if (level == LKM_PRMODE)
455 stats = &res->l_lock_prmode;
456 else if (level == LKM_EXMODE)
457 stats = &res->l_lock_exmode;
458 else
459 return;
461 kt = ktime_sub(ktime_get(), mw->mw_lock_start);
462 usec = ktime_to_us(kt);
464 stats->ls_gets++;
465 stats->ls_total += ktime_to_ns(kt);
466 /* overflow */
467 if (unlikely(stats->ls_gets == 0)) {
468 stats->ls_gets++;
469 stats->ls_total = ktime_to_ns(kt);
472 if (stats->ls_max < usec)
473 stats->ls_max = usec;
475 if (ret)
476 stats->ls_fail++;
479 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
481 lockres->l_lock_refresh++;
484 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
486 mw->mw_lock_start = ktime_get();
488 #else
489 static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
492 static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
493 int level, struct ocfs2_mask_waiter *mw, int ret)
496 static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
499 static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
502 #endif
504 static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
505 struct ocfs2_lock_res *res,
506 enum ocfs2_lock_type type,
507 struct ocfs2_lock_res_ops *ops,
508 void *priv)
510 res->l_type = type;
511 res->l_ops = ops;
512 res->l_priv = priv;
514 res->l_level = DLM_LOCK_IV;
515 res->l_requested = DLM_LOCK_IV;
516 res->l_blocking = DLM_LOCK_IV;
517 res->l_action = OCFS2_AST_INVALID;
518 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
520 res->l_flags = OCFS2_LOCK_INITIALIZED;
522 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
524 ocfs2_init_lock_stats(res);
525 #ifdef CONFIG_DEBUG_LOCK_ALLOC
526 if (type != OCFS2_LOCK_TYPE_OPEN)
527 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
528 &lockdep_keys[type], 0);
529 else
530 res->l_lockdep_map.key = NULL;
531 #endif
534 void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
536 /* This also clears out the lock status block */
537 memset(res, 0, sizeof(struct ocfs2_lock_res));
538 spin_lock_init(&res->l_lock);
539 init_waitqueue_head(&res->l_event);
540 INIT_LIST_HEAD(&res->l_blocked_list);
541 INIT_LIST_HEAD(&res->l_mask_waiters);
542 INIT_LIST_HEAD(&res->l_holders);
545 void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
546 enum ocfs2_lock_type type,
547 unsigned int generation,
548 struct inode *inode)
550 struct ocfs2_lock_res_ops *ops;
552 switch(type) {
553 case OCFS2_LOCK_TYPE_RW:
554 ops = &ocfs2_inode_rw_lops;
555 break;
556 case OCFS2_LOCK_TYPE_META:
557 ops = &ocfs2_inode_inode_lops;
558 break;
559 case OCFS2_LOCK_TYPE_OPEN:
560 ops = &ocfs2_inode_open_lops;
561 break;
562 default:
563 mlog_bug_on_msg(1, "type: %d\n", type);
564 ops = NULL; /* thanks, gcc */
565 break;
568 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
569 generation, res->l_name);
570 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
573 static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
575 struct inode *inode = ocfs2_lock_res_inode(lockres);
577 return OCFS2_SB(inode->i_sb);
580 static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
582 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
584 return OCFS2_SB(info->dqi_gi.dqi_sb);
587 static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
589 struct ocfs2_file_private *fp = lockres->l_priv;
591 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
594 static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
596 __be64 inode_blkno_be;
598 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
599 sizeof(__be64));
601 return be64_to_cpu(inode_blkno_be);
604 static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
606 struct ocfs2_dentry_lock *dl = lockres->l_priv;
608 return OCFS2_SB(dl->dl_inode->i_sb);
611 void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
612 u64 parent, struct inode *inode)
614 int len;
615 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
616 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
617 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
619 ocfs2_lock_res_init_once(lockres);
622 * Unfortunately, the standard lock naming scheme won't work
623 * here because we have two 16 byte values to use. Instead,
624 * we'll stuff the inode number as a binary value. We still
625 * want error prints to show something without garbling the
626 * display, so drop a null byte in there before the inode
627 * number. A future version of OCFS2 will likely use all
628 * binary lock names. The stringified names have been a
629 * tremendous aid in debugging, but now that the debugfs
630 * interface exists, we can mangle things there if need be.
632 * NOTE: We also drop the standard "pad" value (the total lock
633 * name size stays the same though - the last part is all
634 * zeros due to the memset in ocfs2_lock_res_init_once()
636 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
637 "%c%016llx",
638 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
639 (long long)parent);
641 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
643 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
644 sizeof(__be64));
646 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
647 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
648 dl);
651 static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
652 struct ocfs2_super *osb)
654 /* Superblock lockres doesn't come from a slab so we call init
655 * once on it manually. */
656 ocfs2_lock_res_init_once(res);
657 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
658 0, res->l_name);
659 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
660 &ocfs2_super_lops, osb);
663 static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
664 struct ocfs2_super *osb)
666 /* Rename lockres doesn't come from a slab so we call init
667 * once on it manually. */
668 ocfs2_lock_res_init_once(res);
669 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
670 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
671 &ocfs2_rename_lops, osb);
674 static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
675 struct ocfs2_super *osb)
677 /* nfs_sync lockres doesn't come from a slab so we call init
678 * once on it manually. */
679 ocfs2_lock_res_init_once(res);
680 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
681 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
682 &ocfs2_nfs_sync_lops, osb);
685 static void ocfs2_nfs_sync_lock_init(struct ocfs2_super *osb)
687 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
688 init_rwsem(&osb->nfs_sync_rwlock);
691 void ocfs2_trim_fs_lock_res_init(struct ocfs2_super *osb)
693 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
695 ocfs2_lock_res_init_once(lockres);
696 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_TRIM_FS, 0, 0, lockres->l_name);
697 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_TRIM_FS,
698 &ocfs2_trim_fs_lops, osb);
701 void ocfs2_trim_fs_lock_res_uninit(struct ocfs2_super *osb)
703 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
705 ocfs2_simple_drop_lockres(osb, lockres);
706 ocfs2_lock_res_free(lockres);
709 static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
710 struct ocfs2_super *osb)
712 ocfs2_lock_res_init_once(res);
713 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
714 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
715 &ocfs2_orphan_scan_lops, osb);
718 void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
719 struct ocfs2_file_private *fp)
721 struct inode *inode = fp->fp_file->f_mapping->host;
722 struct ocfs2_inode_info *oi = OCFS2_I(inode);
724 ocfs2_lock_res_init_once(lockres);
725 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
726 inode->i_generation, lockres->l_name);
727 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
728 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
729 fp);
730 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
733 void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
734 struct ocfs2_mem_dqinfo *info)
736 ocfs2_lock_res_init_once(lockres);
737 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
738 0, lockres->l_name);
739 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
740 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
741 info);
744 void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
745 struct ocfs2_super *osb, u64 ref_blkno,
746 unsigned int generation)
748 ocfs2_lock_res_init_once(lockres);
749 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
750 generation, lockres->l_name);
751 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
752 &ocfs2_refcount_block_lops, osb);
755 void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
757 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
758 return;
760 ocfs2_remove_lockres_tracking(res);
762 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
763 "Lockres %s is on the blocked list\n",
764 res->l_name);
765 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
766 "Lockres %s has mask waiters pending\n",
767 res->l_name);
768 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
769 "Lockres %s is locked\n",
770 res->l_name);
771 mlog_bug_on_msg(res->l_ro_holders,
772 "Lockres %s has %u ro holders\n",
773 res->l_name, res->l_ro_holders);
774 mlog_bug_on_msg(res->l_ex_holders,
775 "Lockres %s has %u ex holders\n",
776 res->l_name, res->l_ex_holders);
778 /* Need to clear out the lock status block for the dlm */
779 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
781 res->l_flags = 0UL;
785 * Keep a list of processes who have interest in a lockres.
786 * Note: this is now only uesed for check recursive cluster locking.
788 static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
789 struct ocfs2_lock_holder *oh)
791 INIT_LIST_HEAD(&oh->oh_list);
792 oh->oh_owner_pid = get_pid(task_pid(current));
794 spin_lock(&lockres->l_lock);
795 list_add_tail(&oh->oh_list, &lockres->l_holders);
796 spin_unlock(&lockres->l_lock);
799 static struct ocfs2_lock_holder *
800 ocfs2_pid_holder(struct ocfs2_lock_res *lockres,
801 struct pid *pid)
803 struct ocfs2_lock_holder *oh;
805 spin_lock(&lockres->l_lock);
806 list_for_each_entry(oh, &lockres->l_holders, oh_list) {
807 if (oh->oh_owner_pid == pid) {
808 spin_unlock(&lockres->l_lock);
809 return oh;
812 spin_unlock(&lockres->l_lock);
813 return NULL;
816 static inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
817 struct ocfs2_lock_holder *oh)
819 spin_lock(&lockres->l_lock);
820 list_del(&oh->oh_list);
821 spin_unlock(&lockres->l_lock);
823 put_pid(oh->oh_owner_pid);
827 static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
828 int level)
830 BUG_ON(!lockres);
832 switch(level) {
833 case DLM_LOCK_EX:
834 lockres->l_ex_holders++;
835 break;
836 case DLM_LOCK_PR:
837 lockres->l_ro_holders++;
838 break;
839 default:
840 BUG();
844 static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
845 int level)
847 BUG_ON(!lockres);
849 switch(level) {
850 case DLM_LOCK_EX:
851 BUG_ON(!lockres->l_ex_holders);
852 lockres->l_ex_holders--;
853 break;
854 case DLM_LOCK_PR:
855 BUG_ON(!lockres->l_ro_holders);
856 lockres->l_ro_holders--;
857 break;
858 default:
859 BUG();
863 /* WARNING: This function lives in a world where the only three lock
864 * levels are EX, PR, and NL. It *will* have to be adjusted when more
865 * lock types are added. */
866 static inline int ocfs2_highest_compat_lock_level(int level)
868 int new_level = DLM_LOCK_EX;
870 if (level == DLM_LOCK_EX)
871 new_level = DLM_LOCK_NL;
872 else if (level == DLM_LOCK_PR)
873 new_level = DLM_LOCK_PR;
874 return new_level;
877 static void lockres_set_flags(struct ocfs2_lock_res *lockres,
878 unsigned long newflags)
880 struct ocfs2_mask_waiter *mw, *tmp;
882 assert_spin_locked(&lockres->l_lock);
884 lockres->l_flags = newflags;
886 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
887 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
888 continue;
890 list_del_init(&mw->mw_item);
891 mw->mw_status = 0;
892 complete(&mw->mw_complete);
895 static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
897 lockres_set_flags(lockres, lockres->l_flags | or);
899 static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
900 unsigned long clear)
902 lockres_set_flags(lockres, lockres->l_flags & ~clear);
905 static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
907 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
908 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
909 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
910 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
912 lockres->l_level = lockres->l_requested;
913 if (lockres->l_level <=
914 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
915 lockres->l_blocking = DLM_LOCK_NL;
916 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
918 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
921 static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
923 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
924 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
926 /* Convert from RO to EX doesn't really need anything as our
927 * information is already up to data. Convert from NL to
928 * *anything* however should mark ourselves as needing an
929 * update */
930 if (lockres->l_level == DLM_LOCK_NL &&
931 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
932 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
934 lockres->l_level = lockres->l_requested;
937 * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
938 * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
939 * downconverting the lock before the upconvert has fully completed.
940 * Do not prevent the dc thread from downconverting if NONBLOCK lock
941 * had already returned.
943 if (!(lockres->l_flags & OCFS2_LOCK_NONBLOCK_FINISHED))
944 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
945 else
946 lockres_clear_flags(lockres, OCFS2_LOCK_NONBLOCK_FINISHED);
948 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
951 static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
953 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
954 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
956 if (lockres->l_requested > DLM_LOCK_NL &&
957 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
958 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
959 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
961 lockres->l_level = lockres->l_requested;
962 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
963 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
966 static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
967 int level)
969 int needs_downconvert = 0;
971 assert_spin_locked(&lockres->l_lock);
973 if (level > lockres->l_blocking) {
974 /* only schedule a downconvert if we haven't already scheduled
975 * one that goes low enough to satisfy the level we're
976 * blocking. this also catches the case where we get
977 * duplicate BASTs */
978 if (ocfs2_highest_compat_lock_level(level) <
979 ocfs2_highest_compat_lock_level(lockres->l_blocking))
980 needs_downconvert = 1;
982 lockres->l_blocking = level;
985 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
986 lockres->l_name, level, lockres->l_level, lockres->l_blocking,
987 needs_downconvert);
989 if (needs_downconvert)
990 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
991 mlog(0, "needs_downconvert = %d\n", needs_downconvert);
992 return needs_downconvert;
996 * OCFS2_LOCK_PENDING and l_pending_gen.
998 * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
999 * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
1000 * for more details on the race.
1002 * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
1003 * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
1004 * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
1005 * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
1006 * the caller is going to try to clear PENDING again. If nothing else is
1007 * happening, __lockres_clear_pending() sees PENDING is unset and does
1008 * nothing.
1010 * But what if another path (eg downconvert thread) has just started a
1011 * new locking action? The other path has re-set PENDING. Our path
1012 * cannot clear PENDING, because that will re-open the original race
1013 * window.
1015 * [Example]
1017 * ocfs2_meta_lock()
1018 * ocfs2_cluster_lock()
1019 * set BUSY
1020 * set PENDING
1021 * drop l_lock
1022 * ocfs2_dlm_lock()
1023 * ocfs2_locking_ast() ocfs2_downconvert_thread()
1024 * clear PENDING ocfs2_unblock_lock()
1025 * take_l_lock
1026 * !BUSY
1027 * ocfs2_prepare_downconvert()
1028 * set BUSY
1029 * set PENDING
1030 * drop l_lock
1031 * take l_lock
1032 * clear PENDING
1033 * drop l_lock
1034 * <window>
1035 * ocfs2_dlm_lock()
1037 * So as you can see, we now have a window where l_lock is not held,
1038 * PENDING is not set, and ocfs2_dlm_lock() has not been called.
1040 * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
1041 * set by ocfs2_prepare_downconvert(). That wasn't nice.
1043 * To solve this we introduce l_pending_gen. A call to
1044 * lockres_clear_pending() will only do so when it is passed a generation
1045 * number that matches the lockres. lockres_set_pending() will return the
1046 * current generation number. When ocfs2_cluster_lock() goes to clear
1047 * PENDING, it passes the generation it got from set_pending(). In our
1048 * example above, the generation numbers will *not* match. Thus,
1049 * ocfs2_cluster_lock() will not clear the PENDING set by
1050 * ocfs2_prepare_downconvert().
1053 /* Unlocked version for ocfs2_locking_ast() */
1054 static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
1055 unsigned int generation,
1056 struct ocfs2_super *osb)
1058 assert_spin_locked(&lockres->l_lock);
1061 * The ast and locking functions can race us here. The winner
1062 * will clear pending, the loser will not.
1064 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
1065 (lockres->l_pending_gen != generation))
1066 return;
1068 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1069 lockres->l_pending_gen++;
1072 * The downconvert thread may have skipped us because we
1073 * were PENDING. Wake it up.
1075 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1076 ocfs2_wake_downconvert_thread(osb);
1079 /* Locked version for callers of ocfs2_dlm_lock() */
1080 static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1081 unsigned int generation,
1082 struct ocfs2_super *osb)
1084 unsigned long flags;
1086 spin_lock_irqsave(&lockres->l_lock, flags);
1087 __lockres_clear_pending(lockres, generation, osb);
1088 spin_unlock_irqrestore(&lockres->l_lock, flags);
1091 static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1093 assert_spin_locked(&lockres->l_lock);
1094 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1096 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1098 return lockres->l_pending_gen;
1101 static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
1103 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1104 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1105 int needs_downconvert;
1106 unsigned long flags;
1108 BUG_ON(level <= DLM_LOCK_NL);
1110 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1111 "type %s\n", lockres->l_name, level, lockres->l_level,
1112 ocfs2_lock_type_string(lockres->l_type));
1115 * We can skip the bast for locks which don't enable caching -
1116 * they'll be dropped at the earliest possible time anyway.
1118 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1119 return;
1121 spin_lock_irqsave(&lockres->l_lock, flags);
1122 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1123 if (needs_downconvert)
1124 ocfs2_schedule_blocked_lock(osb, lockres);
1125 spin_unlock_irqrestore(&lockres->l_lock, flags);
1127 wake_up(&lockres->l_event);
1129 ocfs2_wake_downconvert_thread(osb);
1132 static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
1134 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1135 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1136 unsigned long flags;
1137 int status;
1139 spin_lock_irqsave(&lockres->l_lock, flags);
1141 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1143 if (status == -EAGAIN) {
1144 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1145 goto out;
1148 if (status) {
1149 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1150 lockres->l_name, status);
1151 spin_unlock_irqrestore(&lockres->l_lock, flags);
1152 return;
1155 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
1156 "level %d => %d\n", lockres->l_name, lockres->l_action,
1157 lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
1159 switch(lockres->l_action) {
1160 case OCFS2_AST_ATTACH:
1161 ocfs2_generic_handle_attach_action(lockres);
1162 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1163 break;
1164 case OCFS2_AST_CONVERT:
1165 ocfs2_generic_handle_convert_action(lockres);
1166 break;
1167 case OCFS2_AST_DOWNCONVERT:
1168 ocfs2_generic_handle_downconvert_action(lockres);
1169 break;
1170 default:
1171 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1172 "flags 0x%lx, unlock: %u\n",
1173 lockres->l_name, lockres->l_action, lockres->l_flags,
1174 lockres->l_unlock_action);
1175 BUG();
1177 out:
1178 /* set it to something invalid so if we get called again we
1179 * can catch it. */
1180 lockres->l_action = OCFS2_AST_INVALID;
1182 /* Did we try to cancel this lock? Clear that state */
1183 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1184 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1187 * We may have beaten the locking functions here. We certainly
1188 * know that dlm_lock() has been called :-)
1189 * Because we can't have two lock calls in flight at once, we
1190 * can use lockres->l_pending_gen.
1192 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1194 wake_up(&lockres->l_event);
1195 spin_unlock_irqrestore(&lockres->l_lock, flags);
1198 static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
1200 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1201 unsigned long flags;
1203 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1204 lockres->l_name, lockres->l_unlock_action);
1206 spin_lock_irqsave(&lockres->l_lock, flags);
1207 if (error) {
1208 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
1209 "unlock_action %d\n", error, lockres->l_name,
1210 lockres->l_unlock_action);
1211 spin_unlock_irqrestore(&lockres->l_lock, flags);
1212 return;
1215 switch(lockres->l_unlock_action) {
1216 case OCFS2_UNLOCK_CANCEL_CONVERT:
1217 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
1218 lockres->l_action = OCFS2_AST_INVALID;
1219 /* Downconvert thread may have requeued this lock, we
1220 * need to wake it. */
1221 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1222 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
1223 break;
1224 case OCFS2_UNLOCK_DROP_LOCK:
1225 lockres->l_level = DLM_LOCK_IV;
1226 break;
1227 default:
1228 BUG();
1231 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1232 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1233 wake_up(&lockres->l_event);
1234 spin_unlock_irqrestore(&lockres->l_lock, flags);
1238 * This is the filesystem locking protocol. It provides the lock handling
1239 * hooks for the underlying DLM. It has a maximum version number.
1240 * The version number allows interoperability with systems running at
1241 * the same major number and an equal or smaller minor number.
1243 * Whenever the filesystem does new things with locks (adds or removes a
1244 * lock, orders them differently, does different things underneath a lock),
1245 * the version must be changed. The protocol is negotiated when joining
1246 * the dlm domain. A node may join the domain if its major version is
1247 * identical to all other nodes and its minor version is greater than
1248 * or equal to all other nodes. When its minor version is greater than
1249 * the other nodes, it will run at the minor version specified by the
1250 * other nodes.
1252 * If a locking change is made that will not be compatible with older
1253 * versions, the major number must be increased and the minor version set
1254 * to zero. If a change merely adds a behavior that can be disabled when
1255 * speaking to older versions, the minor version must be increased. If a
1256 * change adds a fully backwards compatible change (eg, LVB changes that
1257 * are just ignored by older versions), the version does not need to be
1258 * updated.
1260 static struct ocfs2_locking_protocol lproto = {
1261 .lp_max_version = {
1262 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
1263 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
1265 .lp_lock_ast = ocfs2_locking_ast,
1266 .lp_blocking_ast = ocfs2_blocking_ast,
1267 .lp_unlock_ast = ocfs2_unlock_ast,
1270 void ocfs2_set_locking_protocol(void)
1272 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1275 static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1276 int convert)
1278 unsigned long flags;
1280 spin_lock_irqsave(&lockres->l_lock, flags);
1281 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1282 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1283 if (convert)
1284 lockres->l_action = OCFS2_AST_INVALID;
1285 else
1286 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1287 spin_unlock_irqrestore(&lockres->l_lock, flags);
1289 wake_up(&lockres->l_event);
1292 /* Note: If we detect another process working on the lock (i.e.,
1293 * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
1294 * to do the right thing in that case.
1296 static int ocfs2_lock_create(struct ocfs2_super *osb,
1297 struct ocfs2_lock_res *lockres,
1298 int level,
1299 u32 dlm_flags)
1301 int ret = 0;
1302 unsigned long flags;
1303 unsigned int gen;
1305 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1306 dlm_flags);
1308 spin_lock_irqsave(&lockres->l_lock, flags);
1309 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1310 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1311 spin_unlock_irqrestore(&lockres->l_lock, flags);
1312 goto bail;
1315 lockres->l_action = OCFS2_AST_ATTACH;
1316 lockres->l_requested = level;
1317 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1318 gen = lockres_set_pending(lockres);
1319 spin_unlock_irqrestore(&lockres->l_lock, flags);
1321 ret = ocfs2_dlm_lock(osb->cconn,
1322 level,
1323 &lockres->l_lksb,
1324 dlm_flags,
1325 lockres->l_name,
1326 OCFS2_LOCK_ID_MAX_LEN - 1);
1327 lockres_clear_pending(lockres, gen, osb);
1328 if (ret) {
1329 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1330 ocfs2_recover_from_dlm_error(lockres, 1);
1333 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1335 bail:
1336 return ret;
1339 static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1340 int flag)
1342 unsigned long flags;
1343 int ret;
1345 spin_lock_irqsave(&lockres->l_lock, flags);
1346 ret = lockres->l_flags & flag;
1347 spin_unlock_irqrestore(&lockres->l_lock, flags);
1349 return ret;
1352 static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1355 wait_event(lockres->l_event,
1356 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1359 static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1362 wait_event(lockres->l_event,
1363 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1366 /* predict what lock level we'll be dropping down to on behalf
1367 * of another node, and return true if the currently wanted
1368 * level will be compatible with it. */
1369 static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1370 int wanted)
1372 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1374 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1377 static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1379 INIT_LIST_HEAD(&mw->mw_item);
1380 init_completion(&mw->mw_complete);
1381 ocfs2_init_start_time(mw);
1384 static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1386 wait_for_completion(&mw->mw_complete);
1387 /* Re-arm the completion in case we want to wait on it again */
1388 reinit_completion(&mw->mw_complete);
1389 return mw->mw_status;
1392 static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1393 struct ocfs2_mask_waiter *mw,
1394 unsigned long mask,
1395 unsigned long goal)
1397 BUG_ON(!list_empty(&mw->mw_item));
1399 assert_spin_locked(&lockres->l_lock);
1401 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1402 mw->mw_mask = mask;
1403 mw->mw_goal = goal;
1406 /* returns 0 if the mw that was removed was already satisfied, -EBUSY
1407 * if the mask still hadn't reached its goal */
1408 static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1409 struct ocfs2_mask_waiter *mw)
1411 int ret = 0;
1413 assert_spin_locked(&lockres->l_lock);
1414 if (!list_empty(&mw->mw_item)) {
1415 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1416 ret = -EBUSY;
1418 list_del_init(&mw->mw_item);
1419 init_completion(&mw->mw_complete);
1422 return ret;
1425 static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1426 struct ocfs2_mask_waiter *mw)
1428 unsigned long flags;
1429 int ret = 0;
1431 spin_lock_irqsave(&lockres->l_lock, flags);
1432 ret = __lockres_remove_mask_waiter(lockres, mw);
1433 spin_unlock_irqrestore(&lockres->l_lock, flags);
1435 return ret;
1439 static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1440 struct ocfs2_lock_res *lockres)
1442 int ret;
1444 ret = wait_for_completion_interruptible(&mw->mw_complete);
1445 if (ret)
1446 lockres_remove_mask_waiter(lockres, mw);
1447 else
1448 ret = mw->mw_status;
1449 /* Re-arm the completion in case we want to wait on it again */
1450 reinit_completion(&mw->mw_complete);
1451 return ret;
1454 static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1455 struct ocfs2_lock_res *lockres,
1456 int level,
1457 u32 lkm_flags,
1458 int arg_flags,
1459 int l_subclass,
1460 unsigned long caller_ip)
1462 struct ocfs2_mask_waiter mw;
1463 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1464 int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
1465 unsigned long flags;
1466 unsigned int gen;
1467 int noqueue_attempted = 0;
1468 int dlm_locked = 0;
1469 int kick_dc = 0;
1471 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) {
1472 mlog_errno(-EINVAL);
1473 return -EINVAL;
1476 ocfs2_init_mask_waiter(&mw);
1478 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1479 lkm_flags |= DLM_LKF_VALBLK;
1481 again:
1482 wait = 0;
1484 spin_lock_irqsave(&lockres->l_lock, flags);
1486 if (catch_signals && signal_pending(current)) {
1487 ret = -ERESTARTSYS;
1488 goto unlock;
1491 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1492 "Cluster lock called on freeing lockres %s! flags "
1493 "0x%lx\n", lockres->l_name, lockres->l_flags);
1495 /* We only compare against the currently granted level
1496 * here. If the lock is blocked waiting on a downconvert,
1497 * we'll get caught below. */
1498 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1499 level > lockres->l_level) {
1500 /* is someone sitting in dlm_lock? If so, wait on
1501 * them. */
1502 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1503 wait = 1;
1504 goto unlock;
1507 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1509 * We've upconverted. If the lock now has a level we can
1510 * work with, we take it. If, however, the lock is not at the
1511 * required level, we go thru the full cycle. One way this could
1512 * happen is if a process requesting an upconvert to PR is
1513 * closely followed by another requesting upconvert to an EX.
1514 * If the process requesting EX lands here, we want it to
1515 * continue attempting to upconvert and let the process
1516 * requesting PR take the lock.
1517 * If multiple processes request upconvert to PR, the first one
1518 * here will take the lock. The others will have to go thru the
1519 * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
1520 * downconvert request.
1522 if (level <= lockres->l_level)
1523 goto update_holders;
1526 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1527 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1528 /* is the lock is currently blocked on behalf of
1529 * another node */
1530 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1531 wait = 1;
1532 goto unlock;
1535 if (level > lockres->l_level) {
1536 if (noqueue_attempted > 0) {
1537 ret = -EAGAIN;
1538 goto unlock;
1540 if (lkm_flags & DLM_LKF_NOQUEUE)
1541 noqueue_attempted = 1;
1543 if (lockres->l_action != OCFS2_AST_INVALID)
1544 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1545 lockres->l_name, lockres->l_action);
1547 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1548 lockres->l_action = OCFS2_AST_ATTACH;
1549 lkm_flags &= ~DLM_LKF_CONVERT;
1550 } else {
1551 lockres->l_action = OCFS2_AST_CONVERT;
1552 lkm_flags |= DLM_LKF_CONVERT;
1555 lockres->l_requested = level;
1556 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1557 gen = lockres_set_pending(lockres);
1558 spin_unlock_irqrestore(&lockres->l_lock, flags);
1560 BUG_ON(level == DLM_LOCK_IV);
1561 BUG_ON(level == DLM_LOCK_NL);
1563 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
1564 lockres->l_name, lockres->l_level, level);
1566 /* call dlm_lock to upgrade lock now */
1567 ret = ocfs2_dlm_lock(osb->cconn,
1568 level,
1569 &lockres->l_lksb,
1570 lkm_flags,
1571 lockres->l_name,
1572 OCFS2_LOCK_ID_MAX_LEN - 1);
1573 lockres_clear_pending(lockres, gen, osb);
1574 if (ret) {
1575 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1576 (ret != -EAGAIN)) {
1577 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1578 ret, lockres);
1580 ocfs2_recover_from_dlm_error(lockres, 1);
1581 goto out;
1583 dlm_locked = 1;
1585 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1586 lockres->l_name);
1588 /* At this point we've gone inside the dlm and need to
1589 * complete our work regardless. */
1590 catch_signals = 0;
1592 /* wait for busy to clear and carry on */
1593 goto again;
1596 update_holders:
1597 /* Ok, if we get here then we're good to go. */
1598 ocfs2_inc_holders(lockres, level);
1600 ret = 0;
1601 unlock:
1602 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1604 /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */
1605 kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED);
1607 spin_unlock_irqrestore(&lockres->l_lock, flags);
1608 if (kick_dc)
1609 ocfs2_wake_downconvert_thread(osb);
1610 out:
1612 * This is helping work around a lock inversion between the page lock
1613 * and dlm locks. One path holds the page lock while calling aops
1614 * which block acquiring dlm locks. The voting thread holds dlm
1615 * locks while acquiring page locks while down converting data locks.
1616 * This block is helping an aop path notice the inversion and back
1617 * off to unlock its page lock before trying the dlm lock again.
1619 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1620 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1621 wait = 0;
1622 spin_lock_irqsave(&lockres->l_lock, flags);
1623 if (__lockres_remove_mask_waiter(lockres, &mw)) {
1624 if (dlm_locked)
1625 lockres_or_flags(lockres,
1626 OCFS2_LOCK_NONBLOCK_FINISHED);
1627 spin_unlock_irqrestore(&lockres->l_lock, flags);
1628 ret = -EAGAIN;
1629 } else {
1630 spin_unlock_irqrestore(&lockres->l_lock, flags);
1631 goto again;
1634 if (wait) {
1635 ret = ocfs2_wait_for_mask(&mw);
1636 if (ret == 0)
1637 goto again;
1638 mlog_errno(ret);
1640 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1642 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1643 if (!ret && lockres->l_lockdep_map.key != NULL) {
1644 if (level == DLM_LOCK_PR)
1645 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1646 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1647 caller_ip);
1648 else
1649 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1650 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1651 caller_ip);
1653 #endif
1654 return ret;
1657 static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1658 struct ocfs2_lock_res *lockres,
1659 int level,
1660 u32 lkm_flags,
1661 int arg_flags)
1663 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1664 0, _RET_IP_);
1668 static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1669 struct ocfs2_lock_res *lockres,
1670 int level,
1671 unsigned long caller_ip)
1673 unsigned long flags;
1675 spin_lock_irqsave(&lockres->l_lock, flags);
1676 ocfs2_dec_holders(lockres, level);
1677 ocfs2_downconvert_on_unlock(osb, lockres);
1678 spin_unlock_irqrestore(&lockres->l_lock, flags);
1679 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1680 if (lockres->l_lockdep_map.key != NULL)
1681 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1682 #endif
1685 static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1686 struct ocfs2_lock_res *lockres,
1687 int ex,
1688 int local)
1690 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1691 unsigned long flags;
1692 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1694 spin_lock_irqsave(&lockres->l_lock, flags);
1695 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1696 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1697 spin_unlock_irqrestore(&lockres->l_lock, flags);
1699 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1702 /* Grants us an EX lock on the data and metadata resources, skipping
1703 * the normal cluster directory lookup. Use this ONLY on newly created
1704 * inodes which other nodes can't possibly see, and which haven't been
1705 * hashed in the inode hash yet. This can give us a good performance
1706 * increase as it'll skip the network broadcast normally associated
1707 * with creating a new lock resource. */
1708 int ocfs2_create_new_inode_locks(struct inode *inode)
1710 int ret;
1711 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1713 BUG_ON(!ocfs2_inode_is_new(inode));
1715 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1717 /* NOTE: That we don't increment any of the holder counts, nor
1718 * do we add anything to a journal handle. Since this is
1719 * supposed to be a new inode which the cluster doesn't know
1720 * about yet, there is no need to. As far as the LVB handling
1721 * is concerned, this is basically like acquiring an EX lock
1722 * on a resource which has an invalid one -- we'll set it
1723 * valid when we release the EX. */
1725 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1726 if (ret) {
1727 mlog_errno(ret);
1728 goto bail;
1732 * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
1733 * don't use a generation in their lock names.
1735 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1736 if (ret) {
1737 mlog_errno(ret);
1738 goto bail;
1741 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1742 if (ret)
1743 mlog_errno(ret);
1745 bail:
1746 return ret;
1749 int ocfs2_rw_lock(struct inode *inode, int write)
1751 int status, level;
1752 struct ocfs2_lock_res *lockres;
1753 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1755 mlog(0, "inode %llu take %s RW lock\n",
1756 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1757 write ? "EXMODE" : "PRMODE");
1759 if (ocfs2_mount_local(osb))
1760 return 0;
1762 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1764 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1766 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1767 if (status < 0)
1768 mlog_errno(status);
1770 return status;
1773 int ocfs2_try_rw_lock(struct inode *inode, int write)
1775 int status, level;
1776 struct ocfs2_lock_res *lockres;
1777 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1779 mlog(0, "inode %llu try to take %s RW lock\n",
1780 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1781 write ? "EXMODE" : "PRMODE");
1783 if (ocfs2_mount_local(osb))
1784 return 0;
1786 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1788 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1790 status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
1791 return status;
1794 void ocfs2_rw_unlock(struct inode *inode, int write)
1796 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1797 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1798 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1800 mlog(0, "inode %llu drop %s RW lock\n",
1801 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1802 write ? "EXMODE" : "PRMODE");
1804 if (!ocfs2_mount_local(osb))
1805 ocfs2_cluster_unlock(osb, lockres, level);
1809 * ocfs2_open_lock always get PR mode lock.
1811 int ocfs2_open_lock(struct inode *inode)
1813 int status = 0;
1814 struct ocfs2_lock_res *lockres;
1815 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1817 mlog(0, "inode %llu take PRMODE open lock\n",
1818 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1820 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
1821 goto out;
1823 lockres = &OCFS2_I(inode)->ip_open_lockres;
1825 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_PR, 0, 0);
1826 if (status < 0)
1827 mlog_errno(status);
1829 out:
1830 return status;
1833 int ocfs2_try_open_lock(struct inode *inode, int write)
1835 int status = 0, level;
1836 struct ocfs2_lock_res *lockres;
1837 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1839 mlog(0, "inode %llu try to take %s open lock\n",
1840 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1841 write ? "EXMODE" : "PRMODE");
1843 if (ocfs2_is_hard_readonly(osb)) {
1844 if (write)
1845 status = -EROFS;
1846 goto out;
1849 if (ocfs2_mount_local(osb))
1850 goto out;
1852 lockres = &OCFS2_I(inode)->ip_open_lockres;
1854 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1857 * The file system may already holding a PRMODE/EXMODE open lock.
1858 * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
1859 * other nodes and the -EAGAIN will indicate to the caller that
1860 * this inode is still in use.
1862 status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
1864 out:
1865 return status;
1869 * ocfs2_open_unlock unlock PR and EX mode open locks.
1871 void ocfs2_open_unlock(struct inode *inode)
1873 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1874 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1876 mlog(0, "inode %llu drop open lock\n",
1877 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1879 if (ocfs2_mount_local(osb))
1880 goto out;
1882 if(lockres->l_ro_holders)
1883 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_PR);
1884 if(lockres->l_ex_holders)
1885 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
1887 out:
1888 return;
1891 static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1892 int level)
1894 int ret;
1895 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1896 unsigned long flags;
1897 struct ocfs2_mask_waiter mw;
1899 ocfs2_init_mask_waiter(&mw);
1901 retry_cancel:
1902 spin_lock_irqsave(&lockres->l_lock, flags);
1903 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1904 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1905 if (ret) {
1906 spin_unlock_irqrestore(&lockres->l_lock, flags);
1907 ret = ocfs2_cancel_convert(osb, lockres);
1908 if (ret < 0) {
1909 mlog_errno(ret);
1910 goto out;
1912 goto retry_cancel;
1914 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1915 spin_unlock_irqrestore(&lockres->l_lock, flags);
1917 ocfs2_wait_for_mask(&mw);
1918 goto retry_cancel;
1921 ret = -ERESTARTSYS;
1923 * We may still have gotten the lock, in which case there's no
1924 * point to restarting the syscall.
1926 if (lockres->l_level == level)
1927 ret = 0;
1929 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1930 lockres->l_flags, lockres->l_level, lockres->l_action);
1932 spin_unlock_irqrestore(&lockres->l_lock, flags);
1934 out:
1935 return ret;
1939 * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
1940 * flock() calls. The locking approach this requires is sufficiently
1941 * different from all other cluster lock types that we implement a
1942 * separate path to the "low-level" dlm calls. In particular:
1944 * - No optimization of lock levels is done - we take at exactly
1945 * what's been requested.
1947 * - No lock caching is employed. We immediately downconvert to
1948 * no-lock at unlock time. This also means flock locks never go on
1949 * the blocking list).
1951 * - Since userspace can trivially deadlock itself with flock, we make
1952 * sure to allow cancellation of a misbehaving applications flock()
1953 * request.
1955 * - Access to any flock lockres doesn't require concurrency, so we
1956 * can simplify the code by requiring the caller to guarantee
1957 * serialization of dlmglue flock calls.
1959 int ocfs2_file_lock(struct file *file, int ex, int trylock)
1961 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1962 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1963 unsigned long flags;
1964 struct ocfs2_file_private *fp = file->private_data;
1965 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1966 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1967 struct ocfs2_mask_waiter mw;
1969 ocfs2_init_mask_waiter(&mw);
1971 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1972 (lockres->l_level > DLM_LOCK_NL)) {
1973 mlog(ML_ERROR,
1974 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1975 "level: %u\n", lockres->l_name, lockres->l_flags,
1976 lockres->l_level);
1977 return -EINVAL;
1980 spin_lock_irqsave(&lockres->l_lock, flags);
1981 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1982 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1983 spin_unlock_irqrestore(&lockres->l_lock, flags);
1986 * Get the lock at NLMODE to start - that way we
1987 * can cancel the upconvert request if need be.
1989 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1990 if (ret < 0) {
1991 mlog_errno(ret);
1992 goto out;
1995 ret = ocfs2_wait_for_mask(&mw);
1996 if (ret) {
1997 mlog_errno(ret);
1998 goto out;
2000 spin_lock_irqsave(&lockres->l_lock, flags);
2003 lockres->l_action = OCFS2_AST_CONVERT;
2004 lkm_flags |= DLM_LKF_CONVERT;
2005 lockres->l_requested = level;
2006 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2008 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2009 spin_unlock_irqrestore(&lockres->l_lock, flags);
2011 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
2012 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
2013 if (ret) {
2014 if (!trylock || (ret != -EAGAIN)) {
2015 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
2016 ret = -EINVAL;
2019 ocfs2_recover_from_dlm_error(lockres, 1);
2020 lockres_remove_mask_waiter(lockres, &mw);
2021 goto out;
2024 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
2025 if (ret == -ERESTARTSYS) {
2027 * Userspace can cause deadlock itself with
2028 * flock(). Current behavior locally is to allow the
2029 * deadlock, but abort the system call if a signal is
2030 * received. We follow this example, otherwise a
2031 * poorly written program could sit in kernel until
2032 * reboot.
2034 * Handling this is a bit more complicated for Ocfs2
2035 * though. We can't exit this function with an
2036 * outstanding lock request, so a cancel convert is
2037 * required. We intentionally overwrite 'ret' - if the
2038 * cancel fails and the lock was granted, it's easier
2039 * to just bubble success back up to the user.
2041 ret = ocfs2_flock_handle_signal(lockres, level);
2042 } else if (!ret && (level > lockres->l_level)) {
2043 /* Trylock failed asynchronously */
2044 BUG_ON(!trylock);
2045 ret = -EAGAIN;
2048 out:
2050 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
2051 lockres->l_name, ex, trylock, ret);
2052 return ret;
2055 void ocfs2_file_unlock(struct file *file)
2057 int ret;
2058 unsigned int gen;
2059 unsigned long flags;
2060 struct ocfs2_file_private *fp = file->private_data;
2061 struct ocfs2_lock_res *lockres = &fp->fp_flock;
2062 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
2063 struct ocfs2_mask_waiter mw;
2065 ocfs2_init_mask_waiter(&mw);
2067 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
2068 return;
2070 if (lockres->l_level == DLM_LOCK_NL)
2071 return;
2073 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
2074 lockres->l_name, lockres->l_flags, lockres->l_level,
2075 lockres->l_action);
2077 spin_lock_irqsave(&lockres->l_lock, flags);
2079 * Fake a blocking ast for the downconvert code.
2081 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
2082 lockres->l_blocking = DLM_LOCK_EX;
2084 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
2085 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2086 spin_unlock_irqrestore(&lockres->l_lock, flags);
2088 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
2089 if (ret) {
2090 mlog_errno(ret);
2091 return;
2094 ret = ocfs2_wait_for_mask(&mw);
2095 if (ret)
2096 mlog_errno(ret);
2099 static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
2100 struct ocfs2_lock_res *lockres)
2102 int kick = 0;
2104 /* If we know that another node is waiting on our lock, kick
2105 * the downconvert thread * pre-emptively when we reach a release
2106 * condition. */
2107 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
2108 switch(lockres->l_blocking) {
2109 case DLM_LOCK_EX:
2110 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
2111 kick = 1;
2112 break;
2113 case DLM_LOCK_PR:
2114 if (!lockres->l_ex_holders)
2115 kick = 1;
2116 break;
2117 default:
2118 BUG();
2122 if (kick)
2123 ocfs2_wake_downconvert_thread(osb);
2126 #define OCFS2_SEC_BITS 34
2127 #define OCFS2_SEC_SHIFT (64 - 34)
2128 #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2130 /* LVB only has room for 64 bits of time here so we pack it for
2131 * now. */
2132 static u64 ocfs2_pack_timespec(struct timespec *spec)
2134 u64 res;
2135 u64 sec = spec->tv_sec;
2136 u32 nsec = spec->tv_nsec;
2138 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
2140 return res;
2143 /* Call this with the lockres locked. I am reasonably sure we don't
2144 * need ip_lock in this function as anyone who would be changing those
2145 * values is supposed to be blocked in ocfs2_inode_lock right now. */
2146 static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2148 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2149 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2150 struct ocfs2_meta_lvb *lvb;
2151 struct timespec ts;
2153 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2156 * Invalidate the LVB of a deleted inode - this way other
2157 * nodes are forced to go to disk and discover the new inode
2158 * status.
2160 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2161 lvb->lvb_version = 0;
2162 goto out;
2165 lvb->lvb_version = OCFS2_LVB_VERSION;
2166 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2167 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
2168 lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
2169 lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
2170 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2171 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2172 ts = timespec64_to_timespec(inode->i_atime);
2173 lvb->lvb_iatime_packed =
2174 cpu_to_be64(ocfs2_pack_timespec(&ts));
2175 ts = timespec64_to_timespec(inode->i_ctime);
2176 lvb->lvb_ictime_packed =
2177 cpu_to_be64(ocfs2_pack_timespec(&ts));
2178 ts = timespec64_to_timespec(inode->i_mtime);
2179 lvb->lvb_imtime_packed =
2180 cpu_to_be64(ocfs2_pack_timespec(&ts));
2181 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
2182 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
2183 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
2185 out:
2186 mlog_meta_lvb(0, lockres);
2189 static void ocfs2_unpack_timespec(struct timespec *spec,
2190 u64 packed_time)
2192 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2193 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2196 static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2198 struct timespec ts;
2199 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2200 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2201 struct ocfs2_meta_lvb *lvb;
2203 mlog_meta_lvb(0, lockres);
2205 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2207 /* We're safe here without the lockres lock... */
2208 spin_lock(&oi->ip_lock);
2209 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2210 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2212 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
2213 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
2214 ocfs2_set_inode_flags(inode);
2216 /* fast-symlinks are a special case */
2217 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2218 inode->i_blocks = 0;
2219 else
2220 inode->i_blocks = ocfs2_inode_sector_count(inode);
2222 i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
2223 i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
2224 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2225 set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
2226 ocfs2_unpack_timespec(&ts,
2227 be64_to_cpu(lvb->lvb_iatime_packed));
2228 inode->i_atime = timespec_to_timespec64(ts);
2229 ocfs2_unpack_timespec(&ts,
2230 be64_to_cpu(lvb->lvb_imtime_packed));
2231 inode->i_mtime = timespec_to_timespec64(ts);
2232 ocfs2_unpack_timespec(&ts,
2233 be64_to_cpu(lvb->lvb_ictime_packed));
2234 inode->i_ctime = timespec_to_timespec64(ts);
2235 spin_unlock(&oi->ip_lock);
2238 static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2239 struct ocfs2_lock_res *lockres)
2241 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2243 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2244 && lvb->lvb_version == OCFS2_LVB_VERSION
2245 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
2246 return 1;
2247 return 0;
2250 /* Determine whether a lock resource needs to be refreshed, and
2251 * arbitrate who gets to refresh it.
2253 * 0 means no refresh needed.
2255 * > 0 means you need to refresh this and you MUST call
2256 * ocfs2_complete_lock_res_refresh afterwards. */
2257 static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2259 unsigned long flags;
2260 int status = 0;
2262 refresh_check:
2263 spin_lock_irqsave(&lockres->l_lock, flags);
2264 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2265 spin_unlock_irqrestore(&lockres->l_lock, flags);
2266 goto bail;
2269 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2270 spin_unlock_irqrestore(&lockres->l_lock, flags);
2272 ocfs2_wait_on_refreshing_lock(lockres);
2273 goto refresh_check;
2276 /* Ok, I'll be the one to refresh this lock. */
2277 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2278 spin_unlock_irqrestore(&lockres->l_lock, flags);
2280 status = 1;
2281 bail:
2282 mlog(0, "status %d\n", status);
2283 return status;
2286 /* If status is non zero, I'll mark it as not being in refresh
2287 * anymroe, but i won't clear the needs refresh flag. */
2288 static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2289 int status)
2291 unsigned long flags;
2293 spin_lock_irqsave(&lockres->l_lock, flags);
2294 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2295 if (!status)
2296 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2297 spin_unlock_irqrestore(&lockres->l_lock, flags);
2299 wake_up(&lockres->l_event);
2302 /* may or may not return a bh if it went to disk. */
2303 static int ocfs2_inode_lock_update(struct inode *inode,
2304 struct buffer_head **bh)
2306 int status = 0;
2307 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2308 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2309 struct ocfs2_dinode *fe;
2310 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2312 if (ocfs2_mount_local(osb))
2313 goto bail;
2315 spin_lock(&oi->ip_lock);
2316 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2317 mlog(0, "Orphaned inode %llu was deleted while we "
2318 "were waiting on a lock. ip_flags = 0x%x\n",
2319 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2320 spin_unlock(&oi->ip_lock);
2321 status = -ENOENT;
2322 goto bail;
2324 spin_unlock(&oi->ip_lock);
2326 if (!ocfs2_should_refresh_lock_res(lockres))
2327 goto bail;
2329 /* This will discard any caching information we might have had
2330 * for the inode metadata. */
2331 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
2333 ocfs2_extent_map_trunc(inode, 0);
2335 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2336 mlog(0, "Trusting LVB on inode %llu\n",
2337 (unsigned long long)oi->ip_blkno);
2338 ocfs2_refresh_inode_from_lvb(inode);
2339 } else {
2340 /* Boo, we have to go to disk. */
2341 /* read bh, cast, ocfs2_refresh_inode */
2342 status = ocfs2_read_inode_block(inode, bh);
2343 if (status < 0) {
2344 mlog_errno(status);
2345 goto bail_refresh;
2347 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2349 /* This is a good chance to make sure we're not
2350 * locking an invalid object. ocfs2_read_inode_block()
2351 * already checked that the inode block is sane.
2353 * We bug on a stale inode here because we checked
2354 * above whether it was wiped from disk. The wiping
2355 * node provides a guarantee that we receive that
2356 * message and can mark the inode before dropping any
2357 * locks associated with it. */
2358 mlog_bug_on_msg(inode->i_generation !=
2359 le32_to_cpu(fe->i_generation),
2360 "Invalid dinode %llu disk generation: %u "
2361 "inode->i_generation: %u\n",
2362 (unsigned long long)oi->ip_blkno,
2363 le32_to_cpu(fe->i_generation),
2364 inode->i_generation);
2365 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2366 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2367 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2368 (unsigned long long)oi->ip_blkno,
2369 (unsigned long long)le64_to_cpu(fe->i_dtime),
2370 le32_to_cpu(fe->i_flags));
2372 ocfs2_refresh_inode(inode, fe);
2373 ocfs2_track_lock_refresh(lockres);
2376 status = 0;
2377 bail_refresh:
2378 ocfs2_complete_lock_res_refresh(lockres, status);
2379 bail:
2380 return status;
2383 static int ocfs2_assign_bh(struct inode *inode,
2384 struct buffer_head **ret_bh,
2385 struct buffer_head *passed_bh)
2387 int status;
2389 if (passed_bh) {
2390 /* Ok, the update went to disk for us, use the
2391 * returned bh. */
2392 *ret_bh = passed_bh;
2393 get_bh(*ret_bh);
2395 return 0;
2398 status = ocfs2_read_inode_block(inode, ret_bh);
2399 if (status < 0)
2400 mlog_errno(status);
2402 return status;
2406 * returns < 0 error if the callback will never be called, otherwise
2407 * the result of the lock will be communicated via the callback.
2409 int ocfs2_inode_lock_full_nested(struct inode *inode,
2410 struct buffer_head **ret_bh,
2411 int ex,
2412 int arg_flags,
2413 int subclass)
2415 int status, level, acquired;
2416 u32 dlm_flags;
2417 struct ocfs2_lock_res *lockres = NULL;
2418 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2419 struct buffer_head *local_bh = NULL;
2421 mlog(0, "inode %llu, take %s META lock\n",
2422 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2423 ex ? "EXMODE" : "PRMODE");
2425 status = 0;
2426 acquired = 0;
2427 /* We'll allow faking a readonly metadata lock for
2428 * rodevices. */
2429 if (ocfs2_is_hard_readonly(osb)) {
2430 if (ex)
2431 status = -EROFS;
2432 goto getbh;
2435 if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
2436 ocfs2_mount_local(osb))
2437 goto update;
2439 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2440 ocfs2_wait_for_recovery(osb);
2442 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2443 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2444 dlm_flags = 0;
2445 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2446 dlm_flags |= DLM_LKF_NOQUEUE;
2448 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2449 arg_flags, subclass, _RET_IP_);
2450 if (status < 0) {
2451 if (status != -EAGAIN)
2452 mlog_errno(status);
2453 goto bail;
2456 /* Notify the error cleanup path to drop the cluster lock. */
2457 acquired = 1;
2459 /* We wait twice because a node may have died while we were in
2460 * the lower dlm layers. The second time though, we've
2461 * committed to owning this lock so we don't allow signals to
2462 * abort the operation. */
2463 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2464 ocfs2_wait_for_recovery(osb);
2466 update:
2468 * We only see this flag if we're being called from
2469 * ocfs2_read_locked_inode(). It means we're locking an inode
2470 * which hasn't been populated yet, so clear the refresh flag
2471 * and let the caller handle it.
2473 if (inode->i_state & I_NEW) {
2474 status = 0;
2475 if (lockres)
2476 ocfs2_complete_lock_res_refresh(lockres, 0);
2477 goto bail;
2480 /* This is fun. The caller may want a bh back, or it may
2481 * not. ocfs2_inode_lock_update definitely wants one in, but
2482 * may or may not read one, depending on what's in the
2483 * LVB. The result of all of this is that we've *only* gone to
2484 * disk if we have to, so the complexity is worthwhile. */
2485 status = ocfs2_inode_lock_update(inode, &local_bh);
2486 if (status < 0) {
2487 if (status != -ENOENT)
2488 mlog_errno(status);
2489 goto bail;
2491 getbh:
2492 if (ret_bh) {
2493 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2494 if (status < 0) {
2495 mlog_errno(status);
2496 goto bail;
2500 bail:
2501 if (status < 0) {
2502 if (ret_bh && (*ret_bh)) {
2503 brelse(*ret_bh);
2504 *ret_bh = NULL;
2506 if (acquired)
2507 ocfs2_inode_unlock(inode, ex);
2510 if (local_bh)
2511 brelse(local_bh);
2513 return status;
2517 * This is working around a lock inversion between tasks acquiring DLM
2518 * locks while holding a page lock and the downconvert thread which
2519 * blocks dlm lock acquiry while acquiring page locks.
2521 * ** These _with_page variantes are only intended to be called from aop
2522 * methods that hold page locks and return a very specific *positive* error
2523 * code that aop methods pass up to the VFS -- test for errors with != 0. **
2525 * The DLM is called such that it returns -EAGAIN if it would have
2526 * blocked waiting for the downconvert thread. In that case we unlock
2527 * our page so the downconvert thread can make progress. Once we've
2528 * done this we have to return AOP_TRUNCATED_PAGE so the aop method
2529 * that called us can bubble that back up into the VFS who will then
2530 * immediately retry the aop call.
2532 int ocfs2_inode_lock_with_page(struct inode *inode,
2533 struct buffer_head **ret_bh,
2534 int ex,
2535 struct page *page)
2537 int ret;
2539 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2540 if (ret == -EAGAIN) {
2541 unlock_page(page);
2543 * If we can't get inode lock immediately, we should not return
2544 * directly here, since this will lead to a softlockup problem.
2545 * The method is to get a blocking lock and immediately unlock
2546 * before returning, this can avoid CPU resource waste due to
2547 * lots of retries, and benefits fairness in getting lock.
2549 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2550 ocfs2_inode_unlock(inode, ex);
2551 ret = AOP_TRUNCATED_PAGE;
2554 return ret;
2557 int ocfs2_inode_lock_atime(struct inode *inode,
2558 struct vfsmount *vfsmnt,
2559 int *level, int wait)
2561 int ret;
2563 if (wait)
2564 ret = ocfs2_inode_lock(inode, NULL, 0);
2565 else
2566 ret = ocfs2_try_inode_lock(inode, NULL, 0);
2568 if (ret < 0) {
2569 if (ret != -EAGAIN)
2570 mlog_errno(ret);
2571 return ret;
2575 * If we should update atime, we will get EX lock,
2576 * otherwise we just get PR lock.
2578 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2579 struct buffer_head *bh = NULL;
2581 ocfs2_inode_unlock(inode, 0);
2582 if (wait)
2583 ret = ocfs2_inode_lock(inode, &bh, 1);
2584 else
2585 ret = ocfs2_try_inode_lock(inode, &bh, 1);
2587 if (ret < 0) {
2588 if (ret != -EAGAIN)
2589 mlog_errno(ret);
2590 return ret;
2592 *level = 1;
2593 if (ocfs2_should_update_atime(inode, vfsmnt))
2594 ocfs2_update_inode_atime(inode, bh);
2595 if (bh)
2596 brelse(bh);
2597 } else
2598 *level = 0;
2600 return ret;
2603 void ocfs2_inode_unlock(struct inode *inode,
2604 int ex)
2606 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2607 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2608 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2610 mlog(0, "inode %llu drop %s META lock\n",
2611 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2612 ex ? "EXMODE" : "PRMODE");
2614 if (!ocfs2_is_hard_readonly(osb) &&
2615 !ocfs2_mount_local(osb))
2616 ocfs2_cluster_unlock(osb, lockres, level);
2620 * This _tracker variantes are introduced to deal with the recursive cluster
2621 * locking issue. The idea is to keep track of a lock holder on the stack of
2622 * the current process. If there's a lock holder on the stack, we know the
2623 * task context is already protected by cluster locking. Currently, they're
2624 * used in some VFS entry routines.
2626 * return < 0 on error, return == 0 if there's no lock holder on the stack
2627 * before this call, return == 1 if this call would be a recursive locking.
2628 * return == -1 if this lock attempt will cause an upgrade which is forbidden.
2630 * When taking lock levels into account,we face some different situations.
2632 * 1. no lock is held
2633 * In this case, just lock the inode as requested and return 0
2635 * 2. We are holding a lock
2636 * For this situation, things diverges into several cases
2638 * wanted holding what to do
2639 * ex ex see 2.1 below
2640 * ex pr see 2.2 below
2641 * pr ex see 2.1 below
2642 * pr pr see 2.1 below
2644 * 2.1 lock level that is been held is compatible
2645 * with the wanted level, so no lock action will be tacken.
2647 * 2.2 Otherwise, an upgrade is needed, but it is forbidden.
2649 * Reason why upgrade within a process is forbidden is that
2650 * lock upgrade may cause dead lock. The following illustrates
2651 * how it happens.
2653 * thread on node1 thread on node2
2654 * ocfs2_inode_lock_tracker(ex=0)
2656 * <====== ocfs2_inode_lock_tracker(ex=1)
2658 * ocfs2_inode_lock_tracker(ex=1)
2660 int ocfs2_inode_lock_tracker(struct inode *inode,
2661 struct buffer_head **ret_bh,
2662 int ex,
2663 struct ocfs2_lock_holder *oh)
2665 int status = 0;
2666 struct ocfs2_lock_res *lockres;
2667 struct ocfs2_lock_holder *tmp_oh;
2668 struct pid *pid = task_pid(current);
2671 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2672 tmp_oh = ocfs2_pid_holder(lockres, pid);
2674 if (!tmp_oh) {
2676 * This corresponds to the case 1.
2677 * We haven't got any lock before.
2679 status = ocfs2_inode_lock_full(inode, ret_bh, ex, 0);
2680 if (status < 0) {
2681 if (status != -ENOENT)
2682 mlog_errno(status);
2683 return status;
2686 oh->oh_ex = ex;
2687 ocfs2_add_holder(lockres, oh);
2688 return 0;
2691 if (unlikely(ex && !tmp_oh->oh_ex)) {
2693 * case 2.2 upgrade may cause dead lock, forbid it.
2695 mlog(ML_ERROR, "Recursive locking is not permitted to "
2696 "upgrade to EX level from PR level.\n");
2697 dump_stack();
2698 return -EINVAL;
2702 * case 2.1 OCFS2_META_LOCK_GETBH flag make ocfs2_inode_lock_full.
2703 * ignore the lock level and just update it.
2705 if (ret_bh) {
2706 status = ocfs2_inode_lock_full(inode, ret_bh, ex,
2707 OCFS2_META_LOCK_GETBH);
2708 if (status < 0) {
2709 if (status != -ENOENT)
2710 mlog_errno(status);
2711 return status;
2714 return tmp_oh ? 1 : 0;
2717 void ocfs2_inode_unlock_tracker(struct inode *inode,
2718 int ex,
2719 struct ocfs2_lock_holder *oh,
2720 int had_lock)
2722 struct ocfs2_lock_res *lockres;
2724 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2725 /* had_lock means that the currect process already takes the cluster
2726 * lock previously.
2727 * If had_lock is 1, we have nothing to do here.
2728 * If had_lock is 0, we will release the lock.
2730 if (!had_lock) {
2731 ocfs2_inode_unlock(inode, oh->oh_ex);
2732 ocfs2_remove_holder(lockres, oh);
2736 int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
2738 struct ocfs2_lock_res *lockres;
2739 struct ocfs2_orphan_scan_lvb *lvb;
2740 int status = 0;
2742 if (ocfs2_is_hard_readonly(osb))
2743 return -EROFS;
2745 if (ocfs2_mount_local(osb))
2746 return 0;
2748 lockres = &osb->osb_orphan_scan.os_lockres;
2749 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2750 if (status < 0)
2751 return status;
2753 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2754 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2755 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2756 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2757 else
2758 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2760 return status;
2763 void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
2765 struct ocfs2_lock_res *lockres;
2766 struct ocfs2_orphan_scan_lvb *lvb;
2768 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2769 lockres = &osb->osb_orphan_scan.os_lockres;
2770 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2771 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2772 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2773 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2777 int ocfs2_super_lock(struct ocfs2_super *osb,
2778 int ex)
2780 int status = 0;
2781 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2782 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2784 if (ocfs2_is_hard_readonly(osb))
2785 return -EROFS;
2787 if (ocfs2_mount_local(osb))
2788 goto bail;
2790 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2791 if (status < 0) {
2792 mlog_errno(status);
2793 goto bail;
2796 /* The super block lock path is really in the best position to
2797 * know when resources covered by the lock need to be
2798 * refreshed, so we do it here. Of course, making sense of
2799 * everything is up to the caller :) */
2800 status = ocfs2_should_refresh_lock_res(lockres);
2801 if (status) {
2802 status = ocfs2_refresh_slot_info(osb);
2804 ocfs2_complete_lock_res_refresh(lockres, status);
2806 if (status < 0) {
2807 ocfs2_cluster_unlock(osb, lockres, level);
2808 mlog_errno(status);
2810 ocfs2_track_lock_refresh(lockres);
2812 bail:
2813 return status;
2816 void ocfs2_super_unlock(struct ocfs2_super *osb,
2817 int ex)
2819 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2820 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2822 if (!ocfs2_mount_local(osb))
2823 ocfs2_cluster_unlock(osb, lockres, level);
2826 int ocfs2_rename_lock(struct ocfs2_super *osb)
2828 int status;
2829 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2831 if (ocfs2_is_hard_readonly(osb))
2832 return -EROFS;
2834 if (ocfs2_mount_local(osb))
2835 return 0;
2837 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2838 if (status < 0)
2839 mlog_errno(status);
2841 return status;
2844 void ocfs2_rename_unlock(struct ocfs2_super *osb)
2846 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2848 if (!ocfs2_mount_local(osb))
2849 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2852 int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2854 int status;
2855 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2857 if (ocfs2_is_hard_readonly(osb))
2858 return -EROFS;
2860 if (ex)
2861 down_write(&osb->nfs_sync_rwlock);
2862 else
2863 down_read(&osb->nfs_sync_rwlock);
2865 if (ocfs2_mount_local(osb))
2866 return 0;
2868 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2869 0, 0);
2870 if (status < 0)
2871 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2873 return status;
2876 void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2878 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2880 if (!ocfs2_mount_local(osb))
2881 ocfs2_cluster_unlock(osb, lockres,
2882 ex ? LKM_EXMODE : LKM_PRMODE);
2883 if (ex)
2884 up_write(&osb->nfs_sync_rwlock);
2885 else
2886 up_read(&osb->nfs_sync_rwlock);
2889 int ocfs2_trim_fs_lock(struct ocfs2_super *osb,
2890 struct ocfs2_trim_fs_info *info, int trylock)
2892 int status;
2893 struct ocfs2_trim_fs_lvb *lvb;
2894 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2896 if (info)
2897 info->tf_valid = 0;
2899 if (ocfs2_is_hard_readonly(osb))
2900 return -EROFS;
2902 if (ocfs2_mount_local(osb))
2903 return 0;
2905 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX,
2906 trylock ? DLM_LKF_NOQUEUE : 0, 0);
2907 if (status < 0) {
2908 if (status != -EAGAIN)
2909 mlog_errno(status);
2910 return status;
2913 if (info) {
2914 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2915 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2916 lvb->lvb_version == OCFS2_TRIMFS_LVB_VERSION) {
2917 info->tf_valid = 1;
2918 info->tf_success = lvb->lvb_success;
2919 info->tf_nodenum = be32_to_cpu(lvb->lvb_nodenum);
2920 info->tf_start = be64_to_cpu(lvb->lvb_start);
2921 info->tf_len = be64_to_cpu(lvb->lvb_len);
2922 info->tf_minlen = be64_to_cpu(lvb->lvb_minlen);
2923 info->tf_trimlen = be64_to_cpu(lvb->lvb_trimlen);
2927 return status;
2930 void ocfs2_trim_fs_unlock(struct ocfs2_super *osb,
2931 struct ocfs2_trim_fs_info *info)
2933 struct ocfs2_trim_fs_lvb *lvb;
2934 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2936 if (ocfs2_mount_local(osb))
2937 return;
2939 if (info) {
2940 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2941 lvb->lvb_version = OCFS2_TRIMFS_LVB_VERSION;
2942 lvb->lvb_success = info->tf_success;
2943 lvb->lvb_nodenum = cpu_to_be32(info->tf_nodenum);
2944 lvb->lvb_start = cpu_to_be64(info->tf_start);
2945 lvb->lvb_len = cpu_to_be64(info->tf_len);
2946 lvb->lvb_minlen = cpu_to_be64(info->tf_minlen);
2947 lvb->lvb_trimlen = cpu_to_be64(info->tf_trimlen);
2950 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2953 int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2955 int ret;
2956 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2957 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2958 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2960 BUG_ON(!dl);
2962 if (ocfs2_is_hard_readonly(osb)) {
2963 if (ex)
2964 return -EROFS;
2965 return 0;
2968 if (ocfs2_mount_local(osb))
2969 return 0;
2971 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2972 if (ret < 0)
2973 mlog_errno(ret);
2975 return ret;
2978 void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2980 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2981 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2982 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2984 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
2985 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2988 /* Reference counting of the dlm debug structure. We want this because
2989 * open references on the debug inodes can live on after a mount, so
2990 * we can't rely on the ocfs2_super to always exist. */
2991 static void ocfs2_dlm_debug_free(struct kref *kref)
2993 struct ocfs2_dlm_debug *dlm_debug;
2995 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2997 kfree(dlm_debug);
3000 void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
3002 if (dlm_debug)
3003 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
3006 static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
3008 kref_get(&debug->d_refcnt);
3011 struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
3013 struct ocfs2_dlm_debug *dlm_debug;
3015 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
3016 if (!dlm_debug) {
3017 mlog_errno(-ENOMEM);
3018 goto out;
3021 kref_init(&dlm_debug->d_refcnt);
3022 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
3023 dlm_debug->d_locking_state = NULL;
3024 out:
3025 return dlm_debug;
3028 /* Access to this is arbitrated for us via seq_file->sem. */
3029 struct ocfs2_dlm_seq_priv {
3030 struct ocfs2_dlm_debug *p_dlm_debug;
3031 struct ocfs2_lock_res p_iter_res;
3032 struct ocfs2_lock_res p_tmp_res;
3035 static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
3036 struct ocfs2_dlm_seq_priv *priv)
3038 struct ocfs2_lock_res *iter, *ret = NULL;
3039 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
3041 assert_spin_locked(&ocfs2_dlm_tracking_lock);
3043 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
3044 /* discover the head of the list */
3045 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
3046 mlog(0, "End of list found, %p\n", ret);
3047 break;
3050 /* We track our "dummy" iteration lockres' by a NULL
3051 * l_ops field. */
3052 if (iter->l_ops != NULL) {
3053 ret = iter;
3054 break;
3058 return ret;
3061 static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
3063 struct ocfs2_dlm_seq_priv *priv = m->private;
3064 struct ocfs2_lock_res *iter;
3066 spin_lock(&ocfs2_dlm_tracking_lock);
3067 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
3068 if (iter) {
3069 /* Since lockres' have the lifetime of their container
3070 * (which can be inodes, ocfs2_supers, etc) we want to
3071 * copy this out to a temporary lockres while still
3072 * under the spinlock. Obviously after this we can't
3073 * trust any pointers on the copy returned, but that's
3074 * ok as the information we want isn't typically held
3075 * in them. */
3076 priv->p_tmp_res = *iter;
3077 iter = &priv->p_tmp_res;
3079 spin_unlock(&ocfs2_dlm_tracking_lock);
3081 return iter;
3084 static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
3088 static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
3090 struct ocfs2_dlm_seq_priv *priv = m->private;
3091 struct ocfs2_lock_res *iter = v;
3092 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
3094 spin_lock(&ocfs2_dlm_tracking_lock);
3095 iter = ocfs2_dlm_next_res(iter, priv);
3096 list_del_init(&dummy->l_debug_list);
3097 if (iter) {
3098 list_add(&dummy->l_debug_list, &iter->l_debug_list);
3099 priv->p_tmp_res = *iter;
3100 iter = &priv->p_tmp_res;
3102 spin_unlock(&ocfs2_dlm_tracking_lock);
3104 return iter;
3108 * Version is used by debugfs.ocfs2 to determine the format being used
3110 * New in version 2
3111 * - Lock stats printed
3112 * New in version 3
3113 * - Max time in lock stats is in usecs (instead of nsecs)
3115 #define OCFS2_DLM_DEBUG_STR_VERSION 3
3116 static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
3118 int i;
3119 char *lvb;
3120 struct ocfs2_lock_res *lockres = v;
3122 if (!lockres)
3123 return -EINVAL;
3125 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
3127 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
3128 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
3129 lockres->l_name,
3130 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
3131 else
3132 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
3134 seq_printf(m, "%d\t"
3135 "0x%lx\t"
3136 "0x%x\t"
3137 "0x%x\t"
3138 "%u\t"
3139 "%u\t"
3140 "%d\t"
3141 "%d\t",
3142 lockres->l_level,
3143 lockres->l_flags,
3144 lockres->l_action,
3145 lockres->l_unlock_action,
3146 lockres->l_ro_holders,
3147 lockres->l_ex_holders,
3148 lockres->l_requested,
3149 lockres->l_blocking);
3151 /* Dump the raw LVB */
3152 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3153 for(i = 0; i < DLM_LVB_LEN; i++)
3154 seq_printf(m, "0x%x\t", lvb[i]);
3156 #ifdef CONFIG_OCFS2_FS_STATS
3157 # define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
3158 # define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
3159 # define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
3160 # define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
3161 # define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
3162 # define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
3163 # define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
3164 # define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
3165 # define lock_refresh(_l) ((_l)->l_lock_refresh)
3166 #else
3167 # define lock_num_prmode(_l) (0)
3168 # define lock_num_exmode(_l) (0)
3169 # define lock_num_prmode_failed(_l) (0)
3170 # define lock_num_exmode_failed(_l) (0)
3171 # define lock_total_prmode(_l) (0ULL)
3172 # define lock_total_exmode(_l) (0ULL)
3173 # define lock_max_prmode(_l) (0)
3174 # define lock_max_exmode(_l) (0)
3175 # define lock_refresh(_l) (0)
3176 #endif
3177 /* The following seq_print was added in version 2 of this output */
3178 seq_printf(m, "%u\t"
3179 "%u\t"
3180 "%u\t"
3181 "%u\t"
3182 "%llu\t"
3183 "%llu\t"
3184 "%u\t"
3185 "%u\t"
3186 "%u\t",
3187 lock_num_prmode(lockres),
3188 lock_num_exmode(lockres),
3189 lock_num_prmode_failed(lockres),
3190 lock_num_exmode_failed(lockres),
3191 lock_total_prmode(lockres),
3192 lock_total_exmode(lockres),
3193 lock_max_prmode(lockres),
3194 lock_max_exmode(lockres),
3195 lock_refresh(lockres));
3197 /* End the line */
3198 seq_printf(m, "\n");
3199 return 0;
3202 static const struct seq_operations ocfs2_dlm_seq_ops = {
3203 .start = ocfs2_dlm_seq_start,
3204 .stop = ocfs2_dlm_seq_stop,
3205 .next = ocfs2_dlm_seq_next,
3206 .show = ocfs2_dlm_seq_show,
3209 static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
3211 struct seq_file *seq = file->private_data;
3212 struct ocfs2_dlm_seq_priv *priv = seq->private;
3213 struct ocfs2_lock_res *res = &priv->p_iter_res;
3215 ocfs2_remove_lockres_tracking(res);
3216 ocfs2_put_dlm_debug(priv->p_dlm_debug);
3217 return seq_release_private(inode, file);
3220 static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
3222 struct ocfs2_dlm_seq_priv *priv;
3223 struct ocfs2_super *osb;
3225 priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
3226 if (!priv) {
3227 mlog_errno(-ENOMEM);
3228 return -ENOMEM;
3231 osb = inode->i_private;
3232 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
3233 priv->p_dlm_debug = osb->osb_dlm_debug;
3234 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
3236 ocfs2_add_lockres_tracking(&priv->p_iter_res,
3237 priv->p_dlm_debug);
3239 return 0;
3242 static const struct file_operations ocfs2_dlm_debug_fops = {
3243 .open = ocfs2_dlm_debug_open,
3244 .release = ocfs2_dlm_debug_release,
3245 .read = seq_read,
3246 .llseek = seq_lseek,
3249 static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
3251 int ret = 0;
3252 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3254 dlm_debug->d_locking_state = debugfs_create_file("locking_state",
3255 S_IFREG|S_IRUSR,
3256 osb->osb_debug_root,
3257 osb,
3258 &ocfs2_dlm_debug_fops);
3259 if (!dlm_debug->d_locking_state) {
3260 ret = -EINVAL;
3261 mlog(ML_ERROR,
3262 "Unable to create locking state debugfs file.\n");
3263 goto out;
3266 ocfs2_get_dlm_debug(dlm_debug);
3267 out:
3268 return ret;
3271 static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
3273 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3275 if (dlm_debug) {
3276 debugfs_remove(dlm_debug->d_locking_state);
3277 ocfs2_put_dlm_debug(dlm_debug);
3281 int ocfs2_dlm_init(struct ocfs2_super *osb)
3283 int status = 0;
3284 struct ocfs2_cluster_connection *conn = NULL;
3286 if (ocfs2_mount_local(osb)) {
3287 osb->node_num = 0;
3288 goto local;
3291 status = ocfs2_dlm_init_debug(osb);
3292 if (status < 0) {
3293 mlog_errno(status);
3294 goto bail;
3297 /* launch downconvert thread */
3298 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
3299 osb->uuid_str);
3300 if (IS_ERR(osb->dc_task)) {
3301 status = PTR_ERR(osb->dc_task);
3302 osb->dc_task = NULL;
3303 mlog_errno(status);
3304 goto bail;
3307 /* for now, uuid == domain */
3308 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
3309 osb->osb_cluster_name,
3310 strlen(osb->osb_cluster_name),
3311 osb->uuid_str,
3312 strlen(osb->uuid_str),
3313 &lproto, ocfs2_do_node_down, osb,
3314 &conn);
3315 if (status) {
3316 mlog_errno(status);
3317 goto bail;
3320 status = ocfs2_cluster_this_node(conn, &osb->node_num);
3321 if (status < 0) {
3322 mlog_errno(status);
3323 mlog(ML_ERROR,
3324 "could not find this host's node number\n");
3325 ocfs2_cluster_disconnect(conn, 0);
3326 goto bail;
3329 local:
3330 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3331 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
3332 ocfs2_nfs_sync_lock_init(osb);
3333 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
3335 osb->cconn = conn;
3336 bail:
3337 if (status < 0) {
3338 ocfs2_dlm_shutdown_debug(osb);
3339 if (osb->dc_task)
3340 kthread_stop(osb->dc_task);
3343 return status;
3346 void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3347 int hangup_pending)
3349 ocfs2_drop_osb_locks(osb);
3352 * Now that we have dropped all locks and ocfs2_dismount_volume()
3353 * has disabled recovery, the DLM won't be talking to us. It's
3354 * safe to tear things down before disconnecting the cluster.
3357 if (osb->dc_task) {
3358 kthread_stop(osb->dc_task);
3359 osb->dc_task = NULL;
3362 ocfs2_lock_res_free(&osb->osb_super_lockres);
3363 ocfs2_lock_res_free(&osb->osb_rename_lockres);
3364 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
3365 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
3367 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
3368 osb->cconn = NULL;
3370 ocfs2_dlm_shutdown_debug(osb);
3373 static int ocfs2_drop_lock(struct ocfs2_super *osb,
3374 struct ocfs2_lock_res *lockres)
3376 int ret;
3377 unsigned long flags;
3378 u32 lkm_flags = 0;
3380 /* We didn't get anywhere near actually using this lockres. */
3381 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3382 goto out;
3384 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3385 lkm_flags |= DLM_LKF_VALBLK;
3387 spin_lock_irqsave(&lockres->l_lock, flags);
3389 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3390 "lockres %s, flags 0x%lx\n",
3391 lockres->l_name, lockres->l_flags);
3393 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3394 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3395 "%u, unlock_action = %u\n",
3396 lockres->l_name, lockres->l_flags, lockres->l_action,
3397 lockres->l_unlock_action);
3399 spin_unlock_irqrestore(&lockres->l_lock, flags);
3401 /* XXX: Today we just wait on any busy
3402 * locks... Perhaps we need to cancel converts in the
3403 * future? */
3404 ocfs2_wait_on_busy_lock(lockres);
3406 spin_lock_irqsave(&lockres->l_lock, flags);
3409 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3410 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3411 lockres->l_level == DLM_LOCK_EX &&
3412 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3413 lockres->l_ops->set_lvb(lockres);
3416 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3417 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3418 lockres->l_name);
3419 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3420 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3422 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3423 spin_unlock_irqrestore(&lockres->l_lock, flags);
3424 goto out;
3427 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3429 /* make sure we never get here while waiting for an ast to
3430 * fire. */
3431 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3433 /* is this necessary? */
3434 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3435 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3436 spin_unlock_irqrestore(&lockres->l_lock, flags);
3438 mlog(0, "lock %s\n", lockres->l_name);
3440 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
3441 if (ret) {
3442 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3443 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3444 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3445 BUG();
3447 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3448 lockres->l_name);
3450 ocfs2_wait_on_busy_lock(lockres);
3451 out:
3452 return 0;
3455 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3456 struct ocfs2_lock_res *lockres);
3458 /* Mark the lockres as being dropped. It will no longer be
3459 * queued if blocking, but we still may have to wait on it
3460 * being dequeued from the downconvert thread before we can consider
3461 * it safe to drop.
3463 * You can *not* attempt to call cluster_lock on this lockres anymore. */
3464 void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
3465 struct ocfs2_lock_res *lockres)
3467 int status;
3468 struct ocfs2_mask_waiter mw;
3469 unsigned long flags, flags2;
3471 ocfs2_init_mask_waiter(&mw);
3473 spin_lock_irqsave(&lockres->l_lock, flags);
3474 lockres->l_flags |= OCFS2_LOCK_FREEING;
3475 if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
3477 * We know the downconvert is queued but not in progress
3478 * because we are the downconvert thread and processing
3479 * different lock. So we can just remove the lock from the
3480 * queue. This is not only an optimization but also a way
3481 * to avoid the following deadlock:
3482 * ocfs2_dentry_post_unlock()
3483 * ocfs2_dentry_lock_put()
3484 * ocfs2_drop_dentry_lock()
3485 * iput()
3486 * ocfs2_evict_inode()
3487 * ocfs2_clear_inode()
3488 * ocfs2_mark_lockres_freeing()
3489 * ... blocks waiting for OCFS2_LOCK_QUEUED
3490 * since we are the downconvert thread which
3491 * should clear the flag.
3493 spin_unlock_irqrestore(&lockres->l_lock, flags);
3494 spin_lock_irqsave(&osb->dc_task_lock, flags2);
3495 list_del_init(&lockres->l_blocked_list);
3496 osb->blocked_lock_count--;
3497 spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
3499 * Warn if we recurse into another post_unlock call. Strictly
3500 * speaking it isn't a problem but we need to be careful if
3501 * that happens (stack overflow, deadlocks, ...) so warn if
3502 * ocfs2 grows a path for which this can happen.
3504 WARN_ON_ONCE(lockres->l_ops->post_unlock);
3505 /* Since the lock is freeing we don't do much in the fn below */
3506 ocfs2_process_blocked_lock(osb, lockres);
3507 return;
3509 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3510 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3511 spin_unlock_irqrestore(&lockres->l_lock, flags);
3513 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3515 status = ocfs2_wait_for_mask(&mw);
3516 if (status)
3517 mlog_errno(status);
3519 spin_lock_irqsave(&lockres->l_lock, flags);
3521 spin_unlock_irqrestore(&lockres->l_lock, flags);
3524 void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3525 struct ocfs2_lock_res *lockres)
3527 int ret;
3529 ocfs2_mark_lockres_freeing(osb, lockres);
3530 ret = ocfs2_drop_lock(osb, lockres);
3531 if (ret)
3532 mlog_errno(ret);
3535 static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3537 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3538 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3539 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3540 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3543 int ocfs2_drop_inode_locks(struct inode *inode)
3545 int status, err;
3547 /* No need to call ocfs2_mark_lockres_freeing here -
3548 * ocfs2_clear_inode has done it for us. */
3550 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3551 &OCFS2_I(inode)->ip_open_lockres);
3552 if (err < 0)
3553 mlog_errno(err);
3555 status = err;
3557 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3558 &OCFS2_I(inode)->ip_inode_lockres);
3559 if (err < 0)
3560 mlog_errno(err);
3561 if (err < 0 && !status)
3562 status = err;
3564 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3565 &OCFS2_I(inode)->ip_rw_lockres);
3566 if (err < 0)
3567 mlog_errno(err);
3568 if (err < 0 && !status)
3569 status = err;
3571 return status;
3574 static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3575 int new_level)
3577 assert_spin_locked(&lockres->l_lock);
3579 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3581 if (lockres->l_level <= new_level) {
3582 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3583 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3584 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3585 new_level, list_empty(&lockres->l_blocked_list),
3586 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3587 lockres->l_flags, lockres->l_ro_holders,
3588 lockres->l_ex_holders, lockres->l_action,
3589 lockres->l_unlock_action, lockres->l_requested,
3590 lockres->l_blocking, lockres->l_pending_gen);
3591 BUG();
3594 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3595 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
3597 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3598 lockres->l_requested = new_level;
3599 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3600 return lockres_set_pending(lockres);
3603 static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3604 struct ocfs2_lock_res *lockres,
3605 int new_level,
3606 int lvb,
3607 unsigned int generation)
3609 int ret;
3610 u32 dlm_flags = DLM_LKF_CONVERT;
3612 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3613 lockres->l_level, new_level);
3616 * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
3617 * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
3618 * we can recover correctly from node failure. Otherwise, we may get
3619 * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
3621 if (ocfs2_userspace_stack(osb) &&
3622 lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3623 lvb = 1;
3625 if (lvb)
3626 dlm_flags |= DLM_LKF_VALBLK;
3628 ret = ocfs2_dlm_lock(osb->cconn,
3629 new_level,
3630 &lockres->l_lksb,
3631 dlm_flags,
3632 lockres->l_name,
3633 OCFS2_LOCK_ID_MAX_LEN - 1);
3634 lockres_clear_pending(lockres, generation, osb);
3635 if (ret) {
3636 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3637 ocfs2_recover_from_dlm_error(lockres, 1);
3638 goto bail;
3641 ret = 0;
3642 bail:
3643 return ret;
3646 /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
3647 static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3648 struct ocfs2_lock_res *lockres)
3650 assert_spin_locked(&lockres->l_lock);
3652 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3653 /* If we're already trying to cancel a lock conversion
3654 * then just drop the spinlock and allow the caller to
3655 * requeue this lock. */
3656 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
3657 return 0;
3660 /* were we in a convert when we got the bast fire? */
3661 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3662 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3663 /* set things up for the unlockast to know to just
3664 * clear out the ast_action and unset busy, etc. */
3665 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3667 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3668 "lock %s, invalid flags: 0x%lx\n",
3669 lockres->l_name, lockres->l_flags);
3671 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3673 return 1;
3676 static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3677 struct ocfs2_lock_res *lockres)
3679 int ret;
3681 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3682 DLM_LKF_CANCEL);
3683 if (ret) {
3684 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3685 ocfs2_recover_from_dlm_error(lockres, 0);
3688 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3690 return ret;
3693 static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3694 struct ocfs2_lock_res *lockres,
3695 struct ocfs2_unblock_ctl *ctl)
3697 unsigned long flags;
3698 int blocking;
3699 int new_level;
3700 int level;
3701 int ret = 0;
3702 int set_lvb = 0;
3703 unsigned int gen;
3705 spin_lock_irqsave(&lockres->l_lock, flags);
3707 recheck:
3709 * Is it still blocking? If not, we have no more work to do.
3711 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3712 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3713 spin_unlock_irqrestore(&lockres->l_lock, flags);
3714 ret = 0;
3715 goto leave;
3718 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3719 /* XXX
3720 * This is a *big* race. The OCFS2_LOCK_PENDING flag
3721 * exists entirely for one reason - another thread has set
3722 * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
3724 * If we do ocfs2_cancel_convert() before the other thread
3725 * calls dlm_lock(), our cancel will do nothing. We will
3726 * get no ast, and we will have no way of knowing the
3727 * cancel failed. Meanwhile, the other thread will call
3728 * into dlm_lock() and wait...forever.
3730 * Why forever? Because another node has asked for the
3731 * lock first; that's why we're here in unblock_lock().
3733 * The solution is OCFS2_LOCK_PENDING. When PENDING is
3734 * set, we just requeue the unblock. Only when the other
3735 * thread has called dlm_lock() and cleared PENDING will
3736 * we then cancel their request.
3738 * All callers of dlm_lock() must set OCFS2_DLM_PENDING
3739 * at the same time they set OCFS2_DLM_BUSY. They must
3740 * clear OCFS2_DLM_PENDING after dlm_lock() returns.
3742 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3743 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3744 lockres->l_name);
3745 goto leave_requeue;
3748 ctl->requeue = 1;
3749 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3750 spin_unlock_irqrestore(&lockres->l_lock, flags);
3751 if (ret) {
3752 ret = ocfs2_cancel_convert(osb, lockres);
3753 if (ret < 0)
3754 mlog_errno(ret);
3756 goto leave;
3760 * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
3761 * set when the ast is received for an upconvert just before the
3762 * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
3763 * on the heels of the ast, we want to delay the downconvert just
3764 * enough to allow the up requestor to do its task. Because this
3765 * lock is in the blocked queue, the lock will be downconverted
3766 * as soon as the requestor is done with the lock.
3768 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3769 goto leave_requeue;
3772 * How can we block and yet be at NL? We were trying to upconvert
3773 * from NL and got canceled. The code comes back here, and now
3774 * we notice and clear BLOCKING.
3776 if (lockres->l_level == DLM_LOCK_NL) {
3777 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
3778 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
3779 lockres->l_blocking = DLM_LOCK_NL;
3780 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3781 spin_unlock_irqrestore(&lockres->l_lock, flags);
3782 goto leave;
3785 /* if we're blocking an exclusive and we have *any* holders,
3786 * then requeue. */
3787 if ((lockres->l_blocking == DLM_LOCK_EX)
3788 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3789 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3790 lockres->l_name, lockres->l_ex_holders,
3791 lockres->l_ro_holders);
3792 goto leave_requeue;
3795 /* If it's a PR we're blocking, then only
3796 * requeue if we've got any EX holders */
3797 if (lockres->l_blocking == DLM_LOCK_PR &&
3798 lockres->l_ex_holders) {
3799 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3800 lockres->l_name, lockres->l_ex_holders);
3801 goto leave_requeue;
3805 * Can we get a lock in this state if the holder counts are
3806 * zero? The meta data unblock code used to check this.
3808 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3809 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3810 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3811 lockres->l_name);
3812 goto leave_requeue;
3815 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3817 if (lockres->l_ops->check_downconvert
3818 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3819 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3820 lockres->l_name);
3821 goto leave_requeue;
3824 /* If we get here, then we know that there are no more
3825 * incompatible holders (and anyone asking for an incompatible
3826 * lock is blocked). We can now downconvert the lock */
3827 if (!lockres->l_ops->downconvert_worker)
3828 goto downconvert;
3830 /* Some lockres types want to do a bit of work before
3831 * downconverting a lock. Allow that here. The worker function
3832 * may sleep, so we save off a copy of what we're blocking as
3833 * it may change while we're not holding the spin lock. */
3834 blocking = lockres->l_blocking;
3835 level = lockres->l_level;
3836 spin_unlock_irqrestore(&lockres->l_lock, flags);
3838 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3840 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3841 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3842 lockres->l_name);
3843 goto leave;
3846 spin_lock_irqsave(&lockres->l_lock, flags);
3847 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
3848 /* If this changed underneath us, then we can't drop
3849 * it just yet. */
3850 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3851 "Recheck\n", lockres->l_name, blocking,
3852 lockres->l_blocking, level, lockres->l_level);
3853 goto recheck;
3856 downconvert:
3857 ctl->requeue = 0;
3859 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3860 if (lockres->l_level == DLM_LOCK_EX)
3861 set_lvb = 1;
3864 * We only set the lvb if the lock has been fully
3865 * refreshed - otherwise we risk setting stale
3866 * data. Otherwise, there's no need to actually clear
3867 * out the lvb here as it's value is still valid.
3869 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3870 lockres->l_ops->set_lvb(lockres);
3873 gen = ocfs2_prepare_downconvert(lockres, new_level);
3874 spin_unlock_irqrestore(&lockres->l_lock, flags);
3875 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3876 gen);
3878 leave:
3879 if (ret)
3880 mlog_errno(ret);
3881 return ret;
3883 leave_requeue:
3884 spin_unlock_irqrestore(&lockres->l_lock, flags);
3885 ctl->requeue = 1;
3887 return 0;
3890 static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3891 int blocking)
3893 struct inode *inode;
3894 struct address_space *mapping;
3895 struct ocfs2_inode_info *oi;
3897 inode = ocfs2_lock_res_inode(lockres);
3898 mapping = inode->i_mapping;
3900 if (S_ISDIR(inode->i_mode)) {
3901 oi = OCFS2_I(inode);
3902 oi->ip_dir_lock_gen++;
3903 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3904 goto out;
3907 if (!S_ISREG(inode->i_mode))
3908 goto out;
3911 * We need this before the filemap_fdatawrite() so that it can
3912 * transfer the dirty bit from the PTE to the
3913 * page. Unfortunately this means that even for EX->PR
3914 * downconverts, we'll lose our mappings and have to build
3915 * them up again.
3917 unmap_mapping_range(mapping, 0, 0, 0);
3919 if (filemap_fdatawrite(mapping)) {
3920 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3921 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3923 sync_mapping_buffers(mapping);
3924 if (blocking == DLM_LOCK_EX) {
3925 truncate_inode_pages(mapping, 0);
3926 } else {
3927 /* We only need to wait on the I/O if we're not also
3928 * truncating pages because truncate_inode_pages waits
3929 * for us above. We don't truncate pages if we're
3930 * blocking anything < EXMODE because we want to keep
3931 * them around in that case. */
3932 filemap_fdatawait(mapping);
3935 forget_all_cached_acls(inode);
3937 out:
3938 return UNBLOCK_CONTINUE;
3941 static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3942 struct ocfs2_lock_res *lockres,
3943 int new_level)
3945 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
3947 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3948 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3950 if (checkpointed)
3951 return 1;
3953 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
3954 return 0;
3957 static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3958 int new_level)
3960 struct inode *inode = ocfs2_lock_res_inode(lockres);
3962 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3965 static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3967 struct inode *inode = ocfs2_lock_res_inode(lockres);
3969 __ocfs2_stuff_meta_lvb(inode);
3973 * Does the final reference drop on our dentry lock. Right now this
3974 * happens in the downconvert thread, but we could choose to simplify the
3975 * dlmglue API and push these off to the ocfs2_wq in the future.
3977 static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3978 struct ocfs2_lock_res *lockres)
3980 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3981 ocfs2_dentry_lock_put(osb, dl);
3985 * d_delete() matching dentries before the lock downconvert.
3987 * At this point, any process waiting to destroy the
3988 * dentry_lock due to last ref count is stopped by the
3989 * OCFS2_LOCK_QUEUED flag.
3991 * We have two potential problems
3993 * 1) If we do the last reference drop on our dentry_lock (via dput)
3994 * we'll wind up in ocfs2_release_dentry_lock(), waiting on
3995 * the downconvert to finish. Instead we take an elevated
3996 * reference and push the drop until after we've completed our
3997 * unblock processing.
3999 * 2) There might be another process with a final reference,
4000 * waiting on us to finish processing. If this is the case, we
4001 * detect it and exit out - there's no more dentries anyway.
4003 static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
4004 int blocking)
4006 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
4007 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
4008 struct dentry *dentry;
4009 unsigned long flags;
4010 int extra_ref = 0;
4013 * This node is blocking another node from getting a read
4014 * lock. This happens when we've renamed within a
4015 * directory. We've forced the other nodes to d_delete(), but
4016 * we never actually dropped our lock because it's still
4017 * valid. The downconvert code will retain a PR for this node,
4018 * so there's no further work to do.
4020 if (blocking == DLM_LOCK_PR)
4021 return UNBLOCK_CONTINUE;
4024 * Mark this inode as potentially orphaned. The code in
4025 * ocfs2_delete_inode() will figure out whether it actually
4026 * needs to be freed or not.
4028 spin_lock(&oi->ip_lock);
4029 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
4030 spin_unlock(&oi->ip_lock);
4033 * Yuck. We need to make sure however that the check of
4034 * OCFS2_LOCK_FREEING and the extra reference are atomic with
4035 * respect to a reference decrement or the setting of that
4036 * flag.
4038 spin_lock_irqsave(&lockres->l_lock, flags);
4039 spin_lock(&dentry_attach_lock);
4040 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
4041 && dl->dl_count) {
4042 dl->dl_count++;
4043 extra_ref = 1;
4045 spin_unlock(&dentry_attach_lock);
4046 spin_unlock_irqrestore(&lockres->l_lock, flags);
4048 mlog(0, "extra_ref = %d\n", extra_ref);
4051 * We have a process waiting on us in ocfs2_dentry_iput(),
4052 * which means we can't have any more outstanding
4053 * aliases. There's no need to do any more work.
4055 if (!extra_ref)
4056 return UNBLOCK_CONTINUE;
4058 spin_lock(&dentry_attach_lock);
4059 while (1) {
4060 dentry = ocfs2_find_local_alias(dl->dl_inode,
4061 dl->dl_parent_blkno, 1);
4062 if (!dentry)
4063 break;
4064 spin_unlock(&dentry_attach_lock);
4066 if (S_ISDIR(dl->dl_inode->i_mode))
4067 shrink_dcache_parent(dentry);
4069 mlog(0, "d_delete(%pd);\n", dentry);
4072 * The following dcache calls may do an
4073 * iput(). Normally we don't want that from the
4074 * downconverting thread, but in this case it's ok
4075 * because the requesting node already has an
4076 * exclusive lock on the inode, so it can't be queued
4077 * for a downconvert.
4079 d_delete(dentry);
4080 dput(dentry);
4082 spin_lock(&dentry_attach_lock);
4084 spin_unlock(&dentry_attach_lock);
4087 * If we are the last holder of this dentry lock, there is no
4088 * reason to downconvert so skip straight to the unlock.
4090 if (dl->dl_count == 1)
4091 return UNBLOCK_STOP_POST;
4093 return UNBLOCK_CONTINUE_POST;
4096 static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
4097 int new_level)
4099 struct ocfs2_refcount_tree *tree =
4100 ocfs2_lock_res_refcount_tree(lockres);
4102 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
4105 static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
4106 int blocking)
4108 struct ocfs2_refcount_tree *tree =
4109 ocfs2_lock_res_refcount_tree(lockres);
4111 ocfs2_metadata_cache_purge(&tree->rf_ci);
4113 return UNBLOCK_CONTINUE;
4116 static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
4118 struct ocfs2_qinfo_lvb *lvb;
4119 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
4120 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4121 oinfo->dqi_gi.dqi_type);
4123 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
4124 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
4125 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
4126 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
4127 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
4128 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
4129 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
4130 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
4133 void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4135 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4136 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4137 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4139 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
4140 ocfs2_cluster_unlock(osb, lockres, level);
4143 static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
4145 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4146 oinfo->dqi_gi.dqi_type);
4147 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4148 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
4149 struct buffer_head *bh = NULL;
4150 struct ocfs2_global_disk_dqinfo *gdinfo;
4151 int status = 0;
4153 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
4154 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
4155 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
4156 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
4157 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
4158 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
4159 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
4160 oinfo->dqi_gi.dqi_free_entry =
4161 be32_to_cpu(lvb->lvb_free_entry);
4162 } else {
4163 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
4164 oinfo->dqi_giblk, &bh);
4165 if (status) {
4166 mlog_errno(status);
4167 goto bail;
4169 gdinfo = (struct ocfs2_global_disk_dqinfo *)
4170 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
4171 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
4172 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
4173 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
4174 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
4175 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
4176 oinfo->dqi_gi.dqi_free_entry =
4177 le32_to_cpu(gdinfo->dqi_free_entry);
4178 brelse(bh);
4179 ocfs2_track_lock_refresh(lockres);
4182 bail:
4183 return status;
4186 /* Lock quota info, this function expects at least shared lock on the quota file
4187 * so that we can safely refresh quota info from disk. */
4188 int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4190 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4191 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4192 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4193 int status = 0;
4195 /* On RO devices, locking really isn't needed... */
4196 if (ocfs2_is_hard_readonly(osb)) {
4197 if (ex)
4198 status = -EROFS;
4199 goto bail;
4201 if (ocfs2_mount_local(osb))
4202 goto bail;
4204 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4205 if (status < 0) {
4206 mlog_errno(status);
4207 goto bail;
4209 if (!ocfs2_should_refresh_lock_res(lockres))
4210 goto bail;
4211 /* OK, we have the lock but we need to refresh the quota info */
4212 status = ocfs2_refresh_qinfo(oinfo);
4213 if (status)
4214 ocfs2_qinfo_unlock(oinfo, ex);
4215 ocfs2_complete_lock_res_refresh(lockres, status);
4216 bail:
4217 return status;
4220 int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
4222 int status;
4223 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4224 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4225 struct ocfs2_super *osb = lockres->l_priv;
4228 if (ocfs2_is_hard_readonly(osb))
4229 return -EROFS;
4231 if (ocfs2_mount_local(osb))
4232 return 0;
4234 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4235 if (status < 0)
4236 mlog_errno(status);
4238 return status;
4241 void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
4243 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4244 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4245 struct ocfs2_super *osb = lockres->l_priv;
4247 if (!ocfs2_mount_local(osb))
4248 ocfs2_cluster_unlock(osb, lockres, level);
4251 static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
4252 struct ocfs2_lock_res *lockres)
4254 int status;
4255 struct ocfs2_unblock_ctl ctl = {0, 0,};
4256 unsigned long flags;
4258 /* Our reference to the lockres in this function can be
4259 * considered valid until we remove the OCFS2_LOCK_QUEUED
4260 * flag. */
4262 BUG_ON(!lockres);
4263 BUG_ON(!lockres->l_ops);
4265 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
4267 /* Detect whether a lock has been marked as going away while
4268 * the downconvert thread was processing other things. A lock can
4269 * still be marked with OCFS2_LOCK_FREEING after this check,
4270 * but short circuiting here will still save us some
4271 * performance. */
4272 spin_lock_irqsave(&lockres->l_lock, flags);
4273 if (lockres->l_flags & OCFS2_LOCK_FREEING)
4274 goto unqueue;
4275 spin_unlock_irqrestore(&lockres->l_lock, flags);
4277 status = ocfs2_unblock_lock(osb, lockres, &ctl);
4278 if (status < 0)
4279 mlog_errno(status);
4281 spin_lock_irqsave(&lockres->l_lock, flags);
4282 unqueue:
4283 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
4284 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
4285 } else
4286 ocfs2_schedule_blocked_lock(osb, lockres);
4288 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
4289 ctl.requeue ? "yes" : "no");
4290 spin_unlock_irqrestore(&lockres->l_lock, flags);
4292 if (ctl.unblock_action != UNBLOCK_CONTINUE
4293 && lockres->l_ops->post_unlock)
4294 lockres->l_ops->post_unlock(osb, lockres);
4297 static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4298 struct ocfs2_lock_res *lockres)
4300 unsigned long flags;
4302 assert_spin_locked(&lockres->l_lock);
4304 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4305 /* Do not schedule a lock for downconvert when it's on
4306 * the way to destruction - any nodes wanting access
4307 * to the resource will get it soon. */
4308 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
4309 lockres->l_name, lockres->l_flags);
4310 return;
4313 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4315 spin_lock_irqsave(&osb->dc_task_lock, flags);
4316 if (list_empty(&lockres->l_blocked_list)) {
4317 list_add_tail(&lockres->l_blocked_list,
4318 &osb->blocked_lock_list);
4319 osb->blocked_lock_count++;
4321 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4324 static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4326 unsigned long processed;
4327 unsigned long flags;
4328 struct ocfs2_lock_res *lockres;
4330 spin_lock_irqsave(&osb->dc_task_lock, flags);
4331 /* grab this early so we know to try again if a state change and
4332 * wake happens part-way through our work */
4333 osb->dc_work_sequence = osb->dc_wake_sequence;
4335 processed = osb->blocked_lock_count;
4337 * blocked lock processing in this loop might call iput which can
4338 * remove items off osb->blocked_lock_list. Downconvert up to
4339 * 'processed' number of locks, but stop short if we had some
4340 * removed in ocfs2_mark_lockres_freeing when downconverting.
4342 while (processed && !list_empty(&osb->blocked_lock_list)) {
4343 lockres = list_entry(osb->blocked_lock_list.next,
4344 struct ocfs2_lock_res, l_blocked_list);
4345 list_del_init(&lockres->l_blocked_list);
4346 osb->blocked_lock_count--;
4347 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4349 BUG_ON(!processed);
4350 processed--;
4352 ocfs2_process_blocked_lock(osb, lockres);
4354 spin_lock_irqsave(&osb->dc_task_lock, flags);
4356 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4359 static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4361 int empty = 0;
4362 unsigned long flags;
4364 spin_lock_irqsave(&osb->dc_task_lock, flags);
4365 if (list_empty(&osb->blocked_lock_list))
4366 empty = 1;
4368 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4369 return empty;
4372 static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4374 int should_wake = 0;
4375 unsigned long flags;
4377 spin_lock_irqsave(&osb->dc_task_lock, flags);
4378 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4379 should_wake = 1;
4380 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4382 return should_wake;
4385 static int ocfs2_downconvert_thread(void *arg)
4387 int status = 0;
4388 struct ocfs2_super *osb = arg;
4390 /* only quit once we've been asked to stop and there is no more
4391 * work available */
4392 while (!(kthread_should_stop() &&
4393 ocfs2_downconvert_thread_lists_empty(osb))) {
4395 wait_event_interruptible(osb->dc_event,
4396 ocfs2_downconvert_thread_should_wake(osb) ||
4397 kthread_should_stop());
4399 mlog(0, "downconvert_thread: awoken\n");
4401 ocfs2_downconvert_thread_do_work(osb);
4404 osb->dc_task = NULL;
4405 return status;
4408 void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4410 unsigned long flags;
4412 spin_lock_irqsave(&osb->dc_task_lock, flags);
4413 /* make sure the voting thread gets a swipe at whatever changes
4414 * the caller may have made to the voting state */
4415 osb->dc_wake_sequence++;
4416 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4417 wake_up(&osb->dc_event);