Consolidate the file descriptor destruction code used when a newly created
[dragonfly.git] / sys / vfs / fdesc / fdesc_vnops.c
... / ...
CommitLineData
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
37 *
38 * $FreeBSD: src/sys/miscfs/fdesc/fdesc_vnops.c,v 1.47.2.1 2001/10/22 22:49:26 chris Exp $
39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.28 2006/05/06 18:48:52 dillon Exp $
40 */
41
42/*
43 * /dev/fd Filesystem
44 */
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/conf.h>
49#include <sys/dirent.h>
50#include <sys/filedesc.h>
51#include <sys/kernel.h> /* boottime */
52#include <sys/lock.h>
53#include <sys/malloc.h>
54#include <sys/file.h> /* Must come after sys/malloc.h */
55#include <sys/mount.h>
56#include <sys/proc.h>
57#include <sys/namei.h>
58#include <sys/socket.h>
59#include <sys/stat.h>
60#include <sys/vnode.h>
61#include <sys/file2.h>
62
63#include <machine/limits.h>
64
65#include "fdesc.h"
66
67#define FDL_WANT 0x01
68#define FDL_LOCKED 0x02
69static int fdcache_lock;
70
71#define NFDCACHE 4
72#define FD_NHASH(ix) \
73 (&fdhashtbl[(ix) & fdhash])
74static LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
75static u_long fdhash;
76
77static int fdesc_getattr (struct vop_getattr_args *ap);
78static int fdesc_inactive (struct vop_inactive_args *ap);
79static int fdesc_lookup (struct vop_old_lookup_args *ap);
80static int fdesc_open (struct vop_open_args *ap);
81static int fdesc_print (struct vop_print_args *ap);
82static int fdesc_readdir (struct vop_readdir_args *ap);
83static int fdesc_reclaim (struct vop_reclaim_args *ap);
84static int fdesc_poll (struct vop_poll_args *ap);
85static int fdesc_setattr (struct vop_setattr_args *ap);
86
87/*
88 * Initialise cache headers
89 */
90int
91fdesc_init(struct vfsconf *vfsp)
92{
93
94 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
95 return (0);
96}
97
98int
99fdesc_uninit(struct vfsconf *vfsp)
100{
101 if (fdhashtbl)
102 free(fdhashtbl, M_CACHE);
103 return (0);
104}
105int
106fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp)
107{
108 struct fdhashhead *fc;
109 struct fdescnode *fd;
110 int error = 0;
111
112 fc = FD_NHASH(ix);
113loop:
114 LIST_FOREACH(fd, fc, fd_hash) {
115 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
116 if (vget(fd->fd_vnode, LK_EXCLUSIVE|LK_SLEEPFAIL))
117 goto loop;
118 *vpp = fd->fd_vnode;
119 return (error);
120 }
121 }
122
123 /*
124 * otherwise lock the array while we call getnewvnode
125 * since that can block.
126 */
127 if (fdcache_lock & FDL_LOCKED) {
128 fdcache_lock |= FDL_WANT;
129 tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
130 goto loop;
131 }
132 fdcache_lock |= FDL_LOCKED;
133
134 /*
135 * Do the MALLOC before the getnewvnode since doing so afterward
136 * might cause a bogus v_data pointer to get dereferenced
137 * elsewhere if MALLOC should block.
138 */
139 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
140
141 error = getnewvnode(VT_FDESC, mp, vpp, 0, 0);
142 if (error) {
143 FREE(fd, M_TEMP);
144 goto out;
145 }
146 (*vpp)->v_data = fd;
147 fd->fd_vnode = *vpp;
148 fd->fd_type = ftype;
149 fd->fd_fd = -1;
150 fd->fd_ix = ix;
151 LIST_INSERT_HEAD(fc, fd, fd_hash);
152 vx_unlock(*vpp);
153
154out:
155 fdcache_lock &= ~FDL_LOCKED;
156
157 if (fdcache_lock & FDL_WANT) {
158 fdcache_lock &= ~FDL_WANT;
159 wakeup((caddr_t) &fdcache_lock);
160 }
161
162 return (error);
163}
164
165/*
166 * vp is the current namei directory
167 * ndp is the name to locate in that directory...
168 *
169 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
170 * struct componentname *a_cnp)
171 */
172static int
173fdesc_lookup(struct vop_old_lookup_args *ap)
174{
175 struct componentname *cnp = ap->a_cnp;
176 struct thread *td = cnp->cn_td;
177 struct proc *p = td->td_proc;
178 struct vnode **vpp = ap->a_vpp;
179 struct vnode *dvp = ap->a_dvp;
180 char *pname = cnp->cn_nameptr;
181 int nlen = cnp->cn_namelen;
182 int nfiles;
183 u_int fd;
184 int error;
185 struct vnode *fvp;
186
187 KKASSERT(p);
188 nfiles = p->p_fd->fd_nfiles;
189 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
190 error = EROFS;
191 goto bad;
192 }
193
194 VOP_UNLOCK(dvp, 0);
195 if (cnp->cn_namelen == 1 && *pname == '.') {
196 *vpp = dvp;
197 vref(dvp);
198 vn_lock(dvp, LK_SHARED | LK_RETRY);
199 return (0);
200 }
201
202 if (VTOFDESC(dvp)->fd_type != Froot) {
203 error = ENOTDIR;
204 goto bad;
205 }
206
207 fd = 0;
208 /* the only time a leading 0 is acceptable is if it's "0" */
209 if (*pname == '0' && nlen != 1) {
210 error = ENOENT;
211 goto bad;
212 }
213 while (nlen--) {
214 if (*pname < '0' || *pname > '9') {
215 error = ENOENT;
216 goto bad;
217 }
218 fd = 10 * fd + *pname++ - '0';
219 }
220
221 if (fd >= nfiles || p->p_fd->fd_files[fd].fp == NULL) {
222 error = EBADF;
223 goto bad;
224 }
225
226 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
227 if (error)
228 goto bad;
229 VTOFDESC(fvp)->fd_fd = fd;
230 vn_lock(fvp, LK_SHARED | LK_RETRY);
231 *vpp = fvp;
232 return (0);
233
234bad:
235 vn_lock(dvp, LK_SHARED | LK_RETRY);
236 *vpp = NULL;
237 return (error);
238}
239
240/*
241 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
242 */
243static int
244fdesc_open(struct vop_open_args *ap)
245{
246 struct vnode *vp = ap->a_vp;
247 struct lwp *lp = curthread->td_lwp;
248
249 KKASSERT(lp);
250
251 if (VTOFDESC(vp)->fd_type == Froot)
252 return (vop_stdopen(ap));
253
254 /*
255 * XXX Kludge: set lp->lwp_dupfd to contain the value of the the file
256 * descriptor being sought for duplication. The error return ensures
257 * that the vnode for this device will be released by vn_open. Open
258 * will detect this special error and take the actions in dupfdopen.
259 * Other callers of vn_open or VOP_OPEN will simply report the
260 * error.
261 */
262 lp->lwp_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
263 return (ENODEV);
264}
265
266/*
267 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
268 */
269static int
270fdesc_getattr(struct vop_getattr_args *ap)
271{
272 struct proc *p = curproc;
273 struct vnode *vp = ap->a_vp;
274 struct vattr *vap = ap->a_vap;
275 struct filedesc *fdp;
276 struct file *fp;
277 struct stat stb;
278 u_int fd;
279 int error = 0;
280
281 KKASSERT(p);
282 fdp = p->p_fd;
283 switch (VTOFDESC(vp)->fd_type) {
284 case Froot:
285 VATTR_NULL(vap);
286
287 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
288 vap->va_type = VDIR;
289 vap->va_nlink = 2;
290 vap->va_size = DEV_BSIZE;
291 vap->va_fileid = VTOFDESC(vp)->fd_ix;
292 vap->va_uid = 0;
293 vap->va_gid = 0;
294 vap->va_blocksize = DEV_BSIZE;
295 vap->va_atime.tv_sec = boottime.tv_sec;
296 vap->va_atime.tv_nsec = 0;
297 vap->va_mtime = vap->va_atime;
298 vap->va_ctime = vap->va_mtime;
299 vap->va_gen = 0;
300 vap->va_flags = 0;
301 vap->va_rdev = 0;
302 vap->va_bytes = 0;
303 break;
304
305 case Fdesc:
306 fd = VTOFDESC(vp)->fd_fd;
307
308 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_files[fd].fp) == NULL)
309 return (EBADF);
310
311 bzero(&stb, sizeof(stb));
312 error = fo_stat(fp, &stb, curproc->p_ucred);
313 if (error == 0) {
314 VATTR_NULL(vap);
315 vap->va_type = IFTOVT(stb.st_mode);
316 vap->va_mode = stb.st_mode;
317#define FDRX (VREAD|VEXEC)
318 if (vap->va_type == VDIR)
319 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
320#undef FDRX
321 vap->va_nlink = 1;
322 vap->va_flags = 0;
323 vap->va_bytes = stb.st_blocks * stb.st_blksize;
324 vap->va_fileid = VTOFDESC(vp)->fd_ix;
325 vap->va_size = stb.st_size;
326 vap->va_blocksize = stb.st_blksize;
327 vap->va_rdev = stb.st_rdev;
328
329 /*
330 * If no time data is provided, use the current time.
331 */
332 if (stb.st_atimespec.tv_sec == 0 &&
333 stb.st_atimespec.tv_nsec == 0)
334 nanotime(&stb.st_atimespec);
335
336 if (stb.st_ctimespec.tv_sec == 0 &&
337 stb.st_ctimespec.tv_nsec == 0)
338 nanotime(&stb.st_ctimespec);
339
340 if (stb.st_mtimespec.tv_sec == 0 &&
341 stb.st_mtimespec.tv_nsec == 0)
342 nanotime(&stb.st_mtimespec);
343
344 vap->va_atime = stb.st_atimespec;
345 vap->va_mtime = stb.st_mtimespec;
346 vap->va_ctime = stb.st_ctimespec;
347 vap->va_uid = stb.st_uid;
348 vap->va_gid = stb.st_gid;
349 }
350 break;
351
352 default:
353 panic("fdesc_getattr");
354 break;
355 }
356
357 if (error == 0)
358 vp->v_type = vap->va_type;
359 return (error);
360}
361
362/*
363 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
364 * struct ucred *a_cred, struct thread *a_td)
365 */
366static int
367fdesc_setattr(struct vop_setattr_args *ap)
368{
369 struct proc *p = curproc;
370 struct vattr *vap = ap->a_vap;
371 struct file *fp;
372 unsigned fd;
373 int error;
374
375 /*
376 * Can't mess with the root vnode
377 */
378 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
379 return (EACCES);
380
381 fd = VTOFDESC(ap->a_vp)->fd_fd;
382 KKASSERT(p);
383
384 /*
385 * Allow setattr where there is an underlying vnode.
386 */
387 error = getvnode(p->p_fd, fd, &fp);
388 if (error) {
389 /*
390 * getvnode() returns EINVAL if the file descriptor is not
391 * backed by a vnode. Silently drop all changes except
392 * chflags(2) in this case.
393 */
394 if (error == EINVAL) {
395 if (vap->va_flags != VNOVAL)
396 error = EOPNOTSUPP;
397 else
398 error = 0;
399 }
400 return (error);
401 }
402 return (error);
403}
404
405#define UIO_MX 16
406
407/*
408 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
409 * int *a_eofflag, u_long *a_cookies, int a_ncookies)
410 */
411static int
412fdesc_readdir(struct vop_readdir_args *ap)
413{
414 struct uio *uio = ap->a_uio;
415 struct filedesc *fdp;
416 int error, i, fcnt;
417 size_t namelen;
418 char name[20]; /* enough for %d */
419
420 /*
421 * We don't allow exporting fdesc mounts, and currently local
422 * requests do not need cookies.
423 */
424 if (ap->a_ncookies)
425 panic("fdesc_readdir: not hungry");
426
427 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
428 panic("fdesc_readdir: not dir");
429
430 if (uio->uio_offset < 0 || uio->uio_offset > INT_MAX ||
431 uio->uio_resid < 0)
432 return(EINVAL);
433 i = uio->uio_offset;
434 KKASSERT(uio->uio_td->td_proc);
435 fdp = uio->uio_td->td_proc->p_fd;
436 error = 0;
437
438 fcnt = i - 2; /* The first two nodes are `.' and `..' */
439
440 while (fcnt < fdp->fd_nfiles && uio->uio_resid > 0 && !error) {
441 switch (i) {
442 case 0: /* `.' */
443 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
444 1, "."))
445 goto done;
446 if (error)
447 return (error);
448 break;
449 case 1: /* `..' */
450 if (vop_write_dirent(&error, uio, FD_ROOT + i, DT_DIR,
451 2, ".."))
452 goto done;
453 if (error)
454 return (error);
455 break;
456 default:
457 if (fdp->fd_files[fcnt].fp == NULL) {
458 fcnt++;
459 continue;
460 }
461
462 namelen = snprintf(name, sizeof(name), "%d", fcnt);
463 if (vop_write_dirent(&error, uio, FD_ROOT + i,
464 DT_UNKNOWN, namelen, name))
465 goto done;
466 if (error)
467 return (error);
468 break;
469 }
470 i++;
471 fcnt++;
472 }
473
474done:
475 if (i >= 2)
476 uio->uio_offset = fcnt + 2;
477 else
478 uio->uio_offset = i;
479 return (error);
480}
481
482/*
483 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
484 * struct thread *a_td)
485 */
486static int
487fdesc_poll(struct vop_poll_args *ap)
488{
489 return seltrue(0, ap->a_events, curthread);
490}
491
492/*
493 * fdesc_inactive(struct vnode *a_vp, struct thread *a_td)
494 */
495static int
496fdesc_inactive(struct vop_inactive_args *ap)
497{
498 struct vnode *vp = ap->a_vp;
499
500 /*
501 * Clear out the v_type field to avoid
502 * nasty things happening in vgone().
503 */
504 vp->v_type = VNON;
505 return (0);
506}
507
508/*
509 * fdesc_reclaim(struct vnode *a_vp)
510 */
511static int
512fdesc_reclaim(struct vop_reclaim_args *ap)
513{
514 struct vnode *vp = ap->a_vp;
515 struct fdescnode *fd = VTOFDESC(vp);
516
517 LIST_REMOVE(fd, fd_hash);
518 FREE(vp->v_data, M_TEMP);
519 vp->v_data = 0;
520
521 return (0);
522}
523
524/*
525 * Print out the contents of a /dev/fd vnode.
526 *
527 * fdesc_print(struct vnode *a_vp)
528 */
529/* ARGSUSED */
530static int
531fdesc_print(struct vop_print_args *ap)
532{
533 printf("tag VT_NON, fdesc vnode\n");
534 return (0);
535}
536
537struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
538 { &vop_default_desc, vop_defaultop },
539 { &vop_access_desc, vop_null },
540 { &vop_getattr_desc, (vnodeopv_entry_t) fdesc_getattr },
541 { &vop_inactive_desc, (vnodeopv_entry_t) fdesc_inactive },
542 { &vop_old_lookup_desc, (vnodeopv_entry_t) fdesc_lookup },
543 { &vop_open_desc, (vnodeopv_entry_t) fdesc_open },
544 { &vop_pathconf_desc, (vnodeopv_entry_t) vop_stdpathconf },
545 { &vop_poll_desc, (vnodeopv_entry_t) fdesc_poll },
546 { &vop_print_desc, (vnodeopv_entry_t) fdesc_print },
547 { &vop_readdir_desc, (vnodeopv_entry_t) fdesc_readdir },
548 { &vop_reclaim_desc, (vnodeopv_entry_t) fdesc_reclaim },
549 { &vop_setattr_desc, (vnodeopv_entry_t) fdesc_setattr },
550 { NULL, NULL }
551};
552