4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * (C) Copyright 2017 Commissariat a l'energie atomique et aux energies
9 * All rights reserved. This program and the accompanying materials
10 * are made available under the terms of the GNU Lesser General Public License
11 * (LGPL) version 2.1 or (at your discretion) any later version.
12 * (LGPL) version 2.1 accompanies this distribution, and is available at
13 * http://www.gnu.org/licenses/lgpl-2.1.html
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
23 * lustre/utils/liblustreapi_chlg.c
25 * lustreapi library for filesystem changelog
27 * Author: Henri Doreau <henri.doreau@cea.fr>
38 #include <sys/types.h>
40 #include <lustre/lustreapi.h>
43 static int chlg_dev_path(char *path, size_t path_len, const char *device)
47 rc = snprintf(path, path_len, "/dev/changelog-%s", device);
57 #define CHANGELOG_PRIV_MAGIC 0xCA8E1080
58 #define CHANGELOG_BUFFER_SZ 4096
61 * Record state for efficient changelog consumption.
62 * Read chunks of CHANGELOG_BUFFER_SZ bytes.
64 struct changelog_private {
65 /* Ensure that the structure is valid and initialized */
67 /* File descriptor on the changelog character device */
69 /* Changelog delivery mode */
70 enum changelog_send_flag clp_send_flags;
71 /* Available bytes in buffer */
73 /* Current position in buffer */
75 /* Read buffer with records read from system */
80 * Start reading from a changelog
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)
88 int llapi_changelog_start(void **priv, enum changelog_send_flag flags,
89 const char *device, long long startrec)
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];
98 rc = chlg_dev_path(cdev_path, sizeof(cdev_path), device);
102 /* Set up the receiver control struct */
103 cp = calloc(1, sizeof(*cp) + CHANGELOG_BUFFER_SZ);
107 cp->clp_magic = CHANGELOG_PRIV_MAGIC;
108 cp->clp_send_flags = flags;
111 cp->clp_buf_pos = cp->clp_buf;
113 /* Set up the receiver */
114 cp->clp_fd = open(cdev_path, O_RDONLY);
115 if (cp->clp_fd < 0) {
123 res = lseek(cp->clp_fd, startrec, SEEK_SET);
124 if (res == (off_t)-1) {
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.
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",
140 warned_extra_flags = true;
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__);
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;
168 /** Finish reading from a changelog */
169 int llapi_changelog_fini(void **priv)
171 struct changelog_private *cp = *priv;
173 if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
182 static ssize_t chlg_read_bulk(struct changelog_private *cp)
186 if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
189 rd_bytes = read(cp->clp_fd, cp->clp_buf, CHANGELOG_BUFFER_SZ);
193 cp->clp_buf_pos = cp->clp_buf;
194 cp->clp_buf_len = rd_bytes;
200 * Returns a file descriptor to poll on.
202 * \@param[in] priv Opaque changelog reader structure.
203 * @return valid file descriptor on success, negated errno code on failure.
205 int llapi_changelog_get_fd(void *priv)
207 struct changelog_private *cp = priv;
209 if (!cp || cp->clp_magic != CHANGELOG_PRIV_MAGIC)
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
222 #define DEFAULT_RECORD_FMT (CLF_VERSION | CLF_RENAME)
223 int llapi_changelog_recv(void *priv, struct changelog_rec **rech)
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;
231 if (!cp || (cp->clp_magic != CHANGELOG_PRIV_MAGIC))
237 *rech = malloc(CR_MAXSIZE);
241 if (cp->clp_send_flags & CHANGELOG_FLAG_JOBID)
242 rec_fmt |= CLF_JOBID;
244 if (cp->clp_send_flags & CHANGELOG_FLAG_EXTRA_FLAGS) {
245 rec_fmt |= CLF_EXTRA_FLAGS;
248 if (cp->clp_buf + cp->clp_buf_len <= cp->clp_buf_pos) {
251 refresh = chlg_read_bulk(cp);
253 /* EOF, CHANGELOG_FLAG_FOLLOW ignored for now LU-7659 */
256 } else if (refresh < 0) {
262 /* TODO check changelog_rec_size */
263 tmp = (struct changelog_rec *)cp->clp_buf_pos;
265 memcpy(*rech, cp->clp_buf_pos,
266 changelog_rec_size(tmp) + tmp->cr_namelen);
268 cp->clp_buf_pos += changelog_rec_size(tmp) + tmp->cr_namelen;
269 changelog_remap_rec(*rech, rec_fmt, rec_extra_fmt);
279 /** Release the changelog record when done with it. */
280 int llapi_changelog_free(struct changelog_rec **rech)
287 int llapi_changelog_clear(const char *mdtname, const char *idstr,
290 char dev_path[PATH_MAX];
292 size_t cmd_len = sizeof(cmd);
297 llapi_err_noerrno(LLAPI_MSG_ERROR,
298 "can't purge negative records\n");
302 chlg_dev_path(dev_path, sizeof(dev_path), mdtname);
304 rc = snprintf(cmd, cmd_len, "clear:%s:%lld", idstr, endrec);
305 if (rc >= sizeof(cmd))
310 fd = open(dev_path, O_WRONLY);
313 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", dev_path);
317 rc = write(fd, cmd, cmd_len);
320 llapi_error(LLAPI_MSG_ERROR, rc,
321 "cannot purge records for '%s'", idstr);