s3: OneFS bulk directory enumeration support
[ira/wip.git] / source3 / modules / vfs_onefs.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS
4  *
5  * Copyright (C) Tim Prouty, 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "onefs.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_VFS
26
27 #define ONEFS_DATA_FASTBUF      10
28
29 struct onefs_vfs_config share_config[ONEFS_DATA_FASTBUF];
30 struct onefs_vfs_config *pshare_config;
31
32 static void onefs_load_faketimestamp_config(struct vfs_handle_struct *handle,
33                                             struct onefs_vfs_config *cfg)
34 {
35         const char **parm;
36         int snum = SNUM(handle->conn);
37
38         parm = lp_parm_string_list(snum, PARM_ONEFS_TYPE, PARM_ATIME_NOW,
39                                    NULL);
40
41         if (parm) {
42                 cfg->init_flags |= ONEFS_VFS_CONFIG_FAKETIMESTAMPS;
43                 set_namearray(&cfg->atime_now_list,*parm);
44         }
45
46         parm = lp_parm_string_list(snum, PARM_ONEFS_TYPE, PARM_CTIME_NOW,
47                                    NULL);
48
49         if (parm) {
50                 cfg->init_flags |= ONEFS_VFS_CONFIG_FAKETIMESTAMPS;
51                 set_namearray(&cfg->ctime_now_list,*parm);
52         }
53
54         parm = lp_parm_string_list(snum, PARM_ONEFS_TYPE, PARM_MTIME_NOW,
55                                    NULL);
56
57         if (parm) {
58                 cfg->init_flags |= ONEFS_VFS_CONFIG_FAKETIMESTAMPS;
59                 set_namearray(&cfg->mtime_now_list,*parm);
60         }
61
62         parm = lp_parm_string_list(snum, PARM_ONEFS_TYPE, PARM_ATIME_STATIC,
63                                    NULL);
64
65         if (parm) {
66                 cfg->init_flags |= ONEFS_VFS_CONFIG_FAKETIMESTAMPS;
67                 set_namearray(&cfg->atime_static_list,*parm);
68         }
69
70         parm = lp_parm_string_list(snum, PARM_ONEFS_TYPE, PARM_MTIME_STATIC,
71                                    NULL);
72
73         if (parm) {
74                 cfg->init_flags |= ONEFS_VFS_CONFIG_FAKETIMESTAMPS;
75                 set_namearray(&cfg->mtime_static_list,*parm);
76         }
77
78         cfg->atime_slop = lp_parm_int(snum, PARM_ONEFS_TYPE, PARM_ATIME_SLOP,0);
79         cfg->ctime_slop = lp_parm_int(snum, PARM_ONEFS_TYPE, PARM_CTIME_SLOP,0);
80         cfg->mtime_slop = lp_parm_int(snum, PARM_ONEFS_TYPE, PARM_MTIME_SLOP,0);
81 }
82
83
84 static int onefs_load_config(struct vfs_handle_struct *handle)
85 {
86         int snum = SNUM(handle->conn);
87         int share_count = lp_numservices();
88
89         if (!pshare_config) {
90
91                 if (share_count <= ONEFS_DATA_FASTBUF)
92                         pshare_config = share_config;
93                 else {
94                         pshare_config =
95                             SMB_MALLOC_ARRAY(struct onefs_vfs_config,
96                                              share_count);
97                         if (!pshare_config) {
98                                 errno = ENOMEM;
99                                 return -1;
100                         }
101
102                         memset(pshare_config, 0,
103                             (sizeof(struct onefs_vfs_config) * share_count));
104                 }
105         }
106
107         if ((pshare_config[snum].init_flags &
108                 ONEFS_VFS_CONFIG_INITIALIZED) == 0) {
109                         pshare_config[snum].init_flags =
110                             ONEFS_VFS_CONFIG_INITIALIZED;
111                         onefs_load_faketimestamp_config(handle,
112                                                         &pshare_config[snum]);
113         }
114
115         return 0;
116 }
117
118 bool onefs_get_config(int snum, int config_type,
119                       struct onefs_vfs_config *cfg)
120 {
121         if (share_config[snum].init_flags & config_type)
122                 *cfg = share_config[snum];
123         else
124                 return false;
125
126         return true;
127 }
128
129 static int onefs_connect(struct vfs_handle_struct *handle, const char *service,
130                          const char *user)
131 {
132         int ret = onefs_load_config(handle);
133
134         if (ret)
135                 return ret;
136
137         return SMB_VFS_NEXT_CONNECT(handle, service, user);
138 }
139
140 static int onefs_mkdir(vfs_handle_struct *handle, const char *path,
141                        mode_t mode)
142 {
143         /* SMB_VFS_MKDIR should never be called in vfs_onefs */
144         SMB_ASSERT(false);
145         return SMB_VFS_NEXT_MKDIR(handle, path, mode);
146 }
147
148 static int onefs_open(vfs_handle_struct *handle, const char *fname,
149                       files_struct *fsp, int flags, mode_t mode)
150 {
151         /* SMB_VFS_OPEN should never be called in vfs_onefs */
152         SMB_ASSERT(false);
153         return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
154 }
155
156 static uint64_t onefs_get_alloc_size(struct vfs_handle_struct *handle,
157                                      files_struct *fsp,
158                                      const SMB_STRUCT_STAT *sbuf)
159 {
160         uint64_t result;
161
162         START_PROFILE(syscall_get_alloc_size);
163
164         if(S_ISDIR(sbuf->st_mode)) {
165                 result = 0;
166                 goto out;
167         }
168
169         /* Just use the file size since st_blocks is unreliable on OneFS. */
170         result = get_file_size_stat(sbuf);
171
172         if (fsp && fsp->initial_allocation_size)
173                 result = MAX(result,fsp->initial_allocation_size);
174
175         result = smb_roundup(handle->conn, result);
176
177  out:
178         END_PROFILE(syscall_get_alloc_size);
179         return result;
180 }
181
182 static int onefs_statvfs(vfs_handle_struct *handle, const char *path,
183                          vfs_statvfs_struct *statbuf)
184 {
185         struct statvfs statvfs_buf;
186         int result;
187
188         DEBUG(5, ("Calling SMB_STAT_VFS \n"));
189         result = statvfs(path, &statvfs_buf);
190         ZERO_STRUCTP(statbuf);
191
192         if (!result) {
193                 statbuf->OptimalTransferSize = statvfs_buf.f_iosize;
194                 statbuf->BlockSize = statvfs_buf.f_bsize;
195                 statbuf->TotalBlocks = statvfs_buf.f_blocks;
196                 statbuf->BlocksAvail = statvfs_buf.f_bfree;
197                 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
198                 statbuf->TotalFileNodes = statvfs_buf.f_files;
199                 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
200                 statbuf->FsIdentifier =
201                     (((uint64_t)statvfs_buf.f_fsid.val[0]<<32) &
202                         0xffffffff00000000LL) |
203                     (uint64_t)statvfs_buf.f_fsid.val[1];
204         }
205         return result;
206 }
207
208 static int onefs_ntimes(vfs_handle_struct *handle, const char *fname,
209                         struct smb_file_time *ft)
210 {
211         int flags = 0;
212         struct timespec times[3];
213
214         if (!null_timespec(ft->atime)) {
215                 flags |= VT_ATIME;
216                 times[0] = ft->atime;
217                 DEBUG(6,("**** onefs_ntimes: actime: %s.%d\n",
218                         time_to_asc(convert_timespec_to_time_t(ft->atime)),
219                         ft->atime.tv_nsec));
220         }
221
222         if (!null_timespec(ft->mtime)) {
223                 flags |= VT_MTIME;
224                 times[1] = ft->mtime;
225                 DEBUG(6,("**** onefs_ntimes: modtime: %s.%d\n",
226                         time_to_asc(convert_timespec_to_time_t(ft->mtime)),
227                         ft->mtime.tv_nsec));
228         }
229
230         if (!null_timespec(ft->create_time)) {
231                 flags |= VT_BTIME;
232                 times[2] = ft->create_time;
233                 DEBUG(6,("**** onefs_ntimes: createtime: %s.%d\n",
234                    time_to_asc(convert_timespec_to_time_t(ft->create_time)),
235                    ft->create_time.tv_nsec));
236         }
237
238         return onefs_vtimes_streams(handle, fname, flags, times);
239 }
240
241 static uint32_t onefs_fs_capabilities(struct vfs_handle_struct *handle)
242 {
243         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
244 }
245
246 static vfs_op_tuple onefs_ops[] = {
247         {SMB_VFS_OP(onefs_connect), SMB_VFS_OP_CONNECT,
248          SMB_VFS_LAYER_TRANSPARENT},
249         {SMB_VFS_OP(onefs_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
250          SMB_VFS_LAYER_TRANSPARENT},
251         {SMB_VFS_OP(onefs_opendir), SMB_VFS_OP_OPENDIR,
252          SMB_VFS_LAYER_TRANSPARENT},
253         {SMB_VFS_OP(onefs_readdir), SMB_VFS_OP_READDIR,
254          SMB_VFS_LAYER_OPAQUE},
255         {SMB_VFS_OP(onefs_seekdir), SMB_VFS_OP_SEEKDIR,
256          SMB_VFS_LAYER_OPAQUE},
257         {SMB_VFS_OP(onefs_telldir), SMB_VFS_OP_TELLDIR,
258          SMB_VFS_LAYER_OPAQUE},
259         {SMB_VFS_OP(onefs_rewinddir), SMB_VFS_OP_REWINDDIR,
260          SMB_VFS_LAYER_OPAQUE},
261         {SMB_VFS_OP(onefs_mkdir), SMB_VFS_OP_MKDIR,
262          SMB_VFS_LAYER_OPAQUE},
263         {SMB_VFS_OP(onefs_closedir), SMB_VFS_OP_CLOSEDIR,
264          SMB_VFS_LAYER_TRANSPARENT},
265         {SMB_VFS_OP(onefs_init_search_op), SMB_VFS_OP_INIT_SEARCH_OP,
266          SMB_VFS_LAYER_OPAQUE},
267         {SMB_VFS_OP(onefs_open), SMB_VFS_OP_OPEN,
268          SMB_VFS_LAYER_OPAQUE},
269         {SMB_VFS_OP(onefs_create_file), SMB_VFS_OP_CREATE_FILE,
270          SMB_VFS_LAYER_OPAQUE},
271         {SMB_VFS_OP(onefs_close), SMB_VFS_OP_CLOSE,
272          SMB_VFS_LAYER_TRANSPARENT},
273         {SMB_VFS_OP(onefs_rename), SMB_VFS_OP_RENAME,
274          SMB_VFS_LAYER_TRANSPARENT},
275         {SMB_VFS_OP(onefs_stat), SMB_VFS_OP_STAT,
276          SMB_VFS_LAYER_TRANSPARENT},
277         {SMB_VFS_OP(onefs_fstat), SMB_VFS_OP_FSTAT,
278          SMB_VFS_LAYER_TRANSPARENT},
279         {SMB_VFS_OP(onefs_lstat), SMB_VFS_OP_LSTAT,
280          SMB_VFS_LAYER_TRANSPARENT},
281         {SMB_VFS_OP(onefs_get_alloc_size), SMB_VFS_OP_GET_ALLOC_SIZE,
282          SMB_VFS_LAYER_OPAQUE},
283         {SMB_VFS_OP(onefs_unlink), SMB_VFS_OP_UNLINK,
284          SMB_VFS_LAYER_TRANSPARENT},
285         {SMB_VFS_OP(onefs_ntimes), SMB_VFS_OP_NTIMES,
286          SMB_VFS_LAYER_OPAQUE},
287         {SMB_VFS_OP(onefs_chflags), SMB_VFS_OP_CHFLAGS,
288          SMB_VFS_LAYER_TRANSPARENT},
289         {SMB_VFS_OP(onefs_streaminfo), SMB_VFS_OP_STREAMINFO,
290          SMB_VFS_LAYER_OPAQUE},
291         {SMB_VFS_OP(onefs_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
292          SMB_VFS_LAYER_OPAQUE},
293         {SMB_VFS_OP(onefs_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
294          SMB_VFS_LAYER_OPAQUE},
295         {SMB_VFS_OP(onefs_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
296          SMB_VFS_LAYER_OPAQUE},
297         {SMB_VFS_OP(onefs_statvfs), SMB_VFS_OP_STATVFS,
298          SMB_VFS_LAYER_OPAQUE},
299         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
300 };
301
302 NTSTATUS vfs_onefs_init(void)
303 {
304         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "onefs",
305                                 onefs_ops);
306 }