Whamcloud - gitweb
LU-9727 lustre: Add an additional set of 64 changelog flags.
[fs/lustre-release.git] / lustre / utils / liblustreapi_chlg.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * (C) Copyright 2017 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_chlg.c
24  *
25  * lustreapi library for filesystem changelog
26  *
27  * Author: Henri Doreau <henri.doreau@cea.fr>
28  */
29
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <poll.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39
40 #include <lustre/lustreapi.h>
41
42
43 static int chlg_dev_path(char *path, size_t path_len, const char *device)
44 {
45         int rc;
46
47         rc = snprintf(path, path_len, "/dev/changelog-%s", device);
48         if (rc < 0)
49                 return -EIO;
50
51         if (rc >= path_len)
52                 return -EOVERFLOW;
53
54         return 0;
55 }
56
57 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
58 #define CHANGELOG_BUFFER_SZ  4096
59
60 /**
61  * Record state for efficient changelog consumption.
62  * Read chunks of CHANGELOG_BUFFER_SZ bytes.
63  */
64 struct changelog_private {
65         /* Ensure that the structure is valid and initialized */
66         int                              clp_magic;
67         /* File descriptor on the changelog character device */
68         int                              clp_fd;
69         /* Changelog delivery mode */
70         enum changelog_send_flag         clp_send_flags;
71         /* Available bytes in buffer */
72         size_t                           clp_buf_len;
73         /* Current position in buffer */
74         char                            *clp_buf_pos;
75         /* Read buffer with records read from system */
76         char                             clp_buf[0];
77 };
78
79 /**
80  * Start reading from a changelog
81  *
82  * @param priv      Opaque private control structure
83  * @param flags     Start flags (e.g. CHANGELOG_FLAG_JOBID)
84  * @param device    Report changes recorded on this MDT
85  * @param startrec  Report changes beginning with this record number
86  * (just call llapi_changelog_fini when done; don't need an endrec)
87  */
88 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
89                           const char *device, long long startrec)
90 {
91         struct changelog_private *cp;
92         static bool warned_extra_flags;
93         static bool warned_jobid;
94         static bool warned_follow;
95         char cdev_path[PATH_MAX];
96         int rc;
97
98         rc = chlg_dev_path(cdev_path, sizeof(cdev_path), device);
99         if (rc != 0)
100                 return rc;
101
102         /* Set up the receiver control struct */
103         cp = calloc(1, sizeof(*cp) + CHANGELOG_BUFFER_SZ);
104         if (cp == NULL)
105                 return -ENOMEM;
106
107         cp->clp_magic = CHANGELOG_PRIV_MAGIC;
108         cp->clp_send_flags = flags;
109
110         cp->clp_buf_len = 0;
111         cp->clp_buf_pos = cp->clp_buf;
112
113         /* Set up the receiver */
114         cp->clp_fd = open(cdev_path, O_RDONLY);
115         if (cp->clp_fd < 0) {
116                 rc = -errno;
117                 goto out_free_cp;
118         }
119
120         if (startrec != 0) {
121                 off_t res;
122
123                 res = lseek(cp->clp_fd, startrec, SEEK_SET);
124                 if (res == (off_t)-1) {
125                         rc = -errno;
126                         goto out_close;
127                 }
128         }
129
130         *priv = cp;
131
132         /* CHANGELOG_FLAG_EXTRA_FLAGS will eventually become mandatory.
133          * If it wasn't specified, display a warning here.
134          * Code elsewhere will remove the corresponding extension.
135          */
136         if (!(flags & CHANGELOG_FLAG_EXTRA_FLAGS) && !warned_extra_flags) {
137                 llapi_err_noerrno(LLAPI_MSG_WARN,
138                       "warning: %s() called without CHANGELOG_FLAG_EXTRA_FLAGS",
139                       __func__);
140                 warned_extra_flags = true;
141         }
142
143         /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
144          * warning if it's missing. */
145         if (!(flags & CHANGELOG_FLAG_JOBID) && !warned_jobid) {
146                 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
147                                   "without CHANGELOG_FLAG_JOBID", __func__);
148                 warned_jobid = true;
149         }
150
151         /* Behavior expected by CHANGELOG_FLAG_FOLLOW is not implemented, warn
152          * the user and ignore it. */
153         if (flags & CHANGELOG_FLAG_FOLLOW && !warned_follow) {
154                 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called with "
155                                   "CHANGELOG_FLAG_FOLLOW (ignored)", __func__);
156                 warned_follow = true;
157         }
158
159         return 0;
160
161 out_close:
162         close(cp->clp_fd);
163 out_free_cp:
164         free(cp);
165         return rc;
166 }
167
168 /** Finish reading from a changelog */
169 int llapi_changelog_fini(void **priv)
170 {
171         struct changelog_private *cp = *priv;
172
173         if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
174                 return -EINVAL;
175
176         close(cp->clp_fd);
177         free(cp);
178         *priv = NULL;
179         return 0;
180 }
181
182 static ssize_t chlg_read_bulk(struct changelog_private *cp)
183 {
184         ssize_t rd_bytes;
185
186         if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
187                 return -EINVAL;
188
189         rd_bytes = read(cp->clp_fd, cp->clp_buf, CHANGELOG_BUFFER_SZ);
190         if (rd_bytes < 0)
191                 return -errno;
192
193         cp->clp_buf_pos = cp->clp_buf;
194         cp->clp_buf_len = rd_bytes;
195
196         return rd_bytes;
197 }
198
199 /**
200  * Returns a file descriptor to poll on.
201  *
202  * \@param[in]  priv  Opaque changelog reader structure.
203  * @return valid file descriptor on success, negated errno code on failure.
204  */
205 int llapi_changelog_get_fd(void *priv)
206 {
207         struct changelog_private *cp = priv;
208
209         if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
210                 return -EINVAL;
211
212         return cp->clp_fd;
213 }
214
215 /** Read the next changelog entry
216  * @param priv Opaque private control structure
217  * @param rech Changelog record handle; record will be allocated here
218  * @return 0 valid message received; rec is set
219  *       <0 error code
220  *       1 EOF
221  */
222 #define DEFAULT_RECORD_FMT      (CLF_VERSION | CLF_RENAME)
223 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
224 {
225         struct changelog_private *cp = priv;
226         enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
227         enum changelog_rec_extra_flags rec_extra_fmt = CLFE_INVALID;
228         struct changelog_rec *tmp;
229         int rc = 0;
230
231         if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
232                 return -EINVAL;
233
234         if (rech == NULL)
235                 return -EINVAL;
236
237         *rech = malloc(CR_MAXSIZE);
238         if (*rech == NULL)
239                 return -ENOMEM;
240
241         if (cp->clp_send_flags & CHANGELOG_FLAG_JOBID)
242                 rec_fmt |= CLF_JOBID;
243
244         if (cp->clp_send_flags & CHANGELOG_FLAG_EXTRA_FLAGS) {
245                 rec_fmt |= CLF_EXTRA_FLAGS;
246         }
247
248         if (cp->clp_buf + cp->clp_buf_len <= cp->clp_buf_pos) {
249                 ssize_t refresh;
250
251                 refresh = chlg_read_bulk(cp);
252                 if (refresh == 0) {
253                         /* EOF, CHANGELOG_FLAG_FOLLOW ignored for now LU-7659 */
254                         rc = 1;
255                         goto out_free;
256                 } else if (refresh < 0) {
257                         rc = refresh;
258                         goto out_free;
259                 }
260         }
261
262         /* TODO check changelog_rec_size */
263         tmp = (struct changelog_rec *)cp->clp_buf_pos;
264
265         memcpy(*rech, cp->clp_buf_pos,
266                changelog_rec_size(tmp) + tmp->cr_namelen);
267
268         cp->clp_buf_pos += changelog_rec_size(tmp) + tmp->cr_namelen;
269         changelog_remap_rec(*rech, rec_fmt, rec_extra_fmt);
270
271         return 0;
272
273 out_free:
274         free(*rech);
275         *rech = NULL;
276         return rc;
277 }
278
279 /** Release the changelog record when done with it. */
280 int llapi_changelog_free(struct changelog_rec **rech)
281 {
282         free(*rech);
283         *rech = NULL;
284         return 0;
285 }
286
287 int llapi_changelog_clear(const char *mdtname, const char *idstr,
288                           long long endrec)
289 {
290         char dev_path[PATH_MAX];
291         char cmd[64];
292         size_t cmd_len = sizeof(cmd);
293         int fd;
294         int rc;
295
296         if (endrec < 0) {
297                 llapi_err_noerrno(LLAPI_MSG_ERROR,
298                                   "can't purge negative records\n");
299                 return -EINVAL;
300         }
301
302         chlg_dev_path(dev_path, sizeof(dev_path), mdtname);
303
304         rc = snprintf(cmd, cmd_len, "clear:%s:%lld", idstr, endrec);
305         if (rc >= sizeof(cmd))
306                 return -EINVAL;
307
308         cmd_len = rc + 1;
309
310         fd = open(dev_path, O_WRONLY);
311         if (fd < 0) {
312                 rc = -errno;
313                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", dev_path);
314                 return rc;
315         }
316
317         rc = write(fd, cmd, cmd_len);
318         if (rc < 0) {
319                 rc = -errno;
320                 llapi_error(LLAPI_MSG_ERROR, rc,
321                             "cannot purge records for '%s'", idstr);
322                 goto out_close;
323         }
324
325         rc = 0;
326
327 out_close:
328         close(fd);
329         return rc;
330 }