summaryrefslogtreecommitdiffstats
path: root/xlators/nfs/server/src/acl3.c
blob: 0eca45d8a3e6bed0579656727b35f0598fb70f96 (plain)
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
650
651
652
653
654
655
656
657
658
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*
 * Copyright (c) 2012-2013 Red Hat, Inc. <http://www.redhat.com>
 * This file is part of GlusterFS.
 *
 * This file is licensed to you under your choice of the GNU Lesser
 * General Public License, version 3 or any later version (LGPLv3 or
 * later), or the GNU General Public License, version 2 (GPLv2), in all
 * cases as published by the Free Software Foundation.
 */

#include <glusterfs/defaults.h>
#include "rpcsvc.h"
#include <glusterfs/dict.h>
#include <glusterfs/xlator.h>
#include "nfs.h"
#include <glusterfs/mem-pool.h>
#include <glusterfs/logging.h>
#include "nfs-fops.h"
#include "nfs3.h"
#include "nfs-mem-types.h"
#include "nfs3-helpers.h"
#include "nfs3-fh.h"
#include "nfs-generics.h"
#include "acl3.h"
#include <glusterfs/byte-order.h>
#include <glusterfs/compat-errno.h>
#include "nfs-messages.h"

static int
acl3_nfs_acl_to_xattr(aclentry *ace, void *xattrbuf, int aclcount, int defacl);

static int
acl3_nfs_acl_from_xattr(aclentry *ace, void *xattrbuf, int bufsize, int defacl);

typedef ssize_t (*acl3_serializer)(struct iovec outmsg, void *args);

extern void
nfs3_call_state_wipe(nfs3_call_state_t *cs);

extern nfs3_call_state_t *
nfs3_call_state_init(struct nfs3_state *s, rpcsvc_request_t *req, xlator_t *v);

extern int
nfs3_fh_validate(struct nfs3_fh *fh);

extern void
nfs3_stat_to_fattr3(struct iatt *buf, fattr3 *fa);

#define acl3_validate_nfs3_state(request, state, status, label, retval)        \
    do {                                                                       \
        state = rpcsvc_request_program_private(request);                       \
        if (!state) {                                                          \
            gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_STATE_MISSING,         \
                   "NFSv3 state "                                              \
                   "missing from RPC request");                                \
            rpcsvc_request_seterr(req, SYSTEM_ERR);                            \
            status = NFS3ERR_SERVERFAULT;                                      \
            goto label;                                                        \
        }                                                                      \
    } while (0);

#define acl3_validate_gluster_fh(handle, status, errlabel)                     \
    do {                                                                       \
        if (!nfs3_fh_validate(handle)) {                                       \
            gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_BAD_HANDLE,           \
                   "Bad Handle");                                              \
            status = NFS3ERR_BADHANDLE;                                        \
            goto errlabel;                                                     \
        }                                                                      \
    } while (0)

extern xlator_t *
nfs3_fh_to_xlator(struct nfs3_state *nfs3, struct nfs3_fh *fh);

#define acl3_map_fh_to_volume(nfs3state, handle, req, volume, status, label)   \
    do {                                                                       \
        char exportid[256], gfid[256];                                         \
        rpc_transport_t *trans = NULL;                                         \
        volume = nfs3_fh_to_xlator((nfs3state), handle);                       \
        if (!volume) {                                                         \
            gf_uuid_unparse(handle->exportid, exportid);                       \
            gf_uuid_unparse(handle->gfid, gfid);                               \
            trans = rpcsvc_request_transport(req);                             \
            gf_msg(GF_ACL, GF_LOG_ERROR, 0, NFS_MSG_FH_TO_VOL_FAIL,            \
                   "Failed to map "                                            \
                   "FH to vol: client=%s, exportid=%s, gfid=%s",               \
                   trans->peerinfo.identifier, exportid, gfid);                \
            gf_msg(GF_ACL, GF_LOG_ERROR, ESTALE, NFS_MSG_VOLUME_ERROR,         \
                   "Stale nfs client %s must be trying to "                    \
                   "connect to a deleted volume, please "                      \
                   "unmount it.",                                              \
                   trans->peerinfo.identifier);                                \
            status = NFS3ERR_STALE;                                            \
            goto label;                                                        \
        } else {                                                               \
            gf_msg_trace(GF_ACL, 0, "FH to Volume: %s", volume->name);         \
            rpcsvc_request_set_private(req, volume);                           \
        }                                                                      \
    } while (0);

