1
2
3
4
5
6
7
8
9
10
11
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/highmem.h>
15#include <linux/mm.h>
16#include <linux/kthread.h>
17#include <linux/pagemap.h>
18#include <linux/debugfs.h>
19#include <linux/seq_file.h>
20#include <linux/time.h>
21#include <linux/quotaops.h>
22#include <linux/sched/signal.h>
23
24#define MLOG_MASK_PREFIX ML_DLM_GLUE
25#include <cluster/masklog.h>
26
27#include "ocfs2.h"
28#include "ocfs2_lockingver.h"
29
30#include "alloc.h"
31#include "dcache.h"
32#include "dlmglue.h"
33#include "extent_map.h"
34#include "file.h"
35#include "heartbeat.h"
36#include "inode.h"
37#include "journal.h"
38#include "stackglue.h"
39#include "slot_map.h"
40#include "super.h"
41#include "uptodate.h"
42#include "quota.h"
43#include "refcounttree.h"
44#include "acl.h"
45
46#include "buffer_head_io.h"
47
48struct ocfs2_mask_waiter {
49 struct list_head mw_item;
50 int mw_status;
51 struct completion mw_complete;
52 unsigned long mw_mask;
53 unsigned long mw_goal;
54#ifdef CONFIG_OCFS2_FS_STATS
55 ktime_t mw_lock_start;
56#endif
57};
58
59static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
60static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
61static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
62static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
63
64
65
66
67
68
69
70
71enum ocfs2_unblock_action {
72 UNBLOCK_CONTINUE = 0,
73 UNBLOCK_CONTINUE_POST = 1,
74
75 UNBLOCK_STOP_POST = 2,
76
77};
78
79struct ocfs2_unblock_ctl {
80 int requeue;
81 enum ocfs2_unblock_action unblock_action;
82};
83
84
85#ifdef CONFIG_DEBUG_LOCK_ALLOC
86static struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
87#endif
88
89static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
90 int new_level);
91static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
92
93static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
94 int blocking);
95
96static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
97 int blocking);
98
99static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
100 struct ocfs2_lock_res *lockres);
101
102static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
103
104static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
105 int new_level);
106static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
107 int blocking);
108
109#define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
110
111
112static void ocfs2_dump_meta_lvb_info(u64 level,
113 const char *function,
114 unsigned int line,
115 struct ocfs2_lock_res *lockres)
116{
117 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
118
119 mlog(level, "LVB information for %s (called from %s:%u):\n",
120 lockres->l_name, function, line);
121 mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
122 lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
123 be32_to_cpu(lvb->lvb_igeneration));
124 mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
125 (unsigned long long)be64_to_cpu(lvb->lvb_isize),
126 be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
127 be16_to_cpu(lvb->lvb_imode));
128 mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
129 "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
130 (long long)be64_to_cpu(lvb->lvb_iatime_packed),
131 (long long)be64_to_cpu(lvb->lvb_ictime_packed),
132 (long long)be64_to_cpu(lvb->lvb_imtime_packed),
133 be32_to_cpu(lvb->lvb_iattr));
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150struct ocfs2_lock_res_ops {
151
152
153
154
155 struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
156
157
158
159
160
161
162
163
164
165
166 void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
167
168
169
170
171
172
173
174
175
176
177
178 int (*check_downconvert)(struct ocfs2_lock_res *, int);
179
180
181
182
183
184
185
186
187
188
189 void (*set_lvb)(struct ocfs2_lock_res *);
190
191
192
193
194
195
196
197
198
199
200 int (*downconvert_worker)(struct ocfs2_lock_res *, int);
201
202
203
204
205
206 int flags;
207};
208
209
210
211
212
213
214
215
216
217#define LOCK_TYPE_REQUIRES_REFRESH 0x1
218
219
220
221
222
223#define LOCK_TYPE_USES_LVB 0x2
224
225static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
226 .get_osb = ocfs2_get_inode_osb,
227 .flags = 0,
228};
229
230static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
231 .get_osb = ocfs2_get_inode_osb,
232 .check_downconvert = ocfs2_check_meta_downconvert,
233 .set_lvb = ocfs2_set_meta_lvb,
234 .downconvert_worker = ocfs2_data_convert_worker,
235 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
236};
237
238static struct ocfs2_lock_res_ops ocfs2_super_lops = {
239 .flags = LOCK_TYPE_REQUIRES_REFRESH,
240};
241
242static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
243 .flags = 0,
244};
245
246static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
247 .flags = 0,
248};
249
250static struct ocfs2_lock_res_ops ocfs2_trim_fs_lops = {
251 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
252};
253
254static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
255 .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
256};
257
258static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
259 .get_osb = ocfs2_get_dentry_osb,
260 .post_unlock = ocfs2_dentry_post_unlock,
261 .downconvert_worker = ocfs2_dentry_convert_worker,
262 .flags = 0,
263};
264
265static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
266 .get_osb = ocfs2_get_inode_osb,
267 .flags = 0,
268};
269
270static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
271 .get_osb = ocfs2_get_file_osb,
272 .flags = 0,
273};
274
275static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
276 .set_lvb = ocfs2_set_qinfo_lvb,
277 .get_osb = ocfs2_get_qinfo_osb,
278 .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
279};
280
281static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
282 .check_downconvert = ocfs2_check_refcount_downconvert,
283 .downconvert_worker = ocfs2_refcount_convert_worker,
284 .flags = 0,
285};
286
287static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
288{
289 return lockres->l_type == OCFS2_LOCK_TYPE_META ||
290 lockres->l_type == OCFS2_LOCK_TYPE_RW ||
291 lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
292}
293
294static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
295{
296 return container_of(lksb, struct ocfs2_lock_res, l_lksb);
297}
298
299static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
300{
301 BUG_ON(!ocfs2_is_inode_lock(lockres));
302
303 return (struct inode *) lockres->l_priv;
304}
305
306static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
307{
308 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
309
310 return (struct ocfs2_dentry_lock *)lockres->l_priv;
311}
312
313static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
314{
315 BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
316
317 return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
318}
319
320static inline struct ocfs2_refcount_tree *
321ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
322{
323 return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
324}
325
326static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
327{
328 if (lockres->l_ops->get_osb)
329 return lockres->l_ops->get_osb(lockres);
330
331 return (struct ocfs2_super *)lockres->l_priv;
332}
333
334static int ocfs2_lock_create(struct ocfs2_super *osb,
335 struct ocfs2_lock_res *lockres,
336 int level,
337 u32 dlm_flags);
338static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
339 int wanted);
340static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
341 struct ocfs2_lock_res *lockres,
342 int level, unsigned long caller_ip);
343static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
344 struct ocfs2_lock_res *lockres,
345 int level)
346{
347 __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
348}
349
350static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
351static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
352static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
353static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
354static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
355 struct ocfs2_lock_res *lockres);
356static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
357 int convert);
358#define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
359 if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
360 mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
361 _err, _func, _lockres->l_name); \
362 else \
363 mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
364 _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
365 (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
366} while (0)
367static int ocfs2_downconvert_thread(void *arg);
368static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
369 struct ocfs2_lock_res *lockres);
370static int ocfs2_inode_lock_update(struct inode *inode,
371 struct buffer_head **bh);
372static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
373static inline int ocfs2_highest_compat_lock_level(int level);
374static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
375 int new_level);
376static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
377 struct ocfs2_lock_res *lockres,
378 int new_level,
379 int lvb,
380 unsigned int generation);
381static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
382 struct ocfs2_lock_res *lockres);
383static int ocfs2_cancel_convert(struct ocfs2_super *osb,
384 struct ocfs2_lock_res *lockres);
385
386
387static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
388 u64 blkno,
389 u32 generation,
390 char *name)
391{
392 int len;
393
394 BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
395
396 len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
397 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
398 (long long)blkno, generation);
399
400 BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
401
402 mlog(0, "built lock resource with name: %s\n", name);
403}
404
405static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
406
407static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
408 struct ocfs2_dlm_debug *dlm_debug)
409{
410 mlog(0, "Add tracking for lockres %s\n", res->l_name);
411
412 spin_lock(&ocfs2_dlm_tracking_lock);
413 list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
414 spin_unlock(&ocfs2_dlm_tracking_lock);
415}
416
417static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
418{
419 spin_lock(&ocfs2_dlm_tracking_lock);
420 if (!list_empty(&res->l_debug_list))
421 list_del_init(&res->l_debug_list);
422 spin_unlock(&ocfs2_dlm_tracking_lock);
423}
424
425#ifdef CONFIG_OCFS2_FS_STATS
426static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
427{
428 res->l_lock_refresh = 0;
429 res->l_lock_wait = 0;
430 memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
431 memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
432}
433
434static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
435 struct ocfs2_mask_waiter *mw, int ret)
436{
437 u32 usec;
438 ktime_t kt;
439 struct ocfs2_lock_stats *stats;
440
441 if (level == LKM_PRMODE)
442 stats = &res->l_lock_prmode;
443 else if (level == LKM_EXMODE)
444 stats = &res->l_lock_exmode;
445 else
446 return;
447
448 kt = ktime_sub(ktime_get(), mw->mw_lock_start);
449 usec = ktime_to_us(kt);
450
451 stats->ls_gets++;
452 stats->ls_total += ktime_to_ns(kt);
453
454 if (unlikely(stats->ls_gets == 0)) {
455 stats->ls_gets++;
456 stats->ls_total = ktime_to_ns(kt);
457 }
458
459 if (stats->ls_max < usec)
460 stats->ls_max = usec;
461
462 if (ret)
463 stats->ls_fail++;
464
465 stats->ls_last = ktime_to_us(ktime_get_real());
466}
467
468static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
469{
470 lockres->l_lock_refresh++;
471}
472
473static inline void ocfs2_track_lock_wait(struct ocfs2_lock_res *lockres)
474{
475 struct ocfs2_mask_waiter *mw;
476
477 if (list_empty(&lockres->l_mask_waiters)) {
478 lockres->l_lock_wait = 0;
479 return;
480 }
481
482 mw = list_first_entry(&lockres->l_mask_waiters,
483 struct ocfs2_mask_waiter, mw_item);
484 lockres->l_lock_wait =
485 ktime_to_us(ktime_mono_to_real(mw->mw_lock_start));
486}
487
488static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
489{
490 mw->mw_lock_start = ktime_get();
491}
492#else
493static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
494{
495}
496static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
497 int level, struct ocfs2_mask_waiter *mw, int ret)
498{
499}
500static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
501{
502}
503static inline void ocfs2_track_lock_wait(struct ocfs2_lock_res *lockres)
504{
505}
506static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
507{
508}
509#endif
510
511static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
512 struct ocfs2_lock_res *res,
513 enum ocfs2_lock_type type,
514 struct ocfs2_lock_res_ops *ops,
515 void *priv)
516{
517 res->l_type = type;
518 res->l_ops = ops;
519 res->l_priv = priv;
520
521 res->l_level = DLM_LOCK_IV;
522 res->l_requested = DLM_LOCK_IV;
523 res->l_blocking = DLM_LOCK_IV;
524 res->l_action = OCFS2_AST_INVALID;
525 res->l_unlock_action = OCFS2_UNLOCK_INVALID;
526
527 res->l_flags = OCFS2_LOCK_INITIALIZED;
528
529 ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
530
531 ocfs2_init_lock_stats(res);
532#ifdef CONFIG_DEBUG_LOCK_ALLOC
533 if (type != OCFS2_LOCK_TYPE_OPEN)
534 lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
535 &lockdep_keys[type], 0);
536 else
537 res->l_lockdep_map.key = NULL;
538#endif
539}
540
541void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
542{
543
544 memset(res, 0, sizeof(struct ocfs2_lock_res));
545 spin_lock_init(&res->l_lock);
546 init_waitqueue_head(&res->l_event);
547 INIT_LIST_HEAD(&res->l_blocked_list);
548 INIT_LIST_HEAD(&res->l_mask_waiters);
549 INIT_LIST_HEAD(&res->l_holders);
550}
551
552void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
553 enum ocfs2_lock_type type,
554 unsigned int generation,
555 struct inode *inode)
556{
557 struct ocfs2_lock_res_ops *ops;
558
559 switch(type) {
560 case OCFS2_LOCK_TYPE_RW:
561 ops = &ocfs2_inode_rw_lops;
562 break;
563 case OCFS2_LOCK_TYPE_META:
564 ops = &ocfs2_inode_inode_lops;
565 break;
566 case OCFS2_LOCK_TYPE_OPEN:
567 ops = &ocfs2_inode_open_lops;
568 break;
569 default:
570 mlog_bug_on_msg(1, "type: %d\n", type);
571 ops = NULL;
572 break;
573 };
574
575 ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
576 generation, res->l_name);
577 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
578}
579
580static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
581{
582 struct inode *inode = ocfs2_lock_res_inode(lockres);
583
584 return OCFS2_SB(inode->i_sb);
585}
586
587static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
588{
589 struct ocfs2_mem_dqinfo *info = lockres->l_priv;
590
591 return OCFS2_SB(info->dqi_gi.dqi_sb);
592}
593
594static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
595{
596 struct ocfs2_file_private *fp = lockres->l_priv;
597
598 return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
599}
600
601static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
602{
603 __be64 inode_blkno_be;
604
605 memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
606 sizeof(__be64));
607
608 return be64_to_cpu(inode_blkno_be);
609}
610
611static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
612{
613 struct ocfs2_dentry_lock *dl = lockres->l_priv;
614
615 return OCFS2_SB(dl->dl_inode->i_sb);
616}
617
618void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
619 u64 parent, struct inode *inode)
620{
621 int len;
622 u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
623 __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
624 struct ocfs2_lock_res *lockres = &dl->dl_lockres;
625
626 ocfs2_lock_res_init_once(lockres);
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
644 "%c%016llx",
645 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
646 (long long)parent);
647
648 BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
649
650 memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
651 sizeof(__be64));
652
653 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
654 OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
655 dl);
656}
657
658static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
659 struct ocfs2_super *osb)
660{
661
662
663 ocfs2_lock_res_init_once(res);
664 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
665 0, res->l_name);
666 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
667 &ocfs2_super_lops, osb);
668}
669
670static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
671 struct ocfs2_super *osb)
672{
673
674
675 ocfs2_lock_res_init_once(res);
676 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
677 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
678 &ocfs2_rename_lops, osb);
679}
680
681static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
682 struct ocfs2_super *osb)
683{
684
685
686 ocfs2_lock_res_init_once(res);
687 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
688 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
689 &ocfs2_nfs_sync_lops, osb);
690}
691
692void ocfs2_trim_fs_lock_res_init(struct ocfs2_super *osb)
693{
694 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
695
696
697 mutex_lock(&osb->obs_trim_fs_mutex);
698
699 ocfs2_lock_res_init_once(lockres);
700 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_TRIM_FS, 0, 0, lockres->l_name);
701 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_TRIM_FS,
702 &ocfs2_trim_fs_lops, osb);
703}
704
705void ocfs2_trim_fs_lock_res_uninit(struct ocfs2_super *osb)
706{
707 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
708
709 ocfs2_simple_drop_lockres(osb, lockres);
710 ocfs2_lock_res_free(lockres);
711
712 mutex_unlock(&osb->obs_trim_fs_mutex);
713}
714
715static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
716 struct ocfs2_super *osb)
717{
718 ocfs2_lock_res_init_once(res);
719 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
720 ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
721 &ocfs2_orphan_scan_lops, osb);
722}
723
724void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
725 struct ocfs2_file_private *fp)
726{
727 struct inode *inode = fp->fp_file->f_mapping->host;
728 struct ocfs2_inode_info *oi = OCFS2_I(inode);
729
730 ocfs2_lock_res_init_once(lockres);
731 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
732 inode->i_generation, lockres->l_name);
733 ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
734 OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
735 fp);
736 lockres->l_flags |= OCFS2_LOCK_NOCACHE;
737}
738
739void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
740 struct ocfs2_mem_dqinfo *info)
741{
742 ocfs2_lock_res_init_once(lockres);
743 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
744 0, lockres->l_name);
745 ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
746 OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
747 info);
748}
749
750void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
751 struct ocfs2_super *osb, u64 ref_blkno,
752 unsigned int generation)
753{
754 ocfs2_lock_res_init_once(lockres);
755 ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
756 generation, lockres->l_name);
757 ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
758 &ocfs2_refcount_block_lops, osb);
759}
760
761void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
762{
763 if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
764 return;
765
766 ocfs2_remove_lockres_tracking(res);
767
768 mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
769 "Lockres %s is on the blocked list\n",
770 res->l_name);
771 mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
772 "Lockres %s has mask waiters pending\n",
773 res->l_name);
774 mlog_bug_on_msg(spin_is_locked(&res->l_lock),
775 "Lockres %s is locked\n",
776 res->l_name);
777 mlog_bug_on_msg(res->l_ro_holders,
778 "Lockres %s has %u ro holders\n",
779 res->l_name, res->l_ro_holders);
780 mlog_bug_on_msg(res->l_ex_holders,
781 "Lockres %s has %u ex holders\n",
782 res->l_name, res->l_ex_holders);
783
784
785 memset(&res->l_lksb, 0, sizeof(res->l_lksb));
786
787 res->l_flags = 0UL;
788}
789
790
791
792
793
794static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
795 struct ocfs2_lock_holder *oh)
796{
797 INIT_LIST_HEAD(&oh->oh_list);
798 oh->oh_owner_pid = get_pid(task_pid(current));
799
800 spin_lock(&lockres->l_lock);
801 list_add_tail(&oh->oh_list, &lockres->l_holders);
802 spin_unlock(&lockres->l_lock);
803}
804
805static struct ocfs2_lock_holder *
806ocfs2_pid_holder(struct ocfs2_lock_res *lockres,
807 struct pid *pid)
808{
809 struct ocfs2_lock_holder *oh;
810
811 spin_lock(&lockres->l_lock);
812 list_for_each_entry(oh, &lockres->l_holders, oh_list) {
813 if (oh->oh_owner_pid == pid) {
814 spin_unlock(&lockres->l_lock);
815 return oh;
816 }
817 }
818 spin_unlock(&lockres->l_lock);
819 return NULL;
820}
821
822static inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
823 struct ocfs2_lock_holder *oh)
824{
825 spin_lock(&lockres->l_lock);
826 list_del(&oh->oh_list);
827 spin_unlock(&lockres->l_lock);
828
829 put_pid(oh->oh_owner_pid);
830}
831
832
833static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
834 int level)
835{
836 BUG_ON(!lockres);
837
838 switch(level) {
839 case DLM_LOCK_EX:
840 lockres->l_ex_holders++;
841 break;
842 case DLM_LOCK_PR:
843 lockres->l_ro_holders++;
844 break;
845 default:
846 BUG();
847 }
848}
849
850static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
851 int level)
852{
853 BUG_ON(!lockres);
854
855 switch(level) {
856 case DLM_LOCK_EX:
857 BUG_ON(!lockres->l_ex_holders);
858 lockres->l_ex_holders--;
859 break;
860 case DLM_LOCK_PR:
861 BUG_ON(!lockres->l_ro_holders);
862 lockres->l_ro_holders--;
863 break;
864 default:
865 BUG();
866 }
867}
868
869
870
871
872static inline int ocfs2_highest_compat_lock_level(int level)
873{
874 int new_level = DLM_LOCK_EX;
875
876 if (level == DLM_LOCK_EX)
877 new_level = DLM_LOCK_NL;
878 else if (level == DLM_LOCK_PR)
879 new_level = DLM_LOCK_PR;
880 return new_level;
881}
882
883static void lockres_set_flags(struct ocfs2_lock_res *lockres,
884 unsigned long newflags)
885{
886 struct ocfs2_mask_waiter *mw, *tmp;
887
888 assert_spin_locked(&lockres->l_lock);
889
890 lockres->l_flags = newflags;
891
892 list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
893 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
894 continue;
895
896 list_del_init(&mw->mw_item);
897 mw->mw_status = 0;
898 complete(&mw->mw_complete);
899 ocfs2_track_lock_wait(lockres);
900 }
901}
902static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
903{
904 lockres_set_flags(lockres, lockres->l_flags | or);
905}
906static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
907 unsigned long clear)
908{
909 lockres_set_flags(lockres, lockres->l_flags & ~clear);
910}
911
912static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
913{
914 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
915 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
916 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
917 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
918
919 lockres->l_level = lockres->l_requested;
920 if (lockres->l_level <=
921 ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
922 lockres->l_blocking = DLM_LOCK_NL;
923 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
924 }
925 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
926}
927
928static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
929{
930 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
931 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
932
933
934
935
936
937 if (lockres->l_level == DLM_LOCK_NL &&
938 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
939 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
940
941 lockres->l_level = lockres->l_requested;
942
943
944
945
946
947
948
949
950 if (!(lockres->l_flags & OCFS2_LOCK_NONBLOCK_FINISHED))
951 lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
952 else
953 lockres_clear_flags(lockres, OCFS2_LOCK_NONBLOCK_FINISHED);
954
955 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
956}
957
958static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
959{
960 BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
961 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
962
963 if (lockres->l_requested > DLM_LOCK_NL &&
964 !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
965 lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
966 lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
967
968 lockres->l_level = lockres->l_requested;
969 lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
970 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
971}
972
973static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
974 int level)
975{
976 int needs_downconvert = 0;
977
978 assert_spin_locked(&lockres->l_lock);
979
980 if (level > lockres->l_blocking) {
981
982
983
984
985 if (ocfs2_highest_compat_lock_level(level) <
986 ocfs2_highest_compat_lock_level(lockres->l_blocking))
987 needs_downconvert = 1;
988
989 lockres->l_blocking = level;
990 }
991
992 mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
993 lockres->l_name, level, lockres->l_level, lockres->l_blocking,
994 needs_downconvert);
995
996 if (needs_downconvert)
997 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
998 mlog(0, "needs_downconvert = %d\n", needs_downconvert);
999 return needs_downconvert;
1000}
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
1062 unsigned int generation,
1063 struct ocfs2_super *osb)
1064{
1065 assert_spin_locked(&lockres->l_lock);
1066
1067
1068
1069
1070
1071 if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
1072 (lockres->l_pending_gen != generation))
1073 return;
1074
1075 lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
1076 lockres->l_pending_gen++;
1077
1078
1079
1080
1081
1082 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1083 ocfs2_wake_downconvert_thread(osb);
1084}
1085
1086
1087static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
1088 unsigned int generation,
1089 struct ocfs2_super *osb)
1090{
1091 unsigned long flags;
1092
1093 spin_lock_irqsave(&lockres->l_lock, flags);
1094 __lockres_clear_pending(lockres, generation, osb);
1095 spin_unlock_irqrestore(&lockres->l_lock, flags);
1096}
1097
1098static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
1099{
1100 assert_spin_locked(&lockres->l_lock);
1101 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
1102
1103 lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
1104
1105 return lockres->l_pending_gen;
1106}
1107
1108static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
1109{
1110 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1111 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1112 int needs_downconvert;
1113 unsigned long flags;
1114
1115 BUG_ON(level <= DLM_LOCK_NL);
1116
1117 mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
1118 "type %s\n", lockres->l_name, level, lockres->l_level,
1119 ocfs2_lock_type_string(lockres->l_type));
1120
1121
1122
1123
1124
1125 if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
1126 return;
1127
1128 spin_lock_irqsave(&lockres->l_lock, flags);
1129 needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
1130 if (needs_downconvert)
1131 ocfs2_schedule_blocked_lock(osb, lockres);
1132 spin_unlock_irqrestore(&lockres->l_lock, flags);
1133
1134 wake_up(&lockres->l_event);
1135
1136 ocfs2_wake_downconvert_thread(osb);
1137}
1138
1139static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
1140{
1141 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1142 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1143 unsigned long flags;
1144 int status;
1145
1146 spin_lock_irqsave(&lockres->l_lock, flags);
1147
1148 status = ocfs2_dlm_lock_status(&lockres->l_lksb);
1149
1150 if (status == -EAGAIN) {
1151 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1152 goto out;
1153 }
1154
1155 if (status) {
1156 mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
1157 lockres->l_name, status);
1158 spin_unlock_irqrestore(&lockres->l_lock, flags);
1159 return;
1160 }
1161
1162 mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
1163 "level %d => %d\n", lockres->l_name, lockres->l_action,
1164 lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
1165
1166 switch(lockres->l_action) {
1167 case OCFS2_AST_ATTACH:
1168 ocfs2_generic_handle_attach_action(lockres);
1169 lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
1170 break;
1171 case OCFS2_AST_CONVERT:
1172 ocfs2_generic_handle_convert_action(lockres);
1173 break;
1174 case OCFS2_AST_DOWNCONVERT:
1175 ocfs2_generic_handle_downconvert_action(lockres);
1176 break;
1177 default:
1178 mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
1179 "flags 0x%lx, unlock: %u\n",
1180 lockres->l_name, lockres->l_action, lockres->l_flags,
1181 lockres->l_unlock_action);
1182 BUG();
1183 }
1184out:
1185
1186
1187 lockres->l_action = OCFS2_AST_INVALID;
1188
1189
1190 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
1191 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1192
1193
1194
1195
1196
1197
1198
1199 __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
1200
1201 wake_up(&lockres->l_event);
1202 spin_unlock_irqrestore(&lockres->l_lock, flags);
1203}
1204
1205static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
1206{
1207 struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
1208 unsigned long flags;
1209
1210 mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
1211 lockres->l_name, lockres->l_unlock_action);
1212
1213 spin_lock_irqsave(&lockres->l_lock, flags);
1214 if (error) {
1215 mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
1216 "unlock_action %d\n", error, lockres->l_name,
1217 lockres->l_unlock_action);
1218 spin_unlock_irqrestore(&lockres->l_lock, flags);
1219 return;
1220 }
1221
1222 switch(lockres->l_unlock_action) {
1223 case OCFS2_UNLOCK_CANCEL_CONVERT:
1224 mlog(0, "Cancel convert success for %s\n", lockres->l_name);
1225 lockres->l_action = OCFS2_AST_INVALID;
1226
1227
1228 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
1229 ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
1230 break;
1231 case OCFS2_UNLOCK_DROP_LOCK:
1232 lockres->l_level = DLM_LOCK_IV;
1233 break;
1234 default:
1235 BUG();
1236 }
1237
1238 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1239 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1240 wake_up(&lockres->l_event);
1241 spin_unlock_irqrestore(&lockres->l_lock, flags);
1242}
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267static struct ocfs2_locking_protocol lproto = {
1268 .lp_max_version = {
1269 .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
1270 .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
1271 },
1272 .lp_lock_ast = ocfs2_locking_ast,
1273 .lp_blocking_ast = ocfs2_blocking_ast,
1274 .lp_unlock_ast = ocfs2_unlock_ast,
1275};
1276
1277void ocfs2_set_locking_protocol(void)
1278{
1279 ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
1280}
1281
1282static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
1283 int convert)
1284{
1285 unsigned long flags;
1286
1287 spin_lock_irqsave(&lockres->l_lock, flags);
1288 lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
1289 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1290 if (convert)
1291 lockres->l_action = OCFS2_AST_INVALID;
1292 else
1293 lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
1294 spin_unlock_irqrestore(&lockres->l_lock, flags);
1295
1296 wake_up(&lockres->l_event);
1297}
1298
1299
1300
1301
1302
1303static int ocfs2_lock_create(struct ocfs2_super *osb,
1304 struct ocfs2_lock_res *lockres,
1305 int level,
1306 u32 dlm_flags)
1307{
1308 int ret = 0;
1309 unsigned long flags;
1310 unsigned int gen;
1311
1312 mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
1313 dlm_flags);
1314
1315 spin_lock_irqsave(&lockres->l_lock, flags);
1316 if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
1317 (lockres->l_flags & OCFS2_LOCK_BUSY)) {
1318 spin_unlock_irqrestore(&lockres->l_lock, flags);
1319 goto bail;
1320 }
1321
1322 lockres->l_action = OCFS2_AST_ATTACH;
1323 lockres->l_requested = level;
1324 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1325 gen = lockres_set_pending(lockres);
1326 spin_unlock_irqrestore(&lockres->l_lock, flags);
1327
1328 ret = ocfs2_dlm_lock(osb->cconn,
1329 level,
1330 &lockres->l_lksb,
1331 dlm_flags,
1332 lockres->l_name,
1333 OCFS2_LOCK_ID_MAX_LEN - 1);
1334 lockres_clear_pending(lockres, gen, osb);
1335 if (ret) {
1336 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
1337 ocfs2_recover_from_dlm_error(lockres, 1);
1338 }
1339
1340 mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
1341
1342bail:
1343 return ret;
1344}
1345
1346static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
1347 int flag)
1348{
1349 unsigned long flags;
1350 int ret;
1351
1352 spin_lock_irqsave(&lockres->l_lock, flags);
1353 ret = lockres->l_flags & flag;
1354 spin_unlock_irqrestore(&lockres->l_lock, flags);
1355
1356 return ret;
1357}
1358
1359static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
1360
1361{
1362 wait_event(lockres->l_event,
1363 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
1364}
1365
1366static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
1367
1368{
1369 wait_event(lockres->l_event,
1370 !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
1371}
1372
1373
1374
1375
1376static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
1377 int wanted)
1378{
1379 BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
1380
1381 return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
1382}
1383
1384static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
1385{
1386 INIT_LIST_HEAD(&mw->mw_item);
1387 init_completion(&mw->mw_complete);
1388 ocfs2_init_start_time(mw);
1389}
1390
1391static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
1392{
1393 wait_for_completion(&mw->mw_complete);
1394
1395 reinit_completion(&mw->mw_complete);
1396 return mw->mw_status;
1397}
1398
1399static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
1400 struct ocfs2_mask_waiter *mw,
1401 unsigned long mask,
1402 unsigned long goal)
1403{
1404 BUG_ON(!list_empty(&mw->mw_item));
1405
1406 assert_spin_locked(&lockres->l_lock);
1407
1408 list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
1409 mw->mw_mask = mask;
1410 mw->mw_goal = goal;
1411 ocfs2_track_lock_wait(lockres);
1412}
1413
1414
1415
1416static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1417 struct ocfs2_mask_waiter *mw)
1418{
1419 int ret = 0;
1420
1421 assert_spin_locked(&lockres->l_lock);
1422 if (!list_empty(&mw->mw_item)) {
1423 if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
1424 ret = -EBUSY;
1425
1426 list_del_init(&mw->mw_item);
1427 init_completion(&mw->mw_complete);
1428 ocfs2_track_lock_wait(lockres);
1429 }
1430
1431 return ret;
1432}
1433
1434static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
1435 struct ocfs2_mask_waiter *mw)
1436{
1437 unsigned long flags;
1438 int ret = 0;
1439
1440 spin_lock_irqsave(&lockres->l_lock, flags);
1441 ret = __lockres_remove_mask_waiter(lockres, mw);
1442 spin_unlock_irqrestore(&lockres->l_lock, flags);
1443
1444 return ret;
1445
1446}
1447
1448static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
1449 struct ocfs2_lock_res *lockres)
1450{
1451 int ret;
1452
1453 ret = wait_for_completion_interruptible(&mw->mw_complete);
1454 if (ret)
1455 lockres_remove_mask_waiter(lockres, mw);
1456 else
1457 ret = mw->mw_status;
1458
1459 reinit_completion(&mw->mw_complete);
1460 return ret;
1461}
1462
1463static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
1464 struct ocfs2_lock_res *lockres,
1465 int level,
1466 u32 lkm_flags,
1467 int arg_flags,
1468 int l_subclass,
1469 unsigned long caller_ip)
1470{
1471 struct ocfs2_mask_waiter mw;
1472 int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
1473 int ret = 0;
1474 unsigned long flags;
1475 unsigned int gen;
1476 int noqueue_attempted = 0;
1477 int dlm_locked = 0;
1478 int kick_dc = 0;
1479
1480 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) {
1481 mlog_errno(-EINVAL);
1482 return -EINVAL;
1483 }
1484
1485 ocfs2_init_mask_waiter(&mw);
1486
1487 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
1488 lkm_flags |= DLM_LKF_VALBLK;
1489
1490again:
1491 wait = 0;
1492
1493 spin_lock_irqsave(&lockres->l_lock, flags);
1494
1495 if (catch_signals && signal_pending(current)) {
1496 ret = -ERESTARTSYS;
1497 goto unlock;
1498 }
1499
1500 mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
1501 "Cluster lock called on freeing lockres %s! flags "
1502 "0x%lx\n", lockres->l_name, lockres->l_flags);
1503
1504
1505
1506
1507 if (lockres->l_flags & OCFS2_LOCK_BUSY &&
1508 level > lockres->l_level) {
1509
1510
1511 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1512 wait = 1;
1513 goto unlock;
1514 }
1515
1516 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531 if (level <= lockres->l_level)
1532 goto update_holders;
1533 }
1534
1535 if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
1536 !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
1537
1538
1539 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
1540 wait = 1;
1541 goto unlock;
1542 }
1543
1544 if (level > lockres->l_level) {
1545 if (noqueue_attempted > 0) {
1546 ret = -EAGAIN;
1547 goto unlock;
1548 }
1549 if (lkm_flags & DLM_LKF_NOQUEUE)
1550 noqueue_attempted = 1;
1551
1552 if (lockres->l_action != OCFS2_AST_INVALID)
1553 mlog(ML_ERROR, "lockres %s has action %u pending\n",
1554 lockres->l_name, lockres->l_action);
1555
1556 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1557 lockres->l_action = OCFS2_AST_ATTACH;
1558 lkm_flags &= ~DLM_LKF_CONVERT;
1559 } else {
1560 lockres->l_action = OCFS2_AST_CONVERT;
1561 lkm_flags |= DLM_LKF_CONVERT;
1562 }
1563
1564 lockres->l_requested = level;
1565 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
1566 gen = lockres_set_pending(lockres);
1567 spin_unlock_irqrestore(&lockres->l_lock, flags);
1568
1569 BUG_ON(level == DLM_LOCK_IV);
1570 BUG_ON(level == DLM_LOCK_NL);
1571
1572 mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
1573 lockres->l_name, lockres->l_level, level);
1574
1575
1576 ret = ocfs2_dlm_lock(osb->cconn,
1577 level,
1578 &lockres->l_lksb,
1579 lkm_flags,
1580 lockres->l_name,
1581 OCFS2_LOCK_ID_MAX_LEN - 1);
1582 lockres_clear_pending(lockres, gen, osb);
1583 if (ret) {
1584 if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
1585 (ret != -EAGAIN)) {
1586 ocfs2_log_dlm_error("ocfs2_dlm_lock",
1587 ret, lockres);
1588 }
1589 ocfs2_recover_from_dlm_error(lockres, 1);
1590 goto out;
1591 }
1592 dlm_locked = 1;
1593
1594 mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
1595 lockres->l_name);
1596
1597
1598
1599 catch_signals = 0;
1600
1601
1602 goto again;
1603 }
1604
1605update_holders:
1606
1607 ocfs2_inc_holders(lockres, level);
1608
1609 ret = 0;
1610unlock:
1611 lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
1612
1613
1614 kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED);
1615
1616 spin_unlock_irqrestore(&lockres->l_lock, flags);
1617 if (kick_dc)
1618 ocfs2_wake_downconvert_thread(osb);
1619out:
1620
1621
1622
1623
1624
1625
1626
1627
1628 if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
1629 mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
1630 wait = 0;
1631 spin_lock_irqsave(&lockres->l_lock, flags);
1632 if (__lockres_remove_mask_waiter(lockres, &mw)) {
1633 if (dlm_locked)
1634 lockres_or_flags(lockres,
1635 OCFS2_LOCK_NONBLOCK_FINISHED);
1636 spin_unlock_irqrestore(&lockres->l_lock, flags);
1637 ret = -EAGAIN;
1638 } else {
1639 spin_unlock_irqrestore(&lockres->l_lock, flags);
1640 goto again;
1641 }
1642 }
1643 if (wait) {
1644 ret = ocfs2_wait_for_mask(&mw);
1645 if (ret == 0)
1646 goto again;
1647 mlog_errno(ret);
1648 }
1649 ocfs2_update_lock_stats(lockres, level, &mw, ret);
1650
1651#ifdef CONFIG_DEBUG_LOCK_ALLOC
1652 if (!ret && lockres->l_lockdep_map.key != NULL) {
1653 if (level == DLM_LOCK_PR)
1654 rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
1655 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1656 caller_ip);
1657 else
1658 rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
1659 !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
1660 caller_ip);
1661 }
1662#endif
1663 return ret;
1664}
1665
1666static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
1667 struct ocfs2_lock_res *lockres,
1668 int level,
1669 u32 lkm_flags,
1670 int arg_flags)
1671{
1672 return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
1673 0, _RET_IP_);
1674}
1675
1676
1677static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
1678 struct ocfs2_lock_res *lockres,
1679 int level,
1680 unsigned long caller_ip)
1681{
1682 unsigned long flags;
1683
1684 spin_lock_irqsave(&lockres->l_lock, flags);
1685 ocfs2_dec_holders(lockres, level);
1686 ocfs2_downconvert_on_unlock(osb, lockres);
1687 spin_unlock_irqrestore(&lockres->l_lock, flags);
1688#ifdef CONFIG_DEBUG_LOCK_ALLOC
1689 if (lockres->l_lockdep_map.key != NULL)
1690 rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
1691#endif
1692}
1693
1694static int ocfs2_create_new_lock(struct ocfs2_super *osb,
1695 struct ocfs2_lock_res *lockres,
1696 int ex,
1697 int local)
1698{
1699 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1700 unsigned long flags;
1701 u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
1702
1703 spin_lock_irqsave(&lockres->l_lock, flags);
1704 BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
1705 lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
1706 spin_unlock_irqrestore(&lockres->l_lock, flags);
1707
1708 return ocfs2_lock_create(osb, lockres, level, lkm_flags);
1709}
1710
1711
1712
1713
1714
1715
1716
1717int ocfs2_create_new_inode_locks(struct inode *inode)
1718{
1719 int ret;
1720 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1721
1722 BUG_ON(!ocfs2_inode_is_new(inode));
1723
1724 mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
1735 if (ret) {
1736 mlog_errno(ret);
1737 goto bail;
1738 }
1739
1740
1741
1742
1743
1744 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
1745 if (ret) {
1746 mlog_errno(ret);
1747 goto bail;
1748 }
1749
1750 ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
1751 if (ret)
1752 mlog_errno(ret);
1753
1754bail:
1755 return ret;
1756}
1757
1758int ocfs2_rw_lock(struct inode *inode, int write)
1759{
1760 int status, level;
1761 struct ocfs2_lock_res *lockres;
1762 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1763
1764 mlog(0, "inode %llu take %s RW lock\n",
1765 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1766 write ? "EXMODE" : "PRMODE");
1767
1768 if (ocfs2_mount_local(osb))
1769 return 0;
1770
1771 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1772
1773 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1774
1775 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
1776 if (status < 0)
1777 mlog_errno(status);
1778
1779 return status;
1780}
1781
1782int ocfs2_try_rw_lock(struct inode *inode, int write)
1783{
1784 int status, level;
1785 struct ocfs2_lock_res *lockres;
1786 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1787
1788 mlog(0, "inode %llu try to take %s RW lock\n",
1789 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1790 write ? "EXMODE" : "PRMODE");
1791
1792 if (ocfs2_mount_local(osb))
1793 return 0;
1794
1795 lockres = &OCFS2_I(inode)->ip_rw_lockres;
1796
1797 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1798
1799 status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
1800 return status;
1801}
1802
1803void ocfs2_rw_unlock(struct inode *inode, int write)
1804{
1805 int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1806 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
1807 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1808
1809 mlog(0, "inode %llu drop %s RW lock\n",
1810 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1811 write ? "EXMODE" : "PRMODE");
1812
1813 if (!ocfs2_mount_local(osb))
1814 ocfs2_cluster_unlock(osb, lockres, level);
1815}
1816
1817
1818
1819
1820int ocfs2_open_lock(struct inode *inode)
1821{
1822 int status = 0;
1823 struct ocfs2_lock_res *lockres;
1824 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1825
1826 mlog(0, "inode %llu take PRMODE open lock\n",
1827 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1828
1829 if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
1830 goto out;
1831
1832 lockres = &OCFS2_I(inode)->ip_open_lockres;
1833
1834 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_PR, 0, 0);
1835 if (status < 0)
1836 mlog_errno(status);
1837
1838out:
1839 return status;
1840}
1841
1842int ocfs2_try_open_lock(struct inode *inode, int write)
1843{
1844 int status = 0, level;
1845 struct ocfs2_lock_res *lockres;
1846 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1847
1848 mlog(0, "inode %llu try to take %s open lock\n",
1849 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1850 write ? "EXMODE" : "PRMODE");
1851
1852 if (ocfs2_is_hard_readonly(osb)) {
1853 if (write)
1854 status = -EROFS;
1855 goto out;
1856 }
1857
1858 if (ocfs2_mount_local(osb))
1859 goto out;
1860
1861 lockres = &OCFS2_I(inode)->ip_open_lockres;
1862
1863 level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
1864
1865
1866
1867
1868
1869
1870
1871 status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
1872
1873out:
1874 return status;
1875}
1876
1877
1878
1879
1880void ocfs2_open_unlock(struct inode *inode)
1881{
1882 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
1883 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1884
1885 mlog(0, "inode %llu drop open lock\n",
1886 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1887
1888 if (ocfs2_mount_local(osb))
1889 goto out;
1890
1891 if(lockres->l_ro_holders)
1892 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_PR);
1893 if(lockres->l_ex_holders)
1894 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
1895
1896out:
1897 return;
1898}
1899
1900static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
1901 int level)
1902{
1903 int ret;
1904 struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
1905 unsigned long flags;
1906 struct ocfs2_mask_waiter mw;
1907
1908 ocfs2_init_mask_waiter(&mw);
1909
1910retry_cancel:
1911 spin_lock_irqsave(&lockres->l_lock, flags);
1912 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
1913 ret = ocfs2_prepare_cancel_convert(osb, lockres);
1914 if (ret) {
1915 spin_unlock_irqrestore(&lockres->l_lock, flags);
1916 ret = ocfs2_cancel_convert(osb, lockres);
1917 if (ret < 0) {
1918 mlog_errno(ret);
1919 goto out;
1920 }
1921 goto retry_cancel;
1922 }
1923 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1924 spin_unlock_irqrestore(&lockres->l_lock, flags);
1925
1926 ocfs2_wait_for_mask(&mw);
1927 goto retry_cancel;
1928 }
1929
1930 ret = -ERESTARTSYS;
1931
1932
1933
1934
1935 if (lockres->l_level == level)
1936 ret = 0;
1937
1938 mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
1939 lockres->l_flags, lockres->l_level, lockres->l_action);
1940
1941 spin_unlock_irqrestore(&lockres->l_lock, flags);
1942
1943out:
1944 return ret;
1945}
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968int ocfs2_file_lock(struct file *file, int ex, int trylock)
1969{
1970 int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
1971 unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
1972 unsigned long flags;
1973 struct ocfs2_file_private *fp = file->private_data;
1974 struct ocfs2_lock_res *lockres = &fp->fp_flock;
1975 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
1976 struct ocfs2_mask_waiter mw;
1977
1978 ocfs2_init_mask_waiter(&mw);
1979
1980 if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
1981 (lockres->l_level > DLM_LOCK_NL)) {
1982 mlog(ML_ERROR,
1983 "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
1984 "level: %u\n", lockres->l_name, lockres->l_flags,
1985 lockres->l_level);
1986 return -EINVAL;
1987 }
1988
1989 spin_lock_irqsave(&lockres->l_lock, flags);
1990 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
1991 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
1992 spin_unlock_irqrestore(&lockres->l_lock, flags);
1993
1994
1995
1996
1997
1998 ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
1999 if (ret < 0) {
2000 mlog_errno(ret);
2001 goto out;
2002 }
2003
2004 ret = ocfs2_wait_for_mask(&mw);
2005 if (ret) {
2006 mlog_errno(ret);
2007 goto out;
2008 }
2009 spin_lock_irqsave(&lockres->l_lock, flags);
2010 }
2011
2012 lockres->l_action = OCFS2_AST_CONVERT;
2013 lkm_flags |= DLM_LKF_CONVERT;
2014 lockres->l_requested = level;
2015 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
2016
2017 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2018 spin_unlock_irqrestore(&lockres->l_lock, flags);
2019
2020 ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
2021 lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
2022 if (ret) {
2023 if (!trylock || (ret != -EAGAIN)) {
2024 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
2025 ret = -EINVAL;
2026 }
2027
2028 ocfs2_recover_from_dlm_error(lockres, 1);
2029 lockres_remove_mask_waiter(lockres, &mw);
2030 goto out;
2031 }
2032
2033 ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
2034 if (ret == -ERESTARTSYS) {
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050 ret = ocfs2_flock_handle_signal(lockres, level);
2051 } else if (!ret && (level > lockres->l_level)) {
2052
2053 BUG_ON(!trylock);
2054 ret = -EAGAIN;
2055 }
2056
2057out:
2058
2059 mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
2060 lockres->l_name, ex, trylock, ret);
2061 return ret;
2062}
2063
2064void ocfs2_file_unlock(struct file *file)
2065{
2066 int ret;
2067 unsigned int gen;
2068 unsigned long flags;
2069 struct ocfs2_file_private *fp = file->private_data;
2070 struct ocfs2_lock_res *lockres = &fp->fp_flock;
2071 struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
2072 struct ocfs2_mask_waiter mw;
2073
2074 ocfs2_init_mask_waiter(&mw);
2075
2076 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
2077 return;
2078
2079 if (lockres->l_level == DLM_LOCK_NL)
2080 return;
2081
2082 mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
2083 lockres->l_name, lockres->l_flags, lockres->l_level,
2084 lockres->l_action);
2085
2086 spin_lock_irqsave(&lockres->l_lock, flags);
2087
2088
2089
2090 lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
2091 lockres->l_blocking = DLM_LOCK_EX;
2092
2093 gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
2094 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
2095 spin_unlock_irqrestore(&lockres->l_lock, flags);
2096
2097 ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
2098 if (ret) {
2099 mlog_errno(ret);
2100 return;
2101 }
2102
2103 ret = ocfs2_wait_for_mask(&mw);
2104 if (ret)
2105 mlog_errno(ret);
2106}
2107
2108static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
2109 struct ocfs2_lock_res *lockres)
2110{
2111 int kick = 0;
2112
2113
2114
2115
2116 if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
2117 switch(lockres->l_blocking) {
2118 case DLM_LOCK_EX:
2119 if (!lockres->l_ex_holders && !lockres->l_ro_holders)
2120 kick = 1;
2121 break;
2122 case DLM_LOCK_PR:
2123 if (!lockres->l_ex_holders)
2124 kick = 1;
2125 break;
2126 default:
2127 BUG();
2128 }
2129 }
2130
2131 if (kick)
2132 ocfs2_wake_downconvert_thread(osb);
2133}
2134
2135#define OCFS2_SEC_BITS 34
2136#define OCFS2_SEC_SHIFT (64 - 34)
2137#define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
2138
2139
2140
2141static u64 ocfs2_pack_timespec(struct timespec64 *spec)
2142{
2143 u64 res;
2144 u64 sec = clamp_t(time64_t, spec->tv_sec, 0, 0x3ffffffffull);
2145 u32 nsec = spec->tv_nsec;
2146
2147 res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
2148
2149 return res;
2150}
2151
2152
2153
2154
2155static void __ocfs2_stuff_meta_lvb(struct inode *inode)
2156{
2157 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2158 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2159 struct ocfs2_meta_lvb *lvb;
2160
2161 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2162
2163
2164
2165
2166
2167
2168 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2169 lvb->lvb_version = 0;
2170 goto out;
2171 }
2172
2173 lvb->lvb_version = OCFS2_LVB_VERSION;
2174 lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
2175 lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
2176 lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
2177 lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
2178 lvb->lvb_imode = cpu_to_be16(inode->i_mode);
2179 lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
2180 lvb->lvb_iatime_packed =
2181 cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
2182 lvb->lvb_ictime_packed =
2183 cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
2184 lvb->lvb_imtime_packed =
2185 cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
2186 lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
2187 lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
2188 lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
2189
2190out:
2191 mlog_meta_lvb(0, lockres);
2192}
2193
2194static void ocfs2_unpack_timespec(struct timespec64 *spec,
2195 u64 packed_time)
2196{
2197 spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
2198 spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
2199}
2200
2201static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
2202{
2203 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2204 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2205 struct ocfs2_meta_lvb *lvb;
2206
2207 mlog_meta_lvb(0, lockres);
2208
2209 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2210
2211
2212 spin_lock(&oi->ip_lock);
2213 oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
2214 i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
2215
2216 oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
2217 oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
2218 ocfs2_set_inode_flags(inode);
2219
2220
2221 if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
2222 inode->i_blocks = 0;
2223 else
2224 inode->i_blocks = ocfs2_inode_sector_count(inode);
2225
2226 i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
2227 i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
2228 inode->i_mode = be16_to_cpu(lvb->lvb_imode);
2229 set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
2230 ocfs2_unpack_timespec(&inode->i_atime,
2231 be64_to_cpu(lvb->lvb_iatime_packed));
2232 ocfs2_unpack_timespec(&inode->i_mtime,
2233 be64_to_cpu(lvb->lvb_imtime_packed));
2234 ocfs2_unpack_timespec(&inode->i_ctime,
2235 be64_to_cpu(lvb->lvb_ictime_packed));
2236 spin_unlock(&oi->ip_lock);
2237}
2238
2239static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
2240 struct ocfs2_lock_res *lockres)
2241{
2242 struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2243
2244 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
2245 && lvb->lvb_version == OCFS2_LVB_VERSION
2246 && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
2247 return 1;
2248 return 0;
2249}
2250
2251
2252
2253
2254
2255
2256
2257
2258static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
2259{
2260 unsigned long flags;
2261 int status = 0;
2262
2263refresh_check:
2264 spin_lock_irqsave(&lockres->l_lock, flags);
2265 if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
2266 spin_unlock_irqrestore(&lockres->l_lock, flags);
2267 goto bail;
2268 }
2269
2270 if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
2271 spin_unlock_irqrestore(&lockres->l_lock, flags);
2272
2273 ocfs2_wait_on_refreshing_lock(lockres);
2274 goto refresh_check;
2275 }
2276
2277
2278 lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
2279 spin_unlock_irqrestore(&lockres->l_lock, flags);
2280
2281 status = 1;
2282bail:
2283 mlog(0, "status %d\n", status);
2284 return status;
2285}
2286
2287
2288
2289static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
2290 int status)
2291{
2292 unsigned long flags;
2293
2294 spin_lock_irqsave(&lockres->l_lock, flags);
2295 lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
2296 if (!status)
2297 lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
2298 spin_unlock_irqrestore(&lockres->l_lock, flags);
2299
2300 wake_up(&lockres->l_event);
2301}
2302
2303
2304static int ocfs2_inode_lock_update(struct inode *inode,
2305 struct buffer_head **bh)
2306{
2307 int status = 0;
2308 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2309 struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
2310 struct ocfs2_dinode *fe;
2311 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2312
2313 if (ocfs2_mount_local(osb))
2314 goto bail;
2315
2316 spin_lock(&oi->ip_lock);
2317 if (oi->ip_flags & OCFS2_INODE_DELETED) {
2318 mlog(0, "Orphaned inode %llu was deleted while we "
2319 "were waiting on a lock. ip_flags = 0x%x\n",
2320 (unsigned long long)oi->ip_blkno, oi->ip_flags);
2321 spin_unlock(&oi->ip_lock);
2322 status = -ENOENT;
2323 goto bail;
2324 }
2325 spin_unlock(&oi->ip_lock);
2326
2327 if (!ocfs2_should_refresh_lock_res(lockres))
2328 goto bail;
2329
2330
2331
2332 ocfs2_metadata_cache_purge(INODE_CACHE(inode));
2333
2334 ocfs2_extent_map_trunc(inode, 0);
2335
2336 if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
2337 mlog(0, "Trusting LVB on inode %llu\n",
2338 (unsigned long long)oi->ip_blkno);
2339 ocfs2_refresh_inode_from_lvb(inode);
2340 } else {
2341
2342
2343 status = ocfs2_read_inode_block(inode, bh);
2344 if (status < 0) {
2345 mlog_errno(status);
2346 goto bail_refresh;
2347 }
2348 fe = (struct ocfs2_dinode *) (*bh)->b_data;
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359 mlog_bug_on_msg(inode->i_generation !=
2360 le32_to_cpu(fe->i_generation),
2361 "Invalid dinode %llu disk generation: %u "
2362 "inode->i_generation: %u\n",
2363 (unsigned long long)oi->ip_blkno,
2364 le32_to_cpu(fe->i_generation),
2365 inode->i_generation);
2366 mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
2367 !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
2368 "Stale dinode %llu dtime: %llu flags: 0x%x\n",
2369 (unsigned long long)oi->ip_blkno,
2370 (unsigned long long)le64_to_cpu(fe->i_dtime),
2371 le32_to_cpu(fe->i_flags));
2372
2373 ocfs2_refresh_inode(inode, fe);
2374 ocfs2_track_lock_refresh(lockres);
2375 }
2376
2377 status = 0;
2378bail_refresh:
2379 ocfs2_complete_lock_res_refresh(lockres, status);
2380bail:
2381 return status;
2382}
2383
2384static int ocfs2_assign_bh(struct inode *inode,
2385 struct buffer_head **ret_bh,
2386 struct buffer_head *passed_bh)
2387{
2388 int status;
2389
2390 if (passed_bh) {
2391
2392
2393 *ret_bh = passed_bh;
2394 get_bh(*ret_bh);
2395
2396 return 0;
2397 }
2398
2399 status = ocfs2_read_inode_block(inode, ret_bh);
2400 if (status < 0)
2401 mlog_errno(status);
2402
2403 return status;
2404}
2405
2406
2407
2408
2409
2410int ocfs2_inode_lock_full_nested(struct inode *inode,
2411 struct buffer_head **ret_bh,
2412 int ex,
2413 int arg_flags,
2414 int subclass)
2415{
2416 int status, level, acquired;
2417 u32 dlm_flags;
2418 struct ocfs2_lock_res *lockres = NULL;
2419 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2420 struct buffer_head *local_bh = NULL;
2421
2422 mlog(0, "inode %llu, take %s META lock\n",
2423 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2424 ex ? "EXMODE" : "PRMODE");
2425
2426 status = 0;
2427 acquired = 0;
2428
2429
2430 if (ocfs2_is_hard_readonly(osb)) {
2431 if (ex)
2432 status = -EROFS;
2433 goto getbh;
2434 }
2435
2436 if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
2437 ocfs2_mount_local(osb))
2438 goto update;
2439
2440 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2441 ocfs2_wait_for_recovery(osb);
2442
2443 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2444 level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2445 dlm_flags = 0;
2446 if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
2447 dlm_flags |= DLM_LKF_NOQUEUE;
2448
2449 status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
2450 arg_flags, subclass, _RET_IP_);
2451 if (status < 0) {
2452 if (status != -EAGAIN)
2453 mlog_errno(status);
2454 goto bail;
2455 }
2456
2457
2458 acquired = 1;
2459
2460
2461
2462
2463
2464 if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
2465 ocfs2_wait_for_recovery(osb);
2466
2467update:
2468
2469
2470
2471
2472
2473
2474 if (inode->i_state & I_NEW) {
2475 status = 0;
2476 if (lockres)
2477 ocfs2_complete_lock_res_refresh(lockres, 0);
2478 goto bail;
2479 }
2480
2481
2482
2483
2484
2485
2486 status = ocfs2_inode_lock_update(inode, &local_bh);
2487 if (status < 0) {
2488 if (status != -ENOENT)
2489 mlog_errno(status);
2490 goto bail;
2491 }
2492getbh:
2493 if (ret_bh) {
2494 status = ocfs2_assign_bh(inode, ret_bh, local_bh);
2495 if (status < 0) {
2496 mlog_errno(status);
2497 goto bail;
2498 }
2499 }
2500
2501bail:
2502 if (status < 0) {
2503 if (ret_bh && (*ret_bh)) {
2504 brelse(*ret_bh);
2505 *ret_bh = NULL;
2506 }
2507 if (acquired)
2508 ocfs2_inode_unlock(inode, ex);
2509 }
2510
2511 brelse(local_bh);
2512 return status;
2513}
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531int ocfs2_inode_lock_with_page(struct inode *inode,
2532 struct buffer_head **ret_bh,
2533 int ex,
2534 struct page *page)
2535{
2536 int ret;
2537
2538 ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
2539 if (ret == -EAGAIN) {
2540 unlock_page(page);
2541
2542
2543
2544
2545
2546
2547
2548 if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
2549 ocfs2_inode_unlock(inode, ex);
2550 ret = AOP_TRUNCATED_PAGE;
2551 }
2552
2553 return ret;
2554}
2555
2556int ocfs2_inode_lock_atime(struct inode *inode,
2557 struct vfsmount *vfsmnt,
2558 int *level, int wait)
2559{
2560 int ret;
2561
2562 if (wait)
2563 ret = ocfs2_inode_lock(inode, NULL, 0);
2564 else
2565 ret = ocfs2_try_inode_lock(inode, NULL, 0);
2566
2567 if (ret < 0) {
2568 if (ret != -EAGAIN)
2569 mlog_errno(ret);
2570 return ret;
2571 }
2572
2573
2574
2575
2576
2577 if (ocfs2_should_update_atime(inode, vfsmnt)) {
2578 struct buffer_head *bh = NULL;
2579
2580 ocfs2_inode_unlock(inode, 0);
2581 if (wait)
2582 ret = ocfs2_inode_lock(inode, &bh, 1);
2583 else
2584 ret = ocfs2_try_inode_lock(inode, &bh, 1);
2585
2586 if (ret < 0) {
2587 if (ret != -EAGAIN)
2588 mlog_errno(ret);
2589 return ret;
2590 }
2591 *level = 1;
2592 if (ocfs2_should_update_atime(inode, vfsmnt))
2593 ocfs2_update_inode_atime(inode, bh);
2594 brelse(bh);
2595 } else
2596 *level = 0;
2597
2598 return ret;
2599}
2600
2601void ocfs2_inode_unlock(struct inode *inode,
2602 int ex)
2603{
2604 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2605 struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
2606 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2607
2608 mlog(0, "inode %llu drop %s META lock\n",
2609 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2610 ex ? "EXMODE" : "PRMODE");
2611
2612 if (!ocfs2_is_hard_readonly(osb) &&
2613 !ocfs2_mount_local(osb))
2614 ocfs2_cluster_unlock(osb, lockres, level);
2615}
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658int ocfs2_inode_lock_tracker(struct inode *inode,
2659 struct buffer_head **ret_bh,
2660 int ex,
2661 struct ocfs2_lock_holder *oh)
2662{
2663 int status = 0;
2664 struct ocfs2_lock_res *lockres;
2665 struct ocfs2_lock_holder *tmp_oh;
2666 struct pid *pid = task_pid(current);
2667
2668
2669 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2670 tmp_oh = ocfs2_pid_holder(lockres, pid);
2671
2672 if (!tmp_oh) {
2673
2674
2675
2676
2677 status = ocfs2_inode_lock_full(inode, ret_bh, ex, 0);
2678 if (status < 0) {
2679 if (status != -ENOENT)
2680 mlog_errno(status);
2681 return status;
2682 }
2683
2684 oh->oh_ex = ex;
2685 ocfs2_add_holder(lockres, oh);
2686 return 0;
2687 }
2688
2689 if (unlikely(ex && !tmp_oh->oh_ex)) {
2690
2691
2692
2693 mlog(ML_ERROR, "Recursive locking is not permitted to "
2694 "upgrade to EX level from PR level.\n");
2695 dump_stack();
2696 return -EINVAL;
2697 }
2698
2699
2700
2701
2702
2703 if (ret_bh) {
2704 status = ocfs2_inode_lock_full(inode, ret_bh, ex,
2705 OCFS2_META_LOCK_GETBH);
2706 if (status < 0) {
2707 if (status != -ENOENT)
2708 mlog_errno(status);
2709 return status;
2710 }
2711 }
2712 return tmp_oh ? 1 : 0;
2713}
2714
2715void ocfs2_inode_unlock_tracker(struct inode *inode,
2716 int ex,
2717 struct ocfs2_lock_holder *oh,
2718 int had_lock)
2719{
2720 struct ocfs2_lock_res *lockres;
2721
2722 lockres = &OCFS2_I(inode)->ip_inode_lockres;
2723
2724
2725
2726
2727
2728 if (!had_lock) {
2729 ocfs2_inode_unlock(inode, oh->oh_ex);
2730 ocfs2_remove_holder(lockres, oh);
2731 }
2732}
2733
2734int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
2735{
2736 struct ocfs2_lock_res *lockres;
2737 struct ocfs2_orphan_scan_lvb *lvb;
2738 int status = 0;
2739
2740 if (ocfs2_is_hard_readonly(osb))
2741 return -EROFS;
2742
2743 if (ocfs2_mount_local(osb))
2744 return 0;
2745
2746 lockres = &osb->osb_orphan_scan.os_lockres;
2747 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2748 if (status < 0)
2749 return status;
2750
2751 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2752 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2753 lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
2754 *seqno = be32_to_cpu(lvb->lvb_os_seqno);
2755 else
2756 *seqno = osb->osb_orphan_scan.os_seqno + 1;
2757
2758 return status;
2759}
2760
2761void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
2762{
2763 struct ocfs2_lock_res *lockres;
2764 struct ocfs2_orphan_scan_lvb *lvb;
2765
2766 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
2767 lockres = &osb->osb_orphan_scan.os_lockres;
2768 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2769 lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
2770 lvb->lvb_os_seqno = cpu_to_be32(seqno);
2771 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2772 }
2773}
2774
2775int ocfs2_super_lock(struct ocfs2_super *osb,
2776 int ex)
2777{
2778 int status = 0;
2779 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2780 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2781
2782 if (ocfs2_is_hard_readonly(osb))
2783 return -EROFS;
2784
2785 if (ocfs2_mount_local(osb))
2786 goto bail;
2787
2788 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
2789 if (status < 0) {
2790 mlog_errno(status);
2791 goto bail;
2792 }
2793
2794
2795
2796
2797
2798 status = ocfs2_should_refresh_lock_res(lockres);
2799 if (status) {
2800 status = ocfs2_refresh_slot_info(osb);
2801
2802 ocfs2_complete_lock_res_refresh(lockres, status);
2803
2804 if (status < 0) {
2805 ocfs2_cluster_unlock(osb, lockres, level);
2806 mlog_errno(status);
2807 }
2808 ocfs2_track_lock_refresh(lockres);
2809 }
2810bail:
2811 return status;
2812}
2813
2814void ocfs2_super_unlock(struct ocfs2_super *osb,
2815 int ex)
2816{
2817 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2818 struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
2819
2820 if (!ocfs2_mount_local(osb))
2821 ocfs2_cluster_unlock(osb, lockres, level);
2822}
2823
2824int ocfs2_rename_lock(struct ocfs2_super *osb)
2825{
2826 int status;
2827 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2828
2829 if (ocfs2_is_hard_readonly(osb))
2830 return -EROFS;
2831
2832 if (ocfs2_mount_local(osb))
2833 return 0;
2834
2835 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
2836 if (status < 0)
2837 mlog_errno(status);
2838
2839 return status;
2840}
2841
2842void ocfs2_rename_unlock(struct ocfs2_super *osb)
2843{
2844 struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
2845
2846 if (!ocfs2_mount_local(osb))
2847 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2848}
2849
2850int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
2851{
2852 int status;
2853 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2854
2855 if (ocfs2_is_hard_readonly(osb))
2856 return -EROFS;
2857
2858 if (ocfs2_mount_local(osb))
2859 return 0;
2860
2861 status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
2862 0, 0);
2863 if (status < 0)
2864 mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
2865
2866 return status;
2867}
2868
2869void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
2870{
2871 struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
2872
2873 if (!ocfs2_mount_local(osb))
2874 ocfs2_cluster_unlock(osb, lockres,
2875 ex ? LKM_EXMODE : LKM_PRMODE);
2876}
2877
2878int ocfs2_trim_fs_lock(struct ocfs2_super *osb,
2879 struct ocfs2_trim_fs_info *info, int trylock)
2880{
2881 int status;
2882 struct ocfs2_trim_fs_lvb *lvb;
2883 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2884
2885 if (info)
2886 info->tf_valid = 0;
2887
2888 if (ocfs2_is_hard_readonly(osb))
2889 return -EROFS;
2890
2891 if (ocfs2_mount_local(osb))
2892 return 0;
2893
2894 status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX,
2895 trylock ? DLM_LKF_NOQUEUE : 0, 0);
2896 if (status < 0) {
2897 if (status != -EAGAIN)
2898 mlog_errno(status);
2899 return status;
2900 }
2901
2902 if (info) {
2903 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2904 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
2905 lvb->lvb_version == OCFS2_TRIMFS_LVB_VERSION) {
2906 info->tf_valid = 1;
2907 info->tf_success = lvb->lvb_success;
2908 info->tf_nodenum = be32_to_cpu(lvb->lvb_nodenum);
2909 info->tf_start = be64_to_cpu(lvb->lvb_start);
2910 info->tf_len = be64_to_cpu(lvb->lvb_len);
2911 info->tf_minlen = be64_to_cpu(lvb->lvb_minlen);
2912 info->tf_trimlen = be64_to_cpu(lvb->lvb_trimlen);
2913 }
2914 }
2915
2916 return status;
2917}
2918
2919void ocfs2_trim_fs_unlock(struct ocfs2_super *osb,
2920 struct ocfs2_trim_fs_info *info)
2921{
2922 struct ocfs2_trim_fs_lvb *lvb;
2923 struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
2924
2925 if (ocfs2_mount_local(osb))
2926 return;
2927
2928 if (info) {
2929 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
2930 lvb->lvb_version = OCFS2_TRIMFS_LVB_VERSION;
2931 lvb->lvb_success = info->tf_success;
2932 lvb->lvb_nodenum = cpu_to_be32(info->tf_nodenum);
2933 lvb->lvb_start = cpu_to_be64(info->tf_start);
2934 lvb->lvb_len = cpu_to_be64(info->tf_len);
2935 lvb->lvb_minlen = cpu_to_be64(info->tf_minlen);
2936 lvb->lvb_trimlen = cpu_to_be64(info->tf_trimlen);
2937 }
2938
2939 ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
2940}
2941
2942int ocfs2_dentry_lock(struct dentry *dentry, int ex)
2943{
2944 int ret;
2945 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2946 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2947 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2948
2949 BUG_ON(!dl);
2950
2951 if (ocfs2_is_hard_readonly(osb)) {
2952 if (ex)
2953 return -EROFS;
2954 return 0;
2955 }
2956
2957 if (ocfs2_mount_local(osb))
2958 return 0;
2959
2960 ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
2961 if (ret < 0)
2962 mlog_errno(ret);
2963
2964 return ret;
2965}
2966
2967void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
2968{
2969 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
2970 struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
2971 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
2972
2973 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
2974 ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
2975}
2976
2977
2978
2979
2980static void ocfs2_dlm_debug_free(struct kref *kref)
2981{
2982 struct ocfs2_dlm_debug *dlm_debug;
2983
2984 dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
2985
2986 kfree(dlm_debug);
2987}
2988
2989void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
2990{
2991 if (dlm_debug)
2992 kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
2993}
2994
2995static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
2996{
2997 kref_get(&debug->d_refcnt);
2998}
2999
3000struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
3001{
3002 struct ocfs2_dlm_debug *dlm_debug;
3003
3004 dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
3005 if (!dlm_debug) {
3006 mlog_errno(-ENOMEM);
3007 goto out;
3008 }
3009
3010 kref_init(&dlm_debug->d_refcnt);
3011 INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
3012 dlm_debug->d_filter_secs = 0;
3013out:
3014 return dlm_debug;
3015}
3016
3017
3018struct ocfs2_dlm_seq_priv {
3019 struct ocfs2_dlm_debug *p_dlm_debug;
3020 struct ocfs2_lock_res p_iter_res;
3021 struct ocfs2_lock_res p_tmp_res;
3022};
3023
3024static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
3025 struct ocfs2_dlm_seq_priv *priv)
3026{
3027 struct ocfs2_lock_res *iter, *ret = NULL;
3028 struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
3029
3030 assert_spin_locked(&ocfs2_dlm_tracking_lock);
3031
3032 list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
3033
3034 if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
3035 mlog(0, "End of list found, %p\n", ret);
3036 break;
3037 }
3038
3039
3040
3041 if (iter->l_ops != NULL) {
3042 ret = iter;
3043 break;
3044 }
3045 }
3046
3047 return ret;
3048}
3049
3050static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
3051{
3052 struct ocfs2_dlm_seq_priv *priv = m->private;
3053 struct ocfs2_lock_res *iter;
3054
3055 spin_lock(&ocfs2_dlm_tracking_lock);
3056 iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
3057 if (iter) {
3058
3059
3060
3061
3062
3063
3064
3065 priv->p_tmp_res = *iter;
3066 iter = &priv->p_tmp_res;
3067 }
3068 spin_unlock(&ocfs2_dlm_tracking_lock);
3069
3070 return iter;
3071}
3072
3073static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
3074{
3075}
3076
3077static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
3078{
3079 struct ocfs2_dlm_seq_priv *priv = m->private;
3080 struct ocfs2_lock_res *iter = v;
3081 struct ocfs2_lock_res *dummy = &priv->p_iter_res;
3082
3083 spin_lock(&ocfs2_dlm_tracking_lock);
3084 iter = ocfs2_dlm_next_res(iter, priv);
3085 list_del_init(&dummy->l_debug_list);
3086 if (iter) {
3087 list_add(&dummy->l_debug_list, &iter->l_debug_list);
3088 priv->p_tmp_res = *iter;
3089 iter = &priv->p_tmp_res;
3090 }
3091 spin_unlock(&ocfs2_dlm_tracking_lock);
3092
3093 return iter;
3094}
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106#define OCFS2_DLM_DEBUG_STR_VERSION 4
3107static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
3108{
3109 int i;
3110 char *lvb;
3111 struct ocfs2_lock_res *lockres = v;
3112#ifdef CONFIG_OCFS2_FS_STATS
3113 u64 now, last;
3114 struct ocfs2_dlm_debug *dlm_debug =
3115 ((struct ocfs2_dlm_seq_priv *)m->private)->p_dlm_debug;
3116#endif
3117
3118 if (!lockres)
3119 return -EINVAL;
3120
3121#ifdef CONFIG_OCFS2_FS_STATS
3122 if (!lockres->l_lock_wait && dlm_debug->d_filter_secs) {
3123 now = ktime_to_us(ktime_get_real());
3124 if (lockres->l_lock_prmode.ls_last >
3125 lockres->l_lock_exmode.ls_last)
3126 last = lockres->l_lock_prmode.ls_last;
3127 else
3128 last = lockres->l_lock_exmode.ls_last;
3129
3130
3131
3132
3133
3134
3135 if (div_u64(now - last, 1000000) > dlm_debug->d_filter_secs)
3136 return 0;
3137 }
3138#endif
3139
3140 seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
3141
3142 if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
3143 seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
3144 lockres->l_name,
3145 (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
3146 else
3147 seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
3148
3149 seq_printf(m, "%d\t"
3150 "0x%lx\t"
3151 "0x%x\t"
3152 "0x%x\t"
3153 "%u\t"
3154 "%u\t"
3155 "%d\t"
3156 "%d\t",
3157 lockres->l_level,
3158 lockres->l_flags,
3159 lockres->l_action,
3160 lockres->l_unlock_action,
3161 lockres->l_ro_holders,
3162 lockres->l_ex_holders,
3163 lockres->l_requested,
3164 lockres->l_blocking);
3165
3166
3167 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
3168 for(i = 0; i < DLM_LVB_LEN; i++)
3169 seq_printf(m, "0x%x\t", lvb[i]);
3170
3171#ifdef CONFIG_OCFS2_FS_STATS
3172# define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
3173# define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
3174# define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
3175# define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
3176# define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
3177# define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
3178# define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
3179# define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
3180# define lock_refresh(_l) ((_l)->l_lock_refresh)
3181# define lock_last_prmode(_l) ((_l)->l_lock_prmode.ls_last)
3182# define lock_last_exmode(_l) ((_l)->l_lock_exmode.ls_last)
3183# define lock_wait(_l) ((_l)->l_lock_wait)
3184#else
3185# define lock_num_prmode(_l) (0)
3186# define lock_num_exmode(_l) (0)
3187# define lock_num_prmode_failed(_l) (0)
3188# define lock_num_exmode_failed(_l) (0)
3189# define lock_total_prmode(_l) (0ULL)
3190# define lock_total_exmode(_l) (0ULL)
3191# define lock_max_prmode(_l) (0)
3192# define lock_max_exmode(_l) (0)
3193# define lock_refresh(_l) (0)
3194# define lock_last_prmode(_l) (0ULL)
3195# define lock_last_exmode(_l) (0ULL)
3196# define lock_wait(_l) (0ULL)
3197#endif
3198
3199 seq_printf(m, "%u\t"
3200 "%u\t"
3201 "%u\t"
3202 "%u\t"
3203 "%llu\t"
3204 "%llu\t"
3205 "%u\t"
3206 "%u\t"
3207 "%u\t"
3208 "%llu\t"
3209 "%llu\t"
3210 "%llu\t",
3211 lock_num_prmode(lockres),
3212 lock_num_exmode(lockres),
3213 lock_num_prmode_failed(lockres),
3214 lock_num_exmode_failed(lockres),
3215 lock_total_prmode(lockres),
3216 lock_total_exmode(lockres),
3217 lock_max_prmode(lockres),
3218 lock_max_exmode(lockres),
3219 lock_refresh(lockres),
3220 lock_last_prmode(lockres),
3221 lock_last_exmode(lockres),
3222 lock_wait(lockres));
3223
3224
3225 seq_printf(m, "\n");
3226 return 0;
3227}
3228
3229static const struct seq_operations ocfs2_dlm_seq_ops = {
3230 .start = ocfs2_dlm_seq_start,
3231 .stop = ocfs2_dlm_seq_stop,
3232 .next = ocfs2_dlm_seq_next,
3233 .show = ocfs2_dlm_seq_show,
3234};
3235
3236static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
3237{
3238 struct seq_file *seq = file->private_data;
3239 struct ocfs2_dlm_seq_priv *priv = seq->private;
3240 struct ocfs2_lock_res *res = &priv->p_iter_res;
3241
3242 ocfs2_remove_lockres_tracking(res);
3243 ocfs2_put_dlm_debug(priv->p_dlm_debug);
3244 return seq_release_private(inode, file);
3245}
3246
3247static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
3248{
3249 struct ocfs2_dlm_seq_priv *priv;
3250 struct ocfs2_super *osb;
3251
3252 priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
3253 if (!priv) {
3254 mlog_errno(-ENOMEM);
3255 return -ENOMEM;
3256 }
3257
3258 osb = inode->i_private;
3259 ocfs2_get_dlm_debug(osb->osb_dlm_debug);
3260 priv->p_dlm_debug = osb->osb_dlm_debug;
3261 INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
3262
3263 ocfs2_add_lockres_tracking(&priv->p_iter_res,
3264 priv->p_dlm_debug);
3265
3266 return 0;
3267}
3268
3269static const struct file_operations ocfs2_dlm_debug_fops = {
3270 .open = ocfs2_dlm_debug_open,
3271 .release = ocfs2_dlm_debug_release,
3272 .read = seq_read,
3273 .llseek = seq_lseek,
3274};
3275
3276static void ocfs2_dlm_init_debug(struct ocfs2_super *osb)
3277{
3278 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3279
3280 debugfs_create_file("locking_state", S_IFREG|S_IRUSR,
3281 osb->osb_debug_root, osb, &ocfs2_dlm_debug_fops);
3282
3283 debugfs_create_u32("locking_filter", 0600, osb->osb_debug_root,
3284 &dlm_debug->d_filter_secs);
3285}
3286
3287static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
3288{
3289 struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
3290
3291 if (dlm_debug)
3292 ocfs2_put_dlm_debug(dlm_debug);
3293}
3294
3295int ocfs2_dlm_init(struct ocfs2_super *osb)
3296{
3297 int status = 0;
3298 struct ocfs2_cluster_connection *conn = NULL;
3299
3300 if (ocfs2_mount_local(osb)) {
3301 osb->node_num = 0;
3302 goto local;
3303 }
3304
3305 ocfs2_dlm_init_debug(osb);
3306
3307
3308 osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
3309 osb->uuid_str);
3310 if (IS_ERR(osb->dc_task)) {
3311 status = PTR_ERR(osb->dc_task);
3312 osb->dc_task = NULL;
3313 mlog_errno(status);
3314 goto bail;
3315 }
3316
3317
3318 status = ocfs2_cluster_connect(osb->osb_cluster_stack,
3319 osb->osb_cluster_name,
3320 strlen(osb->osb_cluster_name),
3321 osb->uuid_str,
3322 strlen(osb->uuid_str),
3323 &lproto, ocfs2_do_node_down, osb,
3324 &conn);
3325 if (status) {
3326 mlog_errno(status);
3327 goto bail;
3328 }
3329
3330 status = ocfs2_cluster_this_node(conn, &osb->node_num);
3331 if (status < 0) {
3332 mlog_errno(status);
3333 mlog(ML_ERROR,
3334 "could not find this host's node number\n");
3335 ocfs2_cluster_disconnect(conn, 0);
3336 goto bail;
3337 }
3338
3339local:
3340 ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
3341 ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
3342 ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
3343 ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
3344
3345 osb->cconn = conn;
3346bail:
3347 if (status < 0) {
3348 ocfs2_dlm_shutdown_debug(osb);
3349 if (osb->dc_task)
3350 kthread_stop(osb->dc_task);
3351 }
3352
3353 return status;
3354}
3355
3356void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
3357 int hangup_pending)
3358{
3359 ocfs2_drop_osb_locks(osb);
3360
3361
3362
3363
3364
3365
3366
3367 if (osb->dc_task) {
3368 kthread_stop(osb->dc_task);
3369 osb->dc_task = NULL;
3370 }
3371
3372 ocfs2_lock_res_free(&osb->osb_super_lockres);
3373 ocfs2_lock_res_free(&osb->osb_rename_lockres);
3374 ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
3375 ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
3376
3377 ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
3378 osb->cconn = NULL;
3379
3380 ocfs2_dlm_shutdown_debug(osb);
3381}
3382
3383static int ocfs2_drop_lock(struct ocfs2_super *osb,
3384 struct ocfs2_lock_res *lockres)
3385{
3386 int ret;
3387 unsigned long flags;
3388 u32 lkm_flags = 0;
3389
3390
3391 if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
3392 goto out;
3393
3394 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3395 lkm_flags |= DLM_LKF_VALBLK;
3396
3397 spin_lock_irqsave(&lockres->l_lock, flags);
3398
3399 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
3400 "lockres %s, flags 0x%lx\n",
3401 lockres->l_name, lockres->l_flags);
3402
3403 while (lockres->l_flags & OCFS2_LOCK_BUSY) {
3404 mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
3405 "%u, unlock_action = %u\n",
3406 lockres->l_name, lockres->l_flags, lockres->l_action,
3407 lockres->l_unlock_action);
3408
3409 spin_unlock_irqrestore(&lockres->l_lock, flags);
3410
3411
3412
3413
3414 ocfs2_wait_on_busy_lock(lockres);
3415
3416 spin_lock_irqsave(&lockres->l_lock, flags);
3417 }
3418
3419 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3420 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
3421 lockres->l_level == DLM_LOCK_EX &&
3422 !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3423 lockres->l_ops->set_lvb(lockres);
3424 }
3425
3426 if (lockres->l_flags & OCFS2_LOCK_BUSY)
3427 mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
3428 lockres->l_name);
3429 if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
3430 mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
3431
3432 if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
3433 spin_unlock_irqrestore(&lockres->l_lock, flags);
3434 goto out;
3435 }
3436
3437 lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
3438
3439
3440
3441 BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
3442
3443
3444 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3445 lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
3446 spin_unlock_irqrestore(&lockres->l_lock, flags);
3447
3448 mlog(0, "lock %s\n", lockres->l_name);
3449
3450 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
3451 if (ret) {
3452 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3453 mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
3454 ocfs2_dlm_dump_lksb(&lockres->l_lksb);
3455 BUG();
3456 }
3457 mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
3458 lockres->l_name);
3459
3460 ocfs2_wait_on_busy_lock(lockres);
3461out:
3462 return 0;
3463}
3464
3465static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
3466 struct ocfs2_lock_res *lockres);
3467
3468
3469
3470
3471
3472
3473
3474void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
3475 struct ocfs2_lock_res *lockres)
3476{
3477 int status;
3478 struct ocfs2_mask_waiter mw;
3479 unsigned long flags, flags2;
3480
3481 ocfs2_init_mask_waiter(&mw);
3482
3483 spin_lock_irqsave(&lockres->l_lock, flags);
3484 lockres->l_flags |= OCFS2_LOCK_FREEING;
3485 if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503 spin_unlock_irqrestore(&lockres->l_lock, flags);
3504 spin_lock_irqsave(&osb->dc_task_lock, flags2);
3505 list_del_init(&lockres->l_blocked_list);
3506 osb->blocked_lock_count--;
3507 spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
3508
3509
3510
3511
3512
3513
3514 WARN_ON_ONCE(lockres->l_ops->post_unlock);
3515
3516 ocfs2_process_blocked_lock(osb, lockres);
3517 return;
3518 }
3519 while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
3520 lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
3521 spin_unlock_irqrestore(&lockres->l_lock, flags);
3522
3523 mlog(0, "Waiting on lockres %s\n", lockres->l_name);
3524
3525 status = ocfs2_wait_for_mask(&mw);
3526 if (status)
3527 mlog_errno(status);
3528
3529 spin_lock_irqsave(&lockres->l_lock, flags);
3530 }
3531 spin_unlock_irqrestore(&lockres->l_lock, flags);
3532}
3533
3534void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
3535 struct ocfs2_lock_res *lockres)
3536{
3537 int ret;
3538
3539 ocfs2_mark_lockres_freeing(osb, lockres);
3540 ret = ocfs2_drop_lock(osb, lockres);
3541 if (ret)
3542 mlog_errno(ret);
3543}
3544
3545static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
3546{
3547 ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
3548 ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
3549 ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
3550 ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
3551}
3552
3553int ocfs2_drop_inode_locks(struct inode *inode)
3554{
3555 int status, err;
3556
3557
3558
3559
3560 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3561 &OCFS2_I(inode)->ip_open_lockres);
3562 if (err < 0)
3563 mlog_errno(err);
3564
3565 status = err;
3566
3567 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3568 &OCFS2_I(inode)->ip_inode_lockres);
3569 if (err < 0)
3570 mlog_errno(err);
3571 if (err < 0 && !status)
3572 status = err;
3573
3574 err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
3575 &OCFS2_I(inode)->ip_rw_lockres);
3576 if (err < 0)
3577 mlog_errno(err);
3578 if (err < 0 && !status)
3579 status = err;
3580
3581 return status;
3582}
3583
3584static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
3585 int new_level)
3586{
3587 assert_spin_locked(&lockres->l_lock);
3588
3589 BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
3590
3591 if (lockres->l_level <= new_level) {
3592 mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
3593 "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
3594 "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
3595 new_level, list_empty(&lockres->l_blocked_list),
3596 list_empty(&lockres->l_mask_waiters), lockres->l_type,
3597 lockres->l_flags, lockres->l_ro_holders,
3598 lockres->l_ex_holders, lockres->l_action,
3599 lockres->l_unlock_action, lockres->l_requested,
3600 lockres->l_blocking, lockres->l_pending_gen);
3601 BUG();
3602 }
3603
3604 mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
3605 lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
3606
3607 lockres->l_action = OCFS2_AST_DOWNCONVERT;
3608 lockres->l_requested = new_level;
3609 lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
3610 return lockres_set_pending(lockres);
3611}
3612
3613static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
3614 struct ocfs2_lock_res *lockres,
3615 int new_level,
3616 int lvb,
3617 unsigned int generation)
3618{
3619 int ret;
3620 u32 dlm_flags = DLM_LKF_CONVERT;
3621
3622 mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
3623 lockres->l_level, new_level);
3624
3625
3626
3627
3628
3629
3630
3631 if (ocfs2_userspace_stack(osb) &&
3632 lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
3633 lvb = 1;
3634
3635 if (lvb)
3636 dlm_flags |= DLM_LKF_VALBLK;
3637
3638 ret = ocfs2_dlm_lock(osb->cconn,
3639 new_level,
3640 &lockres->l_lksb,
3641 dlm_flags,
3642 lockres->l_name,
3643 OCFS2_LOCK_ID_MAX_LEN - 1);
3644 lockres_clear_pending(lockres, generation, osb);
3645 if (ret) {
3646 ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
3647 ocfs2_recover_from_dlm_error(lockres, 1);
3648 goto bail;
3649 }
3650
3651 ret = 0;
3652bail:
3653 return ret;
3654}
3655
3656
3657static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
3658 struct ocfs2_lock_res *lockres)
3659{
3660 assert_spin_locked(&lockres->l_lock);
3661
3662 if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
3663
3664
3665
3666 mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
3667 return 0;
3668 }
3669
3670
3671 BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
3672 lockres->l_action != OCFS2_AST_DOWNCONVERT);
3673
3674
3675 lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
3676
3677 mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
3678 "lock %s, invalid flags: 0x%lx\n",
3679 lockres->l_name, lockres->l_flags);
3680
3681 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3682
3683 return 1;
3684}
3685
3686static int ocfs2_cancel_convert(struct ocfs2_super *osb,
3687 struct ocfs2_lock_res *lockres)
3688{
3689 int ret;
3690
3691 ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
3692 DLM_LKF_CANCEL);
3693 if (ret) {
3694 ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
3695 ocfs2_recover_from_dlm_error(lockres, 0);
3696 }
3697
3698 mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
3699
3700 return ret;
3701}
3702
3703static int ocfs2_unblock_lock(struct ocfs2_super *osb,
3704 struct ocfs2_lock_res *lockres,
3705 struct ocfs2_unblock_ctl *ctl)
3706{
3707 unsigned long flags;
3708 int blocking;
3709 int new_level;
3710 int level;
3711 int ret = 0;
3712 int set_lvb = 0;
3713 unsigned int gen;
3714
3715 spin_lock_irqsave(&lockres->l_lock, flags);
3716
3717recheck:
3718
3719
3720
3721 if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
3722 BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
3723 spin_unlock_irqrestore(&lockres->l_lock, flags);
3724 ret = 0;
3725 goto leave;
3726 }
3727
3728 if (lockres->l_flags & OCFS2_LOCK_BUSY) {
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752 if (lockres->l_flags & OCFS2_LOCK_PENDING) {
3753 mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
3754 lockres->l_name);
3755 goto leave_requeue;
3756 }
3757
3758 ctl->requeue = 1;
3759 ret = ocfs2_prepare_cancel_convert(osb, lockres);
3760 spin_unlock_irqrestore(&lockres->l_lock, flags);
3761 if (ret) {
3762 ret = ocfs2_cancel_convert(osb, lockres);
3763 if (ret < 0)
3764 mlog_errno(ret);
3765 }
3766 goto leave;
3767 }
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778 if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
3779 goto leave_requeue;
3780
3781
3782
3783
3784
3785
3786 if (lockres->l_level == DLM_LOCK_NL) {
3787 BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
3788 mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
3789 lockres->l_blocking = DLM_LOCK_NL;
3790 lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
3791 spin_unlock_irqrestore(&lockres->l_lock, flags);
3792 goto leave;
3793 }
3794
3795
3796
3797 if ((lockres->l_blocking == DLM_LOCK_EX)
3798 && (lockres->l_ex_holders || lockres->l_ro_holders)) {
3799 mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
3800 lockres->l_name, lockres->l_ex_holders,
3801 lockres->l_ro_holders);
3802 goto leave_requeue;
3803 }
3804
3805
3806
3807 if (lockres->l_blocking == DLM_LOCK_PR &&
3808 lockres->l_ex_holders) {
3809 mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
3810 lockres->l_name, lockres->l_ex_holders);
3811 goto leave_requeue;
3812 }
3813
3814
3815
3816
3817
3818 if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
3819 && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
3820 mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
3821 lockres->l_name);
3822 goto leave_requeue;
3823 }
3824
3825 new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
3826
3827 if (lockres->l_ops->check_downconvert
3828 && !lockres->l_ops->check_downconvert(lockres, new_level)) {
3829 mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
3830 lockres->l_name);
3831 goto leave_requeue;
3832 }
3833
3834
3835
3836
3837 if (!lockres->l_ops->downconvert_worker)
3838 goto downconvert;
3839
3840
3841
3842
3843
3844 blocking = lockres->l_blocking;
3845 level = lockres->l_level;
3846 spin_unlock_irqrestore(&lockres->l_lock, flags);
3847
3848 ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
3849
3850 if (ctl->unblock_action == UNBLOCK_STOP_POST) {
3851 mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
3852 lockres->l_name);
3853 goto leave;
3854 }
3855
3856 spin_lock_irqsave(&lockres->l_lock, flags);
3857 if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
3858
3859
3860 mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
3861 "Recheck\n", lockres->l_name, blocking,
3862 lockres->l_blocking, level, lockres->l_level);
3863 goto recheck;
3864 }
3865
3866downconvert:
3867 ctl->requeue = 0;
3868
3869 if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
3870 if (lockres->l_level == DLM_LOCK_EX)
3871 set_lvb = 1;
3872
3873
3874
3875
3876
3877
3878
3879 if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
3880 lockres->l_ops->set_lvb(lockres);
3881 }
3882
3883 gen = ocfs2_prepare_downconvert(lockres, new_level);
3884 spin_unlock_irqrestore(&lockres->l_lock, flags);
3885 ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
3886 gen);
3887
3888leave:
3889 if (ret)
3890 mlog_errno(ret);
3891 return ret;
3892
3893leave_requeue:
3894 spin_unlock_irqrestore(&lockres->l_lock, flags);
3895 ctl->requeue = 1;
3896
3897 return 0;
3898}
3899
3900static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
3901 int blocking)
3902{
3903 struct inode *inode;
3904 struct address_space *mapping;
3905 struct ocfs2_inode_info *oi;
3906
3907 inode = ocfs2_lock_res_inode(lockres);
3908 mapping = inode->i_mapping;
3909
3910 if (S_ISDIR(inode->i_mode)) {
3911 oi = OCFS2_I(inode);
3912 oi->ip_dir_lock_gen++;
3913 mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
3914 goto out;
3915 }
3916
3917 if (!S_ISREG(inode->i_mode))
3918 goto out;
3919
3920
3921
3922
3923
3924
3925
3926
3927 unmap_mapping_range(mapping, 0, 0, 0);
3928
3929 if (filemap_fdatawrite(mapping)) {
3930 mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
3931 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3932 }
3933 sync_mapping_buffers(mapping);
3934 if (blocking == DLM_LOCK_EX) {
3935 truncate_inode_pages(mapping, 0);
3936 } else {
3937
3938
3939
3940
3941
3942 filemap_fdatawait(mapping);
3943 }
3944
3945 forget_all_cached_acls(inode);
3946
3947out:
3948 return UNBLOCK_CONTINUE;
3949}
3950
3951static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
3952 struct ocfs2_lock_res *lockres,
3953 int new_level)
3954{
3955 int checkpointed = ocfs2_ci_fully_checkpointed(ci);
3956
3957 BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
3958 BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
3959
3960 if (checkpointed)
3961 return 1;
3962
3963 ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
3964 return 0;
3965}
3966
3967static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
3968 int new_level)
3969{
3970 struct inode *inode = ocfs2_lock_res_inode(lockres);
3971
3972 return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
3973}
3974
3975static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
3976{
3977 struct inode *inode = ocfs2_lock_res_inode(lockres);
3978
3979 __ocfs2_stuff_meta_lvb(inode);
3980}
3981
3982
3983
3984
3985
3986
3987static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
3988 struct ocfs2_lock_res *lockres)
3989{
3990 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
3991 ocfs2_dentry_lock_put(osb, dl);
3992}
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
4014 int blocking)
4015{
4016 struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
4017 struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
4018 struct dentry *dentry;
4019 unsigned long flags;
4020 int extra_ref = 0;
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030 if (blocking == DLM_LOCK_PR)
4031 return UNBLOCK_CONTINUE;
4032
4033
4034
4035
4036
4037
4038 spin_lock(&oi->ip_lock);
4039 oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
4040 spin_unlock(&oi->ip_lock);
4041
4042
4043
4044
4045
4046
4047
4048 spin_lock_irqsave(&lockres->l_lock, flags);
4049 spin_lock(&dentry_attach_lock);
4050 if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
4051 && dl->dl_count) {
4052 dl->dl_count++;
4053 extra_ref = 1;
4054 }
4055 spin_unlock(&dentry_attach_lock);
4056 spin_unlock_irqrestore(&lockres->l_lock, flags);
4057
4058 mlog(0, "extra_ref = %d\n", extra_ref);
4059
4060
4061
4062
4063
4064
4065 if (!extra_ref)
4066 return UNBLOCK_CONTINUE;
4067
4068 spin_lock(&dentry_attach_lock);
4069 while (1) {
4070 dentry = ocfs2_find_local_alias(dl->dl_inode,
4071 dl->dl_parent_blkno, 1);
4072 if (!dentry)
4073 break;
4074 spin_unlock(&dentry_attach_lock);
4075
4076 if (S_ISDIR(dl->dl_inode->i_mode))
4077 shrink_dcache_parent(dentry);
4078
4079 mlog(0, "d_delete(%pd);\n", dentry);
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089 d_delete(dentry);
4090 dput(dentry);
4091
4092 spin_lock(&dentry_attach_lock);
4093 }
4094 spin_unlock(&dentry_attach_lock);
4095
4096
4097
4098
4099
4100 if (dl->dl_count == 1)
4101 return UNBLOCK_STOP_POST;
4102
4103 return UNBLOCK_CONTINUE_POST;
4104}
4105
4106static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
4107 int new_level)
4108{
4109 struct ocfs2_refcount_tree *tree =
4110 ocfs2_lock_res_refcount_tree(lockres);
4111
4112 return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
4113}
4114
4115static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
4116 int blocking)
4117{
4118 struct ocfs2_refcount_tree *tree =
4119 ocfs2_lock_res_refcount_tree(lockres);
4120
4121 ocfs2_metadata_cache_purge(&tree->rf_ci);
4122
4123 return UNBLOCK_CONTINUE;
4124}
4125
4126static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
4127{
4128 struct ocfs2_qinfo_lvb *lvb;
4129 struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
4130 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4131 oinfo->dqi_gi.dqi_type);
4132
4133 lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
4134 lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
4135 lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
4136 lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
4137 lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
4138 lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
4139 lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
4140 lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
4141}
4142
4143void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4144{
4145 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4146 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4147 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4148
4149 if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
4150 ocfs2_cluster_unlock(osb, lockres, level);
4151}
4152
4153static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
4154{
4155 struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
4156 oinfo->dqi_gi.dqi_type);
4157 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4158 struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
4159 struct buffer_head *bh = NULL;
4160 struct ocfs2_global_disk_dqinfo *gdinfo;
4161 int status = 0;
4162
4163 if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
4164 lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
4165 info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
4166 info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
4167 oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
4168 oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
4169 oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
4170 oinfo->dqi_gi.dqi_free_entry =
4171 be32_to_cpu(lvb->lvb_free_entry);
4172 } else {
4173 status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
4174 oinfo->dqi_giblk, &bh);
4175 if (status) {
4176 mlog_errno(status);
4177 goto bail;
4178 }
4179 gdinfo = (struct ocfs2_global_disk_dqinfo *)
4180 (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
4181 info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
4182 info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
4183 oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
4184 oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
4185 oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
4186 oinfo->dqi_gi.dqi_free_entry =
4187 le32_to_cpu(gdinfo->dqi_free_entry);
4188 brelse(bh);
4189 ocfs2_track_lock_refresh(lockres);
4190 }
4191
4192bail:
4193 return status;
4194}
4195
4196
4197
4198int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
4199{
4200 struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
4201 struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
4202 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4203 int status = 0;
4204
4205
4206 if (ocfs2_is_hard_readonly(osb)) {
4207 if (ex)
4208 status = -EROFS;
4209 goto bail;
4210 }
4211 if (ocfs2_mount_local(osb))
4212 goto bail;
4213
4214 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4215 if (status < 0) {
4216 mlog_errno(status);
4217 goto bail;
4218 }
4219 if (!ocfs2_should_refresh_lock_res(lockres))
4220 goto bail;
4221
4222 status = ocfs2_refresh_qinfo(oinfo);
4223 if (status)
4224 ocfs2_qinfo_unlock(oinfo, ex);
4225 ocfs2_complete_lock_res_refresh(lockres, status);
4226bail:
4227 return status;
4228}
4229
4230int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
4231{
4232 int status;
4233 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4234 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4235 struct ocfs2_super *osb = lockres->l_priv;
4236
4237
4238 if (ocfs2_is_hard_readonly(osb))
4239 return -EROFS;
4240
4241 if (ocfs2_mount_local(osb))
4242 return 0;
4243
4244 status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
4245 if (status < 0)
4246 mlog_errno(status);
4247
4248 return status;
4249}
4250
4251void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
4252{
4253 int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
4254 struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
4255 struct ocfs2_super *osb = lockres->l_priv;
4256
4257 if (!ocfs2_mount_local(osb))
4258 ocfs2_cluster_unlock(osb, lockres, level);
4259}
4260
4261static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
4262 struct ocfs2_lock_res *lockres)
4263{
4264 int status;
4265 struct ocfs2_unblock_ctl ctl = {0, 0,};
4266 unsigned long flags;
4267
4268
4269
4270
4271
4272 BUG_ON(!lockres);
4273 BUG_ON(!lockres->l_ops);
4274
4275 mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
4276
4277
4278
4279
4280
4281
4282 spin_lock_irqsave(&lockres->l_lock, flags);
4283 if (lockres->l_flags & OCFS2_LOCK_FREEING)
4284 goto unqueue;
4285 spin_unlock_irqrestore(&lockres->l_lock, flags);
4286
4287 status = ocfs2_unblock_lock(osb, lockres, &ctl);
4288 if (status < 0)
4289 mlog_errno(status);
4290
4291 spin_lock_irqsave(&lockres->l_lock, flags);
4292unqueue:
4293 if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
4294 lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
4295 } else
4296 ocfs2_schedule_blocked_lock(osb, lockres);
4297
4298 mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
4299 ctl.requeue ? "yes" : "no");
4300 spin_unlock_irqrestore(&lockres->l_lock, flags);
4301
4302 if (ctl.unblock_action != UNBLOCK_CONTINUE
4303 && lockres->l_ops->post_unlock)
4304 lockres->l_ops->post_unlock(osb, lockres);
4305}
4306
4307static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
4308 struct ocfs2_lock_res *lockres)
4309{
4310 unsigned long flags;
4311
4312 assert_spin_locked(&lockres->l_lock);
4313
4314 if (lockres->l_flags & OCFS2_LOCK_FREEING) {
4315
4316
4317
4318 mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
4319 lockres->l_name, lockres->l_flags);
4320 return;
4321 }
4322
4323 lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
4324
4325 spin_lock_irqsave(&osb->dc_task_lock, flags);
4326 if (list_empty(&lockres->l_blocked_list)) {
4327 list_add_tail(&lockres->l_blocked_list,
4328 &osb->blocked_lock_list);
4329 osb->blocked_lock_count++;
4330 }
4331 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4332}
4333
4334static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
4335{
4336 unsigned long processed;
4337 unsigned long flags;
4338 struct ocfs2_lock_res *lockres;
4339
4340 spin_lock_irqsave(&osb->dc_task_lock, flags);
4341
4342
4343 osb->dc_work_sequence = osb->dc_wake_sequence;
4344
4345 processed = osb->blocked_lock_count;
4346
4347
4348
4349
4350
4351
4352 while (processed && !list_empty(&osb->blocked_lock_list)) {
4353 lockres = list_entry(osb->blocked_lock_list.next,
4354 struct ocfs2_lock_res, l_blocked_list);
4355 list_del_init(&lockres->l_blocked_list);
4356 osb->blocked_lock_count--;
4357 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4358
4359 BUG_ON(!processed);
4360 processed--;
4361
4362 ocfs2_process_blocked_lock(osb, lockres);
4363
4364 spin_lock_irqsave(&osb->dc_task_lock, flags);
4365 }
4366 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4367}
4368
4369static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
4370{
4371 int empty = 0;
4372 unsigned long flags;
4373
4374 spin_lock_irqsave(&osb->dc_task_lock, flags);
4375 if (list_empty(&osb->blocked_lock_list))
4376 empty = 1;
4377
4378 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4379 return empty;
4380}
4381
4382static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
4383{
4384 int should_wake = 0;
4385 unsigned long flags;
4386
4387 spin_lock_irqsave(&osb->dc_task_lock, flags);
4388 if (osb->dc_work_sequence != osb->dc_wake_sequence)
4389 should_wake = 1;
4390 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4391
4392 return should_wake;
4393}
4394
4395static int ocfs2_downconvert_thread(void *arg)
4396{
4397 struct ocfs2_super *osb = arg;
4398
4399
4400
4401 while (!(kthread_should_stop() &&
4402 ocfs2_downconvert_thread_lists_empty(osb))) {
4403
4404 wait_event_interruptible(osb->dc_event,
4405 ocfs2_downconvert_thread_should_wake(osb) ||
4406 kthread_should_stop());
4407
4408 mlog(0, "downconvert_thread: awoken\n");
4409
4410 ocfs2_downconvert_thread_do_work(osb);
4411 }
4412
4413 osb->dc_task = NULL;
4414 return 0;
4415}
4416
4417void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
4418{
4419 unsigned long flags;
4420
4421 spin_lock_irqsave(&osb->dc_task_lock, flags);
4422
4423
4424 osb->dc_wake_sequence++;
4425 spin_unlock_irqrestore(&osb->dc_task_lock, flags);
4426 wake_up(&osb->dc_event);
4427}
4428