4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
26 * Copyright (c) 2013, 2016, Intel Corporation.
27 * Use is subject to license terms.
30 * lustre/mdt/mdt_coordinator.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
39 #define DEBUG_SUBSYSTEM S_MDS
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_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 %#llx"
106 " archive_id %d flags %#llx\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=%#llx/%#llx"
115 " action=%s extent=%#llx-%#llx gid=%#llx"
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);
221 hal->hal_version = HAL_VERSION;
222 strlcpy(hal->hal_fsname, hsd->fs_name,
223 MTI_NAME_MAXLEN + 1);
224 hal->hal_compound_id = larr->arr_compound_id;
225 hal->hal_archive_id = larr->arr_archive_id;
226 hal->hal_flags = larr->arr_flags;
228 request->hal_used_sz = hal_size(hal);
231 hai = hai_first(hal);
233 /* request is known */
234 /* we check if record archive num is the same as the
235 * known request, if not we will serve it in multiple
236 * time because we do not know if the agent can serve
238 * a use case is a compound made of multiple restore
239 * where the files are not archived in the same backend
241 if (larr->arr_archive_id !=
242 request->hal->hal_archive_id)
245 if (request->hal_sz <
246 request->hal_used_sz +
247 cfs_size_round(larr->arr_hai.hai_len)) {
248 /* Not enough room, need an extension */
252 sz = 2 * request->hal_sz;
253 OBD_ALLOC(hal_buffer, sz);
256 memcpy(hal_buffer, request->hal,
257 request->hal_used_sz);
258 OBD_FREE(request->hal,
260 request->hal = hal_buffer;
261 request->hal_sz = sz;
263 hai = hai_first(request->hal);
264 for (i = 0; i < request->hal->hal_count; i++)
267 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
268 hai->hai_cookie = larr->arr_hai.hai_cookie;
269 hai->hai_gid = larr->arr_hai.hai_gid;
271 request->hal_used_sz += cfs_size_round(hai->hai_len);
272 request->hal->hal_count++;
276 struct hsm_progress_kernel pgs;
277 struct cdt_agent_req *car;
278 cfs_time_t now = cfs_time_current_sec();
281 /* we search for a running request
282 * error may happen if coordinator crashes or stopped
283 * with running request
285 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
287 last = larr->arr_req_change;
289 last = car->car_req_update;
290 mdt_cdt_put_request(car);
293 /* test if request too long, if yes cancel it
294 * the same way the copy tool acknowledge a cancel request */
295 if (now <= last + cdt->cdt_active_req_timeout)
298 dump_llog_agent_req_rec("request timed out, start cleaning",
300 /* a too old cancel request just needs to be removed
301 * this can happen, if copy tool does not support
302 * cancel for other requests, we have to remove the
303 * running request and notify the copytool */
304 pgs.hpk_fid = larr->arr_hai.hai_fid;
305 pgs.hpk_cookie = larr->arr_hai.hai_cookie;
306 pgs.hpk_extent = larr->arr_hai.hai_extent;
307 pgs.hpk_flags = HP_FLAG_COMPLETED;
308 pgs.hpk_errval = ENOSYS;
309 pgs.hpk_data_version = 0;
311 /* update request state, but do not record in llog, to
312 * avoid deadlock on cdt_llog_lock */
313 rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
315 CERROR("%s: cannot cleanup timed out request: "
316 DFID" for cookie %#llx action=%s\n",
318 PFID(&pgs.hpk_fid), pgs.hpk_cookie,
319 hsm_copytool_action2name(
320 larr->arr_hai.hai_action));
323 /* The request no longer exists, forget
324 * about it, and do not send a cancel request
325 * to the client, for which an error will be
326 * sent back, leading to an endless cycle of
328 RETURN(LLOG_DEL_RECORD);
331 /* XXX A cancel request cannot be cancelled. */
332 if (larr->arr_hai.hai_action == HSMA_CANCEL)
335 larr->arr_status = ARS_CANCELED;
336 larr->arr_req_change = now;
337 rc = llog_write(hsd->mti->mti_env, llh, hdr, hdr->lrh_index);
339 CERROR("%s: cannot update agent log: rc = %d\n",
340 mdt_obd_name(mdt), rc);
346 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
347 cfs_time_current_sec())
348 RETURN(LLOG_DEL_RECORD);
355 * create /proc entries for coordinator
358 * \retval -ve failure
360 int hsm_cdt_procfs_init(struct mdt_device *mdt)
362 struct coordinator *cdt = &mdt->mdt_coordinator;
366 /* init /proc entries, failure is not critical */
367 cdt->cdt_proc_dir = lprocfs_register("hsm",
368 mdt2obd_dev(mdt)->obd_proc_entry,
369 lprocfs_mdt_hsm_vars, mdt);
370 if (IS_ERR(cdt->cdt_proc_dir)) {
371 rc = PTR_ERR(cdt->cdt_proc_dir);
372 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
373 " rc=%d\n", mdt_obd_name(mdt), rc);
374 cdt->cdt_proc_dir = NULL;
382 * remove /proc entries for coordinator
385 void hsm_cdt_procfs_fini(struct mdt_device *mdt)
387 struct coordinator *cdt = &mdt->mdt_coordinator;
389 LASSERT(cdt->cdt_state == CDT_STOPPED);
390 if (cdt->cdt_proc_dir != NULL)
391 lprocfs_remove(&cdt->cdt_proc_dir);
395 * get vector of hsm cdt /proc vars
399 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
401 return lprocfs_mdt_hsm_vars;
406 * \param data [IN] obd device
408 * \retval -ve failure
410 static int mdt_coordinator(void *data)
412 struct mdt_thread_info *mti = data;
413 struct mdt_device *mdt = mti->mti_mdt;
414 struct coordinator *cdt = &mdt->mdt_coordinator;
415 struct hsm_scan_data hsd = { NULL };
420 cdt->cdt_flags = SVC_RUNNING;
421 wake_up(&cdt->cdt_waitq);
423 CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
424 mdt_obd_name(mdt), current_pid());
426 /* we use a copy of cdt_max_requests in the cb, so if cdt_max_requests
427 * increases due to a change from /proc we do not overflow the
428 * hsd.request[] vector
430 hsd.max_requests = cdt->cdt_max_requests;
431 request_sz = hsd.max_requests * sizeof(*hsd.request);
432 OBD_ALLOC(hsd.request, request_sz);
434 GOTO(out, rc = -ENOMEM);
437 obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
440 struct l_wait_info lwi;
443 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
445 l_wait_event(cdt->cdt_waitq,
446 cdt->cdt_flags & (SVC_STOPPING|SVC_EVENT),
449 CDEBUG(D_HSM, "coordinator resumes\n");
451 if (cdt->cdt_flags & SVC_STOPPING ||
452 cdt->cdt_state == CDT_STOPPING) {
453 cdt->cdt_flags &= ~SVC_STOPPING;
458 /* wake up before timeout, new work arrives */
459 if (cdt->cdt_flags & SVC_EVENT)
460 cdt->cdt_flags &= ~SVC_EVENT;
462 /* if coordinator is suspended continue to wait */
463 if (cdt->cdt_state == CDT_DISABLE) {
464 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
468 CDEBUG(D_HSM, "coordinator starts reading llog\n");
470 if (hsd.max_requests != cdt->cdt_max_requests) {
471 /* cdt_max_requests has changed,
472 * we need to allocate a new buffer
474 OBD_FREE(hsd.request, request_sz);
475 hsd.max_requests = cdt->cdt_max_requests;
476 request_sz = hsd.max_requests * sizeof(*hsd.request);
477 OBD_ALLOC(hsd.request, request_sz);
486 rc = cdt_llog_process(mti->mti_env, mdt,
487 mdt_coordinator_cb, &hsd);
491 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
493 if (list_empty(&cdt->cdt_agents)) {
494 CDEBUG(D_HSM, "no agent available, "
495 "coordinator sleeps\n");
499 /* here hsd contains a list of requests to be started */
500 for (i = 0; i < hsd.request_cnt; i++) {
501 struct hsm_scan_request *request = &hsd.request[i];
502 struct hsm_action_list *hal = request->hal;
503 struct hsm_action_item *hai;
506 enum agent_req_status status;
508 /* still room for work ? */
509 if (atomic_read(&cdt->cdt_request_count) >=
510 cdt->cdt_max_requests)
513 rc = mdt_hsm_agent_send(mti, hal, 0);
514 /* if failure, we suppose it is temporary
515 * if the copy tool failed to do the request
516 * it has to use hsm_progress
518 status = (rc ? ARS_WAITING : ARS_STARTED);
520 /* set up cookie vector to set records status
521 * after copy tools start or failed
523 sz = hal->hal_count * sizeof(__u64);
524 OBD_ALLOC(cookies, sz);
528 hai = hai_first(hal);
529 for (j = 0; j < hal->hal_count; j++) {
530 cookies[j] = hai->hai_cookie;
534 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
535 hal->hal_count, status);
537 CERROR("%s: mdt_agent_record_update() failed, "
538 "rc=%d, cannot update status to %s "
540 mdt_obd_name(mdt), rc,
541 agent_req_status2name(status),
544 OBD_FREE(cookies, sz);
547 /* free hal allocated by callback */
548 for (i = 0; i < hsd.request_cnt; i++) {
549 struct hsm_scan_request *request = &hsd.request[i];
551 OBD_FREE(request->hal, request->hal_sz);
557 OBD_FREE(hsd.request, request_sz);
559 if (cdt->cdt_state == CDT_STOPPING) {
560 /* request comes from /proc path, so we need to clean cdt
562 mdt_hsm_cdt_stop(mdt);
563 mdt->mdt_opts.mo_coordinator = 0;
565 /* request comes from a thread event, generated
566 * by mdt_stop_coordinator(), we have to ack
567 * and cdt cleaning will be done by event sender
569 cdt->cdt_flags = SVC_STOPPED;
570 wake_up(&cdt->cdt_waitq);
574 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
575 mdt_obd_name(mdt), current_pid(), rc);
577 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
579 mdt_obd_name(mdt), current_pid());
585 * lookup a restore handle by FID
586 * caller needs to hold cdt_restore_lock
587 * \param cdt [IN] coordinator
588 * \param fid [IN] FID
589 * \retval cdt_restore_handle found
590 * \retval NULL not found
592 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
593 const struct lu_fid *fid)
595 struct cdt_restore_handle *crh;
598 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
599 if (lu_fid_eq(&crh->crh_fid, fid))
606 * data passed to llog_cat_process() callback
607 * to scan requests and take actions
609 struct hsm_restore_data {
610 struct mdt_thread_info *hrd_mti;
614 * llog_cat_process() callback, used to:
615 * - find restore request and allocate the restore handle
616 * \param env [IN] environment
617 * \param llh [IN] llog handle
618 * \param hdr [IN] llog record
619 * \param data [IN/OUT] cb data = struct hsm_restore_data
621 * \retval -ve failure
623 static int hsm_restore_cb(const struct lu_env *env,
624 struct llog_handle *llh,
625 struct llog_rec_hdr *hdr, void *data)
627 struct llog_agent_req_rec *larr;
628 struct hsm_restore_data *hrd;
629 struct cdt_restore_handle *crh;
630 struct hsm_action_item *hai;
631 struct mdt_thread_info *mti;
632 struct coordinator *cdt;
633 struct mdt_object *child;
639 cdt = &mti->mti_mdt->mdt_coordinator;
641 larr = (struct llog_agent_req_rec *)hdr;
642 hai = &larr->arr_hai;
643 if (hai->hai_cookie > cdt->cdt_last_cookie)
644 /* update the cookie to avoid collision */
645 cdt->cdt_last_cookie = hai->hai_cookie + 1;
647 if (hai->hai_action != HSMA_RESTORE ||
648 agent_req_in_final_state(larr->arr_status))
651 /* restore request not in a final state */
653 /* force replay of restore requests left in started state from previous
654 * CDT context, to be canceled later if finally found to be incompatible
655 * when being re-started */
656 if (larr->arr_status == ARS_STARTED) {
657 larr->arr_status = ARS_WAITING;
658 larr->arr_req_change = cfs_time_current_sec();
659 rc = llog_write(env, llh, hdr, hdr->lrh_index);
664 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
668 crh->crh_fid = hai->hai_fid;
669 /* in V1 all file is restored
670 crh->extent.start = hai->hai_extent.offset;
671 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
673 crh->crh_extent.start = 0;
674 crh->crh_extent.end = hai->hai_extent.length;
675 /* get the layout lock */
676 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
677 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
678 MDS_INODELOCK_LAYOUT);
680 GOTO(out, rc = PTR_ERR(child));
683 /* we choose to not keep a reference
684 * on the object during the restore time which can be very long */
685 mdt_object_put(mti->mti_env, child);
687 mutex_lock(&cdt->cdt_restore_lock);
688 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
689 mutex_unlock(&cdt->cdt_restore_lock);
696 * restore coordinator state at startup
697 * the goal is to take a layout lock for each registered restore request
698 * \param mti [IN] context
700 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
702 struct hsm_restore_data hrd;
708 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
709 hsm_restore_cb, &hrd);
714 static int hsm_init_ucred(struct lu_ucred *uc)
718 uc->uc_valid = UCRED_OLD;
727 uc->uc_suppgids[0] = -1;
728 uc->uc_suppgids[1] = -1;
729 uc->uc_cap = CFS_CAP_FS_MASK;
732 uc->uc_identity = NULL;
738 * wake up coordinator thread
739 * \param mdt [IN] device
741 * \retval -ve failure
743 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
745 struct coordinator *cdt = &mdt->mdt_coordinator;
748 if (cdt->cdt_state == CDT_STOPPED)
751 /* wake up coordinator */
752 cdt->cdt_flags = SVC_EVENT;
753 wake_up(&cdt->cdt_waitq);
759 * initialize coordinator struct
760 * \param mdt [IN] device
762 * \retval -ve failure
764 int mdt_hsm_cdt_init(struct mdt_device *mdt)
766 struct coordinator *cdt = &mdt->mdt_coordinator;
767 struct mdt_thread_info *cdt_mti = NULL;
771 cdt->cdt_state = CDT_STOPPED;
773 init_waitqueue_head(&cdt->cdt_waitq);
774 mutex_init(&cdt->cdt_llog_lock);
775 init_rwsem(&cdt->cdt_agent_lock);
776 init_rwsem(&cdt->cdt_request_lock);
777 mutex_init(&cdt->cdt_restore_lock);
779 INIT_LIST_HEAD(&cdt->cdt_requests);
780 INIT_LIST_HEAD(&cdt->cdt_agents);
781 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
783 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
787 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
788 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
790 lu_context_enter(&cdt->cdt_session);
791 cdt->cdt_env.le_ses = &cdt->cdt_session;
793 lu_env_fini(&cdt->cdt_env);
797 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
798 LASSERT(cdt_mti != NULL);
800 cdt_mti->mti_env = &cdt->cdt_env;
801 cdt_mti->mti_mdt = mdt;
803 hsm_init_ucred(mdt_ucred(cdt_mti));
805 /* default values for /proc tunnables
806 * can be override by MGS conf */
807 cdt->cdt_default_archive_id = 1;
808 cdt->cdt_grace_delay = 60;
809 cdt->cdt_loop_period = 10;
810 cdt->cdt_max_requests = 3;
811 cdt->cdt_policy = CDT_DEFAULT_POLICY;
812 cdt->cdt_active_req_timeout = 3600;
818 * free a coordinator thread
819 * \param mdt [IN] device
821 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
823 struct coordinator *cdt = &mdt->mdt_coordinator;
826 lu_context_exit(cdt->cdt_env.le_ses);
827 lu_context_fini(cdt->cdt_env.le_ses);
829 lu_env_fini(&cdt->cdt_env);
835 * start a coordinator thread
836 * \param mdt [IN] device
838 * \retval -ve failure
840 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
842 struct coordinator *cdt = &mdt->mdt_coordinator;
845 struct mdt_thread_info *cdt_mti;
846 struct task_struct *task;
849 /* functions defined but not yet used
850 * this avoid compilation warning
854 if (cdt->cdt_state != CDT_STOPPED) {
855 CERROR("%s: Coordinator already started\n",
860 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
861 cdt->cdt_policy = CDT_DEFAULT_POLICY;
863 cdt->cdt_state = CDT_INIT;
865 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
866 /* just need to be larger than previous one */
867 /* cdt_last_cookie is protected by cdt_llog_lock */
868 cdt->cdt_last_cookie = cfs_time_current_sec();
869 atomic_set(&cdt->cdt_request_count, 0);
870 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
871 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
872 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
874 /* to avoid deadlock when start is made through /proc
875 * /proc entries are created by the coordinator thread */
877 /* set up list of started restore requests */
878 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
879 rc = mdt_hsm_pending_restore(cdt_mti);
881 CERROR("%s: cannot take the layout locks needed"
882 " for registered restore: %d\n",
883 mdt_obd_name(mdt), rc);
885 if (mdt->mdt_bottom->dd_rdonly)
888 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
891 cdt->cdt_state = CDT_STOPPED;
892 CERROR("%s: error starting coordinator thread: %d\n",
893 mdt_obd_name(mdt), rc);
896 CDEBUG(D_HSM, "%s: coordinator thread started\n",
901 wait_event(cdt->cdt_waitq,
902 (cdt->cdt_flags & SVC_RUNNING));
904 cdt->cdt_state = CDT_RUNNING;
905 mdt->mdt_opts.mo_coordinator = 1;
910 * stop a coordinator thread
911 * \param mdt [IN] device
913 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
915 struct coordinator *cdt = &mdt->mdt_coordinator;
916 struct cdt_agent_req *car, *tmp1;
917 struct hsm_agent *ha, *tmp2;
918 struct cdt_restore_handle *crh, *tmp3;
919 struct mdt_thread_info *cdt_mti;
922 if (cdt->cdt_state == CDT_STOPPED) {
923 CERROR("%s: Coordinator already stopped\n",
928 if (cdt->cdt_state != CDT_STOPPING) {
929 /* stop coordinator thread before cleaning */
930 cdt->cdt_flags = SVC_STOPPING;
931 wake_up(&cdt->cdt_waitq);
932 wait_event(cdt->cdt_waitq,
933 cdt->cdt_flags & SVC_STOPPED);
935 cdt->cdt_state = CDT_STOPPED;
938 down_write(&cdt->cdt_request_lock);
939 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
941 list_del(&car->car_request_list);
942 mdt_cdt_free_request(car);
944 up_write(&cdt->cdt_request_lock);
946 down_write(&cdt->cdt_agent_lock);
947 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
948 list_del(&ha->ha_list);
951 up_write(&cdt->cdt_agent_lock);
953 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
954 mutex_lock(&cdt->cdt_restore_lock);
955 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
956 struct mdt_object *child;
958 /* give back layout lock */
959 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
961 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
963 list_del(&crh->crh_list);
965 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
967 mutex_unlock(&cdt->cdt_restore_lock);
969 mdt->mdt_opts.mo_coordinator = 0;
975 * register all requests from an hal in the memory list
976 * \param mti [IN] context
977 * \param hal [IN] request
978 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
979 * which is running the CT
981 * \retval -ve failure
983 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
984 struct hsm_action_list *hal, struct obd_uuid *uuid)
986 struct mdt_device *mdt = mti->mti_mdt;
987 struct coordinator *cdt = &mdt->mdt_coordinator;
988 struct hsm_action_item *hai;
992 /* register request in memory list */
993 hai = hai_first(hal);
994 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
995 struct cdt_agent_req *car;
997 /* in case of a cancel request, we first mark the ondisk
998 * record of the request we want to stop as canceled
999 * this does not change the cancel record
1000 * it will be done when updating the request status
1002 if (hai->hai_action == HSMA_CANCEL) {
1003 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1007 CERROR("%s: mdt_agent_record_update() failed, "
1008 "rc=%d, cannot update status to %s "
1009 "for cookie %#llx\n",
1010 mdt_obd_name(mdt), rc,
1011 agent_req_status2name(ARS_CANCELED),
1016 /* find the running request to set it canceled */
1017 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1019 car->car_canceled = 1;
1020 /* uuid has to be changed to the one running the
1021 * request to cancel */
1022 *uuid = car->car_uuid;
1023 mdt_cdt_put_request(car);
1025 /* no need to memorize cancel request
1026 * this also avoid a deadlock when we receive
1027 * a purge all requests command
1032 if (hai->hai_action == HSMA_ARCHIVE) {
1033 struct mdt_object *obj;
1036 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1037 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1040 GOTO(out, rc = PTR_ERR(obj));
1042 hsm.mh_flags |= HS_EXISTS;
1043 hsm.mh_arch_id = hal->hal_archive_id;
1044 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1045 mdt_object_put(mti->mti_env, obj);
1050 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1051 hal->hal_archive_id, hal->hal_flags,
1054 GOTO(out, rc = PTR_ERR(car));
1056 rc = mdt_cdt_add_request(cdt, car);
1058 mdt_cdt_free_request(car);
1065 * swap layouts between 2 fids
1066 * \param mti [IN] context
1069 * \param mh_common [IN] MD HSM
1071 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1072 struct mdt_object *obj, const struct lu_fid *dfid,
1073 struct md_hsm *mh_common)
1075 struct mdt_object *dobj;
1076 struct mdt_lock_handle *dlh;
1080 if (!mdt_object_exists(obj))
1081 GOTO(out, rc = -ENOENT);
1083 /* we already have layout lock on obj so take only
1085 dlh = &mti->mti_lh[MDT_LH_OLD];
1086 mdt_lock_reg_init(dlh, LCK_EX);
1087 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1089 GOTO(out, rc = PTR_ERR(dobj));
1091 /* if copy tool closes the volatile before sending the final
1092 * progress through llapi_hsm_copy_end(), all the objects
1093 * are removed and mdd_swap_layout LBUG */
1094 if (!mdt_object_exists(dobj)) {
1095 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1096 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1097 GOTO(out_dobj, rc = -ENOENT);
1099 /* Since we only handle restores here, unconditionally use
1100 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1101 * be preserved in case of failure during swap_layout and not
1102 * leave a file in an intermediate but incoherent state.
1103 * But need to setup HSM xattr of data FID before, reuse
1104 * mti and mh presets for FID in hsm_cdt_request_completed(),
1105 * only need to clear RELEASED and DIRTY.
1107 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1108 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1110 rc = mo_swap_layouts(mti->mti_env,
1111 mdt_object_child(obj),
1112 mdt_object_child(dobj),
1113 SWAP_LAYOUTS_MDS_HSM);
1116 mdt_object_unlock_put(mti, dobj, dlh, 1);
1122 * update status of a completed request
1123 * \param mti [IN] context
1124 * \param pgs [IN] progress of the copy tool
1125 * \param update_record [IN] update llog record
1127 * \retval -ve failure
1129 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1130 struct hsm_progress_kernel *pgs,
1131 const struct cdt_agent_req *car,
1132 enum agent_req_status *status)
1134 const struct lu_env *env = mti->mti_env;
1135 struct mdt_device *mdt = mti->mti_mdt;
1136 struct coordinator *cdt = &mdt->mdt_coordinator;
1137 struct mdt_object *obj = NULL;
1138 int cl_flags = 0, rc = 0;
1141 bool need_changelog = true;
1144 /* default is to retry */
1145 *status = ARS_WAITING;
1147 /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1148 * if error/removed continue anyway to get correct reporting done */
1149 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1150 /* we will update MD HSM only if needed */
1151 is_mh_changed = false;
1153 /* no need to change mh->mh_arch_id
1154 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1156 if (pgs->hpk_errval != 0) {
1157 switch (pgs->hpk_errval) {
1159 /* the copy tool does not support cancel
1160 * so the cancel request is failed
1161 * As we cannot distinguish a cancel progress
1162 * from another action progress (they have the
1163 * same cookie), we suppose here the CT returns
1164 * ENOSYS only if does not support cancel
1166 /* this can also happen when cdt calls it to
1167 * for a timed out request */
1168 *status = ARS_FAILED;
1169 /* to have a cancel event in changelog */
1170 pgs->hpk_errval = ECANCELED;
1173 /* the request record has already been set to
1174 * ARS_CANCELED, this set the cancel request
1176 *status = ARS_SUCCEED;
1179 /* retry only if current policy or requested, and
1180 * object is not on error/removed */
1181 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1182 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1183 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1187 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1188 CERROR("%s: Request %#llx on "DFID
1189 " failed, error code %d too large\n",
1191 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1193 hsm_set_cl_error(&cl_flags,
1194 CLF_HSM_ERROVERFLOW);
1197 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1200 switch (car->car_hai->hai_action) {
1202 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1205 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1208 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1211 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1212 CERROR("%s: Failed request %#llx on "DFID
1213 " cannot be a CANCEL\n",
1216 PFID(&pgs->hpk_fid));
1219 CERROR("%s: Failed request %#llx on "DFID
1220 " %d is an unknown action\n",
1222 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1223 car->car_hai->hai_action);
1228 *status = ARS_SUCCEED;
1229 switch (car->car_hai->hai_action) {
1231 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1232 /* set ARCHIVE keep EXIST and clear LOST and
1234 mh.mh_arch_ver = pgs->hpk_data_version;
1235 mh.mh_flags |= HS_ARCHIVED;
1236 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1237 is_mh_changed = true;
1240 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1242 /* do not clear RELEASED and DIRTY here
1243 * this will occur in hsm_swap_layouts()
1246 /* Restoring has changed the file version on
1248 mh.mh_arch_ver = pgs->hpk_data_version;
1249 is_mh_changed = true;
1252 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1253 /* clear ARCHIVED EXISTS and LOST */
1254 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1255 is_mh_changed = true;
1258 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1259 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1262 PFID(&pgs->hpk_fid));
1265 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1267 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1268 car->car_hai->hai_action);
1274 /* rc != 0 means error when analysing action, it may come from
1275 * a crasy CT no need to manage DIRTY
1278 hsm_set_cl_flags(&cl_flags,
1279 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1281 /* unlock is done later, after layout lock management */
1282 if (is_mh_changed && !IS_ERR(obj))
1283 rc = mdt_hsm_attr_set(mti, obj, &mh);
1285 /* we give back layout lock only if restore was successful or
1286 * if no retry will be attempted and if object is still alive,
1287 * in other cases we just unlock the object */
1288 if (car->car_hai->hai_action == HSMA_RESTORE) {
1289 struct cdt_restore_handle *crh;
1291 /* restore in data FID done, we swap the layouts
1292 * only if restore is successful */
1293 if (pgs->hpk_errval == 0 && !IS_ERR_OR_NULL(obj)) {
1294 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1297 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1298 *status = ARS_FAILED;
1299 pgs->hpk_errval = -rc;
1302 /* we have to retry, so keep layout lock */
1303 if (*status == ARS_WAITING)
1306 /* restore special case, need to create ChangeLog record
1307 * before to give back layout lock to avoid concurrent
1308 * file updater to post out of order ChangeLog */
1309 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1310 &car->car_hai->hai_fid);
1311 need_changelog = false;
1313 /* give back layout lock */
1314 mutex_lock(&cdt->cdt_restore_lock);
1315 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1317 list_del(&crh->crh_list);
1318 mutex_unlock(&cdt->cdt_restore_lock);
1319 /* Just give back layout lock, we keep the reference
1320 * which is given back later with the lock for HSM
1322 * XXX obj may be invalid so we do not pass it. */
1324 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1327 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1333 /* always add a ChangeLog record */
1335 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1336 &car->car_hai->hai_fid);
1339 mdt_object_put(mti->mti_env, obj);
1345 * update status of a request
1346 * \param mti [IN] context
1347 * \param pgs [IN] progress of the copy tool
1348 * \param update_record [IN] update llog record
1350 * \retval -ve failure
1352 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1353 struct hsm_progress_kernel *pgs,
1354 const int update_record)
1356 struct mdt_device *mdt = mti->mti_mdt;
1357 struct coordinator *cdt = &mdt->mdt_coordinator;
1358 struct cdt_agent_req *car;
1362 /* no coordinator started, so we cannot serve requests */
1363 if (cdt->cdt_state == CDT_STOPPED)
1366 /* first do sanity checks */
1367 car = mdt_cdt_update_request(cdt, pgs);
1369 CERROR("%s: Cannot find running request for cookie %#llx"
1372 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1374 RETURN(PTR_ERR(car));
1377 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1378 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1379 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1380 hsm_copytool_action2name(car->car_hai->hai_action),
1381 pgs->hpk_flags, pgs->hpk_errval,
1382 PFID(&car->car_hai->hai_fid),
1383 PFID(&car->car_hai->hai_dfid));
1385 /* progress is done on FID or data FID depending of the action and
1386 * of the copy progress */
1387 /* for restore progress is used to send back the data FID to cdt */
1388 if (car->car_hai->hai_action == HSMA_RESTORE &&
1389 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1390 car->car_hai->hai_dfid = pgs->hpk_fid;
1392 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1393 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1394 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1395 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1396 CERROR("%s: Progress on "DFID" for cookie %#llx"
1397 " does not match request FID "DFID" nor data FID "
1400 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1401 PFID(&car->car_hai->hai_fid),
1402 PFID(&car->car_hai->hai_dfid));
1403 GOTO(out, rc = -EINVAL);
1406 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1407 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1408 " is not coherent (err=%d and not completed"
1411 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1412 hsm_copytool_action2name(car->car_hai->hai_action),
1413 pgs->hpk_errval, pgs->hpk_flags);
1414 GOTO(out, rc = -EINVAL);
1417 /* now progress is valid */
1419 /* we use a root like ucred */
1420 hsm_init_ucred(mdt_ucred(mti));
1422 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1423 enum agent_req_status status;
1425 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1427 CDEBUG(D_HSM, "%s record: fid="DFID" cookie=%#llx action=%s "
1429 update_record ? "Updating" : "Not updating",
1430 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1431 hsm_copytool_action2name(car->car_hai->hai_action),
1432 agent_req_status2name(status));
1434 /* update record first (LU-9075) */
1435 if (update_record) {
1438 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1439 &pgs->hpk_cookie, 1,
1442 CERROR("%s: mdt_agent_record_update() failed,"
1443 " rc=%d, cannot update status to %s"
1444 " for cookie %#llx\n",
1445 mdt_obd_name(mdt), rc1,
1446 agent_req_status2name(status),
1448 rc = (rc != 0 ? rc : rc1);
1451 /* then remove request from memory list (LU-9075) */
1452 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1454 /* ct has completed a request, so a slot is available, wakeup
1455 * cdt to find new work */
1456 mdt_hsm_cdt_wakeup(mdt);
1458 /* if copytool send a progress on a canceled request
1459 * we inform copytool it should stop
1461 if (car->car_canceled == 1)
1467 /* remove ref got from mdt_cdt_update_request() */
1468 mdt_cdt_put_request(car);
1475 * data passed to llog_cat_process() callback
1476 * to cancel requests
1478 struct hsm_cancel_all_data {
1479 struct mdt_device *mdt;
1483 * llog_cat_process() callback, used to:
1484 * - purge all requests
1485 * \param env [IN] environment
1486 * \param llh [IN] llog handle
1487 * \param hdr [IN] llog record
1488 * \param data [IN] cb data = struct hsm_cancel_all_data
1490 * \retval -ve failure
1492 static int mdt_cancel_all_cb(const struct lu_env *env,
1493 struct llog_handle *llh,
1494 struct llog_rec_hdr *hdr, void *data)
1496 struct llog_agent_req_rec *larr;
1497 struct hsm_cancel_all_data *hcad;
1501 larr = (struct llog_agent_req_rec *)hdr;
1503 if (larr->arr_status == ARS_WAITING ||
1504 larr->arr_status == ARS_STARTED) {
1505 larr->arr_status = ARS_CANCELED;
1506 larr->arr_req_change = cfs_time_current_sec();
1507 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1514 * cancel all actions
1515 * \param obd [IN] MDT device
1517 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1520 struct lu_context session;
1521 struct mdt_thread_info *mti;
1522 struct coordinator *cdt = &mdt->mdt_coordinator;
1523 struct cdt_agent_req *car;
1524 struct hsm_action_list *hal = NULL;
1525 struct hsm_action_item *hai;
1526 struct hsm_cancel_all_data hcad;
1527 int hal_sz = 0, hal_len, rc;
1528 enum cdt_states save_state;
1531 rc = lu_env_init(&env, LCT_MD_THREAD);
1535 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1536 rc = lu_context_init(&session, LCT_SERVER_SESSION);
1540 lu_context_enter(&session);
1541 env.le_ses = &session;
1543 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1544 LASSERT(mti != NULL);
1546 mti->mti_env = &env;
1549 hsm_init_ucred(mdt_ucred(mti));
1551 /* disable coordinator */
1552 save_state = cdt->cdt_state;
1553 cdt->cdt_state = CDT_DISABLE;
1555 /* send cancel to all running requests */
1556 down_read(&cdt->cdt_request_lock);
1557 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1558 mdt_cdt_get_request(car);
1559 /* request is not yet removed from list, it will be done
1560 * when copytool will return progress
1563 if (car->car_hai->hai_action == HSMA_CANCEL) {
1564 mdt_cdt_put_request(car);
1569 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1570 cfs_size_round(car->car_hai->hai_len);
1572 if (hal_len > hal_sz && hal_sz > 0) {
1573 /* not enough room, free old buffer */
1574 OBD_FREE(hal, hal_sz);
1578 /* empty buffer, allocate one */
1581 OBD_ALLOC(hal, hal_sz);
1583 mdt_cdt_put_request(car);
1584 up_read(&cdt->cdt_request_lock);
1585 GOTO(out_cdt_state, rc = -ENOMEM);
1589 hal->hal_version = HAL_VERSION;
1590 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1592 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1593 hal->hal_compound_id = car->car_compound_id;
1594 hal->hal_archive_id = car->car_archive_id;
1595 hal->hal_flags = car->car_flags;
1598 hai = hai_first(hal);
1599 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1600 hai->hai_action = HSMA_CANCEL;
1603 /* it is possible to safely call mdt_hsm_agent_send()
1604 * (ie without a deadlock on cdt_request_lock), because the
1605 * write lock is taken only if we are not in purge mode
1606 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1607 * nor mdt_cdt_remove_request())
1609 /* no conflict with cdt thread because cdt is disable and we
1610 * have the request lock */
1611 mdt_hsm_agent_send(mti, hal, 1);
1613 mdt_cdt_put_request(car);
1615 up_read(&cdt->cdt_request_lock);
1618 OBD_FREE(hal, hal_sz);
1620 /* cancel all on-disk records */
1623 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1624 mdt_cancel_all_cb, &hcad);
1626 /* enable coordinator */
1627 cdt->cdt_state = save_state;
1628 lu_context_exit(&session);
1629 lu_context_fini(&session);
1637 * check if a request is compatible with file status
1638 * \param hai [IN] request description
1639 * \param hal_an [IN] request archive number (not used)
1640 * \param rq_flags [IN] request flags
1641 * \param hsm [IN] file HSM metadata
1644 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1645 const int hal_an, const __u64 rq_flags,
1646 const struct md_hsm *hsm)
1648 int is_compat = false;
1652 hsm_flags = hsm->mh_flags;
1653 switch (hai->hai_action) {
1655 if (!(hsm_flags & HS_NOARCHIVE) &&
1656 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1660 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1661 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1665 if (!(hsm_flags & HS_RELEASED) &&
1666 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1673 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1674 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1675 PFID(&hai->hai_fid),
1676 hsm_copytool_action2name(hai->hai_action), rq_flags,
1677 hai->hai_extent.offset, hai->hai_extent.length,
1679 (is_compat ? "compatible" : "uncompatible"));
1685 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1687 static const struct {
1691 } hsm_policy_names[] = {
1692 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1693 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1698 * convert a policy name to a bit
1699 * \param name [IN] policy name
1701 * \retval policy bit
1703 static __u64 hsm_policy_str2bit(const char *name)
1707 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1708 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1709 strcmp(hsm_policy_names[i].name, name) == 0)
1710 return hsm_policy_names[i].bit;
1715 * convert a policy bit field to a string
1716 * \param mask [IN] policy bit field
1717 * \param hexa [IN] print mask before bit names
1718 * \param buffer [OUT] string
1719 * \param count [IN] size of buffer
1721 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1729 seq_printf(m, "(%#llx) ", mask);
1731 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1734 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1735 if (hsm_policy_names[j].bit == bit)
1739 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1741 seq_printf(m, "%s ", hsm_policy_names[j].name);
1743 /* remove last ' ' */
1748 /* methods to read/write HSM policy flags */
1749 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1751 struct mdt_device *mdt = m->private;
1752 struct coordinator *cdt = &mdt->mdt_coordinator;
1755 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1760 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1761 size_t count, loff_t *off)
1763 struct seq_file *m = file->private_data;
1764 struct mdt_device *mdt = m->private;
1765 struct coordinator *cdt = &mdt->mdt_coordinator;
1766 char *start, *token, sign;
1769 __u64 add_mask, remove_mask, set_mask;
1773 if (count + 1 > PAGE_SIZE)
1776 OBD_ALLOC(buf, count + 1);
1780 if (copy_from_user(buf, buffer, count))
1781 GOTO(out, rc = -EFAULT);
1786 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1789 add_mask = remove_mask = set_mask = 0;
1791 token = strsep(&start, "\n ");
1797 if (sign == '-' || sign == '+')
1800 policy = hsm_policy_str2bit(token);
1802 CWARN("%s: '%s' is unknown, "
1803 "supported policies are:\n", mdt_obd_name(mdt),
1805 hsm_policy_bit2str(m, 0, false);
1806 GOTO(out, rc = -EINVAL);
1810 remove_mask |= policy;
1820 } while (start != NULL);
1822 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1823 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1825 /* if no sign in all string, it is a clear and set
1826 * if some sign found, all unsigned are converted
1828 * P1 P2 = set to P1 and P2
1829 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1831 if (remove_mask == 0 && add_mask == 0) {
1832 cdt->cdt_policy = set_mask;
1834 cdt->cdt_policy |= set_mask | add_mask;
1835 cdt->cdt_policy &= ~remove_mask;
1838 GOTO(out, rc = count);
1841 OBD_FREE(buf, count + 1);
1844 LPROC_SEQ_FOPS(mdt_hsm_policy);
1846 #define GENERATE_PROC_METHOD(VAR) \
1847 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1849 struct mdt_device *mdt = m->private; \
1850 struct coordinator *cdt = &mdt->mdt_coordinator; \
1853 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1857 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1858 size_t count, loff_t *off) \
1861 struct seq_file *m = file->private_data; \
1862 struct mdt_device *mdt = m->private; \
1863 struct coordinator *cdt = &mdt->mdt_coordinator; \
1868 rc = lprocfs_str_to_s64(buffer, count, &val); \
1871 if (val > 0 && val < INT_MAX) { \
1878 GENERATE_PROC_METHOD(cdt_loop_period)
1879 GENERATE_PROC_METHOD(cdt_grace_delay)
1880 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1881 GENERATE_PROC_METHOD(cdt_max_requests)
1882 GENERATE_PROC_METHOD(cdt_default_archive_id)
1885 * procfs write method for MDT/hsm_control
1886 * proc entry is in mdt directory so data is mdt obd_device pointer
1888 #define CDT_ENABLE_CMD "enabled"
1889 #define CDT_STOP_CMD "shutdown"
1890 #define CDT_DISABLE_CMD "disabled"
1891 #define CDT_PURGE_CMD "purge"
1892 #define CDT_HELP_CMD "help"
1893 #define CDT_MAX_CMD_LEN 10
1896 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1897 size_t count, loff_t *off)
1899 struct seq_file *m = file->private_data;
1900 struct obd_device *obd = m->private;
1901 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1902 struct coordinator *cdt = &(mdt->mdt_coordinator);
1904 char kernbuf[CDT_MAX_CMD_LEN];
1907 if (count == 0 || count >= sizeof(kernbuf))
1910 if (copy_from_user(kernbuf, buffer, count))
1914 if (kernbuf[count - 1] == '\n')
1915 kernbuf[count - 1] = 0;
1918 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1919 if (cdt->cdt_state == CDT_DISABLE) {
1920 cdt->cdt_state = CDT_RUNNING;
1921 mdt_hsm_cdt_wakeup(mdt);
1923 rc = mdt_hsm_cdt_start(mdt);
1925 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1926 if ((cdt->cdt_state == CDT_STOPPING) ||
1927 (cdt->cdt_state == CDT_STOPPED)) {
1928 CERROR("%s: Coordinator already stopped\n",
1932 cdt->cdt_state = CDT_STOPPING;
1934 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1935 if ((cdt->cdt_state == CDT_STOPPING) ||
1936 (cdt->cdt_state == CDT_STOPPED)) {
1937 CERROR("%s: Coordinator is stopped\n",
1941 cdt->cdt_state = CDT_DISABLE;
1943 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1944 rc = hsm_cancel_all_actions(mdt);
1945 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1953 CERROR("%s: Valid coordinator control commands are: "
1954 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1955 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1956 CDT_PURGE_CMD, CDT_HELP_CMD);
1964 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1966 struct obd_device *obd = m->private;
1967 struct coordinator *cdt;
1970 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1972 if (cdt->cdt_state == CDT_INIT)
1973 seq_printf(m, "init\n");
1974 else if (cdt->cdt_state == CDT_RUNNING)
1975 seq_printf(m, "enabled\n");
1976 else if (cdt->cdt_state == CDT_STOPPING)
1977 seq_printf(m, "stopping\n");
1978 else if (cdt->cdt_state == CDT_STOPPED)
1979 seq_printf(m, "stopped\n");
1980 else if (cdt->cdt_state == CDT_DISABLE)
1981 seq_printf(m, "disabled\n");
1983 seq_printf(m, "unknown\n");
1989 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
1995 for (i = 0; i < 8 * sizeof(mask); i++) {
1996 if (mask & (1UL << i)) {
1997 seq_printf(m, "%s%s", first ? "" : " ",
1998 hsm_copytool_action2name(i));
2008 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2010 struct mdt_device *mdt = m->private;
2011 struct coordinator *cdt = &mdt->mdt_coordinator;
2013 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2017 mdt_hsm_group_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_group_request_mask);
2026 mdt_hsm_other_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_other_request_mask);
2034 static inline enum hsm_copytool_action
2035 hsm_copytool_name2action(const char *name)
2037 if (strcasecmp(name, "NOOP") == 0)
2039 else if (strcasecmp(name, "ARCHIVE") == 0)
2040 return HSMA_ARCHIVE;
2041 else if (strcasecmp(name, "RESTORE") == 0)
2042 return HSMA_RESTORE;
2043 else if (strcasecmp(name, "REMOVE") == 0)
2045 else if (strcasecmp(name, "CANCEL") == 0)
2052 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2053 size_t user_count, __u64 *mask)
2055 char *buf, *pos, *name;
2061 if (!(user_count < 4096))
2064 buf_size = user_count + 1;
2066 OBD_ALLOC(buf, buf_size);
2070 if (copy_from_user(buf, user_buf, buf_size - 1))
2071 GOTO(out, rc = -EFAULT);
2073 buf[buf_size - 1] = '\0';
2076 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2082 action = hsm_copytool_name2action(name);
2084 GOTO(out, rc = -EINVAL);
2086 new_mask |= (1UL << action);
2092 OBD_FREE(buf, buf_size);
2098 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2099 size_t count, loff_t *off)
2101 struct seq_file *m = file->private_data;
2102 struct mdt_device *mdt = m->private;
2103 struct coordinator *cdt = &mdt->mdt_coordinator;
2105 return mdt_write_hsm_request_mask(file, buf, count,
2106 &cdt->cdt_user_request_mask);
2110 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2111 size_t count, loff_t *off)
2113 struct seq_file *m = file->private_data;
2114 struct mdt_device *mdt = m->private;
2115 struct coordinator *cdt = &mdt->mdt_coordinator;
2117 return mdt_write_hsm_request_mask(file, buf, count,
2118 &cdt->cdt_group_request_mask);
2122 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2123 size_t count, loff_t *off)
2125 struct seq_file *m = file->private_data;
2126 struct mdt_device *mdt = m->private;
2127 struct coordinator *cdt = &mdt->mdt_coordinator;
2129 return mdt_write_hsm_request_mask(file, buf, count,
2130 &cdt->cdt_other_request_mask);
2133 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2134 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2135 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2136 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2137 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2138 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2139 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2140 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2142 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2144 .fops = &mdt_hsm_agent_fops },
2145 { .name = "actions",
2146 .fops = &mdt_hsm_actions_fops,
2147 .proc_mode = 0444 },
2148 { .name = "default_archive_id",
2149 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2150 { .name = "grace_delay",
2151 .fops = &mdt_hsm_cdt_grace_delay_fops },
2152 { .name = "loop_period",
2153 .fops = &mdt_hsm_cdt_loop_period_fops },
2154 { .name = "max_requests",
2155 .fops = &mdt_hsm_cdt_max_requests_fops },
2157 .fops = &mdt_hsm_policy_fops },
2158 { .name = "active_request_timeout",
2159 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2160 { .name = "active_requests",
2161 .fops = &mdt_hsm_active_requests_fops },
2162 { .name = "user_request_mask",
2163 .fops = &mdt_hsm_user_request_mask_fops, },
2164 { .name = "group_request_mask",
2165 .fops = &mdt_hsm_group_request_mask_fops, },
2166 { .name = "other_request_mask",
2167 .fops = &mdt_hsm_other_request_mask_fops, },