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