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 <libcfs/libcfs.h>
57 #include <lnet/lnetctl.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre/lustreapi.h>
60 #include "lustreapi_internal.h"
62 #define OPEN_BY_FID_PATH dot_lustre_name"/fid"
64 /****** HSM Copytool API ********/
65 #define CT_PRIV_MAGIC 0xC0BE2001
66 struct hsm_copytool_private {
72 lustre_kernelcomm kuc;
76 #define CP_PRIV_MAGIC 0x19880429
77 struct hsm_copyaction_private {
80 const struct hsm_copytool_private *ct_priv;
85 #include <libcfs/libcfs.h>
87 enum ct_progress_type {
98 CT_ARCHIVE_START = HSMA_ARCHIVE,
99 CT_ARCHIVE_RUNNING = HSMA_ARCHIVE + CT_RUNNING,
100 CT_ARCHIVE_FINISH = HSMA_ARCHIVE + CT_FINISH,
101 CT_ARCHIVE_CANCEL = HSMA_ARCHIVE + CT_CANCEL,
102 CT_ARCHIVE_ERROR = HSMA_ARCHIVE + CT_ERROR,
103 CT_RESTORE_START = HSMA_RESTORE,
104 CT_RESTORE_RUNNING = HSMA_RESTORE + CT_RUNNING,
105 CT_RESTORE_FINISH = HSMA_RESTORE + CT_FINISH,
106 CT_RESTORE_CANCEL = HSMA_RESTORE + CT_CANCEL,
107 CT_RESTORE_ERROR = HSMA_RESTORE + CT_ERROR,
108 CT_REMOVE_START = HSMA_REMOVE,
109 CT_REMOVE_RUNNING = HSMA_REMOVE + CT_RUNNING,
110 CT_REMOVE_FINISH = HSMA_REMOVE + CT_FINISH,
111 CT_REMOVE_CANCEL = HSMA_REMOVE + CT_CANCEL,
112 CT_REMOVE_ERROR = HSMA_REMOVE + CT_ERROR,
116 /* initialized in llapi_hsm_register_event_fifo() */
117 int llapi_hsm_event_fd = -1;
119 static inline const char *llapi_hsm_ct_ev2str(int type)
126 case CT_ARCHIVE_START:
127 return "ARCHIVE_START";
128 case CT_ARCHIVE_RUNNING:
129 return "ARCHIVE_RUNNING";
130 case CT_ARCHIVE_FINISH:
131 return "ARCHIVE_FINISH";
132 case CT_ARCHIVE_CANCEL:
133 return "ARCHIVE_CANCEL";
134 case CT_ARCHIVE_ERROR:
135 return "ARCHIVE_ERROR";
136 case CT_RESTORE_START:
137 return "RESTORE_START";
138 case CT_RESTORE_RUNNING:
139 return "RESTORE_RUNNING";
140 case CT_RESTORE_FINISH:
141 return "RESTORE_FINISH";
142 case CT_RESTORE_CANCEL:
143 return "RESTORE_CANCEL";
144 case CT_RESTORE_ERROR:
145 return "RESTORE_ERROR";
146 case CT_REMOVE_START:
147 return "REMOVE_START";
148 case CT_REMOVE_RUNNING:
149 return "REMOVE_RUNNING";
150 case CT_REMOVE_FINISH:
151 return "REMOVE_FINISH";
152 case CT_REMOVE_CANCEL:
153 return "REMOVE_CANCEL";
154 case CT_REMOVE_ERROR:
155 return "REMOVE_ERROR";
157 llapi_err_noerrno(LLAPI_MSG_ERROR,
158 "Unknown event type: %d", type);
164 * Writes a JSON event to the monitor FIFO. Noop if no FIFO has been
167 * \param event A list of llapi_json_items comprising a
168 * single JSON-formatted event.
170 * \retval 0 on success.
171 * \retval -errno on error.
173 int llapi_hsm_write_json_event(struct llapi_json_item_list **event)
176 char time_string[40];
177 char json_buf[PIPE_BUF];
179 time_t event_time = time(0);
180 struct tm time_components;
181 struct llapi_json_item_list *json_items;
183 /* Noop unless the event fd was initialized */
184 if (llapi_hsm_event_fd < 0)
187 if (event == NULL || *event == NULL)
192 localtime_r(&event_time, &time_components);
194 if (strftime(time_string, sizeof(time_string), "%Y-%m-%d %T %z",
195 &time_components) == 0) {
197 llapi_error(LLAPI_MSG_ERROR, rc, "strftime() failed");
201 rc = llapi_json_add_item(&json_items, "event_time", LLAPI_JSON_STRING,
204 llapi_error(LLAPI_MSG_ERROR, -rc, "error in "
205 "llapi_json_add_item()");
209 buf_file = fmemopen(json_buf, sizeof(json_buf), "w");
210 if (buf_file == NULL)
213 rc = llapi_json_write_list(event, buf_file);
221 if (write(llapi_hsm_event_fd, json_buf, strlen(json_buf)) < 0) {
222 /* Ignore write failures due to missing reader. */
231 * Hook for llapi_hsm_copytool_register and llapi_hsm_copytool_unregister
232 * to generate JSON events suitable for consumption by a copytool
233 * monitoring process.
235 * \param priv Opaque private control structure.
236 * \param event_type The type of event (register or unregister).
238 * \retval 0 on success.
239 * \retval -errno on error.
241 int llapi_hsm_log_ct_registration(struct hsm_copytool_private **priv,
245 char agent_uuid[UUID_MAX];
246 struct hsm_copytool_private *ct;
247 struct llapi_json_item_list *json_items;
249 if (priv == NULL || *priv == NULL)
253 if (ct->magic != CT_PRIV_MAGIC)
256 if (event_type != CT_REGISTER && event_type != CT_UNREGISTER)
259 rc = llapi_json_init_list(&json_items);
263 rc = llapi_get_agent_uuid(ct->mnt, agent_uuid, sizeof(agent_uuid));
266 llapi_chomp_string(agent_uuid);
268 rc = llapi_json_add_item(&json_items, "uuid", LLAPI_JSON_STRING,
273 rc = llapi_json_add_item(&json_items, "mount_point", LLAPI_JSON_STRING,
278 rc = llapi_json_add_item(&json_items, "archive", LLAPI_JSON_INTEGER,
283 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
284 (char *)llapi_hsm_ct_ev2str(event_type));
288 rc = llapi_hsm_write_json_event(&json_items);
295 llapi_error(LLAPI_MSG_ERROR, rc, "error in "
296 "llapi_hsm_log_ct_registration()");
299 if (json_items != NULL)
300 llapi_json_destroy_list(&json_items);
306 * Given a copytool progress update, construct a JSON event suitable for
307 * consumption by a copytool monitoring process.
309 * Examples of various events generated here and written by
310 * llapi_hsm_write_json_event:
312 * Copytool registration and deregistration:
313 * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "REGISTER",
314 * "archive": 0, "mount_point": "/mnt/lustre",
315 * "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
316 * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "UNREGISTER",
317 * "archive": 0, "mount_point": "/mnt/lustre",
318 * "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
320 * An archive action, start to completion:
321 * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "ARCHIVE_START",
322 * "total_bytes": 0, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
323 * "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
324 * {"event_time": "2014-02-26 14:50:18 -0500", "event_type": "ARCHIVE_RUNNING",
325 * "current_bytes": 5242880, "total_bytes": 39000000,
326 * "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
327 * "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
328 * {"event_time": "2014-02-26 14:50:50 -0500", "event_type": "ARCHIVE_FINISH",
329 * "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
332 * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "LOGGED_MESSAGE",
334 * "message": "lhsmtool_posix[42]: copytool fs=lustre archive#=2 item_count=1"}
336 * \param hcp Opaque action handle returned by
337 * llapi_hsm_action_start.
338 * \param hai The hsm_action_item describing the request.
339 * \param progress_type The ct_progress_type describing the update.
340 * \param total The total expected bytes for the request.
341 * \param current The current copied byte count for the request.
343 * \retval 0 on success.
344 * \retval -errno on error.
346 int llapi_hsm_log_ct_progress(struct hsm_copyaction_private **phcp,
347 const struct hsm_action_item *hai, __u32 progress_type,
348 __u64 total, __u64 current)
352 long long recno = -1;
353 char lustre_path[PATH_MAX];
354 char strfid[FID_NOBRACE_LEN + 1];
355 struct hsm_copyaction_private *hcp;
356 struct llapi_json_item_list *json_items;
358 if (phcp == NULL || *phcp == NULL)
363 rc = llapi_json_init_list(&json_items);
367 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_dfid));
368 rc = llapi_json_add_item(&json_items, "data_fid",
369 LLAPI_JSON_STRING, strfid);
373 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_fid));
374 rc = llapi_json_add_item(&json_items, "source_fid",
375 LLAPI_JSON_STRING, strfid);
379 if (hcp->copy.hc_errval == ECANCELED) {
380 progress_type = CT_CANCEL;
384 if (hcp->copy.hc_errval != 0) {
385 progress_type = CT_ERROR;
387 rc = llapi_json_add_item(&json_items, "errno",
389 &hcp->copy.hc_errval);
393 rc = llapi_json_add_item(&json_items, "error",
395 strerror(hcp->copy.hc_errval));
402 /* lustre_path isn't available after a restore completes */
403 /* total_bytes isn't available after a restore or archive completes */
404 if (progress_type != CT_FINISH) {
405 rc = llapi_fid2path(hcp->ct_priv->mnt, strfid, lustre_path,
406 sizeof(lustre_path), &recno, &linkno);
410 rc = llapi_json_add_item(&json_items, "lustre_path",
411 LLAPI_JSON_STRING, lustre_path);
415 rc = llapi_json_add_item(&json_items, "total_bytes",
416 LLAPI_JSON_BIGNUM, &total);
421 if (progress_type == CT_RUNNING)
422 rc = llapi_json_add_item(&json_items, "current_bytes",
423 LLAPI_JSON_BIGNUM, ¤t);
428 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
429 (char *)llapi_hsm_ct_ev2str(hai->hai_action +
434 rc = llapi_hsm_write_json_event(&json_items);
441 llapi_error(LLAPI_MSG_ERROR, rc, "error in "
442 "llapi_hsm_log_ct_progress()");
445 if (json_items != NULL)
446 llapi_json_destroy_list(&json_items);
452 * Given a path to a FIFO, create a filehandle for nonblocking writes to it.
453 * Intended to be used for copytool monitoring processes that read an
454 * event stream from the FIFO. Events written in the absence of a reader
457 * \param path Path to monitor FIFO.
459 * \retval 0 on success.
460 * \retval -errno on error.
462 int llapi_hsm_register_event_fifo(char *path)
467 /* Create the FIFO if necessary. */
468 if ((mkfifo(path, 0644) < 0) && (errno != EEXIST)) {
469 llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed", path);
472 if (errno == EEXIST) {
473 if (stat(path, &statbuf) < 0) {
474 llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed",
478 if (!S_ISFIFO(statbuf.st_mode) ||
479 ((statbuf.st_mode & 0777) != 0644)) {
480 llapi_error(LLAPI_MSG_ERROR, errno, "%s exists but is "
481 "not a pipe or has a wrong mode", path);
486 /* Open the FIFO for read so that the subsequent open for write
487 * doesn't immediately fail. */
488 read_fd = open(path, O_RDONLY | O_NONBLOCK);
490 llapi_error(LLAPI_MSG_ERROR, errno,
491 "cannot open(%s) for read", path);
495 /* Open the FIFO for writes, but don't block on waiting
497 llapi_hsm_event_fd = open(path, O_WRONLY | O_NONBLOCK);
498 if (llapi_hsm_event_fd < 0) {
499 llapi_error(LLAPI_MSG_ERROR, errno,
500 "cannot open(%s) for write", path);
504 /* Now close the reader. An external monitoring process can
505 * now open the FIFO for reads. If no reader comes along the
506 * events are lost. NOTE: Only one reader at a time! */
509 /* Ignore SIGPIPEs -- can occur if the reader goes away. */
510 signal(SIGPIPE, SIG_IGN);
516 * Given a path to a FIFO, close its filehandle and delete the FIFO.
518 * \param path Path to monitor FIFO.
520 * \retval 0 on success.
521 * \retval -errno on error.
523 int llapi_hsm_unregister_event_fifo(char *path)
525 /* Noop unless the event fd was initialized */
526 if (llapi_hsm_event_fd < 0)
529 if (close(llapi_hsm_event_fd) < 0)
534 llapi_hsm_event_fd = -1;
540 * Custom logging callback to be used when a monitoring FIFO has been
541 * registered. Formats log entries as JSON events suitable for
542 * consumption by a copytool monitoring process.
544 * \param level The message loglevel.
545 * \param _rc The returncode associated with the message.
546 * \param fmt The message format string.
547 * \param args Arguments to be formatted by the format string.
551 void llapi_hsm_log_error(enum llapi_message_level level, int _rc,
552 const char *fmt, va_list args)
559 struct llapi_json_item_list *json_items;
561 /* Noop unless the event fd was initialized */
562 if (llapi_hsm_event_fd < 0)
565 rc = llapi_json_init_list(&json_items);
569 if ((level & LLAPI_MSG_NO_ERRNO) == 0) {
570 rc = llapi_json_add_item(&json_items, "errno",
576 rc = llapi_json_add_item(&json_items, "error",
583 va_copy(args2, args);
584 msg_len = vsnprintf(NULL, 0, fmt, args2) + 1;
587 msg = (char *) alloca(msg_len);
593 rc = vsnprintf(msg, msg_len, fmt, args);
597 rc = llapi_json_add_item(&json_items, "message",
603 rc = llapi_json_add_item(&json_items, "message",
605 "INTERNAL ERROR: message failed");
610 real_level = level & LLAPI_MSG_NO_ERRNO;
611 real_level = real_level > 0 ? level - LLAPI_MSG_NO_ERRNO : level;
613 rc = llapi_json_add_item(&json_items, "level", LLAPI_JSON_STRING,
614 (void *)llapi_msg_level2str(real_level));
618 rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
623 rc = llapi_hsm_write_json_event(&json_items);
630 /* Write directly to stderr to avoid llapi_error, which now
631 * emits JSON event messages. */
632 fprintf(stderr, "\nFATAL ERROR IN llapi_hsm_log_error(): rc %d,", rc);
635 if (json_items != NULL)
636 llapi_json_destroy_list(&json_items);
641 /** Register a copytool
642 * \param[out] priv Opaque private control structure
643 * \param mnt Lustre filesystem mount point
644 * \param flags Open flags, currently unused (e.g. O_NONBLOCK)
645 * \param archive_count
646 * \param archives Which archive numbers this copytool is responsible for
648 int llapi_hsm_copytool_register(struct hsm_copytool_private **priv,
649 const char *mnt, int flags, int archive_count,
652 struct hsm_copytool_private *ct;
655 if (archive_count > 0 && archives == NULL) {
656 llapi_err_noerrno(LLAPI_MSG_ERROR,
657 "NULL archive numbers");
661 ct = calloc(1, sizeof(*ct));
665 ct->magic = CT_PRIV_MAGIC;
667 ct->open_by_fid_fd = -1;
668 ct->kuc.lk_rfd = LK_NOFD;
669 ct->kuc.lk_wfd = LK_NOFD;
671 ct->mnt = strdup(mnt);
672 if (ct->mnt == NULL) {
677 ct->kuch = malloc(HAL_MAXSIZE + sizeof(*ct->kuch));
678 if (ct->kuch == NULL) {
683 ct->mnt_fd = open(ct->mnt, O_RDONLY);
684 if (ct->mnt_fd < 0) {
689 ct->open_by_fid_fd = openat(ct->mnt_fd, OPEN_BY_FID_PATH, O_RDONLY);
690 if (ct->open_by_fid_fd < 0) {
695 /* no archives specified means "match all". */
697 for (rc = 0; rc < archive_count; rc++) {
698 if (archives[rc] > 8 * sizeof(ct->archives)) {
699 llapi_err_noerrno(LLAPI_MSG_ERROR,
700 "maximum of %zu archives supported",
701 8 * sizeof(ct->archives));
704 /* in the list we have a all archive wildcard
705 * so move to all archives mode
707 if (archives[rc] == 0) {
712 ct->archives |= (1 << (archives[rc] - 1));
715 rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
719 /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
720 ct->kuc.lk_data = ct->archives;
721 rc = ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, &ct->kuc);
724 llapi_error(LLAPI_MSG_ERROR, rc,
725 "cannot start copytool on '%s'", mnt);
731 llapi_hsm_log_ct_registration(&ct, CT_REGISTER);
733 /* Only the kernel reference keeps the write side open */
734 close(ct->kuc.lk_wfd);
735 ct->kuc.lk_wfd = LK_NOFD;
743 /* cleanup the kuc channel */
744 libcfs_ukuc_stop(&ct->kuc);
747 if (!(ct->mnt_fd < 0))
750 if (!(ct->open_by_fid_fd < 0))
751 close(ct->open_by_fid_fd);
762 /** Deregister a copytool
763 * Note: under Linux, until llapi_hsm_copytool_unregister is called
764 * (or the program is killed), the libcfs module will be referenced
765 * and unremovable, even after Lustre services stop.
767 int llapi_hsm_copytool_unregister(struct hsm_copytool_private **priv)
769 struct hsm_copytool_private *ct;
771 if (priv == NULL || *priv == NULL)
775 if (ct->magic != CT_PRIV_MAGIC)
778 /* Tell the kernel to stop sending us messages */
779 ct->kuc.lk_flags = LK_FLG_STOP;
780 ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, &ct->kuc);
782 /* Shut down the kernelcomms */
783 libcfs_ukuc_stop(&ct->kuc);
785 llapi_hsm_log_ct_registration(&ct, CT_UNREGISTER);
787 close(ct->open_by_fid_fd);
797 /** Wait for the next hsm_action_list
798 * \param ct Opaque private control structure
799 * \param halh Action list handle, will be allocated here
800 * \param msgsize Number of bytes in the message, will be set here
801 * \return 0 valid message received; halh and msgsize are set
803 * Note: The application must not call llapi_hsm_copytool_recv until it has
804 * cleared the data in ct->kuch from the previous call.
806 int llapi_hsm_copytool_recv(struct hsm_copytool_private *ct,
807 struct hsm_action_list **halh, int *msgsize)
809 struct kuc_hdr *kuch;
810 struct hsm_action_list *hal;
813 if (ct == NULL || ct->magic != CT_PRIV_MAGIC)
816 if (halh == NULL || msgsize == NULL)
821 rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
822 HAL_MAXSIZE + sizeof(*kuch),
827 /* Handle generic messages */
828 if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
829 kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
834 if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
835 kuch->kuc_msgtype != HMT_ACTION_LIST) {
836 llapi_err_noerrno(LLAPI_MSG_ERROR,
837 "Unknown HSM message type %d:%d\n",
838 kuch->kuc_transport, kuch->kuc_msgtype);
843 if (kuch->kuc_msglen < sizeof(*kuch) + sizeof(*hal)) {
844 llapi_err_noerrno(LLAPI_MSG_ERROR, "Short HSM message %d",
850 /* Our message is a hsm_action_list. Use pointer math to skip
851 * kuch_hdr and point directly to the message payload.
853 hal = (struct hsm_action_list *)(kuch + 1);
855 /* Check that we have registered for this archive #
856 * if 0 registered, we serve any archive */
858 ((1 << (hal->hal_archive_id - 1)) & ct->archives) == 0) {
859 llapi_err_noerrno(LLAPI_MSG_INFO,
860 "This copytool does not service archive #%d,"
861 " ignoring this request."
862 " Mask of served archive is 0x%.8X",
863 hal->hal_archive_id, ct->archives);
870 *msgsize = kuch->kuc_msglen - sizeof(*kuch);
879 /** Get parent path from mount point and fid.
881 * \param mnt Filesystem root path.
882 * \param fid Object FID.
883 * \param parent Destination buffer.
884 * \param parent_len Destination buffer size.
885 * \return 0 on success.
887 static int fid_parent(const char *mnt, const lustre_fid *fid, char *parent,
892 long long recno = -1;
894 char strfid[FID_NOBRACE_LEN + 1];
897 snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(fid));
899 rc = llapi_fid2path(mnt, strfid, file, sizeof(file),
904 /* fid2path returns a relative path */
905 rc = snprintf(parent, parent_len, "%s/%s", mnt, file);
906 if (rc >= parent_len)
907 return -ENAMETOOLONG;
909 /* remove file name */
910 ptr = strrchr(parent, '/');
911 if (ptr == NULL || ptr == parent) {
921 static int ct_open_by_fid(const struct hsm_copytool_private *ct,
922 const struct lu_fid *fid, int open_flags)
924 char fid_name[FID_NOBRACE_LEN + 1];
927 snprintf(fid_name, sizeof(fid_name), DFID_NOBRACE, PFID(fid));
929 fd = openat(ct->open_by_fid_fd, fid_name, open_flags);
930 return fd < 0 ? -errno : fd;
933 static int ct_stat_by_fid(const struct hsm_copytool_private *ct,
934 const struct lu_fid *fid,
937 char fid_name[FID_NOBRACE_LEN + 1];
940 snprintf(fid_name, sizeof(fid_name), DFID_NOBRACE, PFID(fid));
942 rc = fstatat(ct->open_by_fid_fd, fid_name, buf, 0);
943 return rc ? -errno : 0;
946 /** Create the destination volatile file for a restore operation.
948 * \param hcp Private copyaction handle.
949 * \param mdt_index MDT index where to create the volatile file.
950 * \param flags Volatile file creation flags.
951 * \return 0 on success.
953 static int create_restore_volatile(struct hsm_copyaction_private *hcp,
954 int mdt_index, int open_flags)
958 char parent[PATH_MAX + 1];
959 const char *mnt = hcp->ct_priv->mnt;
960 struct hsm_action_item *hai = &hcp->copy.hc_hai;
962 rc = fid_parent(mnt, &hai->hai_fid, parent, sizeof(parent));
964 /* fid_parent() failed, try to keep on going */
965 llapi_error(LLAPI_MSG_ERROR, rc,
966 "cannot get parent path to restore "DFID" "
967 "using '%s'", PFID(&hai->hai_fid), mnt);
968 snprintf(parent, sizeof(parent), "%s", mnt);
971 fd = llapi_create_volatile_idx(parent, mdt_index, open_flags);
975 rc = fchown(fd, hcp->stat.st_uid, hcp->stat.st_gid);
979 rc = llapi_fd2fid(fd, &hai->hai_dfid);
994 /** Start processing an HSM action.
995 * Should be called by copytools just before starting handling a request.
996 * It could be skipped if copytool only want to directly report an error,
997 * \see llapi_hsm_action_end().
999 * \param hcp Opaque action handle to be passed to
1000 * llapi_hsm_action_progress and llapi_hsm_action_end.
1001 * \param ct Copytool handle acquired at registration.
1002 * \param hai The hsm_action_item describing the request.
1003 * \param restore_mdt_index On restore: MDT index where to create the volatile
1004 * file. Use -1 for default.
1005 * \param restore_open_flags On restore: volatile file creation mode. Use
1006 * O_LOV_DELAY_CREATE to manually set the LOVEA
1008 * \param is_error Whether this call is just to report an error.
1010 * \return 0 on success.
1012 int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
1013 const struct hsm_copytool_private *ct,
1014 const struct hsm_action_item *hai,
1015 int restore_mdt_index, int restore_open_flags,
1018 struct hsm_copyaction_private *hcp;
1021 hcp = calloc(1, sizeof(*hcp));
1027 hcp->copy.hc_hai = *hai;
1028 hcp->copy.hc_hai.hai_len = sizeof(*hai);
1033 if (hai->hai_action == HSMA_RESTORE) {
1034 rc = ct_stat_by_fid(hcp->ct_priv, &hai->hai_fid, &hcp->stat);
1038 rc = create_restore_volatile(hcp, restore_mdt_index,
1039 restore_open_flags);
1044 rc = ioctl(ct->mnt_fd, LL_IOC_HSM_COPY_START, &hcp->copy);
1050 llapi_hsm_log_ct_progress(&hcp, hai, CT_START, 0, 0);
1053 hcp->magic = CP_PRIV_MAGIC;
1058 if (!(hcp->data_fd < 0))
1059 close(hcp->data_fd);
1066 /** Terminate an HSM action processing.
1067 * Should be called by copytools just having finished handling the request.
1068 * \param hdl[in,out] Handle returned by llapi_hsm_action_start.
1069 * \param he[in] The final range of copied data (for copy actions).
1070 * \param errval[in] The status code of the operation.
1071 * \param flags[in] The flags about the termination status (HP_FLAG_RETRY if
1072 * the error is retryable).
1074 * \return 0 on success.
1076 int llapi_hsm_action_end(struct hsm_copyaction_private **phcp,
1077 const struct hsm_extent *he, int hp_flags, int errval)
1079 struct hsm_copyaction_private *hcp;
1080 struct hsm_action_item *hai;
1083 if (phcp == NULL || *phcp == NULL || he == NULL)
1088 if (hcp->magic != CP_PRIV_MAGIC)
1091 hai = &hcp->copy.hc_hai;
1093 if (hai->hai_action == HSMA_RESTORE && errval == 0) {
1094 struct timeval tv[2];
1096 /* Set {a,m}time of volatile file to that of original. */
1097 tv[0].tv_sec = hcp->stat.st_atime;
1099 tv[1].tv_sec = hcp->stat.st_mtime;
1101 if (futimes(hcp->data_fd, tv) < 0) {
1106 rc = fsync(hcp->data_fd);
1114 /* In some cases, like restore, 2 FIDs are used.
1115 * Set the right FID to use here. */
1116 if (hai->hai_action == HSMA_ARCHIVE || hai->hai_action == HSMA_RESTORE)
1117 hai->hai_fid = hai->hai_dfid;
1119 /* Fill the last missing data that will be needed by
1120 * kernel to send a hsm_progress. */
1121 hcp->copy.hc_flags = hp_flags;
1122 hcp->copy.hc_errval = abs(errval);
1124 hcp->copy.hc_hai.hai_extent = *he;
1126 rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_COPY_END, &hcp->copy);
1132 llapi_hsm_log_ct_progress(&hcp, hai, CT_FINISH, 0, 0);
1135 if (!(hcp->data_fd < 0))
1136 close(hcp->data_fd);
1144 /** Notify a progress in processing an HSM action.
1145 * \param hdl[in,out] handle returned by llapi_hsm_action_start.
1146 * \param he[in] the range of copied data (for copy actions).
1147 * \param total[in] the expected total of copied data (for copy actions).
1148 * \param hp_flags[in] HSM progress flags.
1149 * \return 0 on success.
1151 int llapi_hsm_action_progress(struct hsm_copyaction_private *hcp,
1152 const struct hsm_extent *he, __u64 total,
1156 struct hsm_progress hp;
1157 struct hsm_action_item *hai;
1159 if (hcp == NULL || he == NULL)
1162 if (hcp->magic != CP_PRIV_MAGIC)
1165 hai = &hcp->copy.hc_hai;
1167 memset(&hp, 0, sizeof(hp));
1169 hp.hp_cookie = hai->hai_cookie;
1170 hp.hp_flags = hp_flags;
1172 /* Progress is made on the data fid */
1173 hp.hp_fid = hai->hai_dfid;
1176 rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_PROGRESS, &hp);
1180 llapi_hsm_log_ct_progress(&hcp, hai, CT_RUNNING, total, he->length);
1185 /** Get the fid of object to be used for copying data.
1186 * @return error code if the action is not a copy operation.
1188 int llapi_hsm_action_get_dfid(const struct hsm_copyaction_private *hcp,
1191 const struct hsm_action_item *hai = &hcp->copy.hc_hai;
1193 if (hcp->magic != CP_PRIV_MAGIC)
1196 if (hai->hai_action != HSMA_RESTORE && hai->hai_action != HSMA_ARCHIVE)
1199 *fid = hai->hai_dfid;
1205 * Get a file descriptor to be used for copying data. It's up to the
1206 * caller to close the FDs obtained from this function.
1208 * @retval a file descriptor on success.
1209 * @retval a negative error code on failure.
1211 int llapi_hsm_action_get_fd(const struct hsm_copyaction_private *hcp)
1213 const struct hsm_action_item *hai = &hcp->copy.hc_hai;
1216 if (hcp->magic != CP_PRIV_MAGIC)
1219 if (hai->hai_action == HSMA_ARCHIVE) {
1220 return ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
1221 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
1222 } else if (hai->hai_action == HSMA_RESTORE) {
1223 fd = dup(hcp->data_fd);
1224 return fd < 0 ? -errno : fd;
1231 * Import an existing hsm-archived file into Lustre.
1233 * Caller must access file by (returned) newfid value from now on.
1235 * \param dst path to Lustre destination (e.g. /mnt/lustre/my/file).
1236 * \param archive archive number.
1237 * \param st struct stat buffer containing file ownership, perm, etc.
1238 * \param stripe_* Striping options. Currently ignored, since the restore
1239 * operation will set the striping. In V2, this striping might
1241 * \param newfid[out] Filled with new Lustre fid.
1243 int llapi_hsm_import(const char *dst, int archive, const struct stat *st,
1244 unsigned long long stripe_size, int stripe_offset,
1245 int stripe_count, int stripe_pattern, char *pool_name,
1248 struct hsm_user_import hui;
1252 if (stripe_pattern == 0)
1253 stripe_pattern = LOV_PATTERN_RAID0;
1255 /* Create a non-striped file */
1256 fd = llapi_file_open_pool(dst, O_CREAT | O_WRONLY, st->st_mode,
1257 stripe_size, stripe_offset, stripe_count,
1258 stripe_pattern | LOV_PATTERN_F_RELEASED,
1261 llapi_error(LLAPI_MSG_ERROR, fd,
1262 "cannot create '%s' for import", dst);
1266 /* Get the new fid in Lustre. Caller needs to use this fid
1268 rc = llapi_fd2fid(fd, newfid);
1270 llapi_error(LLAPI_MSG_ERROR, rc,
1271 "cannot get fid of '%s' for import", dst);
1275 hui.hui_uid = st->st_uid;
1276 hui.hui_gid = st->st_gid;
1277 hui.hui_mode = st->st_mode;
1278 hui.hui_size = st->st_size;
1279 hui.hui_archive_id = archive;
1280 hui.hui_atime = st->st_atime;
1281 hui.hui_atime_ns = st->st_atim.tv_nsec;
1282 hui.hui_mtime = st->st_mtime;
1283 hui.hui_mtime_ns = st->st_mtim.tv_nsec;
1284 rc = ioctl(fd, LL_IOC_HSM_IMPORT, &hui);
1287 llapi_error(LLAPI_MSG_ERROR, rc, "cannot import '%s'", dst);
1300 * Return the current HSM states and HSM requests related to file pointed by \a
1303 * \param hus Should be allocated by caller. Will be filled with current file
1306 * \retval 0 on success.
1307 * \retval -errno on error.
1309 int llapi_hsm_state_get_fd(int fd, struct hsm_user_state *hus)
1313 rc = ioctl(fd, LL_IOC_HSM_STATE_GET, hus);
1314 /* If error, save errno value */
1315 rc = rc ? -errno : 0;
1321 * Return the current HSM states and HSM requests related to file pointed by \a
1324 * see llapi_hsm_state_get_fd() for args use and return
1326 int llapi_hsm_state_get(const char *path, struct hsm_user_state *hus)
1331 fd = open(path, O_RDONLY | O_NONBLOCK);
1335 rc = llapi_hsm_state_get_fd(fd, hus);
1342 * Set HSM states of file pointed by \a fd
1344 * Using the provided bitmasks, the current HSM states for this file will be
1345 * changed. \a archive_id could be used to change the archive number also. Set
1346 * it to 0 if you do not want to change it.
1348 * \param setmask Bitmask for flag to be set.
1349 * \param clearmask Bitmask for flag to be cleared.
1350 * \param archive_id Archive number identifier to use. 0 means no change.
1352 * \retval 0 on success.
1353 * \retval -errno on error.
1355 int llapi_hsm_state_set_fd(int fd, __u64 setmask, __u64 clearmask,
1358 struct hsm_state_set hss;
1361 hss.hss_valid = HSS_SETMASK|HSS_CLEARMASK;
1362 hss.hss_setmask = setmask;
1363 hss.hss_clearmask = clearmask;
1364 /* Change archive_id if provided. We can only change
1365 * to set something different than 0. */
1366 if (archive_id > 0) {
1367 hss.hss_valid |= HSS_ARCHIVE_ID;
1368 hss.hss_archive_id = archive_id;
1370 rc = ioctl(fd, LL_IOC_HSM_STATE_SET, &hss);
1371 /* If error, save errno value */
1372 rc = rc ? -errno : 0;
1378 * Set HSM states of file pointed by \a path.
1380 * see llapi_hsm_state_set_fd() for args use and return
1382 int llapi_hsm_state_set(const char *path, __u64 setmask, __u64 clearmask,
1388 fd = open(path, O_WRONLY | O_LOV_DELAY_CREATE | O_NONBLOCK);
1392 rc = llapi_hsm_state_set_fd(fd, setmask, clearmask, archive_id);
1399 * Return the current HSM request related to file pointed by \a path.
1401 * \param hca Should be allocated by caller. Will be filled with current file
1404 * \retval 0 on success.
1405 * \retval -errno on error.
1407 int llapi_hsm_current_action(const char *path, struct hsm_current_action *hca)
1412 fd = open(path, O_RDONLY | O_NONBLOCK);
1416 rc = ioctl(fd, LL_IOC_HSM_ACTION, hca);
1417 /* If error, save errno value */
1418 rc = rc ? -errno : 0;
1425 * Allocate a hsm_user_request with the specified carateristics.
1426 * This structure should be freed with free().
1428 * \return an allocated structure on success, NULL otherwise.
1430 struct hsm_user_request *llapi_hsm_user_request_alloc(int itemcount,
1435 len += sizeof(struct hsm_user_request);
1436 len += sizeof(struct hsm_user_item) * itemcount;
1439 return (struct hsm_user_request *)malloc(len);
1443 * Send a HSM request to Lustre, described in \param request.
1445 * \param path Fullpath to the file to operate on.
1446 * \param request The request, allocated with llapi_hsm_user_request_alloc().
1448 * \return 0 on success, an error code otherwise.
1450 int llapi_hsm_request(const char *path, const struct hsm_user_request *request)
1455 rc = get_root_path(WANT_FD, NULL, &fd, (char *)path, -1);
1459 rc = ioctl(fd, LL_IOC_HSM_REQUEST, request);
1460 /* If error, save errno value */
1461 rc = rc ? -errno : 0;