Home | History | Annotate | Line # | Download | only in chfs
chfs_subr.c revision 1.4.2.3
      1  1.4.2.3  yamt /*	$NetBSD: chfs_subr.c,v 1.4.2.3 2012/10/30 17:22:58 yamt Exp $	*/
      2  1.4.2.2  yamt 
      3  1.4.2.2  yamt /*-
      4  1.4.2.2  yamt  * Copyright (c) 2010 Department of Software Engineering,
      5  1.4.2.2  yamt  *		      University of Szeged, Hungary
      6  1.4.2.2  yamt  * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
      7  1.4.2.2  yamt  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
      8  1.4.2.2  yamt  * All rights reserved.
      9  1.4.2.2  yamt  *
     10  1.4.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
     11  1.4.2.2  yamt  * by the Department of Software Engineering, University of Szeged, Hungary
     12  1.4.2.2  yamt  *
     13  1.4.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     14  1.4.2.2  yamt  * modification, are permitted provided that the following conditions
     15  1.4.2.2  yamt  * are met:
     16  1.4.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     17  1.4.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     18  1.4.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.4.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     20  1.4.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     21  1.4.2.2  yamt  *
     22  1.4.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.4.2.2  yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.4.2.2  yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.4.2.2  yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.4.2.2  yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.4.2.2  yamt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.4.2.2  yamt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.4.2.2  yamt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.4.2.2  yamt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.4.2.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.4.2.2  yamt  * SUCH DAMAGE.
     33  1.4.2.2  yamt  */
     34  1.4.2.2  yamt 
     35  1.4.2.2  yamt #include <sys/cdefs.h>
     36  1.4.2.2  yamt 
     37  1.4.2.2  yamt #include <sys/param.h>
     38  1.4.2.2  yamt #include <sys/dirent.h>
     39  1.4.2.2  yamt #include <sys/event.h>
     40  1.4.2.2  yamt #include <sys/kmem.h>
     41  1.4.2.2  yamt #include <sys/mount.h>
     42  1.4.2.2  yamt #include <sys/namei.h>
     43  1.4.2.2  yamt #include <sys/time.h>
     44  1.4.2.2  yamt #include <sys/stat.h>
     45  1.4.2.2  yamt #include <sys/systm.h>
     46  1.4.2.2  yamt #include <sys/swap.h>
     47  1.4.2.2  yamt #include <sys/vnode.h>
     48  1.4.2.2  yamt #include <sys/kauth.h>
     49  1.4.2.2  yamt #include <sys/proc.h>
     50  1.4.2.2  yamt #include <sys/atomic.h>
     51  1.4.2.2  yamt 
     52  1.4.2.2  yamt #include <uvm/uvm.h>
     53  1.4.2.2  yamt 
     54  1.4.2.2  yamt #include <miscfs/specfs/specdev.h>
     55  1.4.2.2  yamt #include <miscfs/genfs/genfs.h>
     56  1.4.2.2  yamt #include "chfs.h"
     57  1.4.2.2  yamt 
     58  1.4.2.2  yamt 
     59  1.4.2.2  yamt /*
     60  1.4.2.3  yamt  * chfs_mem_info -
     61  1.4.2.2  yamt  * Returns information about the number of available memory pages,
     62  1.4.2.2  yamt  * including physical and virtual ones.
     63  1.4.2.2  yamt  *
     64  1.4.2.2  yamt  * If 'total' is true, the value returned is the total amount of memory
     65  1.4.2.2  yamt  * pages configured for the system (either in use or free).
     66  1.4.2.2  yamt  * If it is FALSE, the value returned is the amount of free memory pages.
     67  1.4.2.2  yamt  *
     68  1.4.2.2  yamt  * Remember to remove DUMMYFS_PAGES_RESERVED from the returned value to avoid
     69  1.4.2.2  yamt  * excessive memory usage.
     70  1.4.2.2  yamt  *
     71  1.4.2.2  yamt  */
     72  1.4.2.2  yamt size_t
     73  1.4.2.2  yamt chfs_mem_info(bool total)
     74  1.4.2.2  yamt {
     75  1.4.2.2  yamt 	size_t size;
     76  1.4.2.2  yamt 
     77  1.4.2.2  yamt 	size = 0;
     78  1.4.2.2  yamt 	size += uvmexp.swpgavail;
     79  1.4.2.2  yamt 	if (!total) {
     80  1.4.2.2  yamt 		size -= uvmexp.swpgonly;
     81  1.4.2.2  yamt 	}
     82  1.4.2.2  yamt 	size += uvmexp.free;
     83  1.4.2.2  yamt 	size += uvmexp.filepages;
     84  1.4.2.2  yamt 	if (size > uvmexp.wired) {
     85  1.4.2.2  yamt 		size -= uvmexp.wired;
     86  1.4.2.2  yamt 	} else {
     87  1.4.2.2  yamt 		size = 0;
     88  1.4.2.2  yamt 	}
     89  1.4.2.2  yamt 
     90  1.4.2.2  yamt 	return size;
     91  1.4.2.2  yamt }
     92  1.4.2.2  yamt 
     93  1.4.2.2  yamt 
     94  1.4.2.2  yamt /*
     95  1.4.2.3  yamt  * chfs_dir_lookup -
     96  1.4.2.2  yamt  * Looks for a directory entry in the directory represented by node.
     97  1.4.2.2  yamt  * 'cnp' describes the name of the entry to look for.  Note that the .
     98  1.4.2.2  yamt  * and .. components are not allowed as they do not physically exist
     99  1.4.2.2  yamt  * within directories.
    100  1.4.2.2  yamt  *
    101  1.4.2.2  yamt  * Returns a pointer to the entry when found, otherwise NULL.
    102  1.4.2.2  yamt  */
    103  1.4.2.2  yamt struct chfs_dirent *
    104  1.4.2.2  yamt chfs_dir_lookup(struct chfs_inode *ip, struct componentname *cnp)
    105  1.4.2.2  yamt {
    106  1.4.2.2  yamt 	bool found;
    107  1.4.2.2  yamt 	struct chfs_dirent *fd;
    108  1.4.2.2  yamt 	dbg("dir_lookup()\n");
    109  1.4.2.2  yamt 
    110  1.4.2.2  yamt 	KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
    111  1.4.2.2  yamt 	KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
    112  1.4.2.2  yamt 		    cnp->cn_nameptr[1] == '.')));
    113  1.4.2.2  yamt 
    114  1.4.2.2  yamt 	found = false;
    115  1.4.2.2  yamt 	TAILQ_FOREACH(fd, &ip->dents, fds) {
    116  1.4.2.2  yamt 		KASSERT(cnp->cn_namelen < 0xffff);
    117  1.4.2.2  yamt 		if (fd->vno == 0)
    118  1.4.2.2  yamt 			continue;
    119  1.4.2.2  yamt 		if (fd->nsize == (uint16_t)cnp->cn_namelen &&
    120  1.4.2.2  yamt 		    memcmp(fd->name, cnp->cn_nameptr, fd->nsize) == 0) {
    121  1.4.2.2  yamt 			found = true;
    122  1.4.2.2  yamt 			break;
    123  1.4.2.2  yamt 		}
    124  1.4.2.2  yamt 	}
    125  1.4.2.2  yamt 
    126  1.4.2.2  yamt 	return found ? fd : NULL;
    127  1.4.2.2  yamt }
    128  1.4.2.2  yamt 
    129  1.4.2.3  yamt /*
    130  1.4.2.3  yamt  * chfs_filldir -
    131  1.4.2.3  yamt  * Creates a (kernel) dirent and moves it to the given memory address.
    132  1.4.2.3  yamt  * Used during readdir.
    133  1.4.2.3  yamt  */
    134  1.4.2.2  yamt int
    135  1.4.2.2  yamt chfs_filldir(struct uio* uio, ino_t ino, const char *name,
    136  1.4.2.2  yamt     int namelen, enum chtype type)
    137  1.4.2.2  yamt {
    138  1.4.2.2  yamt 	struct dirent dent;
    139  1.4.2.2  yamt 	int error;
    140  1.4.2.2  yamt 
    141  1.4.2.2  yamt 	memset(&dent, 0, sizeof(dent));
    142  1.4.2.2  yamt 
    143  1.4.2.2  yamt 	dent.d_fileno = ino;
    144  1.4.2.2  yamt 	switch (type) {
    145  1.4.2.2  yamt 	case CHT_BLK:
    146  1.4.2.2  yamt 		dent.d_type = DT_BLK;
    147  1.4.2.2  yamt 		break;
    148  1.4.2.2  yamt 
    149  1.4.2.2  yamt 	case CHT_CHR:
    150  1.4.2.2  yamt 		dent.d_type = DT_CHR;
    151  1.4.2.2  yamt 		break;
    152  1.4.2.2  yamt 
    153  1.4.2.2  yamt 	case CHT_DIR:
    154  1.4.2.2  yamt 		dent.d_type = DT_DIR;
    155  1.4.2.2  yamt 		break;
    156  1.4.2.2  yamt 
    157  1.4.2.2  yamt 	case CHT_FIFO:
    158  1.4.2.2  yamt 		dent.d_type = DT_FIFO;
    159  1.4.2.2  yamt 		break;
    160  1.4.2.2  yamt 
    161  1.4.2.2  yamt 	case CHT_LNK:
    162  1.4.2.2  yamt 		dent.d_type = DT_LNK;
    163  1.4.2.2  yamt 		break;
    164  1.4.2.2  yamt 
    165  1.4.2.2  yamt 	case CHT_REG:
    166  1.4.2.2  yamt 		dent.d_type = DT_REG;
    167  1.4.2.2  yamt 		break;
    168  1.4.2.2  yamt 
    169  1.4.2.2  yamt 	case CHT_SOCK:
    170  1.4.2.2  yamt 		dent.d_type = DT_SOCK;
    171  1.4.2.2  yamt 		break;
    172  1.4.2.2  yamt 
    173  1.4.2.2  yamt 	default:
    174  1.4.2.2  yamt 		KASSERT(0);
    175  1.4.2.2  yamt 	}
    176  1.4.2.2  yamt 	dent.d_namlen = namelen;
    177  1.4.2.2  yamt 	(void)memcpy(dent.d_name, name, dent.d_namlen);
    178  1.4.2.2  yamt 	dent.d_reclen = _DIRENT_SIZE(&dent);
    179  1.4.2.2  yamt 
    180  1.4.2.2  yamt 	if (dent.d_reclen > uio->uio_resid) {
    181  1.4.2.2  yamt 		error = -1;
    182  1.4.2.2  yamt 	} else {
    183  1.4.2.2  yamt 		error = uiomove(&dent, dent.d_reclen, uio);
    184  1.4.2.2  yamt 	}
    185  1.4.2.2  yamt 
    186  1.4.2.2  yamt 	return error;
    187  1.4.2.2  yamt }
    188  1.4.2.2  yamt 
    189  1.4.2.2  yamt /*
    190  1.4.2.3  yamt  * chfs_chsize - change size of the given vnode
    191  1.4.2.2  yamt  * Caller should execute chfs_update on vp after a successful execution.
    192  1.4.2.2  yamt  * The vnode must be locked on entry and remain locked on exit.
    193  1.4.2.2  yamt  */
    194  1.4.2.2  yamt int
    195  1.4.2.2  yamt chfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred)
    196  1.4.2.2  yamt {
    197  1.4.2.2  yamt 	struct chfs_mount *chmp;
    198  1.4.2.2  yamt 	struct chfs_inode *ip;
    199  1.4.2.2  yamt 
    200  1.4.2.2  yamt 	ip = VTOI(vp);
    201  1.4.2.2  yamt 	chmp = ip->chmp;
    202  1.4.2.2  yamt 
    203  1.4.2.2  yamt 	dbg("chfs_chsize\n");
    204  1.4.2.2  yamt 
    205  1.4.2.2  yamt 	switch (ip->ch_type) {
    206  1.4.2.2  yamt 	case CHT_DIR:
    207  1.4.2.2  yamt 		return EISDIR;
    208  1.4.2.2  yamt 	case CHT_LNK:
    209  1.4.2.2  yamt 	case CHT_REG:
    210  1.4.2.2  yamt 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
    211  1.4.2.2  yamt 			return EROFS;
    212  1.4.2.2  yamt 		break;
    213  1.4.2.2  yamt 	case CHT_BLK:
    214  1.4.2.2  yamt 	case CHT_CHR:
    215  1.4.2.2  yamt 	case CHT_FIFO:
    216  1.4.2.2  yamt 		return 0;
    217  1.4.2.2  yamt 	default:
    218  1.4.2.2  yamt 		return EOPNOTSUPP; /* XXX why not ENODEV? */
    219  1.4.2.2  yamt 	}
    220  1.4.2.2  yamt 
    221  1.4.2.2  yamt 	vflushbuf(vp, 0);
    222  1.4.2.2  yamt 
    223  1.4.2.2  yamt 	mutex_enter(&chmp->chm_lock_mountfields);
    224  1.4.2.2  yamt 
    225  1.4.2.3  yamt 	if (ip->size < size) {
    226  1.4.2.3  yamt 		uvm_vnp_setsize(vp, size);
    227  1.4.2.2  yamt 		chfs_set_vnode_size(vp, size);
    228  1.4.2.3  yamt 		ip->iflag |= IN_CHANGE | IN_UPDATE;
    229  1.4.2.2  yamt 
    230  1.4.2.2  yamt 		mutex_exit(&chmp->chm_lock_mountfields);
    231  1.4.2.2  yamt 		return 0;
    232  1.4.2.2  yamt 	}
    233  1.4.2.2  yamt 
    234  1.4.2.3  yamt 	if (size != 0) {
    235  1.4.2.3  yamt 		ubc_zerorange(&vp->v_uobj, size, ip->size - size, UBC_UNMAP_FLAG(vp));
    236  1.4.2.2  yamt 	}
    237  1.4.2.3  yamt 
    238  1.4.2.3  yamt 	/* drop unused fragments */
    239  1.4.2.3  yamt 	chfs_truncate_fragtree(ip->chmp, &ip->fragtree, size);
    240  1.4.2.2  yamt 
    241  1.4.2.3  yamt 	uvm_vnp_setsize(vp, size);
    242  1.4.2.2  yamt 	chfs_set_vnode_size(vp, size);
    243  1.4.2.3  yamt 	ip->iflag |= IN_CHANGE | IN_UPDATE;
    244  1.4.2.2  yamt 	mutex_exit(&chmp->chm_lock_mountfields);
    245  1.4.2.2  yamt 	return 0;
    246  1.4.2.2  yamt }
    247  1.4.2.2  yamt 
    248  1.4.2.2  yamt /*
    249  1.4.2.3  yamt  * chfs_chflags - change flags of the given vnode
    250  1.4.2.2  yamt  * Caller should execute chfs_update on vp after a successful execution.
    251  1.4.2.2  yamt  * The vnode must be locked on entry and remain locked on exit.
    252  1.4.2.2  yamt  */
    253  1.4.2.2  yamt int
    254  1.4.2.2  yamt chfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred)
    255  1.4.2.2  yamt {
    256  1.4.2.2  yamt 	struct chfs_mount *chmp;
    257  1.4.2.2  yamt 	struct chfs_inode *ip;
    258  1.4.2.2  yamt 	int error = 0;
    259  1.4.2.2  yamt 	kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
    260  1.4.2.2  yamt 	bool changing_sysflags = false;
    261  1.4.2.2  yamt 
    262  1.4.2.2  yamt 	ip = VTOI(vp);
    263  1.4.2.2  yamt 	chmp = ip->chmp;
    264  1.4.2.2  yamt 
    265  1.4.2.2  yamt 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    266  1.4.2.2  yamt 		return EROFS;
    267  1.4.2.2  yamt 
    268  1.4.2.2  yamt 	if ((flags & SF_SNAPSHOT) != (ip->flags & SF_SNAPSHOT))
    269  1.4.2.2  yamt 		return EPERM;
    270  1.4.2.2  yamt 
    271  1.4.2.2  yamt 	/* Indicate we're changing system flags if we are. */
    272  1.4.2.2  yamt 	if ((ip->flags & SF_SETTABLE) != (flags & SF_SETTABLE) ||
    273  1.4.2.2  yamt 	    (flags & UF_SETTABLE) != flags) {
    274  1.4.2.2  yamt 		action |= KAUTH_VNODE_WRITE_SYSFLAGS;
    275  1.4.2.2  yamt 		changing_sysflags = true;
    276  1.4.2.2  yamt 	}
    277  1.4.2.2  yamt 
    278  1.4.2.2  yamt 	/* Indicate the node has system flags if it does. */
    279  1.4.2.2  yamt 	if (ip->flags & (SF_IMMUTABLE | SF_APPEND)) {
    280  1.4.2.2  yamt 		action |= KAUTH_VNODE_HAS_SYSFLAGS;
    281  1.4.2.2  yamt 	}
    282  1.4.2.2  yamt 
    283  1.4.2.2  yamt 	error = kauth_authorize_vnode(cred, action, vp, NULL,
    284  1.4.2.2  yamt 	    genfs_can_chflags(cred, CHTTOVT(ip->ch_type), ip->uid, changing_sysflags));
    285  1.4.2.2  yamt 	if (error)
    286  1.4.2.2  yamt 		return error;
    287  1.4.2.2  yamt 
    288  1.4.2.2  yamt 	if (changing_sysflags) {
    289  1.4.2.2  yamt 		ip->flags = flags;
    290  1.4.2.2  yamt 	} else {
    291  1.4.2.2  yamt 		ip->flags &= SF_SETTABLE;
    292  1.4.2.2  yamt 		ip->flags |= (flags & UF_SETTABLE);
    293  1.4.2.2  yamt 	}
    294  1.4.2.2  yamt 	ip->iflag |= IN_CHANGE;
    295  1.4.2.2  yamt 	error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
    296  1.4.2.2  yamt 	if (error)
    297  1.4.2.2  yamt 		return error;
    298  1.4.2.2  yamt 
    299  1.4.2.2  yamt 	if (flags & (IMMUTABLE | APPEND))
    300  1.4.2.2  yamt 		return 0;
    301  1.4.2.2  yamt 
    302  1.4.2.2  yamt 	return error;
    303  1.4.2.2  yamt }
    304  1.4.2.2  yamt 
    305  1.4.2.2  yamt 
    306  1.4.2.3  yamt /* chfs_itimes - updates a vnode times to the given data */
    307  1.4.2.2  yamt void
    308  1.4.2.2  yamt chfs_itimes(struct chfs_inode *ip, const struct timespec *acc,
    309  1.4.2.2  yamt     const struct timespec *mod, const struct timespec *cre)
    310  1.4.2.2  yamt {
    311  1.4.2.2  yamt 	struct timespec now;
    312  1.4.2.2  yamt 
    313  1.4.2.2  yamt 	if (!(ip->iflag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY))) {
    314  1.4.2.2  yamt 		return;
    315  1.4.2.2  yamt 	}
    316  1.4.2.2  yamt 
    317  1.4.2.2  yamt 	vfs_timestamp(&now);
    318  1.4.2.2  yamt 	if (ip->iflag & IN_ACCESS) {
    319  1.4.2.2  yamt 		if (acc == NULL)
    320  1.4.2.2  yamt 			acc = &now;
    321  1.4.2.2  yamt 		ip->atime = acc->tv_sec;
    322  1.4.2.2  yamt 	}
    323  1.4.2.2  yamt 	if (ip->iflag & (IN_UPDATE | IN_MODIFY)) {
    324  1.4.2.2  yamt 		if (mod == NULL)
    325  1.4.2.2  yamt 			mod = &now;
    326  1.4.2.2  yamt 		ip->mtime = mod->tv_sec;
    327  1.4.2.2  yamt 	}
    328  1.4.2.2  yamt 	if (ip->iflag & (IN_CHANGE | IN_MODIFY)) {
    329  1.4.2.2  yamt 		if (cre == NULL)
    330  1.4.2.2  yamt 			cre = &now;
    331  1.4.2.2  yamt 		ip->ctime = cre->tv_sec;
    332  1.4.2.2  yamt 	}
    333  1.4.2.2  yamt 	if (ip->iflag & (IN_ACCESS | IN_MODIFY))
    334  1.4.2.2  yamt 		ip->iflag |= IN_ACCESSED;
    335  1.4.2.2  yamt 	if (ip->iflag & (IN_UPDATE | IN_CHANGE))
    336  1.4.2.2  yamt 		ip->iflag |= IN_MODIFIED;
    337  1.4.2.2  yamt 	ip->iflag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
    338  1.4.2.2  yamt }
    339  1.4.2.2  yamt 
    340  1.4.2.3  yamt /* chfs_update - updates a vnode times */
    341  1.4.2.2  yamt int
    342  1.4.2.2  yamt chfs_update(struct vnode *vp, const struct timespec *acc,
    343  1.4.2.2  yamt     const struct timespec *mod, int flags)
    344  1.4.2.2  yamt {
    345  1.4.2.2  yamt 	struct chfs_inode *ip;
    346  1.4.2.2  yamt 
    347  1.4.2.2  yamt 	/* XXX ufs_reclaim calls this function unlocked! */
    348  1.4.2.2  yamt 
    349  1.4.2.2  yamt 	ip = VTOI(vp);
    350  1.4.2.2  yamt 	chfs_itimes(ip, acc, mod, NULL);
    351  1.4.2.2  yamt 
    352  1.4.2.2  yamt 	return (0);
    353  1.4.2.2  yamt }
    354  1.4.2.2  yamt 
    355