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;
147 struct hsm_thread_data {
148 struct mdt_thread_info *cdt_mti;
149 struct hsm_scan_request *request;
152 * llog_cat_process() callback, used to:
153 * - find waiting request and start action
154 * - purge canceled and done requests
155 * \param env [IN] environment
156 * \param llh [IN] llog handle
157 * \param hdr [IN] llog record
158 * \param data [IN/OUT] cb data = struct hsm_scan_data
160 * \retval -ve failure
162 static int mdt_coordinator_cb(const struct lu_env *env,
163 struct llog_handle *llh,
164 struct llog_rec_hdr *hdr,
167 struct llog_agent_req_rec *larr;
168 struct hsm_scan_data *hsd;
169 struct hsm_action_item *hai;
170 struct mdt_device *mdt;
171 struct coordinator *cdt;
176 mdt = hsd->mti->mti_mdt;
177 cdt = &mdt->mdt_coordinator;
179 larr = (struct llog_agent_req_rec *)hdr;
180 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
181 switch (larr->arr_status) {
184 struct hsm_scan_request *request;
186 /* Are agents full? */
187 if (atomic_read(&cdt->cdt_request_count) >=
188 cdt->cdt_max_requests)
191 /* first search whether the request is found in the
192 * list we have built. */
194 for (i = 0; i < hsd->request_cnt; i++) {
195 if (hsd->request[i].hal->hal_compound_id ==
196 larr->arr_compound_id) {
197 request = &hsd->request[i];
203 struct hsm_action_list *hal;
205 if (hsd->request_cnt == hsd->max_requests)
206 /* Unknown request and no more room
207 * for a new request. Continue to scan
208 * to find other entries for already
213 request = &hsd->request[hsd->request_cnt];
215 /* allocates hai vector size just needs to be large
218 sizeof(*request->hal) +
219 cfs_size_round(MTI_NAME_MAXLEN+1) +
220 2 * cfs_size_round(larr->arr_hai.hai_len);
221 OBD_ALLOC(hal, request->hal_sz);
224 hal->hal_version = HAL_VERSION;
225 strlcpy(hal->hal_fsname, hsd->fs_name,
226 MTI_NAME_MAXLEN + 1);
227 hal->hal_compound_id = larr->arr_compound_id;
228 hal->hal_archive_id = larr->arr_archive_id;
229 hal->hal_flags = larr->arr_flags;
231 request->hal_used_sz = hal_size(hal);
234 hai = hai_first(hal);
236 /* request is known */
237 /* we check if record archive num is the same as the
238 * known request, if not we will serve it in multiple
239 * time because we do not know if the agent can serve
241 * a use case is a compound made of multiple restore
242 * where the files are not archived in the same backend
244 if (larr->arr_archive_id !=
245 request->hal->hal_archive_id)
248 if (request->hal_sz <
249 request->hal_used_sz +
250 cfs_size_round(larr->arr_hai.hai_len)) {
251 /* Not enough room, need an extension */
255 sz = 2 * request->hal_sz;
256 OBD_ALLOC(hal_buffer, sz);
259 memcpy(hal_buffer, request->hal,
260 request->hal_used_sz);
261 OBD_FREE(request->hal,
263 request->hal = hal_buffer;
264 request->hal_sz = sz;
266 hai = hai_first(request->hal);
267 for (i = 0; i < request->hal->hal_count; i++)
270 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
271 hai->hai_cookie = larr->arr_hai.hai_cookie;
272 hai->hai_gid = larr->arr_hai.hai_gid;
274 request->hal_used_sz += cfs_size_round(hai->hai_len);
275 request->hal->hal_count++;
277 if (hai->hai_action != HSMA_CANCEL)
278 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
279 llh->lgh_hdr->llh_cat_idx,
284 struct hsm_progress_kernel pgs;
285 struct cdt_agent_req *car;
286 cfs_time_t now = cfs_time_current_sec();
289 /* we search for a running request
290 * error may happen if coordinator crashes or stopped
291 * with running request
293 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie);
295 last = larr->arr_req_change;
297 last = car->car_req_update;
298 mdt_cdt_put_request(car);
301 /* test if request too long, if yes cancel it
302 * the same way the copy tool acknowledge a cancel request */
303 if (now <= last + cdt->cdt_active_req_timeout)
306 dump_llog_agent_req_rec("request timed out, start cleaning",
308 /* a too old cancel request just needs to be removed
309 * this can happen, if copy tool does not support
310 * cancel for other requests, we have to remove the
311 * running request and notify the copytool */
312 pgs.hpk_fid = larr->arr_hai.hai_fid;
313 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
314 pgs.hpk_extent = larr->arr_hai.hai_extent;
315 pgs.hpk_flags = HP_FLAG_COMPLETED;
316 pgs.hpk_errval = ENOSYS;
317 pgs.hpk_data_version = 0;
319 /* update request state, but do not record in llog, to
320 * avoid deadlock on cdt_llog_lock */
321 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
323 CERROR("%s: cannot cleanup timed out request: "
324 DFID" for cookie %#llx action=%s\n",
326 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
327 hsm_copytool_action2name(
328 larr->arr_hai.hai_action));
331 /* The request no longer exists, forget
332 * about it, and do not send a cancel request
333 * to the client, for which an error will be
334 * sent back, leading to an endless cycle of
336 cdt_agent_record_hash_del(cdt,
337 larr->arr_hai.hai_cookie);
338 RETURN(LLOG_DEL_RECORD);
341 /* XXX A cancel request cannot be cancelled. */
342 if (larr->arr_hai.hai_action == HSMA_CANCEL)
345 larr->arr_status = ARS_CANCELED;
346 larr->arr_req_change = now;
347 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
349 CERROR("%s: cannot update agent log: rc = %d\n",
350 mdt_obd_name(mdt), rc);
356 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
357 cfs_time_current_sec()) {
358 cdt_agent_record_hash_del(cdt,
359 larr->arr_hai.hai_cookie);
360 RETURN(LLOG_DEL_RECORD);
368 * create /proc entries for coordinator
371 * \retval -ve failure
373 int hsm_cdt_procfs_init(struct mdt_device *mdt)
375 struct coordinator *cdt = &mdt->mdt_coordinator;
379 /* init /proc entries, failure is not critical */
380 cdt->cdt_proc_dir = lprocfs_register("hsm",
381 mdt2obd_dev(mdt)->obd_proc_entry,
382 lprocfs_mdt_hsm_vars, mdt);
383 if (IS_ERR(cdt->cdt_proc_dir)) {
384 rc = PTR_ERR(cdt->cdt_proc_dir);
385 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
386 " rc=%d\n", mdt_obd_name(mdt), rc);
387 cdt->cdt_proc_dir = NULL;
395 * remove /proc entries for coordinator
398 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
400 struct coordinator *cdt = &mdt->mdt_coordinator;
402 if (cdt->cdt_proc_dir != NULL)
403 lprocfs_remove(&cdt->cdt_proc_dir);
407 * get vector of hsm cdt /proc vars
411 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
413 return lprocfs_mdt_hsm_vars;
416 /* Release the ressource used by the coordinator. Called when the
417 * coordinator is stopping. */
418 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
420 struct coordinator *cdt = &mdt->mdt_coordinator;
421 struct cdt_agent_req *car, *tmp1;
422 struct hsm_agent *ha, *tmp2;
423 struct cdt_restore_handle *crh, *tmp3;
424 struct mdt_thread_info *cdt_mti;
427 down_write(&cdt->cdt_request_lock);
428 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
430 cfs_hash_del(cdt->cdt_request_cookie_hash,
431 &car->car_hai->hai_cookie,
432 &car->car_cookie_hash);
433 list_del(&car->car_request_list);
434 mdt_cdt_put_request(car);
436 up_write(&cdt->cdt_request_lock);
438 down_write(&cdt->cdt_agent_lock);
439 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
440 list_del(&ha->ha_list);
443 up_write(&cdt->cdt_agent_lock);
445 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
446 mutex_lock(&cdt->cdt_restore_lock);
447 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
448 list_del(&crh->crh_list);
449 /* give back layout lock */
450 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
451 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
453 mutex_unlock(&cdt->cdt_restore_lock);
457 * Coordinator state transition table, indexed on enum cdt_states, taking
458 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
459 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
461 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
462 /* from -> to: stopped init running disable stopping */
463 /* stopped */ { true, true, false, false, false },
464 /* init */ { true, false, true, false, false },
465 /* running */ { false, false, true, true, true },
466 /* disable */ { false, false, true, true, true },
467 /* stopping */ { true, false, false, false, false }
471 * Change coordinator thread state
472 * Some combinations are not valid, so catch them here.
474 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
475 * the transition was not possible.
477 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state,
478 enum cdt_states *old_state)
481 enum cdt_states state;
483 spin_lock(&cdt->cdt_state_lock);
485 state = cdt->cdt_state;
487 if (cdt_transition[state][new_state]) {
488 cdt->cdt_state = new_state;
489 spin_unlock(&cdt->cdt_state_lock);
494 spin_unlock(&cdt->cdt_state_lock);
496 "unexpected coordinator transition, from=%s, to=%s\n",
497 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
506 * \param data [IN] obd device
508 * \retval -ve failure
510 static int mdt_coordinator(void *data)
512 struct hsm_thread_data *thread_data = data;
513 struct mdt_thread_info *mti = thread_data->cdt_mti;
514 struct mdt_device *mdt = mti->mti_mdt;
515 struct coordinator *cdt = &mdt->mdt_coordinator;
516 struct hsm_scan_data hsd = { NULL };
517 time64_t wait_event_time = 1 * HZ;
518 time64_t last_housekeeping = 0;
523 /* set up hsd->request and max_requests */
524 hsd.max_requests = cdt->cdt_max_requests;
525 request_sz = hsd.max_requests * sizeof(*hsd.request);
526 hsd.request = thread_data->request;
528 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
529 mdt_obd_name(mdt), current_pid());
532 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
534 set_cdt_state(cdt, CDT_RUNNING, NULL);
536 /* Inform mdt_hsm_cdt_start(). */
537 wake_up_all(&cdt->cdt_waitq);
542 /* Limit execution of the expensive requests traversal
543 * to at most every "wait_event_time" jiffies. This prevents
544 * repeatedly locking/unlocking the catalog for each request
545 * and preventing other HSM operations from happening */
546 wait_event_interruptible_timeout(cdt->cdt_waitq,
547 kthread_should_stop(),
550 CDEBUG(D_HSM, "coordinator resumes\n");
552 if (kthread_should_stop()) {
553 CDEBUG(D_HSM, "Coordinator stops\n");
558 /* if coordinator is suspended continue to wait */
559 if (cdt->cdt_state == CDT_DISABLE) {
560 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
564 /* If no event, and no housekeeping to do, continue to
566 if (last_housekeeping + cdt->cdt_loop_period <= get_seconds())
567 last_housekeeping = get_seconds();
568 else if (!cdt->cdt_event)
571 cdt->cdt_event = false;
573 CDEBUG(D_HSM, "coordinator starts reading llog\n");
575 if (hsd.max_requests != cdt->cdt_max_requests) {
576 /* cdt_max_requests has changed,
577 * we need to allocate a new buffer
579 struct hsm_scan_request *tmp = NULL;
580 int max_requests = cdt->cdt_max_requests;
581 OBD_ALLOC_LARGE(tmp, max_requests *
582 sizeof(struct hsm_scan_request));
584 CERROR("Failed to resize request buffer, "
585 "keeping it at %d\n",
587 cdt->cdt_max_requests = hsd.max_requests;
589 OBD_FREE_LARGE(hsd.request, request_sz);
590 hsd.max_requests = max_requests;
591 request_sz = hsd.max_requests *
592 sizeof(struct hsm_scan_request);
599 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
604 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
606 if (list_empty(&cdt->cdt_agents)) {
607 CDEBUG(D_HSM, "no agent available, "
608 "coordinator sleeps\n");
612 /* here hsd contains a list of requests to be started */
613 for (i = 0; i < hsd.request_cnt; i++) {
614 struct hsm_scan_request *request = &hsd.request[i];
615 struct hsm_action_list *hal = request->hal;
616 struct hsm_action_item *hai;
619 enum agent_req_status status;
621 /* still room for work ? */
622 if (atomic_read(&cdt->cdt_request_count) >=
623 cdt->cdt_max_requests)
626 rc = mdt_hsm_agent_send(mti, hal, 0);
627 /* if failure, we suppose it is temporary
628 * if the copy tool failed to do the request
629 * it has to use hsm_progress
631 status = (rc ? ARS_WAITING : ARS_STARTED);
633 /* set up cookie vector to set records status
634 * after copy tools start or failed
636 sz = hal->hal_count * sizeof(__u64);
637 OBD_ALLOC(cookies, sz);
641 hai = hai_first(hal);
642 for (j = 0; j < hal->hal_count; j++) {
643 cookies[j] = hai->hai_cookie;
647 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
648 hal->hal_count, status);
650 CERROR("%s: mdt_agent_record_update() failed, "
651 "rc=%d, cannot update status to %s "
653 mdt_obd_name(mdt), rc,
654 agent_req_status2name(status),
657 OBD_FREE(cookies, sz);
660 /* free hal allocated by callback */
661 for (i = 0; i < hsd.request_cnt; i++) {
662 struct hsm_scan_request *request = &hsd.request[i];
664 OBD_FREE(request->hal, request->hal_sz);
669 OBD_FREE_LARGE(hsd.request, request_sz);
671 mdt_hsm_cdt_cleanup(mdt);
674 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
675 mdt_obd_name(mdt), current_pid(), rc);
677 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
679 mdt_obd_name(mdt), current_pid());
685 * lookup a restore handle by FID
686 * caller needs to hold cdt_restore_lock
687 * \param cdt [IN] coordinator
688 * \param fid [IN] FID
689 * \retval cdt_restore_handle found
690 * \retval NULL not found
692 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
693 const struct lu_fid *fid)
695 struct cdt_restore_handle *crh;
698 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
699 if (lu_fid_eq(&crh->crh_fid, fid))
706 * data passed to llog_cat_process() callback
707 * to scan requests and take actions
709 struct hsm_restore_data {
710 struct mdt_thread_info *hrd_mti;
714 * llog_cat_process() callback, used to:
715 * - find restore request and allocate the restore handle
716 * \param env [IN] environment
717 * \param llh [IN] llog handle
718 * \param hdr [IN] llog record
719 * \param data [IN/OUT] cb data = struct hsm_restore_data
721 * \retval -ve failure
723 static int hsm_restore_cb(const struct lu_env *env,
724 struct llog_handle *llh,
725 struct llog_rec_hdr *hdr, void *data)
727 struct llog_agent_req_rec *larr;
728 struct hsm_restore_data *hrd;
729 struct cdt_restore_handle *crh;
730 struct hsm_action_item *hai;
731 struct mdt_thread_info *mti;
732 struct coordinator *cdt;
733 struct mdt_object *child;
739 cdt = &mti->mti_mdt->mdt_coordinator;
741 larr = (struct llog_agent_req_rec *)hdr;
742 hai = &larr->arr_hai;
743 if (hai->hai_cookie > cdt->cdt_last_cookie)
744 /* update the cookie to avoid collision */
745 cdt->cdt_last_cookie = hai->hai_cookie + 1;
747 if (hai->hai_action != HSMA_RESTORE ||
748 agent_req_in_final_state(larr->arr_status))
751 /* restore request not in a final state */
753 /* force replay of restore requests left in started state from previous
754 * CDT context, to be canceled later if finally found to be incompatible
755 * when being re-started */
756 if (larr->arr_status == ARS_STARTED) {
757 larr->arr_status = ARS_WAITING;
758 larr->arr_req_change = cfs_time_current_sec();
759 rc = llog_write(env, llh, hdr, hdr->lrh_index);
764 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
768 crh->crh_fid = hai->hai_fid;
769 /* in V1 all file is restored
770 crh->extent.start = hai->hai_extent.offset;
771 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
773 crh->crh_extent.start = 0;
774 crh->crh_extent.end = hai->hai_extent.length;
775 /* get the layout lock */
776 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
777 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
778 MDS_INODELOCK_LAYOUT);
780 GOTO(out, rc = PTR_ERR(child));
783 /* we choose to not keep a reference
784 * on the object during the restore time which can be very long */
785 mdt_object_put(mti->mti_env, child);
787 mutex_lock(&cdt->cdt_restore_lock);
788 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
789 mutex_unlock(&cdt->cdt_restore_lock);
796 * restore coordinator state at startup
797 * the goal is to take a layout lock for each registered restore request
798 * \param mti [IN] context
800 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
802 struct hsm_restore_data hrd;
808 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
814 static int hsm_init_ucred(struct lu_ucred *uc)
818 uc->uc_valid = UCRED_OLD;
827 uc->uc_suppgids[0] = -1;
828 uc->uc_suppgids[1] = -1;
829 uc->uc_cap = CFS_CAP_FS_MASK;
832 uc->uc_identity = NULL;
838 * initialize coordinator struct
839 * \param mdt [IN] device
841 * \retval -ve failure
843 int mdt_hsm_cdt_init(struct mdt_device *mdt)
845 struct coordinator *cdt = &mdt->mdt_coordinator;
846 struct mdt_thread_info *cdt_mti = NULL;
850 init_waitqueue_head(&cdt->cdt_waitq);
851 init_rwsem(&cdt->cdt_llog_lock);
852 init_rwsem(&cdt->cdt_agent_lock);
853 init_rwsem(&cdt->cdt_request_lock);
854 mutex_init(&cdt->cdt_restore_lock);
855 spin_lock_init(&cdt->cdt_state_lock);
856 set_cdt_state(cdt, CDT_STOPPED, NULL);
858 INIT_LIST_HEAD(&cdt->cdt_request_list);
859 INIT_LIST_HEAD(&cdt->cdt_agents);
860 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
862 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
869 &cdt_request_cookie_hash_ops,
871 if (cdt->cdt_request_cookie_hash == NULL)
874 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
881 &cdt_agent_record_hash_ops,
883 if (cdt->cdt_agent_record_hash == NULL)
884 GOTO(out_request_cookie_hash, rc = -ENOMEM);
886 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
888 GOTO(out_agent_record_hash, rc);
890 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
891 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
895 lu_context_enter(&cdt->cdt_session);
896 cdt->cdt_env.le_ses = &cdt->cdt_session;
898 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
899 LASSERT(cdt_mti != NULL);
901 cdt_mti->mti_env = &cdt->cdt_env;
902 cdt_mti->mti_mdt = mdt;
904 hsm_init_ucred(mdt_ucred(cdt_mti));
906 /* default values for /proc tunnables
907 * can be override by MGS conf */
908 cdt->cdt_default_archive_id = 1;
909 cdt->cdt_grace_delay = 60;
910 cdt->cdt_loop_period = 10;
911 cdt->cdt_max_requests = 3;
912 cdt->cdt_policy = CDT_DEFAULT_POLICY;
913 cdt->cdt_active_req_timeout = 3600;
915 /* Initialize cdt_compound_id here to allow its usage for
916 * delayed requests from RAoLU policy */
917 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
919 /* by default do not remove archives on last unlink */
920 cdt->cdt_remove_archive_on_last_unlink = false;
925 lu_env_fini(&cdt->cdt_env);
926 out_agent_record_hash:
927 cfs_hash_putref(cdt->cdt_agent_record_hash);
928 cdt->cdt_agent_record_hash = NULL;
929 out_request_cookie_hash:
930 cfs_hash_putref(cdt->cdt_request_cookie_hash);
931 cdt->cdt_request_cookie_hash = NULL;
937 * free a coordinator thread
938 * \param mdt [IN] device
940 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
942 struct coordinator *cdt = &mdt->mdt_coordinator;
945 lu_context_exit(cdt->cdt_env.le_ses);
946 lu_context_fini(cdt->cdt_env.le_ses);
948 lu_env_fini(&cdt->cdt_env);
950 cfs_hash_putref(cdt->cdt_agent_record_hash);
951 cdt->cdt_agent_record_hash = NULL;
953 cfs_hash_putref(cdt->cdt_request_cookie_hash);
954 cdt->cdt_request_cookie_hash = NULL;
960 * start a coordinator thread
961 * \param mdt [IN] device
963 * \retval -ve failure
965 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
967 struct coordinator *cdt = &mdt->mdt_coordinator;
970 struct task_struct *task;
972 struct hsm_thread_data thread_data;
975 /* functions defined but not yet used
976 * this avoid compilation warning
980 rc = set_cdt_state(cdt, CDT_INIT, NULL);
982 CERROR("%s: Coordinator already started or stopping\n",
987 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
988 cdt->cdt_policy = CDT_DEFAULT_POLICY;
990 /* just need to be larger than previous one */
991 /* cdt_last_cookie is protected by cdt_llog_lock */
992 cdt->cdt_last_cookie = cfs_time_current_sec();
993 atomic_set(&cdt->cdt_request_count, 0);
994 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
995 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
996 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
998 /* to avoid deadlock when start is made through /proc
999 * /proc entries are created by the coordinator thread */
1001 /* set up list of started restore requests */
1002 thread_data.cdt_mti =
1003 lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1004 rc = mdt_hsm_pending_restore(thread_data.cdt_mti);
1006 CERROR("%s: cannot take the layout locks needed"
1007 " for registered restore: %d\n",
1008 mdt_obd_name(mdt), rc);
1010 if (mdt->mdt_bottom->dd_rdonly)
1013 /* Allocate the initial hsd.request[] vector*/
1014 request_sz = cdt->cdt_max_requests * sizeof(struct hsm_scan_request);
1015 OBD_ALLOC_LARGE(thread_data.request, request_sz);
1016 if (!thread_data.request) {
1017 set_cdt_state(cdt, CDT_STOPPED, NULL);
1021 task = kthread_run(mdt_coordinator, &thread_data, "hsm_cdtr");
1024 set_cdt_state(cdt, CDT_STOPPED, NULL);
1025 OBD_FREE(thread_data.request, request_sz);
1026 CERROR("%s: error starting coordinator thread: %d\n",
1027 mdt_obd_name(mdt), rc);
1029 cdt->cdt_task = task;
1030 wait_event(cdt->cdt_waitq,
1031 cdt->cdt_state != CDT_INIT);
1032 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1041 * stop a coordinator thread
1042 * \param mdt [IN] device
1044 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1046 struct coordinator *cdt = &mdt->mdt_coordinator;
1050 /* stop coordinator thread */
1051 rc = set_cdt_state(cdt, CDT_STOPPING, NULL);
1053 kthread_stop(cdt->cdt_task);
1054 cdt->cdt_task = NULL;
1055 set_cdt_state(cdt, CDT_STOPPED, NULL);
1061 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1062 const struct lu_fid *fid,
1065 struct mdt_object *obj;
1069 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1071 GOTO(out, rc = PTR_ERR(obj));
1073 if (mh.mh_flags & HS_EXISTS &&
1074 mh.mh_arch_id == archive_id)
1075 GOTO(out_obj, rc = 0);
1077 mh.mh_flags |= HS_EXISTS;
1078 mh.mh_arch_id = archive_id;
1079 rc = mdt_hsm_attr_set(mti, obj, &mh);
1082 mdt_object_put(mti->mti_env, obj);
1088 * register all requests from an hal in the memory list
1089 * \param mti [IN] context
1090 * \param hal [IN] request
1091 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1092 * which is running the CT
1094 * \retval -ve failure
1096 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1097 struct hsm_action_list *hal, struct obd_uuid *uuid)
1099 struct mdt_device *mdt = mti->mti_mdt;
1100 struct coordinator *cdt = &mdt->mdt_coordinator;
1101 struct hsm_action_item *hai;
1105 /* register request in memory list */
1106 hai = hai_first(hal);
1107 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1108 struct cdt_agent_req *car;
1110 /* in case of a cancel request, we first mark the ondisk
1111 * record of the request we want to stop as canceled
1112 * this does not change the cancel record
1113 * it will be done when updating the request status
1115 if (hai->hai_action == HSMA_CANCEL) {
1116 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1120 CERROR("%s: mdt_agent_record_update() failed, "
1121 "rc=%d, cannot update status to %s "
1122 "for cookie %#llx\n",
1123 mdt_obd_name(mdt), rc,
1124 agent_req_status2name(ARS_CANCELED),
1129 /* find the running request to set it canceled */
1130 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1132 car->car_canceled = 1;
1133 /* uuid has to be changed to the one running the
1134 * request to cancel */
1135 *uuid = car->car_uuid;
1136 mdt_cdt_put_request(car);
1138 /* no need to memorize cancel request
1139 * this also avoid a deadlock when we receive
1140 * a purge all requests command
1145 if (hai->hai_action == HSMA_ARCHIVE) {
1146 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1147 hal->hal_archive_id);
1154 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1155 hal->hal_archive_id, hal->hal_flags,
1158 GOTO(out, rc = PTR_ERR(car));
1160 rc = mdt_cdt_add_request(cdt, car);
1162 mdt_cdt_free_request(car);
1169 * swap layouts between 2 fids
1170 * \param mti [IN] context
1173 * \param mh_common [IN] MD HSM
1175 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1176 struct mdt_object *obj, const struct lu_fid *dfid,
1177 struct md_hsm *mh_common)
1179 struct mdt_object *dobj;
1180 struct mdt_lock_handle *dlh;
1184 if (!mdt_object_exists(obj))
1185 GOTO(out, rc = -ENOENT);
1187 /* we already have layout lock on obj so take only
1189 dlh = &mti->mti_lh[MDT_LH_OLD];
1190 mdt_lock_reg_init(dlh, LCK_EX);
1191 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1193 GOTO(out, rc = PTR_ERR(dobj));
1195 /* if copy tool closes the volatile before sending the final
1196 * progress through llapi_hsm_copy_end(), all the objects
1197 * are removed and mdd_swap_layout LBUG */
1198 if (!mdt_object_exists(dobj)) {
1199 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1200 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1201 GOTO(out_dobj, rc = -ENOENT);
1203 /* Since we only handle restores here, unconditionally use
1204 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1205 * be preserved in case of failure during swap_layout and not
1206 * leave a file in an intermediate but incoherent state.
1207 * But need to setup HSM xattr of data FID before, reuse
1208 * mti and mh presets for FID in hsm_cdt_request_completed(),
1209 * only need to clear RELEASED and DIRTY.
1211 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1212 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1214 rc = mo_swap_layouts(mti->mti_env,
1215 mdt_object_child(obj),
1216 mdt_object_child(dobj),
1217 SWAP_LAYOUTS_MDS_HSM);
1220 mdt_object_unlock_put(mti, dobj, dlh, 1);
1226 * update status of a completed request
1227 * \param mti [IN] context
1228 * \param pgs [IN] progress of the copy tool
1229 * \param update_record [IN] update llog record
1231 * \retval -ve failure
1233 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1234 struct hsm_progress_kernel *pgs,
1235 const struct cdt_agent_req *car,
1236 enum agent_req_status *status)
1238 const struct lu_env *env = mti->mti_env;
1239 struct mdt_device *mdt = mti->mti_mdt;
1240 struct coordinator *cdt = &mdt->mdt_coordinator;
1241 struct mdt_object *obj = NULL;
1242 int cl_flags = 0, rc = 0;
1245 bool need_changelog = true;
1248 /* default is to retry */
1249 *status = ARS_WAITING;
1251 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1252 * if error/removed continue anyway to get correct reporting done */
1253 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1254 /* we will update MD HSM only if needed */
1255 is_mh_changed = false;
1257 /* no need to change mh->mh_arch_id
1258 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1260 if (pgs->hpk_errval != 0) {
1261 switch (pgs->hpk_errval) {
1263 /* the copy tool does not support cancel
1264 * so the cancel request is failed
1265 * As we cannot distinguish a cancel progress
1266 * from another action progress (they have the
1267 * same cookie), we suppose here the CT returns
1268 * ENOSYS only if does not support cancel
1270 /* this can also happen when cdt calls it to
1271 * for a timed out request */
1272 *status = ARS_FAILED;
1273 /* to have a cancel event in changelog */
1274 pgs->hpk_errval = ECANCELED;
1277 /* the request record has already been set to
1278 * ARS_CANCELED, this set the cancel request
1280 *status = ARS_SUCCEED;
1283 /* retry only if current policy or requested, and
1284 * object is not on error/removed */
1285 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1286 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1287 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1291 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1292 CERROR("%s: Request %#llx on "DFID
1293 " failed, error code %d too large\n",
1295 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1297 hsm_set_cl_error(&cl_flags,
1298 CLF_HSM_ERROVERFLOW);
1301 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1304 switch (car->car_hai->hai_action) {
1306 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1309 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1312 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1315 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1316 CERROR("%s: Failed request %#llx on "DFID
1317 " cannot be a CANCEL\n",
1320 PFID(&pgs->hpk_fid));
1323 CERROR("%s: Failed request %#llx on "DFID
1324 " %d is an unknown action\n",
1326 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1327 car->car_hai->hai_action);
1332 *status = ARS_SUCCEED;
1333 switch (car->car_hai->hai_action) {
1335 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1336 /* set ARCHIVE keep EXIST and clear LOST and
1338 mh.mh_arch_ver = pgs->hpk_data_version;
1339 mh.mh_flags |= HS_ARCHIVED;
1340 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1341 is_mh_changed = true;
1344 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1346 /* do not clear RELEASED and DIRTY here
1347 * this will occur in hsm_swap_layouts()
1350 /* Restoring has changed the file version on
1352 mh.mh_arch_ver = pgs->hpk_data_version;
1353 is_mh_changed = true;
1356 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1357 /* clear ARCHIVED EXISTS and LOST */
1358 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1359 is_mh_changed = true;
1362 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1363 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1366 PFID(&pgs->hpk_fid));
1369 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1371 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1372 car->car_hai->hai_action);
1378 /* rc != 0 means error when analysing action, it may come from
1379 * a crasy CT no need to manage DIRTY
1382 hsm_set_cl_flags(&cl_flags,
1383 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1385 /* unlock is done later, after layout lock management */
1386 if (is_mh_changed && !IS_ERR(obj))
1387 rc = mdt_hsm_attr_set(mti, obj, &mh);
1389 /* we give back layout lock only if restore was successful or
1390 * if no retry will be attempted and if object is still alive,
1391 * in other cases we just unlock the object */
1392 if (car->car_hai->hai_action == HSMA_RESTORE) {
1393 struct cdt_restore_handle *crh;
1395 /* restore in data FID done, we swap the layouts
1396 * only if restore is successful */
1397 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1398 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1401 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1402 *status = ARS_FAILED;
1403 pgs->hpk_errval = -rc;
1406 /* we have to retry, so keep layout lock */
1407 if (*status == ARS_WAITING)
1410 /* restore special case, need to create ChangeLog record
1411 * before to give back layout lock to avoid concurrent
1412 * file updater to post out of order ChangeLog */
1413 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1414 &car->car_hai->hai_fid);
1415 need_changelog = false;
1417 /* give back layout lock */
1418 mutex_lock(&cdt->cdt_restore_lock);
1419 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1421 list_del(&crh->crh_list);
1422 mutex_unlock(&cdt->cdt_restore_lock);
1423 /* Just give back layout lock, we keep the reference
1424 * which is given back later with the lock for HSM
1426 * XXX obj may be invalid so we do not pass it. */
1428 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1431 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1437 /* always add a ChangeLog record */
1439 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1440 &car->car_hai->hai_fid);
1443 mdt_object_put(mti->mti_env, obj);
1449 * update status of a request
1450 * \param mti [IN] context
1451 * \param pgs [IN] progress of the copy tool
1452 * \param update_record [IN] update llog record
1454 * \retval -ve failure
1456 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1457 struct hsm_progress_kernel *pgs,
1458 const int update_record)
1460 struct mdt_device *mdt = mti->mti_mdt;
1461 struct coordinator *cdt = &mdt->mdt_coordinator;
1462 struct cdt_agent_req *car;
1466 /* no coordinator started, so we cannot serve requests */
1467 if (cdt->cdt_state == CDT_STOPPED)
1470 /* first do sanity checks */
1471 car = mdt_cdt_update_request(cdt, pgs);
1473 CERROR("%s: Cannot find running request for cookie %#llx"
1476 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1478 RETURN(PTR_ERR(car));
1481 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1482 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1483 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1484 hsm_copytool_action2name(car->car_hai->hai_action),
1485 pgs->hpk_flags, pgs->hpk_errval,
1486 PFID(&car->car_hai->hai_fid),
1487 PFID(&car->car_hai->hai_dfid));
1489 /* progress is done on FID or data FID depending of the action and
1490 * of the copy progress */
1491 /* for restore progress is used to send back the data FID to cdt */
1492 if (car->car_hai->hai_action == HSMA_RESTORE &&
1493 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1494 car->car_hai->hai_dfid = pgs->hpk_fid;
1496 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1497 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1498 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1499 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1500 CERROR("%s: Progress on "DFID" for cookie %#llx"
1501 " does not match request FID "DFID" nor data FID "
1504 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1505 PFID(&car->car_hai->hai_fid),
1506 PFID(&car->car_hai->hai_dfid));
1507 GOTO(out, rc = -EINVAL);
1510 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1511 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1512 " is not coherent (err=%d and not completed"
1515 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1516 hsm_copytool_action2name(car->car_hai->hai_action),
1517 pgs->hpk_errval, pgs->hpk_flags);
1518 GOTO(out, rc = -EINVAL);
1521 /* now progress is valid */
1523 /* we use a root like ucred */
1524 hsm_init_ucred(mdt_ucred(mti));
1526 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1527 enum agent_req_status status;
1529 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1531 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1533 update_record ? "Updating" : "Not updating",
1534 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1535 hsm_copytool_action2name(car->car_hai->hai_action),
1536 agent_req_status2name(status));
1538 /* update record first (LU-9075) */
1539 if (update_record) {
1542 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1543 &pgs->hpk_cookie, 1,
1546 CERROR("%s: mdt_agent_record_update() failed,"
1547 " rc=%d, cannot update status to %s"
1548 " for cookie %#llx\n",
1549 mdt_obd_name(mdt), rc1,
1550 agent_req_status2name(status),
1552 rc = (rc != 0 ? rc : rc1);
1555 /* then remove request from memory list (LU-9075) */
1556 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1558 /* ct has completed a request, so a slot is available,
1559 * signal the coordinator to find new work */
1560 mdt_hsm_cdt_event(cdt);
1562 /* if copytool send a progress on a canceled request
1563 * we inform copytool it should stop
1565 if (car->car_canceled == 1)
1571 /* remove ref got from mdt_cdt_update_request() */
1572 mdt_cdt_put_request(car);
1579 * data passed to llog_cat_process() callback
1580 * to cancel requests
1582 struct hsm_cancel_all_data {
1583 struct mdt_device *mdt;
1587 * llog_cat_process() callback, used to:
1588 * - purge all requests
1589 * \param env [IN] environment
1590 * \param llh [IN] llog handle
1591 * \param hdr [IN] llog record
1592 * \param data [IN] cb data = struct hsm_cancel_all_data
1594 * \retval -ve failure
1596 static int mdt_cancel_all_cb(const struct lu_env *env,
1597 struct llog_handle *llh,
1598 struct llog_rec_hdr *hdr, void *data)
1600 struct llog_agent_req_rec *larr;
1601 struct hsm_cancel_all_data *hcad;
1605 larr = (struct llog_agent_req_rec *)hdr;
1607 if (larr->arr_status == ARS_WAITING ||
1608 larr->arr_status == ARS_STARTED) {
1609 larr->arr_status = ARS_CANCELED;
1610 larr->arr_req_change = cfs_time_current_sec();
1611 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1618 * cancel all actions
1619 * \param obd [IN] MDT device
1621 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1624 struct lu_context session;
1625 struct mdt_thread_info *mti;
1626 struct coordinator *cdt = &mdt->mdt_coordinator;
1627 struct cdt_agent_req *car;
1628 struct hsm_action_list *hal = NULL;
1629 struct hsm_action_item *hai;
1630 struct hsm_cancel_all_data hcad;
1631 int hal_sz = 0, hal_len, rc;
1632 enum cdt_states old_state;
1635 rc = lu_env_init(&env, LCT_MD_THREAD);
1639 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1640 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1644 lu_context_enter(&session);
1645 env.le_ses = &session;
1647 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1648 LASSERT(mti != NULL);
1650 mti->mti_env = &env;
1653 hsm_init_ucred(mdt_ucred(mti));
1655 /* disable coordinator */
1656 rc = set_cdt_state(cdt, CDT_DISABLE, &old_state);
1660 /* send cancel to all running requests */
1661 down_read(&cdt->cdt_request_lock);
1662 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1663 mdt_cdt_get_request(car);
1664 /* request is not yet removed from list, it will be done
1665 * when copytool will return progress
1668 if (car->car_hai->hai_action == HSMA_CANCEL) {
1669 mdt_cdt_put_request(car);
1674 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1675 cfs_size_round(car->car_hai->hai_len);
1677 if (hal_len > hal_sz && hal_sz > 0) {
1678 /* not enough room, free old buffer */
1679 OBD_FREE(hal, hal_sz);
1683 /* empty buffer, allocate one */
1686 OBD_ALLOC(hal, hal_sz);
1688 mdt_cdt_put_request(car);
1689 up_read(&cdt->cdt_request_lock);
1690 GOTO(out_cdt_state, rc = -ENOMEM);
1694 hal->hal_version = HAL_VERSION;
1695 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1697 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1698 hal->hal_compound_id = car->car_compound_id;
1699 hal->hal_archive_id = car->car_archive_id;
1700 hal->hal_flags = car->car_flags;
1703 hai = hai_first(hal);
1704 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1705 hai->hai_action = HSMA_CANCEL;
1708 /* it is possible to safely call mdt_hsm_agent_send()
1709 * (ie without a deadlock on cdt_request_lock), because the
1710 * write lock is taken only if we are not in purge mode
1711 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1712 * nor mdt_cdt_remove_request())
1714 /* no conflict with cdt thread because cdt is disable and we
1715 * have the request lock */
1716 mdt_hsm_agent_send(mti, hal, 1);
1718 mdt_cdt_put_request(car);
1720 up_read(&cdt->cdt_request_lock);
1723 OBD_FREE(hal, hal_sz);
1725 /* cancel all on-disk records */
1728 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1729 &hcad, 0, 0, WRITE);
1731 /* Enable coordinator, unless the coordinator was stopping. */
1732 set_cdt_state(cdt, old_state, NULL);
1733 lu_context_exit(&session);
1734 lu_context_fini(&session);
1742 * check if a request is compatible with file status
1743 * \param hai [IN] request description
1744 * \param archive_id [IN] request archive id
1745 * \param rq_flags [IN] request flags
1746 * \param hsm [IN] file HSM metadata
1749 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1750 u32 archive_id, u64 rq_flags,
1751 const struct md_hsm *hsm)
1753 int is_compat = false;
1757 hsm_flags = hsm->mh_flags;
1758 switch (hai->hai_action) {
1760 if (!(hsm_flags & HS_NOARCHIVE) &&
1761 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1764 if (hsm_flags & HS_EXISTS &&
1766 archive_id != hsm->mh_arch_id)
1771 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1772 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1776 if (!(hsm_flags & HS_RELEASED) &&
1777 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1784 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1785 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1786 PFID(&hai->hai_fid),
1787 hsm_copytool_action2name(hai->hai_action), rq_flags,
1788 hai->hai_extent.offset, hai->hai_extent.length,
1790 (is_compat ? "compatible" : "uncompatible"));
1796 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1798 static const struct {
1802 } hsm_policy_names[] = {
1803 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1804 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1809 * convert a policy name to a bit
1810 * \param name [IN] policy name
1812 * \retval policy bit
1814 static __u64 hsm_policy_str2bit(const char *name)
1818 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1819 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1820 strcmp(hsm_policy_names[i].name, name) == 0)
1821 return hsm_policy_names[i].bit;
1826 * convert a policy bit field to a string
1827 * \param mask [IN] policy bit field
1828 * \param hexa [IN] print mask before bit names
1829 * \param buffer [OUT] string
1830 * \param count [IN] size of buffer
1832 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1840 seq_printf(m, "(%#llx) ", mask);
1842 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1845 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1846 if (hsm_policy_names[j].bit == bit)
1850 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1852 seq_printf(m, "%s ", hsm_policy_names[j].name);
1854 /* remove last ' ' */
1859 /* methods to read/write HSM policy flags */
1860 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1862 struct mdt_device *mdt = m->private;
1863 struct coordinator *cdt = &mdt->mdt_coordinator;
1866 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1871 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1872 size_t count, loff_t *off)
1874 struct seq_file *m = file->private_data;
1875 struct mdt_device *mdt = m->private;
1876 struct coordinator *cdt = &mdt->mdt_coordinator;
1877 char *start, *token, sign;
1880 __u64 add_mask, remove_mask, set_mask;
1884 if (count + 1 > PAGE_SIZE)
1887 OBD_ALLOC(buf, count + 1);
1891 if (copy_from_user(buf, buffer, count))
1892 GOTO(out, rc = -EFAULT);
1897 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1900 add_mask = remove_mask = set_mask = 0;
1902 token = strsep(&start, "\n ");
1908 if (sign == '-' || sign == '+')
1911 policy = hsm_policy_str2bit(token);
1913 CWARN("%s: '%s' is unknown, "
1914 "supported policies are:\n", mdt_obd_name(mdt),
1916 hsm_policy_bit2str(m, 0, false);
1917 GOTO(out, rc = -EINVAL);
1921 remove_mask |= policy;
1931 } while (start != NULL);
1933 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1934 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1936 /* if no sign in all string, it is a clear and set
1937 * if some sign found, all unsigned are converted
1939 * P1 P2 = set to P1 and P2
1940 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1942 if (remove_mask == 0 && add_mask == 0) {
1943 cdt->cdt_policy = set_mask;
1945 cdt->cdt_policy |= set_mask | add_mask;
1946 cdt->cdt_policy &= ~remove_mask;
1949 GOTO(out, rc = count);
1952 OBD_FREE(buf, count + 1);
1955 LPROC_SEQ_FOPS(mdt_hsm_policy);
1957 #define GENERATE_PROC_METHOD(VAR) \
1958 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1960 struct mdt_device *mdt = m->private; \
1961 struct coordinator *cdt = &mdt->mdt_coordinator; \
1964 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1968 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1969 size_t count, loff_t *off) \
1972 struct seq_file *m = file->private_data; \
1973 struct mdt_device *mdt = m->private; \
1974 struct coordinator *cdt = &mdt->mdt_coordinator; \
1979 rc = lprocfs_str_to_s64(buffer, count, &val); \
1982 if (val > 0 && val < INT_MAX) { \
1989 GENERATE_PROC_METHOD(cdt_loop_period)
1990 GENERATE_PROC_METHOD(cdt_grace_delay)
1991 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1992 GENERATE_PROC_METHOD(cdt_max_requests)
1993 GENERATE_PROC_METHOD(cdt_default_archive_id)
1996 * procfs write method for MDT/hsm_control
1997 * proc entry is in mdt directory so data is mdt obd_device pointer
1999 #define CDT_ENABLE_CMD "enabled"
2000 #define CDT_STOP_CMD "shutdown"
2001 #define CDT_DISABLE_CMD "disabled"
2002 #define CDT_PURGE_CMD "purge"
2003 #define CDT_HELP_CMD "help"
2004 #define CDT_MAX_CMD_LEN 10
2007 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
2008 size_t count, loff_t *off)
2010 struct seq_file *m = file->private_data;
2011 struct obd_device *obd = m->private;
2012 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2013 struct coordinator *cdt = &(mdt->mdt_coordinator);
2015 char kernbuf[CDT_MAX_CMD_LEN];
2018 if (count == 0 || count >= sizeof(kernbuf))
2021 if (copy_from_user(kernbuf, buffer, count))
2025 if (kernbuf[count - 1] == '\n')
2026 kernbuf[count - 1] = 0;
2029 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2030 if (cdt->cdt_state == CDT_DISABLE) {
2031 rc = set_cdt_state(cdt, CDT_RUNNING, NULL);
2032 mdt_hsm_cdt_event(cdt);
2033 wake_up(&cdt->cdt_waitq);
2035 rc = mdt_hsm_cdt_start(mdt);
2037 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2038 if ((cdt->cdt_state == CDT_STOPPING) ||
2039 (cdt->cdt_state == CDT_STOPPED)) {
2040 CERROR("%s: Coordinator already stopped\n",
2044 rc = mdt_hsm_cdt_stop(mdt);
2046 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2047 if ((cdt->cdt_state == CDT_STOPPING) ||
2048 (cdt->cdt_state == CDT_STOPPED)) {
2049 CERROR("%s: Coordinator is stopped\n",
2053 rc = set_cdt_state(cdt, CDT_DISABLE, NULL);
2055 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2056 rc = hsm_cancel_all_actions(mdt);
2057 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2065 CERROR("%s: Valid coordinator control commands are: "
2066 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2067 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2068 CDT_PURGE_CMD, CDT_HELP_CMD);
2076 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2078 struct obd_device *obd = m->private;
2079 struct coordinator *cdt;
2082 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2084 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2090 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2096 for (i = 0; i < 8 * sizeof(mask); i++) {
2097 if (mask & (1UL << i)) {
2098 seq_printf(m, "%s%s", first ? "" : " ",
2099 hsm_copytool_action2name(i));
2109 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2111 struct mdt_device *mdt = m->private;
2112 struct coordinator *cdt = &mdt->mdt_coordinator;
2114 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2118 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2120 struct mdt_device *mdt = m->private;
2121 struct coordinator *cdt = &mdt->mdt_coordinator;
2123 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2127 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2129 struct mdt_device *mdt = m->private;
2130 struct coordinator *cdt = &mdt->mdt_coordinator;
2132 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2135 static inline enum hsm_copytool_action
2136 hsm_copytool_name2action(const char *name)
2138 if (strcasecmp(name, "NOOP") == 0)
2140 else if (strcasecmp(name, "ARCHIVE") == 0)
2141 return HSMA_ARCHIVE;
2142 else if (strcasecmp(name, "RESTORE") == 0)
2143 return HSMA_RESTORE;
2144 else if (strcasecmp(name, "REMOVE") == 0)
2146 else if (strcasecmp(name, "CANCEL") == 0)
2153 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2154 size_t user_count, __u64 *mask)
2156 char *buf, *pos, *name;
2162 if (!(user_count < 4096))
2165 buf_size = user_count + 1;
2167 OBD_ALLOC(buf, buf_size);
2171 if (copy_from_user(buf, user_buf, buf_size - 1))
2172 GOTO(out, rc = -EFAULT);
2174 buf[buf_size - 1] = '\0';
2177 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2183 action = hsm_copytool_name2action(name);
2185 GOTO(out, rc = -EINVAL);
2187 new_mask |= (1UL << action);
2193 OBD_FREE(buf, buf_size);
2199 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2200 size_t count, loff_t *off)
2202 struct seq_file *m = file->private_data;
2203 struct mdt_device *mdt = m->private;
2204 struct coordinator *cdt = &mdt->mdt_coordinator;
2206 return mdt_write_hsm_request_mask(file, buf, count,
2207 &cdt->cdt_user_request_mask);
2211 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2212 size_t count, loff_t *off)
2214 struct seq_file *m = file->private_data;
2215 struct mdt_device *mdt = m->private;
2216 struct coordinator *cdt = &mdt->mdt_coordinator;
2218 return mdt_write_hsm_request_mask(file, buf, count,
2219 &cdt->cdt_group_request_mask);
2223 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2224 size_t count, loff_t *off)
2226 struct seq_file *m = file->private_data;
2227 struct mdt_device *mdt = m->private;
2228 struct coordinator *cdt = &mdt->mdt_coordinator;
2230 return mdt_write_hsm_request_mask(file, buf, count,
2231 &cdt->cdt_other_request_mask);
2234 static int mdt_hsm_cdt_raolu_seq_show(struct seq_file *m, void *data)
2236 struct mdt_device *mdt = m->private;
2237 struct coordinator *cdt = &mdt->mdt_coordinator;
2240 seq_printf(m, "%d\n", (int)cdt->cdt_remove_archive_on_last_unlink);
2245 mdt_hsm_cdt_raolu_seq_write(struct file *file, const char __user *buffer,
2246 size_t count, loff_t *off)
2249 struct seq_file *m = file->private_data;
2250 struct mdt_device *mdt = m->private;
2251 struct coordinator *cdt = &mdt->mdt_coordinator;
2256 rc = lprocfs_str_to_s64(buffer, count, &val);
2260 cdt->cdt_remove_archive_on_last_unlink = val;
2264 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2265 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2266 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2267 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2268 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2269 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2270 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2271 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2272 LPROC_SEQ_FOPS(mdt_hsm_cdt_raolu);
2274 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2276 .fops = &mdt_hsm_agent_fops },
2277 { .name = "actions",
2278 .fops = &mdt_hsm_actions_fops,
2279 .proc_mode = 0444 },
2280 { .name = "default_archive_id",
2281 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2282 { .name = "grace_delay",
2283 .fops = &mdt_hsm_cdt_grace_delay_fops },
2284 { .name = "loop_period",
2285 .fops = &mdt_hsm_cdt_loop_period_fops },
2286 { .name = "max_requests",
2287 .fops = &mdt_hsm_cdt_max_requests_fops },
2289 .fops = &mdt_hsm_policy_fops },
2290 { .name = "active_request_timeout",
2291 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2292 { .name = "active_requests",
2293 .fops = &mdt_hsm_active_requests_fops },
2294 { .name = "user_request_mask",
2295 .fops = &mdt_hsm_user_request_mask_fops, },
2296 { .name = "group_request_mask",
2297 .fops = &mdt_hsm_group_request_mask_fops, },
2298 { .name = "other_request_mask",
2299 .fops = &mdt_hsm_other_request_mask_fops, },
2300 { .name = "remove_archive_on_last_unlink",
2301 .fops = &mdt_hsm_cdt_raolu_fops, },