root / drivers / tapdisk-storage.c @ abdb293f
History | View | Annotate | Download (2.9 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010, Citrix Systems, Inc.
|
3 |
*
|
4 |
* All rights reserved.
|
5 |
*
|
6 |
* Redistribution and use in source and binary forms, with or without
|
7 |
* modification, are permitted provided that the following conditions are met:
|
8 |
* * Redistributions of source code must retain the above copyright
|
9 |
* notice, this list of conditions and the following disclaimer.
|
10 |
* * Redistributions in binary form must reproduce the above copyright
|
11 |
* notice, this list of conditions and the following disclaimer in the
|
12 |
* documentation and/or other materials provided with the distribution.
|
13 |
* * Neither the name of XenSource Inc. nor the names of its contributors
|
14 |
* may be used to endorse or promote products derived from this software
|
15 |
* without specific prior written permission.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
19 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
20 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
21 |
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
24 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
25 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
26 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
27 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
28 |
*/
|
29 |
|
30 |
#ifdef HAVE_CONFIG_H
|
31 |
#include "config.h" |
32 |
#endif
|
33 |
|
34 |
#include <errno.h> |
35 |
#include <limits.h> |
36 |
#include <stdlib.h> |
37 |
#include <sys/stat.h> |
38 |
#include <sys/vfs.h> |
39 |
|
40 |
#include "tapdisk-storage.h" |
41 |
|
42 |
#ifndef NFS_SUPER_MAGIC
|
43 |
#define NFS_SUPER_MAGIC 0x6969 |
44 |
#endif
|
45 |
|
46 |
static int |
47 |
__tapdisk_fs_storage_type(const char *rpath) |
48 |
{ |
49 |
struct statfs fst;
|
50 |
int type, err;
|
51 |
|
52 |
err = statfs(rpath, &fst); |
53 |
if (err)
|
54 |
return -errno;
|
55 |
|
56 |
switch (fst.f_type) {
|
57 |
case NFS_SUPER_MAGIC:
|
58 |
type = TAPDISK_STORAGE_TYPE_NFS; |
59 |
break;
|
60 |
default:
|
61 |
type = TAPDISK_STORAGE_TYPE_EXT; |
62 |
break;
|
63 |
} |
64 |
|
65 |
return type;
|
66 |
} |
67 |
|
68 |
static int |
69 |
__tapdisk_blk_storage_type(const char *rpath) |
70 |
{ |
71 |
return TAPDISK_STORAGE_TYPE_LVM;
|
72 |
} |
73 |
|
74 |
int
|
75 |
tapdisk_storage_type(const char *path) |
76 |
{ |
77 |
char rpath[PATH_MAX], *p;
|
78 |
struct stat st;
|
79 |
int err, rv;
|
80 |
|
81 |
p = realpath(path, rpath); |
82 |
if (!p)
|
83 |
return -errno;
|
84 |
|
85 |
err = stat(rpath, &st); |
86 |
if (err)
|
87 |
return -errno;
|
88 |
|
89 |
switch (st.st_mode & S_IFMT) {
|
90 |
case S_IFBLK:
|
91 |
rv = __tapdisk_blk_storage_type(rpath); |
92 |
break;
|
93 |
case S_IFREG:
|
94 |
rv = __tapdisk_fs_storage_type(rpath); |
95 |
break;
|
96 |
default:
|
97 |
rv = -EINVAL; |
98 |
break;
|
99 |
} |
100 |
|
101 |
return rv;
|
102 |
} |
103 |
|
104 |
const char * |
105 |
tapdisk_storage_name(int type)
|
106 |
{ |
107 |
switch (type) {
|
108 |
case TAPDISK_STORAGE_TYPE_NFS:
|
109 |
return "nfs"; |
110 |
case TAPDISK_STORAGE_TYPE_EXT:
|
111 |
return "ext"; |
112 |
case TAPDISK_STORAGE_TYPE_LVM:
|
113 |
return "lvm"; |
114 |
case -1: |
115 |
return "n/a"; |
116 |
default:
|
117 |
return "<unknown-type>"; |
118 |
} |
119 |
} |