Whamcloud - gitweb
LU-8050 osp: not use obdo::o_lcookie for non-onwire RPC data
[fs/lustre-release.git] / lustre / osp / osp_sync.c
index 34a0e2c..4f777e8 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -27,7 +23,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2012, 2013, Intel Corporation.
+ * Copyright (c) 2012, 2016, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #define DEBUG_SUBSYSTEM S_MDS
 
+#include <linux/kthread.h>
 #include <lustre_log.h>
+#include <lustre_update.h>
 #include "osp_internal.h"
 
 static int osp_sync_id_traction_init(struct osp_device *d);
 static void osp_sync_id_traction_fini(struct osp_device *d);
-static __u32 osp_sync_id_get(struct osp_device *d, __u32 id);
+static __u64 osp_sync_id_get(struct osp_device *d, __u64 id);
 static void osp_sync_remove_from_tracker(struct osp_device *d);
 
 /*
@@ -93,16 +91,41 @@ static void osp_sync_remove_from_tracker(struct osp_device *d);
 
 #define OSP_JOB_MAGIC          0x26112005
 
+struct osp_job_req_args {
+       /** bytes reserved for ptlrpc_replay_req() */
+       struct ptlrpc_replay_async_args jra_raa;
+       struct list_head                jra_committed_link;
+       struct list_head                jra_inflight_link;
+       struct llog_cookie              jra_lcookie;
+       __u32                           jra_magic;
+};
+
 static inline int osp_sync_running(struct osp_device *d)
 {
        return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
 }
 
+/**
+ * Check status: whether OSP thread has stopped
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval 0           still running
+ * \retval 1           stopped
+ */
 static inline int osp_sync_stopped(struct osp_device *d)
 {
        return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
 }
 
+/*
+ ** Check for new changes to sync
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval 1           there are changes
+ * \retval 0           there are no changes
+ */
 static inline int osp_sync_has_new_job(struct osp_device *d)
 {
        return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
@@ -110,16 +133,84 @@ static inline int osp_sync_has_new_job(struct osp_device *d)
                || (d->opd_syn_prev_done == 0);
 }
 
+static inline int osp_sync_inflight_conflict(struct osp_device *d,
+                                            struct llog_rec_hdr *h)
+{
+       struct osp_job_req_args *jra;
+       struct ost_id            ostid;
+       int                      conflict = 0;
+
+       if (h == NULL || h->lrh_type == LLOG_GEN_REC ||
+           list_empty(&d->opd_syn_inflight_list))
+               return conflict;
+
+       memset(&ostid, 0, sizeof(ostid));
+       switch (h->lrh_type) {
+       case MDS_UNLINK_REC:
+               ostid_set_seq(&ostid, ((struct llog_unlink_rec *)h)->lur_oseq);
+               ostid_set_id(&ostid, ((struct llog_unlink_rec *)h)->lur_oid);
+               break;
+       case MDS_UNLINK64_REC:
+               fid_to_ostid(&((struct llog_unlink64_rec *)h)->lur_fid, &ostid);
+               break;
+       case MDS_SETATTR64_REC:
+               ostid = ((struct llog_setattr64_rec *)h)->lsr_oi;
+               break;
+       default:
+               LBUG();
+       }
+
+       spin_lock(&d->opd_syn_lock);
+       list_for_each_entry(jra, &d->opd_syn_inflight_list, jra_inflight_link) {
+               struct ptlrpc_request   *req;
+               struct ost_body         *body;
+
+               LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
+
+               req = container_of((void *)jra, struct ptlrpc_request,
+                                  rq_async_args);
+               body = req_capsule_client_get(&req->rq_pill,
+                                             &RMF_OST_BODY);
+               LASSERT(body);
+
+               if (memcmp(&ostid, &body->oa.o_oi, sizeof(ostid)) == 0) {
+                       conflict = 1;
+                       break;
+               }
+       }
+       spin_unlock(&d->opd_syn_lock);
+
+       return conflict;
+}
+
 static inline int osp_sync_low_in_progress(struct osp_device *d)
 {
-       return d->opd_syn_rpc_in_progress < d->opd_syn_max_rpc_in_progress;
+       return atomic_read(&d->opd_syn_rpc_in_progress) <
+               d->opd_syn_max_rpc_in_progress;
 }
 
+/**
+ * Check for room in the network pipe to OST
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval 1           there is room
+ * \retval 0           no room, the pipe is full
+ */
 static inline int osp_sync_low_in_flight(struct osp_device *d)
 {
-       return d->opd_syn_rpc_in_flight < d->opd_syn_max_rpc_in_flight;
+       return atomic_read(&d->opd_syn_rpc_in_flight) <
+               d->opd_syn_max_rpc_in_flight;
 }
 
+/**
+ * Wake up check for the main sync thread
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval 1           time to wake up
+ * \retval 0           no need to wake up
+ */
 static inline int osp_sync_has_work(struct osp_device *d)
 {
        /* has new/old changes and low in-progress? */
@@ -146,6 +237,39 @@ void __osp_sync_check_for_work(struct osp_device *d)
        osp_sync_check_for_work(d);
 }
 
+static inline __u64 osp_sync_correct_id(struct osp_device *d,
+                                       struct llog_rec_hdr *rec)
+{
+       /*
+        * llog use cyclic store with 32 bit lrh_id
+        * so overflow lrh_id is possible. Range between
+        * last_processed and last_committed is less than
+        * 64745 ^ 2 and less than 2^32 - 1
+        */
+       __u64 correct_id = d->opd_syn_last_committed_id;
+
+       if ((correct_id & 0xffffffffULL) < rec->lrh_id)
+               correct_id -= 0x100000000ULL;
+
+       correct_id &= ~0xffffffffULL;
+       correct_id |= rec->lrh_id;
+
+       return correct_id;
+}
+/**
+ * Check and return ready-for-new status.
+ *
+ * The thread processing llog record uses this function to check whether
+ * it's time to take another record and process it. The number of conditions
+ * must be met: the connection should be ready, RPCs in flight not exceeding
+ * the limit, the record is committed locally, etc (see the lines below).
+ *
+ * \param[in] d                OSP device
+ * \param[in] rec      next llog record to process
+ *
+ * \retval 0           not ready
+ * \retval 1           ready
+ */
 static inline int osp_sync_can_process_new(struct osp_device *d,
                                           struct llog_rec_hdr *rec)
 {
@@ -153,6 +277,8 @@ static inline int osp_sync_can_process_new(struct osp_device *d,
 
        if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
                return 0;
+       if (unlikely(osp_sync_inflight_conflict(d, rec)))
+               return 0;
        if (!osp_sync_low_in_progress(d))
                return 0;
        if (!osp_sync_low_in_flight(d))
@@ -161,26 +287,46 @@ static inline int osp_sync_can_process_new(struct osp_device *d,
                return 0;
        if (d->opd_syn_prev_done == 0)
                return 1;
-       if (d->opd_syn_changes == 0)
+       if (atomic_read(&d->opd_syn_changes) == 0)
                return 0;
-       if (rec == NULL || rec->lrh_id <= d->opd_syn_last_committed_id)
+       if (rec == NULL ||
+           osp_sync_correct_id(d, rec) <= d->opd_syn_last_committed_id)
                return 1;
        return 0;
 }
 
+/**
+ * Declare intention to add a new change.
+ *
+ * With regard to OSD API, we have to declare any changes ahead. In this
+ * case we declare an intention to add a llog record representing the
+ * change on the local storage.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] o                OSP object
+ * \param[in] type     type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
+ * \param[in] th       transaction handle (local)
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
                         llog_op_type type, struct thandle *th)
 {
        struct osp_thread_info  *osi = osp_env_info(env);
        struct osp_device       *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
        struct llog_ctxt        *ctxt;
+       struct thandle          *storage_th;
        int                      rc;
 
        ENTRY;
 
        /* it's a layering violation, to access internals of th,
         * but we can do this as a sanity check, for a while */
-       LASSERT(th->th_dev == d->opd_storage);
+       LASSERT(th->th_top != NULL);
+       storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
+       if (IS_ERR(storage_th))
+               RETURN(PTR_ERR(storage_th));
 
        switch (type) {
        case MDS_UNLINK64_REC:
@@ -194,17 +340,39 @@ int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
        }
 
        /* we want ->dt_trans_start() to allocate per-thandle structure */
-       th->th_tags |= LCT_OSP_THREAD;
+       storage_th->th_tags |= LCT_OSP_THREAD;
 
        ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
        LASSERT(ctxt);
 
-       rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr, th);
+       rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr,
+                             storage_th);
        llog_ctxt_put(ctxt);
 
        RETURN(rc);
 }
 
