summaryrefslogtreecommitdiffstats
path: root/src/os/filestore/ZFSFileStoreBackend.cc
blob: e85dbd52636c1187106af65ce60fb024e2445f02 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#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>

#include "include/compat.h"
#include "include/linux_fiemap.h"
#include "include/color.h"
#include "include/buffer.h"
#include "include/ceph_assert.h"

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

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

#include "ZFSFileStoreBackend.h"

#define dout_context cct()
#define dout_subsys ceph_subsys_filestore
#undef dout_prefix
#define dout_prefix *_dout << "zfsfilestorebackend(" << get_basedir_path() << ") "

ZFSFileStoreBackend::ZFSFileStoreBackend(FileStore *fs) :
  GenericFileStoreBackend(fs), base_zh(NULL), current_zh(NULL),
  m_filestore_zfs_snap(cct()->_conf->filestore_zfs_snap)
{
  int ret = zfs.init();
  if (ret < 0) {
    dout(0) << "ZFSFileStoreBackend: failed to init libzfs" << dendl;
    return;
  }

  base_zh = zfs.path_to_zhandle(get_basedir_path().c_str(), ZFS::TYPE_FILESYSTEM);
  if (!base_zh) {
    dout(0) << "ZFSFileStoreBackend: failed to get zfs handler for basedir" << dendl;
    return;
  }

  update_current_zh();
}

ZFSFileStoreBackend::~ZFSFileStoreBackend()
{
  if (base_zh)
    zfs.close(base_zh);
  if (current_zh)
    zfs.close(current_zh);
}

int ZFSFileStoreBackend::update_current_zh()
{
  char path[PATH_MAX];
  snprintf(path, sizeof(path), "%s/current", zfs.get_name(base_zh));
  ZFS::Handle *zh = zfs.open(path, ZFS::TYPE_FILESYSTEM);
  if (zh) {
    char *mnt;
    if (zfs.is_mounted(zh, &mnt)) {
      int ret = get_current_path() == mnt;
      free(mnt);
      if (ret) {
	current_zh = zh;
	return 0;
      }
    } else {
      int ret = zfs.mount(zh, NULL, 0);
      if (ret < 0) {
	ret = -errno;
	dout(0) << "update_current_zh: zfs_mount '" << zfs.get_name(zh)
		<< "' got " << cpp_strerror(ret) << dendl;
	return ret;
      }
    }
    zfs.close(zh);
  } else {
    dout(0) << "update_current_zh: zfs_open '" << path << "' got NULL" << dendl;
    return -ENOENT;
  }

  zh = zfs.path_to_zhandle(get_current_path().c_str(), ZFS::TYPE_FILESYSTEM);
  if (zh) {
    if (strcmp(zfs.get_name(base_zh), zfs.get_name(zh))) {
      current_zh = zh;
      return 0;
    }
    zfs.close(zh);
    dout(0) << "update_current_zh: basedir and current/ on the same filesystem" << dendl;
  } else {
    dout(0) << "update_current_zh: current/ not exist" << dendl;
  }
  return -ENOENT;
}

int ZFSFileStoreBackend::detect_features()
{
  if (!current_zh)
    dout(0) << "detect_features: null zfs handle for current/" << dendl;
  return 0;
}

bool ZFSFileStoreBackend::can_checkpoint()
{
  return m_filestore_zfs_snap && current_zh != NULL;
}

int ZFSFileStoreBackend::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;
      return -ENOTDIR;
    }
    return 0;
  } else if (errno != ENOENT) {
    ret = -errno;
    dout(0) << "create_current: cannot stat current/ " << cpp_strerror(ret) << dendl;
    return ret;
  }

  char path[PATH_MAX];
  snprintf(path, sizeof(path), "%s/current", zfs.get_name(base_zh));
  ret = zfs.create(path, ZFS::TYPE_FILESYSTEM);
  if (ret < 0 && errno != EEXIST) {
    ret = -errno;
    dout(0) << "create_current: zfs_create '" << path << "' got " << cpp_strerror(ret) << dendl;
    return ret;
  }

  ret = update_current_zh();
  return ret;
}

