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++;
273 if (hai->hai_action != HSMA_CANCEL)
274 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
275 llh->lgh_hdr->llh_cat_idx,
280 struct hsm_progress_kernel pgs;
281 struct cdt_agent_req *car;
282 cfs_time_t now = cfs_time_current_sec();
285 /* we search for a running request
286 * error may happen if coordinator crashes or stopped
287 * with running request
289 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie);
291 last = larr->arr_req_change;
293 last = car->car_req_update;
294 mdt_cdt_put_request(car);
297 /* test if request too long, if yes cancel it
298 * the same way the copy tool acknowledge a cancel request */
299 if (now <= last + cdt->cdt_active_req_timeout)
302 dump_llog_agent_req_rec("request timed out, start cleaning",
304 /* a too old cancel request just needs to be removed
305 * this can happen, if copy tool does not support
306 * cancel for other requests, we have to remove the
307 * running request and notify the copytool */
308 pgs.hpk_fid = larr->arr_hai.hai_fid;
309 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
310 pgs.hpk_extent = larr->arr_hai.hai_extent;
311 pgs.hpk_flags = HP_FLAG_COMPLETED;
312 pgs.hpk_errval = ENOSYS;
313 pgs.hpk_data_version = 0;
315 /* update request state, but do not record in llog, to
316 * avoid deadlock on cdt_llog_lock */
317 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
319 CERROR("%s: cannot cleanup timed out request: "
320 DFID" for cookie %#llx action=%s\n",
322 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
323 hsm_copytool_action2name(
324 larr->arr_hai.hai_action));
327 /* The request no longer exists, forget
328 * about it, and do not send a cancel request
329 * to the client, for which an error will be
330 * sent back, leading to an endless cycle of
332 cdt_agent_record_hash_del(cdt,
333 larr->arr_hai.hai_cookie);
334 RETURN(LLOG_DEL_RECORD);
337 /* XXX A cancel request cannot be cancelled. */
338 if (larr->arr_hai.hai_action == HSMA_CANCEL)
341 larr->arr_status = ARS_CANCELED;
342 larr->arr_req_change = now;
343 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
345 CERROR("%s: cannot update agent log: rc = %d\n",
346 mdt_obd_name(mdt), rc);
352 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
353 cfs_time_current_sec()) {
354 cdt_agent_record_hash_del(cdt,
355 larr->arr_hai.hai_cookie);
356 RETURN(LLOG_DEL_RECORD);
364 * create /proc entries for coordinator
367 * \retval -ve failure
369 int hsm_cdt_procfs_init(struct mdt_device *mdt)
371 struct coordinator *cdt = &mdt->mdt_coordinator;
375 /* init /proc entries, failure is not critical */
376 cdt->cdt_proc_dir = lprocfs_register("hsm",
377 mdt2obd_dev(mdt)->obd_proc_entry,
378 lprocfs_mdt_hsm_vars, mdt);
379 if (IS_ERR(cdt->cdt_proc_dir)) {
380 rc = PTR_ERR(cdt->cdt_proc_dir);
381 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
382 " rc=%d\n", mdt_obd_name(mdt), rc);
383 cdt->cdt_proc_dir = NULL;
391 * remove /proc entries for coordinator
394 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
396 struct coordinator *cdt = &mdt->mdt_coordinator;
398 if (cdt->cdt_proc_dir != NULL)
399 lprocfs_remove(&cdt->cdt_proc_dir);
403 * get vector of hsm cdt /proc vars
407 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
409 return lprocfs_mdt_hsm_vars;
412 /* Release the ressource used by the coordinator. Called when the
413 * coordinator is stopping. */
414 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
416 struct coordinator *cdt = &mdt->mdt_coordinator;
417 struct cdt_agent_req *car, *tmp1;
418 struct hsm_agent *ha, *tmp2;
419 struct cdt_restore_handle *crh, *tmp3;
420 struct mdt_thread_info *cdt_mti;
423 down_write(&cdt->cdt_request_lock);
424 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
426 cfs_hash_del(cdt->cdt_request_cookie_hash,
427 &car->car_hai->hai_cookie,
428 &car->car_cookie_hash);
429 list_del(&car->car_request_list);
430 mdt_cdt_put_request(car);
432 up_write(&cdt->cdt_request_lock);
434 down_write(&cdt->cdt_agent_lock);
435 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
436 list_del(&ha->ha_list);
439 up_write(&cdt->cdt_agent_lock);
441 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
442 mutex_lock(&cdt->cdt_restore_lock);
443 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
444 list_del(&crh->crh_list);
445 /* give back layout lock */
446 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
447 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
449 mutex_unlock(&cdt->cdt_restore_lock);
453 * Coordinator state transition table, indexed on enum cdt_states, taking
454 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
455 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
457 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
458 /* from -> to: stopped init running disable stopping */
459 /* stopped */ { true, true, false, false, false },
460 /* init */ { true, false, true, false, true },
461 /* running */ { false, false, true, true, true },
462 /* disable */ { false, false, true, true, true },
463 /* stopping */ { true, false, false, false, true }
467 * Change coordinator thread state
468 * Some combinations are not valid, so catch them here.
470 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
471 * the transition was not possible.
473 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state,
474 enum cdt_states *old_state)
477 enum cdt_states state;
479 spin_lock(&cdt->cdt_state_lock);
481 state = cdt->cdt_state;
483 if (cdt_transition[state][new_state]) {
484 cdt->cdt_state = new_state;
485 spin_unlock(&cdt->cdt_state_lock);
490 spin_unlock(&cdt->cdt_state_lock);
492 "unexpected coordinator transition, from=%s, to=%s\n",
493 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
502 * \param data [IN] obd device
504 * \retval -ve failure
506 static int mdt_coordinator(void *data)
508 struct mdt_thread_info *mti = data;
509 struct mdt_device *mdt = mti->mti_mdt;
510 struct coordinator *cdt = &mdt->mdt_coordinator;
511 struct hsm_scan_data hsd = { NULL };
516 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
517 mdt_obd_name(mdt), current_pid());
519 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
520 * increases due to a change from /proc we do not overflow the
521 * hsd.request[] vector
523 hsd.max_requests = cdt->cdt_max_requests;
524 request_sz = hsd.max_requests * sizeof(*hsd.request);
525 OBD_ALLOC_LARGE(hsd.request, request_sz);
527 GOTO(out, rc = -ENOMEM);
530 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
532 set_cdt_state(cdt, CDT_RUNNING, NULL);
534 /* Inform mdt_hsm_cdt_start(). */
535 wake_up_all(&cdt->cdt_waitq);
538 struct l_wait_info lwi;
541 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
543 l_wait_event(cdt->cdt_waitq,
544 cdt->cdt_event || (cdt->cdt_state == CDT_STOPPING),
547 CDEBUG(D_HSM, "coordinator resumes\n");
549 if (cdt->cdt_state == CDT_STOPPING) {
554 cdt->cdt_event = false;
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, mdt_coordinator_cb,
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, 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_event = true;
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 init_rwsem(&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 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
889 &cdt_agent_record_hash_ops,
891 if (cdt->cdt_agent_record_hash == NULL)
892 GOTO(out_request_cookie_hash, rc = -ENOMEM);
894 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
896 GOTO(out_agent_record_hash, rc);
898 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
899 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
903 lu_context_enter(&cdt->cdt_session);
904 cdt->cdt_env.le_ses = &cdt->cdt_session;
906 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
907 LASSERT(cdt_mti != NULL);
909 cdt_mti->mti_env = &cdt->cdt_env;
910 cdt_mti->mti_mdt = mdt;
912 hsm_init_ucred(mdt_ucred(cdt_mti));
914 /* default values for /proc tunnables
915 * can be override by MGS conf */
916 cdt->cdt_default_archive_id = 1;
917 cdt->cdt_grace_delay = 60;
918 cdt->cdt_loop_period = 10;
919 cdt->cdt_max_requests = 3;
920 cdt->cdt_policy = CDT_DEFAULT_POLICY;
921 cdt->cdt_active_req_timeout = 3600;
923 /* Initialize cdt_compound_id here to allow its usage for
924 * delayed requests from RAoLU policy */
925 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
927 /* by default do not remove archives on last unlink */
928 cdt->cdt_remove_archive_on_last_unlink = false;
933 lu_env_fini(&cdt->cdt_env);
934 out_agent_record_hash:
935 cfs_hash_putref(cdt->cdt_agent_record_hash);
936 cdt->cdt_agent_record_hash = NULL;
937 out_request_cookie_hash:
938 cfs_hash_putref(cdt->cdt_request_cookie_hash);
939 cdt->cdt_request_cookie_hash = NULL;
945 * free a coordinator thread
946 * \param mdt [IN] device
948 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
950 struct coordinator *cdt = &mdt->mdt_coordinator;
953 lu_context_exit(cdt->cdt_env.le_ses);
954 lu_context_fini(cdt->cdt_env.le_ses);
956 lu_env_fini(&cdt->cdt_env);
958 cfs_hash_putref(cdt->cdt_agent_record_hash);
959 cdt->cdt_agent_record_hash = NULL;
961 cfs_hash_putref(cdt->cdt_request_cookie_hash);
962 cdt->cdt_request_cookie_hash = NULL;
968 * start a coordinator thread
969 * \param mdt [IN] device
971 * \retval -ve failure
973 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
975 struct coordinator *cdt = &mdt->mdt_coordinator;
978 struct mdt_thread_info *cdt_mti;
979 struct task_struct *task;
982 /* functions defined but not yet used
983 * this avoid compilation warning
987 rc = set_cdt_state(cdt, CDT_INIT, NULL);
989 CERROR("%s: Coordinator already started or stopping\n",
994 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
995 cdt->cdt_policy = CDT_DEFAULT_POLICY;
997 /* just need to be larger than previous one */
998 /* cdt_last_cookie is protected by cdt_llog_lock */
999 cdt->cdt_last_cookie = cfs_time_current_sec();
1000 atomic_set(&cdt->cdt_request_count, 0);
1001 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1002 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1003 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1005 /* to avoid deadlock when start is made through /proc
1006 * /proc entries are created by the coordinator thread */
1008 /* set up list of started restore requests */
1009 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1010 rc = mdt_hsm_pending_restore(cdt_mti);
1012 CERROR("%s: cannot take the layout locks needed"
1013 " for registered restore: %d\n",
1014 mdt_obd_name(mdt), rc);
1016 if (mdt->mdt_bottom->dd_rdonly)
1019 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
1022 set_cdt_state(cdt, CDT_STOPPED, NULL);
1023 CERROR("%s: error starting coordinator thread: %d\n",
1024 mdt_obd_name(mdt), rc);
1026 wait_event(cdt->cdt_waitq,
1027 cdt->cdt_state != CDT_INIT);
1028 if (cdt->cdt_state == CDT_RUNNING) {
1029 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1034 "%s: coordinator thread failed to start\n",
1044 * stop a coordinator thread
1045 * \param mdt [IN] device
1047 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1049 struct coordinator *cdt = &mdt->mdt_coordinator;
1053 /* stop coordinator thread */
1054 rc = set_cdt_state(cdt, CDT_STOPPING, NULL);
1058 wake_up_all(&cdt->cdt_waitq);
1059 wait_event(cdt->cdt_waitq, cdt->cdt_state != CDT_STOPPING);
1064 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1065 const struct lu_fid *fid,
1068 struct mdt_object *obj;
1072 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1074 GOTO(out, rc = PTR_ERR(obj));
1076 if (mh.mh_flags & HS_EXISTS &&
1077 mh.mh_arch_id == archive_id)
1078 GOTO(out_obj, rc = 0);
1080 mh.mh_flags |= HS_EXISTS;
1081 mh.mh_arch_id = archive_id;
1082 rc = mdt_hsm_attr_set(mti, obj, &mh);
1085 mdt_object_put(mti->mti_env, obj);
1091 * register all requests from an hal in the memory list
1092 * \param mti [IN] context
1093 * \param hal [IN] request
1094 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1095 * which is running the CT
1097 * \retval -ve failure
1099 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1100 struct hsm_action_list *hal, struct obd_uuid *uuid)
1102 struct mdt_device *mdt = mti->mti_mdt;
1103 struct coordinator *cdt = &mdt->mdt_coordinator;
1104 struct hsm_action_item *hai;
1108 /* register request in memory list */
1109 hai = hai_first(hal);
1110 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1111 struct cdt_agent_req *car;
1113 /* in case of a cancel request, we first mark the ondisk
1114 * record of the request we want to stop as canceled
1115 * this does not change the cancel record
1116 * it will be done when updating the request status
1118 if (hai->hai_action == HSMA_CANCEL) {
1119 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1123 CERROR("%s: mdt_agent_record_update() failed, "
1124 "rc=%d, cannot update status to %s "
1125 "for cookie %#llx\n",
1126 mdt_obd_name(mdt), rc,
1127 agent_req_status2name(ARS_CANCELED),
1132 /* find the running request to set it canceled */
1133 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1135 car->car_canceled = 1;
1136 /* uuid has to be changed to the one running the
1137 * request to cancel */
1138 *uuid = car->car_uuid;
1139 mdt_cdt_put_request(car);
1141 /* no need to memorize cancel request
1142 * this also avoid a deadlock when we receive
1143 * a purge all requests command
1148 if (hai->hai_action == HSMA_ARCHIVE) {
1149 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1150 hal->hal_archive_id);
1157 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1158 hal->hal_archive_id, hal->hal_flags,
1161 GOTO(out, rc = PTR_ERR(car));
1163 rc = mdt_cdt_add_request(cdt, car);
1165 mdt_cdt_free_request(car);
1172 * swap layouts between 2 fids
1173 * \param mti [IN] context
1176 * \param mh_common [IN] MD HSM
1178 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1179 struct mdt_object *obj, const struct lu_fid *dfid,
1180 struct md_hsm *mh_common)
1182 struct mdt_object *dobj;
1183 struct mdt_lock_handle *dlh;
1187 if (!mdt_object_exists(obj))
1188 GOTO(out, rc = -ENOENT);
1190 /* we already have layout lock on obj so take only
1192 dlh = &mti->mti_lh[MDT_LH_OLD];
1193 mdt_lock_reg_init(dlh, LCK_EX);
1194 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1196 GOTO(out, rc = PTR_ERR(dobj));
1198 /* if copy tool closes the volatile before sending the final
1199 * progress through llapi_hsm_copy_end(), all the objects
1200 * are removed and mdd_swap_layout LBUG */
1201 if (!mdt_object_exists(dobj)) {
1202 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1203 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1204 GOTO(out_dobj, rc = -ENOENT);
1206 /* Since we only handle restores here, unconditionally use
1207 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1208 * be preserved in case of failure during swap_layout and not
1209 * leave a file in an intermediate but incoherent state.
1210 * But need to setup HSM xattr of data FID before, reuse
1211 * mti and mh presets for FID in hsm_cdt_request_completed(),
1212 * only need to clear RELEASED and DIRTY.
1214 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1215 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1217 rc = mo_swap_layouts(mti->mti_env,
1218 mdt_object_child(obj),
1219 mdt_object_child(dobj),
1220 SWAP_LAYOUTS_MDS_HSM);
1223 mdt_object_unlock_put(mti, dobj, dlh, 1);
1229 * update status of a completed request
1230 * \param mti [IN] context
1231 * \param pgs [IN] progress of the copy tool
1232 * \param update_record [IN] update llog record
1234 * \retval -ve failure
1236 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1237 struct hsm_progress_kernel *pgs,
1238 const struct cdt_agent_req *car,
1239 enum agent_req_status *status)
1241 const struct lu_env *env = mti->mti_env;
1242 struct mdt_device *mdt = mti->mti_mdt;
1243 struct coordinator *cdt = &mdt->mdt_coordinator;
1244 struct mdt_object *obj = NULL;
1245 int cl_flags = 0, rc = 0;
1248 bool need_changelog = true;
1251 /* default is to retry */
1252 *status = ARS_WAITING;
1254 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1255 * if error/removed continue anyway to get correct reporting done */
1256 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1257 /* we will update MD HSM only if needed */
1258 is_mh_changed = false;
1260 /* no need to change mh->mh_arch_id
1261 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1263 if (pgs->hpk_errval != 0) {
1264 switch (pgs->hpk_errval) {
1266 /* the copy tool does not support cancel
1267 * so the cancel request is failed
1268 * As we cannot distinguish a cancel progress
1269 * from another action progress (they have the
1270 * same cookie), we suppose here the CT returns
1271 * ENOSYS only if does not support cancel
1273 /* this can also happen when cdt calls it to
1274 * for a timed out request */
1275 *status = ARS_FAILED;
1276 /* to have a cancel event in changelog */
1277 pgs->hpk_errval = ECANCELED;
1280 /* the request record has already been set to
1281 * ARS_CANCELED, this set the cancel request
1283 *status = ARS_SUCCEED;
1286 /* retry only if current policy or requested, and
1287 * object is not on error/removed */
1288 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1289 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1290 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1294 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1295 CERROR("%s: Request %#llx on "DFID
1296 " failed, error code %d too large\n",
1298 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1300 hsm_set_cl_error(&cl_flags,
1301 CLF_HSM_ERROVERFLOW);
1304 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1307 switch (car->car_hai->hai_action) {
1309 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1312 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1315 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1318 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1319 CERROR("%s: Failed request %#llx on "DFID
1320 " cannot be a CANCEL\n",
1323 PFID(&pgs->hpk_fid));
1326 CERROR("%s: Failed request %#llx on "DFID
1327 " %d is an unknown action\n",
1329 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1330 car->car_hai->hai_action);
1335 *status = ARS_SUCCEED;
1336 switch (car->car_hai->hai_action) {
1338 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1339 /* set ARCHIVE keep EXIST and clear LOST and
1341 mh.mh_arch_ver = pgs->hpk_data_version;
1342 mh.mh_flags |= HS_ARCHIVED;
1343 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1344 is_mh_changed = true;
1347 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1349 /* do not clear RELEASED and DIRTY here
1350 * this will occur in hsm_swap_layouts()
1353 /* Restoring has changed the file version on
1355 mh.mh_arch_ver = pgs->hpk_data_version;
1356 is_mh_changed = true;
1359 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1360 /* clear ARCHIVED EXISTS and LOST */
1361 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1362 is_mh_changed = true;
1365 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1366 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1369 PFID(&pgs->hpk_fid));
1372 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1374 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1375 car->car_hai->hai_action);
1381 /* rc != 0 means error when analysing action, it may come from
1382 * a crasy CT no need to manage DIRTY
1385 hsm_set_cl_flags(&cl_flags,
1386 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1388 /* unlock is done later, after layout lock management */
1389 if (is_mh_changed && !IS_ERR(obj))
1390 rc = mdt_hsm_attr_set(mti, obj, &mh);
1392 /* we give back layout lock only if restore was successful or
1393 * if no retry will be attempted and if object is still alive,
1394 * in other cases we just unlock the object */
1395 if (car->car_hai->hai_action == HSMA_RESTORE) {
1396 struct cdt_restore_handle *crh;
1398 /* restore in data FID done, we swap the layouts
1399 * only if restore is successful */
1400 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1401 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1404 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1405 *status = ARS_FAILED;
1406 pgs->hpk_errval = -rc;
1409 /* we have to retry, so keep layout lock */
1410 if (*status == ARS_WAITING)
1413 /* restore special case, need to create ChangeLog record
1414 * before to give back layout lock to avoid concurrent
1415 * file updater to post out of order ChangeLog */
1416 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1417 &car->car_hai->hai_fid);
1418 need_changelog = false;
1420 /* give back layout lock */
1421 mutex_lock(&cdt->cdt_restore_lock);
1422 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1424 list_del(&crh->crh_list);
1425 mutex_unlock(&cdt->cdt_restore_lock);
1426 /* Just give back layout lock, we keep the reference
1427 * which is given back later with the lock for HSM
1429 * XXX obj may be invalid so we do not pass it. */
1431 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1434 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1440 /* always add a ChangeLog record */
1442 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1443 &car->car_hai->hai_fid);
1446 mdt_object_put(mti->mti_env, obj);
1452 * update status of a request
1453 * \param mti [IN] context
1454 * \param pgs [IN] progress of the copy tool
1455 * \param update_record [IN] update llog record
1457 * \retval -ve failure
1459 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1460 struct hsm_progress_kernel *pgs,
1461 const int update_record)
1463 struct mdt_device *mdt = mti->mti_mdt;
1464 struct coordinator *cdt = &mdt->mdt_coordinator;
1465 struct cdt_agent_req *car;
1469 /* no coordinator started, so we cannot serve requests */
1470 if (cdt->cdt_state == CDT_STOPPED)
1473 /* first do sanity checks */
1474 car = mdt_cdt_update_request(cdt, pgs);
1476 CERROR("%s: Cannot find running request for cookie %#llx"
1479 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1481 RETURN(PTR_ERR(car));
1484 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1485 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1486 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1487 hsm_copytool_action2name(car->car_hai->hai_action),
1488 pgs->hpk_flags, pgs->hpk_errval,
1489 PFID(&car->car_hai->hai_fid),
1490 PFID(&car->car_hai->hai_dfid));
1492 /* progress is done on FID or data FID depending of the action and
1493 * of the copy progress */
1494 /* for restore progress is used to send back the data FID to cdt */
1495 if (car->car_hai->hai_action == HSMA_RESTORE &&
1496 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1497 car->car_hai->hai_dfid = pgs->hpk_fid;
1499 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1500 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1501 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1502 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1503 CERROR("%s: Progress on "DFID" for cookie %#llx"
1504 " does not match request FID "DFID" nor data FID "
1507 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1508 PFID(&car->car_hai->hai_fid),
1509 PFID(&car->car_hai->hai_dfid));
1510 GOTO(out, rc = -EINVAL);
1513 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1514 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1515 " is not coherent (err=%d and not completed"
1518 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1519 hsm_copytool_action2name(car->car_hai->hai_action),
1520 pgs->hpk_errval, pgs->hpk_flags);
1521 GOTO(out, rc = -EINVAL);
1524 /* now progress is valid */
1526 /* we use a root like ucred */
1527 hsm_init_ucred(mdt_ucred(mti));
1529 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1530 enum agent_req_status status;
1532 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1534 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1536 update_record ? "Updating" : "Not updating",
1537 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1538 hsm_copytool_action2name(car->car_hai->hai_action),
1539 agent_req_status2name(status));
1541 /* update record first (LU-9075) */
1542 if (update_record) {
1545 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1546 &pgs->hpk_cookie, 1,
1549 CERROR("%s: mdt_agent_record_update() failed,"
1550 " rc=%d, cannot update status to %s"
1551 " for cookie %#llx\n",
1552 mdt_obd_name(mdt), rc1,
1553 agent_req_status2name(status),
1555 rc = (rc != 0 ? rc : rc1);
1558 /* then remove request from memory list (LU-9075) */
1559 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1561 /* ct has completed a request, so a slot is available, wakeup
1562 * cdt to find new work */
1563 mdt_hsm_cdt_wakeup(mdt);
1565 /* if copytool send a progress on a canceled request
1566 * we inform copytool it should stop
1568 if (car->car_canceled == 1)
1574 /* remove ref got from mdt_cdt_update_request() */
1575 mdt_cdt_put_request(car);
1582 * data passed to llog_cat_process() callback
1583 * to cancel requests
1585 struct hsm_cancel_all_data {
1586 struct mdt_device *mdt;
1590 * llog_cat_process() callback, used to:
1591 * - purge all requests
1592 * \param env [IN] environment
1593 * \param llh [IN] llog handle
1594 * \param hdr [IN] llog record
1595 * \param data [IN] cb data = struct hsm_cancel_all_data
1597 * \retval -ve failure
1599 static int mdt_cancel_all_cb(const struct lu_env *env,
1600 struct llog_handle *llh,
1601 struct llog_rec_hdr *hdr, void *data)
1603 struct llog_agent_req_rec *larr;
1604 struct hsm_cancel_all_data *hcad;
1608 larr = (struct llog_agent_req_rec *)hdr;
1610 if (larr->arr_status == ARS_WAITING ||
1611 larr->arr_status == ARS_STARTED) {
1612 larr->arr_status = ARS_CANCELED;
1613 larr->arr_req_change = cfs_time_current_sec();
1614 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1621 * cancel all actions
1622 * \param obd [IN] MDT device
1624 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1627 struct lu_context session;
1628 struct mdt_thread_info *mti;
1629 struct coordinator *cdt = &mdt->mdt_coordinator;
1630 struct cdt_agent_req *car;
1631 struct hsm_action_list *hal = NULL;
1632 struct hsm_action_item *hai;
1633 struct hsm_cancel_all_data hcad;
1634 int hal_sz = 0, hal_len, rc;
1635 enum cdt_states old_state;
1638 rc = lu_env_init(&env, LCT_MD_THREAD);
1642 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1643 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1647 lu_context_enter(&session);
1648 env.le_ses = &session;
1650 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1651 LASSERT(mti != NULL);
1653 mti->mti_env = &env;
1656 hsm_init_ucred(mdt_ucred(mti));
1658 /* disable coordinator */
1659 rc = set_cdt_state(cdt, CDT_DISABLE, &old_state);
1663 /* send cancel to all running requests */
1664 down_read(&cdt->cdt_request_lock);
1665 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1666 mdt_cdt_get_request(car);
1667 /* request is not yet removed from list, it will be done
1668 * when copytool will return progress
1671 if (car->car_hai->hai_action == HSMA_CANCEL) {
1672 mdt_cdt_put_request(car);
1677 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1678 cfs_size_round(car->car_hai->hai_len);
1680 if (hal_len > hal_sz && hal_sz > 0) {
1681 /* not enough room, free old buffer */
1682 OBD_FREE(hal, hal_sz);
1686 /* empty buffer, allocate one */
1689 OBD_ALLOC(hal, hal_sz);
1691 mdt_cdt_put_request(car);
1692 up_read(&cdt->cdt_request_lock);
1693 GOTO(out_cdt_state, rc = -ENOMEM);
1697 hal->hal_version = HAL_VERSION;
1698 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1700 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1701 hal->hal_compound_id = car->car_compound_id;
1702 hal->hal_archive_id = car->car_archive_id;
1703 hal->hal_flags = car->car_flags;
1706 hai = hai_first(hal);
1707 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1708 hai->hai_action = HSMA_CANCEL;
1711 /* it is possible to safely call mdt_hsm_agent_send()
1712 * (ie without a deadlock on cdt_request_lock), because the
1713 * write lock is taken only if we are not in purge mode
1714 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1715 * nor mdt_cdt_remove_request())
1717 /* no conflict with cdt thread because cdt is disable and we
1718 * have the request lock */
1719 mdt_hsm_agent_send(mti, hal, 1);
1721 mdt_cdt_put_request(car);
1723 up_read(&cdt->cdt_request_lock);
1726 OBD_FREE(hal, hal_sz);
1728 /* cancel all on-disk records */
1731 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1732 &hcad, 0, 0, WRITE);
1734 /* Enable coordinator, unless the coordinator was stopping. */
1735 set_cdt_state(cdt, old_state, NULL);
1736 lu_context_exit(&session);
1737 lu_context_fini(&session);
1745 * check if a request is compatible with file status
1746 * \param hai [IN] request description
1747 * \param archive_id [IN] request archive id
1748 * \param rq_flags [IN] request flags
1749 * \param hsm [IN] file HSM metadata
1752 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1753 u32 archive_id, u64 rq_flags,
1754 const struct md_hsm *hsm)
1756 int is_compat = false;
1760 hsm_flags = hsm->mh_flags;
1761 switch (hai->hai_action) {
1763 if (!(hsm_flags & HS_NOARCHIVE) &&
1764 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1767 if (hsm_flags & HS_EXISTS &&
1769 archive_id != hsm->mh_arch_id)
1774 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1775 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1779 if (!(hsm_flags & HS_RELEASED) &&
1780 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1787 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1788 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1789 PFID(&hai->hai_fid),
1790 hsm_copytool_action2name(hai->hai_action), rq_flags,
1791 hai->hai_extent.offset, hai->hai_extent.length,
1793 (is_compat ? "compatible" : "uncompatible"));
1799 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1801 static const struct {
1805 } hsm_policy_names[] = {
1806 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1807 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1812 * convert a policy name to a bit
1813 * \param name [IN] policy name
1815 * \retval policy bit
1817 static __u64 hsm_policy_str2bit(const char *name)
1821 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1822 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1823 strcmp(hsm_policy_names[i].name, name) == 0)
1824 return hsm_policy_names[i].bit;
1829 * convert a policy bit field to a string
1830 * \param mask [IN] policy bit field
1831 * \param hexa [IN] print mask before bit names
1832 * \param buffer [OUT] string
1833 * \param count [IN] size of buffer
1835 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1843 seq_printf(m, "(%#llx) ", mask);
1845 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1848 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1849 if (hsm_policy_names[j].bit == bit)
1853 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1855 seq_printf(m, "%s ", hsm_policy_names[j].name);
1857 /* remove last ' ' */
1862 /* methods to read/write HSM policy flags */
1863 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1865 struct mdt_device *mdt = m->private;
1866 struct coordinator *cdt = &mdt->mdt_coordinator;
1869 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1874 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1875 size_t count, loff_t *off)
1877 struct seq_file *m = file->private_data;
1878 struct mdt_device *mdt = m->private;
1879 struct coordinator *cdt = &mdt->mdt_coordinator;
1880 char *start, *token, sign;
1883 __u64 add_mask, remove_mask, set_mask;
1887 if (count + 1 > PAGE_SIZE)
1890 OBD_ALLOC(buf, count + 1);
1894 if (copy_from_user(buf, buffer, count))
1895 GOTO(out, rc = -EFAULT);
1900 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1903 add_mask = remove_mask = set_mask = 0;
1905 token = strsep(&start, "\n ");
1911 if (sign == '-' || sign == '+')
1914 policy = hsm_policy_str2bit(token);
1916 CWARN("%s: '%s' is unknown, "
1917 "supported policies are:\n", mdt_obd_name(mdt),
1919 hsm_policy_bit2str(m, 0, false);
1920 GOTO(out, rc = -EINVAL);
1924 remove_mask |= policy;
1934 } while (start != NULL);
1936 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1937 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1939 /* if no sign in all string, it is a clear and set
1940 * if some sign found, all unsigned are converted
1942 * P1 P2 = set to P1 and P2
1943 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1945 if (remove_mask == 0 && add_mask == 0) {
1946 cdt->cdt_policy = set_mask;
1948 cdt->cdt_policy |= set_mask | add_mask;
1949 cdt->cdt_policy &= ~remove_mask;
1952 GOTO(out, rc = count);
1955 OBD_FREE(buf, count + 1);
1958 LPROC_SEQ_FOPS(mdt_hsm_policy);
1960 #define GENERATE_PROC_METHOD(VAR) \
1961 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1963 struct mdt_device *mdt = m->private; \
1964 struct coordinator *cdt = &mdt->mdt_coordinator; \
1967 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1971 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1972 size_t count, loff_t *off) \
1975 struct seq_file *m = file->private_data; \
1976 struct mdt_device *mdt = m->private; \
1977 struct coordinator *cdt = &mdt->mdt_coordinator; \
1982 rc = lprocfs_str_to_s64(buffer, count, &val); \
1985 if (val > 0 && val < INT_MAX) { \
1992 GENERATE_PROC_METHOD(cdt_loop_period)
1993 GENERATE_PROC_METHOD(cdt_grace_delay)
1994 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1995 GENERATE_PROC_METHOD(cdt_max_requests)
1996 GENERATE_PROC_METHOD(cdt_default_archive_id)
1999 * procfs write method for MDT/hsm_control
2000 * proc entry is in mdt directory so data is mdt obd_device pointer
2002 #define CDT_ENABLE_CMD "enabled"
2003 #define CDT_STOP_CMD "shutdown"
2004 #define CDT_DISABLE_CMD "disabled"
2005 #define CDT_PURGE_CMD "purge"
2006 #define CDT_HELP_CMD "help"
2007 #define CDT_MAX_CMD_LEN 10
2010 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
2011 size_t count, loff_t *off)
2013 struct seq_file *m = file->private_data;
2014 struct obd_device *obd = m->private;
2015 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2016 struct coordinator *cdt = &(mdt->mdt_coordinator);
2018 char kernbuf[CDT_MAX_CMD_LEN];
2021 if (count == 0 || count >= sizeof(kernbuf))
2024 if (copy_from_user(kernbuf, buffer, count))
2028 if (kernbuf[count - 1] == '\n')
2029 kernbuf[count - 1] = 0;
2032 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2033 if (cdt->cdt_state == CDT_DISABLE) {
2034 rc = set_cdt_state(cdt, CDT_RUNNING, NULL);
2035 mdt_hsm_cdt_wakeup(mdt);
2037 rc = mdt_hsm_cdt_start(mdt);
2039 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2040 if ((cdt->cdt_state == CDT_STOPPING) ||
2041 (cdt->cdt_state == CDT_STOPPED)) {
2042 CERROR("%s: Coordinator already stopped\n",
2046 rc = mdt_hsm_cdt_stop(mdt);
2047 mdt_hsm_cdt_wakeup(mdt);
2049 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2050 if ((cdt->cdt_state == CDT_STOPPING) ||
2051 (cdt->cdt_state == CDT_STOPPED)) {
2052 CERROR("%s: Coordinator is stopped\n",
2056 rc = set_cdt_state(cdt, CDT_DISABLE, NULL);
2058 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2059 rc = hsm_cancel_all_actions(mdt);
2060 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2068 CERROR("%s: Valid coordinator control commands are: "
2069 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2070 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2071 CDT_PURGE_CMD, CDT_HELP_CMD);
2079 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2081 struct obd_device *obd = m->private;
2082 struct coordinator *cdt;
2085 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2087 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2093 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2099 for (i = 0; i < 8 * sizeof(mask); i++) {
2100 if (mask & (1UL << i)) {
2101 seq_printf(m, "%s%s", first ? "" : " ",
2102 hsm_copytool_action2name(i));
2112 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2114 struct mdt_device *mdt = m->private;
2115 struct coordinator *cdt = &mdt->mdt_coordinator;
2117 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2121 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2123 struct mdt_device *mdt = m->private;
2124 struct coordinator *cdt = &mdt->mdt_coordinator;
2126 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2130 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2132 struct mdt_device *mdt = m->private;
2133 struct coordinator *cdt = &mdt->mdt_coordinator;
2135 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2138 static inline enum hsm_copytool_action
2139 hsm_copytool_name2action(const char *name)
2141 if (strcasecmp(name, "NOOP") == 0)
2143 else if (strcasecmp(name, "ARCHIVE") == 0)
2144 return HSMA_ARCHIVE;
2145 else if (strcasecmp(name, "RESTORE") == 0)
2146 return HSMA_RESTORE;
2147 else if (strcasecmp(name, "REMOVE") == 0)
2149 else if (strcasecmp(name, "CANCEL") == 0)
2156 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2157 size_t user_count, __u64 *mask)
2159 char *buf, *pos, *name;
2165 if (!(user_count < 4096))
2168 buf_size = user_count + 1;
2170 OBD_ALLOC(buf, buf_size);
2174 if (copy_from_user(buf, user_buf, buf_size - 1))
2175 GOTO(out, rc = -EFAULT);
2177 buf[buf_size - 1] = '\0';
2180 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2186 action = hsm_copytool_name2action(name);
2188 GOTO(out, rc = -EINVAL);
2190 new_mask |= (1UL << action);
2196 OBD_FREE(buf, buf_size);
2202 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2203 size_t count, loff_t *off)
2205 struct seq_file *m = file->private_data;
2206 struct mdt_device *mdt = m->private;
2207 struct coordinator *cdt = &mdt->mdt_coordinator;
2209 return mdt_write_hsm_request_mask(file, buf, count,
2210 &cdt->cdt_user_request_mask);
2214 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2215 size_t count, loff_t *off)
2217 struct seq_file *m = file->private_data;
2218 struct mdt_device *mdt = m->private;
2219 struct coordinator *cdt = &mdt->mdt_coordinator;
2221 return mdt_write_hsm_request_mask(file, buf, count,
2222 &cdt->cdt_group_request_mask);
2226 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2227 size_t count, loff_t *off)
2229 struct seq_file *m = file->private_data;
2230 struct mdt_device *mdt = m->private;
2231 struct coordinator *cdt = &mdt->mdt_coordinator;
2233 return mdt_write_hsm_request_mask(file, buf, count,
2234 &cdt->cdt_other_request_mask);
2237 static int mdt_hsm_cdt_raolu_seq_show(struct seq_file *m, void *data)
2239 struct mdt_device *mdt = m->private;
2240 struct coordinator *cdt = &mdt->mdt_coordinator;
2243 seq_printf(m, "%d\n", (int)cdt->cdt_remove_archive_on_last_unlink);
2248 mdt_hsm_cdt_raolu_seq_write(struct file *file, const char __user *buffer,
2249 size_t count, loff_t *off)
2252 struct seq_file *m = file->private_data;
2253 struct mdt_device *mdt = m->private;
2254 struct coordinator *cdt = &mdt->mdt_coordinator;
2259 rc = lprocfs_str_to_s64(buffer, count, &val);
2263 cdt->cdt_remove_archive_on_last_unlink = val;
2267 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2268 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2269 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2270 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2271 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2272 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2273 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2274 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2275 LPROC_SEQ_FOPS(mdt_hsm_cdt_raolu);
2277 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2279 .fops = &mdt_hsm_agent_fops },
2280 { .name = "actions",
2281 .fops = &mdt_hsm_actions_fops,
2282 .proc_mode = 0444 },
2283 { .name = "default_archive_id",
2284 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2285 { .name = "grace_delay",
2286 .fops = &mdt_hsm_cdt_grace_delay_fops },
2287 { .name = "loop_period",
2288 .fops = &mdt_hsm_cdt_loop_period_fops },
2289 { .name = "max_requests",
2290 .fops = &mdt_hsm_cdt_max_requests_fops },
2292 .fops = &mdt_hsm_policy_fops },
2293 { .name = "active_request_timeout",
2294 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2295 { .name = "active_requests",
2296 .fops = &mdt_hsm_active_requests_fops },
2297 { .name = "user_request_mask",
2298 .fops = &mdt_hsm_user_request_mask_fops, },
2299 { .name = "group_request_mask",
2300 .fops = &mdt_hsm_group_request_mask_fops, },
2301 { .name = "other_request_mask",
2302 .fops = &mdt_hsm_other_request_mask_fops, },
2303 { .name = "remove_archive_on_last_unlink",
2304 .fops = &mdt_hsm_cdt_raolu_fops, },