+/**
+ * Generate a llog record for a given change.
+ *
+ * Generates a llog record for the change passed. The change can be of two
+ * types: unlink and setattr. The record gets an ID which later will be
+ * used to track commit status of the change. For unlink changes, the caller
+ * can supply a starting FID and the count of the objects to destroy. For
+ * setattr the caller should apply attributes to apply.
+ *
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ * \param[in] fid      fid of the object the change should be applied to
+ * \param[in] type     type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
+ * \param[in] count    count of objects to destroy
+ * \param[in] th       transaction handle (local)
+ * \param[in] attr     attributes for setattr
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
                            const struct lu_fid *fid, llog_op_type type,
                            int count, struct thandle *th,
@@ -213,13 +381,17 @@ static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
        struct osp_thread_info  *osi = osp_env_info(env);
        struct llog_ctxt        *ctxt;
        struct osp_txn_info     *txn;
+       struct thandle          *storage_th;
        int                      rc;
 
        ENTRY;
 
        /* it's a layering violation, to access internals of th,
         * but we can do this as a sanity check, for a while */
-       LASSERT(th->th_dev == d->opd_storage);
+       LASSERT(th->th_top != NULL);
+       storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
+       if (IS_ERR(storage_th))
+               RETURN(PTR_ERR(storage_th));
 
        switch (type) {
        case MDS_UNLINK64_REC:
@@ -237,40 +409,37 @@ static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
                LASSERT(attr);
                osi->osi_setattr.lsr_uid = attr->la_uid;
                osi->osi_setattr.lsr_gid = attr->la_gid;
-               osi->osi_setattr.lsr_valid = attr->la_valid;
+               osi->osi_setattr.lsr_valid =
+                       ((attr->la_valid & LA_UID) ? OBD_MD_FLUID : 0) |
+                       ((attr->la_valid & LA_GID) ? OBD_MD_FLGID : 0);
                break;
        default:
                LBUG();
        }
 
-       txn = osp_txn_info(&th->th_ctx);
+       txn = osp_txn_info(&storage_th->th_ctx);
        LASSERT(txn);
 
        txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
-       osi->osi_hdr.lrh_id = txn->oti_current_id;
-
+       osi->osi_hdr.lrh_id = (txn->oti_current_id & 0xffffffffULL);
        ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
        if (ctxt == NULL)
                RETURN(-ENOMEM);
+
        rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
-                     th);
+                     storage_th);
        llog_ctxt_put(ctxt);
 
-       CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
-              d->opd_obd->obd_name, POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
-              (unsigned long) osi->osi_cookie.lgc_lgl.lgl_ogen,
-              (unsigned long) osi->osi_cookie.lgc_index, rc);
-
-       if (rc > 0)
-               rc = 0;
-
-       if (likely(rc == 0)) {
-               spin_lock(&d->opd_syn_lock);
-               d->opd_syn_changes++;
-               spin_unlock(&d->opd_syn_lock);
+       if (likely(rc >= 0)) {
+               CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
+                      d->opd_obd->obd_name,
+                      POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
+                      (unsigned long)osi->osi_cookie.lgc_lgl.lgl_ogen,
+                      (unsigned long)osi->osi_cookie.lgc_index, rc);
+               atomic_inc(&d->opd_syn_changes);
        }
-
-       RETURN(rc);
+       /* return 0 always here, error case just cause no llog record */
+       RETURN(0);
 }
 
 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
@@ -283,7 +452,7 @@ int osp_sync_add(const struct lu_env *env, struct osp_object *o,
 }
 
 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
-                struct lu_fid *fid, int lost, struct thandle *th)
+                       struct lu_fid *fid, int lost, struct thandle *th)
 {
        return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
 }
@@ -301,49 +470,74 @@ int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
  *  subsequent commit callback (at the most)
  */
 
-/*
- * called for each atomic on-disk change (not once per transaction batch)
- * and goes over the list
- * XXX: should be optimized?
- */
-
 /**
- * called for each RPC reported committed
+ * ptlrpc commit callback.
+ *
+ * The callback is called by PTLRPC when a RPC is reported committed by the
+ * target (OST). We register the callback for the every RPC applying a change
+ * from the llog. This way we know then the llog records can be cancelled.
+ * Notice the callback can be called when OSP is finishing. We can detect this
+ * checking that actual transno in the request is less or equal of known
+ * committed transno (see osp_sync_process_committed() for the details).
+ * XXX: this is pretty expensive and can be improved later using batching.
+ *
+ * \param[in] req      request
  */
 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
 {
        struct osp_device *d = req->rq_cb_data;
+       struct osp_job_req_args *jra;
 
-       CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
+       CDEBUG(D_HA, "commit req %p, transno %llu\n", req, req->rq_transno);
 
        if (unlikely(req->rq_transno == 0))
                return;
 
        /* do not do any opd_dyn_rpc_* accounting here
         * it's done in osp_sync_interpret sooner or later */
-
        LASSERT(d);
-       LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
-       LASSERT(list_empty(&req->rq_exp_list));
+
+       jra = ptlrpc_req_async_args(req);
+       LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
+       LASSERT(list_empty(&jra->jra_committed_link));
 
        ptlrpc_request_addref(req);
 
        spin_lock(&d->opd_syn_lock);
-       list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
+       list_add(&jra->jra_committed_link, &d->opd_syn_committed_there);
        spin_unlock(&d->opd_syn_lock);
 
        /* XXX: some batching wouldn't hurt */
        wake_up(&d->opd_syn_waitq);
 }
 
