Whamcloud - gitweb
LU-13453 osd-ldiskfs: do not leak inode if OI insertion fails
[fs/lustre-release.git] / lustre / obdclass / scrub.c
index c4feee6..adcffa9 100644 (file)
@@ -41,7 +41,8 @@
 
 static inline struct dt_device *scrub_obj2dev(struct dt_object *obj)
 {
-       return container_of0(obj->do_lu.lo_dev, struct dt_device, dd_lu_dev);
+       return container_of_safe(obj->do_lu.lo_dev, struct dt_device,
+                                dd_lu_dev);
 }
 
 static void scrub_file_to_cpu(struct scrub_file *des, struct scrub_file *src)
@@ -274,26 +275,12 @@ EXPORT_SYMBOL(scrub_checkpoint);
 int scrub_start(int (*threadfn)(void *data), struct lustre_scrub *scrub,
                void *data, __u32 flags)
 {
-       struct ptlrpc_thread *thread = &scrub->os_thread;
        struct task_struct *task;
        int rc;
        ENTRY;
 
-again:
-       /* os_lock: sync status between stop and scrub thread */
-       spin_lock(&scrub->os_lock);
-       if (thread_is_running(thread)) {
-               spin_unlock(&scrub->os_lock);
+       if (scrub->os_task)
                RETURN(-EALREADY);
-       }
-
-       if (unlikely(thread_is_stopping(thread))) {
-               spin_unlock(&scrub->os_lock);
-               wait_event_idle(thread->t_ctl_waitq,
-                               thread_is_stopped(thread));
-               goto again;
-       }
-       spin_unlock(&scrub->os_lock);
 
        if (scrub->os_file.sf_status == SS_COMPLETED) {
                if (!(flags & SS_SET_FAILOUT))
@@ -305,18 +292,25 @@ again:
                flags |= SS_RESET;
        }
 
-       scrub->os_start_flags = flags;
-       thread_set_flags(thread, 0);
-       task = kthread_run(threadfn, data, "OI_scrub");
+       task = kthread_create(threadfn, data, "OI_scrub");
        if (IS_ERR(task)) {
                rc = PTR_ERR(task);
                CERROR("%s: cannot start iteration thread: rc = %d\n",
                       scrub->os_name, rc);
                RETURN(rc);
        }
-
-       wait_event_idle(thread->t_ctl_waitq,
-                       thread_is_running(thread) || thread_is_stopped(thread));
+       spin_lock(&scrub->os_lock);
+       if (scrub->os_task) {
+               /* Lost a race */
+               spin_unlock(&scrub->os_lock);
+               kthread_stop(task);
+               RETURN(-EALREADY);
+       }
+       scrub->os_start_flags = flags;
+       scrub->os_task = task;
+       wake_up_process(task);
+       spin_unlock(&scrub->os_lock);
+       wait_var_event(scrub, scrub->os_running || !scrub->os_task);
 
        RETURN(0);
 }
@@ -324,21 +318,14 @@ EXPORT_SYMBOL(scrub_start);
 
 void scrub_stop(struct lustre_scrub *scrub)
 {
-       struct ptlrpc_thread *thread = &scrub->os_thread;
+       struct task_struct *task;
 
-       /* os_lock: sync status between stop and scrub thread */
        spin_lock(&scrub->os_lock);
-       if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
-               thread_set_flags(thread, SVC_STOPPING);
-               spin_unlock(&scrub->os_lock);
-               wake_up_all(&thread->t_ctl_waitq);
-               wait_event_idle(thread->t_ctl_waitq,
-                               thread_is_stopped(thread));
-               /* Do not skip the last lock/unlock, which can guarantee that
-                * the caller cannot return until the OI scrub thread exit. */
-               spin_lock(&scrub->os_lock);
-       }
+       scrub->os_running = 0;
        spin_unlock(&scrub->os_lock);
+       task = xchg(&scrub->os_task, NULL);
+       if (task)
+               kthread_stop(task);
 }
 EXPORT_SYMBOL(scrub_stop);
 
@@ -375,7 +362,7 @@ static void scrub_bits_dump(struct seq_file *m, int bits, const char *names[],
 
        seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
 
-       for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
+       for (i = 0, flag = 1; bits != 0; i++, flag = BIT(i)) {
                if (flag & bits) {
                        bits &= ~flag;
                        seq_printf(m, "%s%c", names[i],
@@ -454,7 +441,7 @@ void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub)
                   sf->sf_items_igif, sf->sf_success_count);
 
        speed = checked;
-       if (thread_is_running(&scrub->os_thread)) {
+       if (scrub->os_running) {
                s64 new_checked = scrub->os_new_checked;
                time64_t duration;
                time64_t rtime;
@@ -487,7 +474,7 @@ void scrub_dump(struct seq_file *m, struct lustre_scrub *scrub)
        } else {
                if (sf->sf_run_time != 0)
                        speed = div_s64(speed, sf->sf_run_time);
-               seq_printf(m, "run_time: %ld seconds\n"
+               seq_printf(m, "run_time: %d seconds\n"
                           "average_speed: %lld objects/sec\n"
                           "real_time_speed: N/A\n"
                           "current_position: N/A\n",
@@ -1075,6 +1062,10 @@ int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
        if (rc)
                GOTO(stop, rc);
 
+       rc = dt_declare_ref_del(env, tgt_obj, th);
+       if (rc)
+               GOTO(stop, rc);
+
        rc = dt_declare_destroy(env, tgt_obj, th);
        if (rc)
                GOTO(stop, rc);
@@ -1088,7 +1079,12 @@ int lustre_index_restore(const struct lu_env *env, struct dt_device *dev,
                GOTO(stop, rc);
 
        dt_write_lock(env, tgt_obj, 0);
-       rc = dt_destroy(env, tgt_obj, th);
+       rc = dt_ref_del(env, tgt_obj, th);
+       if (rc == 0) {
+               if (S_ISDIR(tgt_obj->do_lu.lo_header->loh_attr))
+                       dt_ref_del(env, tgt_obj, th);
+               rc = dt_destroy(env, tgt_obj, th);
+       }
        dt_write_unlock(env, tgt_obj);
        dt_trans_stop(env, dev, th);
        if (rc)