1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* NFSv4.1 client for Windows
 * Copyright © 2012 The Regents of the University of Michigan
 *
 * Olga Kornievskaia <aglo@umich.edu>
 * Casey Bodley <cbodley@umich.edu>
 *
 * 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 Street, Fifth Floor, Boston, MA
 */

#include <windows.h>
#include <strsafe.h>
#include <stdlib.h>
#include "from_kernel.h"
#include "nfs41_ops.h"
#include "daemon_debug.h"
#include "upcall.h"
#include "util.h"


typedef union _FILE_DIR_INFO_UNION {
    ULONG NextEntryOffset;
    FILE_NAMES_INFORMATION fni;
    FILE_DIRECTORY_INFO fdi;
    FILE_FULL_DIR_INFO ffdi;
    FILE_ID_FULL_DIR_INFO fifdi;
    FILE_BOTH_DIR_INFORMATION fbdi;
    FILE_ID_BOTH_DIR_INFO fibdi;
} FILE_DIR_INFO_UNION, *PFILE_DIR_INFO_UNION;


/* NFS41_DIR_QUERY */
static int parse_readdir(unsigned char *buffer, uint32_t length, nfs41_upcall *upcall)
{
    int status;
    readdir_upcall_args *args = &upcall->args.readdir;

    status = safe_read(&buffer, &length, &args->query_class, sizeof(args->query_class));
    if (status) goto out;
    status = safe_read(&buffer, &length, &args->buf_len, sizeof(args->buf_len));
    if (status) goto out;
    status = get_name(&buffer, &length, &args->filter);
    if (status) goto out;
    status = safe_read(&buffer, &length, &args->initial, sizeof(args->initial));
    if (status) goto out;
    status = safe_read(&buffer, &length, &args->restart, sizeof(args->restart));
    if (status) goto out;
    status = safe_read(&buffer, &length, &args->single, sizeof(args->single));
    if (status) goto out;
    status = safe_read(&buffer, &length, &args->kbuf, sizeof(args->kbuf));
    if (status) goto out;
    args->root = upcall->root_ref;
    args->state = upcall->state_ref;

    dprintf(1, "parsing NFS41_DIR_QUERY: info_class=%d buf_len=%d "
        "filter='%s'\n\tInitial\\Restart\\Single %d\\%d\\%d buf=%p\n",
        args->query_class, args->buf_len, args->filter,
        args->initial, args->restart, args->single, args->kbuf);
out:
    return status;
}

#define FILTER_STAR '*'
#define FILTER_QM   '>'

static __inline const char* skip_stars(
    const char *filter)
{
    while (*filter == FILTER_STAR)
        filter++;
    return filter;
}

static int readdir_filter(
    const char *filter,
    const char *name)
{
    const char *f = filter, *n = name;

    while (*f && *n) {
        if (*f == FILTER_STAR) {
            f = skip_stars(f);
            if (*f == '\0')
                return 1;
            while (*n && !readdir_filter(f, n))
                n++;
        } else if (*f == FILTER_QM || *f == *n) {
            f++;
            n++;
        } else
            return 0;
    }
    return *f == *n || *skip_stars(f) == '\0';
}

static uint32_t readdir_size_for_entry(
    IN int query_class,
    IN uint32_t wname_size)
{
    uint32_t needed = wname_size;
    switch (query_class)
    {
    case FileDirectoryInformation:
        needed += FIELD_OFFSET(FILE_DIRECTORY_INFO, FileName);
        break;
    case FileIdFullDirectoryInformation:
        needed += FIELD_OFFSET(FILE_ID_FULL_DIR_INFO, FileName);
        break;
    case FileFullDirectoryInformation:
        needed += FIELD_OFFSET(FILE_FULL_DIR_INFO, FileName);
        break;
    case FileIdBothDirectoryInformation:
        needed += FIELD_OFFSET(FILE_ID_BOTH_DIR_INFO, FileName);
        break;
    case FileBothDirectoryInformation:
        needed += FIELD_OFFSET(FILE_BOTH_DIR_INFORMATION, FileName);
        break;
    case FileNamesInformation:
        needed += FIELD_OFFSET(FILE_NAMES_INFORMATION, FileName);
        break;
    default:
        eprintf("unhandled dir query class %d\n", query_class);
        return 0;
    }
    return needed;
}

