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, 2016, 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 %#llx"
106 " archive_id %d flags %#llx\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=%#llx/%#llx"
115 " action=%s extent=%#llx-%#llx gid=%#llx"
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_request {
136 struct hsm_action_list *hal;
139 struct hsm_scan_data {
140 struct mdt_thread_info *mti;
141 char fs_name[MTI_NAME_MAXLEN+1];
142 /* request to be send to agents */
143 int max_requests; /** vector size */
144 int request_cnt; /** used count */
145 struct hsm_scan_request *request;
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 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) {
181 struct hsm_scan_request *request;
183 /* Are agents full? */
184 if (atomic_read(&cdt->cdt_request_count) >=
185 cdt->cdt_max_requests)
188 /* first search whether the request is found in the
189 * list we have built. */
191 for (i = 0; i < hsd->request_cnt; i++) {
192 if (hsd->request[i].hal->hal_compound_id ==
193 larr->arr_compound_id) {
194 request = &hsd->request[i];
200 struct hsm_action_list *hal;
202 if (hsd->request_cnt == hsd->max_requests)
203 /* Unknown request and no more room
204 * for a new request. Continue to scan
205 * to find other entries for already
210 request = &hsd->request[hsd->request_cnt];
212 /* allocates hai vector size just needs to be large
215 sizeof(*request->hal) +
216 cfs_size_round(MTI_NAME_MAXLEN+1) +
217 2 * cfs_size_round(larr->arr_hai.hai_len);
218 OBD_ALLOC(hal, request->hal_sz);
221 hal->hal_version = HAL_VERSION;
222 strlcpy(hal->hal_fsname, hsd->fs_name,
223 MTI_NAME_MAXLEN + 1);
224 hal->hal_compound_id = larr->arr_compound_id;
225 hal->hal_archive_id = larr->arr_archive_id;
226 hal->hal_flags = larr->arr_flags;
228 request->hal_used_sz = hal_size(hal);
231 hai = hai_first(hal);
233 /* request is known */
234 /* we check if record archive num is the same as the
235 * known request, if not we will serve it in multiple
236 * time because we do not know if the agent can serve
238 * a use case is a compound made of multiple restore
239 * where the files are not archived in the same backend
241 if (larr->arr_archive_id !=
242 request->hal->hal_archive_id)
245 if (request->hal_sz <
246 request->hal_used_sz +
247 cfs_size_round(larr->arr_hai.hai_len)) {
248 /* Not enough room, need an extension */
252 sz = 2 * request->hal_sz;
253 OBD_ALLOC(hal_buffer, sz);
256 memcpy(hal_buffer, request->hal,
257 request->hal_used_sz);
258 OBD_FREE(request->hal,
260 request->hal = hal_buffer;
261 request->hal_sz = sz;
263 hai = hai_first(request->hal);
264 for (i = 0; i < request->hal->hal_count; i++)
267 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
268 hai->hai_cookie = larr->arr_hai.hai_cookie;
269 hai->hai_gid = larr->arr_hai.hai_gid;
271 request->hal_used_sz += cfs_size_round(hai->hai_len);
272 request->hal->hal_count++;
276 struct hsm_progress_kernel pgs;
277 struct cdt_agent_req *car;
278 cfs_time_t now = cfs_time_current_sec();
281 /* we search for a running request
282 * error may happen if coordinator crashes or stopped
283 * with running request
285 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie);
287 last = larr->arr_req_change;
289 last = car->car_req_update;
290 mdt_cdt_put_request(car);
293 /* test if request too long, if yes cancel it
294 * the same way the copy tool acknowledge a cancel request */
295 if (now <= last + cdt->cdt_active_req_timeout)
298 dump_llog_agent_req_rec("request timed out, start cleaning",
300 /* a too old cancel request just needs to be removed
301 * this can happen, if copy tool does not support
302 * cancel for other requests, we have to remove the
303 * running request and notify the copytool */
304 pgs.hpk_fid = larr->arr_hai.hai_fid;
305 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
306 pgs.hpk_extent = larr->arr_hai.hai_extent;
307 pgs.hpk_flags = HP_FLAG_COMPLETED;
308 pgs.hpk_errval = ENOSYS;
309 pgs.hpk_data_version = 0;
311 /* update request state, but do not record in llog, to
312 * avoid deadlock on cdt_llog_lock */
313 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
315 CERROR("%s: cannot cleanup timed out request: "
316 DFID" for cookie %#llx action=%s\n",
318 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
319 hsm_copytool_action2name(
320 larr->arr_hai.hai_action));
323 /* The request no longer exists, forget
324 * about it, and do not send a cancel request
325 * to the client, for which an error will be
326 * sent back, leading to an endless cycle of
328 RETURN(LLOG_DEL_RECORD);
331 /* XXX A cancel request cannot be cancelled. */
332 if (larr->arr_hai.hai_action == HSMA_CANCEL)
335 larr->arr_status = ARS_CANCELED;
336 larr->arr_req_change = now;
337 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
339 CERROR("%s: cannot update agent log: rc = %d\n",
340 mdt_obd_name(mdt), rc);
346 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
347 cfs_time_current_sec())
348 RETURN(LLOG_DEL_RECORD);
355 * create /proc entries for coordinator
358 * \retval -ve failure
360 int hsm_cdt_procfs_init(struct mdt_device *mdt)
362 struct coordinator *cdt = &mdt->mdt_coordinator;
366 /* init /proc entries, failure is not critical */
367 cdt->cdt_proc_dir = lprocfs_register("hsm",
368 mdt2obd_dev(mdt)->obd_proc_entry,
369 lprocfs_mdt_hsm_vars, mdt);
370 if (IS_ERR(cdt->cdt_proc_dir)) {
371 rc = PTR_ERR(cdt->cdt_proc_dir);
372 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
373 " rc=%d\n", mdt_obd_name(mdt), rc);
374 cdt->cdt_proc_dir = NULL;
382 * remove /proc entries for coordinator
385 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
387 struct coordinator *cdt = &mdt->mdt_coordinator;
389 if (cdt->cdt_proc_dir != NULL)
390 lprocfs_remove(&cdt->cdt_proc_dir);
394 * get vector of hsm cdt /proc vars
398 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
400 return lprocfs_mdt_hsm_vars;
403 /* Release the ressource used by the coordinator. Called when the
404 * coordinator is stopping. */
405 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
407 struct coordinator *cdt = &mdt->mdt_coordinator;
408 struct cdt_agent_req *car, *tmp1;
409 struct hsm_agent *ha, *tmp2;
410 struct cdt_restore_handle *crh, *tmp3;
411 struct mdt_thread_info *cdt_mti;
414 down_write(&cdt->cdt_request_lock);
415 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
417 cfs_hash_del(cdt->cdt_request_cookie_hash,
418 &car->car_hai->hai_cookie,
419 &car->car_cookie_hash);
420 list_del(&car->car_request_list);
421 mdt_cdt_put_request(car);
423 up_write(&cdt->cdt_request_lock);
425 down_write(&cdt->cdt_agent_lock);
426 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
427 list_del(&ha->ha_list);
430 up_write(&cdt->cdt_agent_lock);
432 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
433 mutex_lock(&cdt->cdt_restore_lock);
434 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
435 struct mdt_object *child;
437 /* give back layout lock */
438 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
440 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
442 list_del(&crh->crh_list);
444 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
446 mutex_unlock(&cdt->cdt_restore_lock);
450 * Coordinator state transition table, indexed on enum cdt_states, taking
451 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
452 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
454 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
455 /* from -> to: stopped init running disable stopping */
456 /* stopped */ { true, true, false, false, false },
457 /* init */ { true, false, true, false, true },
458 /* running */ { false, false, true, true, true },
459 /* disable */ { false, false, true, true, true },
460 /* stopping */ { true, false, false, false, true }
464 * Change coordinator thread state
465 * Some combinations are not valid, so catch them here.
467 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
468 * the transition was not possible.
470 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state,
471 enum cdt_states *old_state)
474 enum cdt_states state;
476 spin_lock(&cdt->cdt_state_lock);
478 state = cdt->cdt_state;
480 if (cdt_transition[state][new_state]) {
481 cdt->cdt_state = new_state;
482 spin_unlock(&cdt->cdt_state_lock);
487 spin_unlock(&cdt->cdt_state_lock);
489 "unexpected coordinator transition, from=%s, to=%s\n",
490 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
499 * \param data [IN] obd device
501 * \retval -ve failure
503 static int mdt_coordinator(void *data)
505 struct mdt_thread_info *mti = data;
506 struct mdt_device *mdt = mti->mti_mdt;
507 struct coordinator *cdt = &mdt->mdt_coordinator;
508 struct hsm_scan_data hsd = { NULL };
513 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
514 mdt_obd_name(mdt), current_pid());
516 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
517 * increases due to a change from /proc we do not overflow the
518 * hsd.request[] vector
520 hsd.max_requests = cdt->cdt_max_requests;
521 request_sz = hsd.max_requests * sizeof(*hsd.request);
522 OBD_ALLOC_LARGE(hsd.request, request_sz);
524 GOTO(out, rc = -ENOMEM);
527 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
529 set_cdt_state(cdt, CDT_RUNNING, NULL);
531 /* Inform mdt_hsm_cdt_start(). */
532 wake_up_all(&cdt->cdt_waitq);
535 struct l_wait_info lwi;
538 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
540 l_wait_event(cdt->cdt_waitq,
541 (cdt->cdt_flags & SVC_EVENT) ||
542 (cdt->cdt_state == CDT_STOPPING),
545 CDEBUG(D_HSM, "coordinator resumes\n");
547 if (cdt->cdt_state == CDT_STOPPING) {
552 /* wake up before timeout, new work arrives */
553 if (cdt->cdt_flags & SVC_EVENT)
554 cdt->cdt_flags &= ~SVC_EVENT;
556 /* if coordinator is suspended continue to wait */
557 if (cdt->cdt_state == CDT_DISABLE) {
558 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
562 CDEBUG(D_HSM, "coordinator starts reading llog\n");
564 if (hsd.max_requests != cdt->cdt_max_requests) {
565 /* cdt_max_requests has changed,
566 * we need to allocate a new buffer
568 OBD_FREE_LARGE(hsd.request, request_sz);
569 hsd.max_requests = cdt->cdt_max_requests;
570 request_sz = hsd.max_requests * sizeof(*hsd.request);
571 OBD_ALLOC_LARGE(hsd.request, request_sz);
580 rc = cdt_llog_process(mti->mti_env, mdt,
581 mdt_coordinator_cb, &hsd);
585 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
587 if (list_empty(&cdt->cdt_agents)) {
588 CDEBUG(D_HSM, "no agent available, "
589 "coordinator sleeps\n");
593 /* here hsd contains a list of requests to be started */
594 for (i = 0; i < hsd.request_cnt; i++) {
595 struct hsm_scan_request *request = &hsd.request[i];
596 struct hsm_action_list *hal = request->hal;
597 struct hsm_action_item *hai;
600 enum agent_req_status status;
602 /* still room for work ? */
603 if (atomic_read(&cdt->cdt_request_count) >=
604 cdt->cdt_max_requests)
607 rc = mdt_hsm_agent_send(mti, hal, 0);
608 /* if failure, we suppose it is temporary
609 * if the copy tool failed to do the request
610 * it has to use hsm_progress
612 status = (rc ? ARS_WAITING : ARS_STARTED);
614 /* set up cookie vector to set records status
615 * after copy tools start or failed
617 sz = hal->hal_count * sizeof(__u64);
618 OBD_ALLOC(cookies, sz);
622 hai = hai_first(hal);
623 for (j = 0; j < hal->hal_count; j++) {
624 cookies[j] = hai->hai_cookie;
628 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
629 hal->hal_count, status);
631 CERROR("%s: mdt_agent_record_update() failed, "
632 "rc=%d, cannot update status to %s "
634 mdt_obd_name(mdt), rc,
635 agent_req_status2name(status),
638 OBD_FREE(cookies, sz);
641 /* free hal allocated by callback */
642 for (i = 0; i < hsd.request_cnt; i++) {
643 struct hsm_scan_request *request = &hsd.request[i];
645 OBD_FREE(request->hal, request->hal_sz);
650 set_cdt_state(cdt, CDT_STOPPING, NULL);
653 OBD_FREE_LARGE(hsd.request, request_sz);
655 mdt_hsm_cdt_cleanup(mdt);
657 set_cdt_state(cdt, CDT_STOPPED, NULL);
658 wake_up_all(&cdt->cdt_waitq);
661 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
662 mdt_obd_name(mdt), current_pid(), rc);
664 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
666 mdt_obd_name(mdt), current_pid());
672 * lookup a restore handle by FID
673 * caller needs to hold cdt_restore_lock
674 * \param cdt [IN] coordinator
675 * \param fid [IN] FID
676 * \retval cdt_restore_handle found
677 * \retval NULL not found
679 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
680 const struct lu_fid *fid)
682 struct cdt_restore_handle *crh;
685 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
686 if (lu_fid_eq(&crh->crh_fid, fid))
693 * data passed to llog_cat_process() callback
694 * to scan requests and take actions
696 struct hsm_restore_data {
697 struct mdt_thread_info *hrd_mti;
701 * llog_cat_process() callback, used to:
702 * - find restore request and allocate the restore handle
703 * \param env [IN] environment
704 * \param llh [IN] llog handle
705 * \param hdr [IN] llog record
706 * \param data [IN/OUT] cb data = struct hsm_restore_data
708 * \retval -ve failure
710 static int hsm_restore_cb(const struct lu_env *env,
711 struct llog_handle *llh,
712 struct llog_rec_hdr *hdr, void *data)
714 struct llog_agent_req_rec *larr;
715 struct hsm_restore_data *hrd;
716 struct cdt_restore_handle *crh;
717 struct hsm_action_item *hai;
718 struct mdt_thread_info *mti;
719 struct coordinator *cdt;
720 struct mdt_object *child;
726 cdt = &mti->mti_mdt->mdt_coordinator;
728 larr = (struct llog_agent_req_rec *)hdr;
729 hai = &larr->arr_hai;
730 if (hai->hai_cookie > cdt->cdt_last_cookie)
731 /* update the cookie to avoid collision */
732 cdt->cdt_last_cookie = hai->hai_cookie + 1;
734 if (hai->hai_action != HSMA_RESTORE ||
735 agent_req_in_final_state(larr->arr_status))
738 /* restore request not in a final state */
740 /* force replay of restore requests left in started state from previous
741 * CDT context, to be canceled later if finally found to be incompatible
742 * when being re-started */
743 if (larr->arr_status == ARS_STARTED) {
744 larr->arr_status = ARS_WAITING;
745 larr->arr_req_change = cfs_time_current_sec();
746 rc = llog_write(env, llh, hdr, hdr->lrh_index);
751 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
755 crh->crh_fid = hai->hai_fid;
756 /* in V1 all file is restored
757 crh->extent.start = hai->hai_extent.offset;
758 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
760 crh->crh_extent.start = 0;
761 crh->crh_extent.end = hai->hai_extent.length;
762 /* get the layout lock */
763 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
764 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
765 MDS_INODELOCK_LAYOUT);
767 GOTO(out, rc = PTR_ERR(child));
770 /* we choose to not keep a reference
771 * on the object during the restore time which can be very long */
772 mdt_object_put(mti->mti_env, child);
774 mutex_lock(&cdt->cdt_restore_lock);
775 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
776 mutex_unlock(&cdt->cdt_restore_lock);
783 * restore coordinator state at startup
784 * the goal is to take a layout lock for each registered restore request
785 * \param mti [IN] context
787 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
789 struct hsm_restore_data hrd;
795 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
796 hsm_restore_cb, &hrd);
801 static int hsm_init_ucred(struct lu_ucred *uc)
805 uc->uc_valid = UCRED_OLD;
814 uc->uc_suppgids[0] = -1;
815 uc->uc_suppgids[1] = -1;
816 uc->uc_cap = CFS_CAP_FS_MASK;
819 uc->uc_identity = NULL;
825 * wake up coordinator thread
826 * \param mdt [IN] device
828 * \retval -ve failure
830 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
832 struct coordinator *cdt = &mdt->mdt_coordinator;
835 if (cdt->cdt_state == CDT_STOPPED)
838 /* wake up coordinator */
839 cdt->cdt_flags = SVC_EVENT;
840 wake_up_all(&cdt->cdt_waitq);
846 * initialize coordinator struct
847 * \param mdt [IN] device
849 * \retval -ve failure
851 int mdt_hsm_cdt_init(struct mdt_device *mdt)
853 struct coordinator *cdt = &mdt->mdt_coordinator;
854 struct mdt_thread_info *cdt_mti = NULL;
858 init_waitqueue_head(&cdt->cdt_waitq);
859 mutex_init(&cdt->cdt_llog_lock);
860 init_rwsem(&cdt->cdt_agent_lock);
861 init_rwsem(&cdt->cdt_request_lock);
862 mutex_init(&cdt->cdt_restore_lock);
863 spin_lock_init(&cdt->cdt_state_lock);
864 cdt->cdt_state = CDT_STOPPED;
866 INIT_LIST_HEAD(&cdt->cdt_request_list);
867 INIT_LIST_HEAD(&cdt->cdt_agents);
868 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
870 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
877 &cdt_request_cookie_hash_ops,
879 if (cdt->cdt_request_cookie_hash == NULL)
882 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
884 GOTO(out_request_cookie_hash, rc);
886 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
887 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
891 lu_context_enter(&cdt->cdt_session);
892 cdt->cdt_env.le_ses = &cdt->cdt_session;
894 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
895 LASSERT(cdt_mti != NULL);
897 cdt_mti->mti_env = &cdt->cdt_env;
898 cdt_mti->mti_mdt = mdt;
900 hsm_init_ucred(mdt_ucred(cdt_mti));
902 /* default values for /proc tunnables
903 * can be override by MGS conf */
904 cdt->cdt_default_archive_id = 1;
905 cdt->cdt_grace_delay = 60;
906 cdt->cdt_loop_period = 10;
907 cdt->cdt_max_requests = 3;
908 cdt->cdt_policy = CDT_DEFAULT_POLICY;
909 cdt->cdt_active_req_timeout = 3600;
914 lu_env_fini(&cdt->cdt_env);
915 out_request_cookie_hash:
916 cfs_hash_putref(cdt->cdt_request_cookie_hash);
917 cdt->cdt_request_cookie_hash = NULL;
923 * free a coordinator thread
924 * \param mdt [IN] device
926 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
928 struct coordinator *cdt = &mdt->mdt_coordinator;
931 lu_context_exit(cdt->cdt_env.le_ses);
932 lu_context_fini(cdt->cdt_env.le_ses);
934 lu_env_fini(&cdt->cdt_env);
936 cfs_hash_putref(cdt->cdt_request_cookie_hash);
937 cdt->cdt_request_cookie_hash = NULL;
943 * start a coordinator thread
944 * \param mdt [IN] device
946 * \retval -ve failure
948 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
950 struct coordinator *cdt = &mdt->mdt_coordinator;
953 struct mdt_thread_info *cdt_mti;
954 struct task_struct *task;
957 /* functions defined but not yet used
958 * this avoid compilation warning
962 rc = set_cdt_state(cdt, CDT_INIT, NULL);
964 CERROR("%s: Coordinator already started or stopping\n",
969 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
970 cdt->cdt_policy = CDT_DEFAULT_POLICY;
972 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
973 /* just need to be larger than previous one */
974 /* cdt_last_cookie is protected by cdt_llog_lock */
975 cdt->cdt_last_cookie = cfs_time_current_sec();
976 atomic_set(&cdt->cdt_request_count, 0);
977 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
978 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
979 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
981 /* to avoid deadlock when start is made through /proc
982 * /proc entries are created by the coordinator thread */
984 /* set up list of started restore requests */
985 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
986 rc = mdt_hsm_pending_restore(cdt_mti);
988 CERROR("%s: cannot take the layout locks needed"
989 " for registered restore: %d\n",
990 mdt_obd_name(mdt), rc);
992 if (mdt->mdt_bottom->dd_rdonly)
995 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
998 set_cdt_state(cdt, CDT_STOPPED, NULL);
999 CERROR("%s: error starting coordinator thread: %d\n",
1000 mdt_obd_name(mdt), rc);
1002 wait_event(cdt->cdt_waitq,
1003 cdt->cdt_state != CDT_INIT);
1004 if (cdt->cdt_state == CDT_RUNNING) {
1005 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1010 "%s: coordinator thread failed to start\n",
1020 * stop a coordinator thread
1021 * \param mdt [IN] device
1023 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1025 struct coordinator *cdt = &mdt->mdt_coordinator;
1029 /* stop coordinator thread */
1030 rc = set_cdt_state(cdt, CDT_STOPPING, NULL);
1034 wake_up_all(&cdt->cdt_waitq);
1035 wait_event(cdt->cdt_waitq, cdt->cdt_state != CDT_STOPPING);
1040 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1041 const struct lu_fid *fid,
1044 struct mdt_object *obj;
1048 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1050 GOTO(out, rc = PTR_ERR(obj));
1052 if (mh.mh_flags & HS_EXISTS &&
1053 mh.mh_arch_id == archive_id)
1054 GOTO(out_obj, rc = 0);
1056 mh.mh_flags |= HS_EXISTS;
1057 mh.mh_arch_id = archive_id;
1058 rc = mdt_hsm_attr_set(mti, obj, &mh);
1061 mdt_object_put(mti->mti_env, obj);
1067 * register all requests from an hal in the memory list
1068 * \param mti [IN] context
1069 * \param hal [IN] request
1070 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1071 * which is running the CT
1073 * \retval -ve failure
1075 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1076 struct hsm_action_list *hal, struct obd_uuid *uuid)
1078 struct mdt_device *mdt = mti->mti_mdt;
1079 struct coordinator *cdt = &mdt->mdt_coordinator;
1080 struct hsm_action_item *hai;
1084 /* register request in memory list */
1085 hai = hai_first(hal);
1086 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1087 struct cdt_agent_req *car;
1089 /* in case of a cancel request, we first mark the ondisk
1090 * record of the request we want to stop as canceled
1091 * this does not change the cancel record
1092 * it will be done when updating the request status
1094 if (hai->hai_action == HSMA_CANCEL) {
1095 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1099 CERROR("%s: mdt_agent_record_update() failed, "
1100 "rc=%d, cannot update status to %s "
1101 "for cookie %#llx\n",
1102 mdt_obd_name(mdt), rc,
1103 agent_req_status2name(ARS_CANCELED),
1108 /* find the running request to set it canceled */
1109 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1111 car->car_canceled = 1;
1112 /* uuid has to be changed to the one running the
1113 * request to cancel */
1114 *uuid = car->car_uuid;
1115 mdt_cdt_put_request(car);
1117 /* no need to memorize cancel request
1118 * this also avoid a deadlock when we receive
1119 * a purge all requests command
1124 if (hai->hai_action == HSMA_ARCHIVE) {
1125 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1126 hal->hal_archive_id);
1133 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1134 hal->hal_archive_id, hal->hal_flags,
1137 GOTO(out, rc = PTR_ERR(car));
1139 rc = mdt_cdt_add_request(cdt, car);
1141 mdt_cdt_free_request(car);
1148 * swap layouts between 2 fids
1149 * \param mti [IN] context
1152 * \param mh_common [IN] MD HSM
1154 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1155 struct mdt_object *obj, const struct lu_fid *dfid,
1156 struct md_hsm *mh_common)
1158 struct mdt_object *dobj;
1159 struct mdt_lock_handle *dlh;
1163 if (!mdt_object_exists(obj))
1164 GOTO(out, rc = -ENOENT);
1166 /* we already have layout lock on obj so take only
1168 dlh = &mti->mti_lh[MDT_LH_OLD];
1169 mdt_lock_reg_init(dlh, LCK_EX);
1170 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1172 GOTO(out, rc = PTR_ERR(dobj));
1174 /* if copy tool closes the volatile before sending the final
1175 * progress through llapi_hsm_copy_end(), all the objects
1176 * are removed and mdd_swap_layout LBUG */
1177 if (!mdt_object_exists(dobj)) {
1178 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1179 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1180 GOTO(out_dobj, rc = -ENOENT);
1182 /* Since we only handle restores here, unconditionally use
1183 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1184 * be preserved in case of failure during swap_layout and not
1185 * leave a file in an intermediate but incoherent state.
1186 * But need to setup HSM xattr of data FID before, reuse
1187 * mti and mh presets for FID in hsm_cdt_request_completed(),
1188 * only need to clear RELEASED and DIRTY.
1190 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1191 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1193 rc = mo_swap_layouts(mti->mti_env,
1194 mdt_object_child(obj),
1195 mdt_object_child(dobj),
1196 SWAP_LAYOUTS_MDS_HSM);
1199 mdt_object_unlock_put(mti, dobj, dlh, 1);
1205 * update status of a completed request
1206 * \param mti [IN] context
1207 * \param pgs [IN] progress of the copy tool
1208 * \param update_record [IN] update llog record
1210 * \retval -ve failure
1212 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1213 struct hsm_progress_kernel *pgs,
1214 const struct cdt_agent_req *car,
1215 enum agent_req_status *status)
1217 const struct lu_env *env = mti->mti_env;
1218 struct mdt_device *mdt = mti->mti_mdt;
1219 struct coordinator *cdt = &mdt->mdt_coordinator;
1220 struct mdt_object *obj = NULL;
1221 int cl_flags = 0, rc = 0;
1224 bool need_changelog = true;
1227 /* default is to retry */
1228 *status = ARS_WAITING;
1230 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1231 * if error/removed continue anyway to get correct reporting done */
1232 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1233 /* we will update MD HSM only if needed */
1234 is_mh_changed = false;
1236 /* no need to change mh->mh_arch_id
1237 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1239 if (pgs->hpk_errval != 0) {
1240 switch (pgs->hpk_errval) {
1242 /* the copy tool does not support cancel
1243 * so the cancel request is failed
1244 * As we cannot distinguish a cancel progress
1245 * from another action progress (they have the
1246 * same cookie), we suppose here the CT returns
1247 * ENOSYS only if does not support cancel
1249 /* this can also happen when cdt calls it to
1250 * for a timed out request */
1251 *status = ARS_FAILED;
1252 /* to have a cancel event in changelog */
1253 pgs->hpk_errval = ECANCELED;
1256 /* the request record has already been set to
1257 * ARS_CANCELED, this set the cancel request
1259 *status = ARS_SUCCEED;
1262 /* retry only if current policy or requested, and
1263 * object is not on error/removed */
1264 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1265 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1266 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1270 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1271 CERROR("%s: Request %#llx on "DFID
1272 " failed, error code %d too large\n",
1274 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1276 hsm_set_cl_error(&cl_flags,
1277 CLF_HSM_ERROVERFLOW);
1280 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1283 switch (car->car_hai->hai_action) {
1285 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1288 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1291 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1294 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1295 CERROR("%s: Failed request %#llx on "DFID
1296 " cannot be a CANCEL\n",
1299 PFID(&pgs->hpk_fid));
1302 CERROR("%s: Failed request %#llx on "DFID
1303 " %d is an unknown action\n",
1305 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1306 car->car_hai->hai_action);
1311 *status = ARS_SUCCEED;
1312 switch (car->car_hai->hai_action) {
1314 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1315 /* set ARCHIVE keep EXIST and clear LOST and
1317 mh.mh_arch_ver = pgs->hpk_data_version;
1318 mh.mh_flags |= HS_ARCHIVED;
1319 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1320 is_mh_changed = true;
1323 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1325 /* do not clear RELEASED and DIRTY here
1326 * this will occur in hsm_swap_layouts()
1329 /* Restoring has changed the file version on
1331 mh.mh_arch_ver = pgs->hpk_data_version;
1332 is_mh_changed = true;
1335 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1336 /* clear ARCHIVED EXISTS and LOST */
1337 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1338 is_mh_changed = true;
1341 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1342 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1345 PFID(&pgs->hpk_fid));
1348 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1350 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1351 car->car_hai->hai_action);
1357 /* rc != 0 means error when analysing action, it may come from
1358 * a crasy CT no need to manage DIRTY
1361 hsm_set_cl_flags(&cl_flags,
1362 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1364 /* unlock is done later, after layout lock management */
1365 if (is_mh_changed && !IS_ERR(obj))
1366 rc = mdt_hsm_attr_set(mti, obj, &mh);
1368 /* we give back layout lock only if restore was successful or
1369 * if no retry will be attempted and if object is still alive,
1370 * in other cases we just unlock the object */
1371 if (car->car_hai->hai_action == HSMA_RESTORE) {
1372 struct cdt_restore_handle *crh;
1374 /* restore in data FID done, we swap the layouts
1375 * only if restore is successful */
1376 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1377 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1380 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1381 *status = ARS_FAILED;
1382 pgs->hpk_errval = -rc;
1385 /* we have to retry, so keep layout lock */
1386 if (*status == ARS_WAITING)
1389 /* restore special case, need to create ChangeLog record
1390 * before to give back layout lock to avoid concurrent
1391 * file updater to post out of order ChangeLog */
1392 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1393 &car->car_hai->hai_fid);
1394 need_changelog = false;
1396 /* give back layout lock */
1397 mutex_lock(&cdt->cdt_restore_lock);
1398 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1400 list_del(&crh->crh_list);
1401 mutex_unlock(&cdt->cdt_restore_lock);
1402 /* Just give back layout lock, we keep the reference
1403 * which is given back later with the lock for HSM
1405 * XXX obj may be invalid so we do not pass it. */
1407 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1410 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1416 /* always add a ChangeLog record */
1418 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1419 &car->car_hai->hai_fid);
1422 mdt_object_put(mti->mti_env, obj);
1428 * update status of a request
1429 * \param mti [IN] context
1430 * \param pgs [IN] progress of the copy tool
1431 * \param update_record [IN] update llog record
1433 * \retval -ve failure
1435 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1436 struct hsm_progress_kernel *pgs,
1437 const int update_record)
1439 struct mdt_device *mdt = mti->mti_mdt;
1440 struct coordinator *cdt = &mdt->mdt_coordinator;
1441 struct cdt_agent_req *car;
1445 /* no coordinator started, so we cannot serve requests */
1446 if (cdt->cdt_state == CDT_STOPPED)
1449 /* first do sanity checks */
1450 car = mdt_cdt_update_request(cdt, pgs);
1452 CERROR("%s: Cannot find running request for cookie %#llx"
1455 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1457 RETURN(PTR_ERR(car));
1460 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1461 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1462 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1463 hsm_copytool_action2name(car->car_hai->hai_action),
1464 pgs->hpk_flags, pgs->hpk_errval,
1465 PFID(&car->car_hai->hai_fid),
1466 PFID(&car->car_hai->hai_dfid));
1468 /* progress is done on FID or data FID depending of the action and
1469 * of the copy progress */
1470 /* for restore progress is used to send back the data FID to cdt */
1471 if (car->car_hai->hai_action == HSMA_RESTORE &&
1472 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1473 car->car_hai->hai_dfid = pgs->hpk_fid;
1475 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1476 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1477 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1478 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1479 CERROR("%s: Progress on "DFID" for cookie %#llx"
1480 " does not match request FID "DFID" nor data FID "
1483 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1484 PFID(&car->car_hai->hai_fid),
1485 PFID(&car->car_hai->hai_dfid));
1486 GOTO(out, rc = -EINVAL);
1489 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1490 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1491 " is not coherent (err=%d and not completed"
1494 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1495 hsm_copytool_action2name(car->car_hai->hai_action),
1496 pgs->hpk_errval, pgs->hpk_flags);
1497 GOTO(out, rc = -EINVAL);
1500 /* now progress is valid */
1502 /* we use a root like ucred */
1503 hsm_init_ucred(mdt_ucred(mti));
1505 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1506 enum agent_req_status status;
1508 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1510 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1512 update_record ? "Updating" : "Not updating",
1513 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1514 hsm_copytool_action2name(car->car_hai->hai_action),
1515 agent_req_status2name(status));
1517 /* update record first (LU-9075) */
1518 if (update_record) {
1521 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1522 &pgs->hpk_cookie, 1,
1525 CERROR("%s: mdt_agent_record_update() failed,"
1526 " rc=%d, cannot update status to %s"
1527 " for cookie %#llx\n",
1528 mdt_obd_name(mdt), rc1,
1529 agent_req_status2name(status),
1531 rc = (rc != 0 ? rc : rc1);
1534 /* then remove request from memory list (LU-9075) */
1535 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1537 /* ct has completed a request, so a slot is available, wakeup
1538 * cdt to find new work */
1539 mdt_hsm_cdt_wakeup(mdt);
1541 /* if copytool send a progress on a canceled request
1542 * we inform copytool it should stop
1544 if (car->car_canceled == 1)
1550 /* remove ref got from mdt_cdt_update_request() */
1551 mdt_cdt_put_request(car);
1558 * data passed to llog_cat_process() callback
1559 * to cancel requests
1561 struct hsm_cancel_all_data {
1562 struct mdt_device *mdt;
1566 * llog_cat_process() callback, used to:
1567 * - purge all requests
1568 * \param env [IN] environment
1569 * \param llh [IN] llog handle
1570 * \param hdr [IN] llog record
1571 * \param data [IN] cb data = struct hsm_cancel_all_data
1573 * \retval -ve failure
1575 static int mdt_cancel_all_cb(const struct lu_env *env,
1576 struct llog_handle *llh,
1577 struct llog_rec_hdr *hdr, void *data)
1579 struct llog_agent_req_rec *larr;
1580 struct hsm_cancel_all_data *hcad;
1584 larr = (struct llog_agent_req_rec *)hdr;
1586 if (larr->arr_status == ARS_WAITING ||
1587 larr->arr_status == ARS_STARTED) {
1588 larr->arr_status = ARS_CANCELED;
1589 larr->arr_req_change = cfs_time_current_sec();
1590 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1597 * cancel all actions
1598 * \param obd [IN] MDT device
1600 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1603 struct lu_context session;
1604 struct mdt_thread_info *mti;
1605 struct coordinator *cdt = &mdt->mdt_coordinator;
1606 struct cdt_agent_req *car;
1607 struct hsm_action_list *hal = NULL;
1608 struct hsm_action_item *hai;
1609 struct hsm_cancel_all_data hcad;
1610 int hal_sz = 0, hal_len, rc;
1611 enum cdt_states old_state;
1614 rc = lu_env_init(&env, LCT_MD_THREAD);
1618 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1619 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1623 lu_context_enter(&session);
1624 env.le_ses = &session;
1626 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1627 LASSERT(mti != NULL);
1629 mti->mti_env = &env;
1632 hsm_init_ucred(mdt_ucred(mti));
1634 /* disable coordinator */
1635 rc = set_cdt_state(cdt, CDT_DISABLE, &old_state);
1639 /* send cancel to all running requests */
1640 down_read(&cdt->cdt_request_lock);
1641 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1642 mdt_cdt_get_request(car);
1643 /* request is not yet removed from list, it will be done
1644 * when copytool will return progress
1647 if (car->car_hai->hai_action == HSMA_CANCEL) {
1648 mdt_cdt_put_request(car);
1653 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1654 cfs_size_round(car->car_hai->hai_len);
1656 if (hal_len > hal_sz && hal_sz > 0) {
1657 /* not enough room, free old buffer */
1658 OBD_FREE(hal, hal_sz);
1662 /* empty buffer, allocate one */
1665 OBD_ALLOC(hal, hal_sz);
1667 mdt_cdt_put_request(car);
1668 up_read(&cdt->cdt_request_lock);
1669 GOTO(out_cdt_state, rc = -ENOMEM);
1673 hal->hal_version = HAL_VERSION;
1674 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1676 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1677 hal->hal_compound_id = car->car_compound_id;
1678 hal->hal_archive_id = car->car_archive_id;
1679 hal->hal_flags = car->car_flags;
1682 hai = hai_first(hal);
1683 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1684 hai->hai_action = HSMA_CANCEL;
1687 /* it is possible to safely call mdt_hsm_agent_send()
1688 * (ie without a deadlock on cdt_request_lock), because the
1689 * write lock is taken only if we are not in purge mode
1690 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1691 * nor mdt_cdt_remove_request())
1693 /* no conflict with cdt thread because cdt is disable and we
1694 * have the request lock */
1695 mdt_hsm_agent_send(mti, hal, 1);
1697 mdt_cdt_put_request(car);
1699 up_read(&cdt->cdt_request_lock);
1702 OBD_FREE(hal, hal_sz);
1704 /* cancel all on-disk records */
1707 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1708 mdt_cancel_all_cb, &hcad);
1711 /* Enable coordinator, unless the coordinator was stopping. */
1712 set_cdt_state(cdt, old_state, NULL);
1713 lu_context_exit(&session);
1714 lu_context_fini(&session);
1722 * check if a request is compatible with file status
1723 * \param hai [IN] request description
1724 * \param archive_id [IN] request archive id
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 u32 archive_id, 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)))
1744 if (hsm_flags & HS_EXISTS &&
1746 archive_id != hsm->mh_arch_id)
1751 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1752 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1756 if (!(hsm_flags & HS_RELEASED) &&
1757 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1764 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1765 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1766 PFID(&hai->hai_fid),
1767 hsm_copytool_action2name(hai->hai_action), rq_flags,
1768 hai->hai_extent.offset, hai->hai_extent.length,
1770 (is_compat ? "compatible" : "uncompatible"));
1776 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1778 static const struct {
1782 } hsm_policy_names[] = {
1783 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1784 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1789 * convert a policy name to a bit
1790 * \param name [IN] policy name
1792 * \retval policy bit
1794 static __u64 hsm_policy_str2bit(const char *name)
1798 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1799 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1800 strcmp(hsm_policy_names[i].name, name) == 0)
1801 return hsm_policy_names[i].bit;
1806 * convert a policy bit field to a string
1807 * \param mask [IN] policy bit field
1808 * \param hexa [IN] print mask before bit names
1809 * \param buffer [OUT] string
1810 * \param count [IN] size of buffer
1812 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1820 seq_printf(m, "(%#llx) ", mask);
1822 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1825 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1826 if (hsm_policy_names[j].bit == bit)
1830 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1832 seq_printf(m, "%s ", hsm_policy_names[j].name);
1834 /* remove last ' ' */
1839 /* methods to read/write HSM policy flags */
1840 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1842 struct mdt_device *mdt = m->private;
1843 struct coordinator *cdt = &mdt->mdt_coordinator;
1846 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1851 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1852 size_t count, loff_t *off)
1854 struct seq_file *m = file->private_data;
1855 struct mdt_device *mdt = m->private;
1856 struct coordinator *cdt = &mdt->mdt_coordinator;
1857 char *start, *token, sign;
1860 __u64 add_mask, remove_mask, set_mask;
1864 if (count + 1 > PAGE_SIZE)
1867 OBD_ALLOC(buf, count + 1);
1871 if (copy_from_user(buf, buffer, count))
1872 GOTO(out, rc = -EFAULT);
1877 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1880 add_mask = remove_mask = set_mask = 0;
1882 token = strsep(&start, "\n ");
1888 if (sign == '-' || sign == '+')
1891 policy = hsm_policy_str2bit(token);
1893 CWARN("%s: '%s' is unknown, "
1894 "supported policies are:\n", mdt_obd_name(mdt),
1896 hsm_policy_bit2str(m, 0, false);
1897 GOTO(out, rc = -EINVAL);
1901 remove_mask |= policy;
1911 } while (start != NULL);
1913 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1914 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1916 /* if no sign in all string, it is a clear and set
1917 * if some sign found, all unsigned are converted
1919 * P1 P2 = set to P1 and P2
1920 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1922 if (remove_mask == 0 && add_mask == 0) {
1923 cdt->cdt_policy = set_mask;
1925 cdt->cdt_policy |= set_mask | add_mask;
1926 cdt->cdt_policy &= ~remove_mask;
1929 GOTO(out, rc = count);
1932 OBD_FREE(buf, count + 1);
1935 LPROC_SEQ_FOPS(mdt_hsm_policy);
1937 #define GENERATE_PROC_METHOD(VAR) \
1938 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1940 struct mdt_device *mdt = m->private; \
1941 struct coordinator *cdt = &mdt->mdt_coordinator; \
1944 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1948 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1949 size_t count, loff_t *off) \
1952 struct seq_file *m = file->private_data; \
1953 struct mdt_device *mdt = m->private; \
1954 struct coordinator *cdt = &mdt->mdt_coordinator; \
1959 rc = lprocfs_str_to_s64(buffer, count, &val); \
1962 if (val > 0 && val < INT_MAX) { \
1969 GENERATE_PROC_METHOD(cdt_loop_period)
1970 GENERATE_PROC_METHOD(cdt_grace_delay)
1971 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1972 GENERATE_PROC_METHOD(cdt_max_requests)
1973 GENERATE_PROC_METHOD(cdt_default_archive_id)
1976 * procfs write method for MDT/hsm_control
1977 * proc entry is in mdt directory so data is mdt obd_device pointer
1979 #define CDT_ENABLE_CMD "enabled"
1980 #define CDT_STOP_CMD "shutdown"
1981 #define CDT_DISABLE_CMD "disabled"
1982 #define CDT_PURGE_CMD "purge"
1983 #define CDT_HELP_CMD "help"
1984 #define CDT_MAX_CMD_LEN 10
1987 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1988 size_t count, loff_t *off)
1990 struct seq_file *m = file->private_data;
1991 struct obd_device *obd = m->private;
1992 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1993 struct coordinator *cdt = &(mdt->mdt_coordinator);
1995 char kernbuf[CDT_MAX_CMD_LEN];
1998 if (count == 0 || count >= sizeof(kernbuf))
2001 if (copy_from_user(kernbuf, buffer, count))
2005 if (kernbuf[count - 1] == '\n')
2006 kernbuf[count - 1] = 0;
2009 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2010 if (cdt->cdt_state == CDT_DISABLE) {
2011 rc = set_cdt_state(cdt, CDT_RUNNING, NULL);
2012 mdt_hsm_cdt_wakeup(mdt);
2014 rc = mdt_hsm_cdt_start(mdt);
2016 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2017 if ((cdt->cdt_state == CDT_STOPPING) ||
2018 (cdt->cdt_state == CDT_STOPPED)) {
2019 CERROR("%s: Coordinator already stopped\n",
2023 rc = mdt_hsm_cdt_stop(mdt);
2024 mdt_hsm_cdt_wakeup(mdt);
2026 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2027 if ((cdt->cdt_state == CDT_STOPPING) ||
2028 (cdt->cdt_state == CDT_STOPPED)) {
2029 CERROR("%s: Coordinator is stopped\n",
2033 rc = set_cdt_state(cdt, CDT_DISABLE, NULL);
2035 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2036 rc = hsm_cancel_all_actions(mdt);
2037 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2045 CERROR("%s: Valid coordinator control commands are: "
2046 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2047 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2048 CDT_PURGE_CMD, CDT_HELP_CMD);
2056 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2058 struct obd_device *obd = m->private;
2059 struct coordinator *cdt;
2062 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2064 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2070 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2076 for (i = 0; i < 8 * sizeof(mask); i++) {
2077 if (mask & (1UL << i)) {
2078 seq_printf(m, "%s%s", first ? "" : " ",
2079 hsm_copytool_action2name(i));
2089 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2091 struct mdt_device *mdt = m->private;
2092 struct coordinator *cdt = &mdt->mdt_coordinator;
2094 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2098 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2100 struct mdt_device *mdt = m->private;
2101 struct coordinator *cdt = &mdt->mdt_coordinator;
2103 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2107 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2109 struct mdt_device *mdt = m->private;
2110 struct coordinator *cdt = &mdt->mdt_coordinator;
2112 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2115 static inline enum hsm_copytool_action
2116 hsm_copytool_name2action(const char *name)
2118 if (strcasecmp(name, "NOOP") == 0)
2120 else if (strcasecmp(name, "ARCHIVE") == 0)
2121 return HSMA_ARCHIVE;
2122 else if (strcasecmp(name, "RESTORE") == 0)
2123 return HSMA_RESTORE;
2124 else if (strcasecmp(name, "REMOVE") == 0)
2126 else if (strcasecmp(name, "CANCEL") == 0)
2133 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2134 size_t user_count, __u64 *mask)
2136 char *buf, *pos, *name;
2142 if (!(user_count < 4096))
2145 buf_size = user_count + 1;
2147 OBD_ALLOC(buf, buf_size);
2151 if (copy_from_user(buf, user_buf, buf_size - 1))
2152 GOTO(out, rc = -EFAULT);
2154 buf[buf_size - 1] = '\0';
2157 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2163 action = hsm_copytool_name2action(name);
2165 GOTO(out, rc = -EINVAL);
2167 new_mask |= (1UL << action);
2173 OBD_FREE(buf, buf_size);
2179 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2180 size_t count, loff_t *off)
2182 struct seq_file *m = file->private_data;
2183 struct mdt_device *mdt = m->private;
2184 struct coordinator *cdt = &mdt->mdt_coordinator;
2186 return mdt_write_hsm_request_mask(file, buf, count,
2187 &cdt->cdt_user_request_mask);
2191 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2192 size_t count, loff_t *off)
2194 struct seq_file *m = file->private_data;
2195 struct mdt_device *mdt = m->private;
2196 struct coordinator *cdt = &mdt->mdt_coordinator;
2198 return mdt_write_hsm_request_mask(file, buf, count,
2199 &cdt->cdt_group_request_mask);
2203 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2204 size_t count, loff_t *off)
2206 struct seq_file *m = file->private_data;
2207 struct mdt_device *mdt = m->private;
2208 struct coordinator *cdt = &mdt->mdt_coordinator;
2210 return mdt_write_hsm_request_mask(file, buf, count,
2211 &cdt->cdt_other_request_mask);
2214 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2215 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2216 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2217 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2218 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2219 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2220 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2221 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2223 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2225 .fops = &mdt_hsm_agent_fops },
2226 { .name = "actions",
2227 .fops = &mdt_hsm_actions_fops,
2228 .proc_mode = 0444 },
2229 { .name = "default_archive_id",
2230 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2231 { .name = "grace_delay",
2232 .fops = &mdt_hsm_cdt_grace_delay_fops },
2233 { .name = "loop_period",
2234 .fops = &mdt_hsm_cdt_loop_period_fops },
2235 { .name = "max_requests",
2236 .fops = &mdt_hsm_cdt_max_requests_fops },
2238 .fops = &mdt_hsm_policy_fops },
2239 { .name = "active_request_timeout",
2240 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2241 { .name = "active_requests",
2242 .fops = &mdt_hsm_active_requests_fops },
2243 { .name = "user_request_mask",
2244 .fops = &mdt_hsm_user_request_mask_fops, },
2245 { .name = "group_request_mask",
2246 .fops = &mdt_hsm_group_request_mask_fops, },
2247 { .name = "other_request_mask",
2248 .fops = &mdt_hsm_other_request_mask_fops, },