522b94399df4219bcd683bd8ffd8a1a6684616f3
[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                                    PARM_ATIME_NOW_DEFAULT);
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                                    PARM_CTIME_NOW_DEFAULT);
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                                    PARM_MTIME_NOW_DEFAULT);
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                                    PARM_ATIME_STATIC_DEFAULT);
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                                    PARM_MTIME_STATIC_DEFAULT);
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,
79                                       PARM_ATIME_SLOP_DEFAULT);
80         cfg->ctime_slop = lp_parm_int(snum, PARM_ONEFS_TYPE, PARM_CTIME_SLOP,
81                                       PARM_CTIME_SLOP_DEFAULT);
82         cfg->mtime_slop = lp_parm_int(snum, PARM_ONEFS_TYPE, PARM_MTIME_SLOP,
83                                       PARM_MTIME_SLOP_DEFAULT);
84 }
85
86
87 static int onefs_load_config(struct vfs_handle_struct *handle)
88 {
89         int snum = SNUM(handle->conn);
90         int share_count = lp_numservices();
91
92         if (!pshare_config) {
93
94                 if (share_count <= ONEFS_DATA_FASTBUF)
95                         pshare_config = share_config;
96                 else {
97                         pshare_config =
98                             SMB_MALLOC_ARRAY(struct onefs_vfs_config,
99                                              share_count);
100                         if (!pshare_config) {
101                                 errno = ENOMEM;
102                                 return -1;
103                         }
104
105                         memset(pshare_config, 0,
106                             (sizeof(struct onefs_vfs_config) * share_count));
107                 }
108         }
109
110         if ((pshare_config[snum].init_flags &
111                 ONEFS_VFS_CONFIG_INITIALIZED) == 0) {
112                         pshare_config[snum].init_flags =
113                             ONEFS_VFS_CONFIG_INITIALIZED;
114                         onefs_load_faketimestamp_config(handle,
115                                                         &pshare_config[snum]);
116         }
117
118         return 0;
119 }
120
121 bool onefs_get_config(int snum, int config_type,
122                       struct onefs_vfs_config *cfg)
123 {
124         if (share_config[snum].init_flags & config_type)
125                 *cfg = share_config[snum];
126         else
127                 return false;
128
129         return true;
130 }
131
132 static int onefs_connect(struct vfs_handle_struct *handle, const char *service,
133                          const char *user)
134 {
135         int ret = onefs_load_config(handle);
136
137         if (ret)
138                 return ret;
139
140         return SMB_VFS_NEXT_CONNECT(handle, service, user);
141 }
142
143 static int onefs_mkdir(vfs_handle_struct *handle, const char *path,
144                        mode_t mode)
145 {
146         /* SMB_VFS_MKDIR should never be called in vfs_onefs */
147         SMB_ASSERT(false);
148         return SMB_VFS_NEXT_MKDIR(handle, path, mode);
149 }
150
151 static int onefs_open(vfs_handle_struct *handle, const char *fname,
152                       files_struct *fsp, int flags, mode_t mode)
153 {
154         /* SMB_VFS_OPEN should never be called in vfs_onefs */
155         SMB_ASSERT(false);
156         return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
157 }
158
159 static ssize_t onefs_recvfile(vfs_handle_struct *handle, int fromfd,
160                               files_struct *tofsp, SMB_OFF_T offset,
161                               size_t count)
162 {
163         ssize_t result;
164
165         START_PROFILE_BYTES(syscall_recvfile, count);
166         result = onefs_sys_recvfile(fromfd, tofsp->fh->fd, offset, count);
167         END_PROFILE(syscall_recvfile);
168         return result;
169 }
170
171 static uint64_t onefs_get_alloc_size(struct vfs_handle_struct *handle,
172                                      files_struct *fsp,
173                                      const SMB_STRUCT_STAT *sbuf)
174 {
175         uint64_t result;
176
177         START_PROFILE(syscall_get_alloc_size);
178
179         if(S_ISDIR(sbuf->st_mode)) {
180                 result = 0;
181                 goto out;
182         }
183
184         /* Just use the file size since st_blocks is unreliable on OneFS. */
185         result = get_file_size_stat(sbuf);
186
187         if (fsp && fsp->initial_allocation_size)
188                 result = MAX(result,fsp->initial_allocation_size);
189
190         result = smb_roundup(handle->conn, result);
191
192  out:
193         END_PROFILE(syscall_get_alloc_size);
194         return result;
195 }
196
197 static struct file_id onefs_file_id_create(struct vfs_handle_struct *handle,
198                                              SMB_STRUCT_STAT *sbuf)
199 {
200         struct file_id key;
201
202         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
203          * blob */
204         ZERO_STRUCT(key);
205
206         key.devid = sbuf->st_dev;
207         key.inode = sbuf->st_ino;
208         key.extid = sbuf->st_snapid;
209
210         return key;
211 }
212
213 static int onefs_statvfs(vfs_handle_struct *handle, const char *path,
214                          vfs_statvfs_struct *statbuf)
215 {
216         struct statvfs statvfs_buf;
217         int result;
218
219         DEBUG(5, ("Calling SMB_STAT_VFS \n"));
220         result = statvfs(path, &statvfs_buf);
221         ZERO_STRUCTP(statbuf);
222
223         if (!result) {
224                 statbuf->OptimalTransferSize = statvfs_buf.f_iosize;
225                 statbuf->BlockSize = statvfs_buf.f_bsize;
226                 statbuf->TotalBlocks = statvfs_buf.f_blocks;
227                 statbuf->BlocksAvail = statvfs_buf.f_bfree;
228                 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
229                 statbuf->TotalFileNodes = statvfs_buf.f_files;
230                 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
231                 statbuf->FsIdentifier =
232                     (((uint64_t)statvfs_buf.f_fsid.val[0]<<32) &
233                         0xffffffff00000000LL) |
234                     (uint64_t)statvfs_buf.f_fsid.val[1];
235         }
236         return result;
237 }
238
239 static int onefs_get_real_filename(vfs_handle_struct *handle, const char *path,
240                                    const char *name, TALLOC_CTX *mem_ctx,
241                                    char **found_name)
242 {
243         SMB_STRUCT_STAT sb;
244         struct stat_extra se;
245         int result;
246         char *full_name = NULL;
247
248         ZERO_STRUCT(se);
249         se.se_version = ESTAT_CURRENT_VERSION;
250         se.se_flags = ESTAT_CASE_INSENSITIVE | ESTAT_SYMLINK_NOFOLLOW;
251
252         if (*path != '\0') {
253                 if (!(full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name))) {
254                         errno = ENOMEM;
255                         DEBUG(2, ("talloc_asprintf failed\n"));
256                         result = -1;
257                         goto done;
258                 }
259         }
260
261         if ((result = estat(full_name ? full_name : name, &sb, &se)) != 0) {
262                 DEBUG(2, ("error calling estat: %s\n", strerror(errno)));
263                 goto done;
264         }
265
266         *found_name = talloc_strdup(mem_ctx, se.se_realname);
267         if (*found_name == NULL) {
268                 errno = ENOMEM;
269                 result = -1;
270                 goto done;
271         }
272
273 done:
274         TALLOC_FREE(full_name);
275         return result;
276 }
277
278 static int onefs_ntimes(vfs_handle_struct *handle, const char *fname,
279                         struct smb_file_time *ft)
280 {
281         int flags = 0;
282         struct timespec times[3];
283
284         if (!null_timespec(ft->atime)) {
285                 flags |= VT_ATIME;
286                 times[0] = ft->atime;
287                 DEBUG(6,("**** onefs_ntimes: actime: %s.%d\n",
288                         time_to_asc(convert_timespec_to_time_t(ft->atime)),
289                         ft->atime.tv_nsec));
290         }
291
292         if (!null_timespec(ft->mtime)) {
293                 flags |= VT_MTIME;
294                 times[1] = ft->mtime;
295                 DEBUG(6,("**** onefs_ntimes: modtime: %s.%d\n",
296                         time_to_asc(convert_timespec_to_time_t(ft->mtime)),
297                         ft->mtime.tv_nsec));
298         }
299
300         if (!null_timespec(ft->create_time)) {
301                 flags |= VT_BTIME;
302                 times[2] = ft->create_time;
303                 DEBUG(6,("**** onefs_ntimes: createtime: %s.%d\n",
304                    time_to_asc(convert_timespec_to_time_t(ft->create_time)),
305                    ft->create_time.tv_nsec));
306         }
307
308         return onefs_vtimes_streams(handle, fname, flags, times);
309 }
310
311 static uint32_t onefs_fs_capabilities(struct vfs_handle_struct *handle)
312 {
313         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
314 }
315
316 static vfs_op_tuple onefs_ops[] = {
317         {SMB_VFS_OP(onefs_connect), SMB_VFS_OP_CONNECT,
318          SMB_VFS_LAYER_TRANSPARENT},
319         {SMB_VFS_OP(onefs_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
320          SMB_VFS_LAYER_TRANSPARENT},
321         {SMB_VFS_OP(onefs_opendir), SMB_VFS_OP_OPENDIR,
322          SMB_VFS_LAYER_TRANSPARENT},
323         {SMB_VFS_OP(onefs_readdir), SMB_VFS_OP_READDIR,
324          SMB_VFS_LAYER_OPAQUE},
325         {SMB_VFS_OP(onefs_seekdir), SMB_VFS_OP_SEEKDIR,
326          SMB_VFS_LAYER_OPAQUE},
327         {SMB_VFS_OP(onefs_telldir), SMB_VFS_OP_TELLDIR,
328          SMB_VFS_LAYER_OPAQUE},
329         {SMB_VFS_OP(onefs_rewinddir), SMB_VFS_OP_REWINDDIR,
330          SMB_VFS_LAYER_OPAQUE},
331         {SMB_VFS_OP(onefs_mkdir), SMB_VFS_OP_MKDIR,
332          SMB_VFS_LAYER_OPAQUE},
333         {SMB_VFS_OP(onefs_closedir), SMB_VFS_OP_CLOSEDIR,
334          SMB_VFS_LAYER_TRANSPARENT},
335         {SMB_VFS_OP(onefs_init_search_op), SMB_VFS_OP_INIT_SEARCH_OP,
336          SMB_VFS_LAYER_OPAQUE},
337         {SMB_VFS_OP(onefs_open), SMB_VFS_OP_OPEN,
338          SMB_VFS_LAYER_OPAQUE},
339         {SMB_VFS_OP(onefs_create_file), SMB_VFS_OP_CREATE_FILE,
340          SMB_VFS_LAYER_OPAQUE},
341         {SMB_VFS_OP(onefs_close), SMB_VFS_OP_CLOSE,
342          SMB_VFS_LAYER_TRANSPARENT},
343         {SMB_VFS_OP(onefs_recvfile), SMB_VFS_OP_RECVFILE,
344          SMB_VFS_LAYER_OPAQUE},
345         {SMB_VFS_OP(onefs_rename), SMB_VFS_OP_RENAME,
346          SMB_VFS_LAYER_TRANSPARENT},
347         {SMB_VFS_OP(onefs_stat), SMB_VFS_OP_STAT,
348          SMB_VFS_LAYER_TRANSPARENT},
349         {SMB_VFS_OP(onefs_fstat), SMB_VFS_OP_FSTAT,
350          SMB_VFS_LAYER_TRANSPARENT},
351         {SMB_VFS_OP(onefs_lstat), SMB_VFS_OP_LSTAT,
352          SMB_VFS_LAYER_TRANSPARENT},
353         {SMB_VFS_OP(onefs_get_alloc_size), SMB_VFS_OP_GET_ALLOC_SIZE,
354          SMB_VFS_LAYER_OPAQUE},
355         {SMB_VFS_OP(onefs_unlink), SMB_VFS_OP_UNLINK,
356          SMB_VFS_LAYER_TRANSPARENT},
357         {SMB_VFS_OP(onefs_ntimes), SMB_VFS_OP_NTIMES,
358          SMB_VFS_LAYER_OPAQUE},
359         {SMB_VFS_OP(onefs_chflags), SMB_VFS_OP_CHFLAGS,
360          SMB_VFS_LAYER_TRANSPARENT},
361         {SMB_VFS_OP(onefs_file_id_create), SMB_VFS_OP_FILE_ID_CREATE,
362          SMB_VFS_LAYER_OPAQUE},
363         {SMB_VFS_OP(onefs_streaminfo), SMB_VFS_OP_STREAMINFO,
364          SMB_VFS_LAYER_OPAQUE},
365         {SMB_VFS_OP(onefs_brl_lock_windows), SMB_VFS_OP_BRL_LOCK_WINDOWS,
366          SMB_VFS_LAYER_OPAQUE},
367         {SMB_VFS_OP(onefs_brl_unlock_windows), SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
368          SMB_VFS_LAYER_OPAQUE},
369         {SMB_VFS_OP(onefs_brl_cancel_windows), SMB_VFS_OP_BRL_CANCEL_WINDOWS,
370          SMB_VFS_LAYER_OPAQUE},
371         {SMB_VFS_OP(onefs_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
372          SMB_VFS_LAYER_OPAQUE},
373         {SMB_VFS_OP(onefs_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
374          SMB_VFS_LAYER_OPAQUE},
375         {SMB_VFS_OP(onefs_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
376          SMB_VFS_LAYER_OPAQUE},
377         {SMB_VFS_OP(onefs_statvfs), SMB_VFS_OP_STATVFS,
378          SMB_VFS_LAYER_OPAQUE},
379         {SMB_VFS_OP(onefs_get_real_filename), SMB_VFS_OP_GET_REAL_FILENAME,
380          SMB_VFS_LAYER_OPAQUE},
381         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
382 };
383
384 NTSTATUS vfs_onefs_init(void)
385 {
386         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "onefs",
387                                 onefs_ops);
388 }