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 max_requests; /** vector size */
138 int request_cnt; /** used count */
142 struct hsm_action_list *hal;
147 * llog_cat_process() callback, used to:
148 * - find waiting request and start action
149 * - purge canceled and done requests
150 * \param env [IN] environment
151 * \param llh [IN] llog handle
152 * \param hdr [IN] llog record
153 * \param data [IN/OUT] cb data = struct hsm_scan_data
155 * \retval -ve failure
157 static int mdt_coordinator_cb(const struct lu_env *env,
158 struct llog_handle *llh,
159 struct llog_rec_hdr *hdr,
162 struct llog_agent_req_rec *larr;
163 struct hsm_scan_data *hsd;
164 struct hsm_action_item *hai;
165 struct mdt_device *mdt;
166 struct coordinator *cdt;
171 mdt = hsd->mti->mti_mdt;
172 cdt = &mdt->mdt_coordinator;
174 larr = (struct llog_agent_req_rec *)hdr;
175 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
176 switch (larr->arr_status) {
178 int i, empty_slot, found;
180 /* Are agents full? */
181 if (atomic_read(&cdt->cdt_request_count) >=
182 cdt->cdt_max_requests)
185 /* first search whether the request is found in the list we
186 * have built and if there is room in the request vector */
189 for (i = 0; i < hsd->max_requests &&
190 (empty_slot == -1 || found == -1); i++) {
191 if (hsd->request[i].hal == NULL) {
195 if (hsd->request[i].hal->hal_compound_id ==
196 larr->arr_compound_id) {
201 if (found == -1 && empty_slot == -1)
202 /* unknown request and no more room for new request,
203 * continue scan for to find other entries for
204 * already found request
209 struct hsm_action_list *hal;
211 /* request is not already known */
212 /* allocates hai vector size just needs to be large
214 hsd->request[empty_slot].hal_sz =
215 sizeof(*hsd->request[empty_slot].hal) +
216 cfs_size_round(MTI_NAME_MAXLEN+1) +
217 2 * cfs_size_round(larr->arr_hai.hai_len);
218 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
220 CERROR("%s: Cannot allocate memory (%d o)"
221 "for compound "LPX64"\n",
223 hsd->request[i].hal_sz,
224 larr->arr_compound_id);
227 hal->hal_version = HAL_VERSION;
228 strlcpy(hal->hal_fsname, hsd->fs_name,
229 MTI_NAME_MAXLEN + 1);
230 hal->hal_compound_id = larr->arr_compound_id;
231 hal->hal_archive_id = larr->arr_archive_id;
232 hal->hal_flags = larr->arr_flags;
234 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
235 hsd->request[empty_slot].hal = hal;
238 hai = hai_first(hal);
240 /* request is known */
241 /* we check if record archive num is the same as the
242 * known request, if not we will serve it in multiple
243 * time because we do not know if the agent can serve
245 * a use case is a compound made of multiple restore
246 * where the files are not archived in the same backend
248 if (larr->arr_archive_id !=
249 hsd->request[found].hal->hal_archive_id)
252 if (hsd->request[found].hal_sz <
253 hsd->request[found].hal_used_sz +
254 cfs_size_round(larr->arr_hai.hai_len)) {
255 /* Not enough room, need an extension */
259 sz = 2 * hsd->request[found].hal_sz;
260 OBD_ALLOC(hal_buffer, sz);
262 CERROR("%s: Cannot allocate memory "
263 "(%d o) for compound "LPX64"\n",
264 mdt_obd_name(mdt), sz,
265 larr->arr_compound_id);
268 memcpy(hal_buffer, hsd->request[found].hal,
269 hsd->request[found].hal_used_sz);
270 OBD_FREE(hsd->request[found].hal,
271 hsd->request[found].hal_sz);
272 hsd->request[found].hal = hal_buffer;
273 hsd->request[found].hal_sz = sz;
275 hai = hai_first(hsd->request[found].hal);
276 for (i = 0; i < hsd->request[found].hal->hal_count;
280 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
281 hai->hai_cookie = larr->arr_hai.hai_cookie;
282 hai->hai_gid = larr->arr_hai.hai_gid;
284 hsd->request[found].hal_used_sz +=
285 cfs_size_round(hai->hai_len);
286 hsd->request[found].hal->hal_count++;
290 struct hsm_progress_kernel pgs;
291 struct cdt_agent_req *car;
292 cfs_time_t now = cfs_time_current_sec();
295 /* we search for a running request
296 * error may happen if coordinator crashes or stopped
297 * with running request
299 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
301 last = larr->arr_req_create;
303 last = car->car_req_update;
304 mdt_cdt_put_request(car);
307 /* test if request too long, if yes cancel it
308 * the same way the copy tool acknowledge a cancel request */
309 if (now <= last + cdt->cdt_active_req_timeout)
312 dump_llog_agent_req_rec("request timed out, start cleaning",
314 /* a too old cancel request just needs to be removed
315 * this can happen, if copy tool does not support
316 * cancel for other requests, we have to remove the
317 * running request and notify the copytool */
318 pgs.hpk_fid = larr->arr_hai.hai_fid;
319 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
320 pgs.hpk_extent = larr->arr_hai.hai_extent;
321 pgs.hpk_flags = HP_FLAG_COMPLETED;
322 pgs.hpk_errval = ENOSYS;
323 pgs.hpk_data_version = 0;
325 /* update request state, but do not record in llog, to
326 * avoid deadlock on cdt_llog_lock */
327 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
329 CERROR("%s: cannot cleanup timed out request: "
330 DFID" for cookie "LPX64" action=%s\n",
332 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
333 hsm_copytool_action2name(
334 larr->arr_hai.hai_action));
337 /* The request no longer exists, forget
338 * about it, and do not send a cancel request
339 * to the client, for which an error will be
340 * sent back, leading to an endless cycle of
342 RETURN(LLOG_DEL_RECORD);
345 /* XXX A cancel request cannot be cancelled. */
346 if (larr->arr_hai.hai_action == HSMA_CANCEL)
349 larr->arr_status = ARS_CANCELED;
350 larr->arr_req_change = now;
351 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
353 CERROR("%s: cannot update agent log: rc = %d\n",
354 mdt_obd_name(mdt), rc);
360 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
361 cfs_time_current_sec())
362 RETURN(LLOG_DEL_RECORD);
369 * create /proc entries for coordinator
372 * \retval -ve failure
374 int hsm_cdt_procfs_init(struct mdt_device *mdt)
376 struct coordinator *cdt = &mdt->mdt_coordinator;
380 /* init /proc entries, failure is not critical */
381 cdt->cdt_proc_dir = lprocfs_register("hsm",
382 mdt2obd_dev(mdt)->obd_proc_entry,
383 lprocfs_mdt_hsm_vars, mdt);
384 if (IS_ERR(cdt->cdt_proc_dir)) {
385 rc = PTR_ERR(cdt->cdt_proc_dir);
386 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
387 " rc=%d\n", mdt_obd_name(mdt), rc);
388 cdt->cdt_proc_dir = NULL;
396 * remove /proc entries for coordinator
399 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
401 struct coordinator *cdt = &mdt->mdt_coordinator;
403 LASSERT(cdt->cdt_state == CDT_STOPPED);
404 if (cdt->cdt_proc_dir != NULL)
405 lprocfs_remove(&cdt->cdt_proc_dir);
409 * get vector of hsm cdt /proc vars
413 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
415 return lprocfs_mdt_hsm_vars;
420 * \param data [IN] obd device
422 * \retval -ve failure
424 static int mdt_coordinator(void *data)
426 struct mdt_thread_info *mti = data;
427 struct mdt_device *mdt = mti->mti_mdt;
428 struct coordinator *cdt = &mdt->mdt_coordinator;
429 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 request_sz = hsd.max_requests * sizeof(*hsd.request);
446 OBD_ALLOC(hsd.request, 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, request_sz);
490 hsd.max_requests = cdt->cdt_max_requests;
491 request_sz = hsd.max_requests * sizeof(*hsd.request);
492 OBD_ALLOC(hsd.request, request_sz);
501 rc = cdt_llog_process(mti->mti_env, mdt,
502 mdt_coordinator_cb, &hsd);
506 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
508 if (list_empty(&cdt->cdt_agents)) {
509 CDEBUG(D_HSM, "no agent available, "
510 "coordinator sleeps\n");
514 /* here hsd contains a list of requests to be started */
515 for (i = 0; i < hsd.max_requests; i++) {
516 struct hsm_action_list *hal = hsd.request[i].hal;
517 struct hsm_action_item *hai;
520 enum agent_req_status status;
522 /* still room for work ? */
523 if (atomic_read(&cdt->cdt_request_count) >=
524 cdt->cdt_max_requests)
530 /* found a request, we start it */
531 rc = mdt_hsm_agent_send(mti, hal, 0);
532 /* if failure, we suppose it is temporary
533 * if the copy tool failed to do the request
534 * it has to use hsm_progress
536 status = (rc ? ARS_WAITING : ARS_STARTED);
538 /* set up cookie vector to set records status
539 * after copy tools start or failed
541 sz = hal->hal_count * sizeof(__u64);
542 OBD_ALLOC(cookies, sz);
543 if (cookies == NULL) {
544 CERROR("%s: Cannot allocate memory (%d o) "
545 "for cookies vector "LPX64"\n",
546 mdt_obd_name(mdt), sz,
547 hal->hal_compound_id);
550 hai = hai_first(hal);
551 for (j = 0; j < hal->hal_count; j++) {
552 cookies[j] = hai->hai_cookie;
556 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
557 hal->hal_count, status);
559 CERROR("%s: mdt_agent_record_update() failed, "
560 "rc=%d, cannot update status to %s "
562 mdt_obd_name(mdt), rc,
563 agent_req_status2name(status),
566 OBD_FREE(cookies, sz);
569 /* free hal allocated by callback */
570 for (i = 0; i < hsd.max_requests; i++) {
571 if (hsd.request[i].hal) {
572 OBD_FREE(hsd.request[i].hal,
573 hsd.request[i].hal_sz);
574 hsd.request[i].hal_sz = 0;
575 hsd.request[i].hal = NULL;
579 LASSERT(hsd.request_cnt == 0);
581 /* reset callback data */
582 memset(hsd.request, 0, request_sz);
587 OBD_FREE(hsd.request, request_sz);
589 if (cdt->cdt_state == CDT_STOPPING) {
590 /* request comes from /proc path, so we need to clean cdt
592 mdt_hsm_cdt_stop(mdt);
593 mdt->mdt_opts.mo_coordinator = 0;
595 /* request comes from a thread event, generated
596 * by mdt_stop_coordinator(), we have to ack
597 * and cdt cleaning will be done by event sender
599 cdt->cdt_thread.t_flags = SVC_STOPPED;
600 wake_up(&cdt->cdt_thread.t_ctl_waitq);
604 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
605 mdt_obd_name(mdt), current_pid(), rc);
607 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
609 mdt_obd_name(mdt), current_pid());
615 * lookup a restore handle by FID
616 * caller needs to hold cdt_restore_lock
617 * \param cdt [IN] coordinator
618 * \param fid [IN] FID
619 * \retval cdt_restore_handle found
620 * \retval NULL not found
622 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
623 const struct lu_fid *fid)
625 struct cdt_restore_handle *crh;
628 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
629 if (lu_fid_eq(&crh->crh_fid, fid))
636 * data passed to llog_cat_process() callback
637 * to scan requests and take actions
639 struct hsm_restore_data {
640 struct mdt_thread_info *hrd_mti;
644 * llog_cat_process() callback, used to:
645 * - find restore request and allocate the restore handle
646 * \param env [IN] environment
647 * \param llh [IN] llog handle
648 * \param hdr [IN] llog record
649 * \param data [IN/OUT] cb data = struct hsm_restore_data
651 * \retval -ve failure
653 static int hsm_restore_cb(const struct lu_env *env,
654 struct llog_handle *llh,
655 struct llog_rec_hdr *hdr, void *data)
657 struct llog_agent_req_rec *larr;
658 struct hsm_restore_data *hrd;
659 struct cdt_restore_handle *crh;
660 struct hsm_action_item *hai;
661 struct mdt_thread_info *mti;
662 struct coordinator *cdt;
663 struct mdt_object *child;
669 cdt = &mti->mti_mdt->mdt_coordinator;
671 larr = (struct llog_agent_req_rec *)hdr;
672 hai = &larr->arr_hai;
673 if (hai->hai_cookie > cdt->cdt_last_cookie)
674 /* update the cookie to avoid collision */
675 cdt->cdt_last_cookie = hai->hai_cookie + 1;
677 if (hai->hai_action != HSMA_RESTORE ||
678 agent_req_in_final_state(larr->arr_status))
681 /* restore request not in a final state */
683 /* force replay of restore requests left in started state from previous
684 * CDT context, to be canceled later if finally found to be incompatible
685 * when being re-started */
686 if (larr->arr_status == ARS_STARTED) {
687 larr->arr_status = ARS_WAITING;
688 larr->arr_req_change = cfs_time_current_sec();
689 rc = llog_write(env, llh, hdr, hdr->lrh_index);
694 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
698 crh->crh_fid = hai->hai_fid;
699 /* in V1 all file is restored
700 crh->extent.start = hai->hai_extent.offset;
701 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
703 crh->crh_extent.start = 0;
704 crh->crh_extent.end = hai->hai_extent.length;
705 /* get the layout lock */
706 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
707 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
708 MDS_INODELOCK_LAYOUT);
710 GOTO(out, rc = PTR_ERR(child));
713 /* we choose to not keep a reference
714 * on the object during the restore time which can be very long */
715 mdt_object_put(mti->mti_env, child);
717 mutex_lock(&cdt->cdt_restore_lock);
718 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
719 mutex_unlock(&cdt->cdt_restore_lock);
726 * restore coordinator state at startup
727 * the goal is to take a layout lock for each registered restore request
728 * \param mti [IN] context
730 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
732 struct hsm_restore_data hrd;
738 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
739 hsm_restore_cb, &hrd);
744 static int hsm_init_ucred(struct lu_ucred *uc)
748 uc->uc_valid = UCRED_OLD;
757 uc->uc_suppgids[0] = -1;
758 uc->uc_suppgids[1] = -1;
759 uc->uc_cap = CFS_CAP_FS_MASK;
762 uc->uc_identity = NULL;
768 * wake up coordinator thread
769 * \param mdt [IN] device
771 * \retval -ve failure
773 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
775 struct coordinator *cdt = &mdt->mdt_coordinator;
778 if (cdt->cdt_state == CDT_STOPPED)
781 /* wake up coordinator */
782 cdt->cdt_thread.t_flags = SVC_EVENT;
783 wake_up(&cdt->cdt_thread.t_ctl_waitq);
789 * initialize coordinator struct
790 * \param mdt [IN] device
792 * \retval -ve failure
794 int mdt_hsm_cdt_init(struct mdt_device *mdt)
796 struct coordinator *cdt = &mdt->mdt_coordinator;
797 struct mdt_thread_info *cdt_mti = NULL;
801 cdt->cdt_state = CDT_STOPPED;
803 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
804 mutex_init(&cdt->cdt_llog_lock);
805 init_rwsem(&cdt->cdt_agent_lock);
806 init_rwsem(&cdt->cdt_request_lock);
807 mutex_init(&cdt->cdt_restore_lock);
809 INIT_LIST_HEAD(&cdt->cdt_requests);
810 INIT_LIST_HEAD(&cdt->cdt_agents);
811 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
813 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
817 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
818 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
820 lu_context_enter(&cdt->cdt_session);
821 cdt->cdt_env.le_ses = &cdt->cdt_session;
823 lu_env_fini(&cdt->cdt_env);
827 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
828 LASSERT(cdt_mti != NULL);
830 cdt_mti->mti_env = &cdt->cdt_env;
831 cdt_mti->mti_mdt = mdt;
833 hsm_init_ucred(mdt_ucred(cdt_mti));
835 /* default values for /proc tunnables
836 * can be override by MGS conf */
837 cdt->cdt_default_archive_id = 1;
838 cdt->cdt_grace_delay = 60;
839 cdt->cdt_loop_period = 10;
840 cdt->cdt_max_requests = 3;
841 cdt->cdt_policy = CDT_DEFAULT_POLICY;
842 cdt->cdt_active_req_timeout = 3600;
848 * free a coordinator thread
849 * \param mdt [IN] device
851 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
853 struct coordinator *cdt = &mdt->mdt_coordinator;
856 lu_context_exit(cdt->cdt_env.le_ses);
857 lu_context_fini(cdt->cdt_env.le_ses);
859 lu_env_fini(&cdt->cdt_env);
865 * start a coordinator thread
866 * \param mdt [IN] device
868 * \retval -ve failure
870 int mdt_hsm_cdt_start(struct mdt_device *mdt)
872 struct coordinator *cdt = &mdt->mdt_coordinator;
875 struct mdt_thread_info *cdt_mti;
876 struct task_struct *task;
879 /* functions defined but not yet used
880 * this avoid compilation warning
884 if (cdt->cdt_state != CDT_STOPPED) {
885 CERROR("%s: Coordinator already started\n",
890 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
891 cdt->cdt_policy = CDT_DEFAULT_POLICY;
893 cdt->cdt_state = CDT_INIT;
895 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
896 /* just need to be larger than previous one */
897 /* cdt_last_cookie is protected by cdt_llog_lock */
898 cdt->cdt_last_cookie = cfs_time_current_sec();
899 atomic_set(&cdt->cdt_request_count, 0);
900 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
901 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
902 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
904 /* to avoid deadlock when start is made through /proc
905 * /proc entries are created by the coordinator thread */
907 /* set up list of started restore requests */
908 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
909 rc = mdt_hsm_pending_restore(cdt_mti);
911 CERROR("%s: cannot take the layout locks needed"
912 " for registered restore: %d\n",
913 mdt_obd_name(mdt), rc);
915 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
918 cdt->cdt_state = CDT_STOPPED;
919 CERROR("%s: error starting coordinator thread: %d\n",
920 mdt_obd_name(mdt), rc);
923 CDEBUG(D_HSM, "%s: coordinator thread started\n",
928 wait_event(cdt->cdt_thread.t_ctl_waitq,
929 (cdt->cdt_thread.t_flags & SVC_RUNNING));
931 cdt->cdt_state = CDT_RUNNING;
932 mdt->mdt_opts.mo_coordinator = 1;
937 * stop a coordinator thread
938 * \param mdt [IN] device
940 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
942 struct coordinator *cdt = &mdt->mdt_coordinator;
943 struct cdt_agent_req *car, *tmp1;
944 struct hsm_agent *ha, *tmp2;
945 struct cdt_restore_handle *crh, *tmp3;
946 struct mdt_thread_info *cdt_mti;
949 if (cdt->cdt_state == CDT_STOPPED) {
950 CERROR("%s: Coordinator already stopped\n",
955 if (cdt->cdt_state != CDT_STOPPING) {
956 /* stop coordinator thread before cleaning */
957 cdt->cdt_thread.t_flags = SVC_STOPPING;
958 wake_up(&cdt->cdt_thread.t_ctl_waitq);
959 wait_event(cdt->cdt_thread.t_ctl_waitq,
960 cdt->cdt_thread.t_flags & SVC_STOPPED);
962 cdt->cdt_state = CDT_STOPPED;
965 down_write(&cdt->cdt_request_lock);
966 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
968 list_del(&car->car_request_list);
969 mdt_cdt_free_request(car);
971 up_write(&cdt->cdt_request_lock);
973 down_write(&cdt->cdt_agent_lock);
974 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
975 list_del(&ha->ha_list);
978 up_write(&cdt->cdt_agent_lock);
980 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
981 mutex_lock(&cdt->cdt_restore_lock);
982 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
983 struct mdt_object *child;
985 /* give back layout lock */
986 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
988 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
990 list_del(&crh->crh_list);
992 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
994 mutex_unlock(&cdt->cdt_restore_lock);
996 mdt->mdt_opts.mo_coordinator = 0;
1002 * register all requests from an hal in the memory list
1003 * \param mti [IN] context
1004 * \param hal [IN] request
1005 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1006 * which is running the CT
1008 * \retval -ve failure
1010 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1011 struct hsm_action_list *hal, struct obd_uuid *uuid)
1013 struct mdt_device *mdt = mti->mti_mdt;
1014 struct coordinator *cdt = &mdt->mdt_coordinator;
1015 struct hsm_action_item *hai;
1019 /* register request in memory list */
1020 hai = hai_first(hal);
1021 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1022 struct cdt_agent_req *car;
1024 /* in case of a cancel request, we first mark the ondisk
1025 * record of the request we want to stop as canceled
1026 * this does not change the cancel record
1027 * it will be done when updating the request status
1029 if (hai->hai_action == HSMA_CANCEL) {
1030 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1034 CERROR("%s: mdt_agent_record_update() failed, "
1035 "rc=%d, cannot update status to %s "
1036 "for cookie "LPX64"\n",
1037 mdt_obd_name(mdt), rc,
1038 agent_req_status2name(ARS_CANCELED),
1043 /* find the running request to set it canceled */
1044 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1046 car->car_canceled = 1;
1047 /* uuid has to be changed to the one running the
1048 * request to cancel */
1049 *uuid = car->car_uuid;
1050 mdt_cdt_put_request(car);
1052 /* no need to memorize cancel request
1053 * this also avoid a deadlock when we receive
1054 * a purge all requests command
1059 if (hai->hai_action == HSMA_ARCHIVE) {
1060 struct mdt_object *obj;
1063 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1064 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1067 GOTO(out, rc = PTR_ERR(obj));
1069 hsm.mh_flags |= HS_EXISTS;
1070 hsm.mh_arch_id = hal->hal_archive_id;
1071 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1072 mdt_object_put(mti->mti_env, obj);
1077 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1078 hal->hal_archive_id, hal->hal_flags,
1081 GOTO(out, rc = PTR_ERR(car));
1083 rc = mdt_cdt_add_request(cdt, car);
1085 mdt_cdt_free_request(car);
1092 * swap layouts between 2 fids
1093 * \param mti [IN] context
1096 * \param mh_common [IN] MD HSM
1098 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1099 const lustre_fid *fid, const lustre_fid *dfid,
1100 struct md_hsm *mh_common)
1102 struct mdt_device *mdt = mti->mti_mdt;
1103 struct mdt_object *child1, *child2;
1104 struct mdt_lock_handle *lh2;
1108 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1110 GOTO(out, rc = PTR_ERR(child1));
1112 /* we already have layout lock on FID so take only
1114 lh2 = &mti->mti_lh[MDT_LH_OLD];
1115 mdt_lock_reg_init(lh2, LCK_EX);
1116 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1118 GOTO(out_child1, rc = PTR_ERR(child2));
1120 /* if copy tool closes the volatile before sending the final
1121 * progress through llapi_hsm_copy_end(), all the objects
1122 * are removed and mdd_swap_layout LBUG */
1123 if (!mdt_object_exists(child2)) {
1124 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1125 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1126 GOTO(out_child2, rc = -ENOENT);
1128 /* Since we only handle restores here, unconditionally use
1129 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1130 * be preserved in case of failure during swap_layout and not
1131 * leave a file in an intermediate but incoherent state.
1132 * But need to setup HSM xattr of data FID before, reuse
1133 * mti and mh presets for FID in hsm_cdt_request_completed(),
1134 * only need to clear RELEASED and DIRTY.
1136 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1137 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1139 rc = mo_swap_layouts(mti->mti_env,
1140 mdt_object_child(child1),
1141 mdt_object_child(child2),
1142 SWAP_LAYOUTS_MDS_HSM);
1145 mdt_object_unlock_put(mti, child2, lh2, 1);
1147 mdt_object_put(mti->mti_env, child1);
1153 * update status of a completed request
1154 * \param mti [IN] context
1155 * \param pgs [IN] progress of the copy tool
1156 * \param update_record [IN] update llog record
1158 * \retval -ve failure
1160 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1161 struct hsm_progress_kernel *pgs,
1162 const struct cdt_agent_req *car,
1163 enum agent_req_status *status)
1165 const struct lu_env *env = mti->mti_env;
1166 struct mdt_device *mdt = mti->mti_mdt;
1167 struct coordinator *cdt = &mdt->mdt_coordinator;
1168 struct mdt_object *obj = NULL;
1169 int cl_flags = 0, rc = 0;
1174 /* default is to retry */
1175 *status = ARS_WAITING;
1177 /* find object by FID */
1178 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1179 /* we will update MD HSM only if needed */
1180 is_mh_changed = false;
1182 /* object removed */
1183 *status = ARS_SUCCEED;
1187 /* no need to change mh->mh_arch_id
1188 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1190 if (pgs->hpk_errval != 0) {
1191 switch (pgs->hpk_errval) {
1193 /* the copy tool does not support cancel
1194 * so the cancel request is failed
1195 * As we cannot distinguish a cancel progress
1196 * from another action progress (they have the
1197 * same cookie), we suppose here the CT returns
1198 * ENOSYS only if does not support cancel
1200 /* this can also happen when cdt calls it to
1201 * for a timed out request */
1202 *status = ARS_FAILED;
1203 /* to have a cancel event in changelog */
1204 pgs->hpk_errval = ECANCELED;
1207 /* the request record has already been set to
1208 * ARS_CANCELED, this set the cancel request
1210 *status = ARS_SUCCEED;
1213 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1214 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1215 ARS_FAILED : ARS_WAITING);
1219 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1220 CERROR("%s: Request "LPX64" on "DFID
1221 " failed, error code %d too large\n",
1223 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1225 hsm_set_cl_error(&cl_flags,
1226 CLF_HSM_ERROVERFLOW);
1229 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1232 switch (car->car_hai->hai_action) {
1234 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1237 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1240 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1243 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1244 CERROR("%s: Failed request "LPX64" on "DFID
1245 " cannot be a CANCEL\n",
1248 PFID(&pgs->hpk_fid));
1251 CERROR("%s: Failed request "LPX64" on "DFID
1252 " %d is an unknown action\n",
1254 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1255 car->car_hai->hai_action);
1260 *status = ARS_SUCCEED;
1261 switch (car->car_hai->hai_action) {
1263 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1264 /* set ARCHIVE keep EXIST and clear LOST and
1266 mh.mh_arch_ver = pgs->hpk_data_version;
1267 mh.mh_flags |= HS_ARCHIVED;
1268 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1269 is_mh_changed = true;
1272 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1274 /* do not clear RELEASED and DIRTY here
1275 * this will occur in hsm_swap_layouts()
1278 /* Restoring has changed the file version on
1280 mh.mh_arch_ver = pgs->hpk_data_version;
1281 is_mh_changed = true;
1284 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1285 /* clear ARCHIVED EXISTS and LOST */
1286 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1287 is_mh_changed = true;
1290 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1291 CERROR("%s: Successful request "LPX64
1293 " cannot be a CANCEL\n",
1296 PFID(&pgs->hpk_fid));
1299 CERROR("%s: Successful request "LPX64
1301 " %d is an unknown action\n",
1303 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1304 car->car_hai->hai_action);
1310 /* rc != 0 means error when analysing action, it may come from
1311 * a crasy CT no need to manage DIRTY
1314 hsm_set_cl_flags(&cl_flags,
1315 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1317 /* unlock is done later, after layout lock management */
1319 rc = mdt_hsm_attr_set(mti, obj, &mh);
1322 /* we give back layout lock only if restore was successful or
1323 * if restore was canceled or if policy is to not retry
1324 * in other cases we just unlock the object */
1325 if (car->car_hai->hai_action == HSMA_RESTORE &&
1326 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1327 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1328 struct cdt_restore_handle *crh;
1330 /* restore in data FID done, we swap the layouts
1331 * only if restore is successful */
1332 if (pgs->hpk_errval == 0) {
1333 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1334 &car->car_hai->hai_dfid, &mh);
1336 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1337 *status = ARS_FAILED;
1338 pgs->hpk_errval = -rc;
1341 /* we have to retry, so keep layout lock */
1342 if (*status == ARS_WAITING)
1345 /* give back layout lock */
1346 mutex_lock(&cdt->cdt_restore_lock);
1347 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1349 list_del(&crh->crh_list);
1350 mutex_unlock(&cdt->cdt_restore_lock);
1351 /* just give back layout lock, we keep
1352 * the reference which is given back
1353 * later with the lock for HSM flags */
1354 if (!IS_ERR(obj) && crh != NULL)
1355 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1358 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1364 if (obj != NULL && !IS_ERR(obj)) {
1365 mo_changelog(env, CL_HSM, cl_flags,
1366 mdt_object_child(obj));
1367 mdt_object_put(mti->mti_env, obj);
1374 * update status of a request
1375 * \param mti [IN] context
1376 * \param pgs [IN] progress of the copy tool
1377 * \param update_record [IN] update llog record
1379 * \retval -ve failure
1381 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1382 struct hsm_progress_kernel *pgs,
1383 const int update_record)
1385 struct mdt_device *mdt = mti->mti_mdt;
1386 struct coordinator *cdt = &mdt->mdt_coordinator;
1387 struct cdt_agent_req *car;
1391 /* no coordinator started, so we cannot serve requests */
1392 if (cdt->cdt_state == CDT_STOPPED)
1395 /* first do sanity checks */
1396 car = mdt_cdt_update_request(cdt, pgs);
1398 CERROR("%s: Cannot find running request for cookie "LPX64
1401 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1403 RETURN(PTR_ERR(car));
1406 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1407 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1408 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1409 hsm_copytool_action2name(car->car_hai->hai_action),
1410 pgs->hpk_flags, pgs->hpk_errval,
1411 PFID(&car->car_hai->hai_fid),
1412 PFID(&car->car_hai->hai_dfid));
1414 /* progress is done on FID or data FID depending of the action and
1415 * of the copy progress */
1416 /* for restore progress is used to send back the data FID to cdt */
1417 if (car->car_hai->hai_action == HSMA_RESTORE &&
1418 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1419 car->car_hai->hai_dfid = pgs->hpk_fid;
1421 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1422 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1423 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1424 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1425 CERROR("%s: Progress on "DFID" for cookie "LPX64
1426 " does not match request FID "DFID" nor data FID "
1429 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1430 PFID(&car->car_hai->hai_fid),
1431 PFID(&car->car_hai->hai_dfid));
1432 GOTO(out, rc = -EINVAL);
1435 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1436 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1437 " is not coherent (err=%d and not completed"
1440 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1441 hsm_copytool_action2name(car->car_hai->hai_action),
1442 pgs->hpk_errval, pgs->hpk_flags);
1443 GOTO(out, rc = -EINVAL);
1446 /* now progress is valid */
1448 /* we use a root like ucred */
1449 hsm_init_ucred(mdt_ucred(mti));
1451 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1452 enum agent_req_status status;
1454 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1456 /* remove request from memory list */
1457 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1459 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1460 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1462 hsm_copytool_action2name(car->car_hai->hai_action),
1463 agent_req_status2name(status));
1465 if (update_record) {
1468 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1469 &pgs->hpk_cookie, 1,
1472 CERROR("%s: mdt_agent_record_update() failed,"
1473 " rc=%d, cannot update status to %s"
1474 " for cookie "LPX64"\n",
1475 mdt_obd_name(mdt), rc1,
1476 agent_req_status2name(status),
1478 rc = (rc != 0 ? rc : rc1);
1480 /* ct has completed a request, so a slot is available, wakeup
1481 * cdt to find new work */
1482 mdt_hsm_cdt_wakeup(mdt);
1484 /* if copytool send a progress on a canceled request
1485 * we inform copytool it should stop
1487 if (car->car_canceled == 1)
1493 /* remove ref got from mdt_cdt_update_request() */
1494 mdt_cdt_put_request(car);
1501 * data passed to llog_cat_process() callback
1502 * to cancel requests
1504 struct hsm_cancel_all_data {
1505 struct mdt_device *mdt;
1509 * llog_cat_process() callback, used to:
1510 * - purge all requests
1511 * \param env [IN] environment
1512 * \param llh [IN] llog handle
1513 * \param hdr [IN] llog record
1514 * \param data [IN] cb data = struct hsm_cancel_all_data
1516 * \retval -ve failure
1518 static int mdt_cancel_all_cb(const struct lu_env *env,
1519 struct llog_handle *llh,
1520 struct llog_rec_hdr *hdr, void *data)
1522 struct llog_agent_req_rec *larr;
1523 struct hsm_cancel_all_data *hcad;
1527 larr = (struct llog_agent_req_rec *)hdr;
1529 if (larr->arr_status == ARS_WAITING ||
1530 larr->arr_status == ARS_STARTED) {
1531 larr->arr_status = ARS_CANCELED;
1532 larr->arr_req_change = cfs_time_current_sec();
1533 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1540 * cancel all actions
1541 * \param obd [IN] MDT device
1543 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1545 struct mdt_thread_info *mti;
1546 struct coordinator *cdt = &mdt->mdt_coordinator;
1547 struct cdt_agent_req *car;
1548 struct hsm_action_list *hal = NULL;
1549 struct hsm_action_item *hai;
1550 struct hsm_cancel_all_data hcad;
1551 int hal_sz = 0, hal_len, rc;
1552 enum cdt_states save_state;
1555 /* retrieve coordinator context */
1556 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1558 /* disable coordinator */
1559 save_state = cdt->cdt_state;
1560 cdt->cdt_state = CDT_DISABLE;
1562 /* send cancel to all running requests */
1563 down_read(&cdt->cdt_request_lock);
1564 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1565 mdt_cdt_get_request(car);
1566 /* request is not yet removed from list, it will be done
1567 * when copytool will return progress
1570 if (car->car_hai->hai_action == HSMA_CANCEL) {
1571 mdt_cdt_put_request(car);
1576 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1577 cfs_size_round(car->car_hai->hai_len);
1579 if (hal_len > hal_sz && hal_sz > 0) {
1580 /* not enough room, free old buffer */
1581 OBD_FREE(hal, hal_sz);
1585 /* empty buffer, allocate one */
1588 OBD_ALLOC(hal, hal_sz);
1590 mdt_cdt_put_request(car);
1591 up_read(&cdt->cdt_request_lock);
1592 GOTO(out, rc = -ENOMEM);
1596 hal->hal_version = HAL_VERSION;
1597 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1599 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1600 hal->hal_compound_id = car->car_compound_id;
1601 hal->hal_archive_id = car->car_archive_id;
1602 hal->hal_flags = car->car_flags;
1605 hai = hai_first(hal);
1606 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1607 hai->hai_action = HSMA_CANCEL;
1610 /* it is possible to safely call mdt_hsm_agent_send()
1611 * (ie without a deadlock on cdt_request_lock), because the
1612 * write lock is taken only if we are not in purge mode
1613 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1614 * nor mdt_cdt_remove_request())
1616 /* no conflict with cdt thread because cdt is disable and we
1617 * have the request lock */
1618 mdt_hsm_agent_send(mti, hal, 1);
1620 mdt_cdt_put_request(car);
1622 up_read(&cdt->cdt_request_lock);
1625 OBD_FREE(hal, hal_sz);
1627 /* cancel all on-disk records */
1630 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1631 mdt_cancel_all_cb, &hcad);
1633 /* enable coordinator */
1634 cdt->cdt_state = save_state;
1640 * check if a request is compatible with file status
1641 * \param hai [IN] request description
1642 * \param hal_an [IN] request archive number (not used)
1643 * \param rq_flags [IN] request flags
1644 * \param hsm [IN] file HSM metadata
1647 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1648 const int hal_an, const __u64 rq_flags,
1649 const struct md_hsm *hsm)
1651 int is_compat = false;
1655 hsm_flags = hsm->mh_flags;
1656 switch (hai->hai_action) {
1658 if (!(hsm_flags & HS_NOARCHIVE) &&
1659 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1663 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1664 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1668 if (!(hsm_flags & HS_RELEASED) &&
1669 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1676 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1677 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1678 PFID(&hai->hai_fid),
1679 hsm_copytool_action2name(hai->hai_action), rq_flags,
1680 hai->hai_extent.offset, hai->hai_extent.length,
1682 (is_compat ? "compatible" : "uncompatible"));
1688 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1690 static const struct {
1694 } hsm_policy_names[] = {
1695 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1696 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1701 * convert a policy name to a bit
1702 * \param name [IN] policy name
1704 * \retval policy bit
1706 static __u64 hsm_policy_str2bit(const char *name)
1710 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1711 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1712 strcmp(hsm_policy_names[i].name, name) == 0)
1713 return hsm_policy_names[i].bit;
1718 * convert a policy bit field to a string
1719 * \param mask [IN] policy bit field
1720 * \param hexa [IN] print mask before bit names
1721 * \param buffer [OUT] string
1722 * \param count [IN] size of buffer
1724 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1732 seq_printf(m, "("LPX64") ", mask);
1734 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1737 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1738 if (hsm_policy_names[j].bit == bit)
1742 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1744 seq_printf(m, "%s ", hsm_policy_names[j].name);
1746 /* remove last ' ' */
1751 /* methods to read/write HSM policy flags */
1752 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1754 struct mdt_device *mdt = m->private;
1755 struct coordinator *cdt = &mdt->mdt_coordinator;
1758 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1763 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1764 size_t count, loff_t *off)
1766 struct seq_file *m = file->private_data;
1767 struct mdt_device *mdt = m->private;
1768 struct coordinator *cdt = &mdt->mdt_coordinator;
1769 char *start, *token, sign;
1772 __u64 add_mask, remove_mask, set_mask;
1776 if (count + 1 > PAGE_SIZE)
1779 OBD_ALLOC(buf, count + 1);
1783 if (copy_from_user(buf, buffer, count))
1784 GOTO(out, rc = -EFAULT);
1789 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1792 add_mask = remove_mask = set_mask = 0;
1794 token = strsep(&start, "\n ");
1800 if (sign == '-' || sign == '+')
1803 policy = hsm_policy_str2bit(token);
1805 CWARN("%s: '%s' is unknown, "
1806 "supported policies are:\n", mdt_obd_name(mdt),
1808 hsm_policy_bit2str(m, 0, false);
1809 GOTO(out, rc = -EINVAL);
1813 remove_mask |= policy;
1823 } while (start != NULL);
1825 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1826 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1828 /* if no sign in all string, it is a clear and set
1829 * if some sign found, all unsigned are converted
1831 * P1 P2 = set to P1 and P2
1832 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1834 if (remove_mask == 0 && add_mask == 0) {
1835 cdt->cdt_policy = set_mask;
1837 cdt->cdt_policy |= set_mask | add_mask;
1838 cdt->cdt_policy &= ~remove_mask;
1841 GOTO(out, rc = count);
1844 OBD_FREE(buf, count + 1);
1847 LPROC_SEQ_FOPS(mdt_hsm_policy);
1849 #define GENERATE_PROC_METHOD(VAR) \
1850 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1852 struct mdt_device *mdt = m->private; \
1853 struct coordinator *cdt = &mdt->mdt_coordinator; \
1856 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1860 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1861 size_t count, loff_t *off) \
1864 struct seq_file *m = file->private_data; \
1865 struct mdt_device *mdt = m->private; \
1866 struct coordinator *cdt = &mdt->mdt_coordinator; \
1871 rc = lprocfs_str_to_s64(buffer, count, &val); \
1874 if (val > 0 && val < INT_MAX) { \
1881 GENERATE_PROC_METHOD(cdt_loop_period)
1882 GENERATE_PROC_METHOD(cdt_grace_delay)
1883 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1884 GENERATE_PROC_METHOD(cdt_max_requests)
1885 GENERATE_PROC_METHOD(cdt_default_archive_id)
1888 * procfs write method for MDT/hsm_control
1889 * proc entry is in mdt directory so data is mdt obd_device pointer
1891 #define CDT_ENABLE_CMD "enabled"
1892 #define CDT_STOP_CMD "shutdown"
1893 #define CDT_DISABLE_CMD "disabled"
1894 #define CDT_PURGE_CMD "purge"
1895 #define CDT_HELP_CMD "help"
1896 #define CDT_MAX_CMD_LEN 10
1899 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1900 size_t count, loff_t *off)
1902 struct seq_file *m = file->private_data;
1903 struct obd_device *obd = m->private;
1904 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1905 struct coordinator *cdt = &(mdt->mdt_coordinator);
1907 char kernbuf[CDT_MAX_CMD_LEN];
1910 if (count == 0 || count >= sizeof(kernbuf))
1913 if (copy_from_user(kernbuf, buffer, count))
1917 if (kernbuf[count - 1] == '\n')
1918 kernbuf[count - 1] = 0;
1921 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1922 if (cdt->cdt_state == CDT_DISABLE) {
1923 cdt->cdt_state = CDT_RUNNING;
1924 mdt_hsm_cdt_wakeup(mdt);
1926 rc = mdt_hsm_cdt_start(mdt);
1928 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1929 if ((cdt->cdt_state == CDT_STOPPING) ||
1930 (cdt->cdt_state == CDT_STOPPED)) {
1931 CERROR("%s: Coordinator already stopped\n",
1935 cdt->cdt_state = CDT_STOPPING;
1937 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1938 if ((cdt->cdt_state == CDT_STOPPING) ||
1939 (cdt->cdt_state == CDT_STOPPED)) {
1940 CERROR("%s: Coordinator is stopped\n",
1944 cdt->cdt_state = CDT_DISABLE;
1946 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1947 rc = hsm_cancel_all_actions(mdt);
1948 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1956 CERROR("%s: Valid coordinator control commands are: "
1957 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1958 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1959 CDT_PURGE_CMD, CDT_HELP_CMD);
1967 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1969 struct obd_device *obd = m->private;
1970 struct coordinator *cdt;
1973 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1975 if (cdt->cdt_state == CDT_INIT)
1976 seq_printf(m, "init\n");
1977 else if (cdt->cdt_state == CDT_RUNNING)
1978 seq_printf(m, "enabled\n");
1979 else if (cdt->cdt_state == CDT_STOPPING)
1980 seq_printf(m, "stopping\n");
1981 else if (cdt->cdt_state == CDT_STOPPED)
1982 seq_printf(m, "stopped\n");
1983 else if (cdt->cdt_state == CDT_DISABLE)
1984 seq_printf(m, "disabled\n");
1986 seq_printf(m, "unknown\n");
1992 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
1998 for (i = 0; i < 8 * sizeof(mask); i++) {
1999 if (mask & (1UL << i)) {
2000 seq_printf(m, "%s%s", first ? "" : " ",
2001 hsm_copytool_action2name(i));
2011 mdt_hsm_user_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_user_request_mask);
2020 mdt_hsm_group_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_group_request_mask);
2029 mdt_hsm_other_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_other_request_mask);
2037 static inline enum hsm_copytool_action
2038 hsm_copytool_name2action(const char *name)
2040 if (strcasecmp(name, "NOOP") == 0)
2042 else if (strcasecmp(name, "ARCHIVE") == 0)
2043 return HSMA_ARCHIVE;
2044 else if (strcasecmp(name, "RESTORE") == 0)
2045 return HSMA_RESTORE;
2046 else if (strcasecmp(name, "REMOVE") == 0)
2048 else if (strcasecmp(name, "CANCEL") == 0)
2055 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2056 size_t user_count, __u64 *mask)
2058 char *buf, *pos, *name;
2064 if (!(user_count < 4096))
2067 buf_size = user_count + 1;
2069 OBD_ALLOC(buf, buf_size);
2073 if (copy_from_user(buf, user_buf, buf_size - 1))
2074 GOTO(out, rc = -EFAULT);
2076 buf[buf_size - 1] = '\0';
2079 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2085 action = hsm_copytool_name2action(name);
2087 GOTO(out, rc = -EINVAL);
2089 new_mask |= (1UL << action);
2095 OBD_FREE(buf, buf_size);
2101 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2102 size_t count, loff_t *off)
2104 struct seq_file *m = file->private_data;
2105 struct mdt_device *mdt = m->private;
2106 struct coordinator *cdt = &mdt->mdt_coordinator;
2108 return mdt_write_hsm_request_mask(file, buf, count,
2109 &cdt->cdt_user_request_mask);
2113 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2114 size_t count, loff_t *off)
2116 struct seq_file *m = file->private_data;
2117 struct mdt_device *mdt = m->private;
2118 struct coordinator *cdt = &mdt->mdt_coordinator;
2120 return mdt_write_hsm_request_mask(file, buf, count,
2121 &cdt->cdt_group_request_mask);
2125 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2126 size_t count, loff_t *off)
2128 struct seq_file *m = file->private_data;
2129 struct mdt_device *mdt = m->private;
2130 struct coordinator *cdt = &mdt->mdt_coordinator;
2132 return mdt_write_hsm_request_mask(file, buf, count,
2133 &cdt->cdt_other_request_mask);
2136 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2137 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2138 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2139 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2140 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2141 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2142 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2143 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2145 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2147 .fops = &mdt_hsm_agent_fops },
2148 { .name = "actions",
2149 .fops = &mdt_hsm_actions_fops,
2150 .proc_mode = 0444 },
2151 { .name = "default_archive_id",
2152 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2153 { .name = "grace_delay",
2154 .fops = &mdt_hsm_cdt_grace_delay_fops },
2155 { .name = "loop_period",
2156 .fops = &mdt_hsm_cdt_loop_period_fops },
2157 { .name = "max_requests",
2158 .fops = &mdt_hsm_cdt_max_requests_fops },
2160 .fops = &mdt_hsm_policy_fops },
2161 { .name = "active_request_timeout",
2162 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2163 { .name = "active_requests",
2164 .fops = &mdt_hsm_active_requests_fops },
2165 { .name = "user_request_mask",
2166 .fops = &mdt_hsm_user_request_mask_fops, },
2167 { .name = "group_request_mask",
2168 .fops = &mdt_hsm_group_request_mask_fops, },
2169 { .name = "other_request_mask",
2170 .fops = &mdt_hsm_other_request_mask_fops, },