device.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Server-side device support
 *
 * Copyright (C) 2007 Alexandre Julliard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

21 22 23
#include "config.h"
#include "wine/port.h"

24
#include <assert.h>
25
#include <fcntl.h>
26 27 28 29 30 31 32 33
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
34
#include "ddk/wdm.h"
35 36 37 38 39

#include "object.h"
#include "file.h"
#include "handle.h"
#include "request.h"
40
#include "process.h"
41

42 43
/* IRP object */

44
struct irp_call
45 46 47 48
{
    struct object          obj;           /* object header */
    struct list            dev_entry;     /* entry in device queue */
    struct list            mgr_entry;     /* entry in manager queue */
49
    struct device_file    *file;          /* file containing this irp */
50
    struct thread         *thread;        /* thread that queued the irp */
51
    struct async          *async;         /* pending async op */
52
    irp_params_t           params;        /* irp parameters */
53
    struct iosb           *iosb;          /* I/O status block */
54 55
};

56 57 58
static void irp_call_dump( struct object *obj, int verbose );
static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
static void irp_call_destroy( struct object *obj );
59

60
static const struct object_ops irp_call_ops =
61
{
62 63
    sizeof(struct irp_call),          /* size */
    irp_call_dump,                    /* dump */
64
    no_get_type,                      /* get_type */
65 66
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
67
    irp_call_signaled,                /* signaled */
68 69 70 71
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    no_get_fd,                        /* get_fd */
    no_map_access,                    /* map_access */
72 73
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
74
    no_lookup_name,                   /* lookup_name */
75 76
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
77 78
    no_open_file,                     /* open_file */
    no_close_handle,                  /* close_handle */
79
    irp_call_destroy                  /* destroy */
80 81 82
};


83 84
/* device manager (a list of devices managed by the same client process) */

85 86 87 88
struct device_manager
{
    struct object          obj;           /* object header */
    struct list            devices;       /* list of devices */
89
    struct list            requests;      /* list of pending irps across all devices */
90 91 92
};

static void device_manager_dump( struct object *obj, int verbose );
93
static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
94 95 96 97 98 99
static void device_manager_destroy( struct object *obj );

static const struct object_ops device_manager_ops =
{
    sizeof(struct device_manager),    /* size */
    device_manager_dump,              /* dump */
100
    no_get_type,                      /* get_type */
101 102 103
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
    device_manager_signaled,          /* signaled */
104 105 106 107
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    no_get_fd,                        /* get_fd */
    no_map_access,                    /* map_access */
108 109
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
110
    no_lookup_name,                   /* lookup_name */
111 112
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
113 114 115 116 117 118
    no_open_file,                     /* open_file */
    no_close_handle,                  /* close_handle */
    device_manager_destroy            /* destroy */
};


119 120
/* device (a single device object) */

121 122 123 124
struct device
{
    struct object          obj;           /* object header */
    struct device_manager *manager;       /* manager for this device (or NULL if deleted) */
125
    char                  *unix_path;     /* path to unix device if any */
126
    client_ptr_t           user_ptr;      /* opaque ptr for client side */
127
    struct list            entry;         /* entry in device manager list */
128
    struct list            files;         /* list of open files */
129 130 131
};

static void device_dump( struct object *obj, int verbose );
132
static struct object_type *device_get_type( struct object *obj );
133 134 135 136 137 138 139 140
static void device_destroy( struct object *obj );
static struct object *device_open_file( struct object *obj, unsigned int access,
                                        unsigned int sharing, unsigned int options );

static const struct object_ops device_ops =
{
    sizeof(struct device),            /* size */
    device_dump,                      /* dump */
141
    device_get_type,                  /* get_type */
142 143 144 145 146
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
147
    no_get_fd,                        /* get_fd */
148
    default_fd_map_access,            /* map_access */
149 150
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
151
    no_lookup_name,                   /* lookup_name */
152 153
    directory_link_name,              /* link_name */
    default_unlink_name,              /* unlink_name */
154 155 156 157 158
    device_open_file,                 /* open_file */
    no_close_handle,                  /* close_handle */
    device_destroy                    /* destroy */
};

159 160 161 162 163 164 165 166

/* device file (an open file handle to a device) */

