Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / os / filestore / XfsFileStoreBackend.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) 2014 Inktank, Inc
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 "XfsFileStoreBackend.h"
16
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <sys/utsname.h>
23
24 #include <xfs/xfs.h>
25
26 #include "common/errno.h"
27 #include "common/linux_version.h"
28 #include "include/assert.h"
29 #include "include/compat.h"
30
31 #define dout_context cct()
32 #define dout_subsys ceph_subsys_filestore
33 #undef dout_prefix
34 #define dout_prefix *_dout << "xfsfilestorebackend(" << get_basedir_path() << ") "
35
36 XfsFileStoreBackend::XfsFileStoreBackend(FileStore *fs):
37   GenericFileStoreBackend(fs), m_has_extsize(false) { }
38
39 /*
40  * Set extsize attr on a file to val.  Should be a free-standing
41  * function, but dout_prefix expanding to a call to get_basedir_path()
42  * protected member function won't let it.
43  */
44 int XfsFileStoreBackend::set_extsize(int fd, unsigned int val)
45 {
46   struct fsxattr fsx;
47   struct stat sb;
48   int ret;
49
50   if (fstat(fd, &sb) < 0) {
51     ret = -errno;
52     dout(0) << "set_extsize: fstat: " << cpp_strerror(ret) << dendl;
53     return ret;
54   }
55   if (!S_ISREG(sb.st_mode)) {
56     dout(0) << "set_extsize: invalid target file type" << dendl;
57     return -EINVAL;
58   }
59
60   if (ioctl(fd, XFS_IOC_FSGETXATTR, &fsx) < 0) {
61     ret = -errno;
62     dout(0) << "set_extsize: FSGETXATTR: " << cpp_strerror(ret) << dendl;
63     return ret;
64   }
65
66   // already set?
67   if ((fsx.fsx_xflags & XFS_XFLAG_EXTSIZE) && fsx.fsx_extsize == val)
68     return 0;
69
70   // xfs won't change extent size if any extents are allocated
71   if (fsx.fsx_nextents != 0)
72     return 0;
73
74   fsx.fsx_xflags |= XFS_XFLAG_EXTSIZE;
75   fsx.fsx_extsize = val;
76
77   if (ioctl(fd, XFS_IOC_FSSETXATTR, &fsx) < 0) {
78     ret = -errno;
79     dout(0) << "set_extsize: FSSETXATTR: " << cpp_strerror(ret) << dendl;
80     return ret;
81   }
82
83   return 0;
84 }
85
86 int XfsFileStoreBackend::detect_features()
87 {
88   int ret;
89
90   ret = GenericFileStoreBackend::detect_features();
91   if (ret < 0)
92     return ret;
93
94   // extsize?
95   int fd = ::openat(get_basedir_fd(), "extsize_test", O_CREAT|O_WRONLY, 0600);
96   if (fd < 0) {
97     ret = -errno;
98     dout(0) << "detect_feature: failed to create test file for extsize attr: "
99             << cpp_strerror(ret) << dendl;
100     goto out;
101   }
102   if (::unlinkat(get_basedir_fd(), "extsize_test", 0) < 0) {
103     ret = -errno;
104     dout(0) << "detect_feature: failed to unlink test file for extsize attr: "
105             << cpp_strerror(ret) << dendl;
106     goto out_close;
107   }
108
109   if (cct()->_conf->filestore_xfs_extsize) {
110     ret = set_extsize(fd, 1U << 15); // a few pages
111     if (ret) {
112       ret = 0;
113       dout(0) << "detect_feature: failed to set test file extsize, assuming extsize is NOT supported" << dendl;
114       goto out_close;
115     }
116
117     // make sure we have 3.5 or newer, which includes this fix
118     //   aff3a9edb7080f69f07fe76a8bd089b3dfa4cb5d
119     // for this set_extsize bug
120     //   http://oss.sgi.com/bugzilla/show_bug.cgi?id=874
121     int ver = get_linux_version();
122     if (ver == 0) {
123       dout(0) << __func__ << ": couldn't verify extsize not buggy, disabling extsize" << dendl;
124       m_has_extsize = false;
125     } else if (ver < KERNEL_VERSION(3, 5, 0)) {
126       dout(0) << __func__ << ": disabling extsize, your kernel < 3.5 and has buggy extsize ioctl" << dendl;
127       m_has_extsize = false;
128     } else {
129       dout(0) << __func__ << ": extsize is supported and your kernel >= 3.5" << dendl;
130       m_has_extsize = true;
131     }
132   } else {
133     dout(0) << "detect_feature: extsize is disabled by conf" << dendl;
134   }
135
136 out_close:
137   TEMP_FAILURE_RETRY(::close(fd));
138 out:
139   return ret;
140 }
141
142 int XfsFileStoreBackend::set_alloc_hint(int fd, uint64_t hint)
143 {
144   if (!m_has_extsize)
145     return -EOPNOTSUPP;
146
147   assert(hint < UINT_MAX);
148   return set_extsize(fd, hint);
149 }