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 = hsd.request[i].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)
531 /* found a request, we start it */
532 rc = mdt_hsm_agent_send(mti, hal, 0);
533 /* if failure, we suppose it is temporary
534 * if the copy tool failed to do the request
535 * it has to use hsm_progress
537 status = (rc ? ARS_WAITING : ARS_STARTED);
539 /* set up cookie vector to set records status
540 * after copy tools start or failed
542 sz = hal->hal_count * sizeof(__u64);
543 OBD_ALLOC(cookies, sz);
544 if (cookies == NULL) {
545 CERROR("%s: Cannot allocate memory (%d o) "
546 "for cookies vector "LPX64"\n",
547 mdt_obd_name(mdt), sz,
548 hal->hal_compound_id);
549 kuc_free(hal, hsd.request[i].hal_used_sz);
552 hai = hai_first(hal);
553 for (j = 0; j < hal->hal_count; j++) {
554 cookies[j] = hai->hai_cookie;
558 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
559 hal->hal_count, status);
561 CERROR("%s: mdt_agent_record_update() failed, "
562 "rc=%d, cannot update status to %s "
564 mdt_obd_name(mdt), rc,
565 agent_req_status2name(status),
568 OBD_FREE(cookies, sz);
571 /* free hal allocated by callback */
572 for (i = 0; i < hsd.max_requests; i++) {
573 if (hsd.request[i].hal) {
574 OBD_FREE(hsd.request[i].hal,
575 hsd.request[i].hal_sz);
576 hsd.request[i].hal_sz = 0;
577 hsd.request[i].hal = NULL;
581 LASSERT(hsd.request_cnt == 0);
583 /* reset callback data */
584 memset(hsd.request, 0, hsd.request_sz);
589 OBD_FREE(hsd.request, hsd.request_sz);
591 if (cdt->cdt_state == CDT_STOPPING) {
592 /* request comes from /proc path, so we need to clean cdt
594 mdt_hsm_cdt_stop(mdt);
595 mdt->mdt_opts.mo_coordinator = 0;
597 /* request comes from a thread event, generated
598 * by mdt_stop_coordinator(), we have to ack
599 * and cdt cleaning will be done by event sender
601 cdt->cdt_thread.t_flags = SVC_STOPPED;
602 wake_up(&cdt->cdt_thread.t_ctl_waitq);
606 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
607 mdt_obd_name(mdt), current_pid(), rc);
609 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
611 mdt_obd_name(mdt), current_pid());
617 * lookup a restore handle by FID
618 * caller needs to hold cdt_restore_lock
619 * \param cdt [IN] coordinator
620 * \param fid [IN] FID
621 * \retval cdt_restore_handle found
622 * \retval NULL not found
624 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
625 const struct lu_fid *fid)
627 struct cdt_restore_handle *crh;
630 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
631 if (lu_fid_eq(&crh->crh_fid, fid))
638 * data passed to llog_cat_process() callback
639 * to scan requests and take actions
641 struct hsm_restore_data {
642 struct mdt_thread_info *hrd_mti;
646 * llog_cat_process() callback, used to:
647 * - find restore request and allocate the restore handle
648 * \param env [IN] environment
649 * \param llh [IN] llog handle
650 * \param hdr [IN] llog record
651 * \param data [IN/OUT] cb data = struct hsm_restore_data
653 * \retval -ve failure
655 static int hsm_restore_cb(const struct lu_env *env,
656 struct llog_handle *llh,
657 struct llog_rec_hdr *hdr, void *data)
659 struct llog_agent_req_rec *larr;
660 struct hsm_restore_data *hrd;
661 struct cdt_restore_handle *crh;
662 struct hsm_action_item *hai;
663 struct mdt_thread_info *mti;
664 struct coordinator *cdt;
665 struct mdt_object *child;
671 cdt = &mti->mti_mdt->mdt_coordinator;
673 larr = (struct llog_agent_req_rec *)hdr;
674 hai = &larr->arr_hai;
675 if (hai->hai_cookie > cdt->cdt_last_cookie)
676 /* update the cookie to avoid collision */
677 cdt->cdt_last_cookie = hai->hai_cookie + 1;
679 if (hai->hai_action != HSMA_RESTORE ||
680 agent_req_in_final_state(larr->arr_status))
683 /* restore request not in a final state */
685 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
689 crh->crh_fid = hai->hai_fid;
690 /* in V1 all file is restored
691 crh->extent.start = hai->hai_extent.offset;
692 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
694 crh->crh_extent.start = 0;
695 crh->crh_extent.end = hai->hai_extent.length;
696 /* get the layout lock */
697 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
698 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
699 MDS_INODELOCK_LAYOUT);
701 GOTO(out, rc = PTR_ERR(child));
704 /* we choose to not keep a reference
705 * on the object during the restore time which can be very long */
706 mdt_object_put(mti->mti_env, child);
708 mutex_lock(&cdt->cdt_restore_lock);
709 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
710 mutex_unlock(&cdt->cdt_restore_lock);
717 * restore coordinator state at startup
718 * the goal is to take a layout lock for each registered restore request
719 * \param mti [IN] context
721 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
723 struct hsm_restore_data hrd;
729 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
730 hsm_restore_cb, &hrd);
735 static int hsm_init_ucred(struct lu_ucred *uc)
739 uc->uc_valid = UCRED_OLD;
748 uc->uc_suppgids[0] = -1;
749 uc->uc_suppgids[1] = -1;
750 uc->uc_cap = CFS_CAP_FS_MASK;
753 uc->uc_identity = NULL;
759 * wake up coordinator thread
760 * \param mdt [IN] device
762 * \retval -ve failure
764 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
766 struct coordinator *cdt = &mdt->mdt_coordinator;
769 if (cdt->cdt_state == CDT_STOPPED)
772 /* wake up coordinator */
773 cdt->cdt_thread.t_flags = SVC_EVENT;
774 wake_up(&cdt->cdt_thread.t_ctl_waitq);
780 * initialize coordinator struct
781 * \param mdt [IN] device
783 * \retval -ve failure
785 int mdt_hsm_cdt_init(struct mdt_device *mdt)
787 struct coordinator *cdt = &mdt->mdt_coordinator;
788 struct mdt_thread_info *cdt_mti = NULL;
792 cdt->cdt_state = CDT_STOPPED;
794 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
795 mutex_init(&cdt->cdt_llog_lock);
796 init_rwsem(&cdt->cdt_agent_lock);
797 init_rwsem(&cdt->cdt_request_lock);
798 mutex_init(&cdt->cdt_restore_lock);
800 INIT_LIST_HEAD(&cdt->cdt_requests);
801 INIT_LIST_HEAD(&cdt->cdt_agents);
802 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
804 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
808 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
809 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
811 lu_context_enter(&cdt->cdt_session);
812 cdt->cdt_env.le_ses = &cdt->cdt_session;
814 lu_env_fini(&cdt->cdt_env);
818 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
819 LASSERT(cdt_mti != NULL);
821 cdt_mti->mti_env = &cdt->cdt_env;
822 cdt_mti->mti_mdt = mdt;
824 hsm_init_ucred(mdt_ucred(cdt_mti));
826 /* default values for /proc tunnables
827 * can be override by MGS conf */
828 cdt->cdt_default_archive_id = 1;
829 cdt->cdt_grace_delay = 60;
830 cdt->cdt_loop_period = 10;
831 cdt->cdt_max_requests = 3;
832 cdt->cdt_policy = CDT_DEFAULT_POLICY;
833 cdt->cdt_active_req_timeout = 3600;
839 * free a coordinator thread
840 * \param mdt [IN] device
842 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
844 struct coordinator *cdt = &mdt->mdt_coordinator;
847 lu_context_exit(cdt->cdt_env.le_ses);
848 lu_context_fini(cdt->cdt_env.le_ses);
850 lu_env_fini(&cdt->cdt_env);
856 * start a coordinator thread
857 * \param mdt [IN] device
859 * \retval -ve failure
861 int mdt_hsm_cdt_start(struct mdt_device *mdt)
863 struct coordinator *cdt = &mdt->mdt_coordinator;
866 struct mdt_thread_info *cdt_mti;
867 struct task_struct *task;
870 /* functions defined but not yet used
871 * this avoid compilation warning
875 if (cdt->cdt_state != CDT_STOPPED) {
876 CERROR("%s: Coordinator already started\n",
881 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
882 cdt->cdt_policy = CDT_DEFAULT_POLICY;
884 cdt->cdt_state = CDT_INIT;
886 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
887 /* just need to be larger than previous one */
888 /* cdt_last_cookie is protected by cdt_llog_lock */
889 cdt->cdt_last_cookie = cfs_time_current_sec();
890 atomic_set(&cdt->cdt_request_count, 0);
891 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
892 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
893 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
895 /* to avoid deadlock when start is made through /proc
896 * /proc entries are created by the coordinator thread */
898 /* set up list of started restore requests */
899 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
900 rc = mdt_hsm_pending_restore(cdt_mti);
902 CERROR("%s: cannot take the layout locks needed"
903 " for registered restore: %d\n",
904 mdt_obd_name(mdt), rc);
906 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
909 cdt->cdt_state = CDT_STOPPED;
910 CERROR("%s: error starting coordinator thread: %d\n",
911 mdt_obd_name(mdt), rc);
914 CDEBUG(D_HSM, "%s: coordinator thread started\n",
919 wait_event(cdt->cdt_thread.t_ctl_waitq,
920 (cdt->cdt_thread.t_flags & SVC_RUNNING));
922 cdt->cdt_state = CDT_RUNNING;
923 mdt->mdt_opts.mo_coordinator = 1;
928 * stop a coordinator thread
929 * \param mdt [IN] device
931 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
933 struct coordinator *cdt = &mdt->mdt_coordinator;
934 struct cdt_agent_req *car, *tmp1;
935 struct hsm_agent *ha, *tmp2;
936 struct cdt_restore_handle *crh, *tmp3;
937 struct mdt_thread_info *cdt_mti;
940 if (cdt->cdt_state == CDT_STOPPED) {
941 CERROR("%s: Coordinator already stopped\n",
946 if (cdt->cdt_state != CDT_STOPPING) {
947 /* stop coordinator thread before cleaning */
948 cdt->cdt_thread.t_flags = SVC_STOPPING;
949 wake_up(&cdt->cdt_thread.t_ctl_waitq);
950 wait_event(cdt->cdt_thread.t_ctl_waitq,
951 cdt->cdt_thread.t_flags & SVC_STOPPED);
953 cdt->cdt_state = CDT_STOPPED;
956 down_write(&cdt->cdt_request_lock);
957 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
959 list_del(&car->car_request_list);
960 mdt_cdt_free_request(car);
962 up_write(&cdt->cdt_request_lock);
964 down_write(&cdt->cdt_agent_lock);
965 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
966 list_del(&ha->ha_list);
969 up_write(&cdt->cdt_agent_lock);
971 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
972 mutex_lock(&cdt->cdt_restore_lock);
973 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
974 struct mdt_object *child;
976 /* give back layout lock */
977 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
979 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
981 list_del(&crh->crh_list);
983 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
985 mutex_unlock(&cdt->cdt_restore_lock);
987 mdt->mdt_opts.mo_coordinator = 0;
993 * register all requests from an hal in the memory list
994 * \param mti [IN] context
995 * \param hal [IN] request
996 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
997 * which is running the CT
999 * \retval -ve failure
1001 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1002 struct hsm_action_list *hal, struct obd_uuid *uuid)
1004 struct mdt_device *mdt = mti->mti_mdt;
1005 struct coordinator *cdt = &mdt->mdt_coordinator;
1006 struct hsm_action_item *hai;
1010 /* register request in memory list */
1011 hai = hai_first(hal);
1012 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1013 struct cdt_agent_req *car;
1015 /* in case of a cancel request, we first mark the ondisk
1016 * record of the request we want to stop as canceled
1017 * this does not change the cancel record
1018 * it will be done when updating the request status
1020 if (hai->hai_action == HSMA_CANCEL) {
1021 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1025 CERROR("%s: mdt_agent_record_update() failed, "
1026 "rc=%d, cannot update status to %s "
1027 "for cookie "LPX64"\n",
1028 mdt_obd_name(mdt), rc,
1029 agent_req_status2name(ARS_CANCELED),
1034 /* find the running request to set it canceled */
1035 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1037 car->car_canceled = 1;
1038 /* uuid has to be changed to the one running the
1039 * request to cancel */
1040 *uuid = car->car_uuid;
1041 mdt_cdt_put_request(car);
1043 /* no need to memorize cancel request
1044 * this also avoid a deadlock when we receive
1045 * a purge all requests command
1050 if (hai->hai_action == HSMA_ARCHIVE) {
1051 struct mdt_object *obj;
1054 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1055 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1058 GOTO(out, rc = PTR_ERR(obj));
1060 hsm.mh_flags |= HS_EXISTS;
1061 hsm.mh_arch_id = hal->hal_archive_id;
1062 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1063 mdt_object_put(mti->mti_env, obj);
1068 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1069 hal->hal_archive_id, hal->hal_flags,
1072 GOTO(out, rc = PTR_ERR(car));
1074 rc = mdt_cdt_add_request(cdt, car);
1076 mdt_cdt_free_request(car);
1083 * swap layouts between 2 fids
1084 * \param mti [IN] context
1087 * \param mh_common [IN] MD HSM
1089 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1090 const lustre_fid *fid, const lustre_fid *dfid,
1091 struct md_hsm *mh_common)
1093 struct mdt_device *mdt = mti->mti_mdt;
1094 struct mdt_object *child1, *child2;
1095 struct mdt_lock_handle *lh2;
1099 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1101 GOTO(out, rc = PTR_ERR(child1));
1103 /* we already have layout lock on FID so take only
1105 lh2 = &mti->mti_lh[MDT_LH_OLD];
1106 mdt_lock_reg_init(lh2, LCK_EX);
1107 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1109 GOTO(out_child1, rc = PTR_ERR(child2));
1111 /* if copy tool closes the volatile before sending the final
1112 * progress through llapi_hsm_copy_end(), all the objects
1113 * are removed and mdd_swap_layout LBUG */
1114 if (!mdt_object_exists(child2)) {
1115 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1116 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1117 GOTO(out_child2, rc = -ENOENT);
1119 /* Since we only handle restores here, unconditionally use
1120 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1121 * be preserved in case of failure during swap_layout and not
1122 * leave a file in an intermediate but incoherent state.
1123 * But need to setup HSM xattr of data FID before, reuse
1124 * mti and mh presets for FID in hsm_cdt_request_completed(),
1125 * only need to clear RELEASED and DIRTY.
1127 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1128 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1130 rc = mo_swap_layouts(mti->mti_env,
1131 mdt_object_child(child1),
1132 mdt_object_child(child2),
1133 SWAP_LAYOUTS_MDS_HSM);
1136 mdt_object_unlock_put(mti, child2, lh2, 1);
1138 mdt_object_put(mti->mti_env, child1);
1144 * update status of a completed request
1145 * \param mti [IN] context
1146 * \param pgs [IN] progress of the copy tool
1147 * \param update_record [IN] update llog record
1149 * \retval -ve failure
1151 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1152 struct hsm_progress_kernel *pgs,
1153 const struct cdt_agent_req *car,
1154 enum agent_req_status *status)
1156 const struct lu_env *env = mti->mti_env;
1157 struct mdt_device *mdt = mti->mti_mdt;
1158 struct coordinator *cdt = &mdt->mdt_coordinator;
1159 struct mdt_object *obj = NULL;
1160 int cl_flags = 0, rc = 0;
1165 /* default is to retry */
1166 *status = ARS_WAITING;
1168 /* find object by FID */
1169 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1170 /* we will update MD HSM only if needed */
1171 is_mh_changed = false;
1173 /* object removed */
1174 *status = ARS_SUCCEED;
1178 /* no need to change mh->mh_arch_id
1179 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1181 if (pgs->hpk_errval != 0) {
1182 switch (pgs->hpk_errval) {
1184 /* the copy tool does not support cancel
1185 * so the cancel request is failed
1186 * As we cannot distinguish a cancel progress
1187 * from another action progress (they have the
1188 * same cookie), we suppose here the CT returns
1189 * ENOSYS only if does not support cancel
1191 /* this can also happen when cdt calls it to
1192 * for a timed out request */
1193 *status = ARS_FAILED;
1194 /* to have a cancel event in changelog */
1195 pgs->hpk_errval = ECANCELED;
1198 /* the request record has already been set to
1199 * ARS_CANCELED, this set the cancel request
1201 *status = ARS_SUCCEED;
1204 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1205 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1206 ARS_FAILED : ARS_WAITING);
1210 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1211 CERROR("%s: Request "LPX64" on "DFID
1212 " failed, error code %d too large\n",
1214 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1216 hsm_set_cl_error(&cl_flags,
1217 CLF_HSM_ERROVERFLOW);
1220 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1223 switch (car->car_hai->hai_action) {
1225 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1228 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1231 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1234 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1235 CERROR("%s: Failed request "LPX64" on "DFID
1236 " cannot be a CANCEL\n",
1239 PFID(&pgs->hpk_fid));
1242 CERROR("%s: Failed request "LPX64" on "DFID
1243 " %d is an unknown action\n",
1245 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1246 car->car_hai->hai_action);
1251 *status = ARS_SUCCEED;
1252 switch (car->car_hai->hai_action) {
1254 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1255 /* set ARCHIVE keep EXIST and clear LOST and
1257 mh.mh_arch_ver = pgs->hpk_data_version;
1258 mh.mh_flags |= HS_ARCHIVED;
1259 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1260 is_mh_changed = true;
1263 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1265 /* do not clear RELEASED and DIRTY here
1266 * this will occur in hsm_swap_layouts()
1269 /* Restoring has changed the file version on
1271 mh.mh_arch_ver = pgs->hpk_data_version;
1272 is_mh_changed = true;
1275 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1276 /* clear ARCHIVED EXISTS and LOST */
1277 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1278 is_mh_changed = true;
1281 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1282 CERROR("%s: Successful request "LPX64
1284 " cannot be a CANCEL\n",
1287 PFID(&pgs->hpk_fid));
1290 CERROR("%s: Successful request "LPX64
1292 " %d is an unknown action\n",
1294 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1295 car->car_hai->hai_action);
1301 /* rc != 0 means error when analysing action, it may come from
1302 * a crasy CT no need to manage DIRTY
1305 hsm_set_cl_flags(&cl_flags,
1306 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1308 /* unlock is done later, after layout lock management */
1310 rc = mdt_hsm_attr_set(mti, obj, &mh);
1313 /* we give back layout lock only if restore was successful or
1314 * if restore was canceled or if policy is to not retry
1315 * in other cases we just unlock the object */
1316 if (car->car_hai->hai_action == HSMA_RESTORE &&
1317 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1318 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1319 struct cdt_restore_handle *crh;
1321 /* restore in data FID done, we swap the layouts
1322 * only if restore is successful */
1323 if (pgs->hpk_errval == 0) {
1324 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1325 &car->car_hai->hai_dfid, &mh);
1327 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1328 *status = ARS_FAILED;
1329 pgs->hpk_errval = -rc;
1332 /* we have to retry, so keep layout lock */
1333 if (*status == ARS_WAITING)
1336 /* give back layout lock */
1337 mutex_lock(&cdt->cdt_restore_lock);
1338 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1340 list_del(&crh->crh_list);
1341 mutex_unlock(&cdt->cdt_restore_lock);
1342 /* just give back layout lock, we keep
1343 * the reference which is given back
1344 * later with the lock for HSM flags */
1345 if (!IS_ERR(obj) && crh != NULL)
1346 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1349 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1355 if (obj != NULL && !IS_ERR(obj)) {
1356 mo_changelog(env, CL_HSM, cl_flags,
1357 mdt_object_child(obj));
1358 mdt_object_put(mti->mti_env, obj);
1365 * update status of a request
1366 * \param mti [IN] context
1367 * \param pgs [IN] progress of the copy tool
1368 * \param update_record [IN] update llog record
1370 * \retval -ve failure
1372 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1373 struct hsm_progress_kernel *pgs,
1374 const int update_record)
1376 struct mdt_device *mdt = mti->mti_mdt;
1377 struct coordinator *cdt = &mdt->mdt_coordinator;
1378 struct cdt_agent_req *car;
1382 /* no coordinator started, so we cannot serve requests */
1383 if (cdt->cdt_state == CDT_STOPPED)
1386 /* first do sanity checks */
1387 car = mdt_cdt_update_request(cdt, pgs);
1389 CERROR("%s: Cannot find running request for cookie "LPX64
1392 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1394 RETURN(PTR_ERR(car));
1397 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1398 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1399 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1400 hsm_copytool_action2name(car->car_hai->hai_action),
1401 pgs->hpk_flags, pgs->hpk_errval,
1402 PFID(&car->car_hai->hai_fid),
1403 PFID(&car->car_hai->hai_dfid));
1405 /* progress is done on FID or data FID depending of the action and
1406 * of the copy progress */
1407 /* for restore progress is used to send back the data FID to cdt */
1408 if (car->car_hai->hai_action == HSMA_RESTORE &&
1409 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1410 car->car_hai->hai_dfid = pgs->hpk_fid;
1412 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1413 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1414 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1415 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1416 CERROR("%s: Progress on "DFID" for cookie "LPX64
1417 " does not match request FID "DFID" nor data FID "
1420 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1421 PFID(&car->car_hai->hai_fid),
1422 PFID(&car->car_hai->hai_dfid));
1423 GOTO(out, rc = -EINVAL);
1426 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1427 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1428 " is not coherent (err=%d and not completed"
1431 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1432 hsm_copytool_action2name(car->car_hai->hai_action),
1433 pgs->hpk_errval, pgs->hpk_flags);
1434 GOTO(out, rc = -EINVAL);
1437 /* now progress is valid */
1439 /* we use a root like ucred */
1440 hsm_init_ucred(mdt_ucred(mti));
1442 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1443 enum agent_req_status status;
1445 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1447 /* remove request from memory list */
1448 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1450 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1451 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1453 hsm_copytool_action2name(car->car_hai->hai_action),
1454 agent_req_status2name(status));
1456 if (update_record) {
1459 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1460 &pgs->hpk_cookie, 1,
1463 CERROR("%s: mdt_agent_record_update() failed,"
1464 " rc=%d, cannot update status to %s"
1465 " for cookie "LPX64"\n",
1466 mdt_obd_name(mdt), rc1,
1467 agent_req_status2name(status),
1469 rc = (rc != 0 ? rc : rc1);
1471 /* ct has completed a request, so a slot is available, wakeup
1472 * cdt to find new work */
1473 mdt_hsm_cdt_wakeup(mdt);
1475 /* if copytool send a progress on a canceled request
1476 * we inform copytool it should stop
1478 if (car->car_canceled == 1)
1484 /* remove ref got from mdt_cdt_update_request() */
1485 mdt_cdt_put_request(car);
1492 * data passed to llog_cat_process() callback
1493 * to cancel requests
1495 struct hsm_cancel_all_data {
1496 struct mdt_device *mdt;
1500 * llog_cat_process() callback, used to:
1501 * - purge all requests
1502 * \param env [IN] environment
1503 * \param llh [IN] llog handle
1504 * \param hdr [IN] llog record
1505 * \param data [IN] cb data = struct hsm_cancel_all_data
1507 * \retval -ve failure
1509 static int mdt_cancel_all_cb(const struct lu_env *env,
1510 struct llog_handle *llh,
1511 struct llog_rec_hdr *hdr, void *data)
1513 struct llog_agent_req_rec *larr;
1514 struct hsm_cancel_all_data *hcad;
1518 larr = (struct llog_agent_req_rec *)hdr;
1520 if (larr->arr_status == ARS_WAITING ||
1521 larr->arr_status == ARS_STARTED) {
1522 larr->arr_status = ARS_CANCELED;
1523 larr->arr_req_change = cfs_time_current_sec();
1524 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1531 * cancel all actions
1532 * \param obd [IN] MDT device
1534 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1536 struct mdt_thread_info *mti;
1537 struct coordinator *cdt = &mdt->mdt_coordinator;
1538 struct cdt_agent_req *car;
1539 struct hsm_action_list *hal = NULL;
1540 struct hsm_action_item *hai;
1541 struct hsm_cancel_all_data hcad;
1542 int hal_sz = 0, hal_len, rc;
1543 enum cdt_states save_state;
1546 /* retrieve coordinator context */
1547 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1549 /* disable coordinator */
1550 save_state = cdt->cdt_state;
1551 cdt->cdt_state = CDT_DISABLE;
1553 /* send cancel to all running requests */
1554 down_read(&cdt->cdt_request_lock);
1555 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1556 mdt_cdt_get_request(car);
1557 /* request is not yet removed from list, it will be done
1558 * when copytool will return progress
1561 if (car->car_hai->hai_action == HSMA_CANCEL) {
1562 mdt_cdt_put_request(car);
1567 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1568 cfs_size_round(car->car_hai->hai_len);
1570 if (hal_len > hal_sz && hal_sz > 0) {
1571 /* not enough room, free old buffer */
1572 OBD_FREE(hal, hal_sz);
1576 /* empty buffer, allocate one */
1579 OBD_ALLOC(hal, hal_sz);
1581 mdt_cdt_put_request(car);
1582 up_read(&cdt->cdt_request_lock);
1583 GOTO(out, rc = -ENOMEM);
1587 hal->hal_version = HAL_VERSION;
1588 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1590 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1591 hal->hal_compound_id = car->car_compound_id;
1592 hal->hal_archive_id = car->car_archive_id;
1593 hal->hal_flags = car->car_flags;
1596 hai = hai_first(hal);
1597 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1598 hai->hai_action = HSMA_CANCEL;
1601 /* it is possible to safely call mdt_hsm_agent_send()
1602 * (ie without a deadlock on cdt_request_lock), because the
1603 * write lock is taken only if we are not in purge mode
1604 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1605 * nor mdt_cdt_remove_request())
1607 /* no conflict with cdt thread because cdt is disable and we
1608 * have the request lock */
1609 mdt_hsm_agent_send(mti, hal, 1);
1611 mdt_cdt_put_request(car);
1613 up_read(&cdt->cdt_request_lock);
1616 OBD_FREE(hal, hal_sz);
1618 /* cancel all on-disk records */
1621 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1622 mdt_cancel_all_cb, &hcad);
1624 /* enable coordinator */
1625 cdt->cdt_state = save_state;
1631 * check if a request is compatible with file status
1632 * \param hai [IN] request description
1633 * \param hal_an [IN] request archive number (not used)
1634 * \param rq_flags [IN] request flags
1635 * \param hsm [IN] file HSM metadata
1638 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1639 const int hal_an, const __u64 rq_flags,
1640 const struct md_hsm *hsm)
1642 int is_compat = false;
1646 hsm_flags = hsm->mh_flags;
1647 switch (hai->hai_action) {
1649 if (!(hsm_flags & HS_NOARCHIVE) &&
1650 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1654 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1655 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1659 if (!(hsm_flags & HS_RELEASED) &&
1660 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1667 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1668 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1669 PFID(&hai->hai_fid),
1670 hsm_copytool_action2name(hai->hai_action), rq_flags,
1671 hai->hai_extent.offset, hai->hai_extent.length,
1673 (is_compat ? "compatible" : "uncompatible"));
1679 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1681 static const struct {
1685 } hsm_policy_names[] = {
1686 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1687 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1692 * convert a policy name to a bit
1693 * \param name [IN] policy name
1695 * \retval policy bit
1697 static __u64 hsm_policy_str2bit(const char *name)
1701 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1702 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1703 strcmp(hsm_policy_names[i].name, name) == 0)
1704 return hsm_policy_names[i].bit;
1709 * convert a policy bit field to a string
1710 * \param mask [IN] policy bit field
1711 * \param hexa [IN] print mask before bit names
1712 * \param buffer [OUT] string
1713 * \param count [IN] size of buffer
1715 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1723 seq_printf(m, "("LPX64") ", mask);
1725 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1728 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1729 if (hsm_policy_names[j].bit == bit)
1733 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1735 seq_printf(m, "%s ", hsm_policy_names[j].name);
1737 /* remove last ' ' */
1742 /* methods to read/write HSM policy flags */
1743 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1745 struct mdt_device *mdt = m->private;
1746 struct coordinator *cdt = &mdt->mdt_coordinator;
1749 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1754 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1755 size_t count, loff_t *off)
1757 struct seq_file *m = file->private_data;
1758 struct mdt_device *mdt = m->private;
1759 struct coordinator *cdt = &mdt->mdt_coordinator;
1760 char *start, *token, sign;
1763 __u64 add_mask, remove_mask, set_mask;
1767 if (count + 1 > PAGE_SIZE)
1770 OBD_ALLOC(buf, count + 1);
1774 if (copy_from_user(buf, buffer, count))
1775 GOTO(out, rc = -EFAULT);
1780 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1783 add_mask = remove_mask = set_mask = 0;
1785 token = strsep(&start, "\n ");
1791 if (sign == '-' || sign == '+')
1794 policy = hsm_policy_str2bit(token);
1796 CWARN("%s: '%s' is unknown, "
1797 "supported policies are:\n", mdt_obd_name(mdt),
1799 hsm_policy_bit2str(m, 0, false);
1800 GOTO(out, rc = -EINVAL);
1804 remove_mask |= policy;
1814 } while (start != NULL);
1816 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1817 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1819 /* if no sign in all string, it is a clear and set
1820 * if some sign found, all unsigned are converted
1822 * P1 P2 = set to P1 and P2
1823 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1825 if (remove_mask == 0 && add_mask == 0) {
1826 cdt->cdt_policy = set_mask;
1828 cdt->cdt_policy |= set_mask | add_mask;
1829 cdt->cdt_policy &= ~remove_mask;
1832 GOTO(out, rc = count);
1835 OBD_FREE(buf, count + 1);
1838 LPROC_SEQ_FOPS(mdt_hsm_policy);
1840 #define GENERATE_PROC_METHOD(VAR) \
1841 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1843 struct mdt_device *mdt = m->private; \
1844 struct coordinator *cdt = &mdt->mdt_coordinator; \
1847 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1851 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1852 size_t count, loff_t *off) \
1855 struct seq_file *m = file->private_data; \
1856 struct mdt_device *mdt = m->private; \
1857 struct coordinator *cdt = &mdt->mdt_coordinator; \
1862 rc = lprocfs_str_to_s64(buffer, count, &val); \
1865 if (val > 0 && val < INT_MAX) { \
1872 GENERATE_PROC_METHOD(cdt_loop_period)
1873 GENERATE_PROC_METHOD(cdt_grace_delay)
1874 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1875 GENERATE_PROC_METHOD(cdt_max_requests)
1876 GENERATE_PROC_METHOD(cdt_default_archive_id)
1879 * procfs write method for MDT/hsm_control
1880 * proc entry is in mdt directory so data is mdt obd_device pointer
1882 #define CDT_ENABLE_CMD "enabled"
1883 #define CDT_STOP_CMD "shutdown"
1884 #define CDT_DISABLE_CMD "disabled"
1885 #define CDT_PURGE_CMD "purge"
1886 #define CDT_HELP_CMD "help"
1887 #define CDT_MAX_CMD_LEN 10
1890 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1891 size_t count, loff_t *off)
1893 struct seq_file *m = file->private_data;
1894 struct obd_device *obd = m->private;
1895 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1896 struct coordinator *cdt = &(mdt->mdt_coordinator);
1898 char kernbuf[CDT_MAX_CMD_LEN];
1901 if (count == 0 || count >= sizeof(kernbuf))
1904 if (copy_from_user(kernbuf, buffer, count))
1908 if (kernbuf[count - 1] == '\n')
1909 kernbuf[count - 1] = 0;
1912 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1913 if (cdt->cdt_state == CDT_DISABLE) {
1914 cdt->cdt_state = CDT_RUNNING;
1915 mdt_hsm_cdt_wakeup(mdt);
1917 rc = mdt_hsm_cdt_start(mdt);
1919 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1920 if ((cdt->cdt_state == CDT_STOPPING) ||
1921 (cdt->cdt_state == CDT_STOPPED)) {
1922 CERROR("%s: Coordinator already stopped\n",
1926 cdt->cdt_state = CDT_STOPPING;
1928 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1929 if ((cdt->cdt_state == CDT_STOPPING) ||
1930 (cdt->cdt_state == CDT_STOPPED)) {
1931 CERROR("%s: Coordinator is stopped\n",
1935 cdt->cdt_state = CDT_DISABLE;
1937 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1938 rc = hsm_cancel_all_actions(mdt);
1939 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1947 CERROR("%s: Valid coordinator control commands are: "
1948 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1949 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1950 CDT_PURGE_CMD, CDT_HELP_CMD);
1958 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1960 struct obd_device *obd = m->private;
1961 struct coordinator *cdt;
1964 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1966 if (cdt->cdt_state == CDT_INIT)
1967 seq_printf(m, "init\n");
1968 else if (cdt->cdt_state == CDT_RUNNING)
1969 seq_printf(m, "enabled\n");
1970 else if (cdt->cdt_state == CDT_STOPPING)
1971 seq_printf(m, "stopping\n");
1972 else if (cdt->cdt_state == CDT_STOPPED)
1973 seq_printf(m, "stopped\n");
1974 else if (cdt->cdt_state == CDT_DISABLE)
1975 seq_printf(m, "disabled\n");
1977 seq_printf(m, "unknown\n");
1983 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
1989 for (i = 0; i < 8 * sizeof(mask); i++) {
1990 if (mask & (1UL << i)) {
1991 seq_printf(m, "%s%s", first ? "" : " ",
1992 hsm_copytool_action2name(i));
2002 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2004 struct mdt_device *mdt = m->private;
2005 struct coordinator *cdt = &mdt->mdt_coordinator;
2007 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2011 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2013 struct mdt_device *mdt = m->private;
2014 struct coordinator *cdt = &mdt->mdt_coordinator;
2016 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2020 mdt_hsm_other_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_other_request_mask);
2028 static inline enum hsm_copytool_action
2029 hsm_copytool_name2action(const char *name)
2031 if (strcasecmp(name, "NOOP") == 0)
2033 else if (strcasecmp(name, "ARCHIVE") == 0)
2034 return HSMA_ARCHIVE;
2035 else if (strcasecmp(name, "RESTORE") == 0)
2036 return HSMA_RESTORE;
2037 else if (strcasecmp(name, "REMOVE") == 0)
2039 else if (strcasecmp(name, "CANCEL") == 0)
2046 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2047 size_t user_count, __u64 *mask)
2049 char *buf, *pos, *name;
2055 if (!(user_count < 4096))
2058 buf_size = user_count + 1;
2060 OBD_ALLOC(buf, buf_size);
2064 if (copy_from_user(buf, user_buf, buf_size - 1))
2065 GOTO(out, rc = -EFAULT);
2067 buf[buf_size - 1] = '\0';
2070 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2076 action = hsm_copytool_name2action(name);
2078 GOTO(out, rc = -EINVAL);
2080 new_mask |= (1UL << action);
2086 OBD_FREE(buf, buf_size);
2092 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2093 size_t count, loff_t *off)
2095 struct seq_file *m = file->private_data;
2096 struct mdt_device *mdt = m->private;
2097 struct coordinator *cdt = &mdt->mdt_coordinator;
2099 return mdt_write_hsm_request_mask(file, buf, count,
2100 &cdt->cdt_user_request_mask);
2104 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2105 size_t count, loff_t *off)
2107 struct seq_file *m = file->private_data;
2108 struct mdt_device *mdt = m->private;
2109 struct coordinator *cdt = &mdt->mdt_coordinator;
2111 return mdt_write_hsm_request_mask(file, buf, count,
2112 &cdt->cdt_group_request_mask);
2116 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2117 size_t count, loff_t *off)
2119 struct seq_file *m = file->private_data;
2120 struct mdt_device *mdt = m->private;
2121 struct coordinator *cdt = &mdt->mdt_coordinator;
2123 return mdt_write_hsm_request_mask(file, buf, count,
2124 &cdt->cdt_other_request_mask);
2127 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2128 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2129 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2130 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2131 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2132 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2133 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2134 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2136 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2138 .fops = &mdt_hsm_agent_fops },
2139 { .name = "actions",
2140 .fops = &mdt_hsm_actions_fops,
2141 .proc_mode = 0444 },
2142 { .name = "default_archive_id",
2143 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2144 { .name = "grace_delay",
2145 .fops = &mdt_hsm_cdt_grace_delay_fops },
2146 { .name = "loop_period",
2147 .fops = &mdt_hsm_cdt_loop_period_fops },
2148 { .name = "max_requests",
2149 .fops = &mdt_hsm_cdt_max_requests_fops },
2151 .fops = &mdt_hsm_policy_fops },
2152 { .name = "active_request_timeout",
2153 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2154 { .name = "active_requests",
2155 .fops = &mdt_hsm_active_requests_fops },
2156 { .name = "user_request_mask",
2157 .fops = &mdt_hsm_user_request_mask_fops, },
2158 { .name = "group_request_mask",
2159 .fops = &mdt_hsm_group_request_mask_fops, },
2160 { .name = "other_request_mask",
2161 .fops = &mdt_hsm_other_request_mask_fops, },