Whamcloud - gitweb
f0653598f1df67060b3dea6a79c3b5b9cee2bdae
[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  * 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
14  *
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.
19  *
20  * LGPL HEADER END
21  */
22 /*
23  * lustre/utils/liblustreapi_hsm.c
24  *
25  * lustreapi library for hsm calls
26  *
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  */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stddef.h>
36 #include <sys/ioctl.h>
37 #include <unistd.h>
38 #include <malloc.h>
39 #include <errno.h>
40 #include <dirent.h>
41 #include <stdarg.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <utime.h>
45 #include <sys/syscall.h>
46 #include <fnmatch.h>
47 #include <glob.h>
48 #ifdef HAVE_LINUX_UNISTD_H
49 #include <linux/unistd.h>
50 #else
51 #include <unistd.h>
52 #endif
53
54 #include <liblustre.h>
55 #include <lnet/lnetctl.h>
56 #include <obd.h>
57 #include <obd_lov.h>
58 #include <lustre/lustreapi.h>
59 #include "lustreapi_internal.h"
60
61 /****** HSM Copytool API ********/
62 #define CT_PRIV_MAGIC 0xC0BE2001
63 struct hsm_copytool_private {
64         int                      magic;
65         char                    *fsname;
66         lustre_kernelcomm        kuc;
67         __u32                    archives;
68 };
69
70 #include <libcfs/libcfs.h>
71
72 /** Register a copytool
73  * \param[out] priv Opaque private control structure
74  * \param fsname Lustre filesystem
75  * \param flags Open flags, currently unused (e.g. O_NONBLOCK)
76  * \param archive_count
77  * \param archives Which archive numbers this copytool is responsible for
78  */
79 int llapi_hsm_copytool_start(void **priv, char *fsname, int flags,
80                              int archive_count, int *archives)
81 {
82         struct hsm_copytool_private     *ct;
83         int                              rc;
84
85         if (archive_count > 0 && archives == NULL) {
86                 llapi_err_noerrno(LLAPI_MSG_ERROR,
87                                   "NULL archive numbers");
88                 return -EINVAL;
89         }
90
91         ct = calloc(1, sizeof(*ct));
92         if (ct == NULL)
93                 return -ENOMEM;
94
95         ct->fsname = malloc(strlen(fsname) + 1);
96         if (ct->fsname == NULL) {
97                 rc = -ENOMEM;
98                 goto out_err;
99         }
100         strcpy(ct->fsname, fsname);
101         ct->magic = CT_PRIV_MAGIC;
102
103         /* no archives specified means "match all". */
104         ct->archives = 0;
105         for (rc = 0; rc < archive_count; rc++) {
106                 if (archives[rc] > 8 * sizeof(ct->archives)) {
107                         llapi_err_noerrno(LLAPI_MSG_ERROR,
108                                           "Maximum of %d archives supported",
109                                           8 * sizeof(ct->archives));
110                         goto out_err;
111                 }
112                 /* in the list we have a all archive wildcard
113                  * so move to all archives mode
114                  */
115                 if (archives[rc] == 0) {
116                         ct->archives = 0;
117                         archive_count = 0;
118                         break;
119                 }
120                 ct->archives |= (1 << (archives[rc] - 1));
121         }
122
123         rc = libcfs_ukuc_start(&ct->kuc, KUC_GRP_HSM);
124         if (rc < 0)
125                 goto out_err;
126
127         /* Storing archive(s) in lk_data; see mdc_ioc_hsm_ct_start */
128         ct->kuc.lk_data = ct->archives;
129         rc = root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL,
130                         WANT_ERROR);
131         /* ignore if it was already registered on coordinator */
132         if (rc == -EEXIST)
133                 rc = 0;
134         /* Only the kernel reference keeps the write side open */
135         close(ct->kuc.lk_wfd);
136         ct->kuc.lk_wfd = 0;
137         if (rc < 0)
138                 goto out_err;
139
140         *priv = ct;
141         return 0;
142
143 out_err:
144         if (ct->fsname)
145                 free(ct->fsname);
146         free(ct);
147         return rc;
148 }
149
150 /** Deregister a copytool
151  * Note: under Linux, until llapi_hsm_copytool_fini is called (or the program is
152  * killed), the libcfs module will be referenced and unremovable,
153  * even after Lustre services stop.
154  */
155 int llapi_hsm_copytool_fini(void **priv)
156 {
157         struct hsm_copytool_private *ct;
158
159         ct = (struct hsm_copytool_private *)priv;
160         if (!ct || (ct->magic != CT_PRIV_MAGIC))
161                 return -EINVAL;
162
163         /* Tell the kernel to stop sending us messages */
164         ct->kuc.lk_flags = LK_FLG_STOP;
165         root_ioctl(ct->fsname, LL_IOC_HSM_CT_START, &(ct->kuc), NULL, 0);
166
167         /* Shut down the kernelcomms */
168         libcfs_ukuc_stop(&ct->kuc);
169
170         free(ct->fsname);
171         free(ct);
172         *priv = NULL;
173         return 0;
174 }
175
176 /** Wait for the next hsm_action_list
177  * \param priv Opaque private control structure
178  * \param halh Action list handle, will be allocated here
179  * \param msgsize Number of bytes in the message, will be set here
180  * \return 0 valid message received; halh and msgsize are set
181  *         <0 error code
182  */
183 int llapi_hsm_copytool_recv(void *priv, struct hsm_action_list **halh,
184                             int *msgsize)
185 {
186         struct hsm_copytool_private     *ct;
187         struct kuc_hdr                  *kuch;
188         struct hsm_action_list          *hal;
189         int                              rc = 0;
190
191         ct = (struct hsm_copytool_private *)priv;
192         if (!ct || (ct->magic != CT_PRIV_MAGIC))
193                 return -EINVAL;
194         if (halh == NULL || msgsize == NULL)
195                 return -EINVAL;
196
197         kuch = malloc(HAL_MAXSIZE + sizeof(*kuch));
198         if (kuch == NULL)
199                 return -ENOMEM;
200
201         rc = libcfs_ukuc_msg_get(&ct->kuc, (char *)kuch,
202                                  HAL_MAXSIZE + sizeof(*kuch),
203                                  KUC_TRANSPORT_HSM);
204         if (rc < 0)
205                 goto out_free;
206
207         /* Handle generic messages */
208         if (kuch->kuc_transport == KUC_TRANSPORT_GENERIC &&
209             kuch->kuc_msgtype == KUC_MSG_SHUTDOWN) {
210                 rc = -ESHUTDOWN;
211                 goto out_free;
212         }
213
214         if (kuch->kuc_transport != KUC_TRANSPORT_HSM ||
215             kuch->kuc_msgtype != HMT_ACTION_LIST) {
216                 llapi_err_noerrno(LLAPI_MSG_ERROR,
217                                   "Unknown HSM message type %d:%d\n",
218                                   kuch->kuc_transport, kuch->kuc_msgtype);
219                 rc = -EPROTO;
220                 goto out_free;
221         }
222
223         if (kuch->kuc_msglen < sizeof(*kuch) + sizeof(*hal)) {
224                 llapi_err_noerrno(LLAPI_MSG_ERROR, "Short HSM message %d",
225                                   kuch->kuc_msglen);
226                 rc = -EPROTO;
227                 goto out_free;
228         }
229
230         /* Our message is a hsm_action_list. Use pointer math to skip
231         * kuch_hdr and point directly to the message payload.
232         */
233         hal = (struct hsm_action_list *)(kuch + 1);
234
235         /* Check that we have registered for this archive #
236          * if 0 registered, we serve any archive */
237         if (ct->archives &&
238             ((1 << (hal->hal_archive_num - 1)) & ct->archives) == 0) {
239                 llapi_err_noerrno(LLAPI_MSG_INFO,
240                                   "This copytool does not service archive #%d,"
241                                   " ignoring this request."
242                                   " Mask of served archive is 0x%.8X",
243                                   hal->hal_archive_num, ct->archives);
244                 rc = -EAGAIN;
245
246                 goto out_free;
247         }
248
249         *halh = hal;
250         *msgsize = kuch->kuc_msglen - sizeof(*kuch);
251         return 0;
252
253 out_free:
254         *halh = NULL;
255         *msgsize = 0;
256         free(kuch);
257         return rc;
258 }
259
260 /** Release the action list when done with it. */
261 int llapi_hsm_copytool_free(struct hsm_action_list **hal)
262 {
263         /* Reuse the llapi_changelog_free function */
264         return llapi_changelog_free((struct changelog_ext_rec **)hal);
265 }
266
267
268 /**
269  * Should be called by copytools just before starting handling a request.
270  * It could be skipped if copytool only want to directly report an error,
271  * \see llapi_hsm_copy_end().
272  *
273  * \param mnt   Mount point of the corresponding Lustre filesystem.
274  * \param hai   The hsm_action_item describing the request they will handle.
275  * \param copy  Updated by this call. Caller will passed it to
276  *              llapi_hsm_copy_end()
277  *
278  * \return 0 on success.
279  */
280 int llapi_hsm_copy_start(char *mnt, struct hsm_copy *copy,
281                          const struct hsm_action_item *hai)
282 {
283         int     fd;
284         int     rc;
285
286         if (memcpy(&copy->hc_hai, hai, sizeof(*hai)) == NULL)
287                 RETURN(-EFAULT);
288
289         rc = get_root_path(WANT_FD, NULL, &fd, mnt, -1);
290         if (rc)
291                 return rc;
292
293         rc = ioctl(fd, LL_IOC_HSM_COPY_START, copy);
294         close(fd);
295
296         return rc;
297 }
298
299 /**
300  * Should be called by copytools just having finished handling the request.
301  *
302  * \param mnt   Mount point of the corresponding Lustre filesystem.
303  * \param copy  The element used when calling llapi_hsm_copy_start()
304  * \param hp    A hsm_progress structure describing the final state of the
305  *              request.
306  *
307  * There is a special case which can be used only when the copytool cannot
308  * start the copy at all and want to directly return an error. In this case,
309  * simply fill \a hp structure and set \a copy to NULL. It is useless to call
310  * llapi_hsm_copy_start() in this case.
311  *
312  * \return 0 on success.
313  */
314 int llapi_hsm_copy_end(char *mnt, struct hsm_copy *copy,
315                        const struct hsm_progress *hp)
316 {
317         int     end_only = 0;
318         int     fd;
319         int     rc;
320
321         /* llapi_hsm_copy_start() was skipped, so alloc copy. It will
322          * only be used to give the needed progress information. */
323         if (copy == NULL) {
324                 /* This is only ok if there is an error. */
325                 if (hp->hp_errval == 0)
326                         return -EINVAL;
327
328                 copy = (struct hsm_copy *)malloc(sizeof(*copy));
329                 if (copy == NULL)
330                         return -ENOMEM;
331                 end_only = 1;
332                 copy->hc_hai.hai_cookie = hp->hp_cookie;
333                 copy->hc_hai.hai_fid = hp->hp_fid;
334                 copy->hc_hai.hai_action = HSMA_NONE;
335         }
336
337         /* Fill the last missing data that will be needed by kernel
338          * to send a hsm_progress. */
339         copy->hc_flags = hp->hp_flags;
340         copy->hc_errval = hp->hp_errval;
341         /* Update hai if it has changed since start */
342         copy->hc_hai.hai_extent = hp->hp_extent;
343
344         rc = get_root_path(WANT_FD, NULL, &fd, mnt, -1);
345         if (rc)
346                 goto out_free;
347
348         rc = ioctl(fd, LL_IOC_HSM_COPY_END, copy);
349         close(fd);
350
351 out_free:
352         if (end_only)
353                 free(copy);
354
355         return rc;
356 }
357
358 /**
359  * Copytool progress reporting.
360  *
361  * \a hp->hp_errval should be EAGAIN until action is completely finished.
362  *
363  * \return 0 on success, an error code otherwise.
364  */
365 int llapi_hsm_progress(char *mnt, struct hsm_progress *hp)
366 {
367         int     fd;
368         int     rc;
369
370         rc = get_root_path(WANT_FD, NULL, &fd, mnt, -1);
371         if (rc)
372                 return rc;
373
374         rc = ioctl(fd, LL_IOC_HSM_PROGRESS, hp);
375         /* If error, save errno value */
376         rc = rc ? -errno : 0;
377
378         close(fd);
379         return rc;
380 }
381
382 /**
383  * Import an existing hsm-archived file into Lustre.
384  *
385  * Caller must access file by (returned) newfid value from now on.
386  *
387  * \param dst      path to Lustre destination (e.g. /mnt/lustre/my/file).
388  * \param archive  archive number.
389  * \param st       struct stat buffer containing file ownership, perm, etc.
390  * \param stripe_* Striping options.  Currently ignored, since the restore
391  *                 operation will set the striping.  In V2, this striping might
392  *                 be used.
393  * \param newfid[out] Filled with new Lustre fid.
394  */
395 int llapi_hsm_import(const char *dst, int archive, struct stat *st,
396                      unsigned long long stripe_size, int stripe_offset,
397                      int stripe_count, int stripe_pattern, char *pool_name,
398                      lustre_fid *newfid)
399 {
400         struct utimbuf  time;
401         int             fd;
402         int             rc = 0;
403
404         /* Create a non-striped file */
405         fd = open(dst, O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NONBLOCK,
406                   st->st_mode);
407
408         if (fd < 0)
409                 return -errno;
410         close(fd);
411
412         /* set size on MDT */
413         if (truncate(dst, st->st_size) != 0) {
414                 rc = -errno;
415                 goto out_unlink;
416         }
417         /* Mark archived */
418         rc = llapi_hsm_state_set(dst, HS_EXISTS | HS_RELEASED | HS_ARCHIVED, 0,
419                                  archive);
420         if (rc)
421                 goto out_unlink;
422
423         /* Get the new fid in the archive. Caller needs to use this fid
424            from now on. */
425         rc = llapi_path2fid(dst, newfid);
426         if (rc)
427                 goto out_unlink;
428
429         /* Copy the file attributes */
430         time.actime = st->st_atime;
431         time.modtime = st->st_mtime;
432         if (utime(dst, &time) == -1 ||
433                 chmod(dst, st->st_mode) == -1 ||
434                 chown(dst, st->st_uid, st->st_gid) == -1) {
435                 /* we might fail here because we change perms/owner */
436                 rc = -errno;
437                 goto out_unlink;
438         }
439
440 out_unlink:
441         if (rc)
442                 unlink(dst);
443         return rc;
444 }
445
446
447 /**
448  * Return the current HSM states and HSM requests related to file pointed by \a
449  * path.
450  *
451  * \param hus  Should be allocated by caller. Will be filled with current file
452  *             states.
453  *
454  * \retval 0 on success.
455  * \retval -errno on error.
456  */
457 int llapi_hsm_state_get(const char *path, struct hsm_user_state *hus)
458 {
459         int fd;
460         int rc;
461
462         fd = open(path, O_RDONLY | O_NONBLOCK);
463         if (fd < 0)
464                 return -errno;
465
466         rc = ioctl(fd, LL_IOC_HSM_STATE_GET, hus);
467         /* If error, save errno value */
468         rc = rc ? -errno : 0;
469
470         close(fd);
471         return rc;
472 }
473
474 /**
475  * Set HSM states of file pointed by \a path.
476  *
477  * Using the provided bitmasks, the current HSM states for this file will be
478  * changed. \a archive_id could be used to change the archive number also. Set
479  * it to 0 if you do not want to change it.
480  *
481  * \param setmask      Bitmask for flag to be set.
482  * \param clearmask    Bitmask for flag to be cleared.
483  * \param archive_id  Archive number identifier to use. 0 means no change.
484  *
485  * \retval 0 on success.
486  * \retval -errno on error.
487  */
488 int llapi_hsm_state_set(const char *path, __u64 setmask, __u64 clearmask,
489                         __u32 archive_id)
490 {
491         struct hsm_state_set hss;
492         int fd;
493         int rc;
494
495         fd = open(path, O_WRONLY | O_LOV_DELAY_CREATE | O_NONBLOCK);
496         if (fd < 0)
497                 return -errno;
498
499         hss.hss_valid = HSS_SETMASK|HSS_CLEARMASK;
500         hss.hss_setmask = setmask;
501         hss.hss_clearmask = clearmask;
502         /* Change archive_id if provided. We can only change
503          * to set something different than 0. */
504         if (archive_id > 0) {
505                 hss.hss_valid |= HSS_ARCHIVE_ID;
506                 hss.hss_archive_id = archive_id;
507         }
508         rc = ioctl(fd, LL_IOC_HSM_STATE_SET, &hss);
509         /* If error, save errno value */
510         rc = rc ? -errno : 0;
511
512         close(fd);
513         return rc;
514 }
515