2 Unix SMB/CIFS implementation.
5 Copyright (C) James Peach 2006
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.
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.
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/>.
24 #define DBGC_CLASS DBGC_DMAPI
28 uint32 dmapi_file_flags(const char * const path) { return 0; }
29 bool dmapi_have_session(void) { return False; }
30 const void * dmapi_get_current_session(void) { return NULL; }
34 #ifdef HAVE_XFS_DMAPI_H
35 #include <xfs/dmapi.h>
36 #elif defined(HAVE_SYS_DMI_H)
38 #elif defined(HAVE_SYS_JFSDMAPI_H)
39 #include <sys/jfsdmapi.h>
40 #elif defined(HAVE_SYS_DMAPI_H)
41 #include <sys/dmapi.h>
42 #elif defined(HAVE_DMAPI_H)
46 #define DMAPI_SESSION_NAME "samba"
47 #define DMAPI_TRACE 10
49 struct smbd_dmapi_context {
54 static struct smbd_dmapi_context *dmapi_ctx;
57 Initialise DMAPI session. The session is persistant kernel state,
58 so it might already exist, in which case we merely want to
59 reconnect to it. This function should be called as root.
61 static int dmapi_init_session(struct smbd_dmapi_context *ctx)
63 char buf[DM_SESSION_INFO_LEN];
66 dm_sessid_t *sessions = NULL;
69 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
73 if (ctx->session_num == 0) {
74 session_name = talloc_strdup(tmp_ctx, DMAPI_SESSION_NAME);
76 session_name = talloc_asprintf(tmp_ctx, "%s%u", DMAPI_SESSION_NAME,
80 if (session_name == NULL) {
81 DEBUG(0,("Out of memory in dmapi_init_session\n"));
87 if (dm_init_service(&version) < 0) {
88 DEBUG(0, ("dm_init_service failed - disabling DMAPI\n"));
95 /* Fetch kernel DMAPI sessions until we get any of them */
97 dm_sessid_t *new_sessions;
99 new_sessions = TALLOC_REALLOC_ARRAY(tmp_ctx, sessions,
100 dm_sessid_t, nsessions);
101 if (new_sessions == NULL) {
102 talloc_free(tmp_ctx);
106 sessions = new_sessions;
107 err = dm_getall_sessions(nsessions, sessions, &nsessions);
108 } while (err == -1 && errno == E2BIG);
111 DEBUGADD(DMAPI_TRACE,
112 ("failed to retrieve DMAPI sessions: %s\n",
114 talloc_free(tmp_ctx);
118 /* Look through existing kernel DMAPI sessions to find out ours */
119 for (i = 0; i < nsessions; ++i) {
120 err = dm_query_session(sessions[i], sizeof(buf), buf, &buflen);
121 buf[sizeof(buf) - 1] = '\0';
122 if (err == 0 && strcmp(session_name, buf) == 0) {
123 ctx->session = sessions[i];
124 DEBUGADD(DMAPI_TRACE,
125 ("attached to existing DMAPI session "
126 "named '%s'\n", buf));
131 /* No session already defined. */
132 if (ctx->session == DM_NO_SESSION) {
133 err = dm_create_session(DM_NO_SESSION,
137 DEBUGADD(DMAPI_TRACE,
138 ("failed to create new DMAPI session: %s\n",
140 ctx->session = DM_NO_SESSION;
141 talloc_free(tmp_ctx);
145 DEBUG(0, ("created new DMAPI session named '%s' for %s\n",
146 session_name, version));
149 if (ctx->session != DM_NO_SESSION) {
150 set_effective_capability(DMAPI_ACCESS_CAPABILITY);
154 Note that we never end the DMAPI session. It gets re-used if possiblie.
155 DMAPI session is a kernel resource that is usually lives until server reboot
156 and doesn't get destroed when an application finishes.
158 However, we free list of references to DMAPI sessions we've got from the kernel
159 as it is not needed anymore once we have found (or created) our session.
162 talloc_free(tmp_ctx);
167 Return a pointer to our DMAPI session, if available.
168 This assumes that you have called dmapi_have_session() first.
170 const void *dmapi_get_current_session(void)
176 if (dmapi_ctx->session == DM_NO_SESSION) {
180 return (void *)&dmapi_ctx->session;
184 dmapi_have_session() must be the first DMAPI call you make in Samba. It will
185 initialize DMAPI, if available, and tell you if you can get a DMAPI session.
186 This should be called in the client-specific child process.
189 bool dmapi_have_session(void)
192 dmapi_ctx = talloc(talloc_autofree_context(),
193 struct smbd_dmapi_context);
195 exit_server("unable to allocate smbd_dmapi_context");
197 dmapi_ctx->session = DM_NO_SESSION;
198 dmapi_ctx->session_num = 0;
201 dmapi_init_session(dmapi_ctx);
206 return dmapi_ctx->session != DM_NO_SESSION;
210 only call this when you get back an EINVAL error indicating that the
211 session you are using is invalid. This destroys the existing session
212 and creates a new one.
214 bool dmapi_new_session(void)
216 if (dmapi_have_session()) {
217 /* try to destroy the old one - this may not succeed */
218 dm_destroy_session(dmapi_ctx->session);
220 dmapi_ctx->session = DM_NO_SESSION;
222 dmapi_ctx->session_num++;
223 dmapi_init_session(dmapi_ctx);
225 return dmapi_ctx->session != DM_NO_SESSION;
229 only call this when exiting from master smbd process. DMAPI sessions
230 are long-lived kernel resources we ought to share across smbd processes.
231 However, we must free them when all smbd processes are finished to
232 allow other subsystems clean up properly. Not freeing DMAPI session
233 blocks certain HSM implementations from proper shutdown.
235 bool dmapi_destroy_session(void)
240 if (dmapi_ctx->session != DM_NO_SESSION) {
242 if (0 == dm_destroy_session(dmapi_ctx->session)) {
243 dmapi_ctx->session_num--;
244 dmapi_ctx->session = DM_NO_SESSION;
246 DEBUG(0,("Couldn't destroy DMAPI session: %s\n",
251 return dmapi_ctx->session == DM_NO_SESSION;
256 This is default implementation of dmapi_file_flags() that is
257 called from VFS is_offline() call to know whether file is offline.
258 For GPFS-specific version see modules/vfs_tsmsm.c. It might be
259 that approach on quering existence of a specific attribute that
260 is used in vfs_tsmsm.c will work with other DMAPI-based HSM
261 implementations as well.
263 uint32 dmapi_file_flags(const char * const path)
266 dm_eventset_t events = {0};
269 dm_sessid_t dmapi_session;
270 const void *dmapi_session_ptr;
271 void *dm_handle = NULL;
272 size_t dm_handle_len = 0;
276 dmapi_session_ptr = dmapi_get_current_session();
277 if (dmapi_session_ptr == NULL) {
281 dmapi_session = *(dm_sessid_t *)dmapi_session_ptr;
282 if (dmapi_session == DM_NO_SESSION) {
286 /* AIX has DMAPI but no POSIX capablities support. In this case,
287 * we need to be root to do DMAPI manipulations.
289 #ifndef HAVE_POSIX_CAPABILITIES
293 err = dm_path_to_handle(CONST_DISCARD(char *, path),
294 &dm_handle, &dm_handle_len);
296 DEBUG(DMAPI_TRACE, ("dm_path_to_handle(%s): %s\n",
297 path, strerror(errno)));
299 if (errno != EPERM) {
303 /* Linux capabilities are broken in that changing our
304 * user ID will clobber out effective capabilities irrespective
305 * of whether we have set PR_SET_KEEPCAPS. Fortunately, the
306 * capabilities are not removed from our permitted set, so we
307 * can re-acquire them if necessary.
310 set_effective_capability(DMAPI_ACCESS_CAPABILITY);
312 err = dm_path_to_handle(CONST_DISCARD(char *, path),
313 &dm_handle, &dm_handle_len);
316 ("retrying dm_path_to_handle(%s): %s\n",
317 path, strerror(errno)));
322 err = dm_get_eventlist(dmapi_session, dm_handle, dm_handle_len,
323 DM_NO_TOKEN, DM_EVENT_MAX, &events, &nevents);
325 DEBUG(DMAPI_TRACE, ("dm_get_eventlist(%s): %s\n",
326 path, strerror(errno)));
327 dm_handle_free(dm_handle, dm_handle_len);
331 /* We figure that the only reason a DMAPI application would be
332 * interested in trapping read events is that part of the file is
335 DEBUG(DMAPI_TRACE, ("DMAPI event list for %s\n", path));
336 if (DMEV_ISSET(DM_EVENT_READ, events)) {
337 flags = FILE_ATTRIBUTE_OFFLINE;
340 dm_handle_free(dm_handle, dm_handle_len);
342 if (flags & FILE_ATTRIBUTE_OFFLINE) {
343 DEBUG(DMAPI_TRACE, ("%s is OFFLINE\n", path));
348 #ifndef HAVE_POSIX_CAPABILITIES
356 #endif /* USE_DMAPI */