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_export.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_kernelcomm.h>
48 #include "mdt_internal.h"
50 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
53 * get obj and HSM attributes on a fid
54 * \param mti [IN] context
55 * \param fid [IN] object fid
56 * \param hsm [OUT] HSM meta data
57 * \retval obj or error (-ENOENT if not found)
59 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
60 const struct lu_fid *fid,
64 struct mdt_object *obj;
72 /* find object by FID */
73 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
77 if (!mdt_object_exists(obj)) {
79 mdt_object_put(mti->mti_env, obj);
80 RETURN(ERR_PTR(-ENOENT));
83 rc = mdt_attr_get_complex(mti, obj, ma);
85 mdt_object_put(mti->mti_env, obj);
89 if (ma->ma_valid & MA_HSM)
92 memset(hsm, 0, sizeof(*hsm));
97 void mdt_hsm_dump_hal(int level, const char *prefix,
98 struct hsm_action_list *hal)
101 struct hsm_action_item *hai;
104 CDEBUG(level, "%s: HAL header: version %X count %d compound %#llx"
105 " archive_id %d flags %#llx\n",
106 prefix, hal->hal_version, hal->hal_count,
107 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
109 hai = hai_first(hal);
110 for (i = 0; i < hal->hal_count; i++) {
111 sz = hai->hai_len - sizeof(*hai);
112 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
113 " compound/cookie=%#llx/%#llx"
114 " action=%s extent=%#llx-%#llx gid=%#llx"
115 " datalen=%d data=[%s]\n",
117 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
118 hal->hal_compound_id, hai->hai_cookie,
119 hsm_copytool_action2name(hai->hai_action),
120 hai->hai_extent.offset,
121 hai->hai_extent.length,
123 hai_dump_data_field(hai, buf, sizeof(buf)));
129 * data passed to llog_cat_process() callback
130 * to scan requests and take actions
132 struct hsm_scan_request {
135 struct hsm_action_list *hal;
138 struct hsm_scan_data {
139 struct mdt_thread_info *mti;
140 char fs_name[MTI_NAME_MAXLEN+1];
141 /* request to be send to agents */
142 int max_requests; /** vector size */
143 int request_cnt; /** used count */
144 struct hsm_scan_request *request;
148 * llog_cat_process() callback, used to:
149 * - find waiting request and start action
150 * - purge canceled and done requests
151 * \param env [IN] environment
152 * \param llh [IN] llog handle
153 * \param hdr [IN] llog record
154 * \param data [IN/OUT] cb data = struct hsm_scan_data
156 * \retval -ve failure
158 static int mdt_coordinator_cb(const struct lu_env *env,
159 struct llog_handle *llh,
160 struct llog_rec_hdr *hdr,
163 struct llog_agent_req_rec *larr;
164 struct hsm_scan_data *hsd;
165 struct hsm_action_item *hai;
166 struct mdt_device *mdt;
167 struct coordinator *cdt;
172 mdt = hsd->mti->mti_mdt;
173 cdt = &mdt->mdt_coordinator;
175 larr = (struct llog_agent_req_rec *)hdr;
176 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
177 switch (larr->arr_status) {
180 struct hsm_scan_request *request;
182 /* Are agents full? */
183 if (atomic_read(&cdt->cdt_request_count) >=
184 cdt->cdt_max_requests)
187 /* first search whether the request is found in the
188 * list we have built. */
190 for (i = 0; i < hsd->request_cnt; i++) {
191 if (hsd->request[i].hal->hal_compound_id ==
192 larr->arr_compound_id) {
193 request = &hsd->request[i];
199 struct hsm_action_list *hal;
201 if (hsd->request_cnt == hsd->max_requests)
202 /* Unknown request and no more room
203 * for a new request. Continue to scan
204 * to find other entries for already
209 request = &hsd->request[hsd->request_cnt];
211 /* allocates hai vector size just needs to be large
214 sizeof(*request->hal) +
215 cfs_size_round(MTI_NAME_MAXLEN+1) +
216 2 * cfs_size_round(larr->arr_hai.hai_len);
217 OBD_ALLOC(hal, request->hal_sz);
220 hal->hal_version = HAL_VERSION;
221 strlcpy(hal->hal_fsname, hsd->fs_name,
222 MTI_NAME_MAXLEN + 1);
223 hal->hal_compound_id = larr->arr_compound_id;
224 hal->hal_archive_id = larr->arr_archive_id;
225 hal->hal_flags = larr->arr_flags;
227 request->hal_used_sz = hal_size(hal);
230 hai = hai_first(hal);
232 /* request is known */
233 /* we check if record archive num is the same as the
234 * known request, if not we will serve it in multiple
235 * time because we do not know if the agent can serve
237 * a use case is a compound made of multiple restore
238 * where the files are not archived in the same backend
240 if (larr->arr_archive_id !=
241 request->hal->hal_archive_id)
244 if (request->hal_sz <
245 request->hal_used_sz +
246 cfs_size_round(larr->arr_hai.hai_len)) {
247 /* Not enough room, need an extension */
251 sz = 2 * request->hal_sz;
252 OBD_ALLOC(hal_buffer, sz);
255 memcpy(hal_buffer, request->hal,
256 request->hal_used_sz);
257 OBD_FREE(request->hal,
259 request->hal = hal_buffer;
260 request->hal_sz = sz;
262 hai = hai_first(request->hal);
263 for (i = 0; i < request->hal->hal_count; i++)
266 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
267 hai->hai_cookie = larr->arr_hai.hai_cookie;
268 hai->hai_gid = larr->arr_hai.hai_gid;
270 request->hal_used_sz += cfs_size_round(hai->hai_len);
271 request->hal->hal_count++;
275 struct hsm_progress_kernel pgs;
276 struct cdt_agent_req *car;
277 cfs_time_t now = cfs_time_current_sec();
280 /* we search for a running request
281 * error may happen if coordinator crashes or stopped
282 * with running request
284 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie);
286 last = larr->arr_req_change;
288 last = car->car_req_update;
289 mdt_cdt_put_request(car);
292 /* test if request too long, if yes cancel it
293 * the same way the copy tool acknowledge a cancel request */
294 if (now <= last + cdt->cdt_active_req_timeout)
297 dump_llog_agent_req_rec("request timed out, start cleaning",
299 /* a too old cancel request just needs to be removed
300 * this can happen, if copy tool does not support
301 * cancel for other requests, we have to remove the
302 * running request and notify the copytool */
303 pgs.hpk_fid = larr->arr_hai.hai_fid;
304 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
305 pgs.hpk_extent = larr->arr_hai.hai_extent;
306 pgs.hpk_flags = HP_FLAG_COMPLETED;
307 pgs.hpk_errval = ENOSYS;
308 pgs.hpk_data_version = 0;
310 /* update request state, but do not record in llog, to
311 * avoid deadlock on cdt_llog_lock */
312 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
314 CERROR("%s: cannot cleanup timed out request: "
315 DFID" for cookie %#llx action=%s\n",
317 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
318 hsm_copytool_action2name(
319 larr->arr_hai.hai_action));
322 /* The request no longer exists, forget
323 * about it, and do not send a cancel request
324 * to the client, for which an error will be
325 * sent back, leading to an endless cycle of
327 RETURN(LLOG_DEL_RECORD);
330 /* XXX A cancel request cannot be cancelled. */
331 if (larr->arr_hai.hai_action == HSMA_CANCEL)
334 larr->arr_status = ARS_CANCELED;
335 larr->arr_req_change = now;
336 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
338 CERROR("%s: cannot update agent log: rc = %d\n",
339 mdt_obd_name(mdt), rc);
345 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
346 cfs_time_current_sec())
347 RETURN(LLOG_DEL_RECORD);
354 * create /proc entries for coordinator
357 * \retval -ve failure
359 int hsm_cdt_procfs_init(struct mdt_device *mdt)
361 struct coordinator *cdt = &mdt->mdt_coordinator;
365 /* init /proc entries, failure is not critical */
366 cdt->cdt_proc_dir = lprocfs_register("hsm",
367 mdt2obd_dev(mdt)->obd_proc_entry,
368 lprocfs_mdt_hsm_vars, mdt);
369 if (IS_ERR(cdt->cdt_proc_dir)) {
370 rc = PTR_ERR(cdt->cdt_proc_dir);
371 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
372 " rc=%d\n", mdt_obd_name(mdt), rc);
373 cdt->cdt_proc_dir = NULL;
381 * remove /proc entries for coordinator
384 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
386 struct coordinator *cdt = &mdt->mdt_coordinator;
388 if (cdt->cdt_proc_dir != NULL)
389 lprocfs_remove(&cdt->cdt_proc_dir);
393 * get vector of hsm cdt /proc vars
397 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
399 return lprocfs_mdt_hsm_vars;
402 /* Release the ressource used by the coordinator. Called when the
403 * coordinator is stopping. */
404 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
406 struct coordinator *cdt = &mdt->mdt_coordinator;
407 struct cdt_agent_req *car, *tmp1;
408 struct hsm_agent *ha, *tmp2;
409 struct cdt_restore_handle *crh, *tmp3;
410 struct mdt_thread_info *cdt_mti;
413 down_write(&cdt->cdt_request_lock);
414 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
416 cfs_hash_del(cdt->cdt_request_cookie_hash,
417 &car->car_hai->hai_cookie,
418 &car->car_cookie_hash);
419 list_del(&car->car_request_list);
420 mdt_cdt_put_request(car);
422 up_write(&cdt->cdt_request_lock);
424 down_write(&cdt->cdt_agent_lock);
425 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
426 list_del(&ha->ha_list);
429 up_write(&cdt->cdt_agent_lock);
431 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
432 mutex_lock(&cdt->cdt_restore_lock);
433 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
434 list_del(&crh->crh_list);
435 /* give back layout lock */
436 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
437 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
439 mutex_unlock(&cdt->cdt_restore_lock);
443 * Coordinator state transition table, indexed on enum cdt_states, taking
444 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
445 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
447 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
448 /* from -> to: stopped init running disable stopping */
449 /* stopped */ { true, true, false, false, false },
450 /* init */ { true, false, true, false, true },
451 /* running */ { false, false, true, true, true },
452 /* disable */ { false, false, true, true, true },
453 /* stopping */ { true, false, false, false, true }
457 * Change coordinator thread state
458 * Some combinations are not valid, so catch them here.
460 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
461 * the transition was not possible.
463 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state,
464 enum cdt_states *old_state)
467 enum cdt_states state;
469 spin_lock(&cdt->cdt_state_lock);
471 state = cdt->cdt_state;
473 if (cdt_transition[state][new_state]) {
474 cdt->cdt_state = new_state;
475 spin_unlock(&cdt->cdt_state_lock);
480 spin_unlock(&cdt->cdt_state_lock);
482 "unexpected coordinator transition, from=%s, to=%s\n",
483 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
492 * \param data [IN] obd device
494 * \retval -ve failure
496 static int mdt_coordinator(void *data)
498 struct mdt_thread_info *mti = data;
499 struct mdt_device *mdt = mti->mti_mdt;
500 struct coordinator *cdt = &mdt->mdt_coordinator;
501 struct hsm_scan_data hsd = { NULL };
506 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
507 mdt_obd_name(mdt), current_pid());
509 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
510 * increases due to a change from /proc we do not overflow the
511 * hsd.request[] vector
513 hsd.max_requests = cdt->cdt_max_requests;
514 request_sz = hsd.max_requests * sizeof(*hsd.request);
515 OBD_ALLOC_LARGE(hsd.request, request_sz);
517 GOTO(out, rc = -ENOMEM);
520 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
522 set_cdt_state(cdt, CDT_RUNNING, NULL);
524 /* Inform mdt_hsm_cdt_start(). */
525 wake_up_all(&cdt->cdt_waitq);
528 struct l_wait_info lwi;
531 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
533 l_wait_event(cdt->cdt_waitq,
534 cdt->cdt_event || (cdt->cdt_state == CDT_STOPPING),
537 CDEBUG(D_HSM, "coordinator resumes\n");
539 if (cdt->cdt_state == CDT_STOPPING) {
544 cdt->cdt_event = false;
546 /* if coordinator is suspended continue to wait */
547 if (cdt->cdt_state == CDT_DISABLE) {
548 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
552 CDEBUG(D_HSM, "coordinator starts reading llog\n");
554 if (hsd.max_requests != cdt->cdt_max_requests) {
555 /* cdt_max_requests has changed,
556 * we need to allocate a new buffer
558 OBD_FREE_LARGE(hsd.request, request_sz);
559 hsd.max_requests = cdt->cdt_max_requests;
560 request_sz = hsd.max_requests * sizeof(*hsd.request);
561 OBD_ALLOC_LARGE(hsd.request, request_sz);
570 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
575 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
577 if (list_empty(&cdt->cdt_agents)) {
578 CDEBUG(D_HSM, "no agent available, "
579 "coordinator sleeps\n");
583 /* here hsd contains a list of requests to be started */
584 for (i = 0; i < hsd.request_cnt; i++) {
585 struct hsm_scan_request *request = &hsd.request[i];
586 struct hsm_action_list *hal = request->hal;
587 struct hsm_action_item *hai;
590 enum agent_req_status status;
592 /* still room for work ? */
593 if (atomic_read(&cdt->cdt_request_count) >=
594 cdt->cdt_max_requests)
597 rc = mdt_hsm_agent_send(mti, hal, 0);
598 /* if failure, we suppose it is temporary
599 * if the copy tool failed to do the request
600 * it has to use hsm_progress
602 status = (rc ? ARS_WAITING : ARS_STARTED);
604 /* set up cookie vector to set records status
605 * after copy tools start or failed
607 sz = hal->hal_count * sizeof(__u64);
608 OBD_ALLOC(cookies, sz);
612 hai = hai_first(hal);
613 for (j = 0; j < hal->hal_count; j++) {
614 cookies[j] = hai->hai_cookie;
618 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
619 hal->hal_count, status);
621 CERROR("%s: mdt_agent_record_update() failed, "
622 "rc=%d, cannot update status to %s "
624 mdt_obd_name(mdt), rc,
625 agent_req_status2name(status),
628 OBD_FREE(cookies, sz);
631 /* free hal allocated by callback */
632 for (i = 0; i < hsd.request_cnt; i++) {
633 struct hsm_scan_request *request = &hsd.request[i];
635 OBD_FREE(request->hal, request->hal_sz);
640 set_cdt_state(cdt, CDT_STOPPING, NULL);
643 OBD_FREE_LARGE(hsd.request, request_sz);
645 mdt_hsm_cdt_cleanup(mdt);
647 set_cdt_state(cdt, CDT_STOPPED, NULL);
648 wake_up_all(&cdt->cdt_waitq);
651 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
652 mdt_obd_name(mdt), current_pid(), rc);
654 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
656 mdt_obd_name(mdt), current_pid());
662 * lookup a restore handle by FID
663 * caller needs to hold cdt_restore_lock
664 * \param cdt [IN] coordinator
665 * \param fid [IN] FID
666 * \retval cdt_restore_handle found
667 * \retval NULL not found
669 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
670 const struct lu_fid *fid)
672 struct cdt_restore_handle *crh;
675 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
676 if (lu_fid_eq(&crh->crh_fid, fid))
683 * data passed to llog_cat_process() callback
684 * to scan requests and take actions
686 struct hsm_restore_data {
687 struct mdt_thread_info *hrd_mti;
691 * llog_cat_process() callback, used to:
692 * - find restore request and allocate the restore handle
693 * \param env [IN] environment
694 * \param llh [IN] llog handle
695 * \param hdr [IN] llog record
696 * \param data [IN/OUT] cb data = struct hsm_restore_data
698 * \retval -ve failure
700 static int hsm_restore_cb(const struct lu_env *env,
701 struct llog_handle *llh,
702 struct llog_rec_hdr *hdr, void *data)
704 struct llog_agent_req_rec *larr;
705 struct hsm_restore_data *hrd;
706 struct cdt_restore_handle *crh;
707 struct hsm_action_item *hai;
708 struct mdt_thread_info *mti;
709 struct coordinator *cdt;
710 struct mdt_object *child;
716 cdt = &mti->mti_mdt->mdt_coordinator;
718 larr = (struct llog_agent_req_rec *)hdr;
719 hai = &larr->arr_hai;
720 if (hai->hai_cookie > cdt->cdt_last_cookie)
721 /* update the cookie to avoid collision */
722 cdt->cdt_last_cookie = hai->hai_cookie + 1;
724 if (hai->hai_action != HSMA_RESTORE ||
725 agent_req_in_final_state(larr->arr_status))
728 /* restore request not in a final state */
730 /* force replay of restore requests left in started state from previous
731 * CDT context, to be canceled later if finally found to be incompatible
732 * when being re-started */
733 if (larr->arr_status == ARS_STARTED) {
734 larr->arr_status = ARS_WAITING;
735 larr->arr_req_change = cfs_time_current_sec();
736 rc = llog_write(env, llh, hdr, hdr->lrh_index);
741 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
745 crh->crh_fid = hai->hai_fid;
746 /* in V1 all file is restored
747 crh->extent.start = hai->hai_extent.offset;
748 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
750 crh->crh_extent.start = 0;
751 crh->crh_extent.end = hai->hai_extent.length;
752 /* get the layout lock */
753 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
754 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
755 MDS_INODELOCK_LAYOUT);
757 GOTO(out, rc = PTR_ERR(child));
760 /* we choose to not keep a reference
761 * on the object during the restore time which can be very long */
762 mdt_object_put(mti->mti_env, child);
764 mutex_lock(&cdt->cdt_restore_lock);
765 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
766 mutex_unlock(&cdt->cdt_restore_lock);
773 * restore coordinator state at startup
774 * the goal is to take a layout lock for each registered restore request
775 * \param mti [IN] context
777 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
779 struct hsm_restore_data hrd;
785 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
786 hsm_restore_cb, &hrd, WRITE);
791 static int hsm_init_ucred(struct lu_ucred *uc)
795 uc->uc_valid = UCRED_OLD;
804 uc->uc_suppgids[0] = -1;
805 uc->uc_suppgids[1] = -1;
806 uc->uc_cap = CFS_CAP_FS_MASK;
809 uc->uc_identity = NULL;
815 * wake up coordinator thread
816 * \param mdt [IN] device
818 * \retval -ve failure
820 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
822 struct coordinator *cdt = &mdt->mdt_coordinator;
825 if (cdt->cdt_state == CDT_STOPPED)
828 /* wake up coordinator */
829 cdt->cdt_event = true;
830 wake_up_all(&cdt->cdt_waitq);
836 * initialize coordinator struct
837 * \param mdt [IN] device
839 * \retval -ve failure
841 int mdt_hsm_cdt_init(struct mdt_device *mdt)
843 struct coordinator *cdt = &mdt->mdt_coordinator;
844 struct mdt_thread_info *cdt_mti = NULL;
848 init_waitqueue_head(&cdt->cdt_waitq);
849 init_rwsem(&cdt->cdt_llog_lock);
850 init_rwsem(&cdt->cdt_agent_lock);
851 init_rwsem(&cdt->cdt_request_lock);
852 mutex_init(&cdt->cdt_restore_lock);
853 spin_lock_init(&cdt->cdt_state_lock);
854 cdt->cdt_state = CDT_STOPPED;
856 INIT_LIST_HEAD(&cdt->cdt_request_list);
857 INIT_LIST_HEAD(&cdt->cdt_agents);
858 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
860 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
867 &cdt_request_cookie_hash_ops,
869 if (cdt->cdt_request_cookie_hash == NULL)
872 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
874 GOTO(out_request_cookie_hash, rc);
876 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
877 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
881 lu_context_enter(&cdt->cdt_session);
882 cdt->cdt_env.le_ses = &cdt->cdt_session;
884 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
885 LASSERT(cdt_mti != NULL);
887 cdt_mti->mti_env = &cdt->cdt_env;
888 cdt_mti->mti_mdt = mdt;
890 hsm_init_ucred(mdt_ucred(cdt_mti));
892 /* default values for /proc tunnables
893 * can be override by MGS conf */
894 cdt->cdt_default_archive_id = 1;
895 cdt->cdt_grace_delay = 60;
896 cdt->cdt_loop_period = 10;
897 cdt->cdt_max_requests = 3;
898 cdt->cdt_policy = CDT_DEFAULT_POLICY;
899 cdt->cdt_active_req_timeout = 3600;
904 lu_env_fini(&cdt->cdt_env);
905 out_request_cookie_hash:
906 cfs_hash_putref(cdt->cdt_request_cookie_hash);
907 cdt->cdt_request_cookie_hash = NULL;
913 * free a coordinator thread
914 * \param mdt [IN] device
916 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
918 struct coordinator *cdt = &mdt->mdt_coordinator;
921 lu_context_exit(cdt->cdt_env.le_ses);
922 lu_context_fini(cdt->cdt_env.le_ses);
924 lu_env_fini(&cdt->cdt_env);
926 cfs_hash_putref(cdt->cdt_request_cookie_hash);
927 cdt->cdt_request_cookie_hash = NULL;
933 * start a coordinator thread
934 * \param mdt [IN] device
936 * \retval -ve failure
938 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
940 struct coordinator *cdt = &mdt->mdt_coordinator;
943 struct mdt_thread_info *cdt_mti;
944 struct task_struct *task;
947 /* functions defined but not yet used
948 * this avoid compilation warning
952 rc = set_cdt_state(cdt, CDT_INIT, NULL);
954 CERROR("%s: Coordinator already started or stopping\n",
959 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
960 cdt->cdt_policy = CDT_DEFAULT_POLICY;
962 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
963 /* just need to be larger than previous one */
964 /* cdt_last_cookie is protected by cdt_llog_lock */
965 cdt->cdt_last_cookie = cfs_time_current_sec();
966 atomic_set(&cdt->cdt_request_count, 0);
967 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
968 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
969 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
971 /* to avoid deadlock when start is made through /proc
972 * /proc entries are created by the coordinator thread */
974 /* set up list of started restore requests */
975 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
976 rc = mdt_hsm_pending_restore(cdt_mti);
978 CERROR("%s: cannot take the layout locks needed"
979 " for registered restore: %d\n",
980 mdt_obd_name(mdt), rc);
982 if (mdt->mdt_bottom->dd_rdonly)
985 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
988 set_cdt_state(cdt, CDT_STOPPED, NULL);
989 CERROR("%s: error starting coordinator thread: %d\n",
990 mdt_obd_name(mdt), rc);
992 wait_event(cdt->cdt_waitq,
993 cdt->cdt_state != CDT_INIT);
994 if (cdt->cdt_state == CDT_RUNNING) {
995 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1000 "%s: coordinator thread failed to start\n",
1010 * stop a coordinator thread
1011 * \param mdt [IN] device
1013 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1015 struct coordinator *cdt = &mdt->mdt_coordinator;
1019 /* stop coordinator thread */
1020 rc = set_cdt_state(cdt, CDT_STOPPING, NULL);
1024 wake_up_all(&cdt->cdt_waitq);
1025 wait_event(cdt->cdt_waitq, cdt->cdt_state != CDT_STOPPING);
1030 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1031 const struct lu_fid *fid,
1034 struct mdt_object *obj;
1038 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1040 GOTO(out, rc = PTR_ERR(obj));
1042 if (mh.mh_flags & HS_EXISTS &&
1043 mh.mh_arch_id == archive_id)
1044 GOTO(out_obj, rc = 0);
1046 mh.mh_flags |= HS_EXISTS;
1047 mh.mh_arch_id = archive_id;
1048 rc = mdt_hsm_attr_set(mti, obj, &mh);
1051 mdt_object_put(mti->mti_env, obj);
1057 * register all requests from an hal in the memory list
1058 * \param mti [IN] context
1059 * \param hal [IN] request
1060 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1061 * which is running the CT
1063 * \retval -ve failure
1065 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1066 struct hsm_action_list *hal, struct obd_uuid *uuid)
1068 struct mdt_device *mdt = mti->mti_mdt;
1069 struct coordinator *cdt = &mdt->mdt_coordinator;
1070 struct hsm_action_item *hai;
1074 /* register request in memory list */
1075 hai = hai_first(hal);
1076 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1077 struct cdt_agent_req *car;
1079 /* in case of a cancel request, we first mark the ondisk
1080 * record of the request we want to stop as canceled
1081 * this does not change the cancel record
1082 * it will be done when updating the request status
1084 if (hai->hai_action == HSMA_CANCEL) {
1085 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1089 CERROR("%s: mdt_agent_record_update() failed, "
1090 "rc=%d, cannot update status to %s "
1091 "for cookie %#llx\n",
1092 mdt_obd_name(mdt), rc,
1093 agent_req_status2name(ARS_CANCELED),
1098 /* find the running request to set it canceled */
1099 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1101 car->car_canceled = 1;
1102 /* uuid has to be changed to the one running the
1103 * request to cancel */
1104 *uuid = car->car_uuid;
1105 mdt_cdt_put_request(car);
1107 /* no need to memorize cancel request
1108 * this also avoid a deadlock when we receive
1109 * a purge all requests command
1114 if (hai->hai_action == HSMA_ARCHIVE) {
1115 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1116 hal->hal_archive_id);
1123 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1124 hal->hal_archive_id, hal->hal_flags,
1127 GOTO(out, rc = PTR_ERR(car));
1129 rc = mdt_cdt_add_request(cdt, car);
1131 mdt_cdt_free_request(car);
1138 * swap layouts between 2 fids
1139 * \param mti [IN] context
1142 * \param mh_common [IN] MD HSM
1144 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1145 struct mdt_object *obj, const struct lu_fid *dfid,
1146 struct md_hsm *mh_common)
1148 struct mdt_object *dobj;
1149 struct mdt_lock_handle *dlh;
1153 if (!mdt_object_exists(obj))
1154 GOTO(out, rc = -ENOENT);
1156 /* we already have layout lock on obj so take only
1158 dlh = &mti->mti_lh[MDT_LH_OLD];
1159 mdt_lock_reg_init(dlh, LCK_EX);
1160 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1162 GOTO(out, rc = PTR_ERR(dobj));
1164 /* if copy tool closes the volatile before sending the final
1165 * progress through llapi_hsm_copy_end(), all the objects
1166 * are removed and mdd_swap_layout LBUG */
1167 if (!mdt_object_exists(dobj)) {
1168 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1169 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1170 GOTO(out_dobj, rc = -ENOENT);
1172 /* Since we only handle restores here, unconditionally use
1173 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1174 * be preserved in case of failure during swap_layout and not
1175 * leave a file in an intermediate but incoherent state.
1176 * But need to setup HSM xattr of data FID before, reuse
1177 * mti and mh presets for FID in hsm_cdt_request_completed(),
1178 * only need to clear RELEASED and DIRTY.
1180 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1181 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1183 rc = mo_swap_layouts(mti->mti_env,
1184 mdt_object_child(obj),
1185 mdt_object_child(dobj),
1186 SWAP_LAYOUTS_MDS_HSM);
1189 mdt_object_unlock_put(mti, dobj, dlh, 1);
1195 * update status of a completed request
1196 * \param mti [IN] context
1197 * \param pgs [IN] progress of the copy tool
1198 * \param update_record [IN] update llog record
1200 * \retval -ve failure
1202 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1203 struct hsm_progress_kernel *pgs,
1204 const struct cdt_agent_req *car,
1205 enum agent_req_status *status)
1207 const struct lu_env *env = mti->mti_env;
1208 struct mdt_device *mdt = mti->mti_mdt;
1209 struct coordinator *cdt = &mdt->mdt_coordinator;
1210 struct mdt_object *obj = NULL;
1211 int cl_flags = 0, rc = 0;
1214 bool need_changelog = true;
1217 /* default is to retry */
1218 *status = ARS_WAITING;
1220 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1221 * if error/removed continue anyway to get correct reporting done */
1222 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1223 /* we will update MD HSM only if needed */
1224 is_mh_changed = false;
1226 /* no need to change mh->mh_arch_id
1227 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1229 if (pgs->hpk_errval != 0) {
1230 switch (pgs->hpk_errval) {
1232 /* the copy tool does not support cancel
1233 * so the cancel request is failed
1234 * As we cannot distinguish a cancel progress
1235 * from another action progress (they have the
1236 * same cookie), we suppose here the CT returns
1237 * ENOSYS only if does not support cancel
1239 /* this can also happen when cdt calls it to
1240 * for a timed out request */
1241 *status = ARS_FAILED;
1242 /* to have a cancel event in changelog */
1243 pgs->hpk_errval = ECANCELED;
1246 /* the request record has already been set to
1247 * ARS_CANCELED, this set the cancel request
1249 *status = ARS_SUCCEED;
1252 /* retry only if current policy or requested, and
1253 * object is not on error/removed */
1254 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1255 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1256 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1260 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1261 CERROR("%s: Request %#llx on "DFID
1262 " failed, error code %d too large\n",
1264 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1266 hsm_set_cl_error(&cl_flags,
1267 CLF_HSM_ERROVERFLOW);
1270 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1273 switch (car->car_hai->hai_action) {
1275 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1278 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1281 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1284 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1285 CERROR("%s: Failed request %#llx on "DFID
1286 " cannot be a CANCEL\n",
1289 PFID(&pgs->hpk_fid));
1292 CERROR("%s: Failed request %#llx on "DFID
1293 " %d is an unknown action\n",
1295 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1296 car->car_hai->hai_action);
1301 *status = ARS_SUCCEED;
1302 switch (car->car_hai->hai_action) {
1304 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1305 /* set ARCHIVE keep EXIST and clear LOST and
1307 mh.mh_arch_ver = pgs->hpk_data_version;
1308 mh.mh_flags |= HS_ARCHIVED;
1309 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1310 is_mh_changed = true;
1313 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1315 /* do not clear RELEASED and DIRTY here
1316 * this will occur in hsm_swap_layouts()
1319 /* Restoring has changed the file version on
1321 mh.mh_arch_ver = pgs->hpk_data_version;
1322 is_mh_changed = true;
1325 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1326 /* clear ARCHIVED EXISTS and LOST */
1327 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1328 is_mh_changed = true;
1331 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1332 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1335 PFID(&pgs->hpk_fid));
1338 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1340 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1341 car->car_hai->hai_action);
1347 /* rc != 0 means error when analysing action, it may come from
1348 * a crasy CT no need to manage DIRTY
1351 hsm_set_cl_flags(&cl_flags,
1352 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1354 /* unlock is done later, after layout lock management */
1355 if (is_mh_changed && !IS_ERR(obj))
1356 rc = mdt_hsm_attr_set(mti, obj, &mh);
1358 /* we give back layout lock only if restore was successful or
1359 * if no retry will be attempted and if object is still alive,
1360 * in other cases we just unlock the object */
1361 if (car->car_hai->hai_action == HSMA_RESTORE) {
1362 struct cdt_restore_handle *crh;
1364 /* restore in data FID done, we swap the layouts
1365 * only if restore is successful */
1366 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1367 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1370 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1371 *status = ARS_FAILED;
1372 pgs->hpk_errval = -rc;
1375 /* we have to retry, so keep layout lock */
1376 if (*status == ARS_WAITING)
1379 /* restore special case, need to create ChangeLog record
1380 * before to give back layout lock to avoid concurrent
1381 * file updater to post out of order ChangeLog */
1382 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1383 &car->car_hai->hai_fid);
1384 need_changelog = false;
1386 /* give back layout lock */
1387 mutex_lock(&cdt->cdt_restore_lock);
1388 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1390 list_del(&crh->crh_list);
1391 mutex_unlock(&cdt->cdt_restore_lock);
1392 /* Just give back layout lock, we keep the reference
1393 * which is given back later with the lock for HSM
1395 * XXX obj may be invalid so we do not pass it. */
1397 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1400 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1406 /* always add a ChangeLog record */
1408 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1409 &car->car_hai->hai_fid);
1412 mdt_object_put(mti->mti_env, obj);
1418 * update status of a request
1419 * \param mti [IN] context
1420 * \param pgs [IN] progress of the copy tool
1421 * \param update_record [IN] update llog record
1423 * \retval -ve failure
1425 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1426 struct hsm_progress_kernel *pgs,
1427 const int update_record)
1429 struct mdt_device *mdt = mti->mti_mdt;
1430 struct coordinator *cdt = &mdt->mdt_coordinator;
1431 struct cdt_agent_req *car;
1435 /* no coordinator started, so we cannot serve requests */
1436 if (cdt->cdt_state == CDT_STOPPED)
1439 /* first do sanity checks */
1440 car = mdt_cdt_update_request(cdt, pgs);
1442 CERROR("%s: Cannot find running request for cookie %#llx"
1445 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1447 RETURN(PTR_ERR(car));
1450 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1451 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1452 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1453 hsm_copytool_action2name(car->car_hai->hai_action),
1454 pgs->hpk_flags, pgs->hpk_errval,
1455 PFID(&car->car_hai->hai_fid),
1456 PFID(&car->car_hai->hai_dfid));
1458 /* progress is done on FID or data FID depending of the action and
1459 * of the copy progress */
1460 /* for restore progress is used to send back the data FID to cdt */
1461 if (car->car_hai->hai_action == HSMA_RESTORE &&
1462 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1463 car->car_hai->hai_dfid = pgs->hpk_fid;
1465 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1466 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1467 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1468 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1469 CERROR("%s: Progress on "DFID" for cookie %#llx"
1470 " does not match request FID "DFID" nor data FID "
1473 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1474 PFID(&car->car_hai->hai_fid),
1475 PFID(&car->car_hai->hai_dfid));
1476 GOTO(out, rc = -EINVAL);
1479 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1480 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1481 " is not coherent (err=%d and not completed"
1484 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1485 hsm_copytool_action2name(car->car_hai->hai_action),
1486 pgs->hpk_errval, pgs->hpk_flags);
1487 GOTO(out, rc = -EINVAL);
1490 /* now progress is valid */
1492 /* we use a root like ucred */
1493 hsm_init_ucred(mdt_ucred(mti));
1495 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1496 enum agent_req_status status;
1498 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1500 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1502 update_record ? "Updating" : "Not updating",
1503 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1504 hsm_copytool_action2name(car->car_hai->hai_action),
1505 agent_req_status2name(status));
1507 /* update record first (LU-9075) */
1508 if (update_record) {
1511 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1512 &pgs->hpk_cookie, 1,
1515 CERROR("%s: mdt_agent_record_update() failed,"
1516 " rc=%d, cannot update status to %s"
1517 " for cookie %#llx\n",
1518 mdt_obd_name(mdt), rc1,
1519 agent_req_status2name(status),
1521 rc = (rc != 0 ? rc : rc1);
1524 /* then remove request from memory list (LU-9075) */
1525 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1527 /* ct has completed a request, so a slot is available, wakeup
1528 * cdt to find new work */
1529 mdt_hsm_cdt_wakeup(mdt);
1531 /* if copytool send a progress on a canceled request
1532 * we inform copytool it should stop
1534 if (car->car_canceled == 1)
1540 /* remove ref got from mdt_cdt_update_request() */
1541 mdt_cdt_put_request(car);
1548 * data passed to llog_cat_process() callback
1549 * to cancel requests
1551 struct hsm_cancel_all_data {
1552 struct mdt_device *mdt;
1556 * llog_cat_process() callback, used to:
1557 * - purge all requests
1558 * \param env [IN] environment
1559 * \param llh [IN] llog handle
1560 * \param hdr [IN] llog record
1561 * \param data [IN] cb data = struct hsm_cancel_all_data
1563 * \retval -ve failure
1565 static int mdt_cancel_all_cb(const struct lu_env *env,
1566 struct llog_handle *llh,
1567 struct llog_rec_hdr *hdr, void *data)
1569 struct llog_agent_req_rec *larr;
1570 struct hsm_cancel_all_data *hcad;
1574 larr = (struct llog_agent_req_rec *)hdr;
1576 if (larr->arr_status == ARS_WAITING ||
1577 larr->arr_status == ARS_STARTED) {
1578 larr->arr_status = ARS_CANCELED;
1579 larr->arr_req_change = cfs_time_current_sec();
1580 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1587 * cancel all actions
1588 * \param obd [IN] MDT device
1590 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1593 struct lu_context session;
1594 struct mdt_thread_info *mti;
1595 struct coordinator *cdt = &mdt->mdt_coordinator;
1596 struct cdt_agent_req *car;
1597 struct hsm_action_list *hal = NULL;
1598 struct hsm_action_item *hai;
1599 struct hsm_cancel_all_data hcad;
1600 int hal_sz = 0, hal_len, rc;
1601 enum cdt_states old_state;
1604 rc = lu_env_init(&env, LCT_MD_THREAD);
1608 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1609 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1613 lu_context_enter(&session);
1614 env.le_ses = &session;
1616 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1617 LASSERT(mti != NULL);
1619 mti->mti_env = &env;
1622 hsm_init_ucred(mdt_ucred(mti));
1624 /* disable coordinator */
1625 rc = set_cdt_state(cdt, CDT_DISABLE, &old_state);
1629 /* send cancel to all running requests */
1630 down_read(&cdt->cdt_request_lock);
1631 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1632 mdt_cdt_get_request(car);
1633 /* request is not yet removed from list, it will be done
1634 * when copytool will return progress
1637 if (car->car_hai->hai_action == HSMA_CANCEL) {
1638 mdt_cdt_put_request(car);
1643 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1644 cfs_size_round(car->car_hai->hai_len);
1646 if (hal_len > hal_sz && hal_sz > 0) {
1647 /* not enough room, free old buffer */
1648 OBD_FREE(hal, hal_sz);
1652 /* empty buffer, allocate one */
1655 OBD_ALLOC(hal, hal_sz);
1657 mdt_cdt_put_request(car);
1658 up_read(&cdt->cdt_request_lock);
1659 GOTO(out_cdt_state, rc = -ENOMEM);
1663 hal->hal_version = HAL_VERSION;
1664 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1666 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1667 hal->hal_compound_id = car->car_compound_id;
1668 hal->hal_archive_id = car->car_archive_id;
1669 hal->hal_flags = car->car_flags;
1672 hai = hai_first(hal);
1673 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1674 hai->hai_action = HSMA_CANCEL;
1677 /* it is possible to safely call mdt_hsm_agent_send()
1678 * (ie without a deadlock on cdt_request_lock), because the
1679 * write lock is taken only if we are not in purge mode
1680 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1681 * nor mdt_cdt_remove_request())
1683 /* no conflict with cdt thread because cdt is disable and we
1684 * have the request lock */
1685 mdt_hsm_agent_send(mti, hal, 1);
1687 mdt_cdt_put_request(car);
1689 up_read(&cdt->cdt_request_lock);
1692 OBD_FREE(hal, hal_sz);
1694 /* cancel all on-disk records */
1697 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1698 mdt_cancel_all_cb, &hcad, WRITE);
1700 /* Enable coordinator, unless the coordinator was stopping. */
1701 set_cdt_state(cdt, old_state, NULL);
1702 lu_context_exit(&session);
1703 lu_context_fini(&session);
1711 * check if a request is compatible with file status
1712 * \param hai [IN] request description
1713 * \param archive_id [IN] request archive id
1714 * \param rq_flags [IN] request flags
1715 * \param hsm [IN] file HSM metadata
1718 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1719 u32 archive_id, u64 rq_flags,
1720 const struct md_hsm *hsm)
1722 int is_compat = false;
1726 hsm_flags = hsm->mh_flags;
1727 switch (hai->hai_action) {
1729 if (!(hsm_flags & HS_NOARCHIVE) &&
1730 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1733 if (hsm_flags & HS_EXISTS &&
1735 archive_id != hsm->mh_arch_id)
1740 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1741 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1745 if (!(hsm_flags & HS_RELEASED) &&
1746 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1753 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1754 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1755 PFID(&hai->hai_fid),
1756 hsm_copytool_action2name(hai->hai_action), rq_flags,
1757 hai->hai_extent.offset, hai->hai_extent.length,
1759 (is_compat ? "compatible" : "uncompatible"));
1765 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1767 static const struct {
1771 } hsm_policy_names[] = {
1772 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1773 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1778 * convert a policy name to a bit
1779 * \param name [IN] policy name
1781 * \retval policy bit
1783 static __u64 hsm_policy_str2bit(const char *name)
1787 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1788 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1789 strcmp(hsm_policy_names[i].name, name) == 0)
1790 return hsm_policy_names[i].bit;
1795 * convert a policy bit field to a string
1796 * \param mask [IN] policy bit field
1797 * \param hexa [IN] print mask before bit names
1798 * \param buffer [OUT] string
1799 * \param count [IN] size of buffer
1801 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1809 seq_printf(m, "(%#llx) ", mask);
1811 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1814 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1815 if (hsm_policy_names[j].bit == bit)
1819 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1821 seq_printf(m, "%s ", hsm_policy_names[j].name);
1823 /* remove last ' ' */
1828 /* methods to read/write HSM policy flags */
1829 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1831 struct mdt_device *mdt = m->private;
1832 struct coordinator *cdt = &mdt->mdt_coordinator;
1835 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1840 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1841 size_t count, loff_t *off)
1843 struct seq_file *m = file->private_data;
1844 struct mdt_device *mdt = m->private;
1845 struct coordinator *cdt = &mdt->mdt_coordinator;
1846 char *start, *token, sign;
1849 __u64 add_mask, remove_mask, set_mask;
1853 if (count + 1 > PAGE_SIZE)
1856 OBD_ALLOC(buf, count + 1);
1860 if (copy_from_user(buf, buffer, count))
1861 GOTO(out, rc = -EFAULT);
1866 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1869 add_mask = remove_mask = set_mask = 0;
1871 token = strsep(&start, "\n ");
1877 if (sign == '-' || sign == '+')
1880 policy = hsm_policy_str2bit(token);
1882 CWARN("%s: '%s' is unknown, "
1883 "supported policies are:\n", mdt_obd_name(mdt),
1885 hsm_policy_bit2str(m, 0, false);
1886 GOTO(out, rc = -EINVAL);
1890 remove_mask |= policy;
1900 } while (start != NULL);
1902 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1903 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1905 /* if no sign in all string, it is a clear and set
1906 * if some sign found, all unsigned are converted
1908 * P1 P2 = set to P1 and P2
1909 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1911 if (remove_mask == 0 && add_mask == 0) {
1912 cdt->cdt_policy = set_mask;
1914 cdt->cdt_policy |= set_mask | add_mask;
1915 cdt->cdt_policy &= ~remove_mask;
1918 GOTO(out, rc = count);
1921 OBD_FREE(buf, count + 1);
1924 LPROC_SEQ_FOPS(mdt_hsm_policy);
1926 #define GENERATE_PROC_METHOD(VAR) \
1927 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1929 struct mdt_device *mdt = m->private; \
1930 struct coordinator *cdt = &mdt->mdt_coordinator; \
1933 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1937 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1938 size_t count, loff_t *off) \
1941 struct seq_file *m = file->private_data; \
1942 struct mdt_device *mdt = m->private; \
1943 struct coordinator *cdt = &mdt->mdt_coordinator; \
1948 rc = lprocfs_str_to_s64(buffer, count, &val); \
1951 if (val > 0 && val < INT_MAX) { \
1958 GENERATE_PROC_METHOD(cdt_loop_period)
1959 GENERATE_PROC_METHOD(cdt_grace_delay)
1960 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1961 GENERATE_PROC_METHOD(cdt_max_requests)
1962 GENERATE_PROC_METHOD(cdt_default_archive_id)
1965 * procfs write method for MDT/hsm_control
1966 * proc entry is in mdt directory so data is mdt obd_device pointer
1968 #define CDT_ENABLE_CMD "enabled"
1969 #define CDT_STOP_CMD "shutdown"
1970 #define CDT_DISABLE_CMD "disabled"
1971 #define CDT_PURGE_CMD "purge"
1972 #define CDT_HELP_CMD "help"
1973 #define CDT_MAX_CMD_LEN 10
1976 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1977 size_t count, loff_t *off)
1979 struct seq_file *m = file->private_data;
1980 struct obd_device *obd = m->private;
1981 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1982 struct coordinator *cdt = &(mdt->mdt_coordinator);
1984 char kernbuf[CDT_MAX_CMD_LEN];
1987 if (count == 0 || count >= sizeof(kernbuf))
1990 if (copy_from_user(kernbuf, buffer, count))
1994 if (kernbuf[count - 1] == '\n')
1995 kernbuf[count - 1] = 0;
1998 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1999 if (cdt->cdt_state == CDT_DISABLE) {
2000 rc = set_cdt_state(cdt, CDT_RUNNING, NULL);
2001 mdt_hsm_cdt_wakeup(mdt);
2003 rc = mdt_hsm_cdt_start(mdt);
2005 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2006 if ((cdt->cdt_state == CDT_STOPPING) ||
2007 (cdt->cdt_state == CDT_STOPPED)) {
2008 CERROR("%s: Coordinator already stopped\n",
2012 rc = mdt_hsm_cdt_stop(mdt);
2013 mdt_hsm_cdt_wakeup(mdt);
2015 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2016 if ((cdt->cdt_state == CDT_STOPPING) ||
2017 (cdt->cdt_state == CDT_STOPPED)) {
2018 CERROR("%s: Coordinator is stopped\n",
2022 rc = set_cdt_state(cdt, CDT_DISABLE, NULL);
2024 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2025 rc = hsm_cancel_all_actions(mdt);
2026 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2034 CERROR("%s: Valid coordinator control commands are: "
2035 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2036 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2037 CDT_PURGE_CMD, CDT_HELP_CMD);
2045 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2047 struct obd_device *obd = m->private;
2048 struct coordinator *cdt;
2051 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2053 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2059 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2065 for (i = 0; i < 8 * sizeof(mask); i++) {
2066 if (mask & (1UL << i)) {
2067 seq_printf(m, "%s%s", first ? "" : " ",
2068 hsm_copytool_action2name(i));
2078 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2080 struct mdt_device *mdt = m->private;
2081 struct coordinator *cdt = &mdt->mdt_coordinator;
2083 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2087 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2089 struct mdt_device *mdt = m->private;
2090 struct coordinator *cdt = &mdt->mdt_coordinator;
2092 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2096 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2098 struct mdt_device *mdt = m->private;
2099 struct coordinator *cdt = &mdt->mdt_coordinator;
2101 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2104 static inline enum hsm_copytool_action
2105 hsm_copytool_name2action(const char *name)
2107 if (strcasecmp(name, "NOOP") == 0)
2109 else if (strcasecmp(name, "ARCHIVE") == 0)
2110 return HSMA_ARCHIVE;
2111 else if (strcasecmp(name, "RESTORE") == 0)
2112 return HSMA_RESTORE;
2113 else if (strcasecmp(name, "REMOVE") == 0)
2115 else if (strcasecmp(name, "CANCEL") == 0)
2122 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2123 size_t user_count, __u64 *mask)
2125 char *buf, *pos, *name;
2131 if (!(user_count < 4096))
2134 buf_size = user_count + 1;
2136 OBD_ALLOC(buf, buf_size);
2140 if (copy_from_user(buf, user_buf, buf_size - 1))
2141 GOTO(out, rc = -EFAULT);
2143 buf[buf_size - 1] = '\0';
2146 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2152 action = hsm_copytool_name2action(name);
2154 GOTO(out, rc = -EINVAL);
2156 new_mask |= (1UL << action);
2162 OBD_FREE(buf, buf_size);
2168 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2169 size_t count, loff_t *off)
2171 struct seq_file *m = file->private_data;
2172 struct mdt_device *mdt = m->private;
2173 struct coordinator *cdt = &mdt->mdt_coordinator;
2175 return mdt_write_hsm_request_mask(file, buf, count,
2176 &cdt->cdt_user_request_mask);
2180 mdt_hsm_group_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_group_request_mask);
2192 mdt_hsm_other_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_other_request_mask);
2203 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2204 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2205 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2206 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2207 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2208 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2209 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2210 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2212 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2214 .fops = &mdt_hsm_agent_fops },
2215 { .name = "actions",
2216 .fops = &mdt_hsm_actions_fops,
2217 .proc_mode = 0444 },
2218 { .name = "default_archive_id",
2219 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2220 { .name = "grace_delay",
2221 .fops = &mdt_hsm_cdt_grace_delay_fops },
2222 { .name = "loop_period",
2223 .fops = &mdt_hsm_cdt_loop_period_fops },
2224 { .name = "max_requests",
2225 .fops = &mdt_hsm_cdt_max_requests_fops },
2227 .fops = &mdt_hsm_policy_fops },
2228 { .name = "active_request_timeout",
2229 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2230 { .name = "active_requests",
2231 .fops = &mdt_hsm_active_requests_fops },
2232 { .name = "user_request_mask",
2233 .fops = &mdt_hsm_user_request_mask_fops, },
2234 { .name = "group_request_mask",
2235 .fops = &mdt_hsm_group_request_mask_fops, },
2236 { .name = "other_request_mask",
2237 .fops = &mdt_hsm_other_request_mask_fops, },