2 * Copyright (c) 2010-2012 Semihalf
3 * Copyright (c) 2008, 2009 Reinoud Zandijk
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * From: NetBSD: nilfs_subr.c,v 1.4 2009/07/29 17:06:57 reinoud
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/namei.h>
35 #include <sys/kernel.h>
40 #include <sys/mount.h>
41 #include <sys/vnode.h>
42 #include <sys/signalvar.h>
43 #include <sys/malloc.h>
44 #include <sys/dirent.h>
45 #include <sys/lockf.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_object.h>
51 #include <vm/vnode_pager.h>
53 #include <machine/_inttypes.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vnode_pager.h>
60 #include "nandfs_mount.h"
62 #include "nandfs_subr.h"
66 nandfs_get_maxfilesize(struct nandfs_device *fsdev)
69 return (get_maxfilesize(fsdev));
73 nandfs_bmap_lookup(struct nandfs_node *node, nandfs_lbn_t lblk,
78 if (node->nn_ino == NANDFS_GC_INO && lblk >= 0)
81 error = bmap_lookup(node, lblk, vblk);
83 DPRINTF(TRANSLATE, ("%s: error %d ino %#jx lblocknr %#jx -> %#jx\n",
84 __func__, error, (uintmax_t)node->nn_ino, (uintmax_t)lblk,
88 nandfs_error("%s: returned %d", __func__, error);
94 nandfs_bmap_insert_block(struct nandfs_node *node, nandfs_lbn_t lblk,
97 struct nandfs_device *fsdev;
101 fsdev = node->nn_nandfsdev;
104 if (node->nn_ino != NANDFS_DAT_INO) {
105 error = nandfs_vblock_alloc(fsdev, &vblk);
110 nandfs_buf_set(bp, NANDFS_VBLK_ASSIGNED);
111 nandfs_vblk_set(bp, vblk);
113 error = bmap_insert_block(node, lblk, vblk);
115 nandfs_vblock_free(fsdev, vblk);
123 nandfs_bmap_dirty_blocks(struct nandfs_node *node, struct buf *bp, int force)
127 error = bmap_dirty_meta(node, bp->b_lblkno, force);
129 nandfs_error("%s: cannot dirty buffer %p\n",
136 nandfs_bmap_update_mapping(struct nandfs_node *node, nandfs_lbn_t lblk,
137 nandfs_daddr_t blknr)
142 ("%s: node: %p ino: %#jx lblk: %#jx vblk: %#jx\n",
143 __func__, node, (uintmax_t)node->nn_ino, (uintmax_t)lblk,
146 error = bmap_insert_block(node, lblk, blknr);
152 nandfs_bmap_update_block(struct nandfs_node *node, struct buf *bp,
159 nandfs_vblk_set(bp, blknr);
161 DPRINTF(BMAP, ("%s: node: %p ino: %#jx bp: %p lblk: %#jx blk: %#jx\n",
162 __func__, node, (uintmax_t)node->nn_ino, bp,
163 (uintmax_t)lblk, (uintmax_t)blknr));
165 error = nandfs_bmap_update_mapping(node, lblk, blknr);
167 nandfs_error("%s: cannot update lblk:%jx to blk:%jx for "
168 "node:%p, error:%d\n", __func__, (uintmax_t)lblk,
169 (uintmax_t)blknr, node, error);
177 nandfs_bmap_update_dat(struct nandfs_node *node, nandfs_daddr_t oldblk,
180 struct nandfs_device *fsdev;
181 nandfs_daddr_t vblk = 0;
184 if (node->nn_ino == NANDFS_DAT_INO)
187 if (nandfs_buf_check(bp, NANDFS_VBLK_ASSIGNED)) {
188 nandfs_buf_clear(bp, NANDFS_VBLK_ASSIGNED);
192 fsdev = node->nn_nandfsdev;
194 /* First alloc new virtual block.... */
195 error = nandfs_vblock_alloc(fsdev, &vblk);
199 error = nandfs_bmap_update_block(node, bp, vblk);
203 /* Then we can end up with old one */
204 nandfs_vblock_end(fsdev, oldblk);
207 ("%s: ino %#jx block %#jx: update vblk %#jx to %#jx\n",
208 __func__, (uintmax_t)node->nn_ino, (uintmax_t)bp->b_lblkno,
209 (uintmax_t)oldblk, (uintmax_t)vblk));
214 nandfs_bmap_truncate_mapping(struct nandfs_node *node, nandfs_lbn_t oblk,
222 DPRINTF(BMAP, ("%s: node %p oblk %jx nblk %jx truncate by %jx\n",
223 __func__, node, oblk, nblk, todo));
225 error = bmap_truncate_mapping(node, oblk, todo);