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, 2013, 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 <lustre_log.h>
47 #include "osp_internal.h"
49 static int osp_sync_id_traction_init(struct osp_device *d);
50 static void osp_sync_id_traction_fini(struct osp_device *d);
51 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id);
52 static void osp_sync_remove_from_tracker(struct osp_device *d);
55 * this is a components of OSP implementing synchronization between MDS and OST
56 * it llogs all interesting changes (currently it's uig/gid change and object
57 * destroy) atomically, then makes sure changes hit OST storage
59 * we have 4 queues of work:
61 * the first queue is llog itself, once read a change is stored in 2nd queue
62 * in form of RPC (but RPC isn't fired yet).
64 * the second queue (opd_syn_waiting_for_commit) holds changes awaiting local
65 * commit. once change is committed locally it migrates onto 3rd queue.
67 * the third queue (opd_syn_committed_here) holds changes committed locally,
68 * but not sent to OST (as the pipe can be full). once pipe becomes non-full
69 * we take a change from the queue and fire corresponded RPC.
71 * once RPC is reported committed by OST (using regular last_committed mech.)
72 * the change jumps into 4th queue (opd_syn_committed_there), now we can
73 * cancel corresponded llog record and release RPC
75 * opd_syn_changes is a number of unread llog records (to be processed).
76 * notice this number doesn't include llog records from previous boots.
77 * with OSP_SYN_THRESHOLD we try to batch processing a bit (TO BE IMPLEMENTED)
79 * opd_syn_rpc_in_progress is a number of requests in 2-4 queues.
80 * we control this with OSP_MAX_IN_PROGRESS so that OSP don't consume
81 * too much memory -- how to deal with 1000th OSTs ? batching could help?
83 * opd_syn_rpc_in_flight is a number of RPC in flight.
84 * we control this with OSP_MAX_IN_FLIGHT
87 /* XXX: do math to learn reasonable threshold
88 * should it be ~ number of changes fitting bulk? */
90 #define OSP_SYN_THRESHOLD 10
91 #define OSP_MAX_IN_FLIGHT 8
92 #define OSP_MAX_IN_PROGRESS 4096
94 #define OSP_JOB_MAGIC 0x26112005
96 static inline int osp_sync_running(struct osp_device *d)
98 return !!(d->opd_syn_thread.t_flags & SVC_RUNNING);
101 static inline int osp_sync_stopped(struct osp_device *d)
103 return !!(d->opd_syn_thread.t_flags & SVC_STOPPED);
106 static inline int osp_sync_has_new_job(struct osp_device *d)
108 return ((d->opd_syn_last_processed_id < d->opd_syn_last_used_id) &&
109 (d->opd_syn_last_processed_id < d->opd_syn_last_committed_id))
110 || (d->opd_syn_prev_done == 0);
113 static inline int osp_sync_low_in_progress(struct osp_device *d)
115 return d->opd_syn_rpc_in_progress < d->opd_syn_max_rpc_in_progress;
118 static inline int osp_sync_low_in_flight(struct osp_device *d)
120 return d->opd_syn_rpc_in_flight < d->opd_syn_max_rpc_in_flight;
123 static inline int osp_sync_has_work(struct osp_device *d)
125 /* has new/old changes and low in-progress? */
126 if (osp_sync_has_new_job(d) && osp_sync_low_in_progress(d) &&
127 osp_sync_low_in_flight(d) && d->opd_imp_connected)
130 /* has remotely committed? */
131 if (!cfs_list_empty(&d->opd_syn_committed_there))
137 #define osp_sync_check_for_work(d) \
139 if (osp_sync_has_work(d)) { \
140 wake_up(&d->opd_syn_waitq); \
144 void __osp_sync_check_for_work(struct osp_device *d)
146 osp_sync_check_for_work(d);
149 static inline int osp_sync_can_process_new(struct osp_device *d,
150 struct llog_rec_hdr *rec)
154 if (!osp_sync_low_in_progress(d))
156 if (!osp_sync_low_in_flight(d))
158 if (!d->opd_imp_connected)
160 if (d->opd_syn_prev_done == 0)
162 if (d->opd_syn_changes == 0)
164 if (rec == NULL || rec->lrh_id <= d->opd_syn_last_committed_id)
169 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
170 llog_op_type type, struct thandle *th)
172 struct osp_thread_info *osi = osp_env_info(env);
173 struct osp_device *d = lu2osp_dev(o->opo_obj.do_lu.lo_dev);
174 struct llog_ctxt *ctxt;
179 /* it's a layering violation, to access internals of th,
180 * but we can do this as a sanity check, for a while */
181 LASSERT(th->th_dev == d->opd_storage);
184 case MDS_UNLINK64_REC:
185 osi->osi_hdr.lrh_len = sizeof(struct llog_unlink64_rec);
187 case MDS_SETATTR64_REC:
188 osi->osi_hdr.lrh_len = sizeof(struct llog_setattr64_rec);
194 /* we want ->dt_trans_start() to allocate per-thandle structure */
195 th->th_tags |= LCT_OSP_THREAD;
197 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
200 rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr, th);
206 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
207 const struct lu_fid *fid, llog_op_type type,
208 int count, struct thandle *th,
209 const struct lu_attr *attr)
211 struct osp_thread_info *osi = osp_env_info(env);
212 struct llog_ctxt *ctxt;
213 struct osp_txn_info *txn;
218 /* it's a layering violation, to access internals of th,
219 * but we can do this as a sanity check, for a while */
220 LASSERT(th->th_dev == d->opd_storage);
223 case MDS_UNLINK64_REC:
224 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
225 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
226 osi->osi_unlink.lur_fid = *fid;
227 osi->osi_unlink.lur_count = count;
229 case MDS_SETATTR64_REC:
230 rc = fid_to_ostid(fid, &osi->osi_oi);
232 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
233 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
234 osi->osi_setattr.lsr_oi = osi->osi_oi;
236 osi->osi_setattr.lsr_uid = attr->la_uid;
237 osi->osi_setattr.lsr_gid = attr->la_gid;
243 txn = osp_txn_info(&th->th_ctx);
246 txn->oti_current_id = osp_sync_id_get(d, txn->oti_current_id);
247 osi->osi_hdr.lrh_id = txn->oti_current_id;
249 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
252 rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
256 CDEBUG(D_OTHER, "%s: new record "DOSTID":%lu/%lu: %d\n",
257 d->opd_obd->obd_name, POSTID(&osi->osi_cookie.lgc_lgl.lgl_oi),
258 (unsigned long) osi->osi_cookie.lgc_lgl.lgl_ogen,
259 (unsigned long) osi->osi_cookie.lgc_index, rc);
264 if (likely(rc == 0)) {
265 spin_lock(&d->opd_syn_lock);
266 d->opd_syn_changes++;
267 spin_unlock(&d->opd_syn_lock);
273 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
274 llog_op_type type, struct thandle *th,
275 const struct lu_attr *attr)
277 return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
278 lu_object_fid(&o->opo_obj.do_lu), type, 1,
282 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
283 struct lu_fid *fid, int lost, struct thandle *th)
285 return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
289 * it's quite obvious we can't maintain all the structures in the memory:
290 * while OST is down, MDS can be processing thousands and thousands of unlinks
291 * filling persistent llogs and in-core respresentation
293 * this doesn't scale at all. so we need basically the following:
294 * a) destroy/setattr append llog records
295 * b) once llog has grown to X records, we process first Y committed records
297 * once record R is found via llog_process(), it becomes committed after any
298 * subsequent commit callback (at the most)
302 * called for each atomic on-disk change (not once per transaction batch)
303 * and goes over the list
304 * XXX: should be optimized?
308 * called for each RPC reported committed
310 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
312 struct osp_device *d = req->rq_cb_data;
314 CDEBUG(D_HA, "commit req %p, transno "LPU64"\n", req, req->rq_transno);
316 if (unlikely(req->rq_transno == 0))
319 /* do not do any opd_dyn_rpc_* accounting here
320 * it's done in osp_sync_interpret sooner or later */
323 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
324 LASSERT(cfs_list_empty(&req->rq_exp_list));
326 ptlrpc_request_addref(req);
328 spin_lock(&d->opd_syn_lock);
329 cfs_list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
330 spin_unlock(&d->opd_syn_lock);
332 /* XXX: some batching wouldn't hurt */
333 wake_up(&d->opd_syn_waitq);
336 static int osp_sync_interpret(const struct lu_env *env,
337 struct ptlrpc_request *req, void *aa, int rc)
339 struct osp_device *d = req->rq_cb_data;
341 if (req->rq_svc_thread != (void *) OSP_JOB_MAGIC)
342 DEBUG_REQ(D_ERROR, req, "bad magic %p\n", req->rq_svc_thread);
343 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
346 CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
347 cfs_atomic_read(&req->rq_refcount),
348 rc, (unsigned) req->rq_transno);
349 LASSERT(rc || req->rq_transno);
353 * we tried to destroy object or update attributes,
354 * but object doesn't exist anymore - cancell llog record
356 LASSERT(req->rq_transno == 0);
357 LASSERT(cfs_list_empty(&req->rq_exp_list));
359 ptlrpc_request_addref(req);
361 spin_lock(&d->opd_syn_lock);
362 cfs_list_add(&req->rq_exp_list, &d->opd_syn_committed_there);
363 spin_unlock(&d->opd_syn_lock);
365 wake_up(&d->opd_syn_waitq);
367 struct obd_import *imp = req->rq_import;
369 * error happened, we'll try to repeat on next boot ?
371 LASSERTF(req->rq_transno == 0 ||
372 req->rq_import_generation < imp->imp_generation,
373 "transno "LPU64", rc %d, gen: req %d, imp %d\n",
374 req->rq_transno, rc, req->rq_import_generation,
375 imp->imp_generation);
376 if (req->rq_transno == 0) {
377 /* this is the last time we see the request
378 * if transno is not zero, then commit cb
379 * will be called at some point */
380 LASSERT(d->opd_syn_rpc_in_progress > 0);
381 spin_lock(&d->opd_syn_lock);
382 d->opd_syn_rpc_in_progress--;
383 spin_unlock(&d->opd_syn_lock);
386 wake_up(&d->opd_syn_waitq);
387 } else if (d->opd_pre != NULL &&
388 unlikely(d->opd_pre_status == -ENOSPC)) {
390 * if current status is -ENOSPC (lack of free space on OST)
391 * then we should poll OST immediately once object destroy
394 osp_statfs_need_now(d);
397 LASSERT(d->opd_syn_rpc_in_flight > 0);
398 spin_lock(&d->opd_syn_lock);
399 d->opd_syn_rpc_in_flight--;
400 spin_unlock(&d->opd_syn_lock);
401 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
402 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
403 d->opd_syn_rpc_in_progress);
405 osp_sync_check_for_work(d);
411 * the function walks through list of committed locally changes
412 * and send them to RPC until the pipe is full
414 static void osp_sync_send_new_rpc(struct osp_device *d,
415 struct ptlrpc_request *req)
417 LASSERT(d->opd_syn_rpc_in_flight <= d->opd_syn_max_rpc_in_flight);
418 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
420 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
423 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
424 struct llog_handle *llh,
425 struct llog_rec_hdr *h,
427 const struct req_format *format)
429 struct ptlrpc_request *req;
430 struct ost_body *body;
431 struct obd_import *imp;
434 /* Prepare the request */
435 imp = d->opd_obd->u.cli.cl_import;
437 req = ptlrpc_request_alloc(imp, format);
439 RETURN(ERR_PTR(-ENOMEM));
441 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
443 ptlrpc_req_finished(req);
448 * this is a trick: to save on memory allocations we put cookie
449 * into the request, but don't set corresponded flag in o_valid
450 * so that OST doesn't interpret this cookie. once the request
451 * is committed on OST we take cookie from the request and cancel
453 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
455 body->oa.o_lcookie.lgc_lgl = llh->lgh_id;
456 body->oa.o_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
457 body->oa.o_lcookie.lgc_index = h->lrh_index;
458 CFS_INIT_LIST_HEAD(&req->rq_exp_list);
459 req->rq_svc_thread = (void *) OSP_JOB_MAGIC;
461 req->rq_interpret_reply = osp_sync_interpret;
462 req->rq_commit_cb = osp_sync_request_commit_cb;
465 ptlrpc_request_set_replen(req);
470 static int osp_sync_new_setattr_job(struct osp_device *d,
471 struct llog_handle *llh,
472 struct llog_rec_hdr *h)
474 struct llog_setattr64_rec *rec = (struct llog_setattr64_rec *)h;
475 struct ptlrpc_request *req;
476 struct ost_body *body;
479 LASSERT(h->lrh_type == MDS_SETATTR64_REC);
481 req = osp_sync_new_job(d, llh, h, OST_SETATTR, &RQF_OST_SETATTR);
483 RETURN(PTR_ERR(req));
485 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
487 body->oa.o_oi = rec->lsr_oi;
488 body->oa.o_uid = rec->lsr_uid;
489 body->oa.o_gid = rec->lsr_gid;
490 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
491 OBD_MD_FLUID | OBD_MD_FLGID;
493 osp_sync_send_new_rpc(d, req);
497 /* Old records may be in old format, so we handle that too */
498 static int osp_sync_new_unlink_job(struct osp_device *d,
499 struct llog_handle *llh,
500 struct llog_rec_hdr *h)
502 struct llog_unlink_rec *rec = (struct llog_unlink_rec *)h;
503 struct ptlrpc_request *req;
504 struct ost_body *body;
507 LASSERT(h->lrh_type == MDS_UNLINK_REC);
509 req = osp_sync_new_job(d, llh, h, OST_DESTROY, &RQF_OST_DESTROY);
511 RETURN(PTR_ERR(req));
513 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
515 ostid_set_seq(&body->oa.o_oi, rec->lur_oseq);
516 ostid_set_id(&body->oa.o_oi, rec->lur_oid);
517 body->oa.o_misc = rec->lur_count;
518 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
520 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
522 osp_sync_send_new_rpc(d, req);
526 static int osp_prep_unlink_update_req(const struct lu_env *env,
527 struct osp_device *osp,
528 struct llog_handle *llh,
529 struct llog_rec_hdr *h,
530 struct ptlrpc_request **reqp)
532 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
533 struct dt_update_request *update = NULL;
534 struct ptlrpc_request *req;
536 struct llog_cookie lcookie;
541 update = out_create_update_req(&osp->opd_dt_dev);
543 RETURN(PTR_ERR(update));
545 /* This can only happens for unlink slave directory, so decrease
546 * ref for ".." and "." */
547 rc = out_insert_update(env, update, OUT_REF_DEL, &rec->lur_fid, 0,
552 rc = out_insert_update(env, update, OUT_REF_DEL, &rec->lur_fid, 0,
557 lcookie.lgc_lgl = llh->lgh_id;
558 lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
559 lcookie.lgc_index = h->lrh_index;
560 size = sizeof(lcookie);
561 buf = (const char *)&lcookie;
563 rc = out_insert_update(env, update, OUT_DESTROY, &rec->lur_fid, 1,
568 rc = out_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
569 update->dur_req, &req);
573 INIT_LIST_HEAD(&req->rq_exp_list);
574 req->rq_svc_thread = (void *)OSP_JOB_MAGIC;
576 req->rq_interpret_reply = osp_sync_interpret;
577 req->rq_commit_cb = osp_sync_request_commit_cb;
578 req->rq_cb_data = osp;
580 ptlrpc_request_set_replen(req);
584 out_destroy_update_req(update);
589 static int osp_sync_new_unlink64_job(const struct lu_env *env,
590 struct osp_device *d,
591 struct llog_handle *llh,
592 struct llog_rec_hdr *h)
594 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
595 struct ptlrpc_request *req = NULL;
596 struct ost_body *body;
600 LASSERT(h->lrh_type == MDS_UNLINK64_REC);
602 if (d->opd_connect_mdt) {
603 rc = osp_prep_unlink_update_req(env, d, llh, h, &req);
607 req = osp_sync_new_job(d, llh, h, OST_DESTROY,
610 RETURN(PTR_ERR(req));
612 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
615 rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
618 body->oa.o_misc = rec->lur_count;
619 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
622 osp_sync_send_new_rpc(d, req);
626 static int osp_sync_process_record(const struct lu_env *env,
627 struct osp_device *d,
628 struct llog_handle *llh,
629 struct llog_rec_hdr *rec)
631 struct llog_cookie cookie;
634 cookie.lgc_lgl = llh->lgh_id;
635 cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
636 cookie.lgc_index = rec->lrh_index;
638 if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
639 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
641 /* we're waiting for the record generated by this instance */
642 LASSERT(d->opd_syn_prev_done == 0);
643 if (!memcmp(&d->opd_syn_generation, &gen->lgr_gen,
644 sizeof(gen->lgr_gen))) {
645 CDEBUG(D_HA, "processed all old entries\n");
646 d->opd_syn_prev_done = 1;
649 /* cancel any generation record */
650 rc = llog_cat_cancel_records(env, llh->u.phd.phd_cat_handle,
657 * now we prepare and fill requests to OST, put them on the queue
658 * and fire after next commit callback
661 /* notice we increment counters before sending RPC, to be consistent
662 * in RPC interpret callback which may happen very quickly */
663 spin_lock(&d->opd_syn_lock);
664 d->opd_syn_rpc_in_flight++;
665 d->opd_syn_rpc_in_progress++;
666 spin_unlock(&d->opd_syn_lock);
668 switch (rec->lrh_type) {
669 /* case MDS_UNLINK_REC is kept for compatibility */
671 rc = osp_sync_new_unlink_job(d, llh, rec);
673 case MDS_UNLINK64_REC:
674 rc = osp_sync_new_unlink64_job(env, d, llh, rec);
676 case MDS_SETATTR64_REC:
677 rc = osp_sync_new_setattr_job(d, llh, rec);
680 CERROR("unknown record type: %x\n", rec->lrh_type);
685 if (likely(rc == 0)) {
686 spin_lock(&d->opd_syn_lock);
687 if (d->opd_syn_prev_done) {
688 LASSERT(d->opd_syn_changes > 0);
689 LASSERT(rec->lrh_id <= d->opd_syn_last_committed_id);
691 * NOTE: it's possible to meet same id if
692 * OST stores few stripes of same file
694 if (rec->lrh_id > d->opd_syn_last_processed_id)
695 d->opd_syn_last_processed_id = rec->lrh_id;
697 d->opd_syn_changes--;
699 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
700 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
701 d->opd_syn_rpc_in_progress);
702 spin_unlock(&d->opd_syn_lock);
704 spin_lock(&d->opd_syn_lock);
705 d->opd_syn_rpc_in_flight--;
706 d->opd_syn_rpc_in_progress--;
707 spin_unlock(&d->opd_syn_lock);
710 CDEBUG(D_HA, "found record %x, %d, idx %u, id %u: %d\n",
711 rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id, rc);
715 static void osp_sync_process_committed(const struct lu_env *env,
716 struct osp_device *d)
718 struct obd_device *obd = d->opd_obd;
719 struct obd_import *imp = obd->u.cli.cl_import;
720 struct ost_body *body;
721 struct ptlrpc_request *req, *tmp;
722 struct llog_ctxt *ctxt;
723 struct llog_handle *llh;
729 if (cfs_list_empty(&d->opd_syn_committed_there))
733 * if current status is -ENOSPC (lack of free space on OST)
734 * then we should poll OST immediately once object destroy
736 * notice: we do this upon commit as well because some backends
737 * (like DMU) do not release space right away.
739 if (d->opd_pre != NULL && unlikely(d->opd_pre_status == -ENOSPC))
740 osp_statfs_need_now(d);
743 * now cancel them all
744 * XXX: can we improve this using some batching?
745 * with batch RPC that'll happen automatically?
746 * XXX: can we store ctxt in lod_device and save few cycles ?
748 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
751 llh = ctxt->loc_handle;
754 CFS_INIT_LIST_HEAD(&list);
755 spin_lock(&d->opd_syn_lock);
756 cfs_list_splice(&d->opd_syn_committed_there, &list);
757 CFS_INIT_LIST_HEAD(&d->opd_syn_committed_there);
758 spin_unlock(&d->opd_syn_lock);
760 cfs_list_for_each_entry_safe(req, tmp, &list, rq_exp_list) {
761 struct llog_cookie *lcookie = NULL;
763 LASSERT(req->rq_svc_thread == (void *) OSP_JOB_MAGIC);
764 cfs_list_del_init(&req->rq_exp_list);
766 if (d->opd_connect_mdt) {
767 struct object_update_request *ureq;
768 struct object_update *update;
769 ureq = req_capsule_client_get(&req->rq_pill,
771 LASSERT(ureq != NULL &&
772 ureq->ourq_magic == UPDATE_REQUEST_MAGIC);
774 /* 1st/2nd is for decref . and .., 3rd one is for
775 * destroy, where the log cookie is stored.
776 * See osp_prep_unlink_update_req */
777 update = object_update_request_get(ureq, 2, NULL);
778 LASSERT(update != NULL);
779 lcookie = object_update_param_get(update, 0, NULL);
780 LASSERT(lcookie != NULL);
782 body = req_capsule_client_get(&req->rq_pill,
785 lcookie = &body->oa.o_lcookie;
787 /* import can be closing, thus all commit cb's are
788 * called we can check committness directly */
789 if (req->rq_transno <= imp->imp_peer_committed_transno) {
790 rc = llog_cat_cancel_records(env, llh, 1, lcookie);
792 CERROR("%s: can't cancel record: %d\n",
795 DEBUG_REQ(D_HA, req, "not committed");
798 ptlrpc_req_finished(req);
804 LASSERT(d->opd_syn_rpc_in_progress >= done);
805 spin_lock(&d->opd_syn_lock);
806 d->opd_syn_rpc_in_progress -= done;
807 spin_unlock(&d->opd_syn_lock);
808 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
809 d->opd_obd->obd_name, d->opd_syn_rpc_in_flight,
810 d->opd_syn_rpc_in_progress);
812 osp_sync_check_for_work(d);
814 /* wake up the thread if requested to stop:
815 * it might be waiting for in-progress to complete */
816 if (unlikely(osp_sync_running(d) == 0))
817 wake_up(&d->opd_syn_waitq);
823 * this is where most of queues processing happens
825 static int osp_sync_process_queues(const struct lu_env *env,
826 struct llog_handle *llh,
827 struct llog_rec_hdr *rec,
830 struct osp_device *d = data;
834 struct l_wait_info lwi = { 0 };
836 if (!osp_sync_running(d)) {
837 CDEBUG(D_HA, "stop llog processing\n");
838 return LLOG_PROC_BREAK;
841 /* process requests committed by OST */
842 osp_sync_process_committed(env, d);
844 /* if we there are changes to be processed and we have
845 * resources for this ... do now */
846 if (osp_sync_can_process_new(d, rec)) {
848 /* ask llog for another record */
849 CDEBUG(D_HA, "%lu changes, %u in progress, %u in flight\n",
851 d->opd_syn_rpc_in_progress,
852 d->opd_syn_rpc_in_flight);
857 * try to send, in case of disconnection, suspend
858 * processing till we can send this request
861 rc = osp_sync_process_record(env, d, llh, rec);
863 * XXX: probably different handling is needed
864 * for some bugs, like immediate exit or if
868 CERROR("can't send: %d\n", rc);
869 l_wait_event(d->opd_syn_waitq,
870 !osp_sync_running(d) ||
871 osp_sync_has_work(d),
874 } while (rc != 0 && osp_sync_running(d));
880 if (d->opd_syn_last_processed_id == d->opd_syn_last_used_id)
881 osp_sync_remove_from_tracker(d);
883 l_wait_event(d->opd_syn_waitq,
884 !osp_sync_running(d) ||
885 osp_sync_can_process_new(d, rec) ||
886 !cfs_list_empty(&d->opd_syn_committed_there),
892 * this thread runs llog_cat_process() scanner calling our callback
893 * to process llog records. in the callback we implement tricky
894 * state machine as we don't want to start scanning of the llog again
895 * and again, also we don't want to process too many records and send
896 * too many RPCs a time. so, depending on current load (num of changes
897 * being synced to OST) the callback can suspend awaiting for some
898 * new conditions, like syncs completed.
900 * in order to process llog records left by previous boots and to allow
901 * llog_process_thread() to find something (otherwise it'd just exit
902 * immediately) we add a special GENERATATION record on each boot.
904 static int osp_sync_thread(void *_arg)
906 struct osp_device *d = _arg;
907 struct ptlrpc_thread *thread = &d->opd_syn_thread;
908 struct l_wait_info lwi = { 0 };
909 struct llog_ctxt *ctxt;
910 struct obd_device *obd = d->opd_obd;
911 struct llog_handle *llh;
917 rc = lu_env_init(&env, LCT_LOCAL);
919 CERROR("%s: can't initialize env: rc = %d\n",
924 spin_lock(&d->opd_syn_lock);
925 thread->t_flags = SVC_RUNNING;
926 spin_unlock(&d->opd_syn_lock);
927 wake_up(&thread->t_ctl_waitq);
929 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
931 CERROR("can't get appropriate context\n");
932 GOTO(out, rc = -EINVAL);
935 llh = ctxt->loc_handle;
937 CERROR("can't get llh\n");
939 GOTO(out, rc = -EINVAL);
942 rc = llog_cat_process(&env, llh, osp_sync_process_queues, d, 0, 0);
943 LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
944 "%lu changes, %u in progress, %u in flight: %d\n",
945 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
946 d->opd_syn_rpc_in_flight, rc);
948 /* we don't expect llog_process_thread() to exit till umount */
949 LASSERTF(thread->t_flags != SVC_RUNNING,
950 "%lu changes, %u in progress, %u in flight\n",
951 d->opd_syn_changes, d->opd_syn_rpc_in_progress,
952 d->opd_syn_rpc_in_flight);
954 /* wait till all the requests are completed */
956 while (d->opd_syn_rpc_in_progress > 0) {
957 osp_sync_process_committed(&env, d);
959 lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
960 rc = l_wait_event(d->opd_syn_waitq,
961 d->opd_syn_rpc_in_progress == 0,
963 if (rc == -ETIMEDOUT)
965 LASSERTF(count < 10, "%s: %d %d %sempty\n",
966 d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
967 d->opd_syn_rpc_in_flight,
968 cfs_list_empty(&d->opd_syn_committed_there) ? "" :"!");
972 llog_cat_close(&env, llh);
973 rc = llog_cleanup(&env, ctxt);
975 CERROR("can't cleanup llog: %d\n", rc);
977 LASSERTF(d->opd_syn_rpc_in_progress == 0,
978 "%s: %d %d %sempty\n",
979 d->opd_obd->obd_name, d->opd_syn_rpc_in_progress,
980 d->opd_syn_rpc_in_flight,
981 cfs_list_empty(&d->opd_syn_committed_there) ? "" : "!");
983 thread->t_flags = SVC_STOPPED;
985 wake_up(&thread->t_ctl_waitq);
992 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
994 struct osp_thread_info *osi = osp_env_info(env);
995 struct lu_fid *fid = &osi->osi_fid;
996 struct llog_handle *lgh = NULL;
997 struct obd_device *obd = d->opd_obd;
998 struct llog_ctxt *ctxt;
1006 * open llog corresponding to our OST
1008 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1009 obd->obd_lvfs_ctxt.dt = d->opd_storage;
1011 if (d->opd_connect_mdt)
1012 lu_local_obj_fid(fid, SLAVE_LLOG_CATALOGS_OID);
1014 lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
1016 rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
1017 &osi->osi_cid, fid);
1019 CERROR("%s: can't get id from catalogs: rc = %d\n",
1024 CDEBUG(D_INFO, "%s: Init llog for %d - catid "DOSTID":%x\n",
1025 obd->obd_name, d->opd_index,
1026 POSTID(&osi->osi_cid.lci_logid.lgl_oi),
1027 osi->osi_cid.lci_logid.lgl_ogen);
1029 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, obd,
1030 &osp_mds_ost_orig_logops);
1034 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1037 if (likely(logid_id(&osi->osi_cid.lci_logid) != 0)) {
1038 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
1040 /* re-create llog if it is missing */
1042 logid_set_id(&osi->osi_cid.lci_logid, 0);
1044 GOTO(out_cleanup, rc);
1047 if (unlikely(logid_id(&osi->osi_cid.lci_logid) == 0)) {
1048 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
1050 GOTO(out_cleanup, rc);
1051 osi->osi_cid.lci_logid = lgh->lgh_id;
1054 LASSERT(lgh != NULL);
1055 ctxt->loc_handle = lgh;
1057 rc = llog_cat_init_and_process(env, lgh);
1059 GOTO(out_close, rc);
1061 rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
1062 &osi->osi_cid, fid);
1064 GOTO(out_close, rc);
1067 * put a mark in the llog till which we'll be processing
1068 * old records restless
1070 d->opd_syn_generation.mnt_cnt = cfs_time_current();
1071 d->opd_syn_generation.conn_cnt = cfs_time_current();
1073 osi->osi_hdr.lrh_type = LLOG_GEN_REC;
1074 osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
1076 memcpy(&osi->osi_gen.lgr_gen, &d->opd_syn_generation,
1077 sizeof(osi->osi_gen.lgr_gen));
1079 rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie,
1082 GOTO(out_close, rc);
1083 llog_ctxt_put(ctxt);
1086 llog_cat_close(env, lgh);
1088 llog_cleanup(env, ctxt);
1092 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
1094 struct llog_ctxt *ctxt;
1096 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
1098 llog_cat_close(env, ctxt->loc_handle);
1099 llog_cleanup(env, ctxt);
1103 * initializes sync component of OSP
1105 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1107 struct l_wait_info lwi = { 0 };
1112 rc = osp_sync_id_traction_init(d);
1117 * initialize llog storing changes
1119 rc = osp_sync_llog_init(env, d);
1121 CERROR("%s: can't initialize llog: rc = %d\n",
1122 d->opd_obd->obd_name, rc);
1127 * Start synchronization thread
1129 d->opd_syn_max_rpc_in_flight = OSP_MAX_IN_FLIGHT;
1130 d->opd_syn_max_rpc_in_progress = OSP_MAX_IN_PROGRESS;
1131 spin_lock_init(&d->opd_syn_lock);
1132 init_waitqueue_head(&d->opd_syn_waitq);
1133 init_waitqueue_head(&d->opd_syn_thread.t_ctl_waitq);
1134 CFS_INIT_LIST_HEAD(&d->opd_syn_committed_there);
1136 rc = PTR_ERR(kthread_run(osp_sync_thread, d,
1137 "osp-syn-%u-%u", d->opd_index, d->opd_group));
1138 if (IS_ERR_VALUE(rc)) {
1139 CERROR("%s: can't start sync thread: rc = %d\n",
1140 d->opd_obd->obd_name, rc);
1144 l_wait_event(d->opd_syn_thread.t_ctl_waitq,
1145 osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1149 osp_sync_llog_fini(env, d);
1151 osp_sync_id_traction_fini(d);
1155 int osp_sync_fini(struct osp_device *d)
1157 struct ptlrpc_thread *thread = &d->opd_syn_thread;
1161 thread->t_flags = SVC_STOPPING;
1162 wake_up(&d->opd_syn_waitq);
1163 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
1166 * unregister transaction callbacks only when sync thread
1167 * has finished operations with llog
1169 osp_sync_id_traction_fini(d);
1174 static DEFINE_MUTEX(osp_id_tracker_sem);
1175 static CFS_LIST_HEAD(osp_id_tracker_list);
1177 static void osp_sync_tracker_commit_cb(struct thandle *th, void *cookie)
1179 struct osp_id_tracker *tr = cookie;
1180 struct osp_device *d;
1181 struct osp_txn_info *txn;
1185 txn = osp_txn_info(&th->th_ctx);
1186 if (txn == NULL || txn->oti_current_id < tr->otr_committed_id)
1189 spin_lock(&tr->otr_lock);
1190 if (likely(txn->oti_current_id > tr->otr_committed_id)) {
1191 CDEBUG(D_OTHER, "committed: %u -> %u\n",
1192 tr->otr_committed_id, txn->oti_current_id);
1193 tr->otr_committed_id = txn->oti_current_id;
1195 cfs_list_for_each_entry(d, &tr->otr_wakeup_list,
1197 d->opd_syn_last_committed_id = tr->otr_committed_id;
1198 wake_up(&d->opd_syn_waitq);
1201 spin_unlock(&tr->otr_lock);
1204 static int osp_sync_id_traction_init(struct osp_device *d)
1206 struct osp_id_tracker *tr, *found = NULL;
1210 LASSERT(d->opd_storage);
1211 LASSERT(d->opd_syn_tracker == NULL);
1212 CFS_INIT_LIST_HEAD(&d->opd_syn_ontrack);
1214 mutex_lock(&osp_id_tracker_sem);
1215 cfs_list_for_each_entry(tr, &osp_id_tracker_list, otr_list) {
1216 if (tr->otr_dev == d->opd_storage) {
1217 LASSERT(cfs_atomic_read(&tr->otr_refcount));
1218 cfs_atomic_inc(&tr->otr_refcount);
1219 d->opd_syn_tracker = tr;
1225 if (found == NULL) {
1229 d->opd_syn_tracker = tr;
1230 spin_lock_init(&tr->otr_lock);
1231 tr->otr_dev = d->opd_storage;
1232 tr->otr_next_id = 1;
1233 tr->otr_committed_id = 0;
1234 cfs_atomic_set(&tr->otr_refcount, 1);
1235 CFS_INIT_LIST_HEAD(&tr->otr_wakeup_list);
1236 cfs_list_add(&tr->otr_list, &osp_id_tracker_list);
1237 tr->otr_tx_cb.dtc_txn_commit =
1238 osp_sync_tracker_commit_cb;
1239 tr->otr_tx_cb.dtc_cookie = tr;
1240 tr->otr_tx_cb.dtc_tag = LCT_MD_THREAD;
1241 dt_txn_callback_add(d->opd_storage, &tr->otr_tx_cb);
1245 mutex_unlock(&osp_id_tracker_sem);
1250 static void osp_sync_id_traction_fini(struct osp_device *d)
1252 struct osp_id_tracker *tr;
1257 tr = d->opd_syn_tracker;
1263 osp_sync_remove_from_tracker(d);
1265 mutex_lock(&osp_id_tracker_sem);
1266 if (cfs_atomic_dec_and_test(&tr->otr_refcount)) {
1267 dt_txn_callback_del(d->opd_storage, &tr->otr_tx_cb);
1268 LASSERT(cfs_list_empty(&tr->otr_wakeup_list));
1269 cfs_list_del(&tr->otr_list);
1271 d->opd_syn_tracker = NULL;
1273 mutex_unlock(&osp_id_tracker_sem);
1279 * generates id for the tracker
1281 static __u32 osp_sync_id_get(struct osp_device *d, __u32 id)
1283 struct osp_id_tracker *tr;
1285 tr = d->opd_syn_tracker;
1288 /* XXX: we can improve this introducing per-cpu preallocated ids? */
1289 spin_lock(&tr->otr_lock);
1290 if (unlikely(tr->otr_next_id <= d->opd_syn_last_used_id)) {
1291 spin_unlock(&tr->otr_lock);
1292 CERROR("%s: next %u, last synced %lu\n",
1293 d->opd_obd->obd_name, tr->otr_next_id,
1294 d->opd_syn_last_used_id);
1299 id = tr->otr_next_id++;
1300 if (id > d->opd_syn_last_used_id)
1301 d->opd_syn_last_used_id = id;
1302 if (cfs_list_empty(&d->opd_syn_ontrack))
1303 cfs_list_add(&d->opd_syn_ontrack, &tr->otr_wakeup_list);
1304 spin_unlock(&tr->otr_lock);
1305 CDEBUG(D_OTHER, "new id %u\n", (unsigned) id);
1310 static void osp_sync_remove_from_tracker(struct osp_device *d)
1312 struct osp_id_tracker *tr;
1314 tr = d->opd_syn_tracker;
1317 if (cfs_list_empty(&d->opd_syn_ontrack))
1320 spin_lock(&tr->otr_lock);
1321 cfs_list_del_init(&d->opd_syn_ontrack);
1322 spin_unlock(&tr->otr_lock);