Whamcloud - gitweb
LU-6245 server: remove types abstraction from MDS/MGS code
[fs/lustre-release.git] / lustre / mdt / mdt_hsm_cdt_requests.c
index 9d20164..2dcdcd9 100644 (file)
@@ -23,6 +23,7 @@
  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
  *     alternatives
  *
+ * Copyright (c) 2014, Intel Corporation.
  */
 /*
  * lustre/mdt/mdt_hsm_cdt_requests.c
  */
 void dump_requests(char *prefix, struct coordinator *cdt)
 {
-       cfs_list_t              *pos;
        struct cdt_agent_req    *car;
 
        down_read(&cdt->cdt_request_lock);
-       cfs_list_for_each(pos, &cdt->cdt_requests) {
-               car = cfs_list_entry(pos, struct cdt_agent_req,
-                                    car_request_list);
+       list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
                CDEBUG(D_HSM, "%s fid="DFID" dfid="DFID
-                      " compound/cookie="LPX64"/"LPX64
-                      " action=%s archive#=%d flags="LPX64
-                      " extent="LPX64"-"LPX64
-                      " gid="LPX64" refcount=%d canceled=%d\n",
+                      " compound/cookie=%#llx/%#llx"
+                      " action=%s archive#=%d flags=%#llx"
+                      " extent=%#llx-%#llx"
+                      " gid=%#llx refcount=%d canceled=%d\n",
                       prefix, PFID(&car->car_hai->hai_fid),
                       PFID(&car->car_hai->hai_dfid),
                       car->car_compound_id, car->car_hai->hai_cookie,
@@ -66,7 +64,7 @@ void dump_requests(char *prefix, struct coordinator *cdt)
                       car->car_hai->hai_extent.offset,
                       car->car_hai->hai_extent.length,
                       car->car_hai->hai_gid,
-                      cfs_atomic_read(&car->car_refcount),
+                      atomic_read(&car->car_refcount),
                       car->car_canceled);
        }
        up_read(&cdt->cdt_request_lock);
@@ -153,14 +151,19 @@ out:
 /**
  * update data moved information during a request
  */
-static int mdt_cdt_update_work(struct cdt_req_progress *crp,
-                              struct hsm_extent *extent)
+static int hsm_update_work(struct cdt_req_progress *crp,
+                          const struct hsm_extent *extent)
 {
        int                       rc, osz, nsz;
        struct interval_node    **new_vv;
        struct interval_node     *v, *node;
+       __u64                     end;
        ENTRY;
 
+       end = extent->offset + extent->length;
+       if (end <= extent->offset)
+               RETURN(-EINVAL);
+
        mutex_lock(&crp->crp_lock);
        /* new node index */
 
@@ -199,7 +202,7 @@ static int mdt_cdt_update_work(struct cdt_req_progress *crp,
 
        v = crp->crp_node[crp->crp_cnt / NODE_VECTOR_SZ];
        node = &v[crp->crp_cnt % NODE_VECTOR_SZ];
-       interval_set(node, extent->offset, extent->offset + extent->length);
+       interval_set(node, extent->offset, end);
        /* try to insert, if entry already exist ignore the new one
         * it can happen if ct sends 2 times the same progress */
        if (interval_insert(node, &crp->crp_root) == NULL)
@@ -208,7 +211,7 @@ static int mdt_cdt_update_work(struct cdt_req_progress *crp,
        rc = 0;
 out:
        mutex_unlock(&crp->crp_lock);
-       return rc;
+       RETURN(rc);
 }
 
 /**
@@ -222,7 +225,7 @@ static void mdt_cdt_init_request_tree(struct cdt_req_progress *crp)
        crp->crp_max = 0;
 }
 
-/** Allocate/init a agent request and its sub-structures.
+/** Allocate/init an agent request and its sub-structures.
  *
  * \param compound_id [IN]
  * \param archive_id [IN]
@@ -239,11 +242,11 @@ struct cdt_agent_req *mdt_cdt_alloc_request(__u64 compound_id, __u32 archive_id,
        struct cdt_agent_req *car;
        ENTRY;
 
-       OBD_ALLOC_PTR(car);
+       OBD_SLAB_ALLOC_PTR(car, mdt_hsm_car_kmem);
        if (car == NULL)
                RETURN(ERR_PTR(-ENOMEM));
 
-       cfs_atomic_set(&car->car_refcount, 0);
+       atomic_set(&car->car_refcount, 1);
        car->car_compound_id = compound_id;
        car->car_archive_id = archive_id;
        car->car_flags = flags;
@@ -253,7 +256,7 @@ struct cdt_agent_req *mdt_cdt_alloc_request(__u64 compound_id, __u32 archive_id,
        car->car_uuid = *uuid;
        OBD_ALLOC(car->car_hai, hai->hai_len);
        if (car->car_hai == NULL) {
-               OBD_FREE_PTR(car);
+               OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
                RETURN(ERR_PTR(-ENOMEM));
        }
        memcpy(car->car_hai, hai, hai->hai_len);
@@ -263,7 +266,7 @@ struct cdt_agent_req *mdt_cdt_alloc_request(__u64 compound_id, __u32 archive_id,
 }
 
 /**
- * Free a agent request and its sub-structures.
+ * Free an agent request and its sub-structures.
  *
  * \param car [IN]  Request to be freed.
  */
@@ -271,7 +274,7 @@ void mdt_cdt_free_request(struct cdt_agent_req *car)
 {
        mdt_cdt_free_request_tree(&car->car_progress);
        OBD_FREE(car->car_hai, car->car_hai->hai_len);
-       OBD_FREE_PTR(car);
+       OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
 }
 
 /**
@@ -280,7 +283,7 @@ void mdt_cdt_free_request(struct cdt_agent_req *car)
  */
 void mdt_cdt_get_request(struct cdt_agent_req *car)
 {
-       cfs_atomic_inc(&car->car_refcount);
+       atomic_inc(&car->car_refcount);
 }
 
 /**
@@ -290,41 +293,37 @@ void mdt_cdt_get_request(struct cdt_agent_req *car)
  */
 void mdt_cdt_put_request(struct cdt_agent_req *car)
 {
-       if (cfs_atomic_dec_and_test(&car->car_refcount))
+       LASSERT(atomic_read(&car->car_refcount) > 0);
+       if (atomic_dec_and_test(&car->car_refcount))
                mdt_cdt_free_request(car);
 }
 
 /**
  * find request in the list by cookie or by fid
- * lock cdt_request_lock needs to be hold by caller
+ * lock cdt_request_lock needs to be held by caller
  * \param cdt [IN] coordinator
  * \param cookie [IN] request cookie
  * \param fid [IN] fid
- * \retval request pointer
+ * \retval request pointer or NULL if not found
  */
 static struct cdt_agent_req *cdt_find_request_nolock(struct coordinator *cdt,
                                                     __u64 cookie,
                                                     const struct lu_fid *fid)
 {
-       cfs_list_t              *pos;
-       struct cdt_agent_req    *car;
+       struct cdt_agent_req *car;
+       struct cdt_agent_req *found = NULL;
        ENTRY;
 
-       if (cfs_list_empty(&cdt->cdt_requests))
-               goto notfound;
-
-       cfs_list_for_each(pos, &cdt->cdt_requests) {
-               car = cfs_list_entry(pos, struct cdt_agent_req,
-                                    car_request_list);
-               if ((car->car_hai->hai_cookie == cookie) ||
-                   ((fid != NULL) && lu_fid_eq(fid, &car->car_hai->hai_fid))) {
+       list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
+               if (car->car_hai->hai_cookie == cookie ||
+                   (fid != NULL && lu_fid_eq(fid, &car->car_hai->hai_fid))) {
                        mdt_cdt_get_request(car);
-                       RETURN(car);
+                       found = car;
+                       break;
                }
        }
 
-notfound:
-       RETURN(ERR_PTR(-ENOENT));
+       RETURN(found);
 }
 
 /**
@@ -343,23 +342,19 @@ int mdt_cdt_add_request(struct coordinator *cdt, struct cdt_agent_req *new_car)
        LASSERT(new_car->car_hai->hai_action != HSMA_CANCEL);
 
        down_write(&cdt->cdt_request_lock);
-
        car = cdt_find_request_nolock(cdt, new_car->car_hai->hai_cookie, NULL);
-       if (!IS_ERR(car)) {
+       if (car != NULL) {
                mdt_cdt_put_request(car);
                up_write(&cdt->cdt_request_lock);
                RETURN(-EEXIST);
        }
 
-       mdt_cdt_get_request(new_car);
-       cfs_list_add_tail(&new_car->car_request_list, &cdt->cdt_requests);
+       list_add_tail(&new_car->car_request_list, &cdt->cdt_requests);
        up_write(&cdt->cdt_request_lock);
 
        mdt_hsm_agent_update_statistics(cdt, 0, 0, 1, &new_car->car_uuid);
 
-       down(&cdt->cdt_counter_lock);
-       cdt->cdt_request_count++;
-       up(&cdt->cdt_counter_lock);
+       atomic_inc(&cdt->cdt_request_count);
 
        RETURN(0);
 }
@@ -369,19 +364,17 @@ int mdt_cdt_add_request(struct coordinator *cdt, struct cdt_agent_req *new_car)
  * \param cdt [IN] coordinator
  * \param cookie [IN] request cookie
  * \param fid [IN] fid
- * \retval request pointer
+ * \retval request pointer or NULL if not found
  */
 struct cdt_agent_req *mdt_cdt_find_request(struct coordinator *cdt,
-                                          __u64 cookie,
+                                          const __u64 cookie,
                                           const struct lu_fid *fid)
 {
        struct cdt_agent_req    *car;
        ENTRY;
 
        down_read(&cdt->cdt_request_lock);
-
        car = cdt_find_request_nolock(cdt, cookie, fid);
-
        up_read(&cdt->cdt_request_lock);
 
        RETURN(car);
@@ -395,20 +388,23 @@ struct cdt_agent_req *mdt_cdt_find_request(struct coordinator *cdt,
  */
 int mdt_cdt_remove_request(struct coordinator *cdt, __u64 cookie)
 {
-       struct cdt_agent_req    *car;
+       struct cdt_agent_req *car;
        ENTRY;
 
        down_write(&cdt->cdt_request_lock);
-
        car = cdt_find_request_nolock(cdt, cookie, NULL);
-       if (!IS_ERR(car)) {
-               cfs_list_del(&car->car_request_list);
-               mdt_cdt_put_request(car);
+       if (car != NULL) {
+               list_del(&car->car_request_list);
                up_write(&cdt->cdt_request_lock);
 
-               down(&cdt->cdt_counter_lock);
-               cdt->cdt_request_count--;
-               up(&cdt->cdt_counter_lock);
+               /* reference from cdt_requests list */
+               mdt_cdt_put_request(car);
+
+               /* reference from cdt_find_request_nolock() */
+               mdt_cdt_put_request(car);
+
+               LASSERT(atomic_read(&cdt->cdt_request_count) >= 1);
+               atomic_dec(&cdt->cdt_request_count);
 
                RETURN(0);
        }
@@ -426,21 +422,21 @@ int mdt_cdt_remove_request(struct coordinator *cdt, __u64 cookie)
  * \retval -ve failure
  */
 struct cdt_agent_req *mdt_cdt_update_request(struct coordinator *cdt,
-                                            struct hsm_progress_kernel *pgs)
+                                         const struct hsm_progress_kernel *pgs)
 {
        struct cdt_agent_req    *car;
        int                      rc;
        ENTRY;
 
        car = mdt_cdt_find_request(cdt, pgs->hpk_cookie, NULL);
-       if (IS_ERR(car))
-               RETURN(car);
+       if (car == NULL)
+               RETURN(ERR_PTR(-ENOENT));
 
        car->car_req_update = cfs_time_current_sec();
 
        /* update progress done by copy tool */
        if (pgs->hpk_errval == 0 && pgs->hpk_extent.length != 0) {
-               rc = mdt_cdt_update_work(&car->car_progress, &pgs->hpk_extent);
+               rc = hsm_update_work(&car->car_progress, &pgs->hpk_extent);
                if (rc) {
                        mdt_cdt_put_request(car);
                        RETURN(ERR_PTR(rc));
@@ -461,24 +457,24 @@ struct cdt_agent_req *mdt_cdt_update_request(struct coordinator *cdt,
 /**
  * seq_file method called to start access to /proc file
  */
-static void *mdt_hsm_request_proc_start(struct seq_file *s, loff_t *p)
+static void *mdt_hsm_active_requests_proc_start(struct seq_file *s, loff_t *p)
 {
        struct mdt_device       *mdt = s->private;
        struct coordinator      *cdt = &mdt->mdt_coordinator;
-       cfs_list_t              *pos;
+       struct list_head        *pos;
        loff_t                   i;
        ENTRY;
 
        down_read(&cdt->cdt_request_lock);
 
-       if (cfs_list_empty(&cdt->cdt_requests))
+       if (list_empty(&cdt->cdt_requests))
                RETURN(NULL);
 
        if (*p == 0)
                RETURN(SEQ_START_TOKEN);
 
        i = 0;
-       cfs_list_for_each(pos, &cdt->cdt_requests) {
+       list_for_each(pos, &cdt->cdt_requests) {
                i++;
                if (i >= *p)
                        RETURN(pos);
@@ -490,11 +486,12 @@ static void *mdt_hsm_request_proc_start(struct seq_file *s, loff_t *p)
  * seq_file method called to get next item
  * just returns NULL at eof
  */
-static void *mdt_hsm_request_proc_next(struct seq_file *s, void *v, loff_t *p)
+static void *mdt_hsm_active_requests_proc_next(struct seq_file *s, void *v,
+                                              loff_t *p)
 {
        struct mdt_device       *mdt = s->private;
        struct coordinator      *cdt = &mdt->mdt_coordinator;
-       cfs_list_t              *pos = v;
+       struct list_head        *pos = v;
        ENTRY;
 
        if (pos == SEQ_START_TOKEN)
@@ -512,9 +509,9 @@ static void *mdt_hsm_request_proc_next(struct seq_file *s, void *v, loff_t *p)
 /**
  * display request data
  */
-static int mdt_hsm_request_proc_show(struct seq_file *s, void *v)
+static int mdt_hsm_active_requests_proc_show(struct seq_file *s, void *v)
 {
-       cfs_list_t              *pos = v;
+       struct list_head        *pos = v;
        struct cdt_agent_req    *car;
        char                     buf[12];
        __u64                    data_moved;
@@ -523,14 +520,14 @@ static int mdt_hsm_request_proc_show(struct seq_file *s, void *v)
        if (pos == SEQ_START_TOKEN)
                RETURN(0);
 
-       car = cfs_list_entry(pos, struct cdt_agent_req, car_request_list);
+       car = list_entry(pos, struct cdt_agent_req, car_request_list);
        mdt_cdt_get_work_done(car, &data_moved);
 
        seq_printf(s, "fid="DFID" dfid="DFID
-                  " compound/cookie="LPX64"/"LPX64
-                  " action=%s archive#=%d flags="LPX64
-                  " extent="LPX64"-"LPX64" gid="LPX64
-                  " data=[%s] canceled=%d uuid=%s done="LPU64"%%\n",
+                  " compound/cookie=%#llx/%#llx"
+                  " action=%s archive#=%d flags=%#llx"
+                  " extent=%#llx-%#llx gid=%#llx"
+                  " data=[%s] canceled=%d uuid=%s done=%llu\n",
                   PFID(&car->car_hai->hai_fid),
                   PFID(&car->car_hai->hai_dfid),
                   car->car_compound_id, car->car_hai->hai_cookie,
@@ -548,7 +545,7 @@ static int mdt_hsm_request_proc_show(struct seq_file *s, void *v)
 /**
  * seq_file method called to stop access to /proc file
  */
-static void mdt_hsm_request_proc_stop(struct seq_file *s, void *v)
+static void mdt_hsm_active_requests_proc_stop(struct seq_file *s, void *v)
 {
        struct mdt_device       *mdt = s->private;
        struct coordinator      *cdt = &mdt->mdt_coordinator;
@@ -560,41 +557,38 @@ static void mdt_hsm_request_proc_stop(struct seq_file *s, void *v)
 }
 
 /* hsm agent list proc functions */
-static const struct seq_operations mdt_hsm_request_proc_ops = {
-       .start          = mdt_hsm_request_proc_start,
-       .next           = mdt_hsm_request_proc_next,
-       .show           = mdt_hsm_request_proc_show,
-       .stop           = mdt_hsm_request_proc_stop,
+static const struct seq_operations mdt_hsm_active_requests_proc_ops = {
+       .start          = mdt_hsm_active_requests_proc_start,
+       .next           = mdt_hsm_active_requests_proc_next,
+       .show           = mdt_hsm_active_requests_proc_show,
+       .stop           = mdt_hsm_active_requests_proc_stop,
 };
 
 /**
  * public function called at open of /proc file to get
  * list of agents
  */
-static int lprocfs_open_hsm_request(struct inode *inode, struct file *file)
+static int lprocfs_open_hsm_active_requests(struct inode *inode,
+                                           struct file *file)
 {
        struct seq_file *s;
        int              rc;
        ENTRY;
 
-       if (LPROCFS_ENTRY_AND_CHECK(PDE(inode)))
-               RETURN(-ENOENT);
-
-       rc = seq_open(file, &mdt_hsm_request_proc_ops);
+       rc = seq_open(file, &mdt_hsm_active_requests_proc_ops);
        if (rc) {
-               LPROCFS_EXIT();
                RETURN(rc);
        }
        s = file->private_data;
-       s->private = PDE(inode)->data;
+       s->private = PDE_DATA(inode);
 
        RETURN(rc);
 }
 
 /* methods to access hsm request list */
-const struct file_operations mdt_hsm_request_fops = {
+const struct file_operations mdt_hsm_active_requests_fops = {
        .owner          = THIS_MODULE,
-       .open           = lprocfs_open_hsm_request,
+       .open           = lprocfs_open_hsm_active_requests,
        .read           = seq_read,
        .llseek         = seq_lseek,
        .release        = lprocfs_seq_release,