summaryrefslogtreecommitdiffstats
path: root/xlators/mount/fuse/src/fuse-extra.c
blob: 95bd0f3ad1c6d8f3ef8b43a367d40057118688a3 (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
/*
   Copyright (c) 2006-2009 Z RESEARCH, Inc. <http://www.zresearch.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif /* _CONFIG_H */

#include "fuse-extra.h"
#include "common-utils.h"
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "common-utils.h"

struct fuse_req;
struct fuse_ll;

struct fuse_req {
    struct fuse_ll *f;
    uint64_t unique;
    int ctr;
    pthread_mutex_t lock;
    struct fuse_ctx ctx;
    struct fuse_chan *ch;
    int interrupted;
    union {
        struct {
            uint64_t unique;
        } i;
        struct {
            fuse_interrupt_func_t func;
            void *data;
        } ni;
    } u;
    struct fuse_req *next;
    struct fuse_req *prev;
};

struct fuse_ll {
    int debug;
    int allow_root;
    struct fuse_lowlevel_ops op;
    int got_init;
    void *userdata;
    uid_t owner;
    struct fuse_conn_info conn;
    struct fuse_req list;
    struct fuse_req interrupts;
    pthread_mutex_t lock;
    int got_destroy;
};

struct fuse_out_header {
  uint32_t   len;
  int32_t    error;
  uint64_t   unique;
};

uint64_t req_callid (fuse_req_t req)
{
  return req->unique;
}

static void destroy_req(fuse_req_t req)
{
    pthread_mutex_destroy (&req->lock);
    FREE (req);
}

static void list_del_req(struct fuse_req *req)
{
    struct fuse_req *prev = req->prev;
    struct fuse_req *next = req->next;
    prev->next = next;
    next->prev = prev;
}

static void
free_req (fuse_req_t req)
{
  int ctr;
  struct fuse_ll *f = req->f;
  
  pthread_mutex_lock(&req->lock);
  req->u.ni.func = NULL;
  req->u.ni.data = NULL;
  pthread_mutex_unlock(&req->lock);

  pthread_mutex_lock(&f->lock);
  list_del_req(req);
  ctr = --req->ctr;
  pthread_mutex_unlock(&f->lock);
  if (!ctr)
    destroy_req(req);
}

int32_t
fuse_reply_vec (fuse_req_t req,
		struct iovec *vector,
		int32_t count)
{
  int32_t error = 0;
  struct fuse_out_header out;
  struct iovec *iov;
  int res;

  iov = alloca ((count + 1) * sizeof (*vector));
  out.unique = req->unique;
  out.error = error;
  iov[0].iov_base = &out;
  iov[0].iov_len = sizeof(struct fuse_out_header);
  memcpy (&iov[1], vector, count * sizeof (*vector));
  count++;
  out.len = iov_length(iov, count);
  res = fuse_chan_send(req->ch, iov, count);
  free_req(req);

  return res;
}