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_requests; /** 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_requests)
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_requests &&
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_active_req_timeout)
312 < cfs_time_current_sec()) {
313 struct hsm_progress_kernel pgs;
315 dump_llog_agent_req_rec("mdt_coordinator_cb(): "
316 "request timeouted, start "
318 /* a too old cancel request just needs to be removed
319 * this can happen, if copy tool does not support cancel
320 * for other requests, we have to remove the running
321 * request and notify the copytool
323 pgs.hpk_fid = larr->arr_hai.hai_fid;
324 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
325 pgs.hpk_extent = larr->arr_hai.hai_extent;
326 pgs.hpk_flags = HP_FLAG_COMPLETED;
327 pgs.hpk_errval = ENOSYS;
328 pgs.hpk_data_version = 0;
329 /* update request state, but do not record in llog, to
330 * avoid deadlock on cdt_llog_lock
332 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
334 CERROR("%s: Cannot cleanup timeouted request: "
335 DFID" for cookie "LPX64" action=%s\n",
337 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
338 hsm_copytool_action2name(
339 larr->arr_hai.hai_action));
341 /* add the cookie to the list of record to be
342 * canceled by caller */
343 if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
344 __u64 *ptr, *old_ptr;
345 int old_sz, new_sz, new_cnt;
347 /* need to increase vector size */
348 old_sz = sizeof(__u64) * hsd->max_cookie;
349 old_ptr = hsd->cookies;
351 new_cnt = 2 * hsd->max_cookie;
352 new_sz = sizeof(__u64) * new_cnt;
354 OBD_ALLOC(ptr, new_sz);
356 CERROR("%s: Cannot allocate memory "
357 "(%d o) for cookie vector\n",
358 mdt_obd_name(mdt), new_sz);
361 memcpy(ptr, hsd->cookies, old_sz);
363 hsd->max_cookie = new_cnt;
364 OBD_FREE(old_ptr, old_sz);
366 hsd->cookies[hsd->cookie_cnt] =
367 larr->arr_hai.hai_cookie;
375 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
376 cfs_time_current_sec())
377 RETURN(LLOG_DEL_RECORD);
384 * create /proc entries for coordinator
387 * \retval -ve failure
389 int hsm_cdt_procfs_init(struct mdt_device *mdt)
391 struct coordinator *cdt = &mdt->mdt_coordinator;
395 /* init /proc entries, failure is not critical */
396 cdt->cdt_proc_dir = lprocfs_register("hsm",
397 mdt2obd_dev(mdt)->obd_proc_entry,
398 lprocfs_mdt_hsm_vars, mdt);
399 if (IS_ERR(cdt->cdt_proc_dir)) {
400 rc = PTR_ERR(cdt->cdt_proc_dir);
401 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
402 " rc=%d\n", mdt_obd_name(mdt), rc);
403 cdt->cdt_proc_dir = NULL;
411 * remove /proc entries for coordinator
414 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
416 struct coordinator *cdt = &mdt->mdt_coordinator;
418 LASSERT(cdt->cdt_state == CDT_STOPPED);
419 if (cdt->cdt_proc_dir != NULL)
420 lprocfs_remove(&cdt->cdt_proc_dir);
424 * get vector of hsm cdt /proc vars
428 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
430 return lprocfs_mdt_hsm_vars;
435 * \param data [IN] obd device
437 * \retval -ve failure
439 static int mdt_coordinator(void *data)
441 struct mdt_thread_info *mti = data;
442 struct mdt_device *mdt = mti->mti_mdt;
443 struct coordinator *cdt = &mdt->mdt_coordinator;
444 struct hsm_scan_data hsd = { 0 };
448 cdt->cdt_thread.t_flags = SVC_RUNNING;
449 wake_up(&cdt->cdt_thread.t_ctl_waitq);
451 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
452 mdt_obd_name(mdt), current_pid());
454 /* timeouted cookie vector initialization */
458 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
459 * increases due to a change from /proc we do not overflow the
460 * hsd.request[] vector
462 hsd.max_requests = cdt->cdt_max_requests;
463 hsd.request_sz = hsd.max_requests * sizeof(*hsd.request);
464 OBD_ALLOC(hsd.request, hsd.request_sz);
466 GOTO(out, rc = -ENOMEM);
469 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
472 struct l_wait_info lwi;
475 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
477 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
478 (cdt->cdt_thread.t_flags &
479 (SVC_STOPPING|SVC_EVENT)),
482 CDEBUG(D_HSM, "coordinator resumes\n");
484 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
485 cdt->cdt_state == CDT_STOPPING) {
486 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
491 /* wake up before timeout, new work arrives */
492 if (cdt->cdt_thread.t_flags & SVC_EVENT)
493 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
495 /* if coordinator is suspended continue to wait */
496 if (cdt->cdt_state == CDT_DISABLE) {
497 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
501 CDEBUG(D_HSM, "coordinator starts reading llog\n");
503 if (hsd.max_requests != cdt->cdt_max_requests) {
504 /* cdt_max_requests has changed,
505 * we need to allocate a new buffer
507 OBD_FREE(hsd.request, hsd.request_sz);
508 hsd.max_requests = cdt->cdt_max_requests;
510 hsd.max_requests * sizeof(*hsd.request);
511 OBD_ALLOC(hsd.request, hsd.request_sz);
518 /* create canceled cookie vector for an arbitrary size
519 * if needed, vector will grow during llog scan
523 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
530 rc = cdt_llog_process(mti->mti_env, mdt,
531 mdt_coordinator_cb, &hsd);
535 CDEBUG(D_HSM, "Found %d requests to send and %d"
536 " requests to cancel\n",
537 hsd.request_cnt, hsd.cookie_cnt);
538 /* first we cancel llog records of the timeouted requests */
539 if (hsd.cookie_cnt > 0) {
540 rc = mdt_agent_record_update(mti->mti_env, mdt,
545 CERROR("%s: mdt_agent_record_update() failed, "
546 "rc=%d, cannot update status to %s "
548 mdt_obd_name(mdt), rc,
549 agent_req_status2name(ARS_CANCELED),
553 if (list_empty(&cdt->cdt_agents)) {
554 CDEBUG(D_HSM, "no agent available, "
555 "coordinator sleeps\n");
559 /* here hsd contains a list of requests to be started */
560 for (i = 0; i < hsd.max_requests; i++) {
561 struct hsm_action_list *hal;
562 struct hsm_action_item *hai;
565 enum agent_req_status status;
567 /* still room for work ? */
568 if (atomic_read(&cdt->cdt_request_count) ==
569 cdt->cdt_max_requests)
572 if (hsd.request[i].hal == NULL)
575 /* found a request, we start it */
576 /* kuc payload allocation so we avoid an additionnal
577 * allocation in mdt_hsm_agent_send()
579 hal = kuc_alloc(hsd.request[i].hal_used_sz,
580 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
582 CERROR("%s: Cannot allocate memory (%d o) "
583 "for compound "LPX64"\n",
585 hsd.request[i].hal_used_sz,
586 hsd.request[i].hal->hal_compound_id);
589 memcpy(hal, hsd.request[i].hal,
590 hsd.request[i].hal_used_sz);
592 rc = mdt_hsm_agent_send(mti, hal, 0);
593 /* if failure, we suppose it is temporary
594 * if the copy tool failed to do the request
595 * it has to use hsm_progress
597 status = (rc ? ARS_WAITING : ARS_STARTED);
599 /* set up cookie vector to set records status
600 * after copy tools start or failed
602 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
603 OBD_ALLOC(cookies, sz);
604 if (cookies == NULL) {
605 CERROR("%s: Cannot allocate memory (%d o) "
606 "for cookies vector "LPX64"\n",
607 mdt_obd_name(mdt), sz,
608 hsd.request[i].hal->hal_compound_id);
609 kuc_free(hal, hsd.request[i].hal_used_sz);
612 hai = hai_first(hal);
613 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
614 cookies[j] = hai->hai_cookie;
618 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
619 hsd.request[i].hal->hal_count,
622 CERROR("%s: mdt_agent_record_update() failed, "
623 "rc=%d, cannot update status to %s "
625 mdt_obd_name(mdt), rc,
626 agent_req_status2name(status),
627 hsd.request[i].hal->hal_count);
629 OBD_FREE(cookies, sz);
630 kuc_free(hal, hsd.request[i].hal_used_sz);
633 /* free cookie vector allocated for/by callback */
635 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
641 /* free hal allocated by callback */
642 for (i = 0; i < hsd.max_requests; i++) {
643 if (hsd.request[i].hal) {
644 OBD_FREE(hsd.request[i].hal,
645 hsd.request[i].hal_sz);
646 hsd.request[i].hal_sz = 0;
647 hsd.request[i].hal = NULL;
651 LASSERT(hsd.request_cnt == 0);
653 /* reset callback data */
654 memset(hsd.request, 0, hsd.request_sz);
659 OBD_FREE(hsd.request, hsd.request_sz);
662 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
664 if (cdt->cdt_state == CDT_STOPPING) {
665 /* request comes from /proc path, so we need to clean cdt
667 mdt_hsm_cdt_stop(mdt);
668 mdt->mdt_opts.mo_coordinator = 0;
670 /* request comes from a thread event, generated
671 * by mdt_stop_coordinator(), we have to ack
672 * and cdt cleaning will be done by event sender
674 cdt->cdt_thread.t_flags = SVC_STOPPED;
675 wake_up(&cdt->cdt_thread.t_ctl_waitq);
679 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
680 mdt_obd_name(mdt), current_pid(), rc);
682 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
684 mdt_obd_name(mdt), current_pid());
690 * lookup a restore handle by FID
691 * caller needs to hold cdt_restore_lock
692 * \param cdt [IN] coordinator
693 * \param fid [IN] FID
694 * \retval cdt_restore_handle found
695 * \retval NULL not found
697 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
698 const struct lu_fid *fid)
700 struct cdt_restore_handle *crh;
703 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
704 if (lu_fid_eq(&crh->crh_fid, fid))
711 * data passed to llog_cat_process() callback
712 * to scan requests and take actions
714 struct hsm_restore_data {
715 struct mdt_thread_info *hrd_mti;
719 * llog_cat_process() callback, used to:
720 * - find restore request and allocate the restore handle
721 * \param env [IN] environment
722 * \param llh [IN] llog handle
723 * \param hdr [IN] llog record
724 * \param data [IN/OUT] cb data = struct hsm_restore_data
726 * \retval -ve failure
728 static int hsm_restore_cb(const struct lu_env *env,
729 struct llog_handle *llh,
730 struct llog_rec_hdr *hdr, void *data)
732 struct llog_agent_req_rec *larr;
733 struct hsm_restore_data *hrd;
734 struct cdt_restore_handle *crh;
735 struct hsm_action_item *hai;
736 struct mdt_thread_info *mti;
737 struct coordinator *cdt;
738 struct mdt_object *child;
744 cdt = &mti->mti_mdt->mdt_coordinator;
746 larr = (struct llog_agent_req_rec *)hdr;
747 hai = &larr->arr_hai;
748 if (hai->hai_cookie > cdt->cdt_last_cookie)
749 /* update the cookie to avoid collision */
750 cdt->cdt_last_cookie = hai->hai_cookie + 1;
752 if (hai->hai_action != HSMA_RESTORE ||
753 agent_req_in_final_state(larr->arr_status))
756 /* restore request not in a final state */
758 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
762 crh->crh_fid = hai->hai_fid;
763 /* in V1 all file is restored
764 crh->extent.start = hai->hai_extent.offset;
765 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
767 crh->crh_extent.start = 0;
768 crh->crh_extent.end = hai->hai_extent.length;
769 /* get the layout lock */
770 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
771 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
772 MDS_INODELOCK_LAYOUT);
774 GOTO(out, rc = PTR_ERR(child));
777 /* we choose to not keep a reference
778 * on the object during the restore time which can be very long */
779 mdt_object_put(mti->mti_env, child);
781 mutex_lock(&cdt->cdt_restore_lock);
782 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
783 mutex_unlock(&cdt->cdt_restore_lock);
790 * restore coordinator state at startup
791 * the goal is to take a layout lock for each registered restore request
792 * \param mti [IN] context
794 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
796 struct hsm_restore_data hrd;
802 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
803 hsm_restore_cb, &hrd);
808 static int hsm_init_ucred(struct lu_ucred *uc)
812 uc->uc_valid = UCRED_OLD;
821 uc->uc_suppgids[0] = -1;
822 uc->uc_suppgids[1] = -1;
823 uc->uc_cap = CFS_CAP_FS_MASK;
826 uc->uc_identity = NULL;
832 * wake up coordinator thread
833 * \param mdt [IN] device
835 * \retval -ve failure
837 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
839 struct coordinator *cdt = &mdt->mdt_coordinator;
842 if (cdt->cdt_state == CDT_STOPPED)
845 /* wake up coordinator */
846 cdt->cdt_thread.t_flags = SVC_EVENT;
847 wake_up(&cdt->cdt_thread.t_ctl_waitq);
853 * initialize coordinator struct
854 * \param mdt [IN] device
856 * \retval -ve failure
858 int mdt_hsm_cdt_init(struct mdt_device *mdt)
860 struct coordinator *cdt = &mdt->mdt_coordinator;
861 struct mdt_thread_info *cdt_mti = NULL;
865 cdt->cdt_state = CDT_STOPPED;
867 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
868 mutex_init(&cdt->cdt_llog_lock);
869 init_rwsem(&cdt->cdt_agent_lock);
870 init_rwsem(&cdt->cdt_request_lock);
871 mutex_init(&cdt->cdt_restore_lock);
873 CFS_INIT_LIST_HEAD(&cdt->cdt_requests);
874 CFS_INIT_LIST_HEAD(&cdt->cdt_agents);
875 CFS_INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
877 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
881 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
882 rc = lu_context_init(&cdt->cdt_session, LCT_SESSION);
884 lu_context_enter(&cdt->cdt_session);
885 cdt->cdt_env.le_ses = &cdt->cdt_session;
887 lu_env_fini(&cdt->cdt_env);
891 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
892 LASSERT(cdt_mti != NULL);
894 cdt_mti->mti_env = &cdt->cdt_env;
895 cdt_mti->mti_mdt = mdt;
897 hsm_init_ucred(mdt_ucred(cdt_mti));
899 /* default values for /proc tunnables
900 * can be override by MGS conf */
901 cdt->cdt_default_archive_id = 1;
902 cdt->cdt_grace_delay = 60;
903 cdt->cdt_loop_period = 10;
904 cdt->cdt_max_requests = 3;
905 cdt->cdt_policy = CDT_DEFAULT_POLICY;
906 cdt->cdt_active_req_timeout = 3600;
912 * free a coordinator thread
913 * \param mdt [IN] device
915 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
917 struct coordinator *cdt = &mdt->mdt_coordinator;
920 lu_context_exit(cdt->cdt_env.le_ses);
921 lu_context_fini(cdt->cdt_env.le_ses);
923 lu_env_fini(&cdt->cdt_env);
929 * start a coordinator thread
930 * \param mdt [IN] device
932 * \retval -ve failure
934 int mdt_hsm_cdt_start(struct mdt_device *mdt)
936 struct coordinator *cdt = &mdt->mdt_coordinator;
939 struct mdt_thread_info *cdt_mti;
940 struct task_struct *task;
943 /* functions defined but not yet used
944 * this avoid compilation warning
948 if (cdt->cdt_state != CDT_STOPPED) {
949 CERROR("%s: Coordinator already started\n",
954 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
955 cdt->cdt_policy = CDT_DEFAULT_POLICY;
957 cdt->cdt_state = CDT_INIT;
959 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
960 /* just need to be larger than previous one */
961 /* cdt_last_cookie is protected by cdt_llog_lock */
962 cdt->cdt_last_cookie = cfs_time_current_sec();
963 atomic_set(&cdt->cdt_request_count, 0);
964 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
965 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
966 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
968 /* to avoid deadlock when start is made through /proc
969 * /proc entries are created by the coordinator thread */
971 /* set up list of started restore requests */
972 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
973 rc = mdt_hsm_pending_restore(cdt_mti);
975 CERROR("%s: cannot take the layout locks needed"
976 " for registered restore: %d",
977 mdt_obd_name(mdt), rc);
979 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
982 cdt->cdt_state = CDT_STOPPED;
983 CERROR("%s: error starting coordinator thread: %d\n",
984 mdt_obd_name(mdt), rc);
987 CDEBUG(D_HSM, "%s: coordinator thread started\n",
992 wait_event(cdt->cdt_thread.t_ctl_waitq,
993 (cdt->cdt_thread.t_flags & SVC_RUNNING));
995 cdt->cdt_state = CDT_RUNNING;
996 mdt->mdt_opts.mo_coordinator = 1;
1001 * stop a coordinator thread
1002 * \param mdt [IN] device
1004 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1006 struct coordinator *cdt = &mdt->mdt_coordinator;
1007 struct cdt_agent_req *car, *tmp1;
1008 struct hsm_agent *ha, *tmp2;
1009 struct cdt_restore_handle *crh, *tmp3;
1010 struct mdt_thread_info *cdt_mti;
1013 if (cdt->cdt_state == CDT_STOPPED) {
1014 CERROR("%s: Coordinator already stopped\n",
1019 if (cdt->cdt_state != CDT_STOPPING) {
1020 /* stop coordinator thread before cleaning */
1021 cdt->cdt_thread.t_flags = SVC_STOPPING;
1022 wake_up(&cdt->cdt_thread.t_ctl_waitq);
1023 wait_event(cdt->cdt_thread.t_ctl_waitq,
1024 cdt->cdt_thread.t_flags & SVC_STOPPED);
1026 cdt->cdt_state = CDT_STOPPED;
1028 /* start cleaning */
1029 down_write(&cdt->cdt_request_lock);
1030 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1032 list_del(&car->car_request_list);
1033 mdt_cdt_free_request(car);
1035 up_write(&cdt->cdt_request_lock);
1037 down_write(&cdt->cdt_agent_lock);
1038 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1039 list_del(&ha->ha_list);
1042 up_write(&cdt->cdt_agent_lock);
1044 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1045 mutex_lock(&cdt->cdt_restore_lock);
1046 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1047 struct mdt_object *child;
1049 /* give back layout lock */
1050 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1052 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1054 list_del(&crh->crh_list);
1056 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1058 mutex_unlock(&cdt->cdt_restore_lock);
1060 mdt->mdt_opts.mo_coordinator = 0;
1066 * register all requests from an hal in the memory list
1067 * \param mti [IN] context
1068 * \param hal [IN] request
1069 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1070 * which is running the CT
1072 * \retval -ve failure
1074 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1075 struct hsm_action_list *hal, struct obd_uuid *uuid)
1077 struct mdt_device *mdt = mti->mti_mdt;
1078 struct coordinator *cdt = &mdt->mdt_coordinator;
1079 struct hsm_action_item *hai;
1083 /* register request in memory list */
1084 hai = hai_first(hal);
1085 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1086 struct cdt_agent_req *car;
1088 /* in case of a cancel request, we first mark the ondisk
1089 * record of the request we want to stop as canceled
1090 * this does not change the cancel record
1091 * it will be done when updating the request status
1093 if (hai->hai_action == HSMA_CANCEL) {
1094 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1098 CERROR("%s: mdt_agent_record_update() failed, "
1099 "rc=%d, cannot update status to %s "
1100 "for cookie "LPX64"\n",
1101 mdt_obd_name(mdt), rc,
1102 agent_req_status2name(ARS_CANCELED),
1107 /* find the running request to set it canceled */
1108 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1110 car->car_canceled = 1;
1111 /* uuid has to be changed to the one running the
1112 * request to cancel */
1113 *uuid = car->car_uuid;
1114 mdt_cdt_put_request(car);
1116 /* no need to memorize cancel request
1117 * this also avoid a deadlock when we receive
1118 * a purge all requests command
1123 if (hai->hai_action == HSMA_ARCHIVE) {
1124 struct mdt_object *obj;
1127 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1128 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1131 GOTO(out, rc = PTR_ERR(obj));
1133 hsm.mh_flags |= HS_EXISTS;
1134 hsm.mh_arch_id = hal->hal_archive_id;
1135 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1136 mdt_object_put(mti->mti_env, obj);
1141 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1142 hal->hal_archive_id, hal->hal_flags,
1145 GOTO(out, rc = PTR_ERR(car));
1147 rc = mdt_cdt_add_request(cdt, car);
1149 mdt_cdt_free_request(car);
1156 * swap layouts between 2 fids
1157 * \param mti [IN] context
1161 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1162 const lustre_fid *fid, const lustre_fid *dfid)
1164 struct mdt_device *mdt = mti->mti_mdt;
1165 struct mdt_object *child1, *child2;
1166 struct mdt_lock_handle *lh2;
1170 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1172 GOTO(out, rc = PTR_ERR(child1));
1174 /* we already have layout lock on FID so take only
1176 lh2 = &mti->mti_lh[MDT_LH_OLD];
1177 mdt_lock_reg_init(lh2, LCK_EX);
1178 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1180 GOTO(out_child1, rc = PTR_ERR(child2));
1182 /* if copy tool closes the volatile before sending the final
1183 * progress through llapi_hsm_copy_end(), all the objects
1184 * are removed and mdd_swap_layout LBUG */
1185 if (mdt_object_exists(child2)) {
1186 rc = mo_swap_layouts(mti->mti_env, mdt_object_child(child1),
1187 mdt_object_child(child2), 0);
1189 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1190 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1194 mdt_object_unlock_put(mti, child2, lh2, 1);
1196 mdt_object_put(mti->mti_env, child1);
1202 * update status of a completed request
1203 * \param mti [IN] context
1204 * \param pgs [IN] progress of the copy tool
1205 * \param update_record [IN] update llog record
1207 * \retval -ve failure
1209 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1210 struct hsm_progress_kernel *pgs,
1211 const struct cdt_agent_req *car,
1212 enum agent_req_status *status)
1214 const struct lu_env *env = mti->mti_env;
1215 struct mdt_device *mdt = mti->mti_mdt;
1216 struct coordinator *cdt = &mdt->mdt_coordinator;
1217 struct mdt_object *obj = NULL;
1218 int cl_flags = 0, rc = 0;
1223 /* default is to retry */
1224 *status = ARS_WAITING;
1226 /* find object by FID */
1227 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1228 /* we will update MD HSM only if needed */
1229 is_mh_changed = false;
1231 /* object removed */
1232 *status = ARS_SUCCEED;
1236 /* no need to change mh->mh_arch_id
1237 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1239 if (pgs->hpk_errval != 0) {
1240 switch (pgs->hpk_errval) {
1242 /* the copy tool does not support cancel
1243 * so the cancel request is failed
1244 * As we cannot distinguish a cancel progress
1245 * from another action progress (they have the
1246 * same cookie), we suppose here the CT returns
1247 * ENOSYS only if does not support cancel
1249 /* this can also happen when cdt calls it to
1250 * for a timeouted request */
1251 *status = ARS_FAILED;
1252 /* to have a cancel event in changelog */
1253 pgs->hpk_errval = ECANCELED;
1256 /* the request record has already been set to
1257 * ARS_CANCELED, this set the cancel request
1259 *status = ARS_SUCCEED;
1262 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1263 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1264 ARS_FAILED : ARS_WAITING);
1268 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1269 CERROR("%s: Request "LPX64" on "DFID
1270 " failed, error code %d too large\n",
1272 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1274 hsm_set_cl_error(&cl_flags,
1275 CLF_HSM_ERROVERFLOW);
1278 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1281 switch (car->car_hai->hai_action) {
1283 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1286 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1289 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1292 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1293 CERROR("%s: Failed request "LPX64" on "DFID
1294 " cannot be a CANCEL\n",
1297 PFID(&pgs->hpk_fid));
1300 CERROR("%s: Failed request "LPX64" on "DFID
1301 " %d is an unknown action\n",
1303 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1304 car->car_hai->hai_action);
1309 *status = ARS_SUCCEED;
1310 switch (car->car_hai->hai_action) {
1312 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1313 /* set ARCHIVE keep EXIST and clear LOST and
1315 mh.mh_arch_ver = pgs->hpk_data_version;
1316 mh.mh_flags |= HS_ARCHIVED;
1317 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1318 is_mh_changed = true;
1321 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1323 /* clear RELEASED and DIRTY */
1324 mh.mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1325 /* Restoring has changed the file version on
1327 mh.mh_arch_ver = pgs->hpk_data_version;
1328 is_mh_changed = true;
1331 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1332 /* clear ARCHIVED EXISTS and LOST */
1333 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1334 is_mh_changed = true;
1337 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1338 CERROR("%s: Successful request "LPX64
1340 " cannot be a CANCEL\n",
1343 PFID(&pgs->hpk_fid));
1346 CERROR("%s: Successful request "LPX64
1348 " %d is an unknown action\n",
1350 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1351 car->car_hai->hai_action);
1357 /* rc != 0 means error when analysing action, it may come from
1358 * a crasy CT no need to manage DIRTY
1361 hsm_set_cl_flags(&cl_flags,
1362 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1364 /* unlock is done later, after layout lock management */
1366 rc = mdt_hsm_attr_set(mti, obj, &mh);
1369 /* we give back layout lock only if restore was successful or
1370 * if restore was canceled or if policy is to not retry
1371 * in other cases we just unlock the object */
1372 if (car->car_hai->hai_action == HSMA_RESTORE &&
1373 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1374 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1375 struct cdt_restore_handle *crh;
1377 /* restore in data FID done, we swap the layouts
1378 * only if restore is successfull */
1379 if (pgs->hpk_errval == 0) {
1380 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1381 &car->car_hai->hai_dfid);
1383 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1384 *status = ARS_FAILED;
1385 pgs->hpk_errval = -rc;
1388 /* we have to retry, so keep layout lock */
1389 if (*status == ARS_WAITING)
1392 /* give back layout lock */
1393 mutex_lock(&cdt->cdt_restore_lock);
1394 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1396 list_del(&crh->crh_list);
1397 mutex_unlock(&cdt->cdt_restore_lock);
1398 /* just give back layout lock, we keep
1399 * the reference which is given back
1400 * later with the lock for HSM flags */
1401 if (!IS_ERR(obj) && crh != NULL)
1402 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1405 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1411 if (obj != NULL && !IS_ERR(obj)) {
1412 mo_changelog(env, CL_HSM, cl_flags,
1413 mdt_object_child(obj));
1414 mdt_object_put(mti->mti_env, obj);
1421 * update status of a request
1422 * \param mti [IN] context
1423 * \param pgs [IN] progress of the copy tool
1424 * \param update_record [IN] update llog record
1426 * \retval -ve failure
1428 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1429 struct hsm_progress_kernel *pgs,
1430 const int update_record)
1432 struct mdt_device *mdt = mti->mti_mdt;
1433 struct coordinator *cdt = &mdt->mdt_coordinator;
1434 struct cdt_agent_req *car;
1438 /* no coordinator started, so we cannot serve requests */
1439 if (cdt->cdt_state == CDT_STOPPED)
1442 /* first do sanity checks */
1443 car = mdt_cdt_update_request(cdt, pgs);
1445 CERROR("%s: Cannot find running request for cookie "LPX64
1448 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1451 RETURN(PTR_ERR(car));
1454 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1455 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1456 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1457 hsm_copytool_action2name(car->car_hai->hai_action),
1458 pgs->hpk_flags, pgs->hpk_errval,
1459 PFID(&car->car_hai->hai_fid),
1460 PFID(&car->car_hai->hai_dfid));
1462 /* progress is done on FID or data FID depending of the action and
1463 * of the copy progress */
1464 /* for restore progress is used to send back the data FID to cdt */
1465 if (car->car_hai->hai_action == HSMA_RESTORE &&
1466 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1467 car->car_hai->hai_dfid = pgs->hpk_fid;
1469 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1470 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1471 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1472 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1473 CERROR("%s: Progress on "DFID" for cookie "LPX64
1474 " does not match request FID "DFID" nor data FID "
1477 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1478 PFID(&car->car_hai->hai_fid),
1479 PFID(&car->car_hai->hai_dfid));
1480 GOTO(out, rc = -EINVAL);
1483 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1484 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1485 " is not coherent (err=%d and not completed"
1488 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1489 hsm_copytool_action2name(car->car_hai->hai_action),
1490 pgs->hpk_errval, pgs->hpk_flags);
1491 GOTO(out, rc = -EINVAL);
1494 /* now progress is valid */
1496 /* we use a root like ucred */
1497 hsm_init_ucred(mdt_ucred(mti));
1499 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1500 enum agent_req_status status;
1502 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1504 /* remove request from memory list */
1505 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1507 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1508 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1510 hsm_copytool_action2name(car->car_hai->hai_action),
1511 agent_req_status2name(status));
1513 if (update_record) {
1516 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1517 &pgs->hpk_cookie, 1,
1520 CERROR("%s: mdt_agent_record_update() failed,"
1521 " rc=%d, cannot update status to %s"
1522 " for cookie "LPX64"\n",
1523 mdt_obd_name(mdt), rc1,
1524 agent_req_status2name(status),
1526 rc = (rc != 0 ? rc : rc1);
1528 /* ct has completed a request, so a slot is available, wakeup
1529 * cdt to find new work */
1530 mdt_hsm_cdt_wakeup(mdt);
1532 /* if copytool send a progress on a canceled request
1533 * we inform copytool it should stop
1535 if (car->car_canceled == 1)
1541 /* remove ref got from mdt_cdt_update_request() */
1542 mdt_cdt_put_request(car);
1549 * data passed to llog_cat_process() callback
1550 * to cancel requests
1552 struct hsm_cancel_all_data {
1553 struct mdt_device *mdt;
1557 * llog_cat_process() callback, used to:
1558 * - purge all requests
1559 * \param env [IN] environment
1560 * \param llh [IN] llog handle
1561 * \param hdr [IN] llog record
1562 * \param data [IN] cb data = struct hsm_cancel_all_data
1564 * \retval -ve failure
1566 static int mdt_cancel_all_cb(const struct lu_env *env,
1567 struct llog_handle *llh,
1568 struct llog_rec_hdr *hdr, void *data)
1570 struct llog_agent_req_rec *larr;
1571 struct hsm_cancel_all_data *hcad;
1575 larr = (struct llog_agent_req_rec *)hdr;
1577 if (larr->arr_status == ARS_WAITING ||
1578 larr->arr_status == ARS_STARTED) {
1579 larr->arr_status = ARS_CANCELED;
1580 larr->arr_req_change = cfs_time_current_sec();
1581 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1583 RETURN(LLOG_DEL_RECORD);
1589 * cancel all actions
1590 * \param obd [IN] MDT device
1592 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1594 struct mdt_thread_info *mti;
1595 struct coordinator *cdt = &mdt->mdt_coordinator;
1596 struct cdt_agent_req *car;
1597 struct hsm_action_list *hal = NULL;
1598 struct hsm_action_item *hai;
1599 struct hsm_cancel_all_data hcad;
1600 int hal_sz = 0, hal_len, rc;
1601 enum cdt_states save_state;
1604 /* retrieve coordinator context */
1605 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1607 /* disable coordinator */
1608 save_state = cdt->cdt_state;
1609 cdt->cdt_state = CDT_DISABLE;
1611 /* send cancel to all running requests */
1612 down_read(&cdt->cdt_request_lock);
1613 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1614 mdt_cdt_get_request(car);
1615 /* request is not yet removed from list, it will be done
1616 * when copytool will return progress
1619 if (car->car_hai->hai_action == HSMA_CANCEL) {
1620 mdt_cdt_put_request(car);
1625 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1626 cfs_size_round(car->car_hai->hai_len);
1628 if (hal_len > hal_sz && hal_sz > 0) {
1629 /* not enough room, free old buffer */
1630 OBD_FREE(hal, hal_sz);
1634 /* empty buffer, allocate one */
1637 OBD_ALLOC(hal, hal_sz);
1639 mdt_cdt_put_request(car);
1640 up_read(&cdt->cdt_request_lock);
1641 GOTO(out, rc = -ENOMEM);
1645 hal->hal_version = HAL_VERSION;
1646 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1648 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1649 hal->hal_compound_id = car->car_compound_id;
1650 hal->hal_archive_id = car->car_archive_id;
1651 hal->hal_flags = car->car_flags;
1654 hai = hai_first(hal);
1655 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1656 hai->hai_action = HSMA_CANCEL;
1659 /* it is possible to safely call mdt_hsm_agent_send()
1660 * (ie without a deadlock on cdt_request_lock), because the
1661 * write lock is taken only if we are not in purge mode
1662 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1663 * nor mdt_cdt_remove_request())
1665 /* no conflict with cdt thread because cdt is disable and we
1666 * have the request lock */
1667 mdt_hsm_agent_send(mti, hal, 1);
1669 mdt_cdt_put_request(car);
1671 up_read(&cdt->cdt_request_lock);
1674 OBD_FREE(hal, hal_sz);
1676 /* cancel all on-disk records */
1679 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1680 mdt_cancel_all_cb, &hcad);
1682 /* enable coordinator */
1683 cdt->cdt_state = save_state;
1689 * check if a request is comptaible with file status
1690 * \param hai [IN] request description
1691 * \param hal_an [IN] request archive number (not used)
1692 * \param rq_flags [IN] request flags
1693 * \param hsm [IN] file HSM metadata
1696 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1697 const int hal_an, const __u64 rq_flags,
1698 const struct md_hsm *hsm)
1700 int is_compat = false;
1704 hsm_flags = hsm->mh_flags;
1705 switch (hai->hai_action) {
1707 if (!(hsm_flags & HS_NOARCHIVE) &&
1708 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1712 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1713 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1717 if (!(hsm_flags & HS_RELEASED) &&
1718 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1725 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1726 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1727 PFID(&hai->hai_fid),
1728 hsm_copytool_action2name(hai->hai_action), rq_flags,
1729 hai->hai_extent.offset, hai->hai_extent.length,
1731 (is_compat ? "compatible" : "uncompatible"));
1737 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1739 static const struct {
1743 } hsm_policy_names[] = {
1744 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1745 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1750 * convert a policy name to a bit
1751 * \param name [IN] policy name
1753 * \retval policy bit
1755 static __u64 hsm_policy_str2bit(const char *name)
1759 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1760 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1761 strcmp(hsm_policy_names[i].name, name) == 0)
1762 return hsm_policy_names[i].bit;
1767 * convert a policy bit field to a string
1768 * \param mask [IN] policy bit field
1769 * \param hexa [IN] print mask before bit names
1770 * \param buffer [OUT] string
1771 * \param count [IN] size of buffer
1772 * \retval size filled in buffer
1774 static int hsm_policy_bit2str(const __u64 mask, const bool hexa, char *buffer,
1784 sz = snprintf(buffer, count, "("LPX64") ", mask);
1788 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1791 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1792 if (hsm_policy_names[j].bit == bit)
1796 sz = snprintf(ptr, count, "[%s] ",
1797 hsm_policy_names[j].name);
1799 sz = snprintf(ptr, count, "%s ",
1800 hsm_policy_names[j].name);
1805 /* remove last ' ' */
1808 RETURN(ptr - buffer);
1811 /* methods to read/write HSM policy flags */
1812 static int lprocfs_rd_hsm_policy(char *page, char **start, off_t off,
1813 int count, int *eof, void *data)
1815 struct mdt_device *mdt = data;
1816 struct coordinator *cdt = &mdt->mdt_coordinator;
1820 sz = hsm_policy_bit2str(cdt->cdt_policy, false, page, count);
1828 static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
1829 unsigned long count, void *data)
1831 struct mdt_device *mdt = data;
1832 struct coordinator *cdt = &mdt->mdt_coordinator;
1833 char *start, *token, sign;
1836 __u64 add_mask, remove_mask, set_mask;
1841 if (count + 1 > PAGE_SIZE)
1844 OBD_ALLOC(buf, count + 1);
1848 if (copy_from_user(buf, buffer, count))
1853 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1856 add_mask = remove_mask = set_mask = 0;
1858 token = strsep(&start, "\n ");
1864 if (sign == '-' || sign == '+')
1867 policy = hsm_policy_str2bit(token);
1876 hsm_policy_bit2str(0, false, msg, sz);
1877 CWARN("%s: '%s' is unknown, "
1878 "supported policies are: %s\n", mdt_obd_name(mdt),
1881 GOTO(out, rc = -EINVAL);
1885 remove_mask |= policy;
1895 } while (start != NULL);
1897 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1898 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1900 /* if no sign in all string, it is a clear and set
1901 * if some sign found, all unsigned are converted
1903 * P1 P2 = set to P1 and P2
1904 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1906 if (remove_mask == 0 && add_mask == 0) {
1907 cdt->cdt_policy = set_mask;
1909 cdt->cdt_policy |= set_mask | add_mask;
1910 cdt->cdt_policy &= ~remove_mask;
1913 GOTO(out, rc = count);
1916 OBD_FREE(buf, count + 1);
1920 #define GENERATE_PROC_METHOD(VAR) \
1921 static int lprocfs_rd_hsm_##VAR(char *page, char **start, off_t off, \
1922 int count, int *eof, void *data) \
1924 struct mdt_device *mdt = data; \
1925 struct coordinator *cdt = &mdt->mdt_coordinator; \
1929 sz = snprintf(page, count, LPU64"\n", (__u64)cdt->VAR); \
1933 static int lprocfs_wr_hsm_##VAR(struct file *file, const char *buffer, \
1934 unsigned long count, void *data) \
1937 struct mdt_device *mdt = data; \
1938 struct coordinator *cdt = &mdt->mdt_coordinator; \
1943 rc = lprocfs_write_helper(buffer, count, &val); \
1953 GENERATE_PROC_METHOD(cdt_loop_period)
1954 GENERATE_PROC_METHOD(cdt_grace_delay)
1955 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1956 GENERATE_PROC_METHOD(cdt_max_requests)
1957 GENERATE_PROC_METHOD(cdt_default_archive_id)
1960 * procfs write method for MDT/hsm_control
1961 * proc entry is in mdt directory so data is mdt obd_device pointer
1963 #define CDT_ENABLE_CMD "enabled"
1964 #define CDT_STOP_CMD "shutdown"
1965 #define CDT_DISABLE_CMD "disabled"
1966 #define CDT_PURGE_CMD "purge"
1967 #define CDT_HELP_CMD "help"
1969 int lprocfs_wr_hsm_cdt_control(struct file *file, const char *buffer,
1970 unsigned long count, void *data)
1972 struct obd_device *obd = data;
1973 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1974 struct coordinator *cdt = &(mdt->mdt_coordinator);
1979 if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
1980 if (cdt->cdt_state == CDT_DISABLE) {
1981 cdt->cdt_state = CDT_RUNNING;
1982 mdt_hsm_cdt_wakeup(mdt);
1984 rc = mdt_hsm_cdt_start(mdt);
1986 } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
1987 cdt->cdt_state = CDT_STOPPING;
1988 } else if (strncmp(buffer, CDT_DISABLE_CMD,
1989 strlen(CDT_DISABLE_CMD)) == 0) {
1990 cdt->cdt_state = CDT_DISABLE;
1991 } else if (strncmp(buffer, CDT_PURGE_CMD, strlen(CDT_PURGE_CMD)) == 0) {
1992 rc = hsm_cancel_all_actions(mdt);
1993 } else if (strncmp(buffer, CDT_HELP_CMD, strlen(CDT_HELP_CMD)) == 0) {
2001 CERROR("%s: Valid coordinator control commands are: "
2002 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2003 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2004 CDT_PURGE_CMD, CDT_HELP_CMD);
2012 int lprocfs_rd_hsm_cdt_control(char *page, char **start, off_t off,
2013 int count, int *eof, void *data)
2015 struct obd_device *obd = data;
2016 struct coordinator *cdt;
2020 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2023 if (cdt->cdt_state == CDT_INIT)
2024 sz = snprintf(page, count, "init\n");
2025 else if (cdt->cdt_state == CDT_RUNNING)
2026 sz = snprintf(page, count, "enabled\n");
2027 else if (cdt->cdt_state == CDT_STOPPING)
2028 sz = snprintf(page, count, "stopping\n");
2029 else if (cdt->cdt_state == CDT_STOPPED)
2030 sz = snprintf(page, count, "stopped\n");
2031 else if (cdt->cdt_state == CDT_DISABLE)
2032 sz = snprintf(page, count, "disabled\n");
2034 sz = snprintf(page, count, "unknown\n");
2040 lprocfs_rd_hsm_request_mask(char *page, char **start, off_t off,
2041 int count, int *eof, __u64 mask)
2046 for (i = 0; i < 8 * sizeof(mask); i++) {
2047 if (mask & (1UL << i))
2048 rc += snprintf(page + rc, count - rc, "%s%s",
2050 hsm_copytool_action2name(i));
2053 rc += snprintf(page + rc, count - rc, "\n");
2059 lprocfs_rd_hsm_user_request_mask(char *page, char **start, off_t off,
2060 int count, int *eof, void *data)
2062 struct mdt_device *mdt = data;
2063 struct coordinator *cdt = &mdt->mdt_coordinator;
2065 return lprocfs_rd_hsm_request_mask(page, start, off, count, eof,
2066 cdt->cdt_user_request_mask);
2070 lprocfs_rd_hsm_group_request_mask(char *page, char **start, off_t off,
2071 int count, int *eof, void *data)
2073 struct mdt_device *mdt = data;
2074 struct coordinator *cdt = &mdt->mdt_coordinator;
2076 return lprocfs_rd_hsm_request_mask(page, start, off, count, eof,
2077 cdt->cdt_group_request_mask);
2081 lprocfs_rd_hsm_other_request_mask(char *page, char **start, off_t off,
2082 int count, int *eof, void *data)
2084 struct mdt_device *mdt = data;
2085 struct coordinator *cdt = &mdt->mdt_coordinator;
2087 return lprocfs_rd_hsm_request_mask(page, start, off, count, eof,
2088 cdt->cdt_other_request_mask);
2091 static inline enum hsm_copytool_action
2092 hsm_copytool_name2action(const char *name)
2094 if (strcasecmp(name, "NOOP") == 0)
2096 else if (strcasecmp(name, "ARCHIVE") == 0)
2097 return HSMA_ARCHIVE;
2098 else if (strcasecmp(name, "RESTORE") == 0)
2099 return HSMA_RESTORE;
2100 else if (strcasecmp(name, "REMOVE") == 0)
2102 else if (strcasecmp(name, "CANCEL") == 0)
2109 lprocfs_wr_hsm_request_mask(struct file *file, const char __user *user_buf,
2110 unsigned long user_count, __u64 *mask)
2112 char *buf, *pos, *name;
2118 if (!(user_count < 4096))
2121 buf_size = user_count + 1;
2123 OBD_ALLOC(buf, buf_size);
2127 if (copy_from_user(buf, user_buf, buf_size - 1))
2128 GOTO(out, rc = -EFAULT);
2130 buf[buf_size - 1] = '\0';
2133 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2139 action = hsm_copytool_name2action(name);
2141 GOTO(out, rc = -EINVAL);
2143 new_mask |= (1UL << action);
2149 OBD_FREE(buf, buf_size);
2155 lprocfs_wr_hsm_user_request_mask(struct file *file, const char __user *buf,
2156 unsigned long count, void *data)
2158 struct mdt_device *mdt = data;
2159 struct coordinator *cdt = &mdt->mdt_coordinator;
2161 return lprocfs_wr_hsm_request_mask(file, buf, count,
2162 &cdt->cdt_user_request_mask);
2166 lprocfs_wr_hsm_group_request_mask(struct file *file, const char __user *buf,
2167 unsigned long count, void *data)
2169 struct mdt_device *mdt = data;
2170 struct coordinator *cdt = &mdt->mdt_coordinator;
2172 return lprocfs_wr_hsm_request_mask(file, buf, count,
2173 &cdt->cdt_group_request_mask);
2177 lprocfs_wr_hsm_other_request_mask(struct file *file, const char __user *buf,
2178 unsigned long count, void *data)
2180 struct mdt_device *mdt = data;
2181 struct coordinator *cdt = &mdt->mdt_coordinator;
2183 return lprocfs_wr_hsm_request_mask(file, buf, count,
2184 &cdt->cdt_other_request_mask);
2187 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2188 { "agents", NULL, NULL, NULL, &mdt_hsm_agent_fops,
2190 { "actions", NULL, NULL, NULL, &mdt_hsm_actions_fops,
2192 { "default_archive_id", lprocfs_rd_hsm_cdt_default_archive_id,
2193 lprocfs_wr_hsm_cdt_default_archive_id,
2195 { "grace_delay", lprocfs_rd_hsm_cdt_grace_delay,
2196 lprocfs_wr_hsm_cdt_grace_delay,
2198 { "loop_period", lprocfs_rd_hsm_cdt_loop_period,
2199 lprocfs_wr_hsm_cdt_loop_period,
2201 { "max_requests", lprocfs_rd_hsm_cdt_max_requests,
2202 lprocfs_wr_hsm_cdt_max_requests,
2204 { "policy", lprocfs_rd_hsm_policy,
2205 lprocfs_wr_hsm_policy,
2207 { "active_request_timeout", lprocfs_rd_hsm_cdt_active_req_timeout,
2208 lprocfs_wr_hsm_cdt_active_req_timeout,
2210 { "active_requests", NULL, NULL, NULL,
2211 &mdt_hsm_active_requests_fops, 0 },
2212 { "user_request_mask", lprocfs_rd_hsm_user_request_mask,
2213 lprocfs_wr_hsm_user_request_mask, },
2214 { "group_request_mask", lprocfs_rd_hsm_group_request_mask,
2215 lprocfs_wr_hsm_group_request_mask, },
2216 { "other_request_mask", lprocfs_rd_hsm_other_request_mask,
2217 lprocfs_wr_hsm_other_request_mask, },