summaryrefslogtreecommitdiffstats
path: root/src/os/filestore/BtrfsFileStoreBackend.cc
blob: 27161c22ab752ba16067be8f1141da070623ce93 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
// -*- 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>
#include "include/compat.h"
#include "include/linux_fiemap.h"
#include "include/color.h"
#include "include/buffer.h"
#include "include/ceph_assert.h"

#ifndef __CYGWIN__
#include "os/fs/btrfs_ioctl.h"
#endif

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

#include "BtrfsFileStoreBackend.h"

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

#if defined(__linux__)

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

using std::cerr;
using std::list;
using std::string;

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

BtrfsFileStoreBackend::BtrfsFileStoreBackend(FileStore *fs):
    GenericFileStoreBackend(fs), has_clone_range(false),
    has_snap_create(false), has_snap_destroy(false),
    has_snap_create_v2(false), has_wait_sync(false), stable_commits(false),
    m_filestore_btrfs_clone_range(cct()->_conf->filestore_btrfs_clone_range),
    m_filestore_btrfs_snap (cct()->_conf->filestore_btrfs_snap) { }

int BtrfsFileStoreBackend::detect_features()
{
  int r;

  r = GenericFileStoreBackend::detect_features();
  if (r < 0)
    return r;

  // clone_range?
  if (m_filestore_btrfs_clone_range) {
    int fd = ::openat(get_basedir_fd(), "clone_range_test", O_CREAT|O_WRONLY|O_CLOEXEC, 0600);
    if (fd >= 0) {
      if (::unlinkat(get_basedir_fd(), "clone_range_test", 0) < 0) {
	r = -errno;
	dout(0) << "detect_feature: failed to unlink test file for CLONE_RANGE ioctl: "
		<< cpp_strerror(r) << dendl;
      }
      btrfs_ioctl_clone_range_args clone_args;
      memset(&clone_args, 0, sizeof(clone_args));
      clone_args.src_fd = -1;
      r = ::ioctl(fd, BTRFS_IOC_CLONE_RANGE, &clone_args);
      if (r < 0 && errno == EBADF) {
	dout(0) << "detect_feature: CLONE_RANGE ioctl is supported" << dendl;
	has_clone_range = true;
      } else {
	r = -errno;
	dout(0) << "detect_feature: CLONE_RANGE ioctl is NOT supported: " << cpp_strerror(r) << dendl;
      }
      TEMP_FAILURE_RETRY(::close(fd));
    } else {
      r = -errno;
      dout(0) << "detect_feature: failed to create test file for CLONE_RANGE ioctl: "
	      << cpp_strerror(r) << dendl;
    }
  } else {
    dout(0) << "detect_feature: CLONE_RANGE ioctl is DISABLED via 'filestore btrfs clone range' option" << dendl;
  }

  struct btrfs_ioctl_vol_args vol_args;
  memset(&vol_args, 0, sizeof(vol_args));

  // create test source volume
  vol_args.fd = 0;
  strcpy(vol_args.name, "test_subvol");
  r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SUBVOL_CREATE, &vol_args);
  if (r != 0) {
    r = -errno;
    dout(0) << "detect_feature: failed to create simple subvolume " << vol_args.name << ": " << cpp_strerror(r) << dendl;
  }
  int srcfd = ::openat(get_basedir_fd(), vol_args.name, O_RDONLY|O_CLOEXEC);
  if (srcfd < 0) {
    r = -errno;
    dout(0) << "detect_feature: failed to open " << vol_args.name << ": " << cpp_strerror(r) << dendl;
  }

  // snap_create and snap_destroy?
  vol_args.fd = srcfd;
  strcpy(vol_args.name, "sync_snap_test");
  r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_CREATE, &vol_args);
  int err = errno;
  if (r == 0 || errno == EEXIST) {
    dout(0) << "detect_feature: SNAP_CREATE is supported" << dendl;
    has_snap_create = true;

    r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
    if (r == 0) {
      dout(0) << "detect_feature: SNAP_DESTROY is supported" << dendl;
      has_snap_destroy = true;
    } else {
      err = -errno;
      dout(0) << "detect_feature: SNAP_DESTROY failed: " << cpp_strerror(err) << dendl;

      if (err == -EPERM && getuid() != 0) {
	dout(0) << "detect_feature: failed with EPERM as non-root; remount with -o user_subvol_rm_allowed" << dendl;
	cerr << TEXT_YELLOW
	     << "btrfs SNAP_DESTROY failed as non-root; remount with -o user_subvol_rm_allowed"
	     << TEXT_NORMAL << std::endl;
      } else if (err == -EOPNOTSUPP) {
	derr << "btrfs SNAP_DESTROY ioctl not supported; you need a kernel newer than 2.6.32" << dendl;
      }
    }
  } else {
    dout(0) << "detect_feature: SNAP_CREATE failed: " << cpp_strerror(err) << dendl;
  }

  if (m_filestore_btrfs_snap) {
    if (has_snap_destroy)
      stable_commits = true;
    else
      dout(0) << "detect_feature: snaps enabled, but no SNAP_DESTROY ioctl; DISABLING" << dendl;
  }

  // start_sync?
  __u64 transid = 0;
  r = ::ioctl(get_basedir_fd(), BTRFS_IOC_START_SYNC, &transid);
  if (r < 0) {
    int err = errno;
    dout(0) << "detect_feature: START_SYNC got " << cpp_strerror(err) << dendl;
  }
  if (r == 0 && transid > 0) {
    dout(0) << "detect_feature: START_SYNC is supported (transid " << transid << ")" << dendl;

    // do we have wait_sync too?
    r = ::ioctl(get_basedir_fd(), BTRFS_IOC_WAIT_SYNC, &transid);
    if (r == 0 || errno == ERANGE) {
      dout(0) << "detect_feature: WAIT_SYNC is supported" << dendl;
      has_wait_sync = true;
    } else {
      int err = errno;
      dout(0) << "detect_feature: WAIT_SYNC is NOT supported: " << cpp_strerror(err) << dendl;
    }
  } else {
    int err = errno;
    dout(0) << "detect_feature: START_SYNC is NOT supported: " << cpp_strerror(err) << dendl;
  }

  if (has_wait_sync) {
    // async snap creation?
    struct btrfs_ioctl_vol_args_v2 async_args;
    memset(&async_args, 0, sizeof(async_args));
    async_args.fd = srcfd;
    async_args.flags = BTRFS_SUBVOL_CREATE_ASYNC;
    strcpy(async_args.name, "async_snap_test");

    // remove old one, first
    struct stat st;
    strcpy(vol_args.name, async_args.name);
    if (::fstatat(get_basedir_fd(), vol_args.name, &st, 0) == 0) {
      dout(0) << "detect_feature: removing old async_snap_test" << dendl;
      r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
      if (r != 0) {
	int err = errno;
	dout(0) << "detect_feature: failed to remove old async_snap_test: " << cpp_strerror(err) << dendl;
      }
    }

    r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_CREATE_V2, &async_args);
    if (r == 0 || errno == EEXIST) {
      dout(0) << "detect_feature: SNAP_CREATE_V2 is supported" << dendl;
      has_snap_create_v2 = true;

      // clean up
      strcpy(vol_args.name, "async_snap_test");
      r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
      if (r != 0) {
	int err = errno;
	dout(0) << "detect_feature: SNAP_DESTROY failed: " << cpp_strerror(err) << dendl;
      }
    } else {
      int err = errno;
      dout(0) << "detect_feature: SNAP_CREATE_V2 is NOT supported: " << cpp_strerror(err) << dendl;
    }
  }

  // clean up test subvol
  if (srcfd >= 0)
    TEMP_FAILURE_RETRY(::close(srcfd));

  strcpy(vol_args.name, "test_subvol");
  r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
  if (r < 0) {
    r = -errno;
    dout(0) << "detect_feature: failed to remove " << vol_args.name << ": " << cpp_strerror(r) << dendl;
  }

  if (m_filestore_btrfs_snap && !has_snap_create_v2) {
    dout(0) << "mount WARNING: btrfs snaps enabled, but no SNAP_CREATE_V2 ioctl (from kernel 2.6.37+)" << dendl;
    cerr << TEXT_YELLOW
      << " ** WARNING: 'filestore btrfs snap' is enabled (for safe transactions,\n"
      << "             rollback), but btrfs does not support the SNAP_CREATE_V2 ioctl\n"
      << "             (added in Linux 2.6.37).  Expect slow btrfs sync/commit\n"
      << "             performance.\n"
      << TEXT_NORMAL;
  }

  return 0;
}