static int list_checkpoints_callback(ZFS::Handle *zh, void *data)
{
  list<string> *ls = static_cast<list<string> *>(data);
  string str = ZFS::get_name(zh);
  size_t pos = str.find('@');
  ceph_assert(pos != string::npos && pos + 1 != str.length());
  ls->push_back(str.substr(pos + 1));
  return 0;
}

int ZFSFileStoreBackend::list_checkpoints(list<string>& ls)
{
  dout(10) << "list_checkpoints:" << dendl;
  if (!current_zh)
    return -EINVAL;

  list<string> snaps;
  int ret = zfs.iter_snapshots_sorted(current_zh, list_checkpoints_callback, &snaps);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "list_checkpoints: zfs_iter_snapshots_sorted got" << cpp_strerror(ret) << dendl;
    return ret;
  }
  ls.swap(snaps);
  return 0;
}

int ZFSFileStoreBackend::create_checkpoint(const string& name, uint64_t *cid)
{
  dout(10) << "create_checkpoint: '" << name << "'" << dendl;
  if (!current_zh)
    return -EINVAL;

  // looks like zfsonlinux doesn't flush dirty data when taking snapshot
  int ret = sync_filesystem(get_current_fd());
  if (ret < 0) {
    ret = -errno;
    dout(0) << "create_checkpoint: sync_filesystem got" << cpp_strerror(ret) << dendl;
    return ret;
  }

  char path[PATH_MAX];
  snprintf(path, sizeof(path), "%s@%s", zfs.get_name(current_zh), name.c_str());
  ret = zfs.snapshot(path, false);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "create_checkpoint: zfs_snapshot '" << path << "' got" << cpp_strerror(ret) << dendl;
    return ret;
  }
  if (cid)
    *cid = 0;
  return 0;
}

int ZFSFileStoreBackend::rollback_to(const string& name)
{
  dout(10) << "rollback_to: '" << name << "'" << dendl;
  if (!current_zh)
    return -EINVAL;

  // umount current to avoid triggering online rollback deadlock
  int ret;
  if (zfs.is_mounted(current_zh, NULL)) {
    ret = zfs.umount(current_zh, NULL, 0);
    if (ret < 0) {
      ret = -errno;
      dout(0) << "rollback_to: zfs_umount '" << zfs.get_name(current_zh) << "' got" << cpp_strerror(ret) << dendl;
    }
  }

  char path[PATH_MAX];
  snprintf(path, sizeof(path), "%s@%s", zfs.get_name(current_zh), name.c_str());

  ZFS::Handle *snap_zh = zfs.open(path, ZFS::TYPE_SNAPSHOT);
  if (!snap_zh) {
    dout(0) << "rollback_to: zfs_open '" << path << "' got NULL" << dendl;
    return -ENOENT;
  }

  ret = zfs.rollback(current_zh, snap_zh, false);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "rollback_to: zfs_rollback '" << zfs.get_name(snap_zh) << "' got" << cpp_strerror(ret) << dendl;
  }

  if (!zfs.is_mounted(current_zh, NULL)) {
    int ret = zfs.mount(current_zh, NULL, 0);
    if (ret < 0) {
      ret = -errno;
      dout(0) << "update_current_zh: zfs_mount '" << zfs.get_name(current_zh) << "' got " << cpp_strerror(ret) << dendl;
      return ret;
    }
  }

  zfs.close(snap_zh);
  return ret;
}

int ZFSFileStoreBackend::destroy_checkpoint(const string& name)
{
  dout(10) << "destroy_checkpoint: '" << name << "'" << dendl;
  if (!current_zh)
    return -EINVAL;

  int ret = zfs.destroy_snaps(current_zh, name.c_str(), true);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "destroy_checkpoint: zfs_destroy_snaps '" << name << "' got" << cpp_strerror(ret) << dendl;
  }
  return ret;
}