chfs_vnode.c revision 1.11 1 1.11 he /* $NetBSD: chfs_vnode.c,v 1.11 2014/09/01 16:33:20 he 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/malloc.h>
38 1.1 ahoka #include <sys/kauth.h>
39 1.1 ahoka #include <sys/namei.h>
40 1.1 ahoka #include <sys/uio.h>
41 1.1 ahoka #include <sys/buf.h>
42 1.1 ahoka
43 1.3 elad #include <miscfs/genfs/genfs.h>
44 1.3 elad
45 1.8 ttoth /* chfs_vnode_lookup - lookup for a vnode */
46 1.1 ahoka struct vnode *
47 1.1 ahoka chfs_vnode_lookup(struct chfs_mount *chmp, ino_t vno)
48 1.1 ahoka {
49 1.1 ahoka struct vnode *vp;
50 1.1 ahoka struct chfs_inode *ip;
51 1.1 ahoka
52 1.1 ahoka TAILQ_FOREACH(vp, &chmp->chm_fsmp->mnt_vnodelist, v_mntvnodes) {
53 1.1 ahoka ip = VTOI(vp);
54 1.1 ahoka if (ip && ip->ino == vno)
55 1.1 ahoka return vp;
56 1.1 ahoka }
57 1.1 ahoka return NULL;
58 1.1 ahoka }
59 1.1 ahoka
60 1.8 ttoth /* chfs_readvnode - reads a vnode from the flash and setups its inode */
61 1.1 ahoka int
62 1.8 ttoth chfs_readvnode(struct mount *mp, ino_t ino, struct vnode **vpp)
63 1.1 ahoka {
64 1.1 ahoka struct ufsmount* ump = VFSTOUFS(mp);
65 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
66 1.1 ahoka struct chfs_vnode_cache *chvc;
67 1.1 ahoka struct chfs_flash_vnode *chfvn;
68 1.1 ahoka struct chfs_inode *ip;
69 1.1 ahoka int err;
70 1.1 ahoka char* buf;
71 1.1 ahoka size_t retlen, len;
72 1.1 ahoka struct vnode* vp = NULL;
73 1.2 agc dbg("readvnode | ino: %llu\n", (unsigned long long)ino);
74 1.1 ahoka
75 1.1 ahoka len = sizeof(struct chfs_flash_vnode);
76 1.1 ahoka
77 1.1 ahoka KASSERT(vpp != NULL);
78 1.1 ahoka
79 1.1 ahoka if (vpp != NULL) {
80 1.1 ahoka vp = *vpp;
81 1.1 ahoka }
82 1.1 ahoka
83 1.1 ahoka ip = VTOI(vp);
84 1.1 ahoka chvc = ip->chvc;
85 1.1 ahoka
86 1.8 ttoth /* root node is in-memory only */
87 1.1 ahoka if (chvc && ino != CHFS_ROOTINO) {
88 1.7 ttoth dbg("offset: %" PRIu32 ", lnr: %d\n",
89 1.1 ahoka CHFS_GET_OFS(chvc->v->nref_offset), chvc->v->nref_lnr);
90 1.1 ahoka
91 1.1 ahoka KASSERT((void *)chvc != (void *)chvc->v);
92 1.1 ahoka
93 1.8 ttoth /* reading */
94 1.1 ahoka buf = kmem_alloc(len, KM_SLEEP);
95 1.1 ahoka err = chfs_read_leb(chmp, chvc->v->nref_lnr, buf,
96 1.1 ahoka CHFS_GET_OFS(chvc->v->nref_offset), len, &retlen);
97 1.11 he if (err) {
98 1.11 he kmem_free(buf, len);
99 1.1 ahoka return err;
100 1.11 he }
101 1.1 ahoka if (retlen != len) {
102 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
103 1.1 ahoka len, retlen);
104 1.11 he kmem_free(buf, len);
105 1.1 ahoka return EIO;
106 1.1 ahoka }
107 1.1 ahoka chfvn = (struct chfs_flash_vnode*)buf;
108 1.8 ttoth
109 1.8 ttoth /* setup inode fields */
110 1.1 ahoka chfs_set_vnode_size(vp, chfvn->dn_size);
111 1.1 ahoka ip->mode = chfvn->mode;
112 1.4 ttoth ip->ch_type = IFTOCHT(ip->mode);
113 1.4 ttoth vp->v_type = CHTTOVT(ip->ch_type);
114 1.1 ahoka ip->version = chfvn->version;
115 1.1 ahoka ip->uid = chfvn->uid;
116 1.1 ahoka ip->gid = chfvn->gid;
117 1.1 ahoka ip->atime = chfvn->atime;
118 1.1 ahoka ip->mtime = chfvn->mtime;
119 1.1 ahoka ip->ctime = chfvn->ctime;
120 1.8 ttoth
121 1.1 ahoka kmem_free(buf, len);
122 1.1 ahoka }
123 1.1 ahoka
124 1.1 ahoka
125 1.1 ahoka *vpp = vp;
126 1.1 ahoka return 0;
127 1.1 ahoka }
128 1.1 ahoka
129 1.8 ttoth /*
130 1.8 ttoth * chfs_readddirent -
131 1.8 ttoth * reads a directory entry from flash and adds it to its inode
132 1.8 ttoth */
133 1.1 ahoka int
134 1.1 ahoka chfs_readdirent(struct mount *mp, struct chfs_node_ref *chnr, struct chfs_inode *pdir)
135 1.1 ahoka {
136 1.1 ahoka struct ufsmount *ump = VFSTOUFS(mp);
137 1.1 ahoka struct chfs_mount *chmp = ump->um_chfs;
138 1.1 ahoka struct chfs_flash_dirent_node chfdn;
139 1.8 ttoth struct chfs_dirent *fd;
140 1.1 ahoka size_t len = sizeof(struct chfs_flash_dirent_node);
141 1.1 ahoka size_t retlen;
142 1.1 ahoka int err = 0;
143 1.1 ahoka
144 1.8 ttoth /* read flash_dirent_node */
145 1.1 ahoka err = chfs_read_leb(chmp, chnr->nref_lnr, (char *)&chfdn,
146 1.1 ahoka CHFS_GET_OFS(chnr->nref_offset), len, &retlen);
147 1.1 ahoka if (err) {
148 1.1 ahoka return err;
149 1.1 ahoka }
150 1.1 ahoka if (retlen != len) {
151 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
152 1.1 ahoka retlen, len);
153 1.1 ahoka return EIO;
154 1.1 ahoka }
155 1.1 ahoka
156 1.8 ttoth /* set fields of dirent */
157 1.1 ahoka fd = chfs_alloc_dirent(chfdn.nsize + 1);
158 1.1 ahoka fd->version = chfdn.version;
159 1.1 ahoka fd->vno = chfdn.vno;
160 1.1 ahoka fd->type = chfdn.dtype;
161 1.1 ahoka fd->nsize = chfdn.nsize;
162 1.1 ahoka
163 1.8 ttoth /* read the name of the dirent */
164 1.1 ahoka err = chfs_read_leb(chmp, chnr->nref_lnr, fd->name,
165 1.1 ahoka CHFS_GET_OFS(chnr->nref_offset) + len, chfdn.nsize, &retlen);
166 1.1 ahoka if (err) {
167 1.1 ahoka return err;
168 1.1 ahoka }
169 1.1 ahoka
170 1.1 ahoka if (retlen != chfdn.nsize) {
171 1.1 ahoka chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
172 1.1 ahoka len, retlen);
173 1.1 ahoka return EIO;
174 1.1 ahoka }
175 1.1 ahoka
176 1.1 ahoka fd->name[fd->nsize] = 0;
177 1.1 ahoka fd->nref = chnr;
178 1.1 ahoka
179 1.8 ttoth /* add to inode */
180 1.1 ahoka chfs_add_fd_to_inode(chmp, pdir, fd);
181 1.1 ahoka return 0;
182 1.1 ahoka }
183 1.1 ahoka
184 1.8 ttoth /* chfs_makeinode - makes a new file and initializes its structures */
185 1.1 ahoka int
186 1.1 ahoka chfs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
187 1.4 ttoth struct componentname *cnp, enum vtype type)
188 1.1 ahoka {
189 1.1 ahoka struct chfs_inode *ip, *pdir;
190 1.1 ahoka struct vnode *vp;
191 1.1 ahoka struct ufsmount* ump = VFSTOUFS(dvp->v_mount);
192 1.1 ahoka struct chfs_mount* chmp = ump->um_chfs;
193 1.1 ahoka struct chfs_vnode_cache* chvc;
194 1.3 elad int error;
195 1.1 ahoka ino_t vno;
196 1.8 ttoth struct chfs_dirent *nfd;
197 1.1 ahoka
198 1.1 ahoka dbg("makeinode\n");
199 1.1 ahoka pdir = VTOI(dvp);
200 1.1 ahoka
201 1.1 ahoka *vpp = NULL;
202 1.1 ahoka
203 1.8 ttoth /* number of vnode will be the new maximum */
204 1.1 ahoka vno = ++(chmp->chm_max_vno);
205 1.1 ahoka
206 1.1 ahoka error = VFS_VGET(dvp->v_mount, vno, &vp);
207 1.1 ahoka if (error)
208 1.1 ahoka return (error);
209 1.1 ahoka
210 1.8 ttoth /* setup vnode cache */
211 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
212 1.1 ahoka chvc = chfs_vnode_cache_get(chmp, vno);
213 1.1 ahoka
214 1.1 ahoka chvc->pvno = pdir->ino;
215 1.1 ahoka chvc->vno_version = kmem_alloc(sizeof(uint64_t), KM_SLEEP);
216 1.1 ahoka *(chvc->vno_version) = 1;
217 1.1 ahoka if (type != VDIR)
218 1.1 ahoka chvc->nlink = 1;
219 1.1 ahoka else
220 1.1 ahoka chvc->nlink = 2;
221 1.1 ahoka chvc->state = VNO_STATE_CHECKEDABSENT;
222 1.6 ttoth mutex_exit(&chmp->chm_lock_vnocache);
223 1.1 ahoka
224 1.8 ttoth /* setup inode */
225 1.1 ahoka ip = VTOI(vp);
226 1.1 ahoka ip->ino = vno;
227 1.1 ahoka
228 1.1 ahoka if (type == VDIR)
229 1.1 ahoka chfs_set_vnode_size(vp, 512);
230 1.1 ahoka else
231 1.1 ahoka chfs_set_vnode_size(vp, 0);
232 1.1 ahoka
233 1.1 ahoka ip->uid = kauth_cred_geteuid(cnp->cn_cred);
234 1.1 ahoka ip->gid = kauth_cred_getegid(cnp->cn_cred);
235 1.1 ahoka ip->version = 1;
236 1.1 ahoka ip->iflag |= (IN_ACCESS | IN_CHANGE | IN_UPDATE);
237 1.1 ahoka
238 1.1 ahoka ip->chvc = chvc;
239 1.1 ahoka ip->target = NULL;
240 1.1 ahoka
241 1.1 ahoka ip->mode = mode;
242 1.4 ttoth vp->v_type = type; /* Rest init'd in getnewvnode(). */
243 1.4 ttoth ip->ch_type = VTTOCHT(vp->v_type);
244 1.3 elad
245 1.8 ttoth /* authorize setting SGID if needed */
246 1.3 elad if (ip->mode & ISGID) {
247 1.3 elad error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY,
248 1.3 elad vp, NULL, genfs_can_chmod(vp->v_type, cnp->cn_cred, ip->uid,
249 1.3 elad ip->gid, mode));
250 1.3 elad if (error)
251 1.3 elad ip->mode &= ~ISGID;
252 1.3 elad }
253 1.1 ahoka
254 1.8 ttoth /* write vnode information to the flash */
255 1.1 ahoka chfs_update(vp, NULL, NULL, UPDATE_WAIT);
256 1.1 ahoka
257 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
258 1.1 ahoka
259 1.1 ahoka error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
260 1.1 ahoka if (error) {
261 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
262 1.1 ahoka vput(vp);
263 1.1 ahoka return error;
264 1.1 ahoka }
265 1.8 ttoth
266 1.8 ttoth /* update parent's vnode information and write it to the flash */
267 1.1 ahoka pdir->iflag |= (IN_ACCESS | IN_CHANGE | IN_MODIFY | IN_UPDATE);
268 1.1 ahoka chfs_update(dvp, NULL, NULL, UPDATE_WAIT);
269 1.1 ahoka
270 1.1 ahoka error = chfs_write_flash_vnode(chmp, pdir, ALLOC_NORMAL);
271 1.1 ahoka if (error) {
272 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
273 1.1 ahoka vput(vp);
274 1.1 ahoka return error;
275 1.1 ahoka }
276 1.1 ahoka
277 1.8 ttoth /* setup directory entry */
278 1.1 ahoka nfd = chfs_alloc_dirent(cnp->cn_namelen + 1);
279 1.1 ahoka nfd->vno = ip->ino;
280 1.1 ahoka nfd->version = (++pdir->chvc->highest_version);
281 1.4 ttoth nfd->type = ip->ch_type;
282 1.1 ahoka nfd->nsize = cnp->cn_namelen;
283 1.1 ahoka memcpy(&(nfd->name), cnp->cn_nameptr, cnp->cn_namelen);
284 1.1 ahoka nfd->name[nfd->nsize] = 0;
285 1.1 ahoka nfd->nhash = hash32_buf(nfd->name, cnp->cn_namelen, HASH32_BUF_INIT);
286 1.1 ahoka
287 1.8 ttoth /* write out */
288 1.1 ahoka error = chfs_write_flash_dirent(chmp, pdir, ip, nfd, ip->ino, ALLOC_NORMAL);
289 1.1 ahoka if (error) {
290 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
291 1.1 ahoka vput(vp);
292 1.1 ahoka return error;
293 1.1 ahoka }
294 1.1 ahoka
295 1.1 ahoka //TODO set parent's dir times
296 1.1 ahoka
297 1.8 ttoth /* add dirent to parent */
298 1.1 ahoka chfs_add_fd_to_inode(chmp, pdir, nfd);
299 1.8 ttoth
300 1.1 ahoka pdir->chvc->nlink++;
301 1.1 ahoka
302 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
303 1.1 ahoka
304 1.10 hannken VOP_UNLOCK(vp);
305 1.1 ahoka *vpp = vp;
306 1.1 ahoka return (0);
307 1.1 ahoka }
308 1.1 ahoka
309 1.8 ttoth /* chfs_set_vnode_size - updates size of vnode and also inode */
310 1.1 ahoka void
311 1.1 ahoka chfs_set_vnode_size(struct vnode *vp, size_t size)
312 1.1 ahoka {
313 1.1 ahoka struct chfs_inode *ip;
314 1.1 ahoka
315 1.1 ahoka KASSERT(vp != NULL);
316 1.1 ahoka
317 1.1 ahoka ip = VTOI(vp);
318 1.1 ahoka KASSERT(ip != NULL);
319 1.1 ahoka
320 1.1 ahoka ip->size = size;
321 1.1 ahoka vp->v_size = vp->v_writesize = size;
322 1.1 ahoka return;
323 1.1 ahoka }
324 1.1 ahoka
325 1.8 ttoth /*
326 1.8 ttoth * chfs_change_size_free - updates free size
327 1.8 ttoth * "change" parameter is positive if we have to increase the size
328 1.8 ttoth * and negative if we have to decrease it
329 1.8 ttoth */
330 1.1 ahoka void
331 1.1 ahoka chfs_change_size_free(struct chfs_mount *chmp,
332 1.1 ahoka struct chfs_eraseblock *cheb, int change)
333 1.1 ahoka {
334 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
335 1.1 ahoka KASSERT((int)(chmp->chm_free_size + change) >= 0);
336 1.1 ahoka KASSERT((int)(cheb->free_size + change) >= 0);
337 1.1 ahoka KASSERT((int)(cheb->free_size + change) <= chmp->chm_ebh->eb_size);
338 1.1 ahoka chmp->chm_free_size += change;
339 1.1 ahoka cheb->free_size += change;
340 1.1 ahoka return;
341 1.1 ahoka }
342 1.1 ahoka
343 1.8 ttoth /*
344 1.8 ttoth * chfs_change_size_dirty - updates dirty size
345 1.8 ttoth * "change" parameter is positive if we have to increase the size
346 1.8 ttoth * and negative if we have to decrease it
347 1.8 ttoth */
348 1.1 ahoka void
349 1.1 ahoka chfs_change_size_dirty(struct chfs_mount *chmp,
350 1.1 ahoka struct chfs_eraseblock *cheb, int change)
351 1.1 ahoka {
352 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
353 1.1 ahoka KASSERT((int)(chmp->chm_dirty_size + change) >= 0);
354 1.1 ahoka KASSERT((int)(cheb->dirty_size + change) >= 0);
355 1.1 ahoka KASSERT((int)(cheb->dirty_size + change) <= chmp->chm_ebh->eb_size);
356 1.1 ahoka chmp->chm_dirty_size += change;
357 1.1 ahoka cheb->dirty_size += change;
358 1.1 ahoka return;
359 1.1 ahoka }
360 1.1 ahoka
361 1.8 ttoth /*
362 1.8 ttoth * chfs_change_size_unchecked - updates unchecked size
363 1.8 ttoth * "change" parameter is positive if we have to increase the size
364 1.8 ttoth * and negative if we have to decrease it
365 1.8 ttoth */
366 1.1 ahoka void
367 1.1 ahoka chfs_change_size_unchecked(struct chfs_mount *chmp,
368 1.1 ahoka struct chfs_eraseblock *cheb, int change)
369 1.1 ahoka {
370 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
371 1.1 ahoka KASSERT((int)(chmp->chm_unchecked_size + change) >= 0);
372 1.1 ahoka KASSERT((int)(cheb->unchecked_size + change) >= 0);
373 1.1 ahoka KASSERT((int)(cheb->unchecked_size + change) <= chmp->chm_ebh->eb_size);
374 1.1 ahoka chmp->chm_unchecked_size += change;
375 1.1 ahoka cheb->unchecked_size += change;
376 1.1 ahoka return;
377 1.1 ahoka }
378 1.1 ahoka
379 1.8 ttoth /*
380 1.8 ttoth * chfs_change_size_used - updates used size
381 1.8 ttoth * "change" parameter is positive if we have to increase the size
382 1.8 ttoth * and negative if we have to decrease it
383 1.8 ttoth */
384 1.1 ahoka void
385 1.1 ahoka chfs_change_size_used(struct chfs_mount *chmp,
386 1.1 ahoka struct chfs_eraseblock *cheb, int change)
387 1.1 ahoka {
388 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
389 1.1 ahoka KASSERT((int)(chmp->chm_used_size + change) >= 0);
390 1.1 ahoka KASSERT((int)(cheb->used_size + change) >= 0);
391 1.1 ahoka KASSERT((int)(cheb->used_size + change) <= chmp->chm_ebh->eb_size);
392 1.1 ahoka chmp->chm_used_size += change;
393 1.1 ahoka cheb->used_size += change;
394 1.1 ahoka return;
395 1.1 ahoka }
396 1.1 ahoka
397 1.8 ttoth /*
398 1.8 ttoth * chfs_change_size_wasted - updates wasted size
399 1.8 ttoth * "change" parameter is positive if we have to increase the size
400 1.8 ttoth * and negative if we have to decrease it
401 1.8 ttoth */
402 1.1 ahoka void
403 1.1 ahoka chfs_change_size_wasted(struct chfs_mount *chmp,
404 1.1 ahoka struct chfs_eraseblock *cheb, int change)
405 1.1 ahoka {
406 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_sizes));
407 1.1 ahoka KASSERT((int)(chmp->chm_wasted_size + change) >= 0);
408 1.1 ahoka KASSERT((int)(cheb->wasted_size + change) >= 0);
409 1.1 ahoka KASSERT((int)(cheb->wasted_size + change) <= chmp->chm_ebh->eb_size);
410 1.1 ahoka chmp->chm_wasted_size += change;
411 1.1 ahoka cheb->wasted_size += change;
412 1.1 ahoka return;
413 1.1 ahoka }
414 1.1 ahoka
415