4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
24 * Use is subject to license terms.
25 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
29 * lustre/mdt/mdt_coordinator.c
31 * Lustre HSM Coordinator
33 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <obd_support.h>
41 #include <lustre_net.h>
42 #include <lustre_export.h>
44 #include <lprocfs_status.h>
45 #include <lustre_log.h>
46 #include "mdt_internal.h"
48 static struct lprocfs_seq_vars lprocfs_mdt_hsm_vars[];
51 * get obj and HSM attributes on a fid
52 * \param mti [IN] context
53 * \param fid [IN] object fid
54 * \param hsm [OUT] HSM meta data
55 * \retval obj or error (-ENOENT if not found)
57 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
58 const struct lu_fid *fid,
62 struct mdt_object *obj;
70 /* find object by FID */
71 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
75 if (!mdt_object_exists(obj)) {
77 mdt_object_put(mti->mti_env, obj);
78 RETURN(ERR_PTR(-ENOENT));
81 rc = mdt_attr_get_complex(mti, obj, ma);
83 mdt_object_put(mti->mti_env, obj);
87 if (ma->ma_valid & MA_HSM)
90 memset(hsm, 0, sizeof(*hsm));
95 void mdt_hsm_dump_hal(int level, const char *prefix,
96 struct hsm_action_list *hal)
99 struct hsm_action_item *hai;
102 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
103 " archive_id %d flags "LPX64"\n",
104 prefix, hal->hal_version, hal->hal_count,
105 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
107 hai = hai_first(hal);
108 for (i = 0; i < hal->hal_count; i++) {
109 sz = hai->hai_len - sizeof(*hai);
110 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
111 " compound/cookie="LPX64"/"LPX64
112 " action=%s extent="LPX64"-"LPX64" gid="LPX64
113 " datalen=%d data=[%s]\n",
115 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
116 hal->hal_compound_id, hai->hai_cookie,
117 hsm_copytool_action2name(hai->hai_action),
118 hai->hai_extent.offset,
119 hai->hai_extent.length,
121 hai_dump_data_field(hai, buf, sizeof(buf)));
127 * data passed to llog_cat_process() callback
128 * to scan requests and take actions
130 struct hsm_scan_data {
131 struct mdt_thread_info *mti;
132 char fs_name[MTI_NAME_MAXLEN+1];
133 /* request to be send to agents */
134 int request_sz; /** allocated size */
135 int max_requests; /** vector size */
136 int request_cnt; /** used count */
140 struct hsm_action_list *hal;
142 /* records to be canceled */
143 int max_cookie; /** vector size */
144 int cookie_cnt; /** used count */
149 * llog_cat_process() callback, used to:
150 * - find waiting request and start action
151 * - purge canceled and done requests
152 * \param env [IN] environment
153 * \param llh [IN] llog handle
154 * \param hdr [IN] llog record
155 * \param data [IN/OUT] cb data = struct hsm_scan_data
157 * \retval -ve failure
159 static int mdt_coordinator_cb(const struct lu_env *env,
160 struct llog_handle *llh,
161 struct llog_rec_hdr *hdr,
164 const struct llog_agent_req_rec *larr;
165 struct hsm_scan_data *hsd;
166 struct hsm_action_item *hai;
167 struct mdt_device *mdt;
168 struct coordinator *cdt;
173 mdt = hsd->mti->mti_mdt;
174 cdt = &mdt->mdt_coordinator;
176 larr = (struct llog_agent_req_rec *)hdr;
177 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
178 switch (larr->arr_status) {
180 int i, empty_slot, found;
182 /* Are agents full? */
183 if (atomic_read(&cdt->cdt_request_count) ==
184 cdt->cdt_max_requests)
187 /* first search if the request if known in the list we have
188 * build and if there is room in the request vector */
191 for (i = 0; i < hsd->max_requests &&
192 (empty_slot == -1 || found == -1); i++) {
193 if (hsd->request[i].hal == NULL) {
197 if (hsd->request[i].hal->hal_compound_id ==
198 larr->arr_compound_id) {
203 if (found == -1 && empty_slot == -1)
204 /* unknown request and no more room for new request,
205 * continue scan for to find other entries for
206 * already found request
211 struct hsm_action_list *hal;
213 /* request is not already known */
214 /* allocates hai vector size just needs to be large
216 hsd->request[empty_slot].hal_sz =
217 sizeof(*hsd->request[empty_slot].hal) +
218 cfs_size_round(MTI_NAME_MAXLEN+1) +
219 2 * cfs_size_round(larr->arr_hai.hai_len);
220 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
222 CERROR("%s: Cannot allocate memory (%d o)"
223 "for compound "LPX64"\n",
225 hsd->request[i].hal_sz,
226 larr->arr_compound_id);
229 hal->hal_version = HAL_VERSION;
230 strlcpy(hal->hal_fsname, hsd->fs_name,
231 MTI_NAME_MAXLEN + 1);
232 hal->hal_compound_id = larr->arr_compound_id;
233 hal->hal_archive_id = larr->arr_archive_id;
234 hal->hal_flags = larr->arr_flags;
236 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
237 hsd->request[empty_slot].hal = hal;
240 hai = hai_first(hal);
242 /* request is known */
243 /* we check if record archive num is the same as the
244 * known request, if not we will serve it in multiple
245 * time because we do not know if the agent can serve
247 * a use case is a compound made of multiple restore
248 * where the files are not archived in the same backend
250 if (larr->arr_archive_id !=
251 hsd->request[found].hal->hal_archive_id)
254 if (hsd->request[found].hal_sz <
255 hsd->request[found].hal_used_sz +
256 cfs_size_round(larr->arr_hai.hai_len)) {
257 /* Not enough room, need an extension */
261 sz = 2 * hsd->request[found].hal_sz;
262 OBD_ALLOC(hal_buffer, sz);
264 CERROR("%s: Cannot allocate memory "
265 "(%d o) for compound "LPX64"\n",
266 mdt_obd_name(mdt), sz,
267 larr->arr_compound_id);
270 memcpy(hal_buffer, hsd->request[found].hal,
271 hsd->request[found].hal_used_sz);
272 OBD_FREE(hsd->request[found].hal,
273 hsd->request[found].hal_sz);
274 hsd->request[found].hal = hal_buffer;
275 hsd->request[found].hal_sz = sz;
277 hai = hai_first(hsd->request[found].hal);
278 for (i = 0; i < hsd->request[found].hal->hal_count;
282 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
283 hai->hai_cookie = larr->arr_hai.hai_cookie;
284 hai->hai_gid = larr->arr_hai.hai_gid;
286 hsd->request[found].hal_used_sz +=
287 cfs_size_round(hai->hai_len);
288 hsd->request[found].hal->hal_count++;
292 struct cdt_agent_req *car;
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 ((last + cdt->cdt_active_req_timeout)
310 < cfs_time_current_sec()) {
311 struct hsm_progress_kernel pgs;
313 dump_llog_agent_req_rec("mdt_coordinator_cb(): "
314 "request timeouted, start "
316 /* a too old cancel request just needs to be removed
317 * this can happen, if copy tool does not support cancel
318 * for other requests, we have to remove the running
319 * request and notify the copytool
321 pgs.hpk_fid = larr->arr_hai.hai_fid;
322 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
323 pgs.hpk_extent = larr->arr_hai.hai_extent;
324 pgs.hpk_flags = HP_FLAG_COMPLETED;
325 pgs.hpk_errval = ENOSYS;
326 pgs.hpk_data_version = 0;
327 /* update request state, but do not record in llog, to
328 * avoid deadlock on cdt_llog_lock
330 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
332 CERROR("%s: Cannot cleanup timeouted request: "
333 DFID" for cookie "LPX64" action=%s\n",
335 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
336 hsm_copytool_action2name(
337 larr->arr_hai.hai_action));
339 /* add the cookie to the list of record to be
340 * canceled by caller */
341 if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
342 __u64 *ptr, *old_ptr;
343 int old_sz, new_sz, new_cnt;
345 /* need to increase vector size */
346 old_sz = sizeof(__u64) * hsd->max_cookie;
347 old_ptr = hsd->cookies;
349 new_cnt = 2 * hsd->max_cookie;
350 new_sz = sizeof(__u64) * new_cnt;
352 OBD_ALLOC(ptr, new_sz);
354 CERROR("%s: Cannot allocate memory "
355 "(%d o) for cookie vector\n",
356 mdt_obd_name(mdt), new_sz);
359 memcpy(ptr, hsd->cookies, old_sz);
361 hsd->max_cookie = new_cnt;
362 OBD_FREE(old_ptr, old_sz);
364 hsd->cookies[hsd->cookie_cnt] =
365 larr->arr_hai.hai_cookie;
373 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
374 cfs_time_current_sec())
375 RETURN(LLOG_DEL_RECORD);
382 * create /proc entries for coordinator
385 * \retval -ve failure
387 int hsm_cdt_procfs_init(struct mdt_device *mdt)
389 struct coordinator *cdt = &mdt->mdt_coordinator;
393 /* init /proc entries, failure is not critical */
394 cdt->cdt_proc_dir = lprocfs_seq_register("hsm",
395 mdt2obd_dev(mdt)->obd_proc_entry,
396 lprocfs_mdt_hsm_vars, mdt);
397 if (IS_ERR(cdt->cdt_proc_dir)) {
398 rc = PTR_ERR(cdt->cdt_proc_dir);
399 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
400 " rc=%d\n", mdt_obd_name(mdt), rc);
401 cdt->cdt_proc_dir = NULL;
409 * remove /proc entries for coordinator
412 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
414 struct coordinator *cdt = &mdt->mdt_coordinator;
416 LASSERT(cdt->cdt_state == CDT_STOPPED);
417 if (cdt->cdt_proc_dir != NULL)
418 lprocfs_remove(&cdt->cdt_proc_dir);
422 * get vector of hsm cdt /proc vars
426 struct lprocfs_seq_vars *hsm_cdt_get_proc_vars(void)
428 return lprocfs_mdt_hsm_vars;
433 * \param data [IN] obd device
435 * \retval -ve failure
437 static int mdt_coordinator(void *data)
439 struct mdt_thread_info *mti = data;
440 struct mdt_device *mdt = mti->mti_mdt;
441 struct coordinator *cdt = &mdt->mdt_coordinator;
442 struct hsm_scan_data hsd = { 0 };
446 cdt->cdt_thread.t_flags = SVC_RUNNING;
447 wake_up(&cdt->cdt_thread.t_ctl_waitq);
449 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
450 mdt_obd_name(mdt), current_pid());
452 /* timeouted cookie vector initialization */
456 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
457 * increases due to a change from /proc we do not overflow the
458 * hsd.request[] vector
460 hsd.max_requests = cdt->cdt_max_requests;
461 hsd.request_sz = hsd.max_requests * sizeof(*hsd.request);
462 OBD_ALLOC(hsd.request, hsd.request_sz);
464 GOTO(out, rc = -ENOMEM);
467 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
470 struct l_wait_info lwi;
473 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
475 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
476 (cdt->cdt_thread.t_flags &
477 (SVC_STOPPING|SVC_EVENT)),
480 CDEBUG(D_HSM, "coordinator resumes\n");
482 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
483 cdt->cdt_state == CDT_STOPPING) {
484 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
489 /* wake up before timeout, new work arrives */
490 if (cdt->cdt_thread.t_flags & SVC_EVENT)
491 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
493 /* if coordinator is suspended continue to wait */
494 if (cdt->cdt_state == CDT_DISABLE) {
495 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
499 CDEBUG(D_HSM, "coordinator starts reading llog\n");
501 if (hsd.max_requests != cdt->cdt_max_requests) {
502 /* cdt_max_requests has changed,
503 * we need to allocate a new buffer
505 OBD_FREE(hsd.request, hsd.request_sz);
506 hsd.max_requests = cdt->cdt_max_requests;
508 hsd.max_requests * sizeof(*hsd.request);
509 OBD_ALLOC(hsd.request, hsd.request_sz);
516 /* create canceled cookie vector for an arbitrary size
517 * if needed, vector will grow during llog scan
521 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
528 rc = cdt_llog_process(mti->mti_env, mdt,
529 mdt_coordinator_cb, &hsd);
533 CDEBUG(D_HSM, "Found %d requests to send and %d"
534 " requests to cancel\n",
535 hsd.request_cnt, hsd.cookie_cnt);
536 /* first we cancel llog records of the timeouted requests */
537 if (hsd.cookie_cnt > 0) {
538 rc = mdt_agent_record_update(mti->mti_env, mdt,
543 CERROR("%s: mdt_agent_record_update() failed, "
544 "rc=%d, cannot update status to %s "
546 mdt_obd_name(mdt), rc,
547 agent_req_status2name(ARS_CANCELED),
551 if (list_empty(&cdt->cdt_agents)) {
552 CDEBUG(D_HSM, "no agent available, "
553 "coordinator sleeps\n");
557 /* here hsd contains a list of requests to be started */
558 for (i = 0; i < hsd.max_requests; i++) {
559 struct hsm_action_list *hal;
560 struct hsm_action_item *hai;
563 enum agent_req_status status;
565 /* still room for work ? */
566 if (atomic_read(&cdt->cdt_request_count) ==
567 cdt->cdt_max_requests)
570 if (hsd.request[i].hal == NULL)
573 /* found a request, we start it */
574 /* kuc payload allocation so we avoid an additionnal
575 * allocation in mdt_hsm_agent_send()
577 hal = kuc_alloc(hsd.request[i].hal_used_sz,
578 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
580 CERROR("%s: Cannot allocate memory (%d o) "
581 "for compound "LPX64"\n",
583 hsd.request[i].hal_used_sz,
584 hsd.request[i].hal->hal_compound_id);
587 memcpy(hal, hsd.request[i].hal,
588 hsd.request[i].hal_used_sz);
590 rc = mdt_hsm_agent_send(mti, hal, 0);
591 /* if failure, we suppose it is temporary
592 * if the copy tool failed to do the request
593 * it has to use hsm_progress
595 status = (rc ? ARS_WAITING : ARS_STARTED);
597 /* set up cookie vector to set records status
598 * after copy tools start or failed
600 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
601 OBD_ALLOC(cookies, sz);
602 if (cookies == NULL) {
603 CERROR("%s: Cannot allocate memory (%d o) "
604 "for cookies vector "LPX64"\n",
605 mdt_obd_name(mdt), sz,
606 hsd.request[i].hal->hal_compound_id);
607 kuc_free(hal, hsd.request[i].hal_used_sz);
610 hai = hai_first(hal);
611 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
612 cookies[j] = hai->hai_cookie;
616 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
617 hsd.request[i].hal->hal_count,
620 CERROR("%s: mdt_agent_record_update() failed, "
621 "rc=%d, cannot update status to %s "
623 mdt_obd_name(mdt), rc,
624 agent_req_status2name(status),
625 hsd.request[i].hal->hal_count);
627 OBD_FREE(cookies, sz);
628 kuc_free(hal, hsd.request[i].hal_used_sz);
631 /* free cookie vector allocated for/by callback */
633 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
639 /* free hal allocated by callback */
640 for (i = 0; i < hsd.max_requests; i++) {
641 if (hsd.request[i].hal) {
642 OBD_FREE(hsd.request[i].hal,
643 hsd.request[i].hal_sz);
644 hsd.request[i].hal_sz = 0;
645 hsd.request[i].hal = NULL;
649 LASSERT(hsd.request_cnt == 0);
651 /* reset callback data */
652 memset(hsd.request, 0, hsd.request_sz);
657 OBD_FREE(hsd.request, hsd.request_sz);
660 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
662 if (cdt->cdt_state == CDT_STOPPING) {
663 /* request comes from /proc path, so we need to clean cdt
665 mdt_hsm_cdt_stop(mdt);
666 mdt->mdt_opts.mo_coordinator = 0;
668 /* request comes from a thread event, generated
669 * by mdt_stop_coordinator(), we have to ack
670 * and cdt cleaning will be done by event sender
672 cdt->cdt_thread.t_flags = SVC_STOPPED;
673 wake_up(&cdt->cdt_thread.t_ctl_waitq);
677 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
678 mdt_obd_name(mdt), current_pid(), rc);
680 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
682 mdt_obd_name(mdt), current_pid());
688 * lookup a restore handle by FID
689 * caller needs to hold cdt_restore_lock
690 * \param cdt [IN] coordinator
691 * \param fid [IN] FID
692 * \retval cdt_restore_handle found
693 * \retval NULL not found
695 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
696 const struct lu_fid *fid)
698 struct cdt_restore_handle *crh;
701 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
702 if (lu_fid_eq(&crh->crh_fid, fid))
709 * data passed to llog_cat_process() callback
710 * to scan requests and take actions
712 struct hsm_restore_data {
713 struct mdt_thread_info *hrd_mti;
717 * llog_cat_process() callback, used to:
718 * - find restore request and allocate the restore handle
719 * \param env [IN] environment
720 * \param llh [IN] llog handle
721 * \param hdr [IN] llog record
722 * \param data [IN/OUT] cb data = struct hsm_restore_data
724 * \retval -ve failure
726 static int hsm_restore_cb(const struct lu_env *env,
727 struct llog_handle *llh,
728 struct llog_rec_hdr *hdr, void *data)
730 struct llog_agent_req_rec *larr;
731 struct hsm_restore_data *hrd;
732 struct cdt_restore_handle *crh;
733 struct hsm_action_item *hai;
734 struct mdt_thread_info *mti;
735 struct coordinator *cdt;
736 struct mdt_object *child;
742 cdt = &mti->mti_mdt->mdt_coordinator;
744 larr = (struct llog_agent_req_rec *)hdr;
745 hai = &larr->arr_hai;
746 if (hai->hai_cookie > cdt->cdt_last_cookie)
747 /* update the cookie to avoid collision */
748 cdt->cdt_last_cookie = hai->hai_cookie + 1;
750 if (hai->hai_action != HSMA_RESTORE ||
751 agent_req_in_final_state(larr->arr_status))
754 /* restore request not in a final state */
756 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
760 crh->crh_fid = hai->hai_fid;
761 /* in V1 all file is restored
762 crh->extent.start = hai->hai_extent.offset;
763 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
765 crh->crh_extent.start = 0;
766 crh->crh_extent.end = hai->hai_extent.length;
767 /* get the layout lock */
768 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
769 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
770 MDS_INODELOCK_LAYOUT);
772 GOTO(out, rc = PTR_ERR(child));
775 /* we choose to not keep a reference
776 * on the object during the restore time which can be very long */
777 mdt_object_put(mti->mti_env, child);
779 mutex_lock(&cdt->cdt_restore_lock);
780 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
781 mutex_unlock(&cdt->cdt_restore_lock);
788 * restore coordinator state at startup
789 * the goal is to take a layout lock for each registered restore request
790 * \param mti [IN] context
792 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
794 struct hsm_restore_data hrd;
800 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
801 hsm_restore_cb, &hrd);
806 static int hsm_init_ucred(struct lu_ucred *uc)
810 uc->uc_valid = UCRED_OLD;
819 uc->uc_suppgids[0] = -1;
820 uc->uc_suppgids[1] = -1;
821 uc->uc_cap = CFS_CAP_FS_MASK;
824 uc->uc_identity = NULL;
830 * wake up coordinator thread
831 * \param mdt [IN] device
833 * \retval -ve failure
835 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
837 struct coordinator *cdt = &mdt->mdt_coordinator;
840 if (cdt->cdt_state == CDT_STOPPED)
843 /* wake up coordinator */
844 cdt->cdt_thread.t_flags = SVC_EVENT;
845 wake_up(&cdt->cdt_thread.t_ctl_waitq);
851 * initialize coordinator struct
852 * \param mdt [IN] device
854 * \retval -ve failure
856 int mdt_hsm_cdt_init(struct mdt_device *mdt)
858 struct coordinator *cdt = &mdt->mdt_coordinator;
859 struct mdt_thread_info *cdt_mti = NULL;
863 cdt->cdt_state = CDT_STOPPED;
865 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
866 mutex_init(&cdt->cdt_llog_lock);
867 init_rwsem(&cdt->cdt_agent_lock);
868 init_rwsem(&cdt->cdt_request_lock);
869 mutex_init(&cdt->cdt_restore_lock);
871 CFS_INIT_LIST_HEAD(&cdt->cdt_requests);
872 CFS_INIT_LIST_HEAD(&cdt->cdt_agents);
873 CFS_INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
875 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
879 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
880 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
882 lu_context_enter(&cdt->cdt_session);
883 cdt->cdt_env.le_ses = &cdt->cdt_session;
885 lu_env_fini(&cdt->cdt_env);
889 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
890 LASSERT(cdt_mti != NULL);
892 cdt_mti->mti_env = &cdt->cdt_env;
893 cdt_mti->mti_mdt = mdt;
895 hsm_init_ucred(mdt_ucred(cdt_mti));
897 /* default values for /proc tunnables
898 * can be override by MGS conf */
899 cdt->cdt_default_archive_id = 1;
900 cdt->cdt_grace_delay = 60;
901 cdt->cdt_loop_period = 10;
902 cdt->cdt_max_requests = 3;
903 cdt->cdt_policy = CDT_DEFAULT_POLICY;
904 cdt->cdt_active_req_timeout = 3600;
910 * free a coordinator thread
911 * \param mdt [IN] device
913 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
915 struct coordinator *cdt = &mdt->mdt_coordinator;
918 lu_context_exit(cdt->cdt_env.le_ses);
919 lu_context_fini(cdt->cdt_env.le_ses);
921 lu_env_fini(&cdt->cdt_env);
927 * start a coordinator thread
928 * \param mdt [IN] device
930 * \retval -ve failure
932 int mdt_hsm_cdt_start(struct mdt_device *mdt)
934 struct coordinator *cdt = &mdt->mdt_coordinator;
937 struct mdt_thread_info *cdt_mti;
938 struct task_struct *task;
941 /* functions defined but not yet used
942 * this avoid compilation warning
946 if (cdt->cdt_state != CDT_STOPPED) {
947 CERROR("%s: Coordinator already started\n",
952 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
953 cdt->cdt_policy = CDT_DEFAULT_POLICY;
955 cdt->cdt_state = CDT_INIT;
957 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
958 /* just need to be larger than previous one */
959 /* cdt_last_cookie is protected by cdt_llog_lock */
960 cdt->cdt_last_cookie = cfs_time_current_sec();
961 atomic_set(&cdt->cdt_request_count, 0);
962 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
963 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
964 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
966 /* to avoid deadlock when start is made through /proc
967 * /proc entries are created by the coordinator thread */
969 /* set up list of started restore requests */
970 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
971 rc = mdt_hsm_pending_restore(cdt_mti);
973 CERROR("%s: cannot take the layout locks needed"
974 " for registered restore: %d\n",
975 mdt_obd_name(mdt), rc);
977 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
980 cdt->cdt_state = CDT_STOPPED;
981 CERROR("%s: error starting coordinator thread: %d\n",
982 mdt_obd_name(mdt), rc);
985 CDEBUG(D_HSM, "%s: coordinator thread started\n",
990 wait_event(cdt->cdt_thread.t_ctl_waitq,
991 (cdt->cdt_thread.t_flags & SVC_RUNNING));
993 cdt->cdt_state = CDT_RUNNING;
994 mdt->mdt_opts.mo_coordinator = 1;
999 * stop a coordinator thread
1000 * \param mdt [IN] device
1002 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1004 struct coordinator *cdt = &mdt->mdt_coordinator;
1005 struct cdt_agent_req *car, *tmp1;
1006 struct hsm_agent *ha, *tmp2;
1007 struct cdt_restore_handle *crh, *tmp3;
1008 struct mdt_thread_info *cdt_mti;
1011 if (cdt->cdt_state == CDT_STOPPED) {
1012 CERROR("%s: Coordinator already stopped\n",
1017 if (cdt->cdt_state != CDT_STOPPING) {
1018 /* stop coordinator thread before cleaning */
1019 cdt->cdt_thread.t_flags = SVC_STOPPING;
1020 wake_up(&cdt->cdt_thread.t_ctl_waitq);
1021 wait_event(cdt->cdt_thread.t_ctl_waitq,
1022 cdt->cdt_thread.t_flags & SVC_STOPPED);
1024 cdt->cdt_state = CDT_STOPPED;
1026 /* start cleaning */
1027 down_write(&cdt->cdt_request_lock);
1028 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1030 list_del(&car->car_request_list);
1031 mdt_cdt_free_request(car);
1033 up_write(&cdt->cdt_request_lock);
1035 down_write(&cdt->cdt_agent_lock);
1036 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1037 list_del(&ha->ha_list);
1040 up_write(&cdt->cdt_agent_lock);
1042 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1043 mutex_lock(&cdt->cdt_restore_lock);
1044 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1045 struct mdt_object *child;
1047 /* give back layout lock */
1048 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1050 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1052 list_del(&crh->crh_list);
1054 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1056 mutex_unlock(&cdt->cdt_restore_lock);
1058 mdt->mdt_opts.mo_coordinator = 0;
1064 * register all requests from an hal in the memory list
1065 * \param mti [IN] context
1066 * \param hal [IN] request
1067 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1068 * which is running the CT
1070 * \retval -ve failure
1072 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1073 struct hsm_action_list *hal, struct obd_uuid *uuid)
1075 struct mdt_device *mdt = mti->mti_mdt;
1076 struct coordinator *cdt = &mdt->mdt_coordinator;
1077 struct hsm_action_item *hai;
1081 /* register request in memory list */
1082 hai = hai_first(hal);
1083 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1084 struct cdt_agent_req *car;
1086 /* in case of a cancel request, we first mark the ondisk
1087 * record of the request we want to stop as canceled
1088 * this does not change the cancel record
1089 * it will be done when updating the request status
1091 if (hai->hai_action == HSMA_CANCEL) {
1092 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1096 CERROR("%s: mdt_agent_record_update() failed, "
1097 "rc=%d, cannot update status to %s "
1098 "for cookie "LPX64"\n",
1099 mdt_obd_name(mdt), rc,
1100 agent_req_status2name(ARS_CANCELED),
1105 /* find the running request to set it canceled */
1106 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1108 car->car_canceled = 1;
1109 /* uuid has to be changed to the one running the
1110 * request to cancel */
1111 *uuid = car->car_uuid;
1112 mdt_cdt_put_request(car);
1114 /* no need to memorize cancel request
1115 * this also avoid a deadlock when we receive
1116 * a purge all requests command
1121 if (hai->hai_action == HSMA_ARCHIVE) {
1122 struct mdt_object *obj;
1125 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1126 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1129 GOTO(out, rc = PTR_ERR(obj));
1131 hsm.mh_flags |= HS_EXISTS;
1132 hsm.mh_arch_id = hal->hal_archive_id;
1133 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1134 mdt_object_put(mti->mti_env, obj);
1139 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1140 hal->hal_archive_id, hal->hal_flags,
1143 GOTO(out, rc = PTR_ERR(car));
1145 rc = mdt_cdt_add_request(cdt, car);
1147 mdt_cdt_free_request(car);
1154 * swap layouts between 2 fids
1155 * \param mti [IN] context
1158 * \param mh_common [IN] MD HSM
1160 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1161 const lustre_fid *fid, const lustre_fid *dfid,
1162 struct md_hsm *mh_common)
1164 struct mdt_device *mdt = mti->mti_mdt;
1165 struct mdt_object *child1, *child2;
1166 struct mdt_lock_handle *lh2;
1170 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1172 GOTO(out, rc = PTR_ERR(child1));
1174 /* we already have layout lock on FID so take only
1176 lh2 = &mti->mti_lh[MDT_LH_OLD];
1177 mdt_lock_reg_init(lh2, LCK_EX);
1178 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1180 GOTO(out_child1, rc = PTR_ERR(child2));
1182 /* if copy tool closes the volatile before sending the final
1183 * progress through llapi_hsm_copy_end(), all the objects
1184 * are removed and mdd_swap_layout LBUG */
1185 if (!mdt_object_exists(child2)) {
1186 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1187 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1188 GOTO(out_child2, rc = -ENOENT);
1190 /* Since we only handle restores here, unconditionally use
1191 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1192 * be preserved in case of failure during swap_layout and not
1193 * leave a file in an intermediate but incoherent state.
1194 * But need to setup HSM xattr of data FID before, reuse
1195 * mti and mh presets for FID in hsm_cdt_request_completed(),
1196 * only need to clear RELEASED and DIRTY.
1198 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1199 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1201 rc = mo_swap_layouts(mti->mti_env,
1202 mdt_object_child(child1),
1203 mdt_object_child(child2),
1204 SWAP_LAYOUTS_MDS_HSM);
1207 mdt_object_unlock_put(mti, child2, lh2, 1);
1209 mdt_object_put(mti->mti_env, child1);
1215 * update status of a completed request
1216 * \param mti [IN] context
1217 * \param pgs [IN] progress of the copy tool
1218 * \param update_record [IN] update llog record
1220 * \retval -ve failure
1222 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1223 struct hsm_progress_kernel *pgs,
1224 const struct cdt_agent_req *car,
1225 enum agent_req_status *status)
1227 const struct lu_env *env = mti->mti_env;
1228 struct mdt_device *mdt = mti->mti_mdt;
1229 struct coordinator *cdt = &mdt->mdt_coordinator;
1230 struct mdt_object *obj = NULL;
1231 int cl_flags = 0, rc = 0;
1236 /* default is to retry */
1237 *status = ARS_WAITING;
1239 /* find object by FID */
1240 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1241 /* we will update MD HSM only if needed */
1242 is_mh_changed = false;
1244 /* object removed */
1245 *status = ARS_SUCCEED;
1249 /* no need to change mh->mh_arch_id
1250 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1252 if (pgs->hpk_errval != 0) {
1253 switch (pgs->hpk_errval) {
1255 /* the copy tool does not support cancel
1256 * so the cancel request is failed
1257 * As we cannot distinguish a cancel progress
1258 * from another action progress (they have the
1259 * same cookie), we suppose here the CT returns
1260 * ENOSYS only if does not support cancel
1262 /* this can also happen when cdt calls it to
1263 * for a timeouted request */
1264 *status = ARS_FAILED;
1265 /* to have a cancel event in changelog */
1266 pgs->hpk_errval = ECANCELED;
1269 /* the request record has already been set to
1270 * ARS_CANCELED, this set the cancel request
1272 *status = ARS_SUCCEED;
1275 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1276 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1277 ARS_FAILED : ARS_WAITING);
1281 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1282 CERROR("%s: Request "LPX64" on "DFID
1283 " failed, error code %d too large\n",
1285 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1287 hsm_set_cl_error(&cl_flags,
1288 CLF_HSM_ERROVERFLOW);
1291 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1294 switch (car->car_hai->hai_action) {
1296 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1299 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1302 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1305 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1306 CERROR("%s: Failed request "LPX64" on "DFID
1307 " cannot be a CANCEL\n",
1310 PFID(&pgs->hpk_fid));
1313 CERROR("%s: Failed request "LPX64" on "DFID
1314 " %d is an unknown action\n",
1316 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1317 car->car_hai->hai_action);
1322 *status = ARS_SUCCEED;
1323 switch (car->car_hai->hai_action) {
1325 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1326 /* set ARCHIVE keep EXIST and clear LOST and
1328 mh.mh_arch_ver = pgs->hpk_data_version;
1329 mh.mh_flags |= HS_ARCHIVED;
1330 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1331 is_mh_changed = true;
1334 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1336 /* do not clear RELEASED and DIRTY here
1337 * this will occur in hsm_swap_layouts()
1340 /* Restoring has changed the file version on
1342 mh.mh_arch_ver = pgs->hpk_data_version;
1343 is_mh_changed = true;
1346 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1347 /* clear ARCHIVED EXISTS and LOST */
1348 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1349 is_mh_changed = true;
1352 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1353 CERROR("%s: Successful request "LPX64
1355 " cannot be a CANCEL\n",
1358 PFID(&pgs->hpk_fid));
1361 CERROR("%s: Successful request "LPX64
1363 " %d is an unknown action\n",
1365 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1366 car->car_hai->hai_action);
1372 /* rc != 0 means error when analysing action, it may come from
1373 * a crasy CT no need to manage DIRTY
1376 hsm_set_cl_flags(&cl_flags,
1377 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1379 /* unlock is done later, after layout lock management */
1381 rc = mdt_hsm_attr_set(mti, obj, &mh);
1384 /* we give back layout lock only if restore was successful or
1385 * if restore was canceled or if policy is to not retry
1386 * in other cases we just unlock the object */
1387 if (car->car_hai->hai_action == HSMA_RESTORE &&
1388 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1389 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1390 struct cdt_restore_handle *crh;
1392 /* restore in data FID done, we swap the layouts
1393 * only if restore is successfull */
1394 if (pgs->hpk_errval == 0) {
1395 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1396 &car->car_hai->hai_dfid, &mh);
1398 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1399 *status = ARS_FAILED;
1400 pgs->hpk_errval = -rc;
1403 /* we have to retry, so keep layout lock */
1404 if (*status == ARS_WAITING)
1407 /* give back layout lock */
1408 mutex_lock(&cdt->cdt_restore_lock);
1409 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1411 list_del(&crh->crh_list);
1412 mutex_unlock(&cdt->cdt_restore_lock);
1413 /* just give back layout lock, we keep
1414 * the reference which is given back
1415 * later with the lock for HSM flags */
1416 if (!IS_ERR(obj) && crh != NULL)
1417 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1420 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1426 if (obj != NULL && !IS_ERR(obj)) {
1427 mo_changelog(env, CL_HSM, cl_flags,
1428 mdt_object_child(obj));
1429 mdt_object_put(mti->mti_env, obj);
1436 * update status of a request
1437 * \param mti [IN] context
1438 * \param pgs [IN] progress of the copy tool
1439 * \param update_record [IN] update llog record
1441 * \retval -ve failure
1443 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1444 struct hsm_progress_kernel *pgs,
1445 const int update_record)
1447 struct mdt_device *mdt = mti->mti_mdt;
1448 struct coordinator *cdt = &mdt->mdt_coordinator;
1449 struct cdt_agent_req *car;
1453 /* no coordinator started, so we cannot serve requests */
1454 if (cdt->cdt_state == CDT_STOPPED)
1457 /* first do sanity checks */
1458 car = mdt_cdt_update_request(cdt, pgs);
1460 CERROR("%s: Cannot find running request for cookie "LPX64
1463 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1466 RETURN(PTR_ERR(car));
1469 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1470 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1471 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1472 hsm_copytool_action2name(car->car_hai->hai_action),
1473 pgs->hpk_flags, pgs->hpk_errval,
1474 PFID(&car->car_hai->hai_fid),
1475 PFID(&car->car_hai->hai_dfid));
1477 /* progress is done on FID or data FID depending of the action and
1478 * of the copy progress */
1479 /* for restore progress is used to send back the data FID to cdt */
1480 if (car->car_hai->hai_action == HSMA_RESTORE &&
1481 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1482 car->car_hai->hai_dfid = pgs->hpk_fid;
1484 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1485 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1486 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1487 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1488 CERROR("%s: Progress on "DFID" for cookie "LPX64
1489 " does not match request FID "DFID" nor data FID "
1492 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1493 PFID(&car->car_hai->hai_fid),
1494 PFID(&car->car_hai->hai_dfid));
1495 GOTO(out, rc = -EINVAL);
1498 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1499 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1500 " is not coherent (err=%d and not completed"
1503 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1504 hsm_copytool_action2name(car->car_hai->hai_action),
1505 pgs->hpk_errval, pgs->hpk_flags);
1506 GOTO(out, rc = -EINVAL);
1509 /* now progress is valid */
1511 /* we use a root like ucred */
1512 hsm_init_ucred(mdt_ucred(mti));
1514 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1515 enum agent_req_status status;
1517 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1519 /* remove request from memory list */
1520 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1522 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1523 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1525 hsm_copytool_action2name(car->car_hai->hai_action),
1526 agent_req_status2name(status));
1528 if (update_record) {
1531 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1532 &pgs->hpk_cookie, 1,
1535 CERROR("%s: mdt_agent_record_update() failed,"
1536 " rc=%d, cannot update status to %s"
1537 " for cookie "LPX64"\n",
1538 mdt_obd_name(mdt), rc1,
1539 agent_req_status2name(status),
1541 rc = (rc != 0 ? rc : rc1);
1543 /* ct has completed a request, so a slot is available, wakeup
1544 * cdt to find new work */
1545 mdt_hsm_cdt_wakeup(mdt);
1547 /* if copytool send a progress on a canceled request
1548 * we inform copytool it should stop
1550 if (car->car_canceled == 1)
1556 /* remove ref got from mdt_cdt_update_request() */
1557 mdt_cdt_put_request(car);
1564 * data passed to llog_cat_process() callback
1565 * to cancel requests
1567 struct hsm_cancel_all_data {
1568 struct mdt_device *mdt;
1572 * llog_cat_process() callback, used to:
1573 * - purge all requests
1574 * \param env [IN] environment
1575 * \param llh [IN] llog handle
1576 * \param hdr [IN] llog record
1577 * \param data [IN] cb data = struct hsm_cancel_all_data
1579 * \retval -ve failure
1581 static int mdt_cancel_all_cb(const struct lu_env *env,
1582 struct llog_handle *llh,
1583 struct llog_rec_hdr *hdr, void *data)
1585 struct llog_agent_req_rec *larr;
1586 struct hsm_cancel_all_data *hcad;
1590 larr = (struct llog_agent_req_rec *)hdr;
1592 if (larr->arr_status == ARS_WAITING ||
1593 larr->arr_status == ARS_STARTED) {
1594 larr->arr_status = ARS_CANCELED;
1595 larr->arr_req_change = cfs_time_current_sec();
1596 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1598 RETURN(LLOG_DEL_RECORD);
1604 * cancel all actions
1605 * \param obd [IN] MDT device
1607 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1609 struct mdt_thread_info *mti;
1610 struct coordinator *cdt = &mdt->mdt_coordinator;
1611 struct cdt_agent_req *car;
1612 struct hsm_action_list *hal = NULL;
1613 struct hsm_action_item *hai;
1614 struct hsm_cancel_all_data hcad;
1615 int hal_sz = 0, hal_len, rc;
1616 enum cdt_states save_state;
1619 /* retrieve coordinator context */
1620 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1622 /* disable coordinator */
1623 save_state = cdt->cdt_state;
1624 cdt->cdt_state = CDT_DISABLE;
1626 /* send cancel to all running requests */
1627 down_read(&cdt->cdt_request_lock);
1628 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1629 mdt_cdt_get_request(car);
1630 /* request is not yet removed from list, it will be done
1631 * when copytool will return progress
1634 if (car->car_hai->hai_action == HSMA_CANCEL) {
1635 mdt_cdt_put_request(car);
1640 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1641 cfs_size_round(car->car_hai->hai_len);
1643 if (hal_len > hal_sz && hal_sz > 0) {
1644 /* not enough room, free old buffer */
1645 OBD_FREE(hal, hal_sz);
1649 /* empty buffer, allocate one */
1652 OBD_ALLOC(hal, hal_sz);
1654 mdt_cdt_put_request(car);
1655 up_read(&cdt->cdt_request_lock);
1656 GOTO(out, rc = -ENOMEM);
1660 hal->hal_version = HAL_VERSION;
1661 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1663 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1664 hal->hal_compound_id = car->car_compound_id;
1665 hal->hal_archive_id = car->car_archive_id;
1666 hal->hal_flags = car->car_flags;
1669 hai = hai_first(hal);
1670 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1671 hai->hai_action = HSMA_CANCEL;
1674 /* it is possible to safely call mdt_hsm_agent_send()
1675 * (ie without a deadlock on cdt_request_lock), because the
1676 * write lock is taken only if we are not in purge mode
1677 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1678 * nor mdt_cdt_remove_request())
1680 /* no conflict with cdt thread because cdt is disable and we
1681 * have the request lock */
1682 mdt_hsm_agent_send(mti, hal, 1);
1684 mdt_cdt_put_request(car);
1686 up_read(&cdt->cdt_request_lock);
1689 OBD_FREE(hal, hal_sz);
1691 /* cancel all on-disk records */
1694 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1695 mdt_cancel_all_cb, &hcad);
1697 /* enable coordinator */
1698 cdt->cdt_state = save_state;
1704 * check if a request is comptaible with file status
1705 * \param hai [IN] request description
1706 * \param hal_an [IN] request archive number (not used)
1707 * \param rq_flags [IN] request flags
1708 * \param hsm [IN] file HSM metadata
1711 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1712 const int hal_an, const __u64 rq_flags,
1713 const struct md_hsm *hsm)
1715 int is_compat = false;
1719 hsm_flags = hsm->mh_flags;
1720 switch (hai->hai_action) {
1722 if (!(hsm_flags & HS_NOARCHIVE) &&
1723 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1727 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1728 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1732 if (!(hsm_flags & HS_RELEASED) &&
1733 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1740 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1741 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1742 PFID(&hai->hai_fid),
1743 hsm_copytool_action2name(hai->hai_action), rq_flags,
1744 hai->hai_extent.offset, hai->hai_extent.length,
1746 (is_compat ? "compatible" : "uncompatible"));
1752 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1754 static const struct {
1758 } hsm_policy_names[] = {
1759 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1760 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1765 * convert a policy name to a bit
1766 * \param name [IN] policy name
1768 * \retval policy bit
1770 static __u64 hsm_policy_str2bit(const char *name)
1774 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1775 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1776 strcmp(hsm_policy_names[i].name, name) == 0)
1777 return hsm_policy_names[i].bit;
1782 * convert a policy bit field to a string
1783 * \param mask [IN] policy bit field
1784 * \param hexa [IN] print mask before bit names
1785 * \param buffer [OUT] string
1786 * \param count [IN] size of buffer
1788 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1796 seq_printf(m, "("LPX64") ", mask);
1798 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1801 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1802 if (hsm_policy_names[j].bit == bit)
1806 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1808 seq_printf(m, "%s ", hsm_policy_names[j].name);
1810 /* remove last ' ' */
1815 /* methods to read/write HSM policy flags */
1816 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1818 struct mdt_device *mdt = m->private;
1819 struct coordinator *cdt = &mdt->mdt_coordinator;
1822 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1827 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1828 size_t count, loff_t *off)
1830 struct seq_file *m = file->private_data;
1831 struct mdt_device *mdt = m->private;
1832 struct coordinator *cdt = &mdt->mdt_coordinator;
1833 char *start, *token, sign;
1836 __u64 add_mask, remove_mask, set_mask;
1840 if (count + 1 > PAGE_SIZE)
1843 OBD_ALLOC(buf, count + 1);
1847 if (copy_from_user(buf, buffer, count))
1848 GOTO(out, rc = -EFAULT);
1853 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1856 add_mask = remove_mask = set_mask = 0;
1858 token = strsep(&start, "\n ");
1864 if (sign == '-' || sign == '+')
1867 policy = hsm_policy_str2bit(token);
1869 CWARN("%s: '%s' is unknown, "
1870 "supported policies are:\n", mdt_obd_name(mdt),
1872 hsm_policy_bit2str(m, 0, false);
1873 GOTO(out, rc = -EINVAL);
1877 remove_mask |= policy;
1887 } while (start != NULL);
1889 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1890 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1892 /* if no sign in all string, it is a clear and set
1893 * if some sign found, all unsigned are converted
1895 * P1 P2 = set to P1 and P2
1896 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1898 if (remove_mask == 0 && add_mask == 0) {
1899 cdt->cdt_policy = set_mask;
1901 cdt->cdt_policy |= set_mask | add_mask;
1902 cdt->cdt_policy &= ~remove_mask;
1905 GOTO(out, rc = count);
1908 OBD_FREE(buf, count + 1);
1911 LPROC_SEQ_FOPS(mdt_hsm_policy);
1913 #define GENERATE_PROC_METHOD(VAR) \
1914 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1916 struct mdt_device *mdt = m->private; \
1917 struct coordinator *cdt = &mdt->mdt_coordinator; \
1920 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1924 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1925 size_t count, loff_t *off) \
1928 struct seq_file *m = file->private_data; \
1929 struct mdt_device *mdt = m->private; \
1930 struct coordinator *cdt = &mdt->mdt_coordinator; \
1935 rc = lprocfs_write_helper(buffer, count, &val); \
1945 GENERATE_PROC_METHOD(cdt_loop_period)
1946 GENERATE_PROC_METHOD(cdt_grace_delay)
1947 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1948 GENERATE_PROC_METHOD(cdt_max_requests)
1949 GENERATE_PROC_METHOD(cdt_default_archive_id)
1952 * procfs write method for MDT/hsm_control
1953 * proc entry is in mdt directory so data is mdt obd_device pointer
1955 #define CDT_ENABLE_CMD "enabled"
1956 #define CDT_STOP_CMD "shutdown"
1957 #define CDT_DISABLE_CMD "disabled"
1958 #define CDT_PURGE_CMD "purge"
1959 #define CDT_HELP_CMD "help"
1960 #define CDT_MAX_CMD_LEN 10
1963 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1964 size_t count, loff_t *off)
1966 struct seq_file *m = file->private_data;
1967 struct obd_device *obd = m->private;
1968 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1969 struct coordinator *cdt = &(mdt->mdt_coordinator);
1971 char kernbuf[CDT_MAX_CMD_LEN];
1974 if (count == 0 || count >= sizeof(kernbuf))
1977 if (copy_from_user(kernbuf, buffer, count))
1981 if (kernbuf[count - 1] == '\n')
1982 kernbuf[count - 1] = 0;
1985 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1986 if (cdt->cdt_state == CDT_DISABLE) {
1987 cdt->cdt_state = CDT_RUNNING;
1988 mdt_hsm_cdt_wakeup(mdt);
1990 rc = mdt_hsm_cdt_start(mdt);
1992 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1993 if ((cdt->cdt_state == CDT_STOPPING) ||
1994 (cdt->cdt_state == CDT_STOPPED)) {
1995 CERROR("%s: Coordinator already stopped\n",
1999 cdt->cdt_state = CDT_STOPPING;
2001 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2002 if ((cdt->cdt_state == CDT_STOPPING) ||
2003 (cdt->cdt_state == CDT_STOPPED)) {
2004 CERROR("%s: Coordinator is stopped\n",
2008 cdt->cdt_state = CDT_DISABLE;
2010 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2011 rc = hsm_cancel_all_actions(mdt);
2012 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2020 CERROR("%s: Valid coordinator control commands are: "
2021 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2022 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2023 CDT_PURGE_CMD, CDT_HELP_CMD);
2031 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2033 struct obd_device *obd = m->private;
2034 struct coordinator *cdt;
2037 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2039 if (cdt->cdt_state == CDT_INIT)
2040 seq_printf(m, "init\n");
2041 else if (cdt->cdt_state == CDT_RUNNING)
2042 seq_printf(m, "enabled\n");
2043 else if (cdt->cdt_state == CDT_STOPPING)
2044 seq_printf(m, "stopping\n");
2045 else if (cdt->cdt_state == CDT_STOPPED)
2046 seq_printf(m, "stopped\n");
2047 else if (cdt->cdt_state == CDT_DISABLE)
2048 seq_printf(m, "disabled\n");
2050 seq_printf(m, "unknown\n");
2056 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2061 for (i = 0; i < 8 * sizeof(mask); i++) {
2062 if (mask & (1UL << i))
2063 rc += seq_printf(m, "%s%s", rc == 0 ? "" : " ",
2064 hsm_copytool_action2name(i));
2066 rc += seq_printf(m, "\n");
2072 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2074 struct mdt_device *mdt = m->private;
2075 struct coordinator *cdt = &mdt->mdt_coordinator;
2077 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2081 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2083 struct mdt_device *mdt = m->private;
2084 struct coordinator *cdt = &mdt->mdt_coordinator;
2086 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2090 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2092 struct mdt_device *mdt = m->private;
2093 struct coordinator *cdt = &mdt->mdt_coordinator;
2095 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2098 static inline enum hsm_copytool_action
2099 hsm_copytool_name2action(const char *name)
2101 if (strcasecmp(name, "NOOP") == 0)
2103 else if (strcasecmp(name, "ARCHIVE") == 0)
2104 return HSMA_ARCHIVE;
2105 else if (strcasecmp(name, "RESTORE") == 0)
2106 return HSMA_RESTORE;
2107 else if (strcasecmp(name, "REMOVE") == 0)
2109 else if (strcasecmp(name, "CANCEL") == 0)
2116 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2117 size_t user_count, __u64 *mask)
2119 char *buf, *pos, *name;
2125 if (!(user_count < 4096))
2128 buf_size = user_count + 1;
2130 OBD_ALLOC(buf, buf_size);
2134 if (copy_from_user(buf, user_buf, buf_size - 1))
2135 GOTO(out, rc = -EFAULT);
2137 buf[buf_size - 1] = '\0';
2140 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2146 action = hsm_copytool_name2action(name);
2148 GOTO(out, rc = -EINVAL);
2150 new_mask |= (1UL << action);
2156 OBD_FREE(buf, buf_size);
2162 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2163 size_t count, loff_t *off)
2165 struct seq_file *m = file->private_data;
2166 struct mdt_device *mdt = m->private;
2167 struct coordinator *cdt = &mdt->mdt_coordinator;
2169 return mdt_write_hsm_request_mask(file, buf, count,
2170 &cdt->cdt_user_request_mask);
2174 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2175 size_t count, loff_t *off)
2177 struct seq_file *m = file->private_data;
2178 struct mdt_device *mdt = m->private;
2179 struct coordinator *cdt = &mdt->mdt_coordinator;
2181 return mdt_write_hsm_request_mask(file, buf, count,
2182 &cdt->cdt_group_request_mask);
2186 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2187 size_t count, loff_t *off)
2189 struct seq_file *m = file->private_data;
2190 struct mdt_device *mdt = m->private;
2191 struct coordinator *cdt = &mdt->mdt_coordinator;
2193 return mdt_write_hsm_request_mask(file, buf, count,
2194 &cdt->cdt_other_request_mask);
2197 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2198 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2199 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2200 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2201 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2202 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2203 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2204 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2206 static struct lprocfs_seq_vars lprocfs_mdt_hsm_vars[] = {
2208 .fops = &mdt_hsm_agent_fops },
2209 { .name = "actions",
2210 .fops = &mdt_hsm_actions_fops,
2211 .proc_mode = 0444 },
2212 { .name = "default_archive_id",
2213 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2214 { .name = "grace_delay",
2215 .fops = &mdt_hsm_cdt_grace_delay_fops },
2216 { .name = "loop_period",
2217 .fops = &mdt_hsm_cdt_loop_period_fops },
2218 { .name = "max_requests",
2219 .fops = &mdt_hsm_cdt_max_requests_fops },
2221 .fops = &mdt_hsm_policy_fops },
2222 { .name = "active_request_timeout",
2223 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2224 { .name = "active_requests",
2225 .fops = &mdt_hsm_active_requests_fops },
2226 { .name = "user_request_mask",
2227 .fops = &mdt_hsm_user_request_mask_fops, },
2228 { .name = "group_request_mask",
2229 .fops = &mdt_hsm_group_request_mask_fops, },
2230 { .name = "other_request_mask",
2231 .fops = &mdt_hsm_other_request_mask_fops, },