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 <linux/kernel.h>
43 #include <obd_support.h>
44 #include <lustre_export.h>
46 #include <lprocfs_status.h>
47 #include <lustre_log.h>
48 #include <lustre_kernelcomm.h>
49 #include "mdt_internal.h"
52 * get obj and HSM attributes on a fid
53 * \param mti [IN] context
54 * \param fid [IN] object fid
55 * \param hsm [OUT] HSM meta data
56 * \retval obj or error (-ENOENT if not found)
58 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
59 const struct lu_fid *fid,
63 struct mdt_object *obj;
71 /* find object by FID */
72 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
76 if (!mdt_object_exists(obj)) {
78 mdt_object_put(mti->mti_env, obj);
79 RETURN(ERR_PTR(-ENOENT));
82 rc = mdt_attr_get_complex(mti, obj, ma);
84 mdt_object_put(mti->mti_env, obj);
88 if (ma->ma_valid & MA_HSM)
91 memset(hsm, 0, sizeof(*hsm));
96 void mdt_hsm_dump_hal(int level, const char *prefix,
97 struct hsm_action_list *hal)
100 struct hsm_action_item *hai;
103 CDEBUG(level, "%s: HAL header: version %X count %d"
104 " archive_id %d flags %#llx\n",
105 prefix, hal->hal_version, hal->hal_count,
106 hal->hal_archive_id, hal->hal_flags);
108 hai = hai_first(hal);
109 for (i = 0; i < hal->hal_count; i++) {
110 sz = hai->hai_len - sizeof(*hai);
111 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
113 " action=%s extent=%#llx-%#llx gid=%#llx"
114 " datalen=%d data=[%s]\n",
116 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
118 hsm_copytool_action2name(hai->hai_action),
119 hai->hai_extent.offset,
120 hai->hai_extent.length,
122 hai_dump_data_field(hai, buf, sizeof(buf)));
128 * data passed to llog_cat_process() callback
129 * to scan requests and take actions
131 struct hsm_scan_request {
134 struct hsm_action_list *hal;
137 struct hsm_scan_data {
138 struct mdt_thread_info *hsd_mti;
139 char hsd_fsname[MTI_NAME_MAXLEN + 1];
140 /* are we scanning the logs for housekeeping, or just looking
143 bool hsd_housekeeping;
144 bool hsd_one_restore;
145 u32 hsd_start_cat_idx;
146 u32 hsd_start_rec_idx;
147 int hsd_action_count;
148 int hsd_request_len; /* array alloc len */
149 int hsd_request_count; /* array used count */
150 struct hsm_scan_request *hsd_request;
153 static int mdt_cdt_waiting_cb(const struct lu_env *env,
154 struct mdt_device *mdt,
155 struct llog_handle *llh,
156 struct llog_agent_req_rec *larr,
157 struct hsm_scan_data *hsd)
159 struct coordinator *cdt = &mdt->mdt_coordinator;
160 struct hsm_scan_request *request;
161 struct hsm_action_item *hai;
167 /* Are agents full? */
168 if (atomic_read(&cdt->cdt_request_count) >= cdt->cdt_max_requests)
169 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
171 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
172 cdt->cdt_max_requests) {
173 /* We cannot send any more request
175 * *** SPECIAL CASE ***
177 * Restore requests are too important not to schedule at least
178 * one, everytime we can.
180 if (larr->arr_hai.hai_action != HSMA_RESTORE ||
181 hsd->hsd_one_restore)
182 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
185 hai_size = round_up(larr->arr_hai.hai_len, 8);
186 archive_id = larr->arr_archive_id;
188 /* Can we add this action to one of the existing HALs in hsd. */
190 for (i = 0; i < hsd->hsd_request_count; i++) {
191 if (hsd->hsd_request[i].hal->hal_archive_id == archive_id &&
192 hsd->hsd_request[i].hal_used_sz + hai_size <=
194 request = &hsd->hsd_request[i];
199 /* Are we trying to force-schedule a request? */
200 if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
201 cdt->cdt_max_requests) {
202 /* Is there really no compatible hsm_scan_request? */
204 for (i -= 1; i >= 0; i--) {
205 if (hsd->hsd_request[i].hal->hal_archive_id ==
207 request = &hsd->hsd_request[i];
213 /* Make room for the hai */
215 /* Discard the last hai until there is enough space */
217 request->hal->hal_count--;
219 hai = hai_first(request->hal);
220 for (i = 0; i < request->hal->hal_count; i++)
222 request->hal_used_sz -=
223 round_up(hai->hai_len, 8);
224 hsd->hsd_action_count--;
225 } while (request->hal_used_sz + hai_size >
227 } else if (hsd->hsd_housekeeping) {
228 struct hsm_scan_request *tmp;
230 /* Discard the (whole) last hal */
231 hsd->hsd_request_count--;
232 LASSERT(hsd->hsd_request_count >= 0);
233 tmp = &hsd->hsd_request[hsd->hsd_request_count];
234 hsd->hsd_action_count -= tmp->hal->hal_count;
235 LASSERT(hsd->hsd_action_count >= 0);
236 OBD_FREE(tmp->hal, tmp->hal_sz);
238 /* Bailing out, this code path is too hot */
239 RETURN(LLOG_PROC_BREAK);
245 size_t count = round_up(MTI_NAME_MAXLEN + 1, 8) + 2 * hai_size;
246 struct hsm_action_list *hal;
248 LASSERT(hsd->hsd_request_count < hsd->hsd_request_len);
249 request = &hsd->hsd_request[hsd->hsd_request_count];
251 /* allocates hai vector size just needs to be large
253 request->hal_sz = sizeof(*request->hal) + count;
254 OBD_ALLOC_LARGE(hal, request->hal_sz);
258 hal->hal_version = HAL_VERSION;
259 strlcpy(hal->hal_fsname, hsd->hsd_fsname, MTI_NAME_MAXLEN + 1);
260 hal->hal_archive_id = larr->arr_archive_id;
261 hal->hal_flags = larr->arr_flags;
263 request->hal_used_sz = hal_size(hal);
265 hsd->hsd_request_count++;
266 } else if (request->hal_sz < request->hal_used_sz + hai_size) {
267 /* Not enough room, need an extension */
271 sz = min_t(int, 2 * request->hal_sz, LDLM_MAXREQSIZE);
272 LASSERT(request->hal_used_sz + hai_size < sz);
274 OBD_ALLOC_LARGE(hal_buffer, sz);
278 memcpy(hal_buffer, request->hal, request->hal_used_sz);
279 OBD_FREE_LARGE(request->hal, request->hal_sz);
280 request->hal = hal_buffer;
281 request->hal_sz = sz;
284 hai = hai_first(request->hal);
285 for (i = 0; i < request->hal->hal_count; i++)
288 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
290 request->hal_used_sz += hai_size;
291 request->hal->hal_count++;
293 hsd->hsd_action_count++;
295 switch (hai->hai_action) {
299 hsd->hsd_one_restore = true;
302 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
303 llh->lgh_hdr->llh_cat_idx,
304 larr->arr_hdr.lrh_index);
307 wrapped = llh->lgh_hdr->llh_cat_idx >= llh->lgh_last_idx &&
308 llh->lgh_hdr->llh_count > 1;
309 if ((!wrapped && llh->lgh_hdr->llh_cat_idx > hsd->hsd_start_cat_idx) ||
310 (wrapped && llh->lgh_hdr->llh_cat_idx < hsd->hsd_start_cat_idx) ||
311 (llh->lgh_hdr->llh_cat_idx == hsd->hsd_start_cat_idx &&
312 larr->arr_hdr.lrh_index > hsd->hsd_start_rec_idx)) {
313 hsd->hsd_start_cat_idx = llh->lgh_hdr->llh_cat_idx;
314 hsd->hsd_start_rec_idx = larr->arr_hdr.lrh_index;
320 static int mdt_cdt_started_cb(const struct lu_env *env,
321 struct mdt_device *mdt,
322 struct llog_handle *llh,
323 struct llog_agent_req_rec *larr,
324 struct hsm_scan_data *hsd)
326 struct coordinator *cdt = &mdt->mdt_coordinator;
327 struct hsm_action_item *hai = &larr->arr_hai;
328 struct cdt_agent_req *car;
329 time64_t now = ktime_get_real_seconds();
331 enum changelog_rec_flags clf_flags;
334 if (!hsd->hsd_housekeeping)
337 /* we search for a running request
338 * error may happen if coordinator crashes or stopped
339 * with running request
341 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
343 last = larr->arr_req_change;
345 last = car->car_req_update;
348 /* test if request too long, if yes cancel it
349 * the same way the copy tool acknowledge a cancel request */
350 if (now <= last + cdt->cdt_active_req_timeout)
351 GOTO(out_car, rc = 0);
353 dump_llog_agent_req_rec("request timed out, start cleaning", larr);
356 car->car_req_update = now;
357 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0, &car->car_uuid);
358 /* Remove car from memory list (LU-9075) */
359 mdt_cdt_remove_request(cdt, hai->hai_cookie);
362 /* Emit a changelog record for the failed action.*/
364 hsm_set_cl_error(&clf_flags, ECANCELED);
366 switch (hai->hai_action) {
368 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
371 hsm_set_cl_event(&clf_flags, HE_RESTORE);
374 hsm_set_cl_event(&clf_flags, HE_REMOVE);
377 hsm_set_cl_event(&clf_flags, HE_CANCEL);
380 /* Unknown record type, skip changelog. */
386 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
389 if (hai->hai_action == HSMA_RESTORE)
390 cdt_restore_handle_del(hsd->hsd_mti, cdt, &hai->hai_fid);
392 larr->arr_status = ARS_CANCELED;
393 larr->arr_req_change = now;
394 rc = llog_write(hsd->hsd_mti->mti_env, llh, &larr->arr_hdr,
395 larr->arr_hdr.lrh_index);
397 CERROR("%s: cannot update agent log: rc = %d\n",
398 mdt_obd_name(mdt), rc);
399 rc = LLOG_DEL_RECORD;
402 /* ct has completed a request, so a slot is available,
403 * signal the coordinator to find new work */
404 mdt_hsm_cdt_event(cdt);
407 mdt_cdt_put_request(car);
413 * llog_cat_process() callback, used to:
414 * - find waiting request and start action
415 * - purge canceled and done requests
416 * \param env [IN] environment
417 * \param llh [IN] llog handle
418 * \param hdr [IN] llog record
419 * \param data [IN/OUT] cb data = struct hsm_scan_data
421 * \retval -ve failure
423 static int mdt_coordinator_cb(const struct lu_env *env,
424 struct llog_handle *llh,
425 struct llog_rec_hdr *hdr,
428 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
429 struct hsm_scan_data *hsd = data;
430 struct mdt_device *mdt = hsd->hsd_mti->mti_mdt;
431 struct coordinator *cdt = &mdt->mdt_coordinator;
434 larr = (struct llog_agent_req_rec *)hdr;
435 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
436 switch (larr->arr_status) {
438 RETURN(mdt_cdt_waiting_cb(env, mdt, llh, larr, hsd));
440 RETURN(mdt_cdt_started_cb(env, mdt, llh, larr, hsd));
442 if (!hsd->hsd_housekeeping)
445 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
446 ktime_get_real_seconds()) {
447 cdt_agent_record_hash_del(cdt,
448 larr->arr_hai.hai_cookie);
449 RETURN(LLOG_DEL_RECORD);
456 /* Release the ressource used by the coordinator. Called when the
457 * coordinator is stopping. */
458 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
460 struct coordinator *cdt = &mdt->mdt_coordinator;
461 struct cdt_agent_req *car, *tmp1;
462 struct hsm_agent *ha, *tmp2;
463 struct cdt_restore_handle *crh, *tmp3;
464 struct mdt_thread_info *cdt_mti;
467 down_write(&cdt->cdt_request_lock);
468 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
470 cfs_hash_del(cdt->cdt_request_cookie_hash,
471 &car->car_hai->hai_cookie,
472 &car->car_cookie_hash);
473 list_del(&car->car_request_list);
474 mdt_cdt_put_request(car);
476 up_write(&cdt->cdt_request_lock);
478 down_write(&cdt->cdt_agent_lock);
479 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
480 list_del(&ha->ha_list);
481 if (ha->ha_archive_cnt != 0)
482 OBD_FREE_PTR_ARRAY(ha->ha_archive_id,
486 up_write(&cdt->cdt_agent_lock);
488 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
489 mutex_lock(&cdt->cdt_restore_lock);
490 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_handle_list,
492 /* not locked yet, cleanup by cdt_restore_handle_add() */
493 if (crh->crh_lh.mlh_type == MDT_NUL_LOCK)
495 list_del(&crh->crh_list);
496 /* give back layout lock */
497 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
498 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
500 mutex_unlock(&cdt->cdt_restore_lock);
504 * Coordinator state transition table, indexed on enum cdt_states, taking
505 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
506 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
508 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
509 /* from -> to: stopped init running disable stopping */
510 /* stopped */ { true, true, false, false, false },
511 /* init */ { true, false, true, false, false },
512 /* running */ { false, false, true, true, true },
513 /* disable */ { false, false, true, true, true },
514 /* stopping */ { true, false, false, false, false }
518 * Change coordinator thread state
519 * Some combinations are not valid, so catch them here.
521 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
522 * the transition was not possible.
524 static int set_cdt_state_locked(struct coordinator *cdt,
525 enum cdt_states new_state)
528 enum cdt_states state;
530 state = cdt->cdt_state;
532 if (cdt_transition[state][new_state]) {
533 cdt->cdt_state = new_state;
537 "unexpected coordinator transition, from=%s, to=%s\n",
538 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
545 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state)
549 mutex_lock(&cdt->cdt_state_lock);
550 rc = set_cdt_state_locked(cdt, new_state);
551 mutex_unlock(&cdt->cdt_state_lock);
556 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti);
558 static void cdt_start_pending_restore(struct mdt_device *mdt,
559 struct coordinator *cdt)
561 struct mdt_thread_info *cdt_mti;
565 /* wait until MDD initialize hsm actions llog */
566 while (!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state) && i < obd_timeout) {
567 schedule_timeout_interruptible(cfs_time_seconds(1));
570 if (!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state))
571 CWARN("%s: trying to init HSM before MDD\n", mdt_obd_name(mdt));
573 /* set up list of started restore requests */
574 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
575 rc = mdt_hsm_pending_restore(cdt_mti);
577 CERROR("%s: cannot take the layout locks needed for registered restore: %d\n",
578 mdt_obd_name(mdt), rc);
584 * \param data [IN] obd device
586 * \retval -ve failure
588 static int mdt_coordinator(void *data)
590 struct mdt_thread_info *mti = data;
591 struct mdt_device *mdt = mti->mti_mdt;
592 struct coordinator *cdt = &mdt->mdt_coordinator;
593 struct hsm_scan_data hsd = { NULL };
594 time64_t last_housekeeping = 0;
595 size_t request_sz = 0;
599 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
600 mdt_obd_name(mdt), current->pid);
603 obd_uuid2fsname(hsd.hsd_fsname, mdt_obd_name(mdt),
604 sizeof(hsd.hsd_fsname));
606 set_cdt_state(cdt, CDT_RUNNING);
608 /* Inform mdt_hsm_cdt_start(). */
609 wake_up(&cdt->cdt_waitq);
610 cdt_start_pending_restore(mdt, cdt);
619 struct hsm_record_update *updates;
621 /* Limit execution of the expensive requests traversal
622 * to at most one second. This prevents repeatedly
623 * locking/unlocking the catalog for each request
624 * and preventing other HSM operations from happening
626 wait_event_interruptible_timeout(cdt->cdt_waitq,
627 kthread_should_stop() ||
628 cdt->cdt_wakeup_coordinator,
629 cfs_time_seconds(1));
631 cdt->cdt_wakeup_coordinator = false;
632 CDEBUG(D_HSM, "coordinator resumes\n");
634 if (kthread_should_stop()) {
635 CDEBUG(D_HSM, "Coordinator stops\n");
640 /* if coordinator is suspended continue to wait */
641 if (cdt->cdt_state == CDT_DISABLE) {
642 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
646 /* If no event, and no housekeeping to do, continue to
648 if (last_housekeeping + cdt->cdt_loop_period <=
649 ktime_get_real_seconds()) {
650 last_housekeeping = ktime_get_real_seconds();
651 hsd.hsd_housekeeping = true;
654 } else if (cdt->cdt_event) {
655 hsd.hsd_housekeeping = false;
656 start_cat_idx = hsd.hsd_start_cat_idx;
657 start_rec_idx = hsd.hsd_start_rec_idx;
662 cdt->cdt_event = false;
664 CDEBUG(D_HSM, "coordinator starts reading llog\n");
666 if (hsd.hsd_request_len != cdt->cdt_max_requests) {
667 /* cdt_max_requests has changed,
668 * we need to allocate a new buffer
670 struct hsm_scan_request *tmp = NULL;
671 int max_requests = cdt->cdt_max_requests;
672 OBD_ALLOC_LARGE(tmp, max_requests *
673 sizeof(struct hsm_scan_request));
675 CERROR("Failed to resize request buffer, "
676 "keeping it at %d\n",
677 hsd.hsd_request_len);
679 if (hsd.hsd_request != NULL)
680 OBD_FREE_LARGE(hsd.hsd_request,
683 hsd.hsd_request_len = max_requests;
684 request_sz = hsd.hsd_request_len *
685 sizeof(struct hsm_scan_request);
686 hsd.hsd_request = tmp;
690 hsd.hsd_action_count = 0;
691 hsd.hsd_request_count = 0;
692 hsd.hsd_one_restore = false;
694 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
695 &hsd, start_cat_idx, start_rec_idx,
700 CDEBUG(D_HSM, "found %d requests to send\n",
701 hsd.hsd_request_count);
703 if (list_empty(&cdt->cdt_agents)) {
704 CDEBUG(D_HSM, "no agent available, "
705 "coordinator sleeps\n");
706 /* reset HSM scanning index range. */
707 hsd.hsd_start_cat_idx = start_cat_idx;
708 hsd.hsd_start_rec_idx = start_rec_idx;
712 /* Compute how many HAI we have in all the requests */
714 for (i = 0; i < hsd.hsd_request_count; i++) {
715 const struct hsm_scan_request *request =
718 updates_cnt += request->hal->hal_count;
721 /* Allocate a temporary array to store the cookies to
722 * update, and their status. */
723 updates_sz = updates_cnt * sizeof(*updates);
724 OBD_ALLOC_LARGE(updates, updates_sz);
725 if (updates == NULL) {
726 CERROR("%s: Cannot allocate memory (%d bytes) "
727 "for %d updates. Too many HSM requests?\n",
728 mdt_obd_name(mdt), updates_sz, updates_cnt);
732 /* here hsd contains a list of requests to be started */
733 for (i = 0; i < hsd.hsd_request_count; i++) {
734 struct hsm_scan_request *request = &hsd.hsd_request[i];
735 struct hsm_action_list *hal = request->hal;
736 struct hsm_action_item *hai;
739 /* still room for work ? */
740 if (atomic_read(&cdt->cdt_request_count) >=
741 cdt->cdt_max_requests)
744 rc = mdt_hsm_agent_send(mti, hal, 0);
745 /* if failure, we suppose it is temporary
746 * if the copy tool failed to do the request
747 * it has to use hsm_progress
750 /* set up cookie vector to set records status
751 * after copy tools start or failed
753 hai = hai_first(hal);
754 for (j = 0; j < hal->hal_count; j++) {
755 updates[update_idx].cookie = hai->hai_cookie;
756 updates[update_idx].status =
757 (rc ? ARS_WAITING : ARS_STARTED);
762 /* TODO: narrow down the HSM action range that already
763 * scanned accroding to the cookies when a failure
767 hsd.hsd_start_cat_idx = start_cat_idx;
768 hsd.hsd_start_rec_idx = start_rec_idx;
773 rc = mdt_agent_record_update(mti, updates, update_idx);
775 CERROR("%s: mdt_agent_record_update() failed, "
776 "rc=%d, cannot update records "
778 mdt_obd_name(mdt), rc, update_idx);
781 OBD_FREE_LARGE(updates, updates_sz);
784 /* free hal allocated by callback */
785 for (i = 0; i < hsd.hsd_request_count; i++) {
786 struct hsm_scan_request *request = &hsd.hsd_request[i];
788 OBD_FREE_LARGE(request->hal, request->hal_sz);
792 if (hsd.hsd_request != NULL)
793 OBD_FREE_LARGE(hsd.hsd_request, request_sz);
795 mdt_hsm_cdt_cleanup(mdt);
798 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
799 mdt_obd_name(mdt), current->pid, rc);
801 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
803 mdt_obd_name(mdt), current->pid);
809 * register a new HSM restore handle for a file and take EX lock on the layout
810 * \param mti [IN] thread info
811 * \param cdt [IN] coordinator
812 * \param fid [IN] fid of the file to restore
813 * \param he [IN] HSM extent
815 * \retval 1 restore handle already exists for the fid
816 * \retval -ve failure
818 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
819 const struct lu_fid *fid,
820 const struct hsm_extent *he)
822 struct mdt_lock_handle lh = { 0 };
823 struct cdt_restore_handle *crh;
824 struct mdt_object *obj;
828 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
833 /* in V1 all file is restored
834 * crh->extent.start = he->offset;
835 * crh->extent.end = he->offset + he->length;
837 crh->crh_extent.start = 0;
838 crh->crh_extent.end = he->length;
839 crh->crh_lh.mlh_type = MDT_NUL_LOCK;
841 mutex_lock(&cdt->cdt_restore_lock);
842 if (cdt_restore_handle_find(cdt, fid) != NULL)
843 GOTO(out_crl, rc = 1);
845 if (unlikely(cdt->cdt_state == CDT_STOPPED ||
846 cdt->cdt_state == CDT_STOPPING))
847 GOTO(out_crl, rc = -EAGAIN);
849 list_add_tail(&crh->crh_list, &cdt->cdt_restore_handle_list);
850 mutex_unlock(&cdt->cdt_restore_lock);
852 /* get the layout lock */
853 obj = mdt_object_find_lock(mti, &crh->crh_fid, &lh,
854 MDS_INODELOCK_LAYOUT, LCK_EX);
856 mutex_lock(&cdt->cdt_restore_lock);
857 GOTO(out_ldel, rc = PTR_ERR(obj));
860 /* We do not keep a reference on the object during the restore
861 * which can be very long.
863 mdt_object_put(mti->mti_env, obj);
865 mutex_lock(&cdt->cdt_restore_lock);
866 if (unlikely(cdt->cdt_state == CDT_STOPPED ||
867 cdt->cdt_state == CDT_STOPPING))
868 GOTO(out_lh, rc = -EAGAIN);
871 mutex_unlock(&cdt->cdt_restore_lock);
875 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
877 list_del(&crh->crh_list);
879 mutex_unlock(&cdt->cdt_restore_lock);
880 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
886 * lookup a restore handle by FID
887 * caller needs to hold cdt_restore_lock
888 * \param cdt [IN] coordinator
889 * \param fid [IN] FID
890 * \retval cdt_restore_handle found
891 * \retval NULL not found
893 struct cdt_restore_handle *cdt_restore_handle_find(struct coordinator *cdt,
894 const struct lu_fid *fid)
896 struct cdt_restore_handle *crh;
899 list_for_each_entry(crh, &cdt->cdt_restore_handle_list, crh_list) {
900 if (lu_fid_eq(&crh->crh_fid, fid))
907 void cdt_restore_handle_del(struct mdt_thread_info *mti,
908 struct coordinator *cdt, const struct lu_fid *fid)
910 struct cdt_restore_handle *crh;
912 /* give back layout lock */
913 mutex_lock(&cdt->cdt_restore_lock);
914 crh = cdt_restore_handle_find(cdt, fid);
916 list_del(&crh->crh_list);
917 mutex_unlock(&cdt->cdt_restore_lock);
922 /* XXX We pass a NULL object since the restore handle does not
923 * keep a reference on the object being restored. */
924 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
925 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
929 * data passed to llog_cat_process() callback
930 * to scan requests and take actions
932 struct hsm_restore_data {
933 struct mdt_thread_info *hrd_mti;
937 * llog_cat_process() callback, used to:
938 * - find restore request and allocate the restore handle
939 * \param env [IN] environment
940 * \param llh [IN] llog handle
941 * \param hdr [IN] llog record
942 * \param data [IN/OUT] cb data = struct hsm_restore_data
944 * \retval -ve failure
946 static int hsm_restore_cb(const struct lu_env *env,
947 struct llog_handle *llh,
948 struct llog_rec_hdr *hdr, void *data)
950 struct llog_agent_req_rec *larr;
951 struct hsm_restore_data *hrd;
952 struct hsm_action_item *hai;
953 struct mdt_thread_info *mti;
954 struct coordinator *cdt;
960 cdt = &mti->mti_mdt->mdt_coordinator;
962 larr = (struct llog_agent_req_rec *)hdr;
963 hai = &larr->arr_hai;
964 if (hai->hai_cookie >= cdt->cdt_last_cookie) {
965 /* update the cookie to avoid collision */
966 cdt->cdt_last_cookie = hai->hai_cookie + 1;
969 if (hai->hai_action != HSMA_RESTORE ||
970 agent_req_in_final_state(larr->arr_status))
973 /* restore request not in a final state */
975 /* force replay of restore requests left in started state from previous
976 * CDT context, to be canceled later if finally found to be incompatible
977 * when being re-started */
978 if (larr->arr_status == ARS_STARTED) {
979 larr->arr_status = ARS_WAITING;
980 larr->arr_req_change = ktime_get_real_seconds();
981 rc = llog_write(env, llh, hdr, hdr->lrh_index);
986 rc = cdt_restore_handle_add(mti, cdt, &hai->hai_fid, &hai->hai_extent);
994 * restore coordinator state at startup
995 * the goal is to take a layout lock for each registered restore request
996 * \param mti [IN] context
998 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
1000 struct hsm_restore_data hrd;
1006 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
1012 int hsm_init_ucred(struct lu_ucred *uc)
1015 uc->uc_valid = UCRED_OLD;
1024 uc->uc_suppgids[0] = -1;
1025 uc->uc_suppgids[1] = -1;
1026 uc->uc_cap = cap_combine(CAP_FS_SET, CAP_NFSD_SET);
1027 uc->uc_umask = 0777;
1028 uc->uc_ginfo = NULL;
1029 uc->uc_identity = NULL;
1030 /* always record internal HSM activity if also enabled globally */
1031 uc->uc_enable_audit = 1;
1032 /* do not let rbac interfere with HSM internal processing */
1033 uc->uc_rbac_file_perms = 1;
1034 uc->uc_rbac_dne_ops = 1;
1035 uc->uc_rbac_quota_ops = 1;
1036 uc->uc_rbac_byfid_ops = 1;
1037 uc->uc_rbac_chlg_ops = 1;
1038 uc->uc_rbac_fscrypt_admin = 1;
1044 * initialize coordinator struct
1045 * \param mdt [IN] device
1047 * \retval -ve failure
1049 int mdt_hsm_cdt_init(struct mdt_device *mdt)
1051 struct coordinator *cdt = &mdt->mdt_coordinator;
1052 struct mdt_thread_info *cdt_mti = NULL;
1056 init_waitqueue_head(&cdt->cdt_waitq);
1057 init_rwsem(&cdt->cdt_llog_lock);
1058 init_rwsem(&cdt->cdt_agent_lock);
1059 init_rwsem(&cdt->cdt_request_lock);
1060 mutex_init(&cdt->cdt_restore_lock);
1061 mutex_init(&cdt->cdt_state_lock);
1062 set_cdt_state(cdt, CDT_STOPPED);
1064 INIT_LIST_HEAD(&cdt->cdt_request_list);
1065 INIT_LIST_HEAD(&cdt->cdt_agents);
1066 INIT_LIST_HEAD(&cdt->cdt_restore_handle_list);
1068 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
1072 0 /* extra bytes */,
1075 &cdt_request_cookie_hash_ops,
1077 if (cdt->cdt_request_cookie_hash == NULL)
1080 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
1084 0 /* extra bytes */,
1087 &cdt_agent_record_hash_ops,
1089 if (cdt->cdt_agent_record_hash == NULL)
1090 GOTO(out_request_cookie_hash, rc = -ENOMEM);
1092 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
1094 GOTO(out_agent_record_hash, rc);
1096 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1097 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
1101 lu_context_enter(&cdt->cdt_session);
1102 cdt->cdt_env.le_ses = &cdt->cdt_session;
1104 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1105 LASSERT(cdt_mti != NULL);
1107 cdt_mti->mti_env = &cdt->cdt_env;
1108 cdt_mti->mti_mdt = mdt;
1110 hsm_init_ucred(mdt_ucred(cdt_mti));
1112 /* default values for sysfs tunnables
1113 * can be override by MGS conf */
1114 cdt->cdt_default_archive_id = 1;
1115 cdt->cdt_grace_delay = 60;
1116 cdt->cdt_loop_period = 10;
1117 cdt->cdt_max_requests = 3;
1118 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1119 cdt->cdt_active_req_timeout = 3600;
1121 /* by default do not remove archives on last unlink */
1122 cdt->cdt_remove_archive_on_last_unlink = false;
1127 lu_env_fini(&cdt->cdt_env);
1128 out_agent_record_hash:
1129 cfs_hash_putref(cdt->cdt_agent_record_hash);
1130 cdt->cdt_agent_record_hash = NULL;
1131 out_request_cookie_hash:
1132 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1133 cdt->cdt_request_cookie_hash = NULL;
1139 * free a coordinator thread
1140 * \param mdt [IN] device
1142 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
1144 struct coordinator *cdt = &mdt->mdt_coordinator;
1147 lu_context_exit(cdt->cdt_env.le_ses);
1148 lu_context_fini(cdt->cdt_env.le_ses);
1150 lu_env_fini(&cdt->cdt_env);
1152 cfs_hash_putref(cdt->cdt_agent_record_hash);
1153 cdt->cdt_agent_record_hash = NULL;
1155 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1156 cdt->cdt_request_cookie_hash = NULL;
1162 * start a coordinator thread
1163 * \param mdt [IN] device
1165 * \retval -ve failure
1167 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1169 struct coordinator *cdt = &mdt->mdt_coordinator;
1170 struct mdt_thread_info *cdt_mti;
1173 struct task_struct *task;
1176 /* functions defined but not yet used
1177 * this avoid compilation warning
1179 ptr = dump_requests;
1181 rc = set_cdt_state(cdt, CDT_INIT);
1183 CERROR("%s: Coordinator already started or stopping\n",
1188 BUILD_BUG_ON(BIT(CDT_POLICY_SHIFT_COUNT - 1) != CDT_POLICY_LAST);
1189 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1191 /* just need to be larger than previous one */
1192 /* cdt_last_cookie is protected by cdt_llog_lock */
1193 cdt->cdt_last_cookie = ktime_get_real_seconds();
1194 atomic_set(&cdt->cdt_request_count, 0);
1195 atomic_set(&cdt->cdt_archive_count, 0);
1196 atomic_set(&cdt->cdt_restore_count, 0);
1197 atomic_set(&cdt->cdt_remove_count, 0);
1198 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1199 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1200 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1202 /* to avoid deadlock when start is made through sysfs
1203 * sysfs entries are created by the coordinator thread
1205 if (mdt->mdt_bottom->dd_rdonly)
1208 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1209 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
1212 set_cdt_state(cdt, CDT_STOPPED);
1213 CERROR("%s: error starting coordinator thread: %d\n",
1214 mdt_obd_name(mdt), rc);
1216 cdt->cdt_task = task;
1217 wait_event(cdt->cdt_waitq,
1218 cdt->cdt_state != CDT_INIT);
1219 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1228 * stop a coordinator thread
1229 * \param mdt [IN] device
1231 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1233 struct coordinator *cdt = &mdt->mdt_coordinator;
1237 /* stop coordinator thread */
1238 rc = set_cdt_state(cdt, CDT_STOPPING);
1240 kthread_stop(cdt->cdt_task);
1241 cdt->cdt_task = NULL;
1242 set_cdt_state(cdt, CDT_STOPPED);
1248 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1249 const struct lu_fid *fid,
1252 struct mdt_object *obj;
1256 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1258 GOTO(out, rc = PTR_ERR(obj));
1260 if (mh.mh_flags & HS_EXISTS &&
1261 mh.mh_arch_id == archive_id)
1262 GOTO(out_obj, rc = 0);
1264 mh.mh_flags |= HS_EXISTS;
1265 mh.mh_arch_id = archive_id;
1266 rc = mdt_hsm_attr_set(mti, obj, &mh);
1269 mdt_object_put(mti->mti_env, obj);
1275 * register all requests from an hal in the memory list
1276 * \param mti [IN] context
1277 * \param hal [IN] request
1278 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1279 * which is running the CT
1281 * \retval -ve failure
1283 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1284 struct hsm_action_list *hal, struct obd_uuid *uuid)
1286 struct mdt_device *mdt = mti->mti_mdt;
1287 struct coordinator *cdt = &mdt->mdt_coordinator;
1288 struct hsm_action_item *hai;
1292 /* register request in memory list */
1293 hai = hai_first(hal);
1294 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1295 struct cdt_agent_req *car;
1297 /* in case of a cancel request, we first mark the ondisk
1298 * record of the request we want to stop as canceled
1299 * this does not change the cancel record
1300 * it will be done when updating the request status
1302 if (hai->hai_action == HSMA_CANCEL) {
1303 struct hsm_record_update update = {
1304 .cookie = hai->hai_cookie,
1305 .status = ARS_CANCELED,
1308 rc = mdt_agent_record_update(mti, &update, 1);
1310 CERROR("%s: mdt_agent_record_update() failed, "
1311 "rc=%d, cannot update status to %s "
1312 "for cookie %#llx\n",
1313 mdt_obd_name(mdt), rc,
1314 agent_req_status2name(ARS_CANCELED),
1319 /* find the running request to set it canceled */
1320 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1322 car->car_canceled = 1;
1323 /* uuid has to be changed to the one running the
1324 * request to cancel */
1325 *uuid = car->car_uuid;
1326 mdt_cdt_put_request(car);
1328 /* no need to memorize cancel request
1329 * this also avoid a deadlock when we receive
1330 * a purge all requests command
1335 if (hai->hai_action == HSMA_ARCHIVE) {
1336 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1337 hal->hal_archive_id);
1344 car = mdt_cdt_alloc_request(hal->hal_archive_id, hal->hal_flags,
1347 GOTO(out, rc = PTR_ERR(car));
1349 rc = mdt_cdt_add_request(cdt, car);
1351 mdt_cdt_free_request(car);
1358 * swap layouts between 2 fids
1359 * \param mti [IN] context
1362 * \param mh_common [IN] MD HSM
1364 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1365 struct mdt_object *obj, const struct lu_fid *dfid,
1366 struct md_hsm *mh_common)
1368 struct mdt_object *dobj;
1369 struct mdt_lock_handle *dlh;
1373 if (!mdt_object_exists(obj))
1374 GOTO(out, rc = -ENOENT);
1376 /* we already have layout lock on obj so take only
1378 dlh = &mti->mti_lh[MDT_LH_OLD];
1379 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT,
1382 GOTO(out, rc = PTR_ERR(dobj));
1384 /* if copy tool closes the volatile before sending the final
1385 * progress through llapi_hsm_copy_end(), all the objects
1386 * are removed and mdd_swap_layout LBUG */
1387 if (!mdt_object_exists(dobj)) {
1388 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1389 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1390 GOTO(out_dobj, rc = -ENOENT);
1392 /* Since we only handle restores here, unconditionally use
1393 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1394 * be preserved in case of failure during swap_layout and not
1395 * leave a file in an intermediate but incoherent state.
1396 * But need to setup HSM xattr of data FID before, reuse
1397 * mti and mh presets for FID in hsm_cdt_request_completed(),
1398 * only need to clear RELEASED and DIRTY.
1400 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1401 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1403 rc = mo_swap_layouts(mti->mti_env,
1404 mdt_object_child(obj),
1405 mdt_object_child(dobj),
1406 SWAP_LAYOUTS_MDS_HSM);
1408 rc = mdt_lsom_downgrade(mti, obj);
1411 "%s: File fid="DFID" SOM "
1412 "downgrade failed, rc = %d\n",
1413 mdt_obd_name(mti->mti_mdt),
1414 PFID(mdt_object_fid(obj)), rc);
1417 mdt_object_unlock_put(mti, dobj, dlh, 1);
1423 * update status of a completed request
1424 * \param mti [IN] context
1425 * \param pgs [IN] progress of the copy tool
1427 * \retval -ve failure
1429 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1430 struct hsm_progress_kernel *pgs,
1431 const struct cdt_agent_req *car,
1432 enum agent_req_status *status)
1434 const struct lu_env *env = mti->mti_env;
1435 struct mdt_device *mdt = mti->mti_mdt;
1436 struct coordinator *cdt = &mdt->mdt_coordinator;
1437 struct mdt_object *obj = NULL;
1438 enum changelog_rec_flags clf_flags = 0;
1441 bool need_changelog = true;
1445 /* default is to retry */
1446 *status = ARS_WAITING;
1448 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1449 * if error/removed continue anyway to get correct reporting done */
1450 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1451 /* we will update MD HSM only if needed */
1452 is_mh_changed = false;
1454 /* no need to change mh->mh_arch_id
1455 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1457 if (pgs->hpk_errval != 0) {
1458 switch (pgs->hpk_errval) {
1460 /* the copy tool does not support cancel
1461 * so the cancel request is failed
1462 * As we cannot distinguish a cancel progress
1463 * from another action progress (they have the
1464 * same cookie), we suppose here the CT returns
1465 * ENOSYS only if does not support cancel
1467 /* this can also happen when cdt calls it to
1468 * for a timed out request */
1469 *status = ARS_FAILED;
1470 /* to have a cancel event in changelog */
1471 pgs->hpk_errval = ECANCELED;
1474 /* the request record has already been set to
1475 * ARS_CANCELED, this set the cancel request
1477 *status = ARS_SUCCEED;
1480 /* retry only if current policy or requested, and
1481 * object is not on error/removed */
1482 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1483 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1484 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1488 rc = hsm_set_cl_error(&clf_flags, pgs->hpk_errval);
1489 if (rc == -EOVERFLOW) {
1490 CERROR("%s: Request %#llx on "DFID" failed, error code %d too large\n",
1491 mdt_obd_name(mdt), pgs->hpk_cookie,
1492 PFID(&pgs->hpk_fid), (int)abs(pgs->hpk_errval));
1496 switch (car->car_hai->hai_action) {
1498 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1501 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1504 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1507 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1508 CERROR("%s: Failed request %#llx on "DFID
1509 " cannot be a CANCEL\n",
1512 PFID(&pgs->hpk_fid));
1515 CERROR("%s: Failed request %#llx on "DFID
1516 " %d is an unknown action\n",
1518 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1519 car->car_hai->hai_action);
1524 *status = ARS_SUCCEED;
1525 switch (car->car_hai->hai_action) {
1527 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1528 /* set ARCHIVE keep EXIST and clear LOST and
1530 mh.mh_arch_ver = pgs->hpk_data_version;
1531 mh.mh_flags |= HS_ARCHIVED;
1532 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1533 is_mh_changed = true;
1536 hsm_set_cl_event(&clf_flags, HE_RESTORE);
1538 /* do not clear RELEASED and DIRTY here
1539 * this will occur in hsm_swap_layouts()
1542 /* Restoring has changed the file version on
1544 mh.mh_arch_ver = pgs->hpk_data_version;
1545 is_mh_changed = true;
1548 hsm_set_cl_event(&clf_flags, HE_REMOVE);
1549 /* clear ARCHIVED EXISTS and LOST */
1550 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1551 is_mh_changed = true;
1554 hsm_set_cl_event(&clf_flags, HE_CANCEL);
1555 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1558 PFID(&pgs->hpk_fid));
1561 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1563 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1564 car->car_hai->hai_action);
1570 /* rc != 0 means error when analysing action, it may come from
1571 * a crasy CT no need to manage DIRTY
1572 * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1575 if (rc == 0 && !IS_ERR(obj))
1576 hsm_set_cl_flags(&clf_flags,
1577 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1579 /* unlock is done later, after layout lock management */
1580 if (is_mh_changed && !IS_ERR(obj))
1581 rc = mdt_hsm_attr_set(mti, obj, &mh);
1583 /* we give back layout lock only if restore was successful or
1584 * if no retry will be attempted and if object is still alive,
1585 * in other cases we just unlock the object */
1586 if (car->car_hai->hai_action == HSMA_RESTORE) {
1587 struct mdt_lock_handle *lh;
1589 /* restore in data FID done, we swap the layouts
1590 * only if restore is successful */
1591 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1592 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1595 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1596 *status = ARS_FAILED;
1597 pgs->hpk_errval = -rc;
1598 hsm_set_cl_error(&clf_flags, pgs->hpk_errval);
1601 /* we have to retry, so keep layout lock */
1602 if (*status == ARS_WAITING)
1605 /* restore special case, need to create ChangeLog record
1606 * before to give back layout lock to avoid concurrent
1607 * file updater to post out of order ChangeLog */
1608 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1609 &car->car_hai->hai_fid);
1610 need_changelog = false;
1612 cdt_restore_handle_del(mti, cdt, &car->car_hai->hai_fid);
1613 if (!IS_ERR_OR_NULL(obj)) {
1614 /* flush UPDATE lock so attributes are upadated */
1615 lh = &mti->mti_lh[MDT_LH_OLD];
1616 mdt_object_lock(mti, obj, lh, MDS_INODELOCK_UPDATE,
1618 mdt_object_unlock(mti, obj, lh, 1);
1625 /* always add a ChangeLog record */
1627 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1628 &car->car_hai->hai_fid);
1631 mdt_object_put(mti->mti_env, obj);
1637 * update status of a request
1638 * \param mti [IN] context
1639 * \param pgs [IN] progress of the copy tool
1641 * \retval -ve failure
1643 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1644 struct hsm_progress_kernel *pgs)
1646 struct mdt_device *mdt = mti->mti_mdt;
1647 struct coordinator *cdt = &mdt->mdt_coordinator;
1648 struct cdt_agent_req *car;
1652 /* no coordinator started, so we cannot serve requests */
1653 if (cdt->cdt_state == CDT_STOPPED)
1656 /* first do sanity checks */
1657 car = mdt_cdt_update_request(cdt, pgs);
1659 CERROR("%s: Cannot find running request for cookie %#llx"
1662 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1664 RETURN(PTR_ERR(car));
1667 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1668 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1669 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1670 hsm_copytool_action2name(car->car_hai->hai_action),
1671 pgs->hpk_flags, pgs->hpk_errval,
1672 PFID(&car->car_hai->hai_fid),
1673 PFID(&car->car_hai->hai_dfid));
1675 /* progress is done on FID or data FID depending of the action and
1676 * of the copy progress */
1677 /* for restore progress is used to send back the data FID to cdt */
1678 if (car->car_hai->hai_action == HSMA_RESTORE &&
1679 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1680 car->car_hai->hai_dfid = pgs->hpk_fid;
1682 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1683 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1684 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1685 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1686 CERROR("%s: Progress on "DFID" for cookie %#llx"
1687 " does not match request FID "DFID" nor data FID "
1690 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1691 PFID(&car->car_hai->hai_fid),
1692 PFID(&car->car_hai->hai_dfid));
1693 GOTO(out, rc = -EINVAL);
1696 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1697 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1698 " is not coherent (err=%d and not completed"
1701 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1702 hsm_copytool_action2name(car->car_hai->hai_action),
1703 pgs->hpk_errval, pgs->hpk_flags);
1704 GOTO(out, rc = -EINVAL);
1707 /* now progress is valid */
1709 /* we use a root like ucred */
1710 hsm_init_ucred(mdt_ucred(mti));
1712 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1713 enum agent_req_status status;
1714 struct hsm_record_update update;
1717 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1719 CDEBUG(D_HSM, "updating record: fid="DFID" cookie=%#llx action=%s "
1721 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1722 hsm_copytool_action2name(car->car_hai->hai_action),
1723 agent_req_status2name(status));
1725 /* update record first (LU-9075) */
1726 update.cookie = pgs->hpk_cookie;
1727 update.status = status;
1729 rc1 = mdt_agent_record_update(mti, &update, 1);
1731 CERROR("%s: mdt_agent_record_update() failed,"
1732 " rc=%d, cannot update status to %s"
1733 " for cookie %#llx\n",
1734 mdt_obd_name(mdt), rc1,
1735 agent_req_status2name(status),
1737 rc = (rc != 0 ? rc : rc1);
1739 /* then remove request from memory list (LU-9075) */
1740 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1742 /* ct has completed a request, so a slot is available,
1743 * signal the coordinator to find new work */
1744 mdt_hsm_cdt_event(cdt);
1746 /* if copytool send a progress on a canceled request
1747 * we inform copytool it should stop
1749 if (car->car_canceled == 1)
1755 /* remove ref got from mdt_cdt_update_request() */
1756 mdt_cdt_put_request(car);
1763 * llog_cat_process() callback, used to:
1764 * - purge all requests
1765 * \param env [IN] environment
1766 * \param llh [IN] llog handle
1767 * \param hdr [IN] llog record
1768 * \param data [IN] cb data = struct mdt_thread_info
1770 * \retval -ve failure
1772 static int mdt_cancel_all_cb(const struct lu_env *env,
1773 struct llog_handle *llh,
1774 struct llog_rec_hdr *hdr, void *data)
1776 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
1777 struct hsm_action_item *hai = &larr->arr_hai;
1778 struct mdt_thread_info *mti = data;
1779 struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
1783 if (larr->arr_status != ARS_WAITING &&
1784 larr->arr_status != ARS_STARTED)
1787 /* Unlock the EX layout lock */
1788 if (hai->hai_action == HSMA_RESTORE)
1789 cdt_restore_handle_del(mti, cdt, &hai->hai_fid);
1791 larr->arr_status = ARS_CANCELED;
1792 larr->arr_req_change = ktime_get_real_seconds();
1793 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1795 CERROR("%s: cannot update agent log: rc = %d\n",
1796 mdt_obd_name(mti->mti_mdt), rc);
1797 rc = LLOG_DEL_RECORD;
1804 * cancel all actions
1805 * \param obd [IN] MDT device
1807 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1810 struct lu_context session;
1811 struct mdt_thread_info *mti;
1812 struct coordinator *cdt = &mdt->mdt_coordinator;
1813 struct cdt_agent_req *car;
1814 struct hsm_action_list *hal = NULL;
1815 struct hsm_action_item *hai;
1816 int hal_sz = 0, hal_len, rc;
1817 enum cdt_states old_state;
1820 rc = lu_env_init(&env, LCT_MD_THREAD);
1824 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1825 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1829 lu_context_enter(&session);
1830 env.le_ses = &session;
1832 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1833 LASSERT(mti != NULL);
1835 mti->mti_env = &env;
1838 hsm_init_ucred(mdt_ucred(mti));
1840 mutex_lock(&cdt->cdt_state_lock);
1841 old_state = cdt->cdt_state;
1843 /* disable coordinator */
1844 rc = set_cdt_state_locked(cdt, CDT_DISABLE);
1846 GOTO(out_cdt_state_unlock, rc);
1848 /* send cancel to all running requests */
1849 down_read(&cdt->cdt_request_lock);
1850 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1851 mdt_cdt_get_request(car);
1852 /* request is not yet removed from list, it will be done
1853 * when copytool will return progress
1856 if (car->car_hai->hai_action == HSMA_CANCEL) {
1857 mdt_cdt_put_request(car);
1862 hal_len = sizeof(*hal) + round_up(MTI_NAME_MAXLEN + 1, 8) +
1863 round_up(car->car_hai->hai_len, 8);
1865 if (hal_len > hal_sz && hal_sz > 0) {
1866 /* not enough room, free old buffer */
1867 OBD_FREE(hal, hal_sz);
1871 /* empty buffer, allocate one */
1874 OBD_ALLOC(hal, hal_sz);
1876 mdt_cdt_put_request(car);
1877 up_read(&cdt->cdt_request_lock);
1878 GOTO(out_cdt_state, rc = -ENOMEM);
1882 hal->hal_version = HAL_VERSION;
1883 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1885 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1886 hal->hal_archive_id = car->car_archive_id;
1887 hal->hal_flags = car->car_flags;
1890 hai = hai_first(hal);
1891 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1892 hai->hai_action = HSMA_CANCEL;
1895 /* it is possible to safely call mdt_hsm_agent_send()
1896 * (ie without a deadlock on cdt_request_lock), because the
1897 * write lock is taken only if we are not in purge mode
1898 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1899 * nor mdt_cdt_remove_request())
1901 /* no conflict with cdt thread because cdt is disable and we
1902 * have the request lock */
1903 mdt_hsm_agent_send(mti, hal, 1);
1905 mdt_cdt_put_request(car);
1907 up_read(&cdt->cdt_request_lock);
1910 OBD_FREE(hal, hal_sz);
1912 /* cancel all on-disk records */
1913 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1914 (void *)mti, 0, 0, WRITE);
1916 /* Enable coordinator, unless the coordinator was stopping. */
1917 set_cdt_state_locked(cdt, old_state);
1918 out_cdt_state_unlock:
1919 mutex_unlock(&cdt->cdt_state_lock);
1921 lu_context_exit(&session);
1922 lu_context_fini(&session);
1930 * check if a request is compatible with file status
1931 * \param hai [IN] request description
1932 * \param archive_id [IN] request archive id
1933 * \param rq_flags [IN] request flags
1934 * \param hsm [IN] file HSM metadata
1937 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1938 u32 archive_id, u64 rq_flags,
1939 const struct md_hsm *hsm)
1941 int is_compat = false;
1945 hsm_flags = hsm->mh_flags;
1946 switch (hai->hai_action) {
1948 if (!(hsm_flags & HS_NOARCHIVE) &&
1949 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1952 if (hsm_flags & HS_EXISTS &&
1954 archive_id != hsm->mh_arch_id)
1959 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1960 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1964 if (!(hsm_flags & HS_RELEASED) &&
1965 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1972 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1973 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1974 PFID(&hai->hai_fid),
1975 hsm_copytool_action2name(hai->hai_action), rq_flags,
1976 hai->hai_extent.offset, hai->hai_extent.length,
1978 (is_compat ? "compatible" : "uncompatible"));
1984 * sysfs interface used to get/set HSM behaviour (cdt->cdt_policy)
1986 static const struct {
1990 } hsm_policy_names[] = {
1991 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1992 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1997 * convert a policy name to a bit
1998 * \param name [IN] policy name
2000 * \retval policy bit
2002 static __u64 hsm_policy_str2bit(const char *name)
2006 for (i = 0; hsm_policy_names[i].bit != 0; i++)
2007 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
2008 strcmp(hsm_policy_names[i].name, name) == 0)
2009 return hsm_policy_names[i].bit;
2014 * convert a policy bit field to a string
2015 * \param mask [IN] policy bit field
2016 * \param hexa [IN] print mask before bit names
2017 * \param buffer [OUT] string
2018 * \param count [IN] size of buffer
2020 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
2028 seq_printf(m, "(%#llx) ", mask);
2030 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
2033 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
2034 if (hsm_policy_names[j].bit == bit)
2038 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
2040 seq_printf(m, "%s ", hsm_policy_names[j].name);
2042 /* remove last ' ' */
2047 /* methods to read/write HSM policy flags */
2048 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
2050 struct mdt_device *mdt = m->private;
2051 struct coordinator *cdt = &mdt->mdt_coordinator;
2054 hsm_policy_bit2str(m, cdt->cdt_policy, false);
2059 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
2060 size_t count, loff_t *off)
2062 struct seq_file *m = file->private_data;
2063 struct mdt_device *mdt = m->private;
2064 struct coordinator *cdt = &mdt->mdt_coordinator;
2065 char *start, *token, sign;
2068 __u64 add_mask, remove_mask, set_mask;
2072 if (count + 1 > PAGE_SIZE)
2075 OBD_ALLOC(buf, count + 1);
2079 if (copy_from_user(buf, buffer, count))
2080 GOTO(out, rc = -EFAULT);
2085 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
2088 add_mask = remove_mask = set_mask = 0;
2090 token = strsep(&start, "\n ");
2096 if (sign == '-' || sign == '+')
2099 policy = hsm_policy_str2bit(token);
2101 CWARN("%s: '%s' is unknown, "
2102 "supported policies are:\n", mdt_obd_name(mdt),
2104 hsm_policy_bit2str(m, 0, false);
2105 GOTO(out, rc = -EINVAL);
2109 remove_mask |= policy;
2119 } while (start != NULL);
2121 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
2122 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
2124 /* if no sign in all string, it is a clear and set
2125 * if some sign found, all unsigned are converted
2127 * P1 P2 = set to P1 and P2
2128 * P1 -P2 = add P1 clear P2 same as +P1 -P2
2130 if (remove_mask == 0 && add_mask == 0) {
2131 cdt->cdt_policy = set_mask;
2133 cdt->cdt_policy |= set_mask | add_mask;
2134 cdt->cdt_policy &= ~remove_mask;
2137 GOTO(out, rc = count);
2140 OBD_FREE(buf, count + 1);
2143 LDEBUGFS_SEQ_FOPS(mdt_hsm_policy);
2145 static ssize_t loop_period_show(struct kobject *kobj, struct attribute *attr,
2148 struct coordinator *cdt = container_of(kobj, struct coordinator,
2151 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_loop_period);
2154 static ssize_t loop_period_store(struct kobject *kobj, struct attribute *attr,
2155 const char *buffer, size_t count)
2157 struct coordinator *cdt = container_of(kobj, struct coordinator,
2162 rc = kstrtouint(buffer, 0, &val);
2167 cdt->cdt_loop_period = val;
2169 return val ? count : -EINVAL;
2171 LUSTRE_RW_ATTR(loop_period);
2173 static ssize_t grace_delay_show(struct kobject *kobj, struct attribute *attr,
2176 struct coordinator *cdt = container_of(kobj, struct coordinator,
2179 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_grace_delay);
2182 static ssize_t grace_delay_store(struct kobject *kobj, struct attribute *attr,
2183 const char *buffer, size_t count)
2185 struct coordinator *cdt = container_of(kobj, struct coordinator,
2190 rc = kstrtouint(buffer, 0, &val);
2195 cdt->cdt_grace_delay = val;
2197 return val ? count : -EINVAL;
2199 LUSTRE_RW_ATTR(grace_delay);
2201 static ssize_t active_request_timeout_show(struct kobject *kobj,
2202 struct attribute *attr,
2205 struct coordinator *cdt = container_of(kobj, struct coordinator,
2208 return scnprintf(buf, PAGE_SIZE, "%d\n", cdt->cdt_active_req_timeout);
2211 static ssize_t active_request_timeout_store(struct kobject *kobj,
2212 struct attribute *attr,
2213 const char *buffer, size_t count)
2215 struct coordinator *cdt = container_of(kobj, struct coordinator,
2220 rc = kstrtouint(buffer, 0, &val);
2225 cdt->cdt_active_req_timeout = val;
2227 return val ? count : -EINVAL;
2229 LUSTRE_RW_ATTR(active_request_timeout);
2231 static ssize_t max_requests_show(struct kobject *kobj, struct attribute *attr,
2234 struct coordinator *cdt = container_of(kobj, struct coordinator,
2237 return scnprintf(buf, PAGE_SIZE, "%llu\n", cdt->cdt_max_requests);
2240 static ssize_t max_requests_store(struct kobject *kobj, struct attribute *attr,
2241 const char *buffer, size_t count)
2243 struct coordinator *cdt = container_of(kobj, struct coordinator,
2245 unsigned long long val;
2248 rc = kstrtoull(buffer, 0, &val);
2253 cdt->cdt_max_requests = val;
2255 return val ? count : -EINVAL;
2257 LUSTRE_RW_ATTR(max_requests);
2259 static ssize_t default_archive_id_show(struct kobject *kobj,
2260 struct attribute *attr, char *buf)
2262 struct coordinator *cdt = container_of(kobj, struct coordinator,
2265 return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_default_archive_id);
2268 static ssize_t default_archive_id_store(struct kobject *kobj,
2269 struct attribute *attr,
2270 const char *buffer, size_t count)
2272 struct coordinator *cdt = container_of(kobj, struct coordinator,
2277 rc = kstrtouint(buffer, 0, &val);
2282 cdt->cdt_default_archive_id = val;
2284 return val ? count : -EINVAL;
2286 LUSTRE_RW_ATTR(default_archive_id);
2289 * procfs write method for MDT/hsm_control
2290 * proc entry is in mdt directory so data is mdt obd_device pointer
2292 #define CDT_ENABLE_CMD "enabled"
2293 #define CDT_STOP_CMD "shutdown"
2294 #define CDT_DISABLE_CMD "disabled"
2295 #define CDT_PURGE_CMD "purge"
2296 #define CDT_HELP_CMD "help"
2297 #define CDT_MAX_CMD_LEN 10
2299 ssize_t hsm_control_store(struct kobject *kobj, struct attribute *attr,
2300 const char *buffer, size_t count)
2302 struct obd_device *obd = container_of(kobj, struct obd_device,
2304 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2305 struct coordinator *cdt = &(mdt->mdt_coordinator);
2309 if (count == 0 || count >= CDT_MAX_CMD_LEN)
2312 if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
2313 if (cdt->cdt_state == CDT_DISABLE) {
2314 rc = set_cdt_state(cdt, CDT_RUNNING);
2315 mdt_hsm_cdt_event(cdt);
2316 wake_up(&cdt->cdt_waitq);
2317 } else if (cdt->cdt_state == CDT_RUNNING) {
2320 rc = mdt_hsm_cdt_start(mdt);
2322 } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
2323 if (cdt->cdt_state == CDT_STOPPING) {
2324 CERROR("%s: Coordinator is already stopping\n",
2327 } else if (cdt->cdt_state == CDT_STOPPED) {
2330 rc = mdt_hsm_cdt_stop(mdt);
2332 } else if (strncmp(buffer, CDT_DISABLE_CMD,
2333 strlen(CDT_DISABLE_CMD)) == 0) {
2334 if ((cdt->cdt_state == CDT_STOPPING) ||
2335 (cdt->cdt_state == CDT_STOPPED)) {
2336 /* exit gracefully if coordinator is being stopped
2337 * or stopped already.
2341 rc = set_cdt_state(cdt, CDT_DISABLE);
2343 } else if (strncmp(buffer, CDT_PURGE_CMD,
2344 strlen(CDT_PURGE_CMD)) == 0) {
2345 rc = hsm_cancel_all_actions(mdt);
2346 } else if (strncmp(buffer, CDT_HELP_CMD,
2347 strlen(CDT_HELP_CMD)) == 0) {
2355 CERROR("%s: Valid coordinator control commands are: "
2356 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2357 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2358 CDT_PURGE_CMD, CDT_HELP_CMD);
2366 ssize_t hsm_control_show(struct kobject *kobj, struct attribute *attr,
2369 struct obd_device *obd = container_of(kobj, struct obd_device,
2371 struct coordinator *cdt;
2373 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2375 return scnprintf(buf, PAGE_SIZE, "%s\n",
2376 cdt_mdt_state2str(cdt->cdt_state));
2380 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2386 for (i = 0; i < 8 * sizeof(mask); i++) {
2387 if (mask & (1UL << i)) {
2388 seq_printf(m, "%s%s", first ? "" : " ",
2389 hsm_copytool_action2name(i));
2399 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2401 struct mdt_device *mdt = m->private;
2402 struct coordinator *cdt = &mdt->mdt_coordinator;
2404 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2408 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2410 struct mdt_device *mdt = m->private;
2411 struct coordinator *cdt = &mdt->mdt_coordinator;
2413 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2417 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2419 struct mdt_device *mdt = m->private;
2420 struct coordinator *cdt = &mdt->mdt_coordinator;
2422 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2425 static inline enum hsm_copytool_action
2426 hsm_copytool_name2action(const char *name)
2428 if (strcasecmp(name, "NOOP") == 0)
2430 else if (strcasecmp(name, "ARCHIVE") == 0)
2431 return HSMA_ARCHIVE;
2432 else if (strcasecmp(name, "RESTORE") == 0)
2433 return HSMA_RESTORE;
2434 else if (strcasecmp(name, "REMOVE") == 0)
2436 else if (strcasecmp(name, "CANCEL") == 0)
2443 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2444 size_t user_count, __u64 *mask)
2446 char *buf, *pos, *name;
2452 if (!(user_count < 4096))
2455 buf_size = user_count + 1;
2457 OBD_ALLOC(buf, buf_size);
2461 if (copy_from_user(buf, user_buf, buf_size - 1))
2462 GOTO(out, rc = -EFAULT);
2464 buf[buf_size - 1] = '\0';
2467 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2473 action = hsm_copytool_name2action(name);
2475 GOTO(out, rc = -EINVAL);
2477 new_mask |= (1UL << action);
2483 OBD_FREE(buf, buf_size);
2489 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2490 size_t count, loff_t *off)
2492 struct seq_file *m = file->private_data;
2493 struct mdt_device *mdt = m->private;
2494 struct coordinator *cdt = &mdt->mdt_coordinator;
2496 return mdt_write_hsm_request_mask(file, buf, count,
2497 &cdt->cdt_user_request_mask);
2501 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2502 size_t count, loff_t *off)
2504 struct seq_file *m = file->private_data;
2505 struct mdt_device *mdt = m->private;
2506 struct coordinator *cdt = &mdt->mdt_coordinator;
2508 return mdt_write_hsm_request_mask(file, buf, count,
2509 &cdt->cdt_group_request_mask);
2513 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2514 size_t count, loff_t *off)
2516 struct seq_file *m = file->private_data;
2517 struct mdt_device *mdt = m->private;
2518 struct coordinator *cdt = &mdt->mdt_coordinator;
2520 return mdt_write_hsm_request_mask(file, buf, count,
2521 &cdt->cdt_other_request_mask);
2524 static ssize_t remove_archive_on_last_unlink_show(struct kobject *kobj,
2525 struct attribute *attr,
2528 struct coordinator *cdt = container_of(kobj, struct coordinator,
2531 return scnprintf(buf, PAGE_SIZE, "%u\n",
2532 cdt->cdt_remove_archive_on_last_unlink);
2535 static ssize_t remove_archive_on_last_unlink_store(struct kobject *kobj,
2536 struct attribute *attr,
2540 struct coordinator *cdt = container_of(kobj, struct coordinator,
2545 rc = kstrtobool(buffer, &val);
2549 cdt->cdt_remove_archive_on_last_unlink = val;
2552 LUSTRE_RW_ATTR(remove_archive_on_last_unlink);
2554 LDEBUGFS_SEQ_FOPS(mdt_hsm_user_request_mask);
2555 LDEBUGFS_SEQ_FOPS(mdt_hsm_group_request_mask);
2556 LDEBUGFS_SEQ_FOPS(mdt_hsm_other_request_mask);
2558 /* Read-only sysfs files for request counters */
2559 static ssize_t archive_count_show(struct kobject *kobj, struct attribute *attr,
2562 struct coordinator *cdt = container_of(kobj, struct coordinator,
2565 return scnprintf(buf, PAGE_SIZE, "%d\n",
2566 atomic_read(&cdt->cdt_archive_count));
2568 LUSTRE_RO_ATTR(archive_count);
2570 static ssize_t restore_count_show(struct kobject *kobj, struct attribute *attr,
2573 struct coordinator *cdt = container_of(kobj, struct coordinator,
2576 return scnprintf(buf, PAGE_SIZE, "%d\n",
2577 atomic_read(&cdt->cdt_restore_count));
2579 LUSTRE_RO_ATTR(restore_count);
2581 static ssize_t remove_count_show(struct kobject *kobj, struct attribute *attr,
2584 struct coordinator *cdt = container_of(kobj, struct coordinator,
2587 return scnprintf(buf, PAGE_SIZE, "%d\n",
2588 atomic_read(&cdt->cdt_remove_count));
2590 LUSTRE_RO_ATTR(remove_count);
2592 static struct ldebugfs_vars ldebugfs_mdt_hsm_vars[] = {
2594 .fops = &mdt_hsm_agent_fops },
2595 { .name = "actions",
2596 .fops = &mdt_hsm_actions_fops,
2597 .proc_mode = 0444 },
2599 .fops = &mdt_hsm_policy_fops },
2600 { .name = "active_requests",
2601 .fops = &mdt_hsm_active_requests_fops },
2602 { .name = "user_request_mask",
2603 .fops = &mdt_hsm_user_request_mask_fops, },
2604 { .name = "group_request_mask",
2605 .fops = &mdt_hsm_group_request_mask_fops, },
2606 { .name = "other_request_mask",
2607 .fops = &mdt_hsm_other_request_mask_fops, },
2611 static struct attribute *hsm_attrs[] = {
2612 &lustre_attr_loop_period.attr,
2613 &lustre_attr_grace_delay.attr,
2614 &lustre_attr_active_request_timeout.attr,
2615 &lustre_attr_max_requests.attr,
2616 &lustre_attr_default_archive_id.attr,
2617 &lustre_attr_remove_archive_on_last_unlink.attr,
2618 &lustre_attr_archive_count.attr,
2619 &lustre_attr_restore_count.attr,
2620 &lustre_attr_remove_count.attr,
2624 KOBJ_ATTRIBUTE_GROUPS(hsm); /* creates hsm_groups from hsm_attrs */
2626 static void hsm_kobj_release(struct kobject *kobj)
2628 struct coordinator *cdt = container_of(kobj, struct coordinator,
2631 debugfs_remove_recursive(cdt->cdt_debugfs_dir);
2632 cdt->cdt_debugfs_dir = NULL;
2634 complete(&cdt->cdt_kobj_unregister);
2637 static struct kobj_type hsm_ktype = {
2638 .default_groups = KOBJ_ATTR_GROUPS(hsm),
2639 .sysfs_ops = &lustre_sysfs_ops,
2640 .release = hsm_kobj_release,
2644 * create sysfs entries for coordinator
2647 * \retval -ve failure
2649 int hsm_cdt_tunables_init(struct mdt_device *mdt)
2651 struct coordinator *cdt = &mdt->mdt_coordinator;
2652 struct obd_device *obd = mdt2obd_dev(mdt);
2655 init_completion(&cdt->cdt_kobj_unregister);
2656 rc = kobject_init_and_add(&cdt->cdt_hsm_kobj, &hsm_ktype,
2657 &obd->obd_kset.kobj, "%s", "hsm");
2659 kobject_put(&cdt->cdt_hsm_kobj);
2663 /* init debugfs entries, failure is not critical */
2664 cdt->cdt_debugfs_dir = debugfs_create_dir("hsm",
2665 obd->obd_debugfs_entry);
2666 ldebugfs_add_vars(cdt->cdt_debugfs_dir, ldebugfs_mdt_hsm_vars, mdt);
2672 * remove sysfs entries for coordinator
2676 void hsm_cdt_tunables_fini(struct mdt_device *mdt)
2678 struct coordinator *cdt = &mdt->mdt_coordinator;
2680 kobject_put(&cdt->cdt_hsm_kobj);
2681 wait_for_completion(&cdt->cdt_kobj_unregister);