]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/filestore/GenericFileStoreBackend.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / os / filestore / GenericFileStoreBackend.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "include/int_types.h"
16 #include "include/types.h"
17
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25
26 #if defined(__linux__)
27 #include <linux/fs.h>
28 #endif
29
30 #include "include/compat.h"
31 #include "include/linux_fiemap.h"
32
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36
37 #include "GenericFileStoreBackend.h"
38
39 #include "common/errno.h"
40 #include "common/config.h"
41 #include "common/sync_filesystem.h"
42 #include "common/blkdev.h"
43
44 #include "common/SloppyCRCMap.h"
45 #include "os/filestore/chain_xattr.h"
46
47 #define SLOPPY_CRC_XATTR "user.cephos.scrc"
48
49
50 #define dout_context cct()
51 #define dout_subsys ceph_subsys_filestore
52 #undef dout_prefix
53 #define dout_prefix *_dout << "genericfilestorebackend(" << get_basedir_path() << ") "
54
55 #define ALIGN_DOWN(x, by) ((x) - ((x) % (by)))
56 #define ALIGNED(x, by) (!((x) % (by)))
57 #define ALIGN_UP(x, by) (ALIGNED((x), (by)) ? (x) : (ALIGN_DOWN((x), (by)) + (by)))
58
59 GenericFileStoreBackend::GenericFileStoreBackend(FileStore *fs):
60 FileStoreBackend(fs),
61 ioctl_fiemap(false),
62 seek_data_hole(false),
63 use_splice(false),
64 m_filestore_fiemap(cct()->_conf->filestore_fiemap),
65 m_filestore_seek_data_hole(cct()->_conf->filestore_seek_data_hole),
66 m_filestore_fsync_flushes_journal_data(cct()->_conf->filestore_fsync_flushes_journal_data),
67 m_filestore_splice(cct()->_conf->filestore_splice)
68 {
69 // rotational?
70 {
71 // NOTE: the below won't work on btrfs; we'll assume rotational.
72 string fn = get_basedir_path();
73 int fd = ::open(fn.c_str(), O_RDONLY|O_CLOEXEC);
74 if (fd < 0) {
75 return;
76 }
77 BlkDev blkdev(fd);
78 m_rotational = blkdev.is_rotational();
79 dout(20) << __func__ << " basedir " << fn
80 << " rotational " << (int)m_rotational << dendl;
81 ::close(fd);
82 }
83 // journal rotational?
84 {
85 // NOTE: the below won't work on btrfs; we'll assume rotational.
86 string fn = get_journal_path();
87 int fd = ::open(fn.c_str(), O_RDONLY|O_CLOEXEC);
88 if (fd < 0) {
89 return;
90 }
91 BlkDev blkdev(fd);
92 m_journal_rotational = blkdev.is_rotational();
93 dout(20) << __func__ << " journal filename " << fn.c_str()
94 << " journal rotational " << (int)m_journal_rotational << dendl;
95 ::close(fd);
96 }
97 }
98
99 int GenericFileStoreBackend::detect_features()
100 {
101 char fn[PATH_MAX];
102 snprintf(fn, sizeof(fn), "%s/fiemap_test", get_basedir_path().c_str());
103
104 int fd = ::open(fn, O_CREAT|O_RDWR|O_TRUNC|O_CLOEXEC, 0644);
105 if (fd < 0) {
106 fd = -errno;
107 derr << "detect_features: unable to create " << fn << ": " << cpp_strerror(fd) << dendl;
108 return fd;
109 }
110
111 // ext4 has a bug in older kernels where fiemap will return an empty
112 // result in some cases. this is a file layout that triggers the bug
113 // on 2.6.34-rc5.
114 int v[] = {
115 0x0000000000016000, 0x0000000000007000,
116 0x000000000004a000, 0x0000000000007000,
117 0x0000000000060000, 0x0000000000001000,
118 0x0000000000061000, 0x0000000000008000,
119 0x0000000000069000, 0x0000000000007000,
120 0x00000000000a3000, 0x000000000000c000,
121 0x000000000024e000, 0x000000000000c000,
122 0x000000000028b000, 0x0000000000009000,
123 0x00000000002b1000, 0x0000000000003000,
124 0, 0
125 };
126 for (int i=0; v[i]; i++) {
127 int off = v[i++];
128 int len = v[i];
129
130 // write a large extent
131 char buf[len];
132 memset(buf, 1, sizeof(buf));
133 int r = ::lseek(fd, off, SEEK_SET);
134 if (r < 0) {
135 r = -errno;
136 derr << "detect_features: failed to lseek " << fn << ": " << cpp_strerror(r) << dendl;
137 VOID_TEMP_FAILURE_RETRY(::close(fd));
138 return r;
139 }
140 r = write(fd, buf, sizeof(buf));
141 if (r < 0) {
142 derr << "detect_features: failed to write to " << fn << ": " << cpp_strerror(r) << dendl;
143 VOID_TEMP_FAILURE_RETRY(::close(fd));
144 return r;
145 }
146 }
147
148 // fiemap an extent inside that
149 if (!m_filestore_fiemap) {
150 dout(0) << "detect_features: FIEMAP ioctl is disabled via 'filestore fiemap' config option" << dendl;
151 ioctl_fiemap = false;
152 } else {
153 struct fiemap *fiemap;
154 int r = do_fiemap(fd, 2430421, 59284, &fiemap);
155 if (r < 0) {
156 dout(0) << "detect_features: FIEMAP ioctl is NOT supported" << dendl;
157 ioctl_fiemap = false;
158 } else {
159 if (fiemap->fm_mapped_extents == 0) {
160 dout(0) << "detect_features: FIEMAP ioctl is supported, but buggy -- upgrade your kernel" << dendl;
161 ioctl_fiemap = false;
162 } else {
163 dout(0) << "detect_features: FIEMAP ioctl is supported and appears to work" << dendl;
164 ioctl_fiemap = true;
165 }
166 free(fiemap);
167 }
168 }
169
170 // SEEK_DATA/SEEK_HOLE detection
171 if (!m_filestore_seek_data_hole) {
172 dout(0) << "detect_features: SEEK_DATA/SEEK_HOLE is disabled via 'filestore seek data hole' config option" << dendl;
173 seek_data_hole = false;
174 } else {
175 #if defined(__linux__) && defined(SEEK_HOLE) && defined(SEEK_DATA)
176 // If compiled on an OS with SEEK_HOLE/SEEK_DATA support, but running
177 // on an OS that doesn't support SEEK_HOLE/SEEK_DATA, EINVAL is returned.
178 // Fall back to use fiemap.
179 off_t hole_pos;
180
181 hole_pos = lseek(fd, 0, SEEK_HOLE);
182 if (hole_pos < 0) {
183 if (errno == EINVAL) {
184 dout(0) << "detect_features: lseek SEEK_DATA/SEEK_HOLE is NOT supported" << dendl;
185 seek_data_hole = false;
186 } else {
187 derr << "detect_features: failed to lseek " << fn << ": " << cpp_strerror(-errno) << dendl;
188 VOID_TEMP_FAILURE_RETRY(::close(fd));
189 return -errno;
190 }
191 } else {
192 dout(0) << "detect_features: lseek SEEK_DATA/SEEK_HOLE is supported" << dendl;
193 seek_data_hole = true;
194 }
195 #endif
196 }
197
198 //splice detection
199 #ifdef CEPH_HAVE_SPLICE
200 if (!m_filestore_splice) {
201 dout(0) << __func__ << ": splice() is disabled via 'filestore splice' config option" << dendl;
202 use_splice = false;
203 } else {
204 int pipefd[2];
205 loff_t off_in = 0;
206 int r;
207 if (pipe_cloexec(pipefd, 0) < 0) {
208 int e = errno;
209 dout(0) << "detect_features: splice pipe met error " << cpp_strerror(e) << dendl;
210 } else {
211 lseek(fd, 0, SEEK_SET);
212 r = splice(fd, &off_in, pipefd[1], NULL, 10, 0);
213 if (!(r < 0 && errno == EINVAL)) {
214 use_splice = true;
215 dout(0) << "detect_features: splice is supported" << dendl;
216 } else
217 dout(0) << "detect_features: splice is NOT supported" << dendl;
218 close(pipefd[0]);
219 close(pipefd[1]);
220 }
221 }
222 #endif
223 ::unlink(fn);
224 VOID_TEMP_FAILURE_RETRY(::close(fd));
225
226
227 bool have_syncfs = false;
228 #ifdef HAVE_SYS_SYNCFS
229 if (::syncfs(get_basedir_fd()) == 0) {
230 dout(0) << "detect_features: syncfs(2) syscall fully supported (by glibc and kernel)" << dendl;
231 have_syncfs = true;
232 } else {
233 dout(0) << "detect_features: syncfs(2) syscall supported by glibc BUT NOT the kernel" << dendl;
234 }
235 #elif defined(SYS_syncfs)
236 if (syscall(SYS_syncfs, get_basedir_fd()) == 0) {
237 dout(0) << "detect_features: syscall(SYS_syncfs, fd) fully supported" << dendl;
238 have_syncfs = true;
239 } else {
240 dout(0) << "detect_features: syscall(SYS_syncfs, fd) supported by libc BUT NOT the kernel" << dendl;
241 }
242 #elif defined(__NR_syncfs)
243 if (syscall(__NR_syncfs, get_basedir_fd()) == 0) {
244 dout(0) << "detect_features: syscall(__NR_syncfs, fd) fully supported" << dendl;
245 have_syncfs = true;
246 } else {
247 dout(0) << "detect_features: syscall(__NR_syncfs, fd) supported by libc BUT NOT the kernel" << dendl;
248 }
249 #endif
250 if (!have_syncfs) {
251 dout(0) << "detect_features: syncfs(2) syscall not supported" << dendl;
252 if (m_filestore_fsync_flushes_journal_data) {
253 dout(0) << "detect_features: no syncfs(2), but 'filestore fsync flushes journal data = true', so fsync will suffice." << dendl;
254 } else {
255 dout(0) << "detect_features: no syncfs(2), must use sync(2)." << dendl;
256 dout(0) << "detect_features: WARNING: multiple ceph-osd daemons on the same host will be slow" << dendl;
257 }
258 }
259
260 return 0;
261 }
262
263 int GenericFileStoreBackend::create_current()
264 {
265 struct stat st;
266 int ret = ::stat(get_current_path().c_str(), &st);
267 if (ret == 0) {
268 // current/ exists
269 if (!S_ISDIR(st.st_mode)) {
270 dout(0) << "_create_current: current/ exists but is not a directory" << dendl;
271 ret = -EINVAL;
272 }
273 } else {
274 ret = ::mkdir(get_current_path().c_str(), 0755);
275 if (ret < 0) {
276 ret = -errno;
277 dout(0) << "_create_current: mkdir " << get_current_path() << " failed: "<< cpp_strerror(ret) << dendl;
278 }
279 }
280 return ret;
281 }
282
283 int GenericFileStoreBackend::syncfs()
284 {
285 int ret;
286 if (m_filestore_fsync_flushes_journal_data) {
287 dout(15) << "syncfs: doing fsync on " << get_op_fd() << dendl;
288 // make the file system's journal commit.
289 // this works with ext3, but NOT ext4
290 ret = ::fsync(get_op_fd());
291 if (ret < 0)
292 ret = -errno;
293 } else {
294 dout(15) << "syncfs: doing a full sync (syncfs(2) if possible)" << dendl;
295 ret = sync_filesystem(get_current_fd());
296 }
297 return ret;
298 }
299
300 int GenericFileStoreBackend::do_fiemap(int fd, off_t start, size_t len, struct fiemap **pfiemap)
301 {
302 struct fiemap *fiemap = NULL;
303 struct fiemap *_realloc_fiemap = NULL;
304 int size;
305 int ret;
306
307 fiemap = (struct fiemap*)calloc(sizeof(struct fiemap), 1);
308 if (!fiemap)
309 return -ENOMEM;
310 /*
311 * There is a bug on xfs about fiemap. Suppose(offset=3990, len=4096),
312 * the result is (logical=4096, len=4096). It leak the [3990, 4096).
313 * Commit:"xfs: fix rounding error of fiemap length parameter
314 * (eedf32bfcace7d8e20cc66757d74fc68f3439ff7)" fix this bug.
315 * Here, we make offset aligned with CEPH_PAGE_SIZE to avoid this bug.
316 */
317 fiemap->fm_start = start - start % CEPH_PAGE_SIZE;
318 fiemap->fm_length = len + start % CEPH_PAGE_SIZE;
319 fiemap->fm_flags = FIEMAP_FLAG_SYNC; /* flush extents to disk if needed */
320
321 #if defined(__APPLE__) || defined(__FreeBSD__)
322 ret = -ENOTSUP;
323 goto done_err;
324 #else
325 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
326 ret = -errno;
327 goto done_err;
328 }
329 #endif
330 size = sizeof(struct fiemap_extent) * (fiemap->fm_mapped_extents);
331
332 _realloc_fiemap = (struct fiemap *)realloc(fiemap, sizeof(struct fiemap) + size);
333 if (!_realloc_fiemap) {
334 ret = -ENOMEM;
335 goto done_err;
336 } else {
337 fiemap = _realloc_fiemap;
338 }
339
340 memset(fiemap->fm_extents, 0, size);
341
342 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
343 fiemap->fm_mapped_extents = 0;
344
345 #if defined(__APPLE__) || defined(__FreeBSD__)
346 ret = -ENOTSUP;
347 goto done_err;
348 #else
349 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
350 ret = -errno;
351 goto done_err;
352 }
353 *pfiemap = fiemap;
354 #endif
355 return 0;
356
357 done_err:
358 *pfiemap = NULL;
359 free(fiemap);
360 return ret;
361 }
362
363
364 int GenericFileStoreBackend::_crc_load_or_init(int fd, SloppyCRCMap *cm)
365 {
366 char buf[100];
367 bufferptr bp;
368 int r = 0;
369 int l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, buf, sizeof(buf));
370 if (l == -ENODATA) {
371 return 0;
372 }
373 if (l >= 0) {
374 bp = buffer::create(l);
375 memcpy(bp.c_str(), buf, l);
376 } else if (l == -ERANGE) {
377 l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, 0, 0);
378 if (l > 0) {
379 bp = buffer::create(l);
380 l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, bp.c_str(), l);
381 }
382 }
383 bufferlist bl;
384 bl.append(std::move(bp));
385 auto p = bl.cbegin();
386 try {
387 decode(*cm, p);
388 }
389 catch (buffer::error &e) {
390 r = -EIO;
391 }
392 if (r < 0)
393 derr << __func__ << " got " << cpp_strerror(r) << dendl;
394 return r;
395 }
396
397 int GenericFileStoreBackend::_crc_save(int fd, SloppyCRCMap *cm)
398 {
399 bufferlist bl;
400 encode(*cm, bl);
401 int r = chain_fsetxattr(fd, SLOPPY_CRC_XATTR, bl.c_str(), bl.length());
402 if (r < 0)
403 derr << __func__ << " got " << cpp_strerror(r) << dendl;
404 return r;
405 }
406
407 int GenericFileStoreBackend::_crc_update_write(int fd, loff_t off, size_t len, const bufferlist& bl)
408 {
409 SloppyCRCMap scm(get_crc_block_size());
410 int r = _crc_load_or_init(fd, &scm);
411 if (r < 0)
412 return r;
413 ostringstream ss;
414 scm.write(off, len, bl, &ss);
415 dout(30) << __func__ << "\n" << ss.str() << dendl;
416 r = _crc_save(fd, &scm);
417 return r;
418 }
419
420 int GenericFileStoreBackend::_crc_update_truncate(int fd, loff_t off)
421 {
422 SloppyCRCMap scm(get_crc_block_size());
423 int r = _crc_load_or_init(fd, &scm);
424 if (r < 0)
425 return r;
426 scm.truncate(off);
427 r = _crc_save(fd, &scm);
428 return r;
429 }
430
431 int GenericFileStoreBackend::_crc_update_zero(int fd, loff_t off, size_t len)
432 {
433 SloppyCRCMap scm(get_crc_block_size());
434 int r = _crc_load_or_init(fd, &scm);
435 if (r < 0)
436 return r;
437 scm.zero(off, len);
438 r = _crc_save(fd, &scm);
439 return r;
440 }
441
442 int GenericFileStoreBackend::_crc_update_clone_range(int srcfd, int destfd,
443 loff_t srcoff, size_t len, loff_t dstoff)
444 {
445 SloppyCRCMap scm_src(get_crc_block_size());
446 SloppyCRCMap scm_dst(get_crc_block_size());
447 int r = _crc_load_or_init(srcfd, &scm_src);
448 if (r < 0)
449 return r;
450 r = _crc_load_or_init(destfd, &scm_dst);
451 if (r < 0)
452 return r;
453 ostringstream ss;
454 scm_dst.clone_range(srcoff, len, dstoff, scm_src, &ss);
455 dout(30) << __func__ << "\n" << ss.str() << dendl;
456 r = _crc_save(destfd, &scm_dst);
457 return r;
458 }
459
460 int GenericFileStoreBackend::_crc_verify_read(int fd, loff_t off, size_t len, const bufferlist& bl,
461 ostream *out)
462 {
463 SloppyCRCMap scm(get_crc_block_size());
464 int r = _crc_load_or_init(fd, &scm);
465 if (r < 0)
466 return r;
467 return scm.read(off, len, bl, out);
468 }