bool BtrfsFileStoreBackend::can_checkpoint()
{
  return stable_commits;
}

int BtrfsFileStoreBackend::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 -EINVAL;
    }

    struct stat basest;
    struct statfs currentfs;
    ret = ::fstat(get_basedir_fd(), &basest);
    if (ret < 0) {
      ret = -errno;
      dout(0) << "create_current: cannot fstat basedir " << cpp_strerror(ret) << dendl;
      return ret;
    }
    ret = ::statfs(get_current_path().c_str(), &currentfs);
    if (ret < 0) {
      ret = -errno;
      dout(0) << "create_current: cannot statsf basedir " << cpp_strerror(ret) << dendl;
      return ret;
    }
    if (currentfs.f_type == BTRFS_SUPER_MAGIC && basest.st_dev != st.st_dev) {
      dout(2) << "create_current: current appears to be a btrfs subvolume" << dendl;
      stable_commits = true;
    }
    return 0;
  }

  struct btrfs_ioctl_vol_args volargs;
  memset(&volargs, 0, sizeof(volargs));

  volargs.fd = 0;
  strcpy(volargs.name, "current");
  if (::ioctl(get_basedir_fd(), BTRFS_IOC_SUBVOL_CREATE, (unsigned long int)&volargs) < 0) {
    ret = -errno;
    dout(0) << "create_current: BTRFS_IOC_SUBVOL_CREATE failed with error "
	    << cpp_strerror(ret) << dendl;
    return ret;
  }

  dout(2) << "create_current: created btrfs subvol " << get_current_path() << dendl;
  if (::chmod(get_current_path().c_str(), 0755) < 0) {
    ret = -errno;
    dout(0) << "create_current: failed to chmod " << get_current_path() << " to 0755: "
	    << cpp_strerror(ret) << dendl;
    return ret;
  }

  stable_commits = true;
  return 0;
}

