Add an installer-fetchpkgs target and other related stuff to reduce the
[dragonfly.git] / sys / vfs / fdesc / fdesc_vnops.c
CommitLineData
984263bc
MD
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 $
2d3e977e 39 * $DragonFly: src/sys/vfs/fdesc/fdesc_vnops.c,v 1.12 2004/08/13 17:51:10 dillon Exp $
984263bc
MD
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>
984263bc 56#include <sys/proc.h>
dadab5e9 57#include <sys/namei.h>
984263bc
MD
58#include <sys/socket.h>
59#include <sys/stat.h>
60#include <sys/vnode.h>
dadab5e9 61#include <sys/file2.h>
984263bc 62
1f2de5d4 63#include "fdesc.h"
984263bc
MD
64
65#define FDL_WANT 0x01
66#define FDL_LOCKED 0x02
67static int fdcache_lock;
68
2d3e977e 69static struct vop_ops *fdesc_vnode_vops;
984263bc
MD
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
a6ee311a
RG
77static int fdesc_getattr (struct vop_getattr_args *ap);
78static int fdesc_inactive (struct vop_inactive_args *ap);
79static int fdesc_lookup (struct vop_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);
984263bc
MD
86
87/*
88 * Initialise cache headers
89 */
90int
130da09f 91fdesc_init(struct vfsconf *vfsp)
984263bc
MD
92{
93
94 fdhashtbl = hashinit(NFDCACHE, M_CACHE, &fdhash);
95 return (0);
96}
97
98int
130da09f
CP
99fdesc_allocvp(fdntype ftype, int ix, struct mount *mp, struct vnode **vpp,
100 struct thread *td)
984263bc
MD
101{
102 struct fdhashhead *fc;
103 struct fdescnode *fd;
104 int error = 0;
105
106 fc = FD_NHASH(ix);
107loop:
108 LIST_FOREACH(fd, fc, fd_hash) {
109 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
41a01a4d 110 if (vget(fd->fd_vnode, NULL, 0, td))
984263bc
MD
111 goto loop;
112 *vpp = fd->fd_vnode;
113 return (error);
114 }
115 }
116
117 /*
118 * otherwise lock the array while we call getnewvnode
119 * since that can block.
120 */
121 if (fdcache_lock & FDL_LOCKED) {
122 fdcache_lock |= FDL_WANT;
377d4740 123 (void) tsleep((caddr_t) &fdcache_lock, 0, "fdalvp", 0);
984263bc
MD
124 goto loop;
125 }
126 fdcache_lock |= FDL_LOCKED;
127
128 /*
129 * Do the MALLOC before the getnewvnode since doing so afterward
130 * might cause a bogus v_data pointer to get dereferenced
131 * elsewhere if MALLOC should block.
132 */
133 MALLOC(fd, struct fdescnode *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
134
2d3e977e 135 error = getnewvnode(VT_FDESC, mp, fdesc_vnode_vops, vpp);
984263bc
MD
136 if (error) {
137 FREE(fd, M_TEMP);
138 goto out;
139 }
140 (*vpp)->v_data = fd;
141 fd->fd_vnode = *vpp;
142 fd->fd_type = ftype;
143 fd->fd_fd = -1;
144 fd->fd_ix = ix;
145 LIST_INSERT_HEAD(fc, fd, fd_hash);
146
147out:
148 fdcache_lock &= ~FDL_LOCKED;
149
150 if (fdcache_lock & FDL_WANT) {
151 fdcache_lock &= ~FDL_WANT;
152 wakeup((caddr_t) &fdcache_lock);
153 }
154
155 return (error);
156}
157
158/*
159 * vp is the current namei directory
160 * ndp is the name to locate in that directory...
130da09f
CP
161 *
162 * fdesc_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
163 * struct componentname *a_cnp)
984263bc
MD
164 */
165static int
130da09f 166fdesc_lookup(struct vop_lookup_args *ap)
984263bc 167{
dadab5e9
MD
168 struct componentname *cnp = ap->a_cnp;
169 struct thread *td = cnp->cn_td;
170 struct proc *p = td->td_proc;
984263bc
MD
171 struct vnode **vpp = ap->a_vpp;
172 struct vnode *dvp = ap->a_dvp;
984263bc 173 char *pname = cnp->cn_nameptr;
984263bc 174 int nlen = cnp->cn_namelen;
dadab5e9 175 int nfiles;
984263bc
MD
176 u_int fd;
177 int error;
178 struct vnode *fvp;
179
dadab5e9
MD
180 KKASSERT(p);
181 nfiles = p->p_fd->fd_nfiles;
2b69e610 182 if (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME) {
984263bc
MD
183 error = EROFS;
184 goto bad;
185 }
186
41a01a4d 187 VOP_UNLOCK(dvp, NULL, 0, td);
984263bc
MD
188 if (cnp->cn_namelen == 1 && *pname == '.') {
189 *vpp = dvp;
597aea93 190 vref(dvp);
41a01a4d 191 vn_lock(dvp, NULL, LK_SHARED | LK_RETRY, td);
984263bc
MD
192 return (0);
193 }
194
195 if (VTOFDESC(dvp)->fd_type != Froot) {
196 error = ENOTDIR;
197 goto bad;
198 }
199
200 fd = 0;
201 /* the only time a leading 0 is acceptable is if it's "0" */
202 if (*pname == '0' && nlen != 1) {
203 error = ENOENT;
204 goto bad;
205 }
206 while (nlen--) {
207 if (*pname < '0' || *pname > '9') {
208 error = ENOENT;
209 goto bad;
210 }
211 fd = 10 * fd + *pname++ - '0';
212 }
213
214 if (fd >= nfiles || p->p_fd->fd_ofiles[fd] == NULL) {
215 error = EBADF;
216 goto bad;
217 }
218
dadab5e9 219 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp, td);
984263bc
MD
220 if (error)
221 goto bad;
222 VTOFDESC(fvp)->fd_fd = fd;
41a01a4d 223 vn_lock(fvp, NULL, LK_SHARED | LK_RETRY, td);
984263bc
MD
224 *vpp = fvp;
225 return (0);
226
227bad:
41a01a4d 228 vn_lock(dvp, NULL, LK_SHARED | LK_RETRY, td);
984263bc
MD
229 *vpp = NULL;
230 return (error);
231}
232
130da09f
CP
233/*
234 * fdesc_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
235 * struct thread *a_td)
236 */
984263bc 237static int
130da09f 238fdesc_open(struct vop_open_args *ap)
984263bc
MD
239{
240 struct vnode *vp = ap->a_vp;
dadab5e9
MD
241 struct proc *p = ap->a_td->td_proc;
242
243 KKASSERT(p);
984263bc
MD
244
245 if (VTOFDESC(vp)->fd_type == Froot)
246 return (0);
247
248 /*
249 * XXX Kludge: set p->p_dupfd to contain the value of the the file
250 * descriptor being sought for duplication. The error return ensures
251 * that the vnode for this device will be released by vn_open. Open
252 * will detect this special error and take the actions in dupfdopen.
253 * Other callers of vn_open or VOP_OPEN will simply report the
254 * error.
255 */
dadab5e9 256 p->p_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
984263bc
MD
257 return (ENODEV);
258}
259
130da09f
CP
260/*
261 * fdesc_getattr(struct vnode *a_vp, struct vattr *a_vap,
262 * struct ucred *a_cred, struct thread *a_td)
263 */
984263bc 264static int
130da09f 265fdesc_getattr(struct vop_getattr_args *ap)
984263bc 266{
dadab5e9 267 struct proc *p = ap->a_td->td_proc;
984263bc
MD
268 struct vnode *vp = ap->a_vp;
269 struct vattr *vap = ap->a_vap;
dadab5e9 270 struct filedesc *fdp;
984263bc
MD
271 struct file *fp;
272 struct stat stb;
273 u_int fd;
274 int error = 0;
275
dadab5e9
MD
276 KKASSERT(p);
277 fdp = p->p_fd;
984263bc
MD
278 switch (VTOFDESC(vp)->fd_type) {
279 case Froot:
280 VATTR_NULL(vap);
281
282 vap->va_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
283 vap->va_type = VDIR;
284 vap->va_nlink = 2;
285 vap->va_size = DEV_BSIZE;
286 vap->va_fileid = VTOFDESC(vp)->fd_ix;
287 vap->va_uid = 0;
288 vap->va_gid = 0;
289 vap->va_blocksize = DEV_BSIZE;
290 vap->va_atime.tv_sec = boottime.tv_sec;
291 vap->va_atime.tv_nsec = 0;
292 vap->va_mtime = vap->va_atime;
293 vap->va_ctime = vap->va_mtime;
294 vap->va_gen = 0;
295 vap->va_flags = 0;
296 vap->va_rdev = 0;
297 vap->va_bytes = 0;
298 break;
299
300 case Fdesc:
301 fd = VTOFDESC(vp)->fd_fd;
302
303 if (fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
304 return (EBADF);
305
306 bzero(&stb, sizeof(stb));
dadab5e9 307 error = fo_stat(fp, &stb, ap->a_td);
984263bc
MD
308 if (error == 0) {
309 VATTR_NULL(vap);
310 vap->va_type = IFTOVT(stb.st_mode);
311 vap->va_mode = stb.st_mode;
312#define FDRX (VREAD|VEXEC)
313 if (vap->va_type == VDIR)
314 vap->va_mode &= ~((FDRX)|(FDRX>>3)|(FDRX>>6));
315#undef FDRX
316 vap->va_nlink = 1;
317 vap->va_flags = 0;
318 vap->va_bytes = stb.st_blocks * stb.st_blksize;
319 vap->va_fileid = VTOFDESC(vp)->fd_ix;
320 vap->va_size = stb.st_size;
321 vap->va_blocksize = stb.st_blksize;
322 vap->va_rdev = stb.st_rdev;
323
324 /*
325 * If no time data is provided, use the current time.
326 */
327 if (stb.st_atimespec.tv_sec == 0 &&
328 stb.st_atimespec.tv_nsec == 0)
329 nanotime(&stb.st_atimespec);
330
331 if (stb.st_ctimespec.tv_sec == 0 &&
332 stb.st_ctimespec.tv_nsec == 0)
333 nanotime(&stb.st_ctimespec);
334
335 if (stb.st_mtimespec.tv_sec == 0 &&
336 stb.st_mtimespec.tv_nsec == 0)
337 nanotime(&stb.st_mtimespec);
338
339 vap->va_atime = stb.st_atimespec;
340 vap->va_mtime = stb.st_mtimespec;
341 vap->va_ctime = stb.st_ctimespec;
342 vap->va_uid = stb.st_uid;
343 vap->va_gid = stb.st_gid;
344 }
345 break;
346
347 default:
348 panic("fdesc_getattr");
349 break;
350 }
351
352 if (error == 0)
353 vp->v_type = vap->va_type;
354 return (error);
355}
356
130da09f
CP
357/*
358 * fdesc_setattr(struct vnode *a_vp, struct vattr *a_vap,
359 * struct ucred *a_cred, struct thread *a_td)
360 */
984263bc 361static int
130da09f 362fdesc_setattr(struct vop_setattr_args *ap)
984263bc 363{
dadab5e9 364 struct proc *p = ap->a_td->td_proc;
984263bc
MD
365 struct vattr *vap = ap->a_vap;
366 struct file *fp;
367 unsigned fd;
368 int error;
369
370 /*
371 * Can't mess with the root vnode
372 */
373 if (VTOFDESC(ap->a_vp)->fd_type == Froot)
374 return (EACCES);
375
376 fd = VTOFDESC(ap->a_vp)->fd_fd;
dadab5e9 377 KKASSERT(p);
984263bc
MD
378
379 /*
380 * Allow setattr where there is an underlying vnode.
381 */
dadab5e9 382 error = getvnode(p->p_fd, fd, &fp);
984263bc
MD
383 if (error) {
384 /*
385 * getvnode() returns EINVAL if the file descriptor is not
386 * backed by a vnode. Silently drop all changes except
387 * chflags(2) in this case.
388 */
389 if (error == EINVAL) {
390 if (vap->va_flags != VNOVAL)
391 error = EOPNOTSUPP;
392 else
393 error = 0;
394 }
395 return (error);
396 }
397 return (error);
398}
399
400#define UIO_MX 16
401
130da09f
CP
402/*
403 * fdesc_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
404 * int *a_eofflag, u_long *a_cookies, int a_ncookies)
405 */
984263bc 406static int
130da09f 407fdesc_readdir(struct vop_readdir_args *ap)
984263bc
MD
408{
409 struct uio *uio = ap->a_uio;
410 struct filedesc *fdp;
411 struct dirent d;
412 struct dirent *dp = &d;
413 int error, i, off, fcnt;
414
415 /*
416 * We don't allow exporting fdesc mounts, and currently local
417 * requests do not need cookies.
418 */
419 if (ap->a_ncookies)
420 panic("fdesc_readdir: not hungry");
421
422 if (VTOFDESC(ap->a_vp)->fd_type != Froot)
423 panic("fdesc_readdir: not dir");
424
425 off = (int)uio->uio_offset;
426 if (off != uio->uio_offset || off < 0 || (u_int)off % UIO_MX != 0 ||
427 uio->uio_resid < UIO_MX)
428 return (EINVAL);
429 i = (u_int)off / UIO_MX;
dadab5e9
MD
430 KKASSERT(uio->uio_td->td_proc);
431 fdp = uio->uio_td->td_proc->p_fd;
984263bc
MD
432 error = 0;
433
434 fcnt = i - 2; /* The first two nodes are `.' and `..' */
435
436 while (i < fdp->fd_nfiles + 2 && uio->uio_resid >= UIO_MX) {
437 switch (i) {
438 case 0: /* `.' */
439 case 1: /* `..' */
440 bzero((caddr_t)dp, UIO_MX);
441
442 dp->d_fileno = i + FD_ROOT;
443 dp->d_namlen = i + 1;
444 dp->d_reclen = UIO_MX;
445 bcopy("..", dp->d_name, dp->d_namlen);
446 dp->d_name[i + 1] = '\0';
447 dp->d_type = DT_DIR;
448 break;
449 default:
450 if (fdp->fd_ofiles[fcnt] == NULL)
451 goto done;
452
453 bzero((caddr_t) dp, UIO_MX);
454 dp->d_namlen = sprintf(dp->d_name, "%d", fcnt);
455 dp->d_reclen = UIO_MX;
456 dp->d_type = DT_UNKNOWN;
457 dp->d_fileno = i + FD_DESC;
458 break;
459 }
460 /*
461 * And ship to userland
462 */
463 error = uiomove((caddr_t) dp, UIO_MX, uio);
464 if (error)
465 break;
466 i++;
467 fcnt++;
468 }
469
470done:
471 uio->uio_offset = i * UIO_MX;
472 return (error);
473}
474
130da09f
CP
475/*
476 * fdesc_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
477 * struct thread *a_td)
478 */
984263bc 479static int
130da09f 480fdesc_poll(struct vop_poll_args *ap)
984263bc 481{
dadab5e9 482 return seltrue(0, ap->a_events, ap->a_td);
984263bc
MD
483}
484
130da09f
CP
485/*
486 * fdesc_inactive(struct vnode *a_vp, struct thread *a_td)
487 */
984263bc 488static int
130da09f 489fdesc_inactive(struct vop_inactive_args *ap)
984263bc
MD
490{
491 struct vnode *vp = ap->a_vp;
492
493 /*
494 * Clear out the v_type field to avoid
495 * nasty things happening in vgone().
496 */
41a01a4d 497 VOP_UNLOCK(vp, NULL, 0, ap->a_td);
984263bc
MD
498 vp->v_type = VNON;
499 return (0);
500}
501
130da09f
CP
502/*
503 * fdesc_reclaim(struct vnode *a_vp)
504 */
984263bc 505static int
130da09f 506fdesc_reclaim(struct vop_reclaim_args *ap)
984263bc
MD
507{
508 struct vnode *vp = ap->a_vp;
509 struct fdescnode *fd = VTOFDESC(vp);
510
511 LIST_REMOVE(fd, fd_hash);
512 FREE(vp->v_data, M_TEMP);
513 vp->v_data = 0;
514
515 return (0);
516}
517
518/*
519 * Print out the contents of a /dev/fd vnode.
130da09f
CP
520 *
521 * fdesc_print(struct vnode *a_vp)
984263bc
MD
522 */
523/* ARGSUSED */
524static int
130da09f 525fdesc_print(struct vop_print_args *ap)
984263bc 526{
984263bc
MD
527 printf("tag VT_NON, fdesc vnode\n");
528 return (0);
529}
530
531static struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
2d3e977e
MD
532 { &vop_default_desc, (void *) vop_defaultop },
533 { &vop_access_desc, (void *) vop_null },
534 { &vop_getattr_desc, (void *) fdesc_getattr },
535 { &vop_inactive_desc, (void *) fdesc_inactive },
536 { &vop_lookup_desc, (void *) fdesc_lookup },
537 { &vop_open_desc, (void *) fdesc_open },
538 { &vop_pathconf_desc, (void *) vop_stdpathconf },
539 { &vop_poll_desc, (void *) fdesc_poll },
540 { &vop_print_desc, (void *) fdesc_print },
541 { &vop_readdir_desc, (void *) fdesc_readdir },
542 { &vop_reclaim_desc, (void *) fdesc_reclaim },
543 { &vop_setattr_desc, (void *) fdesc_setattr },
984263bc
MD
544 { NULL, NULL }
545};
546static struct vnodeopv_desc fdesc_vnodeop_opv_desc =
2d3e977e 547 { &fdesc_vnode_vops, fdesc_vnodeop_entries };
984263bc
MD
548
549VNODEOP_SET(fdesc_vnodeop_opv_desc);