6046ecb6f8060b65ecea7929dd2f91651201d6a4
[samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "librpc/gen_ndr/ndr_xattr.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 /*
33   return filesystem space info
34 */
35 NTSTATUS pvfs_fsinfo(struct ntvfs_module_context *ntvfs,
36                      struct smbsrv_request *req, union smb_fsinfo *fs)
37 {
38         struct pvfs_state *pvfs = ntvfs->private_data;
39         uint64_t blocks_free, blocks_total;
40         uint_t bpunit;
41         struct stat st;
42         const uint16_t block_size = 512;
43
44         /* only some levels need the expensive sys_fsusage() call */
45         switch (fs->generic.level) {
46         case RAW_QFS_DSKATTR:
47         case RAW_QFS_ALLOCATION:
48         case RAW_QFS_SIZE_INFO:
49         case RAW_QFS_SIZE_INFORMATION:
50         case RAW_QFS_FULL_SIZE_INFORMATION:
51                 if (sys_fsusage(pvfs->base_directory, &blocks_free, &blocks_total) == -1) {
52                         return pvfs_map_errno(pvfs, errno);
53                 }
54         default:
55                 break;
56         }
57
58         if (stat(pvfs->base_directory, &st) != 0) {
59                 return NT_STATUS_DISK_CORRUPT_ERROR;
60         }
61
62         /* now fill in the out fields */
63         switch (fs->generic.level) {
64         case RAW_QFS_GENERIC:
65                 return NT_STATUS_INVALID_LEVEL;
66
67         case RAW_QFS_DSKATTR:
68                 /* we need to scale the sizes to fit */
69                 for (bpunit=64; bpunit<0x10000; bpunit *= 2) {
70                         if (blocks_total * (double)block_size < bpunit * 512 * 65535.0) {
71                                 break;
72                         }
73                 }
74                 fs->dskattr.out.blocks_per_unit = bpunit;
75                 fs->dskattr.out.block_size = block_size;
76                 fs->dskattr.out.units_total = (blocks_total * (double)block_size) / (bpunit * 512);
77                 fs->dskattr.out.units_free  = (blocks_free  * (double)block_size) / (bpunit * 512);
78
79                 /* we must return a maximum of 2G to old DOS systems, or they get very confused */
80                 if (bpunit > 64 && req->smb_conn->negotiate.protocol <= PROTOCOL_LANMAN2) {
81                         fs->dskattr.out.blocks_per_unit = 64;
82                         fs->dskattr.out.units_total = 0xFFFF;
83                         fs->dskattr.out.units_free = 0xFFFF;
84                 }
85                 return NT_STATUS_OK;
86
87         case RAW_QFS_ALLOCATION:
88                 fs->allocation.out.fs_id = st.st_dev;
89                 fs->allocation.out.total_alloc_units = blocks_total;
90                 fs->allocation.out.avail_alloc_units = blocks_free;
91                 fs->allocation.out.sectors_per_unit = 1;
92                 fs->allocation.out.bytes_per_sector = block_size;
93                 return NT_STATUS_OK;
94
95         case RAW_QFS_VOLUME:
96                 fs->volume.out.serial_number = st.st_ino;
97                 fs->volume.out.volume_name.s = pvfs->share_name;
98                 return NT_STATUS_OK;
99
100         case RAW_QFS_VOLUME_INFO:
101         case RAW_QFS_VOLUME_INFORMATION:
102                 unix_to_nt_time(&fs->volume_info.out.create_time, st.st_ctime);
103                 fs->volume_info.out.serial_number = st.st_ino;
104                 fs->volume_info.out.volume_name.s = pvfs->share_name;
105                 return NT_STATUS_OK;
106
107         case RAW_QFS_SIZE_INFO:
108         case RAW_QFS_SIZE_INFORMATION:
109                 fs->size_info.out.total_alloc_units = blocks_total;
110                 fs->size_info.out.avail_alloc_units = blocks_free;
111                 fs->size_info.out.sectors_per_unit = 1;
112                 fs->size_info.out.bytes_per_sector = block_size;
113                 return NT_STATUS_OK;
114
115         case RAW_QFS_DEVICE_INFO:
116         case RAW_QFS_DEVICE_INFORMATION:
117                 fs->device_info.out.device_type = 0;
118                 fs->device_info.out.characteristics = 0;
119                 return NT_STATUS_OK;
120
121         case RAW_QFS_ATTRIBUTE_INFO:
122         case RAW_QFS_ATTRIBUTE_INFORMATION:
123                 fs->attribute_info.out.fs_attr                   = pvfs->fs_attribs;
124                 fs->attribute_info.out.max_file_component_length = 255;
125                 fs->attribute_info.out.fs_type.s                 = req->tcon->fs_type;
126                 return NT_STATUS_OK;
127
128         case RAW_QFS_QUOTA_INFORMATION:
129                 ZERO_STRUCT(fs->quota_information.out.unknown);
130                 fs->quota_information.out.quota_soft = 0;
131                 fs->quota_information.out.quota_hard = 0;
132                 fs->quota_information.out.quota_flags = 0;
133                 return NT_STATUS_OK;
134
135         case RAW_QFS_FULL_SIZE_INFORMATION:
136                 fs->full_size_information.out.total_alloc_units = blocks_total;
137                 fs->full_size_information.out.call_avail_alloc_units = blocks_free;
138                 fs->full_size_information.out.actual_avail_alloc_units = blocks_free;
139                 fs->full_size_information.out.sectors_per_unit = 1;
140                 fs->full_size_information.out.bytes_per_sector = block_size;
141                 return NT_STATUS_OK;
142
143         case RAW_QFS_OBJECTID_INFORMATION:
144                 {
145 #ifdef HAVE_LIBBLKID
146                         NTSTATUS status;
147                         blkid_cache blk_cache = NULL;
148                         const char *uuid_value;
149                         const char *blkid_devname;
150                         
151 #endif
152                         ZERO_STRUCT(fs->objectid_information.out);
153 #ifdef HAVE_LIBBLKID
154                         blkid_devname = blkid_devno_to_devname(st.st_dev);
155                         if (!blk_cache && blkid_get_cache(&blk_cache,NULL) < 0 ) {
156                           return NT_STATUS_DEVICE_CONFIGURATION_ERROR;
157                         }
158                         if ((uuid_value = blkid_get_tag_value(blk_cache, "UUID", blkid_devname))) {
159                           GUID_from_string(uuid_value, &fs->objectid_information.out.guid);
160                           free(uuid_value);
161                         }                       
162 #endif
163                         return NT_STATUS_OK;
164                 }
165         }
166
167         return NT_STATUS_INVALID_LEVEL;
168 }