2 * Simulate Posix AIO using pthreads.
4 * Based on the aio_fork work from Volker and Volker's pthreadpool library.
6 * Copyright (C) Volker Lendecke 2008
7 * Copyright (C) Jeremy Allison 2012
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, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "system/filesys.h"
26 #include "system/shmem.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "lib/pthreadpool/pthreadpool.h"
30 #ifdef HAVE_LINUX_FALLOC_H
31 #include <linux/falloc.h>
34 #if defined(HAVE_OPENAT) && defined(USE_LINUX_THREAD_CREDENTIALS)
36 /************************************************************************
37 Ensure thread pool is initialized.
38 ***********************************************************************/
40 static bool init_aio_threadpool(struct tevent_context
*ev_ctx
,
41 struct pthreadpool
**pp_pool
,
42 void (*completion_fn
)(struct tevent_context
*,
47 struct tevent_fd
*sock_event
= NULL
;
54 ret
= pthreadpool_init(aio_pending_size
, pp_pool
);
59 sock_event
= tevent_add_fd(ev_ctx
,
61 pthreadpool_signal_fd(*pp_pool
),
65 if (sock_event
== NULL
) {
66 pthreadpool_destroy(*pp_pool
);
71 DEBUG(10,("init_aio_threadpool: initialized with up to %d threads\n",
78 * We must have openat() to do any thread-based
79 * asynchronous opens. We also must be using
80 * thread-specific credentials (Linux-only
85 * NB. This threadpool is shared over all
86 * instances of this VFS module in this
87 * process, as is the current jobid.
90 static struct pthreadpool
*open_pool
;
91 static int aio_pthread_open_jobid
;
93 struct aio_open_private_data
{
94 struct aio_open_private_data
*prev
, *next
;
104 struct smbd_server_connection
*sconn
;
105 const struct security_unix_token
*ux_tok
;
106 uint64_t initial_allocation_size
;
112 /* List of outstanding requests we have. */
113 static struct aio_open_private_data
*open_pd_list
;
115 /************************************************************************
116 Find the open private data by jobid.
117 ***********************************************************************/
119 static struct aio_open_private_data
*find_open_private_data_by_jobid(int jobid
)
121 struct aio_open_private_data
*opd
;
123 for (opd
= open_pd_list
; opd
!= NULL
; opd
= opd
->next
) {
124 if (opd
->jobid
== jobid
) {
132 /************************************************************************
133 Find the open private data by mid.
134 ***********************************************************************/
136 static struct aio_open_private_data
*find_open_private_data_by_mid(uint64_t mid
)
138 struct aio_open_private_data
*opd
;
140 for (opd
= open_pd_list
; opd
!= NULL
; opd
= opd
->next
) {
141 if (opd
->mid
== mid
) {
149 /************************************************************************
150 Callback when an open completes.
151 ***********************************************************************/
153 static void aio_open_handle_completion(struct tevent_context
*event_ctx
,
154 struct tevent_fd
*event
,
158 struct aio_open_private_data
*opd
= NULL
;
161 struct smbXsrv_connection
*xconn
;
163 DEBUG(10, ("aio_open_handle_completion called with flags=%d\n",
166 if ((flags
& TEVENT_FD_READ
) == 0) {
170 ret
= pthreadpool_finished_jobs(open_pool
, &jobid
, 1);
172 smb_panic("aio_open_handle_completion");
177 opd
= find_open_private_data_by_jobid(jobid
);
179 DEBUG(0, ("aio_open_handle_completion cannot find jobid %d\n",
181 smb_panic("aio_open_handle_completion - no jobid");
186 DEBUG(10,("aio_open_handle_completion: jobid %d mid %llu "
187 "for file %s/%s completed\n",
189 (unsigned long long)opd
->mid
,
193 opd
->in_progress
= false;
196 * TODO: In future we need a proper algorithm
197 * to find the correct connection for a fsp.
198 * For now we only have one connection, so this is correct...
200 xconn
= opd
->sconn
->client
->connections
;
202 /* Find outstanding event and reschedule. */
203 if (!schedule_deferred_open_message_smb(xconn
, opd
->mid
)) {
205 * Outstanding event didn't exist or was
206 * cancelled. Free up the fd and throw
209 if (opd
->ret_fd
!= -1) {
217 /*****************************************************************
218 The core of the async open code - the worker function. Note we
219 use the new openat() system call to avoid any problems with
220 current working directory changes plus we change credentials
221 on the thread to prevent any security race conditions.
222 *****************************************************************/
224 static void aio_open_worker(void *private_data
)
226 struct aio_open_private_data
*opd
=
227 (struct aio_open_private_data
*)private_data
;
229 /* Become the correct credential on this thread. */
230 if (set_thread_credentials(opd
->ux_tok
->uid
,
232 (size_t)opd
->ux_tok
->ngroups
,
233 opd
->ux_tok
->groups
) != 0) {
235 opd
->ret_errno
= errno
;
239 opd
->ret_fd
= openat(opd
->dir_fd
,
244 if (opd
->ret_fd
== -1) {
245 opd
->ret_errno
= errno
;
247 /* Create was successful. */
250 #if defined(HAVE_LINUX_FALLOCATE)
252 * See if we can set the initial
253 * allocation size. We don't record
254 * the return for this as it's an
255 * optimization - the upper layer
256 * will also do this for us once
259 if (opd
->initial_allocation_size
) {
260 (void)fallocate(opd
->ret_fd
,
263 (off_t
)opd
->initial_allocation_size
);
269 /************************************************************************
270 Open private data destructor.
271 ***********************************************************************/
273 static int opd_destructor(struct aio_open_private_data
*opd
)
275 if (opd
->dir_fd
!= -1) {
278 DLIST_REMOVE(open_pd_list
, opd
);
282 /************************************************************************
283 Create and initialize a private data struct for async open.
284 ***********************************************************************/
286 static struct aio_open_private_data
*create_private_open_data(const files_struct
*fsp
,
290 struct aio_open_private_data
*opd
= talloc_zero(NULL
,
291 struct aio_open_private_data
);
292 const char *fname
= NULL
;
298 opd
->jobid
= aio_pthread_open_jobid
++;
301 opd
->ret_errno
= EINPROGRESS
;
305 opd
->in_progress
= true;
306 opd
->sconn
= fsp
->conn
->sconn
;
307 opd
->initial_allocation_size
= fsp
->initial_allocation_size
;
309 /* Copy our current credentials. */
310 opd
->ux_tok
= copy_unix_token(opd
, get_current_utok(fsp
->conn
));
311 if (opd
->ux_tok
== NULL
) {
317 * Copy the parent directory name and the
318 * relative path within it.
320 if (parent_dirname(opd
,
321 fsp
->fsp_name
->base_name
,
327 opd
->fname
= talloc_strdup(opd
, fname
);
328 if (opd
->fname
== NULL
) {
333 #if defined(O_DIRECTORY)
334 opd
->dir_fd
= open(opd
->dname
, O_RDONLY
|O_DIRECTORY
);
336 opd
->dir_fd
= open(opd
->dname
, O_RDONLY
);
338 if (opd
->dir_fd
== -1) {
343 talloc_set_destructor(opd
, opd_destructor
);
344 DLIST_ADD_END(open_pd_list
, opd
, struct aio_open_private_data
*);
348 /*****************************************************************
350 *****************************************************************/
352 static int open_async(const files_struct
*fsp
,
356 struct aio_open_private_data
*opd
= NULL
;
359 if (!init_aio_threadpool(fsp
->conn
->sconn
->ev_ctx
,
361 aio_open_handle_completion
)) {
365 opd
= create_private_open_data(fsp
, flags
, mode
);
367 DEBUG(10, ("open_async: Could not create private data.\n"));
371 ret
= pthreadpool_add_job(open_pool
,
380 DEBUG(5,("open_async: mid %llu jobid %d created for file %s/%s\n",
381 (unsigned long long)opd
->mid
,
386 /* Cause the calling code to reschedule us. */
387 errno
= EINTR
; /* Maps to NT_STATUS_RETRY. */
391 /*****************************************************************
392 Look for a matching SMB2 mid. If we find it we're rescheduled,
393 just return the completed open.
394 *****************************************************************/
396 static bool find_completed_open(files_struct
*fsp
,
400 struct aio_open_private_data
*opd
;
402 opd
= find_open_private_data_by_mid(fsp
->mid
);
407 if (opd
->in_progress
) {
408 DEBUG(0,("find_completed_open: mid %llu "
409 "jobid %d still in progress for "
410 "file %s/%s. PANIC !\n",
411 (unsigned long long)opd
->mid
,
415 /* Disaster ! This is an open timeout. Just panic. */
416 smb_panic("find_completed_open - in_progress\n");
422 *p_errno
= opd
->ret_errno
;
424 DEBUG(5,("find_completed_open: mid %llu returning "
425 "fd = %d, errno = %d (%s) "
426 "jobid (%d) for file %s\n",
427 (unsigned long long)opd
->mid
,
430 strerror(opd
->ret_errno
),
432 smb_fname_str_dbg(fsp
->fsp_name
)));
434 /* Now we can free the opd. */
439 /*****************************************************************
440 The core open function. Only go async on O_CREAT|O_EXCL
441 opens to prevent any race conditions.
442 *****************************************************************/
444 static int aio_pthread_open_fn(vfs_handle_struct
*handle
,
445 struct smb_filename
*smb_fname
,
452 bool aio_allow_open
= lp_parm_bool(
453 SNUM(handle
->conn
), "aio_pthread", "aio open", false);
455 if (smb_fname
->stream_name
) {
456 /* Don't handle stream opens. */
461 if (!aio_allow_open
) {
462 /* aio opens turned off. */
463 return open(smb_fname
->base_name
, flags
, mode
);
466 if (!(flags
& O_CREAT
)) {
467 /* Only creates matter. */
468 return open(smb_fname
->base_name
, flags
, mode
);
471 if (!(flags
& O_EXCL
)) {
472 /* Only creates with O_EXCL matter. */
473 return open(smb_fname
->base_name
, flags
, mode
);
477 * See if this is a reentrant call - i.e. is this a
478 * restart of an existing open that just completed.
481 if (find_completed_open(fsp
,
488 /* Ok, it's a create exclusive call - pass it to a thread helper. */
489 return open_async(fsp
, flags
, mode
);
493 static struct vfs_fn_pointers vfs_aio_pthread_fns
= {
494 #if defined(HAVE_OPENAT) && defined(USE_LINUX_THREAD_CREDENTIALS)
495 .open_fn
= aio_pthread_open_fn
,
499 NTSTATUS
vfs_aio_pthread_init(void);
500 NTSTATUS
vfs_aio_pthread_init(void)
502 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
503 "aio_pthread", &vfs_aio_pthread_fns
);