chfs_write.c revision 1.4 1 1.4 ttoth /* $NetBSD: chfs_write.c,v 1.4 2012/08/10 09:26:58 ttoth Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (C) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
8 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
9 1.1 ahoka * All rights reserved.
10 1.1 ahoka *
11 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
12 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
13 1.1 ahoka *
14 1.1 ahoka * Redistribution and use in source and binary forms, with or without
15 1.1 ahoka * modification, are permitted provided that the following conditions
16 1.1 ahoka * are met:
17 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
18 1.1 ahoka * notice, this list of conditions and the following disclaimer.
19 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
21 1.1 ahoka * documentation and/or other materials provided with the distribution.
22 1.1 ahoka *
23 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 ahoka * SUCH DAMAGE.
34 1.1 ahoka */
35 1.1 ahoka
36 1.1 ahoka /*
37 1.1 ahoka * chfs_write.c
38 1.1 ahoka *
39 1.1 ahoka * Created on: 2010.02.17.
40 1.1 ahoka * Author: dtengeri
41 1.1 ahoka */
42 1.1 ahoka
43 1.1 ahoka #include <sys/param.h>
44 1.1 ahoka #include <sys/buf.h>
45 1.1 ahoka
46 1.1 ahoka #include "chfs.h"
47 1.1 ahoka
48 1.1 ahoka int
49 1.1 ahoka chfs_write_flash_vnode(struct chfs_mount *chmp,
50 1.1 ahoka struct chfs_inode *ip, int prio)
51 1.1 ahoka {
52 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
53 1.1 ahoka
54 1.1 ahoka struct chfs_flash_vnode *fvnode;
55 1.1 ahoka struct chfs_vnode_cache* chvc;
56 1.1 ahoka struct chfs_node_ref *nref;
57 1.1 ahoka struct iovec vec;
58 1.1 ahoka size_t size, retlen;
59 1.1 ahoka int err = 0, retries = 0;
60 1.1 ahoka
61 1.1 ahoka if (ip->ino == CHFS_ROOTINO)
62 1.1 ahoka return 0;
63 1.1 ahoka
64 1.1 ahoka fvnode = chfs_alloc_flash_vnode();
65 1.1 ahoka if (!fvnode)
66 1.1 ahoka return ENOMEM;
67 1.1 ahoka
68 1.1 ahoka chvc = ip->chvc;
69 1.1 ahoka
70 1.1 ahoka /* setting up flash_vnode members */
71 1.1 ahoka size = sizeof(*fvnode);
72 1.1 ahoka //dbg("size: %zu | PADDED: %zu\n", size, CHFS_PAD(size));
73 1.1 ahoka fvnode->magic = htole16(CHFS_FS_MAGIC_BITMASK);
74 1.1 ahoka fvnode->type = htole16(CHFS_NODETYPE_VNODE);
75 1.1 ahoka fvnode->length = htole32(CHFS_PAD(size));
76 1.1 ahoka fvnode->hdr_crc = htole32(crc32(0, (uint8_t *)fvnode,
77 1.1 ahoka CHFS_NODE_HDR_SIZE - 4));
78 1.1 ahoka fvnode->vno = htole64(ip->ino);
79 1.1 ahoka fvnode->version = htole64(++ip->chvc->highest_version);
80 1.1 ahoka fvnode->mode = htole32(ip->mode);
81 1.1 ahoka fvnode->dn_size = htole32(ip->size);
82 1.1 ahoka fvnode->atime = htole32(ip->atime);
83 1.1 ahoka fvnode->ctime = htole32(ip->ctime);
84 1.1 ahoka fvnode->mtime = htole32(ip->mtime);
85 1.1 ahoka fvnode->gid = htole32(ip->gid);
86 1.1 ahoka fvnode->uid = htole32(ip->uid);
87 1.1 ahoka fvnode->node_crc = htole32(crc32(0, (uint8_t *)fvnode, size - 4));
88 1.1 ahoka
89 1.1 ahoka /* write out flash_vnode */
90 1.1 ahoka retry:
91 1.1 ahoka if (prio == ALLOC_GC) {
92 1.1 ahoka /* the GC calls this function */
93 1.1 ahoka err = chfs_reserve_space_gc(chmp, CHFS_PAD(size));
94 1.1 ahoka if (err)
95 1.1 ahoka goto out;
96 1.1 ahoka } else {
97 1.1 ahoka chfs_gc_trigger(chmp);
98 1.1 ahoka if (prio == ALLOC_NORMAL)
99 1.1 ahoka err = chfs_reserve_space_normal(chmp,
100 1.1 ahoka CHFS_PAD(size), ALLOC_NORMAL);
101 1.1 ahoka else
102 1.1 ahoka err = chfs_reserve_space_normal(chmp,
103 1.1 ahoka CHFS_PAD(size), ALLOC_DELETION);
104 1.1 ahoka if (err)
105 1.1 ahoka goto out;
106 1.1 ahoka }
107 1.1 ahoka
108 1.1 ahoka nref = chfs_alloc_node_ref(chmp->chm_nextblock);
109 1.1 ahoka if (!nref) {
110 1.1 ahoka err = ENOMEM;
111 1.1 ahoka goto out;
112 1.1 ahoka }
113 1.1 ahoka
114 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
115 1.1 ahoka
116 1.1 ahoka nref->nref_offset = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
117 1.1 ahoka chfs_change_size_free(chmp, chmp->chm_nextblock, -CHFS_PAD(size));
118 1.1 ahoka vec.iov_base = fvnode;
119 1.1 ahoka vec.iov_len = CHFS_PAD(size);
120 1.1 ahoka err = chfs_write_wbuf(chmp, &vec, 1, nref->nref_offset, &retlen);
121 1.1 ahoka if (err || retlen != CHFS_PAD(size)) {
122 1.1 ahoka chfs_err("error while writing out flash vnode to the media\n");
123 1.1 ahoka chfs_err("err: %d | size: %zu | retlen : %zu\n",
124 1.1 ahoka err, CHFS_PAD(size), retlen);
125 1.1 ahoka chfs_change_size_dirty(chmp,
126 1.1 ahoka chmp->chm_nextblock, CHFS_PAD(size));
127 1.1 ahoka if (retries) {
128 1.1 ahoka err = EIO;
129 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
130 1.1 ahoka goto out;
131 1.1 ahoka }
132 1.1 ahoka
133 1.1 ahoka retries++;
134 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
135 1.1 ahoka goto retry;
136 1.1 ahoka }
137 1.1 ahoka //Everything went well
138 1.1 ahoka chfs_change_size_used(chmp,
139 1.1 ahoka &chmp->chm_blocks[nref->nref_lnr], CHFS_PAD(size));
140 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
141 1.1 ahoka
142 1.4 ttoth mutex_enter(&chmp->chm_lock_vnocache);
143 1.1 ahoka chfs_add_vnode_ref_to_vc(chmp, chvc, nref);
144 1.4 ttoth mutex_exit(&chmp->chm_lock_vnocache);
145 1.1 ahoka KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
146 1.1 ahoka out:
147 1.1 ahoka chfs_free_flash_vnode(fvnode);
148 1.1 ahoka return err;
149 1.1 ahoka }
150 1.1 ahoka
151 1.1 ahoka int
152 1.1 ahoka chfs_write_flash_dirent(struct chfs_mount *chmp, struct chfs_inode *pdir,
153 1.1 ahoka struct chfs_inode *ip, struct chfs_dirent *fd,
154 1.1 ahoka ino_t ino, int prio)
155 1.1 ahoka {
156 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
157 1.1 ahoka
158 1.1 ahoka struct chfs_flash_dirent_node *fdirent;
159 1.1 ahoka struct chfs_node_ref *nref;
160 1.1 ahoka struct iovec vec[2];
161 1.1 ahoka size_t size, retlen;
162 1.1 ahoka int err = 0, retries = 0;
163 1.1 ahoka uint8_t *name;
164 1.1 ahoka size_t namelen;
165 1.1 ahoka
166 1.1 ahoka KASSERT(fd->vno != CHFS_ROOTINO);
167 1.1 ahoka
168 1.1 ahoka fdirent = chfs_alloc_flash_dirent();
169 1.1 ahoka if (!fdirent)
170 1.1 ahoka return ENOMEM;
171 1.1 ahoka
172 1.1 ahoka size = sizeof(*fdirent) + fd->nsize;
173 1.1 ahoka namelen = CHFS_PAD(size) - sizeof(*fdirent);
174 1.1 ahoka
175 1.1 ahoka name = kmem_zalloc(namelen, KM_SLEEP);
176 1.1 ahoka memcpy(name, fd->name, fd->nsize);
177 1.1 ahoka //dbg("namelen: %zu | nsize: %hhu\n", namelen, fd->nsize);
178 1.1 ahoka
179 1.1 ahoka
180 1.1 ahoka //dbg("size: %zu | PADDED: %zu\n", size, CHFS_PAD(size));
181 1.1 ahoka fdirent->magic = htole16(CHFS_FS_MAGIC_BITMASK);
182 1.1 ahoka fdirent->type = htole16(CHFS_NODETYPE_DIRENT);
183 1.1 ahoka fdirent->length = htole32(CHFS_PAD(size));
184 1.1 ahoka fdirent->hdr_crc = htole32(crc32(0, (uint8_t *)fdirent,
185 1.1 ahoka CHFS_NODE_HDR_SIZE - 4));
186 1.1 ahoka fdirent->vno = htole64(ino);
187 1.1 ahoka fdirent->pvno = htole64(pdir->ino);
188 1.1 ahoka fdirent->version = htole64(++pdir->chvc->highest_version);
189 1.1 ahoka fdirent->mctime = ip?ip->ctime:0;
190 1.1 ahoka fdirent->nsize = fd->nsize;
191 1.1 ahoka fdirent->dtype = fd->type;
192 1.1 ahoka fdirent->name_crc = crc32(0, (uint8_t *)&(fd->name), fd->nsize);
193 1.1 ahoka fdirent->node_crc = crc32(0, (uint8_t *)fdirent, sizeof(*fdirent) - 4);
194 1.1 ahoka
195 1.1 ahoka vec[0].iov_base = fdirent;
196 1.1 ahoka vec[0].iov_len = sizeof(*fdirent);
197 1.1 ahoka vec[1].iov_base = name;
198 1.1 ahoka vec[1].iov_len = namelen;
199 1.1 ahoka
200 1.1 ahoka retry:
201 1.1 ahoka if (prio == ALLOC_GC) {
202 1.1 ahoka /* the GC calls this function */
203 1.1 ahoka err = chfs_reserve_space_gc(chmp, CHFS_PAD(size));
204 1.1 ahoka if (err)
205 1.1 ahoka goto out;
206 1.1 ahoka } else {
207 1.1 ahoka chfs_gc_trigger(chmp);
208 1.1 ahoka if (prio == ALLOC_NORMAL)
209 1.1 ahoka err = chfs_reserve_space_normal(chmp,
210 1.1 ahoka CHFS_PAD(size), ALLOC_NORMAL);
211 1.1 ahoka else
212 1.1 ahoka err = chfs_reserve_space_normal(chmp,
213 1.1 ahoka CHFS_PAD(size), ALLOC_DELETION);
214 1.1 ahoka if (err)
215 1.1 ahoka goto out;
216 1.1 ahoka }
217 1.1 ahoka
218 1.1 ahoka nref = chfs_alloc_node_ref(chmp->chm_nextblock);
219 1.1 ahoka if (!nref) {
220 1.1 ahoka err = ENOMEM;
221 1.1 ahoka goto out;
222 1.1 ahoka }
223 1.1 ahoka
224 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
225 1.1 ahoka
226 1.1 ahoka nref->nref_offset = chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
227 1.1 ahoka chfs_change_size_free(chmp, chmp->chm_nextblock, -CHFS_PAD(size));
228 1.1 ahoka
229 1.1 ahoka err = chfs_write_wbuf(chmp, vec, 2, nref->nref_offset, &retlen);
230 1.1 ahoka if (err || retlen != CHFS_PAD(size)) {
231 1.1 ahoka chfs_err("error while writing out flash dirent node to the media\n");
232 1.1 ahoka chfs_err("err: %d | size: %zu | retlen : %zu\n",
233 1.1 ahoka err, CHFS_PAD(size), retlen);
234 1.1 ahoka chfs_change_size_dirty(chmp,
235 1.1 ahoka chmp->chm_nextblock, CHFS_PAD(size));
236 1.1 ahoka if (retries) {
237 1.1 ahoka err = EIO;
238 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
239 1.1 ahoka goto out;
240 1.1 ahoka }
241 1.1 ahoka
242 1.1 ahoka retries++;
243 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
244 1.1 ahoka goto retry;
245 1.1 ahoka }
246 1.1 ahoka
247 1.1 ahoka
248 1.1 ahoka // Everything went well
249 1.1 ahoka chfs_change_size_used(chmp,
250 1.1 ahoka &chmp->chm_blocks[nref->nref_lnr], CHFS_PAD(size));
251 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
252 1.1 ahoka KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
253 1.4 ttoth
254 1.1 ahoka fd->nref = nref;
255 1.1 ahoka if (prio != ALLOC_DELETION) {
256 1.4 ttoth mutex_enter(&chmp->chm_lock_vnocache);
257 1.1 ahoka chfs_add_node_to_list(chmp,
258 1.1 ahoka pdir->chvc, nref, &pdir->chvc->dirents);
259 1.4 ttoth mutex_exit(&chmp->chm_lock_vnocache);
260 1.1 ahoka }
261 1.1 ahoka out:
262 1.1 ahoka chfs_free_flash_dirent(fdirent);
263 1.1 ahoka return err;
264 1.1 ahoka }
265 1.1 ahoka
266 1.1 ahoka /**
267 1.1 ahoka * chfs_write_flash_dnode - write out a data node to flash
268 1.1 ahoka * @chmp: chfs mount structure
269 1.1 ahoka * @vp: vnode where the data belongs to
270 1.1 ahoka * @bp: buffer contains data
271 1.1 ahoka */
272 1.1 ahoka int
273 1.1 ahoka chfs_write_flash_dnode(struct chfs_mount *chmp, struct vnode *vp,
274 1.1 ahoka struct buf *bp, struct chfs_full_dnode *fd)
275 1.1 ahoka {
276 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
277 1.1 ahoka
278 1.1 ahoka int err = 0, retries = 0;
279 1.1 ahoka size_t size, retlen;
280 1.1 ahoka off_t ofs;
281 1.1 ahoka struct chfs_flash_data_node *dnode;
282 1.1 ahoka struct chfs_node_ref *nref;
283 1.1 ahoka struct chfs_inode *ip = VTOI(vp);
284 1.1 ahoka struct iovec vec[2];
285 1.1 ahoka uint32_t len;
286 1.1 ahoka void *tmpbuf = NULL;
287 1.1 ahoka
288 1.1 ahoka KASSERT(ip->ino != CHFS_ROOTINO);
289 1.1 ahoka
290 1.1 ahoka dnode = chfs_alloc_flash_dnode();
291 1.1 ahoka if (!dnode)
292 1.1 ahoka return ENOMEM;
293 1.1 ahoka
294 1.1 ahoka /* initialize flash data node */
295 1.1 ahoka ofs = bp->b_blkno * PAGE_SIZE;
296 1.1 ahoka //dbg("vp->v_size: %ju, bp->b_blkno: %ju, bp-b_data: %p,"
297 1.1 ahoka // " bp->b_resid: %ju\n",
298 1.1 ahoka // (uintmax_t )vp->v_size, (uintmax_t )bp->b_blkno,
299 1.1 ahoka // bp->b_data, (uintmax_t )bp->b_resid);
300 1.1 ahoka //dbg("[XXX]vp->v_size - ofs: %llu\n", (vp->v_size - ofs));
301 1.1 ahoka len = MIN((vp->v_size - ofs), bp->b_resid);
302 1.1 ahoka size = sizeof(*dnode) + len;
303 1.1 ahoka
304 1.1 ahoka dnode->magic = htole16(CHFS_FS_MAGIC_BITMASK);
305 1.1 ahoka dnode->type = htole16(CHFS_NODETYPE_DATA);
306 1.1 ahoka dnode->length = htole32(CHFS_PAD(size));
307 1.1 ahoka dnode->hdr_crc = htole32(crc32(0, (uint8_t *)dnode,
308 1.1 ahoka CHFS_NODE_HDR_SIZE - 4));
309 1.1 ahoka dnode->vno = htole64(ip->ino);
310 1.1 ahoka dnode->version = htole64(++ip->chvc->highest_version);
311 1.1 ahoka dnode->offset = htole64(ofs);
312 1.1 ahoka dnode->data_length = htole32(len);
313 1.1 ahoka dnode->data_crc = htole32(crc32(0, (uint8_t *)bp->b_data, len));
314 1.1 ahoka dnode->node_crc = htole32(crc32(0, (uint8_t *)dnode,
315 1.1 ahoka sizeof(*dnode) - 4));
316 1.1 ahoka
317 1.2 agc dbg("dnode @%llu %ub v%llu\n", (unsigned long long)dnode->offset,
318 1.2 agc dnode->data_length, (unsigned long long)dnode->version);
319 1.1 ahoka
320 1.1 ahoka if (CHFS_PAD(size) - sizeof(*dnode)) {
321 1.1 ahoka tmpbuf = kmem_zalloc(CHFS_PAD(size)
322 1.1 ahoka - sizeof(*dnode), KM_SLEEP);
323 1.1 ahoka memcpy(tmpbuf, bp->b_data, len);
324 1.1 ahoka }
325 1.1 ahoka
326 1.1 ahoka /* creating iovecs for wbuf */
327 1.1 ahoka vec[0].iov_base = dnode;
328 1.1 ahoka vec[0].iov_len = sizeof(*dnode);
329 1.1 ahoka vec[1].iov_base = tmpbuf;
330 1.1 ahoka vec[1].iov_len = CHFS_PAD(size) - sizeof(*dnode);
331 1.1 ahoka
332 1.1 ahoka fd->ofs = ofs;
333 1.1 ahoka fd->size = len;
334 1.1 ahoka
335 1.1 ahoka retry:
336 1.1 ahoka
337 1.1 ahoka /* Reserve space for data node. This will set up the next eraseblock
338 1.1 ahoka * where to we will write.
339 1.1 ahoka */
340 1.1 ahoka
341 1.1 ahoka chfs_gc_trigger(chmp);
342 1.1 ahoka err = chfs_reserve_space_normal(chmp,
343 1.1 ahoka CHFS_PAD(size), ALLOC_NORMAL);
344 1.1 ahoka if (err)
345 1.1 ahoka goto out;
346 1.1 ahoka
347 1.1 ahoka nref = chfs_alloc_node_ref(chmp->chm_nextblock);
348 1.1 ahoka if (!nref) {
349 1.1 ahoka err = ENOMEM;
350 1.1 ahoka goto out;
351 1.1 ahoka }
352 1.1 ahoka
353 1.1 ahoka nref->nref_offset =
354 1.1 ahoka chmp->chm_ebh->eb_size - chmp->chm_nextblock->free_size;
355 1.1 ahoka
356 1.1 ahoka KASSERT(nref->nref_offset < chmp->chm_ebh->eb_size);
357 1.1 ahoka
358 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
359 1.1 ahoka
360 1.1 ahoka chfs_change_size_free(chmp,
361 1.1 ahoka chmp->chm_nextblock, -CHFS_PAD(size));
362 1.1 ahoka
363 1.1 ahoka //dbg("vno: %llu nref lnr: %u offset: %u\n",
364 1.1 ahoka // dnode->vno, nref->nref_lnr, nref->nref_offset);
365 1.1 ahoka
366 1.1 ahoka err = chfs_write_wbuf(chmp, vec, 2, nref->nref_offset, &retlen);
367 1.1 ahoka if (err || retlen != CHFS_PAD(size)) {
368 1.1 ahoka chfs_err("error while writing out flash data node to the media\n");
369 1.1 ahoka chfs_err("err: %d | size: %zu | retlen : %zu\n",
370 1.1 ahoka err, size, retlen);
371 1.1 ahoka chfs_change_size_dirty(chmp,
372 1.1 ahoka chmp->chm_nextblock, CHFS_PAD(size));
373 1.1 ahoka if (retries) {
374 1.1 ahoka err = EIO;
375 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
376 1.1 ahoka goto out;
377 1.1 ahoka }
378 1.1 ahoka
379 1.1 ahoka retries++;
380 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
381 1.1 ahoka goto retry;
382 1.1 ahoka }
383 1.1 ahoka /* Everything went well */
384 1.1 ahoka ip->write_size += fd->size;
385 1.1 ahoka chfs_change_size_used(chmp,
386 1.1 ahoka &chmp->chm_blocks[nref->nref_lnr], CHFS_PAD(size));
387 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
388 1.1 ahoka
389 1.4 ttoth mutex_enter(&chmp->chm_lock_vnocache);
390 1.4 ttoth if (fd->nref != NULL) {
391 1.4 ttoth chfs_remove_frags_of_node(chmp, &ip->fragtree, fd->nref);
392 1.4 ttoth chfs_remove_and_obsolete(chmp, ip->chvc, fd->nref, &ip->chvc->dnode);
393 1.4 ttoth }
394 1.4 ttoth
395 1.1 ahoka KASSERT(chmp->chm_blocks[nref->nref_lnr].used_size <= chmp->chm_ebh->eb_size);
396 1.1 ahoka fd->nref = nref;
397 1.1 ahoka chfs_add_node_to_list(chmp, ip->chvc, nref, &ip->chvc->dnode);
398 1.4 ttoth mutex_exit(&chmp->chm_lock_vnocache);
399 1.1 ahoka out:
400 1.1 ahoka chfs_free_flash_dnode(dnode);
401 1.1 ahoka if (CHFS_PAD(size) - sizeof(*dnode)) {
402 1.1 ahoka kmem_free(tmpbuf, CHFS_PAD(size) - sizeof(*dnode));
403 1.1 ahoka }
404 1.1 ahoka
405 1.1 ahoka return err;
406 1.1 ahoka }
407 1.1 ahoka
408 1.1 ahoka /**
409 1.1 ahoka * chfs_do_link - makes a copy from a node
410 1.1 ahoka * @old: old node
411 1.1 ahoka * @oldfd: dirent of old node
412 1.1 ahoka * @parent: parent of new node
413 1.1 ahoka * @name: name of new node
414 1.1 ahoka * @namelen: length of name
415 1.1 ahoka * This function writes the dirent of the new node to the media.
416 1.1 ahoka */
417 1.1 ahoka int
418 1.3 ttoth chfs_do_link(struct chfs_inode *ip, struct chfs_inode *parent, const char *name, int namelen, enum chtype type)
419 1.1 ahoka {
420 1.1 ahoka int error = 0;
421 1.1 ahoka struct vnode *vp = ITOV(ip);
422 1.1 ahoka struct ufsmount *ump = VFSTOUFS(vp->v_mount);
423 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
424 1.1 ahoka struct chfs_dirent *newfd = NULL;
425 1.1 ahoka // struct chfs_dirent *fd = NULL;
426 1.1 ahoka
427 1.1 ahoka //dbg("link vno: %llu\n", ip->ino);
428 1.1 ahoka
429 1.1 ahoka newfd = chfs_alloc_dirent(namelen + 1);
430 1.1 ahoka
431 1.1 ahoka newfd->vno = ip->ino;
432 1.1 ahoka newfd->type = type;
433 1.1 ahoka newfd->nsize = namelen;
434 1.1 ahoka memcpy(newfd->name, name, namelen);
435 1.1 ahoka newfd->name[newfd->nsize] = 0;
436 1.1 ahoka // newfd->next = NULL;
437 1.1 ahoka
438 1.1 ahoka ip->chvc->nlink++;
439 1.1 ahoka parent->chvc->nlink++;
440 1.1 ahoka ip->iflag |= IN_CHANGE;
441 1.1 ahoka chfs_update(vp, NULL, NULL, UPDATE_WAIT);
442 1.1 ahoka
443 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
444 1.1 ahoka
445 1.1 ahoka error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
446 1.1 ahoka if (error)
447 1.1 ahoka return error;
448 1.1 ahoka
449 1.1 ahoka error = chfs_write_flash_dirent(chmp,
450 1.1 ahoka parent, ip, newfd, ip->ino, ALLOC_NORMAL);
451 1.1 ahoka /* TODO: what should we do if error isn't zero? */
452 1.1 ahoka
453 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
454 1.1 ahoka
455 1.1 ahoka /* add fd to the fd list */
456 1.1 ahoka TAILQ_INSERT_TAIL(&parent->dents, newfd, fds);
457 1.1 ahoka #if 0
458 1.1 ahoka fd = parent->dents;
459 1.1 ahoka if (!fd) {
460 1.1 ahoka parent->dents = newfd;
461 1.1 ahoka } else {
462 1.1 ahoka while (fd->next)
463 1.1 ahoka fd = fd->next;
464 1.1 ahoka fd->next = newfd;
465 1.1 ahoka }
466 1.1 ahoka #endif
467 1.1 ahoka
468 1.1 ahoka return error;
469 1.1 ahoka }
470 1.1 ahoka
471 1.1 ahoka
472 1.1 ahoka /**
473 1.1 ahoka * chfs_do_unlink - delete a node
474 1.1 ahoka * @ip: node what we'd like to delete
475 1.1 ahoka * @parent: parent of the node
476 1.1 ahoka * @name: name of the node
477 1.1 ahoka * @namelen: length of name
478 1.1 ahoka * This function set the nlink and vno of the node zero and write its dirent to the media.
479 1.1 ahoka */
480 1.1 ahoka int
481 1.1 ahoka chfs_do_unlink(struct chfs_inode *ip,
482 1.1 ahoka struct chfs_inode *parent, const char *name, int namelen)
483 1.1 ahoka {
484 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
485 1.1 ahoka int error = 0;
486 1.1 ahoka struct vnode *vp = ITOV(ip);
487 1.1 ahoka struct ufsmount *ump = VFSTOUFS(vp->v_mount);
488 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
489 1.1 ahoka struct chfs_node_ref *nref;
490 1.1 ahoka
491 1.1 ahoka //dbg("unlink vno: %llu\n", ip->ino);
492 1.1 ahoka
493 1.1 ahoka vflushbuf(vp, 0);
494 1.1 ahoka
495 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
496 1.1 ahoka
497 1.1 ahoka /* remove the full direntry from the parent dents list */
498 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &parent->dents, fds, tmpfd) {
499 1.1 ahoka if (fd->vno == ip->ino &&
500 1.1 ahoka fd->nsize == namelen &&
501 1.1 ahoka !memcmp(fd->name, name, fd->nsize)) {
502 1.4 ttoth
503 1.4 ttoth chfs_kill_fragtree(chmp, &ip->fragtree);
504 1.4 ttoth
505 1.3 ttoth if (fd->type == CHT_DIR && ip->chvc->nlink == 2)
506 1.1 ahoka ip->chvc->nlink = 0;
507 1.1 ahoka else
508 1.1 ahoka ip->chvc->nlink--;
509 1.1 ahoka
510 1.3 ttoth fd->type = CHT_BLANK;
511 1.1 ahoka
512 1.1 ahoka TAILQ_REMOVE(&parent->dents, fd, fds);
513 1.1 ahoka
514 1.4 ttoth mutex_enter(&chmp->chm_lock_vnocache);
515 1.1 ahoka
516 1.4 ttoth dbg("FD->NREF vno: %llu, lnr: %u, ofs: %u\n",
517 1.4 ttoth fd->vno, fd->nref->nref_lnr, fd->nref->nref_offset);
518 1.4 ttoth chfs_remove_and_obsolete(chmp, parent->chvc, fd->nref,
519 1.4 ttoth &parent->chvc->dirents);
520 1.1 ahoka
521 1.1 ahoka error = chfs_write_flash_dirent(chmp,
522 1.1 ahoka parent, ip, fd, 0, ALLOC_DELETION);
523 1.1 ahoka
524 1.4 ttoth dbg("FD->NREF vno: %llu, lnr: %u, ofs: %u\n",
525 1.4 ttoth fd->vno, fd->nref->nref_lnr, fd->nref->nref_offset);
526 1.4 ttoth // set nref_next field
527 1.4 ttoth chfs_add_node_to_list(chmp, parent->chvc, fd->nref,
528 1.4 ttoth &parent->chvc->dirents);
529 1.4 ttoth // remove from the list
530 1.4 ttoth chfs_remove_and_obsolete(chmp, parent->chvc, fd->nref,
531 1.4 ttoth &parent->chvc->dirents);
532 1.4 ttoth
533 1.4 ttoth // clean dnode list
534 1.4 ttoth while (ip->chvc->dnode != (struct chfs_node_ref *)ip->chvc) {
535 1.4 ttoth nref = ip->chvc->dnode;
536 1.4 ttoth chfs_remove_frags_of_node(chmp, &ip->fragtree, nref);
537 1.4 ttoth chfs_remove_and_obsolete(chmp, ip->chvc, nref, &ip->chvc->dnode);
538 1.1 ahoka }
539 1.1 ahoka
540 1.4 ttoth // clean v list
541 1.4 ttoth while (ip->chvc->v != (struct chfs_node_ref *)ip->chvc) {
542 1.4 ttoth nref = ip->chvc->v;
543 1.4 ttoth chfs_remove_and_obsolete(chmp, ip->chvc, nref, &ip->chvc->v);
544 1.1 ahoka }
545 1.1 ahoka
546 1.1 ahoka parent->chvc->nlink--;
547 1.4 ttoth
548 1.4 ttoth mutex_exit(&chmp->chm_lock_vnocache);
549 1.1 ahoka //TODO: if error
550 1.1 ahoka }
551 1.1 ahoka }
552 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
553 1.1 ahoka
554 1.1 ahoka return error;
555 1.1 ahoka }
556