1 /* $NetBSD: chfs_vnode.c,v 1.14 2015/01/11 17:29:57 hannken Exp $ */
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (C) 2010 Tamas Toth <ttoth@inf.u-szeged.hu>
7 * Copyright (C) 2010 Adam Hoka <ahoka@NetBSD.org>
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by the Department of Software Engineering, University of Szeged, Hungary
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "chfs_inode.h"
37 #include <sys/kauth.h>
38 #include <sys/namei.h>
42 #include <miscfs/genfs/genfs.h>
44 /* chfs_vnode_lookup - lookup for a vnode */
46 chfs_vnode_lookup_selector(void *ctx
, struct vnode
*vp
)
50 return (VTOI(vp
) != NULL
&& VTOI(vp
)->ino
== *ino
);
53 chfs_vnode_lookup(struct chfs_mount
*chmp
, ino_t vno
)
55 struct vnode_iterator
*marker
;
58 vfs_vnode_iterator_init(chmp
->chm_fsmp
, &marker
);
59 vp
= vfs_vnode_iterator_next(marker
, chfs_vnode_lookup_selector
, &vno
);
60 vfs_vnode_iterator_destroy(marker
);
65 /* chfs_readvnode - reads a vnode from the flash and setups its inode */
67 chfs_readvnode(struct mount
*mp
, ino_t ino
, struct vnode
**vpp
)
69 struct ufsmount
* ump
= VFSTOUFS(mp
);
70 struct chfs_mount
*chmp
= ump
->um_chfs
;
71 struct chfs_vnode_cache
*chvc
;
72 struct chfs_flash_vnode
*chfvn
;
73 struct chfs_inode
*ip
;
77 struct vnode
* vp
= NULL
;
78 dbg("readvnode | ino: %llu\n", (unsigned long long)ino
);
80 len
= sizeof(struct chfs_flash_vnode
);
91 /* root node is in-memory only */
92 if (chvc
&& ino
!= CHFS_ROOTINO
) {
93 dbg("offset: %" PRIu32
", lnr: %d\n",
94 CHFS_GET_OFS(chvc
->v
->nref_offset
), chvc
->v
->nref_lnr
);
96 KASSERT((void *)chvc
!= (void *)chvc
->v
);
99 buf
= kmem_alloc(len
, KM_SLEEP
);
100 err
= chfs_read_leb(chmp
, chvc
->v
->nref_lnr
, buf
,
101 CHFS_GET_OFS(chvc
->v
->nref_offset
), len
, &retlen
);
107 chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
112 chfvn
= (struct chfs_flash_vnode
*)buf
;
114 /* setup inode fields */
115 chfs_set_vnode_size(vp
, chfvn
->dn_size
);
116 ip
->mode
= chfvn
->mode
;
117 ip
->ch_type
= IFTOCHT(ip
->mode
);
118 vp
->v_type
= CHTTOVT(ip
->ch_type
);
119 ip
->version
= chfvn
->version
;
120 ip
->uid
= chfvn
->uid
;
121 ip
->gid
= chfvn
->gid
;
122 ip
->atime
= chfvn
->atime
;
123 ip
->mtime
= chfvn
->mtime
;
124 ip
->ctime
= chfvn
->ctime
;
136 * reads a directory entry from flash and adds it to its inode
139 chfs_readdirent(struct mount
*mp
, struct chfs_node_ref
*chnr
, struct chfs_inode
*pdir
)
141 struct ufsmount
*ump
= VFSTOUFS(mp
);
142 struct chfs_mount
*chmp
= ump
->um_chfs
;
143 struct chfs_flash_dirent_node chfdn
;
144 struct chfs_dirent
*fd
;
145 size_t len
= sizeof(struct chfs_flash_dirent_node
);
149 /* read flash_dirent_node */
150 err
= chfs_read_leb(chmp
, chnr
->nref_lnr
, (char *)&chfdn
,
151 CHFS_GET_OFS(chnr
->nref_offset
), len
, &retlen
);
156 chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
161 /* set fields of dirent */
162 fd
= chfs_alloc_dirent(chfdn
.nsize
+ 1);
163 fd
->version
= chfdn
.version
;
165 fd
->type
= chfdn
.dtype
;
166 fd
->nsize
= chfdn
.nsize
;
168 /* read the name of the dirent */
169 err
= chfs_read_leb(chmp
, chnr
->nref_lnr
, fd
->name
,
170 CHFS_GET_OFS(chnr
->nref_offset
) + len
, chfdn
.nsize
, &retlen
);
175 if (retlen
!= chfdn
.nsize
) {
176 chfs_err("Error reading vnode: read: %zu insted of: %zu\n",
181 fd
->name
[fd
->nsize
] = 0;
185 chfs_add_fd_to_inode(chmp
, pdir
, fd
);
189 /* chfs_makeinode - makes a new file and initializes its structures */
191 chfs_makeinode(int mode
, struct vnode
*dvp
, struct vnode
**vpp
,
192 struct componentname
*cnp
, enum vtype type
)
194 struct chfs_inode
*ip
, *pdir
;
196 struct ufsmount
* ump
= VFSTOUFS(dvp
->v_mount
);
197 struct chfs_mount
* chmp
= ump
->um_chfs
;
198 struct chfs_vnode_cache
* chvc
;
201 struct chfs_dirent
*nfd
;
208 /* number of vnode will be the new maximum */
209 vno
= ++(chmp
->chm_max_vno
);
211 error
= VFS_VGET(dvp
->v_mount
, vno
, &vp
);
215 /* setup vnode cache */
216 mutex_enter(&chmp
->chm_lock_vnocache
);
217 chvc
= chfs_vnode_cache_get(chmp
, vno
);
219 chvc
->pvno
= pdir
->ino
;
220 chvc
->vno_version
= kmem_alloc(sizeof(uint64_t), KM_SLEEP
);
221 *(chvc
->vno_version
) = 1;
226 chvc
->state
= VNO_STATE_CHECKEDABSENT
;
227 mutex_exit(&chmp
->chm_lock_vnocache
);
234 chfs_set_vnode_size(vp
, 512);
236 chfs_set_vnode_size(vp
, 0);
238 ip
->uid
= kauth_cred_geteuid(cnp
->cn_cred
);
239 ip
->gid
= kauth_cred_getegid(cnp
->cn_cred
);
241 ip
->iflag
|= (IN_ACCESS
| IN_CHANGE
| IN_UPDATE
);
247 vp
->v_type
= type
; /* Rest init'd in chfs_loadvnode(). */
248 ip
->ch_type
= VTTOCHT(vp
->v_type
);
250 /* authorize setting SGID if needed */
251 if (ip
->mode
& ISGID
) {
252 error
= kauth_authorize_vnode(cnp
->cn_cred
, KAUTH_VNODE_WRITE_SECURITY
,
253 vp
, NULL
, genfs_can_chmod(vp
->v_type
, cnp
->cn_cred
, ip
->uid
,
259 /* write vnode information to the flash */
260 chfs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
262 mutex_enter(&chmp
->chm_lock_mountfields
);
264 error
= chfs_write_flash_vnode(chmp
, ip
, ALLOC_NORMAL
);
266 mutex_exit(&chmp
->chm_lock_mountfields
);
271 /* update parent's vnode information and write it to the flash */
272 pdir
->iflag
|= (IN_ACCESS
| IN_CHANGE
| IN_MODIFY
| IN_UPDATE
);
273 chfs_update(dvp
, NULL
, NULL
, UPDATE_WAIT
);
275 error
= chfs_write_flash_vnode(chmp
, pdir
, ALLOC_NORMAL
);
277 mutex_exit(&chmp
->chm_lock_mountfields
);
282 /* setup directory entry */
283 nfd
= chfs_alloc_dirent(cnp
->cn_namelen
+ 1);
285 nfd
->version
= (++pdir
->chvc
->highest_version
);
286 nfd
->type
= ip
->ch_type
;
287 nfd
->nsize
= cnp
->cn_namelen
;
288 memcpy(&(nfd
->name
), cnp
->cn_nameptr
, cnp
->cn_namelen
);
289 nfd
->name
[nfd
->nsize
] = 0;
290 nfd
->nhash
= hash32_buf(nfd
->name
, cnp
->cn_namelen
, HASH32_BUF_INIT
);
293 error
= chfs_write_flash_dirent(chmp
, pdir
, ip
, nfd
, ip
->ino
, ALLOC_NORMAL
);
295 mutex_exit(&chmp
->chm_lock_mountfields
);
300 //TODO set parent's dir times
302 /* add dirent to parent */
303 chfs_add_fd_to_inode(chmp
, pdir
, nfd
);
307 mutex_exit(&chmp
->chm_lock_mountfields
);
314 /* chfs_set_vnode_size - updates size of vnode and also inode */
316 chfs_set_vnode_size(struct vnode
*vp
, size_t size
)
318 struct chfs_inode
*ip
;
326 vp
->v_size
= vp
->v_writesize
= size
;
331 * chfs_change_size_free - updates free size
332 * "change" parameter is positive if we have to increase the size
333 * and negative if we have to decrease it
336 chfs_change_size_free(struct chfs_mount
*chmp
,
337 struct chfs_eraseblock
*cheb
, int change
)
339 KASSERT(mutex_owned(&chmp
->chm_lock_sizes
));
340 KASSERT((int)(chmp
->chm_free_size
+ change
) >= 0);
341 KASSERT((int)(cheb
->free_size
+ change
) >= 0);
342 KASSERT((int)(cheb
->free_size
+ change
) <= chmp
->chm_ebh
->eb_size
);
343 chmp
->chm_free_size
+= change
;
344 cheb
->free_size
+= change
;
349 * chfs_change_size_dirty - updates dirty size
350 * "change" parameter is positive if we have to increase the size
351 * and negative if we have to decrease it
354 chfs_change_size_dirty(struct chfs_mount
*chmp
,
355 struct chfs_eraseblock
*cheb
, int change
)
357 KASSERT(mutex_owned(&chmp
->chm_lock_sizes
));
358 KASSERT((int)(chmp
->chm_dirty_size
+ change
) >= 0);
359 KASSERT((int)(cheb
->dirty_size
+ change
) >= 0);
360 KASSERT((int)(cheb
->dirty_size
+ change
) <= chmp
->chm_ebh
->eb_size
);
361 chmp
->chm_dirty_size
+= change
;
362 cheb
->dirty_size
+= change
;
367 * chfs_change_size_unchecked - updates unchecked size
368 * "change" parameter is positive if we have to increase the size
369 * and negative if we have to decrease it
372 chfs_change_size_unchecked(struct chfs_mount
*chmp
,
373 struct chfs_eraseblock
*cheb
, int change
)
375 KASSERT(mutex_owned(&chmp
->chm_lock_sizes
));
376 KASSERT((int)(chmp
->chm_unchecked_size
+ change
) >= 0);
377 KASSERT((int)(cheb
->unchecked_size
+ change
) >= 0);
378 KASSERT((int)(cheb
->unchecked_size
+ change
) <= chmp
->chm_ebh
->eb_size
);
379 chmp
->chm_unchecked_size
+= change
;
380 cheb
->unchecked_size
+= change
;
385 * chfs_change_size_used - updates used size
386 * "change" parameter is positive if we have to increase the size
387 * and negative if we have to decrease it
390 chfs_change_size_used(struct chfs_mount
*chmp
,
391 struct chfs_eraseblock
*cheb
, int change
)
393 KASSERT(mutex_owned(&chmp
->chm_lock_sizes
));
394 KASSERT((int)(chmp
->chm_used_size
+ change
) >= 0);
395 KASSERT((int)(cheb
->used_size
+ change
) >= 0);
396 KASSERT((int)(cheb
->used_size
+ change
) <= chmp
->chm_ebh
->eb_size
);
397 chmp
->chm_used_size
+= change
;
398 cheb
->used_size
+= change
;
403 * chfs_change_size_wasted - updates wasted size
404 * "change" parameter is positive if we have to increase the size
405 * and negative if we have to decrease it
408 chfs_change_size_wasted(struct chfs_mount
*chmp
,
409 struct chfs_eraseblock
*cheb
, int change
)
411 KASSERT(mutex_owned(&chmp
->chm_lock_sizes
));
412 KASSERT((int)(chmp
->chm_wasted_size
+ change
) >= 0);
413 KASSERT((int)(cheb
->wasted_size
+ change
) >= 0);
414 KASSERT((int)(cheb
->wasted_size
+ change
) <= chmp
->chm_ebh
->eb_size
);
415 chmp
->chm_wasted_size
+= change
;
416 cheb
->wasted_size
+= change
;