2 * Copyright (c) 2007 Doug Rabson
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Stand-alone file reading package.
36 #include <sys/param.h>
37 #include <sys/disklabel.h>
39 #include <sys/queue.h>
44 #include <bootstrap.h>
48 static int zfs_open(const char *path, struct open_file *f);
49 static int zfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
50 static int zfs_close(struct open_file *f);
51 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
52 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
53 static int zfs_stat(struct open_file *f, struct stat *sb);
54 static int zfs_readdir(struct open_file *f, struct dirent *d);
58 struct fs_ops zfs_fsops = {
73 off_t f_seekp; /* seek pointer */
75 uint64_t f_zap_type; /* zap type for readdir */
76 uint64_t f_num_leafs; /* number of fzap leaf blocks */
77 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
84 zfs_open(const char *upath, struct open_file *f)
86 spa_t *spa = (spa_t *) f->f_devdata;
90 if (f->f_dev != &zfs_dev)
93 rc = zfs_mount_pool(spa);
97 /* allocate file system specific data structure */
98 fp = malloc(sizeof(struct file));
99 bzero(fp, sizeof(struct file));
100 f->f_fsdata = (void *)fp;
102 if (spa->spa_root_objset.os_type != DMU_OST_ZFS) {
103 printf("Unexpected object set type %lld\n",
104 spa->spa_root_objset.os_type);
109 rc = zfs_lookup(spa, upath, &fp->f_dnode);
123 zfs_close(struct open_file *f)
125 struct file *fp = (struct file *)f->f_fsdata;
128 f->f_fsdata = (void *)0;
129 if (fp == (struct file *)0)
137 * Copy a portion of a file into kernel memory.
138 * Cross block boundaries when necessary.
141 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
143 spa_t *spa = (spa_t *) f->f_devdata;
144 struct file *fp = (struct file *)f->f_fsdata;
145 const znode_phys_t *zp = (const znode_phys_t *) fp->f_dnode.dn_bonus;
150 if (fp->f_seekp + n > zp->zp_size)
151 n = zp->zp_size - fp->f_seekp;
153 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
159 for (i = 0; i < n; i++)
160 putchar(((char*) start)[i]);
170 * Don't be silly - the bootstrap has no business writing anything.
173 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
180 zfs_seek(struct open_file *f, off_t offset, int where)
182 struct file *fp = (struct file *)f->f_fsdata;
183 znode_phys_t *zp = (znode_phys_t *) fp->f_dnode.dn_bonus;
187 fp->f_seekp = offset;
190 fp->f_seekp += offset;
193 fp->f_seekp = zp->zp_size - offset;
199 return (fp->f_seekp);
203 zfs_stat(struct open_file *f, struct stat *sb)
205 struct file *fp = (struct file *)f->f_fsdata;
206 znode_phys_t *zp = (znode_phys_t *) fp->f_dnode.dn_bonus;
208 /* only important stuff */
209 sb->st_mode = zp->zp_mode;
210 sb->st_uid = zp->zp_uid;
211 sb->st_gid = zp->zp_gid;
212 sb->st_size = zp->zp_size;
218 zfs_readdir(struct open_file *f, struct dirent *d)
220 spa_t *spa = (spa_t *) f->f_devdata;
221 struct file *fp = (struct file *)f->f_fsdata;
222 znode_phys_t *zp = (znode_phys_t *) fp->f_dnode.dn_bonus;
224 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
227 if ((zp->zp_mode >> 12) != 0x4) {
232 * If this is the first read, get the zap type.
234 if (fp->f_seekp == 0) {
235 rc = dnode_read(spa, &fp->f_dnode,
236 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
240 if (fp->f_zap_type == ZBT_MICRO) {
241 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
243 rc = dnode_read(spa, &fp->f_dnode,
244 offsetof(zap_phys_t, zap_num_leafs),
246 sizeof(fp->f_num_leafs));
251 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
252 rc = dnode_read(spa, &fp->f_dnode,
261 if (fp->f_zap_type == ZBT_MICRO) {
263 if (fp->f_seekp >= bsize)
266 rc = dnode_read(spa, &fp->f_dnode,
267 fp->f_seekp, &mze, sizeof(mze));
268 fp->f_seekp += sizeof(mze);
270 if (!mze.mze_name[0])
273 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
274 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
275 strcpy(d->d_name, mze.mze_name);
276 d->d_namlen = strlen(d->d_name);
280 zap_leaf_chunk_t *zc, *nc;
287 * Initialise this so we can use the ZAP size
288 * calculating macros.
290 zl.l_bs = ilog2(bsize);
291 zl.l_phys = fp->f_zap_leaf;
294 * Figure out which chunk we are currently looking at
295 * and consider seeking to the next leaf. We use the
296 * low bits of f_seekp as a simple chunk index.
299 chunk = fp->f_seekp & (bsize - 1);
300 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
301 fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
305 * Check for EOF and read the new leaf.
307 if (fp->f_seekp >= bsize * fp->f_num_leafs)
310 rc = dnode_read(spa, &fp->f_dnode,
318 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
320 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
323 namelen = zc->l_entry.le_name_length;
324 if (namelen > sizeof(d->d_name))
325 namelen = sizeof(d->d_name);
328 * Paste the name back together.
330 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
332 while (namelen > 0) {
335 if (len > ZAP_LEAF_ARRAY_BYTES)
336 len = ZAP_LEAF_ARRAY_BYTES;
337 memcpy(p, nc->l_array.la_array, len);
340 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
342 d->d_name[sizeof(d->d_name) - 1] = 0;
345 * Assume the first eight bytes of the value are
348 value = fzap_leaf_value(&zl, zc);
350 d->d_fileno = ZFS_DIRENT_OBJ(value);
351 d->d_type = ZFS_DIRENT_TYPE(value);
352 d->d_namlen = strlen(d->d_name);
359 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t size)
363 fd = (uintptr_t) priv;
364 lseek(fd, offset, SEEK_SET);
365 if (read(fd, buf, size) == size) {
373 * Convert a pool guid to a 'unit number' suitable for use with zfs_dev_open.
376 zfs_guid_to_unit(uint64_t guid)
382 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
383 if (spa->spa_guid == guid)
398 * Open all the disks we can find and see if we can reconstruct
399 * ZFS pools from them. Bogusly assumes that the disks are named
403 for (unit = 0; unit < 32 /* XXX */; unit++) {
404 sprintf(devname, "disk%d:", unit);
405 fd = open(devname, O_RDONLY);
410 * If we find a vdev, the zfs code will eat the fd, otherwise
413 if (vdev_probe(vdev_read, (void*) (uintptr_t) fd, 0))
416 for (slice = 1; slice <= 4; slice++) {
417 sprintf(devname, "disk%dp%d:", unit, slice);
418 fd = open(devname, O_RDONLY);
420 sprintf(devname, "disk%ds%d:", unit, slice);
421 fd = open(devname, O_RDONLY);
425 if (vdev_probe(vdev_read, (void*) (uintptr_t) fd, 0))
434 * Print information about ZFS pools
437 zfs_dev_print(int verbose)
448 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
449 sprintf(line, " zfs%d: %s\n", unit, spa->spa_name);
456 * Attempt to open the pool described by (dev) for use by (f).
459 zfs_dev_open(struct open_file *f, ...)
467 dev = va_arg(args, struct devdesc*);
471 * We mostly ignore the stuff that devopen sends us. For now,
472 * use the unit to find a pool - later we will override the
473 * devname parsing so that we can name a pool and a fs within
479 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
494 zfs_dev_close(struct open_file *f)
502 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
508 struct devsw zfs_dev = {
511 .dv_init = zfs_dev_init,
512 .dv_strategy = zfs_dev_strategy,
513 .dv_open = zfs_dev_open,
514 .dv_close = zfs_dev_close,
516 .dv_print = zfs_dev_print,