4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the GNU Lesser General Public License
8 * (LGPL) version 2.1 or (at your discretion) any later version.
9 * (LGPL) version 2.1 accompanies this distribution, and is available at
10 * http://www.gnu.org/licenses/lgpl-2.1.html
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
20 * This file is part of Lustre, http://www.lustre.org/
22 * lustre/utils/liblustreapi_mirror.c
24 * Copyright (c) 2017, Intel Corporation.
26 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
33 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/xattr.h>
43 #include <sys/param.h>
45 #include <libcfs/util/ioctl.h>
46 #include <lustre/lustreapi.h>
47 #include <linux/lustre/lustre_ioctl.h>
50 * Set the mirror id for the opening file pointed by @fd, once the mirror
51 * is set successfully, the policy to choose mirrors will be disabed and the
52 * following I/O from this file descriptor will be led to this dedicated
54 * If @id is zero, it will clear the mirror id setting.
56 * \param fd file descriptor, must be opened with O_DIRECT
59 * \retval 0 on success.
60 * \retval -errno on failure.
62 int llapi_mirror_set(int fd, unsigned int id)
67 rc = ioctl(fd, LL_IOC_FLR_SET_MIRROR, id);
76 /* in the current implementation, llite doesn't verify if the mirror
77 * id is valid, it has to be verified in an I/O context so the fstat()
78 * call is to verify that the mirror id is correct. */
79 rc = fstat(fd, &stbuf);
83 (void) ioctl(fd, LL_IOC_FLR_SET_MIRROR, 0);
90 * Clear mirror id setting.
92 * \See llapi_mirror_set() for details.
94 int llapi_mirror_clear(int fd)
96 return llapi_mirror_set(fd, 0);
100 * Read data from a specified mirror with @id. This function won't read
101 * partial read result; either file end is reached, or number of @count bytes
102 * is read, or an error will be returned.
104 * \param fd file descriptor, should be opened with O_DIRECT
105 * \param id mirror id to be read from
106 * \param buf read buffer
107 * \param count number of bytes to be read
108 * \param pos file postion where the read starts
110 * \result >= 0 Number of bytes has been read
111 * \result < 0 The last seen error
113 ssize_t llapi_mirror_read(int fd, unsigned int id, void *buf, size_t count,
120 page_size = sysconf(_SC_PAGESIZE);
126 rc = llapi_mirror_set(fd, id);
133 bytes_read = pread(fd, buf, count, pos);
134 if (!bytes_read) /* end of file */
137 if (bytes_read < 0) {
142 result += bytes_read;
147 if (bytes_read & (page_size - 1)) /* end of file */
151 (void) llapi_mirror_clear(fd);
156 ssize_t llapi_mirror_write(int fd, unsigned int id, const void *buf,
157 size_t count, off_t pos)
163 page_size = sysconf(_SC_PAGESIZE);
167 if (((unsigned long)buf & (page_size - 1)) || pos & (page_size - 1))
170 rc = llapi_mirror_set(fd, id);
175 ssize_t bytes_written;
177 if (pos & (page_size - 1)) {
182 bytes_written = pwrite(fd, buf, count, pos);
183 if (bytes_written < 0) {
188 result += bytes_written;
189 pos += bytes_written;
190 buf += bytes_written;
191 count -= bytes_written;
194 (void) llapi_mirror_clear(fd);
199 int llapi_mirror_truncate(int fd, unsigned int id, off_t length)
203 rc = llapi_mirror_set(fd, id);
207 rc = ftruncate(fd, length);
211 (void) llapi_mirror_clear(fd);
216 int llapi_mirror_punch(int fd, unsigned int id, off_t start, size_t length)
220 rc = llapi_mirror_set(fd, id);
224 rc = llapi_hole_punch(fd, start, length);
225 (void) llapi_mirror_clear(fd);
230 bool llapi_mirror_is_sparse(int fd, unsigned int id)
235 rc = llapi_mirror_set(fd, id);
239 sparse = llapi_file_is_sparse(fd);
240 (void) llapi_mirror_clear(fd);
246 * Seek data in a specified mirror with @id. This function looks for the
247 * first data segment from given offset and returns its offset and length
249 * \param fd file descriptor, should be opened with O_DIRECT
250 * \param id mirror id to be read from
251 * \param pos position for start data seek from
252 * \param size size of data segment found
254 * \result >= 0 Number of bytes has been read
255 * \result < 0 The last seen error
257 off_t llapi_mirror_data_seek(int fd, unsigned int id, off_t pos, size_t *size)
262 rc = llapi_mirror_set(fd, id);
266 data_off = llapi_data_seek(fd, pos, size);
267 (void) llapi_mirror_clear(fd);
273 * Copy data contents from source mirror @src to multiple destinations
274 * pointed by @dst. The destination array @dst will be altered to store
275 * successfully copied mirrors.
277 * \param fd file descriptor, should be opened with O_DIRECT
278 * \param src source mirror id, usually a valid mirror
279 * \param dst an array of destination mirror ids
280 * \param count number of elements in array @dst
282 * \result > 0 Number of mirrors successfully copied
283 * \result < 0 The last seen error
285 ssize_t llapi_mirror_copy_many(int fd, __u16 src, __u16 *dst, size_t count)
287 const size_t buflen = 4 * 1024 * 1024; /* 4M */
302 page_size = sysconf(_SC_PAGESIZE);
308 rc = posix_memalign(&buf, page_size, buflen);
309 if (rc) /* error code is returned directly */
312 sparse = llapi_mirror_is_sparse(fd, src);
316 /* for sparse src we have to be sure that dst has no
317 * data in src holes, so truncate it first
319 for (i = 0; i < nr; i++) {
320 rc = llapi_mirror_truncate(fd, dst[i], pos);
323 /* exclude the failed one */
338 size_t to_write, to_read;
340 if (sparse && pos >= data_end) {
343 data_off = llapi_mirror_data_seek(fd, src, pos,
346 /* Non-fatal, switch to full copy */
351 /* hole at the end of file, set pos to the
352 * data_off, so truncate block at the end
353 * will set final dst size.
359 data_end = data_off + data_size;
361 pos = data_off & ~(page_size - 1);
362 data_end = ((data_end - 1) | (page_size - 1)) + 1;
363 to_read = MIN(data_end - pos, buflen);
368 bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
369 if (!bytes_read) { /* end of file */
371 } else if (bytes_read < 0) {
377 /* round up to page align to make direct IO happy.
378 * this implies the last segment to write. */
379 to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
381 for (i = 0; i < nr; i++) {
384 written = llapi_mirror_write(fd, dst[i], buf,
389 /* this mirror is not written succesfully,
390 * get rid of it from the array */
395 assert(written == to_write);
398 eof = bytes_read < to_read;
404 for (i = 0; i < nr; i++) {
405 rc = llapi_mirror_truncate(fd, dst[i], pos);
409 /* exclude the failed one */
417 return nr > 0 ? nr : result;
421 * Copy data contents from source mirror @src to target mirror @dst.
423 * \param fd file descriptor, should be opened with O_DIRECT
424 * \param src source mirror id, usually a valid mirror
425 * \param dst mirror id of copy destination
426 * \param pos start file pos
427 * \param count number of bytes to be copied
429 * \result > 0 Number of mirrors successfully copied
430 * \result < 0 The last seen error
432 int llapi_mirror_copy(int fd, unsigned int src, unsigned int dst, off_t pos,
435 const size_t buflen = 4 * 1024 * 1024; /* 4M */
444 page_size = sysconf(_SC_PAGESIZE);
448 if (pos & (page_size - 1) || !dst)
451 if (count != OBD_OBJECT_EOF && count & (page_size - 1))
454 rc = posix_memalign(&buf, page_size, buflen);
455 if (rc) /* error code is returned directly */
458 while (result < count) {
459 ssize_t bytes_read, bytes_written;
460 size_t to_read, to_write;
462 to_read = MIN(buflen, count - result);
464 bytes_read = pread(fd, buf, to_read, pos);
466 bytes_read = llapi_mirror_read(fd, src, buf, to_read,
468 if (!bytes_read) { /* end of file */
470 } else if (bytes_read < 0) {
475 /* round up to page align to make direct IO happy.
476 * this implies the last segment to write. */
477 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
479 bytes_written = llapi_mirror_write(fd, dst, buf, to_write,
481 if (bytes_written < 0) {
482 result = bytes_written;
486 assert(bytes_written == to_write);
489 result += bytes_read;
491 if (bytes_read < to_read) /* short read occurred */
497 if (result > 0 && pos & (page_size - 1)) {
498 rc = llapi_mirror_truncate(fd, dst, pos);