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, 2014, 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_net.h>
44 #include <lustre_export.h>
46 #include <lprocfs_status.h>
47 #include <lustre_log.h>
48 #include <lustre_kernelcomm.h>
49 #include "mdt_internal.h"
51 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
54 * get obj and HSM attributes on a fid
55 * \param mti [IN] context
56 * \param fid [IN] object fid
57 * \param hsm [OUT] HSM meta data
58 * \retval obj or error (-ENOENT if not found)
60 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
61 const struct lu_fid *fid,
65 struct mdt_object *obj;
73 /* find object by FID */
74 obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
78 if (!mdt_object_exists(obj)) {
80 mdt_object_put(mti->mti_env, obj);
81 RETURN(ERR_PTR(-ENOENT));
84 rc = mdt_attr_get_complex(mti, obj, ma);
86 mdt_object_put(mti->mti_env, obj);
90 if (ma->ma_valid & MA_HSM)
93 memset(hsm, 0, sizeof(*hsm));
98 void mdt_hsm_dump_hal(int level, const char *prefix,
99 struct hsm_action_list *hal)
102 struct hsm_action_item *hai;
105 CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
106 " archive_id %d flags "LPX64"\n",
107 prefix, hal->hal_version, hal->hal_count,
108 hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
110 hai = hai_first(hal);
111 for (i = 0; i < hal->hal_count; i++) {
112 sz = hai->hai_len - sizeof(*hai);
113 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
114 " compound/cookie="LPX64"/"LPX64
115 " action=%s extent="LPX64"-"LPX64" gid="LPX64
116 " datalen=%d data=[%s]\n",
118 PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
119 hal->hal_compound_id, hai->hai_cookie,
120 hsm_copytool_action2name(hai->hai_action),
121 hai->hai_extent.offset,
122 hai->hai_extent.length,
124 hai_dump_data_field(hai, buf, sizeof(buf)));
130 * data passed to llog_cat_process() callback
131 * to scan requests and take actions
133 struct hsm_scan_request {
136 struct hsm_action_list *hal;
139 struct hsm_scan_data {
140 struct mdt_thread_info *mti;
141 char fs_name[MTI_NAME_MAXLEN+1];
142 /* request to be send to agents */
143 int max_requests; /** vector size */
144 int request_cnt; /** used count */
145 struct hsm_scan_request *request;
149 * llog_cat_process() callback, used to:
150 * - find waiting request and start action
151 * - purge canceled and done requests
152 * \param env [IN] environment
153 * \param llh [IN] llog handle
154 * \param hdr [IN] llog record
155 * \param data [IN/OUT] cb data = struct hsm_scan_data
157 * \retval -ve failure
159 static int mdt_coordinator_cb(const struct lu_env *env,
160 struct llog_handle *llh,
161 struct llog_rec_hdr *hdr,
164 struct llog_agent_req_rec *larr;
165 struct hsm_scan_data *hsd;
166 struct hsm_action_item *hai;
167 struct mdt_device *mdt;
168 struct coordinator *cdt;
173 mdt = hsd->mti->mti_mdt;
174 cdt = &mdt->mdt_coordinator;
176 larr = (struct llog_agent_req_rec *)hdr;
177 dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
178 switch (larr->arr_status) {
181 struct hsm_scan_request *request;
183 /* Are agents full? */
184 if (atomic_read(&cdt->cdt_request_count) >=
185 cdt->cdt_max_requests)
188 /* first search whether the request is found in the
189 * list we have built. */
191 for (i = 0; i < hsd->request_cnt; i++) {
192 if (hsd->request[i].hal->hal_compound_id ==
193 larr->arr_compound_id) {
194 request = &hsd->request[i];
200 struct hsm_action_list *hal;
202 if (hsd->request_cnt == hsd->max_requests)
203 /* Unknown request and no more room
204 * for a new request. Continue to scan
205 * to find other entries for already
210 request = &hsd->request[hsd->request_cnt];
212 /* allocates hai vector size just needs to be large
215 sizeof(*request->hal) +
216 cfs_size_round(MTI_NAME_MAXLEN+1) +
217 2 * cfs_size_round(larr->arr_hai.hai_len);
218 OBD_ALLOC(hal, request->hal_sz);
220 CERROR("%s: Cannot allocate memory (%d o)"
221 "for compound "LPX64"\n",
224 larr->arr_compound_id);
227 hal->hal_version = HAL_VERSION;
228 strlcpy(hal->hal_fsname, hsd->fs_name,
229 MTI_NAME_MAXLEN + 1);
230 hal->hal_compound_id = larr->arr_compound_id;
231 hal->hal_archive_id = larr->arr_archive_id;
232 hal->hal_flags = larr->arr_flags;
234 request->hal_used_sz = hal_size(hal);
237 hai = hai_first(hal);
239 /* request is known */
240 /* we check if record archive num is the same as the
241 * known request, if not we will serve it in multiple
242 * time because we do not know if the agent can serve
244 * a use case is a compound made of multiple restore
245 * where the files are not archived in the same backend
247 if (larr->arr_archive_id !=
248 request->hal->hal_archive_id)
251 if (request->hal_sz <
252 request->hal_used_sz +
253 cfs_size_round(larr->arr_hai.hai_len)) {
254 /* Not enough room, need an extension */
258 sz = 2 * request->hal_sz;
259 OBD_ALLOC(hal_buffer, sz);
261 CERROR("%s: Cannot allocate memory "
262 "(%d o) for compound "LPX64"\n",
263 mdt_obd_name(mdt), sz,
264 larr->arr_compound_id);
267 memcpy(hal_buffer, request->hal,
268 request->hal_used_sz);
269 OBD_FREE(request->hal,
271 request->hal = hal_buffer;
272 request->hal_sz = sz;
274 hai = hai_first(request->hal);
275 for (i = 0; i < request->hal->hal_count; i++)
278 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
279 hai->hai_cookie = larr->arr_hai.hai_cookie;
280 hai->hai_gid = larr->arr_hai.hai_gid;
282 request->hal_used_sz += cfs_size_round(hai->hai_len);
283 request->hal->hal_count++;
287 struct hsm_progress_kernel pgs;
288 struct cdt_agent_req *car;
289 cfs_time_t now = cfs_time_current_sec();
292 /* we search for a running request
293 * error may happen if coordinator crashes or stopped
294 * with running request
296 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
298 last = larr->arr_req_create;
300 last = car->car_req_update;
301 mdt_cdt_put_request(car);
304 /* test if request too long, if yes cancel it
305 * the same way the copy tool acknowledge a cancel request */
306 if (now <= last + cdt->cdt_active_req_timeout)
309 dump_llog_agent_req_rec("request timed out, start cleaning",
311 /* a too old cancel request just needs to be removed
312 * this can happen, if copy tool does not support
313 * cancel for other requests, we have to remove the
314 * running request and notify the copytool */
315 pgs.hpk_fid = larr->arr_hai.hai_fid;
316 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
317 pgs.hpk_extent = larr->arr_hai.hai_extent;
318 pgs.hpk_flags = HP_FLAG_COMPLETED;
319 pgs.hpk_errval = ENOSYS;
320 pgs.hpk_data_version = 0;
322 /* update request state, but do not record in llog, to
323 * avoid deadlock on cdt_llog_lock */
324 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
326 CERROR("%s: cannot cleanup timed out request: "
327 DFID" for cookie "LPX64" action=%s\n",
329 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
330 hsm_copytool_action2name(
331 larr->arr_hai.hai_action));
334 /* The request no longer exists, forget
335 * about it, and do not send a cancel request
336 * to the client, for which an error will be
337 * sent back, leading to an endless cycle of
339 RETURN(LLOG_DEL_RECORD);
342 /* XXX A cancel request cannot be cancelled. */
343 if (larr->arr_hai.hai_action == HSMA_CANCEL)
346 larr->arr_status = ARS_CANCELED;
347 larr->arr_req_change = now;
348 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
350 CERROR("%s: cannot update agent log: rc = %d\n",
351 mdt_obd_name(mdt), rc);
357 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
358 cfs_time_current_sec())
359 RETURN(LLOG_DEL_RECORD);
366 * create /proc entries for coordinator
369 * \retval -ve failure
371 int hsm_cdt_procfs_init(struct mdt_device *mdt)
373 struct coordinator *cdt = &mdt->mdt_coordinator;
377 /* init /proc entries, failure is not critical */
378 cdt->cdt_proc_dir = lprocfs_register("hsm",
379 mdt2obd_dev(mdt)->obd_proc_entry,
380 lprocfs_mdt_hsm_vars, mdt);
381 if (IS_ERR(cdt->cdt_proc_dir)) {
382 rc = PTR_ERR(cdt->cdt_proc_dir);
383 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
384 " rc=%d\n", mdt_obd_name(mdt), rc);
385 cdt->cdt_proc_dir = NULL;
393 * remove /proc entries for coordinator
396 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
398 struct coordinator *cdt = &mdt->mdt_coordinator;
400 LASSERT(cdt->cdt_state == CDT_STOPPED);
401 if (cdt->cdt_proc_dir != NULL)
402 lprocfs_remove(&cdt->cdt_proc_dir);
406 * get vector of hsm cdt /proc vars
410 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
412 return lprocfs_mdt_hsm_vars;
417 * \param data [IN] obd device
419 * \retval -ve failure
421 static int mdt_coordinator(void *data)
423 struct mdt_thread_info *mti = data;
424 struct mdt_device *mdt = mti->mti_mdt;
425 struct coordinator *cdt = &mdt->mdt_coordinator;
426 struct hsm_scan_data hsd = { NULL };
431 cdt->cdt_thread.t_flags = SVC_RUNNING;
432 wake_up(&cdt->cdt_thread.t_ctl_waitq);
434 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
435 mdt_obd_name(mdt), current_pid());
437 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
438 * increases due to a change from /proc we do not overflow the
439 * hsd.request[] vector
441 hsd.max_requests = cdt->cdt_max_requests;
442 request_sz = hsd.max_requests * sizeof(*hsd.request);
443 OBD_ALLOC(hsd.request, request_sz);
445 GOTO(out, rc = -ENOMEM);
448 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
451 struct l_wait_info lwi;
454 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
456 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
457 (cdt->cdt_thread.t_flags &
458 (SVC_STOPPING|SVC_EVENT)),
461 CDEBUG(D_HSM, "coordinator resumes\n");
463 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
464 cdt->cdt_state == CDT_STOPPING) {
465 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
470 /* wake up before timeout, new work arrives */
471 if (cdt->cdt_thread.t_flags & SVC_EVENT)
472 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
474 /* if coordinator is suspended continue to wait */
475 if (cdt->cdt_state == CDT_DISABLE) {
476 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
480 CDEBUG(D_HSM, "coordinator starts reading llog\n");
482 if (hsd.max_requests != cdt->cdt_max_requests) {
483 /* cdt_max_requests has changed,
484 * we need to allocate a new buffer
486 OBD_FREE(hsd.request, request_sz);
487 hsd.max_requests = cdt->cdt_max_requests;
488 request_sz = hsd.max_requests * sizeof(*hsd.request);
489 OBD_ALLOC(hsd.request, request_sz);
498 rc = cdt_llog_process(mti->mti_env, mdt,
499 mdt_coordinator_cb, &hsd);
503 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
505 if (list_empty(&cdt->cdt_agents)) {
506 CDEBUG(D_HSM, "no agent available, "
507 "coordinator sleeps\n");
511 /* here hsd contains a list of requests to be started */
512 for (i = 0; i < hsd.request_cnt; i++) {
513 struct hsm_scan_request *request = &hsd.request[i];
514 struct hsm_action_list *hal = request->hal;
515 struct hsm_action_item *hai;
518 enum agent_req_status status;
520 /* still room for work ? */
521 if (atomic_read(&cdt->cdt_request_count) >=
522 cdt->cdt_max_requests)
525 rc = mdt_hsm_agent_send(mti, hal, 0);
526 /* if failure, we suppose it is temporary
527 * if the copy tool failed to do the request
528 * it has to use hsm_progress
530 status = (rc ? ARS_WAITING : ARS_STARTED);
532 /* set up cookie vector to set records status
533 * after copy tools start or failed
535 sz = hal->hal_count * sizeof(__u64);
536 OBD_ALLOC(cookies, sz);
537 if (cookies == NULL) {
538 CERROR("%s: Cannot allocate memory (%d o) "
539 "for cookies vector "LPX64"\n",
540 mdt_obd_name(mdt), sz,
541 hal->hal_compound_id);
544 hai = hai_first(hal);
545 for (j = 0; j < hal->hal_count; j++) {
546 cookies[j] = hai->hai_cookie;
550 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
551 hal->hal_count, status);
553 CERROR("%s: mdt_agent_record_update() failed, "
554 "rc=%d, cannot update status to %s "
556 mdt_obd_name(mdt), rc,
557 agent_req_status2name(status),
560 OBD_FREE(cookies, sz);
563 /* free hal allocated by callback */
564 for (i = 0; i < hsd.request_cnt; i++) {
565 struct hsm_scan_request *request = &hsd.request[i];
567 OBD_FREE(request->hal, request->hal_sz);
573 OBD_FREE(hsd.request, request_sz);
575 if (cdt->cdt_state == CDT_STOPPING) {
576 /* request comes from /proc path, so we need to clean cdt
578 mdt_hsm_cdt_stop(mdt);
579 mdt->mdt_opts.mo_coordinator = 0;
581 /* request comes from a thread event, generated
582 * by mdt_stop_coordinator(), we have to ack
583 * and cdt cleaning will be done by event sender
585 cdt->cdt_thread.t_flags = SVC_STOPPED;
586 wake_up(&cdt->cdt_thread.t_ctl_waitq);
590 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
591 mdt_obd_name(mdt), current_pid(), rc);
593 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
595 mdt_obd_name(mdt), current_pid());
601 * lookup a restore handle by FID
602 * caller needs to hold cdt_restore_lock
603 * \param cdt [IN] coordinator
604 * \param fid [IN] FID
605 * \retval cdt_restore_handle found
606 * \retval NULL not found
608 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
609 const struct lu_fid *fid)
611 struct cdt_restore_handle *crh;
614 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
615 if (lu_fid_eq(&crh->crh_fid, fid))
622 * data passed to llog_cat_process() callback
623 * to scan requests and take actions
625 struct hsm_restore_data {
626 struct mdt_thread_info *hrd_mti;
630 * llog_cat_process() callback, used to:
631 * - find restore request and allocate the restore handle
632 * \param env [IN] environment
633 * \param llh [IN] llog handle
634 * \param hdr [IN] llog record
635 * \param data [IN/OUT] cb data = struct hsm_restore_data
637 * \retval -ve failure
639 static int hsm_restore_cb(const struct lu_env *env,
640 struct llog_handle *llh,
641 struct llog_rec_hdr *hdr, void *data)
643 struct llog_agent_req_rec *larr;
644 struct hsm_restore_data *hrd;
645 struct cdt_restore_handle *crh;
646 struct hsm_action_item *hai;
647 struct mdt_thread_info *mti;
648 struct coordinator *cdt;
649 struct mdt_object *child;
655 cdt = &mti->mti_mdt->mdt_coordinator;
657 larr = (struct llog_agent_req_rec *)hdr;
658 hai = &larr->arr_hai;
659 if (hai->hai_cookie > cdt->cdt_last_cookie)
660 /* update the cookie to avoid collision */
661 cdt->cdt_last_cookie = hai->hai_cookie + 1;
663 if (hai->hai_action != HSMA_RESTORE ||
664 agent_req_in_final_state(larr->arr_status))
667 /* restore request not in a final state */
669 /* force replay of restore requests left in started state from previous
670 * CDT context, to be canceled later if finally found to be incompatible
671 * when being re-started */
672 if (larr->arr_status == ARS_STARTED) {
673 larr->arr_status = ARS_WAITING;
674 larr->arr_req_change = cfs_time_current_sec();
675 rc = llog_write(env, llh, hdr, hdr->lrh_index);
680 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
684 crh->crh_fid = hai->hai_fid;
685 /* in V1 all file is restored
686 crh->extent.start = hai->hai_extent.offset;
687 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
689 crh->crh_extent.start = 0;
690 crh->crh_extent.end = hai->hai_extent.length;
691 /* get the layout lock */
692 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
693 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
694 MDS_INODELOCK_LAYOUT);
696 GOTO(out, rc = PTR_ERR(child));
699 /* we choose to not keep a reference
700 * on the object during the restore time which can be very long */
701 mdt_object_put(mti->mti_env, child);
703 mutex_lock(&cdt->cdt_restore_lock);
704 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
705 mutex_unlock(&cdt->cdt_restore_lock);
712 * restore coordinator state at startup
713 * the goal is to take a layout lock for each registered restore request
714 * \param mti [IN] context
716 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
718 struct hsm_restore_data hrd;
724 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
725 hsm_restore_cb, &hrd);
730 static int hsm_init_ucred(struct lu_ucred *uc)
734 uc->uc_valid = UCRED_OLD;
743 uc->uc_suppgids[0] = -1;
744 uc->uc_suppgids[1] = -1;
745 uc->uc_cap = CFS_CAP_FS_MASK;
748 uc->uc_identity = NULL;
754 * wake up coordinator thread
755 * \param mdt [IN] device
757 * \retval -ve failure
759 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
761 struct coordinator *cdt = &mdt->mdt_coordinator;
764 if (cdt->cdt_state == CDT_STOPPED)
767 /* wake up coordinator */
768 cdt->cdt_thread.t_flags = SVC_EVENT;
769 wake_up(&cdt->cdt_thread.t_ctl_waitq);
775 * initialize coordinator struct
776 * \param mdt [IN] device
778 * \retval -ve failure
780 int mdt_hsm_cdt_init(struct mdt_device *mdt)
782 struct coordinator *cdt = &mdt->mdt_coordinator;
783 struct mdt_thread_info *cdt_mti = NULL;
787 cdt->cdt_state = CDT_STOPPED;
789 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
790 mutex_init(&cdt->cdt_llog_lock);
791 init_rwsem(&cdt->cdt_agent_lock);
792 init_rwsem(&cdt->cdt_request_lock);
793 mutex_init(&cdt->cdt_restore_lock);
795 INIT_LIST_HEAD(&cdt->cdt_requests);
796 INIT_LIST_HEAD(&cdt->cdt_agents);
797 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
799 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
803 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
804 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
806 lu_context_enter(&cdt->cdt_session);
807 cdt->cdt_env.le_ses = &cdt->cdt_session;
809 lu_env_fini(&cdt->cdt_env);
813 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
814 LASSERT(cdt_mti != NULL);
816 cdt_mti->mti_env = &cdt->cdt_env;
817 cdt_mti->mti_mdt = mdt;
819 hsm_init_ucred(mdt_ucred(cdt_mti));
821 /* default values for /proc tunnables
822 * can be override by MGS conf */
823 cdt->cdt_default_archive_id = 1;
824 cdt->cdt_grace_delay = 60;
825 cdt->cdt_loop_period = 10;
826 cdt->cdt_max_requests = 3;
827 cdt->cdt_policy = CDT_DEFAULT_POLICY;
828 cdt->cdt_active_req_timeout = 3600;
834 * free a coordinator thread
835 * \param mdt [IN] device
837 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
839 struct coordinator *cdt = &mdt->mdt_coordinator;
842 lu_context_exit(cdt->cdt_env.le_ses);
843 lu_context_fini(cdt->cdt_env.le_ses);
845 lu_env_fini(&cdt->cdt_env);
851 * start a coordinator thread
852 * \param mdt [IN] device
854 * \retval -ve failure
856 int mdt_hsm_cdt_start(struct mdt_device *mdt)
858 struct coordinator *cdt = &mdt->mdt_coordinator;
861 struct mdt_thread_info *cdt_mti;
862 struct task_struct *task;
865 /* functions defined but not yet used
866 * this avoid compilation warning
870 if (cdt->cdt_state != CDT_STOPPED) {
871 CERROR("%s: Coordinator already started\n",
876 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
877 cdt->cdt_policy = CDT_DEFAULT_POLICY;
879 cdt->cdt_state = CDT_INIT;
881 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
882 /* just need to be larger than previous one */
883 /* cdt_last_cookie is protected by cdt_llog_lock */
884 cdt->cdt_last_cookie = cfs_time_current_sec();
885 atomic_set(&cdt->cdt_request_count, 0);
886 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
887 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
888 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
890 /* to avoid deadlock when start is made through /proc
891 * /proc entries are created by the coordinator thread */
893 /* set up list of started restore requests */
894 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
895 rc = mdt_hsm_pending_restore(cdt_mti);
897 CERROR("%s: cannot take the layout locks needed"
898 " for registered restore: %d\n",
899 mdt_obd_name(mdt), rc);
901 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
904 cdt->cdt_state = CDT_STOPPED;
905 CERROR("%s: error starting coordinator thread: %d\n",
906 mdt_obd_name(mdt), rc);
909 CDEBUG(D_HSM, "%s: coordinator thread started\n",
914 wait_event(cdt->cdt_thread.t_ctl_waitq,
915 (cdt->cdt_thread.t_flags & SVC_RUNNING));
917 cdt->cdt_state = CDT_RUNNING;
918 mdt->mdt_opts.mo_coordinator = 1;
923 * stop a coordinator thread
924 * \param mdt [IN] device
926 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
928 struct coordinator *cdt = &mdt->mdt_coordinator;
929 struct cdt_agent_req *car, *tmp1;
930 struct hsm_agent *ha, *tmp2;
931 struct cdt_restore_handle *crh, *tmp3;
932 struct mdt_thread_info *cdt_mti;
935 if (cdt->cdt_state == CDT_STOPPED) {
936 CERROR("%s: Coordinator already stopped\n",
941 if (cdt->cdt_state != CDT_STOPPING) {
942 /* stop coordinator thread before cleaning */
943 cdt->cdt_thread.t_flags = SVC_STOPPING;
944 wake_up(&cdt->cdt_thread.t_ctl_waitq);
945 wait_event(cdt->cdt_thread.t_ctl_waitq,
946 cdt->cdt_thread.t_flags & SVC_STOPPED);
948 cdt->cdt_state = CDT_STOPPED;
951 down_write(&cdt->cdt_request_lock);
952 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
954 list_del(&car->car_request_list);
955 mdt_cdt_free_request(car);
957 up_write(&cdt->cdt_request_lock);
959 down_write(&cdt->cdt_agent_lock);
960 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
961 list_del(&ha->ha_list);
964 up_write(&cdt->cdt_agent_lock);
966 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
967 mutex_lock(&cdt->cdt_restore_lock);
968 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
969 struct mdt_object *child;
971 /* give back layout lock */
972 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
974 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
976 list_del(&crh->crh_list);
978 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
980 mutex_unlock(&cdt->cdt_restore_lock);
982 mdt->mdt_opts.mo_coordinator = 0;
988 * register all requests from an hal in the memory list
989 * \param mti [IN] context
990 * \param hal [IN] request
991 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
992 * which is running the CT
994 * \retval -ve failure
996 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
997 struct hsm_action_list *hal, struct obd_uuid *uuid)
999 struct mdt_device *mdt = mti->mti_mdt;
1000 struct coordinator *cdt = &mdt->mdt_coordinator;
1001 struct hsm_action_item *hai;
1005 /* register request in memory list */
1006 hai = hai_first(hal);
1007 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1008 struct cdt_agent_req *car;
1010 /* in case of a cancel request, we first mark the ondisk
1011 * record of the request we want to stop as canceled
1012 * this does not change the cancel record
1013 * it will be done when updating the request status
1015 if (hai->hai_action == HSMA_CANCEL) {
1016 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1020 CERROR("%s: mdt_agent_record_update() failed, "
1021 "rc=%d, cannot update status to %s "
1022 "for cookie "LPX64"\n",
1023 mdt_obd_name(mdt), rc,
1024 agent_req_status2name(ARS_CANCELED),
1029 /* find the running request to set it canceled */
1030 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1032 car->car_canceled = 1;
1033 /* uuid has to be changed to the one running the
1034 * request to cancel */
1035 *uuid = car->car_uuid;
1036 mdt_cdt_put_request(car);
1038 /* no need to memorize cancel request
1039 * this also avoid a deadlock when we receive
1040 * a purge all requests command
1045 if (hai->hai_action == HSMA_ARCHIVE) {
1046 struct mdt_object *obj;
1049 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1050 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1053 GOTO(out, rc = PTR_ERR(obj));
1055 hsm.mh_flags |= HS_EXISTS;
1056 hsm.mh_arch_id = hal->hal_archive_id;
1057 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1058 mdt_object_put(mti->mti_env, obj);
1063 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1064 hal->hal_archive_id, hal->hal_flags,
1067 GOTO(out, rc = PTR_ERR(car));
1069 rc = mdt_cdt_add_request(cdt, car);
1071 mdt_cdt_free_request(car);
1078 * swap layouts between 2 fids
1079 * \param mti [IN] context
1082 * \param mh_common [IN] MD HSM
1084 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1085 const lustre_fid *fid, const lustre_fid *dfid,
1086 struct md_hsm *mh_common)
1088 struct mdt_device *mdt = mti->mti_mdt;
1089 struct mdt_object *child1, *child2;
1090 struct mdt_lock_handle *lh2;
1094 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1096 GOTO(out, rc = PTR_ERR(child1));
1098 /* we already have layout lock on FID so take only
1100 lh2 = &mti->mti_lh[MDT_LH_OLD];
1101 mdt_lock_reg_init(lh2, LCK_EX);
1102 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1104 GOTO(out_child1, rc = PTR_ERR(child2));
1106 /* if copy tool closes the volatile before sending the final
1107 * progress through llapi_hsm_copy_end(), all the objects
1108 * are removed and mdd_swap_layout LBUG */
1109 if (!mdt_object_exists(child2)) {
1110 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1111 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1112 GOTO(out_child2, rc = -ENOENT);
1114 /* Since we only handle restores here, unconditionally use
1115 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1116 * be preserved in case of failure during swap_layout and not
1117 * leave a file in an intermediate but incoherent state.
1118 * But need to setup HSM xattr of data FID before, reuse
1119 * mti and mh presets for FID in hsm_cdt_request_completed(),
1120 * only need to clear RELEASED and DIRTY.
1122 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1123 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1125 rc = mo_swap_layouts(mti->mti_env,
1126 mdt_object_child(child1),
1127 mdt_object_child(child2),
1128 SWAP_LAYOUTS_MDS_HSM);
1131 mdt_object_unlock_put(mti, child2, lh2, 1);
1133 mdt_object_put(mti->mti_env, child1);
1139 * update status of a completed request
1140 * \param mti [IN] context
1141 * \param pgs [IN] progress of the copy tool
1142 * \param update_record [IN] update llog record
1144 * \retval -ve failure
1146 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1147 struct hsm_progress_kernel *pgs,
1148 const struct cdt_agent_req *car,
1149 enum agent_req_status *status)
1151 const struct lu_env *env = mti->mti_env;
1152 struct mdt_device *mdt = mti->mti_mdt;
1153 struct coordinator *cdt = &mdt->mdt_coordinator;
1154 struct mdt_object *obj = NULL;
1155 int cl_flags = 0, rc = 0;
1160 /* default is to retry */
1161 *status = ARS_WAITING;
1163 /* find object by FID */
1164 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1165 /* we will update MD HSM only if needed */
1166 is_mh_changed = false;
1168 /* object removed */
1169 *status = ARS_SUCCEED;
1173 /* no need to change mh->mh_arch_id
1174 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1176 if (pgs->hpk_errval != 0) {
1177 switch (pgs->hpk_errval) {
1179 /* the copy tool does not support cancel
1180 * so the cancel request is failed
1181 * As we cannot distinguish a cancel progress
1182 * from another action progress (they have the
1183 * same cookie), we suppose here the CT returns
1184 * ENOSYS only if does not support cancel
1186 /* this can also happen when cdt calls it to
1187 * for a timed out request */
1188 *status = ARS_FAILED;
1189 /* to have a cancel event in changelog */
1190 pgs->hpk_errval = ECANCELED;
1193 /* the request record has already been set to
1194 * ARS_CANCELED, this set the cancel request
1196 *status = ARS_SUCCEED;
1199 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1200 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1201 ARS_FAILED : ARS_WAITING);
1205 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1206 CERROR("%s: Request "LPX64" on "DFID
1207 " failed, error code %d too large\n",
1209 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1211 hsm_set_cl_error(&cl_flags,
1212 CLF_HSM_ERROVERFLOW);
1215 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1218 switch (car->car_hai->hai_action) {
1220 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1223 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1226 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1229 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1230 CERROR("%s: Failed request "LPX64" on "DFID
1231 " cannot be a CANCEL\n",
1234 PFID(&pgs->hpk_fid));
1237 CERROR("%s: Failed request "LPX64" on "DFID
1238 " %d is an unknown action\n",
1240 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1241 car->car_hai->hai_action);
1246 *status = ARS_SUCCEED;
1247 switch (car->car_hai->hai_action) {
1249 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1250 /* set ARCHIVE keep EXIST and clear LOST and
1252 mh.mh_arch_ver = pgs->hpk_data_version;
1253 mh.mh_flags |= HS_ARCHIVED;
1254 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1255 is_mh_changed = true;
1258 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1260 /* do not clear RELEASED and DIRTY here
1261 * this will occur in hsm_swap_layouts()
1264 /* Restoring has changed the file version on
1266 mh.mh_arch_ver = pgs->hpk_data_version;
1267 is_mh_changed = true;
1270 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1271 /* clear ARCHIVED EXISTS and LOST */
1272 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1273 is_mh_changed = true;
1276 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1277 CERROR("%s: Successful request "LPX64
1279 " cannot be a CANCEL\n",
1282 PFID(&pgs->hpk_fid));
1285 CERROR("%s: Successful request "LPX64
1287 " %d is an unknown action\n",
1289 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1290 car->car_hai->hai_action);
1296 /* rc != 0 means error when analysing action, it may come from
1297 * a crasy CT no need to manage DIRTY
1300 hsm_set_cl_flags(&cl_flags,
1301 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1303 /* unlock is done later, after layout lock management */
1305 rc = mdt_hsm_attr_set(mti, obj, &mh);
1308 /* we give back layout lock only if restore was successful or
1309 * if restore was canceled or if policy is to not retry
1310 * in other cases we just unlock the object */
1311 if (car->car_hai->hai_action == HSMA_RESTORE &&
1312 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1313 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1314 struct cdt_restore_handle *crh;
1316 /* restore in data FID done, we swap the layouts
1317 * only if restore is successful */
1318 if (pgs->hpk_errval == 0) {
1319 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1320 &car->car_hai->hai_dfid, &mh);
1322 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1323 *status = ARS_FAILED;
1324 pgs->hpk_errval = -rc;
1327 /* we have to retry, so keep layout lock */
1328 if (*status == ARS_WAITING)
1331 /* give back layout lock */
1332 mutex_lock(&cdt->cdt_restore_lock);
1333 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1335 list_del(&crh->crh_list);
1336 mutex_unlock(&cdt->cdt_restore_lock);
1337 /* just give back layout lock, we keep
1338 * the reference which is given back
1339 * later with the lock for HSM flags */
1340 if (!IS_ERR(obj) && crh != NULL)
1341 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1344 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1350 if (obj != NULL && !IS_ERR(obj)) {
1351 mo_changelog(env, CL_HSM, cl_flags,
1352 mdt_object_child(obj));
1353 mdt_object_put(mti->mti_env, obj);
1360 * update status of a request
1361 * \param mti [IN] context
1362 * \param pgs [IN] progress of the copy tool
1363 * \param update_record [IN] update llog record
1365 * \retval -ve failure
1367 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1368 struct hsm_progress_kernel *pgs,
1369 const int update_record)
1371 struct mdt_device *mdt = mti->mti_mdt;
1372 struct coordinator *cdt = &mdt->mdt_coordinator;
1373 struct cdt_agent_req *car;
1377 /* no coordinator started, so we cannot serve requests */
1378 if (cdt->cdt_state == CDT_STOPPED)
1381 /* first do sanity checks */
1382 car = mdt_cdt_update_request(cdt, pgs);
1384 CERROR("%s: Cannot find running request for cookie "LPX64
1387 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1389 RETURN(PTR_ERR(car));
1392 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1393 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1394 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1395 hsm_copytool_action2name(car->car_hai->hai_action),
1396 pgs->hpk_flags, pgs->hpk_errval,
1397 PFID(&car->car_hai->hai_fid),
1398 PFID(&car->car_hai->hai_dfid));
1400 /* progress is done on FID or data FID depending of the action and
1401 * of the copy progress */
1402 /* for restore progress is used to send back the data FID to cdt */
1403 if (car->car_hai->hai_action == HSMA_RESTORE &&
1404 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1405 car->car_hai->hai_dfid = pgs->hpk_fid;
1407 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1408 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1409 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1410 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1411 CERROR("%s: Progress on "DFID" for cookie "LPX64
1412 " does not match request FID "DFID" nor data FID "
1415 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1416 PFID(&car->car_hai->hai_fid),
1417 PFID(&car->car_hai->hai_dfid));
1418 GOTO(out, rc = -EINVAL);
1421 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1422 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1423 " is not coherent (err=%d and not completed"
1426 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1427 hsm_copytool_action2name(car->car_hai->hai_action),
1428 pgs->hpk_errval, pgs->hpk_flags);
1429 GOTO(out, rc = -EINVAL);
1432 /* now progress is valid */
1434 /* we use a root like ucred */
1435 hsm_init_ucred(mdt_ucred(mti));
1437 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1438 enum agent_req_status status;
1440 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1442 /* remove request from memory list */
1443 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1445 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1446 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1448 hsm_copytool_action2name(car->car_hai->hai_action),
1449 agent_req_status2name(status));
1451 if (update_record) {
1454 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1455 &pgs->hpk_cookie, 1,
1458 CERROR("%s: mdt_agent_record_update() failed,"
1459 " rc=%d, cannot update status to %s"
1460 " for cookie "LPX64"\n",
1461 mdt_obd_name(mdt), rc1,
1462 agent_req_status2name(status),
1464 rc = (rc != 0 ? rc : rc1);
1466 /* ct has completed a request, so a slot is available, wakeup
1467 * cdt to find new work */
1468 mdt_hsm_cdt_wakeup(mdt);
1470 /* if copytool send a progress on a canceled request
1471 * we inform copytool it should stop
1473 if (car->car_canceled == 1)
1479 /* remove ref got from mdt_cdt_update_request() */
1480 mdt_cdt_put_request(car);
1487 * data passed to llog_cat_process() callback
1488 * to cancel requests
1490 struct hsm_cancel_all_data {
1491 struct mdt_device *mdt;
1495 * llog_cat_process() callback, used to:
1496 * - purge all requests
1497 * \param env [IN] environment
1498 * \param llh [IN] llog handle
1499 * \param hdr [IN] llog record
1500 * \param data [IN] cb data = struct hsm_cancel_all_data
1502 * \retval -ve failure
1504 static int mdt_cancel_all_cb(const struct lu_env *env,
1505 struct llog_handle *llh,
1506 struct llog_rec_hdr *hdr, void *data)
1508 struct llog_agent_req_rec *larr;
1509 struct hsm_cancel_all_data *hcad;
1513 larr = (struct llog_agent_req_rec *)hdr;
1515 if (larr->arr_status == ARS_WAITING ||
1516 larr->arr_status == ARS_STARTED) {
1517 larr->arr_status = ARS_CANCELED;
1518 larr->arr_req_change = cfs_time_current_sec();
1519 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1526 * cancel all actions
1527 * \param obd [IN] MDT device
1529 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1531 struct mdt_thread_info *mti;
1532 struct coordinator *cdt = &mdt->mdt_coordinator;
1533 struct cdt_agent_req *car;
1534 struct hsm_action_list *hal = NULL;
1535 struct hsm_action_item *hai;
1536 struct hsm_cancel_all_data hcad;
1537 int hal_sz = 0, hal_len, rc;
1538 enum cdt_states save_state;
1541 /* retrieve coordinator context */
1542 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1544 /* disable coordinator */
1545 save_state = cdt->cdt_state;
1546 cdt->cdt_state = CDT_DISABLE;
1548 /* send cancel to all running requests */
1549 down_read(&cdt->cdt_request_lock);
1550 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1551 mdt_cdt_get_request(car);
1552 /* request is not yet removed from list, it will be done
1553 * when copytool will return progress
1556 if (car->car_hai->hai_action == HSMA_CANCEL) {
1557 mdt_cdt_put_request(car);
1562 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1563 cfs_size_round(car->car_hai->hai_len);
1565 if (hal_len > hal_sz && hal_sz > 0) {
1566 /* not enough room, free old buffer */
1567 OBD_FREE(hal, hal_sz);
1571 /* empty buffer, allocate one */
1574 OBD_ALLOC(hal, hal_sz);
1576 mdt_cdt_put_request(car);
1577 up_read(&cdt->cdt_request_lock);
1578 GOTO(out, rc = -ENOMEM);
1582 hal->hal_version = HAL_VERSION;
1583 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1585 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1586 hal->hal_compound_id = car->car_compound_id;
1587 hal->hal_archive_id = car->car_archive_id;
1588 hal->hal_flags = car->car_flags;
1591 hai = hai_first(hal);
1592 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1593 hai->hai_action = HSMA_CANCEL;
1596 /* it is possible to safely call mdt_hsm_agent_send()
1597 * (ie without a deadlock on cdt_request_lock), because the
1598 * write lock is taken only if we are not in purge mode
1599 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1600 * nor mdt_cdt_remove_request())
1602 /* no conflict with cdt thread because cdt is disable and we
1603 * have the request lock */
1604 mdt_hsm_agent_send(mti, hal, 1);
1606 mdt_cdt_put_request(car);
1608 up_read(&cdt->cdt_request_lock);
1611 OBD_FREE(hal, hal_sz);
1613 /* cancel all on-disk records */
1616 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1617 mdt_cancel_all_cb, &hcad);
1619 /* enable coordinator */
1620 cdt->cdt_state = save_state;
1626 * check if a request is compatible with file status
1627 * \param hai [IN] request description
1628 * \param hal_an [IN] request archive number (not used)
1629 * \param rq_flags [IN] request flags
1630 * \param hsm [IN] file HSM metadata
1633 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1634 const int hal_an, const __u64 rq_flags,
1635 const struct md_hsm *hsm)
1637 int is_compat = false;
1641 hsm_flags = hsm->mh_flags;
1642 switch (hai->hai_action) {
1644 if (!(hsm_flags & HS_NOARCHIVE) &&
1645 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1649 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1650 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1654 if (!(hsm_flags & HS_RELEASED) &&
1655 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1662 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1663 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1664 PFID(&hai->hai_fid),
1665 hsm_copytool_action2name(hai->hai_action), rq_flags,
1666 hai->hai_extent.offset, hai->hai_extent.length,
1668 (is_compat ? "compatible" : "uncompatible"));
1674 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1676 static const struct {
1680 } hsm_policy_names[] = {
1681 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1682 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1687 * convert a policy name to a bit
1688 * \param name [IN] policy name
1690 * \retval policy bit
1692 static __u64 hsm_policy_str2bit(const char *name)
1696 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1697 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1698 strcmp(hsm_policy_names[i].name, name) == 0)
1699 return hsm_policy_names[i].bit;
1704 * convert a policy bit field to a string
1705 * \param mask [IN] policy bit field
1706 * \param hexa [IN] print mask before bit names
1707 * \param buffer [OUT] string
1708 * \param count [IN] size of buffer
1710 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1718 seq_printf(m, "("LPX64") ", mask);
1720 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1723 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1724 if (hsm_policy_names[j].bit == bit)
1728 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1730 seq_printf(m, "%s ", hsm_policy_names[j].name);
1732 /* remove last ' ' */
1737 /* methods to read/write HSM policy flags */
1738 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1740 struct mdt_device *mdt = m->private;
1741 struct coordinator *cdt = &mdt->mdt_coordinator;
1744 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1749 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1750 size_t count, loff_t *off)
1752 struct seq_file *m = file->private_data;
1753 struct mdt_device *mdt = m->private;
1754 struct coordinator *cdt = &mdt->mdt_coordinator;
1755 char *start, *token, sign;
1758 __u64 add_mask, remove_mask, set_mask;
1762 if (count + 1 > PAGE_SIZE)
1765 OBD_ALLOC(buf, count + 1);
1769 if (copy_from_user(buf, buffer, count))
1770 GOTO(out, rc = -EFAULT);
1775 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1778 add_mask = remove_mask = set_mask = 0;
1780 token = strsep(&start, "\n ");
1786 if (sign == '-' || sign == '+')
1789 policy = hsm_policy_str2bit(token);
1791 CWARN("%s: '%s' is unknown, "
1792 "supported policies are:\n", mdt_obd_name(mdt),
1794 hsm_policy_bit2str(m, 0, false);
1795 GOTO(out, rc = -EINVAL);
1799 remove_mask |= policy;
1809 } while (start != NULL);
1811 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1812 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1814 /* if no sign in all string, it is a clear and set
1815 * if some sign found, all unsigned are converted
1817 * P1 P2 = set to P1 and P2
1818 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1820 if (remove_mask == 0 && add_mask == 0) {
1821 cdt->cdt_policy = set_mask;
1823 cdt->cdt_policy |= set_mask | add_mask;
1824 cdt->cdt_policy &= ~remove_mask;
1827 GOTO(out, rc = count);
1830 OBD_FREE(buf, count + 1);
1833 LPROC_SEQ_FOPS(mdt_hsm_policy);
1835 #define GENERATE_PROC_METHOD(VAR) \
1836 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1838 struct mdt_device *mdt = m->private; \
1839 struct coordinator *cdt = &mdt->mdt_coordinator; \
1842 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1846 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1847 size_t count, loff_t *off) \
1850 struct seq_file *m = file->private_data; \
1851 struct mdt_device *mdt = m->private; \
1852 struct coordinator *cdt = &mdt->mdt_coordinator; \
1857 rc = lprocfs_str_to_s64(buffer, count, &val); \
1860 if (val > 0 && val < INT_MAX) { \
1867 GENERATE_PROC_METHOD(cdt_loop_period)
1868 GENERATE_PROC_METHOD(cdt_grace_delay)
1869 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1870 GENERATE_PROC_METHOD(cdt_max_requests)
1871 GENERATE_PROC_METHOD(cdt_default_archive_id)
1874 * procfs write method for MDT/hsm_control
1875 * proc entry is in mdt directory so data is mdt obd_device pointer
1877 #define CDT_ENABLE_CMD "enabled"
1878 #define CDT_STOP_CMD "shutdown"
1879 #define CDT_DISABLE_CMD "disabled"
1880 #define CDT_PURGE_CMD "purge"
1881 #define CDT_HELP_CMD "help"
1882 #define CDT_MAX_CMD_LEN 10
1885 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1886 size_t count, loff_t *off)
1888 struct seq_file *m = file->private_data;
1889 struct obd_device *obd = m->private;
1890 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1891 struct coordinator *cdt = &(mdt->mdt_coordinator);
1893 char kernbuf[CDT_MAX_CMD_LEN];
1896 if (count == 0 || count >= sizeof(kernbuf))
1899 if (copy_from_user(kernbuf, buffer, count))
1903 if (kernbuf[count - 1] == '\n')
1904 kernbuf[count - 1] = 0;
1907 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1908 if (cdt->cdt_state == CDT_DISABLE) {
1909 cdt->cdt_state = CDT_RUNNING;
1910 mdt_hsm_cdt_wakeup(mdt);
1912 rc = mdt_hsm_cdt_start(mdt);
1914 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1915 if ((cdt->cdt_state == CDT_STOPPING) ||
1916 (cdt->cdt_state == CDT_STOPPED)) {
1917 CERROR("%s: Coordinator already stopped\n",
1921 cdt->cdt_state = CDT_STOPPING;
1923 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1924 if ((cdt->cdt_state == CDT_STOPPING) ||
1925 (cdt->cdt_state == CDT_STOPPED)) {
1926 CERROR("%s: Coordinator is stopped\n",
1930 cdt->cdt_state = CDT_DISABLE;
1932 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1933 rc = hsm_cancel_all_actions(mdt);
1934 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1942 CERROR("%s: Valid coordinator control commands are: "
1943 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1944 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1945 CDT_PURGE_CMD, CDT_HELP_CMD);
1953 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1955 struct obd_device *obd = m->private;
1956 struct coordinator *cdt;
1959 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1961 if (cdt->cdt_state == CDT_INIT)
1962 seq_printf(m, "init\n");
1963 else if (cdt->cdt_state == CDT_RUNNING)
1964 seq_printf(m, "enabled\n");
1965 else if (cdt->cdt_state == CDT_STOPPING)
1966 seq_printf(m, "stopping\n");
1967 else if (cdt->cdt_state == CDT_STOPPED)
1968 seq_printf(m, "stopped\n");
1969 else if (cdt->cdt_state == CDT_DISABLE)
1970 seq_printf(m, "disabled\n");
1972 seq_printf(m, "unknown\n");
1978 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
1984 for (i = 0; i < 8 * sizeof(mask); i++) {
1985 if (mask & (1UL << i)) {
1986 seq_printf(m, "%s%s", first ? "" : " ",
1987 hsm_copytool_action2name(i));
1997 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
1999 struct mdt_device *mdt = m->private;
2000 struct coordinator *cdt = &mdt->mdt_coordinator;
2002 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2006 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2008 struct mdt_device *mdt = m->private;
2009 struct coordinator *cdt = &mdt->mdt_coordinator;
2011 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2015 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2017 struct mdt_device *mdt = m->private;
2018 struct coordinator *cdt = &mdt->mdt_coordinator;
2020 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2023 static inline enum hsm_copytool_action
2024 hsm_copytool_name2action(const char *name)
2026 if (strcasecmp(name, "NOOP") == 0)
2028 else if (strcasecmp(name, "ARCHIVE") == 0)
2029 return HSMA_ARCHIVE;
2030 else if (strcasecmp(name, "RESTORE") == 0)
2031 return HSMA_RESTORE;
2032 else if (strcasecmp(name, "REMOVE") == 0)
2034 else if (strcasecmp(name, "CANCEL") == 0)
2041 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2042 size_t user_count, __u64 *mask)
2044 char *buf, *pos, *name;
2050 if (!(user_count < 4096))
2053 buf_size = user_count + 1;
2055 OBD_ALLOC(buf, buf_size);
2059 if (copy_from_user(buf, user_buf, buf_size - 1))
2060 GOTO(out, rc = -EFAULT);
2062 buf[buf_size - 1] = '\0';
2065 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2071 action = hsm_copytool_name2action(name);
2073 GOTO(out, rc = -EINVAL);
2075 new_mask |= (1UL << action);
2081 OBD_FREE(buf, buf_size);
2087 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2088 size_t count, loff_t *off)
2090 struct seq_file *m = file->private_data;
2091 struct mdt_device *mdt = m->private;
2092 struct coordinator *cdt = &mdt->mdt_coordinator;
2094 return mdt_write_hsm_request_mask(file, buf, count,
2095 &cdt->cdt_user_request_mask);
2099 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2100 size_t count, loff_t *off)
2102 struct seq_file *m = file->private_data;
2103 struct mdt_device *mdt = m->private;
2104 struct coordinator *cdt = &mdt->mdt_coordinator;
2106 return mdt_write_hsm_request_mask(file, buf, count,
2107 &cdt->cdt_group_request_mask);
2111 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2112 size_t count, loff_t *off)
2114 struct seq_file *m = file->private_data;
2115 struct mdt_device *mdt = m->private;
2116 struct coordinator *cdt = &mdt->mdt_coordinator;
2118 return mdt_write_hsm_request_mask(file, buf, count,
2119 &cdt->cdt_other_request_mask);
2122 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2123 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2124 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2125 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2126 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2127 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2128 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2129 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2131 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2133 .fops = &mdt_hsm_agent_fops },
2134 { .name = "actions",
2135 .fops = &mdt_hsm_actions_fops,
2136 .proc_mode = 0444 },
2137 { .name = "default_archive_id",
2138 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2139 { .name = "grace_delay",
2140 .fops = &mdt_hsm_cdt_grace_delay_fops },
2141 { .name = "loop_period",
2142 .fops = &mdt_hsm_cdt_loop_period_fops },
2143 { .name = "max_requests",
2144 .fops = &mdt_hsm_cdt_max_requests_fops },
2146 .fops = &mdt_hsm_policy_fops },
2147 { .name = "active_request_timeout",
2148 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2149 { .name = "active_requests",
2150 .fops = &mdt_hsm_active_requests_fops },
2151 { .name = "user_request_mask",
2152 .fops = &mdt_hsm_user_request_mask_fops, },
2153 { .name = "group_request_mask",
2154 .fops = &mdt_hsm_group_request_mask_fops, },
2155 { .name = "other_request_mask",
2156 .fops = &mdt_hsm_other_request_mask_fops, },