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 * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
25 * Copyright (c) 2016, Intel Corporation.
30 * lustre/mdt/mdt_hsm_cdt_agent.c
32 * Lustre HSM Coordinator
34 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
38 #define DEBUG_SUBSYSTEM S_MDS
41 #include <obd_support.h>
42 #include <lustre_export.h>
43 #include <lprocfs_status.h>
44 #include <lustre_kernelcomm.h>
45 #include "mdt_internal.h"
52 * find a hsm_agent by uuid
53 * lock cdt_agent_lock needs to be held by caller
54 * \param cdt [IN] coordinator
55 * \param uuid [IN] agent UUID
56 * \retval hsm_agent pointer or NULL if not found
58 static struct hsm_agent *mdt_hsm_agent_lookup(struct coordinator *cdt,
59 const struct obd_uuid *uuid)
63 list_for_each_entry(ha, &cdt->cdt_agents, ha_list) {
64 if (obd_uuid_equals(&ha->ha_uuid, uuid))
71 * register a copy tool
72 * \param mti [IN] MDT context
73 * \param uuid [IN] client UUID to be registered
74 * \param count [IN] number of archives agent serves
75 * \param archive_id [IN] vector of archive number served by the copytool
79 int mdt_hsm_agent_register(struct mdt_thread_info *mti,
80 const struct obd_uuid *uuid,
81 int nr_archives, __u32 *archive_id)
83 struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
84 struct hsm_agent *ha, *tmp;
88 /* no coordinator started, so we cannot serve requests */
89 if (cdt->cdt_state == CDT_STOPPED) {
90 LCONSOLE_WARN("HSM coordinator thread is not running - "
91 "denying agent registration.\n");
97 GOTO(out, rc = -ENOMEM);
100 ha->ha_archive_cnt = nr_archives;
101 if (ha->ha_archive_cnt != 0) {
104 sz = ha->ha_archive_cnt * sizeof(*ha->ha_archive_id);
105 OBD_ALLOC(ha->ha_archive_id, sz);
106 if (ha->ha_archive_id == NULL)
107 GOTO(out_free, rc = -ENOMEM);
108 memcpy(ha->ha_archive_id, archive_id, sz);
110 atomic_set(&ha->ha_requests, 0);
111 atomic_set(&ha->ha_success, 0);
112 atomic_set(&ha->ha_failure, 0);
114 down_write(&cdt->cdt_agent_lock);
115 tmp = mdt_hsm_agent_lookup(cdt, uuid);
117 LCONSOLE_WARN("HSM agent %s already registered\n",
119 up_write(&cdt->cdt_agent_lock);
120 GOTO(out_free, rc = -EEXIST);
123 list_add_tail(&ha->ha_list, &cdt->cdt_agents);
125 if (ha->ha_archive_cnt == 0)
126 CDEBUG(D_HSM, "agent %s registered for all archives\n",
127 obd_uuid2str(&ha->ha_uuid));
129 CDEBUG(D_HSM, "agent %s registered for %d archives\n",
130 obd_uuid2str(&ha->ha_uuid), ha->ha_archive_cnt);
132 up_write(&cdt->cdt_agent_lock);
137 if (ha != NULL && ha->ha_archive_id != NULL)
138 OBD_FREE(ha->ha_archive_id,
139 ha->ha_archive_cnt * sizeof(*ha->ha_archive_id));
147 * register a copy tool
148 * \param mti [IN] MDT context
149 * \param uuid [IN] uuid to be registered
150 * \param archive_mask [IN] bitmask of archive number served by the copytool
152 * \retval -ve failure
154 int mdt_hsm_agent_register_mask(struct mdt_thread_info *mti,
155 const struct obd_uuid *uuid, __u32 archive_mask)
157 int rc, i, nr_archives = 0;
158 __u32 *archive_id = NULL;
161 nr_archives = hweight32(archive_mask);
163 if (nr_archives != 0) {
164 OBD_ALLOC(archive_id, nr_archives * sizeof(*archive_id));
169 for (i = 0; i < sizeof(archive_mask) * 8; i++) {
170 if ((1 << i) & archive_mask) {
171 archive_id[nr_archives] = i + 1;
177 rc = mdt_hsm_agent_register(mti, uuid, nr_archives, archive_id);
179 if (archive_id != NULL)
180 OBD_FREE(archive_id, nr_archives * sizeof(*archive_id));
186 * unregister a copy tool
187 * \param mti [IN] MDT context
188 * \param uuid [IN] uuid to be unregistered
190 * \retval -ve failure
192 int mdt_hsm_agent_unregister(struct mdt_thread_info *mti,
193 const struct obd_uuid *uuid)
195 struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
196 struct hsm_agent *ha;
200 /* no coordinator started, so we cannot serve requests */
201 if (cdt->cdt_state == CDT_STOPPED)
204 down_write(&cdt->cdt_agent_lock);
206 ha = mdt_hsm_agent_lookup(cdt, uuid);
208 list_del_init(&ha->ha_list);
210 up_write(&cdt->cdt_agent_lock);
213 GOTO(out, rc = -ENOENT);
215 if (ha->ha_archive_cnt != 0)
216 OBD_FREE(ha->ha_archive_id,
217 ha->ha_archive_cnt * sizeof(*ha->ha_archive_id));
222 CDEBUG(D_HSM, "agent %s unregistration: %d\n", obd_uuid2str(uuid), rc);
228 * update agent statistics
229 * \param mdt [IN] MDT device
230 * \param succ_rq [IN] number of success
231 * \param fail_rq [IN] number of failure
232 * \param new_rq [IN] number of new requests
233 * \param uuid [IN] agent uuid
234 * if all counters == 0, clear counters
236 * \retval -ve failure
238 int mdt_hsm_agent_update_statistics(struct coordinator *cdt,
239 int succ_rq, int fail_rq, int new_rq,
240 const struct obd_uuid *uuid)
242 struct hsm_agent *ha;
246 down_read(&cdt->cdt_agent_lock);
247 list_for_each_entry(ha, &cdt->cdt_agents, ha_list) {
248 if (obd_uuid_equals(&ha->ha_uuid, uuid)) {
249 if (succ_rq == 0 && fail_rq == 0 && new_rq == 0) {
250 atomic_set(&ha->ha_success, 0);
251 atomic_set(&ha->ha_failure, 0);
252 atomic_set(&ha->ha_requests, 0);
254 atomic_add(succ_rq, &ha->ha_success);
255 atomic_add(fail_rq, &ha->ha_failure);
256 atomic_add(new_rq, &ha->ha_requests);
257 atomic_sub(succ_rq, &ha->ha_requests);
258 atomic_sub(fail_rq, &ha->ha_requests);
266 up_read(&cdt->cdt_agent_lock);
271 * find the best agent
272 * \param cdt [IN] coordinator
273 * \param archive [IN] archive number
274 * \param uuid [OUT] agent who can serve archive
276 * \retval -ve failure
278 int mdt_hsm_find_best_agent(struct coordinator *cdt, __u32 archive,
279 struct obd_uuid *uuid)
281 int rc = -EAGAIN, i, load = -1;
282 struct hsm_agent *ha;
285 /* Choose an export to send a copytool req to */
286 down_read(&cdt->cdt_agent_lock);
287 list_for_each_entry(ha, &cdt->cdt_agents, ha_list) {
288 for (i = 0; (i < ha->ha_archive_cnt) &&
289 (ha->ha_archive_id[i] != archive); i++) {
290 /* nothing to do, just skip unmatching records */
293 /* archive count == 0 means copy tool serves any backend */
294 if (ha->ha_archive_cnt != 0 && i == ha->ha_archive_cnt)
297 if (load == -1 || load > atomic_read(&ha->ha_requests)) {
298 load = atomic_read(&ha->ha_requests);
302 if (atomic_read(&ha->ha_requests) == 0)
305 up_read(&cdt->cdt_agent_lock);
310 int mdt_hsm_send_action_to_each_archive(struct mdt_thread_info *mti,
311 struct hsm_action_item *hai)
314 struct hsm_agent *ha;
315 __u32 archive_mask = 0;
316 struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
318 /* return error by default in case all archive_ids have unregistered */
322 /* send action to all registered archive_ids */
323 down_read(&cdt->cdt_agent_lock);
324 list_for_each_entry(ha, &cdt->cdt_agents, ha_list) {
325 for (i = 0; (i < ha->ha_archive_cnt); i++) {
326 /* only send once for each archive_id */
327 if ((1 << ha->ha_archive_id[i]) & archive_mask)
329 archive_mask |= (1 << ha->ha_archive_id[i]);
331 /* XXX: instead of creating one request record per
332 * new action, it could make sense to gather
333 * all for the same archive_id as one compound
334 * request/id, like in mdt_hsm_add_actions() ?? */
335 compound_id = atomic_inc_return(&cdt->cdt_compound_id);
336 rc = mdt_agent_record_add(mti->mti_env, mti->mti_mdt,
338 ha->ha_archive_id[i], 0,
341 CERROR("%s: unable to add HSM remove request "
342 "for "DFID": rc=%d\n",
343 mdt_obd_name(mti->mti_mdt),
344 PFID(&hai->hai_fid), rc);
347 CDEBUG(D_HSM, "%s: added HSM remove request "
348 "for "DFID", archive_id=%d\n",
349 mdt_obd_name(mti->mti_mdt),
351 ha->ha_archive_id[i]);
354 /* early exit from loop due to error? */
355 if (i != ha->ha_archive_cnt)
358 up_read(&cdt->cdt_agent_lock);
364 * send a compound request to the agent
365 * \param mti [IN] context
366 * \param hal [IN] request (can be a kuc payload)
367 * \param purge [IN] purge mode (no record)
369 * \retval -ve failure
370 * This function supposes:
371 * - all actions are for the same archive number
372 * - in case of cancel, all cancel are for the same agent
373 * This implies that request split has to be done
374 * before when building the hal
376 int mdt_hsm_agent_send(struct mdt_thread_info *mti,
377 struct hsm_action_list *hal, bool purge)
379 struct obd_export *exp;
380 struct mdt_device *mdt = mti->mti_mdt;
381 struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
382 struct hsm_action_list *buf = NULL;
383 struct hsm_action_item *hai;
384 struct obd_uuid uuid;
387 bool is_registered = false;
390 rc = mdt_hsm_find_best_agent(cdt, hal->hal_archive_id, &uuid);
391 if (rc && hal->hal_archive_id == 0) {
395 /* special case of remove requests with no archive_id specified,
396 * and no agent registered to serve all archives, then create a
397 * set of new requests, each to be sent to each registered
399 * Todo so, find all HSMA_REMOVE entries, and then :
400 * _ set completed status as SUCCESS (or FAIL?)
401 * _ create a new LLOG record for each archive_id
402 * presently being served by any CT
404 hai = hai_first(hal);
405 for (i = 0; i < hal->hal_count; i++,
406 hai = hai_next(hai)) {
407 /* only removes are concerned */
408 if (hai->hai_action != HSMA_REMOVE) {
409 /* count if other actions than HSMA_REMOVE,
410 * to return original error/rc */
415 /* send remove request to all registered archive_ids */
416 rc2 = mdt_hsm_send_action_to_each_archive(mti, hai);
420 /* only update original request as SUCCEED if it has
421 * been successfully broadcasted to all available
423 * XXX: this should only cause duplicates to be sent,
424 * unless a method to record already successfully
425 * reached archive_ids is implemented */
426 rc2 = mdt_agent_record_update(mti->mti_env, mdt,
430 CERROR("%s: mdt_agent_record_update() "
431 "failed, cannot update "
432 "status to %s for cookie "
435 agent_req_status2name(ARS_SUCCEED),
436 hai->hai_cookie, rc2);
440 /* only remove requests with archive_id=0 */
447 CERROR("%s: Cannot find agent for archive %d: rc = %d\n",
448 mdt_obd_name(mdt), hal->hal_archive_id, rc);
452 CDEBUG(D_HSM, "Agent %s selected for archive %d\n", obd_uuid2str(&uuid),
453 hal->hal_archive_id);
456 buf = kuc_alloc(len, KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
458 RETURN(PTR_ERR(buf));
459 memcpy(buf, hal, len);
461 /* Check if request is still valid (cf file hsm flags) */
462 fail_request = false;
463 hai = hai_first(hal);
464 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
465 struct mdt_object *obj;
468 if (hai->hai_action == HSMA_CANCEL)
471 obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
473 mdt_object_put(mti->mti_env, obj);
474 } else if (PTR_ERR(obj) == -ENOENT) {
475 if (hai->hai_action == HSMA_REMOVE)
479 rc = mdt_agent_record_update(mti->mti_env, mdt,
483 CERROR("%s: mdt_agent_record_update() failed, "
484 "cannot update status to %s for cookie "
487 agent_req_status2name(ARS_FAILED),
488 hai->hai_cookie, rc);
494 GOTO(out_buf, rc = PTR_ERR(obj));
497 if (!mdt_hsm_is_action_compat(hai, hal->hal_archive_id,
498 hal->hal_flags, &hsm)) {
499 /* incompatible request, we abort the request */
500 /* next time coordinator will wake up, it will
501 * make the same compound with valid only
504 rc = mdt_agent_record_update(mti->mti_env, mdt,
508 CERROR("%s: mdt_agent_record_update() failed, "
509 "cannot update status to %s for cookie "
512 agent_req_status2name(ARS_FAILED),
513 hai->hai_cookie, rc);
517 /* if restore and record status updated, give
518 * back granted layout lock */
519 if (hai->hai_action == HSMA_RESTORE) {
520 struct cdt_restore_handle *crh = NULL;
522 mutex_lock(&cdt->cdt_restore_lock);
523 crh = mdt_hsm_restore_hdl_find(cdt,
526 list_del(&crh->crh_list);
527 mutex_unlock(&cdt->cdt_restore_lock);
529 mdt_object_unlock(mti, NULL,
531 OBD_SLAB_FREE_PTR(crh,
538 /* we found incompatible requests, so the compound cannot be send
539 * as is. Bad records have been invalidated in llog.
540 * Valid one will be reschedule next time coordinator will wake up
541 * So no need the rebuild a full valid compound request now
544 GOTO(out_buf, rc = 0);
546 /* Cancel memory registration is useless for purge
547 * non registration avoid a deadlock :
548 * in case of failure we have to take the write lock
549 * to remove entry which conflict with the read loack needed
553 /* set is_registered even if failure because we may have
554 * partial work done */
555 is_registered = true;
556 rc = mdt_hsm_add_hal(mti, hal, &uuid);
561 /* Uses the ldlm reverse import; this rpc will be seen by
562 * the ldlm_callback_handler. Note this sends a request RPC
563 * from a server (MDT) to a client (MDC), backwards of normal comms.
565 exp = cfs_hash_lookup(mdt2obd_dev(mdt)->obd_uuid_hash, &uuid);
566 if (exp == NULL || exp->exp_disconnected) {
568 class_export_put(exp);
569 /* This should clean up agents on evicted exports */
571 CERROR("%s: agent uuid (%s) not found, unregistering:"
573 mdt_obd_name(mdt), obd_uuid2str(&uuid), rc);
574 mdt_hsm_agent_unregister(mti, &uuid);
578 /* send request to agent */
579 rc = do_set_info_async(exp->exp_imp_reverse, LDLM_SET_INFO,
581 sizeof(KEY_HSM_COPYTOOL_SEND),
582 KEY_HSM_COPYTOOL_SEND,
583 kuc_len(len), kuc_ptr(buf), NULL);
586 CERROR("%s: cannot send request to agent '%s': rc = %d\n",
587 mdt_obd_name(mdt), obd_uuid2str(&uuid), rc);
589 class_export_put(exp);
592 CDEBUG(D_HSM, "Lost connection to agent '%s', unregistering\n",
593 obd_uuid2str(&uuid));
594 mdt_hsm_agent_unregister(mti, &uuid);
598 if (rc != 0 && is_registered) {
599 /* in case of error, we have to unregister requests */
600 hai = hai_first(hal);
601 for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
602 if (hai->hai_action == HSMA_CANCEL)
604 mdt_cdt_remove_request(cdt, hai->hai_cookie);
615 * update status of a request
617 * \param pgs [IN] progress of the copy tool
619 * \retval -ve failure
621 int mdt_hsm_coordinator_update(struct mdt_thread_info *mti,
622 struct hsm_progress_kernel *pgs)
627 /* ask to coordinator to update request state and
628 * to record on disk the result */
629 rc = mdt_hsm_update_request_state(mti, pgs, 1);
634 * seq_file method called to start access to /proc file
636 static void *mdt_hsm_agent_proc_start(struct seq_file *s, loff_t *off)
638 struct mdt_device *mdt = s->private;
639 struct coordinator *cdt = &mdt->mdt_coordinator;
640 struct list_head *pos;
644 down_read(&cdt->cdt_agent_lock);
646 if (list_empty(&cdt->cdt_agents))
650 RETURN(SEQ_START_TOKEN);
653 list_for_each(pos, &cdt->cdt_agents) {
663 * seq_file method called to get next item
664 * just returns NULL at eof
666 static void *mdt_hsm_agent_proc_next(struct seq_file *s, void *v, loff_t *p)
668 struct mdt_device *mdt = s->private;
669 struct coordinator *cdt = &mdt->mdt_coordinator;
670 struct list_head *pos = v;
673 if (pos == SEQ_START_TOKEN)
674 pos = cdt->cdt_agents.next;
679 if (pos != &cdt->cdt_agents)
687 static int mdt_hsm_agent_proc_show(struct seq_file *s, void *v)
689 struct list_head *pos = v;
690 struct hsm_agent *ha;
694 if (pos == SEQ_START_TOKEN)
697 ha = list_entry(pos, struct hsm_agent, ha_list);
698 seq_printf(s, "uuid=%s archive_id=", ha->ha_uuid.uuid);
699 if (ha->ha_archive_cnt == 0) {
700 seq_printf(s, "ANY");
702 seq_printf(s, "%d", ha->ha_archive_id[0]);
703 for (i = 1; i < ha->ha_archive_cnt; i++)
704 seq_printf(s, ",%d", ha->ha_archive_id[i]);
707 seq_printf(s, " requests=[current:%d ok:%d errors:%d]\n",
708 atomic_read(&ha->ha_requests),
709 atomic_read(&ha->ha_success),
710 atomic_read(&ha->ha_failure));
715 * seq_file method called to stop access to /proc file
717 static void mdt_hsm_agent_proc_stop(struct seq_file *s, void *v)
719 struct mdt_device *mdt = s->private;
720 struct coordinator *cdt = &mdt->mdt_coordinator;
722 up_read(&cdt->cdt_agent_lock);
725 /* hsm agent list proc functions */
726 static const struct seq_operations mdt_hsm_agent_proc_ops = {
727 .start = mdt_hsm_agent_proc_start,
728 .next = mdt_hsm_agent_proc_next,
729 .show = mdt_hsm_agent_proc_show,
730 .stop = mdt_hsm_agent_proc_stop,
734 * public function called at open of /proc file to get
737 static int lprocfs_open_hsm_agent(struct inode *inode, struct file *file)
743 rc = seq_open(file, &mdt_hsm_agent_proc_ops);
747 s = file->private_data;
748 s->private = PDE_DATA(inode);
753 /* methods to access hsm agent list */
754 const struct file_operations mdt_hsm_agent_fops = {
755 .owner = THIS_MODULE,
756 .open = lprocfs_open_hsm_agent,
759 .release = lprocfs_seq_release,