r23792: convert Samba4 to GPLv3
[ira/wip.git] / source / ntvfs / posix / pvfs_fsinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - fsinfo
5
6    Copyright (C) Andrew Tridgell 2004
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 "vfs_posix.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/ndr/libndr.h"
26
27 /* We use libblkid out of e2fsprogs to identify UUID of a volume */
28 #ifdef HAVE_LIBBLKID
29 #include <blkid/blkid.h>
30 #endif
31
32 static NTSTATUS pvfs_blkid_fs_uuid(struct pvfs_state *pvfs, struct stat *st, struct GUID *uuid)
33 {
34 #ifdef HAVE_LIBBLKID
35         NTSTATUS status;
36         char *uuid_value = NULL;
37         char *devname = NULL;
38
39         devname = blkid_devno_to_devname(st->st_dev);
40         if (!devname) {
41                 ZERO_STRUCTP(uuid);
42                 return NT_STATUS_OK;
43         }
44
45         uuid_value = blkid_get_tag_value(NULL, "UUID", devname);
46         free(devname);
47         if (!uuid_value) {
48                 ZERO_STRUCTP(uuid);
49                 return NT_STATUS_OK;
50         }
51
52         status = GUID_from_string(uuid_value, uuid);
53         free(uuid_value);
54         if (!NT_STATUS_IS_OK(status)) {
55                 ZERO_STRUCTP(uuid);
56                 return NT_STATUS_OK;
57         }
58         return NT_STATUS_OK;
59 #else
60         ZERO_STRUCTP(uuid);
61         return NT_STATUS_OK;
62 #endif
63 }
64
65 static NTSTATUS pvfs_cache_base_fs_uuid(struct pvfs_state *pvfs, struct stat *st)
66 {
67         NTSTATUS status;
68         struct GUID uuid;
69
70         if (pvfs->base_fs_uuid) return NT_STATUS_OK;
71
72         status = pvfs_blkid_fs_uuid(pvfs, st, &uuid);
73         NT_STATUS_NOT_OK_RETURN(status);
74
75         pvfs->base_fs_uuid = talloc(pvfs, struct GUID);
76         NT_STATUS_HAVE_NO_MEMORY(pvfs->base_fs_uuid);
77         *pvfs->base_fs_uuid = uuid;
78
79         return NT_STATUS_OK;
80 }
81 /*
82   return filesystem space info
83 */
84 NTSTATUS pvfs_fsinfo(struct ntvfs_module_context *ntvfs,
85                      struct ntvfs_request *req, union smb_fsinfo *fs)
86 {
87         NTSTATUS status;
88         struct pvfs_state *pvfs = ntvfs->private_data;
89         uint64_t blocks_free, blocks_total;
90         uint_t bpunit;
91         struct stat st;
92         const uint16_t block_size = 512;
93
94         /* only some levels need the expensive sys_fsusage() call */
95         switch (fs->generic.level) {
96         case RAW_QFS_DSKATTR:
97         case RAW_QFS_ALLOCATION:
98         case RAW_QFS_SIZE_INFO:
99         case RAW_QFS_SIZE_INFORMATION:
100         case RAW_QFS_FULL_SIZE_INFORMATION:
101                 if (sys_fsusage(pvfs->base_directory, &blocks_free, &blocks_total) == -1) {
102                         return pvfs_map_errno(pvfs, errno);
103                 }
104         default:
105                 break;
106         }
107
108         if (stat(pvfs->base_directory, &st) != 0) {
109                 return NT_STATUS_DISK_CORRUPT_ERROR;
110         }
111
112         /* now fill in the out fields */
113         switch (fs->generic.level) {
114         case RAW_QFS_GENERIC:
115                 return NT_STATUS_INVALID_LEVEL;
116
117         case RAW_QFS_DSKATTR:
118                 /* we need to scale the sizes to fit */
119                 for (bpunit=64; bpunit<0x10000; bpunit *= 2) {
120                         if (blocks_total * (double)block_size < bpunit * 512 * 65535.0) {
121                                 break;
122                         }
123                 }
124                 fs->dskattr.out.blocks_per_unit = bpunit;
125                 fs->dskattr.out.block_size = block_size;
126                 fs->dskattr.out.units_total = (blocks_total * (double)block_size) / (bpunit * 512);
127                 fs->dskattr.out.units_free  = (blocks_free  * (double)block_size) / (bpunit * 512);
128
129                 /* we must return a maximum of 2G to old DOS systems, or they get very confused */
130                 if (bpunit > 64 && req->ctx->protocol <= PROTOCOL_LANMAN2) {
131                         fs->dskattr.out.blocks_per_unit = 64;
132                         fs->dskattr.out.units_total = 0xFFFF;
133                         fs->dskattr.out.units_free = 0xFFFF;
134                 }
135                 return NT_STATUS_OK;
136
137         case RAW_QFS_ALLOCATION:
138                 fs->allocation.out.fs_id = st.st_dev;
139                 fs->allocation.out.total_alloc_units = blocks_total;
140                 fs->allocation.out.avail_alloc_units = blocks_free;
141                 fs->allocation.out.sectors_per_unit = 1;
142                 fs->allocation.out.bytes_per_sector = block_size;
143                 return NT_STATUS_OK;
144
145         case RAW_QFS_VOLUME:
146                 fs->volume.out.serial_number = st.st_ino;
147                 fs->volume.out.volume_name.s = pvfs->share_name;
148                 return NT_STATUS_OK;
149
150         case RAW_QFS_VOLUME_INFO:
151         case RAW_QFS_VOLUME_INFORMATION:
152                 unix_to_nt_time(&fs->volume_info.out.create_time, st.st_ctime);
153                 fs->volume_info.out.serial_number = st.st_ino;
154                 fs->volume_info.out.volume_name.s = pvfs->share_name;
155                 return NT_STATUS_OK;
156
157         case RAW_QFS_SIZE_INFO:
158         case RAW_QFS_SIZE_INFORMATION:
159                 fs->size_info.out.total_alloc_units = blocks_total;
160                 fs->size_info.out.avail_alloc_units = blocks_free;
161                 fs->size_info.out.sectors_per_unit = 1;
162                 fs->size_info.out.bytes_per_sector = block_size;
163                 return NT_STATUS_OK;
164
165         case RAW_QFS_DEVICE_INFO:
166         case RAW_QFS_DEVICE_INFORMATION:
167                 fs->device_info.out.device_type = 0;
168                 fs->device_info.out.characteristics = 0;
169                 return NT_STATUS_OK;
170
171         case RAW_QFS_ATTRIBUTE_INFO:
172         case RAW_QFS_ATTRIBUTE_INFORMATION:
173                 fs->attribute_info.out.fs_attr                   = pvfs->fs_attribs;
174                 fs->attribute_info.out.max_file_component_length = 255;
175                 fs->attribute_info.out.fs_type.s                 = ntvfs->ctx->fs_type;
176                 return NT_STATUS_OK;
177
178         case RAW_QFS_QUOTA_INFORMATION:
179                 ZERO_STRUCT(fs->quota_information.out.unknown);
180                 fs->quota_information.out.quota_soft = 0;
181                 fs->quota_information.out.quota_hard = 0;
182                 fs->quota_information.out.quota_flags = 0;
183                 return NT_STATUS_OK;
184
185         case RAW_QFS_FULL_SIZE_INFORMATION:
186                 fs->full_size_information.out.total_alloc_units = blocks_total;
187                 fs->full_size_information.out.call_avail_alloc_units = blocks_free;
188                 fs->full_size_information.out.actual_avail_alloc_units = blocks_free;
189                 fs->full_size_information.out.sectors_per_unit = 1;
190                 fs->full_size_information.out.bytes_per_sector = block_size;
191                 return NT_STATUS_OK;
192
193         case RAW_QFS_OBJECTID_INFORMATION:
194                 ZERO_STRUCT(fs->objectid_information.out.guid);
195                 ZERO_STRUCT(fs->objectid_information.out.unknown);
196
197                 status = pvfs_cache_base_fs_uuid(pvfs, &st);
198                 NT_STATUS_NOT_OK_RETURN(status);
199
200                 fs->objectid_information.out.guid = *pvfs->base_fs_uuid;
201                 return NT_STATUS_OK;
202
203         default:
204                 break;
205         }
206         return NT_STATUS_INVALID_LEVEL;
207 }