Whamcloud - gitweb
New tag 2.15.63
[fs/lustre-release.git] / lustre / utils / liblustreapi_mirror.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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
11  *
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.
16  *
17  * LGPL HEADER END
18  */
19 /*
20  * This file is part of Lustre, http://www.lustre.org/
21  *
22  * lustre/utils/liblustreapi_mirror.c
23  *
24  * Copyright (c) 2017, Intel Corporation.
25  *
26  * Author: Jinshan Xiong <jinshan.xiong@intel.com>
27  */
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stddef.h>
33 #include <sys/ioctl.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <dirent.h>
38 #include <stdarg.h>
39 #include <sys/stat.h>
40 #include <sys/types.h>
41 #include <sys/xattr.h>
42 #include <assert.h>
43 #include <sys/param.h>
44
45 #include <libcfs/util/ioctl.h>
46 #include <lustre/lustreapi.h>
47 #include <linux/lustre/lustre_ioctl.h>
48
49 /**
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
53  * mirror @id.
54  * If @id is zero, it will clear the mirror id setting.
55  *
56  * \param fd    file descriptor, must be opened with O_DIRECT
57  * \param id    mirror id
58  *
59  * \retval      0 on success.
60  * \retval      -errno on failure.
61  */
62 int llapi_mirror_set(int fd, unsigned int id)
63 {
64         struct stat stbuf;
65         int rc;
66
67         rc = ioctl(fd, LL_IOC_FLR_SET_MIRROR, id);
68         if (rc < 0) {
69                 rc = -errno;
70                 return rc;
71         }
72
73         if (!id)
74                 return 0;
75
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);
80         if (rc < 0) {
81                 rc = -errno;
82
83                 (void) ioctl(fd, LL_IOC_FLR_SET_MIRROR, 0);
84         }
85
86         return rc;
87 }
88
89 /**
90  * Clear mirror id setting.
91  *
92  * \See llapi_mirror_set() for details.
93  */
94 int llapi_mirror_clear(int fd)
95 {
96         return llapi_mirror_set(fd, 0);
97 }
98
99 /**
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.
103  *
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
109  *
110  * \result >= 0 Number of bytes has been read
111  * \result < 0  The last seen error
112  */
113 ssize_t llapi_mirror_read(int fd, unsigned int id, void *buf, size_t count,
114                           off_t pos)
115 {
116         ssize_t result = 0;
117         ssize_t page_size;
118         int rc;
119
120         page_size = sysconf(_SC_PAGESIZE);
121         if (page_size < 0) {
122                 rc = -errno;
123                 return rc;
124         }
125
126         rc = llapi_mirror_set(fd, id);
127         if (rc < 0)
128                 return rc;
129
130         while (count > 0) {
131                 ssize_t bytes_read;
132
133                 bytes_read = pread(fd, buf, count, pos);
134                 if (!bytes_read) /* end of file */
135                         break;
136
137                 if (bytes_read < 0) {
138                         result = -errno;
139                         break;
140                 }
141
142                 result += bytes_read;
143                 pos += bytes_read;
144                 buf += bytes_read;
145                 count -= bytes_read;
146
147                 if (bytes_read & (page_size - 1)) /* end of file */
148                         break;
149         }
150
151         (void) llapi_mirror_clear(fd);
152
153         return result;
154 }
155
156 ssize_t llapi_mirror_write(int fd, unsigned int id, const void *buf,
157                            size_t count, off_t pos)
158 {
159         ssize_t result = 0;
160         ssize_t page_size;
161         int rc;
162
163         page_size = sysconf(_SC_PAGESIZE);
164         if (page_size < 0)
165                 return -EINVAL;
166
167         if (((unsigned long)buf & (page_size - 1)) || pos & (page_size - 1))
168                 return -EINVAL;
169
170         rc = llapi_mirror_set(fd, id);
171         if (rc < 0)
172                 return rc;
173
174         while (count > 0) {
175                 ssize_t bytes_written;
176
177                 if (pos & (page_size - 1)) {
178                         result = -EINVAL;
179                         break;
180                 }
181
182                 bytes_written = pwrite(fd, buf, count, pos);
183                 if (bytes_written < 0) {
184                         result = -errno;
185                         break;
186                 }
187
188                 result += bytes_written;
189                 pos += bytes_written;
190                 buf += bytes_written;
191                 count -= bytes_written;
192         }
193
194         (void) llapi_mirror_clear(fd);
195
196         return result;
197 }
198
199 int llapi_mirror_truncate(int fd, unsigned int id, off_t length)
200 {
201         int rc;
202
203         rc = llapi_mirror_set(fd, id);
204         if (rc < 0)
205                 return rc;
206
207         rc = ftruncate(fd, length);
208         if (rc < 0)
209                 rc = -errno;
210
211         (void) llapi_mirror_clear(fd);
212
213         return rc;
214 }
215
216 int llapi_mirror_punch(int fd, unsigned int id, off_t start, size_t length)
217 {
218         int rc;
219
220         rc = llapi_mirror_set(fd, id);
221         if (rc < 0)
222                 return rc;
223
224         rc = llapi_hole_punch(fd, start, length);
225         (void) llapi_mirror_clear(fd);
226
227         return rc;
228 }
229
230 bool llapi_mirror_is_sparse(int fd, unsigned int id)
231 {
232         bool sparse;
233         int rc;
234
235         rc = llapi_mirror_set(fd, id);
236         if (rc < 0)
237                 return false;
238
239         sparse = llapi_file_is_sparse(fd);
240         (void) llapi_mirror_clear(fd);
241
242         return sparse;
243 }
244
245 /**
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
248  *
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
253  *
254  * \result >= 0 Number of bytes has been read
255  * \result < 0  The last seen error
256  */
257 off_t llapi_mirror_data_seek(int fd, unsigned int id, off_t pos, size_t *size)
258 {
259         off_t data_off;
260         int rc;
261
262         rc = llapi_mirror_set(fd, id);
263         if (rc < 0)
264                 return rc;
265
266         data_off = llapi_data_seek(fd, pos, size);
267         (void) llapi_mirror_clear(fd);
268
269         return data_off;
270 }
271
272 /**
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.
276  *
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
281  *
282  * \result > 0  Number of mirrors successfully copied
283  * \result < 0  The last seen error
284  */
285 ssize_t llapi_mirror_copy_many(int fd, __u16 src, __u16 *dst, size_t count)
286 {
287         const size_t buflen = 4 * 1024 * 1024; /* 4M */
288         void *buf;
289         off_t pos = 0;
290         off_t data_end = 0;
291         ssize_t page_size;
292         ssize_t result = 0;
293         bool eof = false;
294         bool sparse;
295         int nr;
296         int i;
297         int rc;
298
299         if (!count)
300                 return 0;
301
302         page_size = sysconf(_SC_PAGESIZE);
303         if (page_size < 0) {
304                 rc = -errno;
305                 return rc;
306         }
307
308         rc = posix_memalign(&buf, page_size, buflen);
309         if (rc) /* error code is returned directly */
310                 return -rc;
311
312         sparse = llapi_mirror_is_sparse(fd, src);
313
314         nr = count;
315         if (sparse) {
316                 /* for sparse src we have to be sure that dst has no
317                  * data in src holes, so truncate it first
318                  */
319                 for (i = 0; i < nr; i++) {
320                         rc = llapi_mirror_truncate(fd, dst[i], pos);
321                         if (rc < 0) {
322                                 result = rc;
323                                 /* exclude the failed one */
324                                 dst[i] = dst[--nr];
325                                 i--;
326                                 continue;
327                         }
328                 }
329                 if (!nr) {
330                         free(buf);
331                         return result;
332                 }
333         }
334
335         while (!eof) {
336                 off_t data_off;
337                 ssize_t bytes_read;
338                 size_t to_write, to_read;
339
340                 if (sparse && pos >= data_end) {
341                         size_t data_size;
342
343                         data_off = llapi_mirror_data_seek(fd, src, pos,
344                                                           &data_size);
345                         if (data_off < 0) {
346                                 /* Non-fatal, switch to full copy */
347                                 sparse = false;
348                                 continue;
349                         }
350                         if (!data_size) {
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.
354                                  */
355                                 pos = data_off;
356                                 break;
357                         }
358
359                         data_end = data_off + data_size;
360                         /* align by page */
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);
364                 } else {
365                         to_read = buflen;
366                 }
367
368                 bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
369                 if (!bytes_read) { /* end of file */
370                         break;
371                 } else if (bytes_read < 0) {
372                         result = bytes_read;
373                         nr = 0;
374                         break;
375                 }
376
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;
380
381                 for (i = 0; i < nr; i++) {
382                         ssize_t written;
383
384                         written = llapi_mirror_write(fd, dst[i], buf,
385                                                       to_write, pos);
386                         if (written < 0) {
387                                 result = written;
388
389                                 /* this mirror is not written succesfully,
390                                  * get rid of it from the array */
391                                 dst[i] = dst[--nr];
392                                 i--;
393                                 continue;
394                         }
395                         assert(written == to_write);
396                 }
397                 pos += bytes_read;
398                 eof = bytes_read < to_read;
399         }
400
401         free(buf);
402
403         if (nr > 0) {
404                 for (i = 0; i < nr; i++) {
405                         rc = llapi_mirror_truncate(fd, dst[i], pos);
406                         if (rc < 0) {
407                                 result = rc;
408
409                                 /* exclude the failed one */
410                                 dst[i] = dst[--nr];
411                                 --i;
412                                 continue;
413                         }
414                 }
415         }
416
417         return nr > 0 ? nr : result;
418 }
419
420 /**
421  * Copy data contents from source mirror @src to target mirror @dst.
422  *
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
428  *
429  * \result > 0  Number of mirrors successfully copied
430  * \result < 0  The last seen error
431  */
432 int llapi_mirror_copy(int fd, unsigned int src, unsigned int dst, off_t pos,
433                       size_t count)
434 {
435         const size_t buflen = 4 * 1024 * 1024; /* 4M */
436         ssize_t result = 0;
437         ssize_t page_size;
438         void *buf;
439         int rc;
440
441         if (!count)
442                 return 0;
443
444         page_size = sysconf(_SC_PAGESIZE);
445         if (page_size < 0)
446                 return -EINVAL;
447
448         if (pos & (page_size - 1) || !dst)
449                 return -EINVAL;
450
451         if (count != OBD_OBJECT_EOF && count & (page_size - 1))
452                 return -EINVAL;
453
454         rc = posix_memalign(&buf, page_size, buflen);
455         if (rc) /* error code is returned directly */
456                 return -rc;
457
458         while (result < count) {
459                 ssize_t bytes_read, bytes_written;
460                 size_t to_read, to_write;
461
462                 to_read = MIN(buflen, count - result);
463                 if (src == 0)
464                         bytes_read = pread(fd, buf, to_read, pos);
465                 else
466                         bytes_read = llapi_mirror_read(fd, src, buf, to_read,
467                                                         pos);
468                 if (!bytes_read) { /* end of file */
469                         break;
470                 } else if (bytes_read < 0) {
471                         result = bytes_read;
472                         break;
473                 }
474
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);
478
479                 bytes_written = llapi_mirror_write(fd, dst, buf, to_write,
480                                                     pos);
481                 if (bytes_written < 0) {
482                         result = bytes_written;
483                         break;
484                 }
485
486                 assert(bytes_written == to_write);
487
488                 pos += bytes_read;
489                 result += bytes_read;
490
491                 if (bytes_read < to_read) /* short read occurred */
492                         break;
493         }
494
495         free(buf);
496
497         if (result > 0 && pos & (page_size - 1)) {
498                 rc = llapi_mirror_truncate(fd, dst, pos);
499                 if (rc < 0)
500                         result = rc;
501         }
502
503         return result;
504 }