]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/sync_filesystem.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / common / sync_filesystem.h
CommitLineData
7c673cae
FG
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) 2011 New Dream Network
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#ifndef CEPH_SYNC_FILESYSTEM_H
16#define CEPH_SYNC_FILESYSTEM_H
17
18#include <unistd.h>
19
20#if defined(__linux__)
21#include <sys/ioctl.h>
22#include <syscall.h>
23#include "os/fs/btrfs_ioctl.h"
24#endif
25
26inline int sync_filesystem(int fd)
27{
28 /* On Linux, newer versions of glibc have a function called syncfs that
29 * performs a sync on only one filesystem. If we don't have this call, we
30 * have to fall back on sync(), which synchronizes every filesystem on the
31 * computer. */
32#ifdef HAVE_SYS_SYNCFS
33 if (syncfs(fd) == 0)
34 return 0;
35#elif defined(SYS_syncfs)
36 if (syscall(SYS_syncfs, fd) == 0)
37 return 0;
38#elif defined(__NR_syncfs)
39 if (syscall(__NR_syncfs, fd) == 0)
40 return 0;
41#endif
42
43#if defined(HAVE_SYS_SYNCFS) || defined(SYS_syncfs) || defined(__NR_syncfs)
44 else if (errno == ENOSYS) {
45 sync();
46 return 0;
47 } else {
48 return -errno;
49 }
50#else
51 sync();
52 return 0;
53#endif
54}
55
56#endif