summaryrefslogtreecommitdiff
path: root/src/os/GenericFileStoreBackend.cc
blob: dad1a9c220c61f5a55a4f44bc7b2f8f68c725eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "include/int_types.h"
#include "include/types.h"

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#if defined(__linux__)
#include <linux/fs.h>
#endif

#include "include/compat.h"
#include "include/linux_fiemap.h"

#include <iostream>
#include <fstream>
#include <sstream>

#include "GenericFileStoreBackend.h"

#include "common/errno.h"
#include "common/config.h"
#include "common/sync_filesystem.h"

#include "common/SloppyCRCMap.h"
#include "os/chain_xattr.h"

#define SLOPPY_CRC_XATTR "user.cephos.scrc"


#define dout_subsys ceph_subsys_filestore
#undef dout_prefix
#define dout_prefix *_dout << "genericfilestorebackend(" << get_basedir_path() << ") "

#define ALIGN_DOWN(x, by) ((x) - ((x) % (by)))
#define ALIGNED(x, by) (!((x) % (by)))
#define ALIGN_UP(x, by) (ALIGNED((x), (by)) ? (x) : (ALIGN_DOWN((x), (by)) + (by)))

GenericFileStoreBackend::GenericFileStoreBackend(FileStore *fs):
  FileStoreBackend(fs),
  ioctl_fiemap(false),
  m_filestore_fiemap(g_conf->filestore_fiemap),
  m_filestore_fsync_flushes_journal_data(g_conf->filestore_fsync_flushes_journal_data) {}

int GenericFileStoreBackend::detect_features()
{
  char fn[PATH_MAX];
  snprintf(fn, sizeof(fn), "%s/fiemap_test", get_basedir_path().c_str());

  int fd = ::open(fn, O_CREAT|O_RDWR|O_TRUNC, 0644);
  if (fd < 0) {
    fd = -errno;
    derr << "detect_features: unable to create " << fn << ": " << cpp_strerror(fd) << dendl;
    return fd;
  }

  // ext4 has a bug in older kernels where fiemap will return an empty
  // result in some cases.  this is a file layout that triggers the bug
  // on 2.6.34-rc5.
  int v[] = {
    0x0000000000016000, 0x0000000000007000,
    0x000000000004a000, 0x0000000000007000,
    0x0000000000060000, 0x0000000000001000,
    0x0000000000061000, 0x0000000000008000,
    0x0000000000069000, 0x0000000000007000,
    0x00000000000a3000, 0x000000000000c000,
    0x000000000024e000, 0x000000000000c000,
    0x000000000028b000, 0x0000000000009000,
    0x00000000002b1000, 0x0000000000003000,
    0, 0
  };
  for (int i=0; v[i]; i++) {
    int off = v[i++];
    int len = v[i];

    // write a large extent
    char buf[len];
    memset(buf, 1, sizeof(buf));
    int r = ::lseek(fd, off, SEEK_SET);
    if (r < 0) {
      r = -errno;
      derr << "detect_features: failed to lseek " << fn << ": " << cpp_strerror(r) << dendl;
      TEMP_FAILURE_RETRY(::close(fd));
      return r;
    }
    r = write(fd, buf, sizeof(buf));
    if (r < 0) {
      derr << "detect_features: failed to write to " << fn << ": " << cpp_strerror(r) << dendl;
      TEMP_FAILURE_RETRY(::close(fd));
      return r;
    }
  }
  ::fsync(fd);

  // fiemap an extent inside that
  struct fiemap *fiemap;
  int r = do_fiemap(fd, 2430421, 59284, &fiemap);
  if (r < 0) {
    dout(0) << "detect_features: FIEMAP ioctl is NOT supported" << dendl;
    ioctl_fiemap = false;
  } else {
    if (fiemap->fm_mapped_extents == 0) {
      dout(0) << "detect_features: FIEMAP ioctl is supported, but buggy -- upgrade your kernel" << dendl;
      ioctl_fiemap = false;
    } else {
      dout(0) << "detect_features: FIEMAP ioctl is supported and appears to work" << dendl;
      ioctl_fiemap = true;
    }
  }
  if (!m_filestore_fiemap) {
    dout(0) << "detect_features: FIEMAP ioctl is disabled via 'filestore fiemap' config option" << dendl;
    ioctl_fiemap = false;
  }
  free(fiemap);

  ::unlink(fn);
  TEMP_FAILURE_RETRY(::close(fd));


  bool have_syncfs = false;
#ifdef HAVE_SYS_SYNCFS
  if (::syncfs(get_basedir_fd()) == 0) {
    dout(0) << "detect_features: syncfs(2) syscall fully supported (by glibc and kernel)" << dendl;
    have_syncfs = true;
  } else {
    dout(0) << "detect_features: syncfs(2) syscall supported by glibc BUT NOT the kernel" << dendl;
  }
#elif defined(SYS_syncfs)
  if (syscall(SYS_syncfs, get_basedir_fd()) == 0) {
    dout(0) << "detect_features: syscall(SYS_syncfs, fd) fully supported" << dendl;
    have_syncfs = true;
  } else {
    dout(0) << "detect_features: syscall(SYS_syncfs, fd) supported by libc BUT NOT the kernel" << dendl;
  }
#elif defined(__NR_syncfs)
  if (syscall(__NR_syncfs, get_basedir_fd()) == 0) {
    dout(0) << "detect_features: syscall(__NR_syncfs, fd) fully supported" << dendl;
    have_syncfs = true;
  } else {
    dout(0) << "detect_features: syscall(__NR_syncfs, fd) supported by libc BUT NOT the kernel" << dendl;
  }
#endif
  if (!have_syncfs) {
    dout(0) << "detect_features: syncfs(2) syscall not supported" << dendl;
    if (m_filestore_fsync_flushes_journal_data) {
      dout(0) << "detect_features: no syncfs(2), but 'filestore fsync flushes journal data = true', so fsync will suffice." << dendl;
    } else {
      dout(0) << "detect_features: no syncfs(2), must use sync(2)." << dendl;
      dout(0) << "detect_features: WARNING: multiple ceph-osd daemons on the same host will be slow" << dendl;
    }
  }

  return 0;
}

