1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
33 *
34 * $FreeBSD$
35 */
36
37 /*
38 * /dev/fd Filesystem
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/dirent.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h> /* boottime */
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/malloc.h>
50 #include <sys/file.h> /* Must come after sys/malloc.h */
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/proc.h>
54 #include <sys/stat.h>
55 #include <sys/vnode.h>
56
57 #include <fs/fdescfs/fdesc.h>
58
59 #define NFDCACHE 4
60 #define FD_NHASH(ix) \
61 (&fdhashtbl[(ix) & fdhash])
62 static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
63 static u_long fdhash;
64
65 struct mtx fdesc_hashmtx;
66
67 static vop_getattr_t fdesc_getattr;
68 static vop_lookup_t fdesc_lookup;
69 static vop_open_t fdesc_open;
70 static vop_readdir_t fdesc_readdir;
71 static vop_reclaim_t fdesc_reclaim;
72 static vop_setattr_t fdesc_setattr;
73
74 static struct vop_vector fdesc_vnodeops = {
75 .vop_default = &default_vnodeops,
76
77 .vop_access = VOP_NULL,
78 .vop_getattr = fdesc_getattr,
79 .vop_lookup = fdesc_lookup,
80 .vop_open = fdesc_open,
81 .vop_pathconf = vop_stdpathconf,
82 .vop_readdir = fdesc_readdir,
83 .vop_reclaim = fdesc_reclaim,
84 .vop_setattr = fdesc_setattr,
85 };
86
87 static void fdesc_insmntque_dtr(struct vnode *, void *);
88 static void fdesc_remove_entry(struct fdescnode *);
89
90 /*
91 * Initialise cache headers
92 */
93 int
94 fdesc_init(vfsp)
95 struct vfsconf *vfsp;
96 {
97
98 mtx_init(&fdesc_hashmtx, "fdescfs_hash", NULL, MTX_DEF);
99 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
100 return (0);
101 }
102
103 /*
104 * Uninit ready for unload.
105 */
106 int
107 fdesc_uninit(vfsp)
108 struct vfsconf *vfsp;
109 {
110
111 hashdestroy(fdhashtbl, M_CACHE, fdhash);
112 mtx_destroy(&fdesc_hashmtx);
113 return (0);
114 }
115
116 /*
117 * If allocating vnode fails, call this.
118 */
119 static void
120 fdesc_insmntque_dtr(struct vnode *vp, void *arg)
121 {
122
123 vgone(vp);
124 vput(vp);
125 }
126
127 /*
128 * Remove an entry from the hash if it exists.
129 */
130 static void
131 fdesc_remove_entry(struct fdescnode *fd)
132 {
133 struct fdhashhead *fc;
134 struct fdescnode *fd2;
135
136 fc = FD_NHASH(fd->fd_ix);
137 mtx_lock(&fdesc_hashmtx);
138 LIST_FOREACH(fd2, fc, fd_hash) {
139 if (fd == fd2) {
140 LIST_REMOVE(fd, fd_hash);
141 break;
142 }
143 }
144 mtx_unlock(&fdesc_hashmtx);
145 }
146
147 int
148 fdesc_allocvp(ftype, fd_fd, ix, mp, vpp, td)
149 fdntype ftype;
150 unsigned fd_fd;
151 int ix;
152 struct mount *mp;
153 struct vnode **vpp;
154 struct thread *td;
155 {
156 struct fdescmount *fmp;
157 struct fdhashhead *fc;
158 struct fdescnode *fd, *fd2;
159 struct vnode *vp, *vp2;
160 int error = 0;
161
162 fc = FD_NHASH(ix);
163 loop:
164 mtx_lock(&fdesc_hashmtx);
165 /*
166 * If a forced unmount is progressing, we need to drop it. The flags are
167 * protected by the hashmtx.
168 */
169 fmp = (struct fdescmount *)mp->mnt_data;
170 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
171 mtx_unlock(&fdesc_hashmtx);
172 return (-1);
173 }
174
175 LIST_FOREACH(fd, fc, fd_hash) {
176 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
177 /* Get reference to vnode in case it's being free'd */
178 vp = fd->fd_vnode;
179 VI_LOCK(vp);
180 mtx_unlock(&fdesc_hashmtx);
181 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td))
182 goto loop;
183 *vpp = vp;
184 return (0);
185 }
186 }
187 mtx_unlock(&fdesc_hashmtx);
188
189 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
190
191 error = getnewvnode("fdescfs", mp, &fdesc_vnodeops, &vp);
192 if (error) {
193 FREE(fd, M_TEMP);
194 return (error);
195 }
196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
197 vp->v_data = fd;
198 fd->fd_vnode = vp;
199 fd->fd_type = ftype;
200 fd->fd_fd = fd_fd;
201 fd->fd_ix = ix;
202 error = insmntque1(vp, mp, fdesc_insmntque_dtr, NULL);
203 if (error != 0) {
204 *vpp = NULLVP;
205 return (error);
206 }
207
208 /* Make sure that someone didn't beat us when inserting the vnode. */
209 mtx_lock(&fdesc_hashmtx);
210 /*
211 * If a forced unmount is progressing, we need to drop it. The flags are
212 * protected by the hashmtx.
213 */
214 fmp = (struct fdescmount *)mp->mnt_data;
215 if (fmp == NULL || fmp->flags & FMNT_UNMOUNTF) {
216 mtx_unlock(&fdesc_hashmtx);
217 vgone(vp);
218 vput(vp);
219 *vpp = NULLVP;
220 return (-1);
221 }
222
223 LIST_FOREACH(fd2, fc, fd_hash) {
224 if (fd2->fd_ix == ix && fd2->fd_vnode->v_mount == mp) {
225 /* Get reference to vnode in case it's being free'd */
226 vp2 = fd2->fd_vnode;
227 VI_LOCK(vp2);
228 mtx_unlock(&fdesc_hashmtx);
229 error = vget(vp2, LK_EXCLUSIVE | LK_INTERLOCK, td);
230 /* Someone beat us, dec use count and wait for reclaim */
231 vgone(vp);
232 vput(vp);
233 /* If we didn't get it, return no vnode. */
234 if (error)
235 vp2 = NULLVP;
236 *vpp = vp2;
237 return (error);
238 }
239 }
240
241 /* If we came here, we can insert it safely. */
242 LIST_INSERT_HEAD(fc, fd, fd_hash);
243 mtx_unlock(&fdesc_hashmtx);
244 *vpp = vp;
245 return (0);
246 }
247
248 /*
249 * vp is the current namei directory
250 * ndp is the name to locate in that directory...
251 */
252 static int
253 fdesc_lookup(ap)
254 struct vop_lookup_args /* {
255 struct vnode * a_dvp;
256 struct vnode ** a_vpp;
257 struct componentname * a_cnp;
258 } */ *ap;
259 {
260 struct vnode **vpp = ap->a_vpp;
261 struct vnode *dvp = ap->a_dvp;
262 struct componentname *cnp = ap->a_cnp;
263 char *pname = cnp->cn_nameptr;
264 struct thread *td = cnp->cn_thread;
265 struct file *fp;
266 int nlen = cnp->cn_namelen;
267 u_int fd, fd1;
268 int error;
269 struct vnode *fvp;
270
271 if ((cnp->cn_flags & ISLASTCN) &&
272 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
273 error = EROFS;
274 goto bad;
275 }
276
277 if (cnp->cn_namelen == 1 && *pname == '.') {
278 *vpp = dvp;
279 VREF(dvp);
280 return (0);
281 }
282
283 if (VTOFDESC(dvp)->fd_type != Froot) {
284 error = ENOTDIR;
285 goto bad;
286 }
287
288 fd = 0;
289 /* the only time a leading 0 is acceptable is if it's "" */
290 if (*pname == '' && nlen != 1) {
291 error = ENOENT;
292 goto bad;
293 }
294 while (nlen--) {
295 if (*pname < '' || *pname > '9') {
296 error = ENOENT;
297 goto bad;
298 }
299 fd1 = 10 * fd + *pname++ - '';
300 if (fd1 < fd) {
301 error = ENOENT;
302 goto bad;
303 }
304 fd = fd1;
305 }
306
307 if ((error = fget(td, fd, &fp)) != 0)
308 goto bad;
309
310 /* Check if we're looking up ourselves. */
311 if (VTOFDESC(dvp)->fd_ix == FD_DESC + fd) {
312 /*
313 * In case we're holding the last reference to the file, the dvp
314 * will be re-acquired.
315 */
316 vhold(dvp);
317 VOP_UNLOCK(dvp, 0, td);
318 fdrop(fp, td);
319
320 /* Re-aquire the lock afterwards. */
321 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE, td);
322 vdrop(dvp);
323 fvp = dvp;
324 } else {
325 /*
326 * Unlock our root node (dvp) when doing this, since we might
327 * deadlock since the vnode might be locked by another thread
328 * and the root vnode lock will be obtained afterwards (in case
329 * we're looking up the fd of the root vnode), which will be the
330 * opposite lock order. Vhold the root vnode first so we don't
331 * loose it.
332 */
333 vhold(dvp);
334 VOP_UNLOCK(dvp, 0, td);
335 error = fdesc_allocvp(Fdesc, fd, FD_DESC + fd, dvp->v_mount,
336 &fvp, td);
337 fdrop(fp, td);
338 /*
339 * The root vnode must be locked last to prevent deadlock condition.
340 */
341 vn_lock(dvp, LK_RETRY | LK_EXCLUSIVE, td);
342 vdrop(dvp);
343 }
344
345 if (error)
346 goto bad;
347 *vpp = fvp;
348 return (0);
349
350 bad:
351 *vpp = NULL;
352 return (error);
353 }
354
355 static int
356 fdesc_open(ap)
357 struct vop_open_args /* {
358 struct vnode *a_vp;
359 int a_mode;
360 struct ucred *a_cred;
361 struct thread *a_td;
362 } */ *ap;
363 {
364 struct vnode *vp = ap->a_vp;
365
366 if (VTOFDESC(vp)->fd_type == Froot)
367 return (0);
368
369 /*
370 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
371 * descriptor being sought for duplication. The error return ensures
372 * that the vnode for this device will be released by vn_open. Open
373 * will detect this special error and take the actions in dupfdopen.
374 * Other callers of vn_open or VOP_OPEN will simply report the
375 * error.
376 */
377 ap->a_td->td_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
378 return (ENODEV);
379 }
380
381 static int
382 fdesc_getattr(ap)
383 struct vop_getattr_args /* {
384 struct vnode *a_vp;
385 struct vattr *a_vap;
386 struct ucred *a_cred;
387 struct thread *a_td;
388 } */ *ap;
389 {
390 struct vnode *vp = ap->a_vp;
391 struct vattr *vap = ap->a_vap;
392 struct file *fp;
393 struct stat stb;
394 u_int fd;
395 int error = 0;
396
397 switch (VTOFDESC(vp)->fd_type) {
398 case Froot:
399 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
400 vap->va_type = VDIR;
401 vap->va_nlink = 2;
402 vap->va_size = DEV_BSIZE;
403 vap->va_fileid = VTOFDESC(vp)->fd_ix;
404 vap->va_uid = 0;
405 vap->va_gid = 0;
406 vap->va_blocksize = DEV_BSIZE;
407 vap->va_atime.tv_sec = boottime.tv_sec;
408 vap->va_atime.tv_nsec = 0;
409 vap->va_mtime = vap->va_atime;
410 vap->va_ctime = vap->va_mtime;
411 vap->va_gen = 0;
412 vap->va_flags = 0;
413 vap->va_rdev = NODEV;
414 vap->va_bytes = 0;
415 vap->va_filerev = 0;
416 break;
417
418 case Fdesc:
419 fd = VTOFDESC(vp)->fd_fd;
420
421 if ((error = fget(ap->a_td, fd, &fp)) != 0)
422 return (error);
423
424 bzero(&stb, sizeof(stb));
425 error = fo_stat(fp, &stb, ap->a_td->td_ucred, ap->a_td);
426 fdrop(fp, ap->a_td);
427 if (error == 0) {
428 vap->va_type = IFTOVT(stb.st_mode);
429 vap->va_mode = stb.st_mode;
430 #define FDRX (VREAD|VEXEC)
431 if (vap->va_type == VDIR)
432 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
433 #undef FDRX
434 vap->va_nlink = 1;
435 vap->va_flags = 0;
436 vap->va_bytes = stb.st_blocks * stb.st_blksize;
437 vap->va_fileid = VTOFDESC(vp)->fd_ix;
438 vap->va_size = stb.st_size;
439 vap->va_blocksize = stb.st_blksize;
440 vap->va_rdev = stb.st_rdev;
441
442 /*
443 * If no time data is provided, use the current time.
444 */
445 if (stb.st_atimespec.tv_sec == 0 &&
446 stb.st_atimespec.tv_nsec == 0)
447 nanotime(&stb.st_atimespec);
448
449 if (stb.st_ctimespec.tv_sec == 0 &&
450 stb.st_ctimespec.tv_nsec == 0)
451 nanotime(&stb.st_ctimespec);
452
453 if (stb.st_mtimespec.tv_sec == 0 &&
454 stb.st_mtimespec.tv_nsec == 0)
455 nanotime(&stb.st_mtimespec);
456
457 vap->va_atime = stb.st_atimespec;
458 vap->va_mtime = stb.st_mtimespec;
459 vap->va_ctime = stb.st_ctimespec;
460 vap->va_uid = stb.st_uid;
461 vap->va_gid = stb.st_gid;
462 vap->va_gen = 0;
463 vap->va_filerev = 0;
464 }
465 break;
466
467 default:
468 panic("fdesc_getattr");
469 break;
470 }
471
472 if (error == 0)
473 vp->v_type = vap->va_type;
474 return (error);
475 }
476
477 static int
478 fdesc_setattr(ap)
479 struct vop_setattr_args /* {
480 struct vnode *a_vp;
481 struct vattr *a_vap;
482 struct ucred *a_cred;
483 struct thread *a_td;
484 } */ *ap;
485 {
486 struct vattr *vap = ap->a_vap;
487 struct vnode *vp;
488 struct mount *mp;
489 struct file *fp;
490 unsigned fd;
491 int error;
492
493 /*
494 * Can't mess with the root vnode
495 */
496 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
497 return (EACCES);
498
499 fd = VTOFDESC(ap->a_vp)->fd_fd;
500
501 /*
502 * Allow setattr where there is an underlying vnode.
503 */
504 error = getvnode(ap->a_td->td_proc->p_fd, fd, &fp);
505 if (error) {
506 /*
507 * getvnode() returns EINVAL if the file descriptor is not
508 * backed by a vnode. Silently drop all changes except
509 * chflags(2) in this case.
510 */
511 if (error == EINVAL) {
512 if (vap->va_flags != VNOVAL)
513 error = EOPNOTSUPP;
514 else
515 error = 0;
516 }
517 return (error);
518 }
519 vp = fp->f_vnode;
520 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) == 0) {
521 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
522 error = VOP_SETATTR(vp, ap->a_vap, ap->a_cred, ap->a_td);
523 VOP_UNLOCK(vp, 0, ap->a_td);
524 vn_finished_write(mp);
525 }
526 fdrop(fp, ap->a_td);
527 return (error);
528 }
529
530 #define UIO_MX 16
531
532 static int
533 fdesc_readdir(ap)
534 struct vop_readdir_args /* {
535 struct vnode *a_vp;
536 struct uio *a_uio;
537 struct ucred *a_cred;
538 int *a_eofflag;
539 u_long *a_cookies;
540 int a_ncookies;
541 } */ *ap;
542 {
543 struct uio *uio = ap->a_uio;
544 struct filedesc *fdp;
545 struct dirent d;
546 struct dirent *dp = &d;
547 int error, i, off, fcnt;
548
549 /*
550 * We don't allow exporting fdesc mounts, and currently local
551 * requests do not need cookies.
552 */
553 if (ap->a_ncookies)
554 panic("fdesc_readdir: not hungry");
555
556 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
557 panic("fdesc_readdir: not dir");
558
559 off = (int)uio->uio_offset;
560 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
561 uio->uio_resid < UIO_MX)
562 return (EINVAL);
563 i = (u_int)off / UIO_MX;
564 fdp = uio->uio_td->td_proc->p_fd;
565 error = 0;
566
567 fcnt = i - 2; /* The first two nodes are `.' and `..' */
568
569 FILEDESC_SLOCK(fdp);
570 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
571 bzero((caddr_t)dp, UIO_MX);
572 switch (i) {
573 case 0: /* `.' */
574 case 1: /* `..' */
575 dp->d_fileno = i + FD_ROOT;
576 dp->d_namlen = i + 1;
577 dp->d_reclen = UIO_MX;
578 bcopy("..", dp->d_name, dp->d_namlen);
579 dp->d_name[i + 1] = '\0';
580 dp->d_type = DT_DIR;
581 break;
582 default:
583 if (fdp->fd_ofiles[fcnt] == NULL)
584 break;
585 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
586 dp->d_reclen = UIO_MX;
587 dp->d_type = DT_UNKNOWN;
588 dp->d_fileno = i + FD_DESC;
589 break;
590 }
591 if (dp->d_namlen != 0) {
592 /*
593 * And ship to userland
594 */
595 FILEDESC_SUNLOCK(fdp);
596 error = uiomove(dp, UIO_MX, uio);
597 if (error)
598 goto done;
599 FILEDESC_SLOCK(fdp);
600 }
601 i++;
602 fcnt++;
603 }
604 FILEDESC_SUNLOCK(fdp);
605
606 done:
607 uio->uio_offset = i * UIO_MX;
608 return (error);
609 }
610
611 static int
612 fdesc_reclaim(ap)
613 struct vop_reclaim_args /* {
614 struct vnode *a_vp;
615 } */ *ap;
616 {
617 struct vnode *vp;
618 struct fdescnode *fd;
619
620 vp = ap->a_vp;
621 fd = VTOFDESC(vp);
622 fdesc_remove_entry(fd);
623 FREE(vp->v_data, M_TEMP);
624 vp->v_data = NULL;
625 return (0);
626 }
Cache object: 8c09f8e58bd56dbf802430fcae447328
|