Whamcloud - gitweb
LU-10467 ptlrpc: convert final users of LWI_TIMEOUT_INTERVAL
[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 /**
207  * Copy data contents from source mirror @src to multiple destinations
208  * pointed by @dst. The destination array @dst will be altered to store
209  * successfully copied mirrors.
210  *
211  * \param fd    file descriptor, should be opened with O_DIRECT
212  * \param src   source mirror id, usually a valid mirror
213  * \param dst   an array of destination mirror ids
214  * \param count number of elements in array @dst
215  *
216  * \result > 0  Number of mirrors successfully copied
217  * \result < 0  The last seen error
218  */
219 ssize_t llapi_mirror_copy_many(int fd, __u16 src, __u16 *dst, size_t count)
220 {
221         const size_t buflen = 4 * 1024 * 1024; /* 4M */
222         void *buf;
223         loff_t pos = 0;
224         size_t page_size = sysconf(_SC_PAGESIZE);
225         ssize_t result = 0;
226         bool eof = false;
227         int nr;
228         int i;
229         int rc;
230
231         if (!count)
232                 return 0;
233
234         rc = posix_memalign(&buf, page_size, buflen);
235         if (rc) /* error code is returned directly */
236                 return -rc;
237
238         nr = count;
239         while (!eof) {
240                 ssize_t bytes_read;
241                 size_t to_write;
242
243                 bytes_read = llapi_mirror_read(fd, src, buf, buflen, pos);
244                 if (!bytes_read) { /* end of file */
245                         break;
246                 } else if (bytes_read < 0) {
247                         result = bytes_read;
248                         nr = 0;
249                         break;
250                 }
251
252                 /* round up to page align to make direct IO happy.
253                  * this implies the last segment to write. */
254                 to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
255
256                 for (i = 0; i < nr; i++) {
257                         ssize_t written;
258
259                         written = llapi_mirror_write(fd, dst[i], buf,
260                                                       to_write, pos);
261                         if (written < 0) {
262                                 result = written;
263
264                                 /* this mirror is not written succesfully,
265                                  * get rid of it from the array */
266                                 dst[i] = dst[--nr];
267                                 i--;
268                                 continue;
269                         }
270
271                         assert(written == to_write);
272                 }
273
274                 pos += bytes_read;
275                 eof = bytes_read < buflen;
276         }
277
278         free(buf);
279
280         if (nr > 0) {
281                 for (i = 0; i < nr; i++) {
282                         rc = llapi_mirror_truncate(fd, dst[i], pos);
283                         if (rc < 0) {
284                                 result = rc;
285
286                                 /* exclude the failed one */
287                                 dst[i] = dst[--nr];
288                                 --i;
289                                 continue;
290                         }
291                 }
292         }
293
294         return nr > 0 ? nr : result;
295 }
296
297 /**
298  * Copy data contents from source mirror @src to target mirror @dst.
299  *
300  * \param fd    file descriptor, should be opened with O_DIRECT
301  * \param src   source mirror id, usually a valid mirror
302  * \param dst   mirror id of copy destination
303  * \param pos   start file pos
304  * \param count number of bytes to be copied
305  *
306  * \result > 0  Number of mirrors successfully copied
307  * \result < 0  The last seen error
308  */
309 int llapi_mirror_copy(int fd, unsigned int src, unsigned int dst, off_t pos,
310                       size_t count)
311 {
312         const size_t buflen = 4 * 1024 * 1024; /* 4M */
313         void *buf;
314         size_t page_size = sysconf(_SC_PAGESIZE);
315         ssize_t result = 0;
316         int rc;
317
318         if (!count)
319                 return 0;
320
321         if (pos & (page_size - 1) || !dst)
322                 return -EINVAL;
323
324         if (count != OBD_OBJECT_EOF && count & (page_size - 1))
325                 return -EINVAL;
326
327         rc = posix_memalign(&buf, page_size, buflen);
328         if (rc) /* error code is returned directly */
329                 return -rc;
330
331         while (result < count) {
332                 ssize_t bytes_read, bytes_written;
333                 size_t to_read, to_write;
334
335                 to_read = MIN(buflen, count - result);
336                 if (src == 0)
337                         bytes_read = pread(fd, buf, to_read, pos);
338                 else
339                         bytes_read = llapi_mirror_read(fd, src, buf, to_read,
340                                                         pos);
341                 if (!bytes_read) { /* end of file */
342                         break;
343                 } else if (bytes_read < 0) {
344                         result = bytes_read;
345                         break;
346                 }
347
348                 /* round up to page align to make direct IO happy.
349                  * this implies the last segment to write. */
350                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
351
352                 bytes_written = llapi_mirror_write(fd, dst, buf, to_write,
353                                                     pos);
354                 if (bytes_written < 0) {
355                         result = bytes_written;
356                         break;
357                 }
358
359                 assert(bytes_written == to_write);
360
361                 pos += bytes_read;
362                 result += bytes_read;
363
364                 if (bytes_read < to_read) /* short read occurred */
365                         break;
366         }
367
368         free(buf);
369
370         if (result > 0 && pos & (page_size - 1)) {
371                 rc = llapi_mirror_truncate(fd, dst, pos);
372                 if (rc < 0)
373                         result = rc;
374         }
375
376         return result;
377 }