struct device_file
{
    struct object          obj;           /* object header */
    struct device         *device;        /* device for this file */
    struct fd             *fd;            /* file descriptor for irp */
167
    client_ptr_t           user_ptr;      /* opaque ptr for client side */
168 169 170 171 172 173
    struct list            entry;         /* entry in device list */
    struct list            requests;      /* list of pending irp requests */
};

static void device_file_dump( struct object *obj, int verbose );
static struct fd *device_file_get_fd( struct object *obj );
174
static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
175 176
static void device_file_destroy( struct object *obj );
static enum server_fd_type device_file_get_fd_type( struct fd *fd );
177
static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos );
178
static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos );
179
static int device_file_flush( struct fd *fd, struct async *async );
180
static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

static const struct object_ops device_file_ops =
{
    sizeof(struct device_file),       /* size */
    device_file_dump,                 /* dump */
    no_get_type,                      /* get_type */
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
    default_fd_signaled,              /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    device_file_get_fd,               /* get_fd */
    default_fd_map_access,            /* map_access */
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
    no_lookup_name,                   /* lookup_name */
197 198
    no_link_name,                     /* link_name */
    NULL,                             /* unlink_name */
199
    no_open_file,                     /* open_file */
200
    device_file_close_handle,         /* close_handle */
201 202 203 204
    device_file_destroy               /* destroy */
};

static const struct fd_ops device_file_fd_ops =
205 206 207
{
    default_fd_get_poll_events,       /* get_poll_events */
    default_poll_event,               /* poll_event */
208 209 210 211
    device_file_get_fd_type,          /* get_fd_type */
    device_file_read,                 /* read */
    device_file_write,                /* write */
    device_file_flush,                /* flush */
212
    no_fd_get_file_info,              /* get_file_info */
213
    no_fd_get_volume_info,            /* get_volume_info */
214
    device_file_ioctl,                /* ioctl */
215
    default_fd_queue_async,           /* queue_async */
216
    default_fd_reselect_async         /* reselect_async */
217 218 219
};


220
static void irp_call_dump( struct object *obj, int verbose )
221
{
222
    struct irp_call *irp = (struct irp_call *)obj;
223
    fprintf( stderr, "IRP call file=%p\n", irp->file );
224 225
}

226
static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
227
{
228
    struct irp_call *irp = (struct irp_call *)obj;
229

230
    return !irp->file;  /* file is cleared once the irp has completed */
231 232
}

233
static void irp_call_destroy( struct object *obj )
234
{
235
    struct irp_call *irp = (struct irp_call *)obj;
236

237
    if (irp->async)
238
    {
239 240
        async_terminate( irp->async, STATUS_CANCELLED );
        release_object( irp->async );
241
    }
242
    if (irp->iosb) release_object( irp->iosb );
243
    if (irp->file) release_object( irp->file );
244
    if (irp->thread) release_object( irp->thread );
245 246
}

247
static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params, struct async *async )
248
{
249
    struct irp_call *irp;
250

251
    if (!file->device->manager)  /* it has been deleted */
252 253 254 255 256
    {
        set_error( STATUS_FILE_DELETED );
        return NULL;
    }

257
    if ((irp = alloc_object( &irp_call_ops )))
258
    {
259
        irp->file     = (struct device_file *)grab_object( file );
260
        irp->thread   = NULL;
261
        irp->async    = NULL;
262
        irp->params   = *params;
263
        irp->iosb     = NULL;
264

265 266
        if (async) irp->iosb = async_get_iosb( async );
        if (!irp->iosb && !(irp->iosb = create_iosb( NULL, 0, 0 )))
267
        {
268 269
            release_object( irp );
            irp = NULL;
270 271
        }
    }
272
    return irp;
273 274
}

275
static void set_irp_result( struct irp_call *irp, unsigned int status,
276
                            const void *out_data, data_size_t out_size, data_size_t result )
277
{
278
    struct device_file *file = irp->file;
279
    struct iosb *iosb = irp->iosb;
280

281
    if (!file) return;  /* already finished */
282 283

    /* FIXME: handle the STATUS_PENDING case */
284 285 286 287 288
    iosb->status = status;
    iosb->result = result;
    iosb->out_size = min( iosb->out_size, out_size );
    if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size )))
        iosb->out_size = 0;
289
    irp->file = NULL;
290
    if (irp->async)
291
    {
292
        if (result) status = STATUS_ALERTED;
293 294 295
        async_terminate( irp->async, status );
        release_object( irp->async );
        irp->async = NULL;
296
    }