int BtrfsFileStoreBackend::list_checkpoints(list<string>& ls)
{
  int ret, err = 0;

  struct stat basest;
  ret = ::fstat(get_basedir_fd(), &basest);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "list_checkpoints: cannot fstat basedir " << cpp_strerror(ret) << dendl;
    return ret;
  }

  // get snap list
  DIR *dir = ::opendir(get_basedir_path().c_str());
  if (!dir) {
    ret = -errno;
    dout(0) << "list_checkpoints: opendir '" << get_basedir_path() << "' failed: "
	    << cpp_strerror(ret) << dendl;
    return ret;
  }

  list<string> snaps;
  char path[PATH_MAX];
  struct dirent *de;
  while (true) {
    errno = 0;
    de = ::readdir(dir);
    if (de == nullptr) {
      if (errno != 0) {
        err = -errno;
        dout(0) << "list_checkpoints: readdir '" << get_basedir_path() << "' failed: "
                << cpp_strerror(err) << dendl;
      }
      break;
    }
    snprintf(path, sizeof(path), "%s/%s", get_basedir_path().c_str(), de->d_name);

    struct stat st;
    ret = ::stat(path, &st);
    if (ret < 0) {
      err = -errno;
      dout(0) << "list_checkpoints: stat '" << path << "' failed: "
	      << cpp_strerror(err) << dendl;
      break;
    }

    if (!S_ISDIR(st.st_mode))
      continue;

    struct statfs fs;
    ret = ::statfs(path, &fs);
    if (ret < 0) {
      err = -errno;
      dout(0) << "list_checkpoints: statfs '" << path << "' failed: "
	      << cpp_strerror(err) << dendl;
      break;
    }

    if (fs.f_type == BTRFS_SUPER_MAGIC && basest.st_dev != st.st_dev)
      snaps.push_back(string(de->d_name));
  }

  if (::closedir(dir) < 0) {
      ret = -errno;
      dout(0) << "list_checkpoints: closedir failed: " << cpp_strerror(ret) << dendl;
      if (!err)
	err = ret;
  }

  if (err)
    return err;

  ls.swap(snaps);
  return 0;
}

