lfs_vfsops.c revision 1.15 1 1.15 thorpej /* $NetBSD: lfs_vfsops.c,v 1.15 1998/02/18 07:05:50 thorpej 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.3 mycroft * @(#)lfs_vfsops.c 8.10 (Berkeley) 11/21/94
36 1.1 mycroft */
37 1.1 mycroft
38 1.1 mycroft #include <sys/param.h>
39 1.1 mycroft #include <sys/systm.h>
40 1.1 mycroft #include <sys/namei.h>
41 1.1 mycroft #include <sys/proc.h>
42 1.1 mycroft #include <sys/kernel.h>
43 1.1 mycroft #include <sys/vnode.h>
44 1.1 mycroft #include <sys/mount.h>
45 1.1 mycroft #include <sys/buf.h>
46 1.1 mycroft #include <sys/mbuf.h>
47 1.1 mycroft #include <sys/file.h>
48 1.1 mycroft #include <sys/disklabel.h>
49 1.1 mycroft #include <sys/ioctl.h>
50 1.1 mycroft #include <sys/errno.h>
51 1.1 mycroft #include <sys/malloc.h>
52 1.1 mycroft #include <sys/socket.h>
53 1.1 mycroft
54 1.1 mycroft #include <miscfs/specfs/specdev.h>
55 1.1 mycroft
56 1.1 mycroft #include <ufs/ufs/quota.h>
57 1.1 mycroft #include <ufs/ufs/inode.h>
58 1.1 mycroft #include <ufs/ufs/ufsmount.h>
59 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
60 1.1 mycroft
61 1.1 mycroft #include <ufs/lfs/lfs.h>
62 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
63 1.1 mycroft
64 1.1 mycroft int lfs_mountfs __P((struct vnode *, struct mount *, struct proc *));
65 1.1 mycroft
66 1.15 thorpej extern struct vnodeopv_desc lfs_vnodeop_opv_desc;
67 1.15 thorpej extern struct vnodeopv_desc lfs_specop_opv_desc;
68 1.15 thorpej #ifdef FIFO
69 1.15 thorpej extern struct vnodeopv_desc lfs_fifoop_opv_desc;
70 1.15 thorpej #endif
71 1.15 thorpej
72 1.15 thorpej struct vnodeopv_desc *lfs_vnodeopv_descs[] = {
73 1.15 thorpej &lfs_vnodeop_opv_desc,
74 1.15 thorpej &lfs_specop_opv_desc,
75 1.15 thorpej #ifdef FIFO
76 1.15 thorpej &lfs_fifoop_opv_desc,
77 1.15 thorpej #endif
78 1.15 thorpej NULL,
79 1.15 thorpej };
80 1.15 thorpej
81 1.1 mycroft struct vfsops lfs_vfsops = {
82 1.1 mycroft MOUNT_LFS,
83 1.1 mycroft lfs_mount,
84 1.1 mycroft ufs_start,
85 1.1 mycroft lfs_unmount,
86 1.1 mycroft ufs_root,
87 1.1 mycroft ufs_quotactl,
88 1.1 mycroft lfs_statfs,
89 1.1 mycroft lfs_sync,
90 1.1 mycroft lfs_vget,
91 1.1 mycroft lfs_fhtovp,
92 1.1 mycroft lfs_vptofh,
93 1.1 mycroft lfs_init,
94 1.15 thorpej NULL, /* vfs_mountroot */
95 1.15 thorpej lfs_vnodeopv_descs,
96 1.1 mycroft };
97 1.1 mycroft
98 1.1 mycroft int
99 1.1 mycroft lfs_mountroot()
100 1.1 mycroft {
101 1.1 mycroft panic("lfs_mountroot"); /* XXX -- implement */
102 1.10 christos return 0;
103 1.1 mycroft }
104 1.1 mycroft
105 1.1 mycroft /*
106 1.1 mycroft * VFS Operations.
107 1.1 mycroft *
108 1.1 mycroft * mount system call
109 1.1 mycroft */
110 1.10 christos int
111 1.1 mycroft lfs_mount(mp, path, data, ndp, p)
112 1.1 mycroft register struct mount *mp;
113 1.12 cgd const char *path;
114 1.12 cgd void *data;
115 1.1 mycroft struct nameidata *ndp;
116 1.1 mycroft struct proc *p;
117 1.1 mycroft {
118 1.1 mycroft struct vnode *devvp;
119 1.1 mycroft struct ufs_args args;
120 1.10 christos struct ufsmount *ump = NULL;
121 1.10 christos register struct lfs *fs = NULL; /* LFS */
122 1.8 mycroft size_t size;
123 1.1 mycroft int error;
124 1.3 mycroft mode_t accessmode;
125 1.1 mycroft
126 1.10 christos error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
127 1.10 christos if (error)
128 1.1 mycroft return (error);
129 1.1 mycroft
130 1.1 mycroft /* Until LFS can do NFS right. XXX */
131 1.1 mycroft if (args.export.ex_flags & MNT_EXPORTED)
132 1.1 mycroft return (EINVAL);
133 1.1 mycroft
134 1.1 mycroft /*
135 1.1 mycroft * If updating, check whether changing from read-only to
136 1.1 mycroft * read/write; if there is no device name, that's all we do.
137 1.1 mycroft */
138 1.1 mycroft if (mp->mnt_flag & MNT_UPDATE) {
139 1.1 mycroft ump = VFSTOUFS(mp);
140 1.3 mycroft if (fs->lfs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
141 1.3 mycroft /*
142 1.3 mycroft * If upgrade to read-write by non-root, then verify
143 1.3 mycroft * that user has necessary permissions on the device.
144 1.3 mycroft */
145 1.3 mycroft if (p->p_ucred->cr_uid != 0) {
146 1.3 mycroft VOP_LOCK(ump->um_devvp);
147 1.10 christos error = VOP_ACCESS(ump->um_devvp, VREAD|VWRITE,
148 1.10 christos p->p_ucred, p);
149 1.10 christos if (error) {
150 1.3 mycroft VOP_UNLOCK(ump->um_devvp);
151 1.3 mycroft return (error);
152 1.3 mycroft }
153 1.3 mycroft VOP_UNLOCK(ump->um_devvp);
154 1.3 mycroft }
155 1.1 mycroft fs->lfs_ronly = 0;
156 1.3 mycroft }
157 1.1 mycroft if (args.fspec == 0) {
158 1.1 mycroft /*
159 1.1 mycroft * Process export requests.
160 1.1 mycroft */
161 1.1 mycroft return (vfs_export(mp, &ump->um_export, &args.export));
162 1.1 mycroft }
163 1.1 mycroft }
164 1.1 mycroft /*
165 1.1 mycroft * Not an update, or updating the name: look up the name
166 1.1 mycroft * and verify that it refers to a sensible block device.
167 1.1 mycroft */
168 1.1 mycroft NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
169 1.10 christos if ((error = namei(ndp)) != 0)
170 1.1 mycroft return (error);
171 1.1 mycroft devvp = ndp->ni_vp;
172 1.1 mycroft if (devvp->v_type != VBLK) {
173 1.1 mycroft vrele(devvp);
174 1.1 mycroft return (ENOTBLK);
175 1.1 mycroft }
176 1.1 mycroft if (major(devvp->v_rdev) >= nblkdev) {
177 1.1 mycroft vrele(devvp);
178 1.1 mycroft return (ENXIO);
179 1.1 mycroft }
180 1.3 mycroft /*
181 1.3 mycroft * If mount by non-root, then verify that user has necessary
182 1.3 mycroft * permissions on the device.
183 1.3 mycroft */
184 1.3 mycroft if (p->p_ucred->cr_uid != 0) {
185 1.3 mycroft accessmode = VREAD;
186 1.3 mycroft if ((mp->mnt_flag & MNT_RDONLY) == 0)
187 1.3 mycroft accessmode |= VWRITE;
188 1.3 mycroft VOP_LOCK(devvp);
189 1.10 christos error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
190 1.10 christos if (error) {
191 1.3 mycroft vput(devvp);
192 1.3 mycroft return (error);
193 1.3 mycroft }
194 1.3 mycroft VOP_UNLOCK(devvp);
195 1.3 mycroft }
196 1.1 mycroft if ((mp->mnt_flag & MNT_UPDATE) == 0)
197 1.1 mycroft error = lfs_mountfs(devvp, mp, p); /* LFS */
198 1.1 mycroft else {
199 1.1 mycroft if (devvp != ump->um_devvp)
200 1.1 mycroft error = EINVAL; /* needs translation */
201 1.1 mycroft else
202 1.1 mycroft vrele(devvp);
203 1.1 mycroft }
204 1.1 mycroft if (error) {
205 1.1 mycroft vrele(devvp);
206 1.1 mycroft return (error);
207 1.1 mycroft }
208 1.1 mycroft ump = VFSTOUFS(mp);
209 1.1 mycroft fs = ump->um_lfs; /* LFS */
210 1.1 mycroft #ifdef NOTLFS /* LFS */
211 1.1 mycroft (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
212 1.1 mycroft bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
213 1.6 mycroft bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
214 1.1 mycroft #else
215 1.1 mycroft (void)copyinstr(path, fs->lfs_fsmnt, sizeof(fs->lfs_fsmnt) - 1, &size);
216 1.1 mycroft bzero(fs->lfs_fsmnt + size, sizeof(fs->lfs_fsmnt) - size);
217 1.6 mycroft bcopy(fs->lfs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
218 1.4 mycroft #endif
219 1.1 mycroft (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
220 1.1 mycroft &size);
221 1.1 mycroft bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
222 1.1 mycroft return (0);
223 1.1 mycroft }
224 1.1 mycroft
225 1.1 mycroft /*
226 1.1 mycroft * Common code for mount and mountroot
227 1.1 mycroft * LFS specific
228 1.1 mycroft */
229 1.1 mycroft int
230 1.1 mycroft lfs_mountfs(devvp, mp, p)
231 1.1 mycroft register struct vnode *devvp;
232 1.1 mycroft struct mount *mp;
233 1.1 mycroft struct proc *p;
234 1.1 mycroft {
235 1.1 mycroft extern struct vnode *rootvp;
236 1.1 mycroft register struct lfs *fs;
237 1.1 mycroft register struct ufsmount *ump;
238 1.1 mycroft struct vnode *vp;
239 1.1 mycroft struct buf *bp;
240 1.1 mycroft struct partinfo dpart;
241 1.1 mycroft dev_t dev;
242 1.1 mycroft int error, i, ronly, size;
243 1.3 mycroft struct ucred *cred;
244 1.1 mycroft
245 1.3 mycroft cred = p ? p->p_ucred : NOCRED;
246 1.1 mycroft /*
247 1.1 mycroft * Disallow multiple mounts of the same device.
248 1.1 mycroft * Disallow mounting of a device that is currently in use
249 1.1 mycroft * (except for root, which might share swap device for miniroot).
250 1.1 mycroft * Flush out any old buffers remaining from a previous use.
251 1.1 mycroft */
252 1.10 christos if ((error = vfs_mountedon(devvp)) != 0)
253 1.1 mycroft return (error);
254 1.1 mycroft if (vcount(devvp) > 1 && devvp != rootvp)
255 1.1 mycroft return (EBUSY);
256 1.10 christos if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
257 1.1 mycroft return (error);
258 1.1 mycroft
259 1.1 mycroft ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
260 1.10 christos error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
261 1.10 christos if (error)
262 1.1 mycroft return (error);
263 1.1 mycroft
264 1.3 mycroft if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
265 1.1 mycroft size = DEV_BSIZE;
266 1.1 mycroft else {
267 1.1 mycroft size = dpart.disklab->d_secsize;
268 1.1 mycroft #ifdef NEVER_USED
269 1.1 mycroft dpart.part->p_fstype = FS_LFS;
270 1.1 mycroft dpart.part->p_fsize = fs->lfs_fsize; /* frag size */
271 1.1 mycroft dpart.part->p_frag = fs->lfs_frag; /* frags per block */
272 1.1 mycroft dpart.part->p_cpg = fs->lfs_segshift; /* segment shift */
273 1.1 mycroft #endif
274 1.1 mycroft }
275 1.1 mycroft
276 1.1 mycroft /* Don't free random space on error. */
277 1.1 mycroft bp = NULL;
278 1.1 mycroft ump = NULL;
279 1.1 mycroft
280 1.1 mycroft /* Read in the superblock. */
281 1.10 christos error = bread(devvp, LFS_LABELPAD / size, LFS_SBPAD, cred, &bp);
282 1.10 christos if (error)
283 1.1 mycroft goto out;
284 1.1 mycroft fs = (struct lfs *)bp->b_data;
285 1.1 mycroft
286 1.1 mycroft /* Check the basics. */
287 1.1 mycroft if (fs->lfs_magic != LFS_MAGIC || fs->lfs_bsize > MAXBSIZE ||
288 1.1 mycroft fs->lfs_bsize < sizeof(struct lfs)) {
289 1.1 mycroft error = EINVAL; /* XXX needs translation */
290 1.1 mycroft goto out;
291 1.1 mycroft }
292 1.1 mycroft
293 1.1 mycroft /* Allocate the mount structure, copy the superblock into it. */
294 1.1 mycroft ump = (struct ufsmount *)malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
295 1.1 mycroft fs = ump->um_lfs = malloc(sizeof(struct lfs), M_UFSMNT, M_WAITOK);
296 1.1 mycroft bcopy(bp->b_data, fs, sizeof(struct lfs));
297 1.1 mycroft if (sizeof(struct lfs) < LFS_SBPAD) /* XXX why? */
298 1.1 mycroft bp->b_flags |= B_INVAL;
299 1.1 mycroft brelse(bp);
300 1.1 mycroft bp = NULL;
301 1.1 mycroft
302 1.1 mycroft /* Set up the I/O information */
303 1.1 mycroft fs->lfs_iocount = 0;
304 1.1 mycroft
305 1.1 mycroft /* Set up the ifile and lock aflags */
306 1.1 mycroft fs->lfs_doifile = 0;
307 1.1 mycroft fs->lfs_writer = 0;
308 1.1 mycroft fs->lfs_dirops = 0;
309 1.1 mycroft fs->lfs_seglock = 0;
310 1.1 mycroft
311 1.1 mycroft /* Set the file system readonly/modify bits. */
312 1.1 mycroft fs->lfs_ronly = ronly;
313 1.1 mycroft if (ronly == 0)
314 1.1 mycroft fs->lfs_fmod = 1;
315 1.1 mycroft
316 1.1 mycroft /* Initialize the mount structure. */
317 1.1 mycroft dev = devvp->v_rdev;
318 1.1 mycroft mp->mnt_data = (qaddr_t)ump;
319 1.1 mycroft mp->mnt_stat.f_fsid.val[0] = (long)dev;
320 1.1 mycroft mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_LFS);
321 1.1 mycroft mp->mnt_maxsymlinklen = fs->lfs_maxsymlinklen;
322 1.1 mycroft mp->mnt_flag |= MNT_LOCAL;
323 1.1 mycroft ump->um_mountp = mp;
324 1.1 mycroft ump->um_dev = dev;
325 1.1 mycroft ump->um_devvp = devvp;
326 1.1 mycroft ump->um_bptrtodb = 0;
327 1.1 mycroft ump->um_seqinc = 1 << fs->lfs_fsbtodb;
328 1.1 mycroft ump->um_nindir = fs->lfs_nindir;
329 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
330 1.1 mycroft ump->um_quotas[i] = NULLVP;
331 1.1 mycroft devvp->v_specflags |= SI_MOUNTEDON;
332 1.1 mycroft
333 1.1 mycroft /*
334 1.1 mycroft * We use the ifile vnode for almost every operation. Instead of
335 1.1 mycroft * retrieving it from the hash table each time we retrieve it here,
336 1.1 mycroft * artificially increment the reference count and keep a pointer
337 1.1 mycroft * to it in the incore copy of the superblock.
338 1.1 mycroft */
339 1.10 christos if ((error = VFS_VGET(mp, LFS_IFILE_INUM, &vp)) != 0)
340 1.1 mycroft goto out;
341 1.1 mycroft fs->lfs_ivnode = vp;
342 1.1 mycroft VREF(vp);
343 1.1 mycroft vput(vp);
344 1.1 mycroft
345 1.1 mycroft return (0);
346 1.1 mycroft out:
347 1.1 mycroft if (bp)
348 1.1 mycroft brelse(bp);
349 1.3 mycroft (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
350 1.1 mycroft if (ump) {
351 1.1 mycroft free(ump->um_lfs, M_UFSMNT);
352 1.1 mycroft free(ump, M_UFSMNT);
353 1.1 mycroft mp->mnt_data = (qaddr_t)0;
354 1.1 mycroft }
355 1.1 mycroft return (error);
356 1.1 mycroft }
357 1.1 mycroft
358 1.1 mycroft /*
359 1.1 mycroft * unmount system call
360 1.1 mycroft */
361 1.10 christos int
362 1.1 mycroft lfs_unmount(mp, mntflags, p)
363 1.1 mycroft struct mount *mp;
364 1.1 mycroft int mntflags;
365 1.1 mycroft struct proc *p;
366 1.1 mycroft {
367 1.1 mycroft register struct ufsmount *ump;
368 1.1 mycroft register struct lfs *fs;
369 1.11 pk int error, flags, ronly;
370 1.1 mycroft
371 1.1 mycroft flags = 0;
372 1.5 mycroft if (mntflags & MNT_FORCE)
373 1.1 mycroft flags |= FORCECLOSE;
374 1.1 mycroft
375 1.1 mycroft ump = VFSTOUFS(mp);
376 1.1 mycroft fs = ump->um_lfs;
377 1.1 mycroft #ifdef QUOTA
378 1.1 mycroft if (mp->mnt_flag & MNT_QUOTA) {
379 1.11 pk int i;
380 1.10 christos error = vflush(mp, fs->lfs_ivnode, SKIPSYSTEM|flags);
381 1.10 christos if (error)
382 1.1 mycroft return (error);
383 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
384 1.1 mycroft if (ump->um_quotas[i] == NULLVP)
385 1.1 mycroft continue;
386 1.1 mycroft quotaoff(p, mp, i);
387 1.1 mycroft }
388 1.1 mycroft /*
389 1.1 mycroft * Here we fall through to vflush again to ensure
390 1.1 mycroft * that we have gotten rid of all the system vnodes.
391 1.1 mycroft */
392 1.1 mycroft }
393 1.1 mycroft #endif
394 1.10 christos if ((error = vflush(mp, fs->lfs_ivnode, flags)) != 0)
395 1.1 mycroft return (error);
396 1.1 mycroft fs->lfs_clean = 1;
397 1.10 christos if ((error = VFS_SYNC(mp, 1, p->p_ucred, p)) != 0)
398 1.1 mycroft return (error);
399 1.1 mycroft if (fs->lfs_ivnode->v_dirtyblkhd.lh_first)
400 1.1 mycroft panic("lfs_unmount: still dirty blocks on ifile vnode\n");
401 1.1 mycroft vrele(fs->lfs_ivnode);
402 1.1 mycroft vgone(fs->lfs_ivnode);
403 1.1 mycroft
404 1.1 mycroft ronly = !fs->lfs_ronly;
405 1.1 mycroft ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
406 1.1 mycroft error = VOP_CLOSE(ump->um_devvp,
407 1.1 mycroft ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
408 1.1 mycroft vrele(ump->um_devvp);
409 1.1 mycroft free(fs, M_UFSMNT);
410 1.1 mycroft free(ump, M_UFSMNT);
411 1.1 mycroft mp->mnt_data = (qaddr_t)0;
412 1.1 mycroft mp->mnt_flag &= ~MNT_LOCAL;
413 1.1 mycroft return (error);
414 1.1 mycroft }
415 1.1 mycroft
416 1.1 mycroft /*
417 1.1 mycroft * Get file system statistics.
418 1.1 mycroft */
419 1.10 christos int
420 1.1 mycroft lfs_statfs(mp, sbp, p)
421 1.1 mycroft struct mount *mp;
422 1.1 mycroft register struct statfs *sbp;
423 1.1 mycroft struct proc *p;
424 1.1 mycroft {
425 1.1 mycroft register struct lfs *fs;
426 1.1 mycroft register struct ufsmount *ump;
427 1.1 mycroft
428 1.1 mycroft ump = VFSTOUFS(mp);
429 1.1 mycroft fs = ump->um_lfs;
430 1.1 mycroft if (fs->lfs_magic != LFS_MAGIC)
431 1.1 mycroft panic("lfs_statfs: magic");
432 1.1 mycroft sbp->f_type = 0;
433 1.1 mycroft sbp->f_bsize = fs->lfs_bsize;
434 1.1 mycroft sbp->f_iosize = fs->lfs_bsize;
435 1.1 mycroft sbp->f_blocks = dbtofsb(fs,fs->lfs_dsize);
436 1.1 mycroft sbp->f_bfree = dbtofsb(fs, fs->lfs_bfree);
437 1.14 mjacob sbp->f_bavail = (long) (((u_int64_t) fs->lfs_dsize * (u_int64_t)
438 1.14 mjacob (100 - fs->lfs_minfree) / (u_int64_t) 100) -
439 1.14 mjacob (u_int64_t) (fs->lfs_dsize - sbp->f_bfree));
440 1.1 mycroft sbp->f_bavail = dbtofsb(fs, sbp->f_bavail);
441 1.1 mycroft sbp->f_files = fs->lfs_nfiles;
442 1.1 mycroft sbp->f_ffree = sbp->f_bfree * INOPB(fs);
443 1.1 mycroft if (sbp != &mp->mnt_stat) {
444 1.6 mycroft bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
445 1.6 mycroft bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
446 1.1 mycroft }
447 1.6 mycroft strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
448 1.1 mycroft return (0);
449 1.1 mycroft }
450 1.1 mycroft
451 1.1 mycroft /*
452 1.1 mycroft * Go through the disk queues to initiate sandbagged IO;
453 1.1 mycroft * go through the inodes to write those that have been modified;
454 1.1 mycroft * initiate the writing of the super block if it has been modified.
455 1.1 mycroft *
456 1.1 mycroft * Note: we are always called with the filesystem marked `MPBUSY'.
457 1.1 mycroft */
458 1.10 christos int
459 1.1 mycroft lfs_sync(mp, waitfor, cred, p)
460 1.1 mycroft struct mount *mp;
461 1.1 mycroft int waitfor;
462 1.1 mycroft struct ucred *cred;
463 1.1 mycroft struct proc *p;
464 1.1 mycroft {
465 1.1 mycroft int error;
466 1.1 mycroft
467 1.1 mycroft /* All syncs must be checkpoints until roll-forward is implemented. */
468 1.1 mycroft error = lfs_segwrite(mp, SEGM_CKP | (waitfor ? SEGM_SYNC : 0));
469 1.1 mycroft #ifdef QUOTA
470 1.1 mycroft qsync(mp);
471 1.1 mycroft #endif
472 1.1 mycroft return (error);
473 1.1 mycroft }
474 1.1 mycroft
475 1.1 mycroft /*
476 1.1 mycroft * Look up an LFS dinode number to find its incore vnode. If not already
477 1.1 mycroft * in core, read it in from the specified device. Return the inode locked.
478 1.1 mycroft * Detection and handling of mount points must be done by the calling routine.
479 1.1 mycroft */
480 1.1 mycroft int
481 1.1 mycroft lfs_vget(mp, ino, vpp)
482 1.1 mycroft struct mount *mp;
483 1.1 mycroft ino_t ino;
484 1.1 mycroft struct vnode **vpp;
485 1.1 mycroft {
486 1.1 mycroft register struct lfs *fs;
487 1.1 mycroft register struct inode *ip;
488 1.1 mycroft struct buf *bp;
489 1.1 mycroft struct ifile *ifp;
490 1.1 mycroft struct vnode *vp;
491 1.1 mycroft struct ufsmount *ump;
492 1.1 mycroft daddr_t daddr;
493 1.1 mycroft dev_t dev;
494 1.1 mycroft int error;
495 1.1 mycroft
496 1.1 mycroft ump = VFSTOUFS(mp);
497 1.1 mycroft dev = ump->um_dev;
498 1.1 mycroft if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
499 1.1 mycroft return (0);
500 1.1 mycroft
501 1.1 mycroft /* Translate the inode number to a disk address. */
502 1.1 mycroft fs = ump->um_lfs;
503 1.1 mycroft if (ino == LFS_IFILE_INUM)
504 1.1 mycroft daddr = fs->lfs_idaddr;
505 1.1 mycroft else {
506 1.1 mycroft LFS_IENTRY(ifp, fs, ino, bp);
507 1.1 mycroft daddr = ifp->if_daddr;
508 1.1 mycroft brelse(bp);
509 1.1 mycroft if (daddr == LFS_UNUSED_DADDR)
510 1.1 mycroft return (ENOENT);
511 1.1 mycroft }
512 1.1 mycroft
513 1.1 mycroft /* Allocate new vnode/inode. */
514 1.10 christos if ((error = lfs_vcreate(mp, ino, &vp)) != 0) {
515 1.1 mycroft *vpp = NULL;
516 1.1 mycroft return (error);
517 1.1 mycroft }
518 1.1 mycroft
519 1.1 mycroft /*
520 1.1 mycroft * Put it onto its hash chain and lock it so that other requests for
521 1.1 mycroft * this inode will block if they arrive while we are sleeping waiting
522 1.1 mycroft * for old data structures to be purged or for the contents of the
523 1.1 mycroft * disk portion of this inode to be read.
524 1.1 mycroft */
525 1.1 mycroft ip = VTOI(vp);
526 1.1 mycroft ufs_ihashins(ip);
527 1.1 mycroft
528 1.1 mycroft /*
529 1.1 mycroft * XXX
530 1.1 mycroft * This may not need to be here, logically it should go down with
531 1.1 mycroft * the i_devvp initialization.
532 1.1 mycroft * Ask Kirk.
533 1.1 mycroft */
534 1.1 mycroft ip->i_lfs = ump->um_lfs;
535 1.1 mycroft
536 1.1 mycroft /* Read in the disk contents for the inode, copy into the inode. */
537 1.10 christos error = bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp);
538 1.10 christos if (error) {
539 1.1 mycroft /*
540 1.1 mycroft * The inode does not contain anything useful, so it would
541 1.1 mycroft * be misleading to leave it on its hash chain. With mode
542 1.1 mycroft * still zero, it will be unlinked and returned to the free
543 1.1 mycroft * list by vput().
544 1.1 mycroft */
545 1.1 mycroft vput(vp);
546 1.1 mycroft brelse(bp);
547 1.1 mycroft *vpp = NULL;
548 1.1 mycroft return (error);
549 1.1 mycroft }
550 1.13 bouyer ip->i_din.ffs_din = *lfs_ifind(fs, ino, (struct dinode *)bp->b_data);
551 1.1 mycroft brelse(bp);
552 1.1 mycroft
553 1.1 mycroft /*
554 1.1 mycroft * Initialize the vnode from the inode, check for aliases. In all
555 1.1 mycroft * cases re-init ip, the underlying vnode/inode may have changed.
556 1.1 mycroft */
557 1.10 christos error = ufs_vinit(mp, lfs_specop_p, LFS_FIFOOPS, &vp);
558 1.10 christos if (error) {
559 1.1 mycroft vput(vp);
560 1.1 mycroft *vpp = NULL;
561 1.1 mycroft return (error);
562 1.1 mycroft }
563 1.1 mycroft /*
564 1.1 mycroft * Finish inode initialization now that aliasing has been resolved.
565 1.1 mycroft */
566 1.1 mycroft ip->i_devvp = ump->um_devvp;
567 1.1 mycroft VREF(ip->i_devvp);
568 1.1 mycroft *vpp = vp;
569 1.1 mycroft return (0);
570 1.1 mycroft }
571 1.1 mycroft
572 1.1 mycroft /*
573 1.1 mycroft * File handle to vnode
574 1.1 mycroft *
575 1.1 mycroft * Have to be really careful about stale file handles:
576 1.1 mycroft * - check that the inode number is valid
577 1.1 mycroft * - call lfs_vget() to get the locked inode
578 1.1 mycroft * - check for an unallocated inode (i_mode == 0)
579 1.1 mycroft * - check that the given client host has export rights and return
580 1.1 mycroft * those rights via. exflagsp and credanonp
581 1.1 mycroft *
582 1.1 mycroft * XXX
583 1.1 mycroft * use ifile to see if inode is allocated instead of reading off disk
584 1.1 mycroft * what is the relationship between my generational number and the NFS
585 1.1 mycroft * generational number.
586 1.1 mycroft */
587 1.1 mycroft int
588 1.1 mycroft lfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
589 1.1 mycroft register struct mount *mp;
590 1.1 mycroft struct fid *fhp;
591 1.1 mycroft struct mbuf *nam;
592 1.1 mycroft struct vnode **vpp;
593 1.1 mycroft int *exflagsp;
594 1.1 mycroft struct ucred **credanonp;
595 1.1 mycroft {
596 1.1 mycroft register struct ufid *ufhp;
597 1.1 mycroft
598 1.1 mycroft ufhp = (struct ufid *)fhp;
599 1.1 mycroft if (ufhp->ufid_ino < ROOTINO)
600 1.1 mycroft return (ESTALE);
601 1.1 mycroft return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
602 1.1 mycroft }
603 1.1 mycroft
604 1.1 mycroft /*
605 1.1 mycroft * Vnode pointer to File handle
606 1.1 mycroft */
607 1.1 mycroft /* ARGSUSED */
608 1.10 christos int
609 1.1 mycroft lfs_vptofh(vp, fhp)
610 1.1 mycroft struct vnode *vp;
611 1.1 mycroft struct fid *fhp;
612 1.1 mycroft {
613 1.1 mycroft register struct inode *ip;
614 1.1 mycroft register struct ufid *ufhp;
615 1.1 mycroft
616 1.1 mycroft ip = VTOI(vp);
617 1.1 mycroft ufhp = (struct ufid *)fhp;
618 1.1 mycroft ufhp->ufid_len = sizeof(struct ufid);
619 1.1 mycroft ufhp->ufid_ino = ip->i_number;
620 1.13 bouyer ufhp->ufid_gen = ip->i_ffs_gen;
621 1.1 mycroft return (0);
622 1.1 mycroft }
623