297
    wake_up( &irp->obj, 0 );
298

299 300 301
    /* remove it from the device queue */
    list_remove( &irp->dev_entry );
    release_object( irp );  /* no longer on the device queue */
302
    release_object( file );
303 304 305
}


306 307
static void device_dump( struct object *obj, int verbose )
{
308
    fputs( "Device\n", stderr );
309 310
}

311 312 313 314 315 316 317
static struct object_type *device_get_type( struct object *obj )
{
    static const WCHAR name[] = {'D','e','v','i','c','e'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

318
static void device_destroy( struct object *obj )
319 320 321
{
    struct device *device = (struct device *)obj;

322 323
    assert( list_empty( &device->files ));

324
    free( device->unix_path );
325
    if (device->manager) list_remove( &device->entry );
326 327
}

328
static void add_irp_to_queue( struct device_file *file, struct irp_call *irp, struct thread *thread )
329 330 331 332 333 334
{
    struct device_manager *manager = file->device->manager;

    assert( manager );

    grab_object( irp );  /* grab reference for queued irp */
335
    irp->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
336 337 338 339 340
    list_add_tail( &file->requests, &irp->dev_entry );
    list_add_tail( &manager->requests, &irp->mgr_entry );
    if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 );  /* first one */
}

341 342
static struct object *device_open_file( struct object *obj, unsigned int access,
                                        unsigned int sharing, unsigned int options )
343 344
{
    struct device *device = (struct device *)obj;
345
    struct device_file *file;
346

347 348 349
    if (!(file = alloc_object( &device_file_ops ))) return NULL;

    file->device = (struct device *)grab_object( device );
350
    file->user_ptr = 0;
351 352 353
    list_init( &file->requests );
    list_add_tail( &device->files, &file->entry );
    if (device->unix_path)
354
    {
355 356 357 358 359 360 361 362 363 364 365 366 367 368
        mode_t mode = 0666;
        access = file->obj.ops->map_access( &file->obj, access );
        file->fd = open_fd( NULL, device->unix_path, O_NONBLOCK | O_LARGEFILE,
                            &mode, access, sharing, options );
        if (file->fd) set_fd_user( file->fd, &device_file_fd_ops, &file->obj );
    }
    else file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, 0 );

    if (!file->fd)
    {
        release_object( file );
        return NULL;
    }

369 370
    allow_fd_caching( file->fd );

371 372 373 374 375
    if (device->manager)
    {
        struct irp_call *irp;
        irp_params_t params;

376
        memset( &params, 0, sizeof(params) );
377 378 379 380 381
        params.create.major   = IRP_MJ_CREATE;
        params.create.access  = access;
        params.create.sharing = sharing;
        params.create.options = options;
        params.create.device  = file->device->user_ptr;
382

383
        if ((irp = create_irp( file, &params, NULL )))
384
        {
385
            add_irp_to_queue( file, irp, NULL );
386
            release_object( irp );
387
        }
388
    }
389
    return &file->obj;
390 391
}

392
static void device_file_dump( struct object *obj, int verbose )
393
{
394 395 396
    struct device_file *file = (struct device_file *)obj;

    fprintf( stderr, "File on device %p\n", file->device );
397 398
}

399
static struct fd *device_file_get_fd( struct object *obj )
400
{
401 402 403 404 405
    struct device_file *file = (struct device_file *)obj;

    return (struct fd *)grab_object( file->fd );
}

406 407 408 409 410 411 412 413 414
static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
{
    struct device_file *file = (struct device_file *)obj;

    if (file->device->manager && obj->handle_count == 1)  /* last handle */
    {
        struct irp_call *irp;
        irp_params_t params;

415
        memset( &params, 0, sizeof(params) );
416 417
        params.close.major = IRP_MJ_CLOSE;
        params.close.file  = file->user_ptr;
418

419
        if ((irp = create_irp( file, &params, NULL )))
420
        {
421
            add_irp_to_queue( file, irp, NULL );
422 423 424 425 426 427
            release_object( irp );
        }
    }
    return 1;
}

