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 <obd_support.h>
42 #include <lustre_net.h>
43 #include <lustre_export.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_kernelcomm.h>
48 #include "mdt_internal.h"
50 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
53 * get obj and HSM attributes on a fid
54 * \param mti [IN] context
55 * \param fid [IN] object fid
56 * \param hsm [OUT] HSM meta data
57 * \retval obj or error (-ENOENT if not found)
59 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
60 const struct lu_fid *fid,
64 struct mdt_object *obj;
72 /* find object by FID */
73 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
77 if (!mdt_object_exists(obj)) {
79 mdt_object_put(mti->mti_env, obj);
80 RETURN(ERR_PTR(-ENOENT));
83 rc = mdt_attr_get_complex(mti, obj, ma);
85 mdt_object_put(mti->mti_env, obj);
89 if (ma->ma_valid & MA_HSM)
92 memset(hsm, 0, sizeof(*hsm));
97 void mdt_hsm_dump_hal(int level, const char *prefix,
98 struct hsm_action_list *hal)
101 struct hsm_action_item *hai;
104 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
105 " archive_id %d flags "LPX64"\n",
106 prefix, hal->hal_version, hal->hal_count,
107 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
109 hai = hai_first(hal);
110 for (i = 0; i < hal->hal_count; i++) {
111 sz = hai->hai_len - sizeof(*hai);
112 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
113 " compound/cookie="LPX64"/"LPX64
114 " action=%s extent="LPX64"-"LPX64" gid="LPX64
115 " datalen=%d data=[%s]\n",
117 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
118 hal->hal_compound_id, hai->hai_cookie,
119 hsm_copytool_action2name(hai->hai_action),
120 hai->hai_extent.offset,
121 hai->hai_extent.length,
123 hai_dump_data_field(hai, buf, sizeof(buf)));
129 * data passed to llog_cat_process() callback
130 * to scan requests and take actions
132 struct hsm_scan_data {
133 struct mdt_thread_info *mti;
134 char fs_name[MTI_NAME_MAXLEN+1];
135 /* request to be send to agents */
136 int request_sz; /** allocated size */
137 int max_requests; /** vector size */
138 int request_cnt; /** used count */
142 struct hsm_action_list *hal;
144 /* records to be canceled */
145 int max_cookie; /** vector size */
146 int cookie_cnt; /** used count */
151 * llog_cat_process() callback, used to:
152 * - find waiting request and start action
153 * - purge canceled and done requests
154 * \param env [IN] environment
155 * \param llh [IN] llog handle
156 * \param hdr [IN] llog record
157 * \param data [IN/OUT] cb data = struct hsm_scan_data
159 * \retval -ve failure
161 static int mdt_coordinator_cb(const struct lu_env *env,
162 struct llog_handle *llh,
163 struct llog_rec_hdr *hdr,
166 const struct llog_agent_req_rec *larr;
167 struct hsm_scan_data *hsd;
168 struct hsm_action_item *hai;
169 struct mdt_device *mdt;
170 struct coordinator *cdt;
175 mdt = hsd->mti->mti_mdt;
176 cdt = &mdt->mdt_coordinator;
178 larr = (struct llog_agent_req_rec *)hdr;
179 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
180 switch (larr->arr_status) {
182 int i, empty_slot, found;
184 /* Are agents full? */
185 if (atomic_read(&cdt->cdt_request_count) ==
186 cdt->cdt_max_requests)
189 /* first search whether the request is found in the list we
190 * have built and if there is room in the request vector */
193 for (i = 0; i < hsd->max_requests &&
194 (empty_slot == -1 || found == -1); i++) {
195 if (hsd->request[i].hal == NULL) {
199 if (hsd->request[i].hal->hal_compound_id ==
200 larr->arr_compound_id) {
205 if (found == -1 && empty_slot == -1)
206 /* unknown request and no more room for new request,
207 * continue scan for to find other entries for
208 * already found request
213 struct hsm_action_list *hal;
215 /* request is not already known */
216 /* allocates hai vector size just needs to be large
218 hsd->request[empty_slot].hal_sz =
219 sizeof(*hsd->request[empty_slot].hal) +
220 cfs_size_round(MTI_NAME_MAXLEN+1) +
221 2 * cfs_size_round(larr->arr_hai.hai_len);
222 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
224 CERROR("%s: Cannot allocate memory (%d o)"
225 "for compound "LPX64"\n",
227 hsd->request[i].hal_sz,
228 larr->arr_compound_id);
231 hal->hal_version = HAL_VERSION;
232 strlcpy(hal->hal_fsname, hsd->fs_name,
233 MTI_NAME_MAXLEN + 1);
234 hal->hal_compound_id = larr->arr_compound_id;
235 hal->hal_archive_id = larr->arr_archive_id;
236 hal->hal_flags = larr->arr_flags;
238 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
239 hsd->request[empty_slot].hal = hal;
242 hai = hai_first(hal);
244 /* request is known */
245 /* we check if record archive num is the same as the
246 * known request, if not we will serve it in multiple
247 * time because we do not know if the agent can serve
249 * a use case is a compound made of multiple restore
250 * where the files are not archived in the same backend
252 if (larr->arr_archive_id !=
253 hsd->request[found].hal->hal_archive_id)
256 if (hsd->request[found].hal_sz <
257 hsd->request[found].hal_used_sz +
258 cfs_size_round(larr->arr_hai.hai_len)) {
259 /* Not enough room, need an extension */
263 sz = 2 * hsd->request[found].hal_sz;
264 OBD_ALLOC(hal_buffer, sz);
266 CERROR("%s: Cannot allocate memory "
267 "(%d o) for compound "LPX64"\n",
268 mdt_obd_name(mdt), sz,
269 larr->arr_compound_id);
272 memcpy(hal_buffer, hsd->request[found].hal,
273 hsd->request[found].hal_used_sz);
274 OBD_FREE(hsd->request[found].hal,
275 hsd->request[found].hal_sz);
276 hsd->request[found].hal = hal_buffer;
277 hsd->request[found].hal_sz = sz;
279 hai = hai_first(hsd->request[found].hal);
280 for (i = 0; i < hsd->request[found].hal->hal_count;
284 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
285 hai->hai_cookie = larr->arr_hai.hai_cookie;
286 hai->hai_gid = larr->arr_hai.hai_gid;
288 hsd->request[found].hal_used_sz +=
289 cfs_size_round(hai->hai_len);
290 hsd->request[found].hal->hal_count++;
294 struct cdt_agent_req *car;
297 /* we search for a running request
298 * error may happen if coordinator crashes or stopped
299 * with running request
301 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
303 last = larr->arr_req_create;
305 last = car->car_req_update;
306 mdt_cdt_put_request(car);
309 /* test if request too long, if yes cancel it
310 * the same way the copy tool acknowledge a cancel request */
311 if ((last + cdt->cdt_active_req_timeout)
312 < cfs_time_current_sec()) {
313 struct hsm_progress_kernel pgs;
315 dump_llog_agent_req_rec("mdt_coordinator_cb(): "
316 "request timed out, start "
318 /* a too old cancel request just needs to be removed
319 * this can happen, if copy tool does not support cancel
320 * for other requests, we have to remove the running
321 * request and notify the copytool
323 pgs.hpk_fid = larr->arr_hai.hai_fid;
324 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
325 pgs.hpk_extent = larr->arr_hai.hai_extent;
326 pgs.hpk_flags = HP_FLAG_COMPLETED;
327 pgs.hpk_errval = ENOSYS;
328 pgs.hpk_data_version = 0;
329 /* update request state, but do not record in llog, to
330 * avoid deadlock on cdt_llog_lock
332 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
334 CERROR("%s: Cannot cleanup timed out request: "
335 DFID" for cookie "LPX64" action=%s\n",
337 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
338 hsm_copytool_action2name(
339 larr->arr_hai.hai_action));
342 /* The request no longer exists, forget
343 * about it, and do not send a cancel request
344 * to the client, for which an error will be
345 * sent back, leading to an endless cycle of
347 RETURN(LLOG_DEL_RECORD);
350 /* add the cookie to the list of record to be
351 * canceled by caller */
352 if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
353 __u64 *ptr, *old_ptr;
354 int old_sz, new_sz, new_cnt;
356 /* need to increase vector size */
357 old_sz = sizeof(__u64) * hsd->max_cookie;
358 old_ptr = hsd->cookies;
360 new_cnt = 2 * hsd->max_cookie;
361 new_sz = sizeof(__u64) * new_cnt;
363 OBD_ALLOC(ptr, new_sz);
365 CERROR("%s: Cannot allocate memory "
366 "(%d o) for cookie vector\n",
367 mdt_obd_name(mdt), new_sz);
370 memcpy(ptr, hsd->cookies, old_sz);
372 hsd->max_cookie = new_cnt;
373 OBD_FREE(old_ptr, old_sz);
375 hsd->cookies[hsd->cookie_cnt] =
376 larr->arr_hai.hai_cookie;
384 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
385 cfs_time_current_sec())
386 RETURN(LLOG_DEL_RECORD);
393 * create /proc entries for coordinator
396 * \retval -ve failure
398 int hsm_cdt_procfs_init(struct mdt_device *mdt)
400 struct coordinator *cdt = &mdt->mdt_coordinator;
404 /* init /proc entries, failure is not critical */
405 cdt->cdt_proc_dir = lprocfs_register("hsm",
406 mdt2obd_dev(mdt)->obd_proc_entry,
407 lprocfs_mdt_hsm_vars, mdt);
408 if (IS_ERR(cdt->cdt_proc_dir)) {
409 rc = PTR_ERR(cdt->cdt_proc_dir);
410 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
411 " rc=%d\n", mdt_obd_name(mdt), rc);
412 cdt->cdt_proc_dir = NULL;
420 * remove /proc entries for coordinator
423 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
425 struct coordinator *cdt = &mdt->mdt_coordinator;
427 LASSERT(cdt->cdt_state == CDT_STOPPED);
428 if (cdt->cdt_proc_dir != NULL)
429 lprocfs_remove(&cdt->cdt_proc_dir);
433 * get vector of hsm cdt /proc vars
437 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
439 return lprocfs_mdt_hsm_vars;
444 * \param data [IN] obd device
446 * \retval -ve failure
448 static int mdt_coordinator(void *data)
450 struct mdt_thread_info *mti = data;
451 struct mdt_device *mdt = mti->mti_mdt;
452 struct coordinator *cdt = &mdt->mdt_coordinator;
453 struct hsm_scan_data hsd = { NULL };
457 cdt->cdt_thread.t_flags = SVC_RUNNING;
458 wake_up(&cdt->cdt_thread.t_ctl_waitq);
460 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
461 mdt_obd_name(mdt), current_pid());
463 /* timeouted cookie vector initialization */
467 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
468 * increases due to a change from /proc we do not overflow the
469 * hsd.request[] vector
471 hsd.max_requests = cdt->cdt_max_requests;
472 hsd.request_sz = hsd.max_requests * sizeof(*hsd.request);
473 OBD_ALLOC(hsd.request, hsd.request_sz);
475 GOTO(out, rc = -ENOMEM);
478 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
481 struct l_wait_info lwi;
484 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
486 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
487 (cdt->cdt_thread.t_flags &
488 (SVC_STOPPING|SVC_EVENT)),
491 CDEBUG(D_HSM, "coordinator resumes\n");
493 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
494 cdt->cdt_state == CDT_STOPPING) {
495 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
500 /* wake up before timeout, new work arrives */
501 if (cdt->cdt_thread.t_flags & SVC_EVENT)
502 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
504 /* if coordinator is suspended continue to wait */
505 if (cdt->cdt_state == CDT_DISABLE) {
506 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
510 CDEBUG(D_HSM, "coordinator starts reading llog\n");
512 if (hsd.max_requests != cdt->cdt_max_requests) {
513 /* cdt_max_requests has changed,
514 * we need to allocate a new buffer
516 OBD_FREE(hsd.request, hsd.request_sz);
517 hsd.max_requests = cdt->cdt_max_requests;
519 hsd.max_requests * sizeof(*hsd.request);
520 OBD_ALLOC(hsd.request, hsd.request_sz);
527 /* create canceled cookie vector for an arbitrary size
528 * if needed, vector will grow during llog scan
532 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
539 rc = cdt_llog_process(mti->mti_env, mdt,
540 mdt_coordinator_cb, &hsd);
544 CDEBUG(D_HSM, "Found %d requests to send and %d"
545 " requests to cancel\n",
546 hsd.request_cnt, hsd.cookie_cnt);
547 /* first we cancel llog records of the timed out requests */
548 if (hsd.cookie_cnt > 0) {
549 rc = mdt_agent_record_update(mti->mti_env, mdt,
554 CERROR("%s: mdt_agent_record_update() failed, "
555 "rc=%d, cannot update status to %s "
557 mdt_obd_name(mdt), rc,
558 agent_req_status2name(ARS_CANCELED),
562 if (list_empty(&cdt->cdt_agents)) {
563 CDEBUG(D_HSM, "no agent available, "
564 "coordinator sleeps\n");
568 /* here hsd contains a list of requests to be started */
569 for (i = 0; i < hsd.max_requests; i++) {
570 struct hsm_action_list *hal;
571 struct hsm_action_item *hai;
574 enum agent_req_status status;
576 /* still room for work ? */
577 if (atomic_read(&cdt->cdt_request_count) ==
578 cdt->cdt_max_requests)
581 if (hsd.request[i].hal == NULL)
584 /* found a request, we start it */
585 /* kuc payload allocation so we avoid an additionnal
586 * allocation in mdt_hsm_agent_send()
588 hal = kuc_alloc(hsd.request[i].hal_used_sz,
589 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
591 CERROR("%s: Cannot allocate memory (%d o) "
592 "for compound "LPX64"\n",
594 hsd.request[i].hal_used_sz,
595 hsd.request[i].hal->hal_compound_id);
598 memcpy(hal, hsd.request[i].hal,
599 hsd.request[i].hal_used_sz);
601 rc = mdt_hsm_agent_send(mti, hal, 0);
602 /* if failure, we suppose it is temporary
603 * if the copy tool failed to do the request
604 * it has to use hsm_progress
606 status = (rc ? ARS_WAITING : ARS_STARTED);
608 /* set up cookie vector to set records status
609 * after copy tools start or failed
611 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
612 OBD_ALLOC(cookies, sz);
613 if (cookies == NULL) {
614 CERROR("%s: Cannot allocate memory (%d o) "
615 "for cookies vector "LPX64"\n",
616 mdt_obd_name(mdt), sz,
617 hsd.request[i].hal->hal_compound_id);
618 kuc_free(hal, hsd.request[i].hal_used_sz);
621 hai = hai_first(hal);
622 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
623 cookies[j] = hai->hai_cookie;
627 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
628 hsd.request[i].hal->hal_count,
631 CERROR("%s: mdt_agent_record_update() failed, "
632 "rc=%d, cannot update status to %s "
634 mdt_obd_name(mdt), rc,
635 agent_req_status2name(status),
636 hsd.request[i].hal->hal_count);
638 OBD_FREE(cookies, sz);
639 kuc_free(hal, hsd.request[i].hal_used_sz);
642 /* free cookie vector allocated for/by callback */
644 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
650 /* free hal allocated by callback */
651 for (i = 0; i < hsd.max_requests; i++) {
652 if (hsd.request[i].hal) {
653 OBD_FREE(hsd.request[i].hal,
654 hsd.request[i].hal_sz);
655 hsd.request[i].hal_sz = 0;
656 hsd.request[i].hal = NULL;
660 LASSERT(hsd.request_cnt == 0);
662 /* reset callback data */
663 memset(hsd.request, 0, hsd.request_sz);
668 OBD_FREE(hsd.request, hsd.request_sz);
671 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
673 if (cdt->cdt_state == CDT_STOPPING) {
674 /* request comes from /proc path, so we need to clean cdt
676 mdt_hsm_cdt_stop(mdt);
677 mdt->mdt_opts.mo_coordinator = 0;
679 /* request comes from a thread event, generated
680 * by mdt_stop_coordinator(), we have to ack
681 * and cdt cleaning will be done by event sender
683 cdt->cdt_thread.t_flags = SVC_STOPPED;
684 wake_up(&cdt->cdt_thread.t_ctl_waitq);
688 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
689 mdt_obd_name(mdt), current_pid(), rc);
691 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
693 mdt_obd_name(mdt), current_pid());
699 * lookup a restore handle by FID
700 * caller needs to hold cdt_restore_lock
701 * \param cdt [IN] coordinator
702 * \param fid [IN] FID
703 * \retval cdt_restore_handle found
704 * \retval NULL not found
706 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
707 const struct lu_fid *fid)
709 struct cdt_restore_handle *crh;
712 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
713 if (lu_fid_eq(&crh->crh_fid, fid))
720 * data passed to llog_cat_process() callback
721 * to scan requests and take actions
723 struct hsm_restore_data {
724 struct mdt_thread_info *hrd_mti;
728 * llog_cat_process() callback, used to:
729 * - find restore request and allocate the restore handle
730 * \param env [IN] environment
731 * \param llh [IN] llog handle
732 * \param hdr [IN] llog record
733 * \param data [IN/OUT] cb data = struct hsm_restore_data
735 * \retval -ve failure
737 static int hsm_restore_cb(const struct lu_env *env,
738 struct llog_handle *llh,
739 struct llog_rec_hdr *hdr, void *data)
741 struct llog_agent_req_rec *larr;
742 struct hsm_restore_data *hrd;
743 struct cdt_restore_handle *crh;
744 struct hsm_action_item *hai;
745 struct mdt_thread_info *mti;
746 struct coordinator *cdt;
747 struct mdt_object *child;
753 cdt = &mti->mti_mdt->mdt_coordinator;
755 larr = (struct llog_agent_req_rec *)hdr;
756 hai = &larr->arr_hai;
757 if (hai->hai_cookie > cdt->cdt_last_cookie)
758 /* update the cookie to avoid collision */
759 cdt->cdt_last_cookie = hai->hai_cookie + 1;
761 if (hai->hai_action != HSMA_RESTORE ||
762 agent_req_in_final_state(larr->arr_status))
765 /* restore request not in a final state */
767 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
771 crh->crh_fid = hai->hai_fid;
772 /* in V1 all file is restored
773 crh->extent.start = hai->hai_extent.offset;
774 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
776 crh->crh_extent.start = 0;
777 crh->crh_extent.end = hai->hai_extent.length;
778 /* get the layout lock */
779 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
780 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
781 MDS_INODELOCK_LAYOUT);
783 GOTO(out, rc = PTR_ERR(child));
786 /* we choose to not keep a reference
787 * on the object during the restore time which can be very long */
788 mdt_object_put(mti->mti_env, child);
790 mutex_lock(&cdt->cdt_restore_lock);
791 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
792 mutex_unlock(&cdt->cdt_restore_lock);
799 * restore coordinator state at startup
800 * the goal is to take a layout lock for each registered restore request
801 * \param mti [IN] context
803 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
805 struct hsm_restore_data hrd;
811 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
812 hsm_restore_cb, &hrd);
817 static int hsm_init_ucred(struct lu_ucred *uc)
821 uc->uc_valid = UCRED_OLD;
830 uc->uc_suppgids[0] = -1;
831 uc->uc_suppgids[1] = -1;
832 uc->uc_cap = CFS_CAP_FS_MASK;
835 uc->uc_identity = NULL;
841 * wake up coordinator thread
842 * \param mdt [IN] device
844 * \retval -ve failure
846 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
848 struct coordinator *cdt = &mdt->mdt_coordinator;
851 if (cdt->cdt_state == CDT_STOPPED)
854 /* wake up coordinator */
855 cdt->cdt_thread.t_flags = SVC_EVENT;
856 wake_up(&cdt->cdt_thread.t_ctl_waitq);
862 * initialize coordinator struct
863 * \param mdt [IN] device
865 * \retval -ve failure
867 int mdt_hsm_cdt_init(struct mdt_device *mdt)
869 struct coordinator *cdt = &mdt->mdt_coordinator;
870 struct mdt_thread_info *cdt_mti = NULL;
874 cdt->cdt_state = CDT_STOPPED;
876 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
877 mutex_init(&cdt->cdt_llog_lock);
878 init_rwsem(&cdt->cdt_agent_lock);
879 init_rwsem(&cdt->cdt_request_lock);
880 mutex_init(&cdt->cdt_restore_lock);
882 INIT_LIST_HEAD(&cdt->cdt_requests);
883 INIT_LIST_HEAD(&cdt->cdt_agents);
884 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
886 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
890 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
891 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
893 lu_context_enter(&cdt->cdt_session);
894 cdt->cdt_env.le_ses = &cdt->cdt_session;
896 lu_env_fini(&cdt->cdt_env);
900 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
901 LASSERT(cdt_mti != NULL);
903 cdt_mti->mti_env = &cdt->cdt_env;
904 cdt_mti->mti_mdt = mdt;
906 hsm_init_ucred(mdt_ucred(cdt_mti));
908 /* default values for /proc tunnables
909 * can be override by MGS conf */
910 cdt->cdt_default_archive_id = 1;
911 cdt->cdt_grace_delay = 60;
912 cdt->cdt_loop_period = 10;
913 cdt->cdt_max_requests = 3;
914 cdt->cdt_policy = CDT_DEFAULT_POLICY;
915 cdt->cdt_active_req_timeout = 3600;
921 * free a coordinator thread
922 * \param mdt [IN] device
924 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
926 struct coordinator *cdt = &mdt->mdt_coordinator;
929 lu_context_exit(cdt->cdt_env.le_ses);
930 lu_context_fini(cdt->cdt_env.le_ses);
932 lu_env_fini(&cdt->cdt_env);
938 * start a coordinator thread
939 * \param mdt [IN] device
941 * \retval -ve failure
943 int mdt_hsm_cdt_start(struct mdt_device *mdt)
945 struct coordinator *cdt = &mdt->mdt_coordinator;
948 struct mdt_thread_info *cdt_mti;
949 struct task_struct *task;
952 /* functions defined but not yet used
953 * this avoid compilation warning
957 if (cdt->cdt_state != CDT_STOPPED) {
958 CERROR("%s: Coordinator already started\n",
963 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
964 cdt->cdt_policy = CDT_DEFAULT_POLICY;
966 cdt->cdt_state = CDT_INIT;
968 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
969 /* just need to be larger than previous one */
970 /* cdt_last_cookie is protected by cdt_llog_lock */
971 cdt->cdt_last_cookie = cfs_time_current_sec();
972 atomic_set(&cdt->cdt_request_count, 0);
973 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
974 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
975 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
977 /* to avoid deadlock when start is made through /proc
978 * /proc entries are created by the coordinator thread */
980 /* set up list of started restore requests */
981 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
982 rc = mdt_hsm_pending_restore(cdt_mti);
984 CERROR("%s: cannot take the layout locks needed"
985 " for registered restore: %d\n",
986 mdt_obd_name(mdt), rc);
988 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
991 cdt->cdt_state = CDT_STOPPED;
992 CERROR("%s: error starting coordinator thread: %d\n",
993 mdt_obd_name(mdt), rc);
996 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1001 wait_event(cdt->cdt_thread.t_ctl_waitq,
1002 (cdt->cdt_thread.t_flags & SVC_RUNNING));
1004 cdt->cdt_state = CDT_RUNNING;
1005 mdt->mdt_opts.mo_coordinator = 1;
1010 * stop a coordinator thread
1011 * \param mdt [IN] device
1013 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1015 struct coordinator *cdt = &mdt->mdt_coordinator;
1016 struct cdt_agent_req *car, *tmp1;
1017 struct hsm_agent *ha, *tmp2;
1018 struct cdt_restore_handle *crh, *tmp3;
1019 struct mdt_thread_info *cdt_mti;
1022 if (cdt->cdt_state == CDT_STOPPED) {
1023 CERROR("%s: Coordinator already stopped\n",
1028 if (cdt->cdt_state != CDT_STOPPING) {
1029 /* stop coordinator thread before cleaning */
1030 cdt->cdt_thread.t_flags = SVC_STOPPING;
1031 wake_up(&cdt->cdt_thread.t_ctl_waitq);
1032 wait_event(cdt->cdt_thread.t_ctl_waitq,
1033 cdt->cdt_thread.t_flags & SVC_STOPPED);
1035 cdt->cdt_state = CDT_STOPPED;
1037 /* start cleaning */
1038 down_write(&cdt->cdt_request_lock);
1039 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1041 list_del(&car->car_request_list);
1042 mdt_cdt_free_request(car);
1044 up_write(&cdt->cdt_request_lock);
1046 down_write(&cdt->cdt_agent_lock);
1047 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1048 list_del(&ha->ha_list);
1051 up_write(&cdt->cdt_agent_lock);
1053 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1054 mutex_lock(&cdt->cdt_restore_lock);
1055 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1056 struct mdt_object *child;
1058 /* give back layout lock */
1059 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1061 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1063 list_del(&crh->crh_list);
1065 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1067 mutex_unlock(&cdt->cdt_restore_lock);
1069 mdt->mdt_opts.mo_coordinator = 0;
1075 * register all requests from an hal in the memory list
1076 * \param mti [IN] context
1077 * \param hal [IN] request
1078 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1079 * which is running the CT
1081 * \retval -ve failure
1083 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1084 struct hsm_action_list *hal, struct obd_uuid *uuid)
1086 struct mdt_device *mdt = mti->mti_mdt;
1087 struct coordinator *cdt = &mdt->mdt_coordinator;
1088 struct hsm_action_item *hai;
1092 /* register request in memory list */
1093 hai = hai_first(hal);
1094 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1095 struct cdt_agent_req *car;
1097 /* in case of a cancel request, we first mark the ondisk
1098 * record of the request we want to stop as canceled
1099 * this does not change the cancel record
1100 * it will be done when updating the request status
1102 if (hai->hai_action == HSMA_CANCEL) {
1103 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1107 CERROR("%s: mdt_agent_record_update() failed, "
1108 "rc=%d, cannot update status to %s "
1109 "for cookie "LPX64"\n",
1110 mdt_obd_name(mdt), rc,
1111 agent_req_status2name(ARS_CANCELED),
1116 /* find the running request to set it canceled */
1117 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1119 car->car_canceled = 1;
1120 /* uuid has to be changed to the one running the
1121 * request to cancel */
1122 *uuid = car->car_uuid;
1123 mdt_cdt_put_request(car);
1125 /* no need to memorize cancel request
1126 * this also avoid a deadlock when we receive
1127 * a purge all requests command
1132 if (hai->hai_action == HSMA_ARCHIVE) {
1133 struct mdt_object *obj;
1136 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1137 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1140 GOTO(out, rc = PTR_ERR(obj));
1142 hsm.mh_flags |= HS_EXISTS;
1143 hsm.mh_arch_id = hal->hal_archive_id;
1144 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1145 mdt_object_put(mti->mti_env, obj);
1150 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1151 hal->hal_archive_id, hal->hal_flags,
1154 GOTO(out, rc = PTR_ERR(car));
1156 rc = mdt_cdt_add_request(cdt, car);
1158 mdt_cdt_free_request(car);
1165 * swap layouts between 2 fids
1166 * \param mti [IN] context
1169 * \param mh_common [IN] MD HSM
1171 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1172 const lustre_fid *fid, const lustre_fid *dfid,
1173 struct md_hsm *mh_common)
1175 struct mdt_device *mdt = mti->mti_mdt;
1176 struct mdt_object *child1, *child2;
1177 struct mdt_lock_handle *lh2;
1181 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1183 GOTO(out, rc = PTR_ERR(child1));
1185 /* we already have layout lock on FID so take only
1187 lh2 = &mti->mti_lh[MDT_LH_OLD];
1188 mdt_lock_reg_init(lh2, LCK_EX);
1189 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1191 GOTO(out_child1, rc = PTR_ERR(child2));
1193 /* if copy tool closes the volatile before sending the final
1194 * progress through llapi_hsm_copy_end(), all the objects
1195 * are removed and mdd_swap_layout LBUG */
1196 if (!mdt_object_exists(child2)) {
1197 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1198 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1199 GOTO(out_child2, rc = -ENOENT);
1201 /* Since we only handle restores here, unconditionally use
1202 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1203 * be preserved in case of failure during swap_layout and not
1204 * leave a file in an intermediate but incoherent state.
1205 * But need to setup HSM xattr of data FID before, reuse
1206 * mti and mh presets for FID in hsm_cdt_request_completed(),
1207 * only need to clear RELEASED and DIRTY.
1209 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1210 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1212 rc = mo_swap_layouts(mti->mti_env,
1213 mdt_object_child(child1),
1214 mdt_object_child(child2),
1215 SWAP_LAYOUTS_MDS_HSM);
1218 mdt_object_unlock_put(mti, child2, lh2, 1);
1220 mdt_object_put(mti->mti_env, child1);
1226 * update status of a completed request
1227 * \param mti [IN] context
1228 * \param pgs [IN] progress of the copy tool
1229 * \param update_record [IN] update llog record
1231 * \retval -ve failure
1233 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1234 struct hsm_progress_kernel *pgs,
1235 const struct cdt_agent_req *car,
1236 enum agent_req_status *status)
1238 const struct lu_env *env = mti->mti_env;
1239 struct mdt_device *mdt = mti->mti_mdt;
1240 struct coordinator *cdt = &mdt->mdt_coordinator;
1241 struct mdt_object *obj = NULL;
1242 int cl_flags = 0, rc = 0;
1247 /* default is to retry */
1248 *status = ARS_WAITING;
1250 /* find object by FID */
1251 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1252 /* we will update MD HSM only if needed */
1253 is_mh_changed = false;
1255 /* object removed */
1256 *status = ARS_SUCCEED;
1260 /* no need to change mh->mh_arch_id
1261 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1263 if (pgs->hpk_errval != 0) {
1264 switch (pgs->hpk_errval) {
1266 /* the copy tool does not support cancel
1267 * so the cancel request is failed
1268 * As we cannot distinguish a cancel progress
1269 * from another action progress (they have the
1270 * same cookie), we suppose here the CT returns
1271 * ENOSYS only if does not support cancel
1273 /* this can also happen when cdt calls it to
1274 * for a timed out request */
1275 *status = ARS_FAILED;
1276 /* to have a cancel event in changelog */
1277 pgs->hpk_errval = ECANCELED;
1280 /* the request record has already been set to
1281 * ARS_CANCELED, this set the cancel request
1283 *status = ARS_SUCCEED;
1286 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1287 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1288 ARS_FAILED : ARS_WAITING);
1292 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1293 CERROR("%s: Request "LPX64" on "DFID
1294 " failed, error code %d too large\n",
1296 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1298 hsm_set_cl_error(&cl_flags,
1299 CLF_HSM_ERROVERFLOW);
1302 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1305 switch (car->car_hai->hai_action) {
1307 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1310 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1313 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1316 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1317 CERROR("%s: Failed request "LPX64" on "DFID
1318 " cannot be a CANCEL\n",
1321 PFID(&pgs->hpk_fid));
1324 CERROR("%s: Failed request "LPX64" on "DFID
1325 " %d is an unknown action\n",
1327 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1328 car->car_hai->hai_action);
1333 *status = ARS_SUCCEED;
1334 switch (car->car_hai->hai_action) {
1336 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1337 /* set ARCHIVE keep EXIST and clear LOST and
1339 mh.mh_arch_ver = pgs->hpk_data_version;
1340 mh.mh_flags |= HS_ARCHIVED;
1341 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1342 is_mh_changed = true;
1345 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1347 /* do not clear RELEASED and DIRTY here
1348 * this will occur in hsm_swap_layouts()
1351 /* Restoring has changed the file version on
1353 mh.mh_arch_ver = pgs->hpk_data_version;
1354 is_mh_changed = true;
1357 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1358 /* clear ARCHIVED EXISTS and LOST */
1359 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1360 is_mh_changed = true;
1363 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1364 CERROR("%s: Successful request "LPX64
1366 " cannot be a CANCEL\n",
1369 PFID(&pgs->hpk_fid));
1372 CERROR("%s: Successful request "LPX64
1374 " %d is an unknown action\n",
1376 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1377 car->car_hai->hai_action);
1383 /* rc != 0 means error when analysing action, it may come from
1384 * a crasy CT no need to manage DIRTY
1387 hsm_set_cl_flags(&cl_flags,
1388 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1390 /* unlock is done later, after layout lock management */
1392 rc = mdt_hsm_attr_set(mti, obj, &mh);
1395 /* we give back layout lock only if restore was successful or
1396 * if restore was canceled or if policy is to not retry
1397 * in other cases we just unlock the object */
1398 if (car->car_hai->hai_action == HSMA_RESTORE &&
1399 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1400 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1401 struct cdt_restore_handle *crh;
1403 /* restore in data FID done, we swap the layouts
1404 * only if restore is successful */
1405 if (pgs->hpk_errval == 0) {
1406 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1407 &car->car_hai->hai_dfid, &mh);
1409 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1410 *status = ARS_FAILED;
1411 pgs->hpk_errval = -rc;
1414 /* we have to retry, so keep layout lock */
1415 if (*status == ARS_WAITING)
1418 /* give back layout lock */
1419 mutex_lock(&cdt->cdt_restore_lock);
1420 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1422 list_del(&crh->crh_list);
1423 mutex_unlock(&cdt->cdt_restore_lock);
1424 /* just give back layout lock, we keep
1425 * the reference which is given back
1426 * later with the lock for HSM flags */
1427 if (!IS_ERR(obj) && crh != NULL)
1428 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1431 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1437 if (obj != NULL && !IS_ERR(obj)) {
1438 mo_changelog(env, CL_HSM, cl_flags,
1439 mdt_object_child(obj));
1440 mdt_object_put(mti->mti_env, obj);
1447 * update status of a request
1448 * \param mti [IN] context
1449 * \param pgs [IN] progress of the copy tool
1450 * \param update_record [IN] update llog record
1452 * \retval -ve failure
1454 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1455 struct hsm_progress_kernel *pgs,
1456 const int update_record)
1458 struct mdt_device *mdt = mti->mti_mdt;
1459 struct coordinator *cdt = &mdt->mdt_coordinator;
1460 struct cdt_agent_req *car;
1464 /* no coordinator started, so we cannot serve requests */
1465 if (cdt->cdt_state == CDT_STOPPED)
1468 /* first do sanity checks */
1469 car = mdt_cdt_update_request(cdt, pgs);
1471 CERROR("%s: Cannot find running request for cookie "LPX64
1474 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1477 RETURN(PTR_ERR(car));
1480 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1481 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1482 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1483 hsm_copytool_action2name(car->car_hai->hai_action),
1484 pgs->hpk_flags, pgs->hpk_errval,
1485 PFID(&car->car_hai->hai_fid),
1486 PFID(&car->car_hai->hai_dfid));
1488 /* progress is done on FID or data FID depending of the action and
1489 * of the copy progress */
1490 /* for restore progress is used to send back the data FID to cdt */
1491 if (car->car_hai->hai_action == HSMA_RESTORE &&
1492 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1493 car->car_hai->hai_dfid = pgs->hpk_fid;
1495 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1496 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1497 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1498 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1499 CERROR("%s: Progress on "DFID" for cookie "LPX64
1500 " does not match request FID "DFID" nor data FID "
1503 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1504 PFID(&car->car_hai->hai_fid),
1505 PFID(&car->car_hai->hai_dfid));
1506 GOTO(out, rc = -EINVAL);
1509 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1510 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1511 " is not coherent (err=%d and not completed"
1514 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1515 hsm_copytool_action2name(car->car_hai->hai_action),
1516 pgs->hpk_errval, pgs->hpk_flags);
1517 GOTO(out, rc = -EINVAL);
1520 /* now progress is valid */
1522 /* we use a root like ucred */
1523 hsm_init_ucred(mdt_ucred(mti));
1525 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1526 enum agent_req_status status;
1528 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1530 /* remove request from memory list */
1531 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1533 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1534 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1536 hsm_copytool_action2name(car->car_hai->hai_action),
1537 agent_req_status2name(status));
1539 if (update_record) {
1542 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1543 &pgs->hpk_cookie, 1,
1546 CERROR("%s: mdt_agent_record_update() failed,"
1547 " rc=%d, cannot update status to %s"
1548 " for cookie "LPX64"\n",
1549 mdt_obd_name(mdt), rc1,
1550 agent_req_status2name(status),
1552 rc = (rc != 0 ? rc : rc1);
1554 /* ct has completed a request, so a slot is available, wakeup
1555 * cdt to find new work */
1556 mdt_hsm_cdt_wakeup(mdt);
1558 /* if copytool send a progress on a canceled request
1559 * we inform copytool it should stop
1561 if (car->car_canceled == 1)
1567 /* remove ref got from mdt_cdt_update_request() */
1568 mdt_cdt_put_request(car);
1575 * data passed to llog_cat_process() callback
1576 * to cancel requests
1578 struct hsm_cancel_all_data {
1579 struct mdt_device *mdt;
1583 * llog_cat_process() callback, used to:
1584 * - purge all requests
1585 * \param env [IN] environment
1586 * \param llh [IN] llog handle
1587 * \param hdr [IN] llog record
1588 * \param data [IN] cb data = struct hsm_cancel_all_data
1590 * \retval -ve failure
1592 static int mdt_cancel_all_cb(const struct lu_env *env,
1593 struct llog_handle *llh,
1594 struct llog_rec_hdr *hdr, void *data)
1596 struct llog_agent_req_rec *larr;
1597 struct hsm_cancel_all_data *hcad;
1601 larr = (struct llog_agent_req_rec *)hdr;
1603 if (larr->arr_status == ARS_WAITING ||
1604 larr->arr_status == ARS_STARTED) {
1605 larr->arr_status = ARS_CANCELED;
1606 larr->arr_req_change = cfs_time_current_sec();
1607 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1609 RETURN(LLOG_DEL_RECORD);
1615 * cancel all actions
1616 * \param obd [IN] MDT device
1618 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1620 struct mdt_thread_info *mti;
1621 struct coordinator *cdt = &mdt->mdt_coordinator;
1622 struct cdt_agent_req *car;
1623 struct hsm_action_list *hal = NULL;
1624 struct hsm_action_item *hai;
1625 struct hsm_cancel_all_data hcad;
1626 int hal_sz = 0, hal_len, rc;
1627 enum cdt_states save_state;
1630 /* retrieve coordinator context */
1631 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1633 /* disable coordinator */
1634 save_state = cdt->cdt_state;
1635 cdt->cdt_state = CDT_DISABLE;
1637 /* send cancel to all running requests */
1638 down_read(&cdt->cdt_request_lock);
1639 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1640 mdt_cdt_get_request(car);
1641 /* request is not yet removed from list, it will be done
1642 * when copytool will return progress
1645 if (car->car_hai->hai_action == HSMA_CANCEL) {
1646 mdt_cdt_put_request(car);
1651 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1652 cfs_size_round(car->car_hai->hai_len);
1654 if (hal_len > hal_sz && hal_sz > 0) {
1655 /* not enough room, free old buffer */
1656 OBD_FREE(hal, hal_sz);
1660 /* empty buffer, allocate one */
1663 OBD_ALLOC(hal, hal_sz);
1665 mdt_cdt_put_request(car);
1666 up_read(&cdt->cdt_request_lock);
1667 GOTO(out, rc = -ENOMEM);
1671 hal->hal_version = HAL_VERSION;
1672 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1674 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1675 hal->hal_compound_id = car->car_compound_id;
1676 hal->hal_archive_id = car->car_archive_id;
1677 hal->hal_flags = car->car_flags;
1680 hai = hai_first(hal);
1681 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1682 hai->hai_action = HSMA_CANCEL;
1685 /* it is possible to safely call mdt_hsm_agent_send()
1686 * (ie without a deadlock on cdt_request_lock), because the
1687 * write lock is taken only if we are not in purge mode
1688 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1689 * nor mdt_cdt_remove_request())
1691 /* no conflict with cdt thread because cdt is disable and we
1692 * have the request lock */
1693 mdt_hsm_agent_send(mti, hal, 1);
1695 mdt_cdt_put_request(car);
1697 up_read(&cdt->cdt_request_lock);
1700 OBD_FREE(hal, hal_sz);
1702 /* cancel all on-disk records */
1705 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1706 mdt_cancel_all_cb, &hcad);
1708 /* enable coordinator */
1709 cdt->cdt_state = save_state;
1715 * check if a request is compatible with file status
1716 * \param hai [IN] request description
1717 * \param hal_an [IN] request archive number (not used)
1718 * \param rq_flags [IN] request flags
1719 * \param hsm [IN] file HSM metadata
1722 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1723 const int hal_an, const __u64 rq_flags,
1724 const struct md_hsm *hsm)
1726 int is_compat = false;
1730 hsm_flags = hsm->mh_flags;
1731 switch (hai->hai_action) {
1733 if (!(hsm_flags & HS_NOARCHIVE) &&
1734 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1738 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1739 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1743 if (!(hsm_flags & HS_RELEASED) &&
1744 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1751 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1752 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1753 PFID(&hai->hai_fid),
1754 hsm_copytool_action2name(hai->hai_action), rq_flags,
1755 hai->hai_extent.offset, hai->hai_extent.length,
1757 (is_compat ? "compatible" : "uncompatible"));
1763 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1765 static const struct {
1769 } hsm_policy_names[] = {
1770 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1771 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1776 * convert a policy name to a bit
1777 * \param name [IN] policy name
1779 * \retval policy bit
1781 static __u64 hsm_policy_str2bit(const char *name)
1785 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1786 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1787 strcmp(hsm_policy_names[i].name, name) == 0)
1788 return hsm_policy_names[i].bit;
1793 * convert a policy bit field to a string
1794 * \param mask [IN] policy bit field
1795 * \param hexa [IN] print mask before bit names
1796 * \param buffer [OUT] string
1797 * \param count [IN] size of buffer
1799 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1807 seq_printf(m, "("LPX64") ", mask);
1809 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1812 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1813 if (hsm_policy_names[j].bit == bit)
1817 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1819 seq_printf(m, "%s ", hsm_policy_names[j].name);
1821 /* remove last ' ' */
1826 /* methods to read/write HSM policy flags */
1827 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1829 struct mdt_device *mdt = m->private;
1830 struct coordinator *cdt = &mdt->mdt_coordinator;
1833 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1838 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1839 size_t count, loff_t *off)
1841 struct seq_file *m = file->private_data;
1842 struct mdt_device *mdt = m->private;
1843 struct coordinator *cdt = &mdt->mdt_coordinator;
1844 char *start, *token, sign;
1847 __u64 add_mask, remove_mask, set_mask;
1851 if (count + 1 > PAGE_SIZE)
1854 OBD_ALLOC(buf, count + 1);
1858 if (copy_from_user(buf, buffer, count))
1859 GOTO(out, rc = -EFAULT);
1864 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1867 add_mask = remove_mask = set_mask = 0;
1869 token = strsep(&start, "\n ");
1875 if (sign == '-' || sign == '+')
1878 policy = hsm_policy_str2bit(token);
1880 CWARN("%s: '%s' is unknown, "
1881 "supported policies are:\n", mdt_obd_name(mdt),
1883 hsm_policy_bit2str(m, 0, false);
1884 GOTO(out, rc = -EINVAL);
1888 remove_mask |= policy;
1898 } while (start != NULL);
1900 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1901 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1903 /* if no sign in all string, it is a clear and set
1904 * if some sign found, all unsigned are converted
1906 * P1 P2 = set to P1 and P2
1907 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1909 if (remove_mask == 0 && add_mask == 0) {
1910 cdt->cdt_policy = set_mask;
1912 cdt->cdt_policy |= set_mask | add_mask;
1913 cdt->cdt_policy &= ~remove_mask;
1916 GOTO(out, rc = count);
1919 OBD_FREE(buf, count + 1);
1922 LPROC_SEQ_FOPS(mdt_hsm_policy);
1924 #define GENERATE_PROC_METHOD(VAR) \
1925 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1927 struct mdt_device *mdt = m->private; \
1928 struct coordinator *cdt = &mdt->mdt_coordinator; \
1931 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1935 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1936 size_t count, loff_t *off) \
1939 struct seq_file *m = file->private_data; \
1940 struct mdt_device *mdt = m->private; \
1941 struct coordinator *cdt = &mdt->mdt_coordinator; \
1946 rc = lprocfs_write_helper(buffer, count, &val); \
1956 GENERATE_PROC_METHOD(cdt_loop_period)
1957 GENERATE_PROC_METHOD(cdt_grace_delay)
1958 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1959 GENERATE_PROC_METHOD(cdt_max_requests)
1960 GENERATE_PROC_METHOD(cdt_default_archive_id)
1963 * procfs write method for MDT/hsm_control
1964 * proc entry is in mdt directory so data is mdt obd_device pointer
1966 #define CDT_ENABLE_CMD "enabled"
1967 #define CDT_STOP_CMD "shutdown"
1968 #define CDT_DISABLE_CMD "disabled"
1969 #define CDT_PURGE_CMD "purge"
1970 #define CDT_HELP_CMD "help"
1971 #define CDT_MAX_CMD_LEN 10
1974 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1975 size_t count, loff_t *off)
1977 struct seq_file *m = file->private_data;
1978 struct obd_device *obd = m->private;
1979 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1980 struct coordinator *cdt = &(mdt->mdt_coordinator);
1982 char kernbuf[CDT_MAX_CMD_LEN];
1985 if (count == 0 || count >= sizeof(kernbuf))
1988 if (copy_from_user(kernbuf, buffer, count))
1992 if (kernbuf[count - 1] == '\n')
1993 kernbuf[count - 1] = 0;
1996 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1997 if (cdt->cdt_state == CDT_DISABLE) {
1998 cdt->cdt_state = CDT_RUNNING;
1999 mdt_hsm_cdt_wakeup(mdt);
2001 rc = mdt_hsm_cdt_start(mdt);
2003 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2004 if ((cdt->cdt_state == CDT_STOPPING) ||
2005 (cdt->cdt_state == CDT_STOPPED)) {
2006 CERROR("%s: Coordinator already stopped\n",
2010 cdt->cdt_state = CDT_STOPPING;
2012 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2013 if ((cdt->cdt_state == CDT_STOPPING) ||
2014 (cdt->cdt_state == CDT_STOPPED)) {
2015 CERROR("%s: Coordinator is stopped\n",
2019 cdt->cdt_state = CDT_DISABLE;
2021 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2022 rc = hsm_cancel_all_actions(mdt);
2023 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2031 CERROR("%s: Valid coordinator control commands are: "
2032 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2033 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2034 CDT_PURGE_CMD, CDT_HELP_CMD);
2042 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2044 struct obd_device *obd = m->private;
2045 struct coordinator *cdt;
2048 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2050 if (cdt->cdt_state == CDT_INIT)
2051 seq_printf(m, "init\n");
2052 else if (cdt->cdt_state == CDT_RUNNING)
2053 seq_printf(m, "enabled\n");
2054 else if (cdt->cdt_state == CDT_STOPPING)
2055 seq_printf(m, "stopping\n");
2056 else if (cdt->cdt_state == CDT_STOPPED)
2057 seq_printf(m, "stopped\n");
2058 else if (cdt->cdt_state == CDT_DISABLE)
2059 seq_printf(m, "disabled\n");
2061 seq_printf(m, "unknown\n");
2067 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2072 for (i = 0; i < 8 * sizeof(mask); i++) {
2073 if (mask & (1UL << i))
2074 rc += seq_printf(m, "%s%s", rc == 0 ? "" : " ",
2075 hsm_copytool_action2name(i));
2077 rc += seq_printf(m, "\n");
2083 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2085 struct mdt_device *mdt = m->private;
2086 struct coordinator *cdt = &mdt->mdt_coordinator;
2088 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2092 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2094 struct mdt_device *mdt = m->private;
2095 struct coordinator *cdt = &mdt->mdt_coordinator;
2097 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2101 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2103 struct mdt_device *mdt = m->private;
2104 struct coordinator *cdt = &mdt->mdt_coordinator;
2106 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2109 static inline enum hsm_copytool_action
2110 hsm_copytool_name2action(const char *name)
2112 if (strcasecmp(name, "NOOP") == 0)
2114 else if (strcasecmp(name, "ARCHIVE") == 0)
2115 return HSMA_ARCHIVE;
2116 else if (strcasecmp(name, "RESTORE") == 0)
2117 return HSMA_RESTORE;
2118 else if (strcasecmp(name, "REMOVE") == 0)
2120 else if (strcasecmp(name, "CANCEL") == 0)
2127 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2128 size_t user_count, __u64 *mask)
2130 char *buf, *pos, *name;
2136 if (!(user_count < 4096))
2139 buf_size = user_count + 1;
2141 OBD_ALLOC(buf, buf_size);
2145 if (copy_from_user(buf, user_buf, buf_size - 1))
2146 GOTO(out, rc = -EFAULT);
2148 buf[buf_size - 1] = '\0';
2151 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2157 action = hsm_copytool_name2action(name);
2159 GOTO(out, rc = -EINVAL);
2161 new_mask |= (1UL << action);
2167 OBD_FREE(buf, buf_size);
2173 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2174 size_t count, loff_t *off)
2176 struct seq_file *m = file->private_data;
2177 struct mdt_device *mdt = m->private;
2178 struct coordinator *cdt = &mdt->mdt_coordinator;
2180 return mdt_write_hsm_request_mask(file, buf, count,
2181 &cdt->cdt_user_request_mask);
2185 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2186 size_t count, loff_t *off)
2188 struct seq_file *m = file->private_data;
2189 struct mdt_device *mdt = m->private;
2190 struct coordinator *cdt = &mdt->mdt_coordinator;
2192 return mdt_write_hsm_request_mask(file, buf, count,
2193 &cdt->cdt_group_request_mask);
2197 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2198 size_t count, loff_t *off)
2200 struct seq_file *m = file->private_data;
2201 struct mdt_device *mdt = m->private;
2202 struct coordinator *cdt = &mdt->mdt_coordinator;
2204 return mdt_write_hsm_request_mask(file, buf, count,
2205 &cdt->cdt_other_request_mask);
2208 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2209 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2210 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2211 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2212 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2213 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2214 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2215 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2217 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2219 .fops = &mdt_hsm_agent_fops },
2220 { .name = "actions",
2221 .fops = &mdt_hsm_actions_fops,
2222 .proc_mode = 0444 },
2223 { .name = "default_archive_id",
2224 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2225 { .name = "grace_delay",
2226 .fops = &mdt_hsm_cdt_grace_delay_fops },
2227 { .name = "loop_period",
2228 .fops = &mdt_hsm_cdt_loop_period_fops },
2229 { .name = "max_requests",
2230 .fops = &mdt_hsm_cdt_max_requests_fops },
2232 .fops = &mdt_hsm_policy_fops },
2233 { .name = "active_request_timeout",
2234 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2235 { .name = "active_requests",
2236 .fops = &mdt_hsm_active_requests_fops },
2237 { .name = "user_request_mask",
2238 .fops = &mdt_hsm_user_request_mask_fops, },
2239 { .name = "group_request_mask",
2240 .fops = &mdt_hsm_group_request_mask_fops, },
2241 { .name = "other_request_mask",
2242 .fops = &mdt_hsm_other_request_mask_fops, },