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) 2011, 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2014, Intel Corporation.
27 * Use is subject to license terms.
30 * lustre/mdt/mdt_coordinator.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <lustre_export.h>
46 #include <lprocfs_status.h>
47 #include <lustre_log.h>
48 #include <lustre_kernelcomm.h>
49 #include "mdt_internal.h"
51 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
54 * get obj and HSM attributes on a fid
55 * \param mti [IN] context
56 * \param fid [IN] object fid
57 * \param hsm [OUT] HSM meta data
58 * \retval obj or error (-ENOENT if not found)
60 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
61 const struct lu_fid *fid,
65 struct mdt_object *obj;
73 /* find object by FID */
74 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
78 if (!mdt_object_exists(obj)) {
80 mdt_object_put(mti->mti_env, obj);
81 RETURN(ERR_PTR(-ENOENT));
84 rc = mdt_attr_get_complex(mti, obj, ma);
86 mdt_object_put(mti->mti_env, obj);
90 if (ma->ma_valid & MA_HSM)
93 memset(hsm, 0, sizeof(*hsm));
98 void mdt_hsm_dump_hal(int level, const char *prefix,
99 struct hsm_action_list *hal)
102 struct hsm_action_item *hai;
105 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
106 " archive_id %d flags "LPX64"\n",
107 prefix, hal->hal_version, hal->hal_count,
108 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
110 hai = hai_first(hal);
111 for (i = 0; i < hal->hal_count; i++) {
112 sz = hai->hai_len - sizeof(*hai);
113 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
114 " compound/cookie="LPX64"/"LPX64
115 " action=%s extent="LPX64"-"LPX64" gid="LPX64
116 " datalen=%d data=[%s]\n",
118 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
119 hal->hal_compound_id, hai->hai_cookie,
120 hsm_copytool_action2name(hai->hai_action),
121 hai->hai_extent.offset,
122 hai->hai_extent.length,
124 hai_dump_data_field(hai, buf, sizeof(buf)));
130 * data passed to llog_cat_process() callback
131 * to scan requests and take actions
133 struct hsm_scan_data {
134 struct mdt_thread_info *mti;
135 char fs_name[MTI_NAME_MAXLEN+1];
136 /* request to be send to agents */
137 int request_sz; /** allocated size */
138 int max_requests; /** vector size */
139 int request_cnt; /** used count */
143 struct hsm_action_list *hal;
148 * llog_cat_process() callback, used to:
149 * - find waiting request and start action
150 * - purge canceled and done requests
151 * \param env [IN] environment
152 * \param llh [IN] llog handle
153 * \param hdr [IN] llog record
154 * \param data [IN/OUT] cb data = struct hsm_scan_data
156 * \retval -ve failure
158 static int mdt_coordinator_cb(const struct lu_env *env,
159 struct llog_handle *llh,
160 struct llog_rec_hdr *hdr,
163 struct llog_agent_req_rec *larr;
164 struct hsm_scan_data *hsd;
165 struct hsm_action_item *hai;
166 struct mdt_device *mdt;
167 struct coordinator *cdt;
172 mdt = hsd->mti->mti_mdt;
173 cdt = &mdt->mdt_coordinator;
175 larr = (struct llog_agent_req_rec *)hdr;
176 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
177 switch (larr->arr_status) {
179 int i, empty_slot, found;
181 /* Are agents full? */
182 if (atomic_read(&cdt->cdt_request_count) ==
183 cdt->cdt_max_requests)
186 /* first search whether the request is found in the list we
187 * have built and if there is room in the request vector */
190 for (i = 0; i < hsd->max_requests &&
191 (empty_slot == -1 || found == -1); i++) {
192 if (hsd->request[i].hal == NULL) {
196 if (hsd->request[i].hal->hal_compound_id ==
197 larr->arr_compound_id) {
202 if (found == -1 && empty_slot == -1)
203 /* unknown request and no more room for new request,
204 * continue scan for to find other entries for
205 * already found request
210 struct hsm_action_list *hal;
212 /* request is not already known */
213 /* allocates hai vector size just needs to be large
215 hsd->request[empty_slot].hal_sz =
216 sizeof(*hsd->request[empty_slot].hal) +
217 cfs_size_round(MTI_NAME_MAXLEN+1) +
218 2 * cfs_size_round(larr->arr_hai.hai_len);
219 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
221 CERROR("%s: Cannot allocate memory (%d o)"
222 "for compound "LPX64"\n",
224 hsd->request[i].hal_sz,
225 larr->arr_compound_id);
228 hal->hal_version = HAL_VERSION;
229 strlcpy(hal->hal_fsname, hsd->fs_name,
230 MTI_NAME_MAXLEN + 1);
231 hal->hal_compound_id = larr->arr_compound_id;
232 hal->hal_archive_id = larr->arr_archive_id;
233 hal->hal_flags = larr->arr_flags;
235 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
236 hsd->request[empty_slot].hal = hal;
239 hai = hai_first(hal);
241 /* request is known */
242 /* we check if record archive num is the same as the
243 * known request, if not we will serve it in multiple
244 * time because we do not know if the agent can serve
246 * a use case is a compound made of multiple restore
247 * where the files are not archived in the same backend
249 if (larr->arr_archive_id !=
250 hsd->request[found].hal->hal_archive_id)
253 if (hsd->request[found].hal_sz <
254 hsd->request[found].hal_used_sz +
255 cfs_size_round(larr->arr_hai.hai_len)) {
256 /* Not enough room, need an extension */
260 sz = 2 * hsd->request[found].hal_sz;
261 OBD_ALLOC(hal_buffer, sz);
263 CERROR("%s: Cannot allocate memory "
264 "(%d o) for compound "LPX64"\n",
265 mdt_obd_name(mdt), sz,
266 larr->arr_compound_id);
269 memcpy(hal_buffer, hsd->request[found].hal,
270 hsd->request[found].hal_used_sz);
271 OBD_FREE(hsd->request[found].hal,
272 hsd->request[found].hal_sz);
273 hsd->request[found].hal = hal_buffer;
274 hsd->request[found].hal_sz = sz;
276 hai = hai_first(hsd->request[found].hal);
277 for (i = 0; i < hsd->request[found].hal->hal_count;
281 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
282 hai->hai_cookie = larr->arr_hai.hai_cookie;
283 hai->hai_gid = larr->arr_hai.hai_gid;
285 hsd->request[found].hal_used_sz +=
286 cfs_size_round(hai->hai_len);
287 hsd->request[found].hal->hal_count++;
291 struct hsm_progress_kernel pgs;
292 struct cdt_agent_req *car;
293 cfs_time_t now = cfs_time_current_sec();
296 /* we search for a running request
297 * error may happen if coordinator crashes or stopped
298 * with running request
300 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
302 last = larr->arr_req_create;
304 last = car->car_req_update;
305 mdt_cdt_put_request(car);
308 /* test if request too long, if yes cancel it
309 * the same way the copy tool acknowledge a cancel request */
310 if (now <= last + cdt->cdt_active_req_timeout)
313 dump_llog_agent_req_rec("request timed out, start cleaning",
315 /* a too old cancel request just needs to be removed
316 * this can happen, if copy tool does not support
317 * cancel for other requests, we have to remove the
318 * running request and notify the copytool */
319 pgs.hpk_fid = larr->arr_hai.hai_fid;
320 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
321 pgs.hpk_extent = larr->arr_hai.hai_extent;
322 pgs.hpk_flags = HP_FLAG_COMPLETED;
323 pgs.hpk_errval = ENOSYS;
324 pgs.hpk_data_version = 0;
326 /* update request state, but do not record in llog, to
327 * avoid deadlock on cdt_llog_lock */
328 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
330 CERROR("%s: cannot cleanup timed out request: "
331 DFID" for cookie "LPX64" action=%s\n",
333 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
334 hsm_copytool_action2name(
335 larr->arr_hai.hai_action));
338 /* The request no longer exists, forget
339 * about it, and do not send a cancel request
340 * to the client, for which an error will be
341 * sent back, leading to an endless cycle of
343 RETURN(LLOG_DEL_RECORD);
346 /* XXX A cancel request cannot be cancelled. */
347 if (larr->arr_hai.hai_action == HSMA_CANCEL)
350 larr->arr_status = ARS_CANCELED;
351 larr->arr_req_change = now;
352 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
354 CERROR("%s: cannot update agent log: rc = %d\n",
355 mdt_obd_name(mdt), rc);
361 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
362 cfs_time_current_sec())
363 RETURN(LLOG_DEL_RECORD);
370 * create /proc entries for coordinator
373 * \retval -ve failure
375 int hsm_cdt_procfs_init(struct mdt_device *mdt)
377 struct coordinator *cdt = &mdt->mdt_coordinator;
381 /* init /proc entries, failure is not critical */
382 cdt->cdt_proc_dir = lprocfs_register("hsm",
383 mdt2obd_dev(mdt)->obd_proc_entry,
384 lprocfs_mdt_hsm_vars, mdt);
385 if (IS_ERR(cdt->cdt_proc_dir)) {
386 rc = PTR_ERR(cdt->cdt_proc_dir);
387 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
388 " rc=%d\n", mdt_obd_name(mdt), rc);
389 cdt->cdt_proc_dir = NULL;
397 * remove /proc entries for coordinator
400 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
402 struct coordinator *cdt = &mdt->mdt_coordinator;
404 LASSERT(cdt->cdt_state == CDT_STOPPED);
405 if (cdt->cdt_proc_dir != NULL)
406 lprocfs_remove(&cdt->cdt_proc_dir);
410 * get vector of hsm cdt /proc vars
414 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
416 return lprocfs_mdt_hsm_vars;
421 * \param data [IN] obd device
423 * \retval -ve failure
425 static int mdt_coordinator(void *data)
427 struct mdt_thread_info *mti = data;
428 struct mdt_device *mdt = mti->mti_mdt;
429 struct coordinator *cdt = &mdt->mdt_coordinator;
430 struct hsm_scan_data hsd = { NULL };
434 cdt->cdt_thread.t_flags = SVC_RUNNING;
435 wake_up(&cdt->cdt_thread.t_ctl_waitq);
437 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
438 mdt_obd_name(mdt), current_pid());
440 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
441 * increases due to a change from /proc we do not overflow the
442 * hsd.request[] vector
444 hsd.max_requests = cdt->cdt_max_requests;
445 hsd.request_sz = hsd.max_requests * sizeof(*hsd.request);
446 OBD_ALLOC(hsd.request, hsd.request_sz);
448 GOTO(out, rc = -ENOMEM);
451 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
454 struct l_wait_info lwi;
457 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
459 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
460 (cdt->cdt_thread.t_flags &
461 (SVC_STOPPING|SVC_EVENT)),
464 CDEBUG(D_HSM, "coordinator resumes\n");
466 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
467 cdt->cdt_state == CDT_STOPPING) {
468 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
473 /* wake up before timeout, new work arrives */
474 if (cdt->cdt_thread.t_flags & SVC_EVENT)
475 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
477 /* if coordinator is suspended continue to wait */
478 if (cdt->cdt_state == CDT_DISABLE) {
479 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
483 CDEBUG(D_HSM, "coordinator starts reading llog\n");
485 if (hsd.max_requests != cdt->cdt_max_requests) {
486 /* cdt_max_requests has changed,
487 * we need to allocate a new buffer
489 OBD_FREE(hsd.request, hsd.request_sz);
490 hsd.max_requests = cdt->cdt_max_requests;
492 hsd.max_requests * sizeof(*hsd.request);
493 OBD_ALLOC(hsd.request, hsd.request_sz);
502 rc = cdt_llog_process(mti->mti_env, mdt,
503 mdt_coordinator_cb, &hsd);
507 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
509 if (list_empty(&cdt->cdt_agents)) {
510 CDEBUG(D_HSM, "no agent available, "
511 "coordinator sleeps\n");
515 /* here hsd contains a list of requests to be started */
516 for (i = 0; i < hsd.max_requests; i++) {
517 struct hsm_action_list *hal;
518 struct hsm_action_item *hai;
521 enum agent_req_status status;
523 /* still room for work ? */
524 if (atomic_read(&cdt->cdt_request_count) ==
525 cdt->cdt_max_requests)
528 if (hsd.request[i].hal == NULL)
531 /* found a request, we start it */
532 /* kuc payload allocation so we avoid an additionnal
533 * allocation in mdt_hsm_agent_send()
535 hal = kuc_alloc(hsd.request[i].hal_used_sz,
536 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
538 CERROR("%s: Cannot allocate memory (%d o) "
539 "for compound "LPX64"\n",
541 hsd.request[i].hal_used_sz,
542 hsd.request[i].hal->hal_compound_id);
545 memcpy(hal, hsd.request[i].hal,
546 hsd.request[i].hal_used_sz);
548 rc = mdt_hsm_agent_send(mti, hal, 0);
549 /* if failure, we suppose it is temporary
550 * if the copy tool failed to do the request
551 * it has to use hsm_progress
553 status = (rc ? ARS_WAITING : ARS_STARTED);
555 /* set up cookie vector to set records status
556 * after copy tools start or failed
558 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
559 OBD_ALLOC(cookies, sz);
560 if (cookies == NULL) {
561 CERROR("%s: Cannot allocate memory (%d o) "
562 "for cookies vector "LPX64"\n",
563 mdt_obd_name(mdt), sz,
564 hsd.request[i].hal->hal_compound_id);
565 kuc_free(hal, hsd.request[i].hal_used_sz);
568 hai = hai_first(hal);
569 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
570 cookies[j] = hai->hai_cookie;
574 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
575 hsd.request[i].hal->hal_count,
578 CERROR("%s: mdt_agent_record_update() failed, "
579 "rc=%d, cannot update status to %s "
581 mdt_obd_name(mdt), rc,
582 agent_req_status2name(status),
583 hsd.request[i].hal->hal_count);
585 OBD_FREE(cookies, sz);
586 kuc_free(hal, hsd.request[i].hal_used_sz);
589 /* free hal allocated by callback */
590 for (i = 0; i < hsd.max_requests; i++) {
591 if (hsd.request[i].hal) {
592 OBD_FREE(hsd.request[i].hal,
593 hsd.request[i].hal_sz);
594 hsd.request[i].hal_sz = 0;
595 hsd.request[i].hal = NULL;
599 LASSERT(hsd.request_cnt == 0);
601 /* reset callback data */
602 memset(hsd.request, 0, hsd.request_sz);
607 OBD_FREE(hsd.request, hsd.request_sz);
609 if (cdt->cdt_state == CDT_STOPPING) {
610 /* request comes from /proc path, so we need to clean cdt
612 mdt_hsm_cdt_stop(mdt);
613 mdt->mdt_opts.mo_coordinator = 0;
615 /* request comes from a thread event, generated
616 * by mdt_stop_coordinator(), we have to ack
617 * and cdt cleaning will be done by event sender
619 cdt->cdt_thread.t_flags = SVC_STOPPED;
620 wake_up(&cdt->cdt_thread.t_ctl_waitq);
624 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
625 mdt_obd_name(mdt), current_pid(), rc);
627 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
629 mdt_obd_name(mdt), current_pid());
635 * lookup a restore handle by FID
636 * caller needs to hold cdt_restore_lock
637 * \param cdt [IN] coordinator
638 * \param fid [IN] FID
639 * \retval cdt_restore_handle found
640 * \retval NULL not found
642 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
643 const struct lu_fid *fid)
645 struct cdt_restore_handle *crh;
648 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
649 if (lu_fid_eq(&crh->crh_fid, fid))
656 * data passed to llog_cat_process() callback
657 * to scan requests and take actions
659 struct hsm_restore_data {
660 struct mdt_thread_info *hrd_mti;
664 * llog_cat_process() callback, used to:
665 * - find restore request and allocate the restore handle
666 * \param env [IN] environment
667 * \param llh [IN] llog handle
668 * \param hdr [IN] llog record
669 * \param data [IN/OUT] cb data = struct hsm_restore_data
671 * \retval -ve failure
673 static int hsm_restore_cb(const struct lu_env *env,
674 struct llog_handle *llh,
675 struct llog_rec_hdr *hdr, void *data)
677 struct llog_agent_req_rec *larr;
678 struct hsm_restore_data *hrd;
679 struct cdt_restore_handle *crh;
680 struct hsm_action_item *hai;
681 struct mdt_thread_info *mti;
682 struct coordinator *cdt;
683 struct mdt_object *child;
689 cdt = &mti->mti_mdt->mdt_coordinator;
691 larr = (struct llog_agent_req_rec *)hdr;
692 hai = &larr->arr_hai;
693 if (hai->hai_cookie > cdt->cdt_last_cookie)
694 /* update the cookie to avoid collision */
695 cdt->cdt_last_cookie = hai->hai_cookie + 1;
697 if (hai->hai_action != HSMA_RESTORE ||
698 agent_req_in_final_state(larr->arr_status))
701 /* restore request not in a final state */
703 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
707 crh->crh_fid = hai->hai_fid;
708 /* in V1 all file is restored
709 crh->extent.start = hai->hai_extent.offset;
710 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
712 crh->crh_extent.start = 0;
713 crh->crh_extent.end = hai->hai_extent.length;
714 /* get the layout lock */
715 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
716 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
717 MDS_INODELOCK_LAYOUT);
719 GOTO(out, rc = PTR_ERR(child));
722 /* we choose to not keep a reference
723 * on the object during the restore time which can be very long */
724 mdt_object_put(mti->mti_env, child);
726 mutex_lock(&cdt->cdt_restore_lock);
727 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
728 mutex_unlock(&cdt->cdt_restore_lock);
735 * restore coordinator state at startup
736 * the goal is to take a layout lock for each registered restore request
737 * \param mti [IN] context
739 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
741 struct hsm_restore_data hrd;
747 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
748 hsm_restore_cb, &hrd);
753 static int hsm_init_ucred(struct lu_ucred *uc)
757 uc->uc_valid = UCRED_OLD;
766 uc->uc_suppgids[0] = -1;
767 uc->uc_suppgids[1] = -1;
768 uc->uc_cap = CFS_CAP_FS_MASK;
771 uc->uc_identity = NULL;
777 * wake up coordinator thread
778 * \param mdt [IN] device
780 * \retval -ve failure
782 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
784 struct coordinator *cdt = &mdt->mdt_coordinator;
787 if (cdt->cdt_state == CDT_STOPPED)
790 /* wake up coordinator */
791 cdt->cdt_thread.t_flags = SVC_EVENT;
792 wake_up(&cdt->cdt_thread.t_ctl_waitq);
798 * initialize coordinator struct
799 * \param mdt [IN] device
801 * \retval -ve failure
803 int mdt_hsm_cdt_init(struct mdt_device *mdt)
805 struct coordinator *cdt = &mdt->mdt_coordinator;
806 struct mdt_thread_info *cdt_mti = NULL;
810 cdt->cdt_state = CDT_STOPPED;
812 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
813 mutex_init(&cdt->cdt_llog_lock);
814 init_rwsem(&cdt->cdt_agent_lock);
815 init_rwsem(&cdt->cdt_request_lock);
816 mutex_init(&cdt->cdt_restore_lock);
818 INIT_LIST_HEAD(&cdt->cdt_requests);
819 INIT_LIST_HEAD(&cdt->cdt_agents);
820 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
822 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
826 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
827 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
829 lu_context_enter(&cdt->cdt_session);
830 cdt->cdt_env.le_ses = &cdt->cdt_session;
832 lu_env_fini(&cdt->cdt_env);
836 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
837 LASSERT(cdt_mti != NULL);
839 cdt_mti->mti_env = &cdt->cdt_env;
840 cdt_mti->mti_mdt = mdt;
842 hsm_init_ucred(mdt_ucred(cdt_mti));
844 /* default values for /proc tunnables
845 * can be override by MGS conf */
846 cdt->cdt_default_archive_id = 1;
847 cdt->cdt_grace_delay = 60;
848 cdt->cdt_loop_period = 10;
849 cdt->cdt_max_requests = 3;
850 cdt->cdt_policy = CDT_DEFAULT_POLICY;
851 cdt->cdt_active_req_timeout = 3600;
857 * free a coordinator thread
858 * \param mdt [IN] device
860 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
862 struct coordinator *cdt = &mdt->mdt_coordinator;
865 lu_context_exit(cdt->cdt_env.le_ses);
866 lu_context_fini(cdt->cdt_env.le_ses);
868 lu_env_fini(&cdt->cdt_env);
874 * start a coordinator thread
875 * \param mdt [IN] device
877 * \retval -ve failure
879 int mdt_hsm_cdt_start(struct mdt_device *mdt)
881 struct coordinator *cdt = &mdt->mdt_coordinator;
884 struct mdt_thread_info *cdt_mti;
885 struct task_struct *task;
888 /* functions defined but not yet used
889 * this avoid compilation warning
893 if (cdt->cdt_state != CDT_STOPPED) {
894 CERROR("%s: Coordinator already started\n",
899 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
900 cdt->cdt_policy = CDT_DEFAULT_POLICY;
902 cdt->cdt_state = CDT_INIT;
904 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
905 /* just need to be larger than previous one */
906 /* cdt_last_cookie is protected by cdt_llog_lock */
907 cdt->cdt_last_cookie = cfs_time_current_sec();
908 atomic_set(&cdt->cdt_request_count, 0);
909 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
910 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
911 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
913 /* to avoid deadlock when start is made through /proc
914 * /proc entries are created by the coordinator thread */
916 /* set up list of started restore requests */
917 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
918 rc = mdt_hsm_pending_restore(cdt_mti);
920 CERROR("%s: cannot take the layout locks needed"
921 " for registered restore: %d\n",
922 mdt_obd_name(mdt), rc);
924 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
927 cdt->cdt_state = CDT_STOPPED;
928 CERROR("%s: error starting coordinator thread: %d\n",
929 mdt_obd_name(mdt), rc);
932 CDEBUG(D_HSM, "%s: coordinator thread started\n",
937 wait_event(cdt->cdt_thread.t_ctl_waitq,
938 (cdt->cdt_thread.t_flags & SVC_RUNNING));
940 cdt->cdt_state = CDT_RUNNING;
941 mdt->mdt_opts.mo_coordinator = 1;
946 * stop a coordinator thread
947 * \param mdt [IN] device
949 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
951 struct coordinator *cdt = &mdt->mdt_coordinator;
952 struct cdt_agent_req *car, *tmp1;
953 struct hsm_agent *ha, *tmp2;
954 struct cdt_restore_handle *crh, *tmp3;
955 struct mdt_thread_info *cdt_mti;
958 if (cdt->cdt_state == CDT_STOPPED) {
959 CERROR("%s: Coordinator already stopped\n",
964 if (cdt->cdt_state != CDT_STOPPING) {
965 /* stop coordinator thread before cleaning */
966 cdt->cdt_thread.t_flags = SVC_STOPPING;
967 wake_up(&cdt->cdt_thread.t_ctl_waitq);
968 wait_event(cdt->cdt_thread.t_ctl_waitq,
969 cdt->cdt_thread.t_flags & SVC_STOPPED);
971 cdt->cdt_state = CDT_STOPPED;
974 down_write(&cdt->cdt_request_lock);
975 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
977 list_del(&car->car_request_list);
978 mdt_cdt_free_request(car);
980 up_write(&cdt->cdt_request_lock);
982 down_write(&cdt->cdt_agent_lock);
983 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
984 list_del(&ha->ha_list);
987 up_write(&cdt->cdt_agent_lock);
989 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
990 mutex_lock(&cdt->cdt_restore_lock);
991 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
992 struct mdt_object *child;
994 /* give back layout lock */
995 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
997 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
999 list_del(&crh->crh_list);
1001 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1003 mutex_unlock(&cdt->cdt_restore_lock);
1005 mdt->mdt_opts.mo_coordinator = 0;
1011 * register all requests from an hal in the memory list
1012 * \param mti [IN] context
1013 * \param hal [IN] request
1014 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1015 * which is running the CT
1017 * \retval -ve failure
1019 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1020 struct hsm_action_list *hal, struct obd_uuid *uuid)
1022 struct mdt_device *mdt = mti->mti_mdt;
1023 struct coordinator *cdt = &mdt->mdt_coordinator;
1024 struct hsm_action_item *hai;
1028 /* register request in memory list */
1029 hai = hai_first(hal);
1030 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1031 struct cdt_agent_req *car;
1033 /* in case of a cancel request, we first mark the ondisk
1034 * record of the request we want to stop as canceled
1035 * this does not change the cancel record
1036 * it will be done when updating the request status
1038 if (hai->hai_action == HSMA_CANCEL) {
1039 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1043 CERROR("%s: mdt_agent_record_update() failed, "
1044 "rc=%d, cannot update status to %s "
1045 "for cookie "LPX64"\n",
1046 mdt_obd_name(mdt), rc,
1047 agent_req_status2name(ARS_CANCELED),
1052 /* find the running request to set it canceled */
1053 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1055 car->car_canceled = 1;
1056 /* uuid has to be changed to the one running the
1057 * request to cancel */
1058 *uuid = car->car_uuid;
1059 mdt_cdt_put_request(car);
1061 /* no need to memorize cancel request
1062 * this also avoid a deadlock when we receive
1063 * a purge all requests command
1068 if (hai->hai_action == HSMA_ARCHIVE) {
1069 struct mdt_object *obj;
1072 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1073 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1076 GOTO(out, rc = PTR_ERR(obj));
1078 hsm.mh_flags |= HS_EXISTS;
1079 hsm.mh_arch_id = hal->hal_archive_id;
1080 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1081 mdt_object_put(mti->mti_env, obj);
1086 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1087 hal->hal_archive_id, hal->hal_flags,
1090 GOTO(out, rc = PTR_ERR(car));
1092 rc = mdt_cdt_add_request(cdt, car);
1094 mdt_cdt_free_request(car);
1101 * swap layouts between 2 fids
1102 * \param mti [IN] context
1105 * \param mh_common [IN] MD HSM
1107 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1108 const lustre_fid *fid, const lustre_fid *dfid,
1109 struct md_hsm *mh_common)
1111 struct mdt_device *mdt = mti->mti_mdt;
1112 struct mdt_object *child1, *child2;
1113 struct mdt_lock_handle *lh2;
1117 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1119 GOTO(out, rc = PTR_ERR(child1));
1121 /* we already have layout lock on FID so take only
1123 lh2 = &mti->mti_lh[MDT_LH_OLD];
1124 mdt_lock_reg_init(lh2, LCK_EX);
1125 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1127 GOTO(out_child1, rc = PTR_ERR(child2));
1129 /* if copy tool closes the volatile before sending the final
1130 * progress through llapi_hsm_copy_end(), all the objects
1131 * are removed and mdd_swap_layout LBUG */
1132 if (!mdt_object_exists(child2)) {
1133 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1134 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1135 GOTO(out_child2, rc = -ENOENT);
1137 /* Since we only handle restores here, unconditionally use
1138 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1139 * be preserved in case of failure during swap_layout and not
1140 * leave a file in an intermediate but incoherent state.
1141 * But need to setup HSM xattr of data FID before, reuse
1142 * mti and mh presets for FID in hsm_cdt_request_completed(),
1143 * only need to clear RELEASED and DIRTY.
1145 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1146 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1148 rc = mo_swap_layouts(mti->mti_env,
1149 mdt_object_child(child1),
1150 mdt_object_child(child2),
1151 SWAP_LAYOUTS_MDS_HSM);
1154 mdt_object_unlock_put(mti, child2, lh2, 1);
1156 mdt_object_put(mti->mti_env, child1);
1162 * update status of a completed request
1163 * \param mti [IN] context
1164 * \param pgs [IN] progress of the copy tool
1165 * \param update_record [IN] update llog record
1167 * \retval -ve failure
1169 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1170 struct hsm_progress_kernel *pgs,
1171 const struct cdt_agent_req *car,
1172 enum agent_req_status *status)
1174 const struct lu_env *env = mti->mti_env;
1175 struct mdt_device *mdt = mti->mti_mdt;
1176 struct coordinator *cdt = &mdt->mdt_coordinator;
1177 struct mdt_object *obj = NULL;
1178 int cl_flags = 0, rc = 0;
1183 /* default is to retry */
1184 *status = ARS_WAITING;
1186 /* find object by FID */
1187 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1188 /* we will update MD HSM only if needed */
1189 is_mh_changed = false;
1191 /* object removed */
1192 *status = ARS_SUCCEED;
1196 /* no need to change mh->mh_arch_id
1197 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1199 if (pgs->hpk_errval != 0) {
1200 switch (pgs->hpk_errval) {
1202 /* the copy tool does not support cancel
1203 * so the cancel request is failed
1204 * As we cannot distinguish a cancel progress
1205 * from another action progress (they have the
1206 * same cookie), we suppose here the CT returns
1207 * ENOSYS only if does not support cancel
1209 /* this can also happen when cdt calls it to
1210 * for a timed out request */
1211 *status = ARS_FAILED;
1212 /* to have a cancel event in changelog */
1213 pgs->hpk_errval = ECANCELED;
1216 /* the request record has already been set to
1217 * ARS_CANCELED, this set the cancel request
1219 *status = ARS_SUCCEED;
1222 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1223 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1224 ARS_FAILED : ARS_WAITING);
1228 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1229 CERROR("%s: Request "LPX64" on "DFID
1230 " failed, error code %d too large\n",
1232 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1234 hsm_set_cl_error(&cl_flags,
1235 CLF_HSM_ERROVERFLOW);
1238 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1241 switch (car->car_hai->hai_action) {
1243 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1246 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1249 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1252 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1253 CERROR("%s: Failed request "LPX64" on "DFID
1254 " cannot be a CANCEL\n",
1257 PFID(&pgs->hpk_fid));
1260 CERROR("%s: Failed request "LPX64" on "DFID
1261 " %d is an unknown action\n",
1263 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1264 car->car_hai->hai_action);
1269 *status = ARS_SUCCEED;
1270 switch (car->car_hai->hai_action) {
1272 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1273 /* set ARCHIVE keep EXIST and clear LOST and
1275 mh.mh_arch_ver = pgs->hpk_data_version;
1276 mh.mh_flags |= HS_ARCHIVED;
1277 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1278 is_mh_changed = true;
1281 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1283 /* do not clear RELEASED and DIRTY here
1284 * this will occur in hsm_swap_layouts()
1287 /* Restoring has changed the file version on
1289 mh.mh_arch_ver = pgs->hpk_data_version;
1290 is_mh_changed = true;
1293 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1294 /* clear ARCHIVED EXISTS and LOST */
1295 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1296 is_mh_changed = true;
1299 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1300 CERROR("%s: Successful request "LPX64
1302 " cannot be a CANCEL\n",
1305 PFID(&pgs->hpk_fid));
1308 CERROR("%s: Successful request "LPX64
1310 " %d is an unknown action\n",
1312 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1313 car->car_hai->hai_action);
1319 /* rc != 0 means error when analysing action, it may come from
1320 * a crasy CT no need to manage DIRTY
1323 hsm_set_cl_flags(&cl_flags,
1324 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1326 /* unlock is done later, after layout lock management */
1328 rc = mdt_hsm_attr_set(mti, obj, &mh);
1331 /* we give back layout lock only if restore was successful or
1332 * if restore was canceled or if policy is to not retry
1333 * in other cases we just unlock the object */
1334 if (car->car_hai->hai_action == HSMA_RESTORE &&
1335 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1336 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1337 struct cdt_restore_handle *crh;
1339 /* restore in data FID done, we swap the layouts
1340 * only if restore is successful */
1341 if (pgs->hpk_errval == 0) {
1342 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1343 &car->car_hai->hai_dfid, &mh);
1345 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1346 *status = ARS_FAILED;
1347 pgs->hpk_errval = -rc;
1350 /* we have to retry, so keep layout lock */
1351 if (*status == ARS_WAITING)
1354 /* give back layout lock */
1355 mutex_lock(&cdt->cdt_restore_lock);
1356 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1358 list_del(&crh->crh_list);
1359 mutex_unlock(&cdt->cdt_restore_lock);
1360 /* just give back layout lock, we keep
1361 * the reference which is given back
1362 * later with the lock for HSM flags */
1363 if (!IS_ERR(obj) && crh != NULL)
1364 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1367 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1373 if (obj != NULL && !IS_ERR(obj)) {
1374 mo_changelog(env, CL_HSM, cl_flags,
1375 mdt_object_child(obj));
1376 mdt_object_put(mti->mti_env, obj);
1383 * update status of a request
1384 * \param mti [IN] context
1385 * \param pgs [IN] progress of the copy tool
1386 * \param update_record [IN] update llog record
1388 * \retval -ve failure
1390 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1391 struct hsm_progress_kernel *pgs,
1392 const int update_record)
1394 struct mdt_device *mdt = mti->mti_mdt;
1395 struct coordinator *cdt = &mdt->mdt_coordinator;
1396 struct cdt_agent_req *car;
1400 /* no coordinator started, so we cannot serve requests */
1401 if (cdt->cdt_state == CDT_STOPPED)
1404 /* first do sanity checks */
1405 car = mdt_cdt_update_request(cdt, pgs);
1407 CERROR("%s: Cannot find running request for cookie "LPX64
1410 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1412 RETURN(PTR_ERR(car));
1415 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1416 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1417 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1418 hsm_copytool_action2name(car->car_hai->hai_action),
1419 pgs->hpk_flags, pgs->hpk_errval,
1420 PFID(&car->car_hai->hai_fid),
1421 PFID(&car->car_hai->hai_dfid));
1423 /* progress is done on FID or data FID depending of the action and
1424 * of the copy progress */
1425 /* for restore progress is used to send back the data FID to cdt */
1426 if (car->car_hai->hai_action == HSMA_RESTORE &&
1427 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1428 car->car_hai->hai_dfid = pgs->hpk_fid;
1430 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1431 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1432 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1433 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1434 CERROR("%s: Progress on "DFID" for cookie "LPX64
1435 " does not match request FID "DFID" nor data FID "
1438 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1439 PFID(&car->car_hai->hai_fid),
1440 PFID(&car->car_hai->hai_dfid));
1441 GOTO(out, rc = -EINVAL);
1444 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1445 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1446 " is not coherent (err=%d and not completed"
1449 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1450 hsm_copytool_action2name(car->car_hai->hai_action),
1451 pgs->hpk_errval, pgs->hpk_flags);
1452 GOTO(out, rc = -EINVAL);
1455 /* now progress is valid */
1457 /* we use a root like ucred */
1458 hsm_init_ucred(mdt_ucred(mti));
1460 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1461 enum agent_req_status status;
1463 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1465 /* remove request from memory list */
1466 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1468 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1469 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1471 hsm_copytool_action2name(car->car_hai->hai_action),
1472 agent_req_status2name(status));
1474 if (update_record) {
1477 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1478 &pgs->hpk_cookie, 1,
1481 CERROR("%s: mdt_agent_record_update() failed,"
1482 " rc=%d, cannot update status to %s"
1483 " for cookie "LPX64"\n",
1484 mdt_obd_name(mdt), rc1,
1485 agent_req_status2name(status),
1487 rc = (rc != 0 ? rc : rc1);
1489 /* ct has completed a request, so a slot is available, wakeup
1490 * cdt to find new work */
1491 mdt_hsm_cdt_wakeup(mdt);
1493 /* if copytool send a progress on a canceled request
1494 * we inform copytool it should stop
1496 if (car->car_canceled == 1)
1502 /* remove ref got from mdt_cdt_update_request() */
1503 mdt_cdt_put_request(car);
1510 * data passed to llog_cat_process() callback
1511 * to cancel requests
1513 struct hsm_cancel_all_data {
1514 struct mdt_device *mdt;
1518 * llog_cat_process() callback, used to:
1519 * - purge all requests
1520 * \param env [IN] environment
1521 * \param llh [IN] llog handle
1522 * \param hdr [IN] llog record
1523 * \param data [IN] cb data = struct hsm_cancel_all_data
1525 * \retval -ve failure
1527 static int mdt_cancel_all_cb(const struct lu_env *env,
1528 struct llog_handle *llh,
1529 struct llog_rec_hdr *hdr, void *data)
1531 struct llog_agent_req_rec *larr;
1532 struct hsm_cancel_all_data *hcad;
1536 larr = (struct llog_agent_req_rec *)hdr;
1538 if (larr->arr_status == ARS_WAITING ||
1539 larr->arr_status == ARS_STARTED) {
1540 larr->arr_status = ARS_CANCELED;
1541 larr->arr_req_change = cfs_time_current_sec();
1542 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1549 * cancel all actions
1550 * \param obd [IN] MDT device
1552 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1554 struct mdt_thread_info *mti;
1555 struct coordinator *cdt = &mdt->mdt_coordinator;
1556 struct cdt_agent_req *car;
1557 struct hsm_action_list *hal = NULL;
1558 struct hsm_action_item *hai;
1559 struct hsm_cancel_all_data hcad;
1560 int hal_sz = 0, hal_len, rc;
1561 enum cdt_states save_state;
1564 /* retrieve coordinator context */
1565 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1567 /* disable coordinator */
1568 save_state = cdt->cdt_state;
1569 cdt->cdt_state = CDT_DISABLE;
1571 /* send cancel to all running requests */
1572 down_read(&cdt->cdt_request_lock);
1573 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1574 mdt_cdt_get_request(car);
1575 /* request is not yet removed from list, it will be done
1576 * when copytool will return progress
1579 if (car->car_hai->hai_action == HSMA_CANCEL) {
1580 mdt_cdt_put_request(car);
1585 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1586 cfs_size_round(car->car_hai->hai_len);
1588 if (hal_len > hal_sz && hal_sz > 0) {
1589 /* not enough room, free old buffer */
1590 OBD_FREE(hal, hal_sz);
1594 /* empty buffer, allocate one */
1597 OBD_ALLOC(hal, hal_sz);
1599 mdt_cdt_put_request(car);
1600 up_read(&cdt->cdt_request_lock);
1601 GOTO(out, rc = -ENOMEM);
1605 hal->hal_version = HAL_VERSION;
1606 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1608 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1609 hal->hal_compound_id = car->car_compound_id;
1610 hal->hal_archive_id = car->car_archive_id;
1611 hal->hal_flags = car->car_flags;
1614 hai = hai_first(hal);
1615 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1616 hai->hai_action = HSMA_CANCEL;
1619 /* it is possible to safely call mdt_hsm_agent_send()
1620 * (ie without a deadlock on cdt_request_lock), because the
1621 * write lock is taken only if we are not in purge mode
1622 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1623 * nor mdt_cdt_remove_request())
1625 /* no conflict with cdt thread because cdt is disable and we
1626 * have the request lock */
1627 mdt_hsm_agent_send(mti, hal, 1);
1629 mdt_cdt_put_request(car);
1631 up_read(&cdt->cdt_request_lock);
1634 OBD_FREE(hal, hal_sz);
1636 /* cancel all on-disk records */
1639 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1640 mdt_cancel_all_cb, &hcad);
1642 /* enable coordinator */
1643 cdt->cdt_state = save_state;
1649 * check if a request is compatible with file status
1650 * \param hai [IN] request description
1651 * \param hal_an [IN] request archive number (not used)
1652 * \param rq_flags [IN] request flags
1653 * \param hsm [IN] file HSM metadata
1656 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1657 const int hal_an, const __u64 rq_flags,
1658 const struct md_hsm *hsm)
1660 int is_compat = false;
1664 hsm_flags = hsm->mh_flags;
1665 switch (hai->hai_action) {
1667 if (!(hsm_flags & HS_NOARCHIVE) &&
1668 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1672 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1673 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1677 if (!(hsm_flags & HS_RELEASED) &&
1678 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1685 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1686 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1687 PFID(&hai->hai_fid),
1688 hsm_copytool_action2name(hai->hai_action), rq_flags,
1689 hai->hai_extent.offset, hai->hai_extent.length,
1691 (is_compat ? "compatible" : "uncompatible"));
1697 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1699 static const struct {
1703 } hsm_policy_names[] = {
1704 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1705 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1710 * convert a policy name to a bit
1711 * \param name [IN] policy name
1713 * \retval policy bit
1715 static __u64 hsm_policy_str2bit(const char *name)
1719 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1720 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1721 strcmp(hsm_policy_names[i].name, name) == 0)
1722 return hsm_policy_names[i].bit;
1727 * convert a policy bit field to a string
1728 * \param mask [IN] policy bit field
1729 * \param hexa [IN] print mask before bit names
1730 * \param buffer [OUT] string
1731 * \param count [IN] size of buffer
1733 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1741 seq_printf(m, "("LPX64") ", mask);
1743 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1746 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1747 if (hsm_policy_names[j].bit == bit)
1751 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1753 seq_printf(m, "%s ", hsm_policy_names[j].name);
1755 /* remove last ' ' */
1760 /* methods to read/write HSM policy flags */
1761 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1763 struct mdt_device *mdt = m->private;
1764 struct coordinator *cdt = &mdt->mdt_coordinator;
1767 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1772 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1773 size_t count, loff_t *off)
1775 struct seq_file *m = file->private_data;
1776 struct mdt_device *mdt = m->private;
1777 struct coordinator *cdt = &mdt->mdt_coordinator;
1778 char *start, *token, sign;
1781 __u64 add_mask, remove_mask, set_mask;
1785 if (count + 1 > PAGE_SIZE)
1788 OBD_ALLOC(buf, count + 1);
1792 if (copy_from_user(buf, buffer, count))
1793 GOTO(out, rc = -EFAULT);
1798 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1801 add_mask = remove_mask = set_mask = 0;
1803 token = strsep(&start, "\n ");
1809 if (sign == '-' || sign == '+')
1812 policy = hsm_policy_str2bit(token);
1814 CWARN("%s: '%s' is unknown, "
1815 "supported policies are:\n", mdt_obd_name(mdt),
1817 hsm_policy_bit2str(m, 0, false);
1818 GOTO(out, rc = -EINVAL);
1822 remove_mask |= policy;
1832 } while (start != NULL);
1834 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1835 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1837 /* if no sign in all string, it is a clear and set
1838 * if some sign found, all unsigned are converted
1840 * P1 P2 = set to P1 and P2
1841 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1843 if (remove_mask == 0 && add_mask == 0) {
1844 cdt->cdt_policy = set_mask;
1846 cdt->cdt_policy |= set_mask | add_mask;
1847 cdt->cdt_policy &= ~remove_mask;
1850 GOTO(out, rc = count);
1853 OBD_FREE(buf, count + 1);
1856 LPROC_SEQ_FOPS(mdt_hsm_policy);
1858 #define GENERATE_PROC_METHOD(VAR) \
1859 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1861 struct mdt_device *mdt = m->private; \
1862 struct coordinator *cdt = &mdt->mdt_coordinator; \
1865 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1869 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1870 size_t count, loff_t *off) \
1873 struct seq_file *m = file->private_data; \
1874 struct mdt_device *mdt = m->private; \
1875 struct coordinator *cdt = &mdt->mdt_coordinator; \
1880 rc = lprocfs_write_helper(buffer, count, &val); \
1890 GENERATE_PROC_METHOD(cdt_loop_period)
1891 GENERATE_PROC_METHOD(cdt_grace_delay)
1892 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1893 GENERATE_PROC_METHOD(cdt_max_requests)
1894 GENERATE_PROC_METHOD(cdt_default_archive_id)
1897 * procfs write method for MDT/hsm_control
1898 * proc entry is in mdt directory so data is mdt obd_device pointer
1900 #define CDT_ENABLE_CMD "enabled"
1901 #define CDT_STOP_CMD "shutdown"
1902 #define CDT_DISABLE_CMD "disabled"
1903 #define CDT_PURGE_CMD "purge"
1904 #define CDT_HELP_CMD "help"
1905 #define CDT_MAX_CMD_LEN 10
1908 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1909 size_t count, loff_t *off)
1911 struct seq_file *m = file->private_data;
1912 struct obd_device *obd = m->private;
1913 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1914 struct coordinator *cdt = &(mdt->mdt_coordinator);
1916 char kernbuf[CDT_MAX_CMD_LEN];
1919 if (count == 0 || count >= sizeof(kernbuf))
1922 if (copy_from_user(kernbuf, buffer, count))
1926 if (kernbuf[count - 1] == '\n')
1927 kernbuf[count - 1] = 0;
1930 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1931 if (cdt->cdt_state == CDT_DISABLE) {
1932 cdt->cdt_state = CDT_RUNNING;
1933 mdt_hsm_cdt_wakeup(mdt);
1935 rc = mdt_hsm_cdt_start(mdt);
1937 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1938 if ((cdt->cdt_state == CDT_STOPPING) ||
1939 (cdt->cdt_state == CDT_STOPPED)) {
1940 CERROR("%s: Coordinator already stopped\n",
1944 cdt->cdt_state = CDT_STOPPING;
1946 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1947 if ((cdt->cdt_state == CDT_STOPPING) ||
1948 (cdt->cdt_state == CDT_STOPPED)) {
1949 CERROR("%s: Coordinator is stopped\n",
1953 cdt->cdt_state = CDT_DISABLE;
1955 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1956 rc = hsm_cancel_all_actions(mdt);
1957 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1965 CERROR("%s: Valid coordinator control commands are: "
1966 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1967 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1968 CDT_PURGE_CMD, CDT_HELP_CMD);
1976 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1978 struct obd_device *obd = m->private;
1979 struct coordinator *cdt;
1982 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1984 if (cdt->cdt_state == CDT_INIT)
1985 seq_printf(m, "init\n");
1986 else if (cdt->cdt_state == CDT_RUNNING)
1987 seq_printf(m, "enabled\n");
1988 else if (cdt->cdt_state == CDT_STOPPING)
1989 seq_printf(m, "stopping\n");
1990 else if (cdt->cdt_state == CDT_STOPPED)
1991 seq_printf(m, "stopped\n");
1992 else if (cdt->cdt_state == CDT_DISABLE)
1993 seq_printf(m, "disabled\n");
1995 seq_printf(m, "unknown\n");
2001 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2007 for (i = 0; i < 8 * sizeof(mask); i++) {
2008 if (mask & (1UL << i)) {
2009 seq_printf(m, "%s%s", first ? "" : " ",
2010 hsm_copytool_action2name(i));
2020 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2022 struct mdt_device *mdt = m->private;
2023 struct coordinator *cdt = &mdt->mdt_coordinator;
2025 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2029 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2031 struct mdt_device *mdt = m->private;
2032 struct coordinator *cdt = &mdt->mdt_coordinator;
2034 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2038 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2040 struct mdt_device *mdt = m->private;
2041 struct coordinator *cdt = &mdt->mdt_coordinator;
2043 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2046 static inline enum hsm_copytool_action
2047 hsm_copytool_name2action(const char *name)
2049 if (strcasecmp(name, "NOOP") == 0)
2051 else if (strcasecmp(name, "ARCHIVE") == 0)
2052 return HSMA_ARCHIVE;
2053 else if (strcasecmp(name, "RESTORE") == 0)
2054 return HSMA_RESTORE;
2055 else if (strcasecmp(name, "REMOVE") == 0)
2057 else if (strcasecmp(name, "CANCEL") == 0)
2064 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2065 size_t user_count, __u64 *mask)
2067 char *buf, *pos, *name;
2073 if (!(user_count < 4096))
2076 buf_size = user_count + 1;
2078 OBD_ALLOC(buf, buf_size);
2082 if (copy_from_user(buf, user_buf, buf_size - 1))
2083 GOTO(out, rc = -EFAULT);
2085 buf[buf_size - 1] = '\0';
2088 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2094 action = hsm_copytool_name2action(name);
2096 GOTO(out, rc = -EINVAL);
2098 new_mask |= (1UL << action);
2104 OBD_FREE(buf, buf_size);
2110 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2111 size_t count, loff_t *off)
2113 struct seq_file *m = file->private_data;
2114 struct mdt_device *mdt = m->private;
2115 struct coordinator *cdt = &mdt->mdt_coordinator;
2117 return mdt_write_hsm_request_mask(file, buf, count,
2118 &cdt->cdt_user_request_mask);
2122 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2123 size_t count, loff_t *off)
2125 struct seq_file *m = file->private_data;
2126 struct mdt_device *mdt = m->private;
2127 struct coordinator *cdt = &mdt->mdt_coordinator;
2129 return mdt_write_hsm_request_mask(file, buf, count,
2130 &cdt->cdt_group_request_mask);
2134 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2135 size_t count, loff_t *off)
2137 struct seq_file *m = file->private_data;
2138 struct mdt_device *mdt = m->private;
2139 struct coordinator *cdt = &mdt->mdt_coordinator;
2141 return mdt_write_hsm_request_mask(file, buf, count,
2142 &cdt->cdt_other_request_mask);
2145 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2146 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2147 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2148 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2149 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2150 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2151 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2152 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2154 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2156 .fops = &mdt_hsm_agent_fops },
2157 { .name = "actions",
2158 .fops = &mdt_hsm_actions_fops,
2159 .proc_mode = 0444 },
2160 { .name = "default_archive_id",
2161 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2162 { .name = "grace_delay",
2163 .fops = &mdt_hsm_cdt_grace_delay_fops },
2164 { .name = "loop_period",
2165 .fops = &mdt_hsm_cdt_loop_period_fops },
2166 { .name = "max_requests",
2167 .fops = &mdt_hsm_cdt_max_requests_fops },
2169 .fops = &mdt_hsm_policy_fops },
2170 { .name = "active_request_timeout",
2171 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2172 { .name = "active_requests",
2173 .fops = &mdt_hsm_active_requests_fops },
2174 { .name = "user_request_mask",
2175 .fops = &mdt_hsm_user_request_mask_fops, },
2176 { .name = "group_request_mask",
2177 .fops = &mdt_hsm_group_request_mask_fops, },
2178 { .name = "other_request_mask",
2179 .fops = &mdt_hsm_other_request_mask_fops, },