428 429 430 431 432 433 434 435 436 437 438 439 440
static void device_file_destroy( struct object *obj )
{
    struct device_file *file = (struct device_file *)obj;
    struct irp_call *irp, *next;

    LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
    {
        list_remove( &irp->dev_entry );
        release_object( irp );  /* no longer on the device queue */
    }
    if (file->fd) release_object( file->fd );
    list_remove( &file->entry );
    release_object( file->device );
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
static void set_file_user_ptr( struct device_file *file, client_ptr_t ptr )
{
    struct irp_call *irp;

    if (file->user_ptr == ptr) return;  /* nothing to do */

    file->user_ptr = ptr;

    /* update already queued irps */

    LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
    {
        switch (irp->params.major)
        {
        case IRP_MJ_CLOSE:          irp->params.close.file = ptr; break;
458 459 460 461
        case IRP_MJ_READ:           irp->params.read.file  = ptr; break;
        case IRP_MJ_WRITE:          irp->params.write.file = ptr; break;
        case IRP_MJ_FLUSH_BUFFERS:  irp->params.flush.file = ptr; break;
        case IRP_MJ_DEVICE_CONTROL: irp->params.ioctl.file = ptr; break;
462 463 464 465
        }
    }
}

466
/* queue an irp to the device */
467
static int queue_irp( struct device_file *file, const irp_params_t *params, struct async *async )
468
{
469 470
    struct irp_call *irp = create_irp( file, params, async );
    if (!irp) return 0;
471

472
    fd_queue_async( file->fd, async, ASYNC_TYPE_WAIT );
473
    irp->async = (struct async *)grab_object( async );
474
    add_irp_to_queue( file, irp, current );
475
    release_object( irp );
476
    set_error( STATUS_PENDING );
477
    return 1;
478 479
}

480
static enum server_fd_type device_file_get_fd_type( struct fd *fd )
481
{
482 483 484
    return FD_TYPE_DEVICE;
}

485
static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos )
486 487
{
    struct device_file *file = get_fd_user( fd );
488
    irp_params_t params;
489

490
    memset( &params, 0, sizeof(params) );
491 492 493 494
    params.read.major = IRP_MJ_READ;
    params.read.key   = 0;
    params.read.pos   = pos;
    params.read.file  = file->user_ptr;
495
    return queue_irp( file, &params, async );
496 497
}

498
static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos )
499
{
500
    struct device_file *file = get_fd_user( fd );
501 502
    irp_params_t params;

503
    memset( &params, 0, sizeof(params) );
504 505 506 507
    params.write.major = IRP_MJ_WRITE;
    params.write.key   = 0;
    params.write.pos   = pos;
    params.write.file  = file->user_ptr;
508
    return queue_irp( file, &params, async );
509 510
}

511
static int device_file_flush( struct fd *fd, struct async *async )
512
{
513
    struct device_file *file = get_fd_user( fd );
514 515
    irp_params_t params;

516
    memset( &params, 0, sizeof(params) );
517 518
    params.flush.major = IRP_MJ_FLUSH_BUFFERS;
    params.flush.file  = file->user_ptr;
519
    return queue_irp( file, &params, async );
520 521
}

522
static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
523
{
524
    struct device_file *file = get_fd_user( fd );
525
    irp_params_t params;
526

527
    memset( &params, 0, sizeof(params) );
528 529 530
    params.ioctl.major = IRP_MJ_DEVICE_CONTROL;
    params.ioctl.code  = code;
    params.ioctl.file = file->user_ptr;
531
    return queue_irp( file, &params, async );
532 533
}

534
static struct device *create_device( struct object *root, const struct unicode_str *name,
535 536 537 538
                                     struct device_manager *manager, unsigned int attr )
{
    struct device *device;

539
    if ((device = create_named_object( root, &device_ops, name, attr, NULL )))
540 541 542 543
    {
        if (get_error() != STATUS_OBJECT_NAME_EXISTS)
        {
            /* initialize it if it didn't already exist */
544
            device->unix_path = NULL;
545 546
            device->manager = manager;
            list_add_tail( &manager->devices, &device->entry );
547
            list_init( &device->files );
548 549 550 551 552
        }
    }
    return device;
}

553
struct object *create_unix_device( struct object *root, const struct unicode_str *name,
554 555 556 557
                                   const char *unix_path )
{
    struct device *device;

558
    if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
559 560 561 562 563
    {
        device->unix_path = strdup( unix_path );
        device->manager = NULL;  /* no manager, requests go straight to the Unix device */
        list_init( &device->files );
    }
564
    return &device->obj;
565 566 567

}

