2 * Copyright (c) 2010-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. 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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
38 #include <sys/ioctl.h>
40 #include <sys/param.h>
41 #include <sys/stdint.h>
42 #include <sys/ucred.h>
44 #include <sys/mount.h>
46 #include <fs/nandfs/nandfs_fs.h>
47 #include <libnandfs.h>
49 #define NANDFS_IS_VALID 0x1
50 #define NANDFS_IS_OPENED 0x2
51 #define NANDFS_IS_OPENED_DEV 0x4
52 #define NANDFS_IS_ERROR 0x8
57 #define NANDFS_DEBUG(fmt, args...) do { \
58 printf("libnandfs:" fmt "\n", ##args); } while (0)
60 #define NANDFS_DEBUG(fmt, args...)
63 #define NANDFS_ASSERT_VALID(fs) assert((fs)->n_flags & NANDFS_IS_VALID)
64 #define NANDFS_ASSERT_VALID_DEV(fs) \
65 assert(((fs)->n_flags & (NANDFS_IS_VALID | NANDFS_IS_OPENED_DEV)) == \
66 (NANDFS_IS_VALID | NANDFS_IS_OPENED_DEV))
69 nandfs_iserror(struct nandfs *fs)
72 NANDFS_ASSERT_VALID(fs);
74 return (fs->n_flags & NANDFS_IS_ERROR);
78 nandfs_errmsg(struct nandfs *fs)
81 NANDFS_ASSERT_VALID(fs);
83 assert(nandfs_iserror(fs));
85 return (fs->n_errmsg);
89 nandfs_seterr(struct nandfs *fs, const char *fmt, ...)
94 vsnprintf(fs->n_errmsg, sizeof(fs->n_errmsg), fmt, ap);
96 fs->n_flags |= NANDFS_IS_ERROR;
100 nandfs_dev(struct nandfs *fs)
103 NANDFS_ASSERT_VALID(fs);
108 nandfs_init(struct nandfs *fs, const char *dir)
111 snprintf(fs->n_ioc, sizeof(fs->n_ioc), "%s/%s", dir, ".");
113 fs->n_flags = NANDFS_IS_VALID;
117 nandfs_destroy(struct nandfs *fs)
120 assert(fs->n_iocfd == -1);
122 ~(NANDFS_IS_ERROR | NANDFS_IS_VALID);
123 assert(fs->n_flags == 0);
127 nandfs_open(struct nandfs *fs)
129 struct nandfs_fsinfo fsinfo;
131 fs->n_flags |= NANDFS_IS_OPENED;
133 fs->n_iocfd = open(fs->n_ioc, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP |
134 S_IWGRP | S_IROTH | S_IWOTH);
135 if (fs->n_iocfd == -1) {
136 nandfs_seterr(fs, "couldn't open %s: %s", fs->n_ioc,
141 if (ioctl(fs->n_iocfd, NANDFS_IOCTL_GET_FSINFO, &fsinfo) == -1) {
142 nandfs_seterr(fs, "couldn't fetch fsinfo: %s",
147 memcpy(&fs->n_fsdata, &fsinfo.fs_fsdata, sizeof(fs->n_fsdata));
148 memcpy(&fs->n_sb, &fsinfo.fs_super, sizeof(fs->n_sb));
149 snprintf(fs->n_dev, sizeof(fs->n_dev), "%s", fsinfo.fs_dev);
155 nandfs_close(struct nandfs *fs)
158 NANDFS_ASSERT_VALID(fs);
159 assert(fs->n_flags & NANDFS_IS_OPENED);
163 fs->n_flags &= ~NANDFS_IS_OPENED;
167 nandfs_get_cpstat(struct nandfs *fs, struct nandfs_cpstat *cpstat)
170 NANDFS_ASSERT_VALID(fs);
172 if (ioctl(fs->n_iocfd, NANDFS_IOCTL_GET_CPSTAT, cpstat) == -1) {
173 nandfs_seterr(fs, "ioctl NANDFS_IOCTL_GET_CPSTAT: %s",
182 nandfs_get_cpinfo(struct nandfs *fs, uint64_t cno, int mode,
183 struct nandfs_cpinfo *cpinfo, size_t nci)
185 struct nandfs_argv args;
187 NANDFS_ASSERT_VALID(fs);
189 args.nv_base = (u_long)cpinfo;
190 args.nv_nmembs = nci;
192 args.nv_flags = mode;
194 if (ioctl(fs->n_iocfd, NANDFS_IOCTL_GET_CPINFO, &args) == -1) {
195 nandfs_seterr(fs, "ioctl NANDFS_IOCTL_GET_CPINFO: %s",
200 return (args.nv_nmembs);
204 nandfs_get_cp(struct nandfs *fs, uint64_t cno, struct nandfs_cpinfo *cpinfo,
208 return (nandfs_get_cpinfo(fs, cno, NANDFS_CHECKPOINT, cpinfo, nci));
212 nandfs_get_snap(struct nandfs *fs, uint64_t cno, struct nandfs_cpinfo *cpinfo,
216 return (nandfs_get_cpinfo(fs, cno, NANDFS_SNAPSHOT, cpinfo, nci));
220 nandfs_make_snap(struct nandfs *fs, uint64_t *cno)
223 NANDFS_ASSERT_VALID(fs);
225 if (ioctl(fs->n_iocfd, NANDFS_IOCTL_MAKE_SNAP, cno) == -1) {
226 nandfs_seterr(fs, "ioctl NANDFS_IOCTL_MAKE_SNAP: %s",
235 nandfs_delete_snap(struct nandfs *fs, uint64_t cno)
238 NANDFS_ASSERT_VALID(fs);
240 if (ioctl(fs->n_iocfd, NANDFS_IOCTL_DELETE_SNAP, &cno) == -1) {
241 nandfs_seterr(fs, "ioctl NANDFS_IOCTL_DELETE_SNAP: %s",