File: blkdiscard.c

package info (click to toggle)
libguestfs 1%3A1.34.6-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,152 kB
  • ctags: 48,429
  • sloc: ansic: 438,854; ml: 49,329; sh: 15,718; java: 9,326; perl: 8,954; makefile: 7,318; cs: 6,153; haskell: 5,531; python: 3,161; erlang: 2,386; xml: 1,739; ruby: 350; pascal: 248; lex: 134; yacc: 128; cpp: 10
file content (128 lines) | stat: -rw-r--r-- 2,937 bytes parent folder | download | duplicates (7)
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
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2014 Red Hat Inc.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
#endif

#include "daemon.h"
#include "actions.h"
#include "optgroups.h"

/* http://rwmj.wordpress.com/2014/03/11/blkdiscard-blkzeroout-blkdiscardzeroes-blksecdiscard/ */

#ifdef BLKDISCARD

int
optgroup_blkdiscard_available (void)
{
  return 1;
}

int
do_blkdiscard (const char *device)
{
  /* XXX We could read /sys/block/<device>/queue/discard_* in order to
   * determine if discard is supported and the largest request size we
   * are allowed to make.  However:
   *
   * (1) Mapping the device name to /sys/block/<device> is quite hard
   * (cf. the lv_canonical function in daemon/lvm.c)
   *
   * (2) We don't really need to do this in modern libguestfs since
   * we're very likely to be using virtio-scsi, which supports
   * arbitrary block discards.
   *
   * Let's wait to see if it causes a problem in real world
   * situations.
   */
  uint64_t range[2];
  int64_t size;
  int fd;

  size = do_blockdev_getsize64 (device);
  if (size == -1)
    return -1;

  range[0] = 0;
  range[1] = (uint64_t) size;

  fd = open (device, O_WRONLY|O_CLOEXEC);
  if (fd == -1) {
    reply_with_perror ("open: %s", device);
    return -1;
  }

  if (ioctl (fd, BLKDISCARD, range) == -1) {
    reply_with_perror ("ioctl: %s: BLKDISCARD", device);
    close (fd);
    return -1;
  }

  close (fd);
  return 0;
}

#else /* !BLKDISCARD */
OPTGROUP_BLKDISCARD_NOT_AVAILABLE
#endif

#ifdef BLKDISCARDZEROES

int
optgroup_blkdiscardzeroes_available (void)
{
  return 1;
}

int
do_blkdiscardzeroes (const char *device)
{
  int fd;
  unsigned int arg;

  fd = open (device, O_RDONLY|O_CLOEXEC);
  if (fd == -1) {
    reply_with_perror ("open: %s", device);
    return -1;
  }

  if (ioctl (fd, BLKDISCARDZEROES, &arg) == -1) {
    reply_with_perror ("ioctl: %s: BLKDISCARDZEROES", device);
    close (fd);
    return -1;
  }

  close (fd);

  return arg != 0;
}

#else /* !BLKDISCARDZEROES */
OPTGROUP_BLKDISCARDZEROES_NOT_AVAILABLE
#endif