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, 2015, 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 __u64 osp_sync_id_get(struct osp_device *d, __u64 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_committed_link;
102 struct list_head jra_inflight_link;
106 static inline int osp_sync_running(struct osp_device *d)
108 return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
112 * Check status: whether OSP thread has stopped
114 * \param[in] d OSP device
116 * \retval 0 still running
119 static inline int osp_sync_stopped(struct osp_device *d)
121 return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
125 ** Check for new changes to sync
127 * \param[in] d OSP device
129 * \retval 1 there are changes
130 * \retval 0 there are no changes
132 static inline int osp_sync_has_new_job(struct osp_device *d)
134 return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
135 (d->opd_syn_last_processed_id < d->opd_syn_last_committed_id))
136 || (d->opd_syn_prev_done == 0);
139 static inline int osp_sync_inflight_conflict(struct osp_device *d,
140 struct llog_rec_hdr *h)
142 struct osp_job_req_args *jra;
146 if (h == NULL || h->lrh_type == LLOG_GEN_REC ||
147 list_empty(&d->opd_syn_inflight_list))
150 memset(&ostid, 0, sizeof(ostid));
151 switch (h->lrh_type) {
153 ostid_set_seq(&ostid, ((struct llog_unlink_rec *)h)->lur_oseq);
154 ostid_set_id(&ostid, ((struct llog_unlink_rec *)h)->lur_oid);
156 case MDS_UNLINK64_REC:
157 fid_to_ostid(&((struct llog_unlink64_rec *)h)->lur_fid, &ostid);
159 case MDS_SETATTR64_REC:
160 ostid = ((struct llog_setattr64_rec *)h)->lsr_oi;
166 spin_lock(&d->opd_syn_lock);
167 list_for_each_entry(jra, &d->opd_syn_inflight_list, jra_inflight_link) {
168 struct ptlrpc_request *req;
169 struct ost_body *body;
171 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
173 req = container_of((void *)jra, struct ptlrpc_request,
175 body = req_capsule_client_get(&req->rq_pill,
179 if (memcmp(&ostid, &body->oa.o_oi, sizeof(ostid)) == 0) {
184 spin_unlock(&d->opd_syn_lock);
189 static inline int osp_sync_low_in_progress(struct osp_device *d)
191 return atomic_read(&d->opd_syn_rpc_in_progress) <
192 d->opd_syn_max_rpc_in_progress;
196 * Check for room in the network pipe to OST
198 * \param[in] d OSP device
200 * \retval 1 there is room
201 * \retval 0 no room, the pipe is full
203 static inline int osp_sync_low_in_flight(struct osp_device *d)
205 return atomic_read(&d->opd_syn_rpc_in_flight) <
206 d->opd_syn_max_rpc_in_flight;
210 * Wake up check for the main sync thread
212 * \param[in] d OSP device
214 * \retval 1 time to wake up
215 * \retval 0 no need to wake up
217 static inline int osp_sync_has_work(struct osp_device *d)
219 /* has new/old changes and low in-progress? */
220 if (osp_sync_has_new_job(d) && osp_sync_low_in_progress(d) &&
221 osp_sync_low_in_flight(d) && d->opd_imp_connected)
224 /* has remotely committed? */
225 if (!list_empty(&d->opd_syn_committed_there))
231 #define osp_sync_check_for_work(d) \
233 if (osp_sync_has_work(d)) { \
234 wake_up(&d->opd_syn_waitq); \
238 void __osp_sync_check_for_work(struct osp_device *d)
240 osp_sync_check_for_work(d);
243 static inline __u64 osp_sync_correct_id(struct osp_device *d,
244 struct llog_rec_hdr *rec)
247 * llog use cyclic store with 32 bit lrh_id
248 * so overflow lrh_id is possible. Range between
249 * last_processed and last_committed is less than
250 * 64745 ^ 2 and less than 2^32 - 1
252 __u64 correct_id = d->opd_syn_last_committed_id;
254 if ((correct_id & 0xffffffffULL) < rec->lrh_id)
255 correct_id -= 0x100000000ULL;
257 correct_id &= ~0xffffffffULL;
258 correct_id |= rec->lrh_id;
263 * Check and return ready-for-new status.
265 * The thread processing llog record uses this function to check whether
266 * it's time to take another record and process it. The number of conditions
267 * must be met: the connection should be ready, RPCs in flight not exceeding
268 * the limit, the record is committed locally, etc (see the lines below).
270 * \param[in] d OSP device
271 * \param[in] rec next llog record to process
273 * \retval 0 not ready
276 static inline int osp_sync_can_process_new(struct osp_device *d,
277 struct llog_rec_hdr *rec)
281 if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
283 if (unlikely(osp_sync_inflight_conflict(d, rec)))
285 if (!osp_sync_low_in_progress(d))
287 if (!osp_sync_low_in_flight(d))
289 if (!d->opd_imp_connected)
291 if (d->opd_syn_prev_done == 0)
293 if (atomic_read(&d->opd_syn_changes) == 0)
296 osp_sync_correct_id(d, rec) <= d->opd_syn_last_committed_id)
302 * Declare intention to add a new change.
304 * With regard to OSD API, we have to declare any changes ahead. In this
305 * case we declare an intention to add a llog record representing the
306 * change on the local storage.
308 * \param[in] env LU environment provided by the caller
309 * \param[in] o OSP object
310 * \param[in] type type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
311 * \param[in] th transaction handle (local)
313 * \retval 0 on success
314 * \retval negative negated errno on error
316 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
317 llog_op_type type, struct thandle *th)
319 struct osp_thread_info *osi = osp_env_info(env);
320 struct osp_device *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
321 struct llog_ctxt *ctxt;
322 struct thandle *storage_th;
327 /* it's a layering violation, to access internals of th,
328 * but we can do this as a sanity check, for a while */
329 LASSERT(th->th_top != NULL);
330 storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
331 if (IS_ERR(storage_th))
332 RETURN(PTR_ERR(storage_th));
335 case MDS_UNLINK64_REC:
336 osi->osi_hdr.lrh_len = sizeof(struct llog_unlink64_rec);
338 case MDS_SETATTR64_REC:
339 osi->osi_hdr.lrh_len = sizeof(struct llog_setattr64_rec);
345 /* we want ->dt_trans_start() to allocate per-thandle structure */
346 storage_th->th_tags |= LCT_OSP_THREAD;
348 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
351 rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr,
359 * Generate a llog record for a given change.
361 * Generates a llog record for the change passed. The change can be of two
362 * types: unlink and setattr. The record gets an ID which later will be
363 * used to track commit status of the change. For unlink changes, the caller
364 * can supply a starting FID and the count of the objects to destroy. For
365 * setattr the caller should apply attributes to apply.
368 * \param[in] env LU environment provided by the caller
369 * \param[in] d OSP device
370 * \param[in] fid fid of the object the change should be applied to
371 * \param[in] type type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
372 * \param[in] count count of objects to destroy
373 * \param[in] th transaction handle (local)
374 * \param[in] attr attributes for setattr
376 * \retval 0 on success
377 * \retval negative negated errno on error
379 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
380 const struct lu_fid *fid, llog_op_type type,
381 int count, struct thandle *th,
382 const struct lu_attr *attr)
384 struct osp_thread_info *osi = osp_env_info(env);
385 struct llog_ctxt *ctxt;
386 struct osp_txn_info *txn;
387 struct thandle *storage_th;
392 /* it's a layering violation, to access internals of th,
393 * but we can do this as a sanity check, for a while */
394 LASSERT(th->th_top != NULL);
395 storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
396 if (IS_ERR(storage_th))
397 RETURN(PTR_ERR(storage_th));
400 case MDS_UNLINK64_REC:
401 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
402 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
403 osi->osi_unlink.lur_fid = *fid;
404 osi->osi_unlink.lur_count = count;
406 case MDS_SETATTR64_REC:
407 rc = fid_to_ostid(fid, &osi->osi_oi);
409 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
410 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
411 osi->osi_setattr.lsr_oi = osi->osi_oi;
413 osi->osi_setattr.lsr_uid = attr->la_uid;
414 osi->osi_setattr.lsr_gid = attr->la_gid;
415 osi->osi_setattr.lsr_valid =
416 ((attr->la_valid & LA_UID) ? OBD_MD_FLUID : 0) |
417 ((attr->la_valid & LA_GID) ? OBD_MD_FLGID : 0);
423 txn = osp_txn_info(&storage_th->th_ctx);
426 txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
427 osi->osi_hdr.lrh_id = (txn->oti_current_id & 0xffffffffULL);
428 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
432 rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
436 if (likely(rc >= 0)) {
437 CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
438 d->opd_obd->obd_name,
439 POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
440 (unsigned long)osi->osi_cookie.lgc_lgl.lgl_ogen,
441 (unsigned long)osi->osi_cookie.lgc_index, rc);
442 atomic_inc(&d->opd_syn_changes);
444 /* return 0 always here, error case just cause no llog record */
448 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
449 llog_op_type type, struct thandle *th,
450 const struct lu_attr *attr)
452 return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
453 lu_object_fid(&o->opo_obj.do_lu), type, 1,
457 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
458 struct lu_fid *fid, int lost, struct thandle *th)
460 return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
464 * it's quite obvious we can't maintain all the structures in the memory:
465 * while OST is down, MDS can be processing thousands and thousands of unlinks
466 * filling persistent llogs and in-core respresentation
468 * this doesn't scale at all. so we need basically the following:
469 * a) destroy/setattr append llog records
470 * b) once llog has grown to X records, we process first Y committed records
472 * once record R is found via llog_process(), it becomes committed after any
473 * subsequent commit callback (at the most)
477 * ptlrpc commit callback.
479 * The callback is called by PTLRPC when a RPC is reported committed by the
480 * target (OST). We register the callback for the every RPC applying a change
481 * from the llog. This way we know then the llog records can be cancelled.
482 * Notice the callback can be called when OSP is finishing. We can detect this
483 * checking that actual transno in the request is less or equal of known
484 * committed transno (see osp_sync_process_committed() for the details).
485 * XXX: this is pretty expensive and can be improved later using batching.
487 * \param[in] req request
489 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
491 struct osp_device *d = req->rq_cb_data;
492 struct osp_job_req_args *jra;
494 CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
496 if (unlikely(req->rq_transno == 0))
499 /* do not do any opd_dyn_rpc_* accounting here
500 * it's done in osp_sync_interpret sooner or later */
503 jra = ptlrpc_req_async_args(req);
504 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
505 LASSERT(list_empty(&jra->jra_committed_link));
507 ptlrpc_request_addref(req);
509 spin_lock(&d->opd_syn_lock);
510 list_add(&jra->jra_committed_link, &d->opd_syn_committed_there);
511 spin_unlock(&d->opd_syn_lock);
513 /* XXX: some batching wouldn't hurt */
514 wake_up(&d->opd_syn_waitq);
518 * RPC interpretation callback.
520 * The callback is called by ptlrpc when RPC is replied. Now we have to decide
522 * - put request on a special list to wait until it's committed by the target,
523 * if the request is successful
524 * - schedule llog record cancel if no target object is found
525 * - try later (essentially after reboot) in case of unexpected error
527 * \param[in] env LU environment provided by the caller
528 * \param[in] req request replied
529 * \param[in] aa callback data
530 * \param[in] rc result of RPC
534 static int osp_sync_interpret(const struct lu_env *env,
535 struct ptlrpc_request *req, void *aa, int rc)
537 struct osp_device *d = req->rq_cb_data;
538 struct osp_job_req_args *jra = aa;
540 if (jra->jra_magic != OSP_JOB_MAGIC) {
541 DEBUG_REQ(D_ERROR, req, "bad magic %u\n", jra->jra_magic);
546 CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
547 atomic_read(&req->rq_refcount),
548 rc, (unsigned) req->rq_transno);
549 LASSERT(rc || req->rq_transno);
553 * we tried to destroy object or update attributes,
554 * but object doesn't exist anymore - cancell llog record
556 LASSERT(req->rq_transno == 0);
557 LASSERT(list_empty(&jra->jra_committed_link));
559 ptlrpc_request_addref(req);
561 spin_lock(&d->opd_syn_lock);
562 list_add(&jra->jra_committed_link, &d->opd_syn_committed_there);
563 spin_unlock(&d->opd_syn_lock);
565 wake_up(&d->opd_syn_waitq);
567 struct obd_import *imp = req->rq_import;
569 * error happened, we'll try to repeat on next boot ?
571 LASSERTF(req->rq_transno == 0 ||
572 req->rq_import_generation < imp->imp_generation,
573 "transno "LPU64", rc %d, gen: req %d, imp %d\n",
574 req->rq_transno, rc, req->rq_import_generation,
575 imp->imp_generation);
576 if (req->rq_transno == 0) {
577 /* this is the last time we see the request
578 * if transno is not zero, then commit cb
579 * will be called at some point */
580 LASSERT(atomic_read(&d->opd_syn_rpc_in_progress) > 0);
581 atomic_dec(&d->opd_syn_rpc_in_progress);
584 wake_up(&d->opd_syn_waitq);
585 } else if (d->opd_pre != NULL &&
586 unlikely(d->opd_pre_status == -ENOSPC)) {
588 * if current status is -ENOSPC (lack of free space on OST)
589 * then we should poll OST immediately once object destroy
592 osp_statfs_need_now(d);
595 spin_lock(&d->opd_syn_lock);
596 list_del_init(&jra->jra_inflight_link);
597 spin_unlock(&d->opd_syn_lock);
598 LASSERT(atomic_read(&d->opd_syn_rpc_in_flight) > 0);
599 atomic_dec(&d->opd_syn_rpc_in_flight);
600 if (unlikely(atomic_read(&d->opd_syn_barrier) > 0))
601 wake_up(&d->opd_syn_barrier_waitq);
602 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
603 d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
604 atomic_read(&d->opd_syn_rpc_in_progress));
606 osp_sync_check_for_work(d);
612 ** Add request to ptlrpc queue.
614 * This is just a tiny helper function to put the request on the sending list
616 * \param[in] d OSP device
617 * \param[in] req request
619 static void osp_sync_send_new_rpc(struct osp_device *d,
620 struct ptlrpc_request *req)
622 struct osp_job_req_args *jra;
624 LASSERT(atomic_read(&d->opd_syn_rpc_in_flight) <=
625 d->opd_syn_max_rpc_in_flight);
627 jra = ptlrpc_req_async_args(req);
628 jra->jra_magic = OSP_JOB_MAGIC;
629 INIT_LIST_HEAD(&jra->jra_committed_link);
630 spin_lock(&d->opd_syn_lock);
631 list_add_tail(&jra->jra_inflight_link, &d->opd_syn_inflight_list);
632 spin_unlock(&d->opd_syn_lock);
634 ptlrpcd_add_req(req);
639 * Allocate and prepare RPC for a new change.
641 * The function allocates and initializes an RPC which will be sent soon to
642 * apply the change to the target OST. The request is initialized from the
643 * llog record passed. Notice only the fields common to all type of changes
646 * \param[in] d OSP device
647 * \param[in] llh llog handle where the record is stored
648 * \param[in] h llog record
649 * \param[in] op type of the change
650 * \param[in] format request format to be used
652 * \retval pointer new request on success
653 * \retval ERR_PTR(errno) on error
655 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
656 struct llog_handle *llh,
657 struct llog_rec_hdr *h,
659 const struct req_format *format)
661 struct ptlrpc_request *req;
662 struct ost_body *body;
663 struct obd_import *imp;
666 /* Prepare the request */
667 imp = d->opd_obd->u.cli.cl_import;
670 if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_ENOMEM))
671 RETURN(ERR_PTR(-ENOMEM));
673 req = ptlrpc_request_alloc(imp, format);
675 RETURN(ERR_PTR(-ENOMEM));
677 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
679 ptlrpc_req_finished(req);
684 * this is a trick: to save on memory allocations we put cookie
685 * into the request, but don't set corresponded flag in o_valid
686 * so that OST doesn't interpret this cookie. once the request
687 * is committed on OST we take cookie from the request and cancel
689 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
691 body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
692 body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
693 body->oa.o_lcookie.lgc_index = h->lrh_index;
695 req->rq_interpret_reply = osp_sync_interpret;
696 req->rq_commit_cb = osp_sync_request_commit_cb;
699 ptlrpc_request_set_replen(req);
705 * Generate a request for setattr change.
707 * The function prepares a new RPC, initializes it with setattr specific
708 * bits and send the RPC.
710 * \param[in] d OSP device
711 * \param[in] llh llog handle where the record is stored
712 * \param[in] h llog record
714 * \retval 0 on success
715 * \retval 1 on invalid record
716 * \retval negative negated errno on error
718 static int osp_sync_new_setattr_job(struct osp_device *d,
719 struct llog_handle *llh,
720 struct llog_rec_hdr *h)
722 struct llog_setattr64_rec *rec = (struct llog_setattr64_rec *)h;
723 struct ptlrpc_request *req;
724 struct ost_body *body;
727 LASSERT(h->lrh_type == MDS_SETATTR64_REC);
729 if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_INVALID_REC))
731 /* lsr_valid can only be 0 or have OBD_MD_{FLUID,FLGID} set,
732 * so no bits other than these should be set. */
733 if ((rec->lsr_valid & ~(OBD_MD_FLUID | OBD_MD_FLGID)) != 0) {
734 CERROR("%s: invalid setattr record, lsr_valid:"LPU64"\n",
735 d->opd_obd->obd_name, rec->lsr_valid);
736 /* return 1 on invalid record */
740 req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
742 RETURN(PTR_ERR(req));
744 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
746 body->oa.o_oi = rec->lsr_oi;
747 body->oa.o_uid = rec->lsr_uid;
748 body->oa.o_gid = rec->lsr_gid;
749 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
750 /* old setattr record (prior 2.6.0) doesn't have 'valid' stored,
751 * we assume that both UID and GID are valid in that case. */
752 if (rec->lsr_valid == 0)
753 body->oa.o_valid |= (OBD_MD_FLUID | OBD_MD_FLGID);
755 body->oa.o_valid |= rec->lsr_valid;
757 osp_sync_send_new_rpc(d, req);
762 * Generate a request for unlink change.
764 * The function prepares a new RPC, initializes it with unlink(destroy)
765 * specific bits and sends the RPC. The function is used to handle
766 * llog_unlink_rec which were used in the older versions of Lustre.
767 * Current version uses llog_unlink_rec64.
769 * \param[in] d OSP device
770 * \param[in] llh llog handle where the record is stored
771 * \param[in] h llog record
773 * \retval 0 on success
774 * \retval negative negated errno on error
776 static int osp_sync_new_unlink_job(struct osp_device *d,
777 struct llog_handle *llh,
778 struct llog_rec_hdr *h)
780 struct llog_unlink_rec *rec = (struct llog_unlink_rec *)h;
781 struct ptlrpc_request *req;
782 struct ost_body *body;
785 LASSERT(h->lrh_type == MDS_UNLINK_REC);
787 req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
789 RETURN(PTR_ERR(req));
791 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
793 ostid_set_seq(&body->oa.o_oi, rec->lur_oseq);
794 ostid_set_id(&body->oa.o_oi, rec->lur_oid);
795 body->oa.o_misc = rec->lur_count;
796 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
798 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
800 osp_sync_send_new_rpc(d, req);
805 * Generate a request for unlink change.
807 * The function prepares a new RPC, initializes it with unlink(destroy)
808 * specific bits and sends the RPC. Depending on the target (MDT or OST)
809 * two different protocols are used. For MDT we use OUT (basically OSD API
810 * updates transferred via a network). For OST we still use the old
811 * protocol (OBD?), originally for compatibility. Later we can start to
812 * use OUT for OST as well, this will allow batching and better code
815 * \param[in] d OSP device
816 * \param[in] llh llog handle where the record is stored
817 * \param[in] h llog record
819 * \retval 0 on success
820 * \retval negative negated errno on error
822 static int osp_sync_new_unlink64_job(struct osp_device *d,
823 struct llog_handle *llh,
824 struct llog_rec_hdr *h)
826 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
827 struct ptlrpc_request *req = NULL;
828 struct ost_body *body;
832 LASSERT(h->lrh_type == MDS_UNLINK64_REC);
833 req = osp_sync_new_job(d, llh, h, OST_DESTROY,
836 RETURN(PTR_ERR(req));
838 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
841 rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
844 body->oa.o_misc = rec->lur_count;
845 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
847 osp_sync_send_new_rpc(d, req);
852 * Process llog records.
854 * This function is called to process the llog records committed locally.
855 * In the recovery model used by OSP we can apply a change to a remote
856 * target once corresponding transaction (like posix unlink) is committed
857 * locally so can't revert.
858 * Depending on the llog record type, a given handler is called that is
859 * responsible for preparing and sending the RPC to apply the change.
860 * Special record type LLOG_GEN_REC marking a reboot is cancelled right away.
862 * \param[in] env LU environment provided by the caller
863 * \param[in] d OSP device
864 * \param[in] llh llog handle where the record is stored
865 * \param[in] rec llog record
867 static void osp_sync_process_record(const struct lu_env *env,
868 struct osp_device *d,
869 struct llog_handle *llh,
870 struct llog_rec_hdr *rec)
872 struct llog_handle *cathandle = llh->u.phd.phd_cat_handle;
873 struct llog_cookie cookie;
878 cookie.lgc_lgl = llh->lgh_id;
879 cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
880 cookie.lgc_index = rec->lrh_index;
882 if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
883 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
885 /* we're waiting for the record generated by this instance */
886 LASSERT(d->opd_syn_prev_done == 0);
887 if (!memcmp(&d->opd_syn_generation, &gen->lgr_gen,
888 sizeof(gen->lgr_gen))) {
889 CDEBUG(D_HA, "processed all old entries\n");
890 d->opd_syn_prev_done = 1;
893 /* cancel any generation record */
894 rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
900 * now we prepare and fill requests to OST, put them on the queue
901 * and fire after next commit callback
904 /* notice we increment counters before sending RPC, to be consistent
905 * in RPC interpret callback which may happen very quickly */
906 atomic_inc(&d->opd_syn_rpc_in_flight);
907 atomic_inc(&d->opd_syn_rpc_in_progress);
909 switch (rec->lrh_type) {
910 /* case MDS_UNLINK_REC is kept for compatibility */
912 rc = osp_sync_new_unlink_job(d, llh, rec);
914 case MDS_UNLINK64_REC:
915 rc = osp_sync_new_unlink64_job(d, llh, rec);
917 case MDS_SETATTR64_REC:
918 rc = osp_sync_new_setattr_job(d, llh, rec);
921 CERROR("%s: unknown record type: %x\n", d->opd_obd->obd_name,
923 /* treat "unknown record type" as "invalid" */
928 /* For all kinds of records, not matter successful or not,
929 * we should decrease changes and bump last_processed_id.
931 if (d->opd_syn_prev_done) {
932 __u64 correct_id = osp_sync_correct_id(d, rec);
933 LASSERT(atomic_read(&d->opd_syn_changes) > 0);
934 LASSERT(correct_id <= d->opd_syn_last_committed_id);
935 /* NOTE: it's possible to meet same id if
936 * OST stores few stripes of same file
939 /* another thread may be trying to set new value */
941 if (correct_id > d->opd_syn_last_processed_id) {
942 d->opd_syn_last_processed_id = correct_id;
943 wake_up(&d->opd_syn_barrier_waitq);
947 atomic_dec(&d->opd_syn_changes);
950 atomic_dec(&d->opd_syn_rpc_in_flight);
951 atomic_dec(&d->opd_syn_rpc_in_progress);
954 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
955 d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
956 atomic_read(&d->opd_syn_rpc_in_progress));
958 /* Delete the invalid record */
960 rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
962 CERROR("%s: can't delete invalid record: "
963 "fid = "DFID", rec_id = %u, rc = %d\n",
964 d->opd_obd->obd_name,
965 PFID(lu_object_fid(&cathandle->lgh_obj->do_lu)),
969 CDEBUG(D_HA, "found record %x, %d, idx %u, id %u\n",
970 rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id);
976 * Cancel llog records for the committed changes.
978 * The function walks through the list of the committed RPCs and cancels
979 * corresponding llog records. see osp_sync_request_commit_cb() for the
982 * \param[in] env LU environment provided by the caller
983 * \param[in] d OSP device
985 static void osp_sync_process_committed(const struct lu_env *env,
986 struct osp_device *d)
988 struct obd_device *obd = d->opd_obd;
989 struct obd_import *imp = obd->u.cli.cl_import;
990 struct ost_body *body;
991 struct ptlrpc_request *req;
992 struct llog_ctxt *ctxt;
993 struct llog_handle *llh;
994 struct list_head list;
999 if (list_empty(&d->opd_syn_committed_there))
1003 * if current status is -ENOSPC (lack of free space on OST)
1004 * then we should poll OST immediately once object destroy
1006 * notice: we do this upon commit as well because some backends
1007 * (like DMU) do not release space right away.
1009 if (d->opd_pre != NULL && unlikely(d->opd_pre_status == -ENOSPC))
1010 osp_statfs_need_now(d);
1013 * now cancel them all
1014 * XXX: can we improve this using some batching?
1015 * with batch RPC that'll happen automatically?
1016 * XXX: can we store ctxt in lod_device and save few cycles ?
1018 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1021 llh = ctxt->loc_handle;
1024 INIT_LIST_HEAD(&list);
1025 spin_lock(&d->opd_syn_lock);
1026 list_splice(&d->opd_syn_committed_there, &list);
1027 INIT_LIST_HEAD(&d->opd_syn_committed_there);
1028 spin_unlock(&d->opd_syn_lock);
1030 while (!list_empty(&list)) {
1031 struct osp_job_req_args *jra;
1033 jra = list_entry(list.next, struct osp_job_req_args,
1034 jra_committed_link);
1035 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
1036 list_del_init(&jra->jra_committed_link);
1038 req = container_of((void *)jra, struct ptlrpc_request,
1040 body = req_capsule_client_get(&req->rq_pill,
1043 /* import can be closing, thus all commit cb's are
1044 * called we can check committness directly */
1045 if (req->rq_import_generation == imp->imp_generation) {
1046 rc = llog_cat_cancel_records(env, llh, 1,
1047 &body->oa.o_lcookie);
1049 CERROR("%s: can't cancel record: %d\n",
1052 DEBUG_REQ(D_OTHER, req, "imp_committed = "LPU64,
1053 imp->imp_peer_committed_transno);
1055 ptlrpc_req_finished(req);
1059 llog_ctxt_put(ctxt);
1061 LASSERT(atomic_read(&d->opd_syn_rpc_in_progress) >= done);
1062 atomic_sub(done, &d->opd_syn_rpc_in_progress);
1063 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
1064 d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_flight),
1065 atomic_read(&d->opd_syn_rpc_in_progress));
1067 osp_sync_check_for_work(d);
1069 /* wake up the thread if requested to stop:
1070 * it might be waiting for in-progress to complete */
1071 if (unlikely(osp_sync_running(d) == 0))
1072 wake_up(&d->opd_syn_waitq);
1078 * The core of the syncing mechanism.
1080 * This is a callback called by the llog processing function. Essentially it
1081 * suspends llog processing until there is a record to process (it's supposed
1082 * to be committed locally). The function handles RPCs committed by the target
1083 * and cancels corresponding llog records.
1085 * \param[in] env LU environment provided by the caller
1086 * \param[in] llh llog handle we're processing
1087 * \param[in] rec current llog record
1088 * \param[in] data callback data containing a pointer to the device
1090 * \retval 0 to ask the caller (llog_process()) to continue
1091 * \retval LLOG_PROC_BREAK to ask the caller to break
1093 static int osp_sync_process_queues(const struct lu_env *env,
1094 struct llog_handle *llh,
1095 struct llog_rec_hdr *rec,
1098 struct osp_device *d = data;
1101 struct l_wait_info lwi = { 0 };
1103 if (!osp_sync_running(d)) {
1104 CDEBUG(D_HA, "stop llog processing\n");
1105 return LLOG_PROC_BREAK;
1108 /* process requests committed by OST */
1109 osp_sync_process_committed(env, d);
1111 /* if we there are changes to be processed and we have
1112 * resources for this ... do now */
1113 if (osp_sync_can_process_new(d, rec)) {
1115 /* ask llog for another record */
1116 CDEBUG(D_HA, "%u changes, %u in progress,"
1118 atomic_read(&d->opd_syn_changes),
1119 atomic_read(&d->opd_syn_rpc_in_progress),
1120 atomic_read(&d->opd_syn_rpc_in_flight));
1123 osp_sync_process_record(env, d, llh, rec);
1128 if (d->opd_syn_last_processed_id == d->opd_syn_last_used_id)
1129 osp_sync_remove_from_tracker(d);
1131 l_wait_event(d->opd_syn_waitq,
1132 !osp_sync_running(d) ||
1133 osp_sync_can_process_new(d, rec) ||
1134 !list_empty(&d->opd_syn_committed_there),
1142 * This thread runs llog_cat_process() scanner calling our callback
1143 * to process llog records. in the callback we implement tricky
1144 * state machine as we don't want to start scanning of the llog again
1145 * and again, also we don't want to process too many records and send
1146 * too many RPCs a time. so, depending on current load (num of changes
1147 * being synced to OST) the callback can suspend awaiting for some
1148 * new conditions, like syncs completed.
1150 * In order to process llog records left by previous boots and to allow
1151 * llog_process_thread() to find something (otherwise it'd just exit
1152 * immediately) we add a special GENERATATION record on each boot.
1154 * \param[in] _arg a pointer to thread's arguments
1156 * \retval 0 on success
1157 * \retval negative negated errno on error
1159 static int osp_sync_thread(void *_arg)
1161 struct osp_device *d = _arg;
1162 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1163 struct l_wait_info lwi = { 0 };
1164 struct llog_ctxt *ctxt;
1165 struct obd_device *obd = d->opd_obd;
1166 struct llog_handle *llh;
1172 rc = lu_env_init(&env, LCT_LOCAL);
1174 CERROR("%s: can't initialize env: rc = %d\n",
1179 spin_lock(&d->opd_syn_lock);
1180 thread->t_flags = SVC_RUNNING;
1181 spin_unlock(&d->opd_syn_lock);
1182 wake_up(&thread->t_ctl_waitq);
1184 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1186 CERROR("can't get appropriate context\n");
1187 GOTO(out, rc = -EINVAL);
1190 llh = ctxt->loc_handle;
1192 CERROR("can't get llh\n");
1193 llog_ctxt_put(ctxt);
1194 GOTO(out, rc = -EINVAL);
1197 rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
1199 CERROR("%s: llog process with osp_sync_process_queues "
1200 "failed: %d\n", d->opd_obd->obd_name, rc);
1203 LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
1204 "%u changes, %u in progress, %u in flight: %d\n",
1205 atomic_read(&d->opd_syn_changes),
1206 atomic_read(&d->opd_syn_rpc_in_progress),
1207 atomic_read(&d->opd_syn_rpc_in_flight), rc);
1209 /* we don't expect llog_process_thread() to exit till umount */
1210 LASSERTF(thread->t_flags != SVC_RUNNING,
1211 "%u changes, %u in progress, %u in flight\n",
1212 atomic_read(&d->opd_syn_changes),
1213 atomic_read(&d->opd_syn_rpc_in_progress),
1214 atomic_read(&d->opd_syn_rpc_in_flight));
1216 /* wait till all the requests are completed */
1218 while (atomic_read(&d->opd_syn_rpc_in_progress) > 0) {
1219 osp_sync_process_committed(&env, d);
1221 lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
1222 rc = l_wait_event(d->opd_syn_waitq,
1223 atomic_read(&d->opd_syn_rpc_in_progress) == 0,
1225 if (rc == -ETIMEDOUT)
1227 LASSERTF(count < 10, "%s: %d %d %sempty\n",
1228 d->opd_obd->obd_name,
1229 atomic_read(&d->opd_syn_rpc_in_progress),
1230 atomic_read(&d->opd_syn_rpc_in_flight),
1231 list_empty(&d->opd_syn_committed_there) ? "" : "!");
1236 llog_cat_close(&env, llh);
1237 rc = llog_cleanup(&env, ctxt);
1239 CERROR("can't cleanup llog: %d\n", rc);
1241 LASSERTF(atomic_read(&d->opd_syn_rpc_in_progress) == 0,
1242 "%s: %d %d %sempty\n",
1243 d->opd_obd->obd_name, atomic_read(&d->opd_syn_rpc_in_progress),
1244 atomic_read(&d->opd_syn_rpc_in_flight),
1245 list_empty(&d->opd_syn_committed_there) ? "" : "!");
1247 thread->t_flags = SVC_STOPPED;
1249 wake_up(&thread->t_ctl_waitq);
1259 * Initializes the llog. Specific llog to be used depends on the type of the
1260 * target OSP represents (OST or MDT). The function adds appends a new llog
1261 * record to mark the place where the records associated with this boot
1264 * \param[in] env LU environment provided by the caller
1265 * \param[in] d OSP device
1267 * \retval 0 on success
1268 * \retval negative negated errno on error
1270 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
1272 struct osp_thread_info *osi = osp_env_info(env);
1273 struct lu_fid *fid = &osi->osi_fid;
1274 struct llog_handle *lgh = NULL;
1275 struct obd_device *obd = d->opd_obd;
1276 struct llog_ctxt *ctxt;
1284 * open llog corresponding to our OST
1286 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1287 obd->obd_lvfs_ctxt.dt = d->opd_storage;
1289 lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
1291 rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
1292 &osi->osi_cid, fid);
1294 if (rc != -EFAULT) {
1295 CERROR("%s: can't get id from catalogs: rc = %d\n",
1300 /* After sparse OST indices is supported, the CATALOG file
1301 * may become a sparse file that results in failure on
1302 * reading. Skip this error as the llog will be created
1304 memset(&osi->osi_cid, 0, sizeof(osi->osi_cid));
1308 CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
1309 obd->obd_name, d->opd_index,
1310 POSTID(&osi->osi_cid.lci_logid.lgl_oi),
1311 osi->osi_cid.lci_logid.lgl_ogen);
1313 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT,
1314 d->opd_storage->dd_lu_dev.ld_obd,
1315 &osp_mds_ost_orig_logops);
1319 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1322 if (likely(logid_id(&osi->osi_cid.lci_logid) != 0)) {
1323 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
1325 /* re-create llog if it is missing */
1327 logid_set_id(&osi->osi_cid.lci_logid, 0);
1329 GOTO(out_cleanup, rc);
1332 if (unlikely(logid_id(&osi->osi_cid.lci_logid) == 0)) {
1333 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
1335 GOTO(out_cleanup, rc);
1336 osi->osi_cid.lci_logid = lgh->lgh_id;
1339 LASSERT(lgh != NULL);
1340 ctxt->loc_handle = lgh;
1342 rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
1344 GOTO(out_close, rc);
1346 rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
1347 &osi->osi_cid, fid);
1349 GOTO(out_close, rc);
1352 * put a mark in the llog till which we'll be processing
1353 * old records restless
1355 d->opd_syn_generation.mnt_cnt = cfs_time_current();
1356 d->opd_syn_generation.conn_cnt = cfs_time_current();
1358 osi->osi_hdr.lrh_type = LLOG_GEN_REC;
1359 osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
1361 memcpy(&osi->osi_gen.lgr_gen, &d->opd_syn_generation,
1362 sizeof(osi->osi_gen.lgr_gen));
1364 rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie);
1366 GOTO(out_close, rc);
1367 llog_ctxt_put(ctxt);
1370 llog_cat_close(env, lgh);
1372 llog_cleanup(env, ctxt);
1377 * Cleanup llog used for syncing.
1379 * Closes and cleanups the llog. The function is called when the device is
1382 * \param[in] env LU environment provided by the caller
1383 * \param[in] d OSP device
1385 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
1387 struct llog_ctxt *ctxt;
1389 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
1391 llog_cat_close(env, ctxt->loc_handle);
1392 llog_cleanup(env, ctxt);
1396 * Initialization of the sync component of OSP.
1398 * Initializes the llog and starts a new thread to handle the changes to
1399 * the remote target (OST or MDT).
1401 * \param[in] env LU environment provided by the caller
1402 * \param[in] d OSP device
1404 * \retval 0 on success
1405 * \retval negative negated errno on error
1407 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1409 struct l_wait_info lwi = { 0 };
1410 struct task_struct *task;
1415 rc = osp_sync_id_traction_init(d);
1420 * initialize llog storing changes
1422 rc = osp_sync_llog_init(env, d);
1424 CERROR("%s: can't initialize llog: rc = %d\n",
1425 d->opd_obd->obd_name, rc);
1430 * Start synchronization thread
1432 d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
1433 d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
1434 spin_lock_init(&d->opd_syn_lock);
1435 init_waitqueue_head(&d->opd_syn_waitq);
1436 init_waitqueue_head(&d->opd_syn_barrier_waitq);
1437 init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
1438 INIT_LIST_HEAD(&d->opd_syn_inflight_list);
1439 INIT_LIST_HEAD(&d->opd_syn_committed_there);
1441 task = kthread_run(osp_sync_thread, d, "osp-syn-%u-%u",
1442 d->opd_index, d->opd_group);
1445 CERROR("%s: cannot start sync thread: rc = %d\n",
1446 d->opd_obd->obd_name, rc);
1450 l_wait_event(d->opd_syn_thread.t_ctl_waitq,
1451 osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1455 osp_sync_llog_fini(env, d);
1457 osp_sync_id_traction_fini(d);
1462 * Stop the syncing thread.
1464 * Asks the syncing thread to stop and wait until it's stopped.
1466 * \param[in] d OSP device
1470 int osp_sync_fini(struct osp_device *d)
1472 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1476 thread->t_flags = SVC_STOPPING;
1477 wake_up(&d->opd_syn_waitq);
1478 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1481 * unregister transaction callbacks only when sync thread
1482 * has finished operations with llog
1484 osp_sync_id_traction_fini(d);
1489 static DEFINE_MUTEX(osp_id_tracker_sem);
1490 static struct list_head osp_id_tracker_list =
1491 LIST_HEAD_INIT(osp_id_tracker_list);
1494 * OSD commit callback.
1496 * The function is used as a local OSD commit callback to track the highest
1497 * committed llog record id. see osp_sync_id_traction_init() for the details.
1499 * \param[in] th local transaction handle committed
1500 * \param[in] cookie commit callback data (our private structure)
1502 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
1504 struct osp_id_tracker *tr = cookie;
1505 struct osp_device *d;
1506 struct osp_txn_info *txn;
1510 txn = osp_txn_info(&th->th_ctx);
1511 if (txn == NULL || txn->oti_current_id < tr->otr_committed_id)
1514 spin_lock(&tr->otr_lock);
1515 if (likely(txn->oti_current_id > tr->otr_committed_id)) {
1516 CDEBUG(D_OTHER, "committed: "LPU64" -> "LPU64"\n",
1517 tr->otr_committed_id, txn->oti_current_id);
1518 tr->otr_committed_id = txn->oti_current_id;
1520 list_for_each_entry(d, &tr->otr_wakeup_list,
1522 d->opd_syn_last_committed_id = tr->otr_committed_id;
1523 wake_up(&d->opd_syn_waitq);
1526 spin_unlock(&tr->otr_lock);
1530 * Initialize commit tracking mechanism.
1532 * Some setups may have thousands of OSTs and each will be represented by OSP.
1533 * Meaning order of magnitute many more changes to apply every second. In order
1534 * to keep the number of commit callbacks low this mechanism was introduced.
1535 * The mechanism is very similar to transno used by MDT service: it's an single
1536 * ID stream which can be assigned by any OSP to its llog records. The tricky
1537 * part is that ID is stored in per-transaction data and re-used by all the OSPs
1538 * involved in that transaction. Then all these OSPs are woken up utilizing a single OSD commit callback.
1540 * The function initializes the data used by the tracker described above.
1541 * A singler tracker per OSD device is created.
1543 * \param[in] d OSP device
1545 * \retval 0 on success
1546 * \retval negative negated errno on error
1548 static int osp_sync_id_traction_init(struct osp_device *d)
1550 struct osp_id_tracker *tr, *found = NULL;
1554 LASSERT(d->opd_storage);
1555 LASSERT(d->opd_syn_tracker == NULL);
1556 INIT_LIST_HEAD(&d->opd_syn_ontrack);
1558 mutex_lock(&osp_id_tracker_sem);
1559 list_for_each_entry(tr, &osp_id_tracker_list, otr_list) {
1560 if (tr->otr_dev == d->opd_storage) {
1561 LASSERT(atomic_read(&tr->otr_refcount));
1562 atomic_inc(&tr->otr_refcount);
1563 d->opd_syn_tracker = tr;
1569 if (found == NULL) {
1573 d->opd_syn_tracker = tr;
1574 spin_lock_init(&tr->otr_lock);
1575 tr->otr_dev = d->opd_storage;
1576 tr->otr_next_id = 1;
1577 tr->otr_committed_id = 0;
1578 atomic_set(&tr->otr_refcount, 1);
1579 INIT_LIST_HEAD(&tr->otr_wakeup_list);
1580 list_add(&tr->otr_list, &osp_id_tracker_list);
1581 tr->otr_tx_cb.dtc_txn_commit =
1582 osp_sync_tracker_commit_cb;
1583 tr->otr_tx_cb.dtc_cookie = tr;
1584 tr->otr_tx_cb.dtc_tag = LCT_MD_THREAD;
1585 dt_txn_callback_add(d->opd_storage, &tr->otr_tx_cb);
1589 mutex_unlock(&osp_id_tracker_sem);
1595 * Release commit tracker.
1597 * Decrease a refcounter on the tracker used by the given OSP device \a d.
1598 * If no more users left, then the tracker is released.
1600 * \param[in] d OSP device
1602 static void osp_sync_id_traction_fini(struct osp_device *d)
1604 struct osp_id_tracker *tr;
1609 tr = d->opd_syn_tracker;
1615 osp_sync_remove_from_tracker(d);
1617 mutex_lock(&osp_id_tracker_sem);
1618 if (atomic_dec_and_test(&tr->otr_refcount)) {
1619 dt_txn_callback_del(d->opd_storage, &tr->otr_tx_cb);
1620 LASSERT(list_empty(&tr->otr_wakeup_list));
1621 list_del(&tr->otr_list);
1623 d->opd_syn_tracker = NULL;
1625 mutex_unlock(&osp_id_tracker_sem);
1631 * Generate a new ID on a tracker.
1633 * Generates a new ID using the tracker associated with the given OSP device
1634 * \a d, if the given ID \a id is non-zero. Unconditially adds OSP device to
1635 * the wakeup list, so OSP won't miss when a transaction using the ID is
1638 * \param[in] d OSP device
1639 * \param[in] id 0 or ID generated previously
1641 * \retval ID the caller should use
1643 static __u64 osp_sync_id_get(struct osp_device *d, __u64 id)
1645 struct osp_id_tracker *tr;
1647 tr = d->opd_syn_tracker;
1650 /* XXX: we can improve this introducing per-cpu preallocated ids? */
1651 spin_lock(&tr->otr_lock);
1652 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_TRACK_OVERFLOW))
1653 tr->otr_next_id = 0xfffffff0;
1655 if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
1656 spin_unlock(&tr->otr_lock);
1657 CERROR("%s: next "LPU64", last synced "LPU64"\n",
1658 d->opd_obd->obd_name, tr->otr_next_id,
1659 d->opd_syn_last_used_id);
1664 id = tr->otr_next_id++;
1665 if (id > d->opd_syn_last_used_id)
1666 d->opd_syn_last_used_id = id;
1667 if (list_empty(&d->opd_syn_ontrack))
1668 list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
1669 spin_unlock(&tr->otr_lock);
1670 CDEBUG(D_OTHER, "new id "LPU64"\n", id);
1676 * Stop to propagate commit status to OSP.
1678 * If the OSP does not have any llog records she's waiting to commit, then
1679 * it is possible to unsubscribe from wakeups from the tracking using this
1682 * \param[in] d OSP device not willing to wakeup
1684 static void osp_sync_remove_from_tracker(struct osp_device *d)
1686 struct osp_id_tracker *tr;
1688 tr = d->opd_syn_tracker;
1691 if (list_empty(&d->opd_syn_ontrack))
1694 spin_lock(&tr->otr_lock);
1695 list_del_init(&d->opd_syn_ontrack);
1696 spin_unlock(&tr->otr_lock);