]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nandfs/nandfs_bmap.c
Merge lld trunk r338150, and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / fs / nandfs / nandfs_bmap.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2012 Semihalf
5  * Copyright (c) 2008, 2009 Reinoud Zandijk
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * From: NetBSD: nilfs_subr.c,v 1.4 2009/07/29 17:06:57 reinoud
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/namei.h>
37 #include <sys/kernel.h>
38 #include <sys/stat.h>
39 #include <sys/buf.h>
40 #include <sys/bio.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/signalvar.h>
45 #include <sys/malloc.h>
46 #include <sys/dirent.h>
47 #include <sys/lockf.h>
48 #include <sys/ktr.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_extern.h>
52 #include <vm/vm_object.h>
53 #include <vm/vnode_pager.h>
54
55 #include <machine/_inttypes.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vnode_pager.h>
61
62 #include "nandfs_mount.h"
63 #include "nandfs.h"
64 #include "nandfs_subr.h"
65 #include "bmap.h"
66
67 nandfs_lbn_t
68 nandfs_get_maxfilesize(struct nandfs_device *fsdev)
69 {
70
71         return (get_maxfilesize(fsdev));
72 }
73
74 int
75 nandfs_bmap_lookup(struct nandfs_node *node, nandfs_lbn_t lblk,
76     nandfs_daddr_t *vblk)
77 {
78         int error = 0;
79
80         if (node->nn_ino == NANDFS_GC_INO && lblk >= 0)
81                 *vblk = lblk;
82         else
83                 error = bmap_lookup(node, lblk, vblk);
84
85         DPRINTF(TRANSLATE, ("%s: error %d ino %#jx lblocknr %#jx -> %#jx\n",
86             __func__, error, (uintmax_t)node->nn_ino, (uintmax_t)lblk,
87             (uintmax_t)*vblk));
88
89         if (error)
90                 nandfs_error("%s: returned %d", __func__, error);
91
92         return (error);
93 }
94
95 int
96 nandfs_bmap_insert_block(struct nandfs_node *node, nandfs_lbn_t lblk,
97     struct buf *bp)
98 {
99         struct nandfs_device *fsdev;
100         nandfs_daddr_t vblk;
101         int error;
102
103         fsdev = node->nn_nandfsdev;
104
105         vblk = 0;
106         if (node->nn_ino != NANDFS_DAT_INO) {
107                 error = nandfs_vblock_alloc(fsdev, &vblk);
108                 if (error)
109                         return (error);
110         }
111
112         nandfs_buf_set(bp, NANDFS_VBLK_ASSIGNED);
113         nandfs_vblk_set(bp, vblk);
114
115         error = bmap_insert_block(node, lblk, vblk);
116         if (error) {
117                 nandfs_vblock_free(fsdev, vblk);
118                 return (error);
119         }
120
121         return (0);
122 }
123
124 int
125 nandfs_bmap_dirty_blocks(struct nandfs_node *node, struct buf *bp, int force)
126 {
127         int error;
128
129         error = bmap_dirty_meta(node, bp->b_lblkno, force);
130         if (error)
131                 nandfs_error("%s: cannot dirty buffer %p\n",
132                     __func__, bp);
133
134         return (error);
135 }
136
137 static int
138 nandfs_bmap_update_mapping(struct nandfs_node *node, nandfs_lbn_t lblk,
139     nandfs_daddr_t blknr)
140 {
141         int error;
142
143         DPRINTF(BMAP,
144             ("%s: node: %p ino: %#jx lblk: %#jx vblk: %#jx\n",
145             __func__, node, (uintmax_t)node->nn_ino, (uintmax_t)lblk,
146             (uintmax_t)blknr));
147
148         error = bmap_insert_block(node, lblk, blknr);
149
150         return (error);
151 }
152
153 int
154 nandfs_bmap_update_block(struct nandfs_node *node, struct buf *bp,
155     nandfs_lbn_t blknr)
156 {
157         nandfs_lbn_t lblk;
158         int error;
159
160         lblk = bp->b_lblkno;
161         nandfs_vblk_set(bp, blknr);
162
163         DPRINTF(BMAP, ("%s: node: %p ino: %#jx bp: %p lblk: %#jx blk: %#jx\n",
164             __func__, node, (uintmax_t)node->nn_ino, bp,
165             (uintmax_t)lblk, (uintmax_t)blknr));
166
167         error = nandfs_bmap_update_mapping(node, lblk, blknr);
168         if (error) {
169                 nandfs_error("%s: cannot update lblk:%jx to blk:%jx for "
170                     "node:%p, error:%d\n", __func__, (uintmax_t)lblk,
171                     (uintmax_t)blknr, node, error);
172                 return (error);
173         }
174
175         return (error);
176 }
177
178 int
179 nandfs_bmap_update_dat(struct nandfs_node *node, nandfs_daddr_t oldblk,
180     struct buf *bp)
181 {
182         struct nandfs_device *fsdev;
183         nandfs_daddr_t vblk = 0;
184         int error;
185
186         if (node->nn_ino == NANDFS_DAT_INO)
187                 return (0);
188
189         if (nandfs_buf_check(bp, NANDFS_VBLK_ASSIGNED)) {
190                 nandfs_buf_clear(bp, NANDFS_VBLK_ASSIGNED);
191                 return (0);
192         }
193
194         fsdev = node->nn_nandfsdev;
195
196         /* First alloc new virtual block.... */
197         error = nandfs_vblock_alloc(fsdev, &vblk);
198         if (error)
199                 return (error);
200
201         error = nandfs_bmap_update_block(node, bp, vblk);
202         if (error)
203                 return (error);
204
205         /* Then we can end up with old one */
206         nandfs_vblock_end(fsdev, oldblk);
207
208         DPRINTF(BMAP,
209             ("%s: ino %#jx block %#jx: update vblk %#jx to %#jx\n",
210             __func__, (uintmax_t)node->nn_ino, (uintmax_t)bp->b_lblkno,
211             (uintmax_t)oldblk, (uintmax_t)vblk));
212         return (error);
213 }
214
215 int
216 nandfs_bmap_truncate_mapping(struct nandfs_node *node, nandfs_lbn_t oblk,
217     nandfs_lbn_t nblk)
218 {
219         nandfs_lbn_t todo;
220         int error;
221
222         todo = oblk - nblk;
223
224         DPRINTF(BMAP, ("%s: node %p oblk %jx nblk %jx truncate by %jx\n",
225             __func__, node, oblk, nblk, todo));
226
227         error = bmap_truncate_mapping(node, oblk, todo);
228         if (error)
229                 return (error);
230
231         return (error);
232 }