+/**
+ * RPC interpretation callback.
+ *
+ * The callback is called by ptlrpc when RPC is replied. Now we have to decide
+ * whether we should:
+ *  - put request on a special list to wait until it's committed by the target,
+ *    if the request is successful
+ *  - schedule llog record cancel if no target object is found
+ *  - try later (essentially after reboot) in case of unexpected error
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] req      request replied
+ * \param[in] aa       callback data
+ * \param[in] rc       result of RPC
+ *
+ * \retval 0           always
+ */
 static int osp_sync_interpret(const struct lu_env *env,
                              struct ptlrpc_request *req, void *aa, int rc)
 {
        struct osp_device *d = req->rq_cb_data;
+       struct osp_job_req_args *jra = aa;
 
-       if (req->rq_svc_thread != (void *) OSP_JOB_MAGIC)
-               DEBUG_REQ(D_ERROR, req, "bad magic %p\n", req->rq_svc_thread);
-       LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
+       if (jra->jra_magic != OSP_JOB_MAGIC) {
+               DEBUG_REQ(D_ERROR, req, "bad magic %u\n", jra->jra_magic);
+               LBUG();
+       }
        LASSERT(d);
 
        CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
@@ -357,12 +551,12 @@ static int osp_sync_interpret(const struct lu_env *env,
                 * but object doesn't exist anymore - cancell llog record
                 */
                LASSERT(req->rq_transno == 0);
-               LASSERT(list_empty(&req->rq_exp_list));
+               LASSERT(list_empty(&jra->jra_committed_link));
 
                ptlrpc_request_addref(req);
 
                spin_lock(&d->opd_syn_lock);
-               list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
+               list_add(&jra->jra_committed_link, &d->opd_syn_committed_there);
                spin_unlock(&d->opd_syn_lock);
 
                wake_up(&d->opd_syn_waitq);
@@ -373,17 +567,15 @@ static int osp_sync_interpret(const struct lu_env *env,
                 */
                LASSERTF(req->rq_transno == 0 ||
                         req->rq_import_generation < imp->imp_generation,
-                        "transno "LPU64", rc %d, gen: req %d, imp %d\n",
+                        "transno %llu, rc %d, gen: req %d, imp %d\n",
                         req->rq_transno, rc, req->rq_import_generation,
                         imp->imp_generation);
                if (req->rq_transno == 0) {
                        /* this is the last time we see the request
                         * if transno is not zero, then commit cb
                         * will be called at some point */
-                       LASSERT(d->opd_syn_rpc_in_progress > 0);
-                       spin_lock(&d->opd_syn_lock);
-                       d->opd_syn_rpc_in_progress--;
-                       spin_unlock(&d->opd_syn_lock);
+                       LASSERT(atomic_read(&d->opd_syn_rpc_in_progress) > 0);
+                       atomic_dec(&d->opd_syn_rpc_in_progress);
                }
 
                wake_up(&d->opd_syn_waitq);
@@ -397,15 +589,16 @@ static int osp_sync_interpret(const struct lu_env *env,
                osp_statfs_need_now(d);
        }
 
-       LASSERT(d->opd_syn_rpc_in_flight > 0);
        spin_lock(&d->opd_syn_lock);
-       d->opd_syn_rpc_in_flight--;
+       list_del_init(&jra->jra_inflight_link);
        spin_unlock(&d->opd_syn_lock);
+       LASSERT(atomic_read(&d->opd_syn_rpc_in_flight) > 0);
+       atomic_dec(&d->opd_syn_rpc_in_flight);
        if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
                wake_up(&d->opd_syn_barrier_waitq);
        CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
-              d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
-              d->opd_syn_rpc_in_progress);
+              d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
+              atomic_read(&d->opd_syn_rpc_in_progress));
 
        osp_sync_check_for_work(d);
 
@@ -413,32 +606,69 @@ static int osp_sync_interpret(const struct lu_env *env,
 }
 
 /*
- * the function walks through list of committed locally changes
- * and send them to RPC until the pipe is full
+ ** Add request to ptlrpc queue.
+ *
+ * This is just a tiny helper function to put the request on the sending list
+ *
+ * \param[in] d                OSP device
+ * \param[in] llh      llog handle where the record is stored
+ * \param[in] h                llog record
+ * \param[in] req      request
  */
 static void osp_sync_send_new_rpc(struct osp_device *d,
+                                 struct llog_handle *llh,
+                                 struct llog_rec_hdr *h,
                                  struct ptlrpc_request *req)
 {
-       LASSERT(d->opd_syn_rpc_in_flight <= d->opd_syn_max_rpc_in_flight);
-       LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
+       struct osp_job_req_args *jra;
 
-       ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
+       LASSERT(atomic_read(&d->opd_syn_rpc_in_flight) <=
+               d->opd_syn_max_rpc_in_flight);
+
+       jra = ptlrpc_req_async_args(req);
+       jra->jra_magic = OSP_JOB_MAGIC;
+       jra->jra_lcookie.lgc_lgl = llh->lgh_id;
+       jra->jra_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
+       jra->jra_lcookie.lgc_index = h->lrh_index;
+       INIT_LIST_HEAD(&jra->jra_committed_link);
+       spin_lock(&d->opd_syn_lock);
+       list_add_tail(&jra->jra_inflight_link, &d->opd_syn_inflight_list);
+       spin_unlock(&d->opd_syn_lock);
+
+       ptlrpcd_add_req(req);
 }
 
+
+/**
+ * Allocate and prepare RPC for a new change.
+ *
+ * The function allocates and initializes an RPC which will be sent soon to
+ * apply the change to the target OST. The request is initialized from the
+ * llog record passed. Notice only the fields common to all type of changes
+ * are initialized.
+ *
+ * \param[in] d                OSP device
+ * \param[in] op       type of the change
+ * \param[in] format   request format to be used
+ *
+ * \retval pointer             new request on success
+ * \retval ERR_PTR(errno)      on error
+ */
 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
-                                              struct llog_handle *llh,
-                                              struct llog_rec_hdr *h,
                                               ost_cmd_t op,
                                               const struct req_format *format)
 {
        struct ptlrpc_request   *req;
-       struct ost_body         *body;
        struct obd_import       *imp;
        int                      rc;
 
        /* Prepare the request */
        imp = d->opd_obd->u.cli.cl_import;
        LASSERT(imp);
+
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_ENOMEM))
+               RETURN(ERR_PTR(-ENOMEM));
+
        req = ptlrpc_request_alloc(imp, format);
        if (req == NULL)
                RETURN(ERR_PTR(-ENOMEM));
@@ -449,20 +679,6 @@ static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
                return ERR_PTR(rc);
        }
 
-       /*
-        * this is a trick: to save on memory allocations we put cookie
-        * into the request, but don't set corresponded flag in o_valid
-        * so that OST doesn't interpret this cookie. once the request
-        * is committed on OST we take cookie from the request and cancel
-        */
-       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-       LASSERT(body);
-       body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
-       body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
-       body->oa.o_lcookie.lgc_index = h->lrh_index;
-       INIT_LIST_HEAD(&req->rq_exp_list);
-       req->rq_svc_thread = (void *) OSP_JOB_MAGIC;
-
        req->rq_interpret_reply = osp_sync_interpret;
        req->rq_commit_cb = osp_sync_request_commit_cb;
        req->rq_cb_data = d;