int BtrfsFileStoreBackend::create_checkpoint(const string& name, uint64_t *transid)
{
  dout(10) << "create_checkpoint: '" << name << "'" << dendl;
  if (has_snap_create_v2 && transid) {
    struct btrfs_ioctl_vol_args_v2 async_args;
    memset(&async_args, 0, sizeof(async_args));
    async_args.fd = get_current_fd();
    async_args.flags = BTRFS_SUBVOL_CREATE_ASYNC;

    size_t name_size = sizeof(async_args.name);
    strncpy(async_args.name, name.c_str(), name_size);
    async_args.name[name_size-1] = '\0';

    int r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_CREATE_V2, &async_args);
    if (r < 0) {
      r = -errno;
      dout(0) << "create_checkpoint: async snap create '" << name << "' got " << cpp_strerror(r) << dendl;
      return r;
    }
    dout(20) << "create_checkpoint: async snap create '" << name << "' transid " << async_args.transid << dendl;
    *transid = async_args.transid;
  } else {
    struct btrfs_ioctl_vol_args vol_args;
    memset(&vol_args, 0, sizeof(vol_args));
    vol_args.fd = get_current_fd();

    size_t name_size = sizeof(vol_args.name);
    strncpy(vol_args.name, name.c_str(), name_size);
    vol_args.name[name_size-1] = '\0';

    int r = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_CREATE, &vol_args);
    if (r < 0) {
      r = -errno;
      dout(0) << "create_checkpoint: snap create '" << name << "' got " << cpp_strerror(r) << dendl;
      return r;
    }
    if (transid)
      *transid = 0;
  }
  return 0;
}

int BtrfsFileStoreBackend::sync_checkpoint(uint64_t transid)
{
  // wait for commit
  dout(10) << "sync_checkpoint: transid " << transid << " to complete" << dendl;
  int ret = ::ioctl(get_op_fd(), BTRFS_IOC_WAIT_SYNC, &transid);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "sync_checkpoint: ioctl WAIT_SYNC got " << cpp_strerror(ret) << dendl;
    return -errno;
  }
  dout(20) << "sync_checkpoint: done waiting for transid " << transid << dendl;
  return 0;
}

int BtrfsFileStoreBackend::rollback_to(const string& name)
{
  dout(10) << "rollback_to: to '" << name << "'" << dendl;
  char s[PATH_MAX];
  btrfs_ioctl_vol_args vol_args;

  memset(&vol_args, 0, sizeof(vol_args));
  vol_args.fd = 0;
  strcpy(vol_args.name, "current");

  int ret = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
  if (ret && errno != ENOENT) {
    dout(0) << "rollback_to: error removing old current subvol: " << cpp_strerror(ret) << dendl;
    snprintf(s, sizeof(s), "%s/current.remove.me.%d", get_basedir_path().c_str(), rand());
    if (::rename(get_current_path().c_str(), s)) {
      ret = -errno;
      dout(0) << "rollback_to: error renaming old current subvol: "
	      << cpp_strerror(ret) << dendl;
      return ret;
    }
  }

  snprintf(s, sizeof(s), "%s/%s", get_basedir_path().c_str(), name.c_str());

  // roll back
  vol_args.fd = ::open(s, O_RDONLY|O_CLOEXEC);
  if (vol_args.fd < 0) {
    ret = -errno;
    dout(0) << "rollback_to: error opening '" << s << "': " << cpp_strerror(ret) << dendl;
    return ret;
  }
  ret = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_CREATE, &vol_args);
  if (ret < 0 ) {
    ret = -errno;
    dout(0) << "rollback_to: ioctl SNAP_CREATE got " << cpp_strerror(ret) << dendl;
  }
  TEMP_FAILURE_RETRY(::close(vol_args.fd));
  return ret;
}

