Merge branch 'master' of git://git.samba.org/samba into convenience
[amitay/samba.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 #include "onefs_config.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_VFS
27
28 static int onefs_connect(struct vfs_handle_struct *handle, const char *service,
29                          const char *user)
30 {
31         int ret;
32
33         ret = onefs_load_config(handle->conn);
34         if (ret) {
35                 DEBUG(3, ("Load config failed: %s\n", strerror(errno)));
36                 return ret;
37         }
38
39         return SMB_VFS_NEXT_CONNECT(handle, service, user);
40 }
41
42 static int onefs_mkdir(vfs_handle_struct *handle, const char *path,
43                        mode_t mode)
44 {
45         /* SMB_VFS_MKDIR should never be called in vfs_onefs */
46         SMB_ASSERT(false);
47         return SMB_VFS_NEXT_MKDIR(handle, path, mode);
48 }
49
50 static int onefs_open(vfs_handle_struct *handle, const char *fname,
51                       files_struct *fsp, int flags, mode_t mode)
52 {
53         /* SMB_VFS_OPEN should never be called in vfs_onefs */
54         SMB_ASSERT(false);
55         return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
56 }
57
58 static ssize_t onefs_sendfile(vfs_handle_struct *handle, int tofd,
59                               files_struct *fromfsp, const DATA_BLOB *header,
60                               SMB_OFF_T offset, size_t count)
61 {
62         ssize_t result;
63
64         START_PROFILE_BYTES(syscall_sendfile, count);
65         result = onefs_sys_sendfile(handle->conn, tofd, fromfsp->fh->fd,
66                                     header, offset, count);
67         END_PROFILE(syscall_sendfile);
68         return result;
69 }
70
71 static ssize_t onefs_recvfile(vfs_handle_struct *handle, int fromfd,
72                               files_struct *tofsp, SMB_OFF_T offset,
73                               size_t count)
74 {
75         ssize_t result;
76
77         START_PROFILE_BYTES(syscall_recvfile, count);
78         result = onefs_sys_recvfile(fromfd, tofsp->fh->fd, offset, count);
79         END_PROFILE(syscall_recvfile);
80         return result;
81 }
82
83 static uint64_t onefs_get_alloc_size(struct vfs_handle_struct *handle,
84                                      files_struct *fsp,
85                                      const SMB_STRUCT_STAT *sbuf)
86 {
87         uint64_t result;
88
89         START_PROFILE(syscall_get_alloc_size);
90
91         if(S_ISDIR(sbuf->st_mode)) {
92                 result = 0;
93                 goto out;
94         }
95
96         /* Just use the file size since st_blocks is unreliable on OneFS. */
97         result = get_file_size_stat(sbuf);
98
99         if (fsp && fsp->initial_allocation_size)
100                 result = MAX(result,fsp->initial_allocation_size);
101
102         result = smb_roundup(handle->conn, result);
103
104  out:
105         END_PROFILE(syscall_get_alloc_size);
106         return result;
107 }
108
109 static struct file_id onefs_file_id_create(struct vfs_handle_struct *handle,
110                                              SMB_STRUCT_STAT *sbuf)
111 {
112         struct file_id key;
113
114         /* the ZERO_STRUCT ensures padding doesn't break using the key as a
115          * blob */
116         ZERO_STRUCT(key);
117
118         key.devid = sbuf->st_dev;
119         key.inode = sbuf->st_ino;
120         key.extid = sbuf->st_snapid;
121
122         return key;
123 }
124
125 static int onefs_statvfs(vfs_handle_struct *handle, const char *path,
126                          vfs_statvfs_struct *statbuf)
127 {
128         struct statvfs statvfs_buf;
129         int result;
130
131         DEBUG(5, ("Calling SMB_STAT_VFS \n"));
132         result = statvfs(path, &statvfs_buf);
133         ZERO_STRUCTP(statbuf);
134
135         if (!result) {
136                 statbuf->OptimalTransferSize = statvfs_buf.f_iosize;
137                 statbuf->BlockSize = statvfs_buf.f_bsize;
138                 statbuf->TotalBlocks = statvfs_buf.f_blocks;
139                 statbuf->BlocksAvail = statvfs_buf.f_bfree;
140                 statbuf->UserBlocksAvail = statvfs_buf.f_bavail;
141                 statbuf->TotalFileNodes = statvfs_buf.f_files;
142                 statbuf->FreeFileNodes = statvfs_buf.f_ffree;
143                 statbuf->FsIdentifier =
144                     (((uint64_t)statvfs_buf.f_fsid.val[0]<<32) &
145                         0xffffffff00000000LL) |
146                     (uint64_t)statvfs_buf.f_fsid.val[1];
147         }
148         return result;
149 }
150
151 static int onefs_get_real_filename(vfs_handle_struct *handle, const char *path,
152                                    const char *name, TALLOC_CTX *mem_ctx,
153                                    char **found_name)
154 {
155         SMB_STRUCT_STAT sb;
156         struct stat_extra se;
157         int result;
158         char *full_name = NULL;
159
160         ZERO_STRUCT(se);
161         se.se_version = ESTAT_CURRENT_VERSION;
162         se.se_flags = ESTAT_CASE_INSENSITIVE | ESTAT_SYMLINK_NOFOLLOW;
163
164         if (*path != '\0') {
165                 if (!(full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name))) {
166                         errno = ENOMEM;
167                         DEBUG(2, ("talloc_asprintf failed\n"));
168                         result = -1;
169                         goto done;
170                 }
171         }
172
173         if ((result = estat(full_name ? full_name : name, &sb, &se)) != 0) {
174                 DEBUG(2, ("error calling estat: %s\n", strerror(errno)));
175                 goto done;
176         }
177
178         *found_name = talloc_strdup(mem_ctx, se.se_realname);
179         if (*found_name == NULL) {
180                 errno = ENOMEM;
181                 result = -1;
182                 goto done;
183         }
184
185 done:
186         TALLOC_FREE(full_name);
187         return result;
188 }
189
190 static int onefs_ntimes(vfs_handle_struct *handle, const char *fname,
191                         struct smb_file_time *ft)
192 {
193         int flags = 0;
194         struct timespec times[3];
195
196         if (!null_timespec(ft->atime)) {
197                 flags |= VT_ATIME;
198                 times[0] = ft->atime;
199                 DEBUG(6,("**** onefs_ntimes: actime: %s.%d\n",
200                         time_to_asc(convert_timespec_to_time_t(ft->atime)),
201                         ft->atime.tv_nsec));
202         }
203
204         if (!null_timespec(ft->mtime)) {
205                 flags |= VT_MTIME;
206                 times[1] = ft->mtime;
207                 DEBUG(6,("**** onefs_ntimes: modtime: %s.%d\n",
208                         time_to_asc(convert_timespec_to_time_t(ft->mtime)),
209                         ft->mtime.tv_nsec));
210         }
211
212         if (!null_timespec(ft->create_time)) {
213                 flags |= VT_BTIME;
214                 times[2] = ft->create_time;
215                 DEBUG(6,("**** onefs_ntimes: createtime: %s.%d\n",
216                    time_to_asc(convert_timespec_to_time_t(ft->create_time)),
217                    ft->create_time.tv_nsec));
218         }
219
220         return onefs_vtimes_streams(handle, fname, flags, times);
221 }
222
223 static uint32_t onefs_fs_capabilities(struct vfs_handle_struct *handle)
224 {
225         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
226 }
227
228 static vfs_op_tuple onefs_ops[] = {
229         {SMB_VFS_OP(onefs_connect), SMB_VFS_OP_CONNECT,
230          SMB_VFS_LAYER_TRANSPARENT},
231         {SMB_VFS_OP(onefs_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
232          SMB_VFS_LAYER_TRANSPARENT},
233         {SMB_VFS_OP(onefs_opendir), SMB_VFS_OP_OPENDIR,
234          SMB_VFS_LAYER_TRANSPARENT},
235         {SMB_VFS_OP(onefs_readdir), SMB_VFS_OP_READDIR,
236          SMB_VFS_LAYER_OPAQUE},
237         {SMB_VFS_OP(onefs_seekdir), SMB_VFS_OP_SEEKDIR,
238          SMB_VFS_LAYER_OPAQUE},
239         {SMB_VFS_OP(onefs_telldir), SMB_VFS_OP_TELLDIR,
240          SMB_VFS_LAYER_OPAQUE},
241         {SMB_VFS_OP(onefs_rewinddir), SMB_VFS_OP_REWINDDIR,
242          SMB_VFS_LAYER_OPAQUE},
243         {SMB_VFS_OP(onefs_mkdir), SMB_VFS_OP_MKDIR,
244          SMB_VFS_LAYER_OPAQUE},
245         {SMB_VFS_OP(onefs_closedir), SMB_VFS_OP_CLOSEDIR,
246          SMB_VFS_LAYER_TRANSPARENT},
247         {SMB_VFS_OP(onefs_init_search_op), SMB_VFS_OP_INIT_SEARCH_OP,
248          SMB_VFS_LAYER_OPAQUE},
249         {SMB_VFS_OP(onefs_open), SMB_VFS_OP_OPEN,
250          SMB_VFS_LAYER_OPAQUE},
251         {SMB_VFS_OP(onefs_create_file), SMB_VFS_OP_CREATE_FILE,
252          SMB_VFS_LAYER_OPAQUE},
253         {SMB_VFS_OP(onefs_close), SMB_VFS_OP_CLOSE,
254          SMB_VFS_LAYER_TRANSPARENT},
255         {SMB_VFS_OP(onefs_sendfile), SMB_VFS_OP_SENDFILE,
256          SMB_VFS_LAYER_OPAQUE},
257         {SMB_VFS_OP(onefs_recvfile), SMB_VFS_OP_RECVFILE,
258          SMB_VFS_LAYER_OPAQUE},
259         {SMB_VFS_OP(onefs_rename), SMB_VFS_OP_RENAME,
260          SMB_VFS_LAYER_TRANSPARENT},
261         {SMB_VFS_OP(onefs_stat), SMB_VFS_OP_STAT,
262          SMB_VFS_LAYER_TRANSPARENT},
263         {SMB_VFS_OP(onefs_fstat), SMB_VFS_OP_FSTAT,
264          SMB_VFS_LAYER_TRANSPARENT},
265         {SMB_VFS_OP(onefs_lstat), SMB_VFS_OP_LSTAT,
266          SMB_VFS_LAYER_TRANSPARENT},
267         {SMB_VFS_OP(onefs_get_alloc_size), SMB_VFS_OP_GET_ALLOC_SIZE,
268          SMB_VFS_LAYER_OPAQUE},
269         {SMB_VFS_OP(onefs_unlink), SMB_VFS_OP_UNLINK,
270          SMB_VFS_LAYER_TRANSPARENT},
271         {SMB_VFS_OP(onefs_ntimes), SMB_VFS_OP_NTIMES,
272          SMB_VFS_LAYER_OPAQUE},
273         {SMB_VFS_OP(onefs_chflags), SMB_VFS_OP_CHFLAGS,
274          SMB_VFS_LAYER_TRANSPARENT},
275         {SMB_VFS_OP(onefs_file_id_create), SMB_VFS_OP_FILE_ID_CREATE,
276          SMB_VFS_LAYER_OPAQUE},
277         {SMB_VFS_OP(onefs_streaminfo), SMB_VFS_OP_STREAMINFO,
278          SMB_VFS_LAYER_OPAQUE},
279         {SMB_VFS_OP(onefs_brl_lock_windows), SMB_VFS_OP_BRL_LOCK_WINDOWS,
280          SMB_VFS_LAYER_OPAQUE},
281         {SMB_VFS_OP(onefs_brl_unlock_windows), SMB_VFS_OP_BRL_UNLOCK_WINDOWS,
282          SMB_VFS_LAYER_OPAQUE},
283         {SMB_VFS_OP(onefs_brl_cancel_windows), SMB_VFS_OP_BRL_CANCEL_WINDOWS,
284          SMB_VFS_LAYER_OPAQUE},
285         {SMB_VFS_OP(onefs_notify_watch), SMB_VFS_OP_NOTIFY_WATCH,
286          SMB_VFS_LAYER_OPAQUE},
287         {SMB_VFS_OP(onefs_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
288          SMB_VFS_LAYER_OPAQUE},
289         {SMB_VFS_OP(onefs_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
290          SMB_VFS_LAYER_OPAQUE},
291         {SMB_VFS_OP(onefs_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
292          SMB_VFS_LAYER_OPAQUE},
293         {SMB_VFS_OP(onefs_statvfs), SMB_VFS_OP_STATVFS,
294          SMB_VFS_LAYER_OPAQUE},
295         {SMB_VFS_OP(onefs_get_real_filename), SMB_VFS_OP_GET_REAL_FILENAME,
296          SMB_VFS_LAYER_OPAQUE},
297         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
298 };
299
300 NTSTATUS vfs_onefs_init(void)
301 {
302         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "onefs",
303                                 onefs_ops);
304 }