@@ -472,6 +688,20 @@ static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
        return req;
 }
 
+/**
+ * Generate a request for setattr change.
+ *
+ * The function prepares a new RPC, initializes it with setattr specific
+ * bits and send the RPC.
+ *
+ * \param[in] d                OSP device
+ * \param[in] llh      llog handle where the record is stored
+ * \param[in] h                llog record
+ *
+ * \retval 0           on success
+ * \retval 1           on invalid record
+ * \retval negative    negated errno on error
+ */
 static int osp_sync_new_setattr_job(struct osp_device *d,
                                    struct llog_handle *llh,
                                    struct llog_rec_hdr *h)
@@ -483,14 +713,18 @@ static int osp_sync_new_setattr_job(struct osp_device *d,
        ENTRY;
        LASSERT(h->lrh_type == MDS_SETATTR64_REC);
 
-       /* lsr_valid can only be 0 or LA_UID/GID set */
-       if (!rec->lsr_valid && !(rec->lsr_valid & ~(LA_UID | LA_GID))) {
-               CERROR("%s: invalid setattr record, lsr_valid:"LPU64"\n",
+       if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_INVALID_REC))
+               RETURN(1);
+       /* lsr_valid can only be 0 or have OBD_MD_{FLUID,FLGID} set,
+        * so no bits other than these should be set. */
+       if ((rec->lsr_valid & ~(OBD_MD_FLUID | OBD_MD_FLGID)) != 0) {
+               CERROR("%s: invalid setattr record, lsr_valid:%llu\n",
                       d->opd_obd->obd_name, rec->lsr_valid);
-               RETURN(-EINVAL);
+               /* return 1 on invalid record */
+               RETURN(1);
        }
 
-       req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
+       req = osp_sync_new_job(d, OST_SETATTR, &RQF_OST_SETATTR);
        if (IS_ERR(req))
                RETURN(PTR_ERR(req));
 
@@ -502,20 +736,30 @@ static int osp_sync_new_setattr_job(struct osp_device *d,
        body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
        /* old setattr record (prior 2.6.0) doesn't have 'valid' stored,
         * we assume that both UID and GID are valid in that case. */
-       if (rec->lsr_valid == 0) {
+       if (rec->lsr_valid == 0)
                body->oa.o_valid |= (OBD_MD_FLUID | OBD_MD_FLGID);
-       } else {
-               if (rec->lsr_valid & LA_UID)
-                       body->oa.o_valid |= OBD_MD_FLUID;
-               if (rec->lsr_valid & LA_GID)
-                       body->oa.o_valid |= OBD_MD_FLGID;
-       }
+       else
+               body->oa.o_valid |= rec->lsr_valid;
 
-       osp_sync_send_new_rpc(d, req);
+       osp_sync_send_new_rpc(d, llh, h, req);
        RETURN(0);
 }
 
-/* Old records may be in old format, so we handle that too */
+/**
+ * Generate a request for unlink change.
+ *
+ * The function prepares a new RPC, initializes it with unlink(destroy)
+ * specific bits and sends the RPC. The function is used to handle
+ * llog_unlink_rec which were used in the older versions of Lustre.
+ * Current version uses llog_unlink_rec64.
+ *
+ * \param[in] d                OSP device
+ * \param[in] llh      llog handle where the record is stored
+ * \param[in] h                llog record
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
 static int osp_sync_new_unlink_job(struct osp_device *d,
                                   struct llog_handle *llh,
                                   struct llog_rec_hdr *h)
@@ -527,7 +771,7 @@ static int osp_sync_new_unlink_job(struct osp_device *d,
        ENTRY;
        LASSERT(h->lrh_type == MDS_UNLINK_REC);
 
-       req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
+       req = osp_sync_new_job(d, OST_DESTROY, &RQF_OST_DESTROY);
        if (IS_ERR(req))
                RETURN(PTR_ERR(req));
 
@@ -540,75 +784,29 @@ static int osp_sync_new_unlink_job(struct osp_device *d,
        if (rec->lur_count)
                body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
 
-       osp_sync_send_new_rpc(d, req);
+       osp_sync_send_new_rpc(d, llh, h, req);
        RETURN(0);
 }
 
-static int osp_prep_unlink_update_req(const struct lu_env *env,
-                                     struct osp_device *osp,
-                                     struct llog_handle *llh,
-                                     struct llog_rec_hdr *h,
-                                     struct ptlrpc_request **reqp)
-{
-       struct llog_unlink64_rec        *rec = (struct llog_unlink64_rec *)h;
-       struct dt_update_request        *update = NULL;
-       struct ptlrpc_request           *req;
-       const char                      *buf;
-       struct llog_cookie              lcookie;
-       int                             size;
-       int                             rc;
-       ENTRY;
-
-       update = out_create_update_req(&osp->opd_dt_dev);
-       if (IS_ERR(update))
-               RETURN(PTR_ERR(update));
-
-       /* This can only happens for unlink slave directory, so decrease
-        * ref for ".." and "." */
-       rc = out_insert_update(env, update, OUT_REF_DEL, &rec->lur_fid, 0,
-                              NULL, NULL);
-       if (rc != 0)
-               GOTO(out, rc);
-
-       rc = out_insert_update(env, update, OUT_REF_DEL, &rec->lur_fid, 0,
-                              NULL, NULL);
-       if (rc != 0)
-               GOTO(out, rc);
-
-       lcookie.lgc_lgl = llh->lgh_id;
-       lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
-       lcookie.lgc_index = h->lrh_index;
-       size = sizeof(lcookie);
-       buf = (const char *)&lcookie;
-
-       rc = out_insert_update(env, update, OUT_DESTROY, &rec->lur_fid, 1,
-                              &size, &buf);
-       if (rc != 0)
-               GOTO(out, rc);
-
-       rc = out_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
-                                update->dur_req, &req);
-       if (rc != 0)
-               GOTO(out, rc);
-
-       INIT_LIST_HEAD(&req->rq_exp_list);
-       req->rq_svc_thread = (void *)OSP_JOB_MAGIC;
-
-       req->rq_interpret_reply = osp_sync_interpret;
-       req->rq_commit_cb = osp_sync_request_commit_cb;
-       req->rq_cb_data = osp;
-
-       ptlrpc_request_set_replen(req);
-       *reqp = req;
-out:
-       if (update != NULL)
-               out_destroy_update_req(update);
-
-       RETURN(rc);
-}
-
-static int osp_sync_new_unlink64_job(const struct lu_env *env,
-                                    struct osp_device *d,
+/**
+ * Generate a request for unlink change.
+ *
+ * The function prepares a new RPC, initializes it with unlink(destroy)
+ * specific bits and sends the RPC. Depending on the target (MDT or OST)
+ * two different protocols are used. For MDT we use OUT (basically OSD API
+ * updates transferred via a network). For OST we still use the old
+ * protocol (OBD?), originally for compatibility. Later we can start to
+ * use OUT for OST as well, this will allow batching and better code
+ * unification.
+ *
+ * \param[in] d                OSP device
+ * \param[in] llh      llog handle where the record is stored
+ * \param[in] h                llog record
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
+static int osp_sync_new_unlink64_job(struct osp_device *d,
                                     struct llog_handle *llh,
                                     struct llog_rec_hdr *h)
 {
@@ -619,39 +817,50 @@ static int osp_sync_new_unlink64_job(const struct lu_env *env,
 
        ENTRY;
        LASSERT(h->lrh_type == MDS_UNLINK64_REC);
+       req = osp_sync_new_job(d, OST_DESTROY, &RQF_OST_DESTROY);
+       if (IS_ERR(req))
+               RETURN(PTR_ERR(req));
 
-       if (d->opd_connect_mdt) {
-               rc = osp_prep_unlink_update_req(env, d, llh, h, &req);
-               if (rc != 0)
-                       RETURN(rc);
-       } else {
-               req = osp_sync_new_job(d, llh, h, OST_DESTROY,
-                                      &RQF_OST_DESTROY);
-               if (IS_ERR(req))
-                       RETURN(PTR_ERR(req));
-
-               body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
-               if (body == NULL)
-                       RETURN(-EFAULT);
-               rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
-               if (rc < 0)
-                       RETURN(rc);
-               body->oa.o_misc = rec->lur_count;
-               body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
-                                  OBD_MD_FLOBJCOUNT;
-       }
-       osp_sync_send_new_rpc(d, req);
+       body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
+       if (body == NULL)
+               RETURN(-EFAULT);
+       rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
+       if (rc < 0)
+               RETURN(rc);
+       body->oa.o_misc = rec->lur_count;
+       body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
+                          OBD_MD_FLOBJCOUNT;
+       osp_sync_send_new_rpc(d, llh, h, req);
        RETURN(0);
 }
 
-static int osp_sync_process_record(const struct lu_env *env,
-                                  struct osp_device *d,
-                                  struct llog_handle *llh,
-                                  struct llog_rec_hdr *rec)
+/**
+ * Process llog records.
+ *
+ * This function is called to process the llog records committed locally.
+ * In the recovery model used by OSP we can apply a change to a remote
+ * target once corresponding transaction (like posix unlink) is committed
+ * locally so can't revert.
+ * Depending on the llog record type, a given handler is called that is
+ * responsible for preparing and sending the RPC to apply the change.
+ * Special record type LLOG_GEN_REC marking a reboot is cancelled right away.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ * \param[in] llh      llog handle where the record is stored
+ * \param[in] rec      llog record
+ */
+static void osp_sync_process_record(const struct lu_env *env,
+                                   struct osp_device *d,
+                                   struct llog_handle *llh,
+                                   struct llog_rec_hdr *rec)
 {
+       struct llog_handle      *cathandle = llh->u.phd.phd_cat_handle;
        struct llog_cookie       cookie;
        int                      rc = 0;
 
+       ENTRY;
+
        cookie.lgc_lgl = llh->lgh_id;
        cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
        cookie.lgc_index = rec->lrh_index;
@@ -668,10 +877,9 @@ static int osp_sync_process_record(const struct lu_env *env,
                }
 
                /* cancel any generation record */
-               rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
-                                            1, &cookie);
+               rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
 
-               return rc;
+               RETURN_EXIT;
        }
 
        /*
@@ -681,10 +889,8 @@ static int osp_sync_process_record(const struct lu_env *env,
 
        /* notice we increment counters before sending RPC, to be consistent
         * in RPC interpret callback which may happen very quickly */
