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"
51 * get obj and HSM attributes on a fid
52 * \param mti [IN] context
53 * \param fid [IN] object fid
54 * \param hsm [OUT] HSM meta data
55 * \retval obj or error (-ENOENT if not found)
57 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
58 const struct lu_fid *fid,
62 struct mdt_object *obj;
70 /* find object by FID */
71 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
75 if (!mdt_object_exists(obj)) {
77 mdt_object_put(mti->mti_env, obj);
78 RETURN(ERR_PTR(-ENOENT));
81 rc = mdt_attr_get_complex(mti, obj, ma);
83 mdt_object_put(mti->mti_env, obj);
87 if (ma->ma_valid & MA_HSM)
90 memset(hsm, 0, sizeof(*hsm));
95 void mdt_hsm_dump_hal(int level, const char *prefix,
96 struct hsm_action_list *hal)
99 struct hsm_action_item *hai;
102 CDEBUG(level, "%s: HAL header: version %X count %d"
103 " archive_id %d flags %#llx\n",
104 prefix, hal->hal_version, hal->hal_count,
105 hal->hal_archive_id, hal->hal_flags);
107 hai = hai_first(hal);
108 for (i = 0; i < hal->hal_count; i++) {
109 sz = hai->hai_len - sizeof(*hai);
110 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
112 " action=%s extent=%#llx-%#llx gid=%#llx"
113 " datalen=%d data=[%s]\n",
115 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
117 hsm_copytool_action2name(hai->hai_action),
118 hai->hai_extent.offset,
119 hai->hai_extent.length,
121 hai_dump_data_field(hai, buf, sizeof(buf)));
127 * data passed to llog_cat_process() callback
128 * to scan requests and take actions
130 struct hsm_scan_request {
133 struct hsm_action_list *hal;
136 struct hsm_scan_data {
137 struct mdt_thread_info *hsd_mti;
138 char hsd_fsname[MTI_NAME_MAXLEN + 1];
139 /* are we scanning the logs for housekeeping, or just looking
142 bool hsd_housekeeping;
143 bool hsd_one_restore;
144 int hsd_action_count;
145 int hsd_request_len; /* array alloc len */
146 int hsd_request_count; /* array used count */
147 struct hsm_scan_request *hsd_request;
150 static int mdt_cdt_waiting_cb(const struct lu_env *env,
151 struct mdt_device *mdt,
152 struct llog_handle *llh,
153 struct llog_agent_req_rec *larr,
154 struct hsm_scan_data *hsd)
156 struct coordinator *cdt = &mdt->mdt_coordinator;
157 struct hsm_scan_request *request;
158 struct hsm_action_item *hai;
163 /* Are agents full? */
164 if (atomic_read(&cdt->cdt_request_count) >= cdt->cdt_max_requests)
165 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
167 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
168 cdt->cdt_max_requests) {
169 /* We cannot send any more request
171 * *** SPECIAL CASE ***
173 * Restore requests are too important not to schedule at least
174 * one, everytime we can.
176 if (larr->arr_hai.hai_action != HSMA_RESTORE ||
177 hsd->hsd_one_restore)
178 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
181 hai_size = cfs_size_round(larr->arr_hai.hai_len);
182 archive_id = larr->arr_archive_id;
184 /* Can we add this action to one of the existing HALs in hsd. */
186 for (i = 0; i < hsd->hsd_request_count; i++) {
187 if (hsd->hsd_request[i].hal->hal_archive_id == archive_id &&
188 hsd->hsd_request[i].hal_used_sz + hai_size <=
190 request = &hsd->hsd_request[i];
195 /* Are we trying to force-schedule a request? */
196 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
197 cdt->cdt_max_requests) {
198 /* Is there really no compatible hsm_scan_request? */
200 for (i -= 1; i >= 0; i--) {
201 if (hsd->hsd_request[i].hal->hal_archive_id ==
203 request = &hsd->hsd_request[i];
209 /* Make room for the hai */
211 /* Discard the last hai until there is enough space */
213 request->hal->hal_count--;
215 hai = hai_first(request->hal);
216 for (i = 0; i < request->hal->hal_count; i++)
218 request->hal_used_sz -=
219 cfs_size_round(hai->hai_len);
220 hsd->hsd_action_count--;
221 } while (request->hal_used_sz + hai_size >
223 } else if (hsd->hsd_housekeeping) {
224 struct hsm_scan_request *tmp;
226 /* Discard the (whole) last hal */
227 hsd->hsd_request_count--;
228 LASSERT(hsd->hsd_request_count >= 0);
229 tmp = &hsd->hsd_request[hsd->hsd_request_count];
230 hsd->hsd_action_count -= tmp->hal->hal_count;
231 LASSERT(hsd->hsd_action_count >= 0);
232 OBD_FREE(tmp->hal, tmp->hal_sz);
234 /* Bailing out, this code path is too hot */
235 RETURN(LLOG_PROC_BREAK);
241 struct hsm_action_list *hal;
243 LASSERT(hsd->hsd_request_count < hsd->hsd_request_len);
244 request = &hsd->hsd_request[hsd->hsd_request_count];
246 /* allocates hai vector size just needs to be large
248 request->hal_sz = sizeof(*request->hal) +
249 cfs_size_round(MTI_NAME_MAXLEN + 1) + 2 * hai_size;
250 OBD_ALLOC_LARGE(hal, request->hal_sz);
254 hal->hal_version = HAL_VERSION;
255 strlcpy(hal->hal_fsname, hsd->hsd_fsname, MTI_NAME_MAXLEN + 1);
256 hal->hal_archive_id = larr->arr_archive_id;
257 hal->hal_flags = larr->arr_flags;
259 request->hal_used_sz = hal_size(hal);
261 hsd->hsd_request_count++;
262 } else if (request->hal_sz < request->hal_used_sz + hai_size) {
263 /* Not enough room, need an extension */
267 sz = min_t(int, 2 * request->hal_sz, LDLM_MAXREQSIZE);
268 LASSERT(request->hal_used_sz + hai_size < sz);
270 OBD_ALLOC_LARGE(hal_buffer, sz);
274 memcpy(hal_buffer, request->hal, request->hal_used_sz);
275 OBD_FREE_LARGE(request->hal, request->hal_sz);
276 request->hal = hal_buffer;
277 request->hal_sz = sz;
280 hai = hai_first(request->hal);
281 for (i = 0; i < request->hal->hal_count; i++)
284 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
286 request->hal_used_sz += hai_size;
287 request->hal->hal_count++;
289 hsd->hsd_action_count++;
291 switch (hai->hai_action) {
295 hsd->hsd_one_restore = true;
296 /* Intentional fallthrough */
298 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
299 llh->lgh_hdr->llh_cat_idx,
300 larr->arr_hdr.lrh_index);
306 static int mdt_cdt_started_cb(const struct lu_env *env,
307 struct mdt_device *mdt,
308 struct llog_handle *llh,
309 struct llog_agent_req_rec *larr,
310 struct hsm_scan_data *hsd)
312 struct coordinator *cdt = &mdt->mdt_coordinator;
313 struct hsm_action_item *hai = &larr->arr_hai;
314 struct cdt_agent_req *car;
315 time64_t now = ktime_get_real_seconds();
317 enum changelog_rec_flags clf_flags;
320 if (!hsd->hsd_housekeeping)
323 /* we search for a running request
324 * error may happen if coordinator crashes or stopped
325 * with running request
327 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
329 last = larr->arr_req_change;
331 last = car->car_req_update;
334 /* test if request too long, if yes cancel it
335 * the same way the copy tool acknowledge a cancel request */
336 if (now <= last + cdt->cdt_active_req_timeout)
337 GOTO(out_car, rc = 0);
339 dump_llog_agent_req_rec("request timed out, start cleaning", larr);
342 car->car_req_update = now;
343 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0, &car->car_uuid);
344 /* Remove car from memory list (LU-9075) */
345 mdt_cdt_remove_request(cdt, hai->hai_cookie);
348 /* Emit a changelog record for the failed action.*/
350 hsm_set_cl_error(&clf_flags, ECANCELED);
352 switch (hai->hai_action) {
354 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
357 hsm_set_cl_event(&clf_flags, HE_RESTORE);
360 hsm_set_cl_event(&clf_flags, HE_REMOVE);
363 hsm_set_cl_event(&clf_flags, HE_CANCEL);
366 /* Unknown record type, skip changelog. */
372 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
375 if (hai->hai_action == HSMA_RESTORE)
376 cdt_restore_handle_del(hsd->hsd_mti, cdt, &hai->hai_fid);
378 larr->arr_status = ARS_CANCELED;
379 larr->arr_req_change = now;
380 rc = llog_write(hsd->hsd_mti->mti_env, llh, &larr->arr_hdr,
381 larr->arr_hdr.lrh_index);
383 CERROR("%s: cannot update agent log: rc = %d\n",
384 mdt_obd_name(mdt), rc);
385 rc = LLOG_DEL_RECORD;
388 /* ct has completed a request, so a slot is available,
389 * signal the coordinator to find new work */
390 mdt_hsm_cdt_event(cdt);
393 mdt_cdt_put_request(car);
399 * llog_cat_process() callback, used to:
400 * - find waiting request and start action
401 * - purge canceled and done requests
402 * \param env [IN] environment
403 * \param llh [IN] llog handle
404 * \param hdr [IN] llog record
405 * \param data [IN/OUT] cb data = struct hsm_scan_data
407 * \retval -ve failure
409 static int mdt_coordinator_cb(const struct lu_env *env,
410 struct llog_handle *llh,
411 struct llog_rec_hdr *hdr,
414 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
415 struct hsm_scan_data *hsd = data;
416 struct mdt_device *mdt = hsd->hsd_mti->mti_mdt;
417 struct coordinator *cdt = &mdt->mdt_coordinator;
420 larr = (struct llog_agent_req_rec *)hdr;
421 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
422 switch (larr->arr_status) {
424 RETURN(mdt_cdt_waiting_cb(env, mdt, llh, larr, hsd));
426 RETURN(mdt_cdt_started_cb(env, mdt, llh, larr, hsd));
428 if (!hsd->hsd_housekeeping)
431 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
432 ktime_get_real_seconds()) {
433 cdt_agent_record_hash_del(cdt,
434 larr->arr_hai.hai_cookie);
435 RETURN(LLOG_DEL_RECORD);
442 /* Release the ressource used by the coordinator. Called when the
443 * coordinator is stopping. */
444 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
446 struct coordinator *cdt = &mdt->mdt_coordinator;
447 struct cdt_agent_req *car, *tmp1;
448 struct hsm_agent *ha, *tmp2;
449 struct cdt_restore_handle *crh, *tmp3;
450 struct mdt_thread_info *cdt_mti;
453 down_write(&cdt->cdt_request_lock);
454 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
456 cfs_hash_del(cdt->cdt_request_cookie_hash,
457 &car->car_hai->hai_cookie,
458 &car->car_cookie_hash);
459 list_del(&car->car_request_list);
460 mdt_cdt_put_request(car);
462 up_write(&cdt->cdt_request_lock);
464 down_write(&cdt->cdt_agent_lock);
465 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
466 list_del(&ha->ha_list);
467 if (ha->ha_archive_cnt != 0)
468 OBD_FREE_PTR_ARRAY(ha->ha_archive_id,
472 up_write(&cdt->cdt_agent_lock);
474 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
475 mutex_lock(&cdt->cdt_restore_lock);
476 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_handle_list,
478 list_del(&crh->crh_list);
479 /* give back layout lock */
480 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
481 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
483 mutex_unlock(&cdt->cdt_restore_lock);
487 * Coordinator state transition table, indexed on enum cdt_states, taking
488 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
489 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
491 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
492 /* from -> to: stopped init running disable stopping */
493 /* stopped */ { true, true, false, false, false },
494 /* init */ { true, false, true, false, false },
495 /* running */ { false, false, true, true, true },
496 /* disable */ { false, false, true, true, true },
497 /* stopping */ { true, false, false, false, false }
501 * Change coordinator thread state
502 * Some combinations are not valid, so catch them here.
504 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
505 * the transition was not possible.
507 static int set_cdt_state_locked(struct coordinator *cdt,
508 enum cdt_states new_state)
511 enum cdt_states state;
513 state = cdt->cdt_state;
515 if (cdt_transition[state][new_state]) {
516 cdt->cdt_state = new_state;
520 "unexpected coordinator transition, from=%s, to=%s\n",
521 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
528 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state)
532 mutex_lock(&cdt->cdt_state_lock);
533 rc = set_cdt_state_locked(cdt, new_state);
534 mutex_unlock(&cdt->cdt_state_lock);
543 * \param data [IN] obd device
545 * \retval -ve failure
547 static int mdt_coordinator(void *data)
549 struct mdt_thread_info *mti = data;
550 struct mdt_device *mdt = mti->mti_mdt;
551 struct coordinator *cdt = &mdt->mdt_coordinator;
552 struct hsm_scan_data hsd = { NULL };
553 time64_t last_housekeeping = 0;
554 size_t request_sz = 0;
558 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
559 mdt_obd_name(mdt), current->pid);
562 obd_uuid2fsname(hsd.hsd_fsname, mdt_obd_name(mdt),
563 sizeof(hsd.hsd_fsname));
565 set_cdt_state(cdt, CDT_RUNNING);
567 /* Inform mdt_hsm_cdt_start(). */
568 wake_up_all(&cdt->cdt_waitq);
575 struct hsm_record_update *updates;
577 /* Limit execution of the expensive requests traversal
578 * to at most one second. This prevents repeatedly
579 * locking/unlocking the catalog for each request
580 * and preventing other HSM operations from happening
582 wait_event_interruptible_timeout(cdt->cdt_waitq,
583 kthread_should_stop() ||
584 cdt->cdt_wakeup_coordinator,
585 cfs_time_seconds(1));
587 cdt->cdt_wakeup_coordinator = false;
588 CDEBUG(D_HSM, "coordinator resumes\n");
590 if (kthread_should_stop()) {
591 CDEBUG(D_HSM, "Coordinator stops\n");
596 /* if coordinator is suspended continue to wait */
597 if (cdt->cdt_state == CDT_DISABLE) {
598 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
602 /* If no event, and no housekeeping to do, continue to
604 if (last_housekeeping + cdt->cdt_loop_period <=
605 ktime_get_real_seconds()) {
606 last_housekeeping = ktime_get_real_seconds();
607 hsd.hsd_housekeeping = true;
608 } else if (cdt->cdt_event) {
609 hsd.hsd_housekeeping = false;
614 cdt->cdt_event = false;
616 CDEBUG(D_HSM, "coordinator starts reading llog\n");
618 if (hsd.hsd_request_len != cdt->cdt_max_requests) {
619 /* cdt_max_requests has changed,
620 * we need to allocate a new buffer
622 struct hsm_scan_request *tmp = NULL;
623 int max_requests = cdt->cdt_max_requests;
624 OBD_ALLOC_LARGE(tmp, max_requests *
625 sizeof(struct hsm_scan_request));
627 CERROR("Failed to resize request buffer, "
628 "keeping it at %d\n",
629 hsd.hsd_request_len);
631 if (hsd.hsd_request != NULL)
632 OBD_FREE_LARGE(hsd.hsd_request,
635 hsd.hsd_request_len = max_requests;
636 request_sz = hsd.hsd_request_len *
637 sizeof(struct hsm_scan_request);
638 hsd.hsd_request = tmp;
642 hsd.hsd_action_count = 0;
643 hsd.hsd_request_count = 0;
644 hsd.hsd_one_restore = false;
646 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
651 CDEBUG(D_HSM, "found %d requests to send\n",
652 hsd.hsd_request_count);
654 if (list_empty(&cdt->cdt_agents)) {
655 CDEBUG(D_HSM, "no agent available, "
656 "coordinator sleeps\n");
660 /* Compute how many HAI we have in all the requests */
662 for (i = 0; i < hsd.hsd_request_count; i++) {
663 const struct hsm_scan_request *request =
666 updates_cnt += request->hal->hal_count;
669 /* Allocate a temporary array to store the cookies to
670 * update, and their status. */
671 updates_sz = updates_cnt * sizeof(*updates);
672 OBD_ALLOC_LARGE(updates, updates_sz);
673 if (updates == NULL) {
674 CERROR("%s: Cannot allocate memory (%d bytes) "
675 "for %d updates. Too many HSM requests?\n",
676 mdt_obd_name(mdt), updates_sz, updates_cnt);
680 /* here hsd contains a list of requests to be started */
681 for (i = 0; i < hsd.hsd_request_count; i++) {
682 struct hsm_scan_request *request = &hsd.hsd_request[i];
683 struct hsm_action_list *hal = request->hal;
684 struct hsm_action_item *hai;
687 /* still room for work ? */
688 if (atomic_read(&cdt->cdt_request_count) >=
689 cdt->cdt_max_requests)
692 rc = mdt_hsm_agent_send(mti, hal, 0);
693 /* if failure, we suppose it is temporary
694 * if the copy tool failed to do the request
695 * it has to use hsm_progress
698 /* set up cookie vector to set records status
699 * after copy tools start or failed
701 hai = hai_first(hal);
702 for (j = 0; j < hal->hal_count; j++) {
703 updates[update_idx].cookie = hai->hai_cookie;
704 updates[update_idx].status =
705 (rc ? ARS_WAITING : ARS_STARTED);
712 rc = mdt_agent_record_update(mti->mti_env, mdt,
713 updates, update_idx);
715 CERROR("%s: mdt_agent_record_update() failed, "
716 "rc=%d, cannot update records "
718 mdt_obd_name(mdt), rc, update_idx);
721 OBD_FREE_LARGE(updates, updates_sz);
724 /* free hal allocated by callback */
725 for (i = 0; i < hsd.hsd_request_count; i++) {
726 struct hsm_scan_request *request = &hsd.hsd_request[i];
728 OBD_FREE_LARGE(request->hal, request->hal_sz);
732 if (hsd.hsd_request != NULL)
733 OBD_FREE_LARGE(hsd.hsd_request, request_sz);
735 mdt_hsm_cdt_cleanup(mdt);
738 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
739 mdt_obd_name(mdt), current->pid, rc);
741 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
743 mdt_obd_name(mdt), current->pid);
748 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
749 const struct lu_fid *fid,
750 const struct hsm_extent *he)
752 struct cdt_restore_handle *crh;
753 struct mdt_object *obj;
757 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
762 /* in V1 all file is restored
763 * crh->extent.start = he->offset;
764 * crh->extent.end = he->offset + he->length;
766 crh->crh_extent.start = 0;
767 crh->crh_extent.end = he->length;
768 /* get the layout lock */
769 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
770 obj = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
771 MDS_INODELOCK_LAYOUT);
773 GOTO(out_crh, rc = PTR_ERR(obj));
775 /* We do not keep a reference on the object during the restore
776 * which can be very long. */
777 mdt_object_put(mti->mti_env, obj);
779 mutex_lock(&cdt->cdt_restore_lock);
780 if (unlikely(cdt->cdt_state == CDT_STOPPED ||
781 cdt->cdt_state == CDT_STOPPING)) {
782 mutex_unlock(&cdt->cdt_restore_lock);
783 GOTO(out_lh, rc = -EAGAIN);
786 list_add_tail(&crh->crh_list, &cdt->cdt_restore_handle_list);
787 mutex_unlock(&cdt->cdt_restore_lock);
791 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
793 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
799 * lookup a restore handle by FID
800 * caller needs to hold cdt_restore_lock
801 * \param cdt [IN] coordinator
802 * \param fid [IN] FID
803 * \retval cdt_restore_handle found
804 * \retval NULL not found
806 struct cdt_restore_handle *cdt_restore_handle_find(struct coordinator *cdt,
807 const struct lu_fid *fid)
809 struct cdt_restore_handle *crh;
812 list_for_each_entry(crh, &cdt->cdt_restore_handle_list, crh_list) {
813 if (lu_fid_eq(&crh->crh_fid, fid))
820 void cdt_restore_handle_del(struct mdt_thread_info *mti,
821 struct coordinator *cdt, const struct lu_fid *fid)
823 struct cdt_restore_handle *crh;
825 /* give back layout lock */
826 mutex_lock(&cdt->cdt_restore_lock);
827 crh = cdt_restore_handle_find(cdt, fid);
829 list_del(&crh->crh_list);
830 mutex_unlock(&cdt->cdt_restore_lock);
835 /* XXX We pass a NULL object since the restore handle does not
836 * keep a reference on the object being restored. */
837 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
838 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
842 * data passed to llog_cat_process() callback
843 * to scan requests and take actions
845 struct hsm_restore_data {
846 struct mdt_thread_info *hrd_mti;
850 * llog_cat_process() callback, used to:
851 * - find restore request and allocate the restore handle
852 * \param env [IN] environment
853 * \param llh [IN] llog handle
854 * \param hdr [IN] llog record
855 * \param data [IN/OUT] cb data = struct hsm_restore_data
857 * \retval -ve failure
859 static int hsm_restore_cb(const struct lu_env *env,
860 struct llog_handle *llh,
861 struct llog_rec_hdr *hdr, void *data)
863 struct llog_agent_req_rec *larr;
864 struct hsm_restore_data *hrd;
865 struct hsm_action_item *hai;
866 struct mdt_thread_info *mti;
867 struct coordinator *cdt;
873 cdt = &mti->mti_mdt->mdt_coordinator;
875 larr = (struct llog_agent_req_rec *)hdr;
876 hai = &larr->arr_hai;
877 if (hai->hai_cookie >= cdt->cdt_last_cookie) {
878 /* update the cookie to avoid collision */
879 cdt->cdt_last_cookie = hai->hai_cookie + 1;
882 if (hai->hai_action != HSMA_RESTORE ||
883 agent_req_in_final_state(larr->arr_status))
886 /* restore request not in a final state */
888 /* force replay of restore requests left in started state from previous
889 * CDT context, to be canceled later if finally found to be incompatible
890 * when being re-started */
891 if (larr->arr_status == ARS_STARTED) {
892 larr->arr_status = ARS_WAITING;
893 larr->arr_req_change = ktime_get_real_seconds();
894 rc = llog_write(env, llh, hdr, hdr->lrh_index);
899 rc = cdt_restore_handle_add(mti, cdt, &hai->hai_fid, &hai->hai_extent);
905 * restore coordinator state at startup
906 * the goal is to take a layout lock for each registered restore request
907 * \param mti [IN] context
909 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
911 struct hsm_restore_data hrd;
917 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
923 int hsm_init_ucred(struct lu_ucred *uc)
927 uc->uc_valid = UCRED_OLD;
936 uc->uc_suppgids[0] = -1;
937 uc->uc_suppgids[1] = -1;
938 uc->uc_cap = CFS_CAP_FS_MASK;
941 uc->uc_identity = NULL;
942 /* always record internal HSM activity if also enabled globally */
943 uc->uc_enable_audit = 1;
949 * initialize coordinator struct
950 * \param mdt [IN] device
952 * \retval -ve failure
954 int mdt_hsm_cdt_init(struct mdt_device *mdt)
956 struct coordinator *cdt = &mdt->mdt_coordinator;
957 struct mdt_thread_info *cdt_mti = NULL;
961 init_waitqueue_head(&cdt->cdt_waitq);
962 init_rwsem(&cdt->cdt_llog_lock);
963 init_rwsem(&cdt->cdt_agent_lock);
964 init_rwsem(&cdt->cdt_request_lock);
965 mutex_init(&cdt->cdt_restore_lock);
966 mutex_init(&cdt->cdt_state_lock);
967 set_cdt_state(cdt, CDT_STOPPED);
969 INIT_LIST_HEAD(&cdt->cdt_request_list);
970 INIT_LIST_HEAD(&cdt->cdt_agents);
971 INIT_LIST_HEAD(&cdt->cdt_restore_handle_list);
973 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
980 &cdt_request_cookie_hash_ops,
982 if (cdt->cdt_request_cookie_hash == NULL)
985 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
992 &cdt_agent_record_hash_ops,
994 if (cdt->cdt_agent_record_hash == NULL)
995 GOTO(out_request_cookie_hash, rc = -ENOMEM);
997 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
999 GOTO(out_agent_record_hash, rc);
1001 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1002 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
1006 lu_context_enter(&cdt->cdt_session);
1007 cdt->cdt_env.le_ses = &cdt->cdt_session;
1009 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1010 LASSERT(cdt_mti != NULL);
1012 cdt_mti->mti_env = &cdt->cdt_env;
1013 cdt_mti->mti_mdt = mdt;
1015 hsm_init_ucred(mdt_ucred(cdt_mti));
1017 /* default values for sysfs tunnables
1018 * can be override by MGS conf */
1019 cdt->cdt_default_archive_id = 1;
1020 cdt->cdt_grace_delay = 60;
1021 cdt->cdt_loop_period = 10;
1022 cdt->cdt_max_requests = 3;
1023 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1024 cdt->cdt_active_req_timeout = 3600;
1026 /* by default do not remove archives on last unlink */
1027 cdt->cdt_remove_archive_on_last_unlink = false;
1032 lu_env_fini(&cdt->cdt_env);
1033 out_agent_record_hash:
1034 cfs_hash_putref(cdt->cdt_agent_record_hash);
1035 cdt->cdt_agent_record_hash = NULL;
1036 out_request_cookie_hash:
1037 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1038 cdt->cdt_request_cookie_hash = NULL;
1044 * free a coordinator thread
1045 * \param mdt [IN] device
1047 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
1049 struct coordinator *cdt = &mdt->mdt_coordinator;
1052 lu_context_exit(cdt->cdt_env.le_ses);
1053 lu_context_fini(cdt->cdt_env.le_ses);
1055 lu_env_fini(&cdt->cdt_env);
1057 cfs_hash_putref(cdt->cdt_agent_record_hash);
1058 cdt->cdt_agent_record_hash = NULL;
1060 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1061 cdt->cdt_request_cookie_hash = NULL;
1067 * start a coordinator thread
1068 * \param mdt [IN] device
1070 * \retval -ve failure
1072 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1074 struct coordinator *cdt = &mdt->mdt_coordinator;
1075 struct mdt_thread_info *cdt_mti;
1078 struct task_struct *task;
1081 /* functions defined but not yet used
1082 * this avoid compilation warning
1084 ptr = dump_requests;
1086 rc = set_cdt_state(cdt, CDT_INIT);
1088 CERROR("%s: Coordinator already started or stopping\n",
1093 BUILD_BUG_ON(BIT(CDT_POLICY_SHIFT_COUNT - 1) != CDT_POLICY_LAST);
1094 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1096 /* just need to be larger than previous one */
1097 /* cdt_last_cookie is protected by cdt_llog_lock */
1098 cdt->cdt_last_cookie = ktime_get_real_seconds();
1099 atomic_set(&cdt->cdt_request_count, 0);
1100 atomic_set(&cdt->cdt_archive_count, 0);
1101 atomic_set(&cdt->cdt_restore_count, 0);
1102 atomic_set(&cdt->cdt_remove_count, 0);
1103 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1104 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1105 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1107 /* to avoid deadlock when start is made through sysfs
1108 * sysfs entries are created by the coordinator thread
1110 /* set up list of started restore requests */
1111 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1112 rc = mdt_hsm_pending_restore(cdt_mti);
1114 CERROR("%s: cannot take the layout locks needed"
1115 " for registered restore: %d\n",
1116 mdt_obd_name(mdt), rc);
1118 if (mdt->mdt_bottom->dd_rdonly)
1121 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
1124 set_cdt_state(cdt, CDT_STOPPED);
1125 CERROR("%s: error starting coordinator thread: %d\n",
1126 mdt_obd_name(mdt), rc);
1128 cdt->cdt_task = task;
1129 wait_event(cdt->cdt_waitq,
1130 cdt->cdt_state != CDT_INIT);
1131 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1140 * stop a coordinator thread
1141 * \param mdt [IN] device
1143 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1145 struct coordinator *cdt = &mdt->mdt_coordinator;
1149 /* stop coordinator thread */
1150 rc = set_cdt_state(cdt, CDT_STOPPING);
1152 kthread_stop(cdt->cdt_task);
1153 cdt->cdt_task = NULL;
1154 set_cdt_state(cdt, CDT_STOPPED);
1160 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1161 const struct lu_fid *fid,
1164 struct mdt_object *obj;
1168 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1170 GOTO(out, rc = PTR_ERR(obj));
1172 if (mh.mh_flags & HS_EXISTS &&
1173 mh.mh_arch_id == archive_id)
1174 GOTO(out_obj, rc = 0);
1176 mh.mh_flags |= HS_EXISTS;
1177 mh.mh_arch_id = archive_id;
1178 rc = mdt_hsm_attr_set(mti, obj, &mh);
1181 mdt_object_put(mti->mti_env, obj);
1187 * register all requests from an hal in the memory list
1188 * \param mti [IN] context
1189 * \param hal [IN] request
1190 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1191 * which is running the CT
1193 * \retval -ve failure
1195 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1196 struct hsm_action_list *hal, struct obd_uuid *uuid)
1198 struct mdt_device *mdt = mti->mti_mdt;
1199 struct coordinator *cdt = &mdt->mdt_coordinator;
1200 struct hsm_action_item *hai;
1204 /* register request in memory list */
1205 hai = hai_first(hal);
1206 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1207 struct cdt_agent_req *car;
1209 /* in case of a cancel request, we first mark the ondisk
1210 * record of the request we want to stop as canceled
1211 * this does not change the cancel record
1212 * it will be done when updating the request status
1214 if (hai->hai_action == HSMA_CANCEL) {
1215 struct hsm_record_update update = {
1216 .cookie = hai->hai_cookie,
1217 .status = ARS_CANCELED,
1220 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1223 CERROR("%s: mdt_agent_record_update() failed, "
1224 "rc=%d, cannot update status to %s "
1225 "for cookie %#llx\n",
1226 mdt_obd_name(mdt), rc,
1227 agent_req_status2name(ARS_CANCELED),
1232 /* find the running request to set it canceled */
1233 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1235 car->car_canceled = 1;
1236 /* uuid has to be changed to the one running the
1237 * request to cancel */
1238 *uuid = car->car_uuid;
1239 mdt_cdt_put_request(car);
1241 /* no need to memorize cancel request
1242 * this also avoid a deadlock when we receive
1243 * a purge all requests command
1248 if (hai->hai_action == HSMA_ARCHIVE) {
1249 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1250 hal->hal_archive_id);
1257 car = mdt_cdt_alloc_request(hal->hal_archive_id, hal->hal_flags,
1260 GOTO(out, rc = PTR_ERR(car));
1262 rc = mdt_cdt_add_request(cdt, car);
1264 mdt_cdt_free_request(car);
1271 * swap layouts between 2 fids
1272 * \param mti [IN] context
1275 * \param mh_common [IN] MD HSM
1277 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1278 struct mdt_object *obj, const struct lu_fid *dfid,
1279 struct md_hsm *mh_common)
1281 struct mdt_object *dobj;
1282 struct mdt_lock_handle *dlh;
1286 if (!mdt_object_exists(obj))
1287 GOTO(out, rc = -ENOENT);
1289 /* we already have layout lock on obj so take only
1291 dlh = &mti->mti_lh[MDT_LH_OLD];
1292 mdt_lock_reg_init(dlh, LCK_EX);
1293 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1295 GOTO(out, rc = PTR_ERR(dobj));
1297 /* if copy tool closes the volatile before sending the final
1298 * progress through llapi_hsm_copy_end(), all the objects
1299 * are removed and mdd_swap_layout LBUG */
1300 if (!mdt_object_exists(dobj)) {
1301 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1302 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1303 GOTO(out_dobj, rc = -ENOENT);
1305 /* Since we only handle restores here, unconditionally use
1306 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1307 * be preserved in case of failure during swap_layout and not
1308 * leave a file in an intermediate but incoherent state.
1309 * But need to setup HSM xattr of data FID before, reuse
1310 * mti and mh presets for FID in hsm_cdt_request_completed(),
1311 * only need to clear RELEASED and DIRTY.
1313 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1314 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1316 rc = mo_swap_layouts(mti->mti_env,
1317 mdt_object_child(obj),
1318 mdt_object_child(dobj),
1319 SWAP_LAYOUTS_MDS_HSM);
1321 rc = mdt_lsom_downgrade(mti, obj);
1324 "%s: File fid="DFID" SOM "
1325 "downgrade failed, rc = %d\n",
1326 mdt_obd_name(mti->mti_mdt),
1327 PFID(mdt_object_fid(obj)), rc);
1330 mdt_object_unlock_put(mti, dobj, dlh, 1);
1336 * update status of a completed request
1337 * \param mti [IN] context
1338 * \param pgs [IN] progress of the copy tool
1340 * \retval -ve failure
1342 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1343 struct hsm_progress_kernel *pgs,
1344 const struct cdt_agent_req *car,
1345 enum agent_req_status *status)
1347 const struct lu_env *env = mti->mti_env;
1348 struct mdt_device *mdt = mti->mti_mdt;
1349 struct coordinator *cdt = &mdt->mdt_coordinator;
1350 struct mdt_object *obj = NULL;
1351 enum changelog_rec_flags clf_flags = 0;
1354 bool need_changelog = true;
1358 /* default is to retry */
1359 *status = ARS_WAITING;
1361 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1362 * if error/removed continue anyway to get correct reporting done */
1363 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1364 /* we will update MD HSM only if needed */
1365 is_mh_changed = false;
1367 /* no need to change mh->mh_arch_id
1368 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1370 if (pgs->hpk_errval != 0) {
1371 switch (pgs->hpk_errval) {
1373 /* the copy tool does not support cancel
1374 * so the cancel request is failed
1375 * As we cannot distinguish a cancel progress
1376 * from another action progress (they have the
1377 * same cookie), we suppose here the CT returns
1378 * ENOSYS only if does not support cancel
1380 /* this can also happen when cdt calls it to
1381 * for a timed out request */
1382 *status = ARS_FAILED;
1383 /* to have a cancel event in changelog */
1384 pgs->hpk_errval = ECANCELED;
1387 /* the request record has already been set to
1388 * ARS_CANCELED, this set the cancel request
1390 *status = ARS_SUCCEED;
1393 /* retry only if current policy or requested, and
1394 * object is not on error/removed */
1395 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1396 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1397 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1401 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1402 CERROR("%s: Request %#llx on "DFID
1403 " failed, error code %d too large\n",
1405 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1407 hsm_set_cl_error(&clf_flags, CLF_HSM_ERROVERFLOW);
1410 hsm_set_cl_error(&clf_flags, pgs->hpk_errval);
1413 switch (car->car_hai->hai_action) {
1415 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1418 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1421 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1424 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1425 CERROR("%s: Failed request %#llx on "DFID
1426 " cannot be a CANCEL\n",
1429 PFID(&pgs->hpk_fid));
1432 CERROR("%s: Failed request %#llx on "DFID
1433 " %d is an unknown action\n",
1435 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1436 car->car_hai->hai_action);
1441 *status = ARS_SUCCEED;
1442 switch (car->car_hai->hai_action) {
1444 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1445 /* set ARCHIVE keep EXIST and clear LOST and
1447 mh.mh_arch_ver = pgs->hpk_data_version;
1448 mh.mh_flags |= HS_ARCHIVED;
1449 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1450 is_mh_changed = true;
1453 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1455 /* do not clear RELEASED and DIRTY here
1456 * this will occur in hsm_swap_layouts()
1459 /* Restoring has changed the file version on
1461 mh.mh_arch_ver = pgs->hpk_data_version;
1462 is_mh_changed = true;
1465 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1466 /* clear ARCHIVED EXISTS and LOST */
1467 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1468 is_mh_changed = true;
1471 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1472 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1475 PFID(&pgs->hpk_fid));
1478 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1480 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1481 car->car_hai->hai_action);
1487 /* rc != 0 means error when analysing action, it may come from
1488 * a crasy CT no need to manage DIRTY
1489 * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1492 if (rc == 0 && !IS_ERR(obj))
1493 hsm_set_cl_flags(&clf_flags,
1494 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1496 /* unlock is done later, after layout lock management */
1497 if (is_mh_changed && !IS_ERR(obj))
1498 rc = mdt_hsm_attr_set(mti, obj, &mh);
1500 /* we give back layout lock only if restore was successful or
1501 * if no retry will be attempted and if object is still alive,
1502 * in other cases we just unlock the object */
1503 if (car->car_hai->hai_action == HSMA_RESTORE) {
1504 struct mdt_lock_handle *lh;
1506 /* restore in data FID done, we swap the layouts
1507 * only if restore is successful */
1508 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1509 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1512 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1513 *status = ARS_FAILED;
1514 pgs->hpk_errval = -rc;
1517 /* we have to retry, so keep layout lock */
1518 if (*status == ARS_WAITING)
1521 /* restore special case, need to create ChangeLog record
1522 * before to give back layout lock to avoid concurrent
1523 * file updater to post out of order ChangeLog */
1524 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1525 &car->car_hai->hai_fid);
1526 need_changelog = false;
1528 cdt_restore_handle_del(mti, cdt, &car->car_hai->hai_fid);
1529 if (!IS_ERR_OR_NULL(obj)) {
1530 /* flush UPDATE lock so attributes are upadated */
1531 lh = &mti->mti_lh[MDT_LH_OLD];
1532 mdt_lock_reg_init(lh, LCK_EX);
1533 mdt_object_lock(mti, obj, lh, MDS_INODELOCK_UPDATE);
1534 mdt_object_unlock(mti, obj, lh, 1);
1541 /* always add a ChangeLog record */
1543 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1544 &car->car_hai->hai_fid);
1547 mdt_object_put(mti->mti_env, obj);
1553 * update status of a request
1554 * \param mti [IN] context
1555 * \param pgs [IN] progress of the copy tool
1557 * \retval -ve failure
1559 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1560 struct hsm_progress_kernel *pgs)
1562 struct mdt_device *mdt = mti->mti_mdt;
1563 struct coordinator *cdt = &mdt->mdt_coordinator;
1564 struct cdt_agent_req *car;
1568 /* no coordinator started, so we cannot serve requests */
1569 if (cdt->cdt_state == CDT_STOPPED)
1572 /* first do sanity checks */
1573 car = mdt_cdt_update_request(cdt, pgs);
1575 CERROR("%s: Cannot find running request for cookie %#llx"
1578 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1580 RETURN(PTR_ERR(car));
1583 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1584 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1585 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1586 hsm_copytool_action2name(car->car_hai->hai_action),
1587 pgs->hpk_flags, pgs->hpk_errval,
1588 PFID(&car->car_hai->hai_fid),
1589 PFID(&car->car_hai->hai_dfid));
1591 /* progress is done on FID or data FID depending of the action and
1592 * of the copy progress */
1593 /* for restore progress is used to send back the data FID to cdt */
1594 if (car->car_hai->hai_action == HSMA_RESTORE &&
1595 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1596 car->car_hai->hai_dfid = pgs->hpk_fid;
1598 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1599 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1600 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1601 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1602 CERROR("%s: Progress on "DFID" for cookie %#llx"
1603 " does not match request FID "DFID" nor data FID "
1606 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1607 PFID(&car->car_hai->hai_fid),
1608 PFID(&car->car_hai->hai_dfid));
1609 GOTO(out, rc = -EINVAL);
1612 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1613 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1614 " is not coherent (err=%d and not completed"
1617 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1618 hsm_copytool_action2name(car->car_hai->hai_action),
1619 pgs->hpk_errval, pgs->hpk_flags);
1620 GOTO(out, rc = -EINVAL);
1623 /* now progress is valid */
1625 /* we use a root like ucred */
1626 hsm_init_ucred(mdt_ucred(mti));
1628 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1629 enum agent_req_status status;
1630 struct hsm_record_update update;
1633 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1635 CDEBUG(D_HSM, "updating record: fid="DFID" cookie=%#llx action=%s "
1637 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1638 hsm_copytool_action2name(car->car_hai->hai_action),
1639 agent_req_status2name(status));
1641 /* update record first (LU-9075) */
1642 update.cookie = pgs->hpk_cookie;
1643 update.status = status;
1645 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1648 CERROR("%s: mdt_agent_record_update() failed,"
1649 " rc=%d, cannot update status to %s"
1650 " for cookie %#llx\n",
1651 mdt_obd_name(mdt), rc1,
1652 agent_req_status2name(status),
1654 rc = (rc != 0 ? rc : rc1);
1656 /* then remove request from memory list (LU-9075) */
1657 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1659 /* ct has completed a request, so a slot is available,
1660 * signal the coordinator to find new work */
1661 mdt_hsm_cdt_event(cdt);
1663 /* if copytool send a progress on a canceled request
1664 * we inform copytool it should stop
1666 if (car->car_canceled == 1)
1672 /* remove ref got from mdt_cdt_update_request() */
1673 mdt_cdt_put_request(car);
1680 * data passed to llog_cat_process() callback
1681 * to cancel requests
1683 struct hsm_cancel_all_data {
1684 struct mdt_device *mdt;
1688 * llog_cat_process() callback, used to:
1689 * - purge all requests
1690 * \param env [IN] environment
1691 * \param llh [IN] llog handle
1692 * \param hdr [IN] llog record
1693 * \param data [IN] cb data = struct hsm_cancel_all_data
1695 * \retval -ve failure
1697 static int mdt_cancel_all_cb(const struct lu_env *env,
1698 struct llog_handle *llh,
1699 struct llog_rec_hdr *hdr, void *data)
1701 struct llog_agent_req_rec *larr;
1702 struct hsm_cancel_all_data *hcad;
1706 larr = (struct llog_agent_req_rec *)hdr;
1708 if (larr->arr_status == ARS_WAITING ||
1709 larr->arr_status == ARS_STARTED) {
1710 larr->arr_status = ARS_CANCELED;
1711 larr->arr_req_change = ktime_get_real_seconds();
1712 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1719 * cancel all actions
1720 * \param obd [IN] MDT device
1722 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1725 struct lu_context session;
1726 struct mdt_thread_info *mti;
1727 struct coordinator *cdt = &mdt->mdt_coordinator;
1728 struct cdt_agent_req *car;
1729 struct hsm_action_list *hal = NULL;
1730 struct hsm_action_item *hai;
1731 struct hsm_cancel_all_data hcad;
1732 int hal_sz = 0, hal_len, rc;
1733 enum cdt_states old_state;
1736 rc = lu_env_init(&env, LCT_MD_THREAD);
1740 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1741 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1745 lu_context_enter(&session);
1746 env.le_ses = &session;
1748 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1749 LASSERT(mti != NULL);
1751 mti->mti_env = &env;
1754 hsm_init_ucred(mdt_ucred(mti));
1756 mutex_lock(&cdt->cdt_state_lock);
1757 old_state = cdt->cdt_state;
1759 /* disable coordinator */
1760 rc = set_cdt_state_locked(cdt, CDT_DISABLE);
1762 GOTO(out_cdt_state_unlock, rc);
1764 /* send cancel to all running requests */
1765 down_read(&cdt->cdt_request_lock);
1766 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1767 mdt_cdt_get_request(car);
1768 /* request is not yet removed from list, it will be done
1769 * when copytool will return progress
1772 if (car->car_hai->hai_action == HSMA_CANCEL) {
1773 mdt_cdt_put_request(car);
1778 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1779 cfs_size_round(car->car_hai->hai_len);
1781 if (hal_len > hal_sz && hal_sz > 0) {
1782 /* not enough room, free old buffer */
1783 OBD_FREE(hal, hal_sz);
1787 /* empty buffer, allocate one */
1790 OBD_ALLOC(hal, hal_sz);
1792 mdt_cdt_put_request(car);
1793 up_read(&cdt->cdt_request_lock);
1794 GOTO(out_cdt_state, rc = -ENOMEM);
1798 hal->hal_version = HAL_VERSION;
1799 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1801 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1802 hal->hal_archive_id = car->car_archive_id;
1803 hal->hal_flags = car->car_flags;
1806 hai = hai_first(hal);
1807 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1808 hai->hai_action = HSMA_CANCEL;
1811 /* it is possible to safely call mdt_hsm_agent_send()
1812 * (ie without a deadlock on cdt_request_lock), because the
1813 * write lock is taken only if we are not in purge mode
1814 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1815 * nor mdt_cdt_remove_request())
1817 /* no conflict with cdt thread because cdt is disable and we
1818 * have the request lock */
1819 mdt_hsm_agent_send(mti, hal, 1);
1821 mdt_cdt_put_request(car);
1823 up_read(&cdt->cdt_request_lock);
1826 OBD_FREE(hal, hal_sz);
1828 /* cancel all on-disk records */
1831 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1832 &hcad, 0, 0, WRITE);
1834 /* Enable coordinator, unless the coordinator was stopping. */
1835 set_cdt_state_locked(cdt, old_state);
1836 out_cdt_state_unlock:
1837 mutex_unlock(&cdt->cdt_state_lock);
1839 lu_context_exit(&session);
1840 lu_context_fini(&session);
1848 * check if a request is compatible with file status
1849 * \param hai [IN] request description
1850 * \param archive_id [IN] request archive id
1851 * \param rq_flags [IN] request flags
1852 * \param hsm [IN] file HSM metadata
1855 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1856 u32 archive_id, u64 rq_flags,
1857 const struct md_hsm *hsm)
1859 int is_compat = false;
1863 hsm_flags = hsm->mh_flags;
1864 switch (hai->hai_action) {
1866 if (!(hsm_flags & HS_NOARCHIVE) &&
1867 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1870 if (hsm_flags & HS_EXISTS &&
1872 archive_id != hsm->mh_arch_id)
1877 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1878 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1882 if (!(hsm_flags & HS_RELEASED) &&
1883 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1890 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1891 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1892 PFID(&hai->hai_fid),
1893 hsm_copytool_action2name(hai->hai_action), rq_flags,
1894 hai->hai_extent.offset, hai->hai_extent.length,
1896 (is_compat ? "compatible" : "uncompatible"));
1902 * sysfs interface used to get/set HSM behaviour (cdt->cdt_policy)
1904 static const struct {
1908 } hsm_policy_names[] = {
1909 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1910 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1915 * convert a policy name to a bit
1916 * \param name [IN] policy name
1918 * \retval policy bit
1920 static __u64 hsm_policy_str2bit(const char *name)
1924 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1925 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1926 strcmp(hsm_policy_names[i].name, name) == 0)
1927 return hsm_policy_names[i].bit;
1932 * convert a policy bit field to a string
1933 * \param mask [IN] policy bit field
1934 * \param hexa [IN] print mask before bit names
1935 * \param buffer [OUT] string
1936 * \param count [IN] size of buffer
1938 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1946 seq_printf(m, "(%#llx) ", mask);
1948 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1951 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1952 if (hsm_policy_names[j].bit == bit)
1956 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1958 seq_printf(m, "%s ", hsm_policy_names[j].name);
1960 /* remove last ' ' */
1965 /* methods to read/write HSM policy flags */
1966 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1968 struct mdt_device *mdt = m->private;
1969 struct coordinator *cdt = &mdt->mdt_coordinator;
1972 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1977 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1978 size_t count, loff_t *off)
1980 struct seq_file *m = file->private_data;
1981 struct mdt_device *mdt = m->private;
1982 struct coordinator *cdt = &mdt->mdt_coordinator;
1983 char *start, *token, sign;
1986 __u64 add_mask, remove_mask, set_mask;
1990 if (count + 1 > PAGE_SIZE)
1993 OBD_ALLOC(buf, count + 1);
1997 if (copy_from_user(buf, buffer, count))
1998 GOTO(out, rc = -EFAULT);
2003 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
2006 add_mask = remove_mask = set_mask = 0;
2008 token = strsep(&start, "\n ");
2014 if (sign == '-' || sign == '+')
2017 policy = hsm_policy_str2bit(token);
2019 CWARN("%s: '%s' is unknown, "
2020 "supported policies are:\n", mdt_obd_name(mdt),
2022 hsm_policy_bit2str(m, 0, false);
2023 GOTO(out, rc = -EINVAL);
2027 remove_mask |= policy;
2037 } while (start != NULL);
2039 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
2040 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
2042 /* if no sign in all string, it is a clear and set
2043 * if some sign found, all unsigned are converted
2045 * P1 P2 = set to P1 and P2
2046 * P1 -P2 = add P1 clear P2 same as +P1 -P2
2048 if (remove_mask == 0 && add_mask == 0) {
2049 cdt->cdt_policy = set_mask;
2051 cdt->cdt_policy |= set_mask | add_mask;
2052 cdt->cdt_policy &= ~remove_mask;
2055 GOTO(out, rc = count);
2058 OBD_FREE(buf, count + 1);
2061 LDEBUGFS_SEQ_FOPS(mdt_hsm_policy);
2063 ssize_t loop_period_show(struct kobject *kobj, struct attribute *attr,
2066 struct coordinator *cdt = container_of(kobj, struct coordinator,
2069 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_loop_period);
2072 ssize_t loop_period_store(struct kobject *kobj, struct attribute *attr,
2073 const char *buffer, size_t count)
2075 struct coordinator *cdt = container_of(kobj, struct coordinator,
2080 rc = kstrtouint(buffer, 0, &val);
2085 cdt->cdt_loop_period = val;
2087 return val ? count : -EINVAL;
2089 LUSTRE_RW_ATTR(loop_period);
2091 ssize_t grace_delay_show(struct kobject *kobj, struct attribute *attr,
2094 struct coordinator *cdt = container_of(kobj, struct coordinator,
2097 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_grace_delay);
2100 ssize_t grace_delay_store(struct kobject *kobj, struct attribute *attr,
2101 const char *buffer, size_t count)
2103 struct coordinator *cdt = container_of(kobj, struct coordinator,
2108 rc = kstrtouint(buffer, 0, &val);
2113 cdt->cdt_grace_delay = val;
2115 return val ? count : -EINVAL;
2117 LUSTRE_RW_ATTR(grace_delay);
2119 ssize_t active_request_timeout_show(struct kobject *kobj,
2120 struct attribute *attr,
2123 struct coordinator *cdt = container_of(kobj, struct coordinator,
2126 return scnprintf(buf, PAGE_SIZE, "%d\n", cdt->cdt_active_req_timeout);
2129 ssize_t active_request_timeout_store(struct kobject *kobj,
2130 struct attribute *attr,
2131 const char *buffer, size_t count)
2133 struct coordinator *cdt = container_of(kobj, struct coordinator,
2138 rc = kstrtouint(buffer, 0, &val);
2143 cdt->cdt_active_req_timeout = val;
2145 return val ? count : -EINVAL;
2147 LUSTRE_RW_ATTR(active_request_timeout);
2149 ssize_t max_requests_show(struct kobject *kobj, struct attribute *attr,
2152 struct coordinator *cdt = container_of(kobj, struct coordinator,
2155 return scnprintf(buf, PAGE_SIZE, "%llu\n", cdt->cdt_max_requests);
2158 ssize_t max_requests_store(struct kobject *kobj, struct attribute *attr,
2159 const char *buffer, size_t count)
2161 struct coordinator *cdt = container_of(kobj, struct coordinator,
2163 unsigned long long val;
2166 rc = kstrtoull(buffer, 0, &val);
2171 cdt->cdt_max_requests = val;
2173 return val ? count : -EINVAL;
2175 LUSTRE_RW_ATTR(max_requests);
2177 ssize_t default_archive_id_show(struct kobject *kobj, struct attribute *attr,
2180 struct coordinator *cdt = container_of(kobj, struct coordinator,
2183 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_default_archive_id);
2186 ssize_t default_archive_id_store(struct kobject *kobj, struct attribute *attr,
2187 const char *buffer, size_t count)
2189 struct coordinator *cdt = container_of(kobj, struct coordinator,
2194 rc = kstrtouint(buffer, 0, &val);
2199 cdt->cdt_default_archive_id = val;
2201 return val ? count : -EINVAL;
2203 LUSTRE_RW_ATTR(default_archive_id);
2206 * procfs write method for MDT/hsm_control
2207 * proc entry is in mdt directory so data is mdt obd_device pointer
2209 #define CDT_ENABLE_CMD "enabled"
2210 #define CDT_STOP_CMD "shutdown"
2211 #define CDT_DISABLE_CMD "disabled"
2212 #define CDT_PURGE_CMD "purge"
2213 #define CDT_HELP_CMD "help"
2214 #define CDT_MAX_CMD_LEN 10
2216 ssize_t hsm_control_store(struct kobject *kobj, struct attribute *attr,
2217 const char *buffer, size_t count)
2219 struct obd_device *obd = container_of(kobj, struct obd_device,
2221 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2222 struct coordinator *cdt = &(mdt->mdt_coordinator);
2226 if (count == 0 || count >= CDT_MAX_CMD_LEN)
2229 if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
2230 if (cdt->cdt_state == CDT_DISABLE) {
2231 rc = set_cdt_state(cdt, CDT_RUNNING);
2232 mdt_hsm_cdt_event(cdt);
2233 wake_up(&cdt->cdt_waitq);
2235 rc = mdt_hsm_cdt_start(mdt);
2237 } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
2238 if ((cdt->cdt_state == CDT_STOPPING) ||
2239 (cdt->cdt_state == CDT_STOPPED)) {
2240 CERROR("%s: Coordinator already stopped\n",
2244 rc = mdt_hsm_cdt_stop(mdt);
2246 } else if (strncmp(buffer, CDT_DISABLE_CMD,
2247 strlen(CDT_DISABLE_CMD)) == 0) {
2248 if ((cdt->cdt_state == CDT_STOPPING) ||
2249 (cdt->cdt_state == CDT_STOPPED)) {
2250 CERROR("%s: Coordinator is stopped\n",
2254 rc = set_cdt_state(cdt, CDT_DISABLE);
2256 } else if (strncmp(buffer, CDT_PURGE_CMD,
2257 strlen(CDT_PURGE_CMD)) == 0) {
2258 rc = hsm_cancel_all_actions(mdt);
2259 } else if (strncmp(buffer, CDT_HELP_CMD,
2260 strlen(CDT_HELP_CMD)) == 0) {
2268 CERROR("%s: Valid coordinator control commands are: "
2269 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2270 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2271 CDT_PURGE_CMD, CDT_HELP_CMD);
2279 ssize_t hsm_control_show(struct kobject *kobj, struct attribute *attr,
2282 struct obd_device *obd = container_of(kobj, struct obd_device,
2284 struct coordinator *cdt;
2286 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2288 return scnprintf(buf, PAGE_SIZE, "%s\n",
2289 cdt_mdt_state2str(cdt->cdt_state));
2293 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2299 for (i = 0; i < 8 * sizeof(mask); i++) {
2300 if (mask & (1UL << i)) {
2301 seq_printf(m, "%s%s", first ? "" : " ",
2302 hsm_copytool_action2name(i));
2312 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2314 struct mdt_device *mdt = m->private;
2315 struct coordinator *cdt = &mdt->mdt_coordinator;
2317 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2321 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2323 struct mdt_device *mdt = m->private;
2324 struct coordinator *cdt = &mdt->mdt_coordinator;
2326 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2330 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2332 struct mdt_device *mdt = m->private;
2333 struct coordinator *cdt = &mdt->mdt_coordinator;
2335 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2338 static inline enum hsm_copytool_action
2339 hsm_copytool_name2action(const char *name)
2341 if (strcasecmp(name, "NOOP") == 0)
2343 else if (strcasecmp(name, "ARCHIVE") == 0)
2344 return HSMA_ARCHIVE;
2345 else if (strcasecmp(name, "RESTORE") == 0)
2346 return HSMA_RESTORE;
2347 else if (strcasecmp(name, "REMOVE") == 0)
2349 else if (strcasecmp(name, "CANCEL") == 0)
2356 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2357 size_t user_count, __u64 *mask)
2359 char *buf, *pos, *name;
2365 if (!(user_count < 4096))
2368 buf_size = user_count + 1;
2370 OBD_ALLOC(buf, buf_size);
2374 if (copy_from_user(buf, user_buf, buf_size - 1))
2375 GOTO(out, rc = -EFAULT);
2377 buf[buf_size - 1] = '\0';
2380 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2386 action = hsm_copytool_name2action(name);
2388 GOTO(out, rc = -EINVAL);
2390 new_mask |= (1UL << action);
2396 OBD_FREE(buf, buf_size);
2402 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2403 size_t count, loff_t *off)
2405 struct seq_file *m = file->private_data;
2406 struct mdt_device *mdt = m->private;
2407 struct coordinator *cdt = &mdt->mdt_coordinator;
2409 return mdt_write_hsm_request_mask(file, buf, count,
2410 &cdt->cdt_user_request_mask);
2414 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2415 size_t count, loff_t *off)
2417 struct seq_file *m = file->private_data;
2418 struct mdt_device *mdt = m->private;
2419 struct coordinator *cdt = &mdt->mdt_coordinator;
2421 return mdt_write_hsm_request_mask(file, buf, count,
2422 &cdt->cdt_group_request_mask);
2426 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2427 size_t count, loff_t *off)
2429 struct seq_file *m = file->private_data;
2430 struct mdt_device *mdt = m->private;
2431 struct coordinator *cdt = &mdt->mdt_coordinator;
2433 return mdt_write_hsm_request_mask(file, buf, count,
2434 &cdt->cdt_other_request_mask);
2437 static ssize_t remove_archive_on_last_unlink_show(struct kobject *kobj,
2438 struct attribute *attr,
2441 struct coordinator *cdt = container_of(kobj, struct coordinator,
2444 return scnprintf(buf, PAGE_SIZE, "%u\n",
2445 cdt->cdt_remove_archive_on_last_unlink);
2448 static ssize_t remove_archive_on_last_unlink_store(struct kobject *kobj,
2449 struct attribute *attr,
2453 struct coordinator *cdt = container_of(kobj, struct coordinator,
2458 rc = kstrtobool(buffer, &val);
2462 cdt->cdt_remove_archive_on_last_unlink = val;
2465 LUSTRE_RW_ATTR(remove_archive_on_last_unlink);
2467 LDEBUGFS_SEQ_FOPS(mdt_hsm_user_request_mask);
2468 LDEBUGFS_SEQ_FOPS(mdt_hsm_group_request_mask);
2469 LDEBUGFS_SEQ_FOPS(mdt_hsm_other_request_mask);
2471 /* Read-only sysfs files for request counters */
2472 static ssize_t archive_count_show(struct kobject *kobj, struct attribute *attr,
2475 struct coordinator *cdt = container_of(kobj, struct coordinator,
2478 return scnprintf(buf, PAGE_SIZE, "%d\n",
2479 atomic_read(&cdt->cdt_archive_count));
2481 LUSTRE_RO_ATTR(archive_count);
2483 static ssize_t restore_count_show(struct kobject *kobj, struct attribute *attr,
2486 struct coordinator *cdt = container_of(kobj, struct coordinator,
2489 return scnprintf(buf, PAGE_SIZE, "%d\n",
2490 atomic_read(&cdt->cdt_restore_count));
2492 LUSTRE_RO_ATTR(restore_count);
2494 static ssize_t remove_count_show(struct kobject *kobj, struct attribute *attr,
2497 struct coordinator *cdt = container_of(kobj, struct coordinator,
2500 return scnprintf(buf, PAGE_SIZE, "%d\n",
2501 atomic_read(&cdt->cdt_remove_count));
2503 LUSTRE_RO_ATTR(remove_count);
2505 static struct ldebugfs_vars ldebugfs_mdt_hsm_vars[] = {
2507 .fops = &mdt_hsm_agent_fops },
2508 { .name = "actions",
2509 .fops = &mdt_hsm_actions_fops,
2510 .proc_mode = 0444 },
2512 .fops = &mdt_hsm_policy_fops },
2513 { .name = "active_requests",
2514 .fops = &mdt_hsm_active_requests_fops },
2515 { .name = "user_request_mask",
2516 .fops = &mdt_hsm_user_request_mask_fops, },
2517 { .name = "group_request_mask",
2518 .fops = &mdt_hsm_group_request_mask_fops, },
2519 { .name = "other_request_mask",
2520 .fops = &mdt_hsm_other_request_mask_fops, },
2524 static struct attribute *hsm_attrs[] = {
2525 &lustre_attr_loop_period.attr,
2526 &lustre_attr_grace_delay.attr,
2527 &lustre_attr_active_request_timeout.attr,
2528 &lustre_attr_max_requests.attr,
2529 &lustre_attr_default_archive_id.attr,
2530 &lustre_attr_remove_archive_on_last_unlink.attr,
2531 &lustre_attr_archive_count.attr,
2532 &lustre_attr_restore_count.attr,
2533 &lustre_attr_remove_count.attr,
2537 static void hsm_kobj_release(struct kobject *kobj)
2539 struct coordinator *cdt = container_of(kobj, struct coordinator,
2542 debugfs_remove_recursive(cdt->cdt_debugfs_dir);
2543 cdt->cdt_debugfs_dir = NULL;
2545 complete(&cdt->cdt_kobj_unregister);
2548 static struct kobj_type hsm_ktype = {
2549 .default_attrs = hsm_attrs,
2550 .sysfs_ops = &lustre_sysfs_ops,
2551 .release = hsm_kobj_release,
2555 * create sysfs entries for coordinator
2558 * \retval -ve failure
2560 int hsm_cdt_tunables_init(struct mdt_device *mdt)
2562 struct coordinator *cdt = &mdt->mdt_coordinator;
2563 struct obd_device *obd = mdt2obd_dev(mdt);
2566 init_completion(&cdt->cdt_kobj_unregister);
2567 rc = kobject_init_and_add(&cdt->cdt_hsm_kobj, &hsm_ktype,
2568 &obd->obd_kset.kobj, "%s", "hsm");
2570 kobject_put(&cdt->cdt_hsm_kobj);
2574 /* init debugfs entries, failure is not critical */
2575 cdt->cdt_debugfs_dir = debugfs_create_dir("hsm",
2576 obd->obd_debugfs_entry);
2577 ldebugfs_add_vars(cdt->cdt_debugfs_dir, ldebugfs_mdt_hsm_vars, mdt);
2583 * remove sysfs entries for coordinator
2587 void hsm_cdt_tunables_fini(struct mdt_device *mdt)
2589 struct coordinator *cdt = &mdt->mdt_coordinator;
2591 kobject_put(&cdt->cdt_hsm_kobj);
2592 wait_for_completion(&cdt->cdt_kobj_unregister);