#define acl3_volume_started_check(nfs3state, vlm, rtval, erlbl)                \
    do {                                                                       \
        if ((!nfs_subvolume_started(nfs_state(nfs3state->nfsx), vlm))) {       \
            gf_msg(GF_ACL, GF_LOG_ERROR, 0, NFS_MSG_VOL_DISABLE,               \
                   "Volume is disabled: %s", vlm->name);                       \
            rtval = RPCSVC_ACTOR_IGNORE;                                       \
            goto erlbl;                                                        \
        }                                                                      \
    } while (0)

#define acl3_check_fh_resolve_status(cst, nfstat, erlabl)                      \
    do {                                                                       \
        xlator_t *xlatorp = NULL;                                              \
        char buf[256], gfid[GF_UUID_BUF_SIZE];                                 \
        rpc_transport_t *trans = NULL;                                         \
        if ((cst)->resolve_ret < 0) {                                          \
            trans = rpcsvc_request_transport(cst->req);                        \
            xlatorp = nfs3_fh_to_xlator(cst->nfs3state, &cst->resolvefh);      \
            gf_uuid_unparse(cst->resolvefh.gfid, gfid);                        \
            snprintf(buf, sizeof(buf), "(%s) %s : %s",                         \
                     trans->peerinfo.identifier,                               \
                     xlatorp ? xlatorp->name : "ERR", gfid);                   \
            gf_msg(GF_ACL, GF_LOG_ERROR, cst->resolve_errno,                   \
                   NFS_MSG_RESOLVE_FH_FAIL,                                    \
                   "Unable to resolve "                                        \
                   "FH: %s",                                                   \
                   buf);                                                       \
            nfstat = nfs3_errno_to_nfsstat3(cst->resolve_errno);               \
            goto erlabl;                                                       \
        }                                                                      \
    } while (0)

#define acl3_handle_call_state_init(nfs3state, calls, rq, v, opstat, errlabel) \
    do {                                                                       \
        calls = nfs3_call_state_init((nfs3state), (rq), v);                    \
        if (!calls) {                                                          \
            gf_msg(GF_ACL, GF_LOG_ERROR, 0, NFS_MSG_INIT_CALL_STAT_FAIL,       \
                   "Failed to "                                                \
                   "init call state");                                         \
            opstat = NFS3ERR_SERVERFAULT;                                      \
            rpcsvc_request_seterr(req, SYSTEM_ERR);                            \
            goto errlabel;                                                     \
        }                                                                      \
    } while (0)

int
acl3svc_submit_reply(rpcsvc_request_t *req, void *arg, acl3_serializer sfunc)
{
    struct iovec outmsg = {
        0,
    };
    struct iobuf *iob = NULL;
    struct nfs3_state *nfs3 = NULL;
    int ret = -1;
    ssize_t msglen = 0;
    struct iobref *iobref = NULL;

    if (!req)
        return -1;

    nfs3 = (struct nfs3_state *)rpcsvc_request_program_private(req);
    if (!nfs3) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_MNT_STATE_NOT_FOUND,
               "mount state not found");
        goto ret;
    }

    /* First, get the io buffer into which the reply in arg will
     * be serialized.
     */
    iob = iobuf_get(nfs3->iobpool);
    if (!iob) {
        gf_msg(GF_ACL, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to get iobuf");
        goto ret;
    }

    iobuf_to_iovec(iob, &outmsg);
    /* Use the given serializer to translate the give C structure in arg
     * to XDR format which will be written into the buffer in outmsg.
     */
    msglen = sfunc(outmsg, arg);
    if (msglen < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_ENCODE_MSG_FAIL,
               "Failed to encode message");
        goto ret;
    }
    outmsg.iov_len = msglen;

    iobref = iobref_new();
    if (iobref == NULL) {
        gf_msg(GF_ACL, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to get iobref");
        goto ret;
    }

    ret = iobref_add(iobref, iob);
    if (ret) {
        gf_msg(GF_ACL, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
               "Failed to add iob to iobref");
        goto ret;
    }

    /* Then, submit the message for transmission. */
    ret = rpcsvc_submit_message(req, &outmsg, 1, NULL, 0, iobref);
    if (ret == -1) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_REP_SUBMIT_FAIL,
               "Reply submission failed");
        goto ret;
    }

    ret = 0;