static void readdir_copy_dir_info(
    IN nfs41_readdir_entry *entry,
    IN PFILE_DIR_INFO_UNION info)
{
    info->fdi.FileIndex = (ULONG)entry->attr_info.fileid;
    nfs_time_to_file_time(&entry->attr_info.time_create,
        &info->fdi.CreationTime);
    nfs_time_to_file_time(&entry->attr_info.time_access,
        &info->fdi.LastAccessTime);
    nfs_time_to_file_time(&entry->attr_info.time_modify,
        &info->fdi.LastWriteTime);
    /* XXX: was using 'change' attr, but that wasn't giving a time */
    nfs_time_to_file_time(&entry->attr_info.time_modify,
        &info->fdi.ChangeTime);
    info->fdi.EndOfFile.QuadPart =
        info->fdi.AllocationSize.QuadPart =
            entry->attr_info.size;
    info->fdi.FileAttributes = nfs_file_info_to_attributes(
        &entry->attr_info);
}

static void readdir_copy_shortname(
    IN LPCWSTR name,
    OUT LPWSTR name_out,
    OUT CCHAR *name_size_out)
{
    /* GetShortPathName returns number of characters, not including \0 */
    *name_size_out = (CCHAR)GetShortPathNameW(name, name_out, 12);
    if (*name_size_out) {
        *name_size_out++;
        *name_size_out *= sizeof(WCHAR);
    }
}

static void readdir_copy_full_dir_info(
    IN nfs41_readdir_entry *entry,
    IN PFILE_DIR_INFO_UNION info)
{
    readdir_copy_dir_info(entry, info);
    /* for files with the FILE_ATTRIBUTE_REPARSE_POINT attribute,
     * EaSize is used instead to specify its reparse tag. this makes
     * the 'dir' command to show files as <SYMLINK>, and triggers a
     * FSCTL_GET_REPARSE_POINT to query the symlink target
     */
    info->fifdi.EaSize = entry->attr_info.type == NF4LNK ?
        IO_REPARSE_TAG_SYMLINK : 0;
}

static void readdir_copy_both_dir_info(
    IN nfs41_readdir_entry *entry,
    IN LPWSTR wname,
    IN PFILE_DIR_INFO_UNION info)
{
    readdir_copy_full_dir_info(entry, info);
    readdir_copy_shortname(wname, info->fbdi.ShortName,
        &info->fbdi.ShortNameLength);
}

static void readdir_copy_filename(
    IN LPCWSTR name,
    IN uint32_t name_size,
    OUT LPWSTR name_out,
    OUT ULONG *name_size_out)
{
    *name_size_out = name_size;
    memcpy(name_out, name, name_size);
}

static int format_abs_path(
    IN const nfs41_abs_path *path,
    IN const nfs41_component *name,
    OUT nfs41_abs_path *path_out)
{
    /* format an absolute path 'parent\name' */
    int status = NO_ERROR;

    InitializeSRWLock(&path_out->lock);
    abs_path_copy(path_out, path);
    if (FAILED(StringCchPrintfA(path_out->path + path_out->len,
        NFS41_MAX_PATH_LEN - path_out->len, "\\%s", name->name))) {
        status = ERROR_FILENAME_EXCED_RANGE;
        goto out;
    }
    path_out->len += name->len + 1;
out:
    return status;
}

static int lookup_entry(
    IN nfs41_root *root,
    IN nfs41_session *session,
    IN nfs41_path_fh *parent,
    OUT nfs41_readdir_entry *entry)
{
    nfs41_abs_path path;
    nfs41_component name;
    int status;

    name.name = entry->name;
    name.len = (unsigned short)entry->name_len - 1;

    status = format_abs_path(parent->path, &name, &path);
    if (status) goto out;

    status = nfs41_lookup(root, session, &path,
        NULL, NULL, &entry->attr_info, NULL);
    if (status) goto out;
out:
    return status;
}

static int lookup_symlink(
    IN nfs41_root *root,
    IN nfs41_session *session,
    IN nfs41_path_fh *parent,
    IN const nfs41_component *name,
    OUT nfs41_file_info *info_out)
{
    nfs41_abs_path path;
    nfs41_path_fh file;
    nfs41_file_info info;
    int status;

    status = format_abs_path(parent->path, name, &path);
    if (status) goto out;

    file.path = &path;
    status = nfs41_lookup(root, session, &path, NULL, &file, &info, &session);
    if (status) goto out;

    last_component(path.path, path.path + path.len, &file.name);

    status = nfs41_symlink_follow(root, session, &file, &info);
    if (status) goto out;

    info_out->symlink_dir = info.type == NF4DIR;
out:
    return status;
}

