chfs_subr.c revision 1.7 1 1.7 ttoth /* $NetBSD: chfs_subr.c,v 1.7 2012/08/22 09:20:13 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 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 /*
36 1.1 ahoka * Efficient memory file system supporting functions.
37 1.1 ahoka */
38 1.1 ahoka
39 1.1 ahoka #include <sys/cdefs.h>
40 1.1 ahoka
41 1.1 ahoka #include <sys/param.h>
42 1.1 ahoka #include <sys/dirent.h>
43 1.1 ahoka #include <sys/event.h>
44 1.1 ahoka #include <sys/kmem.h>
45 1.1 ahoka #include <sys/mount.h>
46 1.1 ahoka #include <sys/namei.h>
47 1.1 ahoka #include <sys/time.h>
48 1.1 ahoka #include <sys/stat.h>
49 1.1 ahoka #include <sys/systm.h>
50 1.1 ahoka #include <sys/swap.h>
51 1.1 ahoka #include <sys/vnode.h>
52 1.1 ahoka #include <sys/kauth.h>
53 1.1 ahoka #include <sys/proc.h>
54 1.1 ahoka #include <sys/atomic.h>
55 1.1 ahoka
56 1.1 ahoka #include <uvm/uvm.h>
57 1.1 ahoka
58 1.1 ahoka #include <miscfs/specfs/specdev.h>
59 1.3 elad #include <miscfs/genfs/genfs.h>
60 1.1 ahoka #include "chfs.h"
61 1.1 ahoka //#include <fs/chfs/chfs_vnops.h>
62 1.1 ahoka //#include </root/xipffs/netbsd.chfs/chfs.h>
63 1.1 ahoka
64 1.1 ahoka /* --------------------------------------------------------------------- */
65 1.1 ahoka
66 1.1 ahoka /*
67 1.1 ahoka * Returns information about the number of available memory pages,
68 1.1 ahoka * including physical and virtual ones.
69 1.1 ahoka *
70 1.1 ahoka * If 'total' is true, the value returned is the total amount of memory
71 1.1 ahoka * pages configured for the system (either in use or free).
72 1.1 ahoka * If it is FALSE, the value returned is the amount of free memory pages.
73 1.1 ahoka *
74 1.1 ahoka * Remember to remove DUMMYFS_PAGES_RESERVED from the returned value to avoid
75 1.1 ahoka * excessive memory usage.
76 1.1 ahoka *
77 1.1 ahoka */
78 1.1 ahoka size_t
79 1.1 ahoka chfs_mem_info(bool total)
80 1.1 ahoka {
81 1.1 ahoka size_t size;
82 1.1 ahoka
83 1.1 ahoka size = 0;
84 1.1 ahoka size += uvmexp.swpgavail;
85 1.1 ahoka if (!total) {
86 1.1 ahoka size -= uvmexp.swpgonly;
87 1.1 ahoka }
88 1.1 ahoka size += uvmexp.free;
89 1.1 ahoka size += uvmexp.filepages;
90 1.1 ahoka if (size > uvmexp.wired) {
91 1.1 ahoka size -= uvmexp.wired;
92 1.1 ahoka } else {
93 1.1 ahoka size = 0;
94 1.1 ahoka }
95 1.1 ahoka
96 1.1 ahoka return size;
97 1.1 ahoka }
98 1.1 ahoka
99 1.1 ahoka
100 1.1 ahoka /* --------------------------------------------------------------------- */
101 1.1 ahoka
102 1.1 ahoka /*
103 1.1 ahoka * Looks for a directory entry in the directory represented by node.
104 1.1 ahoka * 'cnp' describes the name of the entry to look for. Note that the .
105 1.1 ahoka * and .. components are not allowed as they do not physically exist
106 1.1 ahoka * within directories.
107 1.1 ahoka *
108 1.1 ahoka * Returns a pointer to the entry when found, otherwise NULL.
109 1.1 ahoka */
110 1.1 ahoka struct chfs_dirent *
111 1.1 ahoka chfs_dir_lookup(struct chfs_inode *ip, struct componentname *cnp)
112 1.1 ahoka {
113 1.1 ahoka bool found;
114 1.1 ahoka struct chfs_dirent *fd;
115 1.1 ahoka dbg("dir_lookup()\n");
116 1.1 ahoka
117 1.1 ahoka KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
118 1.1 ahoka KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
119 1.1 ahoka cnp->cn_nameptr[1] == '.')));
120 1.1 ahoka //CHFS_VALIDATE_DIR(node);
121 1.1 ahoka
122 1.1 ahoka //node->chn_status |= CHFS_NODE_ACCESSED;
123 1.1 ahoka
124 1.1 ahoka found = false;
125 1.1 ahoka // fd = ip->dents;
126 1.1 ahoka // while(fd) {
127 1.1 ahoka TAILQ_FOREACH(fd, &ip->dents, fds) {
128 1.1 ahoka KASSERT(cnp->cn_namelen < 0xffff);
129 1.1 ahoka if (fd->vno == 0)
130 1.1 ahoka continue;
131 1.1 ahoka /*dbg("dirent dump:\n");
132 1.1 ahoka dbg(" ->vno: %d\n", fd->vno);
133 1.1 ahoka dbg(" ->version: %ld\n", fd->version);
134 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
135 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
136 1.1 ahoka dbg(" ->name: %s\n", fd->name);
137 1.1 ahoka dbg(" ->type: %d\n", fd->type);*/
138 1.1 ahoka if (fd->nsize == (uint16_t)cnp->cn_namelen &&
139 1.1 ahoka memcmp(fd->name, cnp->cn_nameptr, fd->nsize) == 0) {
140 1.1 ahoka found = true;
141 1.1 ahoka break;
142 1.1 ahoka }
143 1.1 ahoka // fd = fd->next;
144 1.1 ahoka }
145 1.1 ahoka
146 1.1 ahoka return found ? fd : NULL;
147 1.1 ahoka }
148 1.1 ahoka
149 1.1 ahoka /* --------------------------------------------------------------------- */
150 1.1 ahoka
151 1.1 ahoka int
152 1.1 ahoka chfs_filldir(struct uio* uio, ino_t ino, const char *name,
153 1.4 ttoth int namelen, enum chtype type)
154 1.1 ahoka {
155 1.1 ahoka struct dirent dent;
156 1.1 ahoka int error;
157 1.1 ahoka
158 1.1 ahoka memset(&dent, 0, sizeof(dent));
159 1.1 ahoka
160 1.1 ahoka dent.d_fileno = ino;
161 1.1 ahoka switch (type) {
162 1.4 ttoth case CHT_BLK:
163 1.1 ahoka dent.d_type = DT_BLK;
164 1.1 ahoka break;
165 1.1 ahoka
166 1.4 ttoth case CHT_CHR:
167 1.1 ahoka dent.d_type = DT_CHR;
168 1.1 ahoka break;
169 1.1 ahoka
170 1.4 ttoth case CHT_DIR:
171 1.1 ahoka dent.d_type = DT_DIR;
172 1.1 ahoka break;
173 1.1 ahoka
174 1.4 ttoth case CHT_FIFO:
175 1.1 ahoka dent.d_type = DT_FIFO;
176 1.1 ahoka break;
177 1.1 ahoka
178 1.4 ttoth case CHT_LNK:
179 1.1 ahoka dent.d_type = DT_LNK;
180 1.1 ahoka break;
181 1.1 ahoka
182 1.4 ttoth case CHT_REG:
183 1.1 ahoka dent.d_type = DT_REG;
184 1.1 ahoka break;
185 1.1 ahoka
186 1.4 ttoth case CHT_SOCK:
187 1.1 ahoka dent.d_type = DT_SOCK;
188 1.1 ahoka break;
189 1.1 ahoka
190 1.1 ahoka default:
191 1.1 ahoka KASSERT(0);
192 1.1 ahoka }
193 1.1 ahoka dent.d_namlen = namelen;
194 1.1 ahoka (void)memcpy(dent.d_name, name, dent.d_namlen);
195 1.1 ahoka dent.d_reclen = _DIRENT_SIZE(&dent);
196 1.1 ahoka
197 1.1 ahoka if (dent.d_reclen > uio->uio_resid) {
198 1.1 ahoka error = -1;
199 1.1 ahoka } else {
200 1.1 ahoka error = uiomove(&dent, dent.d_reclen, uio);
201 1.1 ahoka }
202 1.1 ahoka
203 1.1 ahoka return error;
204 1.1 ahoka }
205 1.1 ahoka
206 1.1 ahoka
207 1.1 ahoka /* --------------------------------------------------------------------- */
208 1.1 ahoka
209 1.1 ahoka /*
210 1.1 ahoka * Change size of the given vnode.
211 1.1 ahoka * Caller should execute chfs_update on vp after a successful execution.
212 1.1 ahoka * The vnode must be locked on entry and remain locked on exit.
213 1.1 ahoka */
214 1.1 ahoka int
215 1.1 ahoka chfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred)
216 1.1 ahoka {
217 1.1 ahoka struct chfs_mount *chmp;
218 1.1 ahoka struct chfs_inode *ip;
219 1.1 ahoka
220 1.1 ahoka ip = VTOI(vp);
221 1.1 ahoka chmp = ip->chmp;
222 1.1 ahoka
223 1.1 ahoka dbg("chfs_chsize\n");
224 1.1 ahoka
225 1.4 ttoth switch (ip->ch_type) {
226 1.4 ttoth case CHT_DIR:
227 1.1 ahoka return EISDIR;
228 1.4 ttoth case CHT_LNK:
229 1.4 ttoth case CHT_REG:
230 1.1 ahoka if (vp->v_mount->mnt_flag & MNT_RDONLY)
231 1.1 ahoka return EROFS;
232 1.1 ahoka break;
233 1.4 ttoth case CHT_BLK:
234 1.4 ttoth case CHT_CHR:
235 1.4 ttoth case CHT_FIFO:
236 1.1 ahoka return 0;
237 1.1 ahoka default:
238 1.1 ahoka return EOPNOTSUPP; /* XXX why not ENODEV? */
239 1.1 ahoka }
240 1.1 ahoka
241 1.1 ahoka vflushbuf(vp, 0);
242 1.1 ahoka
243 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
244 1.1 ahoka
245 1.7 ttoth if (ip->size < size) {
246 1.7 ttoth uvm_vnp_setsize(vp, size);
247 1.1 ahoka chfs_set_vnode_size(vp, size);
248 1.7 ttoth ip->iflag |= IN_CHANGE | IN_UPDATE;
249 1.1 ahoka
250 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
251 1.1 ahoka return 0;
252 1.1 ahoka }
253 1.1 ahoka
254 1.7 ttoth if (size != 0) {
255 1.7 ttoth ubc_zerorange(&vp->v_uobj, size, ip->size - size, UBC_UNMAP_FLAG(vp));
256 1.1 ahoka }
257 1.7 ttoth
258 1.7 ttoth chfs_truncate_fragtree(ip->chmp, &ip->fragtree, size);
259 1.7 ttoth uvm_vnp_setsize(vp, size);
260 1.1 ahoka chfs_set_vnode_size(vp, size);
261 1.7 ttoth ip->iflag |= IN_CHANGE | IN_UPDATE;
262 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
263 1.1 ahoka return 0;
264 1.1 ahoka }
265 1.1 ahoka #if 0
266 1.1 ahoka int error;
267 1.1 ahoka struct chfs_node *node;
268 1.1 ahoka
269 1.1 ahoka KASSERT(VOP_ISLOCKED(vp));
270 1.1 ahoka
271 1.1 ahoka node = VP_TO_CHFS_NODE(vp);
272 1.1 ahoka
273 1.1 ahoka // Decide whether this is a valid operation based on the file type.
274 1.1 ahoka error = 0;
275 1.1 ahoka switch (vp->v_type) {
276 1.1 ahoka case VDIR:
277 1.1 ahoka return EISDIR;
278 1.1 ahoka
279 1.1 ahoka case VREG:
280 1.1 ahoka if (vp->v_mount->mnt_flag & MNT_RDONLY)
281 1.1 ahoka return EROFS;
282 1.1 ahoka break;
283 1.1 ahoka
284 1.1 ahoka case VBLK:
285 1.1 ahoka case VCHR:
286 1.1 ahoka case VFIFO:
287 1.1 ahoka // Allow modifications of special files even if in the file
288 1.1 ahoka // system is mounted read-only (we are not modifying the
289 1.1 ahoka // files themselves, but the objects they represent).
290 1.1 ahoka return 0;
291 1.1 ahoka
292 1.1 ahoka default:
293 1.1 ahoka return ENODEV;
294 1.1 ahoka }
295 1.1 ahoka
296 1.1 ahoka // Immutable or append-only files cannot be modified, either.
297 1.1 ahoka if (node->chn_flags & (IMMUTABLE | APPEND))
298 1.1 ahoka return EPERM;
299 1.1 ahoka
300 1.1 ahoka error = chfs_truncate(vp, size);
301 1.1 ahoka // chfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
302 1.1 ahoka // for us, as will update dn_status; no need to do that here.
303 1.1 ahoka
304 1.1 ahoka KASSERT(VOP_ISLOCKED(vp));
305 1.1 ahoka
306 1.1 ahoka return error;
307 1.1 ahoka #endif
308 1.1 ahoka
309 1.1 ahoka /* --------------------------------------------------------------------- */
310 1.1 ahoka
311 1.1 ahoka /*
312 1.1 ahoka * Change flags of the given vnode.
313 1.1 ahoka * Caller should execute chfs_update on vp after a successful execution.
314 1.1 ahoka * The vnode must be locked on entry and remain locked on exit.
315 1.1 ahoka */
316 1.1 ahoka int
317 1.1 ahoka chfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred)
318 1.1 ahoka {
319 1.1 ahoka struct chfs_mount *chmp;
320 1.1 ahoka struct chfs_inode *ip;
321 1.1 ahoka int error = 0;
322 1.3 elad kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
323 1.3 elad bool changing_sysflags = false;
324 1.1 ahoka
325 1.1 ahoka ip = VTOI(vp);
326 1.1 ahoka chmp = ip->chmp;
327 1.1 ahoka
328 1.1 ahoka if (vp->v_mount->mnt_flag & MNT_RDONLY)
329 1.1 ahoka return EROFS;
330 1.1 ahoka
331 1.3 elad if ((flags & SF_SNAPSHOT) != (ip->flags & SF_SNAPSHOT))
332 1.3 elad return EPERM;
333 1.3 elad
334 1.3 elad /* Indicate we're changing system flags if we are. */
335 1.3 elad if ((ip->flags & SF_SETTABLE) != (flags & SF_SETTABLE) ||
336 1.3 elad (flags & UF_SETTABLE) != flags) {
337 1.3 elad action |= KAUTH_VNODE_WRITE_SYSFLAGS;
338 1.3 elad changing_sysflags = true;
339 1.3 elad }
340 1.3 elad
341 1.3 elad /* Indicate the node has system flags if it does. */
342 1.3 elad if (ip->flags & (SF_IMMUTABLE | SF_APPEND)) {
343 1.3 elad action |= KAUTH_VNODE_HAS_SYSFLAGS;
344 1.3 elad }
345 1.3 elad
346 1.3 elad error = kauth_authorize_vnode(cred, action, vp, NULL,
347 1.4 ttoth genfs_can_chflags(cred, CHTTOVT(ip->ch_type), ip->uid, changing_sysflags));
348 1.3 elad if (error)
349 1.1 ahoka return error;
350 1.1 ahoka
351 1.3 elad if (changing_sysflags) {
352 1.1 ahoka ip->flags = flags;
353 1.1 ahoka } else {
354 1.1 ahoka ip->flags &= SF_SETTABLE;
355 1.1 ahoka ip->flags |= (flags & UF_SETTABLE);
356 1.1 ahoka }
357 1.1 ahoka ip->iflag |= IN_CHANGE;
358 1.1 ahoka error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
359 1.1 ahoka if (error)
360 1.1 ahoka return error;
361 1.1 ahoka
362 1.1 ahoka if (flags & (IMMUTABLE | APPEND))
363 1.1 ahoka return 0;
364 1.1 ahoka
365 1.1 ahoka return error;
366 1.1 ahoka }
367 1.1 ahoka
368 1.1 ahoka /* --------------------------------------------------------------------- */
369 1.1 ahoka
370 1.1 ahoka void
371 1.1 ahoka chfs_itimes(struct chfs_inode *ip, const struct timespec *acc,
372 1.1 ahoka const struct timespec *mod, const struct timespec *cre)
373 1.1 ahoka {
374 1.1 ahoka //dbg("itimes\n");
375 1.1 ahoka struct timespec now;
376 1.1 ahoka
377 1.1 ahoka if (!(ip->iflag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
378 1.1 ahoka return;
379 1.1 ahoka }
380 1.1 ahoka
381 1.1 ahoka vfs_timestamp(&now);
382 1.1 ahoka if (ip->iflag & IN_ACCESS) {
383 1.1 ahoka if (acc == NULL)
384 1.1 ahoka acc = &now;
385 1.1 ahoka ip->atime = acc->tv_sec;
386 1.1 ahoka }
387 1.1 ahoka if (ip->iflag & (IN_UPDATE | IN_MODIFY)) {
388 1.1 ahoka if (mod == NULL)
389 1.1 ahoka mod = &now;
390 1.1 ahoka ip->mtime = mod->tv_sec;
391 1.1 ahoka //ip->i_modrev++;
392 1.1 ahoka }
393 1.1 ahoka if (ip->iflag & (IN_CHANGE | IN_MODIFY)) {
394 1.1 ahoka if (cre == NULL)
395 1.1 ahoka cre = &now;
396 1.1 ahoka ip->ctime = cre->tv_sec;
397 1.1 ahoka }
398 1.1 ahoka if (ip->iflag & (IN_ACCESS | IN_MODIFY))
399 1.1 ahoka ip->iflag |= IN_ACCESSED;
400 1.1 ahoka if (ip->iflag & (IN_UPDATE | IN_CHANGE))
401 1.1 ahoka ip->iflag |= IN_MODIFIED;
402 1.1 ahoka ip->iflag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
403 1.1 ahoka }
404 1.1 ahoka
405 1.1 ahoka /* --------------------------------------------------------------------- */
406 1.1 ahoka
407 1.1 ahoka int
408 1.1 ahoka chfs_update(struct vnode *vp, const struct timespec *acc,
409 1.1 ahoka const struct timespec *mod, int flags)
410 1.1 ahoka {
411 1.1 ahoka
412 1.1 ahoka struct chfs_inode *ip;
413 1.1 ahoka
414 1.1 ahoka /* XXX ufs_reclaim calls this function unlocked! */
415 1.1 ahoka // KASSERT(VOP_ISLOCKED(vp));
416 1.1 ahoka
417 1.1 ahoka #if 0
418 1.1 ahoka if (flags & UPDATE_CLOSE)
419 1.1 ahoka ; /* XXX Need to do anything special? */
420 1.1 ahoka #endif
421 1.1 ahoka
422 1.1 ahoka ip = VTOI(vp);
423 1.1 ahoka chfs_itimes(ip, acc, mod, NULL);
424 1.1 ahoka
425 1.1 ahoka // KASSERT(VOP_ISLOCKED(vp));
426 1.1 ahoka return (0);
427 1.1 ahoka }
428 1.1 ahoka
429 1.1 ahoka /* --------------------------------------------------------------------- */
430 1.1 ahoka /*
431 1.1 ahoka int
432 1.1 ahoka chfs_truncate(struct vnode *vp, off_t length)
433 1.1 ahoka {
434 1.1 ahoka bool extended;
435 1.1 ahoka int error;
436 1.1 ahoka struct chfs_node *node;
437 1.1 ahoka printf("CHFS: truncate()\n");
438 1.1 ahoka
439 1.1 ahoka node = VP_TO_CHFS_NODE(vp);
440 1.1 ahoka extended = length > node->chn_size;
441 1.1 ahoka
442 1.1 ahoka if (length < 0) {
443 1.1 ahoka error = EINVAL;
444 1.1 ahoka goto out;
445 1.1 ahoka }
446 1.1 ahoka
447 1.1 ahoka if (node->chn_size == length) {
448 1.1 ahoka error = 0;
449 1.1 ahoka goto out;
450 1.1 ahoka }
451 1.1 ahoka
452 1.1 ahoka error = chfs_reg_resize(vp, length);
453 1.1 ahoka if (error == 0)
454 1.1 ahoka node->chn_status |= CHFS_NODE_CHANGED | CHFS_NODE_MODIFIED;
455 1.1 ahoka
456 1.1 ahoka out:
457 1.1 ahoka chfs_update(vp, NULL, NULL, 0);
458 1.1 ahoka
459 1.1 ahoka return error;
460 1.1 ahoka }*/
461 1.1 ahoka
462 1.1 ahoka
463