4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2014, Intel Corporation.
27 * Use is subject to license terms.
30 * lustre/mdt/mdt_coordinator.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <lustre_export.h>
46 #include <lprocfs_status.h>
47 #include <lustre_log.h>
48 #include <lustre_kernelcomm.h>
49 #include "mdt_internal.h"
51 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
54 * get obj and HSM attributes on a fid
55 * \param mti [IN] context
56 * \param fid [IN] object fid
57 * \param hsm [OUT] HSM meta data
58 * \retval obj or error (-ENOENT if not found)
60 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
61 const struct lu_fid *fid,
65 struct mdt_object *obj;
73 /* find object by FID */
74 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
78 if (!mdt_object_exists(obj)) {
80 mdt_object_put(mti->mti_env, obj);
81 RETURN(ERR_PTR(-ENOENT));
84 rc = mdt_attr_get_complex(mti, obj, ma);
86 mdt_object_put(mti->mti_env, obj);
90 if (ma->ma_valid & MA_HSM)
93 memset(hsm, 0, sizeof(*hsm));
98 void mdt_hsm_dump_hal(int level, const char *prefix,
99 struct hsm_action_list *hal)
102 struct hsm_action_item *hai;
105 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
106 " archive_id %d flags "LPX64"\n",
107 prefix, hal->hal_version, hal->hal_count,
108 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
110 hai = hai_first(hal);
111 for (i = 0; i < hal->hal_count; i++) {
112 sz = hai->hai_len - sizeof(*hai);
113 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
114 " compound/cookie="LPX64"/"LPX64
115 " action=%s extent="LPX64"-"LPX64" gid="LPX64
116 " datalen=%d data=[%s]\n",
118 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
119 hal->hal_compound_id, hai->hai_cookie,
120 hsm_copytool_action2name(hai->hai_action),
121 hai->hai_extent.offset,
122 hai->hai_extent.length,
124 hai_dump_data_field(hai, buf, sizeof(buf)));
130 * data passed to llog_cat_process() callback
131 * to scan requests and take actions
133 struct hsm_scan_data {
134 struct mdt_thread_info *mti;
135 char fs_name[MTI_NAME_MAXLEN+1];
136 /* request to be send to agents */
137 int request_sz; /** allocated size */
138 int max_requests; /** vector size */
139 int request_cnt; /** used count */
143 struct hsm_action_list *hal;
145 /* records to be canceled */
146 int max_cookie; /** vector size */
147 int cookie_cnt; /** used count */
152 * llog_cat_process() callback, used to:
153 * - find waiting request and start action
154 * - purge canceled and done requests
155 * \param env [IN] environment
156 * \param llh [IN] llog handle
157 * \param hdr [IN] llog record
158 * \param data [IN/OUT] cb data = struct hsm_scan_data
160 * \retval -ve failure
162 static int mdt_coordinator_cb(const struct lu_env *env,
163 struct llog_handle *llh,
164 struct llog_rec_hdr *hdr,
167 const struct llog_agent_req_rec *larr;
168 struct hsm_scan_data *hsd;
169 struct hsm_action_item *hai;
170 struct mdt_device *mdt;
171 struct coordinator *cdt;
176 mdt = hsd->mti->mti_mdt;
177 cdt = &mdt->mdt_coordinator;
179 larr = (struct llog_agent_req_rec *)hdr;
180 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
181 switch (larr->arr_status) {
183 int i, empty_slot, found;
185 /* Are agents full? */
186 if (atomic_read(&cdt->cdt_request_count) ==
187 cdt->cdt_max_requests)
190 /* first search whether the request is found in the list we
191 * have built and if there is room in the request vector */
194 for (i = 0; i < hsd->max_requests &&
195 (empty_slot == -1 || found == -1); i++) {
196 if (hsd->request[i].hal == NULL) {
200 if (hsd->request[i].hal->hal_compound_id ==
201 larr->arr_compound_id) {
206 if (found == -1 && empty_slot == -1)
207 /* unknown request and no more room for new request,
208 * continue scan for to find other entries for
209 * already found request
214 struct hsm_action_list *hal;
216 /* request is not already known */
217 /* allocates hai vector size just needs to be large
219 hsd->request[empty_slot].hal_sz =
220 sizeof(*hsd->request[empty_slot].hal) +
221 cfs_size_round(MTI_NAME_MAXLEN+1) +
222 2 * cfs_size_round(larr->arr_hai.hai_len);
223 OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
225 CERROR("%s: Cannot allocate memory (%d o)"
226 "for compound "LPX64"\n",
228 hsd->request[i].hal_sz,
229 larr->arr_compound_id);
232 hal->hal_version = HAL_VERSION;
233 strlcpy(hal->hal_fsname, hsd->fs_name,
234 MTI_NAME_MAXLEN + 1);
235 hal->hal_compound_id = larr->arr_compound_id;
236 hal->hal_archive_id = larr->arr_archive_id;
237 hal->hal_flags = larr->arr_flags;
239 hsd->request[empty_slot].hal_used_sz = hal_size(hal);
240 hsd->request[empty_slot].hal = hal;
243 hai = hai_first(hal);
245 /* request is known */
246 /* we check if record archive num is the same as the
247 * known request, if not we will serve it in multiple
248 * time because we do not know if the agent can serve
250 * a use case is a compound made of multiple restore
251 * where the files are not archived in the same backend
253 if (larr->arr_archive_id !=
254 hsd->request[found].hal->hal_archive_id)
257 if (hsd->request[found].hal_sz <
258 hsd->request[found].hal_used_sz +
259 cfs_size_round(larr->arr_hai.hai_len)) {
260 /* Not enough room, need an extension */
264 sz = 2 * hsd->request[found].hal_sz;
265 OBD_ALLOC(hal_buffer, sz);
267 CERROR("%s: Cannot allocate memory "
268 "(%d o) for compound "LPX64"\n",
269 mdt_obd_name(mdt), sz,
270 larr->arr_compound_id);
273 memcpy(hal_buffer, hsd->request[found].hal,
274 hsd->request[found].hal_used_sz);
275 OBD_FREE(hsd->request[found].hal,
276 hsd->request[found].hal_sz);
277 hsd->request[found].hal = hal_buffer;
278 hsd->request[found].hal_sz = sz;
280 hai = hai_first(hsd->request[found].hal);
281 for (i = 0; i < hsd->request[found].hal->hal_count;
285 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
286 hai->hai_cookie = larr->arr_hai.hai_cookie;
287 hai->hai_gid = larr->arr_hai.hai_gid;
289 hsd->request[found].hal_used_sz +=
290 cfs_size_round(hai->hai_len);
291 hsd->request[found].hal->hal_count++;
295 struct cdt_agent_req *car;
298 /* we search for a running request
299 * error may happen if coordinator crashes or stopped
300 * with running request
302 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
304 last = larr->arr_req_create;
306 last = car->car_req_update;
307 mdt_cdt_put_request(car);
310 /* test if request too long, if yes cancel it
311 * the same way the copy tool acknowledge a cancel request */
312 if ((last + cdt->cdt_active_req_timeout)
313 < cfs_time_current_sec()) {
314 struct hsm_progress_kernel pgs;
316 dump_llog_agent_req_rec("mdt_coordinator_cb(): "
317 "request timed out, start "
319 /* a too old cancel request just needs to be removed
320 * this can happen, if copy tool does not support cancel
321 * for other requests, we have to remove the running
322 * request and notify the copytool
324 pgs.hpk_fid = larr->arr_hai.hai_fid;
325 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
326 pgs.hpk_extent = larr->arr_hai.hai_extent;
327 pgs.hpk_flags = HP_FLAG_COMPLETED;
328 pgs.hpk_errval = ENOSYS;
329 pgs.hpk_data_version = 0;
330 /* update request state, but do not record in llog, to
331 * avoid deadlock on cdt_llog_lock
333 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
335 CERROR("%s: Cannot cleanup timed out request: "
336 DFID" for cookie "LPX64" action=%s\n",
338 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
339 hsm_copytool_action2name(
340 larr->arr_hai.hai_action));
343 /* The request no longer exists, forget
344 * about it, and do not send a cancel request
345 * to the client, for which an error will be
346 * sent back, leading to an endless cycle of
348 RETURN(LLOG_DEL_RECORD);
351 /* add the cookie to the list of record to be
352 * canceled by caller */
353 if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
354 __u64 *ptr, *old_ptr;
355 int old_sz, new_sz, new_cnt;
357 /* need to increase vector size */
358 old_sz = sizeof(__u64) * hsd->max_cookie;
359 old_ptr = hsd->cookies;
361 new_cnt = 2 * hsd->max_cookie;
362 new_sz = sizeof(__u64) * new_cnt;
364 OBD_ALLOC(ptr, new_sz);
366 CERROR("%s: Cannot allocate memory "
367 "(%d o) for cookie vector\n",
368 mdt_obd_name(mdt), new_sz);
371 memcpy(ptr, hsd->cookies, old_sz);
373 hsd->max_cookie = new_cnt;
374 OBD_FREE(old_ptr, old_sz);
376 hsd->cookies[hsd->cookie_cnt] =
377 larr->arr_hai.hai_cookie;
385 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
386 cfs_time_current_sec())
387 RETURN(LLOG_DEL_RECORD);
394 * create /proc entries for coordinator
397 * \retval -ve failure
399 int hsm_cdt_procfs_init(struct mdt_device *mdt)
401 struct coordinator *cdt = &mdt->mdt_coordinator;
405 /* init /proc entries, failure is not critical */
406 cdt->cdt_proc_dir = lprocfs_register("hsm",
407 mdt2obd_dev(mdt)->obd_proc_entry,
408 lprocfs_mdt_hsm_vars, mdt);
409 if (IS_ERR(cdt->cdt_proc_dir)) {
410 rc = PTR_ERR(cdt->cdt_proc_dir);
411 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
412 " rc=%d\n", mdt_obd_name(mdt), rc);
413 cdt->cdt_proc_dir = NULL;
421 * remove /proc entries for coordinator
424 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
426 struct coordinator *cdt = &mdt->mdt_coordinator;
428 LASSERT(cdt->cdt_state == CDT_STOPPED);
429 if (cdt->cdt_proc_dir != NULL)
430 lprocfs_remove(&cdt->cdt_proc_dir);
434 * get vector of hsm cdt /proc vars
438 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
440 return lprocfs_mdt_hsm_vars;
445 * \param data [IN] obd device
447 * \retval -ve failure
449 static int mdt_coordinator(void *data)
451 struct mdt_thread_info *mti = data;
452 struct mdt_device *mdt = mti->mti_mdt;
453 struct coordinator *cdt = &mdt->mdt_coordinator;
454 struct hsm_scan_data hsd = { NULL };
458 cdt->cdt_thread.t_flags = SVC_RUNNING;
459 wake_up(&cdt->cdt_thread.t_ctl_waitq);
461 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
462 mdt_obd_name(mdt), current_pid());
464 /* timeouted cookie vector initialization */
468 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
469 * increases due to a change from /proc we do not overflow the
470 * hsd.request[] vector
472 hsd.max_requests = cdt->cdt_max_requests;
473 hsd.request_sz = hsd.max_requests * sizeof(*hsd.request);
474 OBD_ALLOC(hsd.request, hsd.request_sz);
476 GOTO(out, rc = -ENOMEM);
479 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
482 struct l_wait_info lwi;
485 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
487 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
488 (cdt->cdt_thread.t_flags &
489 (SVC_STOPPING|SVC_EVENT)),
492 CDEBUG(D_HSM, "coordinator resumes\n");
494 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
495 cdt->cdt_state == CDT_STOPPING) {
496 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
501 /* wake up before timeout, new work arrives */
502 if (cdt->cdt_thread.t_flags & SVC_EVENT)
503 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
505 /* if coordinator is suspended continue to wait */
506 if (cdt->cdt_state == CDT_DISABLE) {
507 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
511 CDEBUG(D_HSM, "coordinator starts reading llog\n");
513 if (hsd.max_requests != cdt->cdt_max_requests) {
514 /* cdt_max_requests has changed,
515 * we need to allocate a new buffer
517 OBD_FREE(hsd.request, hsd.request_sz);
518 hsd.max_requests = cdt->cdt_max_requests;
520 hsd.max_requests * sizeof(*hsd.request);
521 OBD_ALLOC(hsd.request, hsd.request_sz);
528 /* create canceled cookie vector for an arbitrary size
529 * if needed, vector will grow during llog scan
533 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
540 rc = cdt_llog_process(mti->mti_env, mdt,
541 mdt_coordinator_cb, &hsd);
545 CDEBUG(D_HSM, "Found %d requests to send and %d"
546 " requests to cancel\n",
547 hsd.request_cnt, hsd.cookie_cnt);
548 /* first we cancel llog records of the timed out requests */
549 if (hsd.cookie_cnt > 0) {
550 rc = mdt_agent_record_update(mti->mti_env, mdt,
555 CERROR("%s: mdt_agent_record_update() failed, "
556 "rc=%d, cannot update status to %s "
558 mdt_obd_name(mdt), rc,
559 agent_req_status2name(ARS_CANCELED),
563 if (list_empty(&cdt->cdt_agents)) {
564 CDEBUG(D_HSM, "no agent available, "
565 "coordinator sleeps\n");
569 /* here hsd contains a list of requests to be started */
570 for (i = 0; i < hsd.max_requests; i++) {
571 struct hsm_action_list *hal;
572 struct hsm_action_item *hai;
575 enum agent_req_status status;
577 /* still room for work ? */
578 if (atomic_read(&cdt->cdt_request_count) ==
579 cdt->cdt_max_requests)
582 if (hsd.request[i].hal == NULL)
585 /* found a request, we start it */
586 /* kuc payload allocation so we avoid an additionnal
587 * allocation in mdt_hsm_agent_send()
589 hal = kuc_alloc(hsd.request[i].hal_used_sz,
590 KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
592 CERROR("%s: Cannot allocate memory (%d o) "
593 "for compound "LPX64"\n",
595 hsd.request[i].hal_used_sz,
596 hsd.request[i].hal->hal_compound_id);
599 memcpy(hal, hsd.request[i].hal,
600 hsd.request[i].hal_used_sz);
602 rc = mdt_hsm_agent_send(mti, hal, 0);
603 /* if failure, we suppose it is temporary
604 * if the copy tool failed to do the request
605 * it has to use hsm_progress
607 status = (rc ? ARS_WAITING : ARS_STARTED);
609 /* set up cookie vector to set records status
610 * after copy tools start or failed
612 sz = hsd.request[i].hal->hal_count * sizeof(__u64);
613 OBD_ALLOC(cookies, sz);
614 if (cookies == NULL) {
615 CERROR("%s: Cannot allocate memory (%d o) "
616 "for cookies vector "LPX64"\n",
617 mdt_obd_name(mdt), sz,
618 hsd.request[i].hal->hal_compound_id);
619 kuc_free(hal, hsd.request[i].hal_used_sz);
622 hai = hai_first(hal);
623 for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
624 cookies[j] = hai->hai_cookie;
628 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
629 hsd.request[i].hal->hal_count,
632 CERROR("%s: mdt_agent_record_update() failed, "
633 "rc=%d, cannot update status to %s "
635 mdt_obd_name(mdt), rc,
636 agent_req_status2name(status),
637 hsd.request[i].hal->hal_count);
639 OBD_FREE(cookies, sz);
640 kuc_free(hal, hsd.request[i].hal_used_sz);
643 /* free cookie vector allocated for/by callback */
645 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
651 /* free hal allocated by callback */
652 for (i = 0; i < hsd.max_requests; i++) {
653 if (hsd.request[i].hal) {
654 OBD_FREE(hsd.request[i].hal,
655 hsd.request[i].hal_sz);
656 hsd.request[i].hal_sz = 0;
657 hsd.request[i].hal = NULL;
661 LASSERT(hsd.request_cnt == 0);
663 /* reset callback data */
664 memset(hsd.request, 0, hsd.request_sz);
669 OBD_FREE(hsd.request, hsd.request_sz);
672 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
674 if (cdt->cdt_state == CDT_STOPPING) {
675 /* request comes from /proc path, so we need to clean cdt
677 mdt_hsm_cdt_stop(mdt);
678 mdt->mdt_opts.mo_coordinator = 0;
680 /* request comes from a thread event, generated
681 * by mdt_stop_coordinator(), we have to ack
682 * and cdt cleaning will be done by event sender
684 cdt->cdt_thread.t_flags = SVC_STOPPED;
685 wake_up(&cdt->cdt_thread.t_ctl_waitq);
689 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
690 mdt_obd_name(mdt), current_pid(), rc);
692 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
694 mdt_obd_name(mdt), current_pid());
700 * lookup a restore handle by FID
701 * caller needs to hold cdt_restore_lock
702 * \param cdt [IN] coordinator
703 * \param fid [IN] FID
704 * \retval cdt_restore_handle found
705 * \retval NULL not found
707 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
708 const struct lu_fid *fid)
710 struct cdt_restore_handle *crh;
713 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
714 if (lu_fid_eq(&crh->crh_fid, fid))
721 * data passed to llog_cat_process() callback
722 * to scan requests and take actions
724 struct hsm_restore_data {
725 struct mdt_thread_info *hrd_mti;
729 * llog_cat_process() callback, used to:
730 * - find restore request and allocate the restore handle
731 * \param env [IN] environment
732 * \param llh [IN] llog handle
733 * \param hdr [IN] llog record
734 * \param data [IN/OUT] cb data = struct hsm_restore_data
736 * \retval -ve failure
738 static int hsm_restore_cb(const struct lu_env *env,
739 struct llog_handle *llh,
740 struct llog_rec_hdr *hdr, void *data)
742 struct llog_agent_req_rec *larr;
743 struct hsm_restore_data *hrd;
744 struct cdt_restore_handle *crh;
745 struct hsm_action_item *hai;
746 struct mdt_thread_info *mti;
747 struct coordinator *cdt;
748 struct mdt_object *child;
754 cdt = &mti->mti_mdt->mdt_coordinator;
756 larr = (struct llog_agent_req_rec *)hdr;
757 hai = &larr->arr_hai;
758 if (hai->hai_cookie > cdt->cdt_last_cookie)
759 /* update the cookie to avoid collision */
760 cdt->cdt_last_cookie = hai->hai_cookie + 1;
762 if (hai->hai_action != HSMA_RESTORE ||
763 agent_req_in_final_state(larr->arr_status))
766 /* restore request not in a final state */
768 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
772 crh->crh_fid = hai->hai_fid;
773 /* in V1 all file is restored
774 crh->extent.start = hai->hai_extent.offset;
775 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
777 crh->crh_extent.start = 0;
778 crh->crh_extent.end = hai->hai_extent.length;
779 /* get the layout lock */
780 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
781 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
782 MDS_INODELOCK_LAYOUT);
784 GOTO(out, rc = PTR_ERR(child));
787 /* we choose to not keep a reference
788 * on the object during the restore time which can be very long */
789 mdt_object_put(mti->mti_env, child);
791 mutex_lock(&cdt->cdt_restore_lock);
792 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
793 mutex_unlock(&cdt->cdt_restore_lock);
800 * restore coordinator state at startup
801 * the goal is to take a layout lock for each registered restore request
802 * \param mti [IN] context
804 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
806 struct hsm_restore_data hrd;
812 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
813 hsm_restore_cb, &hrd);
818 static int hsm_init_ucred(struct lu_ucred *uc)
822 uc->uc_valid = UCRED_OLD;
831 uc->uc_suppgids[0] = -1;
832 uc->uc_suppgids[1] = -1;
833 uc->uc_cap = CFS_CAP_FS_MASK;
836 uc->uc_identity = NULL;
842 * wake up coordinator thread
843 * \param mdt [IN] device
845 * \retval -ve failure
847 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
849 struct coordinator *cdt = &mdt->mdt_coordinator;
852 if (cdt->cdt_state == CDT_STOPPED)
855 /* wake up coordinator */
856 cdt->cdt_thread.t_flags = SVC_EVENT;
857 wake_up(&cdt->cdt_thread.t_ctl_waitq);
863 * initialize coordinator struct
864 * \param mdt [IN] device
866 * \retval -ve failure
868 int mdt_hsm_cdt_init(struct mdt_device *mdt)
870 struct coordinator *cdt = &mdt->mdt_coordinator;
871 struct mdt_thread_info *cdt_mti = NULL;
875 cdt->cdt_state = CDT_STOPPED;
877 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
878 mutex_init(&cdt->cdt_llog_lock);
879 init_rwsem(&cdt->cdt_agent_lock);
880 init_rwsem(&cdt->cdt_request_lock);
881 mutex_init(&cdt->cdt_restore_lock);
883 INIT_LIST_HEAD(&cdt->cdt_requests);
884 INIT_LIST_HEAD(&cdt->cdt_agents);
885 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
887 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
891 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
892 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
894 lu_context_enter(&cdt->cdt_session);
895 cdt->cdt_env.le_ses = &cdt->cdt_session;
897 lu_env_fini(&cdt->cdt_env);
901 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
902 LASSERT(cdt_mti != NULL);
904 cdt_mti->mti_env = &cdt->cdt_env;
905 cdt_mti->mti_mdt = mdt;
907 hsm_init_ucred(mdt_ucred(cdt_mti));
909 /* default values for /proc tunnables
910 * can be override by MGS conf */
911 cdt->cdt_default_archive_id = 1;
912 cdt->cdt_grace_delay = 60;
913 cdt->cdt_loop_period = 10;
914 cdt->cdt_max_requests = 3;
915 cdt->cdt_policy = CDT_DEFAULT_POLICY;
916 cdt->cdt_active_req_timeout = 3600;
918 /* Initialize cdt_compound_id here to allow its usage for
919 * delayed requests from RAoLU policy */
920 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
922 /* by default do not remove archives on last unlink */
923 cdt->cdt_remove_archive_on_last_unlink = false;
929 * free a coordinator thread
930 * \param mdt [IN] device
932 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
934 struct coordinator *cdt = &mdt->mdt_coordinator;
937 lu_context_exit(cdt->cdt_env.le_ses);
938 lu_context_fini(cdt->cdt_env.le_ses);
940 lu_env_fini(&cdt->cdt_env);
946 * start a coordinator thread
947 * \param mdt [IN] device
949 * \retval -ve failure
951 int mdt_hsm_cdt_start(struct mdt_device *mdt)
953 struct coordinator *cdt = &mdt->mdt_coordinator;
956 struct mdt_thread_info *cdt_mti;
957 struct task_struct *task;
960 /* functions defined but not yet used
961 * this avoid compilation warning
965 if (cdt->cdt_state != CDT_STOPPED) {
966 CERROR("%s: Coordinator already started\n",
971 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
972 cdt->cdt_policy = CDT_DEFAULT_POLICY;
974 cdt->cdt_state = CDT_INIT;
976 /* just need to be larger than previous one */
977 /* cdt_last_cookie is protected by cdt_llog_lock */
978 cdt->cdt_last_cookie = cfs_time_current_sec();
979 atomic_set(&cdt->cdt_request_count, 0);
980 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
981 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
982 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
984 /* to avoid deadlock when start is made through /proc
985 * /proc entries are created by the coordinator thread */
987 /* set up list of started restore requests */
988 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
989 rc = mdt_hsm_pending_restore(cdt_mti);
991 CERROR("%s: cannot take the layout locks needed"
992 " for registered restore: %d\n",
993 mdt_obd_name(mdt), rc);
995 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
998 cdt->cdt_state = CDT_STOPPED;
999 CERROR("%s: error starting coordinator thread: %d\n",
1000 mdt_obd_name(mdt), rc);
1003 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1008 wait_event(cdt->cdt_thread.t_ctl_waitq,
1009 (cdt->cdt_thread.t_flags & SVC_RUNNING));
1011 cdt->cdt_state = CDT_RUNNING;
1012 mdt->mdt_opts.mo_coordinator = 1;
1017 * stop a coordinator thread
1018 * \param mdt [IN] device
1020 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1022 struct coordinator *cdt = &mdt->mdt_coordinator;
1023 struct cdt_agent_req *car, *tmp1;
1024 struct hsm_agent *ha, *tmp2;
1025 struct cdt_restore_handle *crh, *tmp3;
1026 struct mdt_thread_info *cdt_mti;
1029 if (cdt->cdt_state == CDT_STOPPED) {
1030 CERROR("%s: Coordinator already stopped\n",
1035 if (cdt->cdt_state != CDT_STOPPING) {
1036 /* stop coordinator thread before cleaning */
1037 cdt->cdt_thread.t_flags = SVC_STOPPING;
1038 wake_up(&cdt->cdt_thread.t_ctl_waitq);
1039 wait_event(cdt->cdt_thread.t_ctl_waitq,
1040 cdt->cdt_thread.t_flags & SVC_STOPPED);
1042 cdt->cdt_state = CDT_STOPPED;
1044 /* start cleaning */
1045 down_write(&cdt->cdt_request_lock);
1046 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1048 list_del(&car->car_request_list);
1049 mdt_cdt_free_request(car);
1051 up_write(&cdt->cdt_request_lock);
1053 down_write(&cdt->cdt_agent_lock);
1054 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1055 list_del(&ha->ha_list);
1058 up_write(&cdt->cdt_agent_lock);
1060 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1061 mutex_lock(&cdt->cdt_restore_lock);
1062 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1063 struct mdt_object *child;
1065 /* give back layout lock */
1066 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1068 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1070 list_del(&crh->crh_list);
1072 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1074 mutex_unlock(&cdt->cdt_restore_lock);
1076 mdt->mdt_opts.mo_coordinator = 0;
1082 * register all requests from an hal in the memory list
1083 * \param mti [IN] context
1084 * \param hal [IN] request
1085 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1086 * which is running the CT
1088 * \retval -ve failure
1090 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1091 struct hsm_action_list *hal, struct obd_uuid *uuid)
1093 struct mdt_device *mdt = mti->mti_mdt;
1094 struct coordinator *cdt = &mdt->mdt_coordinator;
1095 struct hsm_action_item *hai;
1099 /* register request in memory list */
1100 hai = hai_first(hal);
1101 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1102 struct cdt_agent_req *car;
1104 /* in case of a cancel request, we first mark the ondisk
1105 * record of the request we want to stop as canceled
1106 * this does not change the cancel record
1107 * it will be done when updating the request status
1109 if (hai->hai_action == HSMA_CANCEL) {
1110 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1114 CERROR("%s: mdt_agent_record_update() failed, "
1115 "rc=%d, cannot update status to %s "
1116 "for cookie "LPX64"\n",
1117 mdt_obd_name(mdt), rc,
1118 agent_req_status2name(ARS_CANCELED),
1123 /* find the running request to set it canceled */
1124 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1126 car->car_canceled = 1;
1127 /* uuid has to be changed to the one running the
1128 * request to cancel */
1129 *uuid = car->car_uuid;
1130 mdt_cdt_put_request(car);
1132 /* no need to memorize cancel request
1133 * this also avoid a deadlock when we receive
1134 * a purge all requests command
1139 if (hai->hai_action == HSMA_ARCHIVE) {
1140 struct mdt_object *obj;
1143 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1144 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1147 GOTO(out, rc = PTR_ERR(obj));
1149 hsm.mh_flags |= HS_EXISTS;
1150 hsm.mh_arch_id = hal->hal_archive_id;
1151 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1152 mdt_object_put(mti->mti_env, obj);
1157 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1158 hal->hal_archive_id, hal->hal_flags,
1161 GOTO(out, rc = PTR_ERR(car));
1163 rc = mdt_cdt_add_request(cdt, car);
1165 mdt_cdt_free_request(car);
1172 * swap layouts between 2 fids
1173 * \param mti [IN] context
1176 * \param mh_common [IN] MD HSM
1178 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1179 const lustre_fid *fid, const lustre_fid *dfid,
1180 struct md_hsm *mh_common)
1182 struct mdt_device *mdt = mti->mti_mdt;
1183 struct mdt_object *child1, *child2;
1184 struct mdt_lock_handle *lh2;
1188 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1190 GOTO(out, rc = PTR_ERR(child1));
1192 /* we already have layout lock on FID so take only
1194 lh2 = &mti->mti_lh[MDT_LH_OLD];
1195 mdt_lock_reg_init(lh2, LCK_EX);
1196 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1198 GOTO(out_child1, rc = PTR_ERR(child2));
1200 /* if copy tool closes the volatile before sending the final
1201 * progress through llapi_hsm_copy_end(), all the objects
1202 * are removed and mdd_swap_layout LBUG */
1203 if (!mdt_object_exists(child2)) {
1204 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1205 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1206 GOTO(out_child2, rc = -ENOENT);
1208 /* Since we only handle restores here, unconditionally use
1209 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1210 * be preserved in case of failure during swap_layout and not
1211 * leave a file in an intermediate but incoherent state.
1212 * But need to setup HSM xattr of data FID before, reuse
1213 * mti and mh presets for FID in hsm_cdt_request_completed(),
1214 * only need to clear RELEASED and DIRTY.
1216 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1217 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1219 rc = mo_swap_layouts(mti->mti_env,
1220 mdt_object_child(child1),
1221 mdt_object_child(child2),
1222 SWAP_LAYOUTS_MDS_HSM);
1225 mdt_object_unlock_put(mti, child2, lh2, 1);
1227 mdt_object_put(mti->mti_env, child1);
1233 * update status of a completed request
1234 * \param mti [IN] context
1235 * \param pgs [IN] progress of the copy tool
1236 * \param update_record [IN] update llog record
1238 * \retval -ve failure
1240 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1241 struct hsm_progress_kernel *pgs,
1242 const struct cdt_agent_req *car,
1243 enum agent_req_status *status)
1245 const struct lu_env *env = mti->mti_env;
1246 struct mdt_device *mdt = mti->mti_mdt;
1247 struct coordinator *cdt = &mdt->mdt_coordinator;
1248 struct mdt_object *obj = NULL;
1249 int cl_flags = 0, rc = 0;
1254 /* default is to retry */
1255 *status = ARS_WAITING;
1257 /* find object by FID */
1258 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1259 /* we will update MD HSM only if needed */
1260 is_mh_changed = false;
1262 /* object removed */
1263 *status = ARS_SUCCEED;
1267 /* no need to change mh->mh_arch_id
1268 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1270 if (pgs->hpk_errval != 0) {
1271 switch (pgs->hpk_errval) {
1273 /* the copy tool does not support cancel
1274 * so the cancel request is failed
1275 * As we cannot distinguish a cancel progress
1276 * from another action progress (they have the
1277 * same cookie), we suppose here the CT returns
1278 * ENOSYS only if does not support cancel
1280 /* this can also happen when cdt calls it to
1281 * for a timed out request */
1282 *status = ARS_FAILED;
1283 /* to have a cancel event in changelog */
1284 pgs->hpk_errval = ECANCELED;
1287 /* the request record has already been set to
1288 * ARS_CANCELED, this set the cancel request
1290 *status = ARS_SUCCEED;
1293 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1294 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1295 ARS_FAILED : ARS_WAITING);
1299 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1300 CERROR("%s: Request "LPX64" on "DFID
1301 " failed, error code %d too large\n",
1303 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1305 hsm_set_cl_error(&cl_flags,
1306 CLF_HSM_ERROVERFLOW);
1309 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1312 switch (car->car_hai->hai_action) {
1314 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1317 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1320 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1323 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1324 CERROR("%s: Failed request "LPX64" on "DFID
1325 " cannot be a CANCEL\n",
1328 PFID(&pgs->hpk_fid));
1331 CERROR("%s: Failed request "LPX64" on "DFID
1332 " %d is an unknown action\n",
1334 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1335 car->car_hai->hai_action);
1340 *status = ARS_SUCCEED;
1341 switch (car->car_hai->hai_action) {
1343 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1344 /* set ARCHIVE keep EXIST and clear LOST and
1346 mh.mh_arch_ver = pgs->hpk_data_version;
1347 mh.mh_flags |= HS_ARCHIVED;
1348 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1349 is_mh_changed = true;
1352 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1354 /* do not clear RELEASED and DIRTY here
1355 * this will occur in hsm_swap_layouts()
1358 /* Restoring has changed the file version on
1360 mh.mh_arch_ver = pgs->hpk_data_version;
1361 is_mh_changed = true;
1364 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1365 /* clear ARCHIVED EXISTS and LOST */
1366 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1367 is_mh_changed = true;
1370 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1371 CERROR("%s: Successful request "LPX64
1373 " cannot be a CANCEL\n",
1376 PFID(&pgs->hpk_fid));
1379 CERROR("%s: Successful request "LPX64
1381 " %d is an unknown action\n",
1383 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1384 car->car_hai->hai_action);
1390 /* rc != 0 means error when analysing action, it may come from
1391 * a crasy CT no need to manage DIRTY
1394 hsm_set_cl_flags(&cl_flags,
1395 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1397 /* unlock is done later, after layout lock management */
1399 rc = mdt_hsm_attr_set(mti, obj, &mh);
1402 /* we give back layout lock only if restore was successful or
1403 * if restore was canceled or if policy is to not retry
1404 * in other cases we just unlock the object */
1405 if (car->car_hai->hai_action == HSMA_RESTORE &&
1406 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1407 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1408 struct cdt_restore_handle *crh;
1410 /* restore in data FID done, we swap the layouts
1411 * only if restore is successful */
1412 if (pgs->hpk_errval == 0) {
1413 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1414 &car->car_hai->hai_dfid, &mh);
1416 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1417 *status = ARS_FAILED;
1418 pgs->hpk_errval = -rc;
1421 /* we have to retry, so keep layout lock */
1422 if (*status == ARS_WAITING)
1425 /* give back layout lock */
1426 mutex_lock(&cdt->cdt_restore_lock);
1427 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1429 list_del(&crh->crh_list);
1430 mutex_unlock(&cdt->cdt_restore_lock);
1431 /* just give back layout lock, we keep
1432 * the reference which is given back
1433 * later with the lock for HSM flags */
1434 if (!IS_ERR(obj) && crh != NULL)
1435 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1438 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1444 if (obj != NULL && !IS_ERR(obj)) {
1445 mo_changelog(env, CL_HSM, cl_flags,
1446 mdt_object_child(obj));
1447 mdt_object_put(mti->mti_env, obj);
1454 * update status of a request
1455 * \param mti [IN] context
1456 * \param pgs [IN] progress of the copy tool
1457 * \param update_record [IN] update llog record
1459 * \retval -ve failure
1461 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1462 struct hsm_progress_kernel *pgs,
1463 const int update_record)
1465 struct mdt_device *mdt = mti->mti_mdt;
1466 struct coordinator *cdt = &mdt->mdt_coordinator;
1467 struct cdt_agent_req *car;
1471 /* no coordinator started, so we cannot serve requests */
1472 if (cdt->cdt_state == CDT_STOPPED)
1475 /* first do sanity checks */
1476 car = mdt_cdt_update_request(cdt, pgs);
1478 CERROR("%s: Cannot find running request for cookie "LPX64
1481 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1484 RETURN(PTR_ERR(car));
1487 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1488 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1489 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1490 hsm_copytool_action2name(car->car_hai->hai_action),
1491 pgs->hpk_flags, pgs->hpk_errval,
1492 PFID(&car->car_hai->hai_fid),
1493 PFID(&car->car_hai->hai_dfid));
1495 /* progress is done on FID or data FID depending of the action and
1496 * of the copy progress */
1497 /* for restore progress is used to send back the data FID to cdt */
1498 if (car->car_hai->hai_action == HSMA_RESTORE &&
1499 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1500 car->car_hai->hai_dfid = pgs->hpk_fid;
1502 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1503 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1504 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1505 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1506 CERROR("%s: Progress on "DFID" for cookie "LPX64
1507 " does not match request FID "DFID" nor data FID "
1510 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1511 PFID(&car->car_hai->hai_fid),
1512 PFID(&car->car_hai->hai_dfid));
1513 GOTO(out, rc = -EINVAL);
1516 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1517 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1518 " is not coherent (err=%d and not completed"
1521 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1522 hsm_copytool_action2name(car->car_hai->hai_action),
1523 pgs->hpk_errval, pgs->hpk_flags);
1524 GOTO(out, rc = -EINVAL);
1527 /* now progress is valid */
1529 /* we use a root like ucred */
1530 hsm_init_ucred(mdt_ucred(mti));
1532 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1533 enum agent_req_status status;
1535 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1537 /* remove request from memory list */
1538 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1540 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1541 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1543 hsm_copytool_action2name(car->car_hai->hai_action),
1544 agent_req_status2name(status));
1546 if (update_record) {
1549 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1550 &pgs->hpk_cookie, 1,
1553 CERROR("%s: mdt_agent_record_update() failed,"
1554 " rc=%d, cannot update status to %s"
1555 " for cookie "LPX64"\n",
1556 mdt_obd_name(mdt), rc1,
1557 agent_req_status2name(status),
1559 rc = (rc != 0 ? rc : rc1);
1561 /* ct has completed a request, so a slot is available, wakeup
1562 * cdt to find new work */
1563 mdt_hsm_cdt_wakeup(mdt);
1565 /* if copytool send a progress on a canceled request
1566 * we inform copytool it should stop
1568 if (car->car_canceled == 1)
1574 /* remove ref got from mdt_cdt_update_request() */
1575 mdt_cdt_put_request(car);
1582 * data passed to llog_cat_process() callback
1583 * to cancel requests
1585 struct hsm_cancel_all_data {
1586 struct mdt_device *mdt;
1590 * llog_cat_process() callback, used to:
1591 * - purge all requests
1592 * \param env [IN] environment
1593 * \param llh [IN] llog handle
1594 * \param hdr [IN] llog record
1595 * \param data [IN] cb data = struct hsm_cancel_all_data
1597 * \retval -ve failure
1599 static int mdt_cancel_all_cb(const struct lu_env *env,
1600 struct llog_handle *llh,
1601 struct llog_rec_hdr *hdr, void *data)
1603 struct llog_agent_req_rec *larr;
1604 struct hsm_cancel_all_data *hcad;
1608 larr = (struct llog_agent_req_rec *)hdr;
1610 if (larr->arr_status == ARS_WAITING ||
1611 larr->arr_status == ARS_STARTED) {
1612 larr->arr_status = ARS_CANCELED;
1613 larr->arr_req_change = cfs_time_current_sec();
1614 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1616 RETURN(LLOG_DEL_RECORD);
1622 * cancel all actions
1623 * \param obd [IN] MDT device
1625 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1627 struct mdt_thread_info *mti;
1628 struct coordinator *cdt = &mdt->mdt_coordinator;
1629 struct cdt_agent_req *car;
1630 struct hsm_action_list *hal = NULL;
1631 struct hsm_action_item *hai;
1632 struct hsm_cancel_all_data hcad;
1633 int hal_sz = 0, hal_len, rc;
1634 enum cdt_states save_state;
1637 /* retrieve coordinator context */
1638 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1640 /* disable coordinator */
1641 save_state = cdt->cdt_state;
1642 cdt->cdt_state = CDT_DISABLE;
1644 /* send cancel to all running requests */
1645 down_read(&cdt->cdt_request_lock);
1646 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1647 mdt_cdt_get_request(car);
1648 /* request is not yet removed from list, it will be done
1649 * when copytool will return progress
1652 if (car->car_hai->hai_action == HSMA_CANCEL) {
1653 mdt_cdt_put_request(car);
1658 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1659 cfs_size_round(car->car_hai->hai_len);
1661 if (hal_len > hal_sz && hal_sz > 0) {
1662 /* not enough room, free old buffer */
1663 OBD_FREE(hal, hal_sz);
1667 /* empty buffer, allocate one */
1670 OBD_ALLOC(hal, hal_sz);
1672 mdt_cdt_put_request(car);
1673 up_read(&cdt->cdt_request_lock);
1674 GOTO(out, rc = -ENOMEM);
1678 hal->hal_version = HAL_VERSION;
1679 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1681 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1682 hal->hal_compound_id = car->car_compound_id;
1683 hal->hal_archive_id = car->car_archive_id;
1684 hal->hal_flags = car->car_flags;
1687 hai = hai_first(hal);
1688 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1689 hai->hai_action = HSMA_CANCEL;
1692 /* it is possible to safely call mdt_hsm_agent_send()
1693 * (ie without a deadlock on cdt_request_lock), because the
1694 * write lock is taken only if we are not in purge mode
1695 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1696 * nor mdt_cdt_remove_request())
1698 /* no conflict with cdt thread because cdt is disable and we
1699 * have the request lock */
1700 mdt_hsm_agent_send(mti, hal, 1);
1702 mdt_cdt_put_request(car);
1704 up_read(&cdt->cdt_request_lock);
1707 OBD_FREE(hal, hal_sz);
1709 /* cancel all on-disk records */
1712 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1713 mdt_cancel_all_cb, &hcad);
1715 /* enable coordinator */
1716 cdt->cdt_state = save_state;
1722 * check if a request is compatible with file status
1723 * \param hai [IN] request description
1724 * \param hal_an [IN] request archive number (not used)
1725 * \param rq_flags [IN] request flags
1726 * \param hsm [IN] file HSM metadata
1729 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1730 const int hal_an, const __u64 rq_flags,
1731 const struct md_hsm *hsm)
1733 int is_compat = false;
1737 hsm_flags = hsm->mh_flags;
1738 switch (hai->hai_action) {
1740 if (!(hsm_flags & HS_NOARCHIVE) &&
1741 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1745 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1746 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1750 if (!(hsm_flags & HS_RELEASED) &&
1751 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1758 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1759 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1760 PFID(&hai->hai_fid),
1761 hsm_copytool_action2name(hai->hai_action), rq_flags,
1762 hai->hai_extent.offset, hai->hai_extent.length,
1764 (is_compat ? "compatible" : "uncompatible"));
1770 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1772 static const struct {
1776 } hsm_policy_names[] = {
1777 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1778 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1783 * convert a policy name to a bit
1784 * \param name [IN] policy name
1786 * \retval policy bit
1788 static __u64 hsm_policy_str2bit(const char *name)
1792 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1793 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1794 strcmp(hsm_policy_names[i].name, name) == 0)
1795 return hsm_policy_names[i].bit;
1800 * convert a policy bit field to a string
1801 * \param mask [IN] policy bit field
1802 * \param hexa [IN] print mask before bit names
1803 * \param buffer [OUT] string
1804 * \param count [IN] size of buffer
1806 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1814 seq_printf(m, "("LPX64") ", mask);
1816 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1819 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1820 if (hsm_policy_names[j].bit == bit)
1824 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1826 seq_printf(m, "%s ", hsm_policy_names[j].name);
1828 /* remove last ' ' */
1833 /* methods to read/write HSM policy flags */
1834 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1836 struct mdt_device *mdt = m->private;
1837 struct coordinator *cdt = &mdt->mdt_coordinator;
1840 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1845 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1846 size_t count, loff_t *off)
1848 struct seq_file *m = file->private_data;
1849 struct mdt_device *mdt = m->private;
1850 struct coordinator *cdt = &mdt->mdt_coordinator;
1851 char *start, *token, sign;
1854 __u64 add_mask, remove_mask, set_mask;
1858 if (count + 1 > PAGE_SIZE)
1861 OBD_ALLOC(buf, count + 1);
1865 if (copy_from_user(buf, buffer, count))
1866 GOTO(out, rc = -EFAULT);
1871 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1874 add_mask = remove_mask = set_mask = 0;
1876 token = strsep(&start, "\n ");
1882 if (sign == '-' || sign == '+')
1885 policy = hsm_policy_str2bit(token);
1887 CWARN("%s: '%s' is unknown, "
1888 "supported policies are:\n", mdt_obd_name(mdt),
1890 hsm_policy_bit2str(m, 0, false);
1891 GOTO(out, rc = -EINVAL);
1895 remove_mask |= policy;
1905 } while (start != NULL);
1907 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1908 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1910 /* if no sign in all string, it is a clear and set
1911 * if some sign found, all unsigned are converted
1913 * P1 P2 = set to P1 and P2
1914 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1916 if (remove_mask == 0 && add_mask == 0) {
1917 cdt->cdt_policy = set_mask;
1919 cdt->cdt_policy |= set_mask | add_mask;
1920 cdt->cdt_policy &= ~remove_mask;
1923 GOTO(out, rc = count);
1926 OBD_FREE(buf, count + 1);
1929 LPROC_SEQ_FOPS(mdt_hsm_policy);
1931 #define GENERATE_PROC_METHOD(VAR) \
1932 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1934 struct mdt_device *mdt = m->private; \
1935 struct coordinator *cdt = &mdt->mdt_coordinator; \
1938 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1942 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1943 size_t count, loff_t *off) \
1946 struct seq_file *m = file->private_data; \
1947 struct mdt_device *mdt = m->private; \
1948 struct coordinator *cdt = &mdt->mdt_coordinator; \
1953 rc = lprocfs_write_helper(buffer, count, &val); \
1963 GENERATE_PROC_METHOD(cdt_loop_period)
1964 GENERATE_PROC_METHOD(cdt_grace_delay)
1965 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1966 GENERATE_PROC_METHOD(cdt_max_requests)
1967 GENERATE_PROC_METHOD(cdt_default_archive_id)
1970 * procfs write method for MDT/hsm_control
1971 * proc entry is in mdt directory so data is mdt obd_device pointer
1973 #define CDT_ENABLE_CMD "enabled"
1974 #define CDT_STOP_CMD "shutdown"
1975 #define CDT_DISABLE_CMD "disabled"
1976 #define CDT_PURGE_CMD "purge"
1977 #define CDT_HELP_CMD "help"
1978 #define CDT_MAX_CMD_LEN 10
1981 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1982 size_t count, loff_t *off)
1984 struct seq_file *m = file->private_data;
1985 struct obd_device *obd = m->private;
1986 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1987 struct coordinator *cdt = &(mdt->mdt_coordinator);
1989 char kernbuf[CDT_MAX_CMD_LEN];
1992 if (count == 0 || count >= sizeof(kernbuf))
1995 if (copy_from_user(kernbuf, buffer, count))
1999 if (kernbuf[count - 1] == '\n')
2000 kernbuf[count - 1] = 0;
2003 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2004 if (cdt->cdt_state == CDT_DISABLE) {
2005 cdt->cdt_state = CDT_RUNNING;
2006 mdt_hsm_cdt_wakeup(mdt);
2008 rc = mdt_hsm_cdt_start(mdt);
2010 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2011 if ((cdt->cdt_state == CDT_STOPPING) ||
2012 (cdt->cdt_state == CDT_STOPPED)) {
2013 CERROR("%s: Coordinator already stopped\n",
2017 cdt->cdt_state = CDT_STOPPING;
2019 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2020 if ((cdt->cdt_state == CDT_STOPPING) ||
2021 (cdt->cdt_state == CDT_STOPPED)) {
2022 CERROR("%s: Coordinator is stopped\n",
2026 cdt->cdt_state = CDT_DISABLE;
2028 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2029 rc = hsm_cancel_all_actions(mdt);
2030 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2038 CERROR("%s: Valid coordinator control commands are: "
2039 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2040 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2041 CDT_PURGE_CMD, CDT_HELP_CMD);
2049 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2051 struct obd_device *obd = m->private;
2052 struct coordinator *cdt;
2055 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2057 if (cdt->cdt_state == CDT_INIT)
2058 seq_printf(m, "init\n");
2059 else if (cdt->cdt_state == CDT_RUNNING)
2060 seq_printf(m, "enabled\n");
2061 else if (cdt->cdt_state == CDT_STOPPING)
2062 seq_printf(m, "stopping\n");
2063 else if (cdt->cdt_state == CDT_STOPPED)
2064 seq_printf(m, "stopped\n");
2065 else if (cdt->cdt_state == CDT_DISABLE)
2066 seq_printf(m, "disabled\n");
2068 seq_printf(m, "unknown\n");
2074 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2079 for (i = 0; i < 8 * sizeof(mask); i++) {
2080 if (mask & (1UL << i))
2081 rc += seq_printf(m, "%s%s", rc == 0 ? "" : " ",
2082 hsm_copytool_action2name(i));
2084 rc += seq_printf(m, "\n");
2090 mdt_hsm_user_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_user_request_mask);
2099 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2101 struct mdt_device *mdt = m->private;
2102 struct coordinator *cdt = &mdt->mdt_coordinator;
2104 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2108 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2110 struct mdt_device *mdt = m->private;
2111 struct coordinator *cdt = &mdt->mdt_coordinator;
2113 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2116 static inline enum hsm_copytool_action
2117 hsm_copytool_name2action(const char *name)
2119 if (strcasecmp(name, "NOOP") == 0)
2121 else if (strcasecmp(name, "ARCHIVE") == 0)
2122 return HSMA_ARCHIVE;
2123 else if (strcasecmp(name, "RESTORE") == 0)
2124 return HSMA_RESTORE;
2125 else if (strcasecmp(name, "REMOVE") == 0)
2127 else if (strcasecmp(name, "CANCEL") == 0)
2134 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2135 size_t user_count, __u64 *mask)
2137 char *buf, *pos, *name;
2143 if (!(user_count < 4096))
2146 buf_size = user_count + 1;
2148 OBD_ALLOC(buf, buf_size);
2152 if (copy_from_user(buf, user_buf, buf_size - 1))
2153 GOTO(out, rc = -EFAULT);
2155 buf[buf_size - 1] = '\0';
2158 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2164 action = hsm_copytool_name2action(name);
2166 GOTO(out, rc = -EINVAL);
2168 new_mask |= (1UL << action);
2174 OBD_FREE(buf, buf_size);
2180 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2181 size_t count, loff_t *off)
2183 struct seq_file *m = file->private_data;
2184 struct mdt_device *mdt = m->private;
2185 struct coordinator *cdt = &mdt->mdt_coordinator;
2187 return mdt_write_hsm_request_mask(file, buf, count,
2188 &cdt->cdt_user_request_mask);
2192 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2193 size_t count, loff_t *off)
2195 struct seq_file *m = file->private_data;
2196 struct mdt_device *mdt = m->private;
2197 struct coordinator *cdt = &mdt->mdt_coordinator;
2199 return mdt_write_hsm_request_mask(file, buf, count,
2200 &cdt->cdt_group_request_mask);
2204 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2205 size_t count, loff_t *off)
2207 struct seq_file *m = file->private_data;
2208 struct mdt_device *mdt = m->private;
2209 struct coordinator *cdt = &mdt->mdt_coordinator;
2211 return mdt_write_hsm_request_mask(file, buf, count,
2212 &cdt->cdt_other_request_mask);
2215 static int mdt_hsm_cdt_raolu_seq_show(struct seq_file *m, void *data)
2217 struct mdt_device *mdt = m->private;
2218 struct coordinator *cdt = &mdt->mdt_coordinator;
2221 seq_printf(m, "%d\n", (int)cdt->cdt_remove_archive_on_last_unlink);
2226 mdt_hsm_cdt_raolu_seq_write(struct file *file, const char __user *buffer,
2227 size_t count, loff_t *off)
2230 struct seq_file *m = file->private_data;
2231 struct mdt_device *mdt = m->private;
2232 struct coordinator *cdt = &mdt->mdt_coordinator;
2237 rc = lprocfs_write_helper(buffer, count, &val);
2241 cdt->cdt_remove_archive_on_last_unlink = val;
2245 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2246 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2247 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2248 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2249 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2250 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2251 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2252 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2253 LPROC_SEQ_FOPS(mdt_hsm_cdt_raolu);
2255 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2257 .fops = &mdt_hsm_agent_fops },
2258 { .name = "actions",
2259 .fops = &mdt_hsm_actions_fops,
2260 .proc_mode = 0444 },
2261 { .name = "default_archive_id",
2262 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2263 { .name = "grace_delay",
2264 .fops = &mdt_hsm_cdt_grace_delay_fops },
2265 { .name = "loop_period",
2266 .fops = &mdt_hsm_cdt_loop_period_fops },
2267 { .name = "max_requests",
2268 .fops = &mdt_hsm_cdt_max_requests_fops },
2270 .fops = &mdt_hsm_policy_fops },
2271 { .name = "active_request_timeout",
2272 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2273 { .name = "active_requests",
2274 .fops = &mdt_hsm_active_requests_fops },
2275 { .name = "user_request_mask",
2276 .fops = &mdt_hsm_user_request_mask_fops, },
2277 { .name = "group_request_mask",
2278 .fops = &mdt_hsm_group_request_mask_fops, },
2279 { .name = "other_request_mask",
2280 .fops = &mdt_hsm_other_request_mask_fops, },
2281 { .name = "remove_archive_on_last_unlink",
2282 .fops = &mdt_hsm_cdt_raolu_fops, },