int GenericFileStoreBackend::create_current()
{
  struct stat st;
  int ret = ::stat(get_current_path().c_str(), &st);
  if (ret == 0) {
    // current/ exists
    if (!S_ISDIR(st.st_mode)) {
      dout(0) << "_create_current: current/ exists but is not a directory" << dendl;
      ret = -EINVAL;
    }
  } else {
    ret = ::mkdir(get_current_path().c_str(), 0755);
    if (ret < 0) {
      ret = -errno;
      dout(0) << "_create_current: mkdir " << get_current_path() << " failed: "<< cpp_strerror(ret) << dendl;
    }
  }
  return ret;
}

int GenericFileStoreBackend::syncfs()
{
  int ret;
  if (m_filestore_fsync_flushes_journal_data) {
    dout(15) << "syncfs: doing fsync on " << get_op_fd() << dendl;
    // make the file system's journal commit.
    //  this works with ext3, but NOT ext4
    ret = ::fsync(get_op_fd());
    if (ret < 0)
      ret = -errno;
  } else {
    dout(15) << "syncfs: doing a full sync (syncfs(2) if possible)" << dendl;
    ret = sync_filesystem(get_current_fd());
  }
  return ret;
}

int GenericFileStoreBackend::do_fiemap(int fd, off_t start, size_t len, struct fiemap **pfiemap)
{
  struct fiemap *fiemap = NULL;
  struct fiemap *_realloc_fiemap = NULL;
  int size;
  int ret;

  fiemap = (struct fiemap*)calloc(sizeof(struct fiemap), 1);
  if (!fiemap)
    return -ENOMEM;

  fiemap->fm_start = start;
  fiemap->fm_length = len;

  fsync(fd); /* flush extents to disk if needed */

  if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
    ret = -errno;
    goto done_err;
  }

  size = sizeof(struct fiemap_extent) * (fiemap->fm_mapped_extents);

  _realloc_fiemap = (struct fiemap *)realloc(fiemap, sizeof(struct fiemap) + size);
  if (!_realloc_fiemap) {
    ret = -ENOMEM;
    goto done_err;
  } else {
    fiemap = _realloc_fiemap;
  }

  memset(fiemap->fm_extents, 0, size);

  fiemap->fm_extent_count = fiemap->fm_mapped_extents;
  fiemap->fm_mapped_extents = 0;

  if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
    ret = -errno;
    goto done_err;
  }
  *pfiemap = fiemap;

  return 0;

