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) 2011, 2012, Intel, Inc.
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>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <lustre_log.h>
50 #include "osp_internal.h"
52 static int osp_sync_id_traction_init(struct osp_device *d);
53 static void osp_sync_id_traction_fini(struct osp_device *d);
54 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id);
55 static void osp_sync_remove_from_tracker(struct osp_device *d);
58 * this is a components of OSP implementing synchronization between MDS and OST
59 * it llogs all interesting changes (currently it's uig/gid change and object
60 * destroy) atomically, then makes sure changes hit OST storage
62 * we have 4 queues of work:
64 * the first queue is llog itself, once read a change is stored in 2nd queue
65 * in form of RPC (but RPC isn't fired yet).
67 * the second queue (opd_syn_waiting_for_commit) holds changes awaiting local
68 * commit. once change is committed locally it migrates onto 3rd queue.
70 * the third queue (opd_syn_committed_here) holds changes committed locally,
71 * but not sent to OST (as the pipe can be full). once pipe becomes non-full
72 * we take a change from the queue and fire corresponded RPC.
74 * once RPC is reported committed by OST (using regular last_committed mech.)
75 * the change jumps into 4th queue (opd_syn_committed_there), now we can
76 * cancel corresponded llog record and release RPC
78 * opd_syn_changes is a number of unread llog records (to be processed).
79 * notice this number doesn't include llog records from previous boots.
80 * with OSP_SYN_THRESHOLD we try to batch processing a bit (TO BE IMPLEMENTED)
82 * opd_syn_rpc_in_progress is a number of requests in 2-4 queues.
83 * we control this with OSP_MAX_IN_PROGRESS so that OSP don't consume
84 * too much memory -- how to deal with 1000th OSTs ? batching could help?
86 * opd_syn_rpc_in_flight is a number of RPC in flight.
87 * we control this with OSP_MAX_IN_FLIGHT
90 /* XXX: do math to learn reasonable threshold
91 * should it be ~ number of changes fitting bulk? */
93 #define OSP_SYN_THRESHOLD 10
94 #define OSP_MAX_IN_FLIGHT 8
95 #define OSP_MAX_IN_PROGRESS 4096
97 #define OSP_JOB_MAGIC 0x26112005
99 static inline int osp_sync_running(struct osp_device *d)
101 return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
104 static inline int osp_sync_stopped(struct osp_device *d)
106 return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
109 static inline int osp_sync_has_new_job(struct osp_device *d)
111 return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
112 (d->opd_syn_last_processed_id < d->opd_syn_last_committed_id))
113 || (d->opd_syn_prev_done == 0);
116 static inline int osp_sync_low_in_progress(struct osp_device *d)
118 return d->opd_syn_rpc_in_progress < d->opd_syn_max_rpc_in_progress;
121 static inline int osp_sync_low_in_flight(struct osp_device *d)
123 return d->opd_syn_rpc_in_flight < d->opd_syn_max_rpc_in_flight;
126 static inline int osp_sync_has_work(struct osp_device *d)
128 /* has new/old changes and low in-progress? */
129 if (osp_sync_has_new_job(d) && osp_sync_low_in_progress(d) &&
130 osp_sync_low_in_flight(d) && d->opd_imp_connected)
133 /* has remotely committed? */
134 if (!cfs_list_empty(&d->opd_syn_committed_there))
140 #define osp_sync_check_for_work(d) \
142 if (osp_sync_has_work(d)) { \
143 cfs_waitq_signal(&d->opd_syn_waitq); \
147 void __osp_sync_check_for_work(struct osp_device *d)
149 osp_sync_check_for_work(d);
152 static inline int osp_sync_can_process_new(struct osp_device *d,
153 struct llog_rec_hdr *rec)
157 if (!osp_sync_low_in_progress(d))
159 if (!osp_sync_low_in_flight(d))
161 if (!d->opd_imp_connected)
163 if (d->opd_syn_prev_done == 0)
165 if (d->opd_syn_changes == 0)
167 if (rec == NULL || rec->lrh_id <= d->opd_syn_last_committed_id)
172 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
173 llog_op_type type, struct thandle *th)
175 struct osp_thread_info *osi = osp_env_info(env);
176 struct osp_device *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
177 struct llog_ctxt *ctxt;
182 /* it's a layering violation, to access internals of th,
183 * but we can do this as a sanity check, for a while */
184 LASSERT(th->th_dev == d->opd_storage);
187 case MDS_UNLINK64_REC:
188 osi->osi_hdr.lrh_len = sizeof(struct llog_unlink64_rec);
190 case MDS_SETATTR64_REC:
191 osi->osi_hdr.lrh_len = sizeof(struct llog_setattr64_rec);
197 /* we want ->dt_trans_start() to allocate per-thandle structure */
198 th->th_tags |= LCT_OSP_THREAD;
200 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
203 rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr, th);
209 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
210 const struct lu_fid *fid, llog_op_type type,
211 int count, struct thandle *th,
212 const struct lu_attr *attr)
214 struct osp_thread_info *osi = osp_env_info(env);
215 struct llog_ctxt *ctxt;
216 struct osp_txn_info *txn;
221 /* it's a layering violation, to access internals of th,
222 * but we can do this as a sanity check, for a while */
223 LASSERT(th->th_dev == d->opd_storage);
226 case MDS_UNLINK64_REC:
227 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
228 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
229 osi->osi_unlink.lur_fid = *fid;
230 osi->osi_unlink.lur_count = count;
232 case MDS_SETATTR64_REC:
233 rc = fid_ostid_pack(fid, &osi->osi_oi);
235 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
236 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
237 osi->osi_setattr.lsr_oid = osi->osi_oi.oi_id;
238 osi->osi_setattr.lsr_oseq = osi->osi_oi.oi_seq;
240 osi->osi_setattr.lsr_uid = attr->la_uid;
241 osi->osi_setattr.lsr_gid = attr->la_gid;
247 txn = osp_txn_info(&th->th_ctx);
250 txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
251 osi->osi_hdr.lrh_id = txn->oti_current_id;
253 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
256 rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
260 CDEBUG(D_OTHER, "%s: new record %lu:%lu:%lu/%lu: %d\n",
261 d->opd_obd->obd_name,
262 (unsigned long) osi->osi_cookie.lgc_lgl.lgl_oid,
263 (unsigned long) osi->osi_cookie.lgc_lgl.lgl_oseq,
264 (unsigned long) osi->osi_cookie.lgc_lgl.lgl_ogen,
265 (unsigned long) osi->osi_cookie.lgc_index, rc);
270 if (likely(rc == 0)) {
271 cfs_spin_lock(&d->opd_syn_lock);
272 d->opd_syn_changes++;
273 cfs_spin_unlock(&d->opd_syn_lock);
279 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
280 llog_op_type type, struct thandle *th,
281 const struct lu_attr *attr)
283 return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
284 lu_object_fid(&o->opo_obj.do_lu), type, 1,
288 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
289 struct lu_fid *fid, int lost, struct thandle *th)
291 return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
295 * it's quite obvious we can't maintain all the structures in the memory:
296 * while OST is down, MDS can be processing thousands and thousands of unlinks
297 * filling persistent llogs and in-core respresentation
299 * this doesn't scale at all. so we need basically the following:
300 * a) destroy/setattr append llog records
301 * b) once llog has grown to X records, we process first Y committed records
303 * once record R is found via llog_process(), it becomes committed after any
304 * subsequent commit callback (at the most)
308 * called for each atomic on-disk change (not once per transaction batch)
309 * and goes over the list
310 * XXX: should be optimized?
314 * called for each RPC reported committed
316 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
318 struct osp_device *d = req->rq_cb_data;
320 CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
322 if (unlikely(req->rq_transno == 0))
325 /* XXX: what if request isn't committed for very long? */
327 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
328 LASSERT(cfs_list_empty(&req->rq_exp_list));
330 ptlrpc_request_addref(req);
332 cfs_spin_lock(&d->opd_syn_lock);
333 cfs_list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
334 cfs_spin_unlock(&d->opd_syn_lock);
336 /* XXX: some batching wouldn't hurt */
337 cfs_waitq_signal(&d->opd_syn_waitq);
340 static int osp_sync_interpret(const struct lu_env *env,
341 struct ptlrpc_request *req, void *aa, int rc)
343 struct osp_device *d = req->rq_cb_data;
345 /* XXX: error handling here */
346 if (req->rq_svc_thread != (void *) OSP_JOB_MAGIC)
347 DEBUG_REQ(D_ERROR, req, "bad magic %p\n", req->rq_svc_thread);
348 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
351 CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
352 cfs_atomic_read(&req->rq_refcount),
353 rc, (unsigned) req->rq_transno);
354 LASSERT(rc || req->rq_transno);
358 * we tried to destroy object or update attributes,
359 * but object doesn't exist anymore - cancell llog record
361 LASSERT(req->rq_transno == 0);
362 LASSERT(cfs_list_empty(&req->rq_exp_list));
364 ptlrpc_request_addref(req);
366 cfs_spin_lock(&d->opd_syn_lock);
367 cfs_list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
368 cfs_spin_unlock(&d->opd_syn_lock);
370 cfs_waitq_signal(&d->opd_syn_waitq);
372 struct obd_import *imp = req->rq_import;
374 * error happened, we'll try to repeat on next boot ?
376 LASSERTF(req->rq_transno == 0 ||
377 req->rq_import_generation < imp->imp_generation,
378 "transno "LPU64", rc %d, gen: req %d, imp %d\n",
379 req->rq_transno, rc, req->rq_import_generation,
380 imp->imp_generation);
381 LASSERT(d->opd_syn_rpc_in_progress > 0);
382 if (req->rq_transno == 0) {
383 /* this is the last time we see the request
384 * if transno is not zero, then commit cb
385 * will be called at some point */
386 cfs_spin_lock(&d->opd_syn_lock);
387 d->opd_syn_rpc_in_progress--;
388 cfs_spin_unlock(&d->opd_syn_lock);
391 cfs_waitq_signal(&d->opd_syn_waitq);
392 } else if (unlikely(d->opd_pre_status == -ENOSPC)) {
394 * if current status is -ENOSPC (lack of free space on OST)
395 * then we should poll OST immediately once object destroy
398 osp_statfs_need_now(d);
401 LASSERT(d->opd_syn_rpc_in_flight > 0);
402 cfs_spin_lock(&d->opd_syn_lock);
403 d->opd_syn_rpc_in_flight--;
404 cfs_spin_unlock(&d->opd_syn_lock);
405 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
406 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
407 d->opd_syn_rpc_in_progress);
409 osp_sync_check_for_work(d);
415 * the function walks through list of committed locally changes
416 * and send them to RPC until the pipe is full
418 static void osp_sync_send_new_rpc(struct osp_device *d,
419 struct ptlrpc_request *req)
421 LASSERT(d->opd_syn_rpc_in_flight <= d->opd_syn_max_rpc_in_flight);
422 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
424 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
427 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
428 struct llog_handle *llh,
429 struct llog_rec_hdr *h,
431 const struct req_format *format)
433 struct ptlrpc_request *req;
434 struct ost_body *body;
435 struct obd_import *imp;
438 /* Prepare the request */
439 imp = d->opd_obd->u.cli.cl_import;
441 req = ptlrpc_request_alloc(imp, format);
443 RETURN(ERR_PTR(-ENOMEM));
445 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
447 ptlrpc_req_finished(req);
452 * this is a trick: to save on memory allocations we put cookie
453 * into the request, but don't set corresponded flag in o_valid
454 * so that OST doesn't interpret this cookie. once the request
455 * is committed on OST we take cookie from the request and cancel
457 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
459 body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
460 body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
461 body->oa.o_lcookie.lgc_index = h->lrh_index;
462 CFS_INIT_LIST_HEAD(&req->rq_exp_list);
463 req->rq_svc_thread = (void *) OSP_JOB_MAGIC;
465 req->rq_interpret_reply = osp_sync_interpret;
466 req->rq_commit_cb = osp_sync_request_commit_cb;
469 ptlrpc_request_set_replen(req);
474 static int osp_sync_new_setattr_job(struct osp_device *d,
475 struct llog_handle *llh,
476 struct llog_rec_hdr *h)
478 struct llog_setattr64_rec *rec = (struct llog_setattr64_rec *)h;
479 struct ptlrpc_request *req;
480 struct ost_body *body;
483 LASSERT(h->lrh_type == MDS_SETATTR64_REC);
485 req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
487 RETURN(PTR_ERR(req));
489 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
491 body->oa.o_id = rec->lsr_oid;
492 body->oa.o_seq = rec->lsr_oseq;
493 body->oa.o_uid = rec->lsr_uid;
494 body->oa.o_gid = rec->lsr_gid;
495 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
496 OBD_MD_FLUID | OBD_MD_FLGID;
498 osp_sync_send_new_rpc(d, req);
502 /* Old records may be in old format, so we handle that too */
503 static int osp_sync_new_unlink_job(struct osp_device *d,
504 struct llog_handle *llh,
505 struct llog_rec_hdr *h)
507 struct llog_unlink_rec *rec = (struct llog_unlink_rec *)h;
508 struct ptlrpc_request *req;
509 struct ost_body *body;
512 LASSERT(h->lrh_type == MDS_UNLINK_REC);
514 req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
516 RETURN(PTR_ERR(req));
518 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
520 body->oa.o_id = rec->lur_oid;
521 body->oa.o_seq = rec->lur_oseq;
522 body->oa.o_misc = rec->lur_count;
523 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
525 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
527 osp_sync_send_new_rpc(d, req);
531 static int osp_sync_new_unlink64_job(struct osp_device *d,
532 struct llog_handle *llh,
533 struct llog_rec_hdr *h)
535 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
536 struct ptlrpc_request *req;
537 struct ost_body *body;
541 LASSERT(h->lrh_type == MDS_UNLINK64_REC);
543 req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
545 RETURN(PTR_ERR(req));
547 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
550 rc = fid_ostid_pack(&rec->lur_fid, &body->oa.o_oi);
553 body->oa.o_misc = rec->lur_count;
554 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID | OBD_MD_FLOBJCOUNT;
556 osp_sync_send_new_rpc(d, req);
560 static int osp_sync_process_record(const struct lu_env *env,
561 struct osp_device *d,
562 struct llog_handle *llh,
563 struct llog_rec_hdr *rec)
565 struct llog_cookie cookie;
568 cookie.lgc_lgl = llh->lgh_id;
569 cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
570 cookie.lgc_index = rec->lrh_index;
572 if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
573 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
575 /* we're waiting for the record generated by this instance */
576 LASSERT(d->opd_syn_prev_done == 0);
577 if (!memcmp(&d->opd_syn_generation, &gen->lgr_gen,
578 sizeof(gen->lgr_gen))) {
579 CDEBUG(D_HA, "processed all old entries\n");
580 d->opd_syn_prev_done = 1;
583 /* cancel any generation record */
584 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
591 * now we prepare and fill requests to OST, put them on the queue
592 * and fire after next commit callback
595 /* notice we increment counters before sending RPC, to be consistent
596 * in RPC interpret callback which may happen very quickly */
597 cfs_spin_lock(&d->opd_syn_lock);
598 d->opd_syn_rpc_in_flight++;
599 d->opd_syn_rpc_in_progress++;
600 cfs_spin_unlock(&d->opd_syn_lock);
602 switch (rec->lrh_type) {
603 /* case MDS_UNLINK_REC is kept for compatibility */
605 rc = osp_sync_new_unlink_job(d, llh, rec);
607 case MDS_UNLINK64_REC:
608 rc = osp_sync_new_unlink64_job(d, llh, rec);
610 case MDS_SETATTR64_REC:
611 rc = osp_sync_new_setattr_job(d, llh, rec);
614 CERROR("unknown record type: %x\n", rec->lrh_type);
619 if (likely(rc == 0)) {
620 cfs_spin_lock(&d->opd_syn_lock);
621 if (d->opd_syn_prev_done) {
622 LASSERT(d->opd_syn_changes > 0);
623 LASSERT(rec->lrh_id <= d->opd_syn_last_committed_id);
625 * NOTE: it's possible to meet same id if
626 * OST stores few stripes of same file
628 if (rec->lrh_id > d->opd_syn_last_processed_id)
629 d->opd_syn_last_processed_id = rec->lrh_id;
631 d->opd_syn_changes--;
633 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
634 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
635 d->opd_syn_rpc_in_progress);
636 cfs_spin_unlock(&d->opd_syn_lock);
638 cfs_spin_lock(&d->opd_syn_lock);
639 d->opd_syn_rpc_in_flight--;
640 d->opd_syn_rpc_in_progress--;
641 cfs_spin_unlock(&d->opd_syn_lock);
644 CDEBUG(D_HA, "found record %x, %d, idx %u, id %u: %d\n",
645 rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id, rc);
649 static void osp_sync_process_committed(const struct lu_env *env,
650 struct osp_device *d)
652 struct obd_device *obd = d->opd_obd;
653 struct obd_import *imp = obd->u.cli.cl_import;
654 struct ost_body *body;
655 struct ptlrpc_request *req, *tmp;
656 struct llog_ctxt *ctxt;
657 struct llog_handle *llh;
663 if (cfs_list_empty(&d->opd_syn_committed_there))
667 * if current status is -ENOSPC (lack of free space on OST)
668 * then we should poll OST immediately once object destroy
670 * notice: we do this upon commit as well because some backends
671 * (like DMU) do not release space right away.
673 if (unlikely(d->opd_pre_status == -ENOSPC))
674 osp_statfs_need_now(d);
677 * now cancel them all
678 * XXX: can we improve this using some batching?
679 * with batch RPC that'll happen automatically?
680 * XXX: can we store ctxt in lod_device and save few cycles ?
682 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
685 llh = ctxt->loc_handle;
688 CFS_INIT_LIST_HEAD(&list);
689 cfs_spin_lock(&d->opd_syn_lock);
690 cfs_list_splice(&d->opd_syn_committed_there, &list);
691 CFS_INIT_LIST_HEAD(&d->opd_syn_committed_there);
692 cfs_spin_unlock(&d->opd_syn_lock);
694 cfs_list_for_each_entry_safe(req, tmp, &list, rq_exp_list) {
695 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
696 cfs_list_del_init(&req->rq_exp_list);
698 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
701 /* import can be closing, thus all commit cb's are
702 * called we can check committness directly */
703 if (req->rq_transno <= imp->imp_peer_committed_transno) {
704 rc = llog_cat_cancel_records(env, llh, 1,
705 &body->oa.o_lcookie);
707 CERROR("%s: can't cancel record: %d\n",
710 DEBUG_REQ(D_HA, req, "not committed");
713 ptlrpc_req_finished(req);
719 LASSERT(d->opd_syn_rpc_in_progress >= done);
720 cfs_spin_lock(&d->opd_syn_lock);
721 d->opd_syn_rpc_in_progress -= done;
722 cfs_spin_unlock(&d->opd_syn_lock);
723 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
724 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
725 d->opd_syn_rpc_in_progress);
727 osp_sync_check_for_work(d);
733 * this is where most of queues processing happens
735 static int osp_sync_process_queues(const struct lu_env *env,
736 struct llog_handle *llh,
737 struct llog_rec_hdr *rec,
740 struct osp_device *d = data;
744 struct l_wait_info lwi = { 0 };
746 if (!osp_sync_running(d)) {
747 CDEBUG(D_HA, "stop llog processing\n");
748 return LLOG_PROC_BREAK;
751 /* process requests committed by OST */
752 osp_sync_process_committed(env, d);
754 /* if we there are changes to be processed and we have
755 * resources for this ... do now */
756 if (osp_sync_can_process_new(d, rec)) {
758 /* ask llog for another record */
759 CDEBUG(D_HA, "%lu changes, %u in progress, %u in flight\n",
761 d->opd_syn_rpc_in_progress,
762 d->opd_syn_rpc_in_flight);
767 * try to send, in case of disconnection, suspend
768 * processing till we can send this request
771 rc = osp_sync_process_record(env, d, llh, rec);
773 * XXX: probably different handling is needed
774 * for some bugs, like immediate exit or if
778 CERROR("can't send: %d\n", rc);
779 l_wait_event(d->opd_syn_waitq,
780 !osp_sync_running(d) ||
781 osp_sync_has_work(d),
784 } while (rc != 0 && osp_sync_running(d));
790 if (d->opd_syn_last_processed_id == d->opd_syn_last_used_id)
791 osp_sync_remove_from_tracker(d);
793 l_wait_event(d->opd_syn_waitq,
794 !osp_sync_running(d) ||
795 osp_sync_can_process_new(d, rec) ||
796 !cfs_list_empty(&d->opd_syn_committed_there),
802 * this thread runs llog_cat_process() scanner calling our callback
803 * to process llog records. in the callback we implement tricky
804 * state machine as we don't want to start scanning of the llog again
805 * and again, also we don't want to process too many records and send
806 * too many RPCs a time. so, depending on current load (num of changes
807 * being synced to OST) the callback can suspend awaiting for some
808 * new conditions, like syncs completed.
810 * in order to process llog records left by previous boots and to allow
811 * llog_process_thread() to find something (otherwise it'd just exit
812 * immediately) we add a special GENERATATION record on each boot.
814 static int osp_sync_thread(void *_arg)
816 struct osp_device *d = _arg;
817 struct ptlrpc_thread *thread = &d->opd_syn_thread;
818 struct l_wait_info lwi = { 0 };
819 struct llog_ctxt *ctxt;
820 struct obd_device *obd = d->opd_obd;
821 struct llog_handle *llh;
828 rc = lu_env_init(&env, LCT_LOCAL);
830 CERROR("%s: can't initialize env: rc = %d\n",
835 sprintf(pname, "osp-syn-%u\n", d->opd_index);
836 cfs_daemonize(pname);
838 cfs_spin_lock(&d->opd_syn_lock);
839 thread->t_flags = SVC_RUNNING;
840 cfs_spin_unlock(&d->opd_syn_lock);
841 cfs_waitq_signal(&thread->t_ctl_waitq);
843 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
845 CERROR("can't get appropriate context\n");
846 GOTO(out, rc = -EINVAL);
849 llh = ctxt->loc_handle;
851 CERROR("can't get llh\n");
853 GOTO(out, rc = -EINVAL);
856 rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
857 LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
858 "%lu changes, %u in progress, %u in flight: %d\n",
859 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
860 d->opd_syn_rpc_in_flight, rc);
862 /* we don't expect llog_process_thread() to exit till umount */
863 LASSERTF(thread->t_flags != SVC_RUNNING,
864 "%lu changes, %u in progress, %u in flight\n",
865 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
866 d->opd_syn_rpc_in_flight);
868 osp_sync_process_committed(&env, d);
870 llog_cat_close(&env, llh);
871 rc = llog_cleanup(&env, ctxt);
873 CERROR("can't cleanup llog: %d\n", rc);
875 thread->t_flags = SVC_STOPPED;
878 * there might be a race between osp sync thread sending RPCs and
879 * import invalidation. this can result in RPCs being in ptlrpcd
880 * till this point. for safete reason let's wait till they are done
882 l_wait_event(d->opd_syn_waitq, d->opd_syn_rpc_in_flight == 0, &lwi);
884 cfs_waitq_signal(&thread->t_ctl_waitq);
885 LASSERTF(d->opd_syn_rpc_in_progress == 0,
886 "%s: %d %d %sempty\n",
887 d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
888 d->opd_syn_rpc_in_flight,
889 cfs_list_empty(&d->opd_syn_committed_there) ? "" : "!");
896 static struct llog_operations osp_mds_ost_orig_logops;
898 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
900 struct osp_thread_info *osi = osp_env_info(env);
901 struct llog_handle *lgh;
902 struct obd_device *obd = d->opd_obd;
903 struct llog_ctxt *ctxt;
911 * open llog corresponding to our OST
913 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
914 obd->obd_lvfs_ctxt.dt = d->opd_storage;
916 rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
919 CERROR("%s: can't get id from catalogs: rc = %d\n",
924 CDEBUG(D_INFO, "%s: Init llog for %d - catid "LPX64"/"LPX64":%x\n",
925 obd->obd_name, d->opd_index, osi->osi_cid.lci_logid.lgl_oid,
926 osi->osi_cid.lci_logid.lgl_oseq,
927 osi->osi_cid.lci_logid.lgl_ogen);
929 osp_mds_ost_orig_logops = llog_osd_ops;
930 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, obd,
931 &osp_mds_ost_orig_logops);
935 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
938 if (likely(osi->osi_cid.lci_logid.lgl_oid != 0)) {
939 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
941 /* re-create llog if it is missing */
943 osi->osi_cid.lci_logid.lgl_oid = 0;
945 GOTO(out_cleanup, rc);
948 if (unlikely(osi->osi_cid.lci_logid.lgl_oid == 0)) {
949 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
951 GOTO(out_cleanup, rc);
952 osi->osi_cid.lci_logid = lgh->lgh_id;
955 ctxt->loc_handle = lgh;
956 lgh->lgh_logops->lop_add = llog_cat_add_rec;
957 lgh->lgh_logops->lop_declare_add = llog_cat_declare_add_rec;
959 rc = llog_cat_init_and_process(env, lgh);
963 rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
969 * put a mark in the llog till which we'll be processing
970 * old records restless
972 d->opd_syn_generation.mnt_cnt = cfs_time_current();
973 d->opd_syn_generation.conn_cnt = cfs_time_current();
975 osi->osi_hdr.lrh_type = LLOG_GEN_REC;
976 osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
978 memcpy(&osi->osi_gen.lgr_gen, &d->opd_syn_generation,
979 sizeof(osi->osi_gen.lgr_gen));
981 rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie,
988 llog_cat_close(env, lgh);
990 llog_cleanup(env, ctxt);
994 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
996 struct llog_ctxt *ctxt;
998 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
999 llog_cat_close(env, ctxt->loc_handle);
1000 llog_cleanup(env, ctxt);
1004 * initializes sync component of OSP
1006 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1008 struct l_wait_info lwi = { 0 };
1013 rc = osp_sync_id_traction_init(d);
1018 * initialize llog storing changes
1020 rc = osp_sync_llog_init(env, d);
1022 CERROR("%s: can't initialize llog: rc = %d\n",
1023 d->opd_obd->obd_name, rc);
1028 * Start synchronization thread
1030 d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
1031 d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
1032 cfs_spin_lock_init(&d->opd_syn_lock);
1033 cfs_waitq_init(&d->opd_syn_waitq);
1034 cfs_waitq_init(&d->opd_syn_thread.t_ctl_waitq);
1035 CFS_INIT_LIST_HEAD(&d->opd_syn_committed_there);
1037 rc = cfs_create_thread(osp_sync_thread, d, 0);
1039 CERROR("%s: can't start sync thread: rc = %d\n",
1040 d->opd_obd->obd_name, rc);
1044 l_wait_event(d->opd_syn_thread.t_ctl_waitq,
1045 osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1049 osp_sync_llog_fini(env, d);
1051 osp_sync_id_traction_fini(d);
1055 int osp_sync_fini(struct osp_device *d)
1057 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1061 thread->t_flags = SVC_STOPPING;
1062 cfs_waitq_signal(&d->opd_syn_waitq);
1063 cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1066 * unregister transaction callbacks only when sync thread
1067 * has finished operations with llog
1069 osp_sync_id_traction_fini(d);
1074 static CFS_DEFINE_MUTEX(osp_id_tracker_sem);
1075 static CFS_LIST_HEAD(osp_id_tracker_list);
1077 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
1079 struct osp_id_tracker *tr = cookie;
1080 struct osp_device *d;
1081 struct osp_txn_info *txn;
1085 txn = osp_txn_info(&th->th_ctx);
1086 if (txn == NULL || txn->oti_current_id < tr->otr_committed_id)
1089 cfs_spin_lock(&tr->otr_lock);
1090 if (likely(txn->oti_current_id > tr->otr_committed_id)) {
1091 CDEBUG(D_OTHER, "committed: %u -> %u\n",
1092 tr->otr_committed_id, txn->oti_current_id);
1093 tr->otr_committed_id = txn->oti_current_id;
1095 cfs_list_for_each_entry(d, &tr->otr_wakeup_list,
1097 d->opd_syn_last_committed_id = tr->otr_committed_id;
1098 cfs_waitq_signal(&d->opd_syn_waitq);
1101 cfs_spin_unlock(&tr->otr_lock);
1104 static int osp_sync_id_traction_init(struct osp_device *d)
1106 struct osp_id_tracker *tr, *found = NULL;
1110 LASSERT(d->opd_storage);
1111 LASSERT(d->opd_syn_tracker == NULL);
1112 CFS_INIT_LIST_HEAD(&d->opd_syn_ontrack);
1114 cfs_mutex_lock(&osp_id_tracker_sem);
1115 cfs_list_for_each_entry(tr, &osp_id_tracker_list, otr_list) {
1116 if (tr->otr_dev == d->opd_storage) {
1117 LASSERT(cfs_atomic_read(&tr->otr_refcount));
1118 cfs_atomic_inc(&tr->otr_refcount);
1119 d->opd_syn_tracker = tr;
1125 if (found == NULL) {
1129 d->opd_syn_tracker = tr;
1130 cfs_spin_lock_init(&tr->otr_lock);
1131 tr->otr_dev = d->opd_storage;
1132 tr->otr_next_id = 1;
1133 tr->otr_committed_id = 0;
1134 cfs_atomic_set(&tr->otr_refcount, 1);
1135 CFS_INIT_LIST_HEAD(&tr->otr_wakeup_list);
1136 cfs_list_add(&tr->otr_list, &osp_id_tracker_list);
1137 tr->otr_tx_cb.dtc_txn_commit =
1138 osp_sync_tracker_commit_cb;
1139 tr->otr_tx_cb.dtc_cookie = tr;
1140 tr->otr_tx_cb.dtc_tag = LCT_MD_THREAD;
1141 dt_txn_callback_add(d->opd_storage, &tr->otr_tx_cb);
1145 cfs_mutex_unlock(&osp_id_tracker_sem);
1150 static void osp_sync_id_traction_fini(struct osp_device *d)
1152 struct osp_id_tracker *tr;
1157 tr = d->opd_syn_tracker;
1163 osp_sync_remove_from_tracker(d);
1165 cfs_mutex_lock(&osp_id_tracker_sem);
1166 if (cfs_atomic_dec_and_test(&tr->otr_refcount)) {
1167 dt_txn_callback_del(d->opd_storage, &tr->otr_tx_cb);
1168 LASSERT(cfs_list_empty(&tr->otr_wakeup_list));
1169 cfs_list_del(&tr->otr_list);
1171 d->opd_syn_tracker = NULL;
1173 cfs_mutex_unlock(&osp_id_tracker_sem);
1179 * generates id for the tracker
1181 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
1183 struct osp_id_tracker *tr;
1185 tr = d->opd_syn_tracker;
1188 /* XXX: we can improve this introducing per-cpu preallocated ids? */
1189 cfs_spin_lock(&tr->otr_lock);
1190 if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
1191 cfs_spin_unlock(&tr->otr_lock);
1192 CERROR("%s: next %u, last synced %lu\n",
1193 d->opd_obd->obd_name, tr->otr_next_id,
1194 d->opd_syn_last_used_id);
1199 id = tr->otr_next_id++;
1200 if (id > d->opd_syn_last_used_id)
1201 d->opd_syn_last_used_id = id;
1202 if (cfs_list_empty(&d->opd_syn_ontrack))
1203 cfs_list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
1204 cfs_spin_unlock(&tr->otr_lock);
1205 CDEBUG(D_OTHER, "new id %u\n", (unsigned) id);
1210 static void osp_sync_remove_from_tracker(struct osp_device *d)
1212 struct osp_id_tracker *tr;
1214 tr = d->opd_syn_tracker;
1217 if (cfs_list_empty(&d->opd_syn_ontrack))
1220 cfs_spin_lock(&tr->otr_lock);
1221 cfs_list_del_init(&d->opd_syn_ontrack);
1222 cfs_spin_unlock(&tr->otr_lock);