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, 2017, 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"
105 " archive_id %d flags %#llx\n",
106 prefix, hal->hal_version, hal->hal_count,
107 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
114 " action=%s extent=%#llx-%#llx gid=%#llx"
115 " datalen=%d data=[%s]\n",
117 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
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 *hsd_mti;
140 char hsd_fsname[MTI_NAME_MAXLEN + 1];
141 /* are we scanning the logs for housekeeping, or just looking
144 bool hsd_housekeeping;
145 bool hsd_one_restore;
146 int hsd_action_count;
147 int hsd_request_len; /* array alloc len */
148 int hsd_request_count; /* array used count */
149 struct hsm_scan_request *hsd_request;
152 static int mdt_cdt_waiting_cb(const struct lu_env *env,
153 struct mdt_device *mdt,
154 struct llog_handle *llh,
155 struct llog_agent_req_rec *larr,
156 struct hsm_scan_data *hsd)
158 struct coordinator *cdt = &mdt->mdt_coordinator;
159 struct hsm_scan_request *request;
160 struct hsm_action_item *hai;
165 /* Are agents full? */
166 if (atomic_read(&cdt->cdt_request_count) >= cdt->cdt_max_requests)
167 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
169 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
170 cdt->cdt_max_requests) {
171 /* We cannot send any more request
173 * *** SPECIAL CASE ***
175 * Restore requests are too important not to schedule at least
176 * one, everytime we can.
178 if (larr->arr_hai.hai_action != HSMA_RESTORE ||
179 hsd->hsd_one_restore)
180 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
183 hai_size = cfs_size_round(larr->arr_hai.hai_len);
184 archive_id = larr->arr_archive_id;
186 /* Can we add this action to one of the existing HALs in hsd. */
188 for (i = 0; i < hsd->hsd_request_count; i++) {
189 if (hsd->hsd_request[i].hal->hal_archive_id == archive_id &&
190 hsd->hsd_request[i].hal_used_sz + hai_size <=
192 request = &hsd->hsd_request[i];
197 /* Are we trying to force-schedule a request? */
198 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
199 cdt->cdt_max_requests) {
200 /* Is there really no compatible hsm_scan_request? */
202 for (i -= 1; i >= 0; i--) {
203 if (hsd->hsd_request[i].hal->hal_archive_id ==
205 request = &hsd->hsd_request[i];
211 /* Make room for the hai */
213 /* Discard the last hai until there is enough space */
215 request->hal->hal_count--;
217 hai = hai_first(request->hal);
218 for (i = 0; i < request->hal->hal_count; i++)
220 request->hal_used_sz -=
221 cfs_size_round(hai->hai_len);
222 hsd->hsd_action_count--;
223 } while (request->hal_used_sz + hai_size >
225 } else if (hsd->hsd_housekeeping) {
226 struct hsm_scan_request *tmp;
228 /* Discard the (whole) last hal */
229 hsd->hsd_request_count--;
230 LASSERT(hsd->hsd_request_count >= 0);
231 tmp = &hsd->hsd_request[hsd->hsd_request_count];
232 hsd->hsd_action_count -= tmp->hal->hal_count;
233 LASSERT(hsd->hsd_action_count >= 0);
234 OBD_FREE(tmp->hal, tmp->hal_sz);
236 /* Bailing out, this code path is too hot */
237 RETURN(LLOG_PROC_BREAK);
243 struct hsm_action_list *hal;
245 LASSERT(hsd->hsd_request_count < hsd->hsd_request_len);
246 request = &hsd->hsd_request[hsd->hsd_request_count];
248 /* allocates hai vector size just needs to be large
250 request->hal_sz = sizeof(*request->hal) +
251 cfs_size_round(MTI_NAME_MAXLEN + 1) + 2 * hai_size;
252 OBD_ALLOC_LARGE(hal, request->hal_sz);
256 hal->hal_version = HAL_VERSION;
257 strlcpy(hal->hal_fsname, hsd->hsd_fsname, MTI_NAME_MAXLEN + 1);
258 hal->hal_archive_id = larr->arr_archive_id;
259 hal->hal_flags = larr->arr_flags;
261 request->hal_used_sz = hal_size(hal);
263 hsd->hsd_request_count++;
264 } else if (request->hal_sz < request->hal_used_sz + hai_size) {
265 /* Not enough room, need an extension */
269 sz = min_t(int, 2 * request->hal_sz, LDLM_MAXREQSIZE);
270 LASSERT(request->hal_used_sz + hai_size < sz);
272 OBD_ALLOC_LARGE(hal_buffer, sz);
276 memcpy(hal_buffer, request->hal, request->hal_used_sz);
277 OBD_FREE_LARGE(request->hal, request->hal_sz);
278 request->hal = hal_buffer;
279 request->hal_sz = sz;
282 hai = hai_first(request->hal);
283 for (i = 0; i < request->hal->hal_count; i++)
286 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
288 request->hal_used_sz += hai_size;
289 request->hal->hal_count++;
291 hsd->hsd_action_count++;
293 switch (hai->hai_action) {
297 hsd->hsd_one_restore = true;
298 /* Intentional fallthrough */
300 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
301 llh->lgh_hdr->llh_cat_idx,
302 larr->arr_hdr.lrh_index);
308 static int mdt_cdt_started_cb(const struct lu_env *env,
309 struct mdt_device *mdt,
310 struct llog_handle *llh,
311 struct llog_agent_req_rec *larr,
312 struct hsm_scan_data *hsd)
314 struct coordinator *cdt = &mdt->mdt_coordinator;
315 struct hsm_action_item *hai = &larr->arr_hai;
316 struct cdt_agent_req *car;
317 time64_t now = ktime_get_real_seconds();
319 enum changelog_rec_flags clf_flags;
322 if (!hsd->hsd_housekeeping)
325 /* we search for a running request
326 * error may happen if coordinator crashes or stopped
327 * with running request
329 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
331 last = larr->arr_req_change;
333 last = car->car_req_update;
336 /* test if request too long, if yes cancel it
337 * the same way the copy tool acknowledge a cancel request */
338 if (now <= last + cdt->cdt_active_req_timeout)
339 GOTO(out_car, rc = 0);
341 dump_llog_agent_req_rec("request timed out, start cleaning", larr);
344 car->car_req_update = now;
345 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0, &car->car_uuid);
346 /* Remove car from memory list (LU-9075) */
347 mdt_cdt_remove_request(cdt, hai->hai_cookie);
350 /* Emit a changelog record for the failed action.*/
352 hsm_set_cl_error(&clf_flags, ECANCELED);
354 switch (hai->hai_action) {
356 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
359 hsm_set_cl_event(&clf_flags, HE_RESTORE);
362 hsm_set_cl_event(&clf_flags, HE_REMOVE);
365 hsm_set_cl_event(&clf_flags, HE_CANCEL);
368 /* Unknown record type, skip changelog. */
374 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
377 if (hai->hai_action == HSMA_RESTORE)
378 cdt_restore_handle_del(hsd->hsd_mti, cdt, &hai->hai_fid);
380 larr->arr_status = ARS_CANCELED;
381 larr->arr_req_change = now;
382 rc = llog_write(hsd->hsd_mti->mti_env, llh, &larr->arr_hdr,
383 larr->arr_hdr.lrh_index);
385 CERROR("%s: cannot update agent log: rc = %d\n",
386 mdt_obd_name(mdt), rc);
387 rc = LLOG_DEL_RECORD;
390 /* ct has completed a request, so a slot is available,
391 * signal the coordinator to find new work */
392 mdt_hsm_cdt_event(cdt);
395 mdt_cdt_put_request(car);
401 * llog_cat_process() callback, used to:
402 * - find waiting request and start action
403 * - purge canceled and done requests
404 * \param env [IN] environment
405 * \param llh [IN] llog handle
406 * \param hdr [IN] llog record
407 * \param data [IN/OUT] cb data = struct hsm_scan_data
409 * \retval -ve failure
411 static int mdt_coordinator_cb(const struct lu_env *env,
412 struct llog_handle *llh,
413 struct llog_rec_hdr *hdr,
416 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
417 struct hsm_scan_data *hsd = data;
418 struct mdt_device *mdt = hsd->hsd_mti->mti_mdt;
419 struct coordinator *cdt = &mdt->mdt_coordinator;
422 larr = (struct llog_agent_req_rec *)hdr;
423 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
424 switch (larr->arr_status) {
426 RETURN(mdt_cdt_waiting_cb(env, mdt, llh, larr, hsd));
428 RETURN(mdt_cdt_started_cb(env, mdt, llh, larr, hsd));
430 if (!hsd->hsd_housekeeping)
433 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
434 ktime_get_real_seconds()) {
435 cdt_agent_record_hash_del(cdt,
436 larr->arr_hai.hai_cookie);
437 RETURN(LLOG_DEL_RECORD);
444 /* Release the ressource used by the coordinator. Called when the
445 * coordinator is stopping. */
446 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
448 struct coordinator *cdt = &mdt->mdt_coordinator;
449 struct cdt_agent_req *car, *tmp1;
450 struct hsm_agent *ha, *tmp2;
451 struct cdt_restore_handle *crh, *tmp3;
452 struct mdt_thread_info *cdt_mti;
455 down_write(&cdt->cdt_request_lock);
456 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
458 cfs_hash_del(cdt->cdt_request_cookie_hash,
459 &car->car_hai->hai_cookie,
460 &car->car_cookie_hash);
461 list_del(&car->car_request_list);
462 mdt_cdt_put_request(car);
464 up_write(&cdt->cdt_request_lock);
466 down_write(&cdt->cdt_agent_lock);
467 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
468 list_del(&ha->ha_list);
469 if (ha->ha_archive_cnt != 0)
470 OBD_FREE(ha->ha_archive_id, ha->ha_archive_cnt *
471 sizeof(*ha->ha_archive_id));
474 up_write(&cdt->cdt_agent_lock);
476 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
477 mutex_lock(&cdt->cdt_restore_lock);
478 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_handle_list,
480 list_del(&crh->crh_list);
481 /* give back layout lock */
482 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
483 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
485 mutex_unlock(&cdt->cdt_restore_lock);
489 * Coordinator state transition table, indexed on enum cdt_states, taking
490 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
491 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
493 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
494 /* from -> to: stopped init running disable stopping */
495 /* stopped */ { true, true, false, false, false },
496 /* init */ { true, false, true, false, false },
497 /* running */ { false, false, true, true, true },
498 /* disable */ { false, false, true, true, true },
499 /* stopping */ { true, false, false, false, false }
503 * Change coordinator thread state
504 * Some combinations are not valid, so catch them here.
506 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
507 * the transition was not possible.
509 static int set_cdt_state_locked(struct coordinator *cdt,
510 enum cdt_states new_state)
513 enum cdt_states state;
515 state = cdt->cdt_state;
517 if (cdt_transition[state][new_state]) {
518 cdt->cdt_state = new_state;
522 "unexpected coordinator transition, from=%s, to=%s\n",
523 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
530 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state)
534 mutex_lock(&cdt->cdt_state_lock);
535 rc = set_cdt_state_locked(cdt, new_state);
536 mutex_unlock(&cdt->cdt_state_lock);
545 * \param data [IN] obd device
547 * \retval -ve failure
549 static int mdt_coordinator(void *data)
551 struct mdt_thread_info *mti = data;
552 struct mdt_device *mdt = mti->mti_mdt;
553 struct coordinator *cdt = &mdt->mdt_coordinator;
554 struct hsm_scan_data hsd = { NULL };
555 time64_t last_housekeeping = 0;
556 size_t request_sz = 0;
560 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
561 mdt_obd_name(mdt), current_pid());
564 obd_uuid2fsname(hsd.hsd_fsname, mdt_obd_name(mdt),
565 sizeof(hsd.hsd_fsname));
567 set_cdt_state(cdt, CDT_RUNNING);
569 /* Inform mdt_hsm_cdt_start(). */
570 wake_up_all(&cdt->cdt_waitq);
577 struct hsm_record_update *updates;
579 /* Limit execution of the expensive requests traversal
580 * to at most one second. This prevents repeatedly
581 * locking/unlocking the catalog for each request
582 * and preventing other HSM operations from happening
584 wait_event_interruptible_timeout(cdt->cdt_waitq,
585 kthread_should_stop() ||
586 cdt->cdt_wakeup_coordinator,
587 cfs_time_seconds(1));
589 cdt->cdt_wakeup_coordinator = false;
590 CDEBUG(D_HSM, "coordinator resumes\n");
592 if (kthread_should_stop()) {
593 CDEBUG(D_HSM, "Coordinator stops\n");
598 /* if coordinator is suspended continue to wait */
599 if (cdt->cdt_state == CDT_DISABLE) {
600 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
604 /* If no event, and no housekeeping to do, continue to
606 if (last_housekeeping + cdt->cdt_loop_period <=
607 ktime_get_real_seconds()) {
608 last_housekeeping = ktime_get_real_seconds();
609 hsd.hsd_housekeeping = true;
610 } else if (cdt->cdt_event) {
611 hsd.hsd_housekeeping = false;
616 cdt->cdt_event = false;
618 CDEBUG(D_HSM, "coordinator starts reading llog\n");
620 if (hsd.hsd_request_len != cdt->cdt_max_requests) {
621 /* cdt_max_requests has changed,
622 * we need to allocate a new buffer
624 struct hsm_scan_request *tmp = NULL;
625 int max_requests = cdt->cdt_max_requests;
626 OBD_ALLOC_LARGE(tmp, max_requests *
627 sizeof(struct hsm_scan_request));
629 CERROR("Failed to resize request buffer, "
630 "keeping it at %d\n",
631 hsd.hsd_request_len);
633 if (hsd.hsd_request != NULL)
634 OBD_FREE_LARGE(hsd.hsd_request,
637 hsd.hsd_request_len = max_requests;
638 request_sz = hsd.hsd_request_len *
639 sizeof(struct hsm_scan_request);
640 hsd.hsd_request = tmp;
644 hsd.hsd_action_count = 0;
645 hsd.hsd_request_count = 0;
646 hsd.hsd_one_restore = false;
648 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
653 CDEBUG(D_HSM, "found %d requests to send\n",
654 hsd.hsd_request_count);
656 if (list_empty(&cdt->cdt_agents)) {
657 CDEBUG(D_HSM, "no agent available, "
658 "coordinator sleeps\n");
662 /* Compute how many HAI we have in all the requests */
664 for (i = 0; i < hsd.hsd_request_count; i++) {
665 const struct hsm_scan_request *request =
668 updates_cnt += request->hal->hal_count;
671 /* Allocate a temporary array to store the cookies to
672 * update, and their status. */
673 updates_sz = updates_cnt * sizeof(*updates);
674 OBD_ALLOC_LARGE(updates, updates_sz);
675 if (updates == NULL) {
676 CERROR("%s: Cannot allocate memory (%d bytes) "
677 "for %d updates. Too many HSM requests?\n",
678 mdt_obd_name(mdt), updates_sz, updates_cnt);
682 /* here hsd contains a list of requests to be started */
683 for (i = 0; i < hsd.hsd_request_count; i++) {
684 struct hsm_scan_request *request = &hsd.hsd_request[i];
685 struct hsm_action_list *hal = request->hal;
686 struct hsm_action_item *hai;
689 /* still room for work ? */
690 if (atomic_read(&cdt->cdt_request_count) >=
691 cdt->cdt_max_requests)
694 rc = mdt_hsm_agent_send(mti, hal, 0);
695 /* if failure, we suppose it is temporary
696 * if the copy tool failed to do the request
697 * it has to use hsm_progress
700 /* set up cookie vector to set records status
701 * after copy tools start or failed
703 hai = hai_first(hal);
704 for (j = 0; j < hal->hal_count; j++) {
705 updates[update_idx].cookie = hai->hai_cookie;
706 updates[update_idx].status =
707 (rc ? ARS_WAITING : ARS_STARTED);
714 rc = mdt_agent_record_update(mti->mti_env, mdt,
715 updates, update_idx);
717 CERROR("%s: mdt_agent_record_update() failed, "
718 "rc=%d, cannot update records "
720 mdt_obd_name(mdt), rc, update_idx);
723 OBD_FREE_LARGE(updates, updates_sz);
726 /* free hal allocated by callback */
727 for (i = 0; i < hsd.hsd_request_count; i++) {
728 struct hsm_scan_request *request = &hsd.hsd_request[i];
730 OBD_FREE_LARGE(request->hal, request->hal_sz);
734 if (hsd.hsd_request != NULL)
735 OBD_FREE_LARGE(hsd.hsd_request, request_sz);
737 mdt_hsm_cdt_cleanup(mdt);
740 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
741 mdt_obd_name(mdt), current_pid(), rc);
743 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
745 mdt_obd_name(mdt), current_pid());
750 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
751 const struct lu_fid *fid,
752 const struct hsm_extent *he)
754 struct cdt_restore_handle *crh;
755 struct mdt_object *obj;
759 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
764 /* in V1 all file is restored
765 * crh->extent.start = he->offset;
766 * crh->extent.end = he->offset + he->length;
768 crh->crh_extent.start = 0;
769 crh->crh_extent.end = he->length;
770 /* get the layout lock */
771 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
772 obj = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
773 MDS_INODELOCK_LAYOUT);
775 GOTO(out_crh, rc = PTR_ERR(obj));
777 /* We do not keep a reference on the object during the restore
778 * which can be very long. */
779 mdt_object_put(mti->mti_env, obj);
781 mutex_lock(&cdt->cdt_restore_lock);
782 if (unlikely(cdt->cdt_state == CDT_STOPPED ||
783 cdt->cdt_state == CDT_STOPPING)) {
784 mutex_unlock(&cdt->cdt_restore_lock);
785 GOTO(out_lh, rc = -EAGAIN);
788 list_add_tail(&crh->crh_list, &cdt->cdt_restore_handle_list);
789 mutex_unlock(&cdt->cdt_restore_lock);
793 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
795 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
801 * lookup a restore handle by FID
802 * caller needs to hold cdt_restore_lock
803 * \param cdt [IN] coordinator
804 * \param fid [IN] FID
805 * \retval cdt_restore_handle found
806 * \retval NULL not found
808 struct cdt_restore_handle *cdt_restore_handle_find(struct coordinator *cdt,
809 const struct lu_fid *fid)
811 struct cdt_restore_handle *crh;
814 list_for_each_entry(crh, &cdt->cdt_restore_handle_list, crh_list) {
815 if (lu_fid_eq(&crh->crh_fid, fid))
822 void cdt_restore_handle_del(struct mdt_thread_info *mti,
823 struct coordinator *cdt, const struct lu_fid *fid)
825 struct cdt_restore_handle *crh;
827 /* give back layout lock */
828 mutex_lock(&cdt->cdt_restore_lock);
829 crh = cdt_restore_handle_find(cdt, fid);
831 list_del(&crh->crh_list);
832 mutex_unlock(&cdt->cdt_restore_lock);
837 /* XXX We pass a NULL object since the restore handle does not
838 * keep a reference on the object being restored. */
839 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
840 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
844 * data passed to llog_cat_process() callback
845 * to scan requests and take actions
847 struct hsm_restore_data {
848 struct mdt_thread_info *hrd_mti;
852 * llog_cat_process() callback, used to:
853 * - find restore request and allocate the restore handle
854 * \param env [IN] environment
855 * \param llh [IN] llog handle
856 * \param hdr [IN] llog record
857 * \param data [IN/OUT] cb data = struct hsm_restore_data
859 * \retval -ve failure
861 static int hsm_restore_cb(const struct lu_env *env,
862 struct llog_handle *llh,
863 struct llog_rec_hdr *hdr, void *data)
865 struct llog_agent_req_rec *larr;
866 struct hsm_restore_data *hrd;
867 struct hsm_action_item *hai;
868 struct mdt_thread_info *mti;
869 struct coordinator *cdt;
875 cdt = &mti->mti_mdt->mdt_coordinator;
877 larr = (struct llog_agent_req_rec *)hdr;
878 hai = &larr->arr_hai;
879 if (hai->hai_cookie > cdt->cdt_last_cookie)
880 /* update the cookie to avoid collision */
881 cdt->cdt_last_cookie = hai->hai_cookie + 1;
883 if (hai->hai_action != HSMA_RESTORE ||
884 agent_req_in_final_state(larr->arr_status))
887 /* restore request not in a final state */
889 /* force replay of restore requests left in started state from previous
890 * CDT context, to be canceled later if finally found to be incompatible
891 * when being re-started */
892 if (larr->arr_status == ARS_STARTED) {
893 larr->arr_status = ARS_WAITING;
894 larr->arr_req_change = ktime_get_real_seconds();
895 rc = llog_write(env, llh, hdr, hdr->lrh_index);
900 rc = cdt_restore_handle_add(mti, cdt, &hai->hai_fid, &hai->hai_extent);
906 * restore coordinator state at startup
907 * the goal is to take a layout lock for each registered restore request
908 * \param mti [IN] context
910 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
912 struct hsm_restore_data hrd;
918 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
924 int hsm_init_ucred(struct lu_ucred *uc)
928 uc->uc_valid = UCRED_OLD;
937 uc->uc_suppgids[0] = -1;
938 uc->uc_suppgids[1] = -1;
939 uc->uc_cap = CFS_CAP_FS_MASK;
942 uc->uc_identity = NULL;
943 /* always record internal HSM activity if also enabled globally */
944 uc->uc_enable_audit = 1;
950 * initialize coordinator struct
951 * \param mdt [IN] device
953 * \retval -ve failure
955 int mdt_hsm_cdt_init(struct mdt_device *mdt)
957 struct coordinator *cdt = &mdt->mdt_coordinator;
958 struct mdt_thread_info *cdt_mti = NULL;
962 init_waitqueue_head(&cdt->cdt_waitq);
963 init_rwsem(&cdt->cdt_llog_lock);
964 init_rwsem(&cdt->cdt_agent_lock);
965 init_rwsem(&cdt->cdt_request_lock);
966 mutex_init(&cdt->cdt_restore_lock);
967 mutex_init(&cdt->cdt_state_lock);
968 set_cdt_state(cdt, CDT_STOPPED);
970 INIT_LIST_HEAD(&cdt->cdt_request_list);
971 INIT_LIST_HEAD(&cdt->cdt_agents);
972 INIT_LIST_HEAD(&cdt->cdt_restore_handle_list);
974 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
981 &cdt_request_cookie_hash_ops,
983 if (cdt->cdt_request_cookie_hash == NULL)
986 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
993 &cdt_agent_record_hash_ops,
995 if (cdt->cdt_agent_record_hash == NULL)
996 GOTO(out_request_cookie_hash, rc = -ENOMEM);
998 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
1000 GOTO(out_agent_record_hash, rc);
1002 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1003 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
1007 lu_context_enter(&cdt->cdt_session);
1008 cdt->cdt_env.le_ses = &cdt->cdt_session;
1010 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1011 LASSERT(cdt_mti != NULL);
1013 cdt_mti->mti_env = &cdt->cdt_env;
1014 cdt_mti->mti_mdt = mdt;
1016 hsm_init_ucred(mdt_ucred(cdt_mti));
1018 /* default values for sysfs tunnables
1019 * can be override by MGS conf */
1020 cdt->cdt_default_archive_id = 1;
1021 cdt->cdt_grace_delay = 60;
1022 cdt->cdt_loop_period = 10;
1023 cdt->cdt_max_requests = 3;
1024 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1025 cdt->cdt_active_req_timeout = 3600;
1027 /* by default do not remove archives on last unlink */
1028 cdt->cdt_remove_archive_on_last_unlink = false;
1033 lu_env_fini(&cdt->cdt_env);
1034 out_agent_record_hash:
1035 cfs_hash_putref(cdt->cdt_agent_record_hash);
1036 cdt->cdt_agent_record_hash = NULL;
1037 out_request_cookie_hash:
1038 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1039 cdt->cdt_request_cookie_hash = NULL;
1045 * free a coordinator thread
1046 * \param mdt [IN] device
1048 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
1050 struct coordinator *cdt = &mdt->mdt_coordinator;
1053 lu_context_exit(cdt->cdt_env.le_ses);
1054 lu_context_fini(cdt->cdt_env.le_ses);
1056 lu_env_fini(&cdt->cdt_env);
1058 cfs_hash_putref(cdt->cdt_agent_record_hash);
1059 cdt->cdt_agent_record_hash = NULL;
1061 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1062 cdt->cdt_request_cookie_hash = NULL;
1068 * start a coordinator thread
1069 * \param mdt [IN] device
1071 * \retval -ve failure
1073 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1075 struct coordinator *cdt = &mdt->mdt_coordinator;
1076 struct mdt_thread_info *cdt_mti;
1079 struct task_struct *task;
1082 /* functions defined but not yet used
1083 * this avoid compilation warning
1085 ptr = dump_requests;
1087 rc = set_cdt_state(cdt, CDT_INIT);
1089 CERROR("%s: Coordinator already started or stopping\n",
1094 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
1095 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1097 /* just need to be larger than previous one */
1098 /* cdt_last_cookie is protected by cdt_llog_lock */
1099 cdt->cdt_last_cookie = ktime_get_real_seconds();
1100 atomic_set(&cdt->cdt_request_count, 0);
1101 atomic_set(&cdt->cdt_archive_count, 0);
1102 atomic_set(&cdt->cdt_restore_count, 0);
1103 atomic_set(&cdt->cdt_remove_count, 0);
1104 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1105 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1106 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1108 /* to avoid deadlock when start is made through sysfs
1109 * sysfs entries are created by the coordinator thread
1111 /* set up list of started restore requests */
1112 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1113 rc = mdt_hsm_pending_restore(cdt_mti);
1115 CERROR("%s: cannot take the layout locks needed"
1116 " for registered restore: %d\n",
1117 mdt_obd_name(mdt), rc);
1119 if (mdt->mdt_bottom->dd_rdonly)
1122 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
1125 set_cdt_state(cdt, CDT_STOPPED);
1126 CERROR("%s: error starting coordinator thread: %d\n",
1127 mdt_obd_name(mdt), rc);
1129 cdt->cdt_task = task;
1130 wait_event(cdt->cdt_waitq,
1131 cdt->cdt_state != CDT_INIT);
1132 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1141 * stop a coordinator thread
1142 * \param mdt [IN] device
1144 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1146 struct coordinator *cdt = &mdt->mdt_coordinator;
1150 /* stop coordinator thread */
1151 rc = set_cdt_state(cdt, CDT_STOPPING);
1153 kthread_stop(cdt->cdt_task);
1154 cdt->cdt_task = NULL;
1155 set_cdt_state(cdt, CDT_STOPPED);
1161 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1162 const struct lu_fid *fid,
1165 struct mdt_object *obj;
1169 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1171 GOTO(out, rc = PTR_ERR(obj));
1173 if (mh.mh_flags & HS_EXISTS &&
1174 mh.mh_arch_id == archive_id)
1175 GOTO(out_obj, rc = 0);
1177 mh.mh_flags |= HS_EXISTS;
1178 mh.mh_arch_id = archive_id;
1179 rc = mdt_hsm_attr_set(mti, obj, &mh);
1182 mdt_object_put(mti->mti_env, obj);
1188 * register all requests from an hal in the memory list
1189 * \param mti [IN] context
1190 * \param hal [IN] request
1191 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1192 * which is running the CT
1194 * \retval -ve failure
1196 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1197 struct hsm_action_list *hal, struct obd_uuid *uuid)
1199 struct mdt_device *mdt = mti->mti_mdt;
1200 struct coordinator *cdt = &mdt->mdt_coordinator;
1201 struct hsm_action_item *hai;
1205 /* register request in memory list */
1206 hai = hai_first(hal);
1207 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1208 struct cdt_agent_req *car;
1210 /* in case of a cancel request, we first mark the ondisk
1211 * record of the request we want to stop as canceled
1212 * this does not change the cancel record
1213 * it will be done when updating the request status
1215 if (hai->hai_action == HSMA_CANCEL) {
1216 struct hsm_record_update update = {
1217 .cookie = hai->hai_cookie,
1218 .status = ARS_CANCELED,
1221 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1224 CERROR("%s: mdt_agent_record_update() failed, "
1225 "rc=%d, cannot update status to %s "
1226 "for cookie %#llx\n",
1227 mdt_obd_name(mdt), rc,
1228 agent_req_status2name(ARS_CANCELED),
1233 /* find the running request to set it canceled */
1234 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1236 car->car_canceled = 1;
1237 /* uuid has to be changed to the one running the
1238 * request to cancel */
1239 *uuid = car->car_uuid;
1240 mdt_cdt_put_request(car);
1242 /* no need to memorize cancel request
1243 * this also avoid a deadlock when we receive
1244 * a purge all requests command
1249 if (hai->hai_action == HSMA_ARCHIVE) {
1250 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1251 hal->hal_archive_id);
1258 car = mdt_cdt_alloc_request(hal->hal_archive_id, hal->hal_flags,
1261 GOTO(out, rc = PTR_ERR(car));
1263 rc = mdt_cdt_add_request(cdt, car);
1265 mdt_cdt_free_request(car);
1272 * swap layouts between 2 fids
1273 * \param mti [IN] context
1276 * \param mh_common [IN] MD HSM
1278 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1279 struct mdt_object *obj, const struct lu_fid *dfid,
1280 struct md_hsm *mh_common)
1282 struct mdt_object *dobj;
1283 struct mdt_lock_handle *dlh;
1287 if (!mdt_object_exists(obj))
1288 GOTO(out, rc = -ENOENT);
1290 /* we already have layout lock on obj so take only
1292 dlh = &mti->mti_lh[MDT_LH_OLD];
1293 mdt_lock_reg_init(dlh, LCK_EX);
1294 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1296 GOTO(out, rc = PTR_ERR(dobj));
1298 /* if copy tool closes the volatile before sending the final
1299 * progress through llapi_hsm_copy_end(), all the objects
1300 * are removed and mdd_swap_layout LBUG */
1301 if (!mdt_object_exists(dobj)) {
1302 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1303 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1304 GOTO(out_dobj, rc = -ENOENT);
1306 /* Since we only handle restores here, unconditionally use
1307 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1308 * be preserved in case of failure during swap_layout and not
1309 * leave a file in an intermediate but incoherent state.
1310 * But need to setup HSM xattr of data FID before, reuse
1311 * mti and mh presets for FID in hsm_cdt_request_completed(),
1312 * only need to clear RELEASED and DIRTY.
1314 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1315 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1317 rc = mo_swap_layouts(mti->mti_env,
1318 mdt_object_child(obj),
1319 mdt_object_child(dobj),
1320 SWAP_LAYOUTS_MDS_HSM);
1322 rc = mdt_lsom_downgrade(mti, obj);
1325 "%s: File fid="DFID" SOM "
1326 "downgrade failed, rc = %d\n",
1327 mdt_obd_name(mti->mti_mdt),
1328 PFID(mdt_object_fid(obj)), rc);
1331 mdt_object_unlock_put(mti, dobj, dlh, 1);
1337 * update status of a completed request
1338 * \param mti [IN] context
1339 * \param pgs [IN] progress of the copy tool
1341 * \retval -ve failure
1343 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1344 struct hsm_progress_kernel *pgs,
1345 const struct cdt_agent_req *car,
1346 enum agent_req_status *status)
1348 const struct lu_env *env = mti->mti_env;
1349 struct mdt_device *mdt = mti->mti_mdt;
1350 struct coordinator *cdt = &mdt->mdt_coordinator;
1351 struct mdt_object *obj = NULL;
1352 enum changelog_rec_flags clf_flags = 0;
1355 bool need_changelog = true;
1359 /* default is to retry */
1360 *status = ARS_WAITING;
1362 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1363 * if error/removed continue anyway to get correct reporting done */
1364 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1365 /* we will update MD HSM only if needed */
1366 is_mh_changed = false;
1368 /* no need to change mh->mh_arch_id
1369 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1371 if (pgs->hpk_errval != 0) {
1372 switch (pgs->hpk_errval) {
1374 /* the copy tool does not support cancel
1375 * so the cancel request is failed
1376 * As we cannot distinguish a cancel progress
1377 * from another action progress (they have the
1378 * same cookie), we suppose here the CT returns
1379 * ENOSYS only if does not support cancel
1381 /* this can also happen when cdt calls it to
1382 * for a timed out request */
1383 *status = ARS_FAILED;
1384 /* to have a cancel event in changelog */
1385 pgs->hpk_errval = ECANCELED;
1388 /* the request record has already been set to
1389 * ARS_CANCELED, this set the cancel request
1391 *status = ARS_SUCCEED;
1394 /* retry only if current policy or requested, and
1395 * object is not on error/removed */
1396 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1397 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1398 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1402 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1403 CERROR("%s: Request %#llx on "DFID
1404 " failed, error code %d too large\n",
1406 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1408 hsm_set_cl_error(&clf_flags, CLF_HSM_ERROVERFLOW);
1411 hsm_set_cl_error(&clf_flags, pgs->hpk_errval);
1414 switch (car->car_hai->hai_action) {
1416 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1419 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1422 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1425 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1426 CERROR("%s: Failed request %#llx on "DFID
1427 " cannot be a CANCEL\n",
1430 PFID(&pgs->hpk_fid));
1433 CERROR("%s: Failed request %#llx on "DFID
1434 " %d is an unknown action\n",
1436 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1437 car->car_hai->hai_action);
1442 *status = ARS_SUCCEED;
1443 switch (car->car_hai->hai_action) {
1445 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1446 /* set ARCHIVE keep EXIST and clear LOST and
1448 mh.mh_arch_ver = pgs->hpk_data_version;
1449 mh.mh_flags |= HS_ARCHIVED;
1450 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1451 is_mh_changed = true;
1454 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1456 /* do not clear RELEASED and DIRTY here
1457 * this will occur in hsm_swap_layouts()
1460 /* Restoring has changed the file version on
1462 mh.mh_arch_ver = pgs->hpk_data_version;
1463 is_mh_changed = true;
1466 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1467 /* clear ARCHIVED EXISTS and LOST */
1468 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1469 is_mh_changed = true;
1472 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1473 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1476 PFID(&pgs->hpk_fid));
1479 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1481 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1482 car->car_hai->hai_action);
1488 /* rc != 0 means error when analysing action, it may come from
1489 * a crasy CT no need to manage DIRTY
1490 * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1493 if (rc == 0 && !IS_ERR(obj))
1494 hsm_set_cl_flags(&clf_flags,
1495 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1497 /* unlock is done later, after layout lock management */
1498 if (is_mh_changed && !IS_ERR(obj))
1499 rc = mdt_hsm_attr_set(mti, obj, &mh);
1501 /* we give back layout lock only if restore was successful or
1502 * if no retry will be attempted and if object is still alive,
1503 * in other cases we just unlock the object */
1504 if (car->car_hai->hai_action == HSMA_RESTORE) {
1505 struct mdt_lock_handle *lh;
1507 /* restore in data FID done, we swap the layouts
1508 * only if restore is successful */
1509 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1510 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1513 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1514 *status = ARS_FAILED;
1515 pgs->hpk_errval = -rc;
1518 /* we have to retry, so keep layout lock */
1519 if (*status == ARS_WAITING)
1522 /* restore special case, need to create ChangeLog record
1523 * before to give back layout lock to avoid concurrent
1524 * file updater to post out of order ChangeLog */
1525 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1526 &car->car_hai->hai_fid);
1527 need_changelog = false;
1529 cdt_restore_handle_del(mti, cdt, &car->car_hai->hai_fid);
1530 if (!IS_ERR_OR_NULL(obj)) {
1531 /* flush UPDATE lock so attributes are upadated */
1532 lh = &mti->mti_lh[MDT_LH_OLD];
1533 mdt_lock_reg_init(lh, LCK_EX);
1534 mdt_object_lock(mti, obj, lh, MDS_INODELOCK_UPDATE);
1535 mdt_object_unlock(mti, obj, lh, 1);
1542 /* always add a ChangeLog record */
1544 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1545 &car->car_hai->hai_fid);
1548 mdt_object_put(mti->mti_env, obj);
1554 * update status of a request
1555 * \param mti [IN] context
1556 * \param pgs [IN] progress of the copy tool
1558 * \retval -ve failure
1560 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1561 struct hsm_progress_kernel *pgs)
1563 struct mdt_device *mdt = mti->mti_mdt;
1564 struct coordinator *cdt = &mdt->mdt_coordinator;
1565 struct cdt_agent_req *car;
1569 /* no coordinator started, so we cannot serve requests */
1570 if (cdt->cdt_state == CDT_STOPPED)
1573 /* first do sanity checks */
1574 car = mdt_cdt_update_request(cdt, pgs);
1576 CERROR("%s: Cannot find running request for cookie %#llx"
1579 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1581 RETURN(PTR_ERR(car));
1584 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1585 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1586 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1587 hsm_copytool_action2name(car->car_hai->hai_action),
1588 pgs->hpk_flags, pgs->hpk_errval,
1589 PFID(&car->car_hai->hai_fid),
1590 PFID(&car->car_hai->hai_dfid));
1592 /* progress is done on FID or data FID depending of the action and
1593 * of the copy progress */
1594 /* for restore progress is used to send back the data FID to cdt */
1595 if (car->car_hai->hai_action == HSMA_RESTORE &&
1596 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1597 car->car_hai->hai_dfid = pgs->hpk_fid;
1599 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1600 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1601 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1602 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1603 CERROR("%s: Progress on "DFID" for cookie %#llx"
1604 " does not match request FID "DFID" nor data FID "
1607 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1608 PFID(&car->car_hai->hai_fid),
1609 PFID(&car->car_hai->hai_dfid));
1610 GOTO(out, rc = -EINVAL);
1613 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1614 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1615 " is not coherent (err=%d and not completed"
1618 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1619 hsm_copytool_action2name(car->car_hai->hai_action),
1620 pgs->hpk_errval, pgs->hpk_flags);
1621 GOTO(out, rc = -EINVAL);
1624 /* now progress is valid */
1626 /* we use a root like ucred */
1627 hsm_init_ucred(mdt_ucred(mti));
1629 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1630 enum agent_req_status status;
1631 struct hsm_record_update update;
1634 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1636 CDEBUG(D_HSM, "updating record: fid="DFID" cookie=%#llx action=%s "
1638 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1639 hsm_copytool_action2name(car->car_hai->hai_action),
1640 agent_req_status2name(status));
1642 /* update record first (LU-9075) */
1643 update.cookie = pgs->hpk_cookie;
1644 update.status = status;
1646 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1649 CERROR("%s: mdt_agent_record_update() failed,"
1650 " rc=%d, cannot update status to %s"
1651 " for cookie %#llx\n",
1652 mdt_obd_name(mdt), rc1,
1653 agent_req_status2name(status),
1655 rc = (rc != 0 ? rc : rc1);
1657 /* then remove request from memory list (LU-9075) */
1658 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1660 /* ct has completed a request, so a slot is available,
1661 * signal the coordinator to find new work */
1662 mdt_hsm_cdt_event(cdt);
1664 /* if copytool send a progress on a canceled request
1665 * we inform copytool it should stop
1667 if (car->car_canceled == 1)
1673 /* remove ref got from mdt_cdt_update_request() */
1674 mdt_cdt_put_request(car);
1681 * data passed to llog_cat_process() callback
1682 * to cancel requests
1684 struct hsm_cancel_all_data {
1685 struct mdt_device *mdt;
1689 * llog_cat_process() callback, used to:
1690 * - purge all requests
1691 * \param env [IN] environment
1692 * \param llh [IN] llog handle
1693 * \param hdr [IN] llog record
1694 * \param data [IN] cb data = struct hsm_cancel_all_data
1696 * \retval -ve failure
1698 static int mdt_cancel_all_cb(const struct lu_env *env,
1699 struct llog_handle *llh,
1700 struct llog_rec_hdr *hdr, void *data)
1702 struct llog_agent_req_rec *larr;
1703 struct hsm_cancel_all_data *hcad;
1707 larr = (struct llog_agent_req_rec *)hdr;
1709 if (larr->arr_status == ARS_WAITING ||
1710 larr->arr_status == ARS_STARTED) {
1711 larr->arr_status = ARS_CANCELED;
1712 larr->arr_req_change = ktime_get_real_seconds();
1713 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1720 * cancel all actions
1721 * \param obd [IN] MDT device
1723 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1726 struct lu_context session;
1727 struct mdt_thread_info *mti;
1728 struct coordinator *cdt = &mdt->mdt_coordinator;
1729 struct cdt_agent_req *car;
1730 struct hsm_action_list *hal = NULL;
1731 struct hsm_action_item *hai;
1732 struct hsm_cancel_all_data hcad;
1733 int hal_sz = 0, hal_len, rc;
1734 enum cdt_states old_state;
1737 rc = lu_env_init(&env, LCT_MD_THREAD);
1741 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1742 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1746 lu_context_enter(&session);
1747 env.le_ses = &session;
1749 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1750 LASSERT(mti != NULL);
1752 mti->mti_env = &env;
1755 hsm_init_ucred(mdt_ucred(mti));
1757 mutex_lock(&cdt->cdt_state_lock);
1758 old_state = cdt->cdt_state;
1760 /* disable coordinator */
1761 rc = set_cdt_state_locked(cdt, CDT_DISABLE);
1763 GOTO(out_cdt_state_unlock, rc);
1765 /* send cancel to all running requests */
1766 down_read(&cdt->cdt_request_lock);
1767 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1768 mdt_cdt_get_request(car);
1769 /* request is not yet removed from list, it will be done
1770 * when copytool will return progress
1773 if (car->car_hai->hai_action == HSMA_CANCEL) {
1774 mdt_cdt_put_request(car);
1779 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1780 cfs_size_round(car->car_hai->hai_len);
1782 if (hal_len > hal_sz && hal_sz > 0) {
1783 /* not enough room, free old buffer */
1784 OBD_FREE(hal, hal_sz);
1788 /* empty buffer, allocate one */
1791 OBD_ALLOC(hal, hal_sz);
1793 mdt_cdt_put_request(car);
1794 up_read(&cdt->cdt_request_lock);
1795 GOTO(out_cdt_state, rc = -ENOMEM);
1799 hal->hal_version = HAL_VERSION;
1800 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1802 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1803 hal->hal_archive_id = car->car_archive_id;
1804 hal->hal_flags = car->car_flags;
1807 hai = hai_first(hal);
1808 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1809 hai->hai_action = HSMA_CANCEL;
1812 /* it is possible to safely call mdt_hsm_agent_send()
1813 * (ie without a deadlock on cdt_request_lock), because the
1814 * write lock is taken only if we are not in purge mode
1815 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1816 * nor mdt_cdt_remove_request())
1818 /* no conflict with cdt thread because cdt is disable and we
1819 * have the request lock */
1820 mdt_hsm_agent_send(mti, hal, 1);
1822 mdt_cdt_put_request(car);
1824 up_read(&cdt->cdt_request_lock);
1827 OBD_FREE(hal, hal_sz);
1829 /* cancel all on-disk records */
1832 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1833 &hcad, 0, 0, WRITE);
1835 /* Enable coordinator, unless the coordinator was stopping. */
1836 set_cdt_state_locked(cdt, old_state);
1837 out_cdt_state_unlock:
1838 mutex_unlock(&cdt->cdt_state_lock);
1840 lu_context_exit(&session);
1841 lu_context_fini(&session);
1849 * check if a request is compatible with file status
1850 * \param hai [IN] request description
1851 * \param archive_id [IN] request archive id
1852 * \param rq_flags [IN] request flags
1853 * \param hsm [IN] file HSM metadata
1856 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1857 u32 archive_id, u64 rq_flags,
1858 const struct md_hsm *hsm)
1860 int is_compat = false;
1864 hsm_flags = hsm->mh_flags;
1865 switch (hai->hai_action) {
1867 if (!(hsm_flags & HS_NOARCHIVE) &&
1868 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1871 if (hsm_flags & HS_EXISTS &&
1873 archive_id != hsm->mh_arch_id)
1878 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1879 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1883 if (!(hsm_flags & HS_RELEASED) &&
1884 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1891 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1892 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1893 PFID(&hai->hai_fid),
1894 hsm_copytool_action2name(hai->hai_action), rq_flags,
1895 hai->hai_extent.offset, hai->hai_extent.length,
1897 (is_compat ? "compatible" : "uncompatible"));
1903 * sysfs interface used to get/set HSM behaviour (cdt->cdt_policy)
1905 static const struct {
1909 } hsm_policy_names[] = {
1910 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1911 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1916 * convert a policy name to a bit
1917 * \param name [IN] policy name
1919 * \retval policy bit
1921 static __u64 hsm_policy_str2bit(const char *name)
1925 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1926 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1927 strcmp(hsm_policy_names[i].name, name) == 0)
1928 return hsm_policy_names[i].bit;
1933 * convert a policy bit field to a string
1934 * \param mask [IN] policy bit field
1935 * \param hexa [IN] print mask before bit names
1936 * \param buffer [OUT] string
1937 * \param count [IN] size of buffer
1939 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1947 seq_printf(m, "(%#llx) ", mask);
1949 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1952 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1953 if (hsm_policy_names[j].bit == bit)
1957 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1959 seq_printf(m, "%s ", hsm_policy_names[j].name);
1961 /* remove last ' ' */
1966 /* methods to read/write HSM policy flags */
1967 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1969 struct mdt_device *mdt = m->private;
1970 struct coordinator *cdt = &mdt->mdt_coordinator;
1973 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1978 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1979 size_t count, loff_t *off)
1981 struct seq_file *m = file->private_data;
1982 struct mdt_device *mdt = m->private;
1983 struct coordinator *cdt = &mdt->mdt_coordinator;
1984 char *start, *token, sign;
1987 __u64 add_mask, remove_mask, set_mask;
1991 if (count + 1 > PAGE_SIZE)
1994 OBD_ALLOC(buf, count + 1);
1998 if (copy_from_user(buf, buffer, count))
1999 GOTO(out, rc = -EFAULT);
2004 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
2007 add_mask = remove_mask = set_mask = 0;
2009 token = strsep(&start, "\n ");
2015 if (sign == '-' || sign == '+')
2018 policy = hsm_policy_str2bit(token);
2020 CWARN("%s: '%s' is unknown, "
2021 "supported policies are:\n", mdt_obd_name(mdt),
2023 hsm_policy_bit2str(m, 0, false);
2024 GOTO(out, rc = -EINVAL);
2028 remove_mask |= policy;
2038 } while (start != NULL);
2040 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
2041 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
2043 /* if no sign in all string, it is a clear and set
2044 * if some sign found, all unsigned are converted
2046 * P1 P2 = set to P1 and P2
2047 * P1 -P2 = add P1 clear P2 same as +P1 -P2
2049 if (remove_mask == 0 && add_mask == 0) {
2050 cdt->cdt_policy = set_mask;
2052 cdt->cdt_policy |= set_mask | add_mask;
2053 cdt->cdt_policy &= ~remove_mask;
2056 GOTO(out, rc = count);
2059 OBD_FREE(buf, count + 1);
2062 LDEBUGFS_SEQ_FOPS(mdt_hsm_policy);
2064 ssize_t loop_period_show(struct kobject *kobj, struct attribute *attr,
2067 struct coordinator *cdt = container_of(kobj, struct coordinator,
2070 return scnprintf(buf, PAGE_SIZE, "%lu\n", cdt->cdt_loop_period);
2073 ssize_t loop_period_store(struct kobject *kobj, struct attribute *attr,
2074 const char *buffer, size_t count)
2076 struct coordinator *cdt = container_of(kobj, struct coordinator,
2081 rc = kstrtouint(buffer, 0, &val);
2086 cdt->cdt_loop_period = val;
2088 return val ? count : -EINVAL;
2090 LUSTRE_RW_ATTR(loop_period);
2092 ssize_t grace_delay_show(struct kobject *kobj, struct attribute *attr,
2095 struct coordinator *cdt = container_of(kobj, struct coordinator,
2098 return scnprintf(buf, PAGE_SIZE, "%lu\n", cdt->cdt_grace_delay);
2101 ssize_t grace_delay_store(struct kobject *kobj, struct attribute *attr,
2102 const char *buffer, size_t count)
2104 struct coordinator *cdt = container_of(kobj, struct coordinator,
2109 rc = kstrtouint(buffer, 0, &val);
2114 cdt->cdt_grace_delay = val;
2116 return val ? count : -EINVAL;
2118 LUSTRE_RW_ATTR(grace_delay);
2120 ssize_t active_request_timeout_show(struct kobject *kobj,
2121 struct attribute *attr,
2124 struct coordinator *cdt = container_of(kobj, struct coordinator,
2127 return scnprintf(buf, PAGE_SIZE, "%lu\n", cdt->cdt_active_req_timeout);
2130 ssize_t active_request_timeout_store(struct kobject *kobj,
2131 struct attribute *attr,
2132 const char *buffer, size_t count)
2134 struct coordinator *cdt = container_of(kobj, struct coordinator,
2139 rc = kstrtouint(buffer, 0, &val);
2144 cdt->cdt_active_req_timeout = val;
2146 return val ? count : -EINVAL;
2148 LUSTRE_RW_ATTR(active_request_timeout);
2150 ssize_t max_requests_show(struct kobject *kobj, struct attribute *attr,
2153 struct coordinator *cdt = container_of(kobj, struct coordinator,
2156 return scnprintf(buf, PAGE_SIZE, "%llu\n", cdt->cdt_max_requests);
2159 ssize_t max_requests_store(struct kobject *kobj, struct attribute *attr,
2160 const char *buffer, size_t count)
2162 struct coordinator *cdt = container_of(kobj, struct coordinator,
2164 unsigned long long val;
2167 rc = kstrtoull(buffer, 0, &val);
2172 cdt->cdt_max_requests = val;
2174 return val ? count : -EINVAL;
2176 LUSTRE_RW_ATTR(max_requests);
2178 ssize_t default_archive_id_show(struct kobject *kobj, struct attribute *attr,
2181 struct coordinator *cdt = container_of(kobj, struct coordinator,
2184 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_default_archive_id);
2187 ssize_t default_archive_id_store(struct kobject *kobj, struct attribute *attr,
2188 const char *buffer, size_t count)
2190 struct coordinator *cdt = container_of(kobj, struct coordinator,
2195 rc = kstrtouint(buffer, 0, &val);
2200 cdt->cdt_default_archive_id = val;
2202 return val ? count : -EINVAL;
2204 LUSTRE_RW_ATTR(default_archive_id);
2207 * procfs write method for MDT/hsm_control
2208 * proc entry is in mdt directory so data is mdt obd_device pointer
2210 #define CDT_ENABLE_CMD "enabled"
2211 #define CDT_STOP_CMD "shutdown"
2212 #define CDT_DISABLE_CMD "disabled"
2213 #define CDT_PURGE_CMD "purge"
2214 #define CDT_HELP_CMD "help"
2215 #define CDT_MAX_CMD_LEN 10
2217 ssize_t hsm_control_store(struct kobject *kobj, struct attribute *attr,
2218 const char *buffer, size_t count)
2220 struct obd_device *obd = container_of(kobj, struct obd_device,
2222 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2223 struct coordinator *cdt = &(mdt->mdt_coordinator);
2227 if (count == 0 || count >= CDT_MAX_CMD_LEN)
2230 if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
2231 if (cdt->cdt_state == CDT_DISABLE) {
2232 rc = set_cdt_state(cdt, CDT_RUNNING);
2233 mdt_hsm_cdt_event(cdt);
2234 wake_up(&cdt->cdt_waitq);
2236 rc = mdt_hsm_cdt_start(mdt);
2238 } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
2239 if ((cdt->cdt_state == CDT_STOPPING) ||
2240 (cdt->cdt_state == CDT_STOPPED)) {
2241 CERROR("%s: Coordinator already stopped\n",
2245 rc = mdt_hsm_cdt_stop(mdt);
2247 } else if (strncmp(buffer, CDT_DISABLE_CMD,
2248 strlen(CDT_DISABLE_CMD)) == 0) {
2249 if ((cdt->cdt_state == CDT_STOPPING) ||
2250 (cdt->cdt_state == CDT_STOPPED)) {
2251 CERROR("%s: Coordinator is stopped\n",
2255 rc = set_cdt_state(cdt, CDT_DISABLE);
2257 } else if (strncmp(buffer, CDT_PURGE_CMD,
2258 strlen(CDT_PURGE_CMD)) == 0) {
2259 rc = hsm_cancel_all_actions(mdt);
2260 } else if (strncmp(buffer, CDT_HELP_CMD,
2261 strlen(CDT_HELP_CMD)) == 0) {
2269 CERROR("%s: Valid coordinator control commands are: "
2270 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2271 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2272 CDT_PURGE_CMD, CDT_HELP_CMD);
2280 ssize_t hsm_control_show(struct kobject *kobj, struct attribute *attr,
2283 struct obd_device *obd = container_of(kobj, struct obd_device,
2285 struct coordinator *cdt;
2287 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2289 return scnprintf(buf, PAGE_SIZE, "%s\n",
2290 cdt_mdt_state2str(cdt->cdt_state));
2294 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2300 for (i = 0; i < 8 * sizeof(mask); i++) {
2301 if (mask & (1UL << i)) {
2302 seq_printf(m, "%s%s", first ? "" : " ",
2303 hsm_copytool_action2name(i));
2313 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2315 struct mdt_device *mdt = m->private;
2316 struct coordinator *cdt = &mdt->mdt_coordinator;
2318 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2322 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2324 struct mdt_device *mdt = m->private;
2325 struct coordinator *cdt = &mdt->mdt_coordinator;
2327 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2331 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2333 struct mdt_device *mdt = m->private;
2334 struct coordinator *cdt = &mdt->mdt_coordinator;
2336 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2339 static inline enum hsm_copytool_action
2340 hsm_copytool_name2action(const char *name)
2342 if (strcasecmp(name, "NOOP") == 0)
2344 else if (strcasecmp(name, "ARCHIVE") == 0)
2345 return HSMA_ARCHIVE;
2346 else if (strcasecmp(name, "RESTORE") == 0)
2347 return HSMA_RESTORE;
2348 else if (strcasecmp(name, "REMOVE") == 0)
2350 else if (strcasecmp(name, "CANCEL") == 0)
2357 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2358 size_t user_count, __u64 *mask)
2360 char *buf, *pos, *name;
2366 if (!(user_count < 4096))
2369 buf_size = user_count + 1;
2371 OBD_ALLOC(buf, buf_size);
2375 if (copy_from_user(buf, user_buf, buf_size - 1))
2376 GOTO(out, rc = -EFAULT);
2378 buf[buf_size - 1] = '\0';
2381 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2387 action = hsm_copytool_name2action(name);
2389 GOTO(out, rc = -EINVAL);
2391 new_mask |= (1UL << action);
2397 OBD_FREE(buf, buf_size);
2403 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2404 size_t count, loff_t *off)
2406 struct seq_file *m = file->private_data;
2407 struct mdt_device *mdt = m->private;
2408 struct coordinator *cdt = &mdt->mdt_coordinator;
2410 return mdt_write_hsm_request_mask(file, buf, count,
2411 &cdt->cdt_user_request_mask);
2415 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2416 size_t count, loff_t *off)
2418 struct seq_file *m = file->private_data;
2419 struct mdt_device *mdt = m->private;
2420 struct coordinator *cdt = &mdt->mdt_coordinator;
2422 return mdt_write_hsm_request_mask(file, buf, count,
2423 &cdt->cdt_group_request_mask);
2427 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2428 size_t count, loff_t *off)
2430 struct seq_file *m = file->private_data;
2431 struct mdt_device *mdt = m->private;
2432 struct coordinator *cdt = &mdt->mdt_coordinator;
2434 return mdt_write_hsm_request_mask(file, buf, count,
2435 &cdt->cdt_other_request_mask);
2438 static ssize_t remove_archive_on_last_unlink_show(struct kobject *kobj,
2439 struct attribute *attr,
2442 struct coordinator *cdt = container_of(kobj, struct coordinator,
2445 return scnprintf(buf, PAGE_SIZE, "%u\n",
2446 cdt->cdt_remove_archive_on_last_unlink);
2449 static ssize_t remove_archive_on_last_unlink_store(struct kobject *kobj,
2450 struct attribute *attr,
2454 struct coordinator *cdt = container_of(kobj, struct coordinator,
2459 rc = kstrtobool(buffer, &val);
2463 cdt->cdt_remove_archive_on_last_unlink = val;
2466 LUSTRE_RW_ATTR(remove_archive_on_last_unlink);
2468 LDEBUGFS_SEQ_FOPS(mdt_hsm_user_request_mask);
2469 LDEBUGFS_SEQ_FOPS(mdt_hsm_group_request_mask);
2470 LDEBUGFS_SEQ_FOPS(mdt_hsm_other_request_mask);
2472 /* Read-only sysfs files for request counters */
2473 static ssize_t archive_count_show(struct kobject *kobj, struct attribute *attr,
2476 struct coordinator *cdt = container_of(kobj, struct coordinator,
2479 return scnprintf(buf, PAGE_SIZE, "%d\n",
2480 atomic_read(&cdt->cdt_archive_count));
2482 LUSTRE_RO_ATTR(archive_count);
2484 static ssize_t restore_count_show(struct kobject *kobj, struct attribute *attr,
2487 struct coordinator *cdt = container_of(kobj, struct coordinator,
2490 return scnprintf(buf, PAGE_SIZE, "%d\n",
2491 atomic_read(&cdt->cdt_restore_count));
2493 LUSTRE_RO_ATTR(restore_count);
2495 static ssize_t remove_count_show(struct kobject *kobj, struct attribute *attr,
2498 struct coordinator *cdt = container_of(kobj, struct coordinator,
2501 return scnprintf(buf, PAGE_SIZE, "%d\n",
2502 atomic_read(&cdt->cdt_remove_count));
2504 LUSTRE_RO_ATTR(remove_count);
2506 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2508 .fops = &mdt_hsm_agent_fops },
2509 { .name = "actions",
2510 .fops = &mdt_hsm_actions_fops,
2511 .proc_mode = 0444 },
2513 .fops = &mdt_hsm_policy_fops },
2514 { .name = "active_requests",
2515 .fops = &mdt_hsm_active_requests_fops },
2516 { .name = "user_request_mask",
2517 .fops = &mdt_hsm_user_request_mask_fops, },
2518 { .name = "group_request_mask",
2519 .fops = &mdt_hsm_group_request_mask_fops, },
2520 { .name = "other_request_mask",
2521 .fops = &mdt_hsm_other_request_mask_fops, },
2525 static struct attribute *hsm_attrs[] = {
2526 &lustre_attr_loop_period.attr,
2527 &lustre_attr_grace_delay.attr,
2528 &lustre_attr_active_request_timeout.attr,
2529 &lustre_attr_max_requests.attr,
2530 &lustre_attr_default_archive_id.attr,
2531 &lustre_attr_remove_archive_on_last_unlink.attr,
2532 &lustre_attr_archive_count.attr,
2533 &lustre_attr_restore_count.attr,
2534 &lustre_attr_remove_count.attr,
2538 static void hsm_kobj_release(struct kobject *kobj)
2540 struct coordinator *cdt = container_of(kobj, struct coordinator,
2543 debugfs_remove_recursive(cdt->cdt_debugfs_dir);
2544 cdt->cdt_debugfs_dir = NULL;
2546 complete(&cdt->cdt_kobj_unregister);
2549 static struct kobj_type hsm_ktype = {
2550 .default_attrs = hsm_attrs,
2551 .sysfs_ops = &lustre_sysfs_ops,
2552 .release = hsm_kobj_release,
2556 * create sysfs entries for coordinator
2559 * \retval -ve failure
2561 int hsm_cdt_tunables_init(struct mdt_device *mdt)
2563 struct coordinator *cdt = &mdt->mdt_coordinator;
2564 struct obd_device *obd = mdt2obd_dev(mdt);
2567 init_completion(&cdt->cdt_kobj_unregister);
2568 rc = kobject_init_and_add(&cdt->cdt_hsm_kobj, &hsm_ktype,
2569 &obd->obd_kset.kobj, "%s", "hsm");
2571 kobject_put(&cdt->cdt_hsm_kobj);
2575 /* init debugfs entries, failure is not critical */
2576 cdt->cdt_debugfs_dir = ldebugfs_register("hsm",
2577 obd->obd_debugfs_entry,
2578 lprocfs_mdt_hsm_vars, mdt);
2579 if (IS_ERR_OR_NULL(cdt->cdt_debugfs_dir)) {
2580 rc = cdt->cdt_debugfs_dir ? PTR_ERR(cdt->cdt_debugfs_dir) :
2582 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir, rc = %d\n",
2583 mdt_obd_name(mdt), rc);
2584 cdt->cdt_debugfs_dir = NULL;
2585 kobject_put(&cdt->cdt_hsm_kobj);
2593 * remove sysfs entries for coordinator
2597 void hsm_cdt_tunables_fini(struct mdt_device *mdt)
2599 struct coordinator *cdt = &mdt->mdt_coordinator;
2601 kobject_put(&cdt->cdt_hsm_kobj);
2602 wait_for_completion(&cdt->cdt_kobj_unregister);