done_err:
  *pfiemap = NULL;
  free(fiemap);
  return ret;
}


int GenericFileStoreBackend::_crc_load_or_init(int fd, SloppyCRCMap *cm)
{
  char buf[100];
  bufferptr bp;
  int l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, buf, sizeof(buf));
  if (l == -ENODATA) {
    return 0;
  }
  if (l >= 0) {
    bp = buffer::create(l);
    memcpy(bp.c_str(), buf, l);
  } else if (l == -ERANGE) {
    l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, 0, 0);
    if (l > 0) {
      bp = buffer::create(l);
      l = chain_fgetxattr(fd, SLOPPY_CRC_XATTR, bp.c_str(), l);
    }
  }
  bufferlist bl;
  bl.append(bp);
  bufferlist::iterator p = bl.begin();
  try {
    ::decode(*cm, p);
  }
  catch (buffer::error &e) {
    return -EIO;
  }
  return 0;
}

int GenericFileStoreBackend::_crc_save(int fd, SloppyCRCMap *cm)
{
  bufferlist bl;
  ::encode(*cm, bl);
  return chain_fsetxattr(fd, SLOPPY_CRC_XATTR, bl.c_str(), bl.length());
}

int GenericFileStoreBackend::_crc_update_write(int fd, loff_t off, size_t len, const bufferlist& bl)
{
  SloppyCRCMap scm(get_crc_block_size());
  int r = _crc_load_or_init(fd, &scm);
  if (r < 0)
    return r;
  ostringstream ss;
  scm.write(off, len, bl, &ss);
  dout(30) << __func__ << "\n" << ss.str() << dendl;
  r = _crc_save(fd, &scm);
  return r;
}

int GenericFileStoreBackend::_crc_update_truncate(int fd, loff_t off)
{
  SloppyCRCMap scm(get_crc_block_size());
  int r = _crc_load_or_init(fd, &scm);
  if (r < 0)
    return r;
  scm.truncate(off);
  r = _crc_save(fd, &scm);
  return r;
}

int GenericFileStoreBackend::_crc_update_zero(int fd, loff_t off, size_t len)
{
  SloppyCRCMap scm(get_crc_block_size());
  int r = _crc_load_or_init(fd, &scm);
  if (r < 0)
    return r;
  scm.zero(off, len);
  r = _crc_save(fd, &scm);
  return r;
}

int GenericFileStoreBackend::_crc_update_clone_range(int srcfd, int destfd,
						     loff_t srcoff, size_t len, loff_t dstoff)
{
  SloppyCRCMap scm_src(get_crc_block_size());
  SloppyCRCMap scm_dst(get_crc_block_size());
  int r = _crc_load_or_init(srcfd, &scm_src);
  if (r < 0)
    return r;
  r = _crc_load_or_init(destfd, &scm_dst);
  if (r < 0)
    return r;
  ostringstream ss;
  scm_dst.clone_range(srcoff, len, dstoff, scm_src, &ss);
  dout(30) << __func__ << "\n" << ss.str() << dendl;
  r = _crc_save(destfd, &scm_dst);
  return r;
}

int GenericFileStoreBackend::_crc_verify_read(int fd, loff_t off, size_t len, const bufferlist& bl,
					      ostream *out)
{
  SloppyCRCMap scm(get_crc_block_size());
  int r = _crc_load_or_init(fd, &scm);
  if (r < 0)
    return r;
  return scm.read(off, len, bl, out);
}