static int readdir_copy_entry(
    IN readdir_upcall_args *args,
    IN nfs41_readdir_entry *entry,
    IN OUT unsigned char **dst_pos,
    IN OUT uint32_t *dst_len)
{
    int status = 0;
    WCHAR wname[NFS4_OPAQUE_LIMIT];
    uint32_t wname_len, wname_size, needed;
    PFILE_DIR_INFO_UNION info;

    wname_len = MultiByteToWideChar(CP_UTF8, 0,
        entry->name, entry->name_len, wname, NFS4_OPAQUE_LIMIT);
    wname_size = (wname_len - 1) * sizeof(WCHAR);

    needed = readdir_size_for_entry(args->query_class, wname_size);
    if (!needed || needed > *dst_len) {
        status = -1;
        goto out;
    }

    info = (PFILE_DIR_INFO_UNION)*dst_pos;
    info->NextEntryOffset = align8(needed);
    *dst_pos += info->NextEntryOffset;
    *dst_len -= info->NextEntryOffset;

    if (entry->attr_info.rdattr_error == NFS4ERR_MOVED) {
        entry->attr_info.type = NF4DIR; /* default to dir */
        /* look up attributes for referral entries, but ignore return value;
         * it's okay if lookup fails, we'll just write garbage attributes */
        lookup_entry(args->root, args->state->session,
            &args->state->file, entry);
    } else if (entry->attr_info.type == NF4LNK) {
        nfs41_component name;
        name.name = entry->name;
        name.len = (unsigned short)entry->name_len - 1;
        /* look up the symlink target to see whether it's a directory */
        lookup_symlink(args->root, args->state->session,
            &args->state->file, &name, &entry->attr_info);
    }

    switch (args->query_class)
    {
    case FileNamesInformation:
        info->fni.FileIndex = 0;
        readdir_copy_filename(wname, wname_size,
            info->fni.FileName, &info->fni.FileNameLength);
        break;
    case FileDirectoryInformation:
        readdir_copy_dir_info(entry, info);
        readdir_copy_filename(wname, wname_size,
            info->fdi.FileName, &info->fdi.FileNameLength);
        break;
    case FileFullDirectoryInformation:
        readdir_copy_full_dir_info(entry, info);
        readdir_copy_filename(wname, wname_size,
            info->ffdi.FileName, &info->ffdi.FileNameLength);
        break;
    case FileIdFullDirectoryInformation:
        readdir_copy_full_dir_info(entry, info);
        info->fibdi.FileId.QuadPart = (LONGLONG)entry->attr_info.fileid;
        readdir_copy_filename(wname, wname_size,
            info->fifdi.FileName, &info->fifdi.FileNameLength);
        break;
    case FileBothDirectoryInformation:
        readdir_copy_both_dir_info(entry, wname, info);
        readdir_copy_filename(wname, wname_size,
            info->fbdi.FileName, &info->fbdi.FileNameLength);
        break;
    case FileIdBothDirectoryInformation:
        readdir_copy_both_dir_info(entry, wname, info);
        info->fibdi.FileId.QuadPart = (LONGLONG)entry->attr_info.fileid;
        readdir_copy_filename(wname, wname_size,
            info->fibdi.FileName, &info->fibdi.FileNameLength);
        break;
    default:
        eprintf("unhandled dir query class %d\n", args->query_class);
        status = -1;
        break;
    }
out:
    return status;
}

#define COOKIE_DOT      ((uint64_t)-2)
#define COOKIE_DOTDOT   ((uint64_t)-1)