ret:
    if (iob)
        iobuf_unref(iob);
    if (iobref)
        iobref_unref(iobref);

    return ret;
}

int
acl3svc_null(rpcsvc_request_t *req)
{
    struct iovec dummyvec = {
        0,
    };

    if (!req) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
               "Got NULL request!");
        return 0;
    }
    rpcsvc_submit_generic(req, &dummyvec, 1, NULL, 0, NULL);
    return 0;
}

int
acl3_getacl_reply(rpcsvc_request_t *req, getaclreply *reply)
{
    acl3svc_submit_reply(req, (void *)reply,
                         (acl3_serializer)xdr_serialize_getaclreply);
    return 0;
}

int
acl3_setacl_reply(rpcsvc_request_t *req, setaclreply *reply)
{
    acl3svc_submit_reply(req, (void *)reply,
                         (acl3_serializer)xdr_serialize_setaclreply);
    return 0;
}

/* acl3_getacl_cbk: fetch and decode the ACL in the POSIX_ACL_ACCESS_XATTR
 *
 * The POSIX_ACL_ACCESS_XATTR can be set on files and directories.
 */
int
acl3_getacl_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
                int32_t op_ret, int32_t op_errno, dict_t *dict, dict_t *xdata)
{
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    nfs3_call_state_t *cs = NULL;
    data_t *data = NULL;
    getaclreply *getaclreply = NULL;
    int aclcount = 0;
    int defacl = 1; /* DEFAULT ACL */

    if (!frame->local) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
               "Invalid argument, frame->local NULL");
        return -EINVAL;
    }
    cs = frame->local;
    getaclreply = &cs->args.getaclreply;
    if ((op_ret < 0) && (op_errno != ENODATA && op_errno != ENOATTR)) {
        stat = nfs3_cbk_errno_status(op_ret, op_errno);
        goto err;
    } else if (!dict) {
        /* no ACL has been set */
        stat = NFS3_OK;
        goto err;
    }

    getaclreply->aclentry.aclentry_val = cs->aclentry;

    /* getfacl: NFS USER ACL */
    data = dict_get(dict, POSIX_ACL_ACCESS_XATTR);
    if (data && data->data) {
        aclcount = acl3_nfs_acl_from_xattr(cs->aclentry, data->data, data->len,
                                           !defacl);
        if (aclcount < 0) {
            gf_msg(GF_ACL, GF_LOG_ERROR, aclcount, NFS_MSG_GET_USER_ACL_FAIL,
                   "Failed to get USER ACL");
            stat = nfs3_errno_to_nfsstat3(-aclcount);
            goto err;
        }
        getaclreply->aclcount = aclcount;
        getaclreply->aclentry.aclentry_len = aclcount;
    }

    acl3_getacl_reply(cs->req, getaclreply);
    nfs3_call_state_wipe(cs);
    return 0;

err:
    if (getaclreply)
        getaclreply->status = stat;
    acl3_getacl_reply(cs->req, getaclreply);
    nfs3_call_state_wipe(cs);
    return 0;
}

/* acl3_default_getacl_cbk: fetch and decode the ACL set in the
 * POSIX_ACL_DEFAULT_XATTR xattr.
 *
 * The POSIX_ACL_DEFAULT_XATTR xattr is only set on directories, not on files.
 *
 * When done with POSIX_ACL_DEFAULT_XATTR, we also need to get and decode the
 * ACL that can be set in POSIX_ACL_DEFAULT_XATTR.
 */
