Whamcloud - gitweb
LU-9430 utils: fix logic errors and putchar in sk_name2hmac()
[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_jobid;
93         static bool warned_follow;
94         char cdev_path[PATH_MAX];
95         int rc;
96
97         rc = chlg_dev_path(cdev_path, sizeof(cdev_path), device);
98         if (rc != 0)
99                 return rc;
100
101         /* Set up the receiver control struct */
102         cp = calloc(1, sizeof(*cp) + CHANGELOG_BUFFER_SZ);
103         if (cp == NULL)
104                 return -ENOMEM;
105
106         cp->clp_magic = CHANGELOG_PRIV_MAGIC;
107         cp->clp_send_flags = flags;
108
109         cp->clp_buf_len = 0;
110         cp->clp_buf_pos = cp->clp_buf;
111
112         /* Set up the receiver */
113         cp->clp_fd = open(cdev_path, O_RDONLY);
114         if (cp->clp_fd < 0) {
115                 rc = -errno;
116                 goto out_free_cp;
117         }
118
119         if (startrec != 0) {
120                 rc = lseek(cp->clp_fd, startrec, SEEK_SET);
121                 if (rc < 0) {
122                         rc = -errno;
123                         goto out_close;
124                 }
125         }
126
127         *priv = cp;
128
129         /* CHANGELOG_FLAG_JOBID will eventually become mandatory. Display a
130          * warning if it's missing. */
131         if (!(flags & CHANGELOG_FLAG_JOBID) && !warned_jobid) {
132                 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called "
133                                   "without CHANGELOG_FLAG_JOBID", __func__);
134                 warned_jobid = true;
135         }
136
137         /* Behavior expected by CHANGELOG_FLAG_FOLLOW is not implemented, warn
138          * the user and ignore it. */
139         if (flags & CHANGELOG_FLAG_FOLLOW && !warned_follow) {
140                 llapi_err_noerrno(LLAPI_MSG_WARN, "warning: %s() called with "
141                                   "CHANGELOG_FLAG_FOLLOW (ignored)", __func__);
142                 warned_follow = true;
143         }
144
145         return 0;
146
147 out_close:
148         close(cp->clp_fd);
149 out_free_cp:
150         free(cp);
151         return rc;
152 }
153
154 /** Finish reading from a changelog */
155 int llapi_changelog_fini(void **priv)
156 {
157         struct changelog_private *cp = *priv;
158
159         if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
160                 return -EINVAL;
161
162         close(cp->clp_fd);
163         free(cp);
164         *priv = NULL;
165         return 0;
166 }
167
168 static ssize_t chlg_read_bulk(struct changelog_private *cp)
169 {
170         ssize_t rd_bytes;
171
172         if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
173                 return -EINVAL;
174
175         rd_bytes = read(cp->clp_fd, cp->clp_buf, CHANGELOG_BUFFER_SZ);
176         if (rd_bytes < 0)
177                 return -errno;
178
179         cp->clp_buf_pos = cp->clp_buf;
180         cp->clp_buf_len = rd_bytes;
181
182         return rd_bytes;
183 }
184
185 /**
186  * Returns a file descriptor to poll on.
187  *
188  * \@param[in]  priv  Opaque changelog reader structure.
189  * @return valid file descriptor on success, negated errno code on failure.
190  */
191 int llapi_changelog_get_fd(void *priv)
192 {
193         struct changelog_private *cp = priv;
194
195         if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
196                 return -EINVAL;
197
198         return cp->clp_fd;
199 }
200
201 /** Read the next changelog entry
202  * @param priv Opaque private control structure
203  * @param rech Changelog record handle; record will be allocated here
204  * @return 0 valid message received; rec is set
205  *       <0 error code
206  *       1 EOF
207  */
208 #define DEFAULT_RECORD_FMT      (CLF_VERSION | CLF_RENAME)
209 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
210 {
211         struct changelog_private *cp = priv;
212         enum changelog_rec_flags rec_fmt = DEFAULT_RECORD_FMT;
213         struct changelog_rec *tmp;
214         int rc = 0;
215
216         if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
217                 return -EINVAL;
218
219         if (rech == NULL)
220                 return -EINVAL;
221
222         *rech = malloc(CR_MAXSIZE);
223         if (*rech == NULL)
224                 return -ENOMEM;
225
226         if (cp->clp_send_flags & CHANGELOG_FLAG_JOBID)
227                 rec_fmt |= CLF_JOBID;
228
229         if (cp->clp_buf + cp->clp_buf_len <= cp->clp_buf_pos) {
230                 ssize_t refresh;
231
232                 refresh = chlg_read_bulk(cp);
233                 if (refresh == 0) {
234                         /* EOF, CHANGELOG_FLAG_FOLLOW ignored for now LU-7659 */
235                         rc = 1;
236                         goto out_free;
237                 } else if (refresh < 0) {
238                         rc = refresh;
239                         goto out_free;
240                 }
241         }
242
243         /* TODO check changelog_rec_size */
244         tmp = (struct changelog_rec *)cp->clp_buf_pos;
245
246         memcpy(*rech, cp->clp_buf_pos,
247                changelog_rec_size(tmp) + tmp->cr_namelen);
248
249         cp->clp_buf_pos += changelog_rec_size(tmp) + tmp->cr_namelen;
250         changelog_remap_rec(*rech, rec_fmt);
251
252         return 0;
253
254 out_free:
255         free(*rech);
256         *rech = NULL;
257         return rc;
258 }
259
260 /** Release the changelog record when done with it. */
261 int llapi_changelog_free(struct changelog_rec **rech)
262 {
263         free(*rech);
264         *rech = NULL;
265         return 0;
266 }
267
268 int llapi_changelog_clear(const char *mdtname, const char *idstr,
269                           long long endrec)
270 {
271         char dev_path[PATH_MAX];
272         char cmd[64];
273         size_t cmd_len = sizeof(cmd);
274         int fd;
275         int rc;
276
277         if (endrec < 0) {
278                 llapi_err_noerrno(LLAPI_MSG_ERROR,
279                                   "can't purge negative records\n");
280                 return -EINVAL;
281         }
282
283         chlg_dev_path(dev_path, sizeof(dev_path), mdtname);
284
285         rc = snprintf(cmd, cmd_len, "clear:%s:%lld", idstr, endrec);
286         if (rc >= sizeof(cmd))
287                 return -EINVAL;
288
289         cmd_len = rc + 1;
290
291         fd = open(dev_path, O_WRONLY);
292         if (fd < 0) {
293                 rc = -errno;
294                 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", dev_path);
295                 return rc;
296         }
297
298         rc = write(fd, cmd, cmd_len);
299         if (rc < 0) {
300                 rc = -errno;
301                 llapi_error(LLAPI_MSG_ERROR, rc,
302                             "cannot purge records for '%s'", idstr);
303                 goto out_close;
304         }
305
306         rc = 0;
307
308 out_close:
309         close(fd);
310         return rc;
311 }