4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2016, Intel Corporation.
27 * Use is subject to license terms.
30 * lustre/mdt/mdt_coordinator.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_export.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_kernelcomm.h>
48 #include "mdt_internal.h"
50 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
53 * get obj and HSM attributes on a fid
54 * \param mti [IN] context
55 * \param fid [IN] object fid
56 * \param hsm [OUT] HSM meta data
57 * \retval obj or error (-ENOENT if not found)
59 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
60 const struct lu_fid *fid,
64 struct mdt_object *obj;
72 /* find object by FID */
73 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
77 if (!mdt_object_exists(obj)) {
79 mdt_object_put(mti->mti_env, obj);
80 RETURN(ERR_PTR(-ENOENT));
83 rc = mdt_attr_get_complex(mti, obj, ma);
85 mdt_object_put(mti->mti_env, obj);
89 if (ma->ma_valid & MA_HSM)
92 memset(hsm, 0, sizeof(*hsm));
97 void mdt_hsm_dump_hal(int level, const char *prefix,
98 struct hsm_action_list *hal)
101 struct hsm_action_item *hai;
104 CDEBUG(level, "%s: HAL header: version %X count %d compound %#llx"
105 " archive_id %d flags %#llx\n",
106 prefix, hal->hal_version, hal->hal_count,
107 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
109 hai = hai_first(hal);
110 for (i = 0; i < hal->hal_count; i++) {
111 sz = hai->hai_len - sizeof(*hai);
112 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
113 " compound/cookie=%#llx/%#llx"
114 " action=%s extent=%#llx-%#llx gid=%#llx"
115 " datalen=%d data=[%s]\n",
117 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
118 hal->hal_compound_id, hai->hai_cookie,
119 hsm_copytool_action2name(hai->hai_action),
120 hai->hai_extent.offset,
121 hai->hai_extent.length,
123 hai_dump_data_field(hai, buf, sizeof(buf)));
129 * data passed to llog_cat_process() callback
130 * to scan requests and take actions
132 struct hsm_scan_request {
135 struct hsm_action_list *hal;
138 struct hsm_scan_data {
139 struct mdt_thread_info *mti;
140 char fs_name[MTI_NAME_MAXLEN+1];
141 /* 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 * llog_cat_process() callback, used to:
157 * - find waiting request and start action
158 * - purge canceled and done requests
159 * \param env [IN] environment
160 * \param llh [IN] llog handle
161 * \param hdr [IN] llog record
162 * \param data [IN/OUT] cb data = struct hsm_scan_data
164 * \retval -ve failure
166 static int mdt_coordinator_cb(const struct lu_env *env,
167 struct llog_handle *llh,
168 struct llog_rec_hdr *hdr,
171 struct llog_agent_req_rec *larr;
172 struct hsm_scan_data *hsd;
173 struct hsm_action_item *hai;
174 struct mdt_device *mdt;
175 struct coordinator *cdt;
180 mdt = hsd->mti->mti_mdt;
181 cdt = &mdt->mdt_coordinator;
183 larr = (struct llog_agent_req_rec *)hdr;
184 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
185 switch (larr->arr_status) {
188 struct hsm_scan_request *request;
190 /* Are agents full? */
191 if (atomic_read(&cdt->cdt_request_count) >=
192 cdt->cdt_max_requests)
195 /* first search whether the request is found in the
196 * list we have built. */
198 for (i = 0; i < hsd->request_cnt; i++) {
199 if (hsd->request[i].hal->hal_compound_id ==
200 larr->arr_compound_id) {
201 request = &hsd->request[i];
207 struct hsm_action_list *hal;
209 if (hsd->request_cnt == hsd->max_requests) {
210 if (!hsd->housekeeping) {
211 /* The request array is full,
212 * stop here. There might be
213 * more known requests that
214 * could be merged, but this
215 * avoid analyzing too many
216 * llogs for minor gains.
218 RETURN(LLOG_PROC_BREAK);
220 /* Unknown request and no more room
221 * for a new request. Continue to scan
222 * to find other entries for already
229 request = &hsd->request[hsd->request_cnt];
231 /* allocates hai vector size just needs to be large
234 sizeof(*request->hal) +
235 cfs_size_round(MTI_NAME_MAXLEN+1) +
236 2 * cfs_size_round(larr->arr_hai.hai_len);
237 OBD_ALLOC(hal, request->hal_sz);
240 hal->hal_version = HAL_VERSION;
241 strlcpy(hal->hal_fsname, hsd->fs_name,
242 MTI_NAME_MAXLEN + 1);
243 hal->hal_compound_id = larr->arr_compound_id;
244 hal->hal_archive_id = larr->arr_archive_id;
245 hal->hal_flags = larr->arr_flags;
247 request->hal_used_sz = hal_size(hal);
250 hai = hai_first(hal);
252 /* request is known */
253 /* we check if record archive num is the same as the
254 * known request, if not we will serve it in multiple
255 * time because we do not know if the agent can serve
257 * a use case is a compound made of multiple restore
258 * where the files are not archived in the same backend
260 if (larr->arr_archive_id !=
261 request->hal->hal_archive_id)
264 if (request->hal_sz <
265 request->hal_used_sz +
266 cfs_size_round(larr->arr_hai.hai_len)) {
267 /* Not enough room, need an extension */
271 sz = 2 * request->hal_sz;
272 OBD_ALLOC(hal_buffer, sz);
275 memcpy(hal_buffer, request->hal,
276 request->hal_used_sz);
277 OBD_FREE(request->hal,
279 request->hal = hal_buffer;
280 request->hal_sz = sz;
282 hai = hai_first(request->hal);
283 for (i = 0; i < request->hal->hal_count; i++)
286 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
287 hai->hai_cookie = larr->arr_hai.hai_cookie;
288 hai->hai_gid = larr->arr_hai.hai_gid;
290 request->hal_used_sz += cfs_size_round(hai->hai_len);
291 request->hal->hal_count++;
293 if (hai->hai_action != HSMA_CANCEL)
294 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
295 llh->lgh_hdr->llh_cat_idx,
300 struct hsm_progress_kernel pgs;
301 struct cdt_agent_req *car;
302 cfs_time_t now = cfs_time_current_sec();
305 if (!hsd->housekeeping)
308 /* we search for a running request
309 * error may happen if coordinator crashes or stopped
310 * with running request
312 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie);
314 last = larr->arr_req_change;
316 last = car->car_req_update;
317 mdt_cdt_put_request(car);
320 /* test if request too long, if yes cancel it
321 * the same way the copy tool acknowledge a cancel request */
322 if (now <= last + cdt->cdt_active_req_timeout)
325 dump_llog_agent_req_rec("request timed out, start cleaning",
327 /* a too old cancel request just needs to be removed
328 * this can happen, if copy tool does not support
329 * cancel for other requests, we have to remove the
330 * running request and notify the copytool */
331 pgs.hpk_fid = larr->arr_hai.hai_fid;
332 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
333 pgs.hpk_extent = larr->arr_hai.hai_extent;
334 pgs.hpk_flags = HP_FLAG_COMPLETED;
335 pgs.hpk_errval = ENOSYS;
336 pgs.hpk_data_version = 0;
338 /* update request state, but do not record in llog, to
339 * avoid deadlock on cdt_llog_lock */
340 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
342 CERROR("%s: cannot cleanup timed out request: "
343 DFID" for cookie %#llx action=%s\n",
345 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
346 hsm_copytool_action2name(
347 larr->arr_hai.hai_action));
350 /* The request no longer exists, forget
351 * about it, and do not send a cancel request
352 * to the client, for which an error will be
353 * sent back, leading to an endless cycle of
355 cdt_agent_record_hash_del(cdt,
356 larr->arr_hai.hai_cookie);
357 RETURN(LLOG_DEL_RECORD);
360 /* XXX A cancel request cannot be cancelled. */
361 if (larr->arr_hai.hai_action == HSMA_CANCEL)
364 larr->arr_status = ARS_CANCELED;
365 larr->arr_req_change = now;
366 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
368 CERROR("%s: cannot update agent log: rc = %d\n",
369 mdt_obd_name(mdt), rc);
375 if (!hsd->housekeeping)
378 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
379 cfs_time_current_sec()) {
380 cdt_agent_record_hash_del(cdt,
381 larr->arr_hai.hai_cookie);
382 RETURN(LLOG_DEL_RECORD);
390 * create /proc entries for coordinator
393 * \retval -ve failure
395 int hsm_cdt_procfs_init(struct mdt_device *mdt)
397 struct coordinator *cdt = &mdt->mdt_coordinator;
401 /* init /proc entries, failure is not critical */
402 cdt->cdt_proc_dir = lprocfs_register("hsm",
403 mdt2obd_dev(mdt)->obd_proc_entry,
404 lprocfs_mdt_hsm_vars, mdt);
405 if (IS_ERR(cdt->cdt_proc_dir)) {
406 rc = PTR_ERR(cdt->cdt_proc_dir);
407 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
408 " rc=%d\n", mdt_obd_name(mdt), rc);
409 cdt->cdt_proc_dir = NULL;
417 * remove /proc entries for coordinator
420 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
422 struct coordinator *cdt = &mdt->mdt_coordinator;
424 if (cdt->cdt_proc_dir != NULL)
425 lprocfs_remove(&cdt->cdt_proc_dir);
429 * get vector of hsm cdt /proc vars
433 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
435 return lprocfs_mdt_hsm_vars;
438 /* Release the ressource used by the coordinator. Called when the
439 * coordinator is stopping. */
440 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
442 struct coordinator *cdt = &mdt->mdt_coordinator;
443 struct cdt_agent_req *car, *tmp1;
444 struct hsm_agent *ha, *tmp2;
445 struct cdt_restore_handle *crh, *tmp3;
446 struct mdt_thread_info *cdt_mti;
449 down_write(&cdt->cdt_request_lock);
450 list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
452 cfs_hash_del(cdt->cdt_request_cookie_hash,
453 &car->car_hai->hai_cookie,
454 &car->car_cookie_hash);
455 list_del(&car->car_request_list);
456 mdt_cdt_put_request(car);
458 up_write(&cdt->cdt_request_lock);
460 down_write(&cdt->cdt_agent_lock);
461 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
462 list_del(&ha->ha_list);
465 up_write(&cdt->cdt_agent_lock);
467 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
468 mutex_lock(&cdt->cdt_restore_lock);
469 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
470 list_del(&crh->crh_list);
471 /* give back layout lock */
472 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
473 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
475 mutex_unlock(&cdt->cdt_restore_lock);
479 * Coordinator state transition table, indexed on enum cdt_states, taking
480 * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
481 * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
483 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
484 /* from -> to: stopped init running disable stopping */
485 /* stopped */ { true, true, false, false, false },
486 /* init */ { true, false, true, false, false },
487 /* running */ { false, false, true, true, true },
488 /* disable */ { false, false, true, true, true },
489 /* stopping */ { true, false, false, false, false }
493 * Change coordinator thread state
494 * Some combinations are not valid, so catch them here.
496 * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
497 * the transition was not possible.
499 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state,
500 enum cdt_states *old_state)
503 enum cdt_states state;
505 spin_lock(&cdt->cdt_state_lock);
507 state = cdt->cdt_state;
509 if (cdt_transition[state][new_state]) {
510 cdt->cdt_state = new_state;
511 spin_unlock(&cdt->cdt_state_lock);
516 spin_unlock(&cdt->cdt_state_lock);
518 "unexpected coordinator transition, from=%s, to=%s\n",
519 cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
528 * \param data [IN] obd device
530 * \retval -ve failure
532 static int mdt_coordinator(void *data)
534 struct hsm_thread_data *thread_data = data;
535 struct mdt_thread_info *mti = thread_data->cdt_mti;
536 struct mdt_device *mdt = mti->mti_mdt;
537 struct coordinator *cdt = &mdt->mdt_coordinator;
538 struct hsm_scan_data hsd = { NULL };
539 time64_t wait_event_time = 1 * HZ;
540 time64_t last_housekeeping = 0;
545 /* set up hsd->request and max_requests */
546 hsd.max_requests = cdt->cdt_max_requests;
547 request_sz = hsd.max_requests * sizeof(*hsd.request);
548 hsd.request = thread_data->request;
550 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
551 mdt_obd_name(mdt), current_pid());
554 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
556 set_cdt_state(cdt, CDT_RUNNING, NULL);
558 /* Inform mdt_hsm_cdt_start(). */
559 wake_up_all(&cdt->cdt_waitq);
566 struct hsm_record_update *updates;
568 /* Limit execution of the expensive requests traversal
569 * to at most every "wait_event_time" jiffies. This prevents
570 * repeatedly locking/unlocking the catalog for each request
571 * and preventing other HSM operations from happening */
572 wait_event_interruptible_timeout(cdt->cdt_waitq,
573 kthread_should_stop() ||
574 cdt->cdt_wakeup_coordinator,
577 cdt->cdt_wakeup_coordinator = false;
578 CDEBUG(D_HSM, "coordinator resumes\n");
580 if (kthread_should_stop()) {
581 CDEBUG(D_HSM, "Coordinator stops\n");
586 /* if coordinator is suspended continue to wait */
587 if (cdt->cdt_state == CDT_DISABLE) {
588 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
592 /* If no event, and no housekeeping to do, continue to
594 if (last_housekeeping + cdt->cdt_loop_period <=
596 last_housekeeping = get_seconds();
597 hsd.housekeeping = true;
598 } else if (cdt->cdt_event) {
599 hsd.housekeeping = false;
604 cdt->cdt_event = false;
606 CDEBUG(D_HSM, "coordinator starts reading llog\n");
608 if (hsd.max_requests != cdt->cdt_max_requests) {
609 /* cdt_max_requests has changed,
610 * we need to allocate a new buffer
612 struct hsm_scan_request *tmp = NULL;
613 int max_requests = cdt->cdt_max_requests;
614 OBD_ALLOC_LARGE(tmp, max_requests *
615 sizeof(struct hsm_scan_request));
617 CERROR("Failed to resize request buffer, "
618 "keeping it at %d\n",
620 cdt->cdt_max_requests = hsd.max_requests;
622 OBD_FREE_LARGE(hsd.request, request_sz);
623 hsd.max_requests = max_requests;
624 request_sz = hsd.max_requests *
625 sizeof(struct hsm_scan_request);
632 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
637 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
639 if (list_empty(&cdt->cdt_agents)) {
640 CDEBUG(D_HSM, "no agent available, "
641 "coordinator sleeps\n");
645 /* Compute how many HAI we have in all the requests */
647 for (i = 0; i < hsd.request_cnt; i++) {
648 const struct hsm_scan_request *request =
651 updates_cnt += request->hal->hal_count;
654 /* Allocate a temporary array to store the cookies to
655 * update, and their status. */
656 updates_sz = updates_cnt * sizeof(*updates);
657 OBD_ALLOC(updates, updates_sz);
658 if (updates == NULL) {
659 CERROR("%s: Cannot allocate memory (%d o) "
661 mdt_obd_name(mdt), updates_sz, updates_cnt);
665 /* here hsd contains a list of requests to be started */
666 for (i = 0; i < hsd.request_cnt; i++) {
667 struct hsm_scan_request *request = &hsd.request[i];
668 struct hsm_action_list *hal = request->hal;
669 struct hsm_action_item *hai;
672 /* still room for work ? */
673 if (atomic_read(&cdt->cdt_request_count) >=
674 cdt->cdt_max_requests)
677 rc = mdt_hsm_agent_send(mti, hal, 0);
678 /* if failure, we suppose it is temporary
679 * if the copy tool failed to do the request
680 * it has to use hsm_progress
683 /* set up cookie vector to set records status
684 * after copy tools start or failed
686 hai = hai_first(hal);
687 for (j = 0; j < hal->hal_count; j++) {
688 updates[update_idx].cookie = hai->hai_cookie;
689 updates[update_idx].status =
690 (rc ? ARS_WAITING : ARS_STARTED);
697 rc = mdt_agent_record_update(mti->mti_env, mdt,
698 updates, update_idx);
700 CERROR("%s: mdt_agent_record_update() failed, "
701 "rc=%d, cannot update records "
703 mdt_obd_name(mdt), rc, update_idx);
706 OBD_FREE(updates, updates_sz);
709 /* free hal allocated by callback */
710 for (i = 0; i < hsd.request_cnt; i++) {
711 struct hsm_scan_request *request = &hsd.request[i];
713 OBD_FREE(request->hal, request->hal_sz);
718 OBD_FREE_LARGE(hsd.request, request_sz);
720 mdt_hsm_cdt_cleanup(mdt);
723 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
724 mdt_obd_name(mdt), current_pid(), rc);
726 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
728 mdt_obd_name(mdt), current_pid());
734 * lookup a restore handle by FID
735 * caller needs to hold cdt_restore_lock
736 * \param cdt [IN] coordinator
737 * \param fid [IN] FID
738 * \retval cdt_restore_handle found
739 * \retval NULL not found
741 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
742 const struct lu_fid *fid)
744 struct cdt_restore_handle *crh;
747 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
748 if (lu_fid_eq(&crh->crh_fid, fid))
755 * data passed to llog_cat_process() callback
756 * to scan requests and take actions
758 struct hsm_restore_data {
759 struct mdt_thread_info *hrd_mti;
763 * llog_cat_process() callback, used to:
764 * - find restore request and allocate the restore handle
765 * \param env [IN] environment
766 * \param llh [IN] llog handle
767 * \param hdr [IN] llog record
768 * \param data [IN/OUT] cb data = struct hsm_restore_data
770 * \retval -ve failure
772 static int hsm_restore_cb(const struct lu_env *env,
773 struct llog_handle *llh,
774 struct llog_rec_hdr *hdr, void *data)
776 struct llog_agent_req_rec *larr;
777 struct hsm_restore_data *hrd;
778 struct cdt_restore_handle *crh;
779 struct hsm_action_item *hai;
780 struct mdt_thread_info *mti;
781 struct coordinator *cdt;
782 struct mdt_object *child;
788 cdt = &mti->mti_mdt->mdt_coordinator;
790 larr = (struct llog_agent_req_rec *)hdr;
791 hai = &larr->arr_hai;
792 if (hai->hai_cookie > cdt->cdt_last_cookie)
793 /* update the cookie to avoid collision */
794 cdt->cdt_last_cookie = hai->hai_cookie + 1;
796 if (hai->hai_action != HSMA_RESTORE ||
797 agent_req_in_final_state(larr->arr_status))
800 /* restore request not in a final state */
802 /* force replay of restore requests left in started state from previous
803 * CDT context, to be canceled later if finally found to be incompatible
804 * when being re-started */
805 if (larr->arr_status == ARS_STARTED) {
806 larr->arr_status = ARS_WAITING;
807 larr->arr_req_change = cfs_time_current_sec();
808 rc = llog_write(env, llh, hdr, hdr->lrh_index);
813 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
817 crh->crh_fid = hai->hai_fid;
818 /* in V1 all file is restored
819 crh->extent.start = hai->hai_extent.offset;
820 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
822 crh->crh_extent.start = 0;
823 crh->crh_extent.end = hai->hai_extent.length;
824 /* get the layout lock */
825 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
826 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
827 MDS_INODELOCK_LAYOUT);
829 GOTO(out, rc = PTR_ERR(child));
832 /* we choose to not keep a reference
833 * on the object during the restore time which can be very long */
834 mdt_object_put(mti->mti_env, child);
836 mutex_lock(&cdt->cdt_restore_lock);
837 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
838 mutex_unlock(&cdt->cdt_restore_lock);
845 * restore coordinator state at startup
846 * the goal is to take a layout lock for each registered restore request
847 * \param mti [IN] context
849 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
851 struct hsm_restore_data hrd;
857 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
863 static int hsm_init_ucred(struct lu_ucred *uc)
867 uc->uc_valid = UCRED_OLD;
876 uc->uc_suppgids[0] = -1;
877 uc->uc_suppgids[1] = -1;
878 uc->uc_cap = CFS_CAP_FS_MASK;
881 uc->uc_identity = NULL;
887 * initialize coordinator struct
888 * \param mdt [IN] device
890 * \retval -ve failure
892 int mdt_hsm_cdt_init(struct mdt_device *mdt)
894 struct coordinator *cdt = &mdt->mdt_coordinator;
895 struct mdt_thread_info *cdt_mti = NULL;
899 init_waitqueue_head(&cdt->cdt_waitq);
900 init_rwsem(&cdt->cdt_llog_lock);
901 init_rwsem(&cdt->cdt_agent_lock);
902 init_rwsem(&cdt->cdt_request_lock);
903 mutex_init(&cdt->cdt_restore_lock);
904 spin_lock_init(&cdt->cdt_state_lock);
905 set_cdt_state(cdt, CDT_STOPPED, NULL);
907 INIT_LIST_HEAD(&cdt->cdt_request_list);
908 INIT_LIST_HEAD(&cdt->cdt_agents);
909 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
911 cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
918 &cdt_request_cookie_hash_ops,
920 if (cdt->cdt_request_cookie_hash == NULL)
923 cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
930 &cdt_agent_record_hash_ops,
932 if (cdt->cdt_agent_record_hash == NULL)
933 GOTO(out_request_cookie_hash, rc = -ENOMEM);
935 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
937 GOTO(out_agent_record_hash, rc);
939 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
940 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
944 lu_context_enter(&cdt->cdt_session);
945 cdt->cdt_env.le_ses = &cdt->cdt_session;
947 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
948 LASSERT(cdt_mti != NULL);
950 cdt_mti->mti_env = &cdt->cdt_env;
951 cdt_mti->mti_mdt = mdt;
953 hsm_init_ucred(mdt_ucred(cdt_mti));
955 /* default values for /proc tunnables
956 * can be override by MGS conf */
957 cdt->cdt_default_archive_id = 1;
958 cdt->cdt_grace_delay = 60;
959 cdt->cdt_loop_period = 10;
960 cdt->cdt_max_requests = 3;
961 cdt->cdt_policy = CDT_DEFAULT_POLICY;
962 cdt->cdt_active_req_timeout = 3600;
964 /* Initialize cdt_compound_id here to allow its usage for
965 * delayed requests from RAoLU policy */
966 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
968 /* by default do not remove archives on last unlink */
969 cdt->cdt_remove_archive_on_last_unlink = false;
974 lu_env_fini(&cdt->cdt_env);
975 out_agent_record_hash:
976 cfs_hash_putref(cdt->cdt_agent_record_hash);
977 cdt->cdt_agent_record_hash = NULL;
978 out_request_cookie_hash:
979 cfs_hash_putref(cdt->cdt_request_cookie_hash);
980 cdt->cdt_request_cookie_hash = NULL;
986 * free a coordinator thread
987 * \param mdt [IN] device
989 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
991 struct coordinator *cdt = &mdt->mdt_coordinator;
994 lu_context_exit(cdt->cdt_env.le_ses);
995 lu_context_fini(cdt->cdt_env.le_ses);
997 lu_env_fini(&cdt->cdt_env);
999 cfs_hash_putref(cdt->cdt_agent_record_hash);
1000 cdt->cdt_agent_record_hash = NULL;
1002 cfs_hash_putref(cdt->cdt_request_cookie_hash);
1003 cdt->cdt_request_cookie_hash = NULL;
1009 * start a coordinator thread
1010 * \param mdt [IN] device
1012 * \retval -ve failure
1014 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1016 struct coordinator *cdt = &mdt->mdt_coordinator;
1019 struct task_struct *task;
1021 struct hsm_thread_data thread_data;
1024 /* functions defined but not yet used
1025 * this avoid compilation warning
1027 ptr = dump_requests;
1029 rc = set_cdt_state(cdt, CDT_INIT, NULL);
1031 CERROR("%s: Coordinator already started or stopping\n",
1036 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
1037 cdt->cdt_policy = CDT_DEFAULT_POLICY;
1039 /* just need to be larger than previous one */
1040 /* cdt_last_cookie is protected by cdt_llog_lock */
1041 cdt->cdt_last_cookie = cfs_time_current_sec();
1042 atomic_set(&cdt->cdt_request_count, 0);
1043 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1044 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1045 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1047 /* to avoid deadlock when start is made through /proc
1048 * /proc entries are created by the coordinator thread */
1050 /* set up list of started restore requests */
1051 thread_data.cdt_mti =
1052 lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1053 rc = mdt_hsm_pending_restore(thread_data.cdt_mti);
1055 CERROR("%s: cannot take the layout locks needed"
1056 " for registered restore: %d\n",
1057 mdt_obd_name(mdt), rc);
1059 if (mdt->mdt_bottom->dd_rdonly)
1062 /* Allocate the initial hsd.request[] vector*/
1063 request_sz = cdt->cdt_max_requests * sizeof(struct hsm_scan_request);
1064 OBD_ALLOC_LARGE(thread_data.request, request_sz);
1065 if (!thread_data.request) {
1066 set_cdt_state(cdt, CDT_STOPPED, NULL);
1070 task = kthread_run(mdt_coordinator, &thread_data, "hsm_cdtr");
1073 set_cdt_state(cdt, CDT_STOPPED, NULL);
1074 OBD_FREE(thread_data.request, request_sz);
1075 CERROR("%s: error starting coordinator thread: %d\n",
1076 mdt_obd_name(mdt), rc);
1078 cdt->cdt_task = task;
1079 wait_event(cdt->cdt_waitq,
1080 cdt->cdt_state != CDT_INIT);
1081 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1090 * stop a coordinator thread
1091 * \param mdt [IN] device
1093 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1095 struct coordinator *cdt = &mdt->mdt_coordinator;
1099 /* stop coordinator thread */
1100 rc = set_cdt_state(cdt, CDT_STOPPING, NULL);
1102 kthread_stop(cdt->cdt_task);
1103 cdt->cdt_task = NULL;
1104 set_cdt_state(cdt, CDT_STOPPED, NULL);
1110 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1111 const struct lu_fid *fid,
1114 struct mdt_object *obj;
1118 obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1120 GOTO(out, rc = PTR_ERR(obj));
1122 if (mh.mh_flags & HS_EXISTS &&
1123 mh.mh_arch_id == archive_id)
1124 GOTO(out_obj, rc = 0);
1126 mh.mh_flags |= HS_EXISTS;
1127 mh.mh_arch_id = archive_id;
1128 rc = mdt_hsm_attr_set(mti, obj, &mh);
1131 mdt_object_put(mti->mti_env, obj);
1137 * register all requests from an hal in the memory list
1138 * \param mti [IN] context
1139 * \param hal [IN] request
1140 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1141 * which is running the CT
1143 * \retval -ve failure
1145 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1146 struct hsm_action_list *hal, struct obd_uuid *uuid)
1148 struct mdt_device *mdt = mti->mti_mdt;
1149 struct coordinator *cdt = &mdt->mdt_coordinator;
1150 struct hsm_action_item *hai;
1154 /* register request in memory list */
1155 hai = hai_first(hal);
1156 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1157 struct cdt_agent_req *car;
1159 /* in case of a cancel request, we first mark the ondisk
1160 * record of the request we want to stop as canceled
1161 * this does not change the cancel record
1162 * it will be done when updating the request status
1164 if (hai->hai_action == HSMA_CANCEL) {
1165 struct hsm_record_update update = {
1166 .cookie = hai->hai_cookie,
1167 .status = ARS_CANCELED,
1170 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1173 CERROR("%s: mdt_agent_record_update() failed, "
1174 "rc=%d, cannot update status to %s "
1175 "for cookie %#llx\n",
1176 mdt_obd_name(mdt), rc,
1177 agent_req_status2name(ARS_CANCELED),
1182 /* find the running request to set it canceled */
1183 car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1185 car->car_canceled = 1;
1186 /* uuid has to be changed to the one running the
1187 * request to cancel */
1188 *uuid = car->car_uuid;
1189 mdt_cdt_put_request(car);
1191 /* no need to memorize cancel request
1192 * this also avoid a deadlock when we receive
1193 * a purge all requests command
1198 if (hai->hai_action == HSMA_ARCHIVE) {
1199 rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1200 hal->hal_archive_id);
1207 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1208 hal->hal_archive_id, hal->hal_flags,
1211 GOTO(out, rc = PTR_ERR(car));
1213 rc = mdt_cdt_add_request(cdt, car);
1215 mdt_cdt_free_request(car);
1222 * swap layouts between 2 fids
1223 * \param mti [IN] context
1226 * \param mh_common [IN] MD HSM
1228 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1229 struct mdt_object *obj, const struct lu_fid *dfid,
1230 struct md_hsm *mh_common)
1232 struct mdt_object *dobj;
1233 struct mdt_lock_handle *dlh;
1237 if (!mdt_object_exists(obj))
1238 GOTO(out, rc = -ENOENT);
1240 /* we already have layout lock on obj so take only
1242 dlh = &mti->mti_lh[MDT_LH_OLD];
1243 mdt_lock_reg_init(dlh, LCK_EX);
1244 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1246 GOTO(out, rc = PTR_ERR(dobj));
1248 /* if copy tool closes the volatile before sending the final
1249 * progress through llapi_hsm_copy_end(), all the objects
1250 * are removed and mdd_swap_layout LBUG */
1251 if (!mdt_object_exists(dobj)) {
1252 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1253 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1254 GOTO(out_dobj, rc = -ENOENT);
1256 /* Since we only handle restores here, unconditionally use
1257 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1258 * be preserved in case of failure during swap_layout and not
1259 * leave a file in an intermediate but incoherent state.
1260 * But need to setup HSM xattr of data FID before, reuse
1261 * mti and mh presets for FID in hsm_cdt_request_completed(),
1262 * only need to clear RELEASED and DIRTY.
1264 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1265 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1267 rc = mo_swap_layouts(mti->mti_env,
1268 mdt_object_child(obj),
1269 mdt_object_child(dobj),
1270 SWAP_LAYOUTS_MDS_HSM);
1273 mdt_object_unlock_put(mti, dobj, dlh, 1);
1279 * update status of a completed request
1280 * \param mti [IN] context
1281 * \param pgs [IN] progress of the copy tool
1282 * \param update_record [IN] update llog record
1284 * \retval -ve failure
1286 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1287 struct hsm_progress_kernel *pgs,
1288 const struct cdt_agent_req *car,
1289 enum agent_req_status *status)
1291 const struct lu_env *env = mti->mti_env;
1292 struct mdt_device *mdt = mti->mti_mdt;
1293 struct coordinator *cdt = &mdt->mdt_coordinator;
1294 struct mdt_object *obj = NULL;
1295 int cl_flags = 0, rc = 0;
1298 bool need_changelog = true;
1301 /* default is to retry */
1302 *status = ARS_WAITING;
1304 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1305 * if error/removed continue anyway to get correct reporting done */
1306 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1307 /* we will update MD HSM only if needed */
1308 is_mh_changed = false;
1310 /* no need to change mh->mh_arch_id
1311 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1313 if (pgs->hpk_errval != 0) {
1314 switch (pgs->hpk_errval) {
1316 /* the copy tool does not support cancel
1317 * so the cancel request is failed
1318 * As we cannot distinguish a cancel progress
1319 * from another action progress (they have the
1320 * same cookie), we suppose here the CT returns
1321 * ENOSYS only if does not support cancel
1323 /* this can also happen when cdt calls it to
1324 * for a timed out request */
1325 *status = ARS_FAILED;
1326 /* to have a cancel event in changelog */
1327 pgs->hpk_errval = ECANCELED;
1330 /* the request record has already been set to
1331 * ARS_CANCELED, this set the cancel request
1333 *status = ARS_SUCCEED;
1336 /* retry only if current policy or requested, and
1337 * object is not on error/removed */
1338 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1339 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1340 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1344 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1345 CERROR("%s: Request %#llx on "DFID
1346 " failed, error code %d too large\n",
1348 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1350 hsm_set_cl_error(&cl_flags,
1351 CLF_HSM_ERROVERFLOW);
1354 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1357 switch (car->car_hai->hai_action) {
1359 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1362 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1365 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1368 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1369 CERROR("%s: Failed request %#llx on "DFID
1370 " cannot be a CANCEL\n",
1373 PFID(&pgs->hpk_fid));
1376 CERROR("%s: Failed request %#llx on "DFID
1377 " %d is an unknown action\n",
1379 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1380 car->car_hai->hai_action);
1385 *status = ARS_SUCCEED;
1386 switch (car->car_hai->hai_action) {
1388 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1389 /* set ARCHIVE keep EXIST and clear LOST and
1391 mh.mh_arch_ver = pgs->hpk_data_version;
1392 mh.mh_flags |= HS_ARCHIVED;
1393 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1394 is_mh_changed = true;
1397 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1399 /* do not clear RELEASED and DIRTY here
1400 * this will occur in hsm_swap_layouts()
1403 /* Restoring has changed the file version on
1405 mh.mh_arch_ver = pgs->hpk_data_version;
1406 is_mh_changed = true;
1409 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1410 /* clear ARCHIVED EXISTS and LOST */
1411 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1412 is_mh_changed = true;
1415 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1416 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1419 PFID(&pgs->hpk_fid));
1422 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1424 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1425 car->car_hai->hai_action);
1431 /* rc != 0 means error when analysing action, it may come from
1432 * a crasy CT no need to manage DIRTY
1433 * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1436 if (rc == 0 && !IS_ERR(obj))
1437 hsm_set_cl_flags(&cl_flags,
1438 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1440 /* unlock is done later, after layout lock management */
1441 if (is_mh_changed && !IS_ERR(obj))
1442 rc = mdt_hsm_attr_set(mti, obj, &mh);
1444 /* we give back layout lock only if restore was successful or
1445 * if no retry will be attempted and if object is still alive,
1446 * in other cases we just unlock the object */
1447 if (car->car_hai->hai_action == HSMA_RESTORE) {
1448 struct cdt_restore_handle *crh;
1450 /* restore in data FID done, we swap the layouts
1451 * only if restore is successful */
1452 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1453 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1456 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1457 *status = ARS_FAILED;
1458 pgs->hpk_errval = -rc;
1461 /* we have to retry, so keep layout lock */
1462 if (*status == ARS_WAITING)
1465 /* restore special case, need to create ChangeLog record
1466 * before to give back layout lock to avoid concurrent
1467 * file updater to post out of order ChangeLog */
1468 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1469 &car->car_hai->hai_fid);
1470 need_changelog = false;
1472 /* give back layout lock */
1473 mutex_lock(&cdt->cdt_restore_lock);
1474 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1476 list_del(&crh->crh_list);
1477 mutex_unlock(&cdt->cdt_restore_lock);
1478 /* Just give back layout lock, we keep the reference
1479 * which is given back later with the lock for HSM
1481 * XXX obj may be invalid so we do not pass it. */
1483 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1486 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1492 /* always add a ChangeLog record */
1494 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1495 &car->car_hai->hai_fid);
1498 mdt_object_put(mti->mti_env, obj);
1504 * update status of a request
1505 * \param mti [IN] context
1506 * \param pgs [IN] progress of the copy tool
1507 * \param update_record [IN] update llog record
1509 * \retval -ve failure
1511 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1512 struct hsm_progress_kernel *pgs,
1513 const int update_record)
1515 struct mdt_device *mdt = mti->mti_mdt;
1516 struct coordinator *cdt = &mdt->mdt_coordinator;
1517 struct cdt_agent_req *car;
1521 /* no coordinator started, so we cannot serve requests */
1522 if (cdt->cdt_state == CDT_STOPPED)
1525 /* first do sanity checks */
1526 car = mdt_cdt_update_request(cdt, pgs);
1528 CERROR("%s: Cannot find running request for cookie %#llx"
1531 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1533 RETURN(PTR_ERR(car));
1536 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1537 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1538 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1539 hsm_copytool_action2name(car->car_hai->hai_action),
1540 pgs->hpk_flags, pgs->hpk_errval,
1541 PFID(&car->car_hai->hai_fid),
1542 PFID(&car->car_hai->hai_dfid));
1544 /* progress is done on FID or data FID depending of the action and
1545 * of the copy progress */
1546 /* for restore progress is used to send back the data FID to cdt */
1547 if (car->car_hai->hai_action == HSMA_RESTORE &&
1548 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1549 car->car_hai->hai_dfid = pgs->hpk_fid;
1551 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1552 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1553 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1554 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1555 CERROR("%s: Progress on "DFID" for cookie %#llx"
1556 " does not match request FID "DFID" nor data FID "
1559 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1560 PFID(&car->car_hai->hai_fid),
1561 PFID(&car->car_hai->hai_dfid));
1562 GOTO(out, rc = -EINVAL);
1565 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1566 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1567 " is not coherent (err=%d and not completed"
1570 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1571 hsm_copytool_action2name(car->car_hai->hai_action),
1572 pgs->hpk_errval, pgs->hpk_flags);
1573 GOTO(out, rc = -EINVAL);
1576 /* now progress is valid */
1578 /* we use a root like ucred */
1579 hsm_init_ucred(mdt_ucred(mti));
1581 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1582 enum agent_req_status status;
1584 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1586 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1588 update_record ? "Updating" : "Not updating",
1589 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1590 hsm_copytool_action2name(car->car_hai->hai_action),
1591 agent_req_status2name(status));
1593 /* update record first (LU-9075) */
1594 if (update_record) {
1596 struct hsm_record_update update = {
1597 .cookie = pgs->hpk_cookie,
1601 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1604 CERROR("%s: mdt_agent_record_update() failed,"
1605 " rc=%d, cannot update status to %s"
1606 " for cookie %#llx\n",
1607 mdt_obd_name(mdt), rc1,
1608 agent_req_status2name(status),
1610 rc = (rc != 0 ? rc : rc1);
1613 /* then remove request from memory list (LU-9075) */
1614 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1616 /* ct has completed a request, so a slot is available,
1617 * signal the coordinator to find new work */
1618 mdt_hsm_cdt_event(cdt);
1620 /* if copytool send a progress on a canceled request
1621 * we inform copytool it should stop
1623 if (car->car_canceled == 1)
1629 /* remove ref got from mdt_cdt_update_request() */
1630 mdt_cdt_put_request(car);
1637 * data passed to llog_cat_process() callback
1638 * to cancel requests
1640 struct hsm_cancel_all_data {
1641 struct mdt_device *mdt;
1645 * llog_cat_process() callback, used to:
1646 * - purge all requests
1647 * \param env [IN] environment
1648 * \param llh [IN] llog handle
1649 * \param hdr [IN] llog record
1650 * \param data [IN] cb data = struct hsm_cancel_all_data
1652 * \retval -ve failure
1654 static int mdt_cancel_all_cb(const struct lu_env *env,
1655 struct llog_handle *llh,
1656 struct llog_rec_hdr *hdr, void *data)
1658 struct llog_agent_req_rec *larr;
1659 struct hsm_cancel_all_data *hcad;
1663 larr = (struct llog_agent_req_rec *)hdr;
1665 if (larr->arr_status == ARS_WAITING ||
1666 larr->arr_status == ARS_STARTED) {
1667 larr->arr_status = ARS_CANCELED;
1668 larr->arr_req_change = cfs_time_current_sec();
1669 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1676 * cancel all actions
1677 * \param obd [IN] MDT device
1679 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1682 struct lu_context session;
1683 struct mdt_thread_info *mti;
1684 struct coordinator *cdt = &mdt->mdt_coordinator;
1685 struct cdt_agent_req *car;
1686 struct hsm_action_list *hal = NULL;
1687 struct hsm_action_item *hai;
1688 struct hsm_cancel_all_data hcad;
1689 int hal_sz = 0, hal_len, rc;
1690 enum cdt_states old_state;
1693 rc = lu_env_init(&env, LCT_MD_THREAD);
1697 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1698 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1702 lu_context_enter(&session);
1703 env.le_ses = &session;
1705 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1706 LASSERT(mti != NULL);
1708 mti->mti_env = &env;
1711 hsm_init_ucred(mdt_ucred(mti));
1713 /* disable coordinator */
1714 rc = set_cdt_state(cdt, CDT_DISABLE, &old_state);
1718 /* send cancel to all running requests */
1719 down_read(&cdt->cdt_request_lock);
1720 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1721 mdt_cdt_get_request(car);
1722 /* request is not yet removed from list, it will be done
1723 * when copytool will return progress
1726 if (car->car_hai->hai_action == HSMA_CANCEL) {
1727 mdt_cdt_put_request(car);
1732 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1733 cfs_size_round(car->car_hai->hai_len);
1735 if (hal_len > hal_sz && hal_sz > 0) {
1736 /* not enough room, free old buffer */
1737 OBD_FREE(hal, hal_sz);
1741 /* empty buffer, allocate one */
1744 OBD_ALLOC(hal, hal_sz);
1746 mdt_cdt_put_request(car);
1747 up_read(&cdt->cdt_request_lock);
1748 GOTO(out_cdt_state, rc = -ENOMEM);
1752 hal->hal_version = HAL_VERSION;
1753 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1755 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1756 hal->hal_compound_id = car->car_compound_id;
1757 hal->hal_archive_id = car->car_archive_id;
1758 hal->hal_flags = car->car_flags;
1761 hai = hai_first(hal);
1762 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1763 hai->hai_action = HSMA_CANCEL;
1766 /* it is possible to safely call mdt_hsm_agent_send()
1767 * (ie without a deadlock on cdt_request_lock), because the
1768 * write lock is taken only if we are not in purge mode
1769 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1770 * nor mdt_cdt_remove_request())
1772 /* no conflict with cdt thread because cdt is disable and we
1773 * have the request lock */
1774 mdt_hsm_agent_send(mti, hal, 1);
1776 mdt_cdt_put_request(car);
1778 up_read(&cdt->cdt_request_lock);
1781 OBD_FREE(hal, hal_sz);
1783 /* cancel all on-disk records */
1786 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1787 &hcad, 0, 0, WRITE);
1789 /* Enable coordinator, unless the coordinator was stopping. */
1790 set_cdt_state(cdt, old_state, NULL);
1791 lu_context_exit(&session);
1792 lu_context_fini(&session);
1800 * check if a request is compatible with file status
1801 * \param hai [IN] request description
1802 * \param archive_id [IN] request archive id
1803 * \param rq_flags [IN] request flags
1804 * \param hsm [IN] file HSM metadata
1807 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1808 u32 archive_id, u64 rq_flags,
1809 const struct md_hsm *hsm)
1811 int is_compat = false;
1815 hsm_flags = hsm->mh_flags;
1816 switch (hai->hai_action) {
1818 if (!(hsm_flags & HS_NOARCHIVE) &&
1819 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1822 if (hsm_flags & HS_EXISTS &&
1824 archive_id != hsm->mh_arch_id)
1829 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1830 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1834 if (!(hsm_flags & HS_RELEASED) &&
1835 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1842 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1843 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1844 PFID(&hai->hai_fid),
1845 hsm_copytool_action2name(hai->hai_action), rq_flags,
1846 hai->hai_extent.offset, hai->hai_extent.length,
1848 (is_compat ? "compatible" : "uncompatible"));
1854 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1856 static const struct {
1860 } hsm_policy_names[] = {
1861 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1862 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1867 * convert a policy name to a bit
1868 * \param name [IN] policy name
1870 * \retval policy bit
1872 static __u64 hsm_policy_str2bit(const char *name)
1876 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1877 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1878 strcmp(hsm_policy_names[i].name, name) == 0)
1879 return hsm_policy_names[i].bit;
1884 * convert a policy bit field to a string
1885 * \param mask [IN] policy bit field
1886 * \param hexa [IN] print mask before bit names
1887 * \param buffer [OUT] string
1888 * \param count [IN] size of buffer
1890 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1898 seq_printf(m, "(%#llx) ", mask);
1900 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1903 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1904 if (hsm_policy_names[j].bit == bit)
1908 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1910 seq_printf(m, "%s ", hsm_policy_names[j].name);
1912 /* remove last ' ' */
1917 /* methods to read/write HSM policy flags */
1918 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1920 struct mdt_device *mdt = m->private;
1921 struct coordinator *cdt = &mdt->mdt_coordinator;
1924 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1929 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1930 size_t count, loff_t *off)
1932 struct seq_file *m = file->private_data;
1933 struct mdt_device *mdt = m->private;
1934 struct coordinator *cdt = &mdt->mdt_coordinator;
1935 char *start, *token, sign;
1938 __u64 add_mask, remove_mask, set_mask;
1942 if (count + 1 > PAGE_SIZE)
1945 OBD_ALLOC(buf, count + 1);
1949 if (copy_from_user(buf, buffer, count))
1950 GOTO(out, rc = -EFAULT);
1955 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1958 add_mask = remove_mask = set_mask = 0;
1960 token = strsep(&start, "\n ");
1966 if (sign == '-' || sign == '+')
1969 policy = hsm_policy_str2bit(token);
1971 CWARN("%s: '%s' is unknown, "
1972 "supported policies are:\n", mdt_obd_name(mdt),
1974 hsm_policy_bit2str(m, 0, false);
1975 GOTO(out, rc = -EINVAL);
1979 remove_mask |= policy;
1989 } while (start != NULL);
1991 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1992 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1994 /* if no sign in all string, it is a clear and set
1995 * if some sign found, all unsigned are converted
1997 * P1 P2 = set to P1 and P2
1998 * P1 -P2 = add P1 clear P2 same as +P1 -P2
2000 if (remove_mask == 0 && add_mask == 0) {
2001 cdt->cdt_policy = set_mask;
2003 cdt->cdt_policy |= set_mask | add_mask;
2004 cdt->cdt_policy &= ~remove_mask;
2007 GOTO(out, rc = count);
2010 OBD_FREE(buf, count + 1);
2013 LPROC_SEQ_FOPS(mdt_hsm_policy);
2015 #define GENERATE_PROC_METHOD(VAR) \
2016 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
2018 struct mdt_device *mdt = m->private; \
2019 struct coordinator *cdt = &mdt->mdt_coordinator; \
2022 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
2026 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
2027 size_t count, loff_t *off) \
2030 struct seq_file *m = file->private_data; \
2031 struct mdt_device *mdt = m->private; \
2032 struct coordinator *cdt = &mdt->mdt_coordinator; \
2037 rc = lprocfs_str_to_s64(buffer, count, &val); \
2040 if (val > 0 && val < INT_MAX) { \
2047 GENERATE_PROC_METHOD(cdt_loop_period)
2048 GENERATE_PROC_METHOD(cdt_grace_delay)
2049 GENERATE_PROC_METHOD(cdt_active_req_timeout)
2050 GENERATE_PROC_METHOD(cdt_max_requests)
2051 GENERATE_PROC_METHOD(cdt_default_archive_id)
2054 * procfs write method for MDT/hsm_control
2055 * proc entry is in mdt directory so data is mdt obd_device pointer
2057 #define CDT_ENABLE_CMD "enabled"
2058 #define CDT_STOP_CMD "shutdown"
2059 #define CDT_DISABLE_CMD "disabled"
2060 #define CDT_PURGE_CMD "purge"
2061 #define CDT_HELP_CMD "help"
2062 #define CDT_MAX_CMD_LEN 10
2065 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
2066 size_t count, loff_t *off)
2068 struct seq_file *m = file->private_data;
2069 struct obd_device *obd = m->private;
2070 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2071 struct coordinator *cdt = &(mdt->mdt_coordinator);
2073 char kernbuf[CDT_MAX_CMD_LEN];
2076 if (count == 0 || count >= sizeof(kernbuf))
2079 if (copy_from_user(kernbuf, buffer, count))
2083 if (kernbuf[count - 1] == '\n')
2084 kernbuf[count - 1] = 0;
2087 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
2088 if (cdt->cdt_state == CDT_DISABLE) {
2089 rc = set_cdt_state(cdt, CDT_RUNNING, NULL);
2090 mdt_hsm_cdt_event(cdt);
2091 wake_up(&cdt->cdt_waitq);
2093 rc = mdt_hsm_cdt_start(mdt);
2095 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
2096 if ((cdt->cdt_state == CDT_STOPPING) ||
2097 (cdt->cdt_state == CDT_STOPPED)) {
2098 CERROR("%s: Coordinator already stopped\n",
2102 rc = mdt_hsm_cdt_stop(mdt);
2104 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
2105 if ((cdt->cdt_state == CDT_STOPPING) ||
2106 (cdt->cdt_state == CDT_STOPPED)) {
2107 CERROR("%s: Coordinator is stopped\n",
2111 rc = set_cdt_state(cdt, CDT_DISABLE, NULL);
2113 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
2114 rc = hsm_cancel_all_actions(mdt);
2115 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
2123 CERROR("%s: Valid coordinator control commands are: "
2124 "%s %s %s %s %s\n", mdt_obd_name(mdt),
2125 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2126 CDT_PURGE_CMD, CDT_HELP_CMD);
2134 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
2136 struct obd_device *obd = m->private;
2137 struct coordinator *cdt;
2140 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2142 seq_printf(m, "%s\n", cdt_mdt_state2str(cdt->cdt_state));
2148 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2154 for (i = 0; i < 8 * sizeof(mask); i++) {
2155 if (mask & (1UL << i)) {
2156 seq_printf(m, "%s%s", first ? "" : " ",
2157 hsm_copytool_action2name(i));
2167 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2169 struct mdt_device *mdt = m->private;
2170 struct coordinator *cdt = &mdt->mdt_coordinator;
2172 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2176 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2178 struct mdt_device *mdt = m->private;
2179 struct coordinator *cdt = &mdt->mdt_coordinator;
2181 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2185 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2187 struct mdt_device *mdt = m->private;
2188 struct coordinator *cdt = &mdt->mdt_coordinator;
2190 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2193 static inline enum hsm_copytool_action
2194 hsm_copytool_name2action(const char *name)
2196 if (strcasecmp(name, "NOOP") == 0)
2198 else if (strcasecmp(name, "ARCHIVE") == 0)
2199 return HSMA_ARCHIVE;
2200 else if (strcasecmp(name, "RESTORE") == 0)
2201 return HSMA_RESTORE;
2202 else if (strcasecmp(name, "REMOVE") == 0)
2204 else if (strcasecmp(name, "CANCEL") == 0)
2211 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2212 size_t user_count, __u64 *mask)
2214 char *buf, *pos, *name;
2220 if (!(user_count < 4096))
2223 buf_size = user_count + 1;
2225 OBD_ALLOC(buf, buf_size);
2229 if (copy_from_user(buf, user_buf, buf_size - 1))
2230 GOTO(out, rc = -EFAULT);
2232 buf[buf_size - 1] = '\0';
2235 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2241 action = hsm_copytool_name2action(name);
2243 GOTO(out, rc = -EINVAL);
2245 new_mask |= (1UL << action);
2251 OBD_FREE(buf, buf_size);
2257 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2258 size_t count, loff_t *off)
2260 struct seq_file *m = file->private_data;
2261 struct mdt_device *mdt = m->private;
2262 struct coordinator *cdt = &mdt->mdt_coordinator;
2264 return mdt_write_hsm_request_mask(file, buf, count,
2265 &cdt->cdt_user_request_mask);
2269 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2270 size_t count, loff_t *off)
2272 struct seq_file *m = file->private_data;
2273 struct mdt_device *mdt = m->private;
2274 struct coordinator *cdt = &mdt->mdt_coordinator;
2276 return mdt_write_hsm_request_mask(file, buf, count,
2277 &cdt->cdt_group_request_mask);
2281 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2282 size_t count, loff_t *off)
2284 struct seq_file *m = file->private_data;
2285 struct mdt_device *mdt = m->private;
2286 struct coordinator *cdt = &mdt->mdt_coordinator;
2288 return mdt_write_hsm_request_mask(file, buf, count,
2289 &cdt->cdt_other_request_mask);
2292 static int mdt_hsm_cdt_raolu_seq_show(struct seq_file *m, void *data)
2294 struct mdt_device *mdt = m->private;
2295 struct coordinator *cdt = &mdt->mdt_coordinator;
2298 seq_printf(m, "%d\n", (int)cdt->cdt_remove_archive_on_last_unlink);
2303 mdt_hsm_cdt_raolu_seq_write(struct file *file, const char __user *buffer,
2304 size_t count, loff_t *off)
2307 struct seq_file *m = file->private_data;
2308 struct mdt_device *mdt = m->private;
2309 struct coordinator *cdt = &mdt->mdt_coordinator;
2314 rc = lprocfs_str_to_s64(buffer, count, &val);
2318 cdt->cdt_remove_archive_on_last_unlink = val;
2322 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2323 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2324 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2325 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2326 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2327 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2328 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2329 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2330 LPROC_SEQ_FOPS(mdt_hsm_cdt_raolu);
2332 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2334 .fops = &mdt_hsm_agent_fops },
2335 { .name = "actions",
2336 .fops = &mdt_hsm_actions_fops,
2337 .proc_mode = 0444 },
2338 { .name = "default_archive_id",
2339 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2340 { .name = "grace_delay",
2341 .fops = &mdt_hsm_cdt_grace_delay_fops },
2342 { .name = "loop_period",
2343 .fops = &mdt_hsm_cdt_loop_period_fops },
2344 { .name = "max_requests",
2345 .fops = &mdt_hsm_cdt_max_requests_fops },
2347 .fops = &mdt_hsm_policy_fops },
2348 { .name = "active_request_timeout",
2349 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2350 { .name = "active_requests",
2351 .fops = &mdt_hsm_active_requests_fops },
2352 { .name = "user_request_mask",
2353 .fops = &mdt_hsm_user_request_mask_fops, },
2354 { .name = "group_request_mask",
2355 .fops = &mdt_hsm_group_request_mask_fops, },
2356 { .name = "other_request_mask",
2357 .fops = &mdt_hsm_other_request_mask_fops, },
2358 { .name = "remove_archive_on_last_unlink",
2359 .fops = &mdt_hsm_cdt_raolu_fops, },