int
acl3_default_getacl_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, dict_t *dict,
                        dict_t *xdata)
{
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    nfs3_call_state_t *cs = NULL;
    data_t *data = NULL;
    getaclreply *getaclreply = NULL;
    int aclcount = 0;
    int defacl = 1; /* DEFAULT ACL */
    nfs_user_t nfu = {
        0,
    };
    int ret = -1;

    if (!frame->local) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
               "Invalid argument, frame->local NULL");
        return -EINVAL;
    }
    cs = frame->local;
    getaclreply = &cs->args.getaclreply;
    if ((op_ret < 0) && (op_errno != ENODATA && op_errno != ENOATTR)) {
        stat = nfs3_cbk_errno_status(op_ret, op_errno);
        goto err;
    } else if (!dict) {
        /* no ACL has been set */
        stat = NFS3_OK;
        goto err;
    }

    getaclreply->daclentry.daclentry_val = cs->daclentry;

    /* getfacl: NFS DEFAULT ACL */
    data = dict_get(dict, POSIX_ACL_DEFAULT_XATTR);
    if (data && data->data) {
        aclcount = acl3_nfs_acl_from_xattr(cs->daclentry, data->data, data->len,
                                           defacl);
        if (aclcount < 0) {
            gf_msg(GF_ACL, GF_LOG_ERROR, aclcount, NFS_MSG_GET_DEF_ACL_FAIL,
                   "Failed to get DEFAULT ACL");
            stat = nfs3_errno_to_nfsstat3(-aclcount);
            goto err;
        }

        getaclreply->daclcount = aclcount;
        getaclreply->daclentry.daclentry_len = aclcount;
    }

    getaclreply->attr_follows = TRUE;
    nfs_request_user_init(&nfu, cs->req);
    ret = nfs_getxattr(cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                       POSIX_ACL_ACCESS_XATTR, NULL, acl3_getacl_cbk, cs);
    if (ret < 0) {
        stat = nfs3_errno_to_nfsstat3(-ret);
        goto err;
    }

    return 0;

err:
    if (getaclreply)
        getaclreply->status = stat;
    acl3_getacl_reply(cs->req, getaclreply);
    nfs3_call_state_wipe(cs);
    return 0;
}

int
acl3_stat_cbk(call_frame_t *frame, void *cookie, xlator_t *this, int32_t op_ret,
              int32_t op_errno, struct iatt *buf, dict_t *xdata)
{
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    nfs3_call_state_t *cs = NULL;
    getaclreply *getaclreply = NULL;
    int ret = -1;
    nfs_user_t nfu = {
        0,
    };
    uint64_t deviceid = 0;

    if (!frame->local) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
               "Invalid argument, frame->local NULL");
        return EINVAL;
    }

    cs = frame->local;
    getaclreply = &cs->args.getaclreply;

    if (op_ret == -1) {
        stat = nfs3_cbk_errno_status(op_ret, op_errno);
        goto err;
    }

    /* Fill the attrs before xattrs */
    getaclreply->attr_follows = TRUE;
    deviceid = nfs3_request_xlator_deviceid(cs->req);
    nfs3_map_deviceid_to_statdev(buf, deviceid);
    nfs3_stat_to_fattr3(buf, &(getaclreply->attr));

    nfs_request_user_init(&nfu, cs->req);
    if (buf->ia_type == IA_IFDIR) {
        ret = nfs_getxattr(cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                           POSIX_ACL_DEFAULT_XATTR, NULL,
                           acl3_default_getacl_cbk, cs);
    } else {
        ret = nfs_getxattr(cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                           POSIX_ACL_ACCESS_XATTR, NULL, acl3_getacl_cbk, cs);
    }

    if (ret < 0) {
        stat = nfs3_errno_to_nfsstat3(-ret);
        goto err;
    }

    return 0;
err:
    getaclreply->status = stat;
    acl3_getacl_reply(cs->req, getaclreply);
    nfs3_call_state_wipe(cs);
    return 0;
}