-       spin_lock(&d->opd_syn_lock);
-       d->opd_syn_rpc_in_flight++;
-       d->opd_syn_rpc_in_progress++;
-       spin_unlock(&d->opd_syn_lock);
+       atomic_inc(&d->opd_syn_rpc_in_flight);
+       atomic_inc(&d->opd_syn_rpc_in_progress);
 
        switch (rec->lrh_type) {
        /* case MDS_UNLINK_REC is kept for compatibility */
@@ -692,7 +898,7 @@ static int osp_sync_process_record(const struct lu_env *env,
                rc = osp_sync_new_unlink_job(d, llh, rec);
                break;
        case MDS_UNLINK64_REC:
-               rc = osp_sync_new_unlink64_job(env, d, llh, rec);
+               rc = osp_sync_new_unlink64_job(d, llh, rec);
                break;
        case MDS_SETATTR64_REC:
                rc = osp_sync_new_setattr_job(d, llh, rec);
@@ -700,49 +906,75 @@ static int osp_sync_process_record(const struct lu_env *env,
        default:
                CERROR("%s: unknown record type: %x\n", d->opd_obd->obd_name,
                       rec->lrh_type);
-               /* we should continue processing */
-               return 0;
+               /* treat "unknown record type" as "invalid" */
+               rc = 1;
+               break;
        }
 
-       if (likely(rc == 0)) {
-               spin_lock(&d->opd_syn_lock);
-               if (d->opd_syn_prev_done) {
-                       LASSERT(d->opd_syn_changes > 0);
-                       LASSERT(rec->lrh_id <= d->opd_syn_last_committed_id);
-                       /*
-                        * NOTE: it's possible to meet same id if
-                        * OST stores few stripes of same file
-                        */
-                       if (rec->lrh_id > d->opd_syn_last_processed_id) {
-                               d->opd_syn_last_processed_id = rec->lrh_id;
+       /* For all kinds of records, not matter successful or not,
+        * we should decrease changes and bump last_processed_id.
+        */
+       if (d->opd_syn_prev_done) {
+               __u64 correct_id = osp_sync_correct_id(d, rec);
+               LASSERT(atomic_read(&d->opd_syn_changes) > 0);
+               LASSERT(correct_id <= d->opd_syn_last_committed_id);
+               /* NOTE: it's possible to meet same id if
+                * OST stores few stripes of same file
+                */
+               while (1) {
+                       /* another thread may be trying to set new value */
+                       rmb();
+                       if (correct_id > d->opd_syn_last_processed_id) {
+                               d->opd_syn_last_processed_id = correct_id;
                                wake_up(&d->opd_syn_barrier_waitq);
-                       }
-
-                       d->opd_syn_changes--;
+                       } else
+                               break;
                }
-               CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
-                      d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
-                      d->opd_syn_rpc_in_progress);
-               spin_unlock(&d->opd_syn_lock);
-       } else {
-               spin_lock(&d->opd_syn_lock);
-               d->opd_syn_rpc_in_flight--;
-               d->opd_syn_rpc_in_progress--;
-               spin_unlock(&d->opd_syn_lock);
+               atomic_dec(&d->opd_syn_changes);
+       }
+       if (rc != 0) {
+               atomic_dec(&d->opd_syn_rpc_in_flight);
+               atomic_dec(&d->opd_syn_rpc_in_progress);
        }
 
-       CDEBUG(D_HA, "found record %x, %d, idx %u, id %u: %d\n",
-              rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id, rc);
-       return rc;
+       CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
+              d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
+              atomic_read(&d->opd_syn_rpc_in_progress));
+
+       /* Delete the invalid record */
+       if (rc == 1) {
+               rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
+               if (rc != 0)
+                       CERROR("%s: can't delete invalid record: "
+                              "fid = "DFID", rec_id = %u, rc = %d\n",
+                              d->opd_obd->obd_name,
+                              PFID(lu_object_fid(&cathandle->lgh_obj->do_lu)),
+                              rec->lrh_id, rc);
+       }
+
+       CDEBUG(D_HA, "found record %x, %d, idx %u, id %u\n",
+              rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id);
+
+       RETURN_EXIT;
 }
 
+/**
+ * Cancel llog records for the committed changes.
+ *
+ * The function walks through the list of the committed RPCs and cancels
+ * corresponding llog records. see osp_sync_request_commit_cb() for the
+ * details.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ */
 static void osp_sync_process_committed(const struct lu_env *env,
                                       struct osp_device *d)
 {
        struct obd_device       *obd = d->opd_obd;
        struct obd_import       *imp = obd->u.cli.cl_import;
        struct ost_body         *body;
-       struct ptlrpc_request   *req, *tmp;
+       struct ptlrpc_request   *req;
        struct llog_ctxt        *ctxt;
        struct llog_handle      *llh;
        struct list_head         list;
@@ -781,57 +1013,42 @@ static void osp_sync_process_committed(const struct lu_env *env,
        INIT_LIST_HEAD(&d->opd_syn_committed_there);
        spin_unlock(&d->opd_syn_lock);
 
-       list_for_each_entry_safe(req, tmp, &list, rq_exp_list) {
-               struct llog_cookie *lcookie = NULL;
-
-               LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
-               list_del_init(&req->rq_exp_list);
-
-               if (d->opd_connect_mdt) {
-                       struct object_update_request *ureq;
-                       struct object_update *update;
-                       ureq = req_capsule_client_get(&req->rq_pill,
-                                                     &RMF_OUT_UPDATE);
-                       LASSERT(ureq != NULL &&
-                               ureq->ourq_magic == UPDATE_REQUEST_MAGIC);
-
-                       /* 1st/2nd is for decref . and .., 3rd one is for
-                        * destroy, where the log cookie is stored.
-                        * See osp_prep_unlink_update_req */
-                       update = object_update_request_get(ureq, 2, NULL);
-                       LASSERT(update != NULL);
-                       lcookie = object_update_param_get(update, 0, NULL);
-                       LASSERT(lcookie != NULL);
-               } else {
-                       body = req_capsule_client_get(&req->rq_pill,
-                                                     &RMF_OST_BODY);
-                       LASSERT(body);
-                       lcookie = &body->oa.o_lcookie;
-               }
+       while (!list_empty(&list)) {
+               struct osp_job_req_args *jra;
+
+               jra = list_entry(list.next, struct osp_job_req_args,
+                                jra_committed_link);
+               LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
+               list_del_init(&jra->jra_committed_link);
+
+               req = container_of((void *)jra, struct ptlrpc_request,
+                                  rq_async_args);
+               body = req_capsule_client_get(&req->rq_pill,
+                                             &RMF_OST_BODY);
+               LASSERT(body);
                /* import can be closing, thus all commit cb's are
                 * called we can check committness directly */
-               if (req->rq_transno <= imp->imp_peer_committed_transno) {
-                       rc = llog_cat_cancel_records(env, llh, 1, lcookie);
+               if (req->rq_import_generation == imp->imp_generation) {
+                       rc = llog_cat_cancel_records(env, llh, 1,
+                                                    &jra->jra_lcookie);
                        if (rc)
                                CERROR("%s: can't cancel record: %d\n",
                                       obd->obd_name, rc);
                } else {
-                       DEBUG_REQ(D_HA, req, "not committed");
+                       DEBUG_REQ(D_OTHER, req, "imp_committed = %llu",
+                                 imp->imp_peer_committed_transno);
                }
-
                ptlrpc_req_finished(req);
                done++;
        }
 
        llog_ctxt_put(ctxt);
 
-       LASSERT(d->opd_syn_rpc_in_progress >= done);
-       spin_lock(&d->opd_syn_lock);
-       d->opd_syn_rpc_in_progress -= done;
-       spin_unlock(&d->opd_syn_lock);
+       LASSERT(atomic_read(&d->opd_syn_rpc_in_progress) >= done);
+       atomic_sub(done, &d->opd_syn_rpc_in_progress);
        CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
-              d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
-              d->opd_syn_rpc_in_progress);
+              d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
+              atomic_read(&d->opd_syn_rpc_in_progress));
 
        osp_sync_check_for_work(d);
 
@@ -843,8 +1060,21 @@ static void osp_sync_process_committed(const struct lu_env *env,
        EXIT;
 }
 
-/*
- * this is where most of queues processing happens
+/**
+ * The core of the syncing mechanism.
+ *
+ * This is a callback called by the llog processing function. Essentially it
+ * suspends llog processing until there is a record to process (it's supposed
+ * to be committed locally). The function handles RPCs committed by the target
+ * and cancels corresponding llog records.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] llh      llog handle we're processing
+ * \param[in] rec      current llog record
+ * \param[in] data     callback data containing a pointer to the device
+ *
+ * \retval 0                   to ask the caller (llog_process()) to continue
+ * \retval LLOG_PROC_BREAK     to ask the caller to break
  */
 static int osp_sync_process_queues(const struct lu_env *env,
                                   struct llog_handle *llh,
@@ -852,7 +1082,6 @@ static int osp_sync_process_queues(const struct lu_env *env,
                                   void *data)
 {
        struct osp_device       *d = data;
-       int                      rc;
 
        do {
                struct l_wait_info lwi = { 0 };
@@ -870,33 +1099,14 @@ static int osp_sync_process_queues(const struct lu_env *env,
                if (osp_sync_can_process_new(d, rec)) {
                        if (llh == NULL) {
                                /* ask llog for another record */
-                               CDEBUG(D_HA, "%lu changes, %u in progress, %u in flight\n",
-                                      d->opd_syn_changes,
-                                      d->opd_syn_rpc_in_progress,
-                                      d->opd_syn_rpc_in_flight);
+                               CDEBUG(D_HA, "%u changes, %u in progress,"
+                                      " %u in flight\n",
+                                      atomic_read(&d->opd_syn_changes),
+                                      atomic_read(&d->opd_syn_rpc_in_progress),
+                                      atomic_read(&d->opd_syn_rpc_in_flight));
                                return 0;
                        }
-
-                       /*
-                        * try to send, in case of disconnection, suspend
-                        * processing till we can send this request
-                        */
-                       do {
-                               rc = osp_sync_process_record(env, d, llh, rec);
-                               /*
-                                * XXX: probably different handling is needed
-                                * for some bugs, like immediate exit or if
-                                * OSP gets inactive
-                                */
-                               if (rc) {
-                                       CERROR("can't send: %d\n", rc);
-                                       l_wait_event(d->opd_syn_waitq,
-                                                    !osp_sync_running(d) ||
-                                                    osp_sync_has_work(d),
-                                                    &lwi);
-                               }
-                       } while (rc != 0 && osp_sync_running(d));
-
+                       osp_sync_process_record(env, d, llh, rec);
                        llh = NULL;
                        rec = NULL;
                }
@@ -912,8 +1122,10 @@ static int osp_sync_process_queues(const struct lu_env *env,
        } while (1);
 }
 
-/*
- * this thread runs llog_cat_process() scanner calling our callback
+/**
+ * OSP sync thread.
+ *
+ * This thread runs llog_cat_process() scanner calling our callback
  * to process llog records. in the callback we implement tricky
  * state machine as we don't want to start scanning of the llog again
  * and again, also we don't want to process too many records and send
@@ -921,9 +1133,14 @@ static int osp_sync_process_queues(const struct lu_env *env,
  * being synced to OST) the callback can suspend awaiting for some
  * new conditions, like syncs completed.
  *
- * in order to process llog records left by previous boots and to allow
+ * In order to process llog records left by previous boots and to allow
  * llog_process_thread() to find something (otherwise it'd just exit
  * immediately) we add a special GENERATATION record on each boot.
+ *
+ * \param[in] _arg     a pointer to thread's arguments
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
  */
 static int osp_sync_thread(void *_arg)
 {
@@ -942,6 +1159,12 @@ static int osp_sync_thread(void *_arg)
        if (rc) {
                CERROR("%s: can't initialize env: rc = %d\n",
                       obd->obd_name, rc);
+
+               spin_lock(&d->opd_syn_lock);
+               thread->t_flags = SVC_STOPPED;
+               spin_unlock(&d->opd_syn_lock);
+               wake_up(&thread->t_ctl_waitq);
+
                RETURN(rc);
        }
 
@@ -964,44 +1187,53 @@ static int osp_sync_thread(void *_arg)
        }
 
        rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
+       if (rc < 0) {
+               CERROR("%s: llog process with osp_sync_process_queues "
+                      "failed: %d\n", d->opd_obd->obd_name, rc);
+               GOTO(close, rc);
+       }
        LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
-                "%lu changes, %u in progress, %u in flight: %d\n",
-                d->opd_syn_changes, d->opd_syn_rpc_in_progress,
-                d->opd_syn_rpc_in_flight, rc);
+                "%u changes, %u in progress, %u in flight: %d\n",
+                atomic_read(&d->opd_syn_changes),
+                atomic_read(&d->opd_syn_rpc_in_progress),
+                atomic_read(&d->opd_syn_rpc_in_flight), rc);
 
        /* we don't expect llog_process_thread() to exit till umount */
        LASSERTF(thread->t_flags != SVC_RUNNING,
-                "%lu changes, %u in progress, %u in flight\n",
-                d->opd_syn_changes, d->opd_syn_rpc_in_progress,
-                d->opd_syn_rpc_in_flight);
+                "%u changes, %u in progress, %u in flight\n",
+                atomic_read(&d->opd_syn_changes),
+                atomic_read(&d->opd_syn_rpc_in_progress),
+                atomic_read(&d->opd_syn_rpc_in_flight));
 
        /* wait till all the requests are completed */
        count = 0;
-       while (d->opd_syn_rpc_in_progress > 0) {
+       while (atomic_read(&d->opd_syn_rpc_in_progress) > 0) {
                osp_sync_process_committed(&env, d);
 
                lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
                rc = l_wait_event(d->opd_syn_waitq,
-                                 d->opd_syn_rpc_in_progress == 0,
+                                 atomic_read(&d->opd_syn_rpc_in_progress) == 0,
                                  &lwi);
                if (rc == -ETIMEDOUT)
                        count++;
                LASSERTF(count < 10, "%s: %d %d %sempty\n",
-                        d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
-                        d->opd_syn_rpc_in_flight,
+                        d->opd_obd->obd_name,
+                        atomic_read(&d->opd_syn_rpc_in_progress),
+                        atomic_read(&d->opd_syn_rpc_in_flight),
                         list_empty(&d->opd_syn_committed_there) ? "" : "!");
 
        }
 
+close:
        llog_cat_close(&env, llh);
        rc = llog_cleanup(&env, ctxt);
        if (rc)
                CERROR("can't cleanup llog: %d\n", rc);
 out:
-       LASSERTF(d->opd_syn_rpc_in_progress == 0,
+       LASSERTF(atomic_read(&d->opd_syn_rpc_in_progress) == 0,
                 "%s: %d %d %sempty\n",
-                d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
-                d->opd_syn_rpc_in_flight,
+                d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_progress),
+                atomic_read(&d->opd_syn_rpc_in_flight),
                 list_empty(&d->opd_syn_committed_there) ? "" : "!");
 
        thread->t_flags = SVC_STOPPED;
@@ -1013,6 +1245,20 @@ out:
        RETURN(0);
 }
 
+/**
+ * Initialize llog.
+ *
+ * Initializes the llog. Specific llog to be used depends on the type of the
+ * target OSP represents (OST or MDT). The function adds appends a new llog
+ * record to mark the place where the records associated with this boot
+ * start.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
 {
        struct osp_thread_info  *osi = osp_env_info(env);
@@ -1032,17 +1278,23 @@ static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
        OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
        obd->obd_lvfs_ctxt.dt = d->opd_storage;
 
-       if (d->opd_connect_mdt)
-               lu_local_obj_fid(fid, SLAVE_LLOG_CATALOGS_OID);
-       else
-               lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
+       lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
 
        rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
                                   &osi->osi_cid, fid);
-       if (rc) {
-               CERROR("%s: can't get id from catalogs: rc = %d\n",
-                      obd->obd_name, rc);
-               RETURN(rc);
+       if (rc < 0) {
+               if (rc != -EFAULT) {
+                       CERROR("%s: can't get id from catalogs: rc = %d\n",
+                              obd->obd_name, rc);
+                       RETURN(rc);
+               }
+
+               /* After sparse OST indices is supported, the CATALOG file
+                * may become a sparse file that results in failure on
+                * reading. Skip this error as the llog will be created
+                * later */
+               memset(&osi->osi_cid, 0, sizeof(osi->osi_cid));
+               rc = 0;
        }
 
        CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
@@ -1050,7 +1302,8 @@ static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
               POSTID(&osi->osi_cid.lci_logid.lgl_oi),
               osi->osi_cid.lci_logid.lgl_ogen);
 
-       rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, obd,
+       rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT,
+                       d->opd_storage->dd_lu_dev.ld_obd,
                        &osp_mds_ost_orig_logops);
        if (rc)
                RETURN(rc);
