chfs_vnode.c revision 1.14 1 1.14 hannken /* $NetBSD: chfs_vnode.c,v 1.14 2015/01/11 17:29:57 hannken 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 Tamas Toth <ttoth (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 1.1 ahoka * All rights reserved.
9 1.1 ahoka *
10 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
11 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
12 1.1 ahoka *
13 1.1 ahoka * Redistribution and use in source and binary forms, with or without
14 1.1 ahoka * modification, are permitted provided that the following conditions
15 1.1 ahoka * are met:
16 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
17 1.1 ahoka * notice, this list of conditions and the following disclaimer.
18 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
20 1.1 ahoka * documentation and/or other materials provided with the distribution.
21 1.1 ahoka *
22 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 ahoka * SUCH DAMAGE.
33 1.1 ahoka */
34 1.1 ahoka
35 1.1 ahoka #include "chfs.h"
36 1.1 ahoka #include "chfs_inode.h"
37 1.1 ahoka #include <sys/kauth.h>
38 1.1 ahoka #include <sys/namei.h>
39 1.1 ahoka #include <sys/uio.h>
40 1.1 ahoka #include <sys/buf.h>
41 1.1 ahoka
42 1.3 elad #include <miscfs/genfs/genfs.h>
43 1.3 elad
44 1.8 ttoth /* chfs_vnode_lookup - lookup for a vnode */
45 1.13 hannken static bool
46 1.13 hannken chfs_vnode_lookup_selector(void *ctx, struct vnode *vp)
47 1.13 hannken {
48 1.13 hannken ino_t *ino = ctx;
49 1.13 hannken
50 1.13 hannken return (VTOI(vp) != NULL && VTOI(vp)->ino == *ino);
51 1.13 hannken }
52 1.1 ahoka struct vnode *
53 1.1 ahoka chfs_vnode_lookup(struct chfs_mount *chmp, ino_t vno)
54 1.1 ahoka {
55 1.13 hannken struct vnode_iterator *marker;
56 1.1 ahoka struct vnode *vp;
57 1.1 ahoka
58 1.13 hannken vfs_vnode_iterator_init(chmp->chm_fsmp, &marker);
59 1.13 hannken vp = vfs_vnode_iterator_next(marker, chfs_vnode_lookup_selector, &vno);
60 1.13 hannken vfs_vnode_iterator_destroy(marker);
61 1.13 hannken
62 1.13 hannken return vp;
63 1.1 ahoka }
64 1.1 ahoka
65 1.8 ttoth /* chfs_readvnode - reads a vnode from the flash and setups its inode */
66 1.1 ahoka int
67 1.8 ttoth chfs_readvnode(struct mount *mp, ino_t ino, struct vnode **vpp)
68 1.1 ahoka {
69 1.1 ahoka struct ufsmount* ump = VFSTOUFS(mp);
70 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
71 1.1 ahoka struct chfs_vnode_cache *chvc;
72 1.1 ahoka struct chfs_flash_vnode *chfvn;
73 1.1 ahoka struct chfs_inode *ip;
74 1.1 ahoka int err;
75 1.1 ahoka char* buf;
76 1.1 ahoka size_t retlen, len;
77 1.1 ahoka struct vnode* vp = NULL;
78 1.2 agc dbg("readvnode | ino: %llu\n", (unsigned long long)ino);
79 1.1 ahoka
80 1.1 ahoka len = sizeof(struct chfs_flash_vnode);
81 1.1 ahoka
82 1.1 ahoka KASSERT(vpp != NULL);
83 1.1 ahoka
84 1.1 ahoka if (vpp != NULL) {
85 1.1 ahoka vp = *vpp;
86 1.1 ahoka }
87 1.1 ahoka
88 1.1 ahoka ip = VTOI(vp);
89 1.1 ahoka chvc = ip->chvc;
90 1.1 ahoka
91 1.8 ttoth /* root node is in-memory only */
92 1.1 ahoka if (chvc && ino != CHFS_ROOTINO) {
93 1.7 ttoth dbg("offset: %" PRIu32 ", lnr: %d\n",
94 1.1 ahoka CHFS_GET_OFS(chvc->v->nref_offset), chvc->v->nref_lnr);
95 1.1 ahoka
96 1.1 ahoka KASSERT((void *)chvc != (void *)chvc->v);
97 1.1 ahoka
98 1.8 ttoth /* reading */
99 1.1 ahoka buf = kmem_alloc(len, KM_SLEEP);
100 1.1 ahoka err = chfs_read_leb(chmp, chvc->v->nref_lnr, buf,
101 1.1 ahoka CHFS_GET_OFS(chvc->v->nref_offset), len, &retlen);
102 1.11 he if (err) {
103 1.11 he kmem_free(buf, len);
104 1.1 ahoka return err;
105 1.11 he }
106 1.1 ahoka if (retlen != len) {
107 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
108 1.1 ahoka len, retlen);
109 1.11 he kmem_free(buf, len);
110 1.1 ahoka return EIO;
111 1.1 ahoka }
112 1.1 ahoka chfvn = (struct chfs_flash_vnode*)buf;
113 1.8 ttoth
114 1.8 ttoth /* setup inode fields */
115 1.1 ahoka chfs_set_vnode_size(vp, chfvn->dn_size);
116 1.1 ahoka ip->mode = chfvn->mode;
117 1.4 ttoth ip->ch_type = IFTOCHT(ip->mode);
118 1.4 ttoth vp->v_type = CHTTOVT(ip->ch_type);
119 1.1 ahoka ip->version = chfvn->version;
120 1.1 ahoka ip->uid = chfvn->uid;
121 1.1 ahoka ip->gid = chfvn->gid;
122 1.1 ahoka ip->atime = chfvn->atime;
123 1.1 ahoka ip->mtime = chfvn->mtime;
124 1.1 ahoka ip->ctime = chfvn->ctime;
125 1.8 ttoth
126 1.1 ahoka kmem_free(buf, len);
127 1.1 ahoka }
128 1.1 ahoka
129 1.1 ahoka
130 1.1 ahoka *vpp = vp;
131 1.1 ahoka return 0;
132 1.1 ahoka }
133 1.1 ahoka
134 1.8 ttoth /*
135 1.8 ttoth * chfs_readddirent -
136 1.8 ttoth * reads a directory entry from flash and adds it to its inode
137 1.8 ttoth */
138 1.1 ahoka int
139 1.1 ahoka chfs_readdirent(struct mount *mp, struct chfs_node_ref *chnr, struct chfs_inode *pdir)
140 1.1 ahoka {
141 1.1 ahoka struct ufsmount *ump = VFSTOUFS(mp);
142 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
143 1.1 ahoka struct chfs_flash_dirent_node chfdn;
144 1.8 ttoth struct chfs_dirent *fd;
145 1.1 ahoka size_t len = sizeof(struct chfs_flash_dirent_node);
146 1.1 ahoka size_t retlen;
147 1.1 ahoka int err = 0;
148 1.1 ahoka
149 1.8 ttoth /* read flash_dirent_node */
150 1.1 ahoka err = chfs_read_leb(chmp, chnr->nref_lnr, (char *)&chfdn,
151 1.1 ahoka CHFS_GET_OFS(chnr->nref_offset), len, &retlen);
152 1.1 ahoka if (err) {
153 1.1 ahoka return err;
154 1.1 ahoka }
155 1.1 ahoka if (retlen != len) {
156 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
157 1.1 ahoka retlen, len);
158 1.1 ahoka return EIO;
159 1.1 ahoka }
160 1.1 ahoka
161 1.8 ttoth /* set fields of dirent */
162 1.1 ahoka fd = chfs_alloc_dirent(chfdn.nsize + 1);
163 1.1 ahoka fd->version = chfdn.version;
164 1.1 ahoka fd->vno = chfdn.vno;
165 1.1 ahoka fd->type = chfdn.dtype;
166 1.1 ahoka fd->nsize = chfdn.nsize;
167 1.1 ahoka
168 1.8 ttoth /* read the name of the dirent */
169 1.1 ahoka err = chfs_read_leb(chmp, chnr->nref_lnr, fd->name,
170 1.1 ahoka CHFS_GET_OFS(chnr->nref_offset) + len, chfdn.nsize, &retlen);
171 1.1 ahoka if (err) {
172 1.1 ahoka return err;
173 1.1 ahoka }
174 1.1 ahoka
175 1.1 ahoka if (retlen != chfdn.nsize) {
176 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
177 1.1 ahoka len, retlen);
178 1.1 ahoka return EIO;
179 1.1 ahoka }
180 1.1 ahoka
181 1.1 ahoka fd->name[fd->nsize] = 0;
182 1.1 ahoka fd->nref = chnr;
183 1.1 ahoka
184 1.8 ttoth /* add to inode */
185 1.1 ahoka chfs_add_fd_to_inode(chmp, pdir, fd);
186 1.1 ahoka return 0;
187 1.1 ahoka }
188 1.1 ahoka
189 1.8 ttoth /* chfs_makeinode - makes a new file and initializes its structures */
190 1.1 ahoka int
191 1.1 ahoka chfs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
192 1.4 ttoth struct componentname *cnp, enum vtype type)
193 1.1 ahoka {
194 1.1 ahoka struct chfs_inode *ip, *pdir;
195 1.1 ahoka struct vnode *vp;
196 1.1 ahoka struct ufsmount* ump = VFSTOUFS(dvp->v_mount);
197 1.1 ahoka struct chfs_mount* chmp = ump->um_chfs;
198 1.1 ahoka struct chfs_vnode_cache* chvc;
199 1.3 elad int error;
200 1.1 ahoka ino_t vno;
201 1.8 ttoth struct chfs_dirent *nfd;
202 1.1 ahoka
203 1.1 ahoka dbg("makeinode\n");
204 1.1 ahoka pdir = VTOI(dvp);
205 1.1 ahoka
206 1.1 ahoka *vpp = NULL;
207 1.1 ahoka
208 1.8 ttoth /* number of vnode will be the new maximum */
209 1.1 ahoka vno = ++(chmp->chm_max_vno);
210 1.1 ahoka
211 1.1 ahoka error = VFS_VGET(dvp->v_mount, vno, &vp);
212 1.1 ahoka if (error)
213 1.1 ahoka return (error);
214 1.1 ahoka
215 1.8 ttoth /* setup vnode cache */
216 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
217 1.1 ahoka chvc = chfs_vnode_cache_get(chmp, vno);
218 1.1 ahoka
219 1.1 ahoka chvc->pvno = pdir->ino;
220 1.1 ahoka chvc->vno_version = kmem_alloc(sizeof(uint64_t), KM_SLEEP);
221 1.1 ahoka *(chvc->vno_version) = 1;
222 1.1 ahoka if (type != VDIR)
223 1.1 ahoka chvc->nlink = 1;
224 1.1 ahoka else
225 1.1 ahoka chvc->nlink = 2;
226 1.1 ahoka chvc->state = VNO_STATE_CHECKEDABSENT;
227 1.6 ttoth mutex_exit(&chmp->chm_lock_vnocache);
228 1.1 ahoka
229 1.8 ttoth /* setup inode */
230 1.1 ahoka ip = VTOI(vp);
231 1.1 ahoka ip->ino = vno;
232 1.1 ahoka
233 1.1 ahoka if (type == VDIR)
234 1.1 ahoka chfs_set_vnode_size(vp, 512);
235 1.1 ahoka else
236 1.1 ahoka chfs_set_vnode_size(vp, 0);
237 1.1 ahoka
238 1.1 ahoka ip->uid = kauth_cred_geteuid(cnp->cn_cred);
239 1.1 ahoka ip->gid = kauth_cred_getegid(cnp->cn_cred);
240 1.1 ahoka ip->version = 1;
241 1.1 ahoka ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
242 1.1 ahoka
243 1.1 ahoka ip->chvc = chvc;
244 1.1 ahoka ip->target = NULL;
245 1.1 ahoka
246 1.1 ahoka ip->mode = mode;
247 1.14 hannken vp->v_type = type; /* Rest init'd in chfs_loadvnode(). */
248 1.4 ttoth ip->ch_type = VTTOCHT(vp->v_type);
249 1.3 elad
250 1.8 ttoth /* authorize setting SGID if needed */
251 1.3 elad if (ip->mode & ISGID) {
252 1.3 elad error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
253 1.3 elad vp, NULL, genfs_can_chmod(vp->v_type, cnp->cn_cred, ip->uid,
254 1.3 elad ip->gid, mode));
255 1.3 elad if (error)
256 1.3 elad ip->mode &= ~ISGID;
257 1.3 elad }
258 1.1 ahoka
259 1.8 ttoth /* write vnode information to the flash */
260 1.1 ahoka chfs_update(vp, NULL, NULL, UPDATE_WAIT);
261 1.1 ahoka
262 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
263 1.1 ahoka
264 1.1 ahoka error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
265 1.1 ahoka if (error) {
266 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
267 1.1 ahoka vput(vp);
268 1.1 ahoka return error;
269 1.1 ahoka }
270 1.8 ttoth
271 1.8 ttoth /* update parent's vnode information and write it to the flash */
272 1.1 ahoka pdir->iflag |= (IN_ACCESS | IN_CHANGE | IN_MODIFY | IN_UPDATE);
273 1.1 ahoka chfs_update(dvp, NULL, NULL, UPDATE_WAIT);
274 1.1 ahoka
275 1.1 ahoka error = chfs_write_flash_vnode(chmp, pdir, ALLOC_NORMAL);
276 1.1 ahoka if (error) {
277 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
278 1.1 ahoka vput(vp);
279 1.1 ahoka return error;
280 1.1 ahoka }
281 1.1 ahoka
282 1.8 ttoth /* setup directory entry */
283 1.1 ahoka nfd = chfs_alloc_dirent(cnp->cn_namelen + 1);
284 1.1 ahoka nfd->vno = ip->ino;
285 1.1 ahoka nfd->version = (++pdir->chvc->highest_version);
286 1.4 ttoth nfd->type = ip->ch_type;
287 1.1 ahoka nfd->nsize = cnp->cn_namelen;
288 1.1 ahoka memcpy(&(nfd->name), cnp->cn_nameptr, cnp->cn_namelen);
289 1.1 ahoka nfd->name[nfd->nsize] = 0;
290 1.1 ahoka nfd->nhash = hash32_buf(nfd->name, cnp->cn_namelen, HASH32_BUF_INIT);
291 1.1 ahoka
292 1.8 ttoth /* write out */
293 1.1 ahoka error = chfs_write_flash_dirent(chmp, pdir, ip, nfd, ip->ino, ALLOC_NORMAL);
294 1.1 ahoka if (error) {
295 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
296 1.1 ahoka vput(vp);
297 1.1 ahoka return error;
298 1.1 ahoka }
299 1.1 ahoka
300 1.1 ahoka //TODO set parent's dir times
301 1.1 ahoka
302 1.8 ttoth /* add dirent to parent */
303 1.1 ahoka chfs_add_fd_to_inode(chmp, pdir, nfd);
304 1.8 ttoth
305 1.1 ahoka pdir->chvc->nlink++;
306 1.1 ahoka
307 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
308 1.1 ahoka
309 1.10 hannken VOP_UNLOCK(vp);
310 1.1 ahoka *vpp = vp;
311 1.1 ahoka return (0);
312 1.1 ahoka }
313 1.1 ahoka
314 1.8 ttoth /* chfs_set_vnode_size - updates size of vnode and also inode */
315 1.1 ahoka void
316 1.1 ahoka chfs_set_vnode_size(struct vnode *vp, size_t size)
317 1.1 ahoka {
318 1.1 ahoka struct chfs_inode *ip;
319 1.1 ahoka
320 1.1 ahoka KASSERT(vp != NULL);
321 1.1 ahoka
322 1.1 ahoka ip = VTOI(vp);
323 1.1 ahoka KASSERT(ip != NULL);
324 1.1 ahoka
325 1.1 ahoka ip->size = size;
326 1.1 ahoka vp->v_size = vp->v_writesize = size;
327 1.1 ahoka return;
328 1.1 ahoka }
329 1.1 ahoka
330 1.8 ttoth /*
331 1.8 ttoth * chfs_change_size_free - updates free size
332 1.8 ttoth * "change" parameter is positive if we have to increase the size
333 1.8 ttoth * and negative if we have to decrease it
334 1.8 ttoth */
335 1.1 ahoka void
336 1.1 ahoka chfs_change_size_free(struct chfs_mount *chmp,
337 1.1 ahoka struct chfs_eraseblock *cheb, int change)
338 1.1 ahoka {
339 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
340 1.1 ahoka KASSERT((int)(chmp->chm_free_size + change) >= 0);
341 1.1 ahoka KASSERT((int)(cheb->free_size + change) >= 0);
342 1.1 ahoka KASSERT((int)(cheb->free_size + change) <= chmp->chm_ebh->eb_size);
343 1.1 ahoka chmp->chm_free_size += change;
344 1.1 ahoka cheb->free_size += change;
345 1.1 ahoka return;
346 1.1 ahoka }
347 1.1 ahoka
348 1.8 ttoth /*
349 1.8 ttoth * chfs_change_size_dirty - updates dirty size
350 1.8 ttoth * "change" parameter is positive if we have to increase the size
351 1.8 ttoth * and negative if we have to decrease it
352 1.8 ttoth */
353 1.1 ahoka void
354 1.1 ahoka chfs_change_size_dirty(struct chfs_mount *chmp,
355 1.1 ahoka struct chfs_eraseblock *cheb, int change)
356 1.1 ahoka {
357 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
358 1.1 ahoka KASSERT((int)(chmp->chm_dirty_size + change) >= 0);
359 1.1 ahoka KASSERT((int)(cheb->dirty_size + change) >= 0);
360 1.1 ahoka KASSERT((int)(cheb->dirty_size + change) <= chmp->chm_ebh->eb_size);
361 1.1 ahoka chmp->chm_dirty_size += change;
362 1.1 ahoka cheb->dirty_size += change;
363 1.1 ahoka return;
364 1.1 ahoka }
365 1.1 ahoka
366 1.8 ttoth /*
367 1.8 ttoth * chfs_change_size_unchecked - updates unchecked size
368 1.8 ttoth * "change" parameter is positive if we have to increase the size
369 1.8 ttoth * and negative if we have to decrease it
370 1.8 ttoth */
371 1.1 ahoka void
372 1.1 ahoka chfs_change_size_unchecked(struct chfs_mount *chmp,
373 1.1 ahoka struct chfs_eraseblock *cheb, int change)
374 1.1 ahoka {
375 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
376 1.1 ahoka KASSERT((int)(chmp->chm_unchecked_size + change) >= 0);
377 1.1 ahoka KASSERT((int)(cheb->unchecked_size + change) >= 0);
378 1.1 ahoka KASSERT((int)(cheb->unchecked_size + change) <= chmp->chm_ebh->eb_size);
379 1.1 ahoka chmp->chm_unchecked_size += change;
380 1.1 ahoka cheb->unchecked_size += change;
381 1.1 ahoka return;
382 1.1 ahoka }
383 1.1 ahoka
384 1.8 ttoth /*
385 1.8 ttoth * chfs_change_size_used - updates used size
386 1.8 ttoth * "change" parameter is positive if we have to increase the size
387 1.8 ttoth * and negative if we have to decrease it
388 1.8 ttoth */
389 1.1 ahoka void
390 1.1 ahoka chfs_change_size_used(struct chfs_mount *chmp,
391 1.1 ahoka struct chfs_eraseblock *cheb, int change)
392 1.1 ahoka {
393 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
394 1.1 ahoka KASSERT((int)(chmp->chm_used_size + change) >= 0);
395 1.1 ahoka KASSERT((int)(cheb->used_size + change) >= 0);
396 1.1 ahoka KASSERT((int)(cheb->used_size + change) <= chmp->chm_ebh->eb_size);
397 1.1 ahoka chmp->chm_used_size += change;
398 1.1 ahoka cheb->used_size += change;
399 1.1 ahoka return;
400 1.1 ahoka }
401 1.1 ahoka
402 1.8 ttoth /*
403 1.8 ttoth * chfs_change_size_wasted - updates wasted size
404 1.8 ttoth * "change" parameter is positive if we have to increase the size
405 1.8 ttoth * and negative if we have to decrease it
406 1.8 ttoth */
407 1.1 ahoka void
408 1.1 ahoka chfs_change_size_wasted(struct chfs_mount *chmp,
409 1.1 ahoka struct chfs_eraseblock *cheb, int change)
410 1.1 ahoka {
411 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
412 1.1 ahoka KASSERT((int)(chmp->chm_wasted_size + change) >= 0);
413 1.1 ahoka KASSERT((int)(cheb->wasted_size + change) >= 0);
414 1.1 ahoka KASSERT((int)(cheb->wasted_size + change) <= chmp->chm_ebh->eb_size);
415 1.1 ahoka chmp->chm_wasted_size += change;
416 1.1 ahoka cheb->wasted_size += change;
417 1.1 ahoka return;
418 1.1 ahoka }
419 1.1 ahoka
420