int
acl3_getacl_resume(void *carg)
{
    int ret = -1;
    nfs3_call_state_t *cs = NULL;
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    nfs_user_t nfu = {
        0,
    };

    if (!carg)
        return ret;

    cs = (nfs3_call_state_t *)carg;
    acl3_check_fh_resolve_status(cs, stat, acl3err);
    nfs_request_user_init(&nfu, cs->req);

    ret = nfs_stat(cs->nfsx, cs->vol, &nfu, &cs->resolvedloc, acl3_stat_cbk,
                   cs);
    stat = -ret;
acl3err:
    if (ret < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, stat, NFS_MSG_OPEN_FAIL,
               "unable to open_and_resume");
        cs->args.getaclreply.status = nfs3_errno_to_nfsstat3(stat);
        acl3_getacl_reply(cs->req, &cs->args.getaclreply);
        nfs3_call_state_wipe(cs);
    }

    return ret;
}

int
acl3svc_getacl(rpcsvc_request_t *req)
{
    xlator_t *vol = NULL;
    struct nfs_state *nfs = NULL;
    nfs3_state_t *nfs3 = NULL;
    nfs3_call_state_t *cs = NULL;
    int ret = RPCSVC_ACTOR_ERROR;
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    struct nfs3_fh fh, *fhp = NULL;
    getaclargs getaclargs;
    getaclreply getaclreply;

    if (!req)
        return ret;

    acl3_validate_nfs3_state(req, nfs3, stat, rpcerr, ret);
    nfs = nfs_state(nfs3->nfsx);
    memset(&getaclargs, 0, sizeof(getaclargs));
    memset(&getaclreply, 0, sizeof(getaclreply));
    getaclargs.fh.n_bytes = (char *)&fh;
    if (xdr_to_getaclargs(req->msg[0], &getaclargs) <= 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_ARGS_DECODE_ERROR,
               "Error decoding args");
        rpcsvc_request_seterr(req, GARBAGE_ARGS);
        goto rpcerr;
    }

    /* Validate ACL mask */
    if (getaclargs.mask & ~(NFS_ACL | NFS_ACLCNT | NFS_DFACL | NFS_DFACLCNT)) {
        stat = NFS3ERR_INVAL;
        goto acl3err;
    }

    fhp = &fh;
    acl3_validate_gluster_fh(&fh, stat, acl3err);
    acl3_map_fh_to_volume(nfs->nfs3state, fhp, req, vol, stat, acl3err);
    acl3_volume_started_check(nfs3, vol, ret, rpcerr);
    acl3_handle_call_state_init(nfs->nfs3state, cs, req, vol, stat, acl3err);

    cs->vol = vol;
    cs->args.getaclreply.mask = getaclargs.mask;

    ret = nfs3_fh_resolve_and_resume(cs, fhp, NULL, acl3_getacl_resume);
    stat = nfs3_errno_to_nfsstat3(-ret);

acl3err:
    if (ret < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, -ret, NFS_MSG_RESOLVE_ERROR,
               "unable to resolve and resume");
        getaclreply.status = stat;
        acl3_getacl_reply(req, &getaclreply);
        nfs3_call_state_wipe(cs);
        return 0;
    }

rpcerr:
    return ret;
}

int
acl3_setacl_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
                int32_t op_ret, int32_t op_errno, dict_t *xdata)
{
    nfs3_call_state_t *cs = NULL;
    cs = frame->local;
    if (op_ret < 0) {
        nfsstat3 status = nfs3_cbk_errno_status(op_ret, op_errno);
        cs->args.setaclreply.status = status;
    }

    acl3_setacl_reply(cs->req, &cs->args.setaclreply);

    nfs3_call_state_wipe(cs);

    return 0;
}

