Whamcloud - gitweb
LU-12635 build: Support for gcc -Wimplicit-fallthrough
[fs/lustre-release.git] / lustre / mdc / mdc_changelog.c
index 3ae27f9..fe7e1b2 100644 (file)
@@ -23,6 +23,8 @@
  * Copyright (c) 2017, Commissariat a l'Energie Atomique et aux Energies
  *                     Alternatives.
  *
+ * Copyright (c) 2017, Intel Corporation.
+ *
  * Author: Henri Doreau <henri.doreau@cea.fr>
  */
 
@@ -34,6 +36,7 @@
 #include <linux/miscdevice.h>
 
 #include <lustre_log.h>
+#include <uapi/linux/lustre/lustre_ioctl.h>
 
 #include "mdc_internal.h"
 
@@ -69,12 +72,12 @@ struct chlg_registered_dev {
 struct chlg_reader_state {
        /* Shortcut to the corresponding OBD device */
        struct obd_device       *crs_obd;
+       /* Producer thread (if any) */
+       struct task_struct      *crs_prod_task;
        /* An error occurred that prevents from reading further */
-       bool                     crs_err;
+       int                      crs_err;
        /* EOF, no more records available */
        bool                     crs_eof;
-       /* Userland reader closed connection */
-       bool                     crs_closed;
        /* Desired start position */
        __u64                    crs_start_offset;
        /* Wait queue for the catalog processing thread */
@@ -87,6 +90,9 @@ struct chlg_reader_state {
        __u64                    crs_rec_count;
        /* List of prefetched enqueued_record::enq_linkage_items */
        struct list_head         crs_rec_queue;
+       unsigned int             crs_last_catidx;
+       unsigned int             crs_last_idx;
+       bool                     crs_poll;
 };
 
 struct chlg_rec_entry {
@@ -122,7 +128,6 @@ static int chlg_read_cat_process_cb(const struct lu_env *env,
        struct llog_changelog_rec *rec;
        struct chlg_reader_state *crs = data;
        struct chlg_rec_entry *enq;
-       struct l_wait_info lwi = { 0 };
        size_t len;
        int rc;
        ENTRY;
@@ -132,6 +137,9 @@ static int chlg_read_cat_process_cb(const struct lu_env *env,
 
        rec = container_of(hdr, struct llog_changelog_rec, cr_hdr);
 
+       crs->crs_last_catidx = llh->lgh_hdr->llh_cat_idx;
+       crs->crs_last_idx = hdr->lrh_index;
+
        if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
                rc = -EINVAL;
                CERROR("%s: not a changelog rec %x/%d in llog "DFID" rc = %d\n",
@@ -152,11 +160,11 @@ static int chlg_read_cat_process_cb(const struct lu_env *env,
               PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
               rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
 
-       l_wait_event(crs->crs_waitq_prod,
-                    (crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
-                     crs->crs_closed), &lwi);
+       wait_event_interruptible(crs->crs_waitq_prod,
+                                crs->crs_rec_count < CDEV_CHLG_MAX_PREFETCH ||
+                                kthread_should_stop());
 
-       if (crs->crs_closed)
+       if (kthread_should_stop())
                RETURN(LLOG_PROC_BREAK);
 
        len = changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
@@ -188,22 +196,6 @@ static void enq_record_delete(struct chlg_rec_entry *rec)
 }
 
 /**
- * Release resources associated to a changelog_reader_state instance.
- *
- * @param  crs  CRS instance to release.
- */
-static void crs_free(struct chlg_reader_state *crs)
-{
-       struct chlg_rec_entry *rec;
-       struct chlg_rec_entry *tmp;
-
-       list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
-               enq_record_delete(rec);
-
-       OBD_FREE_PTR(crs);
-}
-
-/**
  * Record prefetch thread entry point. Opens the changelog catalog and starts
  * reading records.
  *
@@ -216,7 +208,6 @@ static int chlg_load(void *args)
        struct obd_device *obd = crs->crs_obd;
        struct llog_ctxt *ctx = NULL;
        struct llog_handle *llh = NULL;
-       struct l_wait_info lwi = { 0 };
        int rc;
        ENTRY;
 
@@ -224,6 +215,10 @@ static int chlg_load(void *args)
        if (ctx == NULL)
                GOTO(err_out, rc = -ENOENT);
 
+       crs->crs_last_catidx = -1;
+       crs->crs_last_idx = 0;
+
+again:
        rc = llog_open(NULL, ctx, &llh, NULL, CHANGELOG_CATALOG,
                       LLOG_OPEN_EXISTS);
        if (rc) {
@@ -232,21 +227,40 @@ static int chlg_load(void *args)
                GOTO(err_out, rc);
        }
 
-       rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT|LLOG_F_EXT_JOBID, NULL);
+
+       rc = llog_init_handle(NULL, llh,
+                             LLOG_F_IS_CAT |
+                             LLOG_F_EXT_JOBID |
+                             LLOG_F_EXT_EXTRA_FLAGS |
+                             LLOG_F_EXT_X_UIDGID |
+                             LLOG_F_EXT_X_NID |
+                             LLOG_F_EXT_X_OMODE |
+                             LLOG_F_EXT_X_XATTR,
+                             NULL);
        if (rc) {
                CERROR("%s: fail to init llog handle: rc = %d\n",
                       obd->obd_name, rc);
                GOTO(err_out, rc);
        }
 
-       rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs, 0, 0);
+       rc = llog_cat_process(NULL, llh, chlg_read_cat_process_cb, crs,
+                               crs->crs_last_catidx, crs->crs_last_idx);
        if (rc < 0) {
                CERROR("%s: fail to process llog: rc = %d\n", obd->obd_name, rc);
                GOTO(err_out, rc);
        }
+       if (!kthread_should_stop() && crs->crs_poll) {
+               llog_cat_close(NULL, llh);
+               schedule_timeout_interruptible(HZ);
+               goto again;
+       }
+
+       crs->crs_eof = true;
 
 err_out:
-       crs->crs_err = true;
+       if (rc < 0)
+               crs->crs_err = rc;
+
        wake_up_all(&crs->crs_waitq_cons);
 
        if (llh != NULL)
@@ -255,8 +269,8 @@ err_out:
        if (ctx != NULL)
                llog_ctxt_put(ctx);
 
-       l_wait_event(crs->crs_waitq_prod, crs->crs_closed, &lwi);
-       crs_free(crs);
+       wait_event_interruptible(crs->crs_waitq_prod, kthread_should_stop());
+
        RETURN(rc);
 }
 
@@ -278,17 +292,22 @@ static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
        struct chlg_reader_state *crs = file->private_data;
        struct chlg_rec_entry *rec;
        struct chlg_rec_entry *tmp;
-       struct l_wait_info lwi = { 0 };
-       ssize_t  written_total = 0;
+       size_t written_total = 0;
+       ssize_t rc;
        LIST_HEAD(consumed);
        ENTRY;
 
-       if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0)
-               RETURN(-EAGAIN);
+       if (file->f_flags & O_NONBLOCK && crs->crs_rec_count == 0) {
+               if (crs->crs_err < 0)
+                       RETURN(crs->crs_err);
+               else if (crs->crs_eof)
+                       RETURN(0);
+               else
+                       RETURN(-EAGAIN);
+       }
 
-       l_wait_event(crs->crs_waitq_cons,
-                    crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err,
-                    &lwi);
+       rc = wait_event_interruptible(crs->crs_waitq_cons,
+                       crs->crs_rec_count > 0 || crs->crs_eof || crs->crs_err);
 
        mutex_lock(&crs->crs_lock);
        list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage) {
@@ -296,8 +315,7 @@ static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
                        break;
 
                if (copy_to_user(buff, rec->enq_record, rec->enq_length)) {
-                       if (written_total == 0)
-                               written_total = -EFAULT;
+                       rc = -EFAULT;
                        break;
                }
 
@@ -311,15 +329,19 @@ static ssize_t chlg_read(struct file *file, char __user *buff, size_t count,
        }
        mutex_unlock(&crs->crs_lock);
 
-       if (written_total > 0)
+       if (written_total > 0) {
+               rc = written_total;
                wake_up_all(&crs->crs_waitq_prod);
+       } else if (rc == 0) {
+               rc = crs->crs_err;
+       }
 
        list_for_each_entry_safe(rec, tmp, &consumed, enq_linkage)
                enq_record_delete(rec);
 
        *ppos = crs->crs_start_offset;
 
-       RETURN(written_total);
+       RETURN(rc);
 }
 
 /**
@@ -462,31 +484,6 @@ out_kbuf:
 }
 
 /**
- * Find the OBD device associated to a changelog character device.
- * @param[in]  cdev  character device instance descriptor
- * @return corresponding OBD device or NULL if none was found.
- */
-static struct obd_device *chlg_obd_get(dev_t cdev)
-{
-       int minor = MINOR(cdev);
-       struct obd_device *obd = NULL;
-       struct chlg_registered_dev *curr;
-
-       mutex_lock(&chlg_registered_dev_lock);
-       list_for_each_entry(curr, &chlg_registered_devices, ced_link) {
-               if (curr->ced_misc.minor == minor) {
-                       /* take the first available OBD device attached */
-                       obd = list_first_entry(&curr->ced_obds,
-                                              struct obd_device,
-                                              u.cli.cl_chg_dev_linkage);
-                       break;
-               }
-       }
-       mutex_unlock(&chlg_registered_dev_lock);
-       return obd;
-}
-
-/**
  * Open handler, initialize internal CRS state and spawn prefetch thread if
  * needed.
  * @param[in]  inode  Inode struct for the open character device.
@@ -496,13 +493,17 @@ static struct obd_device *chlg_obd_get(dev_t cdev)
 static int chlg_open(struct inode *inode, struct file *file)
 {
        struct chlg_reader_state *crs;
-       struct obd_device *obd = chlg_obd_get(inode->i_rdev);
+       struct miscdevice *misc = file->private_data;
+       struct chlg_registered_dev *dev;
+       struct obd_device *obd;
        struct task_struct *task;
        int rc;
        ENTRY;
 
-       if (!obd)
-               RETURN(-ENODEV);
+       dev = container_of(misc, struct chlg_registered_dev, ced_misc);
+       obd = list_first_entry(&dev->ced_obds,
+                              struct obd_device,
+                              u.cli.cl_chg_dev_linkage);
 
        OBD_ALLOC_PTR(crs);
        if (!crs)
@@ -511,7 +512,6 @@ static int chlg_open(struct inode *inode, struct file *file)
        crs->crs_obd = obd;
        crs->crs_err = false;
        crs->crs_eof = false;
-       crs->crs_closed = false;
 
        mutex_init(&crs->crs_lock);
        INIT_LIST_HEAD(&crs->crs_rec_queue);
@@ -526,6 +526,7 @@ static int chlg_open(struct inode *inode, struct file *file)
                               obd->obd_name, rc);
                        GOTO(err_crs, rc);
                }
+               crs->crs_prod_task = task;
        }
 
        file->private_data = crs;
@@ -546,15 +547,19 @@ err_crs:
 static int chlg_release(struct inode *inode, struct file *file)
 {
        struct chlg_reader_state *crs = file->private_data;
+       struct chlg_rec_entry *rec;
+       struct chlg_rec_entry *tmp;
+       int rc = 0;
 
-       if (file->f_mode & FMODE_READ) {
-               crs->crs_closed = true;
-               wake_up_all(&crs->crs_waitq_prod);
-       } else {
-               /* No producer thread, release resource ourselves */
-               crs_free(crs);
-       }
-       return 0;
+       if (crs->crs_prod_task)
+               rc = kthread_stop(crs->crs_prod_task);
+
+       list_for_each_entry_safe(rec, tmp, &crs->crs_rec_queue, enq_linkage)
+               enq_record_delete(rec);
+
+       OBD_FREE_PTR(crs);
+
+       return rc;
 }
 
 /**
@@ -567,7 +572,7 @@ static int chlg_release(struct inode *inode, struct file *file)
  */
 static unsigned int chlg_poll(struct file *file, poll_table *wait)
 {
-       struct chlg_reader_state *crs  = file->private_data;
+       struct chlg_reader_state *crs = file->private_data;
        unsigned int mask = 0;
 
        mutex_lock(&crs->crs_lock);
@@ -582,6 +587,23 @@ static unsigned int chlg_poll(struct file *file, poll_table *wait)
        return mask;
 }
 
+static long chlg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       int rc;
+
+       struct chlg_reader_state *crs = file->private_data;
+       switch (cmd) {
+       case OBD_IOC_CHLG_POLL:
+               crs->crs_poll = !!arg;
+               rc = 0;
+               break;
+       default:
+               rc = -EINVAL;
+               break;
+       }
+       return rc;
+}
+
 static const struct file_operations chlg_fops = {
        .owner          = THIS_MODULE,
        .llseek         = chlg_llseek,
@@ -590,6 +612,7 @@ static const struct file_operations chlg_fops = {
        .open           = chlg_open,
        .release        = chlg_release,
        .poll           = chlg_poll,
+       .unlocked_ioctl = chlg_ioctl,
 };
 
 /**
@@ -622,6 +645,7 @@ chlg_registered_dev_find_by_name(const char *name)
 {
        struct chlg_registered_dev *dit;
 
+       LASSERT(mutex_is_locked(&chlg_registered_dev_lock));
        list_for_each_entry(dit, &chlg_registered_devices, ced_link)
                if (strcmp(name, dit->ced_name) == 0)
                        return dit;
@@ -640,6 +664,7 @@ chlg_registered_dev_find_by_obd(const struct obd_device *obd)
        struct chlg_registered_dev *dit;
        struct obd_device *oit;
 
+       LASSERT(mutex_is_locked(&chlg_registered_dev_lock));
        list_for_each_entry(dit, &chlg_registered_devices, ced_link)
                list_for_each_entry(oit, &dit->ced_obds,
                                    u.cli.cl_chg_dev_linkage)
@@ -685,13 +710,16 @@ int mdc_changelog_cdev_init(struct obd_device *obd)
                GOTO(out_unlock, rc = 0);
        }
 
+       list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
+       list_add_tail(&entry->ced_link, &chlg_registered_devices);
+
        /* Register new character device */
        rc = misc_register(&entry->ced_misc);
-       if (rc != 0)
+       if (rc != 0) {
+               list_del_init(&obd->u.cli.cl_chg_dev_linkage);
+               list_del(&entry->ced_link);
                GOTO(out_unlock, rc);
-
-       list_add_tail(&obd->u.cli.cl_chg_dev_linkage, &entry->ced_obds);
-       list_add_tail(&entry->ced_link, &chlg_registered_devices);
+       }
 
        entry = NULL;   /* prevent it from being freed below */
 
@@ -712,6 +740,7 @@ static void chlg_dev_clear(struct kref *kref)
                                                      ced_refs);
        ENTRY;
 
+       LASSERT(mutex_is_locked(&chlg_registered_dev_lock));
        list_del(&entry->ced_link);
        misc_deregister(&entry->ced_misc);
        OBD_FREE_PTR(entry);
@@ -723,10 +752,11 @@ static void chlg_dev_clear(struct kref *kref)
  */
 void mdc_changelog_cdev_finish(struct obd_device *obd)
 {
-       struct chlg_registered_dev *dev = chlg_registered_dev_find_by_obd(obd);
-       ENTRY;
+       struct chlg_registered_dev *dev;
 
+       ENTRY;
        mutex_lock(&chlg_registered_dev_lock);
+       dev = chlg_registered_dev_find_by_obd(obd);
        list_del_init(&obd->u.cli.cl_chg_dev_linkage);
        kref_put(&dev->ced_refs, chlg_dev_clear);
        mutex_unlock(&chlg_registered_dev_lock);