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_create;
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_thread.t_flags = SVC_RUNNING;
421 wake_up(&cdt->cdt_thread.t_ctl_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_thread.t_ctl_waitq,
446 (cdt->cdt_thread.t_flags &
447 (SVC_STOPPING|SVC_EVENT)),
450 CDEBUG(D_HSM, "coordinator resumes\n");
452 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
453 cdt->cdt_state == CDT_STOPPING) {
454 cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
459 /* wake up before timeout, new work arrives */
460 if (cdt->cdt_thread.t_flags & SVC_EVENT)
461 cdt->cdt_thread.t_flags &= ~SVC_EVENT;
463 /* if coordinator is suspended continue to wait */
464 if (cdt->cdt_state == CDT_DISABLE) {
465 CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
469 CDEBUG(D_HSM, "coordinator starts reading llog\n");
471 if (hsd.max_requests != cdt->cdt_max_requests) {
472 /* cdt_max_requests has changed,
473 * we need to allocate a new buffer
475 OBD_FREE(hsd.request, request_sz);
476 hsd.max_requests = cdt->cdt_max_requests;
477 request_sz = hsd.max_requests * sizeof(*hsd.request);
478 OBD_ALLOC(hsd.request, request_sz);
487 rc = cdt_llog_process(mti->mti_env, mdt,
488 mdt_coordinator_cb, &hsd);
492 CDEBUG(D_HSM, "found %d requests to send\n", hsd.request_cnt);
494 if (list_empty(&cdt->cdt_agents)) {
495 CDEBUG(D_HSM, "no agent available, "
496 "coordinator sleeps\n");
500 /* here hsd contains a list of requests to be started */
501 for (i = 0; i < hsd.request_cnt; i++) {
502 struct hsm_scan_request *request = &hsd.request[i];
503 struct hsm_action_list *hal = request->hal;
504 struct hsm_action_item *hai;
507 enum agent_req_status status;
509 /* still room for work ? */
510 if (atomic_read(&cdt->cdt_request_count) >=
511 cdt->cdt_max_requests)
514 rc = mdt_hsm_agent_send(mti, hal, 0);
515 /* if failure, we suppose it is temporary
516 * if the copy tool failed to do the request
517 * it has to use hsm_progress
519 status = (rc ? ARS_WAITING : ARS_STARTED);
521 /* set up cookie vector to set records status
522 * after copy tools start or failed
524 sz = hal->hal_count * sizeof(__u64);
525 OBD_ALLOC(cookies, sz);
529 hai = hai_first(hal);
530 for (j = 0; j < hal->hal_count; j++) {
531 cookies[j] = hai->hai_cookie;
535 rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
536 hal->hal_count, status);
538 CERROR("%s: mdt_agent_record_update() failed, "
539 "rc=%d, cannot update status to %s "
541 mdt_obd_name(mdt), rc,
542 agent_req_status2name(status),
545 OBD_FREE(cookies, sz);
548 /* free hal allocated by callback */
549 for (i = 0; i < hsd.request_cnt; i++) {
550 struct hsm_scan_request *request = &hsd.request[i];
552 OBD_FREE(request->hal, request->hal_sz);
558 OBD_FREE(hsd.request, request_sz);
560 if (cdt->cdt_state == CDT_STOPPING) {
561 /* request comes from /proc path, so we need to clean cdt
563 mdt_hsm_cdt_stop(mdt);
564 mdt->mdt_opts.mo_coordinator = 0;
566 /* request comes from a thread event, generated
567 * by mdt_stop_coordinator(), we have to ack
568 * and cdt cleaning will be done by event sender
570 cdt->cdt_thread.t_flags = SVC_STOPPED;
571 wake_up(&cdt->cdt_thread.t_ctl_waitq);
575 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
576 mdt_obd_name(mdt), current_pid(), rc);
578 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
580 mdt_obd_name(mdt), current_pid());
586 * lookup a restore handle by FID
587 * caller needs to hold cdt_restore_lock
588 * \param cdt [IN] coordinator
589 * \param fid [IN] FID
590 * \retval cdt_restore_handle found
591 * \retval NULL not found
593 struct cdt_restore_handle *mdt_hsm_restore_hdl_find(struct coordinator *cdt,
594 const struct lu_fid *fid)
596 struct cdt_restore_handle *crh;
599 list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
600 if (lu_fid_eq(&crh->crh_fid, fid))
607 * data passed to llog_cat_process() callback
608 * to scan requests and take actions
610 struct hsm_restore_data {
611 struct mdt_thread_info *hrd_mti;
615 * llog_cat_process() callback, used to:
616 * - find restore request and allocate the restore handle
617 * \param env [IN] environment
618 * \param llh [IN] llog handle
619 * \param hdr [IN] llog record
620 * \param data [IN/OUT] cb data = struct hsm_restore_data
622 * \retval -ve failure
624 static int hsm_restore_cb(const struct lu_env *env,
625 struct llog_handle *llh,
626 struct llog_rec_hdr *hdr, void *data)
628 struct llog_agent_req_rec *larr;
629 struct hsm_restore_data *hrd;
630 struct cdt_restore_handle *crh;
631 struct hsm_action_item *hai;
632 struct mdt_thread_info *mti;
633 struct coordinator *cdt;
634 struct mdt_object *child;
640 cdt = &mti->mti_mdt->mdt_coordinator;
642 larr = (struct llog_agent_req_rec *)hdr;
643 hai = &larr->arr_hai;
644 if (hai->hai_cookie > cdt->cdt_last_cookie)
645 /* update the cookie to avoid collision */
646 cdt->cdt_last_cookie = hai->hai_cookie + 1;
648 if (hai->hai_action != HSMA_RESTORE ||
649 agent_req_in_final_state(larr->arr_status))
652 /* restore request not in a final state */
654 /* force replay of restore requests left in started state from previous
655 * CDT context, to be canceled later if finally found to be incompatible
656 * when being re-started */
657 if (larr->arr_status == ARS_STARTED) {
658 larr->arr_status = ARS_WAITING;
659 larr->arr_req_change = cfs_time_current_sec();
660 rc = llog_write(env, llh, hdr, hdr->lrh_index);
665 OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
669 crh->crh_fid = hai->hai_fid;
670 /* in V1 all file is restored
671 crh->extent.start = hai->hai_extent.offset;
672 crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
674 crh->crh_extent.start = 0;
675 crh->crh_extent.end = hai->hai_extent.length;
676 /* get the layout lock */
677 mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
678 child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
679 MDS_INODELOCK_LAYOUT);
681 GOTO(out, rc = PTR_ERR(child));
684 /* we choose to not keep a reference
685 * on the object during the restore time which can be very long */
686 mdt_object_put(mti->mti_env, child);
688 mutex_lock(&cdt->cdt_restore_lock);
689 list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
690 mutex_unlock(&cdt->cdt_restore_lock);
697 * restore coordinator state at startup
698 * the goal is to take a layout lock for each registered restore request
699 * \param mti [IN] context
701 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
703 struct hsm_restore_data hrd;
709 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
710 hsm_restore_cb, &hrd);
715 static int hsm_init_ucred(struct lu_ucred *uc)
719 uc->uc_valid = UCRED_OLD;
728 uc->uc_suppgids[0] = -1;
729 uc->uc_suppgids[1] = -1;
730 uc->uc_cap = CFS_CAP_FS_MASK;
733 uc->uc_identity = NULL;
739 * wake up coordinator thread
740 * \param mdt [IN] device
742 * \retval -ve failure
744 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
746 struct coordinator *cdt = &mdt->mdt_coordinator;
749 if (cdt->cdt_state == CDT_STOPPED)
752 /* wake up coordinator */
753 cdt->cdt_thread.t_flags = SVC_EVENT;
754 wake_up(&cdt->cdt_thread.t_ctl_waitq);
760 * initialize coordinator struct
761 * \param mdt [IN] device
763 * \retval -ve failure
765 int mdt_hsm_cdt_init(struct mdt_device *mdt)
767 struct coordinator *cdt = &mdt->mdt_coordinator;
768 struct mdt_thread_info *cdt_mti = NULL;
772 cdt->cdt_state = CDT_STOPPED;
774 init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
775 mutex_init(&cdt->cdt_llog_lock);
776 init_rwsem(&cdt->cdt_agent_lock);
777 init_rwsem(&cdt->cdt_request_lock);
778 mutex_init(&cdt->cdt_restore_lock);
780 INIT_LIST_HEAD(&cdt->cdt_requests);
781 INIT_LIST_HEAD(&cdt->cdt_agents);
782 INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
784 rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
788 /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
789 rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
791 lu_context_enter(&cdt->cdt_session);
792 cdt->cdt_env.le_ses = &cdt->cdt_session;
794 lu_env_fini(&cdt->cdt_env);
798 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
799 LASSERT(cdt_mti != NULL);
801 cdt_mti->mti_env = &cdt->cdt_env;
802 cdt_mti->mti_mdt = mdt;
804 hsm_init_ucred(mdt_ucred(cdt_mti));
806 /* default values for /proc tunnables
807 * can be override by MGS conf */
808 cdt->cdt_default_archive_id = 1;
809 cdt->cdt_grace_delay = 60;
810 cdt->cdt_loop_period = 10;
811 cdt->cdt_max_requests = 3;
812 cdt->cdt_policy = CDT_DEFAULT_POLICY;
813 cdt->cdt_active_req_timeout = 3600;
819 * free a coordinator thread
820 * \param mdt [IN] device
822 int mdt_hsm_cdt_fini(struct mdt_device *mdt)
824 struct coordinator *cdt = &mdt->mdt_coordinator;
827 lu_context_exit(cdt->cdt_env.le_ses);
828 lu_context_fini(cdt->cdt_env.le_ses);
830 lu_env_fini(&cdt->cdt_env);
836 * start a coordinator thread
837 * \param mdt [IN] device
839 * \retval -ve failure
841 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
843 struct coordinator *cdt = &mdt->mdt_coordinator;
846 struct mdt_thread_info *cdt_mti;
847 struct task_struct *task;
850 /* functions defined but not yet used
851 * this avoid compilation warning
855 if (cdt->cdt_state != CDT_STOPPED) {
856 CERROR("%s: Coordinator already started\n",
861 CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
862 cdt->cdt_policy = CDT_DEFAULT_POLICY;
864 cdt->cdt_state = CDT_INIT;
866 atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
867 /* just need to be larger than previous one */
868 /* cdt_last_cookie is protected by cdt_llog_lock */
869 cdt->cdt_last_cookie = cfs_time_current_sec();
870 atomic_set(&cdt->cdt_request_count, 0);
871 cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
872 cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
873 cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
875 /* to avoid deadlock when start is made through /proc
876 * /proc entries are created by the coordinator thread */
878 /* set up list of started restore requests */
879 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
880 rc = mdt_hsm_pending_restore(cdt_mti);
882 CERROR("%s: cannot take the layout locks needed"
883 " for registered restore: %d\n",
884 mdt_obd_name(mdt), rc);
886 task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
889 cdt->cdt_state = CDT_STOPPED;
890 CERROR("%s: error starting coordinator thread: %d\n",
891 mdt_obd_name(mdt), rc);
894 CDEBUG(D_HSM, "%s: coordinator thread started\n",
899 wait_event(cdt->cdt_thread.t_ctl_waitq,
900 (cdt->cdt_thread.t_flags & SVC_RUNNING));
902 cdt->cdt_state = CDT_RUNNING;
903 mdt->mdt_opts.mo_coordinator = 1;
908 * stop a coordinator thread
909 * \param mdt [IN] device
911 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
913 struct coordinator *cdt = &mdt->mdt_coordinator;
914 struct cdt_agent_req *car, *tmp1;
915 struct hsm_agent *ha, *tmp2;
916 struct cdt_restore_handle *crh, *tmp3;
917 struct mdt_thread_info *cdt_mti;
920 if (cdt->cdt_state == CDT_STOPPED) {
921 CERROR("%s: Coordinator already stopped\n",
926 if (cdt->cdt_state != CDT_STOPPING) {
927 /* stop coordinator thread before cleaning */
928 cdt->cdt_thread.t_flags = SVC_STOPPING;
929 wake_up(&cdt->cdt_thread.t_ctl_waitq);
930 wait_event(cdt->cdt_thread.t_ctl_waitq,
931 cdt->cdt_thread.t_flags & SVC_STOPPED);
933 cdt->cdt_state = CDT_STOPPED;
936 down_write(&cdt->cdt_request_lock);
937 list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
939 list_del(&car->car_request_list);
940 mdt_cdt_free_request(car);
942 up_write(&cdt->cdt_request_lock);
944 down_write(&cdt->cdt_agent_lock);
945 list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
946 list_del(&ha->ha_list);
949 up_write(&cdt->cdt_agent_lock);
951 cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
952 mutex_lock(&cdt->cdt_restore_lock);
953 list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
954 struct mdt_object *child;
956 /* give back layout lock */
957 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
959 mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
961 list_del(&crh->crh_list);
963 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
965 mutex_unlock(&cdt->cdt_restore_lock);
967 mdt->mdt_opts.mo_coordinator = 0;
973 * register all requests from an hal in the memory list
974 * \param mti [IN] context
975 * \param hal [IN] request
976 * \param uuid [OUT] in case of CANCEL, the uuid of the agent
977 * which is running the CT
979 * \retval -ve failure
981 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
982 struct hsm_action_list *hal, struct obd_uuid *uuid)
984 struct mdt_device *mdt = mti->mti_mdt;
985 struct coordinator *cdt = &mdt->mdt_coordinator;
986 struct hsm_action_item *hai;
990 /* register request in memory list */
991 hai = hai_first(hal);
992 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
993 struct cdt_agent_req *car;
995 /* in case of a cancel request, we first mark the ondisk
996 * record of the request we want to stop as canceled
997 * this does not change the cancel record
998 * it will be done when updating the request status
1000 if (hai->hai_action == HSMA_CANCEL) {
1001 rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1005 CERROR("%s: mdt_agent_record_update() failed, "
1006 "rc=%d, cannot update status to %s "
1007 "for cookie %#llx\n",
1008 mdt_obd_name(mdt), rc,
1009 agent_req_status2name(ARS_CANCELED),
1014 /* find the running request to set it canceled */
1015 car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1017 car->car_canceled = 1;
1018 /* uuid has to be changed to the one running the
1019 * request to cancel */
1020 *uuid = car->car_uuid;
1021 mdt_cdt_put_request(car);
1023 /* no need to memorize cancel request
1024 * this also avoid a deadlock when we receive
1025 * a purge all requests command
1030 if (hai->hai_action == HSMA_ARCHIVE) {
1031 struct mdt_object *obj;
1034 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1035 if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1038 GOTO(out, rc = PTR_ERR(obj));
1040 hsm.mh_flags |= HS_EXISTS;
1041 hsm.mh_arch_id = hal->hal_archive_id;
1042 rc = mdt_hsm_attr_set(mti, obj, &hsm);
1043 mdt_object_put(mti->mti_env, obj);
1048 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1049 hal->hal_archive_id, hal->hal_flags,
1052 GOTO(out, rc = PTR_ERR(car));
1054 rc = mdt_cdt_add_request(cdt, car);
1056 mdt_cdt_free_request(car);
1063 * swap layouts between 2 fids
1064 * \param mti [IN] context
1067 * \param mh_common [IN] MD HSM
1069 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1070 struct mdt_object *obj, const struct lu_fid *dfid,
1071 struct md_hsm *mh_common)
1073 struct mdt_object *dobj;
1074 struct mdt_lock_handle *dlh;
1078 if (!mdt_object_exists(obj))
1079 GOTO(out, rc = -ENOENT);
1081 /* we already have layout lock on obj so take only
1083 dlh = &mti->mti_lh[MDT_LH_OLD];
1084 mdt_lock_reg_init(dlh, LCK_EX);
1085 dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1087 GOTO(out, rc = PTR_ERR(dobj));
1089 /* if copy tool closes the volatile before sending the final
1090 * progress through llapi_hsm_copy_end(), all the objects
1091 * are removed and mdd_swap_layout LBUG */
1092 if (!mdt_object_exists(dobj)) {
1093 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1094 mdt_obd_name(mti->mti_mdt), PFID(dfid));
1095 GOTO(out_dobj, rc = -ENOENT);
1097 /* Since we only handle restores here, unconditionally use
1098 * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1099 * be preserved in case of failure during swap_layout and not
1100 * leave a file in an intermediate but incoherent state.
1101 * But need to setup HSM xattr of data FID before, reuse
1102 * mti and mh presets for FID in hsm_cdt_request_completed(),
1103 * only need to clear RELEASED and DIRTY.
1105 mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1106 rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1108 rc = mo_swap_layouts(mti->mti_env,
1109 mdt_object_child(obj),
1110 mdt_object_child(dobj),
1111 SWAP_LAYOUTS_MDS_HSM);
1114 mdt_object_unlock_put(mti, dobj, dlh, 1);
1120 * update status of a completed request
1121 * \param mti [IN] context
1122 * \param pgs [IN] progress of the copy tool
1123 * \param update_record [IN] update llog record
1125 * \retval -ve failure
1127 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1128 struct hsm_progress_kernel *pgs,
1129 const struct cdt_agent_req *car,
1130 enum agent_req_status *status)
1132 const struct lu_env *env = mti->mti_env;
1133 struct mdt_device *mdt = mti->mti_mdt;
1134 struct coordinator *cdt = &mdt->mdt_coordinator;
1135 struct mdt_object *obj = NULL;
1136 int cl_flags = 0, rc = 0;
1141 /* default is to retry */
1142 *status = ARS_WAITING;
1144 /* find object by FID
1145 * if error/removed continue anyway to get correct reporting done */
1146 obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1147 /* we will update MD HSM only if needed */
1148 is_mh_changed = false;
1150 /* no need to change mh->mh_arch_id
1151 * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1153 if (pgs->hpk_errval != 0) {
1154 switch (pgs->hpk_errval) {
1156 /* the copy tool does not support cancel
1157 * so the cancel request is failed
1158 * As we cannot distinguish a cancel progress
1159 * from another action progress (they have the
1160 * same cookie), we suppose here the CT returns
1161 * ENOSYS only if does not support cancel
1163 /* this can also happen when cdt calls it to
1164 * for a timed out request */
1165 *status = ARS_FAILED;
1166 /* to have a cancel event in changelog */
1167 pgs->hpk_errval = ECANCELED;
1170 /* the request record has already been set to
1171 * ARS_CANCELED, this set the cancel request
1173 *status = ARS_SUCCEED;
1176 /* retry only if current policy or requested, and
1177 * object is not on error/removed */
1178 *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1179 !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1180 IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1184 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1185 CERROR("%s: Request %#llx on "DFID
1186 " failed, error code %d too large\n",
1188 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1190 hsm_set_cl_error(&cl_flags,
1191 CLF_HSM_ERROVERFLOW);
1194 hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1197 switch (car->car_hai->hai_action) {
1199 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1202 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1205 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1208 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1209 CERROR("%s: Failed request %#llx on "DFID
1210 " cannot be a CANCEL\n",
1213 PFID(&pgs->hpk_fid));
1216 CERROR("%s: Failed request %#llx on "DFID
1217 " %d is an unknown action\n",
1219 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1220 car->car_hai->hai_action);
1225 *status = ARS_SUCCEED;
1226 switch (car->car_hai->hai_action) {
1228 hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1229 /* set ARCHIVE keep EXIST and clear LOST and
1231 mh.mh_arch_ver = pgs->hpk_data_version;
1232 mh.mh_flags |= HS_ARCHIVED;
1233 mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1234 is_mh_changed = true;
1237 hsm_set_cl_event(&cl_flags, HE_RESTORE);
1239 /* do not clear RELEASED and DIRTY here
1240 * this will occur in hsm_swap_layouts()
1243 /* Restoring has changed the file version on
1245 mh.mh_arch_ver = pgs->hpk_data_version;
1246 is_mh_changed = true;
1249 hsm_set_cl_event(&cl_flags, HE_REMOVE);
1250 /* clear ARCHIVED EXISTS and LOST */
1251 mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1252 is_mh_changed = true;
1255 hsm_set_cl_event(&cl_flags, HE_CANCEL);
1256 CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1259 PFID(&pgs->hpk_fid));
1262 CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1264 pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1265 car->car_hai->hai_action);
1271 /* rc != 0 means error when analysing action, it may come from
1272 * a crasy CT no need to manage DIRTY
1275 hsm_set_cl_flags(&cl_flags,
1276 mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1278 /* unlock is done later, after layout lock management */
1279 if (is_mh_changed && !IS_ERR(obj))
1280 rc = mdt_hsm_attr_set(mti, obj, &mh);
1282 /* we give back layout lock only if restore was successful or
1283 * if no retry will be attempted and if object is still alive,
1284 * in other cases we just unlock the object */
1285 if (car->car_hai->hai_action == HSMA_RESTORE) {
1286 struct cdt_restore_handle *crh;
1288 /* restore in data FID done, we swap the layouts
1289 * only if restore is successful */
1290 if (pgs->hpk_errval == 0 && !IS_ERR_OR_NULL(obj)) {
1291 rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1294 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1295 *status = ARS_FAILED;
1296 pgs->hpk_errval = -rc;
1299 /* we have to retry, so keep layout lock */
1300 if (*status == ARS_WAITING)
1303 /* give back layout lock */
1304 mutex_lock(&cdt->cdt_restore_lock);
1305 crh = mdt_hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1307 list_del(&crh->crh_list);
1308 mutex_unlock(&cdt->cdt_restore_lock);
1309 /* Just give back layout lock, we keep the reference
1310 * which is given back later with the lock for HSM
1312 * XXX obj may be invalid so we do not pass it. */
1314 mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
1317 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1323 /* always add a ChangeLog record */
1324 mo_changelog(env, CL_HSM, cl_flags, mdt->mdt_child,
1325 &car->car_hai->hai_fid);
1328 mdt_object_put(mti->mti_env, obj);
1334 * update status of a request
1335 * \param mti [IN] context
1336 * \param pgs [IN] progress of the copy tool
1337 * \param update_record [IN] update llog record
1339 * \retval -ve failure
1341 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1342 struct hsm_progress_kernel *pgs,
1343 const int update_record)
1345 struct mdt_device *mdt = mti->mti_mdt;
1346 struct coordinator *cdt = &mdt->mdt_coordinator;
1347 struct cdt_agent_req *car;
1351 /* no coordinator started, so we cannot serve requests */
1352 if (cdt->cdt_state == CDT_STOPPED)
1355 /* first do sanity checks */
1356 car = mdt_cdt_update_request(cdt, pgs);
1358 CERROR("%s: Cannot find running request for cookie %#llx"
1361 pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1363 RETURN(PTR_ERR(car));
1366 CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1367 " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1368 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1369 hsm_copytool_action2name(car->car_hai->hai_action),
1370 pgs->hpk_flags, pgs->hpk_errval,
1371 PFID(&car->car_hai->hai_fid),
1372 PFID(&car->car_hai->hai_dfid));
1374 /* progress is done on FID or data FID depending of the action and
1375 * of the copy progress */
1376 /* for restore progress is used to send back the data FID to cdt */
1377 if (car->car_hai->hai_action == HSMA_RESTORE &&
1378 lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1379 car->car_hai->hai_dfid = pgs->hpk_fid;
1381 if ((car->car_hai->hai_action == HSMA_RESTORE ||
1382 car->car_hai->hai_action == HSMA_ARCHIVE) &&
1383 (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1384 !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1385 CERROR("%s: Progress on "DFID" for cookie %#llx"
1386 " does not match request FID "DFID" nor data FID "
1389 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1390 PFID(&car->car_hai->hai_fid),
1391 PFID(&car->car_hai->hai_dfid));
1392 GOTO(out, rc = -EINVAL);
1395 if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1396 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1397 " is not coherent (err=%d and not completed"
1400 PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1401 hsm_copytool_action2name(car->car_hai->hai_action),
1402 pgs->hpk_errval, pgs->hpk_flags);
1403 GOTO(out, rc = -EINVAL);
1406 /* now progress is valid */
1408 /* we use a root like ucred */
1409 hsm_init_ucred(mdt_ucred(mti));
1411 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1412 enum agent_req_status status;
1414 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1416 /* remove request from memory list */
1417 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1419 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie=%#llx"
1420 " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1422 hsm_copytool_action2name(car->car_hai->hai_action),
1423 agent_req_status2name(status));
1425 if (update_record) {
1428 rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1429 &pgs->hpk_cookie, 1,
1432 CERROR("%s: mdt_agent_record_update() failed,"
1433 " rc=%d, cannot update status to %s"
1434 " for cookie %#llx\n",
1435 mdt_obd_name(mdt), rc1,
1436 agent_req_status2name(status),
1438 rc = (rc != 0 ? rc : rc1);
1440 /* ct has completed a request, so a slot is available, wakeup
1441 * cdt to find new work */
1442 mdt_hsm_cdt_wakeup(mdt);
1444 /* if copytool send a progress on a canceled request
1445 * we inform copytool it should stop
1447 if (car->car_canceled == 1)
1453 /* remove ref got from mdt_cdt_update_request() */
1454 mdt_cdt_put_request(car);
1461 * data passed to llog_cat_process() callback
1462 * to cancel requests
1464 struct hsm_cancel_all_data {
1465 struct mdt_device *mdt;
1469 * llog_cat_process() callback, used to:
1470 * - purge all requests
1471 * \param env [IN] environment
1472 * \param llh [IN] llog handle
1473 * \param hdr [IN] llog record
1474 * \param data [IN] cb data = struct hsm_cancel_all_data
1476 * \retval -ve failure
1478 static int mdt_cancel_all_cb(const struct lu_env *env,
1479 struct llog_handle *llh,
1480 struct llog_rec_hdr *hdr, void *data)
1482 struct llog_agent_req_rec *larr;
1483 struct hsm_cancel_all_data *hcad;
1487 larr = (struct llog_agent_req_rec *)hdr;
1489 if (larr->arr_status == ARS_WAITING ||
1490 larr->arr_status == ARS_STARTED) {
1491 larr->arr_status = ARS_CANCELED;
1492 larr->arr_req_change = cfs_time_current_sec();
1493 rc = llog_write(env, llh, hdr, hdr->lrh_index);
1500 * cancel all actions
1501 * \param obd [IN] MDT device
1503 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1505 struct mdt_thread_info *mti;
1506 struct coordinator *cdt = &mdt->mdt_coordinator;
1507 struct cdt_agent_req *car;
1508 struct hsm_action_list *hal = NULL;
1509 struct hsm_action_item *hai;
1510 struct hsm_cancel_all_data hcad;
1511 int hal_sz = 0, hal_len, rc;
1512 enum cdt_states save_state;
1515 /* retrieve coordinator context */
1516 mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1518 /* disable coordinator */
1519 save_state = cdt->cdt_state;
1520 cdt->cdt_state = CDT_DISABLE;
1522 /* send cancel to all running requests */
1523 down_read(&cdt->cdt_request_lock);
1524 list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1525 mdt_cdt_get_request(car);
1526 /* request is not yet removed from list, it will be done
1527 * when copytool will return progress
1530 if (car->car_hai->hai_action == HSMA_CANCEL) {
1531 mdt_cdt_put_request(car);
1536 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1537 cfs_size_round(car->car_hai->hai_len);
1539 if (hal_len > hal_sz && hal_sz > 0) {
1540 /* not enough room, free old buffer */
1541 OBD_FREE(hal, hal_sz);
1545 /* empty buffer, allocate one */
1548 OBD_ALLOC(hal, hal_sz);
1550 mdt_cdt_put_request(car);
1551 up_read(&cdt->cdt_request_lock);
1552 GOTO(out, rc = -ENOMEM);
1556 hal->hal_version = HAL_VERSION;
1557 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1559 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1560 hal->hal_compound_id = car->car_compound_id;
1561 hal->hal_archive_id = car->car_archive_id;
1562 hal->hal_flags = car->car_flags;
1565 hai = hai_first(hal);
1566 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1567 hai->hai_action = HSMA_CANCEL;
1570 /* it is possible to safely call mdt_hsm_agent_send()
1571 * (ie without a deadlock on cdt_request_lock), because the
1572 * write lock is taken only if we are not in purge mode
1573 * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1574 * nor mdt_cdt_remove_request())
1576 /* no conflict with cdt thread because cdt is disable and we
1577 * have the request lock */
1578 mdt_hsm_agent_send(mti, hal, 1);
1580 mdt_cdt_put_request(car);
1582 up_read(&cdt->cdt_request_lock);
1585 OBD_FREE(hal, hal_sz);
1587 /* cancel all on-disk records */
1590 rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1591 mdt_cancel_all_cb, &hcad);
1593 /* enable coordinator */
1594 cdt->cdt_state = save_state;
1600 * check if a request is compatible with file status
1601 * \param hai [IN] request description
1602 * \param hal_an [IN] request archive number (not used)
1603 * \param rq_flags [IN] request flags
1604 * \param hsm [IN] file HSM metadata
1607 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1608 const int hal_an, const __u64 rq_flags,
1609 const struct md_hsm *hsm)
1611 int is_compat = false;
1615 hsm_flags = hsm->mh_flags;
1616 switch (hai->hai_action) {
1618 if (!(hsm_flags & HS_NOARCHIVE) &&
1619 (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1623 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1624 hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1628 if (!(hsm_flags & HS_RELEASED) &&
1629 (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1636 CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1637 " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1638 PFID(&hai->hai_fid),
1639 hsm_copytool_action2name(hai->hai_action), rq_flags,
1640 hai->hai_extent.offset, hai->hai_extent.length,
1642 (is_compat ? "compatible" : "uncompatible"));
1648 * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1650 static const struct {
1654 } hsm_policy_names[] = {
1655 { CDT_NONBLOCKING_RESTORE, "NonBlockingRestore", "NBR"},
1656 { CDT_NORETRY_ACTION, "NoRetryAction", "NRA"},
1661 * convert a policy name to a bit
1662 * \param name [IN] policy name
1664 * \retval policy bit
1666 static __u64 hsm_policy_str2bit(const char *name)
1670 for (i = 0; hsm_policy_names[i].bit != 0; i++)
1671 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1672 strcmp(hsm_policy_names[i].name, name) == 0)
1673 return hsm_policy_names[i].bit;
1678 * convert a policy bit field to a string
1679 * \param mask [IN] policy bit field
1680 * \param hexa [IN] print mask before bit names
1681 * \param buffer [OUT] string
1682 * \param count [IN] size of buffer
1684 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
1692 seq_printf(m, "(%#llx) ", mask);
1694 for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1697 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1698 if (hsm_policy_names[j].bit == bit)
1702 seq_printf(m, "[%s] ", hsm_policy_names[j].name);
1704 seq_printf(m, "%s ", hsm_policy_names[j].name);
1706 /* remove last ' ' */
1711 /* methods to read/write HSM policy flags */
1712 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
1714 struct mdt_device *mdt = m->private;
1715 struct coordinator *cdt = &mdt->mdt_coordinator;
1718 hsm_policy_bit2str(m, cdt->cdt_policy, false);
1723 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
1724 size_t count, loff_t *off)
1726 struct seq_file *m = file->private_data;
1727 struct mdt_device *mdt = m->private;
1728 struct coordinator *cdt = &mdt->mdt_coordinator;
1729 char *start, *token, sign;
1732 __u64 add_mask, remove_mask, set_mask;
1736 if (count + 1 > PAGE_SIZE)
1739 OBD_ALLOC(buf, count + 1);
1743 if (copy_from_user(buf, buffer, count))
1744 GOTO(out, rc = -EFAULT);
1749 CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1752 add_mask = remove_mask = set_mask = 0;
1754 token = strsep(&start, "\n ");
1760 if (sign == '-' || sign == '+')
1763 policy = hsm_policy_str2bit(token);
1765 CWARN("%s: '%s' is unknown, "
1766 "supported policies are:\n", mdt_obd_name(mdt),
1768 hsm_policy_bit2str(m, 0, false);
1769 GOTO(out, rc = -EINVAL);
1773 remove_mask |= policy;
1783 } while (start != NULL);
1785 CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
1786 mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1788 /* if no sign in all string, it is a clear and set
1789 * if some sign found, all unsigned are converted
1791 * P1 P2 = set to P1 and P2
1792 * P1 -P2 = add P1 clear P2 same as +P1 -P2
1794 if (remove_mask == 0 && add_mask == 0) {
1795 cdt->cdt_policy = set_mask;
1797 cdt->cdt_policy |= set_mask | add_mask;
1798 cdt->cdt_policy &= ~remove_mask;
1801 GOTO(out, rc = count);
1804 OBD_FREE(buf, count + 1);
1807 LPROC_SEQ_FOPS(mdt_hsm_policy);
1809 #define GENERATE_PROC_METHOD(VAR) \
1810 static int mdt_hsm_##VAR##_seq_show(struct seq_file *m, void *data) \
1812 struct mdt_device *mdt = m->private; \
1813 struct coordinator *cdt = &mdt->mdt_coordinator; \
1816 seq_printf(m, "%llu\n", (__u64)cdt->VAR); \
1820 mdt_hsm_##VAR##_seq_write(struct file *file, const char __user *buffer, \
1821 size_t count, loff_t *off) \
1824 struct seq_file *m = file->private_data; \
1825 struct mdt_device *mdt = m->private; \
1826 struct coordinator *cdt = &mdt->mdt_coordinator; \
1831 rc = lprocfs_str_to_s64(buffer, count, &val); \
1834 if (val > 0 && val < INT_MAX) { \
1841 GENERATE_PROC_METHOD(cdt_loop_period)
1842 GENERATE_PROC_METHOD(cdt_grace_delay)
1843 GENERATE_PROC_METHOD(cdt_active_req_timeout)
1844 GENERATE_PROC_METHOD(cdt_max_requests)
1845 GENERATE_PROC_METHOD(cdt_default_archive_id)
1848 * procfs write method for MDT/hsm_control
1849 * proc entry is in mdt directory so data is mdt obd_device pointer
1851 #define CDT_ENABLE_CMD "enabled"
1852 #define CDT_STOP_CMD "shutdown"
1853 #define CDT_DISABLE_CMD "disabled"
1854 #define CDT_PURGE_CMD "purge"
1855 #define CDT_HELP_CMD "help"
1856 #define CDT_MAX_CMD_LEN 10
1859 mdt_hsm_cdt_control_seq_write(struct file *file, const char __user *buffer,
1860 size_t count, loff_t *off)
1862 struct seq_file *m = file->private_data;
1863 struct obd_device *obd = m->private;
1864 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
1865 struct coordinator *cdt = &(mdt->mdt_coordinator);
1867 char kernbuf[CDT_MAX_CMD_LEN];
1870 if (count == 0 || count >= sizeof(kernbuf))
1873 if (copy_from_user(kernbuf, buffer, count))
1877 if (kernbuf[count - 1] == '\n')
1878 kernbuf[count - 1] = 0;
1881 if (strcmp(kernbuf, CDT_ENABLE_CMD) == 0) {
1882 if (cdt->cdt_state == CDT_DISABLE) {
1883 cdt->cdt_state = CDT_RUNNING;
1884 mdt_hsm_cdt_wakeup(mdt);
1886 rc = mdt_hsm_cdt_start(mdt);
1888 } else if (strcmp(kernbuf, CDT_STOP_CMD) == 0) {
1889 if ((cdt->cdt_state == CDT_STOPPING) ||
1890 (cdt->cdt_state == CDT_STOPPED)) {
1891 CERROR("%s: Coordinator already stopped\n",
1895 cdt->cdt_state = CDT_STOPPING;
1897 } else if (strcmp(kernbuf, CDT_DISABLE_CMD) == 0) {
1898 if ((cdt->cdt_state == CDT_STOPPING) ||
1899 (cdt->cdt_state == CDT_STOPPED)) {
1900 CERROR("%s: Coordinator is stopped\n",
1904 cdt->cdt_state = CDT_DISABLE;
1906 } else if (strcmp(kernbuf, CDT_PURGE_CMD) == 0) {
1907 rc = hsm_cancel_all_actions(mdt);
1908 } else if (strcmp(kernbuf, CDT_HELP_CMD) == 0) {
1916 CERROR("%s: Valid coordinator control commands are: "
1917 "%s %s %s %s %s\n", mdt_obd_name(mdt),
1918 CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1919 CDT_PURGE_CMD, CDT_HELP_CMD);
1927 int mdt_hsm_cdt_control_seq_show(struct seq_file *m, void *data)
1929 struct obd_device *obd = m->private;
1930 struct coordinator *cdt;
1933 cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1935 if (cdt->cdt_state == CDT_INIT)
1936 seq_printf(m, "init\n");
1937 else if (cdt->cdt_state == CDT_RUNNING)
1938 seq_printf(m, "enabled\n");
1939 else if (cdt->cdt_state == CDT_STOPPING)
1940 seq_printf(m, "stopping\n");
1941 else if (cdt->cdt_state == CDT_STOPPED)
1942 seq_printf(m, "stopped\n");
1943 else if (cdt->cdt_state == CDT_DISABLE)
1944 seq_printf(m, "disabled\n");
1946 seq_printf(m, "unknown\n");
1952 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
1958 for (i = 0; i < 8 * sizeof(mask); i++) {
1959 if (mask & (1UL << i)) {
1960 seq_printf(m, "%s%s", first ? "" : " ",
1961 hsm_copytool_action2name(i));
1971 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
1973 struct mdt_device *mdt = m->private;
1974 struct coordinator *cdt = &mdt->mdt_coordinator;
1976 return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
1980 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
1982 struct mdt_device *mdt = m->private;
1983 struct coordinator *cdt = &mdt->mdt_coordinator;
1985 return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
1989 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
1991 struct mdt_device *mdt = m->private;
1992 struct coordinator *cdt = &mdt->mdt_coordinator;
1994 return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
1997 static inline enum hsm_copytool_action
1998 hsm_copytool_name2action(const char *name)
2000 if (strcasecmp(name, "NOOP") == 0)
2002 else if (strcasecmp(name, "ARCHIVE") == 0)
2003 return HSMA_ARCHIVE;
2004 else if (strcasecmp(name, "RESTORE") == 0)
2005 return HSMA_RESTORE;
2006 else if (strcasecmp(name, "REMOVE") == 0)
2008 else if (strcasecmp(name, "CANCEL") == 0)
2015 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2016 size_t user_count, __u64 *mask)
2018 char *buf, *pos, *name;
2024 if (!(user_count < 4096))
2027 buf_size = user_count + 1;
2029 OBD_ALLOC(buf, buf_size);
2033 if (copy_from_user(buf, user_buf, buf_size - 1))
2034 GOTO(out, rc = -EFAULT);
2036 buf[buf_size - 1] = '\0';
2039 while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2045 action = hsm_copytool_name2action(name);
2047 GOTO(out, rc = -EINVAL);
2049 new_mask |= (1UL << action);
2055 OBD_FREE(buf, buf_size);
2061 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2062 size_t count, loff_t *off)
2064 struct seq_file *m = file->private_data;
2065 struct mdt_device *mdt = m->private;
2066 struct coordinator *cdt = &mdt->mdt_coordinator;
2068 return mdt_write_hsm_request_mask(file, buf, count,
2069 &cdt->cdt_user_request_mask);
2073 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2074 size_t count, loff_t *off)
2076 struct seq_file *m = file->private_data;
2077 struct mdt_device *mdt = m->private;
2078 struct coordinator *cdt = &mdt->mdt_coordinator;
2080 return mdt_write_hsm_request_mask(file, buf, count,
2081 &cdt->cdt_group_request_mask);
2085 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2086 size_t count, loff_t *off)
2088 struct seq_file *m = file->private_data;
2089 struct mdt_device *mdt = m->private;
2090 struct coordinator *cdt = &mdt->mdt_coordinator;
2092 return mdt_write_hsm_request_mask(file, buf, count,
2093 &cdt->cdt_other_request_mask);
2096 LPROC_SEQ_FOPS(mdt_hsm_cdt_loop_period);
2097 LPROC_SEQ_FOPS(mdt_hsm_cdt_grace_delay);
2098 LPROC_SEQ_FOPS(mdt_hsm_cdt_active_req_timeout);
2099 LPROC_SEQ_FOPS(mdt_hsm_cdt_max_requests);
2100 LPROC_SEQ_FOPS(mdt_hsm_cdt_default_archive_id);
2101 LPROC_SEQ_FOPS(mdt_hsm_user_request_mask);
2102 LPROC_SEQ_FOPS(mdt_hsm_group_request_mask);
2103 LPROC_SEQ_FOPS(mdt_hsm_other_request_mask);
2105 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2107 .fops = &mdt_hsm_agent_fops },
2108 { .name = "actions",
2109 .fops = &mdt_hsm_actions_fops,
2110 .proc_mode = 0444 },
2111 { .name = "default_archive_id",
2112 .fops = &mdt_hsm_cdt_default_archive_id_fops },
2113 { .name = "grace_delay",
2114 .fops = &mdt_hsm_cdt_grace_delay_fops },
2115 { .name = "loop_period",
2116 .fops = &mdt_hsm_cdt_loop_period_fops },
2117 { .name = "max_requests",
2118 .fops = &mdt_hsm_cdt_max_requests_fops },
2120 .fops = &mdt_hsm_policy_fops },
2121 { .name = "active_request_timeout",
2122 .fops = &mdt_hsm_cdt_active_req_timeout_fops },
2123 { .name = "active_requests",
2124 .fops = &mdt_hsm_active_requests_fops },
2125 { .name = "user_request_mask",
2126 .fops = &mdt_hsm_user_request_mask_fops, },
2127 { .name = "group_request_mask",
2128 .fops = &mdt_hsm_group_request_mask_fops, },
2129 { .name = "other_request_mask",
2130 .fops = &mdt_hsm_other_request_mask_fops, },