Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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         size_t page_size = sysconf(_SC_PAGESIZE);
117         ssize_t result = 0;
118         int rc;
119
120         rc = llapi_mirror_set(fd, id);
121         if (rc < 0)
122                 return rc;
123
124         while (count > 0) {
125                 ssize_t bytes_read;
126
127                 bytes_read = pread(fd, buf, count, pos);
128                 if (!bytes_read) /* end of file */
129                         break;
130
131                 if (bytes_read < 0) {
132                         result = -errno;
133                         break;
134                 }
135
136                 result += bytes_read;
137                 pos += bytes_read;
138                 buf += bytes_read;
139                 count -= bytes_read;
140
141                 if (bytes_read & (page_size - 1)) /* end of file */
142                         break;
143         }
144
145         (void) llapi_mirror_clear(fd);
146
147         return result;
148 }
149
150 ssize_t llapi_mirror_write(int fd, unsigned int id, const void *buf,
151                            size_t count, off_t pos)
152 {
153         size_t page_size = sysconf(_SC_PAGESIZE);
154         ssize_t result = 0;
155         int rc;
156
157         if (((unsigned long)buf & (page_size - 1)) || pos & (page_size - 1))
158                 return -EINVAL;
159
160         rc = llapi_mirror_set(fd, id);
161         if (rc < 0)
162                 return rc;
163
164         while (count > 0) {
165                 ssize_t bytes_written;
166
167                 if (pos & (page_size - 1)) {
168                         result = -EINVAL;
169                         break;
170                 }
171
172                 bytes_written = pwrite(fd, buf, count, pos);
173                 if (bytes_written < 0) {
174                         result = -errno;
175                         break;
176                 }
177
178                 result += bytes_written;
179                 pos += bytes_written;
180                 buf += bytes_written;
181                 count -= bytes_written;
182         }
183
184         (void) llapi_mirror_clear(fd);
185
186         return result;
187 }
188
189 int llapi_mirror_truncate(int fd, unsigned int id, off_t length)
190 {
191         int rc;
192
193         rc = llapi_mirror_set(fd, id);
194         if (rc < 0)
195                 return rc;
196
197         rc = ftruncate(fd, length);
198         if (rc < 0)
199                 rc = -errno;
200
201         (void) llapi_mirror_clear(fd);
202
203         return rc;
204 }
205
206 int llapi_mirror_punch(int fd, unsigned int id, off_t start, size_t length)
207 {
208         int rc;
209
210         rc = llapi_mirror_set(fd, id);
211         if (rc < 0)
212                 return rc;
213
214         rc = llapi_hole_punch(fd, start, length);
215         (void) llapi_mirror_clear(fd);
216
217         return rc;
218 }
219
220 bool llapi_mirror_is_sparse(int fd, unsigned int id)
221 {
222         bool sparse;
223         int rc;
224
225         rc = llapi_mirror_set(fd, id);
226         if (rc < 0)
227                 return false;
228
229         sparse = llapi_file_is_sparse(fd);
230         (void) llapi_mirror_clear(fd);
231
232         return sparse;
233 }
234
235 /**
236  * Seek data in a specified mirror with @id. This function looks for the
237  * first data segment from given offset and returns its offset and length
238  *
239  * \param fd    file descriptor, should be opened with O_DIRECT
240  * \param id    mirror id to be read from
241  * \param pos   position for start data seek from
242  * \param size  size of data segment found
243  *
244  * \result >= 0 Number of bytes has been read
245  * \result < 0  The last seen error
246  */
247 off_t llapi_mirror_data_seek(int fd, unsigned int id, off_t pos, size_t *size)
248 {
249         off_t data_off;
250         int rc;
251
252         rc = llapi_mirror_set(fd, id);
253         if (rc < 0)
254                 return rc;
255
256         data_off = llapi_data_seek(fd, pos, size);
257         (void) llapi_mirror_clear(fd);
258
259         return data_off;
260 }
261
262 /**
263  * Copy data contents from source mirror @src to multiple destinations
264  * pointed by @dst. The destination array @dst will be altered to store
265  * successfully copied mirrors.
266  *
267  * \param fd    file descriptor, should be opened with O_DIRECT
268  * \param src   source mirror id, usually a valid mirror
269  * \param dst   an array of destination mirror ids
270  * \param count number of elements in array @dst
271  *
272  * \result > 0  Number of mirrors successfully copied
273  * \result < 0  The last seen error
274  */
275 ssize_t llapi_mirror_copy_many(int fd, __u16 src, __u16 *dst, size_t count)
276 {
277         const size_t buflen = 4 * 1024 * 1024; /* 4M */
278         void *buf;
279         off_t pos = 0;
280         off_t data_end = 0;
281         size_t page_size = sysconf(_SC_PAGESIZE);
282         ssize_t result = 0;
283         bool eof = false;
284         bool sparse;
285         int nr;
286         int i;
287         int rc;
288
289         if (!count)
290                 return 0;
291
292         rc = posix_memalign(&buf, page_size, buflen);
293         if (rc) /* error code is returned directly */
294                 return -rc;
295
296         sparse = llapi_mirror_is_sparse(fd, src);
297
298         nr = count;
299         if (sparse) {
300                 /* for sparse src we have to be sure that dst has no
301                  * data in src holes, so truncate it first
302                  */
303                 for (i = 0; i < nr; i++) {
304                         rc = llapi_mirror_truncate(fd, dst[i], pos);
305                         if (rc < 0) {
306                                 result = rc;
307                                 /* exclude the failed one */
308                                 dst[i] = dst[--nr];
309                                 i--;
310                                 continue;
311                         }
312                 }
313                 if (!nr)
314                         return result;
315         }
316
317         while (!eof) {
318                 off_t data_off;
319                 ssize_t bytes_read;
320                 size_t to_write, to_read;
321
322                 if (sparse && pos >= data_end) {
323                         size_t data_size;
324
325                         data_off = llapi_mirror_data_seek(fd, src, pos,
326                                                           &data_size);
327                         if (data_off < 0) {
328                                 /* Non-fatal, switch to full copy */
329                                 sparse = false;
330                                 continue;
331                         }
332                         if (!data_size) {
333                                 /* hole at the end of file, set pos to the
334                                  * data_off, so truncate block at the end
335                                  * will set final dst size.
336                                  */
337                                 pos = data_off;
338                                 break;
339                         }
340
341                         data_end = data_off + data_size;
342                         /* align by page */
343                         pos = data_off & ~(page_size - 1);
344                         data_end = ((data_end - 1) | (page_size - 1)) + 1;
345                         to_read = MIN(data_end - pos, buflen);
346                 } else {
347                         to_read = buflen;
348                 }
349
350                 bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
351                 if (!bytes_read) { /* end of file */
352                         break;
353                 } else if (bytes_read < 0) {
354                         result = bytes_read;
355                         nr = 0;
356                         break;
357                 }
358
359                 /* round up to page align to make direct IO happy.
360                  * this implies the last segment to write. */
361                 to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
362
363                 for (i = 0; i < nr; i++) {
364                         ssize_t written;
365
366                         written = llapi_mirror_write(fd, dst[i], buf,
367                                                       to_write, pos);
368                         if (written < 0) {
369                                 result = written;
370
371                                 /* this mirror is not written succesfully,
372                                  * get rid of it from the array */
373                                 dst[i] = dst[--nr];
374                                 i--;
375                                 continue;
376                         }
377                         assert(written == to_write);
378                 }
379                 pos += bytes_read;
380                 eof = bytes_read < to_read;
381         }
382
383         free(buf);
384
385         if (nr > 0) {
386                 for (i = 0; i < nr; i++) {
387                         rc = llapi_mirror_truncate(fd, dst[i], pos);
388                         if (rc < 0) {
389                                 result = rc;
390
391                                 /* exclude the failed one */
392                                 dst[i] = dst[--nr];
393                                 --i;
394                                 continue;
395                         }
396                 }
397         }
398
399         return nr > 0 ? nr : result;
400 }
401
402 /**
403  * Copy data contents from source mirror @src to target mirror @dst.
404  *
405  * \param fd    file descriptor, should be opened with O_DIRECT
406  * \param src   source mirror id, usually a valid mirror
407  * \param dst   mirror id of copy destination
408  * \param pos   start file pos
409  * \param count number of bytes to be copied
410  *
411  * \result > 0  Number of mirrors successfully copied
412  * \result < 0  The last seen error
413  */
414 int llapi_mirror_copy(int fd, unsigned int src, unsigned int dst, off_t pos,
415                       size_t count)
416 {
417         const size_t buflen = 4 * 1024 * 1024; /* 4M */
418         void *buf;
419         size_t page_size = sysconf(_SC_PAGESIZE);
420         ssize_t result = 0;
421         int rc;
422
423         if (!count)
424                 return 0;
425
426         if (pos & (page_size - 1) || !dst)
427                 return -EINVAL;
428
429         if (count != OBD_OBJECT_EOF && count & (page_size - 1))
430                 return -EINVAL;
431
432         rc = posix_memalign(&buf, page_size, buflen);
433         if (rc) /* error code is returned directly */
434                 return -rc;
435
436         while (result < count) {
437                 ssize_t bytes_read, bytes_written;
438                 size_t to_read, to_write;
439
440                 to_read = MIN(buflen, count - result);
441                 if (src == 0)
442                         bytes_read = pread(fd, buf, to_read, pos);
443                 else
444                         bytes_read = llapi_mirror_read(fd, src, buf, to_read,
445                                                         pos);
446                 if (!bytes_read) { /* end of file */
447                         break;
448                 } else if (bytes_read < 0) {
449                         result = bytes_read;
450                         break;
451                 }
452
453                 /* round up to page align to make direct IO happy.
454                  * this implies the last segment to write. */
455                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
456
457                 bytes_written = llapi_mirror_write(fd, dst, buf, to_write,
458                                                     pos);
459                 if (bytes_written < 0) {
460                         result = bytes_written;
461                         break;
462                 }
463
464                 assert(bytes_written == to_write);
465
466                 pos += bytes_read;
467                 result += bytes_read;
468
469                 if (bytes_read < to_read) /* short read occurred */
470                         break;
471         }
472
473         free(buf);
474
475         if (result > 0 && pos & (page_size - 1)) {
476                 rc = llapi_mirror_truncate(fd, dst, pos);
477                 if (rc < 0)
478                         result = rc;
479         }
480
481         return result;
482 }