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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/osp/osp_sync.c
34 * Lustre OST Proxy Device
36 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37 * Author: Mikhail Pershin <mike.pershin@intel.com>
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include <linux/kthread.h>
43 #include <lustre_log.h>
44 #include <lustre_update.h>
45 #include "osp_internal.h"
48 * this is a components of OSP implementing synchronization between MDS and OST
49 * it llogs all interesting changes (currently it's uig/gid change and object
50 * destroy) atomically, then makes sure changes hit OST storage
52 * we have 4 queues of work:
54 * the first queue is llog itself, once read a change is stored in 2nd queue
55 * in form of RPC (but RPC isn't fired yet).
57 * the second queue (opd_sync_waiting_for_commit) holds changes awaiting local
58 * commit. once change is committed locally it migrates onto 3rd queue.
60 * the third queue (opd_sync_committed_here) holds changes committed locally,
61 * but not sent to OST (as the pipe can be full). once pipe becomes non-full
62 * we take a change from the queue and fire corresponded RPC.
64 * once RPC is reported committed by OST (using regular last_committed mech.)
65 * the change jumps into 4th queue (opd_sync_committed_there), now we can
66 * cancel corresponded llog record and release RPC
68 * opd_sync_changes is a number of unread llog records (to be processed).
69 * notice this number doesn't include llog records from previous boots.
70 * with OSP_SYNC_THRESHOLD we try to batch processing a bit (TO BE IMPLEMENTED)
72 * opd_sync_rpcs_in_progress is total number of requests in above 2-4 queues.
73 * we control this with OSP_MAX_RPCS_IN_PROGRESS so that OSP don't consume
74 * too much memory -- how to deal with 1000th OSTs ? batching could help?
76 * opd_sync_rpcs_in_flight is a number of RPC in flight.
77 * we control this with OSP_MAX_RPCS_IN_FLIGHT
80 /* XXX: do math to learn reasonable threshold
81 * should it be ~ number of changes fitting bulk? */
83 #define OSP_SYNC_THRESHOLD 10
84 #define OSP_MAX_RPCS_IN_FLIGHT 8
85 #define OSP_MAX_RPCS_IN_PROGRESS 4096
87 #define OSP_JOB_MAGIC 0x26112005
89 struct osp_job_req_args {
90 /** bytes reserved for ptlrpc_replay_req() */
91 struct ptlrpc_replay_async_args jra_raa;
92 struct list_head jra_committed_link;
93 struct list_head jra_in_flight_link;
94 struct llog_cookie jra_lcookie;
98 static int osp_sync_add_commit_cb(const struct lu_env *env,
99 struct osp_device *d, struct thandle *th);
101 static inline int osp_sync_running(struct osp_device *d)
103 return !!(d->opd_sync_thread.t_flags & SVC_RUNNING);
107 * Check status: whether OSP thread has stopped
109 * \param[in] d OSP device
111 * \retval 0 still running
114 static inline int osp_sync_stopped(struct osp_device *d)
116 return !!(d->opd_sync_thread.t_flags & SVC_STOPPED);
120 ** Check for new changes to sync
122 * \param[in] d OSP device
124 * \retval 1 there are changes
125 * \retval 0 there are no changes
127 static inline int osp_sync_has_new_job(struct osp_device *d)
129 return atomic_read(&d->opd_sync_changes) > 0 ||
130 d->opd_sync_prev_done == 0;
133 static inline int osp_sync_in_flight_conflict(struct osp_device *d,
134 struct llog_rec_hdr *h)
136 struct osp_job_req_args *jra;
140 if (h == NULL || h->lrh_type == LLOG_GEN_REC ||
141 list_empty(&d->opd_sync_in_flight_list))
144 memset(&ostid, 0, sizeof(ostid));
145 switch (h->lrh_type) {
146 case MDS_UNLINK_REC: {
147 struct llog_unlink_rec *unlink = (struct llog_unlink_rec *)h;
149 ostid_set_seq(&ostid, unlink->lur_oseq);
150 if (ostid_set_id(&ostid, unlink->lur_oid)) {
151 CERROR("Bad %llu to set " DOSTID "\n",
152 (unsigned long long)(unlink->lur_oid),
158 case MDS_UNLINK64_REC:
159 fid_to_ostid(&((struct llog_unlink64_rec *)h)->lur_fid, &ostid);
161 case MDS_SETATTR64_REC:
162 ostid = ((struct llog_setattr64_rec *)h)->lsr_oi;
168 spin_lock(&d->opd_sync_lock);
169 list_for_each_entry(jra, &d->opd_sync_in_flight_list,
170 jra_in_flight_link) {
171 struct ptlrpc_request *req;
172 struct ost_body *body;
174 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
176 req = container_of((void *)jra, struct ptlrpc_request,
178 body = req_capsule_client_get(&req->rq_pill,
182 if (memcmp(&ostid, &body->oa.o_oi, sizeof(ostid)) == 0) {
187 spin_unlock(&d->opd_sync_lock);
192 static inline int osp_sync_rpcs_in_progress_low(struct osp_device *d)
194 return atomic_read(&d->opd_sync_rpcs_in_progress) <
195 d->opd_sync_max_rpcs_in_progress;
199 * Check for room in the network pipe to OST
201 * \param[in] d OSP device
203 * \retval 1 there is room
204 * \retval 0 no room, the pipe is full
206 static inline int osp_sync_rpcs_in_flight_low(struct osp_device *d)
208 return atomic_read(&d->opd_sync_rpcs_in_flight) <
209 d->opd_sync_max_rpcs_in_flight;
213 * Wake up check for the main sync thread
215 * \param[in] d OSP device
217 * \retval 1 time to wake up
218 * \retval 0 no need to wake up
220 static inline int osp_sync_has_work(struct osp_device *osp)
222 /* has new/old changes and low in-progress? */
223 if (osp_sync_has_new_job(osp) && osp_sync_rpcs_in_progress_low(osp) &&
224 osp_sync_rpcs_in_flight_low(osp) && osp->opd_imp_connected)
227 /* has remotely committed? */
228 if (!list_empty(&osp->opd_sync_committed_there))
234 void osp_sync_check_for_work(struct osp_device *osp)
236 if (osp_sync_has_work(osp))
237 wake_up(&osp->opd_sync_waitq);
240 static inline __u64 osp_sync_correct_id(struct osp_device *d,
241 struct llog_rec_hdr *rec)
244 * llog use cyclic store with 32 bit lrh_id
245 * so overflow lrh_id is possible. Range between
246 * last_processed and last_committed is less than
247 * 64745 ^ 2 and less than 2^32 - 1
249 __u64 correct_id = d->opd_sync_last_committed_id;
251 if ((correct_id & 0xffffffffULL) < rec->lrh_id)
252 correct_id -= 0x100000000ULL;
254 correct_id &= ~0xffffffffULL;
255 correct_id |= rec->lrh_id;
261 * Check and return ready-for-new status.
263 * The thread processing llog record uses this function to check whether
264 * it's time to take another record and process it. The number of conditions
265 * must be met: the connection should be ready, RPCs in flight not exceeding
266 * the limit, the record is committed locally, etc (see the lines below).
268 * \param[in] d OSP device
269 * \param[in] rec next llog record to process
271 * \retval 0 not ready
274 static inline int osp_sync_can_process_new(struct osp_device *d,
275 struct llog_rec_hdr *rec)
279 if (unlikely(atomic_read(&d->opd_sync_barrier) > 0))
281 if (unlikely(osp_sync_in_flight_conflict(d, rec)))
283 if (!osp_sync_rpcs_in_progress_low(d))
285 if (!osp_sync_rpcs_in_flight_low(d))
287 if (!d->opd_imp_connected)
289 if (d->opd_sync_prev_done == 0)
291 if (atomic_read(&d->opd_sync_changes) == 0)
295 /* notice "<" not "<=" */
296 if (osp_sync_correct_id(d, rec) < d->opd_sync_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 enum 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_v2);
345 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
347 /* for a reason OSP wasn't able to open llog,
348 * just skip logging this operation and hope
349 * LFSCK will fix it eventually */
350 CERROR("logging isn't available, run LFSCK\n");
354 rc = llog_declare_add(env, ctxt->loc_handle, &osi->osi_hdr,
362 * Generate a llog record for a given change.
364 * Generates a llog record for the change passed. The change can be of two
365 * types: unlink and setattr. The record gets an ID which later will be
366 * used to track commit status of the change. For unlink changes, the caller
367 * can supply a starting FID and the count of the objects to destroy. For
368 * setattr the caller should apply attributes to apply.
371 * \param[in] env LU environment provided by the caller
372 * \param[in] d OSP device
373 * \param[in] fid fid of the object the change should be applied to
374 * \param[in] type type of change: MDS_UNLINK64_REC or MDS_SETATTR64_REC
375 * \param[in] count count of objects to destroy
376 * \param[in] th transaction handle (local)
377 * \param[in] attr attributes for setattr
379 * \retval 0 on success
380 * \retval negative negated errno on error
382 static int osp_sync_add_rec(const struct lu_env *env, struct osp_device *d,
383 const struct lu_fid *fid, enum llog_op_type type,
384 int count, struct thandle *th,
385 const struct lu_attr *attr)
387 struct osp_thread_info *osi = osp_env_info(env);
388 struct llog_ctxt *ctxt;
389 struct thandle *storage_th;
390 bool immediate_commit_cb = false;
395 /* it's a layering violation, to access internals of th,
396 * but we can do this as a sanity check, for a while */
397 LASSERT(th->th_top != NULL);
398 storage_th = thandle_get_sub_by_dt(env, th->th_top, d->opd_storage);
399 if (IS_ERR(storage_th))
400 RETURN(PTR_ERR(storage_th));
403 case MDS_UNLINK64_REC:
404 osi->osi_hdr.lrh_len = sizeof(osi->osi_unlink);
405 osi->osi_hdr.lrh_type = MDS_UNLINK64_REC;
406 osi->osi_unlink.lur_fid = *fid;
407 osi->osi_unlink.lur_count = count;
409 case MDS_SETATTR64_REC:
410 rc = fid_to_ostid(fid, &osi->osi_oi);
412 osi->osi_hdr.lrh_len = sizeof(osi->osi_setattr);
413 osi->osi_hdr.lrh_type = MDS_SETATTR64_REC;
414 osi->osi_setattr.lsr_oi = osi->osi_oi;
416 osi->osi_setattr.lsr_uid = attr->la_uid;
417 osi->osi_setattr.lsr_gid = attr->la_gid;
418 osi->osi_setattr.lsr_layout_version = attr->la_layout_version;
419 osi->osi_setattr.lsr_projid = attr->la_projid;
420 osi->osi_setattr.lsr_valid =
421 ((attr->la_valid & LA_UID) ? OBD_MD_FLUID : 0) |
422 ((attr->la_valid & LA_GID) ? OBD_MD_FLGID : 0) |
423 ((attr->la_valid & LA_PROJID) ? OBD_MD_FLPROJID : 0);
424 if (attr->la_valid & LA_LAYOUT_VERSION) {
425 osi->osi_setattr.lsr_valid |= OBD_MD_LAYOUT_VERSION;
427 /* FLR: the layout version has to be transferred to
428 * OST objects ASAP, otherwise clients will have to
429 * experience delay to be able to write OST objects. */
430 immediate_commit_cb = true;
437 /* we keep the same id, but increment it when the callback
438 * is registered, so that all records upto the one taken
439 * by the callback are subject to processing */
440 spin_lock(&d->opd_sync_lock);
441 osi->osi_hdr.lrh_id = d->opd_sync_last_used_id;
442 spin_unlock(&d->opd_sync_lock);
444 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
446 /* see comment in osp_sync_declare_add() */
450 rc = llog_add(env, ctxt->loc_handle, &osi->osi_hdr, &osi->osi_cookie,
454 if (likely(rc >= 0)) {
455 CDEBUG(D_OTHER, "%s: new record "DFID":%x.%u: rc = %d\n",
456 d->opd_obd->obd_name,
457 PFID(&osi->osi_cookie.lgc_lgl.lgl_oi.oi_fid),
458 osi->osi_cookie.lgc_lgl.lgl_ogen,
459 osi->osi_cookie.lgc_index, rc);
460 atomic_inc(&d->opd_sync_changes);
463 if (immediate_commit_cb)
464 rc = osp_sync_add_commit_cb(env, d, th);
466 rc = osp_sync_add_commit_cb_1s(env, d, th);
468 /* return 0 always here, error case just cause no llog record */
472 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
473 enum llog_op_type type, struct thandle *th,
474 const struct lu_attr *attr)
476 return osp_sync_add_rec(env, lu2osp_dev(o->opo_obj.do_lu.lo_dev),
477 lu_object_fid(&o->opo_obj.do_lu), type, 1,
481 int osp_sync_gap(const struct lu_env *env, struct osp_device *d,
482 struct lu_fid *fid, int lost, struct thandle *th)
484 return osp_sync_add_rec(env, d, fid, MDS_UNLINK64_REC, lost, th, NULL);
488 * it's quite obvious we can't maintain all the structures in the memory:
489 * while OST is down, MDS can be processing thousands and thousands of unlinks
490 * filling persistent llogs and in-core respresentation
492 * this doesn't scale at all. so we need basically the following:
493 * a) destroy/setattr append llog records
494 * b) once llog has grown to X records, we process first Y committed records
496 * once record R is found via llog_process(), it becomes committed after any
497 * subsequent commit callback (at the most)
501 * ptlrpc commit callback.
503 * The callback is called by PTLRPC when a RPC is reported committed by the
504 * target (OST). We register the callback for the every RPC applying a change
505 * from the llog. This way we know then the llog records can be cancelled.
506 * Notice the callback can be called when OSP is finishing. We can detect this
507 * checking that actual transno in the request is less or equal of known
508 * committed transno (see osp_sync_process_committed() for the details).
509 * XXX: this is pretty expensive and can be improved later using batching.
511 * \param[in] req request
513 static void osp_sync_request_commit_cb(struct ptlrpc_request *req)
515 struct osp_device *d = req->rq_cb_data;
516 struct osp_job_req_args *jra;
518 CDEBUG(D_HA, "commit req %p, transno %llu\n", req, req->rq_transno);
520 if (unlikely(req->rq_transno == 0))
523 /* do not do any opd_sync_rpcs_* accounting here
524 * it's done in osp_sync_interpret sooner or later */
527 jra = ptlrpc_req_async_args(jra, req);
528 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
529 LASSERT(list_empty(&jra->jra_committed_link));
531 ptlrpc_request_addref(req);
533 spin_lock(&d->opd_sync_lock);
534 list_add(&jra->jra_committed_link, &d->opd_sync_committed_there);
535 spin_unlock(&d->opd_sync_lock);
537 /* XXX: some batching wouldn't hurt */
538 wake_up(&d->opd_sync_waitq);
542 * RPC interpretation callback.
544 * The callback is called by ptlrpc when RPC is replied. Now we have to decide
546 * - put request on a special list to wait until it's committed by the target,
547 * if the request is successful
548 * - schedule llog record cancel if no target object is found
549 * - try later (essentially after reboot) in case of unexpected error
551 * \param[in] env LU environment provided by the caller
552 * \param[in] req request replied
553 * \param[in] aa callback data
554 * \param[in] rc result of RPC
558 static int osp_sync_interpret(const struct lu_env *env,
559 struct ptlrpc_request *req, void *args, int rc)
561 struct osp_job_req_args *jra = args;
562 struct osp_device *d = req->rq_cb_data;
564 if (jra->jra_magic != OSP_JOB_MAGIC) {
565 DEBUG_REQ(D_ERROR, req, "bad magic %u", jra->jra_magic);
570 CDEBUG(D_HA, "reply req %p/%d, rc %d, transno %u\n", req,
571 atomic_read(&req->rq_refcount),
572 rc, (unsigned) req->rq_transno);
576 * we tried to destroy object or update attributes,
577 * but object doesn't exist anymore - cancell llog record
579 LASSERT(req->rq_transno == 0);
580 LASSERT(list_empty(&jra->jra_committed_link));
582 ptlrpc_request_addref(req);
584 spin_lock(&d->opd_sync_lock);
585 list_add(&jra->jra_committed_link,
586 &d->opd_sync_committed_there);
587 spin_unlock(&d->opd_sync_lock);
589 wake_up(&d->opd_sync_waitq);
591 struct obd_import *imp = req->rq_import;
593 * error happened, we'll try to repeat on next boot ?
595 LASSERTF(req->rq_transno == 0 || rc == -EIO ||
596 req->rq_import_generation < imp->imp_generation,
597 "transno %llu, rc %d, gen: req %d, imp %d\n",
598 req->rq_transno, rc, req->rq_import_generation,
599 imp->imp_generation);
600 if (req->rq_transno == 0) {
601 /* this is the last time we see the request
602 * if transno is not zero, then commit cb
603 * will be called at some point */
604 LASSERT(atomic_read(&d->opd_sync_rpcs_in_progress) > 0);
605 atomic_dec(&d->opd_sync_rpcs_in_progress);
608 wake_up(&d->opd_sync_waitq);
609 } else if (d->opd_pre != NULL &&
610 unlikely(d->opd_pre_status == -ENOSPC)) {
612 * if current status is -ENOSPC (lack of free space on OST)
613 * then we should poll OST immediately once object destroy
616 osp_statfs_need_now(d);
619 spin_lock(&d->opd_sync_lock);
620 list_del_init(&jra->jra_in_flight_link);
621 spin_unlock(&d->opd_sync_lock);
622 LASSERT(atomic_read(&d->opd_sync_rpcs_in_flight) > 0);
623 atomic_dec(&d->opd_sync_rpcs_in_flight);
624 if (unlikely(atomic_read(&d->opd_sync_barrier) > 0))
625 wake_up(&d->opd_sync_barrier_waitq);
626 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
627 d->opd_obd->obd_name, atomic_read(&d->opd_sync_rpcs_in_flight),
628 atomic_read(&d->opd_sync_rpcs_in_progress));
630 osp_sync_check_for_work(d);
636 ** Add request to ptlrpc queue.
638 * This is just a tiny helper function to put the request on the sending list
640 * \param[in] d OSP device
641 * \param[in] llh llog handle where the record is stored
642 * \param[in] h llog record
643 * \param[in] req request
645 static void osp_sync_send_new_rpc(struct osp_device *d,
646 struct llog_handle *llh,
647 struct llog_rec_hdr *h,
648 struct ptlrpc_request *req)
650 struct osp_job_req_args *jra;
652 LASSERT(atomic_read(&d->opd_sync_rpcs_in_flight) <=
653 d->opd_sync_max_rpcs_in_flight);
655 jra = ptlrpc_req_async_args(jra, req);
656 jra->jra_magic = OSP_JOB_MAGIC;
657 jra->jra_lcookie.lgc_lgl = llh->lgh_id;
658 jra->jra_lcookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
659 jra->jra_lcookie.lgc_index = h->lrh_index;
660 INIT_LIST_HEAD(&jra->jra_committed_link);
661 spin_lock(&d->opd_sync_lock);
662 list_add_tail(&jra->jra_in_flight_link, &d->opd_sync_in_flight_list);
663 spin_unlock(&d->opd_sync_lock);
665 ptlrpcd_add_req(req);
670 * Allocate and prepare RPC for a new change.
672 * The function allocates and initializes an RPC which will be sent soon to
673 * apply the change to the target OST. The request is initialized from the
674 * llog record passed. Notice only the fields common to all type of changes
677 * \param[in] d OSP device
678 * \param[in] op type of the change
679 * \param[in] format request format to be used
681 * \retval pointer new request on success
682 * \retval ERR_PTR(errno) on error
684 static struct ptlrpc_request *osp_sync_new_job(struct osp_device *d,
686 const struct req_format *format)
688 struct ptlrpc_request *req;
689 struct obd_import *imp;
692 /* Prepare the request */
693 imp = d->opd_obd->u.cli.cl_import;
696 if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_ENOMEM))
697 RETURN(ERR_PTR(-ENOMEM));
699 req = ptlrpc_request_alloc(imp, format);
701 RETURN(ERR_PTR(-ENOMEM));
703 rc = ptlrpc_request_pack(req, LUSTRE_OST_VERSION, op);
705 ptlrpc_req_finished(req);
709 req->rq_interpret_reply = osp_sync_interpret;
710 req->rq_commit_cb = osp_sync_request_commit_cb;
713 ptlrpc_request_set_replen(req);
719 * Generate a request for setattr change.
721 * The function prepares a new RPC, initializes it with setattr specific
722 * bits and send the RPC.
724 * \param[in] d OSP device
725 * \param[in] llh llog handle where the record is stored
726 * \param[in] h llog record
728 * \retval 0 on success
729 * \retval 1 on invalid record
730 * \retval negative negated errno on error
732 static int osp_sync_new_setattr_job(struct osp_device *d,
733 struct llog_handle *llh,
734 struct llog_rec_hdr *h)
736 struct llog_setattr64_rec *rec = (struct llog_setattr64_rec *)h;
737 struct ptlrpc_request *req;
738 struct ost_body *body;
741 LASSERT(h->lrh_type == MDS_SETATTR64_REC);
743 if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CHECK_INVALID_REC))
746 /* lsr_valid can only be 0 or HAVE OBD_MD_{FLUID, FLGID, FLPROJID} set,
747 * so no bits other than these should be set. */
748 if ((rec->lsr_valid & ~(OBD_MD_FLUID | OBD_MD_FLGID |
749 OBD_MD_FLPROJID | OBD_MD_LAYOUT_VERSION)) != 0) {
750 CERROR("%s: invalid setattr record, lsr_valid:%llu\n",
751 d->opd_obd->obd_name, rec->lsr_valid);
752 /* return 1 on invalid record */
756 req = osp_sync_new_job(d, OST_SETATTR, &RQF_OST_SETATTR);
758 RETURN(PTR_ERR(req));
760 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
762 body->oa.o_oi = rec->lsr_oi;
763 body->oa.o_uid = rec->lsr_uid;
764 body->oa.o_gid = rec->lsr_gid;
765 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
766 if (h->lrh_len > sizeof(struct llog_setattr64_rec)) {
767 struct llog_setattr64_rec_v2 *rec_v2 = (typeof(rec_v2))rec;
768 body->oa.o_projid = rec_v2->lsr_projid;
769 body->oa.o_layout_version = rec_v2->lsr_layout_version;
772 /* old setattr record (prior 2.6.0) doesn't have 'valid' stored,
773 * we assume that both UID and GID are valid in that case. */
774 if (rec->lsr_valid == 0)
775 body->oa.o_valid |= (OBD_MD_FLUID | OBD_MD_FLGID);
777 body->oa.o_valid |= rec->lsr_valid;
779 if (body->oa.o_valid & OBD_MD_LAYOUT_VERSION) {
780 OBD_FAIL_TIMEOUT(OBD_FAIL_FLR_LV_DELAY, cfs_fail_val);
781 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_FLR_LV_INC)))
782 ++body->oa.o_layout_version;
785 osp_sync_send_new_rpc(d, llh, h, req);
790 * Generate a request for unlink change.
792 * The function prepares a new RPC, initializes it with unlink(destroy)
793 * specific bits and sends the RPC. The function is used to handle
794 * llog_unlink_rec which were used in the older versions of Lustre.
795 * Current version uses llog_unlink_rec64.
797 * \param[in] d OSP device
798 * \param[in] llh llog handle where the record is stored
799 * \param[in] h llog record
801 * \retval 0 on success
802 * \retval negative negated errno on error
804 static int osp_sync_new_unlink_job(struct osp_device *d,
805 struct llog_handle *llh,
806 struct llog_rec_hdr *h)
808 struct llog_unlink_rec *rec = (struct llog_unlink_rec *)h;
809 struct ptlrpc_request *req;
810 struct ost_body *body;
814 LASSERT(h->lrh_type == MDS_UNLINK_REC);
816 req = osp_sync_new_job(d, OST_DESTROY, &RQF_OST_DESTROY);
818 RETURN(PTR_ERR(req));
820 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
822 ostid_set_seq(&body->oa.o_oi, rec->lur_oseq);
823 rc = ostid_set_id(&body->oa.o_oi, rec->lur_oid);
826 body->oa.o_misc = rec->lur_count;
827 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID;
829 body->oa.o_valid |= OBD_MD_FLOBJCOUNT;
831 osp_sync_send_new_rpc(d, llh, h, req);
836 * Generate a request for unlink change.
838 * The function prepares a new RPC, initializes it with unlink(destroy)
839 * specific bits and sends the RPC. Depending on the target (MDT or OST)
840 * two different protocols are used. For MDT we use OUT (basically OSD API
841 * updates transferred via a network). For OST we still use the old
842 * protocol (OBD?), originally for compatibility. Later we can start to
843 * use OUT for OST as well, this will allow batching and better code
846 * \param[in] d OSP device
847 * \param[in] llh llog handle where the record is stored
848 * \param[in] h llog record
850 * \retval 0 on success
851 * \retval negative negated errno on error
853 static int osp_sync_new_unlink64_job(struct osp_device *d,
854 struct llog_handle *llh,
855 struct llog_rec_hdr *h)
857 struct llog_unlink64_rec *rec = (struct llog_unlink64_rec *)h;
858 struct ptlrpc_request *req = NULL;
859 struct ost_body *body;
863 LASSERT(h->lrh_type == MDS_UNLINK64_REC);
864 req = osp_sync_new_job(d, OST_DESTROY, &RQF_OST_DESTROY);
866 RETURN(PTR_ERR(req));
868 body = req_capsule_client_get(&req->rq_pill, &RMF_OST_BODY);
871 rc = fid_to_ostid(&rec->lur_fid, &body->oa.o_oi);
874 body->oa.o_misc = rec->lur_count;
875 body->oa.o_valid = OBD_MD_FLGROUP | OBD_MD_FLID |
877 osp_sync_send_new_rpc(d, llh, h, req);
882 * Process llog records.
884 * This function is called to process the llog records committed locally.
885 * In the recovery model used by OSP we can apply a change to a remote
886 * target once corresponding transaction (like posix unlink) is committed
887 * locally so can't revert.
888 * Depending on the llog record type, a given handler is called that is
889 * responsible for preparing and sending the RPC to apply the change.
890 * Special record type LLOG_GEN_REC marking a reboot is cancelled right away.
892 * \param[in] env LU environment provided by the caller
893 * \param[in] d OSP device
894 * \param[in] llh llog handle where the record is stored
895 * \param[in] rec llog record
897 static void osp_sync_process_record(const struct lu_env *env,
898 struct osp_device *d,
899 struct llog_handle *llh,
900 struct llog_rec_hdr *rec)
902 struct llog_handle *cathandle = llh->u.phd.phd_cat_handle;
903 struct llog_cookie cookie;
908 cookie.lgc_lgl = llh->lgh_id;
909 cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
910 cookie.lgc_index = rec->lrh_index;
912 d->opd_sync_last_catalog_idx = llh->lgh_hdr->llh_cat_idx;
914 if (unlikely(rec->lrh_type == LLOG_GEN_REC)) {
915 struct llog_gen_rec *gen = (struct llog_gen_rec *)rec;
917 /* we're waiting for the record generated by this instance */
918 LASSERT(d->opd_sync_prev_done == 0);
919 if (!memcmp(&d->opd_sync_generation, &gen->lgr_gen,
920 sizeof(gen->lgr_gen))) {
921 CDEBUG(D_HA, "processed all old entries\n");
922 d->opd_sync_prev_done = 1;
925 /* cancel any generation record */
926 rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
928 /* flush all pending records ASAP */
929 osp_sync_force(env, d);
935 * now we prepare and fill requests to OST, put them on the queue
936 * and fire after next commit callback
939 /* notice we increment counters before sending RPC, to be consistent
940 * in RPC interpret callback which may happen very quickly */
941 atomic_inc(&d->opd_sync_rpcs_in_flight);
942 atomic_inc(&d->opd_sync_rpcs_in_progress);
944 switch (rec->lrh_type) {
945 /* case MDS_UNLINK_REC is kept for compatibility */
947 rc = osp_sync_new_unlink_job(d, llh, rec);
949 case MDS_UNLINK64_REC:
950 rc = osp_sync_new_unlink64_job(d, llh, rec);
952 case MDS_SETATTR64_REC:
953 rc = osp_sync_new_setattr_job(d, llh, rec);
956 CERROR("%s: unknown record type: %x\n", d->opd_obd->obd_name,
958 /* treat "unknown record type" as "invalid" */
963 /* For all kinds of records, not matter successful or not,
964 * we should decrease changes and bump last_processed_id.
966 if (d->opd_sync_prev_done) {
967 LASSERT(atomic_read(&d->opd_sync_changes) > 0);
968 atomic_dec(&d->opd_sync_changes);
969 wake_up(&d->opd_sync_barrier_waitq);
971 atomic64_inc(&d->opd_sync_processed_recs);
973 atomic_dec(&d->opd_sync_rpcs_in_flight);
974 atomic_dec(&d->opd_sync_rpcs_in_progress);
977 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress\n",
978 d->opd_obd->obd_name, atomic_read(&d->opd_sync_rpcs_in_flight),
979 atomic_read(&d->opd_sync_rpcs_in_progress));
981 /* Delete the invalid record */
983 rc = llog_cat_cancel_records(env, cathandle, 1, &cookie);
985 CERROR("%s: can't delete invalid record: "
986 "fid = "DFID", rec_id = %u, rc = %d\n",
987 d->opd_obd->obd_name,
988 PFID(lu_object_fid(&cathandle->lgh_obj->do_lu)),
992 CDEBUG(D_HA, "found record %x, %d, idx %u, id %u\n",
993 rec->lrh_type, rec->lrh_len, rec->lrh_index, rec->lrh_id);
999 * Cancel llog records for the committed changes.
1001 * The function walks through the list of the committed RPCs and cancels
1002 * corresponding llog records. see osp_sync_request_commit_cb() for the
1005 * \param[in] env LU environment provided by the caller
1006 * \param[in] d OSP device
1008 static void osp_sync_process_committed(const struct lu_env *env,
1009 struct osp_device *d)
1011 struct obd_device *obd = d->opd_obd;
1012 struct obd_import *imp = obd->u.cli.cl_import;
1013 struct ost_body *body;
1014 struct ptlrpc_request *req;
1015 struct llog_ctxt *ctxt;
1016 struct llog_handle *llh;
1018 struct list_head list, *le;
1019 struct llog_logid lgid;
1020 int rc, i, count = 0, done = 0;
1024 if (list_empty(&d->opd_sync_committed_there))
1028 * if current status is -ENOSPC (lack of free space on OST)
1029 * then we should poll OST immediately once object destroy
1031 * notice: we do this upon commit as well because some backends
1032 * (like DMU) do not release space right away.
1034 if (d->opd_pre != NULL && unlikely(d->opd_pre_status == -ENOSPC))
1035 osp_statfs_need_now(d);
1038 * now cancel them all
1039 * XXX: can we improve this using some batching?
1040 * with batch RPC that'll happen automatically?
1041 * XXX: can we store ctxt in lod_device and save few cycles ?
1043 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1046 llh = ctxt->loc_handle;
1049 INIT_LIST_HEAD(&list);
1050 spin_lock(&d->opd_sync_lock);
1051 list_splice(&d->opd_sync_committed_there, &list);
1052 INIT_LIST_HEAD(&d->opd_sync_committed_there);
1053 spin_unlock(&d->opd_sync_lock);
1055 list_for_each(le, &list)
1058 OBD_ALLOC_WAIT(arr, sizeof(int) * count);
1062 while (!list_empty(&list)) {
1063 struct osp_job_req_args *jra;
1065 jra = list_entry(list.next, struct osp_job_req_args,
1066 jra_committed_link);
1067 LASSERT(jra->jra_magic == OSP_JOB_MAGIC);
1068 list_del_init(&jra->jra_committed_link);
1070 req = container_of((void *)jra, struct ptlrpc_request,
1072 body = req_capsule_client_get(&req->rq_pill,
1075 /* import can be closing, thus all commit cb's are
1076 * called we can check committness directly */
1077 if (req->rq_import_generation == imp->imp_generation) {
1079 !memcmp(&jra->jra_lcookie.lgc_lgl, &lgid,
1082 lgid = jra->jra_lcookie.lgc_lgl;
1084 arr[i++] = jra->jra_lcookie.lgc_index;
1086 rc = llog_cat_cancel_records(env, llh, 1,
1089 CERROR("%s: can't cancel record: %d\n",
1093 DEBUG_REQ(D_OTHER, req, "imp_committed = %llu",
1094 imp->imp_peer_committed_transno);
1096 ptlrpc_req_finished(req);
1100 rc = llog_cat_cancel_arr_rec(env, llh, &lgid, i, arr);
1103 CERROR("%s: can't cancel %d records rc: %d\n",
1104 obd->obd_name, i, rc);
1106 CDEBUG(D_OTHER, "%s: massive records cancel id "DFID\
1107 " num %d\n", obd->obd_name,
1108 PFID(&lgid.lgl_oi.oi_fid), i);
1111 OBD_FREE(arr, sizeof(int) * count);
1113 llog_ctxt_put(ctxt);
1115 LASSERT(atomic_read(&d->opd_sync_rpcs_in_progress) >= done);
1116 atomic_sub(done, &d->opd_sync_rpcs_in_progress);
1117 CDEBUG(D_OTHER, "%s: %d in flight, %d in progress, done %d\n",
1118 d->opd_obd->obd_name, atomic_read(&d->opd_sync_rpcs_in_flight),
1119 atomic_read(&d->opd_sync_rpcs_in_progress), done);
1121 osp_sync_check_for_work(d);
1123 /* wake up the thread if requested to stop:
1124 * it might be waiting for in-progress to complete */
1125 if (unlikely(osp_sync_running(d) == 0))
1126 wake_up(&d->opd_sync_waitq);
1132 * The core of the syncing mechanism.
1134 * This is a callback called by the llog processing function. Essentially it
1135 * suspends llog processing until there is a record to process (it's supposed
1136 * to be committed locally). The function handles RPCs committed by the target
1137 * and cancels corresponding llog records.
1139 * \param[in] env LU environment provided by the caller
1140 * \param[in] llh llog handle we're processing
1141 * \param[in] rec current llog record
1142 * \param[in] data callback data containing a pointer to the device
1144 * \retval 0 to ask the caller (llog_process()) to continue
1145 * \retval LLOG_PROC_BREAK to ask the caller to break
1147 static int osp_sync_process_queues(const struct lu_env *env,
1148 struct llog_handle *llh,
1149 struct llog_rec_hdr *rec,
1152 struct osp_device *d = data;
1155 struct l_wait_info lwi = { 0 };
1157 if (!osp_sync_running(d)) {
1158 CDEBUG(D_HA, "stop llog processing\n");
1159 return LLOG_PROC_BREAK;
1162 /* process requests committed by OST */
1163 osp_sync_process_committed(env, d);
1165 /* if we there are changes to be processed and we have
1166 * resources for this ... do now */
1167 if (osp_sync_can_process_new(d, rec)) {
1169 /* ask llog for another record */
1170 CDEBUG(D_HA, "%u changes, %u in progress,"
1172 atomic_read(&d->opd_sync_changes),
1173 atomic_read(&d->opd_sync_rpcs_in_progress),
1174 atomic_read(&d->opd_sync_rpcs_in_flight));
1177 osp_sync_process_record(env, d, llh, rec);
1182 l_wait_event(d->opd_sync_waitq,
1183 !osp_sync_running(d) ||
1184 osp_sync_can_process_new(d, rec) ||
1185 !list_empty(&d->opd_sync_committed_there),
1193 * This thread runs llog_cat_process() scanner calling our callback
1194 * to process llog records. in the callback we implement tricky
1195 * state machine as we don't want to start scanning of the llog again
1196 * and again, also we don't want to process too many records and send
1197 * too many RPCs a time. so, depending on current load (num of changes
1198 * being synced to OST) the callback can suspend awaiting for some
1199 * new conditions, like syncs completed.
1201 * In order to process llog records left by previous boots and to allow
1202 * llog_process_thread() to find something (otherwise it'd just exit
1203 * immediately) we add a special GENERATATION record on each boot.
1205 * \param[in] _arg a pointer to thread's arguments
1207 * \retval 0 on success
1208 * \retval negative negated errno on error
1210 static int osp_sync_thread(void *_arg)
1212 struct osp_device *d = _arg;
1213 struct ptlrpc_thread *thread = &d->opd_sync_thread;
1214 struct l_wait_info lwi = { 0 };
1215 struct llog_ctxt *ctxt;
1216 struct obd_device *obd = d->opd_obd;
1217 struct llog_handle *llh;
1224 rc = lu_env_init(&env, LCT_LOCAL);
1226 CERROR("%s: can't initialize env: rc = %d\n",
1229 spin_lock(&d->opd_sync_lock);
1230 thread->t_flags = SVC_STOPPED;
1231 spin_unlock(&d->opd_sync_lock);
1232 wake_up(&thread->t_ctl_waitq);
1237 spin_lock(&d->opd_sync_lock);
1238 thread->t_flags = SVC_RUNNING;
1239 spin_unlock(&d->opd_sync_lock);
1240 wake_up(&thread->t_ctl_waitq);
1243 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1245 CERROR("can't get appropriate context\n");
1246 GOTO(out, rc = -EINVAL);
1249 llh = ctxt->loc_handle;
1251 CERROR("can't get llh\n");
1252 llog_ctxt_put(ctxt);
1253 GOTO(out, rc = -EINVAL);
1257 * Catalog processing stops when it processed last catalog record
1258 * with index equal to the end of catalog bitmap. Or if it is wrapped,
1259 * processing stops with index equal to the lgh_last_idx. We need to
1260 * continue processing.
1262 d->opd_sync_last_catalog_idx = 0;
1266 wrapped = (llh->lgh_hdr->llh_cat_idx >= llh->lgh_last_idx &&
1267 llh->lgh_hdr->llh_count > 1);
1269 if (OBD_FAIL_CHECK(OBD_FAIL_OSP_CANT_PROCESS_LLOG)) {
1273 rc = llog_cat_process(&env, llh, osp_sync_process_queues, d,
1274 d->opd_sync_last_catalog_idx, 0);
1277 size = OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ?
1278 cfs_fail_val : (LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr) - 1);
1279 /* processing reaches catalog bottom */
1280 if (d->opd_sync_last_catalog_idx == size)
1281 d->opd_sync_last_catalog_idx = LLOG_CAT_FIRST;
1283 /* If catalog is wrapped we can`t predict last index of
1284 * processing because lgh_last_idx could be changed.
1285 * Starting form the next one */
1286 d->opd_sync_last_catalog_idx++;
1288 } while (rc == 0 && (wrapped ||
1289 d->opd_sync_last_catalog_idx == LLOG_CAT_FIRST));
1292 if (rc == -EINPROGRESS) {
1293 /* can't access the llog now - OI scrub is trying to fix
1294 * underlying issue. let's wait and try again */
1295 llog_cat_close(&env, llh);
1296 rc = llog_cleanup(&env, ctxt);
1299 schedule_timeout_interruptible(HZ * 5);
1303 CERROR("%s: llog process with osp_sync_process_queues "
1304 "failed: %d\n", d->opd_obd->obd_name, rc);
1307 LASSERTF(rc == 0 || rc == LLOG_PROC_BREAK,
1308 "%u changes, %u in progress, %u in flight: %d\n",
1309 atomic_read(&d->opd_sync_changes),
1310 atomic_read(&d->opd_sync_rpcs_in_progress),
1311 atomic_read(&d->opd_sync_rpcs_in_flight), rc);
1313 /* we don't expect llog_process_thread() to exit till umount */
1314 LASSERTF(thread->t_flags != SVC_RUNNING,
1315 "%u changes, %u in progress, %u in flight\n",
1316 atomic_read(&d->opd_sync_changes),
1317 atomic_read(&d->opd_sync_rpcs_in_progress),
1318 atomic_read(&d->opd_sync_rpcs_in_flight));
1320 /* wait till all the requests are completed */
1322 while (atomic_read(&d->opd_sync_rpcs_in_progress) > 0) {
1323 osp_sync_process_committed(&env, d);
1325 lwi = LWI_TIMEOUT(cfs_time_seconds(5), NULL, NULL);
1326 rc = l_wait_event(d->opd_sync_waitq,
1327 atomic_read(&d->opd_sync_rpcs_in_progress) == 0,
1329 if (rc == -ETIMEDOUT)
1331 LASSERTF(count < 10, "%s: %d %d %sempty\n",
1332 d->opd_obd->obd_name,
1333 atomic_read(&d->opd_sync_rpcs_in_progress),
1334 atomic_read(&d->opd_sync_rpcs_in_flight),
1335 list_empty(&d->opd_sync_committed_there) ? "" : "!");
1340 llog_cat_close(&env, llh);
1341 rc = llog_cleanup(&env, ctxt);
1343 CERROR("can't cleanup llog: %d\n", rc);
1345 LASSERTF(atomic_read(&d->opd_sync_rpcs_in_progress) == 0,
1346 "%s: %d %d %sempty\n", d->opd_obd->obd_name,
1347 atomic_read(&d->opd_sync_rpcs_in_progress),
1348 atomic_read(&d->opd_sync_rpcs_in_flight),
1349 list_empty(&d->opd_sync_committed_there) ? "" : "!");
1351 thread->t_flags = SVC_STOPPED;
1353 wake_up(&thread->t_ctl_waitq);
1363 * Initializes the llog. Specific llog to be used depends on the type of the
1364 * target OSP represents (OST or MDT). The function adds appends a new llog
1365 * record to mark the place where the records associated with this boot
1368 * \param[in] env LU environment provided by the caller
1369 * \param[in] d OSP device
1371 * \retval 0 on success
1372 * \retval negative negated errno on error
1374 static int osp_sync_llog_init(const struct lu_env *env, struct osp_device *d)
1376 struct osp_thread_info *osi = osp_env_info(env);
1377 struct lu_fid *fid = &osi->osi_fid;
1378 struct llog_handle *lgh = NULL;
1379 struct obd_device *obd = d->opd_obd;
1380 struct llog_ctxt *ctxt;
1388 * open llog corresponding to our OST
1390 OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
1391 obd->obd_lvfs_ctxt.dt = d->opd_storage;
1393 lu_local_obj_fid(fid, LLOG_CATALOGS_OID);
1395 rc = llog_osd_get_cat_list(env, d->opd_storage, d->opd_index, 1,
1396 &osi->osi_cid, fid);
1398 if (rc != -EFAULT) {
1399 CERROR("%s: can't get id from catalogs: rc = %d\n",
1404 /* After sparse OST indices is supported, the CATALOG file
1405 * may become a sparse file that results in failure on
1406 * reading. Skip this error as the llog will be created
1408 memset(&osi->osi_cid, 0, sizeof(osi->osi_cid));
1412 CDEBUG(D_INFO, "%s: Init llog for %d - catid "DFID":%x\n",
1413 obd->obd_name, d->opd_index,
1414 PFID(&osi->osi_cid.lci_logid.lgl_oi.oi_fid),
1415 osi->osi_cid.lci_logid.lgl_ogen);
1417 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT,
1418 d->opd_storage->dd_lu_dev.ld_obd,
1419 &llog_common_cat_ops);
1423 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
1426 if (likely(logid_id(&osi->osi_cid.lci_logid) != 0)) {
1427 rc = llog_open(env, ctxt, &lgh, &osi->osi_cid.lci_logid, NULL,
1429 /* re-create llog if it is missing */
1431 logid_set_id(&osi->osi_cid.lci_logid, 0);
1433 GOTO(out_cleanup, rc);
1436 if (unlikely(logid_id(&osi->osi_cid.lci_logid) == 0)) {
1437 rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
1439 GOTO(out_cleanup, rc);
1440 osi->osi_cid.lci_logid = lgh->lgh_id;
1443 LASSERT(lgh != NULL);
1444 ctxt->loc_handle = lgh;
1446 rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
1448 GOTO(out_close, rc);
1450 rc = llog_osd_put_cat_list(env, d->opd_storage, d->opd_index, 1,
1451 &osi->osi_cid, fid);
1453 GOTO(out_close, rc);
1456 * put a mark in the llog till which we'll be processing
1457 * old records restless
1459 d->opd_sync_generation.mnt_cnt = ktime_get_ns();
1460 d->opd_sync_generation.conn_cnt = ktime_get_ns();
1462 osi->osi_hdr.lrh_type = LLOG_GEN_REC;
1463 osi->osi_hdr.lrh_len = sizeof(osi->osi_gen);
1465 memcpy(&osi->osi_gen.lgr_gen, &d->opd_sync_generation,
1466 sizeof(osi->osi_gen.lgr_gen));
1468 rc = llog_cat_add(env, lgh, &osi->osi_gen.lgr_hdr, &osi->osi_cookie);
1470 GOTO(out_close, rc);
1471 llog_ctxt_put(ctxt);
1474 llog_cat_close(env, lgh);
1476 llog_cleanup(env, ctxt);
1481 * Cleanup llog used for syncing.
1483 * Closes and cleanups the llog. The function is called when the device is
1486 * \param[in] env LU environment provided by the caller
1487 * \param[in] d OSP device
1489 static void osp_sync_llog_fini(const struct lu_env *env, struct osp_device *d)
1491 struct llog_ctxt *ctxt;
1493 ctxt = llog_get_context(d->opd_obd, LLOG_MDS_OST_ORIG_CTXT);
1495 llog_cat_close(env, ctxt->loc_handle);
1496 llog_cleanup(env, ctxt);
1501 * Initialization of the sync component of OSP.
1503 * Initializes the llog and starts a new thread to handle the changes to
1504 * the remote target (OST or MDT).
1506 * \param[in] env LU environment provided by the caller
1507 * \param[in] d OSP device
1509 * \retval 0 on success
1510 * \retval negative negated errno on error
1512 int osp_sync_init(const struct lu_env *env, struct osp_device *d)
1514 struct l_wait_info lwi = { 0 };
1515 struct task_struct *task;
1520 d->opd_sync_max_rpcs_in_flight = OSP_MAX_RPCS_IN_FLIGHT;
1521 d->opd_sync_max_rpcs_in_progress = OSP_MAX_RPCS_IN_PROGRESS;
1522 spin_lock_init(&d->opd_sync_lock);
1523 init_waitqueue_head(&d->opd_sync_waitq);
1524 init_waitqueue_head(&d->opd_sync_barrier_waitq);
1525 thread_set_flags(&d->opd_sync_thread, SVC_INIT);
1526 init_waitqueue_head(&d->opd_sync_thread.t_ctl_waitq);
1527 INIT_LIST_HEAD(&d->opd_sync_in_flight_list);
1528 INIT_LIST_HEAD(&d->opd_sync_committed_there);
1530 if (d->opd_storage->dd_rdonly)
1534 * initialize llog storing changes
1536 rc = osp_sync_llog_init(env, d);
1538 CERROR("%s: can't initialize llog: rc = %d\n",
1539 d->opd_obd->obd_name, rc);
1544 * Start synchronization thread
1546 task = kthread_run(osp_sync_thread, d, "osp-syn-%u-%u",
1547 d->opd_index, d->opd_group);
1550 CERROR("%s: cannot start sync thread: rc = %d\n",
1551 d->opd_obd->obd_name, rc);
1555 l_wait_event(d->opd_sync_thread.t_ctl_waitq,
1556 osp_sync_running(d) || osp_sync_stopped(d), &lwi);
1560 osp_sync_llog_fini(env, d);
1566 * Stop the syncing thread.
1568 * Asks the syncing thread to stop and wait until it's stopped.
1570 * \param[in] d OSP device
1574 int osp_sync_fini(struct osp_device *d)
1576 struct ptlrpc_thread *thread = &d->opd_sync_thread;
1580 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1581 thread->t_flags = SVC_STOPPING;
1582 wake_up(&d->opd_sync_waitq);
1583 wait_event(thread->t_ctl_waitq, thread_is_stopped(thread));
1589 struct osp_last_committed_cb {
1590 struct dt_txn_commit_cb ospc_cb;
1591 struct osp_device *ospc_dev;
1595 void osp_sync_local_commit_cb(struct lu_env *env, struct thandle *th,
1596 struct dt_txn_commit_cb *dcb, int err)
1598 struct osp_last_committed_cb *cb;
1599 struct osp_device *d;
1601 cb = container_of0(dcb, struct osp_last_committed_cb, ospc_cb);
1604 CDEBUG(D_HA, "%s: %llu committed\n", d->opd_obd->obd_name,
1607 spin_lock(&d->opd_sync_lock);
1608 if (cb->ospc_transno > d->opd_sync_last_committed_id)
1609 d->opd_sync_last_committed_id = cb->ospc_transno;
1610 spin_unlock(&d->opd_sync_lock);
1612 osp_sync_check_for_work(d);
1613 lu_device_put(osp2lu_dev(d));
1614 if (atomic_dec_and_test(&d->opd_commits_registered))
1615 wake_up(&d->opd_sync_waitq);
1620 static int osp_sync_add_commit_cb(const struct lu_env *env,
1621 struct osp_device *d, struct thandle *th)
1623 struct osp_last_committed_cb *cb;
1624 struct dt_txn_commit_cb *dcb;
1632 dcb->dcb_func = osp_sync_local_commit_cb;
1633 spin_lock(&d->opd_sync_lock);
1634 cb->ospc_transno = ++d->opd_sync_last_used_id;
1635 spin_unlock(&d->opd_sync_lock);
1637 rc = dt_trans_cb_add(th, dcb);
1638 CDEBUG(D_HA, "%s: add commit cb at %lluns, next at %lluns, rc = %d\n",
1639 d->opd_obd->obd_name, ktime_get_ns(),
1640 ktime_to_ns(d->opd_sync_next_commit_cb), rc);
1642 if (likely(rc == 0)) {
1643 lu_device_get(osp2lu_dev(d));
1644 atomic_inc(&d->opd_commits_registered);
1651 /* add the commit callback every second */
1652 int osp_sync_add_commit_cb_1s(const struct lu_env *env, struct osp_device *d,
1655 ktime_t now = ktime_get();
1659 if (ktime_before(now, d->opd_sync_next_commit_cb))
1662 spin_lock(&d->opd_sync_lock);
1663 if (ktime_before(d->opd_sync_next_commit_cb, now)) {
1665 d->opd_sync_next_commit_cb = ktime_add_ns(now, NSEC_PER_SEC);
1667 spin_unlock(&d->opd_sync_lock);
1672 return osp_sync_add_commit_cb(env, d, th);
1676 * generate an empty transaction and hook the commit callback in
1677 * then force transaction commit
1679 void osp_sync_force(const struct lu_env *env, struct osp_device *d)
1684 th = dt_trans_create(env, d->opd_storage);
1686 CERROR("%s: can't sync\n", d->opd_obd->obd_name);
1689 rc = dt_trans_start_local(env, d->opd_storage, th);
1691 CDEBUG(D_OTHER, "%s: sync forced, %d changes\n",
1692 d->opd_obd->obd_name,
1693 atomic_read(&d->opd_sync_changes));
1694 rc = osp_sync_add_commit_cb(env, d, th);
1695 dt_trans_stop(env, d->opd_storage, th);
1698 dt_commit_async(env, d->opd_storage);