Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / os / fs / XFS.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 Red Hat
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 "XFS.h"
16
17 #include <xfs/xfs.h>
18
19 int XFS::set_alloc_hint(int fd, uint64_t val)
20 {
21   struct fsxattr fsx;
22   struct stat sb;
23   int ret;
24
25   if (fstat(fd, &sb) < 0) {
26     ret = -errno;
27     return ret;
28   }
29   if (!S_ISREG(sb.st_mode)) {
30     return -EINVAL;
31   }
32
33   if (ioctl(fd, XFS_IOC_FSGETXATTR, &fsx) < 0) {
34     ret = -errno;
35     return ret;
36   }
37
38   // already set?
39   if ((fsx.fsx_xflags & XFS_XFLAG_EXTSIZE) && fsx.fsx_extsize == val)
40     return 0;
41
42   // xfs won't change extent size if any extents are allocated
43   if (fsx.fsx_nextents != 0)
44     return 0;
45
46   fsx.fsx_xflags |= XFS_XFLAG_EXTSIZE;
47   fsx.fsx_extsize = val;
48
49   if (ioctl(fd, XFS_IOC_FSSETXATTR, &fsx) < 0) {
50     ret = -errno;
51     return ret;
52   }
53
54   return 0;
55 }