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
;
162 DEBUG(10, ("aio_open_handle_completion called with flags=%d\n",
165 if ((flags
& TEVENT_FD_READ
) == 0) {
169 ret
= pthreadpool_finished_job(open_pool
, &jobid
);
171 smb_panic("aio_open_handle_completion");
176 opd
= find_open_private_data_by_jobid(jobid
);
178 DEBUG(0, ("aio_open_handle_completion cannot find jobid %d\n",
180 smb_panic("aio_open_handle_completion - no jobid");
185 DEBUG(10,("aio_open_handle_completion: jobid %d mid %llu "
186 "for file %s/%s completed\n",
188 (unsigned long long)opd
->mid
,
192 opd
->in_progress
= false;
194 /* Find outstanding event and reschdule. */
195 if (!schedule_deferred_open_message_smb(opd
->sconn
, opd
->mid
)) {
197 * Outstanding event didn't exist or was
198 * cancelled. Free up the fd and throw
201 if (opd
->ret_fd
!= -1) {
209 /*****************************************************************
210 The core of the async open code - the worker function. Note we
211 use the new openat() system call to avoid any problems with
212 current working directory changes plus we change credentials
213 on the thread to prevent any security race conditions.
214 *****************************************************************/
216 static void aio_open_worker(void *private_data
)
218 struct aio_open_private_data
*opd
=
219 (struct aio_open_private_data
*)private_data
;
221 /* Become the correct credential on this thread. */
222 if (set_thread_credentials(opd
->ux_tok
->uid
,
224 (size_t)opd
->ux_tok
->ngroups
,
225 opd
->ux_tok
->groups
) != 0) {
227 opd
->ret_errno
= errno
;
231 opd
->ret_fd
= openat(opd
->dir_fd
,
236 if (opd
->ret_fd
== -1) {
237 opd
->ret_errno
= errno
;
239 /* Create was successful. */
242 #if defined(HAVE_LINUX_FALLOCATE)
244 * See if we can set the initial
245 * allocation size. We don't record
246 * the return for this as it's an
247 * optimization - the upper layer
248 * will also do this for us once
251 if (opd
->initial_allocation_size
) {
252 (void)fallocate(opd
->ret_fd
,
255 (off_t
)opd
->initial_allocation_size
);
261 /************************************************************************
262 Open private data destructor.
263 ***********************************************************************/
265 static int opd_destructor(struct aio_open_private_data
*opd
)
267 if (opd
->dir_fd
!= -1) {
270 DLIST_REMOVE(open_pd_list
, opd
);
274 /************************************************************************
275 Create and initialize a private data struct for async open.
276 ***********************************************************************/
278 static struct aio_open_private_data
*create_private_open_data(const files_struct
*fsp
,
282 struct aio_open_private_data
*opd
= talloc_zero(NULL
,
283 struct aio_open_private_data
);
284 const char *fname
= NULL
;
290 opd
->jobid
= aio_pthread_open_jobid
++;
293 opd
->ret_errno
= EINPROGRESS
;
297 opd
->in_progress
= true;
298 opd
->sconn
= fsp
->conn
->sconn
;
299 opd
->initial_allocation_size
= fsp
->initial_allocation_size
;
301 /* Copy our current credentials. */
302 opd
->ux_tok
= copy_unix_token(opd
, get_current_utok(fsp
->conn
));
303 if (opd
->ux_tok
== NULL
) {
309 * Copy the parent directory name and the
310 * relative path within it.
312 if (parent_dirname(opd
,
313 fsp
->fsp_name
->base_name
,
319 opd
->fname
= talloc_strdup(opd
, fname
);
320 if (opd
->fname
== NULL
) {
325 #if defined(O_DIRECTORY)
326 opd
->dir_fd
= open(opd
->dname
, O_RDONLY
|O_DIRECTORY
);
328 opd
->dir_fd
= open(opd
->dname
, O_RDONLY
);
330 if (opd
->dir_fd
== -1) {
335 talloc_set_destructor(opd
, opd_destructor
);
336 DLIST_ADD_END(open_pd_list
, opd
, struct aio_open_private_data
*);
340 /*****************************************************************
342 *****************************************************************/
344 static int open_async(const files_struct
*fsp
,
348 struct aio_open_private_data
*opd
= NULL
;
351 if (!init_aio_threadpool(fsp
->conn
->sconn
->ev_ctx
,
353 aio_open_handle_completion
)) {
357 opd
= create_private_open_data(fsp
, flags
, mode
);
359 DEBUG(10, ("open_async: Could not create private data.\n"));
363 ret
= pthreadpool_add_job(open_pool
,
372 DEBUG(5,("open_async: mid %llu jobid %d created for file %s/%s\n",
373 (unsigned long long)opd
->mid
,
378 /* Cause the calling code to reschedule us. */
379 errno
= EINTR
; /* Maps to NT_STATUS_RETRY. */
383 /*****************************************************************
384 Look for a matching SMB2 mid. If we find it we're rescheduled,
385 just return the completed open.
386 *****************************************************************/
388 static bool find_completed_open(files_struct
*fsp
,
392 struct aio_open_private_data
*opd
;
394 opd
= find_open_private_data_by_mid(fsp
->mid
);
399 if (opd
->in_progress
) {
400 DEBUG(0,("find_completed_open: mid %llu "
401 "jobid %d still in progress for "
402 "file %s/%s. PANIC !\n",
403 (unsigned long long)opd
->mid
,
407 /* Disaster ! This is an open timeout. Just panic. */
408 smb_panic("find_completed_open - in_progress\n");
414 *p_errno
= opd
->ret_errno
;
416 DEBUG(5,("find_completed_open: mid %llu returning "
417 "fd = %d, errno = %d (%s) "
418 "jobid (%d) for file %s\n",
419 (unsigned long long)opd
->mid
,
422 strerror(opd
->ret_errno
),
424 smb_fname_str_dbg(fsp
->fsp_name
)));
426 /* Now we can free the opd. */
431 /*****************************************************************
432 The core open function. Only go async on O_CREAT|O_EXCL
433 opens to prevent any race conditions.
434 *****************************************************************/
436 static int aio_pthread_open_fn(vfs_handle_struct
*handle
,
437 struct smb_filename
*smb_fname
,
444 bool aio_allow_open
= lp_parm_bool(
445 SNUM(handle
->conn
), "aio_pthread", "aio open", false);
447 if (smb_fname
->stream_name
) {
448 /* Don't handle stream opens. */
453 if (!aio_allow_open
) {
454 /* aio opens turned off. */
455 return open(smb_fname
->base_name
, flags
, mode
);
458 if (!(flags
& O_CREAT
)) {
459 /* Only creates matter. */
460 return open(smb_fname
->base_name
, flags
, mode
);
463 if (!(flags
& O_EXCL
)) {
464 /* Only creates with O_EXCL matter. */
465 return open(smb_fname
->base_name
, flags
, mode
);
469 * See if this is a reentrant call - i.e. is this a
470 * restart of an existing open that just completed.
473 if (find_completed_open(fsp
,
480 /* Ok, it's a create exclusive call - pass it to a thread helper. */
481 return open_async(fsp
, flags
, mode
);
485 static struct vfs_fn_pointers vfs_aio_pthread_fns
= {
486 #if defined(HAVE_OPENAT) && defined(USE_LINUX_THREAD_CREDENTIALS)
487 .open_fn
= aio_pthread_open_fn
,
491 NTSTATUS
vfs_aio_pthread_init(void);
492 NTSTATUS
vfs_aio_pthread_init(void)
494 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
495 "aio_pthread", &vfs_aio_pthread_fns
);