4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
9 * All rights reserved. This program and the accompanying materials
10 * are made available under the terms of the GNU Lesser General Public License
11 * (LGPL) version 2.1 or (at your discretion) any later version.
12 * (LGPL) version 2.1 accompanies this distribution, and is available at
13 * http://www.gnu.org/licenses/lgpl-2.1.html
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
23 * lustre/utils/liblustreapi_hsm.c
25 * lustreapi library for hsm calls
27 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
28 * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
29 * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
30 * Author: Henri Doreau <henri.doreau@cea.fr>
37 #include <sys/ioctl.h>
44 #include <sys/types.h>
46 #include <sys/syscall.h>
50 #ifdef HAVE_LINUX_UNISTD_H
51 #include <linux/unistd.h>
56 #include <liblustre.h>
57 #include <lnet/lnetctl.h>
60 #include <lustre/lustreapi.h>
61 #include "lustreapi_internal.h"
63 #define OPEN_BY_FID_PATH dot_lustre_name"/fid"
65 /****** HSM Copytool API ********/
66 #define CT_PRIV_MAGIC 0xC0BE2001
67 struct hsm_copytool_private {
73 lustre_kernelcomm kuc;
77 #define CP_PRIV_MAGIC 0x19880429
78 struct hsm_copyaction_private {
81 const struct hsm_copytool_private *ct_priv;
86 #include <libcfs/libcfs.h>
88 enum ct_progress_type {
99 CT_ARCHIVE_START = HSMA_ARCHIVE,
100 CT_ARCHIVE_RUNNING = HSMA_ARCHIVE + CT_RUNNING,
101 CT_ARCHIVE_FINISH = HSMA_ARCHIVE + CT_FINISH,
102 CT_ARCHIVE_CANCEL = HSMA_ARCHIVE + CT_CANCEL,
103 CT_ARCHIVE_ERROR = HSMA_ARCHIVE + CT_ERROR,
104 CT_RESTORE_START = HSMA_RESTORE,
105 CT_RESTORE_RUNNING = HSMA_RESTORE + CT_RUNNING,
106 CT_RESTORE_FINISH = HSMA_RESTORE + CT_FINISH,
107 CT_RESTORE_CANCEL = HSMA_RESTORE + CT_CANCEL,
108 CT_RESTORE_ERROR = HSMA_RESTORE + CT_ERROR,
109 CT_REMOVE_START = HSMA_REMOVE,
110 CT_REMOVE_RUNNING = HSMA_REMOVE + CT_RUNNING,
111 CT_REMOVE_FINISH = HSMA_REMOVE + CT_FINISH,
112 CT_REMOVE_CANCEL = HSMA_REMOVE + CT_CANCEL,
113 CT_REMOVE_ERROR = HSMA_REMOVE + CT_ERROR,
117 /* initialized in llapi_hsm_register_event_fifo() */
118 static int llapi_hsm_event_fd = -1;
119 static bool created_hsm_event_fifo;
121 static inline const char *llapi_hsm_ct_ev2str(int type)
128 case CT_ARCHIVE_START:
129 return "ARCHIVE_START";
130 case CT_ARCHIVE_RUNNING:
131 return "ARCHIVE_RUNNING";
132 case CT_ARCHIVE_FINISH:
133 return "ARCHIVE_FINISH";
134 case CT_ARCHIVE_CANCEL:
135 return "ARCHIVE_CANCEL";
136 case CT_ARCHIVE_ERROR:
137 return "ARCHIVE_ERROR";
138 case CT_RESTORE_START:
139 return "RESTORE_START";
140 case CT_RESTORE_RUNNING:
141 return "RESTORE_RUNNING";
142 case CT_RESTORE_FINISH:
143 return "RESTORE_FINISH";
144 case CT_RESTORE_CANCEL:
145 return "RESTORE_CANCEL";
146 case CT_RESTORE_ERROR:
147 return "RESTORE_ERROR";
148 case CT_REMOVE_START:
149 return "REMOVE_START";
150 case CT_REMOVE_RUNNING:
151 return "REMOVE_RUNNING";
152 case CT_REMOVE_FINISH:
153 return "REMOVE_FINISH";
154 case CT_REMOVE_CANCEL:
155 return "REMOVE_CANCEL";
156 case CT_REMOVE_ERROR:
157 return "REMOVE_ERROR";
159 llapi_err_noerrno(LLAPI_MSG_ERROR,
160 "Unknown event type: %d", type);
166 * Writes a JSON event to the monitor FIFO. Noop if no FIFO has been
169 * \param event A list of llapi_json_items comprising a
170 * single JSON-formatted event.
172 * \retval 0 on success.
173 * \retval -errno on error.
175 static int llapi_hsm_write_json_event(struct llapi_json_item_list **event)
178 char time_string[40];
179 char json_buf[PIPE_BUF];
181 time_t event_time = time(0);
182 struct tm time_components;
183 struct llapi_json_item_list *json_items;
185 /* Noop unless the event fd was initialized */
186 if (llapi_hsm_event_fd < 0)
189 if (event == NULL || *event == NULL)
194 localtime_r(&event_time, &time_components);
196 if (strftime(time_string, sizeof(time_string), "%Y-%m-%d %T %z",
197 &time_components) == 0) {
199 llapi_error(LLAPI_MSG_ERROR, rc, "strftime() failed");
203 rc = llapi_json_add_item(&json_items, "event_time", LLAPI_JSON_STRING,
206 llapi_error(LLAPI_MSG_ERROR, -rc, "error in "
207 "llapi_json_add_item()");
211 buf_file = fmemopen(json_buf, sizeof(json_buf), "w");
212 if (buf_file == NULL)
215 rc = llapi_json_write_list(event, buf_file);
223 if (write(llapi_hsm_event_fd, json_buf, strlen(json_buf)) < 0) {
224 /* Ignore write failures due to missing reader. */
233 * Hook for llapi_hsm_copytool_register and llapi_hsm_copytool_unregister
234 * to generate JSON events suitable for consumption by a copytool
235 * monitoring process.
237 * \param priv Opaque private control structure.
238 * \param event_type The type of event (register or unregister).
240 * \retval 0 on success.
241 * \retval -errno on error.
243 static int llapi_hsm_log_ct_registration(struct hsm_copytool_private **priv,
247 char agent_uuid[UUID_MAX];
248 struct hsm_copytool_private *ct;
249 struct llapi_json_item_list *json_items;
251 /* Noop unless the event fd was initialized */
252 if (llapi_hsm_event_fd < 0)
255 if (priv == NULL || *priv == NULL)
259 if (ct->magic != CT_PRIV_MAGIC)
262 if (event_type != CT_REGISTER && event_type != CT_UNREGISTER)
265 rc = llapi_json_init_list(&json_items);
269 rc = llapi_get_agent_uuid(ct->mnt, agent_uuid, sizeof(agent_uuid));
272 llapi_chomp_string(agent_uuid);
274 rc = llapi_json_add_item(&json_items, "uuid", LLAPI_JSON_STRING,
279 rc = llapi_json_add_item(&json_items, "mount_point", LLAPI_JSON_STRING,
284 rc = llapi_json_add_item(&json_items, "archive", LLAPI_JSON_INTEGER,
289 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
290 (char *)llapi_hsm_ct_ev2str(event_type));
294 rc = llapi_hsm_write_json_event(&json_items);
301 llapi_error(LLAPI_MSG_ERROR, rc, "error in "
302 "llapi_hsm_log_ct_registration()");
305 if (json_items != NULL)
306 llapi_json_destroy_list(&json_items);
312 * Given a copytool progress update, construct a JSON event suitable for
313 * consumption by a copytool monitoring process.
315 * Examples of various events generated here and written by
316 * llapi_hsm_write_json_event:
318 * Copytool registration and deregistration:
319 * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "REGISTER", "archive": 0, "mount_point": "/mnt/lustre", "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
320 * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "UNREGISTER", "archive": 0, "mount_point": "/mnt/lustre", "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
322 * An archive action, start to completion:
323 * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "ARCHIVE_START", "total_bytes": 0, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
324 * {"event_time": "2014-02-26 14:50:18 -0500", "event_type": "ARCHIVE_RUNNING", "current_bytes": 5242880, "total_bytes": 39000000, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
325 * {"event_time": "2014-02-26 14:50:50 -0500", "event_type": "ARCHIVE_FINISH", "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
328 * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "LOGGED_MESSAGE", "level": "INFO", "message": "lhsmtool_posix[59401]: copytool fs=lustre archive#=2 item_count=1"}
330 * \param hcp Opaque action handle returned by
331 * llapi_hsm_action_start.
332 * \param hai The hsm_action_item describing the request.
333 * \param progress_type The ct_progress_type describing the update.
334 * \param total The total expected bytes for the request.
335 * \param current The current copied byte count for the request.
337 * \retval 0 on success.
338 * \retval -errno on error.
340 static int llapi_hsm_log_ct_progress(struct hsm_copyaction_private **phcp,
341 const struct hsm_action_item *hai,
343 __u64 total, __u64 current)
347 long long recno = -1;
348 char lustre_path[PATH_MAX];
349 char strfid[FID_NOBRACE_LEN + 1];
350 struct hsm_copyaction_private *hcp;
351 struct llapi_json_item_list *json_items;
353 /* Noop unless the event fd was initialized */
354 if (llapi_hsm_event_fd < 0)
357 if (phcp == NULL || *phcp == NULL)
362 rc = llapi_json_init_list(&json_items);
366 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_dfid));
367 rc = llapi_json_add_item(&json_items, "data_fid",
368 LLAPI_JSON_STRING, strfid);
372 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_fid));
373 rc = llapi_json_add_item(&json_items, "source_fid",
374 LLAPI_JSON_STRING, strfid);
378 if (hcp->copy.hc_errval == ECANCELED) {
379 progress_type = CT_CANCEL;
383 if (hcp->copy.hc_errval != 0) {
384 progress_type = CT_ERROR;
386 rc = llapi_json_add_item(&json_items, "errno",
388 &hcp->copy.hc_errval);
392 rc = llapi_json_add_item(&json_items, "error",
394 strerror(hcp->copy.hc_errval));
401 /* lustre_path isn't available after a restore completes */
402 /* total_bytes isn't available after a restore or archive completes */
403 if (progress_type != CT_FINISH) {
404 rc = llapi_fid2path(hcp->ct_priv->mnt, strfid, lustre_path,
405 sizeof(lustre_path), &recno, &linkno);
409 rc = llapi_json_add_item(&json_items, "lustre_path",
410 LLAPI_JSON_STRING, lustre_path);
414 rc = llapi_json_add_item(&json_items, "total_bytes",
415 LLAPI_JSON_BIGNUM, &total);
420 if (progress_type == CT_RUNNING)
421 rc = llapi_json_add_item(&json_items, "current_bytes",
422 LLAPI_JSON_BIGNUM, ¤t);
427 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
428 (char *)llapi_hsm_ct_ev2str(hai->hai_action +
433 rc = llapi_hsm_write_json_event(&json_items);
440 llapi_error(LLAPI_MSG_ERROR, rc, "error in "
441 "llapi_hsm_log_ct_progress()");
444 if (json_items != NULL)
445 llapi_json_destroy_list(&json_items);
451 * Given a path to a FIFO, create a filehandle for nonblocking writes to it.
452 * Intended to be used for copytool monitoring processes that read an
453 * event stream from the FIFO. Events written in the absence of a reader
456 * \param path Path to monitor FIFO.
458 * \retval 0 on success.
459 * \retval -errno on error.
461 int llapi_hsm_register_event_fifo(const char *path)
466 /* Create the FIFO if necessary. */
467 if ((mkfifo(path, 0644) < 0) && (errno != EEXIST)) {
468 llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed", path);
471 if (errno == EEXIST) {
472 if (stat(path, &statbuf) < 0) {
473 llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed",
477 if (!S_ISFIFO(statbuf.st_mode) ||
478 ((statbuf.st_mode & 0777) != 0644)) {
479 llapi_error(LLAPI_MSG_ERROR, errno, "%s exists but is "
480 "not a pipe or has a wrong mode", path);
484 created_hsm_event_fifo = true;
487 /* Open the FIFO for read so that the subsequent open for write
488 * doesn't immediately fail. */
489 read_fd = open(path, O_RDONLY | O_NONBLOCK);
491 llapi_error(LLAPI_MSG_ERROR, errno,
492 "cannot open(%s) for read", path);
496 /* Open the FIFO for writes, but don't block on waiting
498 llapi_hsm_event_fd = open(path, O_WRONLY | O_NONBLOCK);
499 if (llapi_hsm_event_fd < 0) {
500 llapi_error(LLAPI_MSG_ERROR, errno,
501 "cannot open(%s) for write", path);
505 /* Now close the reader. An external monitoring process can
506 * now open the FIFO for reads. If no reader comes along the
507 * events are lost. NOTE: Only one reader at a time! */
510 /* Ignore SIGPIPEs -- can occur if the reader goes away. */
511 signal(SIGPIPE, SIG_IGN);
517 * Given a path to a FIFO, close its filehandle and delete the FIFO.
519 * \param path Path to monitor FIFO.
521 * \retval 0 on success.
522 * \retval -errno on error.
524 int llapi_hsm_unregister_event_fifo(const char *path)
526 /* Noop unless the event fd was initialized */
527 if (llapi_hsm_event_fd < 0)
530 if (close(llapi_hsm_event_fd) < 0)
533 if (created_hsm_event_fifo) {
535 created_hsm_event_fifo = false;
538 llapi_hsm_event_fd = -1;
544 * Custom logging callback to be used when a monitoring FIFO has been
545 * registered. Formats log entries as JSON events suitable for
546 * consumption by a copytool monitoring process.
548 * \param level The message loglevel.
549 * \param _rc The returncode associated with the message.
550 * \param fmt The message format string.
551 * \param args Arguments to be formatted by the format string.
555 void llapi_hsm_log_error(enum llapi_message_level level, int _rc,
556 const char *fmt, va_list args)
563 struct llapi_json_item_list *json_items;
565 /* Noop unless the event fd was initialized */
566 if (llapi_hsm_event_fd < 0)
569 rc = llapi_json_init_list(&json_items);
573 if ((level & LLAPI_MSG_NO_ERRNO) == 0) {
574 rc = llapi_json_add_item(&json_items, "errno",
580 rc = llapi_json_add_item(&json_items, "error",
587 va_copy(args2, args);
588 msg_len = vsnprintf(NULL, 0, fmt, args2) + 1;
591 msg = (char *) alloca(msg_len);
597 rc = vsnprintf(msg, msg_len, fmt, args);
601 rc = llapi_json_add_item(&json_items, "message",
607 rc = llapi_json_add_item(&json_items, "message",
609 "INTERNAL ERROR: message failed");
614 real_level = level & LLAPI_MSG_NO_ERRNO;
615 real_level = real_level > 0 ? level - LLAPI_MSG_NO_ERRNO : level;
617 rc = llapi_json_add_item(&json_items, "level", LLAPI_JSON_STRING,
618 (void *)llapi_msg_level2str(real_level));
622 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
627 rc = llapi_hsm_write_json_event(&json_items);
634 /* Write directly to stderr to avoid llapi_error, which now
635 * emits JSON event messages. */
636 fprintf(stderr, "\nFATAL ERROR IN llapi_hsm_log_error(): rc %d,", rc);
639 if (json_items != NULL)
640 llapi_json_destroy_list(&json_items);
645 /** Register a copytool
646 * \param[out] priv Opaque private control structure
647 * \param mnt Lustre filesystem mount point
648 * \param archive_count
649 * \param archives Which archive numbers this copytool is responsible for
650 * \param rfd_flags flags applied to read fd of pipe (e.g. O_NONBLOCK)
652 int llapi_hsm_copytool_register(struct hsm_copytool_private **priv,
653 const char *mnt, int archive_count,
654 int *archives, int rfd_flags)
656 struct hsm_copytool_private *ct;
659 if (archive_count > 0 && archives == NULL) {
660 llapi_err_noerrno(LLAPI_MSG_ERROR,
661 "NULL archive numbers");
665 ct = calloc(1, sizeof(*ct));
669 ct->magic = CT_PRIV_MAGIC;
671 ct->open_by_fid_fd = -1;
672 ct->kuc.lk_rfd = LK_NOFD;
673 ct->kuc.lk_wfd = LK_NOFD;
675 ct->mnt = strdup(mnt);
676 if (ct->mnt == NULL) {
681 ct->kuch = malloc(HAL_MAXSIZE + sizeof(*ct->kuch));
682 if (ct->kuch == NULL) {
687 ct->mnt_fd = open(ct->mnt, O_RDONLY);
688 if (ct->mnt_fd < 0) {
693 ct->open_by_fid_fd = openat(ct->mnt_fd, OPEN_BY_FID_PATH, O_RDONLY);
694 if (ct->open_by_fid_fd < 0) {
699 /* no archives specified means "match all". */
701 for (rc = 0; rc < archive_count; rc++) {
702 if (archives[rc] > 8 * sizeof(ct->archives)) {
703 llapi_err_noerrno(LLAPI_MSG_ERROR,
704 "maximum of %zu archives supported",
705 8 * sizeof(ct->archives));
708 /* in the list we have a all archive wildcard
709 * so move to all archives mode
711 if (archives[rc] == 0) {
716 ct->archives |= (1 << (archives[rc] - 1));
719 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM, rfd_flags);
723 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
724 ct->kuc.lk_data = ct->archives;
725 rc = ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, &ct->kuc);
728 llapi_error(LLAPI_MSG_ERROR, rc,
729 "cannot start copytool on '%s'", mnt);
735 llapi_hsm_log_ct_registration(&ct, CT_REGISTER);
737 /* Only the kernel reference keeps the write side open */
738 close(ct->kuc.lk_wfd);
739 ct->kuc.lk_wfd = LK_NOFD;
747 /* cleanup the kuc channel */
748 libcfs_ukuc_stop(&ct->kuc);
751 if (!(ct->mnt_fd < 0))
754 if (!(ct->open_by_fid_fd < 0))
755 close(ct->open_by_fid_fd);
766 /** Deregister a copytool
767 * Note: under Linux, until llapi_hsm_copytool_unregister is called
768 * (or the program is killed), the libcfs module will be referenced
769 * and unremovable, even after Lustre services stop.
771 int llapi_hsm_copytool_unregister(struct hsm_copytool_private **priv)
773 struct hsm_copytool_private *ct;
775 if (priv == NULL || *priv == NULL)
779 if (ct->magic != CT_PRIV_MAGIC)
782 /* Tell the kernel to stop sending us messages */
783 ct->kuc.lk_flags = LK_FLG_STOP;
784 ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, &ct->kuc);
786 /* Shut down the kernelcomms */
787 libcfs_ukuc_stop(&ct->kuc);
789 llapi_hsm_log_ct_registration(&ct, CT_UNREGISTER);
791 close(ct->open_by_fid_fd);
801 /** Returns a file descriptor to poll/select on.
802 * \param ct Opaque private control structure
803 * \retval -EINVAL on error
804 * \retval the file descriptor for reading HSM events from the kernel
806 int llapi_hsm_copytool_get_fd(struct hsm_copytool_private *ct)
808 if (ct == NULL || ct->magic != CT_PRIV_MAGIC)
811 return libcfs_ukuc_get_rfd(&ct->kuc);
814 /** Wait for the next hsm_action_list
815 * \param ct Opaque private control structure
816 * \param halh Action list handle, will be allocated here
817 * \param msgsize Number of bytes in the message, will be set here
818 * \return 0 valid message received; halh and msgsize are set
820 * Note: The application must not call llapi_hsm_copytool_recv until it has
821 * cleared the data in ct->kuch from the previous call.
823 int llapi_hsm_copytool_recv(struct hsm_copytool_private *ct,
824 struct hsm_action_list **halh, int *msgsize)
826 struct kuc_hdr *kuch;
827 struct hsm_action_list *hal;
830 if (ct == NULL || ct->magic != CT_PRIV_MAGIC)
833 if (halh == NULL || msgsize == NULL)
839 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
840 HAL_MAXSIZE + sizeof(*kuch),
845 /* Handle generic messages */
846 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
847 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
852 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
853 kuch->kuc_msgtype != HMT_ACTION_LIST) {
854 llapi_err_noerrno(LLAPI_MSG_ERROR,
855 "Unknown HSM message type %d:%d\n",
856 kuch->kuc_transport, kuch->kuc_msgtype);
861 if (kuch->kuc_msglen < sizeof(*kuch) + sizeof(*hal)) {
862 llapi_err_noerrno(LLAPI_MSG_ERROR, "Short HSM message %d",
868 /* Our message is a hsm_action_list. Use pointer math to skip
869 * kuch_hdr and point directly to the message payload.
871 hal = (struct hsm_action_list *)(kuch + 1);
873 /* Check that we have registered for this archive #
874 * if 0 registered, we serve any archive */
876 ((1 << (hal->hal_archive_id - 1)) & ct->archives) == 0) {
877 llapi_err_noerrno(LLAPI_MSG_INFO,
878 "This copytool does not service archive #%d,"
879 " ignoring this request."
880 " Mask of served archive is 0x%.8X",
881 hal->hal_archive_id, ct->archives);
887 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
896 /** Get parent path from mount point and fid.
898 * \param mnt Filesystem root path.
899 * \param fid Object FID.
900 * \param parent Destination buffer.
901 * \param parent_len Destination buffer size.
902 * \return 0 on success.
904 static int fid_parent(const char *mnt, const lustre_fid *fid, char *parent,
909 long long recno = -1;
911 char strfid[FID_NOBRACE_LEN + 1];
914 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(fid));
916 rc = llapi_fid2path(mnt, strfid, file, sizeof(file),
921 /* fid2path returns a relative path */
922 rc = snprintf(parent, parent_len, "%s/%s", mnt, file);
923 if (rc >= parent_len)
924 return -ENAMETOOLONG;
926 /* remove file name */
927 ptr = strrchr(parent, '/');
928 if (ptr == NULL || ptr == parent) {
938 static int ct_open_by_fid(const struct hsm_copytool_private *ct,
939 const struct lu_fid *fid, int open_flags)
941 char fid_name[FID_NOBRACE_LEN + 1];
944 snprintf(fid_name, sizeof(fid_name), DFID_NOBRACE, PFID(fid));
946 fd = openat(ct->open_by_fid_fd, fid_name, open_flags);
947 return fd < 0 ? -errno : fd;
950 static int ct_stat_by_fid(const struct hsm_copytool_private *ct,
951 const struct lu_fid *fid,
954 char fid_name[FID_NOBRACE_LEN + 1];
957 snprintf(fid_name, sizeof(fid_name), DFID_NOBRACE, PFID(fid));
959 rc = fstatat(ct->open_by_fid_fd, fid_name, buf, 0);
960 return rc ? -errno : 0;
963 /** Create the destination volatile file for a restore operation.
965 * \param hcp Private copyaction handle.
966 * \param mdt_index MDT index where to create the volatile file.
967 * \param flags Volatile file creation flags.
968 * \return 0 on success.
970 static int create_restore_volatile(struct hsm_copyaction_private *hcp,
971 int mdt_index, int open_flags)
975 char parent[PATH_MAX + 1];
976 const char *mnt = hcp->ct_priv->mnt;
977 struct hsm_action_item *hai = &hcp->copy.hc_hai;
979 rc = fid_parent(mnt, &hai->hai_fid, parent, sizeof(parent));
981 /* fid_parent() failed, try to keep on going */
982 llapi_error(LLAPI_MSG_ERROR, rc,
983 "cannot get parent path to restore "DFID" "
984 "using '%s'", PFID(&hai->hai_fid), mnt);
985 snprintf(parent, sizeof(parent), "%s", mnt);
988 fd = llapi_create_volatile_idx(parent, mdt_index, open_flags);
992 rc = fchown(fd, hcp->stat.st_uid, hcp->stat.st_gid);
996 rc = llapi_fd2fid(fd, &hai->hai_dfid);
1011 /** Start processing an HSM action.
1012 * Should be called by copytools just before starting handling a request.
1013 * It could be skipped if copytool only want to directly report an error,
1014 * \see llapi_hsm_action_end().
1016 * \param hcp Opaque action handle to be passed to
1017 * llapi_hsm_action_progress and llapi_hsm_action_end.
1018 * \param ct Copytool handle acquired at registration.
1019 * \param hai The hsm_action_item describing the request.
1020 * \param restore_mdt_index On restore: MDT index where to create the volatile
1021 * file. Use -1 for default.
1022 * \param restore_open_flags On restore: volatile file creation mode. Use
1023 * O_LOV_DELAY_CREATE to manually set the LOVEA
1025 * \param is_error Whether this call is just to report an error.
1027 * \return 0 on success.
1029 int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
1030 const struct hsm_copytool_private *ct,
1031 const struct hsm_action_item *hai,
1032 int restore_mdt_index, int restore_open_flags,
1035 struct hsm_copyaction_private *hcp;
1038 hcp = calloc(1, sizeof(*hcp));
1044 hcp->copy.hc_hai = *hai;
1045 hcp->copy.hc_hai.hai_len = sizeof(*hai);
1050 if (hai->hai_action == HSMA_RESTORE) {
1051 rc = ct_stat_by_fid(hcp->ct_priv, &hai->hai_fid, &hcp->stat);
1055 rc = create_restore_volatile(hcp, restore_mdt_index,
1056 restore_open_flags);
1061 rc = ioctl(ct->mnt_fd, LL_IOC_HSM_COPY_START, &hcp->copy);
1067 llapi_hsm_log_ct_progress(&hcp, hai, CT_START, 0, 0);
1070 hcp->magic = CP_PRIV_MAGIC;
1075 if (!(hcp->data_fd < 0))
1076 close(hcp->data_fd);
1083 /** Terminate an HSM action processing.
1084 * Should be called by copytools just having finished handling the request.
1085 * \param hdl[in,out] Handle returned by llapi_hsm_action_start.
1086 * \param he[in] The final range of copied data (for copy actions).
1087 * \param errval[in] The status code of the operation.
1088 * \param flags[in] The flags about the termination status (HP_FLAG_RETRY if
1089 * the error is retryable).
1091 * \return 0 on success.
1093 int llapi_hsm_action_end(struct hsm_copyaction_private **phcp,
1094 const struct hsm_extent *he, int hp_flags, int errval)
1096 struct hsm_copyaction_private *hcp;
1097 struct hsm_action_item *hai;
1100 if (phcp == NULL || *phcp == NULL || he == NULL)
1105 if (hcp->magic != CP_PRIV_MAGIC)
1108 hai = &hcp->copy.hc_hai;
1110 if (hai->hai_action == HSMA_RESTORE && errval == 0) {
1111 struct timeval tv[2];
1113 /* Set {a,m}time of volatile file to that of original. */
1114 tv[0].tv_sec = hcp->stat.st_atime;
1116 tv[1].tv_sec = hcp->stat.st_mtime;
1118 if (futimes(hcp->data_fd, tv) < 0) {
1123 rc = fsync(hcp->data_fd);
1131 /* In some cases, like restore, 2 FIDs are used.
1132 * Set the right FID to use here. */
1133 if (hai->hai_action == HSMA_ARCHIVE || hai->hai_action == HSMA_RESTORE)
1134 hai->hai_fid = hai->hai_dfid;
1136 /* Fill the last missing data that will be needed by
1137 * kernel to send a hsm_progress. */
1138 hcp->copy.hc_flags = hp_flags;
1139 hcp->copy.hc_errval = abs(errval);
1141 hcp->copy.hc_hai.hai_extent = *he;
1143 rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_COPY_END, &hcp->copy);
1149 llapi_hsm_log_ct_progress(&hcp, hai, CT_FINISH, 0, 0);
1152 if (!(hcp->data_fd < 0))
1153 close(hcp->data_fd);
1161 /** Notify a progress in processing an HSM action.
1162 * \param hdl[in,out] handle returned by llapi_hsm_action_start.
1163 * \param he[in] the range of copied data (for copy actions).
1164 * \param total[in] the expected total of copied data (for copy actions).
1165 * \param hp_flags[in] HSM progress flags.
1166 * \return 0 on success.
1168 int llapi_hsm_action_progress(struct hsm_copyaction_private *hcp,
1169 const struct hsm_extent *he, __u64 total,
1173 struct hsm_progress hp;
1174 struct hsm_action_item *hai;
1176 if (hcp == NULL || he == NULL)
1179 if (hcp->magic != CP_PRIV_MAGIC)
1182 hai = &hcp->copy.hc_hai;
1184 memset(&hp, 0, sizeof(hp));
1186 hp.hp_cookie = hai->hai_cookie;
1187 hp.hp_flags = hp_flags;
1189 /* Progress is made on the data fid */
1190 hp.hp_fid = hai->hai_dfid;
1193 rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_PROGRESS, &hp);
1197 llapi_hsm_log_ct_progress(&hcp, hai, CT_RUNNING, total, he->length);
1202 /** Get the fid of object to be used for copying data.
1203 * @return error code if the action is not a copy operation.
1205 int llapi_hsm_action_get_dfid(const struct hsm_copyaction_private *hcp,
1208 const struct hsm_action_item *hai = &hcp->copy.hc_hai;
1210 if (hcp->magic != CP_PRIV_MAGIC)
1213 if (hai->hai_action != HSMA_RESTORE && hai->hai_action != HSMA_ARCHIVE)
1216 *fid = hai->hai_dfid;
1222 * Get a file descriptor to be used for copying data. It's up to the
1223 * caller to close the FDs obtained from this function.
1225 * @retval a file descriptor on success.
1226 * @retval a negative error code on failure.
1228 int llapi_hsm_action_get_fd(const struct hsm_copyaction_private *hcp)
1230 const struct hsm_action_item *hai = &hcp->copy.hc_hai;
1233 if (hcp->magic != CP_PRIV_MAGIC)
1236 if (hai->hai_action == HSMA_ARCHIVE) {
1237 return ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
1238 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
1239 } else if (hai->hai_action == HSMA_RESTORE) {
1240 fd = dup(hcp->data_fd);
1241 return fd < 0 ? -errno : fd;
1248 * Import an existing hsm-archived file into Lustre.
1250 * Caller must access file by (returned) newfid value from now on.
1252 * \param dst path to Lustre destination (e.g. /mnt/lustre/my/file).
1253 * \param archive archive number.
1254 * \param st struct stat buffer containing file ownership, perm, etc.
1255 * \param stripe_* Striping options. Currently ignored, since the restore
1256 * operation will set the striping. In V2, this striping might
1258 * \param newfid[out] Filled with new Lustre fid.
1260 int llapi_hsm_import(const char *dst, int archive, const struct stat *st,
1261 unsigned long long stripe_size, int stripe_offset,
1262 int stripe_count, int stripe_pattern, char *pool_name,
1265 struct hsm_user_import hui;
1269 if (stripe_pattern == 0)
1270 stripe_pattern = LOV_PATTERN_RAID0;
1272 /* Create a non-striped file */
1273 fd = llapi_file_open_pool(dst, O_CREAT | O_WRONLY, st->st_mode,
1274 stripe_size, stripe_offset, stripe_count,
1275 stripe_pattern | LOV_PATTERN_F_RELEASED,
1278 llapi_error(LLAPI_MSG_ERROR, fd,
1279 "cannot create '%s' for import", dst);
1283 /* Get the new fid in Lustre. Caller needs to use this fid
1285 rc = llapi_fd2fid(fd, newfid);
1287 llapi_error(LLAPI_MSG_ERROR, rc,
1288 "cannot get fid of '%s' for import", dst);
1292 hui.hui_uid = st->st_uid;
1293 hui.hui_gid = st->st_gid;
1294 hui.hui_mode = st->st_mode;
1295 hui.hui_size = st->st_size;
1296 hui.hui_archive_id = archive;
1297 hui.hui_atime = st->st_atime;
1298 hui.hui_atime_ns = st->st_atim.tv_nsec;
1299 hui.hui_mtime = st->st_mtime;
1300 hui.hui_mtime_ns = st->st_mtim.tv_nsec;
1301 rc = ioctl(fd, LL_IOC_HSM_IMPORT, &hui);
1304 llapi_error(LLAPI_MSG_ERROR, rc, "cannot import '%s'", dst);
1317 * Return the current HSM states and HSM requests related to file pointed by \a
1320 * \param hus Should be allocated by caller. Will be filled with current file
1323 * \retval 0 on success.
1324 * \retval -errno on error.
1326 int llapi_hsm_state_get_fd(int fd, struct hsm_user_state *hus)
1330 rc = ioctl(fd, LL_IOC_HSM_STATE_GET, hus);
1331 /* If error, save errno value */
1332 rc = rc ? -errno : 0;
1338 * Return the current HSM states and HSM requests related to file pointed by \a
1341 * see llapi_hsm_state_get_fd() for args use and return
1343 int llapi_hsm_state_get(const char *path, struct hsm_user_state *hus)
1348 fd = open(path, O_RDONLY | O_NONBLOCK);
1352 rc = llapi_hsm_state_get_fd(fd, hus);
1359 * Set HSM states of file pointed by \a fd
1361 * Using the provided bitmasks, the current HSM states for this file will be
1362 * changed. \a archive_id could be used to change the archive number also. Set
1363 * it to 0 if you do not want to change it.
1365 * \param setmask Bitmask for flag to be set.
1366 * \param clearmask Bitmask for flag to be cleared.
1367 * \param archive_id Archive number identifier to use. 0 means no change.
1369 * \retval 0 on success.
1370 * \retval -errno on error.
1372 int llapi_hsm_state_set_fd(int fd, __u64 setmask, __u64 clearmask,
1375 struct hsm_state_set hss;
1378 hss.hss_valid = HSS_SETMASK|HSS_CLEARMASK;
1379 hss.hss_setmask = setmask;
1380 hss.hss_clearmask = clearmask;
1381 /* Change archive_id if provided. We can only change
1382 * to set something different than 0. */
1383 if (archive_id > 0) {
1384 hss.hss_valid |= HSS_ARCHIVE_ID;
1385 hss.hss_archive_id = archive_id;
1387 rc = ioctl(fd, LL_IOC_HSM_STATE_SET, &hss);
1388 /* If error, save errno value */
1389 rc = rc ? -errno : 0;
1395 * Set HSM states of file pointed by \a path.
1397 * see llapi_hsm_state_set_fd() for args use and return
1399 int llapi_hsm_state_set(const char *path, __u64 setmask, __u64 clearmask,
1405 fd = open(path, O_WRONLY | O_LOV_DELAY_CREATE | O_NONBLOCK);
1409 rc = llapi_hsm_state_set_fd(fd, setmask, clearmask, archive_id);
1416 * Return the current HSM request related to file pointed by \a path.
1418 * \param hca Should be allocated by caller. Will be filled with current file
1421 * \retval 0 on success.
1422 * \retval -errno on error.
1424 int llapi_hsm_current_action(const char *path, struct hsm_current_action *hca)
1429 fd = open(path, O_RDONLY | O_NONBLOCK);
1433 rc = ioctl(fd, LL_IOC_HSM_ACTION, hca);
1434 /* If error, save errno value */
1435 rc = rc ? -errno : 0;
1442 * Allocate a hsm_user_request with the specified carateristics.
1443 * This structure should be freed with free().
1445 * \return an allocated structure on success, NULL otherwise.
1447 struct hsm_user_request *llapi_hsm_user_request_alloc(int itemcount,
1452 len += sizeof(struct hsm_user_request);
1453 len += sizeof(struct hsm_user_item) * itemcount;
1456 return (struct hsm_user_request *)malloc(len);
1460 * Send a HSM request to Lustre, described in \param request.
1462 * \param path Fullpath to the file to operate on.
1463 * \param request The request, allocated with llapi_hsm_user_request_alloc().
1465 * \return 0 on success, an error code otherwise.
1467 int llapi_hsm_request(const char *path, const struct hsm_user_request *request)
1472 rc = get_root_path(WANT_FD, NULL, &fd, (char *)path, -1);
1476 rc = ioctl(fd, LL_IOC_HSM_REQUEST, request);
1477 /* If error, save errno value */
1478 rc = rc ? -errno : 0;