static int readdir_add_dots(
    IN readdir_upcall_args *args,
    IN OUT unsigned char *entry_buf,
    IN uint32_t entry_buf_len,
    OUT uint32_t *len_out,
    OUT uint32_t **last_offset)
{
    int status = 0;
    const uint32_t entry_len = (uint32_t)FIELD_OFFSET(nfs41_readdir_entry, name);
    nfs41_readdir_entry *entry;
    nfs41_open_state *state = args->state;

    *len_out = 0;
    *last_offset = NULL;
    switch (state->cookie.cookie) {
    case 0:
        if (entry_buf_len < entry_len + 2) {
            status = ERROR_BUFFER_OVERFLOW;
            dprintf(1, "not enough room for '.' entry. received %d need %d\n",
                    entry_buf_len, entry_len + 2);
            args->query_reply_len = entry_len + 2;
            goto out;
        }

        entry = (nfs41_readdir_entry*)entry_buf;
        ZeroMemory(&entry->attr_info, sizeof(nfs41_file_info));

        status = nfs41_cached_getattr(state->session,
            &state->file, &entry->attr_info);
        if (status) {
            dprintf(1, "failed to add '.' entry.\n");
            goto out;
        }
        entry->cookie = COOKIE_DOT;
        entry->name_len = 2;
        StringCbCopyA(entry->name, entry->name_len, ".");
        entry->next_entry_offset = entry_len + entry->name_len;

        entry_buf += entry->next_entry_offset;
        entry_buf_len -= entry->next_entry_offset;
        *len_out += entry->next_entry_offset;
        *last_offset = &entry->next_entry_offset;
        if (args->single)
            break;
        /* else no break! */
    case COOKIE_DOT:
        if (entry_buf_len < entry_len + 3) {
            status = ERROR_BUFFER_OVERFLOW;
            dprintf(1, "not enough room for '..' entry. received %d need %d\n",
                    entry_buf_len, entry_len);
            args->query_reply_len = entry_len + 2;
            goto out;
        }
        /* XXX: this skips '..' when listing root fh */
        if (state->file.name.len == 0)
            break;

        entry = (nfs41_readdir_entry*)entry_buf;
        ZeroMemory(&entry->attr_info, sizeof(nfs41_file_info));

        status = nfs41_cached_getattr(state->session,
            &state->parent, &entry->attr_info);
        if (status) {
            status = ERROR_FILE_NOT_FOUND;
            dprintf(1, "failed to add '..' entry.\n");
            goto out;
        }
        entry->cookie = COOKIE_DOTDOT;
        entry->name_len = 3;
        StringCbCopyA(entry->name, entry->name_len, "..");
        entry->next_entry_offset = entry_len + entry->name_len;

        entry_buf += entry->next_entry_offset;<--- Assignment of function parameter has no effect outside the function. Did you forget dereferencing it?
        entry_buf_len -= entry->next_entry_offset;<--- Assignment of function parameter has no effect outside the function.
        *len_out += entry->next_entry_offset;
        *last_offset = &entry->next_entry_offset;
        break;
    }
    if (state->cookie.cookie == COOKIE_DOTDOT ||
        state->cookie.cookie == COOKIE_DOT)
        ZeroMemory(&state->cookie, sizeof(nfs41_readdir_cookie));
out:
    return status;
}