int BtrfsFileStoreBackend::destroy_checkpoint(const string& name)
{
  dout(10) << "destroy_checkpoint: '" << name << "'" << dendl;
  btrfs_ioctl_vol_args vol_args;
  memset(&vol_args, 0, sizeof(vol_args));
  vol_args.fd = 0;
  strncpy(vol_args.name, name.c_str(), sizeof(vol_args.name) - 1);
  vol_args.name[sizeof(vol_args.name) - 1] = '\0';

  int ret = ::ioctl(get_basedir_fd(), BTRFS_IOC_SNAP_DESTROY, &vol_args);
  if (ret) {
    ret = -errno;
    dout(0) << "destroy_checkpoint: ioctl SNAP_DESTROY got " << cpp_strerror(ret) << dendl;
    return ret;
  }
  return 0;
}

int BtrfsFileStoreBackend::syncfs()
{
  dout(15) << "syncfs" << dendl;
  // do a full btrfs commit
  int ret = ::ioctl(get_op_fd(), BTRFS_IOC_SYNC);
  if (ret < 0) {
    ret = -errno;
    dout(0) << "syncfs: btrfs IOC_SYNC got " << cpp_strerror(ret) << dendl;
  }
  return ret;
}

int BtrfsFileStoreBackend::clone_range(int from, int to, uint64_t srcoff, uint64_t len, uint64_t dstoff)
{
  dout(20) << "clone_range: " << srcoff << "~" << len << " to " << dstoff << dendl;
  size_t blk_size = get_blksize();
  if (!has_clone_range ||
      srcoff % blk_size != dstoff % blk_size) {
    dout(20) << "clone_range: using copy" << dendl;
    return _copy_range(from, to, srcoff, len, dstoff);
  }

  int err = 0;
  int r = 0;

  uint64_t srcoffclone = ALIGN_UP(srcoff, blk_size);
  uint64_t dstoffclone = ALIGN_UP(dstoff, blk_size);
  if (srcoffclone >= srcoff + len) {
    dout(20) << "clone_range: using copy, extent too short to align srcoff" << dendl;
    return _copy_range(from, to, srcoff, len, dstoff);
  }

  uint64_t lenclone = len - (srcoffclone - srcoff);
  if (!ALIGNED(lenclone, blk_size)) {
    struct stat from_stat, to_stat;
    err = ::fstat(from, &from_stat);
    if (err) return -errno;
    err = ::fstat(to , &to_stat);
    if (err) return -errno;

    if (srcoff + len != (uint64_t)from_stat.st_size ||
	dstoff + len < (uint64_t)to_stat.st_size) {
      // Not to the end of the file, need to align length as well
      lenclone = ALIGN_DOWN(lenclone, blk_size);
    }
  }
  if (lenclone == 0) {
    // too short
    return _copy_range(from, to, srcoff, len, dstoff);
  }

  dout(20) << "clone_range: cloning " << srcoffclone << "~" << lenclone
	   << " to " << dstoffclone << " = " << r << dendl;
  btrfs_ioctl_clone_range_args a;
  a.src_fd = from;
  a.src_offset = srcoffclone;
  a.src_length = lenclone;
  a.dest_offset = dstoffclone;
  err = ::ioctl(to, BTRFS_IOC_CLONE_RANGE, &a);
  if (err >= 0) {
    r += err;
  } else if (errno == EINVAL) {
    // Still failed, might be compressed
    dout(20) << "clone_range: failed CLONE_RANGE call with -EINVAL, using copy" << dendl;
    return _copy_range(from, to, srcoff, len, dstoff);
  } else {
    return -errno;
  }

  // Take care any trimmed from front
  if (srcoffclone != srcoff) {
    err = _copy_range(from, to, srcoff, srcoffclone - srcoff, dstoff);
    if (err >= 0) {
      r += err;
    } else {
      return err;
    }
  }

  // Copy end
  if (srcoffclone + lenclone != srcoff + len) {
    err = _copy_range(from, to,
			 srcoffclone + lenclone,
			 (srcoff + len) - (srcoffclone + lenclone),
			 dstoffclone + lenclone);
    if (err >= 0) {
      r += err;
    } else {
      return err;
    }
  }
  dout(20) << "clone_range: finished " << srcoff << "~" << len
	   << " to " << dstoff << " = " << r << dendl;
  return r;
}
#endif