int
acl3_setacl_resume(void *carg)
{
    int ret = -1;
    nfs3_call_state_t *cs = NULL;
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    nfs_user_t nfu = {
        0,
    };
    dict_t *xattr = NULL;

    if (!carg)
        return ret;
    cs = (nfs3_call_state_t *)carg;
    acl3_check_fh_resolve_status(cs, stat, acl3err);
    nfs_request_user_init(&nfu, cs->req);
    xattr = dict_new();
    if (cs->aclcount)
        ret = dict_set_static_bin(xattr, POSIX_ACL_ACCESS_XATTR, cs->aclxattr,
                                  posix_acl_xattr_size(cs->aclcount));
    if (cs->daclcount)
        ret = dict_set_static_bin(xattr, POSIX_ACL_DEFAULT_XATTR, cs->daclxattr,
                                  posix_acl_xattr_size(cs->daclcount));

    ret = nfs_setxattr(cs->nfsx, cs->vol, &nfu, &cs->resolvedloc, xattr, 0,
                       NULL, acl3_setacl_cbk, cs);
    dict_unref(xattr);

acl3err:
    if (ret < 0) {
        stat = -ret;
        gf_msg(GF_ACL, GF_LOG_ERROR, stat, NFS_MSG_OPEN_FAIL,
               "unable to open_and_resume");
        cs->args.setaclreply.status = nfs3_errno_to_nfsstat3(stat);
        acl3_setacl_reply(cs->req, &cs->args.setaclreply);
        nfs3_call_state_wipe(cs);
    }

    return ret;
}

int
acl3svc_setacl(rpcsvc_request_t *req)
{
    xlator_t *vol = NULL;
    struct nfs_state *nfs = NULL;
    nfs3_state_t *nfs3 = NULL;
    nfs3_call_state_t *cs = NULL;
    int ret = RPCSVC_ACTOR_ERROR;
    nfsstat3 stat = NFS3ERR_SERVERFAULT;
    struct nfs3_fh fh;
    struct nfs3_fh *fhp = NULL;
    setaclargs setaclargs;
    setaclreply setaclreply;
    aclentry *daclentry = NULL;
    aclentry *aclentry = NULL;
    int aclerrno = 0;
    int defacl = 1;

    if (!req)
        return ret;
    aclentry = GF_CALLOC(NFS_ACL_MAX_ENTRIES, sizeof(*aclentry), gf_nfs_mt_arr);
    if (!aclentry) {
        goto rpcerr;
    }
    daclentry = GF_CALLOC(NFS_ACL_MAX_ENTRIES, sizeof(*daclentry),
                          gf_nfs_mt_arr);
    if (!daclentry) {
        goto rpcerr;
    }

    acl3_validate_nfs3_state(req, nfs3, stat, rpcerr, ret);
    nfs = nfs_state(nfs3->nfsx);
    memset(&setaclargs, 0, sizeof(setaclargs));
    memset(&setaclreply, 0, sizeof(setaclreply));
    memset(&fh, 0, sizeof(fh));
    setaclargs.fh.n_bytes = (char *)&fh;
    setaclargs.aclentry.aclentry_val = aclentry;
    setaclargs.daclentry.daclentry_val = daclentry;
    if (xdr_to_setaclargs(req->msg[0], &setaclargs) <= 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_ARGS_DECODE_ERROR,
               "Error decoding args");
        rpcsvc_request_seterr(req, GARBAGE_ARGS);
        goto rpcerr;
    }

    /* Validate ACL mask */
    if (setaclargs.mask & ~(NFS_ACL | NFS_ACLCNT | NFS_DFACL | NFS_DFACLCNT)) {
        stat = NFS3ERR_INVAL;
        goto acl3err;
    }

    fhp = &fh;
    acl3_validate_gluster_fh(fhp, stat, acl3err);
    acl3_map_fh_to_volume(nfs->nfs3state, fhp, req, vol, stat, acl3err);
    acl3_volume_started_check(nfs3, vol, ret, rpcerr);
    acl3_handle_call_state_init(nfs->nfs3state, cs, req, vol, stat, acl3err);

    cs->vol = vol;
    cs->aclcount = setaclargs.aclcount;
    cs->daclcount = setaclargs.daclcount;

    /* setfacl: NFS USER ACL */
    aclerrno = acl3_nfs_acl_to_xattr(aclentry, cs->aclxattr, cs->aclcount,
                                     !defacl);
    if (aclerrno < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, -aclerrno, NFS_MSG_SET_USER_ACL_FAIL,
               "Failed to set USER ACL");
        stat = nfs3_errno_to_nfsstat3(-aclerrno);
        goto acl3err;
    }

    /* setfacl: NFS DEFAULT ACL */
    aclerrno = acl3_nfs_acl_to_xattr(daclentry, cs->daclxattr, cs->daclcount,
                                     defacl);
    if (aclerrno < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, -aclerrno, NFS_MSG_SET_DEF_ACL_FAIL,
               "Failed to set DEFAULT ACL");
        stat = nfs3_errno_to_nfsstat3(-aclerrno);
        goto acl3err;
    }

    ret = nfs3_fh_resolve_and_resume(cs, fhp, NULL, acl3_setacl_resume);
    stat = nfs3_errno_to_nfsstat3(-ret);