568 569
/* terminate requests when the underlying device is deleted */
static void delete_file( struct device_file *file )
570
{
571
    struct irp_call *irp, *next;
572 573

    /* terminate all pending requests */
574
    LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
575
    {
576
        list_remove( &irp->mgr_entry );
577
        set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
578
    }
579 580 581 582 583 584 585 586 587 588 589
}

static void delete_device( struct device *device )
{
    struct device_file *file, *next;

    if (!device->manager) return;  /* already deleted */

    LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
        delete_file( file );

590 591 592 593 594 595 596 597 598 599 600
    unlink_named_object( &device->obj );
    list_remove( &device->entry );
    device->manager = NULL;
}


static void device_manager_dump( struct object *obj, int verbose )
{
    fprintf( stderr, "Device manager\n" );
}

601
static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
602 603 604 605 606 607
{
    struct device_manager *manager = (struct device_manager *)obj;

    return !list_empty( &manager->requests );
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
static void device_manager_destroy( struct object *obj )
{
    struct device_manager *manager = (struct device_manager *)obj;
    struct list *ptr;

    while ((ptr = list_head( &manager->devices )))
    {
        struct device *device = LIST_ENTRY( ptr, struct device, entry );
        delete_device( device );
    }
}

static struct device_manager *create_device_manager(void)
{
    struct device_manager *manager;

    if ((manager = alloc_object( &device_manager_ops )))
    {
        list_init( &manager->devices );
627
        list_init( &manager->requests );
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    }
    return manager;
}


/* create a device manager */
DECL_HANDLER(create_device_manager)
{
    struct device_manager *manager = create_device_manager();

    if (manager)
    {
        reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
        release_object( manager );
    }
}


/* create a device */
DECL_HANDLER(create_device)
{
    struct device *device;
650
    struct unicode_str name = get_req_unicode_str();
651
    struct device_manager *manager;
652
    struct object *root = NULL;
653 654 655 656 657

    if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
                                                             0, &device_manager_ops )))
        return;

658
    if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    {
        release_object( manager );
        return;
    }

    if ((device = create_device( root, &name, manager, req->attributes )))
    {
        device->user_ptr = req->user_ptr;
        reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
        release_object( device );
    }

    if (root) release_object( root );
    release_object( manager );
}


/* delete a device */
DECL_HANDLER(delete_device)
{
    struct device *device;

    if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
    {
        delete_device( device );
        release_object( device );
    }
}
687 688


689
/* retrieve the next pending device irp request */
690 691
DECL_HANDLER(get_next_device_request)
{
692
    struct irp_call *irp;
693 694
    struct device_manager *manager;
    struct list *ptr;
695
    struct iosb *iosb;
696

697 698
    reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1;

699 700 701 702 703 704
    if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
                                                             0, &device_manager_ops )))
        return;

    if (req->prev)
    {
705
        if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
706
        {
707
            set_irp_result( irp, req->status, NULL, 0, 0 );
708
            close_handle( current->process, req->prev );  /* avoid an extra round-trip for close */
709
            release_object( irp );
710 711 712 713 714 715
        }
        clear_error();
    }

    if ((ptr = list_head( &manager->requests )))
    {
716
        irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
717 718 719 720 721
        if (irp->thread)
        {
            reply->client_pid = get_process_id( irp->thread->process );
            reply->client_tid = get_thread_id( irp->thread );
        }
722
        reply->params = irp->params;
723 724 725 726
        iosb = irp->iosb;
        reply->in_size = iosb->in_size;
        reply->out_size = iosb->out_size;
        if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
727
        else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
728
        {
729 730 731
            set_reply_data_ptr( iosb->in_data, iosb->in_size );
            iosb->in_data = NULL;
            iosb->in_size = 0;
732 733
            list_remove( &irp->mgr_entry );
            list_init( &irp->mgr_entry );
734 735 736 737 738 739
        }
    }
    else set_error( STATUS_PENDING );

    release_object( manager );
}
740 741


742 743
/* store results of an async irp */
DECL_HANDLER(set_irp_result)
744
{
745
    struct irp_call *irp;
746

747
    if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
748
    {
749
        if (irp->file) set_file_user_ptr( irp->file, req->file_ptr );
750
        set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
751
        close_handle( current->process, req->handle );  /* avoid an extra round-trip for close */
752
        release_object( irp );
753 754
    }
}