1 /*	$OpenBSD: ext2fs_vfsops.c,v 1.40 2005/11/06 00:24:17 pedro Exp $	*/
2 /*	$NetBSD: ext2fs_vfsops.c,v 1.1 1997/06/11 09:34:07 bouyer Exp $	*/
3 
4 /*
5  * Copyright (c) 1997 Manuel Bouyer.
6  * Copyright (c) 1989, 1991, 1993, 1994
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *	notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *	notice, this list of conditions and the following disclaimer in the
16  *	documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *	may be used to endorse or promote products derived from this software
19  *	without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)ffs_vfsops.c	8.14 (Berkeley) 11/28/94
34  * Modified for ext2fs by Manuel Bouyer.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/namei.h>
40 #include <sys/proc.h>
41 #include <sys/kernel.h>
42 #include <sys/vnode.h>
43 #include <sys/socket.h>
44 #include <sys/mount.h>
45 #include <sys/buf.h>
46 #include <sys/device.h>
47 #include <sys/mbuf.h>
48 #include <sys/file.h>
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53 #include <sys/pool.h>
54 #include <sys/lock.h>
55 
56 #include <miscfs/specfs/specdev.h>
57 
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/ufsmount.h>
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/dir.h>
62 #include <ufs/ufs/ufs_extern.h>
63 
64 #include <ufs/ext2fs/ext2fs.h>
65 #include <ufs/ext2fs/ext2fs_extern.h>
66 
67 extern struct lock ufs_hashlock;
68 
69 int ext2fs_sbupdate(struct ufsmount *, int);
70 static int ext2fs_checksb(struct ext2fs *, int);
71 
72 extern struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
73 extern struct vnodeopv_desc ext2fs_specop_opv_desc;
74 #ifdef FIFO
75 extern struct vnodeopv_desc ext2fs_fifoop_opv_desc;
76 #endif
77 
78 struct vnodeopv_desc *ext2fs_vnodeopv_descs[] = {
79 	&ext2fs_vnodeop_opv_desc,
80 	&ext2fs_specop_opv_desc,
81 #ifdef FIFO
82 	&ext2fs_fifoop_opv_desc,
83 #endif
84 	NULL,
85 };
86 
87 const struct vfsops ext2fs_vfsops = {
88 	ext2fs_mount,
89 	ufs_start,
90 	ext2fs_unmount,
91 	ufs_root,
92 	ufs_quotactl,
93 	ext2fs_statfs,
94 	ext2fs_sync,
95 	ext2fs_vget,
96 	ext2fs_fhtovp,
97 	ext2fs_vptofh,
98 	ext2fs_init,
99 	ext2fs_sysctl,
100 	ufs_check_export
101 };
102 
103 /* struct pool ext2fs_inode_pool; */
104 
105 extern u_long ext2gennumber;
106 
107 int
ext2fs_init(vfsp)108 ext2fs_init(vfsp)
109 	struct vfsconf *vfsp;
110 {
111 	return (ufs_init(vfsp));
112 }
113 
114 /*
115  * Called by main() when ext2fs is going to be mounted as root.
116  *
117  * Name is updated by mount(8) after booting.
118  */
119 #define ROOTNAME	"root_device"
120 
121 int
ext2fs_mountroot()122 ext2fs_mountroot()
123 {
124 	register struct m_ext2fs *fs;
125         struct mount *mp;
126 	struct proc *p = curproc;	/* XXX */
127 	struct ufsmount *ump;
128 	int error;
129 
130 	/*
131 	 * Get vnodes for swapdev and rootdev.
132 	 */
133 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
134 		panic("ext2fs_mountroot: can't setup bdevvps");
135 
136 	if ((error = vfs_rootmountalloc("ext2fs", "root_device", &mp)) != 0) {
137 		vrele(rootvp);
138 		return (error);
139 	}
140 
141 	if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
142 		mp->mnt_vfc->vfc_refcount--;
143 		vfs_unbusy(mp, p);
144 		free(mp, M_MOUNT);
145 		vrele(rootvp);
146 		return (error);
147 	}
148 	simple_lock(&mountlist_slock);
149 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
150 	simple_unlock(&mountlist_slock);
151 	ump = VFSTOUFS(mp);
152 	fs = ump->um_e2fs;
153 	bzero(fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt));
154 	(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
155 	    sizeof(fs->e2fs_fsmnt) - 1, 0);
156 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
157 		bzero(fs->e2fs.e2fs_fsmnt, sizeof(fs->e2fs.e2fs_fsmnt));
158 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
159 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
160 	}
161 	(void)ext2fs_statfs(mp, &mp->mnt_stat, p);
162 	vfs_unbusy(mp, p);
163 	inittodr(fs->e2fs.e2fs_wtime);
164 	return (0);
165 }
166 
167 /*
168  * VFS Operations.
169  *
170  * mount system call
171  */
172 int
ext2fs_mount(mp,path,data,ndp,p)173 ext2fs_mount(mp, path, data, ndp, p)
174 	register struct mount *mp;
175 	const char *path;
176 	void *data;
177 	struct nameidata *ndp;
178 	struct proc *p;
179 {
180 	struct vnode *devvp;
181 	struct ufs_args args;
182 	struct ufsmount *ump = NULL;
183 	register struct m_ext2fs *fs;
184 	size_t size;
185 	int error, flags;
186 	mode_t accessmode;
187 
188 	error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
189 	if (error)
190 		return (error);
191 	/*
192 	 * If updating, check whether changing from read-only to
193 	 * read/write; if there is no device name, that's all we do.
194 	 */
195 	if (mp->mnt_flag & MNT_UPDATE) {
196 		ump = VFSTOUFS(mp);
197 		fs = ump->um_e2fs;
198 		if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
199 			flags = WRITECLOSE;
200 			if (mp->mnt_flag & MNT_FORCE)
201 				flags |= FORCECLOSE;
202 			error = ext2fs_flushfiles(mp, flags, p);
203 			if (error == 0 &&
204 				ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
205 				(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
206 				fs->e2fs.e2fs_state = E2FS_ISCLEAN;
207 				(void) ext2fs_sbupdate(ump, MNT_WAIT);
208 			}
209 			if (error)
210 				return (error);
211 			fs->e2fs_ronly = 1;
212 		}
213 		if (mp->mnt_flag & MNT_RELOAD) {
214 			error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
215 			if (error)
216 				return (error);
217 		}
218 		if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
219 			/*
220 			 * If upgrade to read-write by non-root, then verify
221 			 * that user has necessary permissions on the device.
222 			 */
223 			if (p->p_ucred->cr_uid != 0) {
224 				devvp = ump->um_devvp;
225 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
226 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
227 						   p->p_ucred, p);
228 				if (error) {
229 					VOP_UNLOCK(devvp, 0, p);
230 					return (error);
231 				}
232 				VOP_UNLOCK(devvp, 0, p);
233 			}
234 			fs->e2fs_ronly = 0;
235 			if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
236 				fs->e2fs.e2fs_state = 0;
237 			else
238 				fs->e2fs.e2fs_state = E2FS_ERRORS;
239 			fs->e2fs_fmod = 1;
240 		}
241 		if (args.fspec == 0) {
242 			/*
243 			 * Process export requests.
244 			 */
245 			return (vfs_export(mp, &ump->um_export,
246 			    &args.export_info));
247 		}
248 	}
249 	/*
250 	 * Not an update, or updating the name: look up the name
251 	 * and verify that it refers to a sensible block device.
252 	 */
253 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
254 	if ((error = namei(ndp)) != 0)
255 		return (error);
256 	devvp = ndp->ni_vp;
257 
258 	if (devvp->v_type != VBLK) {
259 		vrele(devvp);
260 		return (ENOTBLK);
261 	}
262 	if (major(devvp->v_rdev) >= nblkdev) {
263 		vrele(devvp);
264 		return (ENXIO);
265 	}
266 	/*
267 	 * If mount by non-root, then verify that user has necessary
268 	 * permissions on the device.
269 	 */
270 	if (p->p_ucred->cr_uid != 0) {
271 		accessmode = VREAD;
272 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
273 			accessmode |= VWRITE;
274 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
275 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
276 		if (error) {
277 			vput(devvp);
278 			return (error);
279 		}
280 		VOP_UNLOCK(devvp, 0, p);
281 	}
282 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
283 		error = ext2fs_mountfs(devvp, mp, p);
284 	else {
285 		if (devvp != ump->um_devvp)
286 			error = EINVAL;	/* XXX needs translation */
287 		else
288 			vrele(devvp);
289 	}
290 	if (error) {
291 		vrele(devvp);
292 		return (error);
293 	}
294 	ump = VFSTOUFS(mp);
295 	fs = ump->um_e2fs;
296 	(void) copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1,
297 	    &size);
298 	bzero(fs->e2fs_fsmnt + size, sizeof(fs->e2fs_fsmnt) - size);
299 	if (fs->e2fs.e2fs_rev > E2FS_REV0) {
300 		(void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
301 		    sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
302 		bzero(fs->e2fs.e2fs_fsmnt, sizeof(fs->e2fs.e2fs_fsmnt) - size);
303 	}
304 	bcopy(fs->e2fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
305 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
306 		&size);
307 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
308 	if (fs->e2fs_fmod != 0) {	/* XXX */
309 		fs->e2fs_fmod = 0;
310 		if (fs->e2fs.e2fs_state == 0)
311 			fs->e2fs.e2fs_wtime = time.tv_sec;
312 		else
313 			printf("%s: file system not clean; please fsck(8)\n",
314 				mp->mnt_stat.f_mntfromname);
315 		(void) ext2fs_cgupdate(ump, MNT_WAIT);
316 	}
317 	return (0);
318 }
319 
320 int ext2fs_reload_vnode(struct vnode *, void *args);
321 
322 struct ext2fs_reload_args {
323 	struct m_ext2fs *fs;
324 	struct proc *p;
325 	struct ucred *cred;
326 	struct vnode *devvp;
327 };
328 
329 int
ext2fs_reload_vnode(struct vnode * vp,void * args)330 ext2fs_reload_vnode(struct vnode *vp, void *args) {
331 	struct ext2fs_reload_args *era = args;
332 	struct buf *bp;
333 	struct inode *ip;
334 	int error;
335 	caddr_t cp;
336 
337 	/*
338 	 * Step 4: invalidate all inactive vnodes.
339 	 */
340 	if (vp->v_usecount == 0) {
341 		vgonel(vp, era->p);
342 		return (0);
343 	}
344 
345 	/*
346 	 * Step 5: invalidate all cached file data.
347 	 */
348 	if (vget(vp, LK_EXCLUSIVE  | LK_INTERLOCK, era->p))
349 		return (0);
350 
351 	if (vinvalbuf(vp, 0, era->cred, era->p, 0, 0))
352 		panic("ext2fs_reload: dirty2");
353 	/*
354 	 * Step 6: re-read inode data for all active vnodes.
355 	 */
356 	ip = VTOI(vp);
357 	error = bread(era->devvp,
358 	    fsbtodb(era->fs, ino_to_fsba(era->fs, ip->i_number)),
359 	    (int)era->fs->e2fs_bsize, NOCRED, &bp);
360 	if (error) {
361 		vput(vp);
362 		return (error);
363 	}
364 	cp = (caddr_t)bp->b_data +
365 	    (ino_to_fsbo(era->fs, ip->i_number) * EXT2_DINODE_SIZE(era->fs));
366 	e2fs_iload((struct ext2fs_dinode *)cp, &ip->i_e2din);
367 	brelse(bp);
368 	vput(vp);
369 	return (0);
370 }
371 
372 /*
373  * Reload all incore data for a filesystem (used after running fsck on
374  * the root filesystem and finding things to fix). The filesystem must
375  * be mounted read-only.
376  *
377  * Things to do to update the mount:
378  *	1) invalidate all cached meta-data.
379  *	2) re-read superblock from disk.
380  *	3) re-read summary information from disk.
381  *	4) invalidate all inactive vnodes.
382  *	5) invalidate all cached file data.
383  *	6) re-read inode data for all active vnodes.
384  */
385 int
ext2fs_reload(mountp,cred,p)386 ext2fs_reload(mountp, cred, p)
387 	register struct mount *mountp;
388 	struct ucred *cred;
389 	struct proc *p;
390 {
391 	struct vnode *devvp;
392 	struct buf *bp;
393 	struct m_ext2fs *fs;
394 	struct ext2fs *newfs;
395 	struct partinfo dpart;
396 	int i, size, error;
397 	struct ext2fs_reload_args era;
398 
399 	if ((mountp->mnt_flag & MNT_RDONLY) == 0)
400 		return (EINVAL);
401 	/*
402 	 * Step 1: invalidate all cached meta-data.
403 	 */
404 	devvp = VFSTOUFS(mountp)->um_devvp;
405 	if (vinvalbuf(devvp, 0, cred, p, 0, 0))
406 		panic("ext2fs_reload: dirty1");
407 
408 	/*
409 	 * Step 2: re-read superblock from disk.
410 	 */
411 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
412 		size = DEV_BSIZE;
413 	else
414 		size = dpart.disklab->d_secsize;
415 	error = bread(devvp, (ufs1_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
416 	if (error) {
417 		brelse(bp);
418 		return (error);
419 	}
420 	newfs = (struct ext2fs *)bp->b_data;
421 	error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
422 	if (error) {
423 		brelse(bp);
424 		return (error);
425 	}
426 
427 	fs = VFSTOUFS(mountp)->um_e2fs;
428 	/*
429 	 * copy in new superblock, and compute in-memory values
430 	 */
431 	e2fs_sbload(newfs, &fs->e2fs);
432 	fs->e2fs_ncg =
433 	    howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
434 	    fs->e2fs.e2fs_bpg);
435 	/* XXX assume hw bsize = 512 */
436 	fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
437 	fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
438 	fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
439 	fs->e2fs_qbmask = fs->e2fs_bsize - 1;
440 	fs->e2fs_bmask = ~fs->e2fs_qbmask;
441 	fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
442 			fs->e2fs_bsize / sizeof(struct ext2_gd));
443 	fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE(fs);
444 	fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
445 
446 	/*
447 	 * Step 3: re-read summary information from disk.
448 	 */
449 
450 	for (i=0; i < fs->e2fs_ngdb; i++) {
451 		error = bread(devvp ,
452 		    fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
453 		    fs->e2fs_bsize, NOCRED, &bp);
454 		if (error) {
455 			brelse(bp);
456 			return (error);
457 		}
458 		e2fs_cgload((struct ext2_gd*)bp->b_data,
459 		    &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
460 		    fs->e2fs_bsize);
461 		brelse(bp);
462 	}
463 
464 	era.p = p;
465 	era.cred = cred;
466 	era.fs = fs;
467 	era.devvp = devvp;
468 
469 	error = vfs_mount_foreach_vnode(mountp, ext2fs_reload_vnode, &era);
470 
471 	return (error);
472 }
473 
474 /*
475  * Common code for mount and mountroot
476  */
477 int
ext2fs_mountfs(devvp,mp,p)478 ext2fs_mountfs(devvp, mp, p)
479 	register struct vnode *devvp;
480 	struct mount *mp;
481 	struct proc *p;
482 {
483 	register struct ufsmount *ump;
484 	struct buf *bp;
485 	register struct ext2fs *fs;
486 	register struct m_ext2fs *m_fs;
487 	dev_t dev;
488 	struct partinfo dpart;
489 	int error, i, size, ronly;
490 	struct ucred *cred;
491 
492 	dev = devvp->v_rdev;
493 	cred = p ? p->p_ucred : NOCRED;
494 	/*
495 	 * Disallow multiple mounts of the same device.
496 	 * Disallow mounting of a device that is currently in use
497 	 * (except for root, which might share swap device for miniroot).
498 	 * Flush out any old buffers remaining from a previous use.
499 	 */
500 	if ((error = vfs_mountedon(devvp)) != 0)
501 		return (error);
502 	if (vcount(devvp) > 1 && devvp != rootvp)
503 		return (EBUSY);
504 	if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
505 		return (error);
506 
507 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
508 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
509 	if (error)
510 		return (error);
511 	if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
512 		size = DEV_BSIZE;
513 	else
514 		size = dpart.disklab->d_secsize;
515 
516 	bp = NULL;
517 	ump = NULL;
518 
519 #ifdef DEBUG_EXT2
520 	printf("ext2 sb size: %d\n", sizeof(struct ext2fs));
521 #endif
522 	error = bread(devvp, (SBOFF / DEV_BSIZE), SBSIZE, cred, &bp);
523 	if (error)
524 		goto out;
525 	fs = (struct ext2fs *)bp->b_data;
526 	error = ext2fs_checksb(fs, ronly);
527 	if (error)
528 		goto out;
529 	ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
530 	memset((caddr_t)ump, 0, sizeof *ump);
531 	ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
532 	memset((caddr_t)ump->um_e2fs, 0, sizeof(struct m_ext2fs));
533 	e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
534 	brelse(bp);
535 	bp = NULL;
536 	m_fs = ump->um_e2fs;
537 	m_fs->e2fs_ronly = ronly;
538 	ump->um_fstype = UM_EXT2FS;
539 
540 #ifdef DEBUG_EXT2
541 	printf("ext2 ino size %d\n", EXT2_DINODE_SIZE(m_fs));
542 #endif
543 	if (ronly == 0) {
544 		if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
545 			m_fs->e2fs.e2fs_state = 0;
546 		else
547 			m_fs->e2fs.e2fs_state = E2FS_ERRORS;
548 		m_fs->e2fs_fmod = 1;
549 	}
550 
551 	/* compute dynamic sb infos */
552 	m_fs->e2fs_ncg =
553 		howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
554 		m_fs->e2fs.e2fs_bpg);
555 	/* XXX assume hw bsize = 512 */
556 	m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
557 	m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
558 	m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
559 	m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
560 	m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
561 	m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
562 		m_fs->e2fs_bsize / sizeof(struct ext2_gd));
563 	m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
564 	m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
565 
566 	m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
567 		M_UFSMNT, M_WAITOK);
568 	for (i=0; i < m_fs->e2fs_ngdb; i++) {
569 		error = bread(devvp ,
570 		    fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
571 		    m_fs->e2fs_bsize, NOCRED, &bp);
572 		if (error) {
573 			free(m_fs->e2fs_gd, M_UFSMNT);
574 			goto out;
575 		}
576 		e2fs_cgload((struct ext2_gd*)bp->b_data,
577 		    &m_fs->e2fs_gd[
578 			i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
579 		    m_fs->e2fs_bsize);
580 		brelse(bp);
581 		bp = NULL;
582 	}
583 
584 	mp->mnt_data = (qaddr_t)ump;
585 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
586 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
587 	mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
588 	mp->mnt_flag |= MNT_LOCAL;
589 	ump->um_mountp = mp;
590 	ump->um_dev = dev;
591 	ump->um_devvp = devvp;
592 	ump->um_nindir = NINDIR(m_fs);
593 	ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
594 	ump->um_seqinc = 1; /* no frags */
595 	devvp->v_specmountpoint = mp;
596 	return (0);
597 out:
598 	if (bp)
599 		brelse(bp);
600 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
601 	(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
602 	VOP_UNLOCK(devvp, 0, p);
603 	if (ump) {
604 		free(ump->um_e2fs, M_UFSMNT);
605 		free(ump, M_UFSMNT);
606 		mp->mnt_data = (qaddr_t)0;
607 	}
608 	return (error);
609 }
610 
611 /*
612  * unmount system call
613  */
614 int
ext2fs_unmount(mp,mntflags,p)615 ext2fs_unmount(mp, mntflags, p)
616 	struct mount *mp;
617 	int mntflags;
618 	struct proc *p;
619 {
620 	register struct ufsmount *ump;
621 	register struct m_ext2fs *fs;
622 	int error, flags;
623 
624 	flags = 0;
625 	if (mntflags & MNT_FORCE)
626 		flags |= FORCECLOSE;
627 	if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
628 		return (error);
629 	ump = VFSTOUFS(mp);
630 	fs = ump->um_e2fs;
631 	if (fs->e2fs_ronly == 0 &&
632 		ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
633 		(fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
634 		fs->e2fs.e2fs_state = E2FS_ISCLEAN;
635 		(void) ext2fs_sbupdate(ump, MNT_WAIT);
636 	}
637 
638 	if (ump->um_devvp->v_type != VBAD)
639 		ump->um_devvp->v_specmountpoint = NULL;
640 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
641 	error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
642 		NOCRED, p);
643 	vput(ump->um_devvp);
644 	free(fs->e2fs_gd, M_UFSMNT);
645 	free(fs, M_UFSMNT);
646 	free(ump, M_UFSMNT);
647 	mp->mnt_data = (qaddr_t)0;
648 	mp->mnt_flag &= ~MNT_LOCAL;
649 	return (error);
650 }
651 
652 /*
653  * Flush out all the files in a filesystem.
654  */
655 int
ext2fs_flushfiles(mp,flags,p)656 ext2fs_flushfiles(mp, flags, p)
657 	register struct mount *mp;
658 	int flags;
659 	struct proc *p;
660 {
661 	register struct ufsmount *ump;
662 	int error;
663 
664 	ump = VFSTOUFS(mp);
665 	/*
666 	 * Flush all the files.
667 	 */
668 	if ((error = vflush(mp, NULL, flags)) != 0)
669 		return (error);
670 	/*
671 	 * Flush filesystem metadata.
672 	 */
673 	vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
674 	error = VOP_FSYNC(ump->um_devvp, p->p_ucred, MNT_WAIT, p);
675 	VOP_UNLOCK(ump->um_devvp, 0, p);
676 	return (error);
677 }
678 
679 /*
680  * Get file system statistics.
681  */
682 int
ext2fs_statfs(mp,sbp,p)683 ext2fs_statfs(mp, sbp, p)
684 	struct mount *mp;
685 	register struct statfs *sbp;
686 	struct proc *p;
687 {
688 	register struct ufsmount *ump;
689 	register struct m_ext2fs *fs;
690 	u_int32_t overhead, overhead_per_group;
691 	int i, ngroups;
692 
693 	ump = VFSTOUFS(mp);
694 	fs = ump->um_e2fs;
695 	if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
696 		panic("ext2fs_statfs");
697 
698 	/*
699 	 * Compute the overhead (FS structures)
700 	 */
701 	overhead_per_group = 1 /* block bitmap */ +
702 				 1 /* inode bitmap */ +
703 				 fs->e2fs_itpg;
704 	overhead = fs->e2fs.e2fs_first_dblock +
705 		   fs->e2fs_ncg * overhead_per_group;
706 	if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
707 	    fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
708 		for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
709 			if (cg_has_sb(i))
710 				ngroups++;
711 		}
712 	} else {
713 		ngroups = fs->e2fs_ncg;
714 	}
715 	overhead += ngroups * (1 + fs->e2fs_ngdb);
716 
717 	sbp->f_bsize = fs->e2fs_bsize;
718 	sbp->f_iosize = fs->e2fs_bsize;
719 	sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
720 	sbp->f_bfree = fs->e2fs.e2fs_fbcount;
721 	sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
722 	sbp->f_files =  fs->e2fs.e2fs_icount;
723 	sbp->f_ffree = fs->e2fs.e2fs_ficount;
724 	if (sbp != &mp->mnt_stat) {
725 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
726 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
727 	}
728 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
729 	return (0);
730 }
731 
732 int ext2fs_sync_vnode(struct vnode *vp, void *);
733 
734 struct ext2fs_sync_args {
735 	int allerror;
736 	int waitfor;
737 	struct proc *p;
738 	struct ucred *cred;
739 };
740 
741 int
ext2fs_sync_vnode(struct vnode * vp,void * args)742 ext2fs_sync_vnode(struct vnode *vp, void *args)
743 {
744 	struct ext2fs_sync_args *esa = args;
745 	struct inode *ip;
746 	int error;
747 
748 	ip = VTOI(vp);
749 	if (vp->v_type == VNON ||
750 	    ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
751 		LIST_EMPTY(&vp->v_dirtyblkhd)) ||
752 	    esa->waitfor == MNT_LAZY) {
753 		simple_unlock(&vp->v_interlock);
754 		return (0);
755 	}
756 
757 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, esa->p))
758 		return (0);
759 
760 	if ((error = VOP_FSYNC(vp, esa->cred, esa->waitfor, esa->p)) != 0)
761 		esa->allerror = error;
762 	vput(vp);
763 	return (0);
764 }
765 /*
766  * Go through the disk queues to initiate sandbagged IO;
767  * go through the inodes to write those that have been modified;
768  * initiate the writing of the super block if it has been modified.
769  *
770  * Should always be called with the mount point locked.
771  */
772 int
ext2fs_sync(mp,waitfor,cred,p)773 ext2fs_sync(mp, waitfor, cred, p)
774 	struct mount *mp;
775 	int waitfor;
776 	struct ucred *cred;
777 	struct proc *p;
778 {
779 	struct ufsmount *ump = VFSTOUFS(mp);
780 	struct m_ext2fs *fs;
781 	int error, allerror = 0;
782 	struct ext2fs_sync_args esa;
783 
784 	fs = ump->um_e2fs;
785 	if (fs->e2fs_ronly != 0) {		/* XXX */
786 		printf("fs = %s\n", fs->e2fs_fsmnt);
787 		panic("update: rofs mod");
788 	}
789 
790 	/*
791 	 * Write back each (modified) inode.
792 	 */
793 	esa.p = p;
794 	esa.cred = cred;
795 	esa.allerror = 0;
796 	esa.waitfor = waitfor;
797 
798 	vfs_mount_foreach_vnode(mp, ext2fs_sync_vnode, &esa);
799 	if (esa.allerror != 0)
800 		allerror = esa.allerror;
801 
802 	/*
803 	 * Force stale file system control information to be flushed.
804 	 */
805 	if (waitfor != MNT_LAZY) {
806 		vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY, p);
807 		if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
808 			allerror = error;
809 		VOP_UNLOCK(ump->um_devvp, 0, p);
810 	}
811 	/*
812 	 * Write back modified superblock.
813 	 */
814 	if (fs->e2fs_fmod != 0) {
815 		fs->e2fs_fmod = 0;
816 		fs->e2fs.e2fs_wtime = time.tv_sec;
817 		if ((error = ext2fs_cgupdate(ump, waitfor)))
818 			allerror = error;
819 	}
820 	return (allerror);
821 }
822 
823 /*
824  * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
825  * in from disk.  If it is in core, wait for the lock bit to clear, then
826  * return the inode locked.  Detection and handling of mount points must be
827  * done by the calling routine.
828  */
829 int
ext2fs_vget(mp,ino,vpp)830 ext2fs_vget(mp, ino, vpp)
831 	struct mount *mp;
832 	ino_t ino;
833 	struct vnode **vpp;
834 {
835 	register struct m_ext2fs *fs;
836 	register struct inode *ip;
837 	struct ufsmount *ump;
838 	struct buf *bp;
839 	struct vnode *vp;
840 	dev_t dev;
841 	int error;
842 
843 	ump = VFSTOUFS(mp);
844 	dev = ump->um_dev;
845 
846  retry:
847 	if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
848 		return (0);
849 
850 	/* Allocate a new vnode/inode. */
851 	if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
852 		*vpp = NULL;
853 		return (error);
854 	}
855 	MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2FSNODE, M_WAITOK);
856 	bzero((caddr_t)ip, sizeof(struct inode));
857 	lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
858 	vp->v_data = ip;
859 	ip->i_vnode = vp;
860 	ip->i_ump = ump;
861 	ip->i_e2fs = fs = ump->um_e2fs;
862 	ip->i_dev = dev;
863 	ip->i_number = ino;
864 	ip->i_e2fs_last_lblk = 0;
865 	ip->i_e2fs_last_blk = 0;
866 
867 	/*
868 	 * Put it onto its hash chain and lock it so that other requests for
869 	 * this inode will block if they arrive while we are sleeping waiting
870 	 * for old data structures to be purged or for the contents of the
871 	 * disk portion of this inode to be read.
872 	 */
873 	error = ufs_ihashins(ip);
874 
875 	if (error) {
876 		vrele(vp);
877 
878 		if (error == EEXIST)
879 			goto retry;
880 
881 		return (error);
882 	}
883 
884 	/* Read in the disk contents for the inode, copy into the inode. */
885 	error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
886 			  (int)fs->e2fs_bsize, NOCRED, &bp);
887 	if (error) {
888 		/*
889 		 * The inode does not contain anything useful, so it would
890 	 	 * be misleading to leave it on its hash chain. With mode
891 		 * still zero, it will be unlinked and returned to the free
892 		 * list by vput().
893 		 */
894 		vput(vp);
895 		brelse(bp);
896 		*vpp = NULL;
897 		return (error);
898 	}
899 	bcopy(((struct ext2fs_dinode *)((char *)bp->b_data +
900 	    EXT2_DINODE_SIZE(fs) * ino_to_fsbo(fs, ino))),
901 	    &ip->i_e2din, sizeof(struct ext2fs_dinode));
902 	ip->i_effnlink = ip->i_e2fs_nlink;
903 
904 	/*
905 	 * The fields for storing the UID and GID of an ext2fs inode are
906 	 * limited to 16 bits. To overcome this limitation, Linux decided to
907 	 * scatter the highest bits of these values into a previously reserved
908 	 * area on the disk inode. We deal with this situation by having two
909 	 * 32-bit fields *out* of the disk inode to hold the complete values.
910 	 * Now that we are reading in the inode, compute these fields.
911 	 */
912 	ip->i_e2fs_uid = ip->i_e2fs_uid_low | (ip->i_e2fs_uid_high << 16);
913 	ip->i_e2fs_gid = ip->i_e2fs_gid_low | (ip->i_e2fs_gid_high << 16);
914 
915 	brelse(bp);
916 
917 	/* If the inode was deleted, reset all fields */
918 	if (ip->i_e2fs_dtime != 0) {
919 		ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
920 		(void)ext2fs_setsize(ip, 0);
921 	}
922 
923 	/*
924 	 * Initialize the vnode from the inode, check for aliases.
925 	 * Note that the underlying vnode may have changed.
926 	 */
927 	error = ufs_vinit(mp, ext2fs_specop_p, EXT2FS_FIFOOPS, &vp);
928 	if (error) {
929 		vput(vp);
930 		*vpp = NULL;
931 		return (error);
932 	}
933 	/*
934 	 * Finish inode initialization now that aliasing has been resolved.
935 	 */
936 	VREF(ip->i_devvp);
937 	/*
938 	 * Set up a generation number for this inode if it does not
939 	 * already have one. This should only happen on old filesystems.
940 	 */
941 	if (ip->i_e2fs_gen == 0) {
942 		if (++ext2gennumber < (u_long)time.tv_sec)
943 			ext2gennumber = time.tv_sec;
944 		ip->i_e2fs_gen = ext2gennumber;
945 		if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
946 			ip->i_flag |= IN_MODIFIED;
947 	}
948 
949 	*vpp = vp;
950 	return (0);
951 }
952 
953 /*
954  * File handle to vnode
955  *
956  * Have to be really careful about stale file handles:
957  * - check that the inode number is valid
958  * - call ext2fs_vget() to get the locked inode
959  * - check for an unallocated inode (i_mode == 0)
960  * - check that the given client host has export rights and return
961  *   those rights via. exflagsp and credanonp
962  */
963 int
ext2fs_fhtovp(mp,fhp,vpp)964 ext2fs_fhtovp(mp, fhp, vpp)
965 	register struct mount *mp;
966 	struct fid *fhp;
967 	struct vnode **vpp;
968 {
969 	register struct inode *ip;
970 	struct vnode *nvp;
971 	int error;
972 	register struct ufid *ufhp;
973 	struct m_ext2fs *fs;
974 
975 	ufhp = (struct ufid *)fhp;
976 	fs = VFSTOUFS(mp)->um_e2fs;
977 	if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
978 		ufhp->ufid_ino > fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
979 		return (ESTALE);
980 
981 	if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
982 		*vpp = NULLVP;
983 		return (error);
984 	}
985 	ip = VTOI(nvp);
986 	if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
987 		ip->i_e2fs_gen != ufhp->ufid_gen) {
988 		vput(nvp);
989 		*vpp = NULLVP;
990 		return (ESTALE);
991 	}
992 	*vpp = nvp;
993 	return (0);
994 }
995 
996 /*
997  * Vnode pointer to File handle
998  */
999 /* ARGSUSED */
1000 int
ext2fs_vptofh(vp,fhp)1001 ext2fs_vptofh(vp, fhp)
1002 	struct vnode *vp;
1003 	struct fid *fhp;
1004 {
1005 	register struct inode *ip;
1006 	register struct ufid *ufhp;
1007 
1008 	ip = VTOI(vp);
1009 	ufhp = (struct ufid *)fhp;
1010 	ufhp->ufid_len = sizeof(struct ufid);
1011 	ufhp->ufid_ino = ip->i_number;
1012 	ufhp->ufid_gen = ip->i_e2fs_gen;
1013 	return (0);
1014 }
1015 
1016 /*
1017  * no sysctl for ext2fs
1018  */
1019 
1020 int
ext2fs_sysctl(name,namelen,oldp,oldlenp,newp,newlen,p)1021 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1022 	int *name;
1023 	u_int namelen;
1024 	void *oldp;
1025 	size_t *oldlenp;
1026 	void *newp;
1027 	size_t newlen;
1028 	struct proc *p;
1029 {
1030 	return (EOPNOTSUPP);
1031 }
1032 
1033 /*
1034  * Write a superblock and associated information back to disk.
1035  */
1036 int
ext2fs_sbupdate(mp,waitfor)1037 ext2fs_sbupdate(mp, waitfor)
1038 	struct ufsmount *mp;
1039 	int waitfor;
1040 {
1041 	register struct m_ext2fs *fs = mp->um_e2fs;
1042 	register struct buf *bp;
1043 	int error = 0;
1044 
1045 	bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1046 	e2fs_sbsave(&fs->e2fs, (struct ext2fs *) bp->b_data);
1047 	if (waitfor == MNT_WAIT)
1048 		error = bwrite(bp);
1049 	else
1050 		bawrite(bp);
1051 	return (error);
1052 }
1053 
1054 int
ext2fs_cgupdate(mp,waitfor)1055 ext2fs_cgupdate(mp, waitfor)
1056 	struct ufsmount *mp;
1057 	int waitfor;
1058 {
1059 	register struct m_ext2fs *fs = mp->um_e2fs;
1060 	register struct buf *bp;
1061 	int i, error = 0, allerror = 0;
1062 
1063 	allerror = ext2fs_sbupdate(mp, waitfor);
1064 	for (i = 0; i < fs->e2fs_ngdb; i++) {
1065 		bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1066 		    fs->e2fs_bsize, 0, 0);
1067 		e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)], (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1068 		if (waitfor == MNT_WAIT)
1069 			error = bwrite(bp);
1070 		else
1071 			bawrite(bp);
1072 	}
1073 
1074 	if (!allerror && error)
1075 		allerror = error;
1076 	return (allerror);
1077 }
1078 
1079 static int
ext2fs_checksb(fs,ronly)1080 ext2fs_checksb(fs, ronly)
1081 	struct ext2fs *fs;
1082 	int ronly;
1083 {
1084 	if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1085 		return (EIO);		/* XXX needs translation */
1086 	}
1087 	if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
1088 #ifdef DIAGNOSTIC
1089 		printf("Ext2 fs: unsupported revision number: %x\n",
1090 		    fs2h32(fs->e2fs_rev));
1091 #endif
1092 		return (EIO);		/* XXX needs translation */
1093 	}
1094 	if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
1095 #ifdef DIAGNOSTIC
1096 		printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
1097 		    fs2h32(fs->e2fs_log_bsize));
1098 #endif
1099 		return (EIO);	   /* XXX needs translation */
1100 	}
1101 	if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1102 		if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO) {
1103 			printf("Ext2 fs: unsupported first inode position");
1104 			return (EINVAL);      /* XXX needs translation */
1105 		}
1106 		if (fs2h32(fs->e2fs_features_incompat) &
1107 		    ~EXT2F_INCOMPAT_SUPP) {
1108 			printf("Ext2 fs: unsupported optional feature\n");
1109 			return (EINVAL);      /* XXX needs translation */
1110 		}
1111 		if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
1112 		    ~EXT2F_ROCOMPAT_SUPP) {
1113 			return (EROFS);      /* XXX needs translation */
1114 		}
1115 	}
1116 	return (0);
1117 }
1118