Whamcloud - gitweb
LU-13048 mdd: allow release after a non-blocking migrate
[fs/lustre-release.git] / lustre / utils / liblustreapi_hsm.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
7  *     alternatives
8  *
9  * Copyright (c) 2013, 2017, Intel Corporation.
10  *
11  * All rights reserved. This program and the accompanying materials
12  * are made available under the terms of the GNU Lesser General Public License
13  * (LGPL) version 2.1 or (at your discretion) any later version.
14  * (LGPL) version 2.1 accompanies this distribution, and is available at
15  * http://www.gnu.org/licenses/lgpl-2.1.html
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * LGPL HEADER END
23  */
24 /*
25  * lustre/utils/liblustreapi_hsm.c
26  *
27  * lustreapi library for hsm calls
28  *
29  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
30  * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
31  * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
32  * Author: Henri Doreau <henri.doreau@cea.fr>
33  */
34
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stddef.h>
40 #include <sys/ioctl.h>
41 #include <unistd.h>
42 #include <malloc.h>
43 #include <errno.h>
44 #include <dirent.h>
45 #include <stdarg.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <time.h>
50 #include <utime.h>
51 #include <sys/syscall.h>
52 #include <fnmatch.h>
53 #include <signal.h>
54 #ifdef HAVE_LINUX_UNISTD_H
55 #include <linux/unistd.h>
56 #else
57 #include <unistd.h>
58 #endif
59
60 #include <linux/lnet/lnetctl.h>
61 #include <lustre/lustreapi.h>
62 #include "lustreapi_internal.h"
63
64 #define OPEN_BY_FID_PATH dot_lustre_name"/fid"
65
66 /****** HSM Copytool API ********/
67 #define CT_PRIV_MAGIC 0xC0BE2001
68 struct hsm_copytool_private {
69         int                              magic;
70         char                            *mnt;
71         struct kuc_hdr                  *kuch;
72         int                              mnt_fd;
73         int                              open_by_fid_fd;
74         struct lustre_kernelcomm        *kuc;
75 };
76
77 #define CP_PRIV_MAGIC 0x19880429
78 struct hsm_copyaction_private {
79         __u32                                    magic;
80         __u32                                    source_fd;
81         __s32                                    data_fd;
82         const struct hsm_copytool_private       *ct_priv;
83         struct hsm_copy                          copy;
84         lstatx_t                                 statx;
85 };
86
87 enum ct_progress_type {
88         CT_START        = 0,
89         CT_RUNNING      = 50,
90         CT_FINISH       = 100,
91         CT_CANCEL       = 150,
92         CT_ERROR        = 175
93 };
94
95 enum ct_event {
96         CT_REGISTER             = 1,
97         CT_UNREGISTER           = 2,
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,
113         CT_EVENT_MAX
114 };
115
116 /* initialized in llapi_hsm_register_event_fifo() */
117 static int llapi_hsm_event_fd = -1;
118 static bool created_hsm_event_fifo;
119
120 static inline const char *llapi_hsm_ct_ev2str(int type)
121 {
122         switch (type) {
123         case CT_REGISTER:
124                 return "REGISTER";
125         case CT_UNREGISTER:
126                 return "UNREGISTER";
127         case CT_ARCHIVE_START:
128                 return "ARCHIVE_START";
129         case CT_ARCHIVE_RUNNING:
130                 return "ARCHIVE_RUNNING";
131         case CT_ARCHIVE_FINISH:
132                 return "ARCHIVE_FINISH";
133         case CT_ARCHIVE_CANCEL:
134                 return "ARCHIVE_CANCEL";
135         case CT_ARCHIVE_ERROR:
136                 return "ARCHIVE_ERROR";
137         case CT_RESTORE_START:
138                 return "RESTORE_START";
139         case CT_RESTORE_RUNNING:
140                 return "RESTORE_RUNNING";
141         case CT_RESTORE_FINISH:
142                 return "RESTORE_FINISH";
143         case CT_RESTORE_CANCEL:
144                 return "RESTORE_CANCEL";
145         case CT_RESTORE_ERROR:
146                 return "RESTORE_ERROR";
147         case CT_REMOVE_START:
148                 return "REMOVE_START";
149         case CT_REMOVE_RUNNING:
150                 return "REMOVE_RUNNING";
151         case CT_REMOVE_FINISH:
152                 return "REMOVE_FINISH";
153         case CT_REMOVE_CANCEL:
154                 return "REMOVE_CANCEL";
155         case CT_REMOVE_ERROR:
156                 return "REMOVE_ERROR";
157         default:
158                 llapi_err_noerrno(LLAPI_MSG_ERROR,
159                                   "Unknown event type: %d", type);
160                 return NULL;
161         }
162 }
163
164 /**
165  * Writes a JSON event to the monitor FIFO. Noop if no FIFO has been
166  * registered.
167  *
168  * \param event              A list of llapi_json_items comprising a
169  *                           single JSON-formatted event.
170  *
171  * \retval 0 on success.
172  * \retval -errno on error.
173  */
174 static int llapi_hsm_write_json_event(struct llapi_json_item_list **event)
175 {
176         int                             rc;
177         char                            time_string[40];
178         char                            json_buf[PIPE_BUF];
179         FILE                            *buf_file;
180         time_t                          event_time = time(0);
181         struct tm                       time_components;
182         struct llapi_json_item_list     *json_items;
183
184         /* Noop unless the event fd was initialized */
185         if (llapi_hsm_event_fd < 0)
186                 return 0;
187
188         if (event == NULL || *event == NULL)
189                 return -EINVAL;
190
191         json_items = *event;
192
193         localtime_r(&event_time, &time_components);
194
195         if (strftime(time_string, sizeof(time_string), "%Y-%m-%d %T %z",
196                      &time_components) == 0) {
197                 rc = -EINVAL;
198                 llapi_error(LLAPI_MSG_ERROR, rc, "strftime() failed");
199                 return rc;
200         }
201
202         rc = llapi_json_add_item(&json_items, "event_time", LLAPI_JSON_STRING,
203                                  time_string);
204         if (rc < 0) {
205                 llapi_error(LLAPI_MSG_ERROR, -rc, "error in "
206                             "llapi_json_add_item()");
207                 return rc;
208         }
209
210         buf_file = fmemopen(json_buf, sizeof(json_buf), "w");
211         if (buf_file == NULL)
212                 return -errno;
213
214         rc = llapi_json_write_list(event, buf_file);
215         if (rc < 0) {
216                 fclose(buf_file);
217                 return rc;
218         }
219
220         fclose(buf_file);
221
222         if (write(llapi_hsm_event_fd, json_buf, strlen(json_buf)) < 0) {
223                 /* Ignore write failures due to missing reader. */
224                 if (errno != EPIPE)
225                         return -errno;
226         }
227
228         return 0;
229 }
230
231 /**
232  * Hook for llapi_hsm_copytool_register and llapi_hsm_copytool_unregister
233  * to generate JSON events suitable for consumption by a copytool
234  * monitoring process.
235  *
236  * \param priv               Opaque private control structure.
237  * \param event_type         The type of event (register or unregister).
238  *
239  * \retval 0 on success.
240  * \retval -errno on error.
241  */
242 static int llapi_hsm_log_ct_registration(struct hsm_copytool_private **priv,
243                                          __u32 event_type)
244 {
245         int                             rc;
246         char                            agent_uuid[UUID_MAX];
247         struct hsm_copytool_private     *ct;
248         struct llapi_json_item_list     *json_items;
249
250         /* Noop unless the event fd was initialized */
251         if (llapi_hsm_event_fd < 0)
252                 return 0;
253
254         if (priv == NULL || *priv == NULL)
255                 return -EINVAL;
256
257         ct = *priv;
258         if (ct->magic != CT_PRIV_MAGIC)
259                 return -EINVAL;
260
261         if (event_type != CT_REGISTER && event_type != CT_UNREGISTER)
262                 return -EINVAL;
263
264         rc = llapi_json_init_list(&json_items);
265         if (rc < 0)
266                 goto err;
267
268         rc = llapi_get_agent_uuid(ct->mnt, agent_uuid, sizeof(agent_uuid));
269         if (rc < 0)
270                 goto err;
271         llapi_chomp_string(agent_uuid);
272
273         rc = llapi_json_add_item(&json_items, "uuid", LLAPI_JSON_STRING,
274                                  agent_uuid);
275         if (rc < 0)
276                 goto err;
277
278         rc = llapi_json_add_item(&json_items, "mount_point", LLAPI_JSON_STRING,
279                                  ct->mnt);
280         if (rc < 0)
281                 goto err;
282
283         rc = llapi_json_add_item(&json_items, "archive", LLAPI_JSON_INTEGER,
284                                  &ct->kuc->lk_data_count);
285         if (rc < 0)
286                 goto err;
287
288         rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
289                                  (char *)llapi_hsm_ct_ev2str(event_type));
290         if (rc < 0)
291                 goto err;
292
293         rc = llapi_hsm_write_json_event(&json_items);
294         if (rc < 0)
295                 goto err;
296
297         goto out_free;
298
299 err:
300         llapi_error(LLAPI_MSG_ERROR, rc, "error in "
301                     "llapi_hsm_log_ct_registration()");
302
303 out_free:
304         if (json_items != NULL)
305                 llapi_json_destroy_list(&json_items);
306
307         return rc;
308 }
309
310 /**
311  * Given a copytool progress update, construct a JSON event suitable for
312  * consumption by a copytool monitoring process.
313  *
314  * Examples of various events generated here and written by
315  * llapi_hsm_write_json_event:
316  *
317  * Copytool registration and deregistration:
318  * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "REGISTER",
319  *  "archive": 0, "mount_point": "/mnt/lustre",
320  *  "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
321  * {"event_time": "2014-02-26 14:58:01 -0500", "event_type": "UNREGISTER",
322  *  "archive": 0, "mount_point": "/mnt/lustre",
323  *  "uuid": "80379a60-1f8a-743f-daf2-307cde793ec2"}
324  *
325  * An archive action, start to completion:
326  * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "ARCHIVE_START",
327  *  "total_bytes": 0, "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
328  *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
329  * {"event_time": "2014-02-26 14:50:18 -0500", "event_type": "ARCHIVE_RUNNING",
330  *  "current_bytes": 5242880, "total_bytes": 39000000,
331  *  "lustre_path": "d71.sanity-hsm/f71.sanity-hsm",
332  *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
333  * {"event_time": "2014-02-26 14:50:50 -0500", "event_type": "ARCHIVE_FINISH",
334  *  "source_fid": "0x2000013a1:0x2:0x0", "data_fid": "0x2000013a1:0x2:0x0"}
335  *
336  * A log message:
337  * {"event_time": "2014-02-26 14:50:13 -0500", "event_type": "LOGGED_MESSAGE",
338  *  "level": "INFO",
339  *  "message": "lhsmtool_posix[42]: copytool fs=lustre archive#=2 item_count=1"}
340  *
341  * \param hcp                Opaque action handle returned by
342  *                           llapi_hsm_action_start.
343  * \param hai                The hsm_action_item describing the request.
344  * \param progress_type      The ct_progress_type describing the update.
345  * \param total              The total expected bytes for the request.
346  * \param current            The current copied byte count for the request.
347  *
348  * \retval 0 on success.
349  * \retval -errno on error.
350  */
351 static int llapi_hsm_log_ct_progress(struct hsm_copyaction_private **phcp,
352                                      const struct hsm_action_item *hai,
353                                      __u32 progress_type,
354                                      __u64 total, __u64 current)
355 {
356         int                             rc;
357         int                             linkno = 0;
358         long long                       recno = -1;
359         char                            lustre_path[PATH_MAX];
360         char                            strfid[FID_NOBRACE_LEN + 1];
361         struct hsm_copyaction_private   *hcp;
362         struct llapi_json_item_list     *json_items;
363
364         /* Noop unless the event fd was initialized */
365         if (llapi_hsm_event_fd < 0)
366                 return 0;
367
368         if (phcp == NULL || *phcp == NULL)
369                 return -EINVAL;
370
371         hcp = *phcp;
372
373         rc = llapi_json_init_list(&json_items);
374         if (rc < 0)
375                 goto err;
376
377         snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_dfid));
378         rc = llapi_json_add_item(&json_items, "data_fid",
379                                  LLAPI_JSON_STRING, strfid);
380         if (rc < 0)
381                 goto err;
382
383         snprintf(strfid, sizeof(strfid), DFID_NOBRACE, PFID(&hai->hai_fid));
384         rc = llapi_json_add_item(&json_items, "source_fid",
385                                  LLAPI_JSON_STRING, strfid);
386         if (rc < 0)
387                 goto err;
388
389         if (hcp->copy.hc_errval == ECANCELED) {
390                 progress_type = CT_CANCEL;
391                 goto cancel;
392         }
393
394         if (hcp->copy.hc_errval != 0) {
395                 progress_type = CT_ERROR;
396
397                 rc = llapi_json_add_item(&json_items, "errno",
398                                          LLAPI_JSON_INTEGER,
399                                          &hcp->copy.hc_errval);
400                 if (rc < 0)
401                         goto err;
402
403                 rc = llapi_json_add_item(&json_items, "error",
404                                          LLAPI_JSON_STRING,
405                                          strerror(hcp->copy.hc_errval));
406                 if (rc < 0)
407                         goto err;
408
409                 goto cancel;
410         }
411
412         /* lustre_path isn't available after a restore completes */
413         /* total_bytes isn't available after a restore or archive completes */
414         if (progress_type != CT_FINISH) {
415                 rc = llapi_fid2path_at(hcp->ct_priv->mnt_fd, &hai->hai_dfid,
416                                        lustre_path, sizeof(lustre_path),
417                                        &recno, &linkno);
418                 if (rc < 0)
419                         goto err;
420
421                 rc = llapi_json_add_item(&json_items, "lustre_path",
422                                          LLAPI_JSON_STRING, lustre_path);
423                 if (rc < 0)
424                         goto err;
425
426                 rc = llapi_json_add_item(&json_items, "total_bytes",
427                                          LLAPI_JSON_BIGNUM, &total);
428                 if (rc < 0)
429                         goto err;
430         }
431
432         if (progress_type == CT_RUNNING) {
433                 rc = llapi_json_add_item(&json_items, "current_bytes",
434                                          LLAPI_JSON_BIGNUM, &current);
435                 if (rc < 0)
436                         goto err;
437         }
438
439 cancel:
440         rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
441                                  (char *)llapi_hsm_ct_ev2str(hai->hai_action +
442                                                              progress_type));
443         if (rc < 0)
444                 goto err;
445
446         rc = llapi_hsm_write_json_event(&json_items);
447         if (rc < 0)
448                 goto err;
449
450         goto out_free;
451
452 err:
453         llapi_error(LLAPI_MSG_ERROR, rc, "error in "
454                     "llapi_hsm_log_ct_progress()");
455
456 out_free:
457         if (json_items != NULL)
458                 llapi_json_destroy_list(&json_items);
459
460         return rc;
461 }
462
463 /**
464  * Given a path to a FIFO, create a filehandle for nonblocking writes to it.
465  * Intended to be used for copytool monitoring processes that read an
466  * event stream from the FIFO. Events written in the absence of a reader
467  * are lost.
468  *
469  * \param path               Path to monitor FIFO.
470  *
471  * \retval 0 on success.
472  * \retval -errno on error.
473  */
474 int llapi_hsm_register_event_fifo(const char *path)
475 {
476         int read_fd;
477         struct stat statbuf;
478         struct sigaction ignore_action;
479         int rc;
480
481         /* Create the FIFO if necessary. */
482         if ((mkfifo(path, 0644) < 0) && (errno != EEXIST)) {
483                 llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed", path);
484                 return -errno;
485         }
486         if (errno == EEXIST) {
487                 if (stat(path, &statbuf) < 0) {
488                         llapi_error(LLAPI_MSG_ERROR, errno, "mkfifo(%s) failed",
489                                     path);
490                         return -errno;
491                 }
492                 if (!S_ISFIFO(statbuf.st_mode) ||
493                     ((statbuf.st_mode & 0777) != 0644)) {
494                         llapi_error(LLAPI_MSG_ERROR, errno, "%s exists but is "
495                                     "not a pipe or has a wrong mode", path);
496                         return -errno;
497                 }
498         } else {
499                 created_hsm_event_fifo = true;
500         }
501
502         /* Open the FIFO for read so that the subsequent open for write
503          * doesn't immediately fail. */
504         read_fd = open(path, O_RDONLY | O_NONBLOCK);
505         if (read_fd < 0) {
506                 llapi_error(LLAPI_MSG_ERROR, errno,
507                             "cannot open(%s) for read", path);
508                 return -errno;
509         }
510
511         /* Open the FIFO for writes, but don't block on waiting
512          * for a reader. */
513         llapi_hsm_event_fd = open(path, O_WRONLY | O_NONBLOCK);
514         rc = -errno;
515
516         /* Now close the reader. An external monitoring process can
517          * now open the FIFO for reads. If no reader comes along the
518          * events are lost. NOTE: Only one reader at a time! */
519         close(read_fd);
520
521         if (llapi_hsm_event_fd < 0) {
522                 llapi_error(LLAPI_MSG_ERROR, -rc,
523                             "cannot open(%s) for write", path);
524                 return rc;
525         }
526
527         /* Ignore SIGPIPEs -- can occur if the reader goes away. */
528         memset(&ignore_action, 0, sizeof(ignore_action));
529         ignore_action.sa_handler = SIG_IGN;
530         sigemptyset(&ignore_action.sa_mask);
531         sigaction(SIGPIPE, &ignore_action, NULL);
532
533         return 0;
534 }
535
536 /**
537  * Given a path to a FIFO, close its filehandle and delete the FIFO.
538  *
539  * \param path               Path to monitor FIFO.
540  *
541  * \retval 0 on success.
542  * \retval -errno on error.
543  */
544 int llapi_hsm_unregister_event_fifo(const char *path)
545 {
546         /* Noop unless the event fd was initialized */
547         if (llapi_hsm_event_fd < 0)
548                 return 0;
549
550         if (close(llapi_hsm_event_fd) < 0)
551                 return -errno;
552
553         if (created_hsm_event_fifo) {
554                 unlink(path);
555                 created_hsm_event_fifo = false;
556         }
557
558         llapi_hsm_event_fd = -1;
559
560         return 0;
561 }
562
563 /**
564  * Custom logging callback to be used when a monitoring FIFO has been
565  * registered. Formats log entries as JSON events suitable for
566  * consumption by a copytool monitoring process.
567  *
568  * \param level              The message loglevel.
569  * \param _rc                The returncode associated with the message.
570  * \param fmt                The message format string.
571  * \param args               Arguments to be formatted by the format string.
572  *
573  * \retval None.
574  */
575 void llapi_hsm_log_error(enum llapi_message_level level, int _rc,
576                          const char *fmt, va_list args)
577 {
578         int                             rc;
579         int                             msg_len;
580         int                             real_level;
581         char                            *msg = NULL;
582         va_list                         args2;
583         struct llapi_json_item_list     *json_items;
584
585         /* Noop unless the event fd was initialized */
586         if (llapi_hsm_event_fd < 0)
587                 return;
588
589         rc = llapi_json_init_list(&json_items);
590         if (rc < 0)
591                 goto err;
592
593         if ((level & LLAPI_MSG_NO_ERRNO) == 0) {
594                 rc = llapi_json_add_item(&json_items, "errno",
595                                          LLAPI_JSON_INTEGER,
596                                          &_rc);
597                 if (rc < 0)
598                         goto err;
599
600                 rc = llapi_json_add_item(&json_items, "error",
601                                          LLAPI_JSON_STRING,
602                                          strerror(abs(_rc)));
603                 if (rc < 0)
604                         goto err;
605         }
606
607         va_copy(args2, args);
608         msg_len = vsnprintf(NULL, 0, fmt, args2) + 1;
609         va_end(args2);
610         if (msg_len >= 0) {
611                 msg = (char *) alloca(msg_len);
612                 if (msg == NULL) {
613                         rc = -ENOMEM;
614                         goto err;
615                 }
616
617                 rc = vsnprintf(msg, msg_len, fmt, args);
618                 if (rc < 0)
619                         goto err;
620
621                 rc = llapi_json_add_item(&json_items, "message",
622                                          LLAPI_JSON_STRING,
623                                          msg);
624                 if (rc < 0)
625                         goto err;
626         } else {
627                 rc = llapi_json_add_item(&json_items, "message",
628                                          LLAPI_JSON_STRING,
629                                          "INTERNAL ERROR: message failed");
630                 if (rc < 0)
631                         goto err;
632         }
633
634         real_level = level & LLAPI_MSG_NO_ERRNO;
635         real_level = real_level > 0 ? level - LLAPI_MSG_NO_ERRNO : level;
636
637         rc = llapi_json_add_item(&json_items, "level", LLAPI_JSON_STRING,
638                                  (void *)llapi_msg_level2str(real_level));
639         if (rc < 0)
640                 goto err;
641
642         rc = llapi_json_add_item(&json_items, "event_type", LLAPI_JSON_STRING,
643                                  "LOGGED_MESSAGE");
644         if (rc < 0)
645                 goto err;
646
647         rc = llapi_hsm_write_json_event(&json_items);
648         if (rc < 0)
649                 goto err;
650
651         goto out_free;
652
653 err:
654         /* Write directly to stderr to avoid llapi_error, which now
655          * emits JSON event messages. */
656         fprintf(stderr, "\nFATAL ERROR IN llapi_hsm_log_error(): rc %d,", rc);
657
658 out_free:
659         if (json_items != NULL)
660                 llapi_json_destroy_list(&json_items);
661 }
662
663 /** Register a copytool
664  * \param[out] priv             Opaque private control structure
665  * \param mnt                   Lustre filesystem mount point
666  * \param archive_count         Number of valid archive IDs in \a archives
667  * \param archives              Which archive numbers this copytool is
668  *                              responsible for
669  * \param rfd_flags             flags applied to read fd of pipe
670  *                              (e.g. O_NONBLOCK)
671  *
672  * \retval 0 on success.
673  * \retval -errno on error.
674  */
675 int llapi_hsm_copytool_register(struct hsm_copytool_private **priv,
676                                 const char *mnt, int archive_count,
677                                 int *archives, int rfd_flags)
678 {
679         struct hsm_copytool_private     *ct;
680         int                              rc;
681
682         if (archive_count > 0 && archives == NULL) {
683                 llapi_err_noerrno(LLAPI_MSG_ERROR,
684                                   "NULL archive numbers");
685                 return -EINVAL;
686         }
687
688         for (rc = 0; rc < archive_count; rc++) {
689                 /* in the list we have an all archive wildcard
690                  * so move to all archives mode
691                  */
692                 if (archives[rc] == 0) {
693                         archive_count = 0;
694                         break;
695                 }
696         }
697
698         ct = calloc(1, sizeof(*ct));
699         if (ct == NULL)
700                 return -ENOMEM;
701
702         ct->magic = CT_PRIV_MAGIC;
703         ct->mnt_fd = -1;
704         ct->open_by_fid_fd = -1;
705
706         ct->mnt = strdup(mnt);
707         if (ct->mnt == NULL) {
708                 rc = -ENOMEM;
709                 goto out_err;
710         }
711
712         ct->kuch = calloc(1, HAL_MAXSIZE + sizeof(*ct->kuch));
713         if (ct->kuch == NULL) {
714                 rc = -ENOMEM;
715                 goto out_err;
716         }
717
718         ct->mnt_fd = open(ct->mnt, O_RDONLY);
719         if (ct->mnt_fd < 0) {
720                 rc = -errno;
721                 goto out_err;
722         }
723
724         ct->open_by_fid_fd = openat(ct->mnt_fd, OPEN_BY_FID_PATH, O_RDONLY);
725         if (ct->open_by_fid_fd < 0) {
726                 rc = -errno;
727                 goto out_err;
728         }
729
730         ct->kuc = malloc(sizeof(*ct) + archive_count * sizeof(__u32));
731         if (ct->kuc == NULL) {
732                 rc = -ENOMEM;
733                 goto out_err;
734         }
735
736         ct->kuc->lk_rfd = LK_NOFD;
737         ct->kuc->lk_wfd = LK_NOFD;
738
739         rc = libcfs_ukuc_start(ct->kuc, KUC_GRP_HSM, rfd_flags);
740         if (rc < 0)
741                 goto out_free_kuc;
742
743         ct->kuc->lk_flags = LK_FLG_DATANR;
744         ct->kuc->lk_data_count = archive_count;
745         for (rc = 0; rc < archive_count; rc++) {
746                 if (archives[rc] < 0) {
747                         llapi_err_noerrno(LLAPI_MSG_ERROR, "%d requested when "
748                                           "archive id >= 0 is supported",
749                                           archives[rc]);
750                         rc = -EINVAL;
751                         goto out_kuc;
752                 }
753
754                 ct->kuc->lk_data[rc] = archives[rc];
755         }
756
757         rc = ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, ct->kuc);
758         if (rc < 0) {
759                 rc = -errno;
760                 llapi_error(LLAPI_MSG_ERROR, rc,
761                             "cannot start copytool on '%s'", mnt);
762                 goto out_kuc;
763         }
764
765         llapi_hsm_log_ct_registration(&ct, CT_REGISTER);
766
767         /* Only the kernel reference keeps the write side open */
768         close(ct->kuc->lk_wfd);
769         ct->kuc->lk_wfd = LK_NOFD;
770         *priv = ct;
771
772         return 0;
773
774 out_kuc:
775         /* cleanup the kuc channel */
776         libcfs_ukuc_stop(ct->kuc);
777
778 out_free_kuc:
779         free(ct->kuc);
780
781 out_err:
782         if (!(ct->mnt_fd < 0))
783                 close(ct->mnt_fd);
784
785         if (!(ct->open_by_fid_fd < 0))
786                 close(ct->open_by_fid_fd);
787
788         free(ct->mnt);
789
790         free(ct->kuch);
791
792         free(ct);
793
794         return rc;
795 }
796
797 /** Deregister a copytool
798  * Note: under Linux, until llapi_hsm_copytool_unregister is called
799  * (or the program is killed), the libcfs module will be referenced
800  * and unremovable, even after Lustre services stop.
801  */
802 int llapi_hsm_copytool_unregister(struct hsm_copytool_private **priv)
803 {
804         struct hsm_copytool_private *ct;
805
806         if (priv == NULL || *priv == NULL)
807                 return -EINVAL;
808
809         ct = *priv;
810         if (ct->magic != CT_PRIV_MAGIC)
811                 return -EINVAL;
812
813         /* Close the read side of the KUC pipe. This should be done
814          * before unregistering to avoid deadlock: a ldlm_cb thread
815          * enters libcfs_kkuc_group_put() acquires kg_sem and blocks
816          * in pipe_write() due to full pipe; then we attempt to
817          * unregister and block on kg_sem. */
818         libcfs_ukuc_stop(ct->kuc);
819
820         /* Tell the kernel to stop sending us messages */
821         ct->kuc->lk_flags = LK_FLG_STOP;
822         ioctl(ct->mnt_fd, LL_IOC_HSM_CT_START, ct->kuc);
823
824         llapi_hsm_log_ct_registration(&ct, CT_UNREGISTER);
825
826         close(ct->open_by_fid_fd);
827         close(ct->mnt_fd);
828         free(ct->mnt);
829         free(ct->kuch);
830         free(ct->kuc);
831         free(ct);
832         *priv = NULL;
833
834         return 0;
835 }
836
837 /** Returns a file descriptor to poll/select on.
838  * \param ct Opaque private control structure
839  * \retval -EINVAL on error
840  * \retval the file descriptor for reading HSM events from the kernel
841  */
842 int llapi_hsm_copytool_get_fd(struct hsm_copytool_private *ct)
843 {
844         if (ct == NULL || ct->magic != CT_PRIV_MAGIC)
845                 return -EINVAL;
846
847         return libcfs_ukuc_get_rfd(ct->kuc);
848 }
849
850 /** Wait for the next hsm_action_list
851  * \param ct Opaque private control structure
852  * \param halh Action list handle, will be allocated here
853  * \param msgsize Number of bytes in the message, will be set here
854  * \return 0 valid message received; halh and msgsize are set
855  *         <0 error code
856  * Note: The application must not call llapi_hsm_copytool_recv until it has
857  * cleared the data in ct->kuch from the previous call.
858  */
859 int llapi_hsm_copytool_recv(struct hsm_copytool_private *ct,
860                             struct hsm_action_list **halh, int *msgsize)
861 {
862         struct kuc_hdr          *kuch;
863         struct hsm_action_list  *hal;
864         int                      rc = 0;
865
866         if (ct == NULL || ct->magic != CT_PRIV_MAGIC)
867                 return -EINVAL;
868
869         if (halh == NULL || msgsize == NULL)
870                 return -EINVAL;
871
872         kuch = ct->kuch;
873
874 repeat:
875         rc = libcfs_ukuc_msg_get(ct->kuc, (char *)kuch,
876                                  HAL_MAXSIZE + sizeof(*kuch),
877                                  KUC_TRANSPORT_HSM);
878         if (rc < 0)
879                 goto out_err;
880
881         /* Handle generic messages */
882         if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
883             kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
884                 rc = -ESHUTDOWN;
885                 goto out_err;
886         }
887
888         if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
889             kuch->kuc_msgtype != HMT_ACTION_LIST) {
890                 llapi_err_noerrno(LLAPI_MSG_ERROR,
891                                   "Unknown HSM message type %d:%d\n",
892                                   kuch->kuc_transport, kuch->kuc_msgtype);
893                 rc = -EPROTO;
894                 goto out_err;
895         }
896
897         if (kuch->kuc_msglen < sizeof(*kuch) + sizeof(*hal)) {
898                 llapi_err_noerrno(LLAPI_MSG_ERROR, "Short HSM message %d",
899                                   kuch->kuc_msglen);
900                 rc = -EPROTO;
901                 goto out_err;
902         }
903
904         /* Our message is a hsm_action_list. Use pointer math to skip
905         * kuch_hdr and point directly to the message payload.
906         */
907         hal = (struct hsm_action_list *)(kuch + 1);
908
909         /* Check that we have registered for this archive #
910          * if 0 registered, we serve any archive */
911         if (ct->kuc != NULL && ct->kuc->lk_data_count != 0) {
912                 int i;
913
914                 for (i = 0; i < ct->kuc->lk_data_count; i++) {
915                         if (hal->hal_archive_id == ct->kuc->lk_data[i])
916                                 break;
917                 }
918
919                 if (i >= ct->kuc->lk_data_count)
920                         goto repeat;
921         }
922
923         *halh = hal;
924         *msgsize = kuch->kuc_msglen - sizeof(*kuch);
925         return 0;
926
927 out_err:
928         *halh = NULL;
929         *msgsize = 0;
930         return rc;
931 }
932
933 /** Get parent path from mount point and fid.
934  *
935  * \param mnt        Filesystem root path.
936  * \param fid        Object FID.
937  * \param parent     Destination buffer.
938  * \param parent_len Destination buffer size.
939  * \return 0 on success.
940  */
941 static int fid_parent(const struct hsm_copytool_private *ct,
942                       const struct lu_fid *fid, char *parent, size_t parent_len)
943 {
944         int              rc;
945         int              linkno = 0;
946         long long        recno = -1;
947         char             file[PATH_MAX];
948         char            *ptr;
949
950         rc = llapi_fid2path_at(ct->mnt_fd, fid, file, sizeof(file),
951                                &recno, &linkno);
952         if (rc < 0)
953                 return rc;
954
955         /* fid2path returns a relative path */
956         rc = snprintf(parent, parent_len, "%s/%s", ct->mnt, file);
957         if (rc >= parent_len)
958                 return -ENAMETOOLONG;
959
960         /* remove file name */
961         ptr = strrchr(parent, '/');
962         if (ptr == NULL || ptr == parent) {
963                 rc = -EINVAL;
964         } else {
965                 *ptr = '\0';
966                 rc = 0;
967         }
968
969         return rc;
970 }
971
972 static int ct_open_by_fid(const struct hsm_copytool_private *ct,
973                           const struct lu_fid *fid, int open_flags)
974 {
975         char fid_name[FID_NOBRACE_LEN + 1];
976         int fd;
977
978         snprintf(fid_name, sizeof(fid_name), DFID_NOBRACE, PFID(fid));
979
980         fd = openat(ct->open_by_fid_fd, fid_name, open_flags);
981         return fd < 0 ? -errno : fd;
982 }
983
984 /**
985  * Get metadata attributes of file by FID.
986  *
987  * Use the IOC_MDC_GETFILEINFO ioctl (to send a MDS_GETATTR_NAME RPC)
988  * to get the attributes of the file identified by \a fid. This
989  * returns only the attributes stored on the MDT and avoids taking
990  * layout locks or accessing OST objects. It also bypasses the inode
991  * cache. Attributes are returned in \a st.
992  */
993 static int ct_md_getattr(const struct hsm_copytool_private *ct,
994                          const struct lu_fid *fid,
995                          lstatx_t *stx)
996 {
997         struct lov_user_mds_data *lmd;
998         char fname[FID_NOBRACE_LEN + 1] = "";
999         size_t lmd_size;
1000         int rc;
1001
1002         rc = snprintf(fname, sizeof(fname), DFID_NOBRACE, PFID(fid));
1003         if (rc < 0)
1004                 return rc;
1005         if (rc >= sizeof(fname) || rc == 0)
1006                 return -EINVAL;
1007
1008         lmd_size = offsetof(typeof(*lmd), lmd_lmm) +
1009                 lov_user_md_size(LOV_MAX_STRIPE_COUNT, LOV_USER_MAGIC_V3);
1010
1011         if (lmd_size < offsetof(typeof(*lmd), lmd_lmm) + XATTR_SIZE_MAX)
1012                 lmd_size = offsetof(typeof(*lmd), lmd_lmm) + XATTR_SIZE_MAX;
1013
1014         lmd = malloc(lmd_size);
1015         if (lmd == NULL)
1016                 return -ENOMEM;
1017
1018         rc = get_lmd_info_fd(fname, ct->open_by_fid_fd, -1,
1019                              lmd, lmd_size, GET_LMD_INFO);
1020         if (rc)
1021                 goto out;
1022
1023         *stx = lmd->lmd_stx;
1024 out:
1025         free(lmd);
1026
1027         return rc;
1028 }
1029
1030 /** Create the destination volatile file for a restore operation.
1031  *
1032  * \param hcp        Private copyaction handle.
1033  * \param mdt_index  MDT index where to create the volatile file.
1034  * \param flags      Volatile file creation flags.
1035  * \return 0 on success.
1036  */
1037 static int create_restore_volatile(struct hsm_copyaction_private *hcp,
1038                                    int mdt_index, int open_flags)
1039 {
1040         const struct hsm_copytool_private *ct = hcp->ct_priv;
1041         struct hsm_action_item *hai = &hcp->copy.hc_hai;
1042         char parent[PATH_MAX + 1];
1043         int fd;
1044         int rc;
1045
1046         rc = fid_parent(ct, &hai->hai_fid, parent, sizeof(parent));
1047         if (rc < 0) {
1048                 /* fid_parent() failed, try to keep on going */
1049                 llapi_error(LLAPI_MSG_ERROR, rc,
1050                             "cannot get parent path to restore "DFID" "
1051                             "using '%s'", PFID(&hai->hai_fid), ct->mnt);
1052                 snprintf(parent, sizeof(parent), "%s", ct->mnt);
1053         }
1054
1055         if (hcp->source_fd < 0) {
1056                 fd = llapi_create_volatile_idx(parent, mdt_index, open_flags);
1057         } else {
1058                 /* We need to insert source_fd in volatile file name, so open
1059                  * it manually.
1060                  */
1061                 char file_path[PATH_MAX];
1062                 unsigned int rnumber;
1063
1064                 do {
1065                         rnumber = random();
1066                         if (mdt_index == -1)
1067                                 rc = snprintf(file_path, sizeof(file_path),
1068                                        "%s/"LUSTRE_VOLATILE_HDR"::%.4X:fd=%.2d",
1069                                        parent, rnumber, hcp->source_fd);
1070                         else
1071                                 rc = snprintf(file_path, sizeof(file_path),
1072                                    "%s/"LUSTRE_VOLATILE_HDR":%.4X:%.4X:fd=%.2d",
1073                                    parent, mdt_index, rnumber, hcp->source_fd);
1074                         if (rc < 0 || rc >= sizeof(file_path)) {
1075                                 fd = -ENAMETOOLONG;
1076                                 break;
1077                         }
1078
1079                         /*
1080                          * Either open O_WRONLY or O_RDWR, creating RDONLY
1081                          * is non-sensical here.
1082                          */
1083                         if ((open_flags & O_ACCMODE) == O_RDONLY)
1084                                 open_flags = O_RDWR | (open_flags & ~O_ACCMODE);
1085                         open_flags |= O_CREAT | O_EXCL | O_NOFOLLOW;
1086                         fd = open(file_path, open_flags, S_IRUSR | S_IWUSR);
1087                         if (fd < 0)
1088                                 rc = -errno;
1089                         else
1090                                 (void)unlink(file_path);
1091                 } while (fd < 0 && rc == -EEXIST);
1092         }
1093         if (fd < 0)
1094                 return fd;
1095
1096         rc = fchown(fd, hcp->statx.stx_uid, hcp->statx.stx_gid);
1097         if (rc < 0)
1098                 goto err_cleanup;
1099
1100         rc = llapi_fd2fid(fd, &hai->hai_dfid);
1101         if (rc < 0)
1102                 goto err_cleanup;
1103
1104         hcp->data_fd = fd;
1105
1106         return 0;
1107
1108 err_cleanup:
1109         hcp->data_fd = -1;
1110         close(fd);
1111
1112         return rc;
1113 }
1114
1115 /** Start processing an HSM action.
1116  * Should be called by copytools just before starting handling a request.
1117  * It could be skipped if copytool only want to directly report an error,
1118  * \see llapi_hsm_action_end().
1119  *
1120  * \param hcp                Opaque action handle to be passed to
1121  *                           llapi_hsm_action_progress and llapi_hsm_action_end.
1122  * \param ct                 Copytool handle acquired at registration.
1123  * \param hai                The hsm_action_item describing the request.
1124  * \param restore_mdt_index  On restore: MDT index where to create the volatile
1125  *                           file. Use -1 for default.
1126  * \param restore_open_flags On restore: volatile file creation mode. Use
1127  *                           O_LOV_DELAY_CREATE to manually set the LOVEA
1128  *                           afterwards.
1129  * \param is_error           Whether this call is just to report an error.
1130  *
1131  * \return 0 on success.
1132  */
1133 int llapi_hsm_action_begin(struct hsm_copyaction_private **phcp,
1134                            const struct hsm_copytool_private *ct,
1135                            const struct hsm_action_item *hai,
1136                            int restore_mdt_index, int restore_open_flags,
1137                            bool is_error)
1138 {
1139         struct hsm_copyaction_private *hcp;
1140         int fd;
1141         int rc;
1142
1143         hcp = calloc(1, sizeof(*hcp));
1144         if (hcp == NULL)
1145                 return -ENOMEM;
1146
1147         hcp->source_fd = -1;
1148         hcp->data_fd = -1;
1149         hcp->ct_priv = ct;
1150         hcp->copy.hc_hai = *hai;
1151         hcp->copy.hc_hai.hai_len = sizeof(*hai);
1152
1153         if (is_error)
1154                 goto ok_out;
1155
1156         if (hai->hai_action == HSMA_ARCHIVE) {
1157                 fd = ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
1158                                 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
1159                 if (fd < 0) {
1160                         rc = fd;
1161                         goto err_out;
1162                 }
1163
1164                 hcp->source_fd = fd;
1165         } else if (hai->hai_action == HSMA_RESTORE) {
1166                 rc = ct_md_getattr(hcp->ct_priv, &hai->hai_fid, &hcp->statx);
1167                 if (rc < 0)
1168                         goto err_out;
1169
1170                 /* Use source_fd to store fd of Lustre file identified by fid.
1171                  * This fd is appended to volatile file name, useful in case
1172                  * of encrypted file in order to copy encryption context.
1173                  */
1174                 hcp->source_fd = ct_open_by_fid(hcp->ct_priv, &hai->hai_dfid,
1175                                 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
1176                 if (hcp->source_fd < 0) {
1177                         rc = hcp->source_fd;
1178                         goto err_out;
1179                 }
1180
1181                 rc = create_restore_volatile(hcp, restore_mdt_index,
1182                                              restore_open_flags);
1183                 /* Now that volatile file has been created,
1184                  * source_fd can be closed.
1185                  */
1186                 if (hcp->source_fd >= 0) {
1187                         close(hcp->source_fd);
1188                         hcp->source_fd = -1;
1189                 }
1190                 if (rc < 0)
1191                         goto err_out;
1192         } else if (hai->hai_action == HSMA_REMOVE) {
1193                 /* Since remove is atomic there is no need to send an
1194                  * initial MDS_HSM_PROGRESS RPC.
1195                  * RW-PCC uses Lustre HSM mechanism for data synchronization.
1196                  * At the beginning of RW-PCC attach, the client tries to
1197                  * exclusively open the file by using a lease lock. A
1198                  * successful lease open ensures that the current attach
1199                  * process is the unique opener for the file.
1200                  * After taking the lease, the file data is then copied from
1201                  * OSTs into PCC and then the client closes the lease with
1202                  * with a PCC attach intent.
1203                  * However, for a file with HSM exists, archived state (i.e. a
1204                  * cached file just was detached from PCC and restore into
1205                  * OST), a HSM REMOVE request may delete the above PCC copy
1206                  * during RW-PCC attach wrongly.
1207                  * Thus, a open/close on the corresponding Lustre file is added
1208                  * for HSMA_REMOVE here to solve this conflict.
1209                  */
1210                 fd = ct_open_by_fid(hcp->ct_priv, &hai->hai_fid,
1211                                 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NONBLOCK);
1212                 if (fd < 0) {
1213                         rc = fd;
1214                         /* ignore the error in case of Remove Archive on Last
1215                          * Unlink (RAoLU).
1216                          */
1217                         if (rc == -ENOENT) {
1218                                 rc = 0;
1219                                 goto out_log;
1220                         }
1221                         goto err_out;
1222                 }
1223
1224                 hcp->source_fd = fd;
1225                 goto out_log;
1226         }
1227
1228         rc = ioctl(ct->mnt_fd, LL_IOC_HSM_COPY_START, &hcp->copy);
1229         if (rc < 0) {
1230                 rc = -errno;
1231                 goto err_out;
1232         }
1233
1234 out_log:
1235         llapi_hsm_log_ct_progress(&hcp, hai, CT_START, 0, 0);
1236
1237 ok_out:
1238         hcp->magic = CP_PRIV_MAGIC;
1239         *phcp = hcp;
1240         return 0;
1241
1242 err_out:
1243         if (!(hcp->source_fd < 0))
1244                 close(hcp->source_fd);
1245
1246         if (!(hcp->data_fd < 0))
1247                 close(hcp->data_fd);
1248
1249         free(hcp);
1250
1251         return rc;
1252 }
1253
1254 /** Terminate an HSM action processing.
1255  * Should be called by copytools just having finished handling the request.
1256  * \param hdl[in,out]  Handle returned by llapi_hsm_action_start.
1257  * \param he[in]       The final range of copied data (for copy actions).
1258  * \param errval[in]   The status code of the operation.
1259  * \param flags[in]    The flags about the termination status (HP_FLAG_RETRY if
1260  *                     the error is retryable).
1261  *
1262  * \return 0 on success.
1263  */
1264 int llapi_hsm_action_end(struct hsm_copyaction_private **phcp,
1265                          const struct hsm_extent *he, int hp_flags, int errval)
1266 {
1267         struct hsm_copyaction_private   *hcp;
1268         struct hsm_action_item          *hai;
1269         int                              rc;
1270
1271         if (phcp == NULL || *phcp == NULL || he == NULL)
1272                 return -EINVAL;
1273
1274         hcp = *phcp;
1275
1276         if (hcp->magic != CP_PRIV_MAGIC)
1277                 return -EINVAL;
1278
1279         hai = &hcp->copy.hc_hai;
1280
1281         if (hai->hai_action == HSMA_RESTORE && errval == 0) {
1282                 struct ll_futimes_3 lfu = {
1283                         .lfu_atime_sec = hcp->statx.stx_atime.tv_sec,
1284                         .lfu_atime_nsec = hcp->statx.stx_atime.tv_nsec,
1285                         .lfu_mtime_sec = hcp->statx.stx_mtime.tv_sec,
1286                         .lfu_mtime_nsec = hcp->statx.stx_mtime.tv_nsec,
1287                         .lfu_ctime_sec = hcp->statx.stx_ctime.tv_sec,
1288                         .lfu_ctime_nsec = hcp->statx.stx_ctime.tv_nsec,
1289                 };
1290
1291                 rc = fsync(hcp->data_fd);
1292                 if (rc < 0) {
1293                         errval = -errno;
1294                         goto end;
1295                 }
1296
1297                 /* Set {a,m,c}time of volatile file to that of original. */
1298                 rc = ioctl(hcp->data_fd, LL_IOC_FUTIMES_3, &lfu);
1299                 if (rc < 0) {
1300                         errval = -errno;
1301                         goto end;
1302                 }
1303         }
1304
1305 end:
1306         /* In some cases, like restore, 2 FIDs are used.
1307          * Set the right FID to use here. */
1308         if (hai->hai_action == HSMA_ARCHIVE || hai->hai_action == HSMA_RESTORE)
1309                 hai->hai_fid = hai->hai_dfid;
1310
1311         /* Fill the last missing data that will be needed by
1312          * kernel to send a hsm_progress. */
1313         hcp->copy.hc_flags  = hp_flags;
1314         hcp->copy.hc_errval = abs(errval);
1315
1316         hcp->copy.hc_hai.hai_extent = *he;
1317
1318         rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_COPY_END, &hcp->copy);
1319         if (rc) {
1320                 rc = -errno;
1321                 goto err_cleanup;
1322         }
1323
1324         llapi_hsm_log_ct_progress(&hcp, hai, CT_FINISH, 0, 0);
1325
1326 err_cleanup:
1327         if (!(hcp->source_fd < 0))
1328                 close(hcp->source_fd);
1329
1330         if (!(hcp->data_fd < 0))
1331                 close(hcp->data_fd);
1332
1333         free(hcp);
1334         *phcp = NULL;
1335
1336         return rc;
1337 }
1338
1339 /** Notify a progress in processing an HSM action.
1340  * \param hdl[in,out]   handle returned by llapi_hsm_action_start.
1341  * \param he[in]        the range of copied data (for copy actions).
1342  * \param total[in]     the expected total of copied data (for copy actions).
1343  * \param hp_flags[in]  HSM progress flags.
1344  * \return 0 on success.
1345  */
1346 int llapi_hsm_action_progress(struct hsm_copyaction_private *hcp,
1347                               const struct hsm_extent *he, __u64 total,
1348                               int hp_flags)
1349 {
1350         int                      rc;
1351         struct hsm_progress      hp;
1352         struct hsm_action_item  *hai;
1353
1354         if (hcp == NULL || he == NULL)
1355                 return -EINVAL;
1356
1357         if (hcp->magic != CP_PRIV_MAGIC)
1358                 return -EINVAL;
1359
1360         hai = &hcp->copy.hc_hai;
1361
1362         memset(&hp, 0, sizeof(hp));
1363
1364         hp.hp_cookie = hai->hai_cookie;
1365         hp.hp_flags  = hp_flags;
1366
1367         /* Progress is made on the data fid */
1368         hp.hp_fid = hai->hai_dfid;
1369         hp.hp_extent = *he;
1370
1371         rc = ioctl(hcp->ct_priv->mnt_fd, LL_IOC_HSM_PROGRESS, &hp);
1372         if (rc < 0)
1373                 rc = -errno;
1374
1375         llapi_hsm_log_ct_progress(&hcp, hai, CT_RUNNING, total, he->length);
1376
1377         return rc;
1378 }
1379
1380 /** Get the fid of object to be used for copying data.
1381  * @return error code if the action is not a copy operation.
1382  */
1383 int llapi_hsm_action_get_dfid(const struct hsm_copyaction_private *hcp,
1384                               struct lu_fid *fid)
1385 {
1386         const struct hsm_action_item    *hai = &hcp->copy.hc_hai;
1387
1388         if (hcp->magic != CP_PRIV_MAGIC)
1389                 return -EINVAL;
1390
1391         if (hai->hai_action != HSMA_RESTORE && hai->hai_action != HSMA_ARCHIVE)
1392                 return -EINVAL;
1393
1394         *fid = hai->hai_dfid;
1395
1396         return 0;
1397 }
1398
1399 /**
1400  * Get a file descriptor to be used for copying data. It's up to the
1401  * caller to close the FDs obtained from this function.
1402  *
1403  * @retval a file descriptor on success.
1404  * @retval a negative error code on failure.
1405  */
1406 int llapi_hsm_action_get_fd(const struct hsm_copyaction_private *hcp)
1407 {
1408         const struct hsm_action_item    *hai = &hcp->copy.hc_hai;
1409         int fd;
1410
1411         if (hcp->magic != CP_PRIV_MAGIC)
1412                 return -EINVAL;
1413
1414         if (hai->hai_action == HSMA_ARCHIVE) {
1415                 fd = dup(hcp->source_fd);
1416                 return fd < 0 ? -errno : fd;
1417         } else if (hai->hai_action == HSMA_RESTORE) {
1418                 fd = dup(hcp->data_fd);
1419                 return fd < 0 ? -errno : fd;
1420         } else {
1421                 return -EINVAL;
1422         }
1423 }
1424
1425 /**
1426  * Import an existing hsm-archived file into Lustre.
1427  *
1428  * Caller must access file by (returned) newfid value from now on.
1429  *
1430  * \param dst      path to Lustre destination (e.g. /mnt/lustre/my/file).
1431  * \param archive  archive number.
1432  * \param st       struct stat buffer containing file ownership, perm, etc.
1433  * \param stripe_* Striping options.  Currently ignored, since the restore
1434  *                 operation will set the striping.  In V2, this striping might
1435  *                 be used.
1436  * \param newfid[out] Filled with new Lustre fid.
1437  */
1438 int llapi_hsm_import(const char *dst, int archive, const struct stat *st,
1439                      unsigned long long stripe_size, int stripe_offset,
1440                      int stripe_count, int stripe_pattern, char *pool_name,
1441                      struct lu_fid *newfid)
1442 {
1443         struct hsm_user_import   hui;
1444         int                      fd;
1445         int                      rc = 0;
1446
1447         if (stripe_pattern == 0)
1448                 stripe_pattern = LOV_PATTERN_RAID0;
1449
1450         /* Create a non-striped file */
1451         fd = llapi_file_open_pool(dst, O_CREAT | O_WRONLY, st->st_mode,
1452                                   stripe_size, stripe_offset, stripe_count,
1453                                   stripe_pattern | LOV_PATTERN_F_RELEASED,
1454                                   pool_name);
1455         if (fd < 0) {
1456                 llapi_error(LLAPI_MSG_ERROR, fd,
1457                             "cannot create '%s' for import", dst);
1458                 return fd;
1459         }
1460
1461         /* Get the new fid in Lustre. Caller needs to use this fid
1462            from now on. */
1463         rc = llapi_fd2fid(fd, newfid);
1464         if (rc != 0) {
1465                 llapi_error(LLAPI_MSG_ERROR, rc,
1466                             "cannot get fid of '%s' for import", dst);
1467                 goto out_unlink;
1468         }
1469
1470         hui.hui_uid = st->st_uid;
1471         hui.hui_gid = st->st_gid;
1472         hui.hui_mode = st->st_mode;
1473         hui.hui_size = st->st_size;
1474         hui.hui_archive_id = archive;
1475         hui.hui_atime = st->st_atime;
1476         hui.hui_atime_ns = st->st_atim.tv_nsec;
1477         hui.hui_mtime = st->st_mtime;
1478         hui.hui_mtime_ns = st->st_mtim.tv_nsec;
1479         rc = ioctl(fd, LL_IOC_HSM_IMPORT, &hui);
1480         if (rc != 0) {
1481                 rc = -errno;
1482                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot import '%s'", dst);
1483                 goto out_unlink;
1484         }
1485
1486 out_unlink:
1487         if (fd >= 0)
1488                 close(fd);
1489         if (rc)
1490                 unlink(dst);
1491         return rc;
1492 }
1493
1494 /**
1495  * Return the current HSM states and HSM requests related to file pointed by \a
1496  * path.
1497  *
1498  * \param hus  Should be allocated by caller. Will be filled with current file
1499  *             states.
1500  *
1501  * \retval 0 on success.
1502  * \retval -errno on error.
1503  */
1504 int llapi_hsm_state_get_fd(int fd, struct hsm_user_state *hus)
1505 {
1506         int rc;
1507
1508         rc = ioctl(fd, LL_IOC_HSM_STATE_GET, hus);
1509         /* If error, save errno value */
1510         rc = rc ? -errno : 0;
1511
1512         return rc;
1513 }
1514
1515 /**
1516  * Return the current HSM states and HSM requests related to file pointed by \a
1517  * path.
1518  *
1519  * see llapi_hsm_state_get_fd() for args use and return
1520  */
1521 int llapi_hsm_state_get(const char *path, struct hsm_user_state *hus)
1522 {
1523         int fd;
1524         int rc;
1525
1526         fd = open(path, O_RDONLY | O_NONBLOCK);
1527         if (fd < 0)
1528                 return -errno;
1529
1530         rc = llapi_hsm_state_get_fd(fd, hus);
1531
1532         close(fd);
1533         return rc;
1534 }
1535
1536 /**
1537  * Set HSM states of file pointed by \a fd
1538  *
1539  * Using the provided bitmasks, the current HSM states for this file will be
1540  * changed. \a archive_id could be used to change the archive number also. Set
1541  * it to 0 if you do not want to change it.
1542  *
1543  * \param setmask      Bitmask for flag to be set.
1544  * \param clearmask    Bitmask for flag to be cleared.
1545  * \param archive_id  Archive number identifier to use. 0 means no change.
1546  *
1547  * \retval 0 on success.
1548  * \retval -errno on error.
1549  */
1550 int llapi_hsm_state_set_fd(int fd, __u64 setmask, __u64 clearmask,
1551                            __u32 archive_id)
1552 {
1553         struct hsm_state_set     hss;
1554         int                      rc;
1555
1556         hss.hss_valid = HSS_SETMASK|HSS_CLEARMASK;
1557         hss.hss_setmask = setmask;
1558         hss.hss_clearmask = clearmask;
1559         /* Change archive_id if provided. We can only change
1560          * to set something different than 0. */
1561         if (archive_id > 0) {
1562                 hss.hss_valid |= HSS_ARCHIVE_ID;
1563                 hss.hss_archive_id = archive_id;
1564         }
1565         rc = ioctl(fd, LL_IOC_HSM_STATE_SET, &hss);
1566         /* If error, save errno value */
1567         rc = rc ? -errno : 0;
1568
1569         return rc;
1570 }
1571
1572 /**
1573  * Set HSM states of file pointed by \a path.
1574  *
1575  * see llapi_hsm_state_set_fd() for args use and return
1576  */
1577 int llapi_hsm_state_set(const char *path, __u64 setmask, __u64 clearmask,
1578                         __u32 archive_id)
1579 {
1580         int fd;
1581         int rc;
1582
1583         fd = open(path, O_WRONLY | O_LOV_DELAY_CREATE | O_NONBLOCK);
1584         if (fd < 0)
1585                 return -errno;
1586
1587         rc = llapi_hsm_state_set_fd(fd, setmask, clearmask, archive_id);
1588
1589         close(fd);
1590         return rc;
1591 }
1592
1593 /**
1594  * Return the current HSM request related to file pointed by \a path.
1595  *
1596  * \param hca  Should be allocated by caller. Will be filled with current file
1597  *             actions.
1598  *
1599  * \retval 0 on success.
1600  * \retval -errno on error.
1601  */
1602 int llapi_hsm_current_action(const char *path, struct hsm_current_action *hca)
1603 {
1604         int fd;
1605         int rc;
1606
1607         fd = open(path, O_RDONLY | O_NONBLOCK);
1608         if (fd < 0)
1609                 return -errno;
1610
1611         rc = ioctl(fd, LL_IOC_HSM_ACTION, hca);
1612         /* If error, save errno value */
1613         rc = rc ? -errno : 0;
1614
1615         close(fd);
1616         return rc;
1617 }
1618
1619 /**
1620  * Allocate a hsm_user_request with the specified carateristics.
1621  * This structure should be freed with free().
1622  *
1623  * \return an allocated structure on success, NULL otherwise.
1624  */
1625 struct hsm_user_request *llapi_hsm_user_request_alloc(int itemcount,
1626                                                       int data_len)
1627 {
1628         int len = 0;
1629
1630         len += sizeof(struct hsm_user_request);
1631         len += sizeof(struct hsm_user_item) * itemcount;
1632         len += data_len;
1633
1634         return (struct hsm_user_request *)calloc(1, len);
1635 }
1636
1637 /**
1638  * Send a HSM request to Lustre, described in \param request.
1639  *
1640  * \param path    Fullpath to the file to operate on.
1641  * \param request The request, allocated with llapi_hsm_user_request_alloc().
1642  *
1643  * \return 0 on success, an error code otherwise.
1644  */
1645 int llapi_hsm_request(const char *path, const struct hsm_user_request *request)
1646 {
1647         int rc;
1648         int fd;
1649
1650         rc = llapi_root_path_open(path, &fd);
1651         if (rc)
1652                 return rc;
1653
1654         rc = ioctl(fd, LL_IOC_HSM_REQUEST, request);
1655         /* If error, save errno value */
1656         rc = rc ? -errno : 0;
1657
1658         close(fd);
1659         return rc;
1660 }
1661