lfs_vfsops.c revision 1.25 1 1.25 wrstuden /* $NetBSD: lfs_vfsops.c,v 1.25 1999/02/26 23:44:49 wrstuden Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1989, 1991, 1993, 1994
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by the University of
18 1.1 mycroft * California, Berkeley and its contributors.
19 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
20 1.1 mycroft * may be used to endorse or promote products derived from this software
21 1.1 mycroft * without specific prior written permission.
22 1.1 mycroft *
23 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mycroft * SUCH DAMAGE.
34 1.1 mycroft *
35 1.16 fvdl * @(#)lfs_vfsops.c 8.20 (Berkeley) 6/10/95
36 1.1 mycroft */
37 1.19 scottr
38 1.20 scottr #if defined(_KERNEL) && !defined(_LKM)
39 1.19 scottr #include "opt_quota.h"
40 1.20 scottr #endif
41 1.1 mycroft
42 1.1 mycroft #include <sys/param.h>
43 1.1 mycroft #include <sys/systm.h>
44 1.1 mycroft #include <sys/namei.h>
45 1.1 mycroft #include <sys/proc.h>
46 1.1 mycroft #include <sys/kernel.h>
47 1.1 mycroft #include <sys/vnode.h>
48 1.1 mycroft #include <sys/mount.h>
49 1.1 mycroft #include <sys/buf.h>
50 1.1 mycroft #include <sys/mbuf.h>
51 1.1 mycroft #include <sys/file.h>
52 1.1 mycroft #include <sys/disklabel.h>
53 1.1 mycroft #include <sys/ioctl.h>
54 1.1 mycroft #include <sys/errno.h>
55 1.1 mycroft #include <sys/malloc.h>
56 1.23 thorpej #include <sys/pool.h>
57 1.1 mycroft #include <sys/socket.h>
58 1.1 mycroft
59 1.1 mycroft #include <miscfs/specfs/specdev.h>
60 1.1 mycroft
61 1.1 mycroft #include <ufs/ufs/quota.h>
62 1.1 mycroft #include <ufs/ufs/inode.h>
63 1.1 mycroft #include <ufs/ufs/ufsmount.h>
64 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
65 1.1 mycroft
66 1.1 mycroft #include <ufs/lfs/lfs.h>
67 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
68 1.1 mycroft
69 1.1 mycroft int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
70 1.1 mycroft
71 1.15 thorpej extern struct vnodeopv_desc lfs_vnodeop_opv_desc;
72 1.15 thorpej extern struct vnodeopv_desc lfs_specop_opv_desc;
73 1.15 thorpej extern struct vnodeopv_desc lfs_fifoop_opv_desc;
74 1.15 thorpej
75 1.15 thorpej struct vnodeopv_desc *lfs_vnodeopv_descs[] = {
76 1.15 thorpej &lfs_vnodeop_opv_desc,
77 1.15 thorpej &lfs_specop_opv_desc,
78 1.15 thorpej &lfs_fifoop_opv_desc,
79 1.15 thorpej NULL,
80 1.15 thorpej };
81 1.15 thorpej
82 1.1 mycroft struct vfsops lfs_vfsops = {
83 1.1 mycroft MOUNT_LFS,
84 1.1 mycroft lfs_mount,
85 1.1 mycroft ufs_start,
86 1.1 mycroft lfs_unmount,
87 1.1 mycroft ufs_root,
88 1.1 mycroft ufs_quotactl,
89 1.1 mycroft lfs_statfs,
90 1.1 mycroft lfs_sync,
91 1.1 mycroft lfs_vget,
92 1.1 mycroft lfs_fhtovp,
93 1.1 mycroft lfs_vptofh,
94 1.1 mycroft lfs_init,
95 1.16 fvdl lfs_sysctl,
96 1.17 fvdl NULL,
97 1.25 wrstuden ufs_check_export,
98 1.15 thorpej lfs_vnodeopv_descs,
99 1.1 mycroft };
100 1.1 mycroft
101 1.23 thorpej struct pool lfs_inode_pool;
102 1.23 thorpej
103 1.16 fvdl /*
104 1.16 fvdl * Initialize the filesystem, most work done by ufs_init.
105 1.16 fvdl */
106 1.16 fvdl void
107 1.16 fvdl lfs_init()
108 1.16 fvdl {
109 1.16 fvdl ufs_init();
110 1.23 thorpej
111 1.23 thorpej /*
112 1.23 thorpej * XXX Same structure as FFS inodes? Should we share a common pool?
113 1.23 thorpej */
114 1.23 thorpej pool_init(&lfs_inode_pool, sizeof(struct inode), 0, 0, 0,
115 1.23 thorpej "lfsinopl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
116 1.23 thorpej M_LFSNODE);
117 1.16 fvdl }
118 1.16 fvdl
119 1.16 fvdl /*
120 1.16 fvdl * Called by main() when ufs is going to be mounted as root.
121 1.16 fvdl */
122 1.1 mycroft int
123 1.1 mycroft lfs_mountroot()
124 1.1 mycroft {
125 1.16 fvdl extern struct vnode *rootvp;
126 1.16 fvdl struct mount *mp;
127 1.16 fvdl struct proc *p = curproc; /* XXX */
128 1.16 fvdl int error;
129 1.16 fvdl
130 1.16 fvdl /*
131 1.16 fvdl * Get vnodes for swapdev and rootdev.
132 1.16 fvdl */
133 1.16 fvdl if ((error = bdevvp(rootdev, &rootvp))) {
134 1.16 fvdl printf("lfs_mountroot: can't setup bdevvp's");
135 1.16 fvdl return (error);
136 1.16 fvdl }
137 1.16 fvdl if ((error = vfs_rootmountalloc(MOUNT_LFS, "root_device", &mp)))
138 1.16 fvdl return (error);
139 1.16 fvdl if ((error = lfs_mountfs(rootvp, mp, p))) {
140 1.16 fvdl mp->mnt_op->vfs_refcount--;
141 1.16 fvdl vfs_unbusy(mp);
142 1.16 fvdl free(mp, M_MOUNT);
143 1.16 fvdl return (error);
144 1.16 fvdl }
145 1.16 fvdl simple_lock(&mountlist_slock);
146 1.16 fvdl CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
147 1.16 fvdl simple_unlock(&mountlist_slock);
148 1.16 fvdl (void)lfs_statfs(mp, &mp->mnt_stat, p);
149 1.16 fvdl vfs_unbusy(mp);
150 1.16 fvdl return (0);
151 1.1 mycroft }
152 1.1 mycroft
153 1.1 mycroft /*
154 1.1 mycroft * VFS Operations.
155 1.1 mycroft *
156 1.1 mycroft * mount system call
157 1.1 mycroft */
158 1.10 christos int
159 1.1 mycroft lfs_mount(mp, path, data, ndp, p)
160 1.1 mycroft register struct mount *mp;
161 1.12 cgd const char *path;
162 1.12 cgd void *data;
163 1.1 mycroft struct nameidata *ndp;
164 1.1 mycroft struct proc *p;
165 1.1 mycroft {
166 1.1 mycroft struct vnode *devvp;
167 1.1 mycroft struct ufs_args args;
168 1.10 christos struct ufsmount *ump = NULL;
169 1.10 christos register struct lfs *fs = NULL; /* LFS */
170 1.8 mycroft size_t size;
171 1.1 mycroft int error;
172 1.3 mycroft mode_t accessmode;
173 1.1 mycroft
174 1.10 christos error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
175 1.10 christos if (error)
176 1.1 mycroft return (error);
177 1.1 mycroft
178 1.1 mycroft /* Until LFS can do NFS right. XXX */
179 1.1 mycroft if (args.export.ex_flags & MNT_EXPORTED)
180 1.1 mycroft return (EINVAL);
181 1.1 mycroft
182 1.1 mycroft /*
183 1.1 mycroft * If updating, check whether changing from read-only to
184 1.1 mycroft * read/write; if there is no device name, that's all we do.
185 1.1 mycroft */
186 1.1 mycroft if (mp->mnt_flag & MNT_UPDATE) {
187 1.1 mycroft ump = VFSTOUFS(mp);
188 1.3 mycroft if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
189 1.3 mycroft /*
190 1.3 mycroft * If upgrade to read-write by non-root, then verify
191 1.3 mycroft * that user has necessary permissions on the device.
192 1.3 mycroft */
193 1.3 mycroft if (p->p_ucred->cr_uid != 0) {
194 1.16 fvdl vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
195 1.10 christos error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
196 1.10 christos p->p_ucred, p);
197 1.16 fvdl VOP_UNLOCK(ump->um_devvp, 0);
198 1.16 fvdl if (error)
199 1.3 mycroft return (error);
200 1.3 mycroft }
201 1.1 mycroft fs->lfs_ronly = 0;
202 1.3 mycroft }
203 1.1 mycroft if (args.fspec == 0) {
204 1.1 mycroft /*
205 1.1 mycroft * Process export requests.
206 1.1 mycroft */
207 1.1 mycroft return (vfs_export(mp, &ump->um_export, &args.export));
208 1.1 mycroft }
209 1.1 mycroft }
210 1.1 mycroft /*
211 1.1 mycroft * Not an update, or updating the name: look up the name
212 1.1 mycroft * and verify that it refers to a sensible block device.
213 1.1 mycroft */
214 1.1 mycroft NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
215 1.10 christos if ((error = namei(ndp)) != 0)
216 1.1 mycroft return (error);
217 1.1 mycroft devvp = ndp->ni_vp;
218 1.1 mycroft if (devvp->v_type != VBLK) {
219 1.1 mycroft vrele(devvp);
220 1.1 mycroft return (ENOTBLK);
221 1.1 mycroft }
222 1.1 mycroft if (major(devvp->v_rdev) >= nblkdev) {
223 1.1 mycroft vrele(devvp);
224 1.1 mycroft return (ENXIO);
225 1.1 mycroft }
226 1.3 mycroft /*
227 1.3 mycroft * If mount by non-root, then verify that user has necessary
228 1.3 mycroft * permissions on the device.
229 1.3 mycroft */
230 1.3 mycroft if (p->p_ucred->cr_uid != 0) {
231 1.3 mycroft accessmode = VREAD;
232 1.3 mycroft if ((mp->mnt_flag & MNT_RDONLY) == 0)
233 1.3 mycroft accessmode |= VWRITE;
234 1.16 fvdl vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
235 1.10 christos error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
236 1.10 christos if (error) {
237 1.3 mycroft vput(devvp);
238 1.3 mycroft return (error);
239 1.3 mycroft }
240 1.16 fvdl VOP_UNLOCK(devvp, 0);
241 1.3 mycroft }
242 1.1 mycroft if ((mp->mnt_flag & MNT_UPDATE) == 0)
243 1.1 mycroft error = lfs_mountfs(devvp, mp, p); /* LFS */
244 1.1 mycroft else {
245 1.1 mycroft if (devvp != ump->um_devvp)
246 1.1 mycroft error = EINVAL; /* needs translation */
247 1.1 mycroft else
248 1.1 mycroft vrele(devvp);
249 1.1 mycroft }
250 1.1 mycroft if (error) {
251 1.1 mycroft vrele(devvp);
252 1.1 mycroft return (error);
253 1.1 mycroft }
254 1.1 mycroft ump = VFSTOUFS(mp);
255 1.1 mycroft fs = ump->um_lfs; /* LFS */
256 1.1 mycroft #ifdef NOTLFS /* LFS */
257 1.1 mycroft (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
258 1.1 mycroft bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
259 1.6 mycroft bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
260 1.1 mycroft #else
261 1.1 mycroft (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
262 1.1 mycroft bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
263 1.6 mycroft bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
264 1.4 mycroft #endif
265 1.1 mycroft (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
266 1.1 mycroft &size);
267 1.1 mycroft bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
268 1.1 mycroft return (0);
269 1.1 mycroft }
270 1.1 mycroft
271 1.1 mycroft /*
272 1.1 mycroft * Common code for mount and mountroot
273 1.1 mycroft * LFS specific
274 1.1 mycroft */
275 1.1 mycroft int
276 1.1 mycroft lfs_mountfs(devvp, mp, p)
277 1.1 mycroft register struct vnode *devvp;
278 1.1 mycroft struct mount *mp;
279 1.1 mycroft struct proc *p;
280 1.1 mycroft {
281 1.1 mycroft extern struct vnode *rootvp;
282 1.24 pk struct dlfs *dfs;
283 1.1 mycroft register struct lfs *fs;
284 1.1 mycroft register struct ufsmount *ump;
285 1.1 mycroft struct vnode *vp;
286 1.1 mycroft struct buf *bp;
287 1.1 mycroft struct partinfo dpart;
288 1.1 mycroft dev_t dev;
289 1.1 mycroft int error, i, ronly, size;
290 1.3 mycroft struct ucred *cred;
291 1.1 mycroft
292 1.3 mycroft cred = p ? p->p_ucred : NOCRED;
293 1.1 mycroft /*
294 1.1 mycroft * Disallow multiple mounts of the same device.
295 1.1 mycroft * Disallow mounting of a device that is currently in use
296 1.1 mycroft * (except for root, which might share swap device for miniroot).
297 1.1 mycroft * Flush out any old buffers remaining from a previous use.
298 1.1 mycroft */
299 1.10 christos if ((error = vfs_mountedon(devvp)) != 0)
300 1.1 mycroft return (error);
301 1.1 mycroft if (vcount(devvp) > 1 && devvp != rootvp)
302 1.1 mycroft return (EBUSY);
303 1.10 christos if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
304 1.1 mycroft return (error);
305 1.1 mycroft
306 1.1 mycroft ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
307 1.10 christos error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
308 1.10 christos if (error)
309 1.1 mycroft return (error);
310 1.1 mycroft
311 1.3 mycroft if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
312 1.1 mycroft size = DEV_BSIZE;
313 1.1 mycroft else {
314 1.1 mycroft size = dpart.disklab->d_secsize;
315 1.1 mycroft #ifdef NEVER_USED
316 1.1 mycroft dpart.part->p_fstype = FS_LFS;
317 1.1 mycroft dpart.part->p_fsize = fs->lfs_fsize; /* frag size */
318 1.1 mycroft dpart.part->p_frag = fs->lfs_frag; /* frags per block */
319 1.1 mycroft dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */
320 1.1 mycroft #endif
321 1.1 mycroft }
322 1.1 mycroft
323 1.1 mycroft /* Don't free random space on error. */
324 1.1 mycroft bp = NULL;
325 1.1 mycroft ump = NULL;
326 1.1 mycroft
327 1.1 mycroft /* Read in the superblock. */
328 1.10 christos error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
329 1.10 christos if (error)
330 1.1 mycroft goto out;
331 1.24 pk
332 1.24 pk dfs = (struct dlfs *)bp->b_data;
333 1.1 mycroft
334 1.1 mycroft /* Check the basics. */
335 1.24 pk if (dfs->dlfs_magic != LFS_MAGIC || dfs->dlfs_bsize > MAXBSIZE ||
336 1.24 pk dfs->dlfs_bsize < sizeof(struct dlfs)) {
337 1.1 mycroft error = EINVAL; /* XXX needs translation */
338 1.1 mycroft goto out;
339 1.1 mycroft }
340 1.1 mycroft
341 1.1 mycroft /* Allocate the mount structure, copy the superblock into it. */
342 1.1 mycroft ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
343 1.1 mycroft fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
344 1.24 pk bcopy(bp->b_data, &(fs->lfs_dlfs), sizeof(struct dlfs));
345 1.1 mycroft if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
346 1.1 mycroft bp->b_flags |= B_INVAL;
347 1.1 mycroft brelse(bp);
348 1.1 mycroft bp = NULL;
349 1.1 mycroft
350 1.1 mycroft /* Set up the I/O information */
351 1.1 mycroft fs->lfs_iocount = 0;
352 1.1 mycroft
353 1.1 mycroft /* Set up the ifile and lock aflags */
354 1.1 mycroft fs->lfs_doifile = 0;
355 1.1 mycroft fs->lfs_writer = 0;
356 1.1 mycroft fs->lfs_dirops = 0;
357 1.1 mycroft fs->lfs_seglock = 0;
358 1.1 mycroft
359 1.1 mycroft /* Set the file system readonly/modify bits. */
360 1.1 mycroft fs->lfs_ronly = ronly;
361 1.1 mycroft if (ronly == 0)
362 1.1 mycroft fs->lfs_fmod = 1;
363 1.1 mycroft
364 1.1 mycroft /* Initialize the mount structure. */
365 1.1 mycroft dev = devvp->v_rdev;
366 1.1 mycroft mp->mnt_data = (qaddr_t)ump;
367 1.1 mycroft mp->mnt_stat.f_fsid.val[0] = (long)dev;
368 1.1 mycroft mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
369 1.1 mycroft mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
370 1.1 mycroft mp->mnt_flag |= MNT_LOCAL;
371 1.18 bouyer ump->um_flags = 0;
372 1.1 mycroft ump->um_mountp = mp;
373 1.1 mycroft ump->um_dev = dev;
374 1.1 mycroft ump->um_devvp = devvp;
375 1.1 mycroft ump->um_bptrtodb = 0;
376 1.1 mycroft ump->um_seqinc = 1 << fs->lfs_fsbtodb;
377 1.1 mycroft ump->um_nindir = fs->lfs_nindir;
378 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
379 1.1 mycroft ump->um_quotas[i] = NULLVP;
380 1.1 mycroft devvp->v_specflags |= SI_MOUNTEDON;
381 1.1 mycroft
382 1.1 mycroft /*
383 1.1 mycroft * We use the ifile vnode for almost every operation. Instead of
384 1.1 mycroft * retrieving it from the hash table each time we retrieve it here,
385 1.1 mycroft * artificially increment the reference count and keep a pointer
386 1.1 mycroft * to it in the incore copy of the superblock.
387 1.1 mycroft */
388 1.10 christos if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
389 1.1 mycroft goto out;
390 1.1 mycroft fs->lfs_ivnode = vp;
391 1.1 mycroft VREF(vp);
392 1.1 mycroft vput(vp);
393 1.1 mycroft
394 1.1 mycroft return (0);
395 1.1 mycroft out:
396 1.1 mycroft if (bp)
397 1.1 mycroft brelse(bp);
398 1.3 mycroft (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
399 1.1 mycroft if (ump) {
400 1.1 mycroft free(ump->um_lfs, M_UFSMNT);
401 1.1 mycroft free(ump, M_UFSMNT);
402 1.1 mycroft mp->mnt_data = (qaddr_t)0;
403 1.1 mycroft }
404 1.1 mycroft return (error);
405 1.1 mycroft }
406 1.1 mycroft
407 1.1 mycroft /*
408 1.1 mycroft * unmount system call
409 1.1 mycroft */
410 1.10 christos int
411 1.1 mycroft lfs_unmount(mp, mntflags, p)
412 1.1 mycroft struct mount *mp;
413 1.1 mycroft int mntflags;
414 1.1 mycroft struct proc *p;
415 1.1 mycroft {
416 1.1 mycroft register struct ufsmount *ump;
417 1.1 mycroft register struct lfs *fs;
418 1.11 pk int error, flags, ronly;
419 1.1 mycroft
420 1.1 mycroft flags = 0;
421 1.5 mycroft if (mntflags & MNT_FORCE)
422 1.1 mycroft flags |= FORCECLOSE;
423 1.1 mycroft
424 1.1 mycroft ump = VFSTOUFS(mp);
425 1.1 mycroft fs = ump->um_lfs;
426 1.1 mycroft #ifdef QUOTA
427 1.1 mycroft if (mp->mnt_flag & MNT_QUOTA) {
428 1.11 pk int i;
429 1.10 christos error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
430 1.10 christos if (error)
431 1.1 mycroft return (error);
432 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
433 1.1 mycroft if (ump->um_quotas[i] == NULLVP)
434 1.1 mycroft continue;
435 1.1 mycroft quotaoff(p, mp, i);
436 1.1 mycroft }
437 1.1 mycroft /*
438 1.1 mycroft * Here we fall through to vflush again to ensure
439 1.1 mycroft * that we have gotten rid of all the system vnodes.
440 1.1 mycroft */
441 1.1 mycroft }
442 1.1 mycroft #endif
443 1.10 christos if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
444 1.1 mycroft return (error);
445 1.1 mycroft fs->lfs_clean = 1;
446 1.10 christos if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
447 1.1 mycroft return (error);
448 1.1 mycroft if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
449 1.1 mycroft panic("lfs_unmount: still dirty blocks on ifile vnode\n");
450 1.1 mycroft vrele(fs->lfs_ivnode);
451 1.1 mycroft vgone(fs->lfs_ivnode);
452 1.1 mycroft
453 1.1 mycroft ronly = !fs->lfs_ronly;
454 1.1 mycroft ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
455 1.1 mycroft error = VOP_CLOSE(ump->um_devvp,
456 1.1 mycroft ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
457 1.1 mycroft vrele(ump->um_devvp);
458 1.1 mycroft free(fs, M_UFSMNT);
459 1.1 mycroft free(ump, M_UFSMNT);
460 1.1 mycroft mp->mnt_data = (qaddr_t)0;
461 1.1 mycroft mp->mnt_flag &= ~MNT_LOCAL;
462 1.1 mycroft return (error);
463 1.1 mycroft }
464 1.1 mycroft
465 1.1 mycroft /*
466 1.1 mycroft * Get file system statistics.
467 1.1 mycroft */
468 1.10 christos int
469 1.1 mycroft lfs_statfs(mp, sbp, p)
470 1.1 mycroft struct mount *mp;
471 1.1 mycroft register struct statfs *sbp;
472 1.1 mycroft struct proc *p;
473 1.1 mycroft {
474 1.1 mycroft register struct lfs *fs;
475 1.1 mycroft register struct ufsmount *ump;
476 1.1 mycroft
477 1.1 mycroft ump = VFSTOUFS(mp);
478 1.1 mycroft fs = ump->um_lfs;
479 1.1 mycroft if (fs->lfs_magic != LFS_MAGIC)
480 1.1 mycroft panic("lfs_statfs: magic");
481 1.1 mycroft sbp->f_type = 0;
482 1.1 mycroft sbp->f_bsize = fs->lfs_bsize;
483 1.1 mycroft sbp->f_iosize = fs->lfs_bsize;
484 1.16 fvdl sbp->f_blocks = dbtofrags(fs, fs->lfs_dsize);
485 1.16 fvdl sbp->f_bfree = dbtofrags(fs, fs->lfs_bfree);
486 1.16 fvdl /*
487 1.16 fvdl * To compute the available space. Subtract the minimum free
488 1.16 fvdl * from the total number of blocks in the file system. Set avail
489 1.16 fvdl * to the smaller of this number and fs->lfs_bfree.
490 1.16 fvdl */
491 1.16 fvdl sbp->f_bavail = (long) ((u_int64_t) fs->lfs_dsize * (u_int64_t)
492 1.16 fvdl (100 - fs->lfs_minfree) / (u_int64_t) 100);
493 1.16 fvdl sbp->f_bavail =
494 1.16 fvdl sbp->f_bavail > fs->lfs_bfree ? fs->lfs_bfree : sbp->f_bavail;
495 1.16 fvdl sbp->f_bavail = dbtofrags(fs, sbp->f_bavail);
496 1.1 mycroft sbp->f_files = fs->lfs_nfiles;
497 1.1 mycroft sbp->f_ffree = sbp->f_bfree * INOPB(fs);
498 1.1 mycroft if (sbp != &mp->mnt_stat) {
499 1.6 mycroft bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
500 1.6 mycroft bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
501 1.1 mycroft }
502 1.6 mycroft strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
503 1.1 mycroft return (0);
504 1.1 mycroft }
505 1.1 mycroft
506 1.1 mycroft /*
507 1.1 mycroft * Go through the disk queues to initiate sandbagged IO;
508 1.1 mycroft * go through the inodes to write those that have been modified;
509 1.1 mycroft * initiate the writing of the super block if it has been modified.
510 1.1 mycroft *
511 1.1 mycroft * Note: we are always called with the filesystem marked `MPBUSY'.
512 1.1 mycroft */
513 1.10 christos int
514 1.1 mycroft lfs_sync(mp, waitfor, cred, p)
515 1.1 mycroft struct mount *mp;
516 1.1 mycroft int waitfor;
517 1.1 mycroft struct ucred *cred;
518 1.1 mycroft struct proc *p;
519 1.1 mycroft {
520 1.1 mycroft int error;
521 1.1 mycroft
522 1.1 mycroft /* All syncs must be checkpoints until roll-forward is implemented. */
523 1.1 mycroft error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
524 1.1 mycroft #ifdef QUOTA
525 1.1 mycroft qsync(mp);
526 1.1 mycroft #endif
527 1.1 mycroft return (error);
528 1.1 mycroft }
529 1.1 mycroft
530 1.1 mycroft /*
531 1.1 mycroft * Look up an LFS dinode number to find its incore vnode. If not already
532 1.1 mycroft * in core, read it in from the specified device. Return the inode locked.
533 1.1 mycroft * Detection and handling of mount points must be done by the calling routine.
534 1.1 mycroft */
535 1.1 mycroft int
536 1.1 mycroft lfs_vget(mp, ino, vpp)
537 1.1 mycroft struct mount *mp;
538 1.1 mycroft ino_t ino;
539 1.1 mycroft struct vnode **vpp;
540 1.1 mycroft {
541 1.1 mycroft register struct lfs *fs;
542 1.1 mycroft register struct inode *ip;
543 1.1 mycroft struct buf *bp;
544 1.1 mycroft struct ifile *ifp;
545 1.1 mycroft struct vnode *vp;
546 1.1 mycroft struct ufsmount *ump;
547 1.16 fvdl ufs_daddr_t daddr;
548 1.1 mycroft dev_t dev;
549 1.1 mycroft int error;
550 1.1 mycroft
551 1.1 mycroft ump = VFSTOUFS(mp);
552 1.1 mycroft dev = ump->um_dev;
553 1.1 mycroft if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
554 1.1 mycroft return (0);
555 1.1 mycroft
556 1.1 mycroft /* Translate the inode number to a disk address. */
557 1.1 mycroft fs = ump->um_lfs;
558 1.1 mycroft if (ino == LFS_IFILE_INUM)
559 1.1 mycroft daddr = fs->lfs_idaddr;
560 1.1 mycroft else {
561 1.1 mycroft LFS_IENTRY(ifp, fs, ino, bp);
562 1.1 mycroft daddr = ifp->if_daddr;
563 1.1 mycroft brelse(bp);
564 1.1 mycroft if (daddr == LFS_UNUSED_DADDR)
565 1.1 mycroft return (ENOENT);
566 1.1 mycroft }
567 1.1 mycroft
568 1.1 mycroft /* Allocate new vnode/inode. */
569 1.10 christos if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
570 1.1 mycroft *vpp = NULL;
571 1.1 mycroft return (error);
572 1.1 mycroft }
573 1.1 mycroft
574 1.1 mycroft /*
575 1.1 mycroft * Put it onto its hash chain and lock it so that other requests for
576 1.1 mycroft * this inode will block if they arrive while we are sleeping waiting
577 1.1 mycroft * for old data structures to be purged or for the contents of the
578 1.1 mycroft * disk portion of this inode to be read.
579 1.1 mycroft */
580 1.1 mycroft ip = VTOI(vp);
581 1.1 mycroft ufs_ihashins(ip);
582 1.1 mycroft
583 1.1 mycroft /*
584 1.1 mycroft * XXX
585 1.1 mycroft * This may not need to be here, logically it should go down with
586 1.1 mycroft * the i_devvp initialization.
587 1.1 mycroft * Ask Kirk.
588 1.1 mycroft */
589 1.1 mycroft ip->i_lfs = ump->um_lfs;
590 1.1 mycroft
591 1.1 mycroft /* Read in the disk contents for the inode, copy into the inode. */
592 1.10 christos error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
593 1.10 christos if (error) {
594 1.1 mycroft /*
595 1.1 mycroft * The inode does not contain anything useful, so it would
596 1.1 mycroft * be misleading to leave it on its hash chain. With mode
597 1.1 mycroft * still zero, it will be unlinked and returned to the free
598 1.1 mycroft * list by vput().
599 1.1 mycroft */
600 1.1 mycroft vput(vp);
601 1.1 mycroft brelse(bp);
602 1.1 mycroft *vpp = NULL;
603 1.1 mycroft return (error);
604 1.1 mycroft }
605 1.13 bouyer ip->i_din.ffs_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
606 1.1 mycroft brelse(bp);
607 1.1 mycroft
608 1.1 mycroft /*
609 1.1 mycroft * Initialize the vnode from the inode, check for aliases. In all
610 1.1 mycroft * cases re-init ip, the underlying vnode/inode may have changed.
611 1.1 mycroft */
612 1.22 sommerfe error = ufs_vinit(mp, lfs_specop_p, lfs_fifoop_p, &vp);
613 1.10 christos if (error) {
614 1.1 mycroft vput(vp);
615 1.1 mycroft *vpp = NULL;
616 1.1 mycroft return (error);
617 1.1 mycroft }
618 1.1 mycroft /*
619 1.1 mycroft * Finish inode initialization now that aliasing has been resolved.
620 1.1 mycroft */
621 1.1 mycroft ip->i_devvp = ump->um_devvp;
622 1.1 mycroft VREF(ip->i_devvp);
623 1.1 mycroft *vpp = vp;
624 1.1 mycroft return (0);
625 1.1 mycroft }
626 1.1 mycroft
627 1.1 mycroft /*
628 1.1 mycroft * File handle to vnode
629 1.1 mycroft *
630 1.1 mycroft * Have to be really careful about stale file handles:
631 1.1 mycroft * - check that the inode number is valid
632 1.1 mycroft * - call lfs_vget() to get the locked inode
633 1.1 mycroft * - check for an unallocated inode (i_mode == 0)
634 1.1 mycroft *
635 1.1 mycroft * XXX
636 1.1 mycroft * use ifile to see if inode is allocated instead of reading off disk
637 1.1 mycroft * what is the relationship between my generational number and the NFS
638 1.1 mycroft * generational number.
639 1.1 mycroft */
640 1.1 mycroft int
641 1.25 wrstuden lfs_fhtovp(mp, fhp, vpp)
642 1.1 mycroft register struct mount *mp;
643 1.1 mycroft struct fid *fhp;
644 1.1 mycroft struct vnode **vpp;
645 1.1 mycroft {
646 1.1 mycroft register struct ufid *ufhp;
647 1.1 mycroft
648 1.1 mycroft ufhp = (struct ufid *)fhp;
649 1.1 mycroft if (ufhp->ufid_ino < ROOTINO)
650 1.1 mycroft return (ESTALE);
651 1.25 wrstuden return (ufs_fhtovp(mp, ufhp, vpp));
652 1.1 mycroft }
653 1.1 mycroft
654 1.1 mycroft /*
655 1.1 mycroft * Vnode pointer to File handle
656 1.1 mycroft */
657 1.1 mycroft /* ARGSUSED */
658 1.10 christos int
659 1.1 mycroft lfs_vptofh(vp, fhp)
660 1.1 mycroft struct vnode *vp;
661 1.1 mycroft struct fid *fhp;
662 1.1 mycroft {
663 1.1 mycroft register struct inode *ip;
664 1.1 mycroft register struct ufid *ufhp;
665 1.1 mycroft
666 1.1 mycroft ip = VTOI(vp);
667 1.1 mycroft ufhp = (struct ufid *)fhp;
668 1.1 mycroft ufhp->ufid_len = sizeof(struct ufid);
669 1.1 mycroft ufhp->ufid_ino = ip->i_number;
670 1.13 bouyer ufhp->ufid_gen = ip->i_ffs_gen;
671 1.1 mycroft return (0);
672 1.16 fvdl }
673 1.16 fvdl
674 1.16 fvdl int
675 1.16 fvdl lfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
676 1.16 fvdl int *name;
677 1.16 fvdl u_int namelen;
678 1.16 fvdl void *oldp;
679 1.16 fvdl size_t *oldlenp;
680 1.16 fvdl void *newp;
681 1.16 fvdl size_t newlen;
682 1.16 fvdl struct proc *p;
683 1.16 fvdl {
684 1.16 fvdl return (EOPNOTSUPP);
685 1.1 mycroft }
686