4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_sync.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/kthread.h>
47 #include <lustre_log.h>
48 #include <lustre_update.h>
49 #include "osp_internal.h"
51 static int osp_sync_id_traction_init(struct osp_device *d);
52 static void osp_sync_id_traction_fini(struct osp_device *d);
53 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id);
54 static void osp_sync_remove_from_tracker(struct osp_device *d);
57 * this is a components of OSP implementing synchronization between MDS and OST
58 * it llogs all interesting changes (currently it's uig/gid change and object
59 * destroy) atomically, then makes sure changes hit OST storage
61 * we have 4 queues of work:
63 * the first queue is llog itself, once read a change is stored in 2nd queue
64 * in form of RPC (but RPC isn't fired yet).
66 * the second queue (opd_syn_waiting_for_commit) holds changes awaiting local
67 * commit. once change is committed locally it migrates onto 3rd queue.
69 * the third queue (opd_syn_committed_here) holds changes committed locally,
70 * but not sent to OST (as the pipe can be full). once pipe becomes non-full
71 * we take a change from the queue and fire corresponded RPC.
73 * once RPC is reported committed by OST (using regular last_committed mech.)
74 * the change jumps into 4th queue (opd_syn_committed_there), now we can
75 * cancel corresponded llog record and release RPC
77 * opd_syn_changes is a number of unread llog records (to be processed).
78 * notice this number doesn't include llog records from previous boots.
79 * with OSP_SYN_THRESHOLD we try to batch processing a bit (TO BE IMPLEMENTED)
81 * opd_syn_rpc_in_progress is a number of requests in 2-4 queues.
82 * we control this with OSP_MAX_IN_PROGRESS so that OSP don't consume
83 * too much memory -- how to deal with 1000th OSTs ? batching could help?
85 * opd_syn_rpc_in_flight is a number of RPC in flight.
86 * we control this with OSP_MAX_IN_FLIGHT
89 /* XXX: do math to learn reasonable threshold
90 * should it be ~ number of changes fitting bulk? */
92 #define OSP_SYN_THRESHOLD 10
93 #define OSP_MAX_IN_FLIGHT 8
94 #define OSP_MAX_IN_PROGRESS 4096
96 #define OSP_JOB_MAGIC 0x26112005
98 struct osp_job_req_args {
99 /** bytes reserved for ptlrpc_replay_req() */
100 struct ptlrpc_replay_async_args jra_raa;
101 struct list_head jra_link;
105 static inline int osp_sync_running(struct osp_device *d)
107 return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
111 * Check status: whether OSP thread has stopped
113 * \param[in] d OSP device
115 * \retval 0 still running
118 static inline int osp_sync_stopped(struct osp_device *d)
120 return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
124 ** Check for new changes to sync
126 * \param[in] d OSP device
128 * \retval 1 there are changes
129 * \retval 0 there are no changes
131 static inline int osp_sync_has_new_job(struct osp_device *d)
133 return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
134 (d->opd_syn_last_processed_id < d->opd_syn_last_committed_id))
135 || (d->opd_syn_prev_done == 0);
138 static inline int osp_sync_low_in_progress(struct osp_device *d)
140 return d->opd_syn_rpc_in_progress < d->opd_syn_max_rpc_in_progress;
144 * Check for room in the network pipe to OST
146 * \param[in] d OSP device
148 * \retval 1 there is room
149 * \retval 0 no room, the pipe is full
151 static inline int osp_sync_low_in_flight(struct osp_device *d)
153 return d->opd_syn_rpc_in_flight < d->opd_syn_max_rpc_in_flight;
157 * Wake up check for the main sync thread
159 * \param[in] d OSP device
161 * \retval 1 time to wake up
162 * \retval 0 no need to wake up
164 static inline int osp_sync_has_work(struct osp_device *d)
166 /* has new/old changes and low in-progress? */
167 if (osp_sync_has_new_job(d) && osp_sync_low_in_progress(d) &&
168 osp_sync_low_in_flight(d) && d->opd_imp_connected)
171 /* has remotely committed? */
172 if (!list_empty(&d->opd_syn_committed_there))
178 #define osp_sync_check_for_work(d) \
180 if (osp_sync_has_work(d)) { \
181 wake_up(&d->opd_syn_waitq); \
185 void __osp_sync_check_for_work(struct osp_device *d)
187 osp_sync_check_for_work(d);
191 * Check and return ready-for-new status.
193 * The thread processing llog record uses this function to check whether
194 * it's time to take another record and process it. The number of conditions
195 * must be met: the connection should be ready, RPCs in flight not exceeding
196 * the limit, the record is committed locally, etc (see the lines below).
198 * \param[in] d OSP device
199 * \param[in] rec next llog record to process
201 * \retval 0 not ready
204 static inline int osp_sync_can_process_new(struct osp_device *d,
205 struct llog_rec_hdr *rec)
209 if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
211 if (!osp_sync_low_in_progress(d))
213 if (!osp_sync_low_in_flight(d))
215 if (!d->opd_imp_connected)
217 if (d->opd_syn_prev_done == 0)
219 if (d->opd_syn_changes == 0)
221 if (rec == NULL || rec->lrh_id <= d->opd_syn_last_committed_id)
227 * Declare intention to add a new change.
229 * With regard to OSD API, we have to declare any changes ahead. In this
230 * case we declare an intention to add a llog record representing the
231 * change on the local storage.
233 * \param[in] env LU environment provided by the caller
234 * \param[in] o OSP object
235 * \param[in] type type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
236 * \param[in] th transaction handle (local)
238 * \retval 0 on success
239 * \retval negative negated errno on error
241 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
242 llog_op_type type, struct thandle *th)
244 struct osp_thread_info *osi = osp_env_info(env);
245 struct osp_device *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
246 struct llog_ctxt *ctxt;
247 struct thandle *storage_th;
252 /* it's a layering violation, to access internals of th,
253 * but we can do this as a sanity check, for a while */
254 LASSERT(th->th_top != NULL);
255 storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
256 if (IS_ERR(storage_th))
257 RETURN(PTR_ERR(storage_th));
260 case MDS_UNLINK64_REC:
261 osi->osi_hdr.lrh_len = sizeof(struct llog_unlink64_rec);
263 case MDS_SETATTR64_REC:
264 osi->osi_hdr.lrh_len = sizeof(struct llog_setattr64_rec);
270 /* we want ->dt_trans_start() to allocate per-thandle structure */
271 storage_th->th_tags |= LCT_OSP_THREAD;
273 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
276 rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr,
284 * Generate a llog record for a given change.
286 * Generates a llog record for the change passed. The change can be of two
287 * types: unlink and setattr. The record gets an ID which later will be
288 * used to track commit status of the change. For unlink changes, the caller
289 * can supply a starting FID and the count of the objects to destroy. For
290 * setattr the caller should apply attributes to apply.
293 * \param[in] env LU environment provided by the caller
294 * \param[in] d OSP device
295 * \param[in] fid fid of the object the change should be applied to
296 * \param[in] type type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
297 * \param[in] count count of objects to destroy
298 * \param[in] th transaction handle (local)
299 * \param[in] attr attributes for setattr
301 * \retval 0 on success
302 * \retval negative negated errno on error
304 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
305 const struct lu_fid *fid, llog_op_type type,
306 int count, struct thandle *th,
307 const struct lu_attr *attr)
309 struct osp_thread_info *osi = osp_env_info(env);
310 struct llog_ctxt *ctxt;
311 struct osp_txn_info *txn;
312 struct thandle *storage_th;
317 /* it's a layering violation, to access internals of th,
318 * but we can do this as a sanity check, for a while */
319 LASSERT(th->th_top != NULL);
320 storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
321 if (IS_ERR(storage_th))
322 RETURN(PTR_ERR(storage_th));
325 case MDS_UNLINK64_REC:
326 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
327 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
328 osi->osi_unlink.lur_fid = *fid;
329 osi->osi_unlink.lur_count = count;
331 case MDS_SETATTR64_REC:
332 rc = fid_to_ostid(fid, &osi->osi_oi);
334 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
335 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
336 osi->osi_setattr.lsr_oi = osi->osi_oi;
338 osi->osi_setattr.lsr_uid = attr->la_uid;
339 osi->osi_setattr.lsr_gid = attr->la_gid;
340 osi->osi_setattr.lsr_valid =
341 ((attr->la_valid & LA_UID) ? OBD_MD_FLUID : 0) |
342 ((attr->la_valid & LA_GID) ? OBD_MD_FLGID : 0);
348 txn = osp_txn_info(&storage_th->th_ctx);
351 txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
352 osi->osi_hdr.lrh_id = txn->oti_current_id;
354 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
358 rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
362 if (likely(rc >= 0)) {
363 CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
364 d->opd_obd->obd_name,
365 POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
366 (unsigned long)osi->osi_cookie.lgc_lgl.lgl_ogen,
367 (unsigned long)osi->osi_cookie.lgc_index, rc);
368 spin_lock(&d->opd_syn_lock);
369 d->opd_syn_changes++;
370 spin_unlock(&d->opd_syn_lock);
372 /* return 0 always here, error case just cause no llog record */
376 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
377 llog_op_type type, struct thandle *th,
378 const struct lu_attr *attr)
380 return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
381 lu_object_fid(&o->opo_obj.do_lu), type, 1,
385 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
386 struct lu_fid *fid, int lost, struct thandle *th)
388 return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
392 * it's quite obvious we can't maintain all the structures in the memory:
393 * while OST is down, MDS can be processing thousands and thousands of unlinks
394 * filling persistent llogs and in-core respresentation
396 * this doesn't scale at all. so we need basically the following:
397 * a) destroy/setattr append llog records
398 * b) once llog has grown to X records, we process first Y committed records
400 * once record R is found via llog_process(), it becomes committed after any
401 * subsequent commit callback (at the most)
405 * ptlrpc commit callback.
407 * The callback is called by PTLRPC when a RPC is reported committed by the
408 * target (OST). We register the callback for the every RPC applying a change
409 * from the llog. This way we know then the llog records can be cancelled.
410 * Notice the callback can be called when OSP is finishing. We can detect this
411 * checking that actual transno in the request is less or equal of known
412 * committed transno (see osp_sync_process_committed() for the details).
413 * XXX: this is pretty expensive and can be improved later using batching.
415 * \param[in] req request
417 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
419 struct osp_device *d = req->rq_cb_data;
420 struct osp_job_req_args *jra;
422 CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
424 if (unlikely(req->rq_transno == 0))
427 /* do not do any opd_dyn_rpc_* accounting here
428 * it's done in osp_sync_interpret sooner or later */
431 jra = ptlrpc_req_async_args(req);
432 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
433 LASSERT(list_empty(&jra->jra_link));
435 ptlrpc_request_addref(req);
437 spin_lock(&d->opd_syn_lock);
438 list_add(&jra->jra_link, &d->opd_syn_committed_there);
439 spin_unlock(&d->opd_syn_lock);
441 /* XXX: some batching wouldn't hurt */
442 wake_up(&d->opd_syn_waitq);
446 * RPC interpretation callback.
448 * The callback is called by ptlrpc when RPC is replied. Now we have to decide
450 * - put request on a special list to wait until it's committed by the target,
451 * if the request is successful
452 * - schedule llog record cancel if no target object is found
453 * - try later (essentially after reboot) in case of unexpected error
455 * \param[in] env LU environment provided by the caller
456 * \param[in] req request replied
457 * \param[in] aa callback data
458 * \param[in] rc result of RPC
462 static int osp_sync_interpret(const struct lu_env *env,
463 struct ptlrpc_request *req, void *aa, int rc)
465 struct osp_device *d = req->rq_cb_data;
466 struct osp_job_req_args *jra = aa;
468 if (jra->jra_magic != OSP_JOB_MAGIC) {
469 DEBUG_REQ(D_ERROR, req, "bad magic %u\n", jra->jra_magic);
474 CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
475 atomic_read(&req->rq_refcount),
476 rc, (unsigned) req->rq_transno);
477 LASSERT(rc || req->rq_transno);
481 * we tried to destroy object or update attributes,
482 * but object doesn't exist anymore - cancell llog record
484 LASSERT(req->rq_transno == 0);
485 LASSERT(list_empty(&jra->jra_link));
487 ptlrpc_request_addref(req);
489 spin_lock(&d->opd_syn_lock);
490 list_add(&jra->jra_link, &d->opd_syn_committed_there);
491 spin_unlock(&d->opd_syn_lock);
493 wake_up(&d->opd_syn_waitq);
495 struct obd_import *imp = req->rq_import;
497 * error happened, we'll try to repeat on next boot ?
499 LASSERTF(req->rq_transno == 0 ||
500 req->rq_import_generation < imp->imp_generation,
501 "transno "LPU64", rc %d, gen: req %d, imp %d\n",
502 req->rq_transno, rc, req->rq_import_generation,
503 imp->imp_generation);
504 if (req->rq_transno == 0) {
505 /* this is the last time we see the request
506 * if transno is not zero, then commit cb
507 * will be called at some point */
508 LASSERT(d->opd_syn_rpc_in_progress > 0);
509 spin_lock(&d->opd_syn_lock);
510 d->opd_syn_rpc_in_progress--;
511 spin_unlock(&d->opd_syn_lock);
514 wake_up(&d->opd_syn_waitq);
515 } else if (d->opd_pre != NULL &&
516 unlikely(d->opd_pre_status == -ENOSPC)) {
518 * if current status is -ENOSPC (lack of free space on OST)
519 * then we should poll OST immediately once object destroy
522 osp_statfs_need_now(d);
525 LASSERT(d->opd_syn_rpc_in_flight > 0);
526 spin_lock(&d->opd_syn_lock);
527 d->opd_syn_rpc_in_flight--;
528 spin_unlock(&d->opd_syn_lock);
529 if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
530 wake_up(&d->opd_syn_barrier_waitq);
531 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
532 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
533 d->opd_syn_rpc_in_progress);
535 osp_sync_check_for_work(d);
541 ** Add request to ptlrpc queue.
543 * This is just a tiny helper function to put the request on the sending list
545 * \param[in] d OSP device
546 * \param[in] req request
548 static void osp_sync_send_new_rpc(struct osp_device *d,
549 struct ptlrpc_request *req)
551 struct osp_job_req_args *jra;
553 LASSERT(d->opd_syn_rpc_in_flight <= d->opd_syn_max_rpc_in_flight);
555 jra = ptlrpc_req_async_args(req);
556 jra->jra_magic = OSP_JOB_MAGIC;
557 INIT_LIST_HEAD(&jra->jra_link);
559 ptlrpcd_add_req(req);
564 * Allocate and prepare RPC for a new change.
566 * The function allocates and initializes an RPC which will be sent soon to
567 * apply the change to the target OST. The request is initialized from the
568 * llog record passed. Notice only the fields common to all type of changes
571 * \param[in] d OSP device
572 * \param[in] llh llog handle where the record is stored
573 * \param[in] h llog record
574 * \param[in] op type of the change
575 * \param[in] format request format to be used
577 * \retval pointer new request on success
578 * \retval ERR_PTR(errno) on error
580 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
581 struct llog_handle *llh,
582 struct llog_rec_hdr *h,
584 const struct req_format *format)
586 struct ptlrpc_request *req;
587 struct ost_body *body;
588 struct obd_import *imp;
591 /* Prepare the request */
592 imp = d->opd_obd->u.cli.cl_import;
594 req = ptlrpc_request_alloc(imp, format);
596 RETURN(ERR_PTR(-ENOMEM));
598 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
600 ptlrpc_req_finished(req);
605 * this is a trick: to save on memory allocations we put cookie
606 * into the request, but don't set corresponded flag in o_valid
607 * so that OST doesn't interpret this cookie. once the request
608 * is committed on OST we take cookie from the request and cancel
610 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
612 body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
613 body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
614 body->oa.o_lcookie.lgc_index = h->lrh_index;
616 req->rq_interpret_reply = osp_sync_interpret;
617 req->rq_commit_cb = osp_sync_request_commit_cb;
620 ptlrpc_request_set_replen(req);
626 * Generate a request for setattr change.
628 * The function prepares a new RPC, initializes it with setattr specific
629 * bits and send the RPC.
631 * \param[in] d OSP device
632 * \param[in] llh llog handle where the record is stored
633 * \param[in] h llog record
635 * \retval 0 on success
636 * \retval negative negated errno on error
638 static int osp_sync_new_setattr_job(struct osp_device *d,
639 struct llog_handle *llh,
640 struct llog_rec_hdr *h)
642 struct llog_setattr64_rec *rec = (struct llog_setattr64_rec *)h;
643 struct ptlrpc_request *req;
644 struct ost_body *body;
647 LASSERT(h->lrh_type == MDS_SETATTR64_REC);
649 /* lsr_valid can only be 0 or have OBD_MD_{FLUID,FLGID} set,
650 * so no bits other than these should be set. */
651 if ((rec->lsr_valid & ~(OBD_MD_FLUID | OBD_MD_FLGID)) != 0) {
652 CERROR("%s: invalid setattr record, lsr_valid:"LPU64"\n",
653 d->opd_obd->obd_name, rec->lsr_valid);
654 /* return 0 so that sync thread can continue processing
659 req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
661 RETURN(PTR_ERR(req));
663 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
665 body->oa.o_oi = rec->lsr_oi;
666 body->oa.o_uid = rec->lsr_uid;
667 body->oa.o_gid = rec->lsr_gid;
668 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
669 /* old setattr record (prior 2.6.0) doesn't have 'valid' stored,
670 * we assume that both UID and GID are valid in that case. */
671 if (rec->lsr_valid == 0)
672 body->oa.o_valid |= (OBD_MD_FLUID | OBD_MD_FLGID);
674 body->oa.o_valid |= rec->lsr_valid;
676 osp_sync_send_new_rpc(d, req);
681 * Generate a request for unlink change.
683 * The function prepares a new RPC, initializes it with unlink(destroy)
684 * specific bits and sends the RPC. The function is used to handle
685 * llog_unlink_rec which were used in the older versions of Lustre.
686 * Current version uses llog_unlink_rec64.
688 * \param[in] d OSP device
689 * \param[in] llh llog handle where the record is stored
690 * \param[in] h llog record
692 * \retval 0 on success
693 * \retval negative negated errno on error
695 static int osp_sync_new_unlink_job(struct osp_device *d,
696 struct llog_handle *llh,
697 struct llog_rec_hdr *h)
699 struct llog_unlink_rec *rec = (struct llog_unlink_rec *)h;
700 struct ptlrpc_request *req;
701 struct ost_body *body;
704 LASSERT(h->lrh_type == MDS_UNLINK_REC);
706 req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
708 RETURN(PTR_ERR(req));
710 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
712 ostid_set_seq(&body->oa.o_oi, rec->lur_oseq);
713 ostid_set_id(&body->oa.o_oi, rec->lur_oid);
714 body->oa.o_misc = rec->lur_count;
715 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
717 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
719 osp_sync_send_new_rpc(d, req);
724 * Generate a request for unlink change.
726 * The function prepares a new RPC, initializes it with unlink(destroy)
727 * specific bits and sends the RPC. Depending on the target (MDT or OST)
728 * two different protocols are used. For MDT we use OUT (basically OSD API
729 * updates transferred via a network). For OST we still use the old
730 * protocol (OBD?), originally for compatibility. Later we can start to
731 * use OUT for OST as well, this will allow batching and better code
734 * \param[in] env LU environment provided by the caller
735 * \param[in] d OSP device
736 * \param[in] llh llog handle where the record is stored
737 * \param[in] h llog record
739 * \retval 0 on success
740 * \retval negative negated errno on error
742 static int osp_sync_new_unlink64_job(const struct lu_env *env,
743 struct osp_device *d,
744 struct llog_handle *llh,
745 struct llog_rec_hdr *h)
747 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
748 struct ptlrpc_request *req = NULL;
749 struct ost_body *body;
753 LASSERT(h->lrh_type == MDS_UNLINK64_REC);
754 req = osp_sync_new_job(d, llh, h, OST_DESTROY,
757 RETURN(PTR_ERR(req));
759 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
762 rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
765 body->oa.o_misc = rec->lur_count;
766 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
768 osp_sync_send_new_rpc(d, req);
773 * Process llog records.
775 * This function is called to process the llog records committed locally.
776 * In the recovery model used by OSP we can apply a change to a remote
777 * target once corresponding transaction (like posix unlink) is committed
778 * locally so can't revert.
779 * Depending on the llog record type, a given handler is called that is
780 * responsible for preparing and sending the RPC to apply the change.
781 * Special record type LLOG_GEN_REC marking a reboot is cancelled right away.
783 * \param[in] env LU environment provided by the caller
784 * \param[in] d OSP device
785 * \param[in] llh llog handle where the record is stored
786 * \param[in] rec llog record
788 * \retval 0 on success
789 * \retval negative negated errno on error
791 static int osp_sync_process_record(const struct lu_env *env,
792 struct osp_device *d,
793 struct llog_handle *llh,
794 struct llog_rec_hdr *rec)
796 struct llog_cookie cookie;
799 cookie.lgc_lgl = llh->lgh_id;
800 cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
801 cookie.lgc_index = rec->lrh_index;
803 if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
804 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
806 /* we're waiting for the record generated by this instance */
807 LASSERT(d->opd_syn_prev_done == 0);
808 if (!memcmp(&d->opd_syn_generation, &gen->lgr_gen,
809 sizeof(gen->lgr_gen))) {
810 CDEBUG(D_HA, "processed all old entries\n");
811 d->opd_syn_prev_done = 1;
814 /* cancel any generation record */
815 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
822 * now we prepare and fill requests to OST, put them on the queue
823 * and fire after next commit callback
826 /* notice we increment counters before sending RPC, to be consistent
827 * in RPC interpret callback which may happen very quickly */
828 spin_lock(&d->opd_syn_lock);
829 d->opd_syn_rpc_in_flight++;
830 d->opd_syn_rpc_in_progress++;
831 spin_unlock(&d->opd_syn_lock);
833 switch (rec->lrh_type) {
834 /* case MDS_UNLINK_REC is kept for compatibility */
836 rc = osp_sync_new_unlink_job(d, llh, rec);
838 case MDS_UNLINK64_REC:
839 rc = osp_sync_new_unlink64_job(env, d, llh, rec);
841 case MDS_SETATTR64_REC:
842 rc = osp_sync_new_setattr_job(d, llh, rec);
845 CERROR("%s: unknown record type: %x\n", d->opd_obd->obd_name,
847 /* we should continue processing */
850 /* rc > 0 means sync RPC being added to the queue */
851 if (likely(rc > 0)) {
852 spin_lock(&d->opd_syn_lock);
853 if (d->opd_syn_prev_done) {
854 LASSERT(d->opd_syn_changes > 0);
855 LASSERT(rec->lrh_id <= d->opd_syn_last_committed_id);
857 * NOTE: it's possible to meet same id if
858 * OST stores few stripes of same file
860 if (rec->lrh_id > d->opd_syn_last_processed_id) {
861 d->opd_syn_last_processed_id = rec->lrh_id;
862 wake_up(&d->opd_syn_barrier_waitq);
865 d->opd_syn_changes--;
867 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
868 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
869 d->opd_syn_rpc_in_progress);
870 spin_unlock(&d->opd_syn_lock);
873 spin_lock(&d->opd_syn_lock);
874 d->opd_syn_rpc_in_flight--;
875 d->opd_syn_rpc_in_progress--;
876 spin_unlock(&d->opd_syn_lock);
879 CDEBUG(D_HA, "found record %x, %d, idx %u, id %u: %d\n",
880 rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id, rc);
885 * Cancel llog records for the committed changes.
887 * The function walks through the list of the committed RPCs and cancels
888 * corresponding llog records. see osp_sync_request_commit_cb() for the
891 * \param[in] env LU environment provided by the caller
892 * \param[in] d OSP device
894 static void osp_sync_process_committed(const struct lu_env *env,
895 struct osp_device *d)
897 struct obd_device *obd = d->opd_obd;
898 struct obd_import *imp = obd->u.cli.cl_import;
899 struct ost_body *body;
900 struct ptlrpc_request *req;
901 struct llog_ctxt *ctxt;
902 struct llog_handle *llh;
903 struct list_head list;
908 if (list_empty(&d->opd_syn_committed_there))
912 * if current status is -ENOSPC (lack of free space on OST)
913 * then we should poll OST immediately once object destroy
915 * notice: we do this upon commit as well because some backends
916 * (like DMU) do not release space right away.
918 if (d->opd_pre != NULL && unlikely(d->opd_pre_status == -ENOSPC))
919 osp_statfs_need_now(d);
922 * now cancel them all
923 * XXX: can we improve this using some batching?
924 * with batch RPC that'll happen automatically?
925 * XXX: can we store ctxt in lod_device and save few cycles ?
927 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
930 llh = ctxt->loc_handle;
933 INIT_LIST_HEAD(&list);
934 spin_lock(&d->opd_syn_lock);
935 list_splice(&d->opd_syn_committed_there, &list);
936 INIT_LIST_HEAD(&d->opd_syn_committed_there);
937 spin_unlock(&d->opd_syn_lock);
939 while (!list_empty(&list)) {
940 struct llog_cookie *lcookie = NULL;
941 struct osp_job_req_args *jra;
943 jra = list_entry(list.next, struct osp_job_req_args, jra_link);
944 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
945 list_del_init(&jra->jra_link);
947 req = container_of((void *)jra, struct ptlrpc_request,
949 body = req_capsule_client_get(&req->rq_pill,
952 lcookie = &body->oa.o_lcookie;
953 /* import can be closing, thus all commit cb's are
954 * called we can check committness directly */
955 if (req->rq_transno <= imp->imp_peer_committed_transno) {
956 rc = llog_cat_cancel_records(env, llh, 1, lcookie);
958 CERROR("%s: can't cancel record: %d\n",
961 DEBUG_REQ(D_HA, req, "not committed");
964 ptlrpc_req_finished(req);
970 LASSERT(d->opd_syn_rpc_in_progress >= done);
971 spin_lock(&d->opd_syn_lock);
972 d->opd_syn_rpc_in_progress -= done;
973 spin_unlock(&d->opd_syn_lock);
974 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
975 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
976 d->opd_syn_rpc_in_progress);
978 osp_sync_check_for_work(d);
980 /* wake up the thread if requested to stop:
981 * it might be waiting for in-progress to complete */
982 if (unlikely(osp_sync_running(d) == 0))
983 wake_up(&d->opd_syn_waitq);
989 * The core of the syncing mechanism.
991 * This is a callback called by the llog processing function. Essentially it
992 * suspends llog processing until there is a record to process (it's supposed
993 * to be committed locally). The function handles RPCs committed by the target
994 * and cancels corresponding llog records.
996 * \param[in] env LU environment provided by the caller
997 * \param[in] llh llog handle we're processing
998 * \param[in] rec current llog record
999 * \param[in] data callback data containing a pointer to the device
1001 * \retval 0 to ask the caller (llog_process()) to continue
1002 * \retval LLOG_PROC_BREAK to ask the caller to break
1004 static int osp_sync_process_queues(const struct lu_env *env,
1005 struct llog_handle *llh,
1006 struct llog_rec_hdr *rec,
1009 struct osp_device *d = data;
1013 struct l_wait_info lwi = { 0 };
1015 if (!osp_sync_running(d)) {
1016 CDEBUG(D_HA, "stop llog processing\n");
1017 return LLOG_PROC_BREAK;
1020 /* process requests committed by OST */
1021 osp_sync_process_committed(env, d);
1023 /* if we there are changes to be processed and we have
1024 * resources for this ... do now */
1025 if (osp_sync_can_process_new(d, rec)) {
1027 /* ask llog for another record */
1028 CDEBUG(D_HA, "%lu changes, %u in progress,"
1031 d->opd_syn_rpc_in_progress,
1032 d->opd_syn_rpc_in_flight);
1037 * try to send, in case of disconnection, suspend
1038 * processing till we can send this request
1041 rc = osp_sync_process_record(env, d, llh, rec);
1043 * XXX: probably different handling is needed
1044 * for some bugs, like immediate exit or if
1048 CERROR("can't send: %d\n", rc);
1049 l_wait_event(d->opd_syn_waitq,
1050 !osp_sync_running(d) ||
1051 osp_sync_has_work(d),
1054 } while (rc != 0 && osp_sync_running(d));
1060 if (d->opd_syn_last_processed_id == d->opd_syn_last_used_id)
1061 osp_sync_remove_from_tracker(d);
1063 l_wait_event(d->opd_syn_waitq,
1064 !osp_sync_running(d) ||
1065 osp_sync_can_process_new(d, rec) ||
1066 !list_empty(&d->opd_syn_committed_there),
1074 * This thread runs llog_cat_process() scanner calling our callback
1075 * to process llog records. in the callback we implement tricky
1076 * state machine as we don't want to start scanning of the llog again
1077 * and again, also we don't want to process too many records and send
1078 * too many RPCs a time. so, depending on current load (num of changes
1079 * being synced to OST) the callback can suspend awaiting for some
1080 * new conditions, like syncs completed.
1082 * In order to process llog records left by previous boots and to allow
1083 * llog_process_thread() to find something (otherwise it'd just exit
1084 * immediately) we add a special GENERATATION record on each boot.
1086 * \param[in] _arg a pointer to thread's arguments
1088 * \retval 0 on success
1089 * \retval negative negated errno on error
1091 static int osp_sync_thread(void *_arg)
1093 struct osp_device *d = _arg;
1094 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1095 struct l_wait_info lwi = { 0 };
1096 struct llog_ctxt *ctxt;
1097 struct obd_device *obd = d->opd_obd;
1098 struct llog_handle *llh;
1104 rc = lu_env_init(&env, LCT_LOCAL);
1106 CERROR("%s: can't initialize env: rc = %d\n",
1111 spin_lock(&d->opd_syn_lock);
1112 thread->t_flags = SVC_RUNNING;
1113 spin_unlock(&d->opd_syn_lock);
1114 wake_up(&thread->t_ctl_waitq);
1116 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1118 CERROR("can't get appropriate context\n");
1119 GOTO(out, rc = -EINVAL);
1122 llh = ctxt->loc_handle;
1124 CERROR("can't get llh\n");
1125 llog_ctxt_put(ctxt);
1126 GOTO(out, rc = -EINVAL);
1129 rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
1130 LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
1131 "%lu changes, %u in progress, %u in flight: %d\n",
1132 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1133 d->opd_syn_rpc_in_flight, rc);
1135 /* we don't expect llog_process_thread() to exit till umount */
1136 LASSERTF(thread->t_flags != SVC_RUNNING,
1137 "%lu changes, %u in progress, %u in flight\n",
1138 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
1139 d->opd_syn_rpc_in_flight);
1141 /* wait till all the requests are completed */
1143 while (d->opd_syn_rpc_in_progress > 0) {
1144 osp_sync_process_committed(&env, d);
1146 lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
1147 rc = l_wait_event(d->opd_syn_waitq,
1148 d->opd_syn_rpc_in_progress == 0,
1150 if (rc == -ETIMEDOUT)
1152 LASSERTF(count < 10, "%s: %d %d %sempty\n",
1153 d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
1154 d->opd_syn_rpc_in_flight,
1155 list_empty(&d->opd_syn_committed_there) ? "" : "!");
1159 llog_cat_close(&env, llh);
1160 rc = llog_cleanup(&env, ctxt);
1162 CERROR("can't cleanup llog: %d\n", rc);
1164 LASSERTF(d->opd_syn_rpc_in_progress == 0,
1165 "%s: %d %d %sempty\n",
1166 d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
1167 d->opd_syn_rpc_in_flight,
1168 list_empty(&d->opd_syn_committed_there) ? "" : "!");
1170 thread->t_flags = SVC_STOPPED;
1172 wake_up(&thread->t_ctl_waitq);
1182 * Initializes the llog. Specific llog to be used depends on the type of the
1183 * target OSP represents (OST or MDT). The function adds appends a new llog
1184 * record to mark the place where the records associated with this boot
1187 * \param[in] env LU environment provided by the caller
1188 * \param[in] d OSP device
1190 * \retval 0 on success
1191 * \retval negative negated errno on error
1193 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
1195 struct osp_thread_info *osi = osp_env_info(env);
1196 struct lu_fid *fid = &osi->osi_fid;
1197 struct llog_handle *lgh = NULL;
1198 struct obd_device *obd = d->opd_obd;
1199 struct llog_ctxt *ctxt;
1207 * open llog corresponding to our OST
1209 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1210 obd->obd_lvfs_ctxt.dt = d->opd_storage;
1212 lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
1214 rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
1215 &osi->osi_cid, fid);
1217 if (rc != -EFAULT) {
1218 CERROR("%s: can't get id from catalogs: rc = %d\n",
1223 /* After sparse OST indices is supported, the CATALOG file
1224 * may become a sparse file that results in failure on
1225 * reading. Skip this error as the llog will be created
1227 memset(&osi->osi_cid, 0, sizeof(osi->osi_cid));
1231 CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
1232 obd->obd_name, d->opd_index,
1233 POSTID(&osi->osi_cid.lci_logid.lgl_oi),
1234 osi->osi_cid.lci_logid.lgl_ogen);
1236 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT,
1237 d->opd_storage->dd_lu_dev.ld_obd,
1238 &osp_mds_ost_orig_logops);
1242 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1245 if (likely(logid_id(&osi->osi_cid.lci_logid) != 0)) {
1246 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
1248 /* re-create llog if it is missing */
1250 logid_set_id(&osi->osi_cid.lci_logid, 0);
1252 GOTO(out_cleanup, rc);
1255 if (unlikely(logid_id(&osi->osi_cid.lci_logid) == 0)) {
1256 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
1258 GOTO(out_cleanup, rc);
1259 osi->osi_cid.lci_logid = lgh->lgh_id;
1262 LASSERT(lgh != NULL);
1263 ctxt->loc_handle = lgh;
1265 rc = llog_cat_init_and_process(env, lgh);
1267 GOTO(out_close, rc);
1269 rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
1270 &osi->osi_cid, fid);
1272 GOTO(out_close, rc);
1275 * put a mark in the llog till which we'll be processing
1276 * old records restless
1278 d->opd_syn_generation.mnt_cnt = cfs_time_current();
1279 d->opd_syn_generation.conn_cnt = cfs_time_current();
1281 osi->osi_hdr.lrh_type = LLOG_GEN_REC;
1282 osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
1284 memcpy(&osi->osi_gen.lgr_gen, &d->opd_syn_generation,
1285 sizeof(osi->osi_gen.lgr_gen));
1287 rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie);
1289 GOTO(out_close, rc);
1290 llog_ctxt_put(ctxt);
1293 llog_cat_close(env, lgh);
1295 llog_cleanup(env, ctxt);
1300 * Cleanup llog used for syncing.
1302 * Closes and cleanups the llog. The function is called when the device is
1305 * \param[in] env LU environment provided by the caller
1306 * \param[in] d OSP device
1308 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
1310 struct llog_ctxt *ctxt;
1312 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
1314 llog_cat_close(env, ctxt->loc_handle);
1315 llog_cleanup(env, ctxt);
1319 * Initialization of the sync component of OSP.
1321 * Initializes the llog and starts a new thread to handle the changes to
1322 * the remote target (OST or MDT).
1324 * \param[in] env LU environment provided by the caller
1325 * \param[in] d OSP device
1327 * \retval 0 on success
1328 * \retval negative negated errno on error
1330 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1332 struct l_wait_info lwi = { 0 };
1333 struct task_struct *task;
1338 rc = osp_sync_id_traction_init(d);
1343 * initialize llog storing changes
1345 rc = osp_sync_llog_init(env, d);
1347 CERROR("%s: can't initialize llog: rc = %d\n",
1348 d->opd_obd->obd_name, rc);
1353 * Start synchronization thread
1355 d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
1356 d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
1357 spin_lock_init(&d->opd_syn_lock);
1358 init_waitqueue_head(&d->opd_syn_waitq);
1359 init_waitqueue_head(&d->opd_syn_barrier_waitq);
1360 init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
1361 INIT_LIST_HEAD(&d->opd_syn_committed_there);
1363 task = kthread_run(osp_sync_thread, d, "osp-syn-%u-%u",
1364 d->opd_index, d->opd_group);
1367 CERROR("%s: cannot start sync thread: rc = %d\n",
1368 d->opd_obd->obd_name, rc);
1372 l_wait_event(d->opd_syn_thread.t_ctl_waitq,
1373 osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1377 osp_sync_llog_fini(env, d);
1379 osp_sync_id_traction_fini(d);
1384 * Stop the syncing thread.
1386 * Asks the syncing thread to stop and wait until it's stopped.
1388 * \param[in] d OSP device
1392 int osp_sync_fini(struct osp_device *d)
1394 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1398 thread->t_flags = SVC_STOPPING;
1399 wake_up(&d->opd_syn_waitq);
1400 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1403 * unregister transaction callbacks only when sync thread
1404 * has finished operations with llog
1406 osp_sync_id_traction_fini(d);
1411 static DEFINE_MUTEX(osp_id_tracker_sem);
1412 static struct list_head osp_id_tracker_list =
1413 LIST_HEAD_INIT(osp_id_tracker_list);
1416 * OSD commit callback.
1418 * The function is used as a local OSD commit callback to track the highest
1419 * committed llog record id. see osp_sync_id_traction_init() for the details.
1421 * \param[in] th local transaction handle committed
1422 * \param[in] cookie commit callback data (our private structure)
1424 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
1426 struct osp_id_tracker *tr = cookie;
1427 struct osp_device *d;
1428 struct osp_txn_info *txn;
1432 txn = osp_txn_info(&th->th_ctx);
1433 if (txn == NULL || txn->oti_current_id < tr->otr_committed_id)
1436 spin_lock(&tr->otr_lock);
1437 if (likely(txn->oti_current_id > tr->otr_committed_id)) {
1438 CDEBUG(D_OTHER, "committed: %u -> %u\n",
1439 tr->otr_committed_id, txn->oti_current_id);
1440 tr->otr_committed_id = txn->oti_current_id;
1442 list_for_each_entry(d, &tr->otr_wakeup_list,
1444 d->opd_syn_last_committed_id = tr->otr_committed_id;
1445 wake_up(&d->opd_syn_waitq);
1448 spin_unlock(&tr->otr_lock);
1452 * Initialize commit tracking mechanism.
1454 * Some setups may have thousands of OSTs and each will be represented by OSP.
1455 * Meaning order of magnitute many more changes to apply every second. In order
1456 * to keep the number of commit callbacks low this mechanism was introduced.
1457 * The mechanism is very similar to transno used by MDT service: it's an single
1458 * ID stream which can be assigned by any OSP to its llog records. The tricky
1459 * part is that ID is stored in per-transaction data and re-used by all the OSPs
1460 * involved in that transaction. Then all these OSPs are woken up utilizing a single OSD commit callback.
1462 * The function initializes the data used by the tracker described above.
1463 * A singler tracker per OSD device is created.
1465 * \param[in] d OSP device
1467 * \retval 0 on success
1468 * \retval negative negated errno on error
1470 static int osp_sync_id_traction_init(struct osp_device *d)
1472 struct osp_id_tracker *tr, *found = NULL;
1476 LASSERT(d->opd_storage);
1477 LASSERT(d->opd_syn_tracker == NULL);
1478 INIT_LIST_HEAD(&d->opd_syn_ontrack);
1480 mutex_lock(&osp_id_tracker_sem);
1481 list_for_each_entry(tr, &osp_id_tracker_list, otr_list) {
1482 if (tr->otr_dev == d->opd_storage) {
1483 LASSERT(atomic_read(&tr->otr_refcount));
1484 atomic_inc(&tr->otr_refcount);
1485 d->opd_syn_tracker = tr;
1491 if (found == NULL) {
1495 d->opd_syn_tracker = tr;
1496 spin_lock_init(&tr->otr_lock);
1497 tr->otr_dev = d->opd_storage;
1498 tr->otr_next_id = 1;
1499 tr->otr_committed_id = 0;
1500 atomic_set(&tr->otr_refcount, 1);
1501 INIT_LIST_HEAD(&tr->otr_wakeup_list);
1502 list_add(&tr->otr_list, &osp_id_tracker_list);
1503 tr->otr_tx_cb.dtc_txn_commit =
1504 osp_sync_tracker_commit_cb;
1505 tr->otr_tx_cb.dtc_cookie = tr;
1506 tr->otr_tx_cb.dtc_tag = LCT_MD_THREAD;
1507 dt_txn_callback_add(d->opd_storage, &tr->otr_tx_cb);
1511 mutex_unlock(&osp_id_tracker_sem);
1517 * Release commit tracker.
1519 * Decrease a refcounter on the tracker used by the given OSP device \a d.
1520 * If no more users left, then the tracker is released.
1522 * \param[in] d OSP device
1524 static void osp_sync_id_traction_fini(struct osp_device *d)
1526 struct osp_id_tracker *tr;
1531 tr = d->opd_syn_tracker;
1537 osp_sync_remove_from_tracker(d);
1539 mutex_lock(&osp_id_tracker_sem);
1540 if (atomic_dec_and_test(&tr->otr_refcount)) {
1541 dt_txn_callback_del(d->opd_storage, &tr->otr_tx_cb);
1542 LASSERT(list_empty(&tr->otr_wakeup_list));
1543 list_del(&tr->otr_list);
1545 d->opd_syn_tracker = NULL;
1547 mutex_unlock(&osp_id_tracker_sem);
1553 * Generate a new ID on a tracker.
1555 * Generates a new ID using the tracker associated with the given OSP device
1556 * \a d, if the given ID \a id is non-zero. Unconditially adds OSP device to
1557 * the wakeup list, so OSP won't miss when a transaction using the ID is
1558 * committed. Notice ID is 32bit, but llog doesn't support >2^32 records anyway.
1560 * \param[in] d OSP device
1561 * \param[in] id 0 or ID generated previously
1563 * \retval ID the caller should use
1565 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
1567 struct osp_id_tracker *tr;
1569 tr = d->opd_syn_tracker;
1572 /* XXX: we can improve this introducing per-cpu preallocated ids? */
1573 spin_lock(&tr->otr_lock);
1574 if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
1575 spin_unlock(&tr->otr_lock);
1576 CERROR("%s: next %u, last synced %lu\n",
1577 d->opd_obd->obd_name, tr->otr_next_id,
1578 d->opd_syn_last_used_id);
1583 id = tr->otr_next_id++;
1584 if (id > d->opd_syn_last_used_id)
1585 d->opd_syn_last_used_id = id;
1586 if (list_empty(&d->opd_syn_ontrack))
1587 list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
1588 spin_unlock(&tr->otr_lock);
1589 CDEBUG(D_OTHER, "new id %u\n", (unsigned) id);
1595 * Stop to propagate commit status to OSP.
1597 * If the OSP does not have any llog records she's waiting to commit, then
1598 * it is possible to unsubscribe from wakeups from the tracking using this
1601 * \param[in] d OSP device not willing to wakeup
1603 static void osp_sync_remove_from_tracker(struct osp_device *d)
1605 struct osp_id_tracker *tr;
1607 tr = d->opd_syn_tracker;
1610 if (list_empty(&d->opd_syn_ontrack))
1613 spin_lock(&tr->otr_lock);
1614 list_del_init(&d->opd_syn_ontrack);
1615 spin_unlock(&tr->otr_lock);