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) {
180 int i, empty_slot, found;
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 list we
189 * have built and if there is room in the request vector */
192 for (i = 0; i < hsd->max_requests &&
193 (empty_slot == -1 || found == -1); i++) {
194 if (hsd->request[i].hal == NULL) {
198 if (hsd->request[i].hal->hal_compound_id ==
199 larr->arr_compound_id) {
204 if (found == -1 && empty_slot == -1)
205 /* unknown request and no more room for new request,
206 * continue scan for to find other entries for
207 * already found request
212 struct hsm_action_list *hal;
214 /* request is not already known */
215 request = &hsd->request[empty_slot];
217 /* allocates hai vector size just needs to be large
220 sizeof(*request->hal) +
221 cfs_size_round(MTI_NAME_MAXLEN+1) +
222 2 * cfs_size_round(larr->arr_hai.hai_len);
223 OBD_ALLOC(hal, request->hal_sz);
225 CERROR("%s: Cannot allocate memory (%d o)"
226 "for compound "LPX64"\n",
229 larr->arr_compound_id);
232 hal->hal_version = HAL_VERSION;
233 strlcpy(hal->hal_fsname, hsd->fs_name,
234 MTI_NAME_MAXLEN + 1);
235 hal->hal_compound_id = larr->arr_compound_id;
236 hal->hal_archive_id = larr->arr_archive_id;
237 hal->hal_flags = larr->arr_flags;
239 request->hal_used_sz = hal_size(hal);
243 hai = hai_first(hal);
245 /* request is known */
246 request = &hsd->request[found];
248 /* we check if record archive num is the same as the
249 * known request, if not we will serve it in multiple
250 * time because we do not know if the agent can serve
252 * a use case is a compound made of multiple restore
253 * where the files are not archived in the same backend
255 if (larr->arr_archive_id !=
256 request->hal->hal_archive_id)
259 if (request->hal_sz <
260 request->hal_used_sz +
261 cfs_size_round(larr->arr_hai.hai_len)) {
262 /* Not enough room, need an extension */
266 sz = 2 * request->hal_sz;
267 OBD_ALLOC(hal_buffer, sz);
269 CERROR("%s: Cannot allocate memory "
270 "(%d o) for compound "LPX64"\n",
271 mdt_obd_name(mdt), sz,
272 larr->arr_compound_id);
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++;
295 struct hsm_progress_kernel pgs;
296 struct cdt_agent_req *car;
297 cfs_time_t now = cfs_time_current_sec();
300 /* we search for a running request
301 * error may happen if coordinator crashes or stopped
302 * with running request
304 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
306 last = larr->arr_req_create;
308 last = car->car_req_update;
309 mdt_cdt_put_request(car);
312 /* test if request too long, if yes cancel it
313 * the same way the copy tool acknowledge a cancel request */
314 if (now <= last + cdt->cdt_active_req_timeout)
317 dump_llog_agent_req_rec("request timed out, start cleaning",
319 /* a too old cancel request just needs to be removed
320 * this can happen, if copy tool does not support
321 * cancel for other requests, we have to remove the
322 * running request and notify the copytool */
323 pgs.hpk_fid = larr->arr_hai.hai_fid;
324 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
325 pgs.hpk_extent = larr->arr_hai.hai_extent;
326 pgs.hpk_flags = HP_FLAG_COMPLETED;
327 pgs.hpk_errval = ENOSYS;
328 pgs.hpk_data_version = 0;
330 /* update request state, but do not record in llog, to
331 * avoid deadlock on cdt_llog_lock */
332 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
334 CERROR("%s: cannot cleanup timed out request: "
335 DFID" for cookie "LPX64" action=%s\n",
337 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
338 hsm_copytool_action2name(
339 larr->arr_hai.hai_action));
342 /* The request no longer exists, forget
343 * about it, and do not send a cancel request
344 * to the client, for which an error will be
345 * sent back, leading to an endless cycle of
347 RETURN(LLOG_DEL_RECORD);
350 /* XXX A cancel request cannot be cancelled. */
351 if (larr->arr_hai.hai_action == HSMA_CANCEL)
354 larr->arr_status = ARS_CANCELED;
355 larr->arr_req_change = now;
356 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
358 CERROR("%s: cannot update agent log: rc = %d\n",
359 mdt_obd_name(mdt), rc);
365 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
366 cfs_time_current_sec())
367 RETURN(LLOG_DEL_RECORD);
374 * create /proc entries for coordinator
377 * \retval -ve failure
379 int hsm_cdt_procfs_init(struct mdt_device *mdt)
381 struct coordinator *cdt = &mdt->mdt_coordinator;
385 /* init /proc entries, failure is not critical */
386 cdt->cdt_proc_dir = lprocfs_register("hsm",
387 mdt2obd_dev(mdt)->obd_proc_entry,
388 lprocfs_mdt_hsm_vars, mdt);
389 if (IS_ERR(cdt->cdt_proc_dir)) {
390 rc = PTR_ERR(cdt->cdt_proc_dir);
391 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
392 " rc=%d\n", mdt_obd_name(mdt), rc);
393 cdt->cdt_proc_dir = NULL;
401 * remove /proc entries for coordinator
404 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
406 struct coordinator *cdt = &mdt->mdt_coordinator;
408 LASSERT(cdt->cdt_state == CDT_STOPPED);
409 if (cdt->cdt_proc_dir != NULL)
410 lprocfs_remove(&cdt->cdt_proc_dir);
414 * get vector of hsm cdt /proc vars
418 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
420 return lprocfs_mdt_hsm_vars;
425 * \param data [IN] obd device
427 * \retval -ve failure
429 static int mdt_coordinator(void *data)
431 struct mdt_thread_info *mti = data;
432 struct mdt_device *mdt = mti->mti_mdt;
433 struct coordinator *cdt = &mdt->mdt_coordinator;
434 struct hsm_scan_data hsd = { NULL };
439 cdt->cdt_thread.t_flags = SVC_RUNNING;
440 wake_up(&cdt->cdt_thread.t_ctl_waitq);
442 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
443 mdt_obd_name(mdt), current_pid());
445 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
446 * increases due to a change from /proc we do not overflow the
447 * hsd.request[] vector
449 hsd.max_requests = cdt->cdt_max_requests;
450 request_sz = hsd.max_requests * sizeof(*hsd.request);
451 OBD_ALLOC(hsd.request, request_sz);
453 GOTO(out, rc = -ENOMEM);
456 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
459 struct l_wait_info lwi;
462 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
464 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
465 (cdt->cdt_thread.t_flags &
466 (SVC_STOPPING|SVC_EVENT)),
469 CDEBUG(D_HSM, "coordinator resumes\n");
471 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
472 cdt->cdt_state == CDT_STOPPING) {
473 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
478 /* wake up before timeout, new work arrives */
479 if (cdt->cdt_thread.t_flags & SVC_EVENT)
480 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
482 /* if coordinator is suspended continue to wait */
483 if (cdt->cdt_state == CDT_DISABLE) {
484 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
488 CDEBUG(D_HSM, "coordinator starts reading llog\n");
490 if (hsd.max_requests != cdt->cdt_max_requests) {
491 /* cdt_max_requests has changed,
492 * we need to allocate a new buffer
494 OBD_FREE(hsd.request, request_sz);
495 hsd.max_requests = cdt->cdt_max_requests;
496 request_sz = hsd.max_requests * sizeof(*hsd.request);
497 OBD_ALLOC(hsd.request, request_sz);
506 rc = cdt_llog_process(mti->mti_env, mdt,
507 mdt_coordinator_cb, &hsd);
511 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
513 if (list_empty(&cdt->cdt_agents)) {
514 CDEBUG(D_HSM, "no agent available, "
515 "coordinator sleeps\n");
519 /* here hsd contains a list of requests to be started */
520 for (i = 0; i < hsd.max_requests; i++) {
521 struct hsm_scan_request *request = &hsd.request[i];
522 struct hsm_action_list *hal = request->hal;
523 struct hsm_action_item *hai;
526 enum agent_req_status status;
528 /* still room for work ? */
529 if (atomic_read(&cdt->cdt_request_count) >=
530 cdt->cdt_max_requests)
536 rc = mdt_hsm_agent_send(mti, hal, 0);
537 /* if failure, we suppose it is temporary
538 * if the copy tool failed to do the request
539 * it has to use hsm_progress
541 status = (rc ? ARS_WAITING : ARS_STARTED);
543 /* set up cookie vector to set records status
544 * after copy tools start or failed
546 sz = hal->hal_count * sizeof(__u64);
547 OBD_ALLOC(cookies, sz);
548 if (cookies == NULL) {
549 CERROR("%s: Cannot allocate memory (%d o) "
550 "for cookies vector "LPX64"\n",
551 mdt_obd_name(mdt), sz,
552 hal->hal_compound_id);
555 hai = hai_first(hal);
556 for (j = 0; j < hal->hal_count; j++) {
557 cookies[j] = hai->hai_cookie;
561 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
562 hal->hal_count, status);
564 CERROR("%s: mdt_agent_record_update() failed, "
565 "rc=%d, cannot update status to %s "
567 mdt_obd_name(mdt), rc,
568 agent_req_status2name(status),
571 OBD_FREE(cookies, sz);
574 /* free hal allocated by callback */
575 for (i = 0; i < hsd.max_requests; i++) {
576 struct hsm_scan_request *request = &hsd.request[i];
579 OBD_FREE(request->hal, request->hal_sz);
585 LASSERT(hsd.request_cnt == 0);
587 /* reset callback data */
588 memset(hsd.request, 0, request_sz);
593 OBD_FREE(hsd.request, request_sz);
595 if (cdt->cdt_state == CDT_STOPPING) {
596 /* request comes from /proc path, so we need to clean cdt
598 mdt_hsm_cdt_stop(mdt);
599 mdt->mdt_opts.mo_coordinator = 0;
601 /* request comes from a thread event, generated
602 * by mdt_stop_coordinator(), we have to ack
603 * and cdt cleaning will be done by event sender
605 cdt->cdt_thread.t_flags = SVC_STOPPED;
606 wake_up(&cdt->cdt_thread.t_ctl_waitq);
610 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
611 mdt_obd_name(mdt), current_pid(), rc);
613 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
615 mdt_obd_name(mdt), current_pid());
621 * lookup a restore handle by FID
622 * caller needs to hold cdt_restore_lock
623 * \param cdt [IN] coordinator
624 * \param fid [IN] FID
625 * \retval cdt_restore_handle found
626 * \retval NULL not found
628 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
629 const struct lu_fid *fid)
631 struct cdt_restore_handle *crh;
634 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
635 if (lu_fid_eq(&crh->crh_fid, fid))
642 * data passed to llog_cat_process() callback
643 * to scan requests and take actions
645 struct hsm_restore_data {
646 struct mdt_thread_info *hrd_mti;
650 * llog_cat_process() callback, used to:
651 * - find restore request and allocate the restore handle
652 * \param env [IN] environment
653 * \param llh [IN] llog handle
654 * \param hdr [IN] llog record
655 * \param data [IN/OUT] cb data = struct hsm_restore_data
657 * \retval -ve failure
659 static int hsm_restore_cb(const struct lu_env *env,
660 struct llog_handle *llh,
661 struct llog_rec_hdr *hdr, void *data)
663 struct llog_agent_req_rec *larr;
664 struct hsm_restore_data *hrd;
665 struct cdt_restore_handle *crh;
666 struct hsm_action_item *hai;
667 struct mdt_thread_info *mti;
668 struct coordinator *cdt;
669 struct mdt_object *child;
675 cdt = &mti->mti_mdt->mdt_coordinator;
677 larr = (struct llog_agent_req_rec *)hdr;
678 hai = &larr->arr_hai;
679 if (hai->hai_cookie > cdt->cdt_last_cookie)
680 /* update the cookie to avoid collision */
681 cdt->cdt_last_cookie = hai->hai_cookie + 1;
683 if (hai->hai_action != HSMA_RESTORE ||
684 agent_req_in_final_state(larr->arr_status))
687 /* restore request not in a final state */
689 /* force replay of restore requests left in started state from previous
690 * CDT context, to be canceled later if finally found to be incompatible
691 * when being re-started */
692 if (larr->arr_status == ARS_STARTED) {
693 larr->arr_status = ARS_WAITING;
694 larr->arr_req_change = cfs_time_current_sec();
695 rc = llog_write(env, llh, hdr, hdr->lrh_index);
700 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
704 crh->crh_fid = hai->hai_fid;
705 /* in V1 all file is restored
706 crh->extent.start = hai->hai_extent.offset;
707 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
709 crh->crh_extent.start = 0;
710 crh->crh_extent.end = hai->hai_extent.length;
711 /* get the layout lock */
712 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
713 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
714 MDS_INODELOCK_LAYOUT);
716 GOTO(out, rc = PTR_ERR(child));
719 /* we choose to not keep a reference
720 * on the object during the restore time which can be very long */
721 mdt_object_put(mti->mti_env, child);
723 mutex_lock(&cdt->cdt_restore_lock);
724 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
725 mutex_unlock(&cdt->cdt_restore_lock);
732 * restore coordinator state at startup
733 * the goal is to take a layout lock for each registered restore request
734 * \param mti [IN] context
736 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
738 struct hsm_restore_data hrd;
744 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
745 hsm_restore_cb, &hrd);
750 static int hsm_init_ucred(struct lu_ucred *uc)
754 uc->uc_valid = UCRED_OLD;
763 uc->uc_suppgids[0] = -1;
764 uc->uc_suppgids[1] = -1;
765 uc->uc_cap = CFS_CAP_FS_MASK;
768 uc->uc_identity = NULL;
774 * wake up coordinator thread
775 * \param mdt [IN] device
777 * \retval -ve failure
779 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
781 struct coordinator *cdt = &mdt->mdt_coordinator;
784 if (cdt->cdt_state == CDT_STOPPED)
787 /* wake up coordinator */
788 cdt->cdt_thread.t_flags = SVC_EVENT;
789 wake_up(&cdt->cdt_thread.t_ctl_waitq);
795 * initialize coordinator struct
796 * \param mdt [IN] device
798 * \retval -ve failure
800 int mdt_hsm_cdt_init(struct mdt_device *mdt)
802 struct coordinator *cdt = &mdt->mdt_coordinator;
803 struct mdt_thread_info *cdt_mti = NULL;
807 cdt->cdt_state = CDT_STOPPED;
809 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
810 mutex_init(&cdt->cdt_llog_lock);
811 init_rwsem(&cdt->cdt_agent_lock);
812 init_rwsem(&cdt->cdt_request_lock);
813 mutex_init(&cdt->cdt_restore_lock);
815 INIT_LIST_HEAD(&cdt->cdt_requests);
816 INIT_LIST_HEAD(&cdt->cdt_agents);
817 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
819 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
823 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
824 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
826 lu_context_enter(&cdt->cdt_session);
827 cdt->cdt_env.le_ses = &cdt->cdt_session;
829 lu_env_fini(&cdt->cdt_env);
833 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
834 LASSERT(cdt_mti != NULL);
836 cdt_mti->mti_env = &cdt->cdt_env;
837 cdt_mti->mti_mdt = mdt;
839 hsm_init_ucred(mdt_ucred(cdt_mti));
841 /* default values for /proc tunnables
842 * can be override by MGS conf */
843 cdt->cdt_default_archive_id = 1;
844 cdt->cdt_grace_delay = 60;
845 cdt->cdt_loop_period = 10;
846 cdt->cdt_max_requests = 3;
847 cdt->cdt_policy = CDT_DEFAULT_POLICY;
848 cdt->cdt_active_req_timeout = 3600;
854 * free a coordinator thread
855 * \param mdt [IN] device
857 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
859 struct coordinator *cdt = &mdt->mdt_coordinator;
862 lu_context_exit(cdt->cdt_env.le_ses);
863 lu_context_fini(cdt->cdt_env.le_ses);
865 lu_env_fini(&cdt->cdt_env);
871 * start a coordinator thread
872 * \param mdt [IN] device
874 * \retval -ve failure
876 int mdt_hsm_cdt_start(struct mdt_device *mdt)
878 struct coordinator *cdt = &mdt->mdt_coordinator;
881 struct mdt_thread_info *cdt_mti;
882 struct task_struct *task;
885 /* functions defined but not yet used
886 * this avoid compilation warning
890 if (cdt->cdt_state != CDT_STOPPED) {
891 CERROR("%s: Coordinator already started\n",
896 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
897 cdt->cdt_policy = CDT_DEFAULT_POLICY;
899 cdt->cdt_state = CDT_INIT;
901 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
902 /* just need to be larger than previous one */
903 /* cdt_last_cookie is protected by cdt_llog_lock */
904 cdt->cdt_last_cookie = cfs_time_current_sec();
905 atomic_set(&cdt->cdt_request_count, 0);
906 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
907 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
908 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
910 /* to avoid deadlock when start is made through /proc
911 * /proc entries are created by the coordinator thread */
913 /* set up list of started restore requests */
914 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
915 rc = mdt_hsm_pending_restore(cdt_mti);
917 CERROR("%s: cannot take the layout locks needed"
918 " for registered restore: %d\n",
919 mdt_obd_name(mdt), rc);
921 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
924 cdt->cdt_state = CDT_STOPPED;
925 CERROR("%s: error starting coordinator thread: %d\n",
926 mdt_obd_name(mdt), rc);
929 CDEBUG(D_HSM, "%s: coordinator thread started\n",
934 wait_event(cdt->cdt_thread.t_ctl_waitq,
935 (cdt->cdt_thread.t_flags & SVC_RUNNING));
937 cdt->cdt_state = CDT_RUNNING;
938 mdt->mdt_opts.mo_coordinator = 1;
943 * stop a coordinator thread
944 * \param mdt [IN] device
946 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
948 struct coordinator *cdt = &mdt->mdt_coordinator;
949 struct cdt_agent_req *car, *tmp1;
950 struct hsm_agent *ha, *tmp2;
951 struct cdt_restore_handle *crh, *tmp3;
952 struct mdt_thread_info *cdt_mti;
955 if (cdt->cdt_state == CDT_STOPPED) {
956 CERROR("%s: Coordinator already stopped\n",
961 if (cdt->cdt_state != CDT_STOPPING) {
962 /* stop coordinator thread before cleaning */
963 cdt->cdt_thread.t_flags = SVC_STOPPING;
964 wake_up(&cdt->cdt_thread.t_ctl_waitq);
965 wait_event(cdt->cdt_thread.t_ctl_waitq,
966 cdt->cdt_thread.t_flags & SVC_STOPPED);
968 cdt->cdt_state = CDT_STOPPED;
971 down_write(&cdt->cdt_request_lock);
972 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
974 list_del(&car->car_request_list);
975 mdt_cdt_free_request(car);
977 up_write(&cdt->cdt_request_lock);
979 down_write(&cdt->cdt_agent_lock);
980 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
981 list_del(&ha->ha_list);
984 up_write(&cdt->cdt_agent_lock);
986 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
987 mutex_lock(&cdt->cdt_restore_lock);
988 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
989 struct mdt_object *child;
991 /* give back layout lock */
992 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
994 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
996 list_del(&crh->crh_list);
998 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1000 mutex_unlock(&cdt->cdt_restore_lock);
1002 mdt->mdt_opts.mo_coordinator = 0;
1008 * register all requests from an hal in the memory list
1009 * \param mti [IN] context
1010 * \param hal [IN] request
1011 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1012 * which is running the CT
1014 * \retval -ve failure
1016 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1017 struct hsm_action_list *hal, struct obd_uuid *uuid)
1019 struct mdt_device *mdt = mti->mti_mdt;
1020 struct coordinator *cdt = &mdt->mdt_coordinator;
1021 struct hsm_action_item *hai;
1025 /* register request in memory list */
1026 hai = hai_first(hal);
1027 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1028 struct cdt_agent_req *car;
1030 /* in case of a cancel request, we first mark the ondisk
1031 * record of the request we want to stop as canceled
1032 * this does not change the cancel record
1033 * it will be done when updating the request status
1035 if (hai->hai_action == HSMA_CANCEL) {
1036 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1040 CERROR("%s: mdt_agent_record_update() failed, "
1041 "rc=%d, cannot update status to %s "
1042 "for cookie "LPX64"\n",
1043 mdt_obd_name(mdt), rc,
1044 agent_req_status2name(ARS_CANCELED),
1049 /* find the running request to set it canceled */
1050 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1052 car->car_canceled = 1;
1053 /* uuid has to be changed to the one running the
1054 * request to cancel */
1055 *uuid = car->car_uuid;
1056 mdt_cdt_put_request(car);
1058 /* no need to memorize cancel request
1059 * this also avoid a deadlock when we receive
1060 * a purge all requests command
1065 if (hai->hai_action == HSMA_ARCHIVE) {
1066 struct mdt_object *obj;
1069 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1070 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1073 GOTO(out, rc = PTR_ERR(obj));
1075 hsm.mh_flags |= HS_EXISTS;
1076 hsm.mh_arch_id = hal->hal_archive_id;
1077 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1078 mdt_object_put(mti->mti_env, obj);
1083 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1084 hal->hal_archive_id, hal->hal_flags,
1087 GOTO(out, rc = PTR_ERR(car));
1089 rc = mdt_cdt_add_request(cdt, car);
1091 mdt_cdt_free_request(car);
1098 * swap layouts between 2 fids
1099 * \param mti [IN] context
1102 * \param mh_common [IN] MD HSM
1104 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1105 const lustre_fid *fid, const lustre_fid *dfid,
1106 struct md_hsm *mh_common)
1108 struct mdt_device *mdt = mti->mti_mdt;
1109 struct mdt_object *child1, *child2;
1110 struct mdt_lock_handle *lh2;
1114 child1 = mdt_object_find(mti->mti_env, mdt, fid);
1116 GOTO(out, rc = PTR_ERR(child1));
1118 /* we already have layout lock on FID so take only
1120 lh2 = &mti->mti_lh[MDT_LH_OLD];
1121 mdt_lock_reg_init(lh2, LCK_EX);
1122 child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1124 GOTO(out_child1, rc = PTR_ERR(child2));
1126 /* if copy tool closes the volatile before sending the final
1127 * progress through llapi_hsm_copy_end(), all the objects
1128 * are removed and mdd_swap_layout LBUG */
1129 if (!mdt_object_exists(child2)) {
1130 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1131 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1132 GOTO(out_child2, rc = -ENOENT);
1134 /* Since we only handle restores here, unconditionally use
1135 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1136 * be preserved in case of failure during swap_layout and not
1137 * leave a file in an intermediate but incoherent state.
1138 * But need to setup HSM xattr of data FID before, reuse
1139 * mti and mh presets for FID in hsm_cdt_request_completed(),
1140 * only need to clear RELEASED and DIRTY.
1142 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1143 rc = mdt_hsm_attr_set(mti, child2, mh_common);
1145 rc = mo_swap_layouts(mti->mti_env,
1146 mdt_object_child(child1),
1147 mdt_object_child(child2),
1148 SWAP_LAYOUTS_MDS_HSM);
1151 mdt_object_unlock_put(mti, child2, lh2, 1);
1153 mdt_object_put(mti->mti_env, child1);
1159 * update status of a completed request
1160 * \param mti [IN] context
1161 * \param pgs [IN] progress of the copy tool
1162 * \param update_record [IN] update llog record
1164 * \retval -ve failure
1166 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1167 struct hsm_progress_kernel *pgs,
1168 const struct cdt_agent_req *car,
1169 enum agent_req_status *status)
1171 const struct lu_env *env = mti->mti_env;
1172 struct mdt_device *mdt = mti->mti_mdt;
1173 struct coordinator *cdt = &mdt->mdt_coordinator;
1174 struct mdt_object *obj = NULL;
1175 int cl_flags = 0, rc = 0;
1180 /* default is to retry */
1181 *status = ARS_WAITING;
1183 /* find object by FID */
1184 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1185 /* we will update MD HSM only if needed */
1186 is_mh_changed = false;
1188 /* object removed */
1189 *status = ARS_SUCCEED;
1193 /* no need to change mh->mh_arch_id
1194 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1196 if (pgs->hpk_errval != 0) {
1197 switch (pgs->hpk_errval) {
1199 /* the copy tool does not support cancel
1200 * so the cancel request is failed
1201 * As we cannot distinguish a cancel progress
1202 * from another action progress (they have the
1203 * same cookie), we suppose here the CT returns
1204 * ENOSYS only if does not support cancel
1206 /* this can also happen when cdt calls it to
1207 * for a timed out request */
1208 *status = ARS_FAILED;
1209 /* to have a cancel event in changelog */
1210 pgs->hpk_errval = ECANCELED;
1213 /* the request record has already been set to
1214 * ARS_CANCELED, this set the cancel request
1216 *status = ARS_SUCCEED;
1219 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1220 !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1221 ARS_FAILED : ARS_WAITING);
1225 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1226 CERROR("%s: Request "LPX64" on "DFID
1227 " failed, error code %d too large\n",
1229 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1231 hsm_set_cl_error(&cl_flags,
1232 CLF_HSM_ERROVERFLOW);
1235 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1238 switch (car->car_hai->hai_action) {
1240 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1243 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1246 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1249 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1250 CERROR("%s: Failed request "LPX64" on "DFID
1251 " cannot be a CANCEL\n",
1254 PFID(&pgs->hpk_fid));
1257 CERROR("%s: Failed request "LPX64" on "DFID
1258 " %d is an unknown action\n",
1260 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1261 car->car_hai->hai_action);
1266 *status = ARS_SUCCEED;
1267 switch (car->car_hai->hai_action) {
1269 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1270 /* set ARCHIVE keep EXIST and clear LOST and
1272 mh.mh_arch_ver = pgs->hpk_data_version;
1273 mh.mh_flags |= HS_ARCHIVED;
1274 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1275 is_mh_changed = true;
1278 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1280 /* do not clear RELEASED and DIRTY here
1281 * this will occur in hsm_swap_layouts()
1284 /* Restoring has changed the file version on
1286 mh.mh_arch_ver = pgs->hpk_data_version;
1287 is_mh_changed = true;
1290 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1291 /* clear ARCHIVED EXISTS and LOST */
1292 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1293 is_mh_changed = true;
1296 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1297 CERROR("%s: Successful request "LPX64
1299 " cannot be a CANCEL\n",
1302 PFID(&pgs->hpk_fid));
1305 CERROR("%s: Successful request "LPX64
1307 " %d is an unknown action\n",
1309 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1310 car->car_hai->hai_action);
1316 /* rc != 0 means error when analysing action, it may come from
1317 * a crasy CT no need to manage DIRTY
1320 hsm_set_cl_flags(&cl_flags,
1321 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1323 /* unlock is done later, after layout lock management */
1325 rc = mdt_hsm_attr_set(mti, obj, &mh);
1328 /* we give back layout lock only if restore was successful or
1329 * if restore was canceled or if policy is to not retry
1330 * in other cases we just unlock the object */
1331 if (car->car_hai->hai_action == HSMA_RESTORE &&
1332 (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1333 cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1334 struct cdt_restore_handle *crh;
1336 /* restore in data FID done, we swap the layouts
1337 * only if restore is successful */
1338 if (pgs->hpk_errval == 0) {
1339 rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1340 &car->car_hai->hai_dfid, &mh);
1342 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1343 *status = ARS_FAILED;
1344 pgs->hpk_errval = -rc;
1347 /* we have to retry, so keep layout lock */
1348 if (*status == ARS_WAITING)
1351 /* give back layout lock */
1352 mutex_lock(&cdt->cdt_restore_lock);
1353 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1355 list_del(&crh->crh_list);
1356 mutex_unlock(&cdt->cdt_restore_lock);
1357 /* just give back layout lock, we keep
1358 * the reference which is given back
1359 * later with the lock for HSM flags */
1360 if (!IS_ERR(obj) && crh != NULL)
1361 mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1364 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1370 if (obj != NULL && !IS_ERR(obj)) {
1371 mo_changelog(env, CL_HSM, cl_flags,
1372 mdt_object_child(obj));
1373 mdt_object_put(mti->mti_env, obj);
1380 * update status of a request
1381 * \param mti [IN] context
1382 * \param pgs [IN] progress of the copy tool
1383 * \param update_record [IN] update llog record
1385 * \retval -ve failure
1387 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1388 struct hsm_progress_kernel *pgs,
1389 const int update_record)
1391 struct mdt_device *mdt = mti->mti_mdt;
1392 struct coordinator *cdt = &mdt->mdt_coordinator;
1393 struct cdt_agent_req *car;
1397 /* no coordinator started, so we cannot serve requests */
1398 if (cdt->cdt_state == CDT_STOPPED)
1401 /* first do sanity checks */
1402 car = mdt_cdt_update_request(cdt, pgs);
1404 CERROR("%s: Cannot find running request for cookie "LPX64
1407 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1409 RETURN(PTR_ERR(car));
1412 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1413 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1414 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1415 hsm_copytool_action2name(car->car_hai->hai_action),
1416 pgs->hpk_flags, pgs->hpk_errval,
1417 PFID(&car->car_hai->hai_fid),
1418 PFID(&car->car_hai->hai_dfid));
1420 /* progress is done on FID or data FID depending of the action and
1421 * of the copy progress */
1422 /* for restore progress is used to send back the data FID to cdt */
1423 if (car->car_hai->hai_action == HSMA_RESTORE &&
1424 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1425 car->car_hai->hai_dfid = pgs->hpk_fid;
1427 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1428 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1429 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1430 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1431 CERROR("%s: Progress on "DFID" for cookie "LPX64
1432 " does not match request FID "DFID" nor data FID "
1435 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1436 PFID(&car->car_hai->hai_fid),
1437 PFID(&car->car_hai->hai_dfid));
1438 GOTO(out, rc = -EINVAL);
1441 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1442 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1443 " is not coherent (err=%d and not completed"
1446 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1447 hsm_copytool_action2name(car->car_hai->hai_action),
1448 pgs->hpk_errval, pgs->hpk_flags);
1449 GOTO(out, rc = -EINVAL);
1452 /* now progress is valid */
1454 /* we use a root like ucred */
1455 hsm_init_ucred(mdt_ucred(mti));
1457 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1458 enum agent_req_status status;
1460 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1462 /* remove request from memory list */
1463 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1465 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1466 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1468 hsm_copytool_action2name(car->car_hai->hai_action),
1469 agent_req_status2name(status));
1471 if (update_record) {
1474 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1475 &pgs->hpk_cookie, 1,
1478 CERROR("%s: mdt_agent_record_update() failed,"
1479 " rc=%d, cannot update status to %s"
1480 " for cookie "LPX64"\n",
1481 mdt_obd_name(mdt), rc1,
1482 agent_req_status2name(status),
1484 rc = (rc != 0 ? rc : rc1);
1486 /* ct has completed a request, so a slot is available, wakeup
1487 * cdt to find new work */
1488 mdt_hsm_cdt_wakeup(mdt);
1490 /* if copytool send a progress on a canceled request
1491 * we inform copytool it should stop
1493 if (car->car_canceled == 1)
1499 /* remove ref got from mdt_cdt_update_request() */
1500 mdt_cdt_put_request(car);
1507 * data passed to llog_cat_process() callback
1508 * to cancel requests
1510 struct hsm_cancel_all_data {
1511 struct mdt_device *mdt;
1515 * llog_cat_process() callback, used to:
1516 * - purge all requests
1517 * \param env [IN] environment
1518 * \param llh [IN] llog handle
1519 * \param hdr [IN] llog record
1520 * \param data [IN] cb data = struct hsm_cancel_all_data
1522 * \retval -ve failure
1524 static int mdt_cancel_all_cb(const struct lu_env *env,
1525 struct llog_handle *llh,
1526 struct llog_rec_hdr *hdr, void *data)
1528 struct llog_agent_req_rec *larr;
1529 struct hsm_cancel_all_data *hcad;
1533 larr = (struct llog_agent_req_rec *)hdr;
1535 if (larr->arr_status == ARS_WAITING ||
1536 larr->arr_status == ARS_STARTED) {
1537 larr->arr_status = ARS_CANCELED;
1538 larr->arr_req_change = cfs_time_current_sec();
1539 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1546 * cancel all actions
1547 * \param obd [IN] MDT device
1549 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1551 struct mdt_thread_info *mti;
1552 struct coordinator *cdt = &mdt->mdt_coordinator;
1553 struct cdt_agent_req *car;
1554 struct hsm_action_list *hal = NULL;
1555 struct hsm_action_item *hai;
1556 struct hsm_cancel_all_data hcad;
1557 int hal_sz = 0, hal_len, rc;
1558 enum cdt_states save_state;
1561 /* retrieve coordinator context */
1562 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1564 /* disable coordinator */
1565 save_state = cdt->cdt_state;
1566 cdt->cdt_state = CDT_DISABLE;
1568 /* send cancel to all running requests */
1569 down_read(&cdt->cdt_request_lock);
1570 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1571 mdt_cdt_get_request(car);
1572 /* request is not yet removed from list, it will be done
1573 * when copytool will return progress
1576 if (car->car_hai->hai_action == HSMA_CANCEL) {
1577 mdt_cdt_put_request(car);
1582 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1583 cfs_size_round(car->car_hai->hai_len);
1585 if (hal_len > hal_sz && hal_sz > 0) {
1586 /* not enough room, free old buffer */
1587 OBD_FREE(hal, hal_sz);
1591 /* empty buffer, allocate one */
1594 OBD_ALLOC(hal, hal_sz);
1596 mdt_cdt_put_request(car);
1597 up_read(&cdt->cdt_request_lock);
1598 GOTO(out, rc = -ENOMEM);
1602 hal->hal_version = HAL_VERSION;
1603 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1605 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1606 hal->hal_compound_id = car->car_compound_id;
1607 hal->hal_archive_id = car->car_archive_id;
1608 hal->hal_flags = car->car_flags;
1611 hai = hai_first(hal);
1612 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1613 hai->hai_action = HSMA_CANCEL;
1616 /* it is possible to safely call mdt_hsm_agent_send()
1617 * (ie without a deadlock on cdt_request_lock), because the
1618 * write lock is taken only if we are not in purge mode
1619 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1620 * nor mdt_cdt_remove_request())
1622 /* no conflict with cdt thread because cdt is disable and we
1623 * have the request lock */
1624 mdt_hsm_agent_send(mti, hal, 1);
1626 mdt_cdt_put_request(car);
1628 up_read(&cdt->cdt_request_lock);
1631 OBD_FREE(hal, hal_sz);
1633 /* cancel all on-disk records */
1636 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1637 mdt_cancel_all_cb, &hcad);
1639 /* enable coordinator */
1640 cdt->cdt_state = save_state;
1646 * check if a request is compatible with file status
1647 * \param hai [IN] request description
1648 * \param hal_an [IN] request archive number (not used)
1649 * \param rq_flags [IN] request flags
1650 * \param hsm [IN] file HSM metadata
1653 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1654 const int hal_an, const __u64 rq_flags,
1655 const struct md_hsm *hsm)
1657 int is_compat = false;
1661 hsm_flags = hsm->mh_flags;
1662 switch (hai->hai_action) {
1664 if (!(hsm_flags & HS_NOARCHIVE) &&
1665 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1669 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1670 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1674 if (!(hsm_flags & HS_RELEASED) &&
1675 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1682 CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1683 " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1684 PFID(&hai->hai_fid),
1685 hsm_copytool_action2name(hai->hai_action), rq_flags,
1686 hai->hai_extent.offset, hai->hai_extent.length,
1688 (is_compat ? "compatible" : "uncompatible"));
1694 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1696 static const struct {
1700 } hsm_policy_names[] = {
1701 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1702 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1707 * convert a policy name to a bit
1708 * \param name [IN] policy name
1710 * \retval policy bit
1712 static __u64 hsm_policy_str2bit(const char *name)
1716 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1717 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1718 strcmp(hsm_policy_names[i].name, name) == 0)
1719 return hsm_policy_names[i].bit;
1724 * convert a policy bit field to a string
1725 * \param mask [IN] policy bit field
1726 * \param hexa [IN] print mask before bit names
1727 * \param buffer [OUT] string
1728 * \param count [IN] size of buffer
1730 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1738 seq_printf(m, "("LPX64") ", mask);
1740 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1743 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1744 if (hsm_policy_names[j].bit == bit)
1748 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1750 seq_printf(m, "%s ", hsm_policy_names[j].name);
1752 /* remove last ' ' */
1757 /* methods to read/write HSM policy flags */
1758 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1760 struct mdt_device *mdt = m->private;
1761 struct coordinator *cdt = &mdt->mdt_coordinator;
1764 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1769 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1770 size_t count, loff_t *off)
1772 struct seq_file *m = file->private_data;
1773 struct mdt_device *mdt = m->private;
1774 struct coordinator *cdt = &mdt->mdt_coordinator;
1775 char *start, *token, sign;
1778 __u64 add_mask, remove_mask, set_mask;
1782 if (count + 1 > PAGE_SIZE)
1785 OBD_ALLOC(buf, count + 1);
1789 if (copy_from_user(buf, buffer, count))
1790 GOTO(out, rc = -EFAULT);
1795 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1798 add_mask = remove_mask = set_mask = 0;
1800 token = strsep(&start, "\n ");
1806 if (sign == '-' || sign == '+')
1809 policy = hsm_policy_str2bit(token);
1811 CWARN("%s: '%s' is unknown, "
1812 "supported policies are:\n", mdt_obd_name(mdt),
1814 hsm_policy_bit2str(m, 0, false);
1815 GOTO(out, rc = -EINVAL);
1819 remove_mask |= policy;
1829 } while (start != NULL);
1831 CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1832 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1834 /* if no sign in all string, it is a clear and set
1835 * if some sign found, all unsigned are converted
1837 * P1 P2 = set to P1 and P2
1838 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1840 if (remove_mask == 0 && add_mask == 0) {
1841 cdt->cdt_policy = set_mask;
1843 cdt->cdt_policy |= set_mask | add_mask;
1844 cdt->cdt_policy &= ~remove_mask;
1847 GOTO(out, rc = count);
1850 OBD_FREE(buf, count + 1);
1853 LPROC_SEQ_FOPS(mdt_hsm_policy);
1855 #define GENERATE_PROC_METHOD(VAR) \
1856 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1858 struct mdt_device *mdt = m->private; \
1859 struct coordinator *cdt = &mdt->mdt_coordinator; \
1862 seq_printf(m, LPU64"\n", (__u64)cdt->VAR); \
1866 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1867 size_t count, loff_t *off) \
1870 struct seq_file *m = file->private_data; \
1871 struct mdt_device *mdt = m->private; \
1872 struct coordinator *cdt = &mdt->mdt_coordinator; \
1877 rc = lprocfs_str_to_s64(buffer, count, &val); \
1880 if (val > 0 && val < INT_MAX) { \
1887 GENERATE_PROC_METHOD(cdt_loop_period)
1888 GENERATE_PROC_METHOD(cdt_grace_delay)
1889 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1890 GENERATE_PROC_METHOD(cdt_max_requests)
1891 GENERATE_PROC_METHOD(cdt_default_archive_id)
1894 * procfs write method for MDT/hsm_control
1895 * proc entry is in mdt directory so data is mdt obd_device pointer
1897 #define CDT_ENABLE_CMD "enabled"
1898 #define CDT_STOP_CMD "shutdown"
1899 #define CDT_DISABLE_CMD "disabled"
1900 #define CDT_PURGE_CMD "purge"
1901 #define CDT_HELP_CMD "help"
1902 #define CDT_MAX_CMD_LEN 10
1905 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1906 size_t count, loff_t *off)
1908 struct seq_file *m = file->private_data;
1909 struct obd_device *obd = m->private;
1910 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1911 struct coordinator *cdt = &(mdt->mdt_coordinator);
1913 char kernbuf[CDT_MAX_CMD_LEN];
1916 if (count == 0 || count >= sizeof(kernbuf))
1919 if (copy_from_user(kernbuf, buffer, count))
1923 if (kernbuf[count - 1] == '\n')
1924 kernbuf[count - 1] = 0;
1927 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1928 if (cdt->cdt_state == CDT_DISABLE) {
1929 cdt->cdt_state = CDT_RUNNING;
1930 mdt_hsm_cdt_wakeup(mdt);
1932 rc = mdt_hsm_cdt_start(mdt);
1934 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1935 if ((cdt->cdt_state == CDT_STOPPING) ||
1936 (cdt->cdt_state == CDT_STOPPED)) {
1937 CERROR("%s: Coordinator already stopped\n",
1941 cdt->cdt_state = CDT_STOPPING;
1943 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1944 if ((cdt->cdt_state == CDT_STOPPING) ||
1945 (cdt->cdt_state == CDT_STOPPED)) {
1946 CERROR("%s: Coordinator is stopped\n",
1950 cdt->cdt_state = CDT_DISABLE;
1952 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1953 rc = hsm_cancel_all_actions(mdt);
1954 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1962 CERROR("%s: Valid coordinator control commands are: "
1963 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1964 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1965 CDT_PURGE_CMD, CDT_HELP_CMD);
1973 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1975 struct obd_device *obd = m->private;
1976 struct coordinator *cdt;
1979 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1981 if (cdt->cdt_state == CDT_INIT)
1982 seq_printf(m, "init\n");
1983 else if (cdt->cdt_state == CDT_RUNNING)
1984 seq_printf(m, "enabled\n");
1985 else if (cdt->cdt_state == CDT_STOPPING)
1986 seq_printf(m, "stopping\n");
1987 else if (cdt->cdt_state == CDT_STOPPED)
1988 seq_printf(m, "stopped\n");
1989 else if (cdt->cdt_state == CDT_DISABLE)
1990 seq_printf(m, "disabled\n");
1992 seq_printf(m, "unknown\n");
1998 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2004 for (i = 0; i < 8 * sizeof(mask); i++) {
2005 if (mask & (1UL << i)) {
2006 seq_printf(m, "%s%s", first ? "" : " ",
2007 hsm_copytool_action2name(i));
2017 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2019 struct mdt_device *mdt = m->private;
2020 struct coordinator *cdt = &mdt->mdt_coordinator;
2022 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2026 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2028 struct mdt_device *mdt = m->private;
2029 struct coordinator *cdt = &mdt->mdt_coordinator;
2031 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2035 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2037 struct mdt_device *mdt = m->private;
2038 struct coordinator *cdt = &mdt->mdt_coordinator;
2040 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2043 static inline enum hsm_copytool_action
2044 hsm_copytool_name2action(const char *name)
2046 if (strcasecmp(name, "NOOP") == 0)
2048 else if (strcasecmp(name, "ARCHIVE") == 0)
2049 return HSMA_ARCHIVE;
2050 else if (strcasecmp(name, "RESTORE") == 0)
2051 return HSMA_RESTORE;
2052 else if (strcasecmp(name, "REMOVE") == 0)
2054 else if (strcasecmp(name, "CANCEL") == 0)
2061 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2062 size_t user_count, __u64 *mask)
2064 char *buf, *pos, *name;
2070 if (!(user_count < 4096))
2073 buf_size = user_count + 1;
2075 OBD_ALLOC(buf, buf_size);
2079 if (copy_from_user(buf, user_buf, buf_size - 1))
2080 GOTO(out, rc = -EFAULT);
2082 buf[buf_size - 1] = '\0';
2085 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2091 action = hsm_copytool_name2action(name);
2093 GOTO(out, rc = -EINVAL);
2095 new_mask |= (1UL << action);
2101 OBD_FREE(buf, buf_size);
2107 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2108 size_t count, loff_t *off)
2110 struct seq_file *m = file->private_data;
2111 struct mdt_device *mdt = m->private;
2112 struct coordinator *cdt = &mdt->mdt_coordinator;
2114 return mdt_write_hsm_request_mask(file, buf, count,
2115 &cdt->cdt_user_request_mask);
2119 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2120 size_t count, loff_t *off)
2122 struct seq_file *m = file->private_data;
2123 struct mdt_device *mdt = m->private;
2124 struct coordinator *cdt = &mdt->mdt_coordinator;
2126 return mdt_write_hsm_request_mask(file, buf, count,
2127 &cdt->cdt_group_request_mask);
2131 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2132 size_t count, loff_t *off)
2134 struct seq_file *m = file->private_data;
2135 struct mdt_device *mdt = m->private;
2136 struct coordinator *cdt = &mdt->mdt_coordinator;
2138 return mdt_write_hsm_request_mask(file, buf, count,
2139 &cdt->cdt_other_request_mask);
2142 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2143 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2144 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2145 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2146 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2147 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2148 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2149 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2151 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2153 .fops = &mdt_hsm_agent_fops },
2154 { .name = "actions",
2155 .fops = &mdt_hsm_actions_fops,
2156 .proc_mode = 0444 },
2157 { .name = "default_archive_id",
2158 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2159 { .name = "grace_delay",
2160 .fops = &mdt_hsm_cdt_grace_delay_fops },
2161 { .name = "loop_period",
2162 .fops = &mdt_hsm_cdt_loop_period_fops },
2163 { .name = "max_requests",
2164 .fops = &mdt_hsm_cdt_max_requests_fops },
2166 .fops = &mdt_hsm_policy_fops },
2167 { .name = "active_request_timeout",
2168 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2169 { .name = "active_requests",
2170 .fops = &mdt_hsm_active_requests_fops },
2171 { .name = "user_request_mask",
2172 .fops = &mdt_hsm_user_request_mask_fops, },
2173 { .name = "group_request_mask",
2174 .fops = &mdt_hsm_group_request_mask_fops, },
2175 { .name = "other_request_mask",
2176 .fops = &mdt_hsm_other_request_mask_fops, },