smbd: Remove unused [push_pull]_file_id_24
[samba.git] / source3 / modules / vfs_tsmsm.c
1 /*
2   Unix SMB/CIFS implementation.
3   Samba VFS module for handling offline files
4   with Tivoli Storage Manager Space Management
5
6   (c) Alexander Bokovoy, 2007, 2008
7   (c) Andrew Tridgell, 2007, 2008
8   
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 /*
23   This VFS module accepts following options:
24   tsmsm: hsm script = <path to hsm script> (default does nothing)
25          hsm script should point to a shell script which accepts two arguments:
26          <operation> <filepath>
27          where <operation> is currently 'offline' to set offline status of the <filepath>
28
29   tsmsm: online ratio = ratio to check reported size against actual file size (0.5 by default)
30   tsmsm: attribute name = name of DMAPI attribute that is present when a file is offline. 
31   Default is "IBMobj" (which is what GPFS uses)
32
33   The TSMSM VFS module tries to avoid calling expensive DMAPI calls with some heuristics
34   based on the fact that number of blocks reported of a file multiplied by 512 will be
35   bigger than 'online ratio' of actual size for online (non-migrated) files.
36
37   If checks fail, we call DMAPI and ask for specific attribute which present for
38   offline (migrated) files. If this attribute presents, we consider file offline.
39  */
40
41 #include "includes.h"
42
43 #ifndef USE_DMAPI
44 #error "This module requires DMAPI support!"
45 #endif
46
47 #ifdef HAVE_XFS_DMAPI_H
48 #include <xfs/dmapi.h>
49 #elif defined(HAVE_SYS_DMI_H)
50 #include <sys/dmi.h>
51 #elif defined(HAVE_SYS_JFSDMAPI_H)
52 #include <sys/jfsdmapi.h>
53 #elif defined(HAVE_SYS_DMAPI_H)
54 #include <sys/dmapi.h>
55 #elif defined(HAVE_DMAPI_H)
56 #include <dmapi.h>
57 #endif
58
59 #ifndef _ISOC99_SOURCE
60 #define _ISOC99_SOURCE 
61 #endif
62
63 #include <math.h> 
64
65 /* optimisation tunables - used to avoid the DMAPI slow path */
66 #define FILE_IS_ONLINE_RATIO      0.5
67
68 /* default attribute name to look for */
69 #define DM_ATTRIB_OBJECT "IBMObj"
70
71 struct tsmsm_struct {
72         float online_ratio;
73         char *hsmscript;
74         const char *attrib_name;
75         const char *attrib_value;
76 };
77
78 static void tsmsm_free_data(void **pptr) {
79         struct tsmsm_struct **tsmd = (struct tsmsm_struct **)pptr;
80         if(!tsmd) return;
81         TALLOC_FREE(*tsmd);
82 }
83
84 /* 
85    called when a client connects to a share
86 */
87 static int tsmsm_connect(struct vfs_handle_struct *handle,
88                          const char *service,
89                          const char *user) {
90         struct tsmsm_struct *tsmd;
91         const char *fres;
92         const char *tsmname;
93         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
94
95         if (ret < 0) {
96                 return ret;
97         }
98
99         tsmd = TALLOC_ZERO_P(handle, struct tsmsm_struct);
100         if (!tsmd) {
101                 SMB_VFS_NEXT_DISCONNECT(handle);
102                 DEBUG(0,("tsmsm_connect: out of memory!\n"));
103                 return -1;
104         }
105
106         if (!dmapi_have_session()) {
107                 SMB_VFS_NEXT_DISCONNECT(handle);
108                 DEBUG(0,("tsmsm_connect: no DMAPI session for Samba is available!\n"));
109                 TALLOC_FREE(tsmd);
110                 return -1;
111         }
112
113         tsmname = (handle->param ? handle->param : "tsmsm");
114         
115         /* Get 'hsm script' and 'dmapi attribute' parameters to tsmd context */
116         tsmd->hsmscript = lp_parm_talloc_string(SNUM(handle->conn), tsmname,
117                                                 "hsm script", NULL);
118         talloc_steal(tsmd, tsmd->hsmscript);
119         
120         tsmd->attrib_name = lp_parm_talloc_string(SNUM(handle->conn), tsmname, 
121                                                   "dmapi attribute", DM_ATTRIB_OBJECT);
122         talloc_steal(tsmd, tsmd->attrib_name);
123         
124         tsmd->attrib_value = lp_parm_talloc_string(SNUM(handle->conn), "tsmsm", 
125                                                    "dmapi value", NULL);
126         talloc_steal(tsmd, tsmd->attrib_value);
127         
128         /* retrieve 'online ratio'. In case of error default to FILE_IS_ONLINE_RATIO */
129         fres = lp_parm_const_string(SNUM(handle->conn), tsmname, 
130                                     "online ratio", NULL);
131         if (fres == NULL) {
132                 tsmd->online_ratio = FILE_IS_ONLINE_RATIO;
133         } else {
134                 tsmd->online_ratio = strtof(fres, NULL);
135                 if (tsmd->online_ratio > 1.0 ||
136                     tsmd->online_ratio <= 0.0) {
137                         DEBUG(1, ("tsmsm_connect: invalid online ration %f - using %f.\n",
138                                   tsmd->online_ratio, (float)FILE_IS_ONLINE_RATIO));
139                 }
140         }
141
142         /* Store the private data. */
143         SMB_VFS_HANDLE_SET_DATA(handle, tsmd, tsmsm_free_data,
144                                 struct tsmsm_struct, return -1);
145         return 0;
146 }
147
148 static bool tsmsm_is_offline(struct vfs_handle_struct *handle, 
149                              const struct smb_filename *fname,
150                              SMB_STRUCT_STAT *stbuf)
151 {
152         struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
153         const dm_sessid_t *dmsession_id;
154         void *dmhandle = NULL;
155         size_t dmhandle_len = 0;
156         size_t rlen;
157         dm_attrname_t dmname;
158         int ret, lerrno;
159         bool offline;
160         char *buf = NULL;
161         size_t buflen;
162         NTSTATUS status;
163         char *path;
164
165         status = get_full_smb_filename(talloc_tos(), fname, &path);
166         if (!NT_STATUS_IS_OK(status)) {
167                 errno = map_errno_from_nt_status(status);
168                 return false;
169         }
170
171         /* if the file has more than FILE_IS_ONLINE_RATIO of blocks available,
172            then assume it is not offline (it may not be 100%, as it could be sparse) */
173         if (512 * stbuf->st_ex_blocks >=
174             stbuf->st_ex_size * tsmd->online_ratio) {
175                 DEBUG(10,("%s not offline: st_blocks=%llu st_size=%llu "
176                           "online_ratio=%.2f\n", path,
177                           (unsigned long long)stbuf->st_ex_blocks,
178                           (unsigned long long)stbuf->st_ex_size, tsmd->online_ratio));
179                 return false;
180         }
181
182         dmsession_id = dmapi_get_current_session();
183         if (dmsession_id == NULL) {
184                 DEBUG(2, ("tsmsm_is_offline: no DMAPI session available? "
185                           "Assume file is online.\n"));
186                 return false;
187         }
188
189         /* using POSIX capabilities does not work here. It's a slow path, so 
190          * become_root() is just as good anyway (tridge) 
191          */
192
193         /* Also, AIX has DMAPI but no POSIX capablities support. In this case,
194          * we need to be root to do DMAPI manipulations.
195          */
196         become_root();
197
198         /* go the slow DMAPI route */
199         if (dm_path_to_handle((char*)path, &dmhandle, &dmhandle_len) != 0) {
200                 DEBUG(2,("dm_path_to_handle failed - assuming offline (%s) - %s\n", 
201                          path, strerror(errno)));
202                 offline = true;
203                 goto done;
204         }
205
206         memset(&dmname, 0, sizeof(dmname));
207         strlcpy((char *)&dmname.an_chars[0], tsmd->attrib_name, sizeof(dmname.an_chars));
208
209         if (tsmd->attrib_value != NULL) {
210                 buflen = strlen(tsmd->attrib_value);
211         } else {
212                 buflen = 1;
213         }
214         buf = talloc_zero_size(tsmd, buflen);
215         if (buf == NULL) {
216                 DEBUG(0,("out of memory in tsmsm_is_offline -- assuming online (%s)\n", path));
217                 errno = ENOMEM;
218                 offline = false;
219                 goto done;
220         }
221
222         do {
223                 lerrno = 0;
224
225                 ret = dm_get_dmattr(*dmsession_id, dmhandle, dmhandle_len, 
226                                     DM_NO_TOKEN, &dmname, buflen, buf, &rlen);
227                 if (ret == -1 && errno == EINVAL) {
228                         DEBUG(0, ("Stale DMAPI session, re-creating it.\n"));
229                         lerrno = EINVAL;
230                         if (dmapi_new_session()) {
231                                 dmsession_id = dmapi_get_current_session();
232                         } else {
233                                 DEBUG(0, 
234                                       ("Unable to re-create DMAPI session, assuming offline (%s) - %s\n", 
235                                        path, strerror(errno)));
236                                 offline = true;
237                                 dm_handle_free(dmhandle, dmhandle_len);
238                                 goto done;
239                         }
240                 }
241         } while (ret == -1 && lerrno == EINVAL);
242
243         /* check if we need a specific attribute value */
244         if (tsmd->attrib_value != NULL) {
245                 offline = (ret == 0 && rlen == buflen && 
246                             memcmp(buf, tsmd->attrib_value, buflen) == 0);
247         } else {
248                 /* its offline if the specified DMAPI attribute exists */
249                 offline = (ret == 0 || (ret == -1 && errno == E2BIG));
250         }
251
252         DEBUG(10,("dm_get_dmattr %s ret=%d (%s)\n", path, ret, strerror(errno)));
253
254         ret = 0;
255
256         dm_handle_free(dmhandle, dmhandle_len); 
257
258 done:
259         talloc_free(buf);
260         unbecome_root();
261         return offline;
262 }
263
264
265 static bool tsmsm_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
266 {
267         SMB_STRUCT_STAT sbuf;
268         struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
269         /* see if the file might be offline. This is called before each IO
270            to ensure we use AIO if the file is offline. We don't do the full dmapi
271            call as that would be too slow, instead we err on the side of using AIO
272            if the file might be offline
273         */
274         if(SMB_VFS_FSTAT(fsp, &sbuf) == 0) {
275                 DEBUG(10,("tsmsm_aio_force st_blocks=%llu st_size=%llu "
276                           "online_ratio=%.2f\n", (unsigned long long)sbuf.st_ex_blocks,
277                           (unsigned long long)sbuf.st_ex_size, tsmd->online_ratio));
278                 return !(512 * sbuf.st_ex_blocks >=
279                          sbuf.st_ex_size * tsmd->online_ratio);
280         }
281         return false;
282 }
283
284 static ssize_t tsmsm_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp, 
285                                 SMB_STRUCT_AIOCB *aiocb)
286 {
287         ssize_t result;
288
289         result = SMB_VFS_NEXT_AIO_RETURN(handle, fsp, aiocb);
290         if(result >= 0) {
291                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
292                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
293                              fsp->fsp_name->base_name);
294         }
295
296         return result;
297 }
298
299 static ssize_t tsmsm_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fsp, const DATA_BLOB *hdr,
300                               SMB_OFF_T offset, size_t n)
301 {
302         bool file_offline = tsmsm_aio_force(handle, fsp);
303
304         if (file_offline) {
305                 DEBUG(10,("tsmsm_sendfile on offline file - rejecting\n"));
306                 errno = ENOSYS;
307                 return -1;
308         }
309             
310         return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
311 }
312
313 /* We do overload pread to allow notification when file becomes online after offline status */
314 /* We don't intercept SMB_VFS_READ here because all file I/O now goes through SMB_VFS_PREAD instead */
315 static ssize_t tsmsm_pread(struct vfs_handle_struct *handle, struct files_struct *fsp, 
316                            void *data, size_t n, SMB_OFF_T offset) {
317         ssize_t result;
318         bool notify_online = tsmsm_aio_force(handle, fsp);
319
320         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
321         if((result != -1) && notify_online) {
322             /* We can't actually force AIO at this point (came here not from reply_read_and_X) 
323                what we can do is to send notification that file became online
324             */
325                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
326                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
327                              fsp->fsp_name->base_name);
328         }
329
330         return result;
331 }
332
333 static ssize_t tsmsm_pwrite(struct vfs_handle_struct *handle, struct files_struct *fsp, 
334                             const void *data, size_t n, SMB_OFF_T offset) {
335         ssize_t result;
336         bool notify_online = tsmsm_aio_force(handle, fsp);
337
338         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
339         if((result != -1) && notify_online) {
340             /* We can't actually force AIO at this point (came here not from reply_read_and_X) 
341                what we can do is to send notification that file became online
342             */
343                 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
344                              FILE_NOTIFY_CHANGE_ATTRIBUTES,
345                              fsp->fsp_name->base_name);
346         }
347
348         return result;
349 }
350
351 static int tsmsm_set_offline(struct vfs_handle_struct *handle, 
352                              const struct smb_filename *fname)
353 {
354         struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
355         int result = 0;
356         char *command;
357         NTSTATUS status;
358         char *path;
359
360         if (tsmd->hsmscript == NULL) {
361                 /* no script enabled */
362                 DEBUG(1, ("tsmsm_set_offline: No 'tsmsm:hsm script' configured\n"));
363                 return 0;
364         }
365
366         status = get_full_smb_filename(talloc_tos(), fname, &path);
367         if (!NT_STATUS_IS_OK(status)) {
368                 errno = map_errno_from_nt_status(status);
369                 return false;
370         }
371
372         /* Now, call the script */
373         command = talloc_asprintf(tsmd, "%s offline \"%s\"", tsmd->hsmscript, path);
374         if(!command) {
375                 DEBUG(1, ("tsmsm_set_offline: can't allocate memory to run hsm script"));
376                 return -1;
377         }
378         DEBUG(10, ("tsmsm_set_offline: Running [%s]\n", command));
379         if((result = smbrun(command, NULL)) != 0) {
380                 DEBUG(1,("tsmsm_set_offline: Running [%s] returned %d\n", command, result));
381         }
382         TALLOC_FREE(command);
383         return result;
384 }
385
386 static uint32_t tsmsm_fs_capabilities(struct vfs_handle_struct *handle,
387                         enum timestamp_set_resolution *p_ts_res)
388 {
389         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_SUPPORTS_REMOTE_STORAGE | FILE_SUPPORTS_REPARSE_POINTS;
390 }
391
392 static struct vfs_fn_pointers tsmsm_fns = {
393         .connect_fn = tsmsm_connect,
394         .fs_capabilities = tsmsm_fs_capabilities,
395         .aio_force = tsmsm_aio_force,
396         .aio_return_fn = tsmsm_aio_return,
397         .pread = tsmsm_pread,
398         .pwrite = tsmsm_pwrite,
399         .sendfile = tsmsm_sendfile,
400         .is_offline = tsmsm_is_offline,
401         .set_offline = tsmsm_set_offline,
402 };
403
404 NTSTATUS vfs_tsmsm_init(void);
405 NTSTATUS vfs_tsmsm_init(void)
406 {
407         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
408                                 "tsmsm", &tsmsm_fns);
409 }