The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_aio.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1997 John S. Dyson.  All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. John S. Dyson's name may not be used to endorse or promote products
   12  *    derived from this software without specific prior written permission.
   13  *
   14  * DISCLAIMER:  This code isn't warranted to do anything useful.  Anything
   15  * bad that happens because of using this software isn't the responsibility
   16  * of the author.  This software is distributed AS-IS.
   17  */
   18 
   19 /*
   20  * This file contains support for the POSIX 1003.1B AIO/LIO facility.
   21  */
   22 
   23 #include <sys/cdefs.h>
   24 __FBSDID("$FreeBSD$");
   25 
   26 #include <sys/param.h>
   27 #include <sys/systm.h>
   28 #include <sys/malloc.h>
   29 #include <sys/bio.h>
   30 #include <sys/buf.h>
   31 #include <sys/capsicum.h>
   32 #include <sys/eventhandler.h>
   33 #include <sys/sysproto.h>
   34 #include <sys/filedesc.h>
   35 #include <sys/kernel.h>
   36 #include <sys/module.h>
   37 #include <sys/kthread.h>
   38 #include <sys/fcntl.h>
   39 #include <sys/file.h>
   40 #include <sys/limits.h>
   41 #include <sys/lock.h>
   42 #include <sys/mutex.h>
   43 #include <sys/unistd.h>
   44 #include <sys/posix4.h>
   45 #include <sys/proc.h>
   46 #include <sys/resourcevar.h>
   47 #include <sys/signalvar.h>
   48 #include <sys/syscallsubr.h>
   49 #include <sys/protosw.h>
   50 #include <sys/rwlock.h>
   51 #include <sys/sema.h>
   52 #include <sys/socket.h>
   53 #include <sys/socketvar.h>
   54 #include <sys/syscall.h>
   55 #include <sys/sysent.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/syslog.h>
   58 #include <sys/sx.h>
   59 #include <sys/taskqueue.h>
   60 #include <sys/vnode.h>
   61 #include <sys/conf.h>
   62 #include <sys/event.h>
   63 #include <sys/mount.h>
   64 #include <geom/geom.h>
   65 
   66 #include <machine/atomic.h>
   67 
   68 #include <vm/vm.h>
   69 #include <vm/vm_page.h>
   70 #include <vm/vm_extern.h>
   71 #include <vm/pmap.h>
   72 #include <vm/vm_map.h>
   73 #include <vm/vm_object.h>
   74 #include <vm/uma.h>
   75 #include <sys/aio.h>
   76 
   77 /*
   78  * Counter for allocating reference ids to new jobs.  Wrapped to 1 on
   79  * overflow. (XXX will be removed soon.)
   80  */
   81 static u_long jobrefid;
   82 
   83 /*
   84  * Counter for aio_fsync.
   85  */
   86 static uint64_t jobseqno;
   87 
   88 #ifndef MAX_AIO_PER_PROC
   89 #define MAX_AIO_PER_PROC        32
   90 #endif
   91 
   92 #ifndef MAX_AIO_QUEUE_PER_PROC
   93 #define MAX_AIO_QUEUE_PER_PROC  256
   94 #endif
   95 
   96 #ifndef MAX_AIO_QUEUE
   97 #define MAX_AIO_QUEUE           1024 /* Bigger than MAX_AIO_QUEUE_PER_PROC */
   98 #endif
   99 
  100 #ifndef MAX_BUF_AIO
  101 #define MAX_BUF_AIO             16
  102 #endif
  103 
  104 FEATURE(aio, "Asynchronous I/O");
  105 SYSCTL_DECL(_p1003_1b);
  106 
  107 static MALLOC_DEFINE(M_LIO, "lio", "listio aio control block list");
  108 static MALLOC_DEFINE(M_AIOS, "aios", "aio_suspend aio control block list");
  109 
  110 static SYSCTL_NODE(_vfs, OID_AUTO, aio, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  111     "Async IO management");
  112 
  113 static int enable_aio_unsafe = 0;
  114 SYSCTL_INT(_vfs_aio, OID_AUTO, enable_unsafe, CTLFLAG_RW, &enable_aio_unsafe, 0,
  115     "Permit asynchronous IO on all file types, not just known-safe types");
  116 
  117 static unsigned int unsafe_warningcnt = 1;
  118 SYSCTL_UINT(_vfs_aio, OID_AUTO, unsafe_warningcnt, CTLFLAG_RW,
  119     &unsafe_warningcnt, 0,
  120     "Warnings that will be triggered upon failed IO requests on unsafe files");
  121 
  122 static int max_aio_procs = MAX_AIO_PROCS;
  123 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_procs, CTLFLAG_RW, &max_aio_procs, 0,
  124     "Maximum number of kernel processes to use for handling async IO ");
  125 
  126 static int num_aio_procs = 0;
  127 SYSCTL_INT(_vfs_aio, OID_AUTO, num_aio_procs, CTLFLAG_RD, &num_aio_procs, 0,
  128     "Number of presently active kernel processes for async IO");
  129 
  130 /*
  131  * The code will adjust the actual number of AIO processes towards this
  132  * number when it gets a chance.
  133  */
  134 static int target_aio_procs = TARGET_AIO_PROCS;
  135 SYSCTL_INT(_vfs_aio, OID_AUTO, target_aio_procs, CTLFLAG_RW, &target_aio_procs,
  136     0,
  137     "Preferred number of ready kernel processes for async IO");
  138 
  139 static int max_queue_count = MAX_AIO_QUEUE;
  140 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue, CTLFLAG_RW, &max_queue_count, 0,
  141     "Maximum number of aio requests to queue, globally");
  142 
  143 static int num_queue_count = 0;
  144 SYSCTL_INT(_vfs_aio, OID_AUTO, num_queue_count, CTLFLAG_RD, &num_queue_count, 0,
  145     "Number of queued aio requests");
  146 
  147 static int num_buf_aio = 0;
  148 SYSCTL_INT(_vfs_aio, OID_AUTO, num_buf_aio, CTLFLAG_RD, &num_buf_aio, 0,
  149     "Number of aio requests presently handled by the buf subsystem");
  150 
  151 static int num_unmapped_aio = 0;
  152 SYSCTL_INT(_vfs_aio, OID_AUTO, num_unmapped_aio, CTLFLAG_RD, &num_unmapped_aio,
  153     0,
  154     "Number of aio requests presently handled by unmapped I/O buffers");
  155 
  156 /* Number of async I/O processes in the process of being started */
  157 /* XXX This should be local to aio_aqueue() */
  158 static int num_aio_resv_start = 0;
  159 
  160 static int aiod_lifetime;
  161 SYSCTL_INT(_vfs_aio, OID_AUTO, aiod_lifetime, CTLFLAG_RW, &aiod_lifetime, 0,
  162     "Maximum lifetime for idle aiod");
  163 
  164 static int max_aio_per_proc = MAX_AIO_PER_PROC;
  165 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_per_proc, CTLFLAG_RW, &max_aio_per_proc,
  166     0,
  167     "Maximum active aio requests per process");
  168 
  169 static int max_aio_queue_per_proc = MAX_AIO_QUEUE_PER_PROC;
  170 SYSCTL_INT(_vfs_aio, OID_AUTO, max_aio_queue_per_proc, CTLFLAG_RW,
  171     &max_aio_queue_per_proc, 0,
  172     "Maximum queued aio requests per process");
  173 
  174 static int max_buf_aio = MAX_BUF_AIO;
  175 SYSCTL_INT(_vfs_aio, OID_AUTO, max_buf_aio, CTLFLAG_RW, &max_buf_aio, 0,
  176     "Maximum buf aio requests per process");
  177 
  178 /* 
  179  * Though redundant with vfs.aio.max_aio_queue_per_proc, POSIX requires
  180  * sysconf(3) to support AIO_LISTIO_MAX, and we implement that with
  181  * vfs.aio.aio_listio_max.
  182  */
  183 SYSCTL_INT(_p1003_1b, CTL_P1003_1B_AIO_LISTIO_MAX, aio_listio_max,
  184     CTLFLAG_RD | CTLFLAG_CAPRD, &max_aio_queue_per_proc,
  185     0, "Maximum aio requests for a single lio_listio call");
  186 
  187 #ifdef COMPAT_FREEBSD6
  188 typedef struct oaiocb {
  189         int     aio_fildes;             /* File descriptor */
  190         off_t   aio_offset;             /* File offset for I/O */
  191         volatile void *aio_buf;         /* I/O buffer in process space */
  192         size_t  aio_nbytes;             /* Number of bytes for I/O */
  193         struct  osigevent aio_sigevent; /* Signal to deliver */
  194         int     aio_lio_opcode;         /* LIO opcode */
  195         int     aio_reqprio;            /* Request priority -- ignored */
  196         struct  __aiocb_private _aiocb_private;
  197 } oaiocb_t;
  198 #endif
  199 
  200 /*
  201  * Below is a key of locks used to protect each member of struct kaiocb
  202  * aioliojob and kaioinfo and any backends.
  203  *
  204  * * - need not protected
  205  * a - locked by kaioinfo lock
  206  * b - locked by backend lock, the backend lock can be null in some cases,
  207  *     for example, BIO belongs to this type, in this case, proc lock is
  208  *     reused.
  209  * c - locked by aio_job_mtx, the lock for the generic file I/O backend.
  210  */
  211 
  212 /*
  213  * If the routine that services an AIO request blocks while running in an
  214  * AIO kernel process it can starve other I/O requests.  BIO requests
  215  * queued via aio_qbio() complete asynchronously and do not use AIO kernel
  216  * processes at all.  Socket I/O requests use a separate pool of
  217  * kprocs and also force non-blocking I/O.  Other file I/O requests
  218  * use the generic fo_read/fo_write operations which can block.  The
  219  * fsync and mlock operations can also block while executing.  Ideally
  220  * none of these requests would block while executing.
  221  *
  222  * Note that the service routines cannot toggle O_NONBLOCK in the file
  223  * structure directly while handling a request due to races with
  224  * userland threads.
  225  */
  226 
  227 /* jobflags */
  228 #define KAIOCB_QUEUEING         0x01
  229 #define KAIOCB_CANCELLED        0x02
  230 #define KAIOCB_CANCELLING       0x04
  231 #define KAIOCB_CHECKSYNC        0x08
  232 #define KAIOCB_CLEARED          0x10
  233 #define KAIOCB_FINISHED         0x20
  234 
  235 /*
  236  * AIO process info
  237  */
  238 #define AIOP_FREE       0x1                     /* proc on free queue */
  239 
  240 struct aioproc {
  241         int     aioprocflags;                   /* (c) AIO proc flags */
  242         TAILQ_ENTRY(aioproc) list;              /* (c) list of processes */
  243         struct  proc *aioproc;                  /* (*) the AIO proc */
  244 };
  245 
  246 /*
  247  * data-structure for lio signal management
  248  */
  249 struct aioliojob {
  250         int     lioj_flags;                     /* (a) listio flags */
  251         int     lioj_count;                     /* (a) count of jobs */
  252         int     lioj_finished_count;            /* (a) count of finished jobs */
  253         struct  sigevent lioj_signal;           /* (a) signal on all I/O done */
  254         TAILQ_ENTRY(aioliojob) lioj_list;       /* (a) lio list */
  255         struct  knlist klist;                   /* (a) list of knotes */
  256         ksiginfo_t lioj_ksi;                    /* (a) Realtime signal info */
  257 };
  258 
  259 #define LIOJ_SIGNAL             0x1     /* signal on all done (lio) */
  260 #define LIOJ_SIGNAL_POSTED      0x2     /* signal has been posted */
  261 #define LIOJ_KEVENT_POSTED      0x4     /* kevent triggered */
  262 
  263 /*
  264  * per process aio data structure
  265  */
  266 struct kaioinfo {
  267         struct  mtx kaio_mtx;           /* the lock to protect this struct */
  268         int     kaio_flags;             /* (a) per process kaio flags */
  269         int     kaio_active_count;      /* (c) number of currently used AIOs */
  270         int     kaio_count;             /* (a) size of AIO queue */
  271         int     kaio_buffer_count;      /* (a) number of bio buffers */
  272         TAILQ_HEAD(,kaiocb) kaio_all;   /* (a) all AIOs in a process */
  273         TAILQ_HEAD(,kaiocb) kaio_done;  /* (a) done queue for process */
  274         TAILQ_HEAD(,aioliojob) kaio_liojoblist; /* (a) list of lio jobs */
  275         TAILQ_HEAD(,kaiocb) kaio_jobqueue;      /* (a) job queue for process */
  276         TAILQ_HEAD(,kaiocb) kaio_syncqueue;     /* (a) queue for aio_fsync */
  277         TAILQ_HEAD(,kaiocb) kaio_syncready;  /* (a) second q for aio_fsync */
  278         struct  task kaio_task;         /* (*) task to kick aio processes */
  279         struct  task kaio_sync_task;    /* (*) task to schedule fsync jobs */
  280 };
  281 
  282 #define AIO_LOCK(ki)            mtx_lock(&(ki)->kaio_mtx)
  283 #define AIO_UNLOCK(ki)          mtx_unlock(&(ki)->kaio_mtx)
  284 #define AIO_LOCK_ASSERT(ki, f)  mtx_assert(&(ki)->kaio_mtx, (f))
  285 #define AIO_MTX(ki)             (&(ki)->kaio_mtx)
  286 
  287 #define KAIO_RUNDOWN    0x1     /* process is being run down */
  288 #define KAIO_WAKEUP     0x2     /* wakeup process when AIO completes */
  289 
  290 /*
  291  * Operations used to interact with userland aio control blocks.
  292  * Different ABIs provide their own operations.
  293  */
  294 struct aiocb_ops {
  295         int     (*aio_copyin)(struct aiocb *ujob, struct kaiocb *kjob, int ty);
  296         long    (*fetch_status)(struct aiocb *ujob);
  297         long    (*fetch_error)(struct aiocb *ujob);
  298         int     (*store_status)(struct aiocb *ujob, long status);
  299         int     (*store_error)(struct aiocb *ujob, long error);
  300         int     (*store_kernelinfo)(struct aiocb *ujob, long jobref);
  301         int     (*store_aiocb)(struct aiocb **ujobp, struct aiocb *ujob);
  302 };
  303 
  304 static TAILQ_HEAD(,aioproc) aio_freeproc;               /* (c) Idle daemons */
  305 static struct sema aio_newproc_sem;
  306 static struct mtx aio_job_mtx;
  307 static TAILQ_HEAD(,kaiocb) aio_jobs;                    /* (c) Async job list */
  308 static struct unrhdr *aiod_unr;
  309 
  310 static void     aio_biocleanup(struct bio *bp);
  311 void            aio_init_aioinfo(struct proc *p);
  312 static int      aio_onceonly(void);
  313 static int      aio_free_entry(struct kaiocb *job);
  314 static void     aio_process_rw(struct kaiocb *job);
  315 static void     aio_process_sync(struct kaiocb *job);
  316 static void     aio_process_mlock(struct kaiocb *job);
  317 static void     aio_schedule_fsync(void *context, int pending);
  318 static int      aio_newproc(int *);
  319 int             aio_aqueue(struct thread *td, struct aiocb *ujob,
  320                     struct aioliojob *lio, int type, struct aiocb_ops *ops);
  321 static int      aio_queue_file(struct file *fp, struct kaiocb *job);
  322 static void     aio_biowakeup(struct bio *bp);
  323 static void     aio_proc_rundown(void *arg, struct proc *p);
  324 static void     aio_proc_rundown_exec(void *arg, struct proc *p,
  325                     struct image_params *imgp);
  326 static int      aio_qbio(struct proc *p, struct kaiocb *job);
  327 static void     aio_daemon(void *param);
  328 static void     aio_bio_done_notify(struct proc *userp, struct kaiocb *job);
  329 static bool     aio_clear_cancel_function_locked(struct kaiocb *job);
  330 static int      aio_kick(struct proc *userp);
  331 static void     aio_kick_nowait(struct proc *userp);
  332 static void     aio_kick_helper(void *context, int pending);
  333 static int      filt_aioattach(struct knote *kn);
  334 static void     filt_aiodetach(struct knote *kn);
  335 static int      filt_aio(struct knote *kn, long hint);
  336 static int      filt_lioattach(struct knote *kn);
  337 static void     filt_liodetach(struct knote *kn);
  338 static int      filt_lio(struct knote *kn, long hint);
  339 
  340 /*
  341  * Zones for:
  342  *      kaio    Per process async io info
  343  *      aiop    async io process data
  344  *      aiocb   async io jobs
  345  *      aiolio  list io jobs
  346  */
  347 static uma_zone_t kaio_zone, aiop_zone, aiocb_zone, aiolio_zone;
  348 
  349 /* kqueue filters for aio */
  350 static struct filterops aio_filtops = {
  351         .f_isfd = 0,
  352         .f_attach = filt_aioattach,
  353         .f_detach = filt_aiodetach,
  354         .f_event = filt_aio,
  355 };
  356 static struct filterops lio_filtops = {
  357         .f_isfd = 0,
  358         .f_attach = filt_lioattach,
  359         .f_detach = filt_liodetach,
  360         .f_event = filt_lio
  361 };
  362 
  363 static eventhandler_tag exit_tag, exec_tag;
  364 
  365 TASKQUEUE_DEFINE_THREAD(aiod_kick);
  366 
  367 /*
  368  * Main operations function for use as a kernel module.
  369  */
  370 static int
  371 aio_modload(struct module *module, int cmd, void *arg)
  372 {
  373         int error = 0;
  374 
  375         switch (cmd) {
  376         case MOD_LOAD:
  377                 aio_onceonly();
  378                 break;
  379         case MOD_SHUTDOWN:
  380                 break;
  381         default:
  382                 error = EOPNOTSUPP;
  383                 break;
  384         }
  385         return (error);
  386 }
  387 
  388 static moduledata_t aio_mod = {
  389         "aio",
  390         &aio_modload,
  391         NULL
  392 };
  393 
  394 DECLARE_MODULE(aio, aio_mod, SI_SUB_VFS, SI_ORDER_ANY);
  395 MODULE_VERSION(aio, 1);
  396 
  397 /*
  398  * Startup initialization
  399  */
  400 static int
  401 aio_onceonly(void)
  402 {
  403 
  404         exit_tag = EVENTHANDLER_REGISTER(process_exit, aio_proc_rundown, NULL,
  405             EVENTHANDLER_PRI_ANY);
  406         exec_tag = EVENTHANDLER_REGISTER(process_exec, aio_proc_rundown_exec,
  407             NULL, EVENTHANDLER_PRI_ANY);
  408         kqueue_add_filteropts(EVFILT_AIO, &aio_filtops);
  409         kqueue_add_filteropts(EVFILT_LIO, &lio_filtops);
  410         TAILQ_INIT(&aio_freeproc);
  411         sema_init(&aio_newproc_sem, 0, "aio_new_proc");
  412         mtx_init(&aio_job_mtx, "aio_job", NULL, MTX_DEF);
  413         TAILQ_INIT(&aio_jobs);
  414         aiod_unr = new_unrhdr(1, INT_MAX, NULL);
  415         kaio_zone = uma_zcreate("AIO", sizeof(struct kaioinfo), NULL, NULL,
  416             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  417         aiop_zone = uma_zcreate("AIOP", sizeof(struct aioproc), NULL,
  418             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  419         aiocb_zone = uma_zcreate("AIOCB", sizeof(struct kaiocb), NULL, NULL,
  420             NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  421         aiolio_zone = uma_zcreate("AIOLIO", sizeof(struct aioliojob), NULL,
  422             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  423         aiod_lifetime = AIOD_LIFETIME_DEFAULT;
  424         jobrefid = 1;
  425         p31b_setcfg(CTL_P1003_1B_ASYNCHRONOUS_IO, _POSIX_ASYNCHRONOUS_IO);
  426         p31b_setcfg(CTL_P1003_1B_AIO_MAX, MAX_AIO_QUEUE);
  427         p31b_setcfg(CTL_P1003_1B_AIO_PRIO_DELTA_MAX, 0);
  428 
  429         return (0);
  430 }
  431 
  432 /*
  433  * Init the per-process aioinfo structure.  The aioinfo limits are set
  434  * per-process for user limit (resource) management.
  435  */
  436 void
  437 aio_init_aioinfo(struct proc *p)
  438 {
  439         struct kaioinfo *ki;
  440 
  441         ki = uma_zalloc(kaio_zone, M_WAITOK);
  442         mtx_init(&ki->kaio_mtx, "aiomtx", NULL, MTX_DEF | MTX_NEW);
  443         ki->kaio_flags = 0;
  444         ki->kaio_active_count = 0;
  445         ki->kaio_count = 0;
  446         ki->kaio_buffer_count = 0;
  447         TAILQ_INIT(&ki->kaio_all);
  448         TAILQ_INIT(&ki->kaio_done);
  449         TAILQ_INIT(&ki->kaio_jobqueue);
  450         TAILQ_INIT(&ki->kaio_liojoblist);
  451         TAILQ_INIT(&ki->kaio_syncqueue);
  452         TAILQ_INIT(&ki->kaio_syncready);
  453         TASK_INIT(&ki->kaio_task, 0, aio_kick_helper, p);
  454         TASK_INIT(&ki->kaio_sync_task, 0, aio_schedule_fsync, ki);
  455         PROC_LOCK(p);
  456         if (p->p_aioinfo == NULL) {
  457                 p->p_aioinfo = ki;
  458                 PROC_UNLOCK(p);
  459         } else {
  460                 PROC_UNLOCK(p);
  461                 mtx_destroy(&ki->kaio_mtx);
  462                 uma_zfree(kaio_zone, ki);
  463         }
  464 
  465         while (num_aio_procs < MIN(target_aio_procs, max_aio_procs))
  466                 aio_newproc(NULL);
  467 }
  468 
  469 static int
  470 aio_sendsig(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi, bool ext)
  471 {
  472         struct thread *td;
  473         int error;
  474 
  475         error = sigev_findtd(p, sigev, &td);
  476         if (error)
  477                 return (error);
  478         if (!KSI_ONQ(ksi)) {
  479                 ksiginfo_set_sigev(ksi, sigev);
  480                 ksi->ksi_code = SI_ASYNCIO;
  481                 ksi->ksi_flags |= ext ? (KSI_EXT | KSI_INS) : 0;
  482                 tdsendsignal(p, td, ksi->ksi_signo, ksi);
  483         }
  484         PROC_UNLOCK(p);
  485         return (error);
  486 }
  487 
  488 /*
  489  * Free a job entry.  Wait for completion if it is currently active, but don't
  490  * delay forever.  If we delay, we return a flag that says that we have to
  491  * restart the queue scan.
  492  */
  493 static int
  494 aio_free_entry(struct kaiocb *job)
  495 {
  496         struct kaioinfo *ki;
  497         struct aioliojob *lj;
  498         struct proc *p;
  499 
  500         p = job->userproc;
  501         MPASS(curproc == p);
  502         ki = p->p_aioinfo;
  503         MPASS(ki != NULL);
  504 
  505         AIO_LOCK_ASSERT(ki, MA_OWNED);
  506         MPASS(job->jobflags & KAIOCB_FINISHED);
  507 
  508         atomic_subtract_int(&num_queue_count, 1);
  509 
  510         ki->kaio_count--;
  511         MPASS(ki->kaio_count >= 0);
  512 
  513         TAILQ_REMOVE(&ki->kaio_done, job, plist);
  514         TAILQ_REMOVE(&ki->kaio_all, job, allist);
  515 
  516         lj = job->lio;
  517         if (lj) {
  518                 lj->lioj_count--;
  519                 lj->lioj_finished_count--;
  520 
  521                 if (lj->lioj_count == 0) {
  522                         TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
  523                         /* lio is going away, we need to destroy any knotes */
  524                         knlist_delete(&lj->klist, curthread, 1);
  525                         PROC_LOCK(p);
  526                         sigqueue_take(&lj->lioj_ksi);
  527                         PROC_UNLOCK(p);
  528                         uma_zfree(aiolio_zone, lj);
  529                 }
  530         }
  531 
  532         /* job is going away, we need to destroy any knotes */
  533         knlist_delete(&job->klist, curthread, 1);
  534         PROC_LOCK(p);
  535         sigqueue_take(&job->ksi);
  536         PROC_UNLOCK(p);
  537 
  538         AIO_UNLOCK(ki);
  539 
  540         /*
  541          * The thread argument here is used to find the owning process
  542          * and is also passed to fo_close() which may pass it to various
  543          * places such as devsw close() routines.  Because of that, we
  544          * need a thread pointer from the process owning the job that is
  545          * persistent and won't disappear out from under us or move to
  546          * another process.
  547          *
  548          * Currently, all the callers of this function call it to remove
  549          * a kaiocb from the current process' job list either via a
  550          * syscall or due to the current process calling exit() or
  551          * execve().  Thus, we know that p == curproc.  We also know that
  552          * curthread can't exit since we are curthread.
  553          *
  554          * Therefore, we use curthread as the thread to pass to
  555          * knlist_delete().  This does mean that it is possible for the
  556          * thread pointer at close time to differ from the thread pointer
  557          * at open time, but this is already true of file descriptors in
  558          * a multithreaded process.
  559          */
  560         if (job->fd_file)
  561                 fdrop(job->fd_file, curthread);
  562         crfree(job->cred);
  563         if (job->uiop != &job->uio)
  564                 free(job->uiop, M_IOV);
  565         uma_zfree(aiocb_zone, job);
  566         AIO_LOCK(ki);
  567 
  568         return (0);
  569 }
  570 
  571 static void
  572 aio_proc_rundown_exec(void *arg, struct proc *p,
  573     struct image_params *imgp __unused)
  574 {
  575         aio_proc_rundown(arg, p);
  576 }
  577 
  578 static int
  579 aio_cancel_job(struct proc *p, struct kaioinfo *ki, struct kaiocb *job)
  580 {
  581         aio_cancel_fn_t *func;
  582         int cancelled;
  583 
  584         AIO_LOCK_ASSERT(ki, MA_OWNED);
  585         if (job->jobflags & (KAIOCB_CANCELLED | KAIOCB_FINISHED))
  586                 return (0);
  587         MPASS((job->jobflags & KAIOCB_CANCELLING) == 0);
  588         job->jobflags |= KAIOCB_CANCELLED;
  589 
  590         func = job->cancel_fn;
  591 
  592         /*
  593          * If there is no cancel routine, just leave the job marked as
  594          * cancelled.  The job should be in active use by a caller who
  595          * should complete it normally or when it fails to install a
  596          * cancel routine.
  597          */
  598         if (func == NULL)
  599                 return (0);
  600 
  601         /*
  602          * Set the CANCELLING flag so that aio_complete() will defer
  603          * completions of this job.  This prevents the job from being
  604          * freed out from under the cancel callback.  After the
  605          * callback any deferred completion (whether from the callback
  606          * or any other source) will be completed.
  607          */
  608         job->jobflags |= KAIOCB_CANCELLING;
  609         AIO_UNLOCK(ki);
  610         func(job);
  611         AIO_LOCK(ki);
  612         job->jobflags &= ~KAIOCB_CANCELLING;
  613         if (job->jobflags & KAIOCB_FINISHED) {
  614                 cancelled = job->uaiocb._aiocb_private.error == ECANCELED;
  615                 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
  616                 aio_bio_done_notify(p, job);
  617         } else {
  618                 /*
  619                  * The cancel callback might have scheduled an
  620                  * operation to cancel this request, but it is
  621                  * only counted as cancelled if the request is
  622                  * cancelled when the callback returns.
  623                  */
  624                 cancelled = 0;
  625         }
  626         return (cancelled);
  627 }
  628 
  629 /*
  630  * Rundown the jobs for a given process.
  631  */
  632 static void
  633 aio_proc_rundown(void *arg, struct proc *p)
  634 {
  635         struct kaioinfo *ki;
  636         struct aioliojob *lj;
  637         struct kaiocb *job, *jobn;
  638 
  639         KASSERT(curthread->td_proc == p,
  640             ("%s: called on non-curproc", __func__));
  641         ki = p->p_aioinfo;
  642         if (ki == NULL)
  643                 return;
  644 
  645         AIO_LOCK(ki);
  646         ki->kaio_flags |= KAIO_RUNDOWN;
  647 
  648 restart:
  649 
  650         /*
  651          * Try to cancel all pending requests. This code simulates
  652          * aio_cancel on all pending I/O requests.
  653          */
  654         TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
  655                 aio_cancel_job(p, ki, job);
  656         }
  657 
  658         /* Wait for all running I/O to be finished */
  659         if (TAILQ_FIRST(&ki->kaio_jobqueue) || ki->kaio_active_count != 0) {
  660                 ki->kaio_flags |= KAIO_WAKEUP;
  661                 msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO, "aioprn", hz);
  662                 goto restart;
  663         }
  664 
  665         /* Free all completed I/O requests. */
  666         while ((job = TAILQ_FIRST(&ki->kaio_done)) != NULL)
  667                 aio_free_entry(job);
  668 
  669         while ((lj = TAILQ_FIRST(&ki->kaio_liojoblist)) != NULL) {
  670                 if (lj->lioj_count == 0) {
  671                         TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
  672                         knlist_delete(&lj->klist, curthread, 1);
  673                         PROC_LOCK(p);
  674                         sigqueue_take(&lj->lioj_ksi);
  675                         PROC_UNLOCK(p);
  676                         uma_zfree(aiolio_zone, lj);
  677                 } else {
  678                         panic("LIO job not cleaned up: C:%d, FC:%d\n",
  679                             lj->lioj_count, lj->lioj_finished_count);
  680                 }
  681         }
  682         AIO_UNLOCK(ki);
  683         taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_task);
  684         taskqueue_drain(taskqueue_aiod_kick, &ki->kaio_sync_task);
  685         mtx_destroy(&ki->kaio_mtx);
  686         uma_zfree(kaio_zone, ki);
  687         p->p_aioinfo = NULL;
  688 }
  689 
  690 /*
  691  * Select a job to run (called by an AIO daemon).
  692  */
  693 static struct kaiocb *
  694 aio_selectjob(struct aioproc *aiop)
  695 {
  696         struct kaiocb *job;
  697         struct kaioinfo *ki;
  698         struct proc *userp;
  699 
  700         mtx_assert(&aio_job_mtx, MA_OWNED);
  701 restart:
  702         TAILQ_FOREACH(job, &aio_jobs, list) {
  703                 userp = job->userproc;
  704                 ki = userp->p_aioinfo;
  705 
  706                 if (ki->kaio_active_count < max_aio_per_proc) {
  707                         TAILQ_REMOVE(&aio_jobs, job, list);
  708                         if (!aio_clear_cancel_function(job))
  709                                 goto restart;
  710 
  711                         /* Account for currently active jobs. */
  712                         ki->kaio_active_count++;
  713                         break;
  714                 }
  715         }
  716         return (job);
  717 }
  718 
  719 /*
  720  * Move all data to a permanent storage device.  This code
  721  * simulates the fsync and fdatasync syscalls.
  722  */
  723 static int
  724 aio_fsync_vnode(struct thread *td, struct vnode *vp, int op)
  725 {
  726         struct mount *mp;
  727         vm_object_t obj;
  728         int error;
  729 
  730         for (;;) {
  731                 error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
  732                 if (error != 0)
  733                         break;
  734                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
  735                 obj = vp->v_object;
  736                 if (obj != NULL) {
  737                         VM_OBJECT_WLOCK(obj);
  738                         vm_object_page_clean(obj, 0, 0, 0);
  739                         VM_OBJECT_WUNLOCK(obj);
  740                 }
  741                 if (op == LIO_DSYNC)
  742                         error = VOP_FDATASYNC(vp, td);
  743                 else
  744                         error = VOP_FSYNC(vp, MNT_WAIT, td);
  745 
  746                 VOP_UNLOCK(vp);
  747                 vn_finished_write(mp);
  748                 if (error != ERELOOKUP)
  749                         break;
  750         }
  751         return (error);
  752 }
  753 
  754 /*
  755  * The AIO processing activity for LIO_READ/LIO_WRITE.  This is the code that
  756  * does the I/O request for the non-bio version of the operations.  The normal
  757  * vn operations are used, and this code should work in all instances for every
  758  * type of file, including pipes, sockets, fifos, and regular files.
  759  *
  760  * XXX I don't think it works well for socket, pipe, and fifo.
  761  */
  762 static void
  763 aio_process_rw(struct kaiocb *job)
  764 {
  765         struct ucred *td_savedcred;
  766         struct thread *td;
  767         struct aiocb *cb;
  768         struct file *fp;
  769         ssize_t cnt;
  770         long msgsnd_st, msgsnd_end;
  771         long msgrcv_st, msgrcv_end;
  772         long oublock_st, oublock_end;
  773         long inblock_st, inblock_end;
  774         int error, opcode;
  775 
  776         KASSERT(job->uaiocb.aio_lio_opcode == LIO_READ ||
  777             job->uaiocb.aio_lio_opcode == LIO_READV ||
  778             job->uaiocb.aio_lio_opcode == LIO_WRITE ||
  779             job->uaiocb.aio_lio_opcode == LIO_WRITEV,
  780             ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
  781 
  782         aio_switch_vmspace(job);
  783         td = curthread;
  784         td_savedcred = td->td_ucred;
  785         td->td_ucred = job->cred;
  786         job->uiop->uio_td = td;
  787         cb = &job->uaiocb;
  788         fp = job->fd_file;
  789 
  790         opcode = job->uaiocb.aio_lio_opcode;
  791         cnt = job->uiop->uio_resid;
  792 
  793         msgrcv_st = td->td_ru.ru_msgrcv;
  794         msgsnd_st = td->td_ru.ru_msgsnd;
  795         inblock_st = td->td_ru.ru_inblock;
  796         oublock_st = td->td_ru.ru_oublock;
  797 
  798         /*
  799          * aio_aqueue() acquires a reference to the file that is
  800          * released in aio_free_entry().
  801          */
  802         if (opcode == LIO_READ || opcode == LIO_READV) {
  803                 if (job->uiop->uio_resid == 0)
  804                         error = 0;
  805                 else
  806                         error = fo_read(fp, job->uiop, fp->f_cred, FOF_OFFSET,
  807                             td);
  808         } else {
  809                 if (fp->f_type == DTYPE_VNODE)
  810                         bwillwrite();
  811                 error = fo_write(fp, job->uiop, fp->f_cred, FOF_OFFSET, td);
  812         }
  813         msgrcv_end = td->td_ru.ru_msgrcv;
  814         msgsnd_end = td->td_ru.ru_msgsnd;
  815         inblock_end = td->td_ru.ru_inblock;
  816         oublock_end = td->td_ru.ru_oublock;
  817 
  818         job->msgrcv = msgrcv_end - msgrcv_st;
  819         job->msgsnd = msgsnd_end - msgsnd_st;
  820         job->inblock = inblock_end - inblock_st;
  821         job->outblock = oublock_end - oublock_st;
  822 
  823         if (error != 0 && job->uiop->uio_resid != cnt) {
  824                 if (error == ERESTART || error == EINTR || error == EWOULDBLOCK)
  825                         error = 0;
  826                 if (error == EPIPE && (opcode & LIO_WRITE)) {
  827                         PROC_LOCK(job->userproc);
  828                         kern_psignal(job->userproc, SIGPIPE);
  829                         PROC_UNLOCK(job->userproc);
  830                 }
  831         }
  832 
  833         cnt -= job->uiop->uio_resid;
  834         td->td_ucred = td_savedcred;
  835         if (error)
  836                 aio_complete(job, -1, error);
  837         else
  838                 aio_complete(job, cnt, 0);
  839 }
  840 
  841 static void
  842 aio_process_sync(struct kaiocb *job)
  843 {
  844         struct thread *td = curthread;
  845         struct ucred *td_savedcred = td->td_ucred;
  846         struct file *fp = job->fd_file;
  847         int error = 0;
  848 
  849         KASSERT(job->uaiocb.aio_lio_opcode & LIO_SYNC,
  850             ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
  851 
  852         td->td_ucred = job->cred;
  853         if (fp->f_vnode != NULL) {
  854                 error = aio_fsync_vnode(td, fp->f_vnode,
  855                     job->uaiocb.aio_lio_opcode);
  856         }
  857         td->td_ucred = td_savedcred;
  858         if (error)
  859                 aio_complete(job, -1, error);
  860         else
  861                 aio_complete(job, 0, 0);
  862 }
  863 
  864 static void
  865 aio_process_mlock(struct kaiocb *job)
  866 {
  867         struct aiocb *cb = &job->uaiocb;
  868         int error;
  869 
  870         KASSERT(job->uaiocb.aio_lio_opcode == LIO_MLOCK,
  871             ("%s: opcode %d", __func__, job->uaiocb.aio_lio_opcode));
  872 
  873         aio_switch_vmspace(job);
  874         error = kern_mlock(job->userproc, job->cred,
  875             __DEVOLATILE(uintptr_t, cb->aio_buf), cb->aio_nbytes);
  876         aio_complete(job, error != 0 ? -1 : 0, error);
  877 }
  878 
  879 static void
  880 aio_bio_done_notify(struct proc *userp, struct kaiocb *job)
  881 {
  882         struct aioliojob *lj;
  883         struct kaioinfo *ki;
  884         struct kaiocb *sjob, *sjobn;
  885         int lj_done;
  886         bool schedule_fsync;
  887 
  888         ki = userp->p_aioinfo;
  889         AIO_LOCK_ASSERT(ki, MA_OWNED);
  890         lj = job->lio;
  891         lj_done = 0;
  892         if (lj) {
  893                 lj->lioj_finished_count++;
  894                 if (lj->lioj_count == lj->lioj_finished_count)
  895                         lj_done = 1;
  896         }
  897         TAILQ_INSERT_TAIL(&ki->kaio_done, job, plist);
  898         MPASS(job->jobflags & KAIOCB_FINISHED);
  899 
  900         if (ki->kaio_flags & KAIO_RUNDOWN)
  901                 goto notification_done;
  902 
  903         if (job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
  904             job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID)
  905                 aio_sendsig(userp, &job->uaiocb.aio_sigevent, &job->ksi, true);
  906 
  907         KNOTE_LOCKED(&job->klist, 1);
  908 
  909         if (lj_done) {
  910                 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
  911                         lj->lioj_flags |= LIOJ_KEVENT_POSTED;
  912                         KNOTE_LOCKED(&lj->klist, 1);
  913                 }
  914                 if ((lj->lioj_flags & (LIOJ_SIGNAL | LIOJ_SIGNAL_POSTED))
  915                     == LIOJ_SIGNAL &&
  916                     (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
  917                     lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
  918                         aio_sendsig(userp, &lj->lioj_signal, &lj->lioj_ksi,
  919                             true);
  920                         lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
  921                 }
  922         }
  923 
  924 notification_done:
  925         if (job->jobflags & KAIOCB_CHECKSYNC) {
  926                 schedule_fsync = false;
  927                 TAILQ_FOREACH_SAFE(sjob, &ki->kaio_syncqueue, list, sjobn) {
  928                         if (job->fd_file != sjob->fd_file ||
  929                             job->seqno >= sjob->seqno)
  930                                 continue;
  931                         if (--sjob->pending > 0)
  932                                 continue;
  933                         TAILQ_REMOVE(&ki->kaio_syncqueue, sjob, list);
  934                         if (!aio_clear_cancel_function_locked(sjob))
  935                                 continue;
  936                         TAILQ_INSERT_TAIL(&ki->kaio_syncready, sjob, list);
  937                         schedule_fsync = true;
  938                 }
  939                 if (schedule_fsync)
  940                         taskqueue_enqueue(taskqueue_aiod_kick,
  941                             &ki->kaio_sync_task);
  942         }
  943         if (ki->kaio_flags & KAIO_WAKEUP) {
  944                 ki->kaio_flags &= ~KAIO_WAKEUP;
  945                 wakeup(&userp->p_aioinfo);
  946         }
  947 }
  948 
  949 static void
  950 aio_schedule_fsync(void *context, int pending)
  951 {
  952         struct kaioinfo *ki;
  953         struct kaiocb *job;
  954 
  955         ki = context;
  956         AIO_LOCK(ki);
  957         while (!TAILQ_EMPTY(&ki->kaio_syncready)) {
  958                 job = TAILQ_FIRST(&ki->kaio_syncready);
  959                 TAILQ_REMOVE(&ki->kaio_syncready, job, list);
  960                 AIO_UNLOCK(ki);
  961                 aio_schedule(job, aio_process_sync);
  962                 AIO_LOCK(ki);
  963         }
  964         AIO_UNLOCK(ki);
  965 }
  966 
  967 bool
  968 aio_cancel_cleared(struct kaiocb *job)
  969 {
  970 
  971         /*
  972          * The caller should hold the same queue lock held when
  973          * aio_clear_cancel_function() was called and set this flag
  974          * ensuring this check sees an up-to-date value.  However,
  975          * there is no way to assert that.
  976          */
  977         return ((job->jobflags & KAIOCB_CLEARED) != 0);
  978 }
  979 
  980 static bool
  981 aio_clear_cancel_function_locked(struct kaiocb *job)
  982 {
  983 
  984         AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
  985         MPASS(job->cancel_fn != NULL);
  986         if (job->jobflags & KAIOCB_CANCELLING) {
  987                 job->jobflags |= KAIOCB_CLEARED;
  988                 return (false);
  989         }
  990         job->cancel_fn = NULL;
  991         return (true);
  992 }
  993 
  994 bool
  995 aio_clear_cancel_function(struct kaiocb *job)
  996 {
  997         struct kaioinfo *ki;
  998         bool ret;
  999 
 1000         ki = job->userproc->p_aioinfo;
 1001         AIO_LOCK(ki);
 1002         ret = aio_clear_cancel_function_locked(job);
 1003         AIO_UNLOCK(ki);
 1004         return (ret);
 1005 }
 1006 
 1007 static bool
 1008 aio_set_cancel_function_locked(struct kaiocb *job, aio_cancel_fn_t *func)
 1009 {
 1010 
 1011         AIO_LOCK_ASSERT(job->userproc->p_aioinfo, MA_OWNED);
 1012         if (job->jobflags & KAIOCB_CANCELLED)
 1013                 return (false);
 1014         job->cancel_fn = func;
 1015         return (true);
 1016 }
 1017 
 1018 bool
 1019 aio_set_cancel_function(struct kaiocb *job, aio_cancel_fn_t *func)
 1020 {
 1021         struct kaioinfo *ki;
 1022         bool ret;
 1023 
 1024         ki = job->userproc->p_aioinfo;
 1025         AIO_LOCK(ki);
 1026         ret = aio_set_cancel_function_locked(job, func);
 1027         AIO_UNLOCK(ki);
 1028         return (ret);
 1029 }
 1030 
 1031 void
 1032 aio_complete(struct kaiocb *job, long status, int error)
 1033 {
 1034         struct kaioinfo *ki;
 1035         struct proc *userp;
 1036 
 1037         job->uaiocb._aiocb_private.error = error;
 1038         job->uaiocb._aiocb_private.status = status;
 1039 
 1040         userp = job->userproc;
 1041         ki = userp->p_aioinfo;
 1042 
 1043         AIO_LOCK(ki);
 1044         KASSERT(!(job->jobflags & KAIOCB_FINISHED),
 1045             ("duplicate aio_complete"));
 1046         job->jobflags |= KAIOCB_FINISHED;
 1047         if ((job->jobflags & (KAIOCB_QUEUEING | KAIOCB_CANCELLING)) == 0) {
 1048                 TAILQ_REMOVE(&ki->kaio_jobqueue, job, plist);
 1049                 aio_bio_done_notify(userp, job);
 1050         }
 1051         AIO_UNLOCK(ki);
 1052 }
 1053 
 1054 void
 1055 aio_cancel(struct kaiocb *job)
 1056 {
 1057 
 1058         aio_complete(job, -1, ECANCELED);
 1059 }
 1060 
 1061 void
 1062 aio_switch_vmspace(struct kaiocb *job)
 1063 {
 1064 
 1065         vmspace_switch_aio(job->userproc->p_vmspace);
 1066 }
 1067 
 1068 /*
 1069  * The AIO daemon, most of the actual work is done in aio_process_*,
 1070  * but the setup (and address space mgmt) is done in this routine.
 1071  */
 1072 static void
 1073 aio_daemon(void *_id)
 1074 {
 1075         struct kaiocb *job;
 1076         struct aioproc *aiop;
 1077         struct kaioinfo *ki;
 1078         struct proc *p;
 1079         struct vmspace *myvm;
 1080         struct thread *td = curthread;
 1081         int id = (intptr_t)_id;
 1082 
 1083         /*
 1084          * Grab an extra reference on the daemon's vmspace so that it
 1085          * doesn't get freed by jobs that switch to a different
 1086          * vmspace.
 1087          */
 1088         p = td->td_proc;
 1089         myvm = vmspace_acquire_ref(p);
 1090 
 1091         KASSERT(p->p_textvp == NULL, ("kthread has a textvp"));
 1092 
 1093         /*
 1094          * Allocate and ready the aio control info.  There is one aiop structure
 1095          * per daemon.
 1096          */
 1097         aiop = uma_zalloc(aiop_zone, M_WAITOK);
 1098         aiop->aioproc = p;
 1099         aiop->aioprocflags = 0;
 1100 
 1101         /*
 1102          * Wakeup parent process.  (Parent sleeps to keep from blasting away
 1103          * and creating too many daemons.)
 1104          */
 1105         sema_post(&aio_newproc_sem);
 1106 
 1107         mtx_lock(&aio_job_mtx);
 1108         for (;;) {
 1109                 /*
 1110                  * Take daemon off of free queue
 1111                  */
 1112                 if (aiop->aioprocflags & AIOP_FREE) {
 1113                         TAILQ_REMOVE(&aio_freeproc, aiop, list);
 1114                         aiop->aioprocflags &= ~AIOP_FREE;
 1115                 }
 1116 
 1117                 /*
 1118                  * Check for jobs.
 1119                  */
 1120                 while ((job = aio_selectjob(aiop)) != NULL) {
 1121                         mtx_unlock(&aio_job_mtx);
 1122 
 1123                         ki = job->userproc->p_aioinfo;
 1124                         job->handle_fn(job);
 1125 
 1126                         mtx_lock(&aio_job_mtx);
 1127                         /* Decrement the active job count. */
 1128                         ki->kaio_active_count--;
 1129                 }
 1130 
 1131                 /*
 1132                  * Disconnect from user address space.
 1133                  */
 1134                 if (p->p_vmspace != myvm) {
 1135                         mtx_unlock(&aio_job_mtx);
 1136                         vmspace_switch_aio(myvm);
 1137                         mtx_lock(&aio_job_mtx);
 1138                         /*
 1139                          * We have to restart to avoid race, we only sleep if
 1140                          * no job can be selected.
 1141                          */
 1142                         continue;
 1143                 }
 1144 
 1145                 mtx_assert(&aio_job_mtx, MA_OWNED);
 1146 
 1147                 TAILQ_INSERT_HEAD(&aio_freeproc, aiop, list);
 1148                 aiop->aioprocflags |= AIOP_FREE;
 1149 
 1150                 /*
 1151                  * If daemon is inactive for a long time, allow it to exit,
 1152                  * thereby freeing resources.
 1153                  */
 1154                 if (msleep(p, &aio_job_mtx, PRIBIO, "aiordy",
 1155                     aiod_lifetime) == EWOULDBLOCK && TAILQ_EMPTY(&aio_jobs) &&
 1156                     (aiop->aioprocflags & AIOP_FREE) &&
 1157                     num_aio_procs > target_aio_procs)
 1158                         break;
 1159         }
 1160         TAILQ_REMOVE(&aio_freeproc, aiop, list);
 1161         num_aio_procs--;
 1162         mtx_unlock(&aio_job_mtx);
 1163         uma_zfree(aiop_zone, aiop);
 1164         free_unr(aiod_unr, id);
 1165         vmspace_free(myvm);
 1166 
 1167         KASSERT(p->p_vmspace == myvm,
 1168             ("AIOD: bad vmspace for exiting daemon"));
 1169         KASSERT(refcount_load(&myvm->vm_refcnt) > 1,
 1170             ("AIOD: bad vm refcnt for exiting daemon: %d",
 1171             refcount_load(&myvm->vm_refcnt)));
 1172         kproc_exit(0);
 1173 }
 1174 
 1175 /*
 1176  * Create a new AIO daemon. This is mostly a kernel-thread fork routine. The
 1177  * AIO daemon modifies its environment itself.
 1178  */
 1179 static int
 1180 aio_newproc(int *start)
 1181 {
 1182         int error;
 1183         struct proc *p;
 1184         int id;
 1185 
 1186         id = alloc_unr(aiod_unr);
 1187         error = kproc_create(aio_daemon, (void *)(intptr_t)id, &p,
 1188                 RFNOWAIT, 0, "aiod%d", id);
 1189         if (error == 0) {
 1190                 /*
 1191                  * Wait until daemon is started.
 1192                  */
 1193                 sema_wait(&aio_newproc_sem);
 1194                 mtx_lock(&aio_job_mtx);
 1195                 num_aio_procs++;
 1196                 if (start != NULL)
 1197                         (*start)--;
 1198                 mtx_unlock(&aio_job_mtx);
 1199         } else {
 1200                 free_unr(aiod_unr, id);
 1201         }
 1202         return (error);
 1203 }
 1204 
 1205 /*
 1206  * Try the high-performance, low-overhead bio method for eligible
 1207  * VCHR devices.  This method doesn't use an aio helper thread, and
 1208  * thus has very low overhead.
 1209  *
 1210  * Assumes that the caller, aio_aqueue(), has incremented the file
 1211  * structure's reference count, preventing its deallocation for the
 1212  * duration of this call.
 1213  */
 1214 static int
 1215 aio_qbio(struct proc *p, struct kaiocb *job)
 1216 {
 1217         struct aiocb *cb;
 1218         struct file *fp;
 1219         struct buf *pbuf;
 1220         struct vnode *vp;
 1221         struct cdevsw *csw;
 1222         struct cdev *dev;
 1223         struct kaioinfo *ki;
 1224         struct bio **bios = NULL;
 1225         off_t offset;
 1226         int bio_cmd, error, i, iovcnt, opcode, poff, ref;
 1227         vm_prot_t prot;
 1228         bool use_unmapped;
 1229 
 1230         cb = &job->uaiocb;
 1231         fp = job->fd_file;
 1232         opcode = cb->aio_lio_opcode;
 1233 
 1234         if (!(opcode == LIO_WRITE || opcode == LIO_WRITEV ||
 1235             opcode == LIO_READ || opcode == LIO_READV))
 1236                 return (-1);
 1237         if (fp == NULL || fp->f_type != DTYPE_VNODE)
 1238                 return (-1);
 1239 
 1240         vp = fp->f_vnode;
 1241         if (vp->v_type != VCHR)
 1242                 return (-1);
 1243         if (vp->v_bufobj.bo_bsize == 0)
 1244                 return (-1);
 1245 
 1246         bio_cmd = (opcode & LIO_WRITE) ? BIO_WRITE : BIO_READ;
 1247         iovcnt = job->uiop->uio_iovcnt;
 1248         if (iovcnt > max_buf_aio)
 1249                 return (-1);
 1250         for (i = 0; i < iovcnt; i++) {
 1251                 if (job->uiop->uio_iov[i].iov_len % vp->v_bufobj.bo_bsize != 0)
 1252                         return (-1);
 1253                 if (job->uiop->uio_iov[i].iov_len > maxphys) {
 1254                         error = -1;
 1255                         return (-1);
 1256                 }
 1257         }
 1258         offset = cb->aio_offset;
 1259 
 1260         ref = 0;
 1261         csw = devvn_refthread(vp, &dev, &ref);
 1262         if (csw == NULL)
 1263                 return (ENXIO);
 1264 
 1265         if ((csw->d_flags & D_DISK) == 0) {
 1266                 error = -1;
 1267                 goto unref;
 1268         }
 1269         if (job->uiop->uio_resid > dev->si_iosize_max) {
 1270                 error = -1;
 1271                 goto unref;
 1272         }
 1273 
 1274         ki = p->p_aioinfo;
 1275         job->error = 0;
 1276 
 1277         use_unmapped = (dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed;
 1278         if (!use_unmapped) {
 1279                 AIO_LOCK(ki);
 1280                 if (ki->kaio_buffer_count + iovcnt > max_buf_aio) {
 1281                         AIO_UNLOCK(ki);
 1282                         error = EAGAIN;
 1283                         goto unref;
 1284                 }
 1285                 ki->kaio_buffer_count += iovcnt;
 1286                 AIO_UNLOCK(ki);
 1287         }
 1288 
 1289         bios = malloc(sizeof(struct bio *) * iovcnt, M_TEMP, M_WAITOK);
 1290         atomic_store_int(&job->nbio, iovcnt);
 1291         for (i = 0; i < iovcnt; i++) {
 1292                 struct vm_page** pages;
 1293                 struct bio *bp;
 1294                 void *buf;
 1295                 size_t nbytes;
 1296                 int npages;
 1297 
 1298                 buf = job->uiop->uio_iov[i].iov_base;
 1299                 nbytes = job->uiop->uio_iov[i].iov_len;
 1300 
 1301                 bios[i] = g_alloc_bio();
 1302                 bp = bios[i];
 1303 
 1304                 poff = (vm_offset_t)buf & PAGE_MASK;
 1305                 if (use_unmapped) {
 1306                         pbuf = NULL;
 1307                         pages = malloc(sizeof(vm_page_t) * (atop(round_page(
 1308                             nbytes)) + 1), M_TEMP, M_WAITOK | M_ZERO);
 1309                 } else {
 1310                         pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
 1311                         BUF_KERNPROC(pbuf);
 1312                         pages = pbuf->b_pages;
 1313                 }
 1314 
 1315                 bp->bio_length = nbytes;
 1316                 bp->bio_bcount = nbytes;
 1317                 bp->bio_done = aio_biowakeup;
 1318                 bp->bio_offset = offset;
 1319                 bp->bio_cmd = bio_cmd;
 1320                 bp->bio_dev = dev;
 1321                 bp->bio_caller1 = job;
 1322                 bp->bio_caller2 = pbuf;
 1323 
 1324                 prot = VM_PROT_READ;
 1325                 if (opcode == LIO_READ || opcode == LIO_READV)
 1326                         prot |= VM_PROT_WRITE;  /* Less backwards than it looks */
 1327                 npages = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
 1328                     (vm_offset_t)buf, bp->bio_length, prot, pages,
 1329                     atop(maxphys) + 1);
 1330                 if (npages < 0) {
 1331                         if (pbuf != NULL)
 1332                                 uma_zfree(pbuf_zone, pbuf);
 1333                         else
 1334                                 free(pages, M_TEMP);
 1335                         error = EFAULT;
 1336                         g_destroy_bio(bp);
 1337                         i--;
 1338                         goto destroy_bios;
 1339                 }
 1340                 if (pbuf != NULL) {
 1341                         pmap_qenter((vm_offset_t)pbuf->b_data, pages, npages);
 1342                         bp->bio_data = pbuf->b_data + poff;
 1343                         pbuf->b_npages = npages;
 1344                         atomic_add_int(&num_buf_aio, 1);
 1345                 } else {
 1346                         bp->bio_ma = pages;
 1347                         bp->bio_ma_n = npages;
 1348                         bp->bio_ma_offset = poff;
 1349                         bp->bio_data = unmapped_buf;
 1350                         bp->bio_flags |= BIO_UNMAPPED;
 1351                         atomic_add_int(&num_unmapped_aio, 1);
 1352                 }
 1353 
 1354                 offset += nbytes;
 1355         }
 1356 
 1357         /* Perform transfer. */
 1358         for (i = 0; i < iovcnt; i++)
 1359                 csw->d_strategy(bios[i]);
 1360         free(bios, M_TEMP);
 1361 
 1362         dev_relthread(dev, ref);
 1363         return (0);
 1364 
 1365 destroy_bios:
 1366         for (; i >= 0; i--)
 1367                 aio_biocleanup(bios[i]);
 1368         free(bios, M_TEMP);
 1369 unref:
 1370         dev_relthread(dev, ref);
 1371         return (error);
 1372 }
 1373 
 1374 #ifdef COMPAT_FREEBSD6
 1375 static int
 1376 convert_old_sigevent(struct osigevent *osig, struct sigevent *nsig)
 1377 {
 1378 
 1379         /*
 1380          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
 1381          * supported by AIO with the old sigevent structure.
 1382          */
 1383         nsig->sigev_notify = osig->sigev_notify;
 1384         switch (nsig->sigev_notify) {
 1385         case SIGEV_NONE:
 1386                 break;
 1387         case SIGEV_SIGNAL:
 1388                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
 1389                 break;
 1390         case SIGEV_KEVENT:
 1391                 nsig->sigev_notify_kqueue =
 1392                     osig->__sigev_u.__sigev_notify_kqueue;
 1393                 nsig->sigev_value.sival_ptr = osig->sigev_value.sival_ptr;
 1394                 break;
 1395         default:
 1396                 return (EINVAL);
 1397         }
 1398         return (0);
 1399 }
 1400 
 1401 static int
 1402 aiocb_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
 1403     int type __unused)
 1404 {
 1405         struct oaiocb *ojob;
 1406         struct aiocb *kcb = &kjob->uaiocb;
 1407         int error;
 1408 
 1409         bzero(kcb, sizeof(struct aiocb));
 1410         error = copyin(ujob, kcb, sizeof(struct oaiocb));
 1411         if (error)
 1412                 return (error);
 1413         /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
 1414         ojob = (struct oaiocb *)kcb;
 1415         return (convert_old_sigevent(&ojob->aio_sigevent, &kcb->aio_sigevent));
 1416 }
 1417 #endif
 1418 
 1419 static int
 1420 aiocb_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
 1421 {
 1422         struct aiocb *kcb = &kjob->uaiocb;
 1423         int error;
 1424 
 1425         error = copyin(ujob, kcb, sizeof(struct aiocb));
 1426         if (error)
 1427                 return (error);
 1428         if (type & LIO_VECTORED) {
 1429                 /* malloc a uio and copy in the iovec */
 1430                 error = copyinuio(__DEVOLATILE(struct iovec*, kcb->aio_iov),
 1431                     kcb->aio_iovcnt, &kjob->uiop);
 1432         }
 1433 
 1434         return (error);
 1435 }
 1436 
 1437 static long
 1438 aiocb_fetch_status(struct aiocb *ujob)
 1439 {
 1440 
 1441         return (fuword(&ujob->_aiocb_private.status));
 1442 }
 1443 
 1444 static long
 1445 aiocb_fetch_error(struct aiocb *ujob)
 1446 {
 1447 
 1448         return (fuword(&ujob->_aiocb_private.error));
 1449 }
 1450 
 1451 static int
 1452 aiocb_store_status(struct aiocb *ujob, long status)
 1453 {
 1454 
 1455         return (suword(&ujob->_aiocb_private.status, status));
 1456 }
 1457 
 1458 static int
 1459 aiocb_store_error(struct aiocb *ujob, long error)
 1460 {
 1461 
 1462         return (suword(&ujob->_aiocb_private.error, error));
 1463 }
 1464 
 1465 static int
 1466 aiocb_store_kernelinfo(struct aiocb *ujob, long jobref)
 1467 {
 1468 
 1469         return (suword(&ujob->_aiocb_private.kernelinfo, jobref));
 1470 }
 1471 
 1472 static int
 1473 aiocb_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
 1474 {
 1475 
 1476         return (suword(ujobp, (long)ujob));
 1477 }
 1478 
 1479 static struct aiocb_ops aiocb_ops = {
 1480         .aio_copyin = aiocb_copyin,
 1481         .fetch_status = aiocb_fetch_status,
 1482         .fetch_error = aiocb_fetch_error,
 1483         .store_status = aiocb_store_status,
 1484         .store_error = aiocb_store_error,
 1485         .store_kernelinfo = aiocb_store_kernelinfo,
 1486         .store_aiocb = aiocb_store_aiocb,
 1487 };
 1488 
 1489 #ifdef COMPAT_FREEBSD6
 1490 static struct aiocb_ops aiocb_ops_osigevent = {
 1491         .aio_copyin = aiocb_copyin_old_sigevent,
 1492         .fetch_status = aiocb_fetch_status,
 1493         .fetch_error = aiocb_fetch_error,
 1494         .store_status = aiocb_store_status,
 1495         .store_error = aiocb_store_error,
 1496         .store_kernelinfo = aiocb_store_kernelinfo,
 1497         .store_aiocb = aiocb_store_aiocb,
 1498 };
 1499 #endif
 1500 
 1501 /*
 1502  * Queue a new AIO request.  Choosing either the threaded or direct bio VCHR
 1503  * technique is done in this code.
 1504  */
 1505 int
 1506 aio_aqueue(struct thread *td, struct aiocb *ujob, struct aioliojob *lj,
 1507     int type, struct aiocb_ops *ops)
 1508 {
 1509         struct proc *p = td->td_proc;
 1510         struct file *fp = NULL;
 1511         struct kaiocb *job;
 1512         struct kaioinfo *ki;
 1513         struct kevent kev;
 1514         int opcode;
 1515         int error;
 1516         int fd, kqfd;
 1517         int jid;
 1518         u_short evflags;
 1519 
 1520         if (p->p_aioinfo == NULL)
 1521                 aio_init_aioinfo(p);
 1522 
 1523         ki = p->p_aioinfo;
 1524 
 1525         ops->store_status(ujob, -1);
 1526         ops->store_error(ujob, 0);
 1527         ops->store_kernelinfo(ujob, -1);
 1528 
 1529         if (num_queue_count >= max_queue_count ||
 1530             ki->kaio_count >= max_aio_queue_per_proc) {
 1531                 error = EAGAIN;
 1532                 goto err1;
 1533         }
 1534 
 1535         job = uma_zalloc(aiocb_zone, M_WAITOK | M_ZERO);
 1536         knlist_init_mtx(&job->klist, AIO_MTX(ki));
 1537 
 1538         error = ops->aio_copyin(ujob, job, type);
 1539         if (error)
 1540                 goto err2;
 1541 
 1542         if (job->uaiocb.aio_nbytes > IOSIZE_MAX) {
 1543                 error = EINVAL;
 1544                 goto err2;
 1545         }
 1546 
 1547         if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT &&
 1548             job->uaiocb.aio_sigevent.sigev_notify != SIGEV_SIGNAL &&
 1549             job->uaiocb.aio_sigevent.sigev_notify != SIGEV_THREAD_ID &&
 1550             job->uaiocb.aio_sigevent.sigev_notify != SIGEV_NONE) {
 1551                 error = EINVAL;
 1552                 goto err2;
 1553         }
 1554 
 1555         if ((job->uaiocb.aio_sigevent.sigev_notify == SIGEV_SIGNAL ||
 1556              job->uaiocb.aio_sigevent.sigev_notify == SIGEV_THREAD_ID) &&
 1557                 !_SIG_VALID(job->uaiocb.aio_sigevent.sigev_signo)) {
 1558                 error = EINVAL;
 1559                 goto err2;
 1560         }
 1561 
 1562         /* Get the opcode. */
 1563         if (type == LIO_NOP) {
 1564                 switch (job->uaiocb.aio_lio_opcode) {
 1565                 case LIO_WRITE:
 1566                 case LIO_NOP:
 1567                 case LIO_READ:
 1568                         opcode = job->uaiocb.aio_lio_opcode;
 1569                         break;
 1570                 default:
 1571                         error = EINVAL;
 1572                         goto err2;
 1573                 }
 1574         } else
 1575                 opcode = job->uaiocb.aio_lio_opcode = type;
 1576 
 1577         ksiginfo_init(&job->ksi);
 1578 
 1579         /* Save userspace address of the job info. */
 1580         job->ujob = ujob;
 1581 
 1582         /*
 1583          * Validate the opcode and fetch the file object for the specified
 1584          * file descriptor.
 1585          *
 1586          * XXXRW: Moved the opcode validation up here so that we don't
 1587          * retrieve a file descriptor without knowing what the capabiltity
 1588          * should be.
 1589          */
 1590         fd = job->uaiocb.aio_fildes;
 1591         switch (opcode) {
 1592         case LIO_WRITE:
 1593         case LIO_WRITEV:
 1594                 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
 1595                 break;
 1596         case LIO_READ:
 1597         case LIO_READV:
 1598                 error = fget_read(td, fd, &cap_pread_rights, &fp);
 1599                 break;
 1600         case LIO_SYNC:
 1601         case LIO_DSYNC:
 1602                 error = fget(td, fd, &cap_fsync_rights, &fp);
 1603                 break;
 1604         case LIO_MLOCK:
 1605                 break;
 1606         case LIO_NOP:
 1607                 error = fget(td, fd, &cap_no_rights, &fp);
 1608                 break;
 1609         default:
 1610                 error = EINVAL;
 1611         }
 1612         if (error)
 1613                 goto err3;
 1614 
 1615         if ((opcode & LIO_SYNC) && fp->f_vnode == NULL) {
 1616                 error = EINVAL;
 1617                 goto err3;
 1618         }
 1619 
 1620         if ((opcode == LIO_READ || opcode == LIO_READV ||
 1621             opcode == LIO_WRITE || opcode == LIO_WRITEV) &&
 1622             job->uaiocb.aio_offset < 0 &&
 1623             (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR)) {
 1624                 error = EINVAL;
 1625                 goto err3;
 1626         }
 1627 
 1628         if (fp != NULL && fp->f_ops == &path_fileops) {
 1629                 error = EBADF;
 1630                 goto err3;
 1631         }
 1632 
 1633         job->fd_file = fp;
 1634 
 1635         mtx_lock(&aio_job_mtx);
 1636         jid = jobrefid++;
 1637         job->seqno = jobseqno++;
 1638         mtx_unlock(&aio_job_mtx);
 1639         error = ops->store_kernelinfo(ujob, jid);
 1640         if (error) {
 1641                 error = EINVAL;
 1642                 goto err3;
 1643         }
 1644         job->uaiocb._aiocb_private.kernelinfo = (void *)(intptr_t)jid;
 1645 
 1646         if (opcode == LIO_NOP) {
 1647                 fdrop(fp, td);
 1648                 MPASS(job->uiop == &job->uio || job->uiop == NULL);
 1649                 uma_zfree(aiocb_zone, job);
 1650                 return (0);
 1651         }
 1652 
 1653         if (job->uaiocb.aio_sigevent.sigev_notify != SIGEV_KEVENT)
 1654                 goto no_kqueue;
 1655         evflags = job->uaiocb.aio_sigevent.sigev_notify_kevent_flags;
 1656         if ((evflags & ~(EV_CLEAR | EV_DISPATCH | EV_ONESHOT)) != 0) {
 1657                 error = EINVAL;
 1658                 goto err3;
 1659         }
 1660         kqfd = job->uaiocb.aio_sigevent.sigev_notify_kqueue;
 1661         memset(&kev, 0, sizeof(kev));
 1662         kev.ident = (uintptr_t)job->ujob;
 1663         kev.filter = EVFILT_AIO;
 1664         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1 | evflags;
 1665         kev.data = (intptr_t)job;
 1666         kev.udata = job->uaiocb.aio_sigevent.sigev_value.sival_ptr;
 1667         error = kqfd_register(kqfd, &kev, td, M_WAITOK);
 1668         if (error)
 1669                 goto err3;
 1670 
 1671 no_kqueue:
 1672 
 1673         ops->store_error(ujob, EINPROGRESS);
 1674         job->uaiocb._aiocb_private.error = EINPROGRESS;
 1675         job->userproc = p;
 1676         job->cred = crhold(td->td_ucred);
 1677         job->jobflags = KAIOCB_QUEUEING;
 1678         job->lio = lj;
 1679 
 1680         if (opcode & LIO_VECTORED) {
 1681                 /* Use the uio copied in by aio_copyin */
 1682                 MPASS(job->uiop != &job->uio && job->uiop != NULL);
 1683         } else {
 1684                 /* Setup the inline uio */
 1685                 job->iov[0].iov_base = (void *)(uintptr_t)job->uaiocb.aio_buf;
 1686                 job->iov[0].iov_len = job->uaiocb.aio_nbytes;
 1687                 job->uio.uio_iov = job->iov;
 1688                 job->uio.uio_iovcnt = 1;
 1689                 job->uio.uio_resid = job->uaiocb.aio_nbytes;
 1690                 job->uio.uio_segflg = UIO_USERSPACE;
 1691                 job->uiop = &job->uio;
 1692         }
 1693         switch (opcode & (LIO_READ | LIO_WRITE)) {
 1694         case LIO_READ:
 1695                 job->uiop->uio_rw = UIO_READ;
 1696                 break;
 1697         case LIO_WRITE:
 1698                 job->uiop->uio_rw = UIO_WRITE;
 1699                 break;
 1700         }
 1701         job->uiop->uio_offset = job->uaiocb.aio_offset;
 1702         job->uiop->uio_td = td;
 1703 
 1704         if (opcode == LIO_MLOCK) {
 1705                 aio_schedule(job, aio_process_mlock);
 1706                 error = 0;
 1707         } else if (fp->f_ops->fo_aio_queue == NULL)
 1708                 error = aio_queue_file(fp, job);
 1709         else
 1710                 error = fo_aio_queue(fp, job);
 1711         if (error)
 1712                 goto err4;
 1713 
 1714         AIO_LOCK(ki);
 1715         job->jobflags &= ~KAIOCB_QUEUEING;
 1716         TAILQ_INSERT_TAIL(&ki->kaio_all, job, allist);
 1717         ki->kaio_count++;
 1718         if (lj)
 1719                 lj->lioj_count++;
 1720         atomic_add_int(&num_queue_count, 1);
 1721         if (job->jobflags & KAIOCB_FINISHED) {
 1722                 /*
 1723                  * The queue callback completed the request synchronously.
 1724                  * The bulk of the completion is deferred in that case
 1725                  * until this point.
 1726                  */
 1727                 aio_bio_done_notify(p, job);
 1728         } else
 1729                 TAILQ_INSERT_TAIL(&ki->kaio_jobqueue, job, plist);
 1730         AIO_UNLOCK(ki);
 1731         return (0);
 1732 
 1733 err4:
 1734         crfree(job->cred);
 1735 err3:
 1736         if (fp)
 1737                 fdrop(fp, td);
 1738         knlist_delete(&job->klist, curthread, 0);
 1739 err2:
 1740         if (job->uiop != &job->uio)
 1741                 free(job->uiop, M_IOV);
 1742         uma_zfree(aiocb_zone, job);
 1743 err1:
 1744         ops->store_error(ujob, error);
 1745         return (error);
 1746 }
 1747 
 1748 static void
 1749 aio_cancel_daemon_job(struct kaiocb *job)
 1750 {
 1751 
 1752         mtx_lock(&aio_job_mtx);
 1753         if (!aio_cancel_cleared(job))
 1754                 TAILQ_REMOVE(&aio_jobs, job, list);
 1755         mtx_unlock(&aio_job_mtx);
 1756         aio_cancel(job);
 1757 }
 1758 
 1759 void
 1760 aio_schedule(struct kaiocb *job, aio_handle_fn_t *func)
 1761 {
 1762 
 1763         mtx_lock(&aio_job_mtx);
 1764         if (!aio_set_cancel_function(job, aio_cancel_daemon_job)) {
 1765                 mtx_unlock(&aio_job_mtx);
 1766                 aio_cancel(job);
 1767                 return;
 1768         }
 1769         job->handle_fn = func;
 1770         TAILQ_INSERT_TAIL(&aio_jobs, job, list);
 1771         aio_kick_nowait(job->userproc);
 1772         mtx_unlock(&aio_job_mtx);
 1773 }
 1774 
 1775 static void
 1776 aio_cancel_sync(struct kaiocb *job)
 1777 {
 1778         struct kaioinfo *ki;
 1779 
 1780         ki = job->userproc->p_aioinfo;
 1781         AIO_LOCK(ki);
 1782         if (!aio_cancel_cleared(job))
 1783                 TAILQ_REMOVE(&ki->kaio_syncqueue, job, list);
 1784         AIO_UNLOCK(ki);
 1785         aio_cancel(job);
 1786 }
 1787 
 1788 int
 1789 aio_queue_file(struct file *fp, struct kaiocb *job)
 1790 {
 1791         struct kaioinfo *ki;
 1792         struct kaiocb *job2;
 1793         struct vnode *vp;
 1794         struct mount *mp;
 1795         int error;
 1796         bool safe;
 1797 
 1798         ki = job->userproc->p_aioinfo;
 1799         error = aio_qbio(job->userproc, job);
 1800         if (error >= 0)
 1801                 return (error);
 1802         safe = false;
 1803         if (fp->f_type == DTYPE_VNODE) {
 1804                 vp = fp->f_vnode;
 1805                 if (vp->v_type == VREG || vp->v_type == VDIR) {
 1806                         mp = fp->f_vnode->v_mount;
 1807                         if (mp == NULL || (mp->mnt_flag & MNT_LOCAL) != 0)
 1808                                 safe = true;
 1809                 }
 1810         }
 1811         if (!(safe || enable_aio_unsafe)) {
 1812                 counted_warning(&unsafe_warningcnt,
 1813                     "is attempting to use unsafe AIO requests");
 1814                 return (EOPNOTSUPP);
 1815         }
 1816 
 1817         if (job->uaiocb.aio_lio_opcode & (LIO_WRITE | LIO_READ)) {
 1818                 aio_schedule(job, aio_process_rw);
 1819                 error = 0;
 1820         } else if (job->uaiocb.aio_lio_opcode & LIO_SYNC) {
 1821                 AIO_LOCK(ki);
 1822                 TAILQ_FOREACH(job2, &ki->kaio_jobqueue, plist) {
 1823                         if (job2->fd_file == job->fd_file &&
 1824                             ((job2->uaiocb.aio_lio_opcode & LIO_SYNC) == 0) &&
 1825                             job2->seqno < job->seqno) {
 1826                                 job2->jobflags |= KAIOCB_CHECKSYNC;
 1827                                 job->pending++;
 1828                         }
 1829                 }
 1830                 if (job->pending != 0) {
 1831                         if (!aio_set_cancel_function_locked(job,
 1832                                 aio_cancel_sync)) {
 1833                                 AIO_UNLOCK(ki);
 1834                                 aio_cancel(job);
 1835                                 return (0);
 1836                         }
 1837                         TAILQ_INSERT_TAIL(&ki->kaio_syncqueue, job, list);
 1838                         AIO_UNLOCK(ki);
 1839                         return (0);
 1840                 }
 1841                 AIO_UNLOCK(ki);
 1842                 aio_schedule(job, aio_process_sync);
 1843                 error = 0;
 1844         } else {
 1845                 error = EINVAL;
 1846         }
 1847         return (error);
 1848 }
 1849 
 1850 static void
 1851 aio_kick_nowait(struct proc *userp)
 1852 {
 1853         struct kaioinfo *ki = userp->p_aioinfo;
 1854         struct aioproc *aiop;
 1855 
 1856         mtx_assert(&aio_job_mtx, MA_OWNED);
 1857         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
 1858                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
 1859                 aiop->aioprocflags &= ~AIOP_FREE;
 1860                 wakeup(aiop->aioproc);
 1861         } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
 1862             ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
 1863                 taskqueue_enqueue(taskqueue_aiod_kick, &ki->kaio_task);
 1864         }
 1865 }
 1866 
 1867 static int
 1868 aio_kick(struct proc *userp)
 1869 {
 1870         struct kaioinfo *ki = userp->p_aioinfo;
 1871         struct aioproc *aiop;
 1872         int error, ret = 0;
 1873 
 1874         mtx_assert(&aio_job_mtx, MA_OWNED);
 1875 retryproc:
 1876         if ((aiop = TAILQ_FIRST(&aio_freeproc)) != NULL) {
 1877                 TAILQ_REMOVE(&aio_freeproc, aiop, list);
 1878                 aiop->aioprocflags &= ~AIOP_FREE;
 1879                 wakeup(aiop->aioproc);
 1880         } else if (num_aio_resv_start + num_aio_procs < max_aio_procs &&
 1881             ki->kaio_active_count + num_aio_resv_start < max_aio_per_proc) {
 1882                 num_aio_resv_start++;
 1883                 mtx_unlock(&aio_job_mtx);
 1884                 error = aio_newproc(&num_aio_resv_start);
 1885                 mtx_lock(&aio_job_mtx);
 1886                 if (error) {
 1887                         num_aio_resv_start--;
 1888                         goto retryproc;
 1889                 }
 1890         } else {
 1891                 ret = -1;
 1892         }
 1893         return (ret);
 1894 }
 1895 
 1896 static void
 1897 aio_kick_helper(void *context, int pending)
 1898 {
 1899         struct proc *userp = context;
 1900 
 1901         mtx_lock(&aio_job_mtx);
 1902         while (--pending >= 0) {
 1903                 if (aio_kick(userp))
 1904                         break;
 1905         }
 1906         mtx_unlock(&aio_job_mtx);
 1907 }
 1908 
 1909 /*
 1910  * Support the aio_return system call, as a side-effect, kernel resources are
 1911  * released.
 1912  */
 1913 static int
 1914 kern_aio_return(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
 1915 {
 1916         struct proc *p = td->td_proc;
 1917         struct kaiocb *job;
 1918         struct kaioinfo *ki;
 1919         long status, error;
 1920 
 1921         ki = p->p_aioinfo;
 1922         if (ki == NULL)
 1923                 return (EINVAL);
 1924         AIO_LOCK(ki);
 1925         TAILQ_FOREACH(job, &ki->kaio_done, plist) {
 1926                 if (job->ujob == ujob)
 1927                         break;
 1928         }
 1929         if (job != NULL) {
 1930                 MPASS(job->jobflags & KAIOCB_FINISHED);
 1931                 status = job->uaiocb._aiocb_private.status;
 1932                 error = job->uaiocb._aiocb_private.error;
 1933                 td->td_retval[0] = status;
 1934                 td->td_ru.ru_oublock += job->outblock;
 1935                 td->td_ru.ru_inblock += job->inblock;
 1936                 td->td_ru.ru_msgsnd += job->msgsnd;
 1937                 td->td_ru.ru_msgrcv += job->msgrcv;
 1938                 aio_free_entry(job);
 1939                 AIO_UNLOCK(ki);
 1940                 ops->store_error(ujob, error);
 1941                 ops->store_status(ujob, status);
 1942         } else {
 1943                 error = EINVAL;
 1944                 AIO_UNLOCK(ki);
 1945         }
 1946         return (error);
 1947 }
 1948 
 1949 int
 1950 sys_aio_return(struct thread *td, struct aio_return_args *uap)
 1951 {
 1952 
 1953         return (kern_aio_return(td, uap->aiocbp, &aiocb_ops));
 1954 }
 1955 
 1956 /*
 1957  * Allow a process to wakeup when any of the I/O requests are completed.
 1958  */
 1959 static int
 1960 kern_aio_suspend(struct thread *td, int njoblist, struct aiocb **ujoblist,
 1961     struct timespec *ts)
 1962 {
 1963         struct proc *p = td->td_proc;
 1964         struct timeval atv;
 1965         struct kaioinfo *ki;
 1966         struct kaiocb *firstjob, *job;
 1967         int error, i, timo;
 1968 
 1969         timo = 0;
 1970         if (ts) {
 1971                 if (ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000)
 1972                         return (EINVAL);
 1973 
 1974                 TIMESPEC_TO_TIMEVAL(&atv, ts);
 1975                 if (itimerfix(&atv))
 1976                         return (EINVAL);
 1977                 timo = tvtohz(&atv);
 1978         }
 1979 
 1980         ki = p->p_aioinfo;
 1981         if (ki == NULL)
 1982                 return (EAGAIN);
 1983 
 1984         if (njoblist == 0)
 1985                 return (0);
 1986 
 1987         AIO_LOCK(ki);
 1988         for (;;) {
 1989                 firstjob = NULL;
 1990                 error = 0;
 1991                 TAILQ_FOREACH(job, &ki->kaio_all, allist) {
 1992                         for (i = 0; i < njoblist; i++) {
 1993                                 if (job->ujob == ujoblist[i]) {
 1994                                         if (firstjob == NULL)
 1995                                                 firstjob = job;
 1996                                         if (job->jobflags & KAIOCB_FINISHED)
 1997                                                 goto RETURN;
 1998                                 }
 1999                         }
 2000                 }
 2001                 /* All tasks were finished. */
 2002                 if (firstjob == NULL)
 2003                         break;
 2004 
 2005                 ki->kaio_flags |= KAIO_WAKEUP;
 2006                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
 2007                     "aiospn", timo);
 2008                 if (error == ERESTART)
 2009                         error = EINTR;
 2010                 if (error)
 2011                         break;
 2012         }
 2013 RETURN:
 2014         AIO_UNLOCK(ki);
 2015         return (error);
 2016 }
 2017 
 2018 int
 2019 sys_aio_suspend(struct thread *td, struct aio_suspend_args *uap)
 2020 {
 2021         struct timespec ts, *tsp;
 2022         struct aiocb **ujoblist;
 2023         int error;
 2024 
 2025         if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
 2026                 return (EINVAL);
 2027 
 2028         if (uap->timeout) {
 2029                 /* Get timespec struct. */
 2030                 if ((error = copyin(uap->timeout, &ts, sizeof(ts))) != 0)
 2031                         return (error);
 2032                 tsp = &ts;
 2033         } else
 2034                 tsp = NULL;
 2035 
 2036         ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
 2037         error = copyin(uap->aiocbp, ujoblist, uap->nent * sizeof(ujoblist[0]));
 2038         if (error == 0)
 2039                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
 2040         free(ujoblist, M_AIOS);
 2041         return (error);
 2042 }
 2043 
 2044 /*
 2045  * aio_cancel cancels any non-bio aio operations not currently in progress.
 2046  */
 2047 int
 2048 sys_aio_cancel(struct thread *td, struct aio_cancel_args *uap)
 2049 {
 2050         struct proc *p = td->td_proc;
 2051         struct kaioinfo *ki;
 2052         struct kaiocb *job, *jobn;
 2053         struct file *fp;
 2054         int error;
 2055         int cancelled = 0;
 2056         int notcancelled = 0;
 2057         struct vnode *vp;
 2058 
 2059         /* Lookup file object. */
 2060         error = fget(td, uap->fd, &cap_no_rights, &fp);
 2061         if (error)
 2062                 return (error);
 2063 
 2064         ki = p->p_aioinfo;
 2065         if (ki == NULL)
 2066                 goto done;
 2067 
 2068         if (fp->f_type == DTYPE_VNODE) {
 2069                 vp = fp->f_vnode;
 2070                 if (vn_isdisk(vp)) {
 2071                         fdrop(fp, td);
 2072                         td->td_retval[0] = AIO_NOTCANCELED;
 2073                         return (0);
 2074                 }
 2075         }
 2076 
 2077         AIO_LOCK(ki);
 2078         TAILQ_FOREACH_SAFE(job, &ki->kaio_jobqueue, plist, jobn) {
 2079                 if ((uap->fd == job->uaiocb.aio_fildes) &&
 2080                     ((uap->aiocbp == NULL) ||
 2081                      (uap->aiocbp == job->ujob))) {
 2082                         if (aio_cancel_job(p, ki, job)) {
 2083                                 cancelled++;
 2084                         } else {
 2085                                 notcancelled++;
 2086                         }
 2087                         if (uap->aiocbp != NULL)
 2088                                 break;
 2089                 }
 2090         }
 2091         AIO_UNLOCK(ki);
 2092 
 2093 done:
 2094         fdrop(fp, td);
 2095 
 2096         if (uap->aiocbp != NULL) {
 2097                 if (cancelled) {
 2098                         td->td_retval[0] = AIO_CANCELED;
 2099                         return (0);
 2100                 }
 2101         }
 2102 
 2103         if (notcancelled) {
 2104                 td->td_retval[0] = AIO_NOTCANCELED;
 2105                 return (0);
 2106         }
 2107 
 2108         if (cancelled) {
 2109                 td->td_retval[0] = AIO_CANCELED;
 2110                 return (0);
 2111         }
 2112 
 2113         td->td_retval[0] = AIO_ALLDONE;
 2114 
 2115         return (0);
 2116 }
 2117 
 2118 /*
 2119  * aio_error is implemented in the kernel level for compatibility purposes
 2120  * only.  For a user mode async implementation, it would be best to do it in
 2121  * a userland subroutine.
 2122  */
 2123 static int
 2124 kern_aio_error(struct thread *td, struct aiocb *ujob, struct aiocb_ops *ops)
 2125 {
 2126         struct proc *p = td->td_proc;
 2127         struct kaiocb *job;
 2128         struct kaioinfo *ki;
 2129         int status;
 2130 
 2131         ki = p->p_aioinfo;
 2132         if (ki == NULL) {
 2133                 td->td_retval[0] = EINVAL;
 2134                 return (0);
 2135         }
 2136 
 2137         AIO_LOCK(ki);
 2138         TAILQ_FOREACH(job, &ki->kaio_all, allist) {
 2139                 if (job->ujob == ujob) {
 2140                         if (job->jobflags & KAIOCB_FINISHED)
 2141                                 td->td_retval[0] =
 2142                                         job->uaiocb._aiocb_private.error;
 2143                         else
 2144                                 td->td_retval[0] = EINPROGRESS;
 2145                         AIO_UNLOCK(ki);
 2146                         return (0);
 2147                 }
 2148         }
 2149         AIO_UNLOCK(ki);
 2150 
 2151         /*
 2152          * Hack for failure of aio_aqueue.
 2153          */
 2154         status = ops->fetch_status(ujob);
 2155         if (status == -1) {
 2156                 td->td_retval[0] = ops->fetch_error(ujob);
 2157                 return (0);
 2158         }
 2159 
 2160         td->td_retval[0] = EINVAL;
 2161         return (0);
 2162 }
 2163 
 2164 int
 2165 sys_aio_error(struct thread *td, struct aio_error_args *uap)
 2166 {
 2167 
 2168         return (kern_aio_error(td, uap->aiocbp, &aiocb_ops));
 2169 }
 2170 
 2171 /* syscall - asynchronous read from a file (REALTIME) */
 2172 #ifdef COMPAT_FREEBSD6
 2173 int
 2174 freebsd6_aio_read(struct thread *td, struct freebsd6_aio_read_args *uap)
 2175 {
 2176 
 2177         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
 2178             &aiocb_ops_osigevent));
 2179 }
 2180 #endif
 2181 
 2182 int
 2183 sys_aio_read(struct thread *td, struct aio_read_args *uap)
 2184 {
 2185 
 2186         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READ, &aiocb_ops));
 2187 }
 2188 
 2189 int
 2190 sys_aio_readv(struct thread *td, struct aio_readv_args *uap)
 2191 {
 2192 
 2193         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_READV, &aiocb_ops));
 2194 }
 2195 
 2196 /* syscall - asynchronous write to a file (REALTIME) */
 2197 #ifdef COMPAT_FREEBSD6
 2198 int
 2199 freebsd6_aio_write(struct thread *td, struct freebsd6_aio_write_args *uap)
 2200 {
 2201 
 2202         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
 2203             &aiocb_ops_osigevent));
 2204 }
 2205 #endif
 2206 
 2207 int
 2208 sys_aio_write(struct thread *td, struct aio_write_args *uap)
 2209 {
 2210 
 2211         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITE, &aiocb_ops));
 2212 }
 2213 
 2214 int
 2215 sys_aio_writev(struct thread *td, struct aio_writev_args *uap)
 2216 {
 2217 
 2218         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_WRITEV, &aiocb_ops));
 2219 }
 2220 
 2221 int
 2222 sys_aio_mlock(struct thread *td, struct aio_mlock_args *uap)
 2223 {
 2224 
 2225         return (aio_aqueue(td, uap->aiocbp, NULL, LIO_MLOCK, &aiocb_ops));
 2226 }
 2227 
 2228 static int
 2229 kern_lio_listio(struct thread *td, int mode, struct aiocb * const *uacb_list,
 2230     struct aiocb **acb_list, int nent, struct sigevent *sig,
 2231     struct aiocb_ops *ops)
 2232 {
 2233         struct proc *p = td->td_proc;
 2234         struct aiocb *job;
 2235         struct kaioinfo *ki;
 2236         struct aioliojob *lj;
 2237         struct kevent kev;
 2238         int error;
 2239         int nagain, nerror;
 2240         int i;
 2241 
 2242         if ((mode != LIO_NOWAIT) && (mode != LIO_WAIT))
 2243                 return (EINVAL);
 2244 
 2245         if (nent < 0 || nent > max_aio_queue_per_proc)
 2246                 return (EINVAL);
 2247 
 2248         if (p->p_aioinfo == NULL)
 2249                 aio_init_aioinfo(p);
 2250 
 2251         ki = p->p_aioinfo;
 2252 
 2253         lj = uma_zalloc(aiolio_zone, M_WAITOK);
 2254         lj->lioj_flags = 0;
 2255         lj->lioj_count = 0;
 2256         lj->lioj_finished_count = 0;
 2257         lj->lioj_signal.sigev_notify = SIGEV_NONE;
 2258         knlist_init_mtx(&lj->klist, AIO_MTX(ki));
 2259         ksiginfo_init(&lj->lioj_ksi);
 2260 
 2261         /*
 2262          * Setup signal.
 2263          */
 2264         if (sig && (mode == LIO_NOWAIT)) {
 2265                 bcopy(sig, &lj->lioj_signal, sizeof(lj->lioj_signal));
 2266                 if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
 2267                         /* Assume only new style KEVENT */
 2268                         memset(&kev, 0, sizeof(kev));
 2269                         kev.filter = EVFILT_LIO;
 2270                         kev.flags = EV_ADD | EV_ENABLE | EV_FLAG1;
 2271                         kev.ident = (uintptr_t)uacb_list; /* something unique */
 2272                         kev.data = (intptr_t)lj;
 2273                         /* pass user defined sigval data */
 2274                         kev.udata = lj->lioj_signal.sigev_value.sival_ptr;
 2275                         error = kqfd_register(
 2276                             lj->lioj_signal.sigev_notify_kqueue, &kev, td,
 2277                             M_WAITOK);
 2278                         if (error) {
 2279                                 uma_zfree(aiolio_zone, lj);
 2280                                 return (error);
 2281                         }
 2282                 } else if (lj->lioj_signal.sigev_notify == SIGEV_NONE) {
 2283                         ;
 2284                 } else if (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
 2285                            lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID) {
 2286                                 if (!_SIG_VALID(lj->lioj_signal.sigev_signo)) {
 2287                                         uma_zfree(aiolio_zone, lj);
 2288                                         return EINVAL;
 2289                                 }
 2290                                 lj->lioj_flags |= LIOJ_SIGNAL;
 2291                 } else {
 2292                         uma_zfree(aiolio_zone, lj);
 2293                         return EINVAL;
 2294                 }
 2295         }
 2296 
 2297         AIO_LOCK(ki);
 2298         TAILQ_INSERT_TAIL(&ki->kaio_liojoblist, lj, lioj_list);
 2299         /*
 2300          * Add extra aiocb count to avoid the lio to be freed
 2301          * by other threads doing aio_waitcomplete or aio_return,
 2302          * and prevent event from being sent until we have queued
 2303          * all tasks.
 2304          */
 2305         lj->lioj_count = 1;
 2306         AIO_UNLOCK(ki);
 2307 
 2308         /*
 2309          * Get pointers to the list of I/O requests.
 2310          */
 2311         nagain = 0;
 2312         nerror = 0;
 2313         for (i = 0; i < nent; i++) {
 2314                 job = acb_list[i];
 2315                 if (job != NULL) {
 2316                         error = aio_aqueue(td, job, lj, LIO_NOP, ops);
 2317                         if (error == EAGAIN)
 2318                                 nagain++;
 2319                         else if (error != 0)
 2320                                 nerror++;
 2321                 }
 2322         }
 2323 
 2324         error = 0;
 2325         AIO_LOCK(ki);
 2326         if (mode == LIO_WAIT) {
 2327                 while (lj->lioj_count - 1 != lj->lioj_finished_count) {
 2328                         ki->kaio_flags |= KAIO_WAKEUP;
 2329                         error = msleep(&p->p_aioinfo, AIO_MTX(ki),
 2330                             PRIBIO | PCATCH, "aiospn", 0);
 2331                         if (error == ERESTART)
 2332                                 error = EINTR;
 2333                         if (error)
 2334                                 break;
 2335                 }
 2336         } else {
 2337                 if (lj->lioj_count - 1 == lj->lioj_finished_count) {
 2338                         if (lj->lioj_signal.sigev_notify == SIGEV_KEVENT) {
 2339                                 lj->lioj_flags |= LIOJ_KEVENT_POSTED;
 2340                                 KNOTE_LOCKED(&lj->klist, 1);
 2341                         }
 2342                         if ((lj->lioj_flags & (LIOJ_SIGNAL |
 2343                             LIOJ_SIGNAL_POSTED)) == LIOJ_SIGNAL &&
 2344                             (lj->lioj_signal.sigev_notify == SIGEV_SIGNAL ||
 2345                             lj->lioj_signal.sigev_notify == SIGEV_THREAD_ID)) {
 2346                                 aio_sendsig(p, &lj->lioj_signal, &lj->lioj_ksi,
 2347                                     lj->lioj_count != 1);
 2348                                 lj->lioj_flags |= LIOJ_SIGNAL_POSTED;
 2349                         }
 2350                 }
 2351         }
 2352         lj->lioj_count--;
 2353         if (lj->lioj_count == 0) {
 2354                 TAILQ_REMOVE(&ki->kaio_liojoblist, lj, lioj_list);
 2355                 knlist_delete(&lj->klist, curthread, 1);
 2356                 PROC_LOCK(p);
 2357                 sigqueue_take(&lj->lioj_ksi);
 2358                 PROC_UNLOCK(p);
 2359                 AIO_UNLOCK(ki);
 2360                 uma_zfree(aiolio_zone, lj);
 2361         } else
 2362                 AIO_UNLOCK(ki);
 2363 
 2364         if (nerror)
 2365                 return (EIO);
 2366         else if (nagain)
 2367                 return (EAGAIN);
 2368         else
 2369                 return (error);
 2370 }
 2371 
 2372 /* syscall - list directed I/O (REALTIME) */
 2373 #ifdef COMPAT_FREEBSD6
 2374 int
 2375 freebsd6_lio_listio(struct thread *td, struct freebsd6_lio_listio_args *uap)
 2376 {
 2377         struct aiocb **acb_list;
 2378         struct sigevent *sigp, sig;
 2379         struct osigevent osig;
 2380         int error, nent;
 2381 
 2382         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
 2383                 return (EINVAL);
 2384 
 2385         nent = uap->nent;
 2386         if (nent < 0 || nent > max_aio_queue_per_proc)
 2387                 return (EINVAL);
 2388 
 2389         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
 2390                 error = copyin(uap->sig, &osig, sizeof(osig));
 2391                 if (error)
 2392                         return (error);
 2393                 error = convert_old_sigevent(&osig, &sig);
 2394                 if (error)
 2395                         return (error);
 2396                 sigp = &sig;
 2397         } else
 2398                 sigp = NULL;
 2399 
 2400         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
 2401         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
 2402         if (error == 0)
 2403                 error = kern_lio_listio(td, uap->mode,
 2404                     (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
 2405                     &aiocb_ops_osigevent);
 2406         free(acb_list, M_LIO);
 2407         return (error);
 2408 }
 2409 #endif
 2410 
 2411 /* syscall - list directed I/O (REALTIME) */
 2412 int
 2413 sys_lio_listio(struct thread *td, struct lio_listio_args *uap)
 2414 {
 2415         struct aiocb **acb_list;
 2416         struct sigevent *sigp, sig;
 2417         int error, nent;
 2418 
 2419         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
 2420                 return (EINVAL);
 2421 
 2422         nent = uap->nent;
 2423         if (nent < 0 || nent > max_aio_queue_per_proc)
 2424                 return (EINVAL);
 2425 
 2426         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
 2427                 error = copyin(uap->sig, &sig, sizeof(sig));
 2428                 if (error)
 2429                         return (error);
 2430                 sigp = &sig;
 2431         } else
 2432                 sigp = NULL;
 2433 
 2434         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
 2435         error = copyin(uap->acb_list, acb_list, nent * sizeof(acb_list[0]));
 2436         if (error == 0)
 2437                 error = kern_lio_listio(td, uap->mode, uap->acb_list, acb_list,
 2438                     nent, sigp, &aiocb_ops);
 2439         free(acb_list, M_LIO);
 2440         return (error);
 2441 }
 2442 
 2443 static void
 2444 aio_biocleanup(struct bio *bp)
 2445 {
 2446         struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
 2447         struct kaioinfo *ki;
 2448         struct buf *pbuf = (struct buf *)bp->bio_caller2;
 2449 
 2450         /* Release mapping into kernel space. */
 2451         if (pbuf != NULL) {
 2452                 MPASS(pbuf->b_npages <= atop(maxphys) + 1);
 2453                 pmap_qremove((vm_offset_t)pbuf->b_data, pbuf->b_npages);
 2454                 vm_page_unhold_pages(pbuf->b_pages, pbuf->b_npages);
 2455                 uma_zfree(pbuf_zone, pbuf);
 2456                 atomic_subtract_int(&num_buf_aio, 1);
 2457                 ki = job->userproc->p_aioinfo;
 2458                 AIO_LOCK(ki);
 2459                 ki->kaio_buffer_count--;
 2460                 AIO_UNLOCK(ki);
 2461         } else {
 2462                 MPASS(bp->bio_ma_n <= atop(maxphys) + 1);
 2463                 vm_page_unhold_pages(bp->bio_ma, bp->bio_ma_n);
 2464                 free(bp->bio_ma, M_TEMP);
 2465                 atomic_subtract_int(&num_unmapped_aio, 1);
 2466         }
 2467         g_destroy_bio(bp);
 2468 }
 2469 
 2470 static void
 2471 aio_biowakeup(struct bio *bp)
 2472 {
 2473         struct kaiocb *job = (struct kaiocb *)bp->bio_caller1;
 2474         size_t nbytes;
 2475         long bcount = bp->bio_bcount;
 2476         long resid = bp->bio_resid;
 2477         int error, opcode, nblks;
 2478         int bio_error = bp->bio_error;
 2479         uint16_t flags = bp->bio_flags;
 2480 
 2481         opcode = job->uaiocb.aio_lio_opcode;
 2482 
 2483         aio_biocleanup(bp);
 2484 
 2485         nbytes =bcount - resid;
 2486         atomic_add_acq_long(&job->nbytes, nbytes);
 2487         nblks = btodb(nbytes);
 2488         error = 0;
 2489         /*
 2490          * If multiple bios experienced an error, the job will reflect the
 2491          * error of whichever failed bio completed last.
 2492          */
 2493         if (flags & BIO_ERROR)
 2494                 atomic_set_int(&job->error, bio_error);
 2495         if (opcode & LIO_WRITE)
 2496                 atomic_add_int(&job->outblock, nblks);
 2497         else
 2498                 atomic_add_int(&job->inblock, nblks);
 2499         atomic_subtract_int(&job->nbio, 1);
 2500 
 2501 
 2502         if (atomic_load_int(&job->nbio) == 0) {
 2503                 if (atomic_load_int(&job->error))
 2504                         aio_complete(job, -1, job->error);
 2505                 else
 2506                         aio_complete(job, atomic_load_long(&job->nbytes), 0);
 2507         }
 2508 }
 2509 
 2510 /* syscall - wait for the next completion of an aio request */
 2511 static int
 2512 kern_aio_waitcomplete(struct thread *td, struct aiocb **ujobp,
 2513     struct timespec *ts, struct aiocb_ops *ops)
 2514 {
 2515         struct proc *p = td->td_proc;
 2516         struct timeval atv;
 2517         struct kaioinfo *ki;
 2518         struct kaiocb *job;
 2519         struct aiocb *ujob;
 2520         long error, status;
 2521         int timo;
 2522 
 2523         ops->store_aiocb(ujobp, NULL);
 2524 
 2525         if (ts == NULL) {
 2526                 timo = 0;
 2527         } else if (ts->tv_sec == 0 && ts->tv_nsec == 0) {
 2528                 timo = -1;
 2529         } else {
 2530                 if ((ts->tv_nsec < 0) || (ts->tv_nsec >= 1000000000))
 2531                         return (EINVAL);
 2532 
 2533                 TIMESPEC_TO_TIMEVAL(&atv, ts);
 2534                 if (itimerfix(&atv))
 2535                         return (EINVAL);
 2536                 timo = tvtohz(&atv);
 2537         }
 2538 
 2539         if (p->p_aioinfo == NULL)
 2540                 aio_init_aioinfo(p);
 2541         ki = p->p_aioinfo;
 2542 
 2543         error = 0;
 2544         job = NULL;
 2545         AIO_LOCK(ki);
 2546         while ((job = TAILQ_FIRST(&ki->kaio_done)) == NULL) {
 2547                 if (timo == -1) {
 2548                         error = EWOULDBLOCK;
 2549                         break;
 2550                 }
 2551                 ki->kaio_flags |= KAIO_WAKEUP;
 2552                 error = msleep(&p->p_aioinfo, AIO_MTX(ki), PRIBIO | PCATCH,
 2553                     "aiowc", timo);
 2554                 if (timo && error == ERESTART)
 2555                         error = EINTR;
 2556                 if (error)
 2557                         break;
 2558         }
 2559 
 2560         if (job != NULL) {
 2561                 MPASS(job->jobflags & KAIOCB_FINISHED);
 2562                 ujob = job->ujob;
 2563                 status = job->uaiocb._aiocb_private.status;
 2564                 error = job->uaiocb._aiocb_private.error;
 2565                 td->td_retval[0] = status;
 2566                 td->td_ru.ru_oublock += job->outblock;
 2567                 td->td_ru.ru_inblock += job->inblock;
 2568                 td->td_ru.ru_msgsnd += job->msgsnd;
 2569                 td->td_ru.ru_msgrcv += job->msgrcv;
 2570                 aio_free_entry(job);
 2571                 AIO_UNLOCK(ki);
 2572                 ops->store_aiocb(ujobp, ujob);
 2573                 ops->store_error(ujob, error);
 2574                 ops->store_status(ujob, status);
 2575         } else
 2576                 AIO_UNLOCK(ki);
 2577 
 2578         return (error);
 2579 }
 2580 
 2581 int
 2582 sys_aio_waitcomplete(struct thread *td, struct aio_waitcomplete_args *uap)
 2583 {
 2584         struct timespec ts, *tsp;
 2585         int error;
 2586 
 2587         if (uap->timeout) {
 2588                 /* Get timespec struct. */
 2589                 error = copyin(uap->timeout, &ts, sizeof(ts));
 2590                 if (error)
 2591                         return (error);
 2592                 tsp = &ts;
 2593         } else
 2594                 tsp = NULL;
 2595 
 2596         return (kern_aio_waitcomplete(td, uap->aiocbp, tsp, &aiocb_ops));
 2597 }
 2598 
 2599 static int
 2600 kern_aio_fsync(struct thread *td, int op, struct aiocb *ujob,
 2601     struct aiocb_ops *ops)
 2602 {
 2603         int listop;
 2604 
 2605         switch (op) {
 2606         case O_SYNC:
 2607                 listop = LIO_SYNC;
 2608                 break;
 2609         case O_DSYNC:
 2610                 listop = LIO_DSYNC;
 2611                 break;
 2612         default:
 2613                 return (EINVAL);
 2614         }
 2615 
 2616         return (aio_aqueue(td, ujob, NULL, listop, ops));
 2617 }
 2618 
 2619 int
 2620 sys_aio_fsync(struct thread *td, struct aio_fsync_args *uap)
 2621 {
 2622 
 2623         return (kern_aio_fsync(td, uap->op, uap->aiocbp, &aiocb_ops));
 2624 }
 2625 
 2626 /* kqueue attach function */
 2627 static int
 2628 filt_aioattach(struct knote *kn)
 2629 {
 2630         struct kaiocb *job;
 2631 
 2632         job = (struct kaiocb *)(uintptr_t)kn->kn_sdata;
 2633 
 2634         /*
 2635          * The job pointer must be validated before using it, so
 2636          * registration is restricted to the kernel; the user cannot
 2637          * set EV_FLAG1.
 2638          */
 2639         if ((kn->kn_flags & EV_FLAG1) == 0)
 2640                 return (EPERM);
 2641         kn->kn_ptr.p_aio = job;
 2642         kn->kn_flags &= ~EV_FLAG1;
 2643 
 2644         knlist_add(&job->klist, kn, 0);
 2645 
 2646         return (0);
 2647 }
 2648 
 2649 /* kqueue detach function */
 2650 static void
 2651 filt_aiodetach(struct knote *kn)
 2652 {
 2653         struct knlist *knl;
 2654 
 2655         knl = &kn->kn_ptr.p_aio->klist;
 2656         knl->kl_lock(knl->kl_lockarg);
 2657         if (!knlist_empty(knl))
 2658                 knlist_remove(knl, kn, 1);
 2659         knl->kl_unlock(knl->kl_lockarg);
 2660 }
 2661 
 2662 /* kqueue filter function */
 2663 /*ARGSUSED*/
 2664 static int
 2665 filt_aio(struct knote *kn, long hint)
 2666 {
 2667         struct kaiocb *job = kn->kn_ptr.p_aio;
 2668 
 2669         kn->kn_data = job->uaiocb._aiocb_private.error;
 2670         if (!(job->jobflags & KAIOCB_FINISHED))
 2671                 return (0);
 2672         kn->kn_flags |= EV_EOF;
 2673         return (1);
 2674 }
 2675 
 2676 /* kqueue attach function */
 2677 static int
 2678 filt_lioattach(struct knote *kn)
 2679 {
 2680         struct aioliojob *lj;
 2681 
 2682         lj = (struct aioliojob *)(uintptr_t)kn->kn_sdata;
 2683 
 2684         /*
 2685          * The aioliojob pointer must be validated before using it, so
 2686          * registration is restricted to the kernel; the user cannot
 2687          * set EV_FLAG1.
 2688          */
 2689         if ((kn->kn_flags & EV_FLAG1) == 0)
 2690                 return (EPERM);
 2691         kn->kn_ptr.p_lio = lj;
 2692         kn->kn_flags &= ~EV_FLAG1;
 2693 
 2694         knlist_add(&lj->klist, kn, 0);
 2695 
 2696         return (0);
 2697 }
 2698 
 2699 /* kqueue detach function */
 2700 static void
 2701 filt_liodetach(struct knote *kn)
 2702 {
 2703         struct knlist *knl;
 2704 
 2705         knl = &kn->kn_ptr.p_lio->klist;
 2706         knl->kl_lock(knl->kl_lockarg);
 2707         if (!knlist_empty(knl))
 2708                 knlist_remove(knl, kn, 1);
 2709         knl->kl_unlock(knl->kl_lockarg);
 2710 }
 2711 
 2712 /* kqueue filter function */
 2713 /*ARGSUSED*/
 2714 static int
 2715 filt_lio(struct knote *kn, long hint)
 2716 {
 2717         struct aioliojob * lj = kn->kn_ptr.p_lio;
 2718 
 2719         return (lj->lioj_flags & LIOJ_KEVENT_POSTED);
 2720 }
 2721 
 2722 #ifdef COMPAT_FREEBSD32
 2723 #include <sys/mount.h>
 2724 #include <sys/socket.h>
 2725 #include <compat/freebsd32/freebsd32.h>
 2726 #include <compat/freebsd32/freebsd32_proto.h>
 2727 #include <compat/freebsd32/freebsd32_signal.h>
 2728 #include <compat/freebsd32/freebsd32_syscall.h>
 2729 #include <compat/freebsd32/freebsd32_util.h>
 2730 
 2731 struct __aiocb_private32 {
 2732         int32_t status;
 2733         int32_t error;
 2734         uint32_t kernelinfo;
 2735 };
 2736 
 2737 #ifdef COMPAT_FREEBSD6
 2738 typedef struct oaiocb32 {
 2739         int     aio_fildes;             /* File descriptor */
 2740         uint64_t aio_offset __packed;   /* File offset for I/O */
 2741         uint32_t aio_buf;               /* I/O buffer in process space */
 2742         uint32_t aio_nbytes;            /* Number of bytes for I/O */
 2743         struct  osigevent32 aio_sigevent; /* Signal to deliver */
 2744         int     aio_lio_opcode;         /* LIO opcode */
 2745         int     aio_reqprio;            /* Request priority -- ignored */
 2746         struct  __aiocb_private32 _aiocb_private;
 2747 } oaiocb32_t;
 2748 #endif
 2749 
 2750 typedef struct aiocb32 {
 2751         int32_t aio_fildes;             /* File descriptor */
 2752         uint64_t aio_offset __packed;   /* File offset for I/O */
 2753         uint32_t aio_buf;       /* I/O buffer in process space */
 2754         uint32_t aio_nbytes;    /* Number of bytes for I/O */
 2755         int     __spare__[2];
 2756         uint32_t __spare2__;
 2757         int     aio_lio_opcode;         /* LIO opcode */
 2758         int     aio_reqprio;            /* Request priority -- ignored */
 2759         struct  __aiocb_private32 _aiocb_private;
 2760         struct  sigevent32 aio_sigevent;        /* Signal to deliver */
 2761 } aiocb32_t;
 2762 
 2763 #ifdef COMPAT_FREEBSD6
 2764 static int
 2765 convert_old_sigevent32(struct osigevent32 *osig, struct sigevent *nsig)
 2766 {
 2767 
 2768         /*
 2769          * Only SIGEV_NONE, SIGEV_SIGNAL, and SIGEV_KEVENT are
 2770          * supported by AIO with the old sigevent structure.
 2771          */
 2772         CP(*osig, *nsig, sigev_notify);
 2773         switch (nsig->sigev_notify) {
 2774         case SIGEV_NONE:
 2775                 break;
 2776         case SIGEV_SIGNAL:
 2777                 nsig->sigev_signo = osig->__sigev_u.__sigev_signo;
 2778                 break;
 2779         case SIGEV_KEVENT:
 2780                 nsig->sigev_notify_kqueue =
 2781                     osig->__sigev_u.__sigev_notify_kqueue;
 2782                 PTRIN_CP(*osig, *nsig, sigev_value.sival_ptr);
 2783                 break;
 2784         default:
 2785                 return (EINVAL);
 2786         }
 2787         return (0);
 2788 }
 2789 
 2790 static int
 2791 aiocb32_copyin_old_sigevent(struct aiocb *ujob, struct kaiocb *kjob,
 2792     int type __unused)
 2793 {
 2794         struct oaiocb32 job32;
 2795         struct aiocb *kcb = &kjob->uaiocb;
 2796         int error;
 2797 
 2798         bzero(kcb, sizeof(struct aiocb));
 2799         error = copyin(ujob, &job32, sizeof(job32));
 2800         if (error)
 2801                 return (error);
 2802 
 2803         /* No need to copyin aio_iov, because it did not exist in FreeBSD 6 */
 2804 
 2805         CP(job32, *kcb, aio_fildes);
 2806         CP(job32, *kcb, aio_offset);
 2807         PTRIN_CP(job32, *kcb, aio_buf);
 2808         CP(job32, *kcb, aio_nbytes);
 2809         CP(job32, *kcb, aio_lio_opcode);
 2810         CP(job32, *kcb, aio_reqprio);
 2811         CP(job32, *kcb, _aiocb_private.status);
 2812         CP(job32, *kcb, _aiocb_private.error);
 2813         PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
 2814         return (convert_old_sigevent32(&job32.aio_sigevent,
 2815             &kcb->aio_sigevent));
 2816 }
 2817 #endif
 2818 
 2819 static int
 2820 aiocb32_copyin(struct aiocb *ujob, struct kaiocb *kjob, int type)
 2821 {
 2822         struct aiocb32 job32;
 2823         struct aiocb *kcb = &kjob->uaiocb;
 2824         struct iovec32 *iov32;
 2825         int error;
 2826 
 2827         error = copyin(ujob, &job32, sizeof(job32));
 2828         if (error)
 2829                 return (error);
 2830         CP(job32, *kcb, aio_fildes);
 2831         CP(job32, *kcb, aio_offset);
 2832         CP(job32, *kcb, aio_lio_opcode);
 2833         if (type & LIO_VECTORED) {
 2834                 iov32 = PTRIN(job32.aio_iov);
 2835                 CP(job32, *kcb, aio_iovcnt);
 2836                 /* malloc a uio and copy in the iovec */
 2837                 error = freebsd32_copyinuio(iov32,
 2838                     kcb->aio_iovcnt, &kjob->uiop);
 2839                 if (error)
 2840                         return (error);
 2841         } else {
 2842                 PTRIN_CP(job32, *kcb, aio_buf);
 2843                 CP(job32, *kcb, aio_nbytes);
 2844         }
 2845         CP(job32, *kcb, aio_reqprio);
 2846         CP(job32, *kcb, _aiocb_private.status);
 2847         CP(job32, *kcb, _aiocb_private.error);
 2848         PTRIN_CP(job32, *kcb, _aiocb_private.kernelinfo);
 2849         error = convert_sigevent32(&job32.aio_sigevent, &kcb->aio_sigevent);
 2850 
 2851         return (error);
 2852 }
 2853 
 2854 static long
 2855 aiocb32_fetch_status(struct aiocb *ujob)
 2856 {
 2857         struct aiocb32 *ujob32;
 2858 
 2859         ujob32 = (struct aiocb32 *)ujob;
 2860         return (fuword32(&ujob32->_aiocb_private.status));
 2861 }
 2862 
 2863 static long
 2864 aiocb32_fetch_error(struct aiocb *ujob)
 2865 {
 2866         struct aiocb32 *ujob32;
 2867 
 2868         ujob32 = (struct aiocb32 *)ujob;
 2869         return (fuword32(&ujob32->_aiocb_private.error));
 2870 }
 2871 
 2872 static int
 2873 aiocb32_store_status(struct aiocb *ujob, long status)
 2874 {
 2875         struct aiocb32 *ujob32;
 2876 
 2877         ujob32 = (struct aiocb32 *)ujob;
 2878         return (suword32(&ujob32->_aiocb_private.status, status));
 2879 }
 2880 
 2881 static int
 2882 aiocb32_store_error(struct aiocb *ujob, long error)
 2883 {
 2884         struct aiocb32 *ujob32;
 2885 
 2886         ujob32 = (struct aiocb32 *)ujob;
 2887         return (suword32(&ujob32->_aiocb_private.error, error));
 2888 }
 2889 
 2890 static int
 2891 aiocb32_store_kernelinfo(struct aiocb *ujob, long jobref)
 2892 {
 2893         struct aiocb32 *ujob32;
 2894 
 2895         ujob32 = (struct aiocb32 *)ujob;
 2896         return (suword32(&ujob32->_aiocb_private.kernelinfo, jobref));
 2897 }
 2898 
 2899 static int
 2900 aiocb32_store_aiocb(struct aiocb **ujobp, struct aiocb *ujob)
 2901 {
 2902 
 2903         return (suword32(ujobp, (long)ujob));
 2904 }
 2905 
 2906 static struct aiocb_ops aiocb32_ops = {
 2907         .aio_copyin = aiocb32_copyin,
 2908         .fetch_status = aiocb32_fetch_status,
 2909         .fetch_error = aiocb32_fetch_error,
 2910         .store_status = aiocb32_store_status,
 2911         .store_error = aiocb32_store_error,
 2912         .store_kernelinfo = aiocb32_store_kernelinfo,
 2913         .store_aiocb = aiocb32_store_aiocb,
 2914 };
 2915 
 2916 #ifdef COMPAT_FREEBSD6
 2917 static struct aiocb_ops aiocb32_ops_osigevent = {
 2918         .aio_copyin = aiocb32_copyin_old_sigevent,
 2919         .fetch_status = aiocb32_fetch_status,
 2920         .fetch_error = aiocb32_fetch_error,
 2921         .store_status = aiocb32_store_status,
 2922         .store_error = aiocb32_store_error,
 2923         .store_kernelinfo = aiocb32_store_kernelinfo,
 2924         .store_aiocb = aiocb32_store_aiocb,
 2925 };
 2926 #endif
 2927 
 2928 int
 2929 freebsd32_aio_return(struct thread *td, struct freebsd32_aio_return_args *uap)
 2930 {
 2931 
 2932         return (kern_aio_return(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
 2933 }
 2934 
 2935 int
 2936 freebsd32_aio_suspend(struct thread *td, struct freebsd32_aio_suspend_args *uap)
 2937 {
 2938         struct timespec32 ts32;
 2939         struct timespec ts, *tsp;
 2940         struct aiocb **ujoblist;
 2941         uint32_t *ujoblist32;
 2942         int error, i;
 2943 
 2944         if (uap->nent < 0 || uap->nent > max_aio_queue_per_proc)
 2945                 return (EINVAL);
 2946 
 2947         if (uap->timeout) {
 2948                 /* Get timespec struct. */
 2949                 if ((error = copyin(uap->timeout, &ts32, sizeof(ts32))) != 0)
 2950                         return (error);
 2951                 CP(ts32, ts, tv_sec);
 2952                 CP(ts32, ts, tv_nsec);
 2953                 tsp = &ts;
 2954         } else
 2955                 tsp = NULL;
 2956 
 2957         ujoblist = malloc(uap->nent * sizeof(ujoblist[0]), M_AIOS, M_WAITOK);
 2958         ujoblist32 = (uint32_t *)ujoblist;
 2959         error = copyin(uap->aiocbp, ujoblist32, uap->nent *
 2960             sizeof(ujoblist32[0]));
 2961         if (error == 0) {
 2962                 for (i = uap->nent - 1; i >= 0; i--)
 2963                         ujoblist[i] = PTRIN(ujoblist32[i]);
 2964 
 2965                 error = kern_aio_suspend(td, uap->nent, ujoblist, tsp);
 2966         }
 2967         free(ujoblist, M_AIOS);
 2968         return (error);
 2969 }
 2970 
 2971 int
 2972 freebsd32_aio_error(struct thread *td, struct freebsd32_aio_error_args *uap)
 2973 {
 2974 
 2975         return (kern_aio_error(td, (struct aiocb *)uap->aiocbp, &aiocb32_ops));
 2976 }
 2977 
 2978 #ifdef COMPAT_FREEBSD6
 2979 int
 2980 freebsd6_freebsd32_aio_read(struct thread *td,
 2981     struct freebsd6_freebsd32_aio_read_args *uap)
 2982 {
 2983 
 2984         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
 2985             &aiocb32_ops_osigevent));
 2986 }
 2987 #endif
 2988 
 2989 int
 2990 freebsd32_aio_read(struct thread *td, struct freebsd32_aio_read_args *uap)
 2991 {
 2992 
 2993         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READ,
 2994             &aiocb32_ops));
 2995 }
 2996 
 2997 int
 2998 freebsd32_aio_readv(struct thread *td, struct freebsd32_aio_readv_args *uap)
 2999 {
 3000 
 3001         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_READV,
 3002             &aiocb32_ops));
 3003 }
 3004 
 3005 #ifdef COMPAT_FREEBSD6
 3006 int
 3007 freebsd6_freebsd32_aio_write(struct thread *td,
 3008     struct freebsd6_freebsd32_aio_write_args *uap)
 3009 {
 3010 
 3011         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
 3012             &aiocb32_ops_osigevent));
 3013 }
 3014 #endif
 3015 
 3016 int
 3017 freebsd32_aio_write(struct thread *td, struct freebsd32_aio_write_args *uap)
 3018 {
 3019 
 3020         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITE,
 3021             &aiocb32_ops));
 3022 }
 3023 
 3024 int
 3025 freebsd32_aio_writev(struct thread *td, struct freebsd32_aio_writev_args *uap)
 3026 {
 3027 
 3028         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_WRITEV,
 3029             &aiocb32_ops));
 3030 }
 3031 
 3032 int
 3033 freebsd32_aio_mlock(struct thread *td, struct freebsd32_aio_mlock_args *uap)
 3034 {
 3035 
 3036         return (aio_aqueue(td, (struct aiocb *)uap->aiocbp, NULL, LIO_MLOCK,
 3037             &aiocb32_ops));
 3038 }
 3039 
 3040 int
 3041 freebsd32_aio_waitcomplete(struct thread *td,
 3042     struct freebsd32_aio_waitcomplete_args *uap)
 3043 {
 3044         struct timespec32 ts32;
 3045         struct timespec ts, *tsp;
 3046         int error;
 3047 
 3048         if (uap->timeout) {
 3049                 /* Get timespec struct. */
 3050                 error = copyin(uap->timeout, &ts32, sizeof(ts32));
 3051                 if (error)
 3052                         return (error);
 3053                 CP(ts32, ts, tv_sec);
 3054                 CP(ts32, ts, tv_nsec);
 3055                 tsp = &ts;
 3056         } else
 3057                 tsp = NULL;
 3058 
 3059         return (kern_aio_waitcomplete(td, (struct aiocb **)uap->aiocbp, tsp,
 3060             &aiocb32_ops));
 3061 }
 3062 
 3063 int
 3064 freebsd32_aio_fsync(struct thread *td, struct freebsd32_aio_fsync_args *uap)
 3065 {
 3066 
 3067         return (kern_aio_fsync(td, uap->op, (struct aiocb *)uap->aiocbp,
 3068             &aiocb32_ops));
 3069 }
 3070 
 3071 #ifdef COMPAT_FREEBSD6
 3072 int
 3073 freebsd6_freebsd32_lio_listio(struct thread *td,
 3074     struct freebsd6_freebsd32_lio_listio_args *uap)
 3075 {
 3076         struct aiocb **acb_list;
 3077         struct sigevent *sigp, sig;
 3078         struct osigevent32 osig;
 3079         uint32_t *acb_list32;
 3080         int error, i, nent;
 3081 
 3082         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
 3083                 return (EINVAL);
 3084 
 3085         nent = uap->nent;
 3086         if (nent < 0 || nent > max_aio_queue_per_proc)
 3087                 return (EINVAL);
 3088 
 3089         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
 3090                 error = copyin(uap->sig, &osig, sizeof(osig));
 3091                 if (error)
 3092                         return (error);
 3093                 error = convert_old_sigevent32(&osig, &sig);
 3094                 if (error)
 3095                         return (error);
 3096                 sigp = &sig;
 3097         } else
 3098                 sigp = NULL;
 3099 
 3100         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
 3101         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
 3102         if (error) {
 3103                 free(acb_list32, M_LIO);
 3104                 return (error);
 3105         }
 3106         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
 3107         for (i = 0; i < nent; i++)
 3108                 acb_list[i] = PTRIN(acb_list32[i]);
 3109         free(acb_list32, M_LIO);
 3110 
 3111         error = kern_lio_listio(td, uap->mode,
 3112             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
 3113             &aiocb32_ops_osigevent);
 3114         free(acb_list, M_LIO);
 3115         return (error);
 3116 }
 3117 #endif
 3118 
 3119 int
 3120 freebsd32_lio_listio(struct thread *td, struct freebsd32_lio_listio_args *uap)
 3121 {
 3122         struct aiocb **acb_list;
 3123         struct sigevent *sigp, sig;
 3124         struct sigevent32 sig32;
 3125         uint32_t *acb_list32;
 3126         int error, i, nent;
 3127 
 3128         if ((uap->mode != LIO_NOWAIT) && (uap->mode != LIO_WAIT))
 3129                 return (EINVAL);
 3130 
 3131         nent = uap->nent;
 3132         if (nent < 0 || nent > max_aio_queue_per_proc)
 3133                 return (EINVAL);
 3134 
 3135         if (uap->sig && (uap->mode == LIO_NOWAIT)) {
 3136                 error = copyin(uap->sig, &sig32, sizeof(sig32));
 3137                 if (error)
 3138                         return (error);
 3139                 error = convert_sigevent32(&sig32, &sig);
 3140                 if (error)
 3141                         return (error);
 3142                 sigp = &sig;
 3143         } else
 3144                 sigp = NULL;
 3145 
 3146         acb_list32 = malloc(sizeof(uint32_t) * nent, M_LIO, M_WAITOK);
 3147         error = copyin(uap->acb_list, acb_list32, nent * sizeof(uint32_t));
 3148         if (error) {
 3149                 free(acb_list32, M_LIO);
 3150                 return (error);
 3151         }
 3152         acb_list = malloc(sizeof(struct aiocb *) * nent, M_LIO, M_WAITOK);
 3153         for (i = 0; i < nent; i++)
 3154                 acb_list[i] = PTRIN(acb_list32[i]);
 3155         free(acb_list32, M_LIO);
 3156 
 3157         error = kern_lio_listio(td, uap->mode,
 3158             (struct aiocb * const *)uap->acb_list, acb_list, nent, sigp,
 3159             &aiocb32_ops);
 3160         free(acb_list, M_LIO);
 3161         return (error);
 3162 }
 3163 
 3164 #endif

Cache object: 2d1a3187994fff03d0e2b66c4f306a6f


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.