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