Use a single copy of popt in samba3 and samba4.
[Samba/fernandojvsilva.git] / source3 / modules / vfs_tsmsm.c
blobee958b1be5192725bcfde58da8726bc1a446b0ce
1 /*
2 Unix SMB/CIFS implementation.
3 Samba VFS module for handling offline files
4 with Tivoli Storage Manager Space Management
6 (c) Alexander Bokovoy, 2007, 2008
7 (c) Andrew Tridgell, 2007, 2008
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.
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.
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/>.
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>
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)
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.
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.
41 #include "includes.h"
43 #ifndef USE_DMAPI
44 #error "This module requires DMAPI support!"
45 #endif
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
59 #ifndef _ISOC99_SOURCE
60 #define _ISOC99_SOURCE
61 #endif
63 #include <math.h>
65 /* optimisation tunables - used to avoid the DMAPI slow path */
66 #define FILE_IS_ONLINE_RATIO 0.5
68 /* default attribute name to look for */
69 #define DM_ATTRIB_OBJECT "IBMObj"
71 struct tsmsm_struct {
72 float online_ratio;
73 char *hsmscript;
74 const char *attrib_name;
75 const char *attrib_value;
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);
84 /*
85 called when a client connects to a share
87 static int tsmsm_connect(struct vfs_handle_struct *handle,
88 const char *service,
89 const char *user) {
90 struct tsmsm_struct *tsmd = TALLOC_ZERO_P(handle, struct tsmsm_struct);
91 const char *fres;
92 const char *tsmname;
94 if (!tsmd) {
95 DEBUG(0,("tsmsm_connect: out of memory!\n"));
96 return -1;
99 if (!dmapi_have_session()) {
100 DEBUG(0,("tsmsm_connect: no DMAPI session for Samba is available!\n"));
101 TALLOC_FREE(tsmd);
102 return -1;
105 tsmname = (handle->param ? handle->param : "tsmsm");
107 /* Get 'hsm script' and 'dmapi attribute' parameters to tsmd context */
108 tsmd->hsmscript = lp_parm_talloc_string(SNUM(handle->conn), tsmname,
109 "hsm script", NULL);
110 talloc_steal(tsmd, tsmd->hsmscript);
112 tsmd->attrib_name = lp_parm_talloc_string(SNUM(handle->conn), tsmname,
113 "dmapi attribute", DM_ATTRIB_OBJECT);
114 talloc_steal(tsmd, tsmd->attrib_name);
116 tsmd->attrib_value = lp_parm_talloc_string(SNUM(handle->conn), "tsmsm",
117 "dmapi value", NULL);
118 talloc_steal(tsmd, tsmd->attrib_value);
120 /* retrieve 'online ratio'. In case of error default to FILE_IS_ONLINE_RATIO */
121 fres = lp_parm_const_string(SNUM(handle->conn), tsmname,
122 "online ratio", NULL);
123 if (fres == NULL) {
124 tsmd->online_ratio = FILE_IS_ONLINE_RATIO;
125 } else {
126 tsmd->online_ratio = strtof(fres, NULL);
127 if (tsmd->online_ratio > 1.0 ||
128 tsmd->online_ratio <= 0.0) {
129 DEBUG(1, ("tsmsm_connect: invalid online ration %f - using %f.\n",
130 tsmd->online_ratio, (float)FILE_IS_ONLINE_RATIO));
134 /* Store the private data. */
135 SMB_VFS_HANDLE_SET_DATA(handle, tsmd, tsmsm_free_data,
136 struct tsmsm_struct, return -1);
137 return SMB_VFS_NEXT_CONNECT(handle, service, user);
140 static bool tsmsm_is_offline(struct vfs_handle_struct *handle,
141 const char *path,
142 SMB_STRUCT_STAT *stbuf) {
143 struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
144 const dm_sessid_t *dmsession_id;
145 void *dmhandle = NULL;
146 size_t dmhandle_len = 0;
147 size_t rlen;
148 dm_attrname_t dmname;
149 int ret, lerrno;
150 bool offline;
151 char *buf;
152 size_t buflen;
154 /* if the file has more than FILE_IS_ONLINE_RATIO of blocks available,
155 then assume it is not offline (it may not be 100%, as it could be sparse) */
156 if (512 * (off_t)stbuf->st_blocks >= stbuf->st_size * tsmd->online_ratio) {
157 DEBUG(10,("%s not offline: st_blocks=%ld st_size=%ld "
158 "online_ratio=%.2f\n", path, (long)stbuf->st_blocks,
159 (long)stbuf->st_size, tsmd->online_ratio));
160 return false;
163 dmsession_id = dmapi_get_current_session();
164 if (dmsession_id == NULL) {
165 DEBUG(2, ("tsmsm_is_offline: no DMAPI session available? "
166 "Assume file is online.\n"));
167 return false;
170 /* using POSIX capabilities does not work here. It's a slow path, so
171 * become_root() is just as good anyway (tridge)
174 /* Also, AIX has DMAPI but no POSIX capablities support. In this case,
175 * we need to be root to do DMAPI manipulations.
177 become_root();
179 /* go the slow DMAPI route */
180 if (dm_path_to_handle((char*)path, &dmhandle, &dmhandle_len) != 0) {
181 DEBUG(2,("dm_path_to_handle failed - assuming offline (%s) - %s\n",
182 path, strerror(errno)));
183 offline = true;
184 goto done;
187 memset(&dmname, 0, sizeof(dmname));
188 strlcpy((char *)&dmname.an_chars[0], tsmd->attrib_name, sizeof(dmname.an_chars));
190 if (tsmd->attrib_value != NULL) {
191 buflen = strlen(tsmd->attrib_value);
192 } else {
193 buflen = 1;
195 buf = talloc_zero_size(tsmd, buflen);
196 if (buf == NULL) {
197 DEBUG(0,("out of memory in tsmsm_is_offline -- assuming online (%s)\n", path));
198 errno = ENOMEM;
199 offline = false;
200 goto done;
203 do {
204 lerrno = 0;
206 ret = dm_get_dmattr(*dmsession_id, dmhandle, dmhandle_len,
207 DM_NO_TOKEN, &dmname, buflen, buf, &rlen);
208 if (ret == -1 && errno == EINVAL) {
209 DEBUG(0, ("Stale DMAPI session, re-creating it.\n"));
210 lerrno = EINVAL;
211 if (dmapi_new_session()) {
212 dmsession_id = dmapi_get_current_session();
213 } else {
214 DEBUG(0,
215 ("Unable to re-create DMAPI session, assuming offline (%s) - %s\n",
216 path, strerror(errno)));
217 offline = true;
218 dm_handle_free(dmhandle, dmhandle_len);
219 goto done;
222 } while (ret == -1 && lerrno == EINVAL);
224 /* check if we need a specific attribute value */
225 if (tsmd->attrib_value != NULL) {
226 offline = (ret == 0 && rlen == buflen &&
227 memcmp(buf, tsmd->attrib_value, buflen) == 0);
228 } else {
229 /* its offline if the specified DMAPI attribute exists */
230 offline = (ret == 0 || (ret == -1 && errno == E2BIG));
233 DEBUG(10,("dm_get_dmattr %s ret=%d (%s)\n", path, ret, strerror(errno)));
235 ret = 0;
237 dm_handle_free(dmhandle, dmhandle_len);
239 done:
240 talloc_free(buf);
241 unbecome_root();
242 return offline;
246 static bool tsmsm_aio_force(struct vfs_handle_struct *handle, struct files_struct *fsp)
248 SMB_STRUCT_STAT sbuf;
249 struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
250 /* see if the file might be offline. This is called before each IO
251 to ensure we use AIO if the file is offline. We don't do the full dmapi
252 call as that would be too slow, instead we err on the side of using AIO
253 if the file might be offline
255 if(SMB_VFS_FSTAT(fsp, &sbuf) == 0) {
256 DEBUG(10,("tsmsm_aio_force st_blocks=%ld st_size=%ld "
257 "online_ratio=%.2f\n", (long)sbuf.st_blocks,
258 (long)sbuf.st_size, tsmd->online_ratio));
259 return !(512 * (off_t)sbuf.st_blocks >= sbuf.st_size * tsmd->online_ratio);
261 return false;
264 static ssize_t tsmsm_aio_return(struct vfs_handle_struct *handle, struct files_struct *fsp,
265 SMB_STRUCT_AIOCB *aiocb)
267 ssize_t result;
269 result = SMB_VFS_NEXT_AIO_RETURN(handle, fsp, aiocb);
270 if(result >= 0) {
271 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
272 FILE_NOTIFY_CHANGE_ATTRIBUTES,
273 fsp->fsp_name);
276 return result;
279 static ssize_t tsmsm_sendfile(vfs_handle_struct *handle, int tofd, files_struct *fsp, const DATA_BLOB *hdr,
280 SMB_OFF_T offset, size_t n)
282 bool file_offline = tsmsm_aio_force(handle, fsp);
284 if (file_offline) {
285 DEBUG(10,("tsmsm_sendfile on offline file - rejecting\n"));
286 errno = ENOSYS;
287 return -1;
290 return SMB_VFS_NEXT_SENDFILE(handle, tofd, fsp, hdr, offset, n);
293 /* We do overload pread to allow notification when file becomes online after offline status */
294 /* We don't intercept SMB_VFS_READ here because all file I/O now goes through SMB_VFS_PREAD instead */
295 static ssize_t tsmsm_pread(struct vfs_handle_struct *handle, struct files_struct *fsp,
296 void *data, size_t n, SMB_OFF_T offset) {
297 ssize_t result;
298 bool notify_online = tsmsm_aio_force(handle, fsp);
300 result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
301 if((result != -1) && notify_online) {
302 /* We can't actually force AIO at this point (came here not from reply_read_and_X)
303 what we can do is to send notification that file became online
305 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
306 FILE_NOTIFY_CHANGE_ATTRIBUTES,
307 fsp->fsp_name);
310 return result;
313 static ssize_t tsmsm_pwrite(struct vfs_handle_struct *handle, struct files_struct *fsp,
314 void *data, size_t n, SMB_OFF_T offset) {
315 ssize_t result;
316 bool notify_online = tsmsm_aio_force(handle, fsp);
318 result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
319 if((result != -1) && notify_online) {
320 /* We can't actually force AIO at this point (came here not from reply_read_and_X)
321 what we can do is to send notification that file became online
323 notify_fname(handle->conn, NOTIFY_ACTION_MODIFIED,
324 FILE_NOTIFY_CHANGE_ATTRIBUTES,
325 fsp->fsp_name);
328 return result;
331 static int tsmsm_set_offline(struct vfs_handle_struct *handle,
332 const char *path) {
333 struct tsmsm_struct *tsmd = (struct tsmsm_struct *) handle->data;
334 int result = 0;
335 char *command;
337 if (tsmd->hsmscript == NULL) {
338 /* no script enabled */
339 DEBUG(1, ("tsmsm_set_offline: No 'tsmsm:hsm script' configured\n"));
340 return 0;
343 /* Now, call the script */
344 command = talloc_asprintf(tsmd, "%s offline \"%s\"", tsmd->hsmscript, path);
345 if(!command) {
346 DEBUG(1, ("tsmsm_set_offline: can't allocate memory to run hsm script"));
347 return -1;
349 DEBUG(10, ("tsmsm_set_offline: Running [%s]\n", command));
350 if((result = smbrun(command, NULL)) != 0) {
351 DEBUG(1,("tsmsm_set_offline: Running [%s] returned %d\n", command, result));
353 TALLOC_FREE(command);
354 return result;
357 static uint32_t tsmsm_fs_capabilities(struct vfs_handle_struct *handle)
359 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_SUPPORTS_REMOTE_STORAGE | FILE_SUPPORTS_REPARSE_POINTS;
362 static vfs_op_tuple vfs_tsmsm_ops[] = {
364 /* Disk operations */
366 {SMB_VFS_OP(tsmsm_connect), SMB_VFS_OP_CONNECT,
367 SMB_VFS_LAYER_TRANSPARENT},
368 {SMB_VFS_OP(tsmsm_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
369 SMB_VFS_LAYER_TRANSPARENT},
370 {SMB_VFS_OP(tsmsm_aio_force), SMB_VFS_OP_AIO_FORCE,
371 SMB_VFS_LAYER_TRANSPARENT},
372 {SMB_VFS_OP(tsmsm_aio_return), SMB_VFS_OP_AIO_RETURN,
373 SMB_VFS_LAYER_TRANSPARENT},
374 {SMB_VFS_OP(tsmsm_pread), SMB_VFS_OP_PREAD,
375 SMB_VFS_LAYER_TRANSPARENT},
376 {SMB_VFS_OP(tsmsm_pwrite), SMB_VFS_OP_PWRITE,
377 SMB_VFS_LAYER_TRANSPARENT},
378 {SMB_VFS_OP(tsmsm_sendfile), SMB_VFS_OP_SENDFILE,
379 SMB_VFS_LAYER_TRANSPARENT},
380 {SMB_VFS_OP(tsmsm_is_offline), SMB_VFS_OP_IS_OFFLINE,
381 SMB_VFS_LAYER_OPAQUE},
382 {SMB_VFS_OP(tsmsm_set_offline), SMB_VFS_OP_SET_OFFLINE,
383 SMB_VFS_LAYER_OPAQUE},
385 /* Finish VFS operations definition */
387 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP,
388 SMB_VFS_LAYER_NOOP}
391 NTSTATUS vfs_tsmsm_init(void);
392 NTSTATUS vfs_tsmsm_init(void)
394 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
395 "tsmsm", vfs_tsmsm_ops);