acl3err:
    if (ret < 0) {
        gf_msg(GF_ACL, GF_LOG_ERROR, -ret, NFS_MSG_RESOLVE_ERROR,
               "unable to resolve and resume");
        setaclreply.status = stat;
        acl3_setacl_reply(req, &setaclreply);
        nfs3_call_state_wipe(cs);
        GF_FREE(aclentry);
        GF_FREE(daclentry);
        return 0;
    }

rpcerr:
    if (ret < 0)
        nfs3_call_state_wipe(cs);
    if (aclentry)
        GF_FREE(aclentry);
    if (daclentry)
        GF_FREE(daclentry);
    return ret;
}

rpcsvc_actor_t acl3svc_actors[ACL3_PROC_COUNT] = {
    {"NULL", ACL3_NULL, acl3svc_null, NULL, 0, DRC_NA},
    {"GETACL", ACL3_GETACL, acl3svc_getacl, NULL, 0, DRC_NA},
    {"SETACL", ACL3_SETACL, acl3svc_setacl, NULL, 0, DRC_NA},
};

rpcsvc_program_t acl3prog = {
    .progname = "ACL3",
    .prognum = ACL_PROGRAM,
    .progver = ACLV3_VERSION,
    .progport = GF_NFS3_PORT,
    .actors = acl3svc_actors,
    .numactors = ACL3_PROC_COUNT,
    .min_auth = AUTH_NULL,
};

rpcsvc_program_t *
acl3svc_init(xlator_t *nfsx)
{
    struct nfs3_state *ns = NULL;
    struct nfs_state *nfs = NULL;
    dict_t *options = NULL;
    int ret = -1;
    char *portstr = NULL;
    static gf_boolean_t acl3_inited = _gf_false;

    /* Already inited */
    if (acl3_inited)
        return &acl3prog;

    nfs = (struct nfs_state *)nfsx->private;

    ns = nfs->nfs3state;
    if (!ns) {
        gf_msg(GF_ACL, GF_LOG_ERROR, EINVAL, NFS_MSG_ACL_INIT_FAIL,
               "ACL3 init failed");
        goto err;
    }
    acl3prog.private = ns;

    options = dict_new();

    ret = gf_asprintf(&portstr, "%d", GF_ACL3_PORT);
    if (ret == -1)
        goto err;

    ret = dict_set_dynstr(options, "transport.socket.listen-port", portstr);
    if (ret == -1)
        goto err;
    ret = dict_set_str(options, "transport-type", "socket");
    if (ret == -1) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
               "dict_set_str error");
        goto err;
    }

    if (nfs->allow_insecure) {
        ret = dict_set_str(options, "rpc-auth-allow-insecure", "on");
        if (ret == -1) {
            gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
                   "dict_set_str error");
            goto err;
        }
        ret = dict_set_str(options, "rpc-auth.ports.insecure", "on");
        if (ret == -1) {
            gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
                   "dict_set_str error");
            goto err;
        }
    }

    ret = dict_set_str(options, "transport.address-family", "inet");
    if (ret == -1) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
               "dict_set_str error");
        goto err;
    }

    ret = rpcsvc_create_listeners(nfs->rpcsvc, options, "ACL");
    if (ret == -1) {
        gf_msg(GF_ACL, GF_LOG_ERROR, errno, NFS_MSG_LISTENERS_CREATE_FAIL,
               "Unable to create listeners");
        dict_unref(options);
        goto err;
    }

    acl3_inited = _gf_true;
    return &acl3prog;
err:
    return NULL;
}

