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