@@ -1078,7 +1331,7 @@ static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
        LASSERT(lgh != NULL);
        ctxt->loc_handle = lgh;
 
-       rc = llog_cat_init_and_process(env, lgh);
+       rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
        if (rc)
                GOTO(out_close, rc);
 
@@ -1112,18 +1365,37 @@ out_cleanup:
        RETURN(rc);
 }
 
+/**
+ * Cleanup llog used for syncing.
+ *
+ * Closes and cleanups the llog. The function is called when the device is
+ * shutting down.
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ */
 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
 {
        struct llog_ctxt *ctxt;
 
        ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
-       if (ctxt != NULL)
+       if (ctxt) {
                llog_cat_close(env, ctxt->loc_handle);
-       llog_cleanup(env, ctxt);
+               llog_cleanup(env, ctxt);
+       }
 }
 
-/*
- * initializes sync component of OSP
+/**
+ * Initialization of the sync component of OSP.
+ *
+ * Initializes the llog and starts a new thread to handle the changes to
+ * the remote target (OST or MDT).
+ *
+ * \param[in] env      LU environment provided by the caller
+ * \param[in] d                OSP device
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
  */
 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
 {
@@ -1133,6 +1405,19 @@ int osp_sync_init(const struct lu_env *env, struct osp_device *d)
 
        ENTRY;
 
+       d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
+       d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
+       spin_lock_init(&d->opd_syn_lock);
+       init_waitqueue_head(&d->opd_syn_waitq);
+       init_waitqueue_head(&d->opd_syn_barrier_waitq);
+       thread_set_flags(&d->opd_syn_thread, SVC_INIT);
+       init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
+       INIT_LIST_HEAD(&d->opd_syn_inflight_list);
+       INIT_LIST_HEAD(&d->opd_syn_committed_there);
+
+       if (d->opd_storage->dd_rdonly)
+               RETURN(0);
+
        rc = osp_sync_id_traction_init(d);
        if (rc)
                RETURN(rc);
@@ -1150,14 +1435,6 @@ int osp_sync_init(const struct lu_env *env, struct osp_device *d)
        /*
         * Start synchronization thread
         */
-       d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
-       d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
-       spin_lock_init(&d->opd_syn_lock);
-       init_waitqueue_head(&d->opd_syn_waitq);
-       init_waitqueue_head(&d->opd_syn_barrier_waitq);
-       init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
-       INIT_LIST_HEAD(&d->opd_syn_committed_there);
-
        task = kthread_run(osp_sync_thread, d, "osp-syn-%u-%u",
                           d->opd_index, d->opd_group);
        if (IS_ERR(task)) {
@@ -1178,15 +1455,26 @@ err_id:
        return rc;
 }
 
+/**
+ * Stop the syncing thread.
+ *
+ * Asks the syncing thread to stop and wait until it's stopped.
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval             0
+ */
 int osp_sync_fini(struct osp_device *d)
 {
        struct ptlrpc_thread *thread = &d->opd_syn_thread;
 
        ENTRY;
 
-       thread->t_flags = SVC_STOPPING;
-       wake_up(&d->opd_syn_waitq);
-       wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
+       if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
+               thread->t_flags = SVC_STOPPING;
+               wake_up(&d->opd_syn_waitq);
+               wait_event(thread->t_ctl_waitq, thread_is_stopped(thread));
+       }
 
        /*
         * unregister transaction callbacks only when sync thread
@@ -1201,6 +1489,15 @@ static DEFINE_MUTEX(osp_id_tracker_sem);
 static struct list_head osp_id_tracker_list =
                LIST_HEAD_INIT(osp_id_tracker_list);
 
+/**
+ * OSD commit callback.
+ *
+ * The function is used as a local OSD commit callback to track the highest
+ * committed llog record id. see osp_sync_id_traction_init() for the details.
+ *
+ * \param[in] th       local transaction handle committed
+ * \param[in] cookie   commit callback data (our private structure)
+ */
 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
 {
        struct osp_id_tracker   *tr = cookie;
@@ -1215,7 +1512,7 @@ static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
 
        spin_lock(&tr->otr_lock);
        if (likely(txn->oti_current_id > tr->otr_committed_id)) {
-               CDEBUG(D_OTHER, "committed: %u -> %u\n",
+               CDEBUG(D_OTHER, "committed: %llu -> %llu\n",
                       tr->otr_committed_id, txn->oti_current_id);
                tr->otr_committed_id = txn->oti_current_id;
 
@@ -1228,6 +1525,25 @@ static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
        spin_unlock(&tr->otr_lock);
 }
 
+/**
+ * Initialize commit tracking mechanism.
+ *
+ * Some setups may have thousands of OSTs and each will be represented by OSP.
+ * Meaning order of magnitute many more changes to apply every second. In order
+ * to keep the number of commit callbacks low this mechanism was introduced.
+ * The mechanism is very similar to transno used by MDT service: it's an single
+ * ID stream which can be assigned by any OSP to its llog records. The tricky
+ * part is that ID is stored in per-transaction data and re-used by all the OSPs
+ * involved in that transaction. Then all these OSPs are woken up utilizing a single OSD commit callback.
+ *
+ * The function initializes the data used by the tracker described above.
+ * A singler tracker per OSD device is created.
+ *
+ * \param[in] d                OSP device
+ *
+ * \retval 0           on success
+ * \retval negative    negated errno on error
+ */
 static int osp_sync_id_traction_init(struct osp_device *d)
 {
        struct osp_id_tracker   *tr, *found = NULL;
@@ -1274,6 +1590,14 @@ static int osp_sync_id_traction_init(struct osp_device *d)
        return rc;
 }
 
+/**
+ * Release commit tracker.
+ *
+ * Decrease a refcounter on the tracker used by the given OSP device \a d.
+ * If no more users left, then the tracker is released.
+ *
+ * \param[in] d                OSP device
+ */
 static void osp_sync_id_traction_fini(struct osp_device *d)
 {
        struct osp_id_tracker *tr;
@@ -1302,10 +1626,20 @@ static void osp_sync_id_traction_fini(struct osp_device *d)
        EXIT;
 }
 
-/*
- * generates id for the tracker
+/**
+ * Generate a new ID on a tracker.
+ *
+ * Generates a new ID using the tracker associated with the given OSP device
+ * \a d, if the given ID \a id is non-zero. Unconditially adds OSP device to
+ * the wakeup list, so OSP won't miss when a transaction using the ID is
+ * committed.
+ *
+ * \param[in] d                OSP device
+ * \param[in] id       0 or ID generated previously
+ *
+ * \retval             ID the caller should use
  */
-static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
+static __u64 osp_sync_id_get(struct osp_device *d, __u64 id)
 {
        struct osp_id_tracker *tr;
 
@@ -1314,9 +1648,12 @@ static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
 
        /* XXX: we can improve this introducing per-cpu preallocated ids? */
        spin_lock(&tr->otr_lock);
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_TRACK_OVERFLOW))
+               tr->otr_next_id = 0xfffffff0;
+
        if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
                spin_unlock(&tr->otr_lock);
-               CERROR("%s: next %u, last synced %lu\n",
+               CERROR("%s: next %llu, last synced %llu\n",
                       d->opd_obd->obd_name, tr->otr_next_id,
                       d->opd_syn_last_used_id);
                LBUG();
@@ -1329,11 +1666,20 @@ static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
        if (list_empty(&d->opd_syn_ontrack))
                list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
        spin_unlock(&tr->otr_lock);
-       CDEBUG(D_OTHER, "new id %u\n", (unsigned) id);
+       CDEBUG(D_OTHER, "new id %llu\n", id);
 
        return id;
 }
 
+/**
+ * Stop to propagate commit status to OSP.
+ *
+ * If the OSP does not have any llog records she's waiting to commit, then
+ * it is possible to unsubscribe from wakeups from the tracking using this
+ * method.
+ *
+ * \param[in] d                OSP device not willing to wakeup
+ */
 static void osp_sync_remove_from_tracker(struct osp_device *d)
 {
        struct osp_id_tracker *tr;