static int
acl3_nfs_acl_to_xattr(aclentry *ace,  /* ACL entries to be read */
                      void *xattrbuf, /* XATTR buf to be populated */
                      int aclcount,   /* No of ACLs to be read */
                      int defacl)     /* 1 if DEFAULT ACL */
{
    int idx = 0;
    posix_acl_xattr_header *xheader = NULL;
    posix_acl_xattr_entry *xentry = NULL;

    if ((!ace) || (!xattrbuf))
        return (-EINVAL);

    /* ACL count is ZERO, nothing to do */
    if (!aclcount)
        return (0);

    if ((aclcount < 0) || (aclcount > NFS_ACL_MAX_ENTRIES))
        return (-EINVAL);

    xheader = (posix_acl_xattr_header *)(xattrbuf);
    xentry = (posix_acl_xattr_entry *)(xheader + 1);

    /*
     * For "default ACL", NFSv3 handles the 'type' differently
     * i.e. by logical OR'ing 'type' with NFS_ACL_DEFAULT.
     * Which the backend File system does not understand and
     * that needs to be masked OFF.
     */
    xheader->version = POSIX_ACL_XATTR_VERSION;

    for (idx = 0; idx < aclcount; idx++) {
        xentry->tag = ace->type;
        if (defacl)
            xentry->tag &= ~NFS_ACL_DEFAULT;
        xentry->perm = ace->perm;

        switch (xentry->tag) {
            case POSIX_ACL_USER:
            case POSIX_ACL_GROUP:
                if (xentry->perm & ~S_IRWXO)
                    return (-EINVAL);
                xentry->id = ace->uid;
                break;
            case POSIX_ACL_USER_OBJ:
            case POSIX_ACL_GROUP_OBJ:
            case POSIX_ACL_OTHER:
                if (xentry->perm & ~S_IRWXO)
                    return (-EINVAL);
                xentry->id = POSIX_ACL_UNDEFINED_ID;
                break;
            case POSIX_ACL_MASK:
                /* Solaris sometimes sets additional bits in
                 * the mask.
                 */
                xentry->perm &= S_IRWXO;
                xentry->id = POSIX_ACL_UNDEFINED_ID;
                break;
            default:
                return (-EINVAL);
        }

        xentry++;
        ace++;
    }

    /* SUCCESS */
    return (0);
}

static int
acl3_nfs_acl_from_xattr(aclentry *ace,  /* ACL entries to be filled */
                        void *xattrbuf, /* XATTR buf to be read */
                        int bufsize,    /* Size of XATTR buffer */
                        int defacl)     /*  1 if DEFAULT ACL */
{
    int idx = 0;
    ssize_t aclcount = 0;
    posix_acl_xattr_header *xheader = NULL;
    posix_acl_xattr_entry *xentry = NULL;

    if ((!xattrbuf) || (!ace))
        return (-EINVAL);

    aclcount = posix_acl_xattr_count(bufsize);
    if ((aclcount < 0) || (aclcount > NFS_ACL_MAX_ENTRIES))
        return (-EINVAL);

    xheader = (posix_acl_xattr_header *)(xattrbuf);
    xentry = (posix_acl_xattr_entry *)(xheader + 1);

    /* Check for supported POSIX ACL xattr version */
    if (xheader->version != POSIX_ACL_XATTR_VERSION)
        return (-ENOSYS);

    for (idx = 0; idx < (int)aclcount; idx++) {
        ace->type = xentry->tag;
        if (defacl) {
            /*
             * SET the NFS_ACL_DEFAULT flag for default
             * ACL which was masked OFF during setfacl().
             */
            ace->type |= NFS_ACL_DEFAULT;
        }
        ace->perm = (xentry->perm & S_IRWXO);

        switch (xentry->tag) {
            case POSIX_ACL_USER:
            case POSIX_ACL_GROUP:
                ace->uid = xentry->id;
                break;
            case POSIX_ACL_USER_OBJ:
            case POSIX_ACL_GROUP_OBJ:
            case POSIX_ACL_MASK:
            case POSIX_ACL_OTHER:
                ace->uid = POSIX_ACL_UNDEFINED_ID;
                break;
            default:
                return (-EINVAL);
        }

        xentry++;
        ace++;
    }

    /* SUCCESS: ACL count */
    return aclcount;
}