1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010-2012 Semihalf.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
39 #include <sys/mutex.h>
40 #include <sys/namei.h>
41 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/buf.h>
44 #include <sys/bio.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_page.h>
50
51 #include <fs/nandfs/nandfs_mount.h>
52 #include <fs/nandfs/nandfs.h>
53 #include <fs/nandfs/nandfs_subr.h>
54
55 int
56 nandfs_vblock_alloc(struct nandfs_device *nandfsdev, nandfs_daddr_t *vblock)
57 {
58 struct nandfs_node *dat;
59 struct nandfs_mdt *mdt;
60 struct nandfs_alloc_request req;
61 struct nandfs_dat_entry *dat_entry;
62 uint64_t start;
63 uint32_t entry;
64 int locked, error;
65
66 dat = nandfsdev->nd_dat_node;
67 mdt = &nandfsdev->nd_dat_mdt;
68 start = nandfsdev->nd_last_cno + 1;
69
70 locked = NANDFS_VOP_ISLOCKED(NTOV(dat));
71 if (!locked)
72 VOP_LOCK(NTOV(dat), LK_EXCLUSIVE);
73 req.entrynum = 0;
74
75 /* Alloc vblock number */
76 error = nandfs_find_free_entry(mdt, dat, &req);
77 if (error) {
78 nandfs_error("%s: cannot find free vblk entry\n",
79 __func__);
80 if (!locked)
81 VOP_UNLOCK(NTOV(dat), 0);
82 return (error);
83 }
84
85 /* Read/create buffer */
86 error = nandfs_get_entry_block(mdt, dat, &req, &entry, 1);
87 if (error) {
88 nandfs_error("%s: cannot get free vblk entry\n",
89 __func__);
90 nandfs_abort_entry(&req);
91 if (!locked)
92 VOP_UNLOCK(NTOV(dat), 0);
93 return (error);
94 }
95
96 /* Fill out vblock data */
97 dat_entry = (struct nandfs_dat_entry *) req.bp_entry->b_data;
98 dat_entry[entry].de_start = start;
99 dat_entry[entry].de_end = UINTMAX_MAX;
100 dat_entry[entry].de_blocknr = 0;
101
102 /* Commit allocation */
103 error = nandfs_alloc_entry(mdt, &req);
104 if (error) {
105 nandfs_error("%s: cannot get free vblk entry\n",
106 __func__);
107 if (!locked)
108 VOP_UNLOCK(NTOV(dat), 0);
109 return (error);
110 }
111
112 /* Return allocated vblock */
113 *vblock = req.entrynum;
114 DPRINTF(DAT, ("%s: allocated vblock %#jx\n",
115 __func__, (uintmax_t)*vblock));
116
117 if (!locked)
118 VOP_UNLOCK(NTOV(dat), 0);
119 return (error);
120 }
121
122 int
123 nandfs_vblock_assign(struct nandfs_device *nandfsdev, nandfs_daddr_t vblock,
124 nandfs_lbn_t block)
125 {
126 struct nandfs_node *dat;
127 struct nandfs_mdt *mdt;
128 struct nandfs_alloc_request req;
129 struct nandfs_dat_entry *dat_entry;
130 uint32_t entry;
131 int locked, error;
132
133 dat = nandfsdev->nd_dat_node;
134 mdt = &nandfsdev->nd_dat_mdt;
135
136 locked = NANDFS_VOP_ISLOCKED(NTOV(dat));
137 if (!locked)
138 VOP_LOCK(NTOV(dat), LK_EXCLUSIVE);
139 req.entrynum = vblock;
140
141 error = nandfs_get_entry_block(mdt, dat, &req, &entry, 0);
142 if (!error) {
143 dat_entry = (struct nandfs_dat_entry *) req.bp_entry->b_data;
144 dat_entry[entry].de_blocknr = block;
145
146 DPRINTF(DAT, ("%s: assing vblock %jx->%jx\n",
147 __func__, (uintmax_t)vblock, (uintmax_t)block));
148
149 /*
150 * It is mostly called from syncer() so
151 * we want to force making buf dirty
152 */
153 error = nandfs_dirty_buf(req.bp_entry, 1);
154 }
155
156 if (!locked)
157 VOP_UNLOCK(NTOV(dat), 0);
158
159 return (error);
160 }
161
162 int
163 nandfs_vblock_end(struct nandfs_device *nandfsdev, nandfs_daddr_t vblock)
164 {
165 struct nandfs_node *dat;
166 struct nandfs_mdt *mdt;
167 struct nandfs_alloc_request req;
168 struct nandfs_dat_entry *dat_entry;
169 uint64_t end;
170 uint32_t entry;
171 int locked, error;
172
173 dat = nandfsdev->nd_dat_node;
174 mdt = &nandfsdev->nd_dat_mdt;
175 end = nandfsdev->nd_last_cno;
176
177 locked = NANDFS_VOP_ISLOCKED(NTOV(dat));
178 if (!locked)
179 VOP_LOCK(NTOV(dat), LK_EXCLUSIVE);
180 req.entrynum = vblock;
181
182 error = nandfs_get_entry_block(mdt, dat, &req, &entry, 0);
183 if (!error) {
184 dat_entry = (struct nandfs_dat_entry *) req.bp_entry->b_data;
185 dat_entry[entry].de_end = end;
186 DPRINTF(DAT, ("%s: end vblock %#jx at checkpoint %#jx\n",
187 __func__, (uintmax_t)vblock, (uintmax_t)end));
188
189 /*
190 * It is mostly called from syncer() so
191 * we want to force making buf dirty
192 */
193 error = nandfs_dirty_buf(req.bp_entry, 1);
194 }
195
196 if (!locked)
197 VOP_UNLOCK(NTOV(dat), 0);
198
199 return (error);
200 }
201
202 int
203 nandfs_vblock_free(struct nandfs_device *nandfsdev, nandfs_daddr_t vblock)
204 {
205 struct nandfs_node *dat;
206 struct nandfs_mdt *mdt;
207 struct nandfs_alloc_request req;
208 int error;
209
210 dat = nandfsdev->nd_dat_node;
211 mdt = &nandfsdev->nd_dat_mdt;
212
213 VOP_LOCK(NTOV(dat), LK_EXCLUSIVE);
214 req.entrynum = vblock;
215
216 error = nandfs_find_entry(mdt, dat, &req);
217 if (!error) {
218 DPRINTF(DAT, ("%s: vblk %#jx\n", __func__, (uintmax_t)vblock));
219 nandfs_free_entry(mdt, &req);
220 }
221
222 VOP_UNLOCK(NTOV(dat), 0);
223 return (error);
224 }
225
226 int
227 nandfs_get_dat_vinfo_ioctl(struct nandfs_device *nandfsdev, struct nandfs_argv *nargv)
228 {
229 struct nandfs_vinfo *vinfo;
230 size_t size;
231 int error;
232
233 if (nargv->nv_nmembs > NANDFS_VINFO_MAX)
234 return (EINVAL);
235
236 size = sizeof(struct nandfs_vinfo) * nargv->nv_nmembs;
237 vinfo = malloc(size, M_NANDFSTEMP, M_WAITOK|M_ZERO);
238
239 error = copyin((void *)(uintptr_t)nargv->nv_base, vinfo, size);
240 if (error) {
241 free(vinfo, M_NANDFSTEMP);
242 return (error);
243 }
244
245 error = nandfs_get_dat_vinfo(nandfsdev, vinfo, nargv->nv_nmembs);
246 if (error == 0)
247 error = copyout(vinfo, (void *)(uintptr_t)nargv->nv_base, size);
248 free(vinfo, M_NANDFSTEMP);
249 return (error);
250 }
251
252 int
253 nandfs_get_dat_vinfo(struct nandfs_device *nandfsdev, struct nandfs_vinfo *vinfo,
254 uint32_t nmembs)
255 {
256 struct nandfs_node *dat;
257 struct nandfs_mdt *mdt;
258 struct nandfs_alloc_request req;
259 struct nandfs_dat_entry *dat_entry;
260 uint32_t i, idx;
261 int error = 0;
262
263 dat = nandfsdev->nd_dat_node;
264 mdt = &nandfsdev->nd_dat_mdt;
265
266 DPRINTF(DAT, ("%s: nmembs %#x\n", __func__, nmembs));
267
268 VOP_LOCK(NTOV(dat), LK_EXCLUSIVE);
269
270 for (i = 0; i < nmembs; i++) {
271 req.entrynum = vinfo[i].nvi_vblocknr;
272
273 error = nandfs_get_entry_block(mdt, dat,&req, &idx, 0);
274 if (error)
275 break;
276
277 dat_entry = ((struct nandfs_dat_entry *) req.bp_entry->b_data);
278 vinfo[i].nvi_start = dat_entry[idx].de_start;
279 vinfo[i].nvi_end = dat_entry[idx].de_end;
280 vinfo[i].nvi_blocknr = dat_entry[idx].de_blocknr;
281
282 DPRINTF(DAT, ("%s: vinfo: %jx[%jx-%jx]->%jx\n",
283 __func__, vinfo[i].nvi_vblocknr, vinfo[i].nvi_start,
284 vinfo[i].nvi_end, vinfo[i].nvi_blocknr));
285
286 brelse(req.bp_entry);
287 }
288
289 VOP_UNLOCK(NTOV(dat), 0);
290 return (error);
291 }
292
293 int
294 nandfs_get_dat_bdescs_ioctl(struct nandfs_device *nffsdev,
295 struct nandfs_argv *nargv)
296 {
297 struct nandfs_bdesc *bd;
298 size_t size, sizecheck;
299 int error;
300
301 sizecheck = nargv->nv_nmembs;
302 if (sizecheck >= SIZE_MAX / sizeof(struct nandfs_bdesc))
303 return (EINVAL);
304
305 size = nargv->nv_nmembs * sizeof(struct nandfs_bdesc);
306 bd = malloc(size, M_NANDFSTEMP, M_WAITOK);
307 error = copyin((void *)(uintptr_t)nargv->nv_base, bd, size);
308 if (error) {
309 free(bd, M_NANDFSTEMP);
310 return (error);
311 }
312
313 error = nandfs_get_dat_bdescs(nffsdev, bd, nargv->nv_nmembs);
314
315 if (error == 0)
316 error = copyout(bd, (void *)(uintptr_t)nargv->nv_base, size);
317
318 free(bd, M_NANDFSTEMP);
319 return (error);
320 }
321
322 int
323 nandfs_get_dat_bdescs(struct nandfs_device *nffsdev, struct nandfs_bdesc *bd,
324 uint32_t nmembs)
325 {
326 struct nandfs_node *dat_node;
327 uint64_t map;
328 uint32_t i;
329 int error = 0;
330
331 dat_node = nffsdev->nd_dat_node;
332
333 VOP_LOCK(NTOV(dat_node), LK_EXCLUSIVE);
334
335 for (i = 0; i < nmembs; i++) {
336 DPRINTF(CLEAN,
337 ("%s: bd ino:%#jx oblk:%#jx blocknr:%#jx off:%#jx\n",
338 __func__, (uintmax_t)bd[i].bd_ino,
339 (uintmax_t)bd[i].bd_oblocknr, (uintmax_t)bd[i].bd_blocknr,
340 (uintmax_t)bd[i].bd_offset));
341
342 error = nandfs_bmap_lookup(dat_node, bd[i].bd_offset, &map);
343 if (error)
344 break;
345 bd[i].bd_blocknr = map;
346 }
347
348 VOP_UNLOCK(NTOV(dat_node), 0);
349 return (error);
350 }
Cache object: a8cccfadec36d234c17b92e09a6fd1f0
|