summaryrefslogtreecommitdiffstats
path: root/glfs-operations.c
blob: fecc31559a46b3185fe576ed13e1eaf5e290d9e5 (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
/*
  Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
  This file is part of gluster-block.

  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 "utils.h"
# include "glfs-operations.h"

# define  LOG_FILE         "/var/log/gluster-block/block.log"
# define  LOG_LEVEL        7



int
glusterBlockCreateEntry(blockCreateCli *blk, char *gbid)
{
  struct glfs *glfs;
  struct glfs_fd *fd;
  int ret = 0;

  glfs = glfs_new(blk->volume);
  if (!glfs) {
    ERROR("%s", "glfs_new: returned NULL");
    return -1;
  }

  ret = glfs_set_volfile_server(glfs, "tcp", blk->volfileserver, 24007);
  if (ret) {
    ERROR("%s", "glfs_set_volfile_server: failed");
    goto out;
  }

  ret = glfs_set_logging(glfs, LOG_FILE, LOG_LEVEL);
  if (ret) {
    ERROR("%s", "glfs_set_logging: failed");
    goto out;
  }

  ret = glfs_init(glfs);
  if (ret) {
    ERROR("%s", "glfs_init: failed");
    goto out;
  }

  fd = glfs_creat(glfs, gbid,
                  O_WRONLY | O_CREAT | O_TRUNC,
                  S_IRUSR | S_IWUSR);
  if (!fd) {
    ERROR("%s", "glfs_creat: failed");
    ret = -errno;
  } else {
    ret = glfs_ftruncate(fd, blk->size);
    if (ret) {
      ERROR("%s", "glfs_ftruncate: failed");
      goto out;
    }

    if (glfs_close(fd) != 0) {
      ERROR("%s", "glfs_close: failed");
      ret = -errno;
    }
  }

 out:
  glfs_fini(glfs);
  return ret;
}


int
glusterBlockDeleteEntry(blockCreateCli *blk, char *gbid)
{
  struct glfs *glfs;
  int ret = 0;

  glfs = glfs_new(blk->volume);
  if (!glfs) {
    ERROR("%s", "glfs_new: returned NULL");
    return -1;
  }

  ret = glfs_set_volfile_server(glfs, "tcp", blk->volfileserver, 24007);
  if (ret) {
    ERROR("%s", "glfs_set_volfile_server: failed");
    goto out;
  }

  ret = glfs_set_logging(glfs, LOG_FILE, LOG_LEVEL);
  if (ret) {
    ERROR("%s", "glfs_set_logging: failed");
    goto out;
  }

  ret = glfs_init(glfs);
  if (ret) {
    ERROR("%s", "glfs_init: failed");
    goto out;
  }

  ret = glfs_unlink(glfs, gbid);
  if (ret) {
    ERROR("%s", "glfs_unlink: failed");
    goto out;
  }

 out:
  glfs_fini(glfs);
  return ret;
}