s3: Further fix for bug 8777
[mat/samba.git] / source3 / smbd / statvfs.c
1 /* 
2    Unix SMB/CIFS implementation.
3    VFS API's statvfs abstraction
4    Copyright (C) Alexander Bokovoy                      2005
5    Copyright (C) Steve French                           2005
6    Copyright (C) James Peach                            2006
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "smbd/smbd.h"
25
26 #if defined(DARWINOS)
27
28 #include <sys/attr.h>
29
30 static int darwin_fs_capabilities(const char * path)
31 {
32         int caps = 0;
33         vol_capabilities_attr_t *vcaps;
34         struct attrlist attrlist;
35         char attrbuf[sizeof(u_int32_t) + sizeof(vol_capabilities_attr_t)];
36
37 #define FORMAT_CAP(vinfo, cap) \
38         ( ((vinfo)->valid[VOL_CAPABILITIES_FORMAT] & (cap)) && \
39            ((vinfo)->capabilities[VOL_CAPABILITIES_FORMAT] & (cap)) )
40
41 #define INTERFACE_CAP(vinfo, cap) \
42         ( ((vinfo)->valid[VOL_CAPABILITIES_INTERFACES] & (cap)) && \
43            ((vinfo)->capabilities[VOL_CAPABILITIES_INTERFACES] & (cap)) )
44
45         ZERO_STRUCT(attrlist);
46         attrlist.bitmapcount = ATTR_BIT_MAP_COUNT;
47         attrlist.volattr = ATTR_VOL_CAPABILITIES;
48
49         if (getattrlist(path, &attrlist, attrbuf, sizeof(attrbuf), 0) != 0) {
50                 DEBUG(0, ("getattrlist for %s capabilities failed: %s\n",
51                             path, strerror(errno)));
52                 /* Return no capabilities on failure. */
53                 return 0;
54         }
55
56         vcaps =
57             (vol_capabilities_attr_t *)(attrbuf + sizeof(u_int32_t));
58
59         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_SPARSE_FILES)) {
60                 caps |= FILE_SUPPORTS_SPARSE_FILES;
61         }
62
63         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_SENSITIVE)) {
64                 caps |= FILE_CASE_SENSITIVE_SEARCH;
65         }
66
67         if (FORMAT_CAP(vcaps, VOL_CAP_FMT_CASE_PRESERVING)) {
68                 caps |= FILE_CASE_PRESERVED_NAMES;
69         }
70
71         if (INTERFACE_CAP(vcaps, VOL_CAP_INT_EXTENDED_SECURITY)) {
72                 caps |= FILE_PERSISTENT_ACLS;
73         }
74
75         return caps;
76 }
77
78 static int darwin_statvfs(const char *path, vfs_statvfs_struct *statbuf)
79 {
80         struct statfs sbuf;
81         int ret;
82
83         ret = statfs(path, &sbuf);
84         if (ret != 0) {
85                 return ret;
86         }
87
88         statbuf->OptimalTransferSize = sbuf.f_iosize;
89         statbuf->BlockSize = sbuf.f_bsize;
90         statbuf->TotalBlocks = sbuf.f_blocks;
91         statbuf->BlocksAvail = sbuf.f_bfree;
92         statbuf->UserBlocksAvail = sbuf.f_bavail;
93         statbuf->TotalFileNodes = sbuf.f_files;
94         statbuf->FreeFileNodes = sbuf.f_ffree;
95         statbuf->FsIdentifier = *(uint64_t *)(&sbuf.f_fsid); /* Ick. */
96         statbuf->FsCapabilities = darwin_fs_capabilities(sbuf.f_mntonname);
97
98         return 0;
99 }
100 #elif defined(BSD) && defined(BSD_STATVFS_BSIZE)
101 static int bsd_statvfs(const char *path, vfs_statvfs_struct *statbuf)
102 {
103         struct statfs statfs_buf;
104         int result;
105
106         result = statfs(path, &statfs_buf);
107         if (result != 0) {
108                 return result;
109         }
110
111         statbuf->OptimalTransferSize = statfs_buf.f_iosize;
112         statbuf->BlockSize = statfs_buf.f_bsize;
113         statbuf->TotalBlocks = statfs_buf.f_blocks;
114         statbuf->BlocksAvail = statfs_buf.f_bfree;
115         statbuf->UserBlocksAvail = statfs_buf.f_bavail;
116         statbuf->TotalFileNodes = statfs_buf.f_files;
117         statbuf->FreeFileNodes = statfs_buf.f_ffree;
118         statbuf->FsIdentifier =
119                 (((uint64_t) statfs_buf.f_fsid.val[0] << 32) & 0xffffffff00000000LL) |
120                     (uint64_t) statfs_buf.f_fsid.val[1];
121         /* Try to extrapolate some of the fs flags into the
122          * capabilities
123          */
124         statbuf->FsCapabilities =
125             FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
126 #ifdef MNT_ACLS
127         if (statfs_buf.f_flags & MNT_ACLS)
128                 statbuf->FsCapabilities |= FILE_PERSISTENT_ACLS;
129 #endif
130         if (statfs_buf.f_flags & MNT_QUOTA)
131                 statbuf->FsCapabilities |= FILE_VOLUME_QUOTAS;
132         if (statfs_buf.f_flags & MNT_RDONLY)
133                 statbuf->FsCapabilities |= FILE_READ_ONLY_VOLUME;
134
135         return 0;
136 }
137 #elif defined(STAT_STATVFS) && defined(HAVE_FSID_INT)
138 static int linux_statvfs(const char *path, vfs_statvfs_struct *statbuf)
139 {
140         struct statvfs statvfs_buf;
141         int result;
142
143         result = statvfs(path, &statvfs_buf);
144
145         if (!result) {
146                 statbuf->OptimalTransferSize = statvfs_buf.f_frsize;
147                 statbuf->BlockSize = statvfs_buf.f_bsize;
148                 statbuf->TotalBlocks = statvfs_buf.f_blocks;
149                 statbuf->BlocksAvail = statvfs_buf.f_bfree;
150                 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
151                 statbuf->TotalFileNodes = statvfs_buf.f_files;
152                 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
153                 statbuf->FsIdentifier = statvfs_buf.f_fsid;
154
155                 /* Good defaults for Linux filesystems are case sensitive
156                  * and case preserving.
157                  */
158                 statbuf->FsCapabilities =
159                     FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES;
160         }
161         return result;
162 }
163 #endif
164
165 /* 
166  sys_statvfs() is an abstraction layer over system-dependent statvfs()/statfs()
167  for particular POSIX systems. Due to controversy of what is considered more important
168  between LSB and FreeBSD/POSIX.1 (IEEE Std 1003.1-2001) we need to abstract the interface
169  so that particular OS would use its prefered interface.
170 */
171 int sys_statvfs(const char *path, vfs_statvfs_struct *statbuf)
172 {
173 #if defined(DARWINOS)
174         return darwin_statvfs(path, statbuf);
175 #elif defined(BSD) && defined(BSD_STATVFS_BSIZE)
176         return bsd_statvfs(path, statbuf);
177 #elif defined(STAT_STATVFS) && defined(HAVE_FSID_INT)
178         return linux_statvfs(path, statbuf);
179 #else
180         /* BB change this to return invalid level */
181 #ifdef EOPNOTSUPP
182         return EOPNOTSUPP;
183 #else
184         return -1;
185 #endif /* EOPNOTSUPP */
186 #endif /* LINUX */
187
188 }