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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
24 * Use is subject to license terms.
25 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
29 * lustre/mdt/mdt_coordinator.c
31 * Lustre HSM Coordinator
33 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_support.h>
41 #include <lustre_net.h>
42 #include <lustre_export.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include "mdt_internal.h"
49 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
52 * get obj and HSM attributes on a fid
53 * \param mti [IN] context
54 * \param fid [IN] object fid
55 * \param hsm [OUT] HSM meta data
56 * \retval obj or error (-ENOENT if not found)
58 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
59 const struct lu_fid *fid,
63 struct mdt_object *obj;
71 /* find object by FID */
72 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
76 if (!mdt_object_exists(obj)) {
78 mdt_object_put(mti->mti_env, obj);
79 RETURN(ERR_PTR(-ENOENT));
82 rc = mdt_attr_get_complex(mti, obj, ma);
84 mdt_object_put(mti->mti_env, obj);
88 if (ma->ma_valid & MA_HSM)
91 memset(hsm, 0, sizeof(*hsm));
96 void mdt_hsm_dump_hal(int level, const char *prefix,
97 struct hsm_action_list *hal)
100 struct hsm_action_item *hai;
103 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
104 " archive_id %d flags "LPX64"\n",
105 prefix, hal->hal_version, hal->hal_count,
106 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
108 hai = hai_first(hal);
109 for (i = 0; i < hal->hal_count; i++) {
110 sz = hai->hai_len - sizeof(*hai);
111 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
112 " compound/cookie="LPX64"/"LPX64
113 " action=%s extent="LPX64"-"LPX64" gid="LPX64
114 " datalen=%d data=[%s]\n",
116 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
117 hal->hal_compound_id, hai->hai_cookie,
118 hsm_copytool_action2name(hai->hai_action),
119 hai->hai_extent.offset,
120 hai->hai_extent.length,
122 hai_dump_data_field(hai, buf, sizeof(buf)));
128 * data passed to llog_cat_process() callback
129 * to scan requests and take actions
131 struct hsm_scan_data {
132 struct mdt_thread_info *mti;
133 char fs_name[MTI_NAME_MAXLEN+1];
134 /* request to be send to agents */
135 int request_sz; /** allocated size */
136 int max_request; /** vector size */
137 int request_cnt; /** used count */
141 struct hsm_action_list *hal;
143 /* records to be canceled */
144 int max_cookie; /** vector size */
145 int cookie_cnt; /** used count */
150 * llog_cat_process() callback, used to:
151 * - find waiting request and start action
152 * - purge canceled and done requests
153 * \param env [IN] environment
154 * \param llh [IN] llog handle
155 * \param hdr [IN] llog record
156 * \param data [IN/OUT] cb data = struct hsm_scan_data
158 * \retval -ve failure
160 static int mdt_coordinator_cb(const struct lu_env *env,
161 struct llog_handle *llh,
162 struct llog_rec_hdr *hdr,
165 const struct llog_agent_req_rec *larr;
166 struct hsm_scan_data *hsd;
167 struct hsm_action_item *hai;
168 struct mdt_device *mdt;
169 struct coordinator *cdt;
174 mdt = hsd->mti->mti_mdt;
175 cdt = &mdt->mdt_coordinator;
177 larr = (struct llog_agent_req_rec *)hdr;
178 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
179 switch (larr->arr_status) {
181 int i, empty_slot, found;
183 /* Are agents full? */
184 if (atomic_read(&cdt->cdt_request_count) ==
185 cdt->cdt_max_request)
188 /* first search if the request if known in the list we have
189 * build and if there is room in the request vector */
192 for (i = 0; i < hsd->max_request &&
193 (empty_slot == -1 || found == -1); i++) {
194 if (hsd->request[i].hal == NULL) {
198 if (hsd->request[i].hal->hal_compound_id ==
199 larr->arr_compound_id) {
204 if (found == -1 && empty_slot == -1)
205 /* unknown request and no more room for new request,
206 * continue scan for to find other entries for
207 * already found request
212 struct hsm_action_list *hal;
214 /* request is not already known */
215 /* allocates hai vector size just needs to be large
217 hsd->request[empty_slot].hal_sz =
218 sizeof(*hsd->request[empty_slot].hal) +
219 cfs_size_round(MTI_NAME_MAXLEN+1) +
220 2 * cfs_size_round(larr->arr_hai.hai_len);
221 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
223 CERROR("%s: Cannot allocate memory (%d o)"
224 "for compound "LPX64"\n",
226 hsd->request[i].hal_sz,
227 larr->arr_compound_id);
230 hal->hal_version = HAL_VERSION;
231 strncpy(hal->hal_fsname, hsd->fs_name,
233 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
234 hal->hal_compound_id = larr->arr_compound_id;
235 hal->hal_archive_id = larr->arr_archive_id;
236 hal->hal_flags = larr->arr_flags;
238 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
239 hsd->request[empty_slot].hal = hal;
242 hai = hai_first(hal);
244 /* request is known */
245 /* we check if record archive num is the same as the
246 * known request, if not we will serve it in multiple
247 * time because we do not know if the agent can serve
249 * a use case is a compound made of multiple restore
250 * where the files are not archived in the same backend
252 if (larr->arr_archive_id !=
253 hsd->request[found].hal->hal_archive_id)
256 if (hsd->request[found].hal_sz <
257 hsd->request[found].hal_used_sz +
258 cfs_size_round(larr->arr_hai.hai_len)) {
259 /* Not enough room, need an extension */
263 sz = 2 * hsd->request[found].hal_sz;
264 OBD_ALLOC(hal_buffer, sz);
266 CERROR("%s: Cannot allocate memory "
267 "(%d o) for compound "LPX64"\n",
268 mdt_obd_name(mdt), sz,
269 larr->arr_compound_id);
272 memcpy(hal_buffer, hsd->request[found].hal,
273 hsd->request[found].hal_used_sz);
274 OBD_FREE(hsd->request[found].hal,
275 hsd->request[found].hal_sz);
276 hsd->request[found].hal = hal_buffer;
277 hsd->request[found].hal_sz = sz;
279 hai = hai_first(hsd->request[found].hal);
280 for (i = 0; i < hsd->request[found].hal->hal_count;
284 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
285 hai->hai_cookie = larr->arr_hai.hai_cookie;
286 hai->hai_gid = larr->arr_hai.hai_gid;
288 hsd->request[found].hal_used_sz +=
289 cfs_size_round(hai->hai_len);
290 hsd->request[found].hal->hal_count++;
294 struct cdt_agent_req *car;
297 /* we search for a running request
298 * error may happen if coordinator crashes or stopped
299 * with running request
301 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
303 last = larr->arr_req_create;
305 last = car->car_req_update;
306 mdt_cdt_put_request(car);
309 /* test if request too long, if yes cancel it
310 * the same way the copy tool acknowledge a cancel request */
311 if ((last + cdt->cdt_timeout) < cfs_time_current_sec()) {
312 struct hsm_progress_kernel pgs;
314 dump_llog_agent_req_rec("mdt_coordinator_cb(): "
315 "request timeouted, start "
317 /* a too old cancel request just needs to be removed
318 * this can happen, if copy tool does not support cancel
319 * for other requests, we have to remove the running
320 * request and notify the copytool
322 pgs.hpk_fid = larr->arr_hai.hai_fid;
323 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
324 pgs.hpk_extent = larr->arr_hai.hai_extent;
325 pgs.hpk_flags = HP_FLAG_COMPLETED;
326 pgs.hpk_errval = ENOSYS;
327 pgs.hpk_data_version = 0;
328 /* update request state, but do not record in llog, to
329 * avoid deadlock on cdt_llog_lock
331 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
333 CERROR("%s: Cannot cleanup timeouted request: "
334 DFID" for cookie "LPX64" action=%s\n",
336 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
337 hsm_copytool_action2name(
338 larr->arr_hai.hai_action));
340 /* add the cookie to the list of record to be
341 * canceled by caller */
342 if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
343 __u64 *ptr, *old_ptr;
344 int old_sz, new_sz, new_cnt;
346 /* need to increase vector size */
347 old_sz = sizeof(__u64) * hsd->max_cookie;
348 old_ptr = hsd->cookies;
350 new_cnt = 2 * hsd->max_cookie;
351 new_sz = sizeof(__u64) * new_cnt;
353 OBD_ALLOC(ptr, new_sz);
355 CERROR("%s: Cannot allocate memory "
356 "(%d o) for cookie vector\n",
357 mdt_obd_name(mdt), new_sz);
360 memcpy(ptr, hsd->cookies, old_sz);
362 hsd->max_cookie = new_cnt;
363 OBD_FREE(old_ptr, old_sz);
365 hsd->cookies[hsd->cookie_cnt] =
366 larr->arr_hai.hai_cookie;
374 if ((larr->arr_req_change + cdt->cdt_delay) <
375 cfs_time_current_sec())
376 RETURN(LLOG_DEL_RECORD);
383 * create /proc entries for coordinator
386 * \retval -ve failure
388 static int hsm_cdt_procfs_init(struct mdt_device *mdt)
390 struct coordinator *cdt = &mdt->mdt_coordinator;
394 /* init /proc entries, failure is not critical */
395 cdt->cdt_proc_dir = lprocfs_register("hsm",
396 mdt2obd_dev(mdt)->obd_proc_entry,
397 lprocfs_mdt_hsm_vars, mdt);
398 if (IS_ERR(cdt->cdt_proc_dir)) {
399 rc = PTR_ERR(cdt->cdt_proc_dir);
400 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
401 " rc=%d\n", mdt_obd_name(mdt), rc);
402 cdt->cdt_proc_dir = NULL;
411 * \param data [IN] obd device
413 * \retval -ve failure
415 static int mdt_coordinator(void *data)
417 struct mdt_thread_info *mti = data;
418 struct mdt_device *mdt = mti->mti_mdt;
419 struct coordinator *cdt = &mdt->mdt_coordinator;
420 struct hsm_scan_data hsd = { 0 };
424 cdt->cdt_thread.t_flags = SVC_RUNNING;
425 wake_up(&cdt->cdt_thread.t_ctl_waitq);
427 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
428 mdt_obd_name(mdt), current_pid());
431 * create /proc entries for coordinator
433 hsm_cdt_procfs_init(mdt);
434 /* timeouted cookie vector initialization */
438 /* we use a copy of cdt_max_request in the cb, so if cdt_max_request
439 * increases due to a change from /proc we do not overflow the
440 * hsd.request[] vector
442 hsd.max_request = cdt->cdt_max_request;
443 hsd.request_sz = hsd.max_request * sizeof(*hsd.request);
444 OBD_ALLOC(hsd.request, hsd.request_sz);
446 GOTO(out, rc = -ENOMEM);
449 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
452 struct l_wait_info lwi;
455 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
457 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
458 (cdt->cdt_thread.t_flags &
459 (SVC_STOPPING|SVC_EVENT)),
462 CDEBUG(D_HSM, "coordinator resumes\n");
464 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
465 cdt->cdt_state == CDT_STOPPING) {
466 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
471 /* wake up before timeout, new work arrives */
472 if (cdt->cdt_thread.t_flags & SVC_EVENT)
473 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
475 /* if coordinator is suspended continue to wait */
476 if (cdt->cdt_state == CDT_DISABLE) {
477 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
481 CDEBUG(D_HSM, "coordinator starts reading llog\n");
483 if (hsd.max_request != cdt->cdt_max_request) {
484 /* cdt_max_request has changed,
485 * we need to allocate a new buffer
487 OBD_FREE(hsd.request, hsd.request_sz);
488 hsd.max_request = cdt->cdt_max_request;
490 hsd.max_request * sizeof(*hsd.request);
491 OBD_ALLOC(hsd.request, hsd.request_sz);
498 /* create canceled cookie vector for an arbitrary size
499 * if needed, vector will grow during llog scan
503 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
510 rc = cdt_llog_process(mti->mti_env, mdt,
511 mdt_coordinator_cb, &hsd);
515 CDEBUG(D_HSM, "Found %d requests to send and %d"
516 " requests to cancel\n",
517 hsd.request_cnt, hsd.cookie_cnt);
518 /* first we cancel llog records of the timeouted requests */
519 if (hsd.cookie_cnt > 0) {
520 rc = mdt_agent_record_update(mti->mti_env, mdt,
525 CERROR("%s: mdt_agent_record_update() failed, "
526 "rc=%d, cannot update status to %s "
528 mdt_obd_name(mdt), rc,
529 agent_req_status2name(ARS_CANCELED),
533 if (list_empty(&cdt->cdt_agents)) {
534 CDEBUG(D_HSM, "no agent available, "
535 "coordinator sleeps\n");
539 /* here hsd contains a list of requests to be started */
540 for (i = 0; i < hsd.max_request; i++) {
541 struct hsm_action_list *hal;
542 struct hsm_action_item *hai;
545 enum agent_req_status status;
547 /* still room for work ? */
548 if (atomic_read(&cdt->cdt_request_count) ==
549 cdt->cdt_max_request)
552 if (hsd.request[i].hal == NULL)
555 /* found a request, we start it */
556 /* kuc payload allocation so we avoid an additionnal
557 * allocation in mdt_hsm_agent_send()
559 hal = kuc_alloc(hsd.request[i].hal_used_sz,
560 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
562 CERROR("%s: Cannot allocate memory (%d o) "
563 "for compound "LPX64"\n",
565 hsd.request[i].hal_used_sz,
566 hsd.request[i].hal->hal_compound_id);
569 memcpy(hal, hsd.request[i].hal,
570 hsd.request[i].hal_used_sz);
572 rc = mdt_hsm_agent_send(mti, hal, 0);
573 /* if failure, we suppose it is temporary
574 * if the copy tool failed to do the request
575 * it has to use hsm_progress
577 status = (rc ? ARS_WAITING : ARS_STARTED);
579 /* set up cookie vector to set records status
580 * after copy tools start or failed
582 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
583 OBD_ALLOC(cookies, sz);
584 if (cookies == NULL) {
585 CERROR("%s: Cannot allocate memory (%d o) "
586 "for cookies vector "LPX64"\n",
587 mdt_obd_name(mdt), sz,
588 hsd.request[i].hal->hal_compound_id);
589 kuc_free(hal, hsd.request[i].hal_used_sz);
592 hai = hai_first(hal);
593 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
594 cookies[j] = hai->hai_cookie;
598 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
599 hsd.request[i].hal->hal_count,
602 CERROR("%s: mdt_agent_record_update() failed, "
603 "rc=%d, cannot update status to %s "
605 mdt_obd_name(mdt), rc,
606 agent_req_status2name(status),
607 hsd.request[i].hal->hal_count);
609 OBD_FREE(cookies, sz);
610 kuc_free(hal, hsd.request[i].hal_used_sz);
613 /* free cookie vector allocated for/by callback */
615 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
621 /* free hal allocated by callback */
622 for (i = 0; i < hsd.max_request; i++) {
623 if (hsd.request[i].hal) {
624 OBD_FREE(hsd.request[i].hal,
625 hsd.request[i].hal_sz);
626 hsd.request[i].hal_sz = 0;
627 hsd.request[i].hal = NULL;
631 LASSERT(hsd.request_cnt == 0);
633 /* reset callback data */
634 memset(hsd.request, 0, hsd.request_sz);
639 OBD_FREE(hsd.request, hsd.request_sz);
642 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
644 if (cdt->cdt_state == CDT_STOPPING) {
645 /* request comes from /proc path, so we need to clean cdt
647 mdt_hsm_cdt_stop(mdt);
648 mdt->mdt_opts.mo_coordinator = 0;
650 /* request comes from a thread event, generated
651 * by mdt_stop_coordinator(), we have to ack
652 * and cdt cleaning will be done by event sender
654 cdt->cdt_thread.t_flags = SVC_STOPPED;
655 wake_up(&cdt->cdt_thread.t_ctl_waitq);
659 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
660 mdt_obd_name(mdt), current_pid(), rc);
662 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
664 mdt_obd_name(mdt), current_pid());
670 * lookup a restore handle by FID
671 * caller needs to hold cdt_restore_lock
672 * \param cdt [IN] coordinator
673 * \param fid [IN] FID
674 * \retval cdt_restore_handle found
675 * \retval NULL not found
677 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
678 const struct lu_fid *fid)
680 struct cdt_restore_handle *crh;
683 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
684 if (lu_fid_eq(&crh->crh_fid, fid))
691 * data passed to llog_cat_process() callback
692 * to scan requests and take actions
694 struct hsm_restore_data {
695 struct mdt_thread_info *hrd_mti;
699 * llog_cat_process() callback, used to:
700 * - find restore request and allocate the restore handle
701 * \param env [IN] environment
702 * \param llh [IN] llog handle
703 * \param hdr [IN] llog record
704 * \param data [IN/OUT] cb data = struct hsm_restore_data
706 * \retval -ve failure
708 static int hsm_restore_cb(const struct lu_env *env,
709 struct llog_handle *llh,
710 struct llog_rec_hdr *hdr, void *data)
712 struct llog_agent_req_rec *larr;
713 struct hsm_restore_data *hrd;
714 struct cdt_restore_handle *crh;
715 struct hsm_action_item *hai;
716 struct mdt_thread_info *mti;
717 struct coordinator *cdt;
718 struct mdt_object *child;
724 cdt = &mti->mti_mdt->mdt_coordinator;
726 larr = (struct llog_agent_req_rec *)hdr;
727 hai = &larr->arr_hai;
728 if (hai->hai_cookie > cdt->cdt_last_cookie)
729 /* update the cookie to avoid collision */
730 cdt->cdt_last_cookie = hai->hai_cookie + 1;
732 if (hai->hai_action != HSMA_RESTORE ||
733 agent_req_in_final_state(larr->arr_status))
736 /* restore request not in a final state */
738 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
742 crh->crh_fid = hai->hai_fid;
743 /* in V1 all file is restored
744 crh->extent.start = hai->hai_extent.offset;
745 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
747 crh->crh_extent.start = 0;
748 crh->crh_extent.end = hai->hai_extent.length;
749 /* get the layout lock */
750 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
751 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
752 MDS_INODELOCK_LAYOUT);
754 GOTO(out, rc = PTR_ERR(child));
757 /* we choose to not keep a reference
758 * on the object during the restore time which can be very long */
759 mdt_object_put(mti->mti_env, child);
761 mutex_lock(&cdt->cdt_restore_lock);
762 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
763 mutex_unlock(&cdt->cdt_restore_lock);
770 * restore coordinator state at startup
771 * the goal is to take a layout lock for each registered restore request
772 * \param mti [IN] context
774 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
776 struct hsm_restore_data hrd;
782 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
783 hsm_restore_cb, &hrd);
788 static int hsm_init_ucred(struct lu_ucred *uc)
792 uc->uc_valid = UCRED_OLD;
801 uc->uc_suppgids[0] = -1;
802 uc->uc_suppgids[1] = -1;
803 uc->uc_cap = CFS_CAP_FS_MASK;
806 uc->uc_identity = NULL;
812 * wake up coordinator thread
813 * \param mdt [IN] device
815 * \retval -ve failure
817 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
819 struct coordinator *cdt = &mdt->mdt_coordinator;
822 if (cdt->cdt_state == CDT_STOPPED)
825 /* wake up coordinator */
826 cdt->cdt_thread.t_flags = SVC_EVENT;
827 wake_up(&cdt->cdt_thread.t_ctl_waitq);
833 * initialize coordinator struct
834 * \param mdt [IN] device
836 * \retval -ve failure
838 int mdt_hsm_cdt_init(struct mdt_device *mdt)
840 struct coordinator *cdt = &mdt->mdt_coordinator;
841 struct mdt_thread_info *cdt_mti = NULL;
845 cdt->cdt_state = CDT_STOPPED;
847 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
848 mutex_init(&cdt->cdt_llog_lock);
849 init_rwsem(&cdt->cdt_agent_lock);
850 init_rwsem(&cdt->cdt_request_lock);
851 mutex_init(&cdt->cdt_restore_lock);
853 CFS_INIT_LIST_HEAD(&cdt->cdt_requests);
854 CFS_INIT_LIST_HEAD(&cdt->cdt_agents);
855 CFS_INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
857 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
861 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
862 rc = lu_context_init(&cdt->cdt_session, LCT_SESSION);
864 lu_context_enter(&cdt->cdt_session);
865 cdt->cdt_env.le_ses = &cdt->cdt_session;
867 lu_env_fini(&cdt->cdt_env);
871 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
872 LASSERT(cdt_mti != NULL);
874 cdt_mti->mti_env = &cdt->cdt_env;
875 cdt_mti->mti_mdt = mdt;
877 hsm_init_ucred(mdt_ucred(cdt_mti));
883 * free a coordinator thread
884 * \param mdt [IN] device
886 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
888 struct coordinator *cdt = &mdt->mdt_coordinator;
891 lu_context_exit(cdt->cdt_env.le_ses);
892 lu_context_fini(cdt->cdt_env.le_ses);
894 lu_env_fini(&cdt->cdt_env);
900 * start a coordinator thread
901 * \param mdt [IN] device
903 * \retval -ve failure
905 int mdt_hsm_cdt_start(struct mdt_device *mdt)
907 struct coordinator *cdt = &mdt->mdt_coordinator;
910 struct mdt_thread_info *cdt_mti;
911 struct task_struct *task;
914 /* functions defined but not yet used
915 * this avoid compilation warning
919 if (cdt->cdt_state != CDT_STOPPED) {
920 CERROR("%s: Coordinator already started\n",
925 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
926 cdt->cdt_policy = CDT_DEFAULT_POLICY;
927 cdt->cdt_state = CDT_INIT;
929 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
930 /* just need to be larger than previous one */
931 /* cdt_last_cookie is protected by cdt_llog_lock */
932 cdt->cdt_last_cookie = cfs_time_current_sec();
933 cdt->cdt_loop_period = 10;
935 cdt->cdt_timeout = 3600;
936 cdt->cdt_max_request = 3;
937 cdt->cdt_archive_id = 1;
938 atomic_set(&cdt->cdt_request_count, 0);
940 /* to avoid deadlock when start is made through /proc
941 * /proc entries are created by the coordinator thread */
943 /* set up list of started restore requests */
944 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
945 rc = mdt_hsm_pending_restore(cdt_mti);
947 CERROR("%s: cannot take the layout locks needed"
948 " for registered restore: %d",
949 mdt_obd_name(mdt), rc);
951 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
954 cdt->cdt_state = CDT_STOPPED;
955 CERROR("%s: error starting coordinator thread: %d\n",
956 mdt_obd_name(mdt), rc);
959 CDEBUG(D_HSM, "%s: coordinator thread started\n",
964 wait_event(cdt->cdt_thread.t_ctl_waitq,
965 (cdt->cdt_thread.t_flags & SVC_RUNNING));
967 cdt->cdt_state = CDT_RUNNING;
968 mdt->mdt_opts.mo_coordinator = 1;
973 * stop a coordinator thread
974 * \param mdt [IN] device
976 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
978 struct coordinator *cdt = &mdt->mdt_coordinator;
979 struct cdt_agent_req *car, *tmp1;
980 struct hsm_agent *ha, *tmp2;
981 struct cdt_restore_handle *crh, *tmp3;
982 struct mdt_thread_info *cdt_mti;
985 if (cdt->cdt_state == CDT_STOPPED) {
986 CERROR("%s: Coordinator already stopped\n",
991 /* remove proc entries */
992 if (cdt->cdt_proc_dir != NULL)
993 lprocfs_remove(&cdt->cdt_proc_dir);
995 if (cdt->cdt_state != CDT_STOPPING) {
996 /* stop coordinator thread before cleaning */
997 cdt->cdt_thread.t_flags = SVC_STOPPING;
998 wake_up(&cdt->cdt_thread.t_ctl_waitq);
999 wait_event(cdt->cdt_thread.t_ctl_waitq,
1000 cdt->cdt_thread.t_flags & SVC_STOPPED);
1002 cdt->cdt_state = CDT_STOPPED;
1004 /* start cleaning */
1005 down_write(&cdt->cdt_request_lock);
1006 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1008 list_del(&car->car_request_list);
1009 mdt_cdt_free_request(car);
1011 up_write(&cdt->cdt_request_lock);
1013 down_write(&cdt->cdt_agent_lock);
1014 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1015 list_del(&ha->ha_list);
1018 up_write(&cdt->cdt_agent_lock);
1020 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1021 mutex_lock(&cdt->cdt_restore_lock);
1022 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1023 struct mdt_object *child;
1025 /* give back layout lock */
1026 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1028 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1030 list_del(&crh->crh_list);
1032 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1034 mutex_unlock(&cdt->cdt_restore_lock);
1036 mdt->mdt_opts.mo_coordinator = 0;
1042 * register all requests from an hal in the memory list
1043 * \param mti [IN] context
1044 * \param hal [IN] request
1045 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1046 * which is running the CT
1048 * \retval -ve failure
1050 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1051 struct hsm_action_list *hal, struct obd_uuid *uuid)
1053 struct mdt_device *mdt = mti->mti_mdt;
1054 struct coordinator *cdt = &mdt->mdt_coordinator;
1055 struct hsm_action_item *hai;
1059 /* register request in memory list */
1060 hai = hai_first(hal);
1061 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1062 struct cdt_agent_req *car;
1064 /* in case of a cancel request, we first mark the ondisk
1065 * record of the request we want to stop as canceled
1066 * this does not change the cancel record
1067 * it will be done when updating the request status
1069 if (hai->hai_action == HSMA_CANCEL) {
1070 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1074 CERROR("%s: mdt_agent_record_update() failed, "
1075 "rc=%d, cannot update status to %s "
1076 "for cookie "LPX64"\n",
1077 mdt_obd_name(mdt), rc,
1078 agent_req_status2name(ARS_CANCELED),
1083 /* find the running request to set it canceled */
1084 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1086 car->car_canceled = 1;
1087 /* uuid has to be changed to the one running the
1088 * request to cancel */
1089 *uuid = car->car_uuid;
1090 mdt_cdt_put_request(car);
1092 /* no need to memorize cancel request
1093 * this also avoid a deadlock when we receive
1094 * a purge all requests command
1099 if (hai->hai_action == HSMA_ARCHIVE) {
1100 struct mdt_object *obj;
1103 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1104 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1107 GOTO(out, rc = PTR_ERR(obj));
1109 hsm.mh_flags |= HS_EXISTS;
1110 hsm.mh_arch_id = hal->hal_archive_id;
1111 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1112 mdt_object_put(mti->mti_env, obj);
1117 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1118 hal->hal_archive_id, hal->hal_flags,
1121 GOTO(out, rc = PTR_ERR(car));
1123 rc = mdt_cdt_add_request(cdt, car);
1125 mdt_cdt_free_request(car);
1132 * swap layouts between 2 fids
1133 * \param mti [IN] context
1137 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1138 const lustre_fid *fid, const lustre_fid *dfid)
1140 struct mdt_device *mdt = mti->mti_mdt;
1141 struct mdt_object *child1, *child2;
1142 struct mdt_lock_handle *lh2;
1146 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1148 GOTO(out, rc = PTR_ERR(child1));
1150 /* we already have layout lock on FID so take only
1152 lh2 = &mti->mti_lh[MDT_LH_OLD];
1153 mdt_lock_reg_init(lh2, LCK_EX);
1154 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1156 GOTO(out_child1, rc = PTR_ERR(child2));
1158 /* if copy tool closes the volatile before sending the final
1159 * progress through llapi_hsm_copy_end(), all the objects
1160 * are removed and mdd_swap_layout LBUG */
1161 if (mdt_object_exists(child2)) {
1162 rc = mo_swap_layouts(mti->mti_env, mdt_object_child(child1),
1163 mdt_object_child(child2), 0);
1165 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1166 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1170 mdt_object_unlock_put(mti, child2, lh2, 1);
1172 mdt_object_put(mti->mti_env, child1);
1178 * update status of a completed request
1179 * \param mti [IN] context
1180 * \param pgs [IN] progress of the copy tool
1181 * \param update_record [IN] update llog record
1183 * \retval -ve failure
1185 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1186 struct hsm_progress_kernel *pgs,
1187 const struct cdt_agent_req *car,
1188 enum agent_req_status *status)
1190 const struct lu_env *env = mti->mti_env;
1191 struct mdt_device *mdt = mti->mti_mdt;
1192 struct coordinator *cdt = &mdt->mdt_coordinator;
1193 struct mdt_object *obj = NULL;
1194 int cl_flags = 0, rc = 0;
1199 /* default is to retry */
1200 *status = ARS_WAITING;
1202 /* find object by FID */
1203 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1204 /* we will update MD HSM only if needed */
1205 is_mh_changed = false;
1207 /* object removed */
1208 *status = ARS_SUCCEED;
1212 /* no need to change mh->mh_arch_id
1213 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1215 if (pgs->hpk_errval != 0) {
1216 switch (pgs->hpk_errval) {
1218 /* the copy tool does not support cancel
1219 * so the cancel request is failed
1220 * As we cannot distinguish a cancel progress
1221 * from another action progress (they have the
1222 * same cookie), we suppose here the CT returns
1223 * ENOSYS only if does not support cancel
1225 /* this can also happen when cdt calls it to
1226 * for a timeouted request */
1227 *status = ARS_FAILED;
1228 /* to have a cancel event in changelog */
1229 pgs->hpk_errval = ECANCELED;
1232 /* the request record has already been set to
1233 * ARS_CANCELED, this set the cancel request
1235 *status = ARS_SUCCEED;
1238 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1239 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1240 ARS_FAILED : ARS_WAITING);
1244 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1245 CERROR("%s: Request "LPX64" on "DFID
1246 " failed, error code %d too large\n",
1248 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1250 hsm_set_cl_error(&cl_flags,
1251 CLF_HSM_ERROVERFLOW);
1254 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1257 switch (car->car_hai->hai_action) {
1259 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1262 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1265 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1268 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1269 CERROR("%s: Failed request "LPX64" on "DFID
1270 " cannot be a CANCEL\n",
1273 PFID(&pgs->hpk_fid));
1276 CERROR("%s: Failed request "LPX64" on "DFID
1277 " %d is an unknown action\n",
1279 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1280 car->car_hai->hai_action);
1285 *status = ARS_SUCCEED;
1286 switch (car->car_hai->hai_action) {
1288 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1289 /* set ARCHIVE keep EXIST and clear LOST and
1291 mh.mh_arch_ver = pgs->hpk_data_version;
1292 mh.mh_flags |= HS_ARCHIVED;
1293 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1294 is_mh_changed = true;
1297 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1299 /* clear RELEASED and DIRTY */
1300 mh.mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1301 /* Restoring has changed the file version on
1303 mh.mh_arch_ver = pgs->hpk_data_version;
1304 is_mh_changed = true;
1307 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1308 /* clear ARCHIVED EXISTS and LOST */
1309 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1310 is_mh_changed = true;
1313 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1314 CERROR("%s: Successful request "LPX64
1316 " cannot be a CANCEL\n",
1319 PFID(&pgs->hpk_fid));
1322 CERROR("%s: Successful request "LPX64
1324 " %d is an unknown action\n",
1326 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1327 car->car_hai->hai_action);
1333 /* rc != 0 means error when analysing action, it may come from
1334 * a crasy CT no need to manage DIRTY
1337 hsm_set_cl_flags(&cl_flags,
1338 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1340 /* unlock is done later, after layout lock management */
1342 rc = mdt_hsm_attr_set(mti, obj, &mh);
1345 /* we give back layout lock only if restore was successful or
1346 * if restore was canceled or if policy is to not retry
1347 * in other cases we just unlock the object */
1348 if (car->car_hai->hai_action == HSMA_RESTORE &&
1349 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1350 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1351 struct cdt_restore_handle *crh;
1353 /* restore in data FID done, we swap the layouts
1354 * only if restore is successfull */
1355 if (pgs->hpk_errval == 0) {
1356 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1357 &car->car_hai->hai_dfid);
1359 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1360 *status = ARS_FAILED;
1361 pgs->hpk_errval = -rc;
1364 /* we have to retry, so keep layout lock */
1365 if (*status == ARS_WAITING)
1368 /* give back layout lock */
1369 mutex_lock(&cdt->cdt_restore_lock);
1370 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1372 list_del(&crh->crh_list);
1373 mutex_unlock(&cdt->cdt_restore_lock);
1374 /* just give back layout lock, we keep
1375 * the reference which is given back
1376 * later with the lock for HSM flags */
1377 if (!IS_ERR(obj) && crh != NULL)
1378 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1381 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1387 if (obj != NULL && !IS_ERR(obj)) {
1388 mo_changelog(env, CL_HSM, cl_flags,
1389 mdt_object_child(obj));
1390 mdt_object_put(mti->mti_env, obj);
1397 * update status of a request
1398 * \param mti [IN] context
1399 * \param pgs [IN] progress of the copy tool
1400 * \param update_record [IN] update llog record
1402 * \retval -ve failure
1404 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1405 struct hsm_progress_kernel *pgs,
1406 const int update_record)
1408 struct mdt_device *mdt = mti->mti_mdt;
1409 struct coordinator *cdt = &mdt->mdt_coordinator;
1410 struct cdt_agent_req *car;
1414 /* no coordinator started, so we cannot serve requests */
1415 if (cdt->cdt_state == CDT_STOPPED)
1418 /* first do sanity checks */
1419 car = mdt_cdt_update_request(cdt, pgs);
1421 CERROR("%s: Cannot find running request for cookie "LPX64
1424 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1427 RETURN(PTR_ERR(car));
1430 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1431 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1432 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1433 hsm_copytool_action2name(car->car_hai->hai_action),
1434 pgs->hpk_flags, pgs->hpk_errval,
1435 PFID(&car->car_hai->hai_fid),
1436 PFID(&car->car_hai->hai_dfid));
1438 /* progress is done on FID or data FID depending of the action and
1439 * of the copy progress */
1440 /* for restore progress is used to send back the data FID to cdt */
1441 if (car->car_hai->hai_action == HSMA_RESTORE &&
1442 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1443 car->car_hai->hai_dfid = pgs->hpk_fid;
1445 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1446 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1447 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1448 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1449 CERROR("%s: Progress on "DFID" for cookie "LPX64
1450 " does not match request FID "DFID" nor data FID "
1453 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1454 PFID(&car->car_hai->hai_fid),
1455 PFID(&car->car_hai->hai_dfid));
1456 GOTO(out, rc = -EINVAL);
1459 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1460 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1461 " is not coherent (err=%d and not completed"
1464 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1465 hsm_copytool_action2name(car->car_hai->hai_action),
1466 pgs->hpk_errval, pgs->hpk_flags);
1467 GOTO(out, rc = -EINVAL);
1470 /* now progress is valid */
1472 /* we use a root like ucred */
1473 hsm_init_ucred(mdt_ucred(mti));
1475 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1476 enum agent_req_status status;
1478 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1480 /* remove request from memory list */
1481 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1483 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1484 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1486 hsm_copytool_action2name(car->car_hai->hai_action),
1487 agent_req_status2name(status));
1489 if (update_record) {
1492 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1493 &pgs->hpk_cookie, 1,
1496 CERROR("%s: mdt_agent_record_update() failed,"
1497 " rc=%d, cannot update status to %s"
1498 " for cookie "LPX64"\n",
1499 mdt_obd_name(mdt), rc1,
1500 agent_req_status2name(status),
1502 rc = (rc != 0 ? rc : rc1);
1504 /* ct has completed a request, so a slot is available, wakeup
1505 * cdt to find new work */
1506 mdt_hsm_cdt_wakeup(mdt);
1508 /* if copytool send a progress on a canceled request
1509 * we inform copytool it should stop
1511 if (car->car_canceled == 1)
1517 /* remove ref got from mdt_cdt_update_request() */
1518 mdt_cdt_put_request(car);
1525 * data passed to llog_cat_process() callback
1526 * to cancel requests
1528 struct hsm_cancel_all_data {
1529 struct mdt_device *mdt;
1533 * llog_cat_process() callback, used to:
1534 * - purge all requests
1535 * \param env [IN] environment
1536 * \param llh [IN] llog handle
1537 * \param hdr [IN] llog record
1538 * \param data [IN] cb data = struct hsm_cancel_all_data
1540 * \retval -ve failure
1542 static int mdt_cancel_all_cb(const struct lu_env *env,
1543 struct llog_handle *llh,
1544 struct llog_rec_hdr *hdr, void *data)
1546 struct llog_agent_req_rec *larr;
1547 struct hsm_cancel_all_data *hcad;
1551 larr = (struct llog_agent_req_rec *)hdr;
1553 if (larr->arr_status == ARS_WAITING ||
1554 larr->arr_status == ARS_STARTED) {
1555 larr->arr_status = ARS_CANCELED;
1556 larr->arr_req_change = cfs_time_current_sec();
1557 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1559 RETURN(LLOG_DEL_RECORD);
1565 * cancel all actions
1566 * \param obd [IN] MDT device
1568 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1570 struct mdt_thread_info *mti;
1571 struct coordinator *cdt = &mdt->mdt_coordinator;
1572 struct cdt_agent_req *car;
1573 struct hsm_action_list *hal = NULL;
1574 struct hsm_action_item *hai;
1575 struct hsm_cancel_all_data hcad;
1576 int hal_sz = 0, hal_len, rc;
1577 enum cdt_states save_state;
1580 /* retrieve coordinator context */
1581 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1583 /* disable coordinator */
1584 save_state = cdt->cdt_state;
1585 cdt->cdt_state = CDT_DISABLE;
1587 /* send cancel to all running requests */
1588 down_read(&cdt->cdt_request_lock);
1589 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1590 mdt_cdt_get_request(car);
1591 /* request is not yet removed from list, it will be done
1592 * when copytool will return progress
1595 if (car->car_hai->hai_action == HSMA_CANCEL) {
1596 mdt_cdt_put_request(car);
1601 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1602 cfs_size_round(car->car_hai->hai_len);
1604 if (hal_len > hal_sz && hal_sz > 0) {
1605 /* not enough room, free old buffer */
1606 OBD_FREE(hal, hal_sz);
1610 /* empty buffer, allocate one */
1613 OBD_ALLOC(hal, hal_sz);
1615 mdt_cdt_put_request(car);
1616 up_read(&cdt->cdt_request_lock);
1617 GOTO(out, rc = -ENOMEM);
1621 hal->hal_version = HAL_VERSION;
1622 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1624 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1625 hal->hal_compound_id = car->car_compound_id;
1626 hal->hal_archive_id = car->car_archive_id;
1627 hal->hal_flags = car->car_flags;
1630 hai = hai_first(hal);
1631 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1632 hai->hai_action = HSMA_CANCEL;
1635 /* it is possible to safely call mdt_hsm_agent_send()
1636 * (ie without a deadlock on cdt_request_lock), because the
1637 * write lock is taken only if we are not in purge mode
1638 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1639 * nor mdt_cdt_remove_request())
1641 /* no conflict with cdt thread because cdt is disable and we
1642 * have the request lock */
1643 mdt_hsm_agent_send(mti, hal, 1);
1645 mdt_cdt_put_request(car);
1647 up_read(&cdt->cdt_request_lock);
1650 OBD_FREE(hal, hal_sz);
1652 /* cancel all on-disk records */
1655 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1656 mdt_cancel_all_cb, &hcad);
1658 /* enable coordinator */
1659 cdt->cdt_state = save_state;
1665 * check if a request is comptaible with file status
1666 * \param hai [IN] request description
1667 * \param hal_an [IN] request archive number (not used)
1668 * \param rq_flags [IN] request flags
1669 * \param hsm [IN] file HSM metadata
1672 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1673 const int hal_an, const __u64 rq_flags,
1674 const struct md_hsm *hsm)
1676 int is_compat = false;
1680 hsm_flags = hsm->mh_flags;
1681 switch (hai->hai_action) {
1683 if (!(hsm_flags & HS_NOARCHIVE) &&
1684 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1688 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1689 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1693 if (!(hsm_flags & HS_RELEASED) &&
1694 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1701 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1702 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1703 PFID(&hai->hai_fid),
1704 hsm_copytool_action2name(hai->hai_action), rq_flags,
1705 hai->hai_extent.offset, hai->hai_extent.length,
1707 (is_compat ? "compatible" : "uncompatible"));
1713 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1715 static const struct {
1719 } hsm_policy_names[] = {
1720 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1721 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1726 * convert a policy name to a bit
1727 * \param name [IN] policy name
1729 * \retval policy bit
1731 static __u64 hsm_policy_str2bit(const char *name)
1735 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1736 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1737 strcmp(hsm_policy_names[i].name, name) == 0)
1738 return hsm_policy_names[i].bit;
1743 * convert a policy bit field to a string
1744 * \param mask [IN] policy bit field
1745 * \param hexa [IN] print mask before bit names
1746 * \param buffer [OUT] string
1747 * \param count [IN] size of buffer
1748 * \retval size filled in buffer
1750 static int hsm_policy_bit2str(const __u64 mask, const bool hexa, char *buffer,
1760 sz = snprintf(buffer, count, "("LPX64") ", mask);
1764 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1767 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1768 if (hsm_policy_names[j].bit == bit)
1772 sz = snprintf(ptr, count, "[%s] ",
1773 hsm_policy_names[j].name);
1775 sz = snprintf(ptr, count, "%s ",
1776 hsm_policy_names[j].name);
1781 /* remove last ' ' */
1784 RETURN(ptr - buffer);
1787 /* methods to read/write HSM policy flags */
1788 static int lprocfs_rd_hsm_policy(char *page, char **start, off_t off,
1789 int count, int *eof, void *data)
1791 struct mdt_device *mdt = data;
1792 struct coordinator *cdt = &mdt->mdt_coordinator;
1796 sz = hsm_policy_bit2str(cdt->cdt_policy, false, page, count);
1804 static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
1805 unsigned long count, void *data)
1807 struct mdt_device *mdt = data;
1808 struct coordinator *cdt = &mdt->mdt_coordinator;
1809 char *start, *token, sign;
1812 __u64 add_mask, remove_mask, set_mask;
1817 if (count + 1 > PAGE_SIZE)
1820 OBD_ALLOC(buf, count + 1);
1824 if (copy_from_user(buf, buffer, count))
1829 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1832 add_mask = remove_mask = set_mask = 0;
1834 token = strsep(&start, "\n ");
1840 if (sign == '-' || sign == '+')
1843 policy = hsm_policy_str2bit(token);
1852 hsm_policy_bit2str(0, false, msg, sz);
1853 CWARN("%s: '%s' is unknown, "
1854 "supported policies are: %s\n", mdt_obd_name(mdt),
1857 GOTO(out, rc = -EINVAL);
1861 remove_mask |= policy;
1871 } while (start != NULL);
1873 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1874 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1876 /* if no sign in all string, it is a clear and set
1877 * if some sign found, all unsigned are converted
1879 * P1 P2 = set to P1 and P2
1880 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1882 if (remove_mask == 0 && add_mask == 0) {
1883 cdt->cdt_policy = set_mask;
1885 cdt->cdt_policy |= set_mask | add_mask;
1886 cdt->cdt_policy &= ~remove_mask;
1889 GOTO(out, rc = count);
1892 OBD_FREE(buf, count + 1);
1896 #define GENERATE_PROC_METHOD(VAR) \
1897 static int lprocfs_rd_hsm_##VAR(char *page, char **start, off_t off, \
1898 int count, int *eof, void *data) \
1900 struct mdt_device *mdt = data; \
1901 struct coordinator *cdt = &mdt->mdt_coordinator; \
1905 sz = snprintf(page, count, LPU64"\n", (__u64)cdt->VAR); \
1909 static int lprocfs_wr_hsm_##VAR(struct file *file, const char *buffer, \
1910 unsigned long count, void *data) \
1913 struct mdt_device *mdt = data; \
1914 struct coordinator *cdt = &mdt->mdt_coordinator; \
1919 rc = lprocfs_write_helper(buffer, count, &val); \
1929 GENERATE_PROC_METHOD(cdt_loop_period)
1930 GENERATE_PROC_METHOD(cdt_delay)
1931 GENERATE_PROC_METHOD(cdt_timeout)
1932 GENERATE_PROC_METHOD(cdt_max_request)
1933 GENERATE_PROC_METHOD(cdt_archive_id)
1936 * procfs write method for MDT/hsm_control
1937 * proc entry is in mdt directory so data is mdt obd_device pointer
1939 #define CDT_ENABLE_CMD "enabled"
1940 #define CDT_STOP_CMD "shutdown"
1941 #define CDT_DISABLE_CMD "disabled"
1942 #define CDT_PURGE_CMD "purge"
1943 #define CDT_HELP_CMD "help"
1945 int lprocfs_wr_hsm_cdt_control(struct file *file, const char *buffer,
1946 unsigned long count, void *data)
1948 struct obd_device *obd = data;
1949 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1950 struct coordinator *cdt = &(mdt->mdt_coordinator);
1955 if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
1956 if (cdt->cdt_state == CDT_DISABLE) {
1957 cdt->cdt_state = CDT_RUNNING;
1958 mdt_hsm_cdt_wakeup(mdt);
1960 rc = mdt_hsm_cdt_start(mdt);
1962 } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
1963 cdt->cdt_state = CDT_STOPPING;
1964 } else if (strncmp(buffer, CDT_DISABLE_CMD,
1965 strlen(CDT_DISABLE_CMD)) == 0) {
1966 cdt->cdt_state = CDT_DISABLE;
1967 } else if (strncmp(buffer, CDT_PURGE_CMD, strlen(CDT_PURGE_CMD)) == 0) {
1968 rc = hsm_cancel_all_actions(mdt);
1969 } else if (strncmp(buffer, CDT_HELP_CMD, strlen(CDT_HELP_CMD)) == 0) {
1977 CERROR("%s: Valid coordinator control commands are: "
1978 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1979 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1980 CDT_PURGE_CMD, CDT_HELP_CMD);
1988 int lprocfs_rd_hsm_cdt_control(char *page, char **start, off_t off,
1989 int count, int *eof, void *data)
1991 struct obd_device *obd = data;
1992 struct coordinator *cdt;
1996 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1999 if (cdt->cdt_state == CDT_INIT)
2000 sz = snprintf(page, count, "init\n");
2001 else if (cdt->cdt_state == CDT_RUNNING)
2002 sz = snprintf(page, count, "enabled\n");
2003 else if (cdt->cdt_state == CDT_STOPPING)
2004 sz = snprintf(page, count, "stopping\n");
2005 else if (cdt->cdt_state == CDT_STOPPED)
2006 sz = snprintf(page, count, "stopped\n");
2007 else if (cdt->cdt_state == CDT_DISABLE)
2008 sz = snprintf(page, count, "disabled\n");
2010 sz = snprintf(page, count, "unknown\n");
2015 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2016 { "agents", NULL, NULL, NULL, &mdt_hsm_agent_fops, 0 },
2017 { "agent_actions", NULL, NULL, NULL,
2018 &mdt_agent_actions_fops, 0444 },
2019 { "archive_id", lprocfs_rd_hsm_cdt_archive_id,
2020 lprocfs_wr_hsm_cdt_archive_id,
2022 { "grace_delay", lprocfs_rd_hsm_cdt_delay,
2023 lprocfs_wr_hsm_cdt_delay,
2025 { "loop_period", lprocfs_rd_hsm_cdt_loop_period,
2026 lprocfs_wr_hsm_cdt_loop_period,
2028 { "max_requests", lprocfs_rd_hsm_cdt_max_request,
2029 lprocfs_wr_hsm_cdt_max_request,
2031 { "policy", lprocfs_rd_hsm_policy, lprocfs_wr_hsm_policy,
2033 { "request_timeout", lprocfs_rd_hsm_cdt_timeout,
2034 lprocfs_wr_hsm_cdt_timeout,
2036 { "requests", NULL, NULL, NULL, &mdt_hsm_request_fops, 0 },