1010
1111
1212#include <linux/module.h>
13+ #include <linux/kthread.h>
1314#include <linux/dlm.h>
1415#include <linux/sched.h>
1516#include <linux/raid/md_p.h>
@@ -25,7 +26,8 @@ struct dlm_lock_resource {
2526 struct dlm_lksb lksb ;
2627 char * name ; /* lock name. */
2728 uint32_t flags ; /* flags to pass to dlm_lock() */
28- struct completion completion ; /* completion for synchronized locking */
29+ wait_queue_head_t sync_locking ; /* wait queue for synchronized locking */
30+ bool sync_locking_done ;
2931 void (* bast )(void * arg , int mode ); /* blocking AST function pointer*/
3032 struct mddev * mddev ; /* pointing back to mddev. */
3133 int mode ;
@@ -118,7 +120,8 @@ static void sync_ast(void *arg)
118120 struct dlm_lock_resource * res ;
119121
120122 res = arg ;
121- complete (& res -> completion );
123+ res -> sync_locking_done = true;
124+ wake_up (& res -> sync_locking );
122125}
123126
124127static int dlm_lock_sync (struct dlm_lock_resource * res , int mode )
@@ -130,7 +133,8 @@ static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
130133 0 , sync_ast , res , res -> bast );
131134 if (ret )
132135 return ret ;
133- wait_for_completion (& res -> completion );
136+ wait_event (res -> sync_locking , res -> sync_locking_done );
137+ res -> sync_locking_done = false;
134138 if (res -> lksb .sb_status == 0 )
135139 res -> mode = mode ;
136140 return res -> lksb .sb_status ;
@@ -141,6 +145,44 @@ static int dlm_unlock_sync(struct dlm_lock_resource *res)
141145 return dlm_lock_sync (res , DLM_LOCK_NL );
142146}
143147
148+ /*
149+ * An variation of dlm_lock_sync, which make lock request could
150+ * be interrupted
151+ */
152+ static int dlm_lock_sync_interruptible (struct dlm_lock_resource * res , int mode ,
153+ struct mddev * mddev )
154+ {
155+ int ret = 0 ;
156+
157+ ret = dlm_lock (res -> ls , mode , & res -> lksb ,
158+ res -> flags , res -> name , strlen (res -> name ),
159+ 0 , sync_ast , res , res -> bast );
160+ if (ret )
161+ return ret ;
162+
163+ wait_event (res -> sync_locking , res -> sync_locking_done
164+ || kthread_should_stop ()
165+ || test_bit (MD_CLOSING , & mddev -> flags ));
166+ if (!res -> sync_locking_done ) {
167+ /*
168+ * the convert queue contains the lock request when request is
169+ * interrupted, and sync_ast could still be run, so need to
170+ * cancel the request and reset completion
171+ */
172+ ret = dlm_unlock (res -> ls , res -> lksb .sb_lkid , DLM_LKF_CANCEL ,
173+ & res -> lksb , res );
174+ res -> sync_locking_done = false;
175+ if (unlikely (ret != 0 ))
176+ pr_info ("failed to cancel previous lock request "
177+ "%s return %d\n" , res -> name , ret );
178+ return - EPERM ;
179+ } else
180+ res -> sync_locking_done = false;
181+ if (res -> lksb .sb_status == 0 )
182+ res -> mode = mode ;
183+ return res -> lksb .sb_status ;
184+ }
185+
144186static struct dlm_lock_resource * lockres_init (struct mddev * mddev ,
145187 char * name , void (* bastfn )(void * arg , int mode ), int with_lvb )
146188{
@@ -151,7 +193,8 @@ static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
151193 res = kzalloc (sizeof (struct dlm_lock_resource ), GFP_KERNEL );
152194 if (!res )
153195 return NULL ;
154- init_completion (& res -> completion );
196+ init_waitqueue_head (& res -> sync_locking );
197+ res -> sync_locking_done = false;
155198 res -> ls = cinfo -> lockspace ;
156199 res -> mddev = mddev ;
157200 res -> mode = DLM_LOCK_IV ;
@@ -194,25 +237,21 @@ static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
194237
195238static void lockres_free (struct dlm_lock_resource * res )
196239{
197- int ret ;
240+ int ret = 0 ;
198241
199242 if (!res )
200243 return ;
201244
202- /* cancel a lock request or a conversion request that is blocked */
203- res -> flags |= DLM_LKF_CANCEL ;
204- retry :
205- ret = dlm_unlock (res -> ls , res -> lksb .sb_lkid , 0 , & res -> lksb , res );
206- if (unlikely (ret != 0 )) {
207- pr_info ("%s: failed to unlock %s return %d\n" , __func__ , res -> name , ret );
208-
209- /* if a lock conversion is cancelled, then the lock is put
210- * back to grant queue, need to ensure it is unlocked */
211- if (ret == - DLM_ECANCEL )
212- goto retry ;
213- }
214- res -> flags &= ~DLM_LKF_CANCEL ;
215- wait_for_completion (& res -> completion );
245+ /*
246+ * use FORCEUNLOCK flag, so we can unlock even the lock is on the
247+ * waiting or convert queue
248+ */
249+ ret = dlm_unlock (res -> ls , res -> lksb .sb_lkid , DLM_LKF_FORCEUNLOCK ,
250+ & res -> lksb , res );
251+ if (unlikely (ret != 0 ))
252+ pr_err ("failed to unlock %s return %d\n" , res -> name , ret );
253+ else
254+ wait_event (res -> sync_locking , res -> sync_locking_done );
216255
217256 kfree (res -> name );
218257 kfree (res -> lksb .sb_lvbptr );
@@ -279,7 +318,7 @@ static void recover_bitmaps(struct md_thread *thread)
279318 goto clear_bit ;
280319 }
281320
282- ret = dlm_lock_sync (bm_lockres , DLM_LOCK_PW );
321+ ret = dlm_lock_sync_interruptible (bm_lockres , DLM_LOCK_PW , mddev );
283322 if (ret ) {
284323 pr_err ("md-cluster: Could not DLM lock %s: %d\n" ,
285324 str , ret );
@@ -288,7 +327,7 @@ static void recover_bitmaps(struct md_thread *thread)
288327 ret = bitmap_copy_from_slot (mddev , slot , & lo , & hi , true);
289328 if (ret ) {
290329 pr_err ("md-cluster: Could not copy data from bitmap %d\n" , slot );
291- goto dlm_unlock ;
330+ goto clear_bit ;
292331 }
293332 if (hi > 0 ) {
294333 if (lo < mddev -> recovery_cp )
@@ -300,8 +339,6 @@ static void recover_bitmaps(struct md_thread *thread)
300339 md_wakeup_thread (mddev -> thread );
301340 }
302341 }
303- dlm_unlock :
304- dlm_unlock_sync (bm_lockres );
305342clear_bit :
306343 lockres_free (bm_lockres );
307344 clear_bit (slot , & cinfo -> recovery_map );
@@ -495,9 +532,10 @@ static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg
495532
496533static void process_remove_disk (struct mddev * mddev , struct cluster_msg * msg )
497534{
498- struct md_rdev * rdev = md_find_rdev_nr_rcu (mddev ,
499- le32_to_cpu (msg -> raid_slot ));
535+ struct md_rdev * rdev ;
500536
537+ rcu_read_lock ();
538+ rdev = md_find_rdev_nr_rcu (mddev , le32_to_cpu (msg -> raid_slot ));
501539 if (rdev ) {
502540 set_bit (ClusterRemove , & rdev -> flags );
503541 set_bit (MD_RECOVERY_NEEDED , & mddev -> recovery );
@@ -506,18 +544,21 @@ static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
506544 else
507545 pr_warn ("%s: %d Could not find disk(%d) to REMOVE\n" ,
508546 __func__ , __LINE__ , le32_to_cpu (msg -> raid_slot ));
547+ rcu_read_unlock ();
509548}
510549
511550static void process_readd_disk (struct mddev * mddev , struct cluster_msg * msg )
512551{
513- struct md_rdev * rdev = md_find_rdev_nr_rcu (mddev ,
514- le32_to_cpu (msg -> raid_slot ));
552+ struct md_rdev * rdev ;
515553
554+ rcu_read_lock ();
555+ rdev = md_find_rdev_nr_rcu (mddev , le32_to_cpu (msg -> raid_slot ));
516556 if (rdev && test_bit (Faulty , & rdev -> flags ))
517557 clear_bit (Faulty , & rdev -> flags );
518558 else
519559 pr_warn ("%s: %d Could not find disk(%d) which is faulty" ,
520560 __func__ , __LINE__ , le32_to_cpu (msg -> raid_slot ));
561+ rcu_read_unlock ();
521562}
522563
523564static int process_recvd_msg (struct mddev * mddev , struct cluster_msg * msg )
@@ -770,7 +811,6 @@ static int gather_all_resync_info(struct mddev *mddev, int total_slots)
770811 md_check_recovery (mddev );
771812 }
772813
773- dlm_unlock_sync (bm_lockres );
774814 lockres_free (bm_lockres );
775815 }
776816out :
@@ -1006,7 +1046,7 @@ static void metadata_update_cancel(struct mddev *mddev)
10061046static int resync_start (struct mddev * mddev )
10071047{
10081048 struct md_cluster_info * cinfo = mddev -> cluster_info ;
1009- return dlm_lock_sync (cinfo -> resync_lockres , DLM_LOCK_EX );
1049+ return dlm_lock_sync_interruptible (cinfo -> resync_lockres , DLM_LOCK_EX , mddev );
10101050}
10111051
10121052static int resync_info_update (struct mddev * mddev , sector_t lo , sector_t hi )
@@ -1186,7 +1226,6 @@ static void unlock_all_bitmaps(struct mddev *mddev)
11861226 if (cinfo -> other_bitmap_lockres ) {
11871227 for (i = 0 ; i < mddev -> bitmap_info .nodes - 1 ; i ++ ) {
11881228 if (cinfo -> other_bitmap_lockres [i ]) {
1189- dlm_unlock_sync (cinfo -> other_bitmap_lockres [i ]);
11901229 lockres_free (cinfo -> other_bitmap_lockres [i ]);
11911230 }
11921231 }
0 commit comments