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