static int handle_readdir(nfs41_upcall *upcall)
{
    int status;
    readdir_upcall_args *args = &upcall->args.readdir;
    nfs41_open_state *state = upcall->state_ref;
    unsigned char *entry_buf = NULL;
    uint32_t entry_buf_len;
    bitmap4 attr_request;
    bool_t eof;
    /* make sure we allocate enough space for one nfs41_readdir_entry */
    const uint32_t max_buf_len = max(args->buf_len,
        sizeof(nfs41_readdir_entry) + NFS41_MAX_COMPONENT_LEN);

    dprintf(1, "-> handle_nfs41_dirquery(%s,%d,%d,%d)\n",
        args->filter, args->initial, args->restart, args->single);

    args->query_reply_len = 0;

    if (args->initial || args->restart) {
        ZeroMemory(&state->cookie, sizeof(nfs41_readdir_cookie));
        if (!state->cookie.cookie)
            dprintf(1, "initializing the 1st readdir cookie\n");
        else if (args->restart)
            dprintf(1, "restarting; clearing previous cookie %llu\n",
                state->cookie.cookie);
        else if (args->initial)
            dprintf(1, "*** initial; clearing previous cookie %llu!\n",
                state->cookie.cookie);
    } else if (!state->cookie.cookie) {
        dprintf(1, "handle_nfs41_readdir: EOF\n");
        status = ERROR_NO_MORE_FILES;
        goto out;
    }

    entry_buf = calloc(max_buf_len, sizeof(unsigned char));
    if (entry_buf == NULL) {
        status = GetLastError();
        goto out_free_cookie;
    }
fetch_entries:
    entry_buf_len = max_buf_len;

    nfs41_superblock_getattr_mask(state->file.fh.superblock, &attr_request);
    attr_request.arr[0] |= FATTR4_WORD0_RDATTR_ERROR;

    if (strchr(args->filter, FILTER_STAR) || strchr(args->filter, FILTER_QM)) {
        /* use READDIR for wildcards */

        uint32_t dots_len = 0;
        uint32_t *dots_next_offset = NULL;

        if (args->filter[0] == '*' && args->filter[1] == '\0') {
            status = readdir_add_dots(args, entry_buf,
                entry_buf_len, &dots_len, &dots_next_offset);
            if (status)
                goto out_free_cookie;
            entry_buf_len -= dots_len;
        }

        if (dots_len && args->single) {
            dprintf(2, "skipping nfs41_readdir because the single query "
                "will use . or ..\n");
            entry_buf_len = 0;
            eof = 0;
        } else {
            dprintf(2, "calling nfs41_readdir with cookie %llu\n",
                state->cookie.cookie);
            status = nfs41_readdir(state->session, &state->file,
                &attr_request, &state->cookie, entry_buf + dots_len,
                &entry_buf_len, &eof);
            if (status) {
                dprintf(1, "nfs41_readdir failed with %s\n",
                    nfs_error_string(status));
                status = nfs_to_windows_error(status, ERROR_BAD_NET_RESP);
                goto out_free_cookie;
            }
        }

        if (!entry_buf_len && dots_next_offset)
            *dots_next_offset = 0;
        entry_buf_len += dots_len;
    } else {
        /* use LOOKUP for single files */
        nfs41_readdir_entry *entry = (nfs41_readdir_entry*)entry_buf;
        entry->cookie = 0;
        entry->name_len = (uint32_t)strlen(args->filter) + 1;
        StringCbCopyA(entry->name, entry->name_len, args->filter);
        entry->next_entry_offset = 0;

        status = lookup_entry(upcall->root_ref,
             state->session, &state->file, entry);
        if (status) {
            dprintf(1, "single_lookup failed with %d\n", status);
            goto out_free_cookie;
        }
        entry_buf_len = entry->name_len +
                FIELD_OFFSET(nfs41_readdir_entry, name);

        eof = 1;
    }

    status = args->initial ? ERROR_FILE_NOT_FOUND : ERROR_NO_MORE_FILES;

    if (entry_buf_len) {
        unsigned char *entry_pos = entry_buf;
        unsigned char *dst_pos = args->kbuf;
        uint32_t dst_len = args->buf_len;
        nfs41_readdir_entry *entry;
        PULONG offset, last_offset = NULL;

        for (;;) {
            entry = (nfs41_readdir_entry*)entry_pos;
            offset = (PULONG)dst_pos; /* ULONG NextEntryOffset */

            dprintf(2, "filter %s looking at %s with cookie %d\n",
                args->filter, entry->name, entry->cookie);
            if (readdir_filter((const char*)args->filter, entry->name)) {
                if (readdir_copy_entry(args, entry, &dst_pos, &dst_len)) {
                    eof = 0;
                    dprintf(2, "not enough space to copy entry %s (cookie %d)\n",
                        entry->name, entry->cookie);
                    break;
                }
                last_offset = offset;
                status = NO_ERROR;
            }
            state->cookie.cookie = entry->cookie;

            /* last entry we got from the server */
            if (!entry->next_entry_offset)
                break;

            /* we found our single entry, but the server has more */
            if (args->single && last_offset) {
                eof = 0;
                break;
            }
            entry_pos += entry->next_entry_offset;
        }
        args->query_reply_len = args->buf_len - dst_len;
        if (last_offset) {
            *last_offset = 0;
        } else if (!eof) {
            dprintf(1, "no entries matched; fetch more\n");
            goto fetch_entries;
        }
    }

    if (eof) {
        dprintf(1, "we don't need to save a cookie\n");
        goto out_free_cookie;
    } else
        dprintf(1, "saving cookie %llu\n", state->cookie.cookie);

out_free_entry:
    free(entry_buf);
out:
    dprintf(1, "<- handle_nfs41_dirquery(%s,%d,%d,%d) returning ",
        args->filter, args->initial, args->restart, args->single);
    if (status) {
        switch (status) {
        case ERROR_FILE_NOT_FOUND:
            dprintf(1, "ERROR_FILE_NOT_FOUND.\n");
            break;
        case ERROR_NO_MORE_FILES:
            dprintf(1, "ERROR_NO_MORE_FILES.\n");
            break;
        case ERROR_BUFFER_OVERFLOW:
            upcall->last_error = status;
            status = ERROR_SUCCESS;
            break;
        default:
            dprintf(1, "error code %d.\n", status);
            break;
        }
    } else {
        dprintf(1, "success!\n");
    }
    return status;
out_free_cookie:
    state->cookie.cookie = 0;
    goto out_free_entry;
}

static int marshall_readdir(unsigned char *buffer, uint32_t *length, nfs41_upcall *upcall)
{
    int status;
    readdir_upcall_args *args = &upcall->args.readdir;

    status = safe_write(&buffer, length, &args->query_reply_len, sizeof(args->query_reply_len));
    return status;
}


const nfs41_upcall_op nfs41_op_readdir = {
    parse_readdir,
    handle_readdir,
    marshall_readdir
};