4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2017, Intel Corporation.
27 * Use is subject to license terms.
30 * lustre/mdt/mdt_coordinator.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_export.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_kernelcomm.h>
48 #include "mdt_internal.h"
50 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
53 * get obj and HSM attributes on a fid
54 * \param mti [IN] context
55 * \param fid [IN] object fid
56 * \param hsm [OUT] HSM meta data
57 * \retval obj or error (-ENOENT if not found)
59 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
60 const struct lu_fid *fid,
64 struct mdt_object *obj;
72 /* find object by FID */
73 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
77 if (!mdt_object_exists(obj)) {
79 mdt_object_put(mti->mti_env, obj);
80 RETURN(ERR_PTR(-ENOENT));
83 rc = mdt_attr_get_complex(mti, obj, ma);
85 mdt_object_put(mti->mti_env, obj);
89 if (ma->ma_valid & MA_HSM)
92 memset(hsm, 0, sizeof(*hsm));
97 void mdt_hsm_dump_hal(int level, const char *prefix,
98 struct hsm_action_list *hal)
101 struct hsm_action_item *hai;
104 CDEBUG(level, "%s: HAL header: version %X count %d compound %#llx"
105 " archive_id %d flags %#llx\n",
106 prefix, hal->hal_version, hal->hal_count,
107 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
109 hai = hai_first(hal);
110 for (i = 0; i < hal->hal_count; i++) {
111 sz = hai->hai_len - sizeof(*hai);
112 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
113 " compound/cookie=%#llx/%#llx"
114 " action=%s extent=%#llx-%#llx gid=%#llx"
115 " datalen=%d data=[%s]\n",
117 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
118 hal->hal_compound_id, hai->hai_cookie,
119 hsm_copytool_action2name(hai->hai_action),
120 hai->hai_extent.offset,
121 hai->hai_extent.length,
123 hai_dump_data_field(hai, buf, sizeof(buf)));
129 * data passed to llog_cat_process() callback
130 * to scan requests and take actions
132 struct hsm_scan_request {
135 struct hsm_action_list *hal;
138 struct hsm_scan_data {
139 struct mdt_thread_info *mti;
140 char fs_name[MTI_NAME_MAXLEN+1];
141 /* are we scanning the logs for housekeeping, or just looking
145 /* request to be send to agents */
146 int max_requests; /** vector size */
147 int request_cnt; /** used count */
148 struct hsm_scan_request *request;
151 struct hsm_thread_data {
152 struct mdt_thread_info *cdt_mti;
153 struct hsm_scan_request *request;
156 static int mdt_cdt_waiting_cb(const struct lu_env *env,
157 struct mdt_device *mdt,
158 struct llog_handle *llh,
159 struct llog_agent_req_rec *larr,
160 struct hsm_scan_data *hsd)
162 struct coordinator *cdt = &mdt->mdt_coordinator;
163 struct hsm_scan_request *request;
164 struct hsm_action_item *hai;
167 /* Are agents full? */
168 if (atomic_read(&cdt->cdt_request_count) >= cdt->cdt_max_requests)
171 /* first search whether the request is found in the list we
174 for (i = 0; i < hsd->request_cnt; i++) {
175 if (hsd->request[i].hal->hal_compound_id ==
176 larr->arr_compound_id) {
177 request = &hsd->request[i];
183 struct hsm_action_list *hal;
185 if (hsd->request_cnt == hsd->max_requests) {
186 if (!hsd->housekeeping) {
187 /* The request array is full, stop
188 * here. There might be more known
189 * requests that could be merged, but
190 * this avoid analyzing too many llogs
191 * for minor gains. */
192 RETURN(LLOG_PROC_BREAK);
194 /* Unknown request and no more room
195 * for a new request. Continue to scan
196 * to find other entries for already
197 * existing requests. */
202 request = &hsd->request[hsd->request_cnt];
204 /* allocates hai vector size just needs to be large
206 request->hal_sz = sizeof(*request->hal) +
207 cfs_size_round(MTI_NAME_MAXLEN + 1) +
208 2 * cfs_size_round(larr->arr_hai.hai_len);
209 OBD_ALLOC(hal, request->hal_sz);
213 hal->hal_version = HAL_VERSION;
214 strlcpy(hal->hal_fsname, hsd->fs_name, MTI_NAME_MAXLEN + 1);
215 hal->hal_compound_id = larr->arr_compound_id;
216 hal->hal_archive_id = larr->arr_archive_id;
217 hal->hal_flags = larr->arr_flags;
219 request->hal_used_sz = hal_size(hal);
222 hai = hai_first(hal);
224 /* request is known */
225 /* we check if record archive num is the same as the
226 * known request, if not we will serve it in multiple
227 * time because we do not know if the agent can serve
228 * multiple backend a use case is a compound made of
229 * multiple restore where the files are not archived
230 * in the same backend */
231 if (larr->arr_archive_id != request->hal->hal_archive_id)
234 if (request->hal_sz < request->hal_used_sz +
235 cfs_size_round(larr->arr_hai.hai_len)) {
236 /* Not enough room, need an extension */
240 sz = 2 * request->hal_sz;
241 OBD_ALLOC(hal_buffer, sz);
244 memcpy(hal_buffer, request->hal, request->hal_used_sz);
245 OBD_FREE(request->hal, request->hal_sz);
246 request->hal = hal_buffer;
247 request->hal_sz = sz;
250 hai = hai_first(request->hal);
251 for (i = 0; i < request->hal->hal_count; i++)
255 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
256 hai->hai_cookie = larr->arr_hai.hai_cookie;
257 hai->hai_gid = larr->arr_hai.hai_gid;
259 request->hal_used_sz += cfs_size_round(hai->hai_len);
260 request->hal->hal_count++;
262 if (hai->hai_action != HSMA_CANCEL)
263 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
264 llh->lgh_hdr->llh_cat_idx,
265 larr->arr_hdr.lrh_index);
270 static int mdt_cdt_started_cb(const struct lu_env *env,
271 struct mdt_device *mdt,
272 struct llog_handle *llh,
273 struct llog_agent_req_rec *larr,
274 struct hsm_scan_data *hsd)
276 struct coordinator *cdt = &mdt->mdt_coordinator;
277 struct hsm_action_item *hai = &larr->arr_hai;
278 struct cdt_agent_req *car;
279 time64_t now = ktime_get_real_seconds();
284 if (!hsd->housekeeping)
287 /* we search for a running request
288 * error may happen if coordinator crashes or stopped
289 * with running request
291 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
293 last = larr->arr_req_change;
295 last = car->car_req_update;
298 /* test if request too long, if yes cancel it
299 * the same way the copy tool acknowledge a cancel request */
300 if (now <= last + cdt->cdt_active_req_timeout)
301 GOTO(out_car, rc = 0);
303 dump_llog_agent_req_rec("request timed out, start cleaning", larr);
306 car->car_req_update = now;
307 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0, &car->car_uuid);
308 /* Remove car from memory list (LU-9075) */
309 mdt_cdt_remove_request(cdt, hai->hai_cookie);
312 /* Emit a changelog record for the failed action.*/
314 hsm_set_cl_error(&cl_flags, ECANCELED);
316 switch (hai->hai_action) {
318 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
321 hsm_set_cl_event(&cl_flags, HE_RESTORE);
324 hsm_set_cl_event(&cl_flags, HE_REMOVE);
327 hsm_set_cl_event(&cl_flags, HE_CANCEL);
330 /* Unknown record type, skip changelog. */
336 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
339 if (hai->hai_action == HSMA_RESTORE)
340 cdt_restore_handle_del(hsd->mti, cdt, &hai->hai_fid);
342 larr->arr_status = ARS_CANCELED;
343 larr->arr_req_change = now;
344 rc = llog_write(hsd->mti->mti_env, llh, &larr->arr_hdr,
345 larr->arr_hdr.lrh_index);
347 CERROR("%s: cannot update agent log: rc = %d\n",
348 mdt_obd_name(mdt), rc);
349 rc = LLOG_DEL_RECORD;
352 /* ct has completed a request, so a slot is available,
353 * signal the coordinator to find new work */
354 mdt_hsm_cdt_event(cdt);
357 mdt_cdt_put_request(car);
363 * llog_cat_process() callback, used to:
364 * - find waiting request and start action
365 * - purge canceled and done requests
366 * \param env [IN] environment
367 * \param llh [IN] llog handle
368 * \param hdr [IN] llog record
369 * \param data [IN/OUT] cb data = struct hsm_scan_data
371 * \retval -ve failure
373 static int mdt_coordinator_cb(const struct lu_env *env,
374 struct llog_handle *llh,
375 struct llog_rec_hdr *hdr,
378 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
379 struct hsm_scan_data *hsd = data;
380 struct mdt_device *mdt = hsd->mti->mti_mdt;
381 struct coordinator *cdt = &mdt->mdt_coordinator;
384 larr = (struct llog_agent_req_rec *)hdr;
385 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
386 switch (larr->arr_status) {
388 RETURN(mdt_cdt_waiting_cb(env, mdt, llh, larr, hsd));
390 RETURN(mdt_cdt_started_cb(env, mdt, llh, larr, hsd));
392 if (!hsd->housekeeping)
395 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
396 ktime_get_real_seconds()) {
397 cdt_agent_record_hash_del(cdt,
398 larr->arr_hai.hai_cookie);
399 RETURN(LLOG_DEL_RECORD);
407 * create /proc entries for coordinator
410 * \retval -ve failure
412 int hsm_cdt_procfs_init(struct mdt_device *mdt)
414 struct coordinator *cdt = &mdt->mdt_coordinator;
418 /* init /proc entries, failure is not critical */
419 cdt->cdt_proc_dir = lprocfs_register("hsm",
420 mdt2obd_dev(mdt)->obd_proc_entry,
421 lprocfs_mdt_hsm_vars, mdt);
422 if (IS_ERR(cdt->cdt_proc_dir)) {
423 rc = PTR_ERR(cdt->cdt_proc_dir);
424 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
425 " rc=%d\n", mdt_obd_name(mdt), rc);
426 cdt->cdt_proc_dir = NULL;
434 * remove /proc entries for coordinator
437 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
439 struct coordinator *cdt = &mdt->mdt_coordinator;
441 if (cdt->cdt_proc_dir != NULL)
442 lprocfs_remove(&cdt->cdt_proc_dir);
446 * get vector of hsm cdt /proc vars
450 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
452 return lprocfs_mdt_hsm_vars;
455 /* Release the ressource used by the coordinator. Called when the
456 * coordinator is stopping. */
457 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
459 struct coordinator *cdt = &mdt->mdt_coordinator;
460 struct cdt_agent_req *car, *tmp1;
461 struct hsm_agent *ha, *tmp2;
462 struct cdt_restore_handle *crh, *tmp3;
463 struct mdt_thread_info *cdt_mti;
466 down_write(&cdt->cdt_request_lock);
467 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
469 cfs_hash_del(cdt->cdt_request_cookie_hash,
470 &car->car_hai->hai_cookie,
471 &car->car_cookie_hash);
472 list_del(&car->car_request_list);
473 mdt_cdt_put_request(car);
475 up_write(&cdt->cdt_request_lock);
477 down_write(&cdt->cdt_agent_lock);
478 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
479 list_del(&ha->ha_list);
482 up_write(&cdt->cdt_agent_lock);
484 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
485 mutex_lock(&cdt->cdt_restore_lock);
486 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_handle_list,
488 list_del(&crh->crh_list);
489 /* give back layout lock */
490 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
491 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
493 mutex_unlock(&cdt->cdt_restore_lock);
497 * Coordinator state transition table, indexed on enum cdt_states, taking
498 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
499 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
501 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
502 /* from -> to: stopped init running disable stopping */
503 /* stopped */ { true, true, false, false, false },
504 /* init */ { true, false, true, false, false },
505 /* running */ { false, false, true, true, true },
506 /* disable */ { false, false, true, true, true },
507 /* stopping */ { true, false, false, false, false }
511 * Change coordinator thread state
512 * Some combinations are not valid, so catch them here.
514 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
515 * the transition was not possible.
517 static int set_cdt_state_locked(struct coordinator *cdt,
518 enum cdt_states new_state)
521 enum cdt_states state;
523 state = cdt->cdt_state;
525 if (cdt_transition[state][new_state]) {
526 cdt->cdt_state = new_state;
530 "unexpected coordinator transition, from=%s, to=%s\n",
531 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
538 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state)
542 mutex_lock(&cdt->cdt_state_lock);
543 rc = set_cdt_state_locked(cdt, new_state);
544 mutex_unlock(&cdt->cdt_state_lock);
553 * \param data [IN] obd device
555 * \retval -ve failure
557 static int mdt_coordinator(void *data)
559 struct hsm_thread_data *thread_data = data;
560 struct mdt_thread_info *mti = thread_data->cdt_mti;
561 struct mdt_device *mdt = mti->mti_mdt;
562 struct coordinator *cdt = &mdt->mdt_coordinator;
563 struct hsm_scan_data hsd = { NULL };
564 time64_t last_housekeeping = 0;
569 /* set up hsd->request and max_requests */
570 hsd.max_requests = cdt->cdt_max_requests;
571 request_sz = hsd.max_requests * sizeof(*hsd.request);
572 hsd.request = thread_data->request;
574 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
575 mdt_obd_name(mdt), current_pid());
578 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
580 set_cdt_state(cdt, CDT_RUNNING);
582 /* Inform mdt_hsm_cdt_start(). */
583 wake_up_all(&cdt->cdt_waitq);
590 struct hsm_record_update *updates;
592 /* Limit execution of the expensive requests traversal
593 * to at most one second. This prevents repeatedly
594 * locking/unlocking the catalog for each request
595 * and preventing other HSM operations from happening
597 wait_event_interruptible_timeout(cdt->cdt_waitq,
598 kthread_should_stop() ||
599 cdt->cdt_wakeup_coordinator,
600 cfs_time_seconds(1));
602 cdt->cdt_wakeup_coordinator = false;
603 CDEBUG(D_HSM, "coordinator resumes\n");
605 if (kthread_should_stop()) {
606 CDEBUG(D_HSM, "Coordinator stops\n");
611 /* if coordinator is suspended continue to wait */
612 if (cdt->cdt_state == CDT_DISABLE) {
613 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
617 /* If no event, and no housekeeping to do, continue to
619 if (last_housekeeping + cdt->cdt_loop_period <=
620 ktime_get_real_seconds()) {
621 last_housekeeping = ktime_get_real_seconds();
622 hsd.housekeeping = true;
623 } else if (cdt->cdt_event) {
624 hsd.housekeeping = false;
629 cdt->cdt_event = false;
631 CDEBUG(D_HSM, "coordinator starts reading llog\n");
633 if (hsd.max_requests != cdt->cdt_max_requests) {
634 /* cdt_max_requests has changed,
635 * we need to allocate a new buffer
637 struct hsm_scan_request *tmp = NULL;
638 int max_requests = cdt->cdt_max_requests;
639 OBD_ALLOC_LARGE(tmp, max_requests *
640 sizeof(struct hsm_scan_request));
642 CERROR("Failed to resize request buffer, "
643 "keeping it at %d\n",
645 cdt->cdt_max_requests = hsd.max_requests;
647 OBD_FREE_LARGE(hsd.request, request_sz);
648 hsd.max_requests = max_requests;
649 request_sz = hsd.max_requests *
650 sizeof(struct hsm_scan_request);
657 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
662 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
664 if (list_empty(&cdt->cdt_agents)) {
665 CDEBUG(D_HSM, "no agent available, "
666 "coordinator sleeps\n");
670 /* Compute how many HAI we have in all the requests */
672 for (i = 0; i < hsd.request_cnt; i++) {
673 const struct hsm_scan_request *request =
676 updates_cnt += request->hal->hal_count;
679 /* Allocate a temporary array to store the cookies to
680 * update, and their status. */
681 updates_sz = updates_cnt * sizeof(*updates);
682 OBD_ALLOC(updates, updates_sz);
683 if (updates == NULL) {
684 CERROR("%s: Cannot allocate memory (%d o) "
686 mdt_obd_name(mdt), updates_sz, updates_cnt);
690 /* here hsd contains a list of requests to be started */
691 for (i = 0; i < hsd.request_cnt; i++) {
692 struct hsm_scan_request *request = &hsd.request[i];
693 struct hsm_action_list *hal = request->hal;
694 struct hsm_action_item *hai;
697 /* still room for work ? */
698 if (atomic_read(&cdt->cdt_request_count) >=
699 cdt->cdt_max_requests)
702 rc = mdt_hsm_agent_send(mti, hal, 0);
703 /* if failure, we suppose it is temporary
704 * if the copy tool failed to do the request
705 * it has to use hsm_progress
708 /* set up cookie vector to set records status
709 * after copy tools start or failed
711 hai = hai_first(hal);
712 for (j = 0; j < hal->hal_count; j++) {
713 updates[update_idx].cookie = hai->hai_cookie;
714 updates[update_idx].status =
715 (rc ? ARS_WAITING : ARS_STARTED);
722 rc = mdt_agent_record_update(mti->mti_env, mdt,
723 updates, update_idx);
725 CERROR("%s: mdt_agent_record_update() failed, "
726 "rc=%d, cannot update records "
728 mdt_obd_name(mdt), rc, update_idx);
731 OBD_FREE(updates, updates_sz);
734 /* free hal allocated by callback */
735 for (i = 0; i < hsd.request_cnt; i++) {
736 struct hsm_scan_request *request = &hsd.request[i];
738 OBD_FREE(request->hal, request->hal_sz);
743 OBD_FREE_LARGE(hsd.request, request_sz);
745 mdt_hsm_cdt_cleanup(mdt);
748 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
749 mdt_obd_name(mdt), current_pid(), rc);
751 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
753 mdt_obd_name(mdt), current_pid());
758 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
759 const struct lu_fid *fid,
760 const struct hsm_extent *he)
762 struct cdt_restore_handle *crh;
763 struct mdt_object *obj;
767 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
772 /* in V1 all file is restored
773 * crh->extent.start = he->offset;
774 * crh->extent.end = he->offset + he->length;
776 crh->crh_extent.start = 0;
777 crh->crh_extent.end = he->length;
778 /* get the layout lock */
779 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
780 obj = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
781 MDS_INODELOCK_LAYOUT);
783 GOTO(out_crh, rc = PTR_ERR(obj));
785 /* We do not keep a reference on the object during the restore
786 * which can be very long. */
787 mdt_object_put(mti->mti_env, obj);
789 mutex_lock(&cdt->cdt_restore_lock);
790 if (unlikely(cdt->cdt_state == CDT_STOPPED ||
791 cdt->cdt_state == CDT_STOPPING)) {
792 mutex_unlock(&cdt->cdt_restore_lock);
793 GOTO(out_lh, rc = -EAGAIN);
796 list_add_tail(&crh->crh_list, &cdt->cdt_restore_handle_list);
797 mutex_unlock(&cdt->cdt_restore_lock);
801 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
803 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
809 * lookup a restore handle by FID
810 * caller needs to hold cdt_restore_lock
811 * \param cdt [IN] coordinator
812 * \param fid [IN] FID
813 * \retval cdt_restore_handle found
814 * \retval NULL not found
816 struct cdt_restore_handle *cdt_restore_handle_find(struct coordinator *cdt,
817 const struct lu_fid *fid)
819 struct cdt_restore_handle *crh;
822 list_for_each_entry(crh, &cdt->cdt_restore_handle_list, crh_list) {
823 if (lu_fid_eq(&crh->crh_fid, fid))
830 void cdt_restore_handle_del(struct mdt_thread_info *mti,
831 struct coordinator *cdt, const struct lu_fid *fid)
833 struct cdt_restore_handle *crh;
835 /* give back layout lock */
836 mutex_lock(&cdt->cdt_restore_lock);
837 crh = cdt_restore_handle_find(cdt, fid);
839 list_del(&crh->crh_list);
840 mutex_unlock(&cdt->cdt_restore_lock);
845 /* XXX We pass a NULL object since the restore handle does not
846 * keep a reference on the object being restored. */
847 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
848 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
852 * data passed to llog_cat_process() callback
853 * to scan requests and take actions
855 struct hsm_restore_data {
856 struct mdt_thread_info *hrd_mti;
860 * llog_cat_process() callback, used to:
861 * - find restore request and allocate the restore handle
862 * \param env [IN] environment
863 * \param llh [IN] llog handle
864 * \param hdr [IN] llog record
865 * \param data [IN/OUT] cb data = struct hsm_restore_data
867 * \retval -ve failure
869 static int hsm_restore_cb(const struct lu_env *env,
870 struct llog_handle *llh,
871 struct llog_rec_hdr *hdr, void *data)
873 struct llog_agent_req_rec *larr;
874 struct hsm_restore_data *hrd;
875 struct hsm_action_item *hai;
876 struct mdt_thread_info *mti;
877 struct coordinator *cdt;
883 cdt = &mti->mti_mdt->mdt_coordinator;
885 larr = (struct llog_agent_req_rec *)hdr;
886 hai = &larr->arr_hai;
887 if (hai->hai_cookie > cdt->cdt_last_cookie)
888 /* update the cookie to avoid collision */
889 cdt->cdt_last_cookie = hai->hai_cookie + 1;
891 if (hai->hai_action != HSMA_RESTORE ||
892 agent_req_in_final_state(larr->arr_status))
895 /* restore request not in a final state */
897 /* force replay of restore requests left in started state from previous
898 * CDT context, to be canceled later if finally found to be incompatible
899 * when being re-started */
900 if (larr->arr_status == ARS_STARTED) {
901 larr->arr_status = ARS_WAITING;
902 larr->arr_req_change = ktime_get_real_seconds();
903 rc = llog_write(env, llh, hdr, hdr->lrh_index);
908 rc = cdt_restore_handle_add(mti, cdt, &hai->hai_fid, &hai->hai_extent);
914 * restore coordinator state at startup
915 * the goal is to take a layout lock for each registered restore request
916 * \param mti [IN] context
918 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
920 struct hsm_restore_data hrd;
926 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
932 static int hsm_init_ucred(struct lu_ucred *uc)
936 uc->uc_valid = UCRED_OLD;
945 uc->uc_suppgids[0] = -1;
946 uc->uc_suppgids[1] = -1;
947 uc->uc_cap = CFS_CAP_FS_MASK;
950 uc->uc_identity = NULL;
951 /* always record internal HSM activity if also enabled globally */
952 uc->uc_enable_audit = 1;
958 * initialize coordinator struct
959 * \param mdt [IN] device
961 * \retval -ve failure
963 int mdt_hsm_cdt_init(struct mdt_device *mdt)
965 struct coordinator *cdt = &mdt->mdt_coordinator;
966 struct mdt_thread_info *cdt_mti = NULL;
970 init_waitqueue_head(&cdt->cdt_waitq);
971 init_rwsem(&cdt->cdt_llog_lock);
972 init_rwsem(&cdt->cdt_agent_lock);
973 init_rwsem(&cdt->cdt_request_lock);
974 mutex_init(&cdt->cdt_restore_lock);
975 mutex_init(&cdt->cdt_state_lock);
976 set_cdt_state(cdt, CDT_STOPPED);
978 INIT_LIST_HEAD(&cdt->cdt_request_list);
979 INIT_LIST_HEAD(&cdt->cdt_agents);
980 INIT_LIST_HEAD(&cdt->cdt_restore_handle_list);
982 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
989 &cdt_request_cookie_hash_ops,
991 if (cdt->cdt_request_cookie_hash == NULL)
994 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
1001 &cdt_agent_record_hash_ops,
1003 if (cdt->cdt_agent_record_hash == NULL)
1004 GOTO(out_request_cookie_hash, rc = -ENOMEM);
1006 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
1008 GOTO(out_agent_record_hash, rc);
1010 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1011 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
1015 lu_context_enter(&cdt->cdt_session);
1016 cdt->cdt_env.le_ses = &cdt->cdt_session;
1018 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1019 LASSERT(cdt_mti != NULL);
1021 cdt_mti->mti_env = &cdt->cdt_env;
1022 cdt_mti->mti_mdt = mdt;
1024 hsm_init_ucred(mdt_ucred(cdt_mti));
1026 /* default values for /proc tunnables
1027 * can be override by MGS conf */
1028 cdt->cdt_default_archive_id = 1;
1029 cdt->cdt_grace_delay = 60;
1030 cdt->cdt_loop_period = 10;
1031 cdt->cdt_max_requests = 3;
1032 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1033 cdt->cdt_active_req_timeout = 3600;
1035 /* Initialize cdt_compound_id here to allow its usage for
1036 * delayed requests from RAoLU policy */
1037 atomic_set(&cdt->cdt_compound_id, ktime_get_real_seconds());
1039 /* by default do not remove archives on last unlink */
1040 cdt->cdt_remove_archive_on_last_unlink = false;
1045 lu_env_fini(&cdt->cdt_env);
1046 out_agent_record_hash:
1047 cfs_hash_putref(cdt->cdt_agent_record_hash);
1048 cdt->cdt_agent_record_hash = NULL;
1049 out_request_cookie_hash:
1050 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1051 cdt->cdt_request_cookie_hash = NULL;
1057 * free a coordinator thread
1058 * \param mdt [IN] device
1060 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
1062 struct coordinator *cdt = &mdt->mdt_coordinator;
1065 lu_context_exit(cdt->cdt_env.le_ses);
1066 lu_context_fini(cdt->cdt_env.le_ses);
1068 lu_env_fini(&cdt->cdt_env);
1070 cfs_hash_putref(cdt->cdt_agent_record_hash);
1071 cdt->cdt_agent_record_hash = NULL;
1073 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1074 cdt->cdt_request_cookie_hash = NULL;
1080 * start a coordinator thread
1081 * \param mdt [IN] device
1083 * \retval -ve failure
1085 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1087 struct coordinator *cdt = &mdt->mdt_coordinator;
1090 struct task_struct *task;
1092 struct hsm_thread_data thread_data;
1095 /* functions defined but not yet used
1096 * this avoid compilation warning
1098 ptr = dump_requests;
1100 rc = set_cdt_state(cdt, CDT_INIT);
1102 CERROR("%s: Coordinator already started or stopping\n",
1107 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
1108 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1110 /* just need to be larger than previous one */
1111 /* cdt_last_cookie is protected by cdt_llog_lock */
1112 cdt->cdt_last_cookie = ktime_get_real_seconds();
1113 atomic_set(&cdt->cdt_request_count, 0);
1114 atomic_set(&cdt->cdt_archive_count, 0);
1115 atomic_set(&cdt->cdt_restore_count, 0);
1116 atomic_set(&cdt->cdt_remove_count, 0);
1117 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1118 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1119 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1121 /* to avoid deadlock when start is made through /proc
1122 * /proc entries are created by the coordinator thread */
1124 /* set up list of started restore requests */
1125 thread_data.cdt_mti =
1126 lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1127 rc = mdt_hsm_pending_restore(thread_data.cdt_mti);
1129 CERROR("%s: cannot take the layout locks needed"
1130 " for registered restore: %d\n",
1131 mdt_obd_name(mdt), rc);
1133 if (mdt->mdt_bottom->dd_rdonly)
1136 /* Allocate the initial hsd.request[] vector*/
1137 request_sz = cdt->cdt_max_requests * sizeof(struct hsm_scan_request);
1138 OBD_ALLOC_LARGE(thread_data.request, request_sz);
1139 if (!thread_data.request) {
1140 set_cdt_state(cdt, CDT_STOPPED);
1144 task = kthread_run(mdt_coordinator, &thread_data, "hsm_cdtr");
1147 set_cdt_state(cdt, CDT_STOPPED);
1148 OBD_FREE(thread_data.request, request_sz);
1149 CERROR("%s: error starting coordinator thread: %d\n",
1150 mdt_obd_name(mdt), rc);
1152 cdt->cdt_task = task;
1153 wait_event(cdt->cdt_waitq,
1154 cdt->cdt_state != CDT_INIT);
1155 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1164 * stop a coordinator thread
1165 * \param mdt [IN] device
1167 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1169 struct coordinator *cdt = &mdt->mdt_coordinator;
1173 /* stop coordinator thread */
1174 rc = set_cdt_state(cdt, CDT_STOPPING);
1176 kthread_stop(cdt->cdt_task);
1177 cdt->cdt_task = NULL;
1178 set_cdt_state(cdt, CDT_STOPPED);
1184 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1185 const struct lu_fid *fid,
1188 struct mdt_object *obj;
1192 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1194 GOTO(out, rc = PTR_ERR(obj));
1196 if (mh.mh_flags & HS_EXISTS &&
1197 mh.mh_arch_id == archive_id)
1198 GOTO(out_obj, rc = 0);
1200 mh.mh_flags |= HS_EXISTS;
1201 mh.mh_arch_id = archive_id;
1202 rc = mdt_hsm_attr_set(mti, obj, &mh);
1205 mdt_object_put(mti->mti_env, obj);
1211 * register all requests from an hal in the memory list
1212 * \param mti [IN] context
1213 * \param hal [IN] request
1214 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1215 * which is running the CT
1217 * \retval -ve failure
1219 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1220 struct hsm_action_list *hal, struct obd_uuid *uuid)
1222 struct mdt_device *mdt = mti->mti_mdt;
1223 struct coordinator *cdt = &mdt->mdt_coordinator;
1224 struct hsm_action_item *hai;
1228 /* register request in memory list */
1229 hai = hai_first(hal);
1230 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1231 struct cdt_agent_req *car;
1233 /* in case of a cancel request, we first mark the ondisk
1234 * record of the request we want to stop as canceled
1235 * this does not change the cancel record
1236 * it will be done when updating the request status
1238 if (hai->hai_action == HSMA_CANCEL) {
1239 struct hsm_record_update update = {
1240 .cookie = hai->hai_cookie,
1241 .status = ARS_CANCELED,
1244 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1247 CERROR("%s: mdt_agent_record_update() failed, "
1248 "rc=%d, cannot update status to %s "
1249 "for cookie %#llx\n",
1250 mdt_obd_name(mdt), rc,
1251 agent_req_status2name(ARS_CANCELED),
1256 /* find the running request to set it canceled */
1257 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1259 car->car_canceled = 1;
1260 /* uuid has to be changed to the one running the
1261 * request to cancel */
1262 *uuid = car->car_uuid;
1263 mdt_cdt_put_request(car);
1265 /* no need to memorize cancel request
1266 * this also avoid a deadlock when we receive
1267 * a purge all requests command
1272 if (hai->hai_action == HSMA_ARCHIVE) {
1273 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1274 hal->hal_archive_id);
1281 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1282 hal->hal_archive_id, hal->hal_flags,
1285 GOTO(out, rc = PTR_ERR(car));
1287 rc = mdt_cdt_add_request(cdt, car);
1289 mdt_cdt_free_request(car);
1296 * swap layouts between 2 fids
1297 * \param mti [IN] context
1300 * \param mh_common [IN] MD HSM
1302 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1303 struct mdt_object *obj, const struct lu_fid *dfid,
1304 struct md_hsm *mh_common)
1306 struct mdt_object *dobj;
1307 struct mdt_lock_handle *dlh;
1311 if (!mdt_object_exists(obj))
1312 GOTO(out, rc = -ENOENT);
1314 /* we already have layout lock on obj so take only
1316 dlh = &mti->mti_lh[MDT_LH_OLD];
1317 mdt_lock_reg_init(dlh, LCK_EX);
1318 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1320 GOTO(out, rc = PTR_ERR(dobj));
1322 /* if copy tool closes the volatile before sending the final
1323 * progress through llapi_hsm_copy_end(), all the objects
1324 * are removed and mdd_swap_layout LBUG */
1325 if (!mdt_object_exists(dobj)) {
1326 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1327 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1328 GOTO(out_dobj, rc = -ENOENT);
1330 /* Since we only handle restores here, unconditionally use
1331 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1332 * be preserved in case of failure during swap_layout and not
1333 * leave a file in an intermediate but incoherent state.
1334 * But need to setup HSM xattr of data FID before, reuse
1335 * mti and mh presets for FID in hsm_cdt_request_completed(),
1336 * only need to clear RELEASED and DIRTY.
1338 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1339 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1341 rc = mo_swap_layouts(mti->mti_env,
1342 mdt_object_child(obj),
1343 mdt_object_child(dobj),
1344 SWAP_LAYOUTS_MDS_HSM);
1347 mdt_object_unlock_put(mti, dobj, dlh, 1);
1353 * update status of a completed request
1354 * \param mti [IN] context
1355 * \param pgs [IN] progress of the copy tool
1357 * \retval -ve failure
1359 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1360 struct hsm_progress_kernel *pgs,
1361 const struct cdt_agent_req *car,
1362 enum agent_req_status *status)
1364 const struct lu_env *env = mti->mti_env;
1365 struct mdt_device *mdt = mti->mti_mdt;
1366 struct coordinator *cdt = &mdt->mdt_coordinator;
1367 struct mdt_object *obj = NULL;
1368 int cl_flags = 0, rc = 0;
1371 bool need_changelog = true;
1374 /* default is to retry */
1375 *status = ARS_WAITING;
1377 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1378 * if error/removed continue anyway to get correct reporting done */
1379 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1380 /* we will update MD HSM only if needed */
1381 is_mh_changed = false;
1383 /* no need to change mh->mh_arch_id
1384 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1386 if (pgs->hpk_errval != 0) {
1387 switch (pgs->hpk_errval) {
1389 /* the copy tool does not support cancel
1390 * so the cancel request is failed
1391 * As we cannot distinguish a cancel progress
1392 * from another action progress (they have the
1393 * same cookie), we suppose here the CT returns
1394 * ENOSYS only if does not support cancel
1396 /* this can also happen when cdt calls it to
1397 * for a timed out request */
1398 *status = ARS_FAILED;
1399 /* to have a cancel event in changelog */
1400 pgs->hpk_errval = ECANCELED;
1403 /* the request record has already been set to
1404 * ARS_CANCELED, this set the cancel request
1406 *status = ARS_SUCCEED;
1409 /* retry only if current policy or requested, and
1410 * object is not on error/removed */
1411 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1412 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1413 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1417 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1418 CERROR("%s: Request %#llx on "DFID
1419 " failed, error code %d too large\n",
1421 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1423 hsm_set_cl_error(&cl_flags,
1424 CLF_HSM_ERROVERFLOW);
1427 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1430 switch (car->car_hai->hai_action) {
1432 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1435 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1438 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1441 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1442 CERROR("%s: Failed request %#llx on "DFID
1443 " cannot be a CANCEL\n",
1446 PFID(&pgs->hpk_fid));
1449 CERROR("%s: Failed request %#llx on "DFID
1450 " %d is an unknown action\n",
1452 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1453 car->car_hai->hai_action);
1458 *status = ARS_SUCCEED;
1459 switch (car->car_hai->hai_action) {
1461 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1462 /* set ARCHIVE keep EXIST and clear LOST and
1464 mh.mh_arch_ver = pgs->hpk_data_version;
1465 mh.mh_flags |= HS_ARCHIVED;
1466 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1467 is_mh_changed = true;
1470 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1472 /* do not clear RELEASED and DIRTY here
1473 * this will occur in hsm_swap_layouts()
1476 /* Restoring has changed the file version on
1478 mh.mh_arch_ver = pgs->hpk_data_version;
1479 is_mh_changed = true;
1482 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1483 /* clear ARCHIVED EXISTS and LOST */
1484 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1485 is_mh_changed = true;
1488 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1489 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1492 PFID(&pgs->hpk_fid));
1495 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1497 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1498 car->car_hai->hai_action);
1504 /* rc != 0 means error when analysing action, it may come from
1505 * a crasy CT no need to manage DIRTY
1506 * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1509 if (rc == 0 && !IS_ERR(obj))
1510 hsm_set_cl_flags(&cl_flags,
1511 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1513 /* unlock is done later, after layout lock management */
1514 if (is_mh_changed && !IS_ERR(obj))
1515 rc = mdt_hsm_attr_set(mti, obj, &mh);
1517 /* we give back layout lock only if restore was successful or
1518 * if no retry will be attempted and if object is still alive,
1519 * in other cases we just unlock the object */
1520 if (car->car_hai->hai_action == HSMA_RESTORE) {
1521 /* restore in data FID done, we swap the layouts
1522 * only if restore is successful */
1523 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1524 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1527 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1528 *status = ARS_FAILED;
1529 pgs->hpk_errval = -rc;
1532 /* we have to retry, so keep layout lock */
1533 if (*status == ARS_WAITING)
1536 /* restore special case, need to create ChangeLog record
1537 * before to give back layout lock to avoid concurrent
1538 * file updater to post out of order ChangeLog */
1539 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1540 &car->car_hai->hai_fid);
1541 need_changelog = false;
1543 cdt_restore_handle_del(mti, cdt, &car->car_hai->hai_fid);
1549 /* always add a ChangeLog record */
1551 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1552 &car->car_hai->hai_fid);
1555 mdt_object_put(mti->mti_env, obj);
1561 * update status of a request
1562 * \param mti [IN] context
1563 * \param pgs [IN] progress of the copy tool
1565 * \retval -ve failure
1567 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1568 struct hsm_progress_kernel *pgs)
1570 struct mdt_device *mdt = mti->mti_mdt;
1571 struct coordinator *cdt = &mdt->mdt_coordinator;
1572 struct cdt_agent_req *car;
1576 /* no coordinator started, so we cannot serve requests */
1577 if (cdt->cdt_state == CDT_STOPPED)
1580 /* first do sanity checks */
1581 car = mdt_cdt_update_request(cdt, pgs);
1583 CERROR("%s: Cannot find running request for cookie %#llx"
1586 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1588 RETURN(PTR_ERR(car));
1591 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1592 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1593 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1594 hsm_copytool_action2name(car->car_hai->hai_action),
1595 pgs->hpk_flags, pgs->hpk_errval,
1596 PFID(&car->car_hai->hai_fid),
1597 PFID(&car->car_hai->hai_dfid));
1599 /* progress is done on FID or data FID depending of the action and
1600 * of the copy progress */
1601 /* for restore progress is used to send back the data FID to cdt */
1602 if (car->car_hai->hai_action == HSMA_RESTORE &&
1603 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1604 car->car_hai->hai_dfid = pgs->hpk_fid;
1606 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1607 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1608 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1609 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1610 CERROR("%s: Progress on "DFID" for cookie %#llx"
1611 " does not match request FID "DFID" nor data FID "
1614 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1615 PFID(&car->car_hai->hai_fid),
1616 PFID(&car->car_hai->hai_dfid));
1617 GOTO(out, rc = -EINVAL);
1620 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1621 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1622 " is not coherent (err=%d and not completed"
1625 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1626 hsm_copytool_action2name(car->car_hai->hai_action),
1627 pgs->hpk_errval, pgs->hpk_flags);
1628 GOTO(out, rc = -EINVAL);
1631 /* now progress is valid */
1633 /* we use a root like ucred */
1634 hsm_init_ucred(mdt_ucred(mti));
1636 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1637 enum agent_req_status status;
1638 struct hsm_record_update update;
1641 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1643 CDEBUG(D_HSM, "updating record: fid="DFID" cookie=%#llx action=%s "
1645 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1646 hsm_copytool_action2name(car->car_hai->hai_action),
1647 agent_req_status2name(status));
1649 /* update record first (LU-9075) */
1650 update.cookie = pgs->hpk_cookie;
1651 update.status = status;
1653 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1656 CERROR("%s: mdt_agent_record_update() failed,"
1657 " rc=%d, cannot update status to %s"
1658 " for cookie %#llx\n",
1659 mdt_obd_name(mdt), rc1,
1660 agent_req_status2name(status),
1662 rc = (rc != 0 ? rc : rc1);
1664 /* then remove request from memory list (LU-9075) */
1665 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1667 /* ct has completed a request, so a slot is available,
1668 * signal the coordinator to find new work */
1669 mdt_hsm_cdt_event(cdt);
1671 /* if copytool send a progress on a canceled request
1672 * we inform copytool it should stop
1674 if (car->car_canceled == 1)
1680 /* remove ref got from mdt_cdt_update_request() */
1681 mdt_cdt_put_request(car);
1688 * data passed to llog_cat_process() callback
1689 * to cancel requests
1691 struct hsm_cancel_all_data {
1692 struct mdt_device *mdt;
1696 * llog_cat_process() callback, used to:
1697 * - purge all requests
1698 * \param env [IN] environment
1699 * \param llh [IN] llog handle
1700 * \param hdr [IN] llog record
1701 * \param data [IN] cb data = struct hsm_cancel_all_data
1703 * \retval -ve failure
1705 static int mdt_cancel_all_cb(const struct lu_env *env,
1706 struct llog_handle *llh,
1707 struct llog_rec_hdr *hdr, void *data)
1709 struct llog_agent_req_rec *larr;
1710 struct hsm_cancel_all_data *hcad;
1714 larr = (struct llog_agent_req_rec *)hdr;
1716 if (larr->arr_status == ARS_WAITING ||
1717 larr->arr_status == ARS_STARTED) {
1718 larr->arr_status = ARS_CANCELED;
1719 larr->arr_req_change = ktime_get_real_seconds();
1720 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1727 * cancel all actions
1728 * \param obd [IN] MDT device
1730 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1733 struct lu_context session;
1734 struct mdt_thread_info *mti;
1735 struct coordinator *cdt = &mdt->mdt_coordinator;
1736 struct cdt_agent_req *car;
1737 struct hsm_action_list *hal = NULL;
1738 struct hsm_action_item *hai;
1739 struct hsm_cancel_all_data hcad;
1740 int hal_sz = 0, hal_len, rc;
1741 enum cdt_states old_state;
1744 rc = lu_env_init(&env, LCT_MD_THREAD);
1748 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1749 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1753 lu_context_enter(&session);
1754 env.le_ses = &session;
1756 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1757 LASSERT(mti != NULL);
1759 mti->mti_env = &env;
1762 hsm_init_ucred(mdt_ucred(mti));
1764 mutex_lock(&cdt->cdt_state_lock);
1765 old_state = cdt->cdt_state;
1767 /* disable coordinator */
1768 rc = set_cdt_state_locked(cdt, CDT_DISABLE);
1770 GOTO(out_cdt_state_unlock, rc);
1772 /* send cancel to all running requests */
1773 down_read(&cdt->cdt_request_lock);
1774 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1775 mdt_cdt_get_request(car);
1776 /* request is not yet removed from list, it will be done
1777 * when copytool will return progress
1780 if (car->car_hai->hai_action == HSMA_CANCEL) {
1781 mdt_cdt_put_request(car);
1786 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1787 cfs_size_round(car->car_hai->hai_len);
1789 if (hal_len > hal_sz && hal_sz > 0) {
1790 /* not enough room, free old buffer */
1791 OBD_FREE(hal, hal_sz);
1795 /* empty buffer, allocate one */
1798 OBD_ALLOC(hal, hal_sz);
1800 mdt_cdt_put_request(car);
1801 up_read(&cdt->cdt_request_lock);
1802 GOTO(out_cdt_state, rc = -ENOMEM);
1806 hal->hal_version = HAL_VERSION;
1807 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1809 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1810 hal->hal_compound_id = car->car_compound_id;
1811 hal->hal_archive_id = car->car_archive_id;
1812 hal->hal_flags = car->car_flags;
1815 hai = hai_first(hal);
1816 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1817 hai->hai_action = HSMA_CANCEL;
1820 /* it is possible to safely call mdt_hsm_agent_send()
1821 * (ie without a deadlock on cdt_request_lock), because the
1822 * write lock is taken only if we are not in purge mode
1823 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1824 * nor mdt_cdt_remove_request())
1826 /* no conflict with cdt thread because cdt is disable and we
1827 * have the request lock */
1828 mdt_hsm_agent_send(mti, hal, 1);
1830 mdt_cdt_put_request(car);
1832 up_read(&cdt->cdt_request_lock);
1835 OBD_FREE(hal, hal_sz);
1837 /* cancel all on-disk records */
1840 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1841 &hcad, 0, 0, WRITE);
1843 /* Enable coordinator, unless the coordinator was stopping. */
1844 set_cdt_state_locked(cdt, old_state);
1845 out_cdt_state_unlock:
1846 mutex_unlock(&cdt->cdt_state_lock);
1848 lu_context_exit(&session);
1849 lu_context_fini(&session);
1857 * check if a request is compatible with file status
1858 * \param hai [IN] request description
1859 * \param archive_id [IN] request archive id
1860 * \param rq_flags [IN] request flags
1861 * \param hsm [IN] file HSM metadata
1864 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1865 u32 archive_id, u64 rq_flags,
1866 const struct md_hsm *hsm)
1868 int is_compat = false;
1872 hsm_flags = hsm->mh_flags;
1873 switch (hai->hai_action) {
1875 if (!(hsm_flags & HS_NOARCHIVE) &&
1876 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1879 if (hsm_flags & HS_EXISTS &&
1881 archive_id != hsm->mh_arch_id)
1886 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1887 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1891 if (!(hsm_flags & HS_RELEASED) &&
1892 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1899 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1900 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1901 PFID(&hai->hai_fid),
1902 hsm_copytool_action2name(hai->hai_action), rq_flags,
1903 hai->hai_extent.offset, hai->hai_extent.length,
1905 (is_compat ? "compatible" : "uncompatible"));
1911 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1913 static const struct {
1917 } hsm_policy_names[] = {
1918 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1919 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1924 * convert a policy name to a bit
1925 * \param name [IN] policy name
1927 * \retval policy bit
1929 static __u64 hsm_policy_str2bit(const char *name)
1933 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1934 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1935 strcmp(hsm_policy_names[i].name, name) == 0)
1936 return hsm_policy_names[i].bit;
1941 * convert a policy bit field to a string
1942 * \param mask [IN] policy bit field
1943 * \param hexa [IN] print mask before bit names
1944 * \param buffer [OUT] string
1945 * \param count [IN] size of buffer
1947 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1955 seq_printf(m, "(%#llx) ", mask);
1957 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1960 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1961 if (hsm_policy_names[j].bit == bit)
1965 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1967 seq_printf(m, "%s ", hsm_policy_names[j].name);
1969 /* remove last ' ' */
1974 /* methods to read/write HSM policy flags */
1975 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1977 struct mdt_device *mdt = m->private;
1978 struct coordinator *cdt = &mdt->mdt_coordinator;
1981 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1986 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1987 size_t count, loff_t *off)
1989 struct seq_file *m = file->private_data;
1990 struct mdt_device *mdt = m->private;
1991 struct coordinator *cdt = &mdt->mdt_coordinator;
1992 char *start, *token, sign;
1995 __u64 add_mask, remove_mask, set_mask;
1999 if (count + 1 > PAGE_SIZE)
2002 OBD_ALLOC(buf, count + 1);
2006 if (copy_from_user(buf, buffer, count))
2007 GOTO(out, rc = -EFAULT);
2012 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
2015 add_mask = remove_mask = set_mask = 0;
2017 token = strsep(&start, "\n ");
2023 if (sign == '-' || sign == '+')
2026 policy = hsm_policy_str2bit(token);
2028 CWARN("%s: '%s' is unknown, "
2029 "supported policies are:\n", mdt_obd_name(mdt),
2031 hsm_policy_bit2str(m, 0, false);
2032 GOTO(out, rc = -EINVAL);
2036 remove_mask |= policy;
2046 } while (start != NULL);
2048 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
2049 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
2051 /* if no sign in all string, it is a clear and set
2052 * if some sign found, all unsigned are converted
2054 * P1 P2 = set to P1 and P2
2055 * P1 -P2 = add P1 clear P2 same as +P1 -P2
2057 if (remove_mask == 0 && add_mask == 0) {
2058 cdt->cdt_policy = set_mask;
2060 cdt->cdt_policy |= set_mask | add_mask;
2061 cdt->cdt_policy &= ~remove_mask;
2064 GOTO(out, rc = count);
2067 OBD_FREE(buf, count + 1);
2070 LPROC_SEQ_FOPS(mdt_hsm_policy);
2072 #define GENERATE_PROC_METHOD(VAR) \
2073 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
2075 struct mdt_device *mdt = m->private; \
2076 struct coordinator *cdt = &mdt->mdt_coordinator; \
2079 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
2083 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
2084 size_t count, loff_t *off) \
2087 struct seq_file *m = file->private_data; \
2088 struct mdt_device *mdt = m->private; \
2089 struct coordinator *cdt = &mdt->mdt_coordinator; \
2094 rc = kstrtouint_from_user(buffer, count, 0, &val); \
2105 GENERATE_PROC_METHOD(cdt_loop_period)
2106 GENERATE_PROC_METHOD(cdt_grace_delay)
2107 GENERATE_PROC_METHOD(cdt_active_req_timeout)
2108 GENERATE_PROC_METHOD(cdt_max_requests)
2109 GENERATE_PROC_METHOD(cdt_default_archive_id)
2112 * procfs write method for MDT/hsm_control
2113 * proc entry is in mdt directory so data is mdt obd_device pointer
2115 #define CDT_ENABLE_CMD "enabled"
2116 #define CDT_STOP_CMD "shutdown"
2117 #define CDT_DISABLE_CMD "disabled"
2118 #define CDT_PURGE_CMD "purge"
2119 #define CDT_HELP_CMD "help"
2120 #define CDT_MAX_CMD_LEN 10
2123 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
2124 size_t count, loff_t *off)
2126 struct seq_file *m = file->private_data;
2127 struct obd_device *obd = m->private;
2128 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2129 struct coordinator *cdt = &(mdt->mdt_coordinator);
2131 char kernbuf[CDT_MAX_CMD_LEN];
2134 if (count == 0 || count >= sizeof(kernbuf))
2137 if (copy_from_user(kernbuf, buffer, count))
2141 if (kernbuf[count - 1] == '\n')
2142 kernbuf[count - 1] = 0;
2145 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2146 if (cdt->cdt_state == CDT_DISABLE) {
2147 rc = set_cdt_state(cdt, CDT_RUNNING);
2148 mdt_hsm_cdt_event(cdt);
2149 wake_up(&cdt->cdt_waitq);
2151 rc = mdt_hsm_cdt_start(mdt);
2153 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2154 if ((cdt->cdt_state == CDT_STOPPING) ||
2155 (cdt->cdt_state == CDT_STOPPED)) {
2156 CERROR("%s: Coordinator already stopped\n",
2160 rc = mdt_hsm_cdt_stop(mdt);
2162 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2163 if ((cdt->cdt_state == CDT_STOPPING) ||
2164 (cdt->cdt_state == CDT_STOPPED)) {
2165 CERROR("%s: Coordinator is stopped\n",
2169 rc = set_cdt_state(cdt, CDT_DISABLE);
2171 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2172 rc = hsm_cancel_all_actions(mdt);
2173 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2181 CERROR("%s: Valid coordinator control commands are: "
2182 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2183 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2184 CDT_PURGE_CMD, CDT_HELP_CMD);
2192 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2194 struct obd_device *obd = m->private;
2195 struct coordinator *cdt;
2198 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2200 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2206 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2212 for (i = 0; i < 8 * sizeof(mask); i++) {
2213 if (mask & (1UL << i)) {
2214 seq_printf(m, "%s%s", first ? "" : " ",
2215 hsm_copytool_action2name(i));
2225 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2227 struct mdt_device *mdt = m->private;
2228 struct coordinator *cdt = &mdt->mdt_coordinator;
2230 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2234 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2236 struct mdt_device *mdt = m->private;
2237 struct coordinator *cdt = &mdt->mdt_coordinator;
2239 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2243 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2245 struct mdt_device *mdt = m->private;
2246 struct coordinator *cdt = &mdt->mdt_coordinator;
2248 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2251 static inline enum hsm_copytool_action
2252 hsm_copytool_name2action(const char *name)
2254 if (strcasecmp(name, "NOOP") == 0)
2256 else if (strcasecmp(name, "ARCHIVE") == 0)
2257 return HSMA_ARCHIVE;
2258 else if (strcasecmp(name, "RESTORE") == 0)
2259 return HSMA_RESTORE;
2260 else if (strcasecmp(name, "REMOVE") == 0)
2262 else if (strcasecmp(name, "CANCEL") == 0)
2269 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2270 size_t user_count, __u64 *mask)
2272 char *buf, *pos, *name;
2278 if (!(user_count < 4096))
2281 buf_size = user_count + 1;
2283 OBD_ALLOC(buf, buf_size);
2287 if (copy_from_user(buf, user_buf, buf_size - 1))
2288 GOTO(out, rc = -EFAULT);
2290 buf[buf_size - 1] = '\0';
2293 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2299 action = hsm_copytool_name2action(name);
2301 GOTO(out, rc = -EINVAL);
2303 new_mask |= (1UL << action);
2309 OBD_FREE(buf, buf_size);
2315 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2316 size_t count, loff_t *off)
2318 struct seq_file *m = file->private_data;
2319 struct mdt_device *mdt = m->private;
2320 struct coordinator *cdt = &mdt->mdt_coordinator;
2322 return mdt_write_hsm_request_mask(file, buf, count,
2323 &cdt->cdt_user_request_mask);
2327 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2328 size_t count, loff_t *off)
2330 struct seq_file *m = file->private_data;
2331 struct mdt_device *mdt = m->private;
2332 struct coordinator *cdt = &mdt->mdt_coordinator;
2334 return mdt_write_hsm_request_mask(file, buf, count,
2335 &cdt->cdt_group_request_mask);
2339 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2340 size_t count, loff_t *off)
2342 struct seq_file *m = file->private_data;
2343 struct mdt_device *mdt = m->private;
2344 struct coordinator *cdt = &mdt->mdt_coordinator;
2346 return mdt_write_hsm_request_mask(file, buf, count,
2347 &cdt->cdt_other_request_mask);
2350 static int mdt_hsm_cdt_raolu_seq_show(struct seq_file *m, void *data)
2352 struct mdt_device *mdt = m->private;
2353 struct coordinator *cdt = &mdt->mdt_coordinator;
2356 seq_printf(m, "%d\n", (int)cdt->cdt_remove_archive_on_last_unlink);
2361 mdt_hsm_cdt_raolu_seq_write(struct file *file, const char __user *buffer,
2362 size_t count, loff_t *off)
2365 struct seq_file *m = file->private_data;
2366 struct mdt_device *mdt = m->private;
2367 struct coordinator *cdt = &mdt->mdt_coordinator;
2372 rc = kstrtobool_from_user(buffer, count, &val);
2376 cdt->cdt_remove_archive_on_last_unlink = val;
2380 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2381 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2382 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2383 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2384 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2385 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2386 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2387 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2388 LPROC_SEQ_FOPS(mdt_hsm_cdt_raolu);
2390 /* Read-only proc files for request counters */
2391 static int mdt_hsm_cdt_archive_count_seq_show(struct seq_file *m, void *data)
2393 struct mdt_device *mdt = m->private;
2394 struct coordinator *cdt = &mdt->mdt_coordinator;
2397 seq_printf(m, "%d\n", atomic_read(&cdt->cdt_archive_count));
2401 static int mdt_hsm_cdt_restore_count_seq_show(struct seq_file *m, void *data)
2403 struct mdt_device *mdt = m->private;
2404 struct coordinator *cdt = &mdt->mdt_coordinator;
2407 seq_printf(m, "%d\n", atomic_read(&cdt->cdt_restore_count));
2411 static int mdt_hsm_cdt_remove_count_seq_show(struct seq_file *m, void *data)
2413 struct mdt_device *mdt = m->private;
2414 struct coordinator *cdt = &mdt->mdt_coordinator;
2417 seq_printf(m, "%d\n", atomic_read(&cdt->cdt_remove_count));
2421 LPROC_SEQ_FOPS_RO(mdt_hsm_cdt_archive_count);
2422 LPROC_SEQ_FOPS_RO(mdt_hsm_cdt_restore_count);
2423 LPROC_SEQ_FOPS_RO(mdt_hsm_cdt_remove_count);
2425 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2427 .fops = &mdt_hsm_agent_fops },
2428 { .name = "actions",
2429 .fops = &mdt_hsm_actions_fops,
2430 .proc_mode = 0444 },
2431 { .name = "default_archive_id",
2432 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2433 { .name = "grace_delay",
2434 .fops = &mdt_hsm_cdt_grace_delay_fops },
2435 { .name = "loop_period",
2436 .fops = &mdt_hsm_cdt_loop_period_fops },
2437 { .name = "max_requests",
2438 .fops = &mdt_hsm_cdt_max_requests_fops },
2440 .fops = &mdt_hsm_policy_fops },
2441 { .name = "active_request_timeout",
2442 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2443 { .name = "active_requests",
2444 .fops = &mdt_hsm_active_requests_fops },
2445 { .name = "user_request_mask",
2446 .fops = &mdt_hsm_user_request_mask_fops, },
2447 { .name = "group_request_mask",
2448 .fops = &mdt_hsm_group_request_mask_fops, },
2449 { .name = "other_request_mask",
2450 .fops = &mdt_hsm_other_request_mask_fops, },
2451 { .name = "remove_archive_on_last_unlink",
2452 .fops = &mdt_hsm_cdt_raolu_fops, },
2453 { .name = "archive_count",
2454 .fops = &mdt_hsm_cdt_archive_count_fops, },
2455 { .name = "restore_count",
2456 .fops = &mdt_hsm_cdt_restore_count_fops, },
2457 { .name = "remove_count",
2458 .fops = &mdt_hsm_cdt_remove_count_fops, },