2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)fdesc_vnops.c 8.9 (Berkeley) 1/21/94
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
45 #include <sys/dirent.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h> /* boottime */
49 #include <sys/mutex.h>
50 #include <sys/malloc.h>
51 #include <sys/file.h> /* Must come after sys/malloc.h */
52 #include <sys/mount.h>
53 #include <sys/namei.h>
56 #include <sys/vnode.h>
58 #include <fs/fdescfs/fdesc.h>
61 #define FD_NHASH(ix) \
62 (&fdhashtbl[(ix) & fdhash])
63 static LIST_HEAD(fdhashhead
, fdescnode
) *fdhashtbl
;
66 struct mtx fdesc_hashmtx
;
68 static vop_getattr_t fdesc_getattr
;
69 static vop_lookup_t fdesc_lookup
;
70 static vop_open_t fdesc_open
;
71 static vop_readdir_t fdesc_readdir
;
72 static vop_reclaim_t fdesc_reclaim
;
73 static vop_setattr_t fdesc_setattr
;
75 static struct vop_vector fdesc_vnodeops
= {
76 .vop_default
= &default_vnodeops
,
78 .vop_access
= VOP_NULL
,
79 .vop_getattr
= fdesc_getattr
,
80 .vop_lookup
= fdesc_lookup
,
81 .vop_open
= fdesc_open
,
82 .vop_pathconf
= vop_stdpathconf
,
83 .vop_readdir
= fdesc_readdir
,
84 .vop_reclaim
= fdesc_reclaim
,
85 .vop_setattr
= fdesc_setattr
,
88 static void fdesc_insmntque_dtr(struct vnode
*, void *);
89 static void fdesc_remove_entry(struct fdescnode
*);
92 * Initialise cache headers
95 fdesc_init(struct vfsconf
*vfsp
)
98 mtx_init(&fdesc_hashmtx
, "fdescfs_hash", NULL
, MTX_DEF
);
99 fdhashtbl
= hashinit(NFDCACHE
, M_CACHE
, &fdhash
);
104 * Uninit ready for unload.
107 fdesc_uninit(struct vfsconf
*vfsp
)
110 hashdestroy(fdhashtbl
, M_CACHE
, fdhash
);
111 mtx_destroy(&fdesc_hashmtx
);
116 * If allocating vnode fails, call this.
119 fdesc_insmntque_dtr(struct vnode
*vp
, void *arg
)
127 * Remove an entry from the hash if it exists.
130 fdesc_remove_entry(struct fdescnode
*fd
)
132 struct fdhashhead
*fc
;
133 struct fdescnode
*fd2
;
135 fc
= FD_NHASH(fd
->fd_ix
);
136 mtx_lock(&fdesc_hashmtx
);
137 LIST_FOREACH(fd2
, fc
, fd_hash
) {
139 LIST_REMOVE(fd
, fd_hash
);
143 mtx_unlock(&fdesc_hashmtx
);
147 fdesc_allocvp(fdntype ftype
, unsigned fd_fd
, int ix
, struct mount
*mp
,
150 struct fdescmount
*fmp
;
151 struct fdhashhead
*fc
;
152 struct fdescnode
*fd
, *fd2
;
153 struct vnode
*vp
, *vp2
;
160 mtx_lock(&fdesc_hashmtx
);
162 * If a forced unmount is progressing, we need to drop it. The flags are
163 * protected by the hashmtx.
165 fmp
= (struct fdescmount
*)mp
->mnt_data
;
166 if (fmp
== NULL
|| fmp
->flags
& FMNT_UNMOUNTF
) {
167 mtx_unlock(&fdesc_hashmtx
);
171 LIST_FOREACH(fd
, fc
, fd_hash
) {
172 if (fd
->fd_ix
== ix
&& fd
->fd_vnode
->v_mount
== mp
) {
173 /* Get reference to vnode in case it's being free'd */
176 mtx_unlock(&fdesc_hashmtx
);
177 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
, td
))
183 mtx_unlock(&fdesc_hashmtx
);
185 fd
= malloc(sizeof(struct fdescnode
), M_TEMP
, M_WAITOK
);
187 error
= getnewvnode("fdescfs", mp
, &fdesc_vnodeops
, &vp
);
192 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
198 error
= insmntque1(vp
, mp
, fdesc_insmntque_dtr
, NULL
);
204 /* Make sure that someone didn't beat us when inserting the vnode. */
205 mtx_lock(&fdesc_hashmtx
);
207 * If a forced unmount is progressing, we need to drop it. The flags are
208 * protected by the hashmtx.
210 fmp
= (struct fdescmount
*)mp
->mnt_data
;
211 if (fmp
== NULL
|| fmp
->flags
& FMNT_UNMOUNTF
) {
212 mtx_unlock(&fdesc_hashmtx
);
219 LIST_FOREACH(fd2
, fc
, fd_hash
) {
220 if (fd2
->fd_ix
== ix
&& fd2
->fd_vnode
->v_mount
== mp
) {
221 /* Get reference to vnode in case it's being free'd */
224 mtx_unlock(&fdesc_hashmtx
);
225 error
= vget(vp2
, LK_EXCLUSIVE
| LK_INTERLOCK
, td
);
226 /* Someone beat us, dec use count and wait for reclaim */
229 /* If we didn't get it, return no vnode. */
237 /* If we came here, we can insert it safely. */
238 LIST_INSERT_HEAD(fc
, fd
, fd_hash
);
239 mtx_unlock(&fdesc_hashmtx
);
244 struct fdesc_get_ino_args
{
253 fdesc_get_ino_alloc(struct mount
*mp
, void *arg
, int lkflags
,
256 struct fdesc_get_ino_args
*a
;
260 error
= fdesc_allocvp(a
->ftype
, a
->fd_fd
, a
->ix
, mp
, rvp
);
267 * vp is the current namei directory
268 * ndp is the name to locate in that directory...
271 fdesc_lookup(struct vop_lookup_args
*ap
)
273 struct vnode
**vpp
= ap
->a_vpp
;
274 struct vnode
*dvp
= ap
->a_dvp
;
275 struct componentname
*cnp
= ap
->a_cnp
;
276 char *pname
= cnp
->cn_nameptr
;
277 struct thread
*td
= cnp
->cn_thread
;
279 struct fdesc_get_ino_args arg
;
281 int nlen
= cnp
->cn_namelen
;
286 if ((cnp
->cn_flags
& ISLASTCN
) &&
287 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
)) {
292 if (cnp
->cn_namelen
== 1 && *pname
== '.') {
298 if (VTOFDESC(dvp
)->fd_type
!= Froot
) {
304 /* the only time a leading 0 is acceptable is if it's "0" */
305 if (*pname
== '0' && nlen
!= 1) {
310 if (*pname
< '0' || *pname
> '9') {
314 fd1
= 10 * fd
+ *pname
++ - '0';
323 * No rights to check since 'fp' isn't actually used.
325 if ((error
= fget(td
, fd
, cap_rights_init(&rights
), &fp
)) != 0)
328 /* Check if we're looking up ourselves. */
329 if (VTOFDESC(dvp
)->fd_ix
== FD_DESC
+ fd
) {
331 * In case we're holding the last reference to the file, the dvp
332 * will be re-acquired.
338 /* Re-aquire the lock afterwards. */
339 vn_lock(dvp
, LK_RETRY
| LK_EXCLUSIVE
);
342 if ((dvp
->v_iflag
& VI_DOOMED
) != 0)
346 * Unlock our root node (dvp) when doing this, since we might
347 * deadlock since the vnode might be locked by another thread
348 * and the root vnode lock will be obtained afterwards (in case
349 * we're looking up the fd of the root vnode), which will be the
350 * opposite lock order. Vhold the root vnode first so we don't
355 arg
.ix
= FD_DESC
+ fd
;
358 error
= vn_vget_ino_gen(dvp
, fdesc_get_ino_alloc
, &arg
,
373 fdesc_open(struct vop_open_args
*ap
)
375 struct vnode
*vp
= ap
->a_vp
;
377 if (VTOFDESC(vp
)->fd_type
== Froot
)
381 * XXX Kludge: set td->td_proc->p_dupfd to contain the value of the file
382 * descriptor being sought for duplication. The error return ensures
383 * that the vnode for this device will be released by vn_open. Open
384 * will detect this special error and take the actions in dupfdopen.
385 * Other callers of vn_open or VOP_OPEN will simply report the
388 ap
->a_td
->td_dupfd
= VTOFDESC(vp
)->fd_fd
; /* XXX */
393 fdesc_getattr(struct vop_getattr_args
*ap
)
395 struct vnode
*vp
= ap
->a_vp
;
396 struct vattr
*vap
= ap
->a_vap
;
398 vap
->va_mode
= S_IRUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
;
399 vap
->va_fileid
= VTOFDESC(vp
)->fd_ix
;
402 vap
->va_blocksize
= DEV_BSIZE
;
403 vap
->va_atime
.tv_sec
= boottime
.tv_sec
;
404 vap
->va_atime
.tv_nsec
= 0;
405 vap
->va_mtime
= vap
->va_atime
;
406 vap
->va_ctime
= vap
->va_mtime
;
412 switch (VTOFDESC(vp
)->fd_type
) {
416 vap
->va_size
= DEV_BSIZE
;
417 vap
->va_rdev
= NODEV
;
424 vap
->va_rdev
= makedev(0, vap
->va_fileid
);
428 panic("fdesc_getattr");
432 vp
->v_type
= vap
->va_type
;
437 fdesc_setattr(struct vop_setattr_args
*ap
)
439 struct vattr
*vap
= ap
->a_vap
;
443 struct thread
*td
= curthread
;
449 * Can't mess with the root vnode
451 if (VTOFDESC(ap
->a_vp
)->fd_type
== Froot
)
454 fd
= VTOFDESC(ap
->a_vp
)->fd_fd
;
457 * Allow setattr where there is an underlying vnode.
459 error
= getvnode(td
, fd
,
460 cap_rights_init(&rights
, CAP_EXTATTR_SET
), &fp
);
463 * getvnode() returns EINVAL if the file descriptor is not
464 * backed by a vnode. Silently drop all changes except
465 * chflags(2) in this case.
467 if (error
== EINVAL
) {
468 if (vap
->va_flags
!= VNOVAL
)
476 if ((error
= vn_start_write(vp
, &mp
, V_WAIT
| PCATCH
)) == 0) {
477 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
478 error
= VOP_SETATTR(vp
, ap
->a_vap
, ap
->a_cred
);
480 vn_finished_write(mp
);
489 fdesc_readdir(struct vop_readdir_args
*ap
)
491 struct uio
*uio
= ap
->a_uio
;
492 struct filedesc
*fdp
;
494 struct dirent
*dp
= &d
;
495 int error
, i
, off
, fcnt
;
497 if (VTOFDESC(ap
->a_vp
)->fd_type
!= Froot
)
498 panic("fdesc_readdir: not dir");
500 if (ap
->a_ncookies
!= NULL
)
503 off
= (int)uio
->uio_offset
;
504 if (off
!= uio
->uio_offset
|| off
< 0 || (u_int
)off
% UIO_MX
!= 0 ||
505 uio
->uio_resid
< UIO_MX
)
507 i
= (u_int
)off
/ UIO_MX
;
508 fdp
= uio
->uio_td
->td_proc
->p_fd
;
511 fcnt
= i
- 2; /* The first two nodes are `.' and `..' */
514 while (i
< fdp
->fd_nfiles
+ 2 && uio
->uio_resid
>= UIO_MX
) {
515 bzero((caddr_t
)dp
, UIO_MX
);
519 dp
->d_fileno
= i
+ FD_ROOT
;
520 dp
->d_namlen
= i
+ 1;
521 dp
->d_reclen
= UIO_MX
;
522 bcopy("..", dp
->d_name
, dp
->d_namlen
);
523 dp
->d_name
[i
+ 1] = '\0';
527 if (fdp
->fd_ofiles
[fcnt
].fde_file
== NULL
)
529 dp
->d_namlen
= sprintf(dp
->d_name
, "%d", fcnt
);
530 dp
->d_reclen
= UIO_MX
;
532 dp
->d_fileno
= i
+ FD_DESC
;
535 if (dp
->d_namlen
!= 0) {
537 * And ship to userland
539 FILEDESC_SUNLOCK(fdp
);
540 error
= uiomove(dp
, UIO_MX
, uio
);
548 FILEDESC_SUNLOCK(fdp
);
551 uio
->uio_offset
= i
* UIO_MX
;
556 fdesc_reclaim(struct vop_reclaim_args
*ap
)
559 struct fdescnode
*fd
;
563 fdesc_remove_entry(fd
);
564 free(vp
->v_data
, M_TEMP
);