vfs_syscalls.c revision 1.1.1.3 1 1.1.1.2 fvdl /*
2 1.1.1.2 fvdl * Copyright (c) 1989, 1993
3 1.1.1.2 fvdl * The Regents of the University of California. All rights reserved.
4 1.1.1.2 fvdl * (c) UNIX System Laboratories, Inc.
5 1.1.1.2 fvdl * All or some portions of this file are derived from material licensed
6 1.1.1.2 fvdl * to the University of California by American Telephone and Telegraph
7 1.1.1.2 fvdl * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.1.1.2 fvdl * the permission of UNIX System Laboratories, Inc.
9 1.1.1.2 fvdl *
10 1.1.1.2 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1.1.2 fvdl * modification, are permitted provided that the following conditions
12 1.1.1.2 fvdl * are met:
13 1.1.1.2 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1.1.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1.1.2 fvdl * documentation and/or other materials provided with the distribution.
18 1.1.1.2 fvdl * 3. All advertising materials mentioning features or use of this software
19 1.1.1.2 fvdl * must display the following acknowledgement:
20 1.1.1.2 fvdl * This product includes software developed by the University of
21 1.1.1.2 fvdl * California, Berkeley and its contributors.
22 1.1.1.2 fvdl * 4. Neither the name of the University nor the names of its contributors
23 1.1.1.2 fvdl * may be used to endorse or promote products derived from this software
24 1.1.1.2 fvdl * without specific prior written permission.
25 1.1.1.2 fvdl *
26 1.1.1.2 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1.1.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1.1.2 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1.1.2 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1.1.2 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1.1.2 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1.1.2 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1.1.2 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1.1.2 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1.1.2 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1.1.2 fvdl * SUCH DAMAGE.
37 1.1.1.2 fvdl *
38 1.1.1.3 fvdl * @(#)vfs_syscalls.c 8.41 (Berkeley) 6/15/95
39 1.1.1.2 fvdl */
40 1.1.1.2 fvdl
41 1.1.1.2 fvdl #include <sys/param.h>
42 1.1.1.2 fvdl #include <sys/systm.h>
43 1.1.1.2 fvdl #include <sys/namei.h>
44 1.1.1.2 fvdl #include <sys/filedesc.h>
45 1.1.1.2 fvdl #include <sys/kernel.h>
46 1.1.1.2 fvdl #include <sys/file.h>
47 1.1.1.2 fvdl #include <sys/stat.h>
48 1.1.1.2 fvdl #include <sys/vnode.h>
49 1.1.1.2 fvdl #include <sys/mount.h>
50 1.1.1.2 fvdl #include <sys/proc.h>
51 1.1.1.2 fvdl #include <sys/uio.h>
52 1.1.1.2 fvdl #include <sys/malloc.h>
53 1.1.1.2 fvdl #include <sys/dirent.h>
54 1.1.1.2 fvdl
55 1.1.1.3 fvdl #include <sys/syscallargs.h>
56 1.1.1.3 fvdl
57 1.1.1.2 fvdl #include <vm/vm.h>
58 1.1.1.2 fvdl #include <sys/sysctl.h>
59 1.1.1.2 fvdl
60 1.1.1.2 fvdl static int change_dir __P((struct nameidata *ndp, struct proc *p));
61 1.1.1.3 fvdl static void checkdirs __P((struct vnode *olddp));
62 1.1.1.2 fvdl
63 1.1.1.2 fvdl /*
64 1.1.1.2 fvdl * Virtual File System System Calls
65 1.1.1.2 fvdl */
66 1.1.1.2 fvdl
67 1.1.1.2 fvdl /*
68 1.1.1.2 fvdl * Mount a file system.
69 1.1.1.2 fvdl */
70 1.1.1.2 fvdl /* ARGSUSED */
71 1.1.1.3 fvdl int
72 1.1.1.2 fvdl mount(p, uap, retval)
73 1.1.1.2 fvdl struct proc *p;
74 1.1.1.3 fvdl register struct mount_args /* {
75 1.1.1.3 fvdl syscallarg(char *) type;
76 1.1.1.3 fvdl syscallarg(char *) path;
77 1.1.1.3 fvdl syscallarg(int) flags;
78 1.1.1.3 fvdl syscallarg(caddr_t) data;
79 1.1.1.3 fvdl } */ *uap;
80 1.1.1.3 fvdl register_t *retval;
81 1.1.1.2 fvdl {
82 1.1.1.3 fvdl struct vnode *vp;
83 1.1.1.3 fvdl struct mount *mp;
84 1.1.1.3 fvdl struct vfsconf *vfsp;
85 1.1.1.2 fvdl int error, flag;
86 1.1.1.3 fvdl struct vattr va;
87 1.1.1.3 fvdl u_long fstypenum;
88 1.1.1.2 fvdl struct nameidata nd;
89 1.1.1.3 fvdl char fstypename[MFSNAMELEN];
90 1.1.1.2 fvdl
91 1.1.1.2 fvdl /*
92 1.1.1.2 fvdl * Get vnode to be covered
93 1.1.1.2 fvdl */
94 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
95 1.1.1.3 fvdl SCARG(uap, path), p);
96 1.1.1.2 fvdl if (error = namei(&nd))
97 1.1.1.2 fvdl return (error);
98 1.1.1.2 fvdl vp = nd.ni_vp;
99 1.1.1.3 fvdl if (SCARG(uap, flags) & MNT_UPDATE) {
100 1.1.1.2 fvdl if ((vp->v_flag & VROOT) == 0) {
101 1.1.1.2 fvdl vput(vp);
102 1.1.1.2 fvdl return (EINVAL);
103 1.1.1.2 fvdl }
104 1.1.1.2 fvdl mp = vp->v_mount;
105 1.1.1.2 fvdl flag = mp->mnt_flag;
106 1.1.1.2 fvdl /*
107 1.1.1.2 fvdl * We only allow the filesystem to be reloaded if it
108 1.1.1.2 fvdl * is currently mounted read-only.
109 1.1.1.2 fvdl */
110 1.1.1.3 fvdl if ((SCARG(uap, flags) & MNT_RELOAD) &&
111 1.1.1.2 fvdl ((mp->mnt_flag & MNT_RDONLY) == 0)) {
112 1.1.1.2 fvdl vput(vp);
113 1.1.1.2 fvdl return (EOPNOTSUPP); /* Needs translation */
114 1.1.1.2 fvdl }
115 1.1.1.2 fvdl mp->mnt_flag |=
116 1.1.1.3 fvdl SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
117 1.1.1.3 fvdl /*
118 1.1.1.3 fvdl * Only root, or the user that did the original mount is
119 1.1.1.3 fvdl * permitted to update it.
120 1.1.1.3 fvdl */
121 1.1.1.3 fvdl if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
122 1.1.1.3 fvdl (error = suser(p->p_ucred, &p->p_acflag))) {
123 1.1.1.3 fvdl vput(vp);
124 1.1.1.3 fvdl return (error);
125 1.1.1.3 fvdl }
126 1.1.1.3 fvdl /*
127 1.1.1.3 fvdl * Do not allow NFS export by non-root users. Silently
128 1.1.1.3 fvdl * enforce MNT_NOSUID and MNT_NODEV for non-root users.
129 1.1.1.3 fvdl */
130 1.1.1.3 fvdl if (p->p_ucred->cr_uid != 0) {
131 1.1.1.3 fvdl if (SCARG(uap, flags) & MNT_EXPORTED) {
132 1.1.1.3 fvdl vput(vp);
133 1.1.1.3 fvdl return (EPERM);
134 1.1.1.3 fvdl }
135 1.1.1.3 fvdl SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
136 1.1.1.3 fvdl }
137 1.1.1.3 fvdl if (vfs_busy(mp, LK_NOWAIT, 0, p)) {
138 1.1.1.3 fvdl vput(vp);
139 1.1.1.3 fvdl return (EBUSY);
140 1.1.1.3 fvdl }
141 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
142 1.1.1.2 fvdl goto update;
143 1.1.1.2 fvdl }
144 1.1.1.3 fvdl /*
145 1.1.1.3 fvdl * If the user is not root, ensure that they own the directory
146 1.1.1.3 fvdl * onto which we are attempting to mount.
147 1.1.1.3 fvdl */
148 1.1.1.3 fvdl if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
149 1.1.1.3 fvdl (va.va_uid != p->p_ucred->cr_uid &&
150 1.1.1.3 fvdl (error = suser(p->p_ucred, &p->p_acflag)))) {
151 1.1.1.3 fvdl vput(vp);
152 1.1.1.3 fvdl return (error);
153 1.1.1.3 fvdl }
154 1.1.1.3 fvdl /*
155 1.1.1.3 fvdl * Do not allow NFS export by non-root users. Silently
156 1.1.1.3 fvdl * enforce MNT_NOSUID and MNT_NODEV for non-root users.
157 1.1.1.3 fvdl */
158 1.1.1.3 fvdl if (p->p_ucred->cr_uid != 0) {
159 1.1.1.3 fvdl if (SCARG(uap, flags) & MNT_EXPORTED) {
160 1.1.1.3 fvdl vput(vp);
161 1.1.1.3 fvdl return (EPERM);
162 1.1.1.3 fvdl }
163 1.1.1.3 fvdl SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
164 1.1.1.3 fvdl }
165 1.1.1.2 fvdl if (error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0))
166 1.1.1.2 fvdl return (error);
167 1.1.1.2 fvdl if (vp->v_type != VDIR) {
168 1.1.1.2 fvdl vput(vp);
169 1.1.1.2 fvdl return (ENOTDIR);
170 1.1.1.2 fvdl }
171 1.1.1.3 fvdl #ifdef COMPAT_43
172 1.1.1.2 fvdl /*
173 1.1.1.3 fvdl * Historically filesystem types were identified by number. If we
174 1.1.1.3 fvdl * get an integer for the filesystem type instead of a string, we
175 1.1.1.3 fvdl * check to see if it matches one of the historic filesystem types.
176 1.1.1.2 fvdl */
177 1.1.1.3 fvdl fstypenum = (u_long)SCARG(uap, type);
178 1.1.1.3 fvdl if (fstypenum < maxvfsconf) {
179 1.1.1.3 fvdl for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
180 1.1.1.3 fvdl if (vfsp->vfc_typenum == fstypenum)
181 1.1.1.3 fvdl break;
182 1.1.1.3 fvdl if (vfsp == NULL) {
183 1.1.1.3 fvdl vput(vp);
184 1.1.1.3 fvdl return (ENODEV);
185 1.1.1.3 fvdl }
186 1.1.1.3 fvdl strncpy(fstypename, vfsp->vfc_name, MFSNAMELEN);
187 1.1.1.3 fvdl } else
188 1.1.1.3 fvdl #endif /* COMPAT_43 */
189 1.1.1.3 fvdl if (error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL)) {
190 1.1.1.2 fvdl vput(vp);
191 1.1.1.2 fvdl return (error);
192 1.1.1.2 fvdl }
193 1.1.1.3 fvdl for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
194 1.1.1.3 fvdl if (!strcmp(vfsp->vfc_name, fstypename))
195 1.1.1.3 fvdl break;
196 1.1.1.3 fvdl if (vfsp == NULL) {
197 1.1.1.3 fvdl vput(vp);
198 1.1.1.3 fvdl return (ENODEV);
199 1.1.1.3 fvdl }
200 1.1.1.2 fvdl if (vp->v_mountedhere != NULL) {
201 1.1.1.2 fvdl vput(vp);
202 1.1.1.2 fvdl return (EBUSY);
203 1.1.1.2 fvdl }
204 1.1.1.3 fvdl
205 1.1.1.3 fvdl /*
206 1.1.1.3 fvdl * Allocate and initialize the filesystem.
207 1.1.1.3 fvdl */
208 1.1.1.3 fvdl mp = (struct mount *)malloc((u_long)sizeof(struct mount),
209 1.1.1.3 fvdl M_MOUNT, M_WAITOK);
210 1.1.1.3 fvdl bzero((char *)mp, (u_long)sizeof(struct mount));
211 1.1.1.3 fvdl lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
212 1.1.1.3 fvdl (void)vfs_busy(mp, LK_NOWAIT, 0, p);
213 1.1.1.3 fvdl mp->mnt_op = vfsp->vfc_vfsops;
214 1.1.1.3 fvdl mp->mnt_vfc = vfsp;
215 1.1.1.3 fvdl vfsp->vfc_refcount++;
216 1.1.1.3 fvdl mp->mnt_stat.f_type = vfsp->vfc_typenum;
217 1.1.1.3 fvdl mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
218 1.1.1.3 fvdl strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
219 1.1.1.2 fvdl vp->v_mountedhere = mp;
220 1.1.1.2 fvdl mp->mnt_vnodecovered = vp;
221 1.1.1.3 fvdl mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
222 1.1.1.2 fvdl update:
223 1.1.1.2 fvdl /*
224 1.1.1.2 fvdl * Set the mount level flags.
225 1.1.1.2 fvdl */
226 1.1.1.3 fvdl if (SCARG(uap, flags) & MNT_RDONLY)
227 1.1.1.2 fvdl mp->mnt_flag |= MNT_RDONLY;
228 1.1.1.2 fvdl else if (mp->mnt_flag & MNT_RDONLY)
229 1.1.1.2 fvdl mp->mnt_flag |= MNT_WANTRDWR;
230 1.1.1.2 fvdl mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
231 1.1.1.2 fvdl MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
232 1.1.1.3 fvdl mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
233 1.1.1.3 fvdl MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC);
234 1.1.1.2 fvdl /*
235 1.1.1.2 fvdl * Mount the filesystem.
236 1.1.1.2 fvdl */
237 1.1.1.3 fvdl error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
238 1.1.1.2 fvdl if (mp->mnt_flag & MNT_UPDATE) {
239 1.1.1.2 fvdl vrele(vp);
240 1.1.1.2 fvdl if (mp->mnt_flag & MNT_WANTRDWR)
241 1.1.1.2 fvdl mp->mnt_flag &= ~MNT_RDONLY;
242 1.1.1.2 fvdl mp->mnt_flag &=~
243 1.1.1.2 fvdl (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
244 1.1.1.2 fvdl if (error)
245 1.1.1.2 fvdl mp->mnt_flag = flag;
246 1.1.1.3 fvdl vfs_unbusy(mp, p);
247 1.1.1.2 fvdl return (error);
248 1.1.1.2 fvdl }
249 1.1.1.2 fvdl /*
250 1.1.1.2 fvdl * Put the new filesystem on the mount list after root.
251 1.1.1.2 fvdl */
252 1.1.1.2 fvdl cache_purge(vp);
253 1.1.1.2 fvdl if (!error) {
254 1.1.1.3 fvdl simple_lock(&mountlist_slock);
255 1.1.1.3 fvdl CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
256 1.1.1.3 fvdl simple_unlock(&mountlist_slock);
257 1.1.1.3 fvdl checkdirs(vp);
258 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
259 1.1.1.3 fvdl vfs_unbusy(mp, p);
260 1.1.1.3 fvdl if (error = VFS_START(mp, 0, p))
261 1.1.1.3 fvdl vrele(vp);
262 1.1.1.2 fvdl } else {
263 1.1.1.2 fvdl mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
264 1.1.1.3 fvdl mp->mnt_vfc->vfc_refcount--;
265 1.1.1.3 fvdl vfs_unbusy(mp, p);
266 1.1.1.2 fvdl free((caddr_t)mp, M_MOUNT);
267 1.1.1.2 fvdl vput(vp);
268 1.1.1.2 fvdl }
269 1.1.1.2 fvdl return (error);
270 1.1.1.2 fvdl }
271 1.1.1.2 fvdl
272 1.1.1.2 fvdl /*
273 1.1.1.3 fvdl * Scan all active processes to see if any of them have a current
274 1.1.1.3 fvdl * or root directory onto which the new filesystem has just been
275 1.1.1.3 fvdl * mounted. If so, replace them with the new mount point.
276 1.1.1.3 fvdl */
277 1.1.1.3 fvdl static void
278 1.1.1.3 fvdl checkdirs(olddp)
279 1.1.1.3 fvdl struct vnode *olddp;
280 1.1.1.3 fvdl {
281 1.1.1.3 fvdl struct filedesc *fdp;
282 1.1.1.3 fvdl struct vnode *newdp;
283 1.1.1.3 fvdl struct proc *p;
284 1.1.1.3 fvdl
285 1.1.1.3 fvdl if (olddp->v_usecount == 1)
286 1.1.1.3 fvdl return;
287 1.1.1.3 fvdl if (VFS_ROOT(olddp->v_mountedhere, &newdp))
288 1.1.1.3 fvdl panic("mount: lost mount");
289 1.1.1.3 fvdl for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
290 1.1.1.3 fvdl fdp = p->p_fd;
291 1.1.1.3 fvdl if (fdp->fd_cdir == olddp) {
292 1.1.1.3 fvdl vrele(fdp->fd_cdir);
293 1.1.1.3 fvdl VREF(newdp);
294 1.1.1.3 fvdl fdp->fd_cdir = newdp;
295 1.1.1.3 fvdl }
296 1.1.1.3 fvdl if (fdp->fd_rdir == olddp) {
297 1.1.1.3 fvdl vrele(fdp->fd_rdir);
298 1.1.1.3 fvdl VREF(newdp);
299 1.1.1.3 fvdl fdp->fd_rdir = newdp;
300 1.1.1.3 fvdl }
301 1.1.1.3 fvdl }
302 1.1.1.3 fvdl if (rootvnode == olddp) {
303 1.1.1.3 fvdl vrele(rootvnode);
304 1.1.1.3 fvdl VREF(newdp);
305 1.1.1.3 fvdl rootvnode = newdp;
306 1.1.1.3 fvdl }
307 1.1.1.3 fvdl vput(newdp);
308 1.1.1.3 fvdl }
309 1.1.1.3 fvdl
310 1.1.1.3 fvdl /*
311 1.1.1.2 fvdl * Unmount a file system.
312 1.1.1.2 fvdl *
313 1.1.1.2 fvdl * Note: unmount takes a path to the vnode mounted on as argument,
314 1.1.1.2 fvdl * not special file (as before).
315 1.1.1.2 fvdl */
316 1.1.1.2 fvdl /* ARGSUSED */
317 1.1.1.3 fvdl int
318 1.1.1.2 fvdl unmount(p, uap, retval)
319 1.1.1.2 fvdl struct proc *p;
320 1.1.1.3 fvdl register struct unmount_args /* {
321 1.1.1.3 fvdl syscallarg(char *) path;
322 1.1.1.3 fvdl syscallarg(int) flags;
323 1.1.1.3 fvdl } */ *uap;
324 1.1.1.3 fvdl register_t *retval;
325 1.1.1.2 fvdl {
326 1.1.1.2 fvdl register struct vnode *vp;
327 1.1.1.2 fvdl struct mount *mp;
328 1.1.1.2 fvdl int error;
329 1.1.1.2 fvdl struct nameidata nd;
330 1.1.1.2 fvdl
331 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
332 1.1.1.3 fvdl SCARG(uap, path), p);
333 1.1.1.2 fvdl if (error = namei(&nd))
334 1.1.1.2 fvdl return (error);
335 1.1.1.2 fvdl vp = nd.ni_vp;
336 1.1.1.3 fvdl mp = vp->v_mount;
337 1.1.1.2 fvdl
338 1.1.1.2 fvdl /*
339 1.1.1.3 fvdl * Only root, or the user that did the original mount is
340 1.1.1.3 fvdl * permitted to unmount this filesystem.
341 1.1.1.2 fvdl */
342 1.1.1.3 fvdl if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
343 1.1.1.2 fvdl (error = suser(p->p_ucred, &p->p_acflag))) {
344 1.1.1.2 fvdl vput(vp);
345 1.1.1.2 fvdl return (error);
346 1.1.1.2 fvdl }
347 1.1.1.2 fvdl
348 1.1.1.2 fvdl /*
349 1.1.1.3 fvdl * Don't allow unmounting the root file system.
350 1.1.1.3 fvdl */
351 1.1.1.3 fvdl if (mp->mnt_flag & MNT_ROOTFS) {
352 1.1.1.3 fvdl vput(vp);
353 1.1.1.3 fvdl return (EINVAL);
354 1.1.1.3 fvdl }
355 1.1.1.3 fvdl
356 1.1.1.3 fvdl /*
357 1.1.1.2 fvdl * Must be the root of the filesystem
358 1.1.1.2 fvdl */
359 1.1.1.2 fvdl if ((vp->v_flag & VROOT) == 0) {
360 1.1.1.2 fvdl vput(vp);
361 1.1.1.2 fvdl return (EINVAL);
362 1.1.1.2 fvdl }
363 1.1.1.2 fvdl vput(vp);
364 1.1.1.3 fvdl return (dounmount(mp, SCARG(uap, flags), p));
365 1.1.1.2 fvdl }
366 1.1.1.2 fvdl
367 1.1.1.2 fvdl /*
368 1.1.1.2 fvdl * Do the actual file system unmount.
369 1.1.1.2 fvdl */
370 1.1.1.3 fvdl int
371 1.1.1.2 fvdl dounmount(mp, flags, p)
372 1.1.1.2 fvdl register struct mount *mp;
373 1.1.1.2 fvdl int flags;
374 1.1.1.2 fvdl struct proc *p;
375 1.1.1.2 fvdl {
376 1.1.1.2 fvdl struct vnode *coveredvp;
377 1.1.1.2 fvdl int error;
378 1.1.1.2 fvdl
379 1.1.1.3 fvdl simple_lock(&mountlist_slock);
380 1.1.1.2 fvdl mp->mnt_flag |= MNT_UNMOUNT;
381 1.1.1.3 fvdl lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock, p);
382 1.1.1.2 fvdl mp->mnt_flag &=~ MNT_ASYNC;
383 1.1.1.2 fvdl vnode_pager_umount(mp); /* release cached vnodes */
384 1.1.1.2 fvdl cache_purgevfs(mp); /* remove cache entries for this file sys */
385 1.1.1.3 fvdl if (((mp->mnt_flag & MNT_RDONLY) ||
386 1.1.1.3 fvdl (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
387 1.1.1.2 fvdl (flags & MNT_FORCE))
388 1.1.1.2 fvdl error = VFS_UNMOUNT(mp, flags, p);
389 1.1.1.3 fvdl simple_lock(&mountlist_slock);
390 1.1.1.2 fvdl if (error) {
391 1.1.1.3 fvdl mp->mnt_flag &= ~MNT_UNMOUNT;
392 1.1.1.3 fvdl lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
393 1.1.1.3 fvdl &mountlist_slock, p);
394 1.1.1.3 fvdl return (error);
395 1.1.1.3 fvdl }
396 1.1.1.3 fvdl CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
397 1.1.1.3 fvdl if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
398 1.1.1.3 fvdl coveredvp->v_mountedhere = (struct mount *)0;
399 1.1.1.2 fvdl vrele(coveredvp);
400 1.1.1.2 fvdl }
401 1.1.1.3 fvdl mp->mnt_vfc->vfc_refcount--;
402 1.1.1.3 fvdl if (mp->mnt_vnodelist.lh_first != NULL)
403 1.1.1.3 fvdl panic("unmount: dangling vnode");
404 1.1.1.3 fvdl lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock, p);
405 1.1.1.3 fvdl if (mp->mnt_flag & MNT_MWAIT)
406 1.1.1.3 fvdl wakeup((caddr_t)mp);
407 1.1.1.3 fvdl free((caddr_t)mp, M_MOUNT);
408 1.1.1.3 fvdl return (0);
409 1.1.1.2 fvdl }
410 1.1.1.2 fvdl
411 1.1.1.2 fvdl /*
412 1.1.1.2 fvdl * Sync each mounted filesystem.
413 1.1.1.2 fvdl */
414 1.1.1.3 fvdl #ifdef DEBUG
415 1.1.1.2 fvdl int syncprt = 0;
416 1.1.1.2 fvdl struct ctldebug debug0 = { "syncprt", &syncprt };
417 1.1.1.2 fvdl #endif
418 1.1.1.2 fvdl
419 1.1.1.2 fvdl /* ARGSUSED */
420 1.1.1.3 fvdl int
421 1.1.1.2 fvdl sync(p, uap, retval)
422 1.1.1.2 fvdl struct proc *p;
423 1.1.1.3 fvdl void *uap;
424 1.1.1.3 fvdl register_t *retval;
425 1.1.1.2 fvdl {
426 1.1.1.2 fvdl register struct mount *mp, *nmp;
427 1.1.1.2 fvdl int asyncflag;
428 1.1.1.2 fvdl
429 1.1.1.3 fvdl simple_lock(&mountlist_slock);
430 1.1.1.3 fvdl for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
431 1.1.1.3 fvdl if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
432 1.1.1.3 fvdl nmp = mp->mnt_list.cqe_next;
433 1.1.1.3 fvdl continue;
434 1.1.1.3 fvdl }
435 1.1.1.3 fvdl if ((mp->mnt_flag & MNT_RDONLY) == 0) {
436 1.1.1.2 fvdl asyncflag = mp->mnt_flag & MNT_ASYNC;
437 1.1.1.2 fvdl mp->mnt_flag &= ~MNT_ASYNC;
438 1.1.1.2 fvdl VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
439 1.1.1.2 fvdl if (asyncflag)
440 1.1.1.2 fvdl mp->mnt_flag |= MNT_ASYNC;
441 1.1.1.2 fvdl }
442 1.1.1.3 fvdl simple_lock(&mountlist_slock);
443 1.1.1.3 fvdl nmp = mp->mnt_list.cqe_next;
444 1.1.1.3 fvdl vfs_unbusy(mp, p);
445 1.1.1.2 fvdl }
446 1.1.1.3 fvdl simple_unlock(&mountlist_slock);
447 1.1.1.2 fvdl #ifdef DIAGNOSTIC
448 1.1.1.2 fvdl if (syncprt)
449 1.1.1.2 fvdl vfs_bufstats();
450 1.1.1.2 fvdl #endif /* DIAGNOSTIC */
451 1.1.1.2 fvdl return (0);
452 1.1.1.2 fvdl }
453 1.1.1.2 fvdl
454 1.1.1.2 fvdl /*
455 1.1.1.2 fvdl * Change filesystem quotas.
456 1.1.1.2 fvdl */
457 1.1.1.2 fvdl /* ARGSUSED */
458 1.1.1.3 fvdl int
459 1.1.1.2 fvdl quotactl(p, uap, retval)
460 1.1.1.2 fvdl struct proc *p;
461 1.1.1.3 fvdl register struct quotactl_args /* {
462 1.1.1.3 fvdl syscallarg(char *) path;
463 1.1.1.3 fvdl syscallarg(int) cmd;
464 1.1.1.3 fvdl syscallarg(int) uid;
465 1.1.1.3 fvdl syscallarg(caddr_t) arg;
466 1.1.1.3 fvdl } */ *uap;
467 1.1.1.3 fvdl register_t *retval;
468 1.1.1.2 fvdl {
469 1.1.1.2 fvdl register struct mount *mp;
470 1.1.1.2 fvdl int error;
471 1.1.1.2 fvdl struct nameidata nd;
472 1.1.1.2 fvdl
473 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
474 1.1.1.2 fvdl if (error = namei(&nd))
475 1.1.1.2 fvdl return (error);
476 1.1.1.2 fvdl mp = nd.ni_vp->v_mount;
477 1.1.1.2 fvdl vrele(nd.ni_vp);
478 1.1.1.3 fvdl return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
479 1.1.1.3 fvdl SCARG(uap, arg), p));
480 1.1.1.2 fvdl }
481 1.1.1.2 fvdl
482 1.1.1.2 fvdl /*
483 1.1.1.2 fvdl * Get filesystem statistics.
484 1.1.1.2 fvdl */
485 1.1.1.2 fvdl /* ARGSUSED */
486 1.1.1.3 fvdl int
487 1.1.1.2 fvdl statfs(p, uap, retval)
488 1.1.1.2 fvdl struct proc *p;
489 1.1.1.3 fvdl register struct statfs_args /* {
490 1.1.1.3 fvdl syscallarg(char *) path;
491 1.1.1.3 fvdl syscallarg(struct statfs *) buf;
492 1.1.1.3 fvdl } */ *uap;
493 1.1.1.3 fvdl register_t *retval;
494 1.1.1.2 fvdl {
495 1.1.1.2 fvdl register struct mount *mp;
496 1.1.1.2 fvdl register struct statfs *sp;
497 1.1.1.2 fvdl int error;
498 1.1.1.2 fvdl struct nameidata nd;
499 1.1.1.2 fvdl
500 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
501 1.1.1.2 fvdl if (error = namei(&nd))
502 1.1.1.2 fvdl return (error);
503 1.1.1.2 fvdl mp = nd.ni_vp->v_mount;
504 1.1.1.2 fvdl sp = &mp->mnt_stat;
505 1.1.1.2 fvdl vrele(nd.ni_vp);
506 1.1.1.2 fvdl if (error = VFS_STATFS(mp, sp, p))
507 1.1.1.2 fvdl return (error);
508 1.1.1.2 fvdl sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
509 1.1.1.3 fvdl return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
510 1.1.1.2 fvdl }
511 1.1.1.2 fvdl
512 1.1.1.2 fvdl /*
513 1.1.1.2 fvdl * Get filesystem statistics.
514 1.1.1.2 fvdl */
515 1.1.1.2 fvdl /* ARGSUSED */
516 1.1.1.3 fvdl int
517 1.1.1.2 fvdl fstatfs(p, uap, retval)
518 1.1.1.2 fvdl struct proc *p;
519 1.1.1.3 fvdl register struct fstatfs_args /* {
520 1.1.1.3 fvdl syscallarg(int) fd;
521 1.1.1.3 fvdl syscallarg(struct statfs *) buf;
522 1.1.1.3 fvdl } */ *uap;
523 1.1.1.3 fvdl register_t *retval;
524 1.1.1.2 fvdl {
525 1.1.1.2 fvdl struct file *fp;
526 1.1.1.2 fvdl struct mount *mp;
527 1.1.1.2 fvdl register struct statfs *sp;
528 1.1.1.2 fvdl int error;
529 1.1.1.2 fvdl
530 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
531 1.1.1.2 fvdl return (error);
532 1.1.1.2 fvdl mp = ((struct vnode *)fp->f_data)->v_mount;
533 1.1.1.2 fvdl sp = &mp->mnt_stat;
534 1.1.1.2 fvdl if (error = VFS_STATFS(mp, sp, p))
535 1.1.1.2 fvdl return (error);
536 1.1.1.2 fvdl sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
537 1.1.1.3 fvdl return (copyout((caddr_t)sp, (caddr_t)SCARG(uap, buf), sizeof(*sp)));
538 1.1.1.2 fvdl }
539 1.1.1.2 fvdl
540 1.1.1.2 fvdl /*
541 1.1.1.2 fvdl * Get statistics on all filesystems.
542 1.1.1.2 fvdl */
543 1.1.1.3 fvdl int
544 1.1.1.2 fvdl getfsstat(p, uap, retval)
545 1.1.1.2 fvdl struct proc *p;
546 1.1.1.3 fvdl register struct getfsstat_args /* {
547 1.1.1.3 fvdl syscallarg(struct statfs *) buf;
548 1.1.1.3 fvdl syscallarg(long) bufsize;
549 1.1.1.3 fvdl syscallarg(int) flags;
550 1.1.1.3 fvdl } */ *uap;
551 1.1.1.3 fvdl register_t *retval;
552 1.1.1.2 fvdl {
553 1.1.1.2 fvdl register struct mount *mp, *nmp;
554 1.1.1.2 fvdl register struct statfs *sp;
555 1.1.1.2 fvdl caddr_t sfsp;
556 1.1.1.2 fvdl long count, maxcount, error;
557 1.1.1.2 fvdl
558 1.1.1.3 fvdl maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
559 1.1.1.3 fvdl sfsp = (caddr_t)SCARG(uap, buf);
560 1.1.1.3 fvdl count = 0;
561 1.1.1.3 fvdl simple_lock(&mountlist_slock);
562 1.1.1.3 fvdl for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
563 1.1.1.3 fvdl if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
564 1.1.1.3 fvdl nmp = mp->mnt_list.cqe_next;
565 1.1.1.3 fvdl continue;
566 1.1.1.3 fvdl }
567 1.1.1.3 fvdl if (sfsp && count < maxcount) {
568 1.1.1.2 fvdl sp = &mp->mnt_stat;
569 1.1.1.2 fvdl /*
570 1.1.1.2 fvdl * If MNT_NOWAIT is specified, do not refresh the
571 1.1.1.2 fvdl * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
572 1.1.1.2 fvdl */
573 1.1.1.3 fvdl if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
574 1.1.1.3 fvdl (SCARG(uap, flags) & MNT_WAIT)) &&
575 1.1.1.3 fvdl (error = VFS_STATFS(mp, sp, p))) {
576 1.1.1.3 fvdl simple_lock(&mountlist_slock);
577 1.1.1.3 fvdl nmp = mp->mnt_list.cqe_next;
578 1.1.1.3 fvdl vfs_unbusy(mp, p);
579 1.1.1.2 fvdl continue;
580 1.1.1.3 fvdl }
581 1.1.1.2 fvdl sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
582 1.1.1.2 fvdl if (error = copyout((caddr_t)sp, sfsp, sizeof(*sp)))
583 1.1.1.2 fvdl return (error);
584 1.1.1.2 fvdl sfsp += sizeof(*sp);
585 1.1.1.2 fvdl }
586 1.1.1.2 fvdl count++;
587 1.1.1.3 fvdl simple_lock(&mountlist_slock);
588 1.1.1.3 fvdl nmp = mp->mnt_list.cqe_next;
589 1.1.1.3 fvdl vfs_unbusy(mp, p);
590 1.1.1.2 fvdl }
591 1.1.1.3 fvdl simple_unlock(&mountlist_slock);
592 1.1.1.2 fvdl if (sfsp && count > maxcount)
593 1.1.1.2 fvdl *retval = maxcount;
594 1.1.1.2 fvdl else
595 1.1.1.2 fvdl *retval = count;
596 1.1.1.2 fvdl return (0);
597 1.1.1.2 fvdl }
598 1.1.1.2 fvdl
599 1.1.1.2 fvdl /*
600 1.1.1.2 fvdl * Change current working directory to a given file descriptor.
601 1.1.1.2 fvdl */
602 1.1.1.2 fvdl /* ARGSUSED */
603 1.1.1.3 fvdl int
604 1.1.1.2 fvdl fchdir(p, uap, retval)
605 1.1.1.2 fvdl struct proc *p;
606 1.1.1.3 fvdl struct fchdir_args /* {
607 1.1.1.3 fvdl syscallarg(int) fd;
608 1.1.1.3 fvdl } */ *uap;
609 1.1.1.3 fvdl register_t *retval;
610 1.1.1.2 fvdl {
611 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
612 1.1.1.3 fvdl struct vnode *vp, *tdp;
613 1.1.1.3 fvdl struct mount *mp;
614 1.1.1.2 fvdl struct file *fp;
615 1.1.1.2 fvdl int error;
616 1.1.1.2 fvdl
617 1.1.1.3 fvdl if (error = getvnode(fdp, SCARG(uap, fd), &fp))
618 1.1.1.2 fvdl return (error);
619 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
620 1.1.1.3 fvdl VREF(vp);
621 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
622 1.1.1.2 fvdl if (vp->v_type != VDIR)
623 1.1.1.2 fvdl error = ENOTDIR;
624 1.1.1.2 fvdl else
625 1.1.1.2 fvdl error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
626 1.1.1.3 fvdl while (!error && (mp = vp->v_mountedhere) != NULL) {
627 1.1.1.3 fvdl if (vfs_busy(mp, 0, 0, p))
628 1.1.1.3 fvdl continue;
629 1.1.1.3 fvdl error = VFS_ROOT(mp, &tdp);
630 1.1.1.3 fvdl vfs_unbusy(mp, p);
631 1.1.1.3 fvdl if (error)
632 1.1.1.3 fvdl break;
633 1.1.1.3 fvdl vput(vp);
634 1.1.1.3 fvdl vp = tdp;
635 1.1.1.3 fvdl }
636 1.1.1.3 fvdl if (error) {
637 1.1.1.3 fvdl vput(vp);
638 1.1.1.2 fvdl return (error);
639 1.1.1.3 fvdl }
640 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
641 1.1.1.2 fvdl vrele(fdp->fd_cdir);
642 1.1.1.2 fvdl fdp->fd_cdir = vp;
643 1.1.1.2 fvdl return (0);
644 1.1.1.2 fvdl }
645 1.1.1.2 fvdl
646 1.1.1.2 fvdl /*
647 1.1.1.2 fvdl * Change current working directory (``.'').
648 1.1.1.2 fvdl */
649 1.1.1.2 fvdl /* ARGSUSED */
650 1.1.1.3 fvdl int
651 1.1.1.2 fvdl chdir(p, uap, retval)
652 1.1.1.2 fvdl struct proc *p;
653 1.1.1.3 fvdl struct chdir_args /* {
654 1.1.1.3 fvdl syscallarg(char *) path;
655 1.1.1.3 fvdl } */ *uap;
656 1.1.1.3 fvdl register_t *retval;
657 1.1.1.2 fvdl {
658 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
659 1.1.1.2 fvdl int error;
660 1.1.1.2 fvdl struct nameidata nd;
661 1.1.1.2 fvdl
662 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
663 1.1.1.3 fvdl SCARG(uap, path), p);
664 1.1.1.2 fvdl if (error = change_dir(&nd, p))
665 1.1.1.2 fvdl return (error);
666 1.1.1.2 fvdl vrele(fdp->fd_cdir);
667 1.1.1.2 fvdl fdp->fd_cdir = nd.ni_vp;
668 1.1.1.2 fvdl return (0);
669 1.1.1.2 fvdl }
670 1.1.1.2 fvdl
671 1.1.1.2 fvdl /*
672 1.1.1.2 fvdl * Change notion of root (``/'') directory.
673 1.1.1.2 fvdl */
674 1.1.1.2 fvdl /* ARGSUSED */
675 1.1.1.3 fvdl int
676 1.1.1.2 fvdl chroot(p, uap, retval)
677 1.1.1.2 fvdl struct proc *p;
678 1.1.1.3 fvdl struct chroot_args /* {
679 1.1.1.3 fvdl syscallarg(char *) path;
680 1.1.1.3 fvdl } */ *uap;
681 1.1.1.3 fvdl register_t *retval;
682 1.1.1.2 fvdl {
683 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
684 1.1.1.2 fvdl int error;
685 1.1.1.2 fvdl struct nameidata nd;
686 1.1.1.2 fvdl
687 1.1.1.2 fvdl if (error = suser(p->p_ucred, &p->p_acflag))
688 1.1.1.2 fvdl return (error);
689 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
690 1.1.1.3 fvdl SCARG(uap, path), p);
691 1.1.1.2 fvdl if (error = change_dir(&nd, p))
692 1.1.1.2 fvdl return (error);
693 1.1.1.2 fvdl if (fdp->fd_rdir != NULL)
694 1.1.1.2 fvdl vrele(fdp->fd_rdir);
695 1.1.1.2 fvdl fdp->fd_rdir = nd.ni_vp;
696 1.1.1.2 fvdl return (0);
697 1.1.1.2 fvdl }
698 1.1.1.2 fvdl
699 1.1.1.2 fvdl /*
700 1.1.1.2 fvdl * Common routine for chroot and chdir.
701 1.1.1.2 fvdl */
702 1.1.1.2 fvdl static int
703 1.1.1.2 fvdl change_dir(ndp, p)
704 1.1.1.2 fvdl register struct nameidata *ndp;
705 1.1.1.2 fvdl struct proc *p;
706 1.1.1.2 fvdl {
707 1.1.1.2 fvdl struct vnode *vp;
708 1.1.1.2 fvdl int error;
709 1.1.1.2 fvdl
710 1.1.1.2 fvdl if (error = namei(ndp))
711 1.1.1.2 fvdl return (error);
712 1.1.1.2 fvdl vp = ndp->ni_vp;
713 1.1.1.2 fvdl if (vp->v_type != VDIR)
714 1.1.1.2 fvdl error = ENOTDIR;
715 1.1.1.2 fvdl else
716 1.1.1.2 fvdl error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
717 1.1.1.2 fvdl if (error)
718 1.1.1.3 fvdl vput(vp);
719 1.1.1.3 fvdl else
720 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
721 1.1.1.2 fvdl return (error);
722 1.1.1.2 fvdl }
723 1.1.1.2 fvdl
724 1.1.1.2 fvdl /*
725 1.1.1.2 fvdl * Check permissions, allocate an open file structure,
726 1.1.1.2 fvdl * and call the device open routine if any.
727 1.1.1.2 fvdl */
728 1.1.1.3 fvdl int
729 1.1.1.2 fvdl open(p, uap, retval)
730 1.1.1.2 fvdl struct proc *p;
731 1.1.1.3 fvdl register struct open_args /* {
732 1.1.1.3 fvdl syscallarg(char *) path;
733 1.1.1.3 fvdl syscallarg(int) flags;
734 1.1.1.3 fvdl syscallarg(int) mode;
735 1.1.1.3 fvdl } */ *uap;
736 1.1.1.3 fvdl register_t *retval;
737 1.1.1.2 fvdl {
738 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
739 1.1.1.2 fvdl register struct file *fp;
740 1.1.1.2 fvdl register struct vnode *vp;
741 1.1.1.2 fvdl int flags, cmode;
742 1.1.1.2 fvdl struct file *nfp;
743 1.1.1.2 fvdl int type, indx, error;
744 1.1.1.2 fvdl struct flock lf;
745 1.1.1.2 fvdl struct nameidata nd;
746 1.1.1.2 fvdl extern struct fileops vnops;
747 1.1.1.2 fvdl
748 1.1.1.2 fvdl if (error = falloc(p, &nfp, &indx))
749 1.1.1.2 fvdl return (error);
750 1.1.1.2 fvdl fp = nfp;
751 1.1.1.3 fvdl flags = FFLAGS(SCARG(uap, flags));
752 1.1.1.3 fvdl cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
753 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
754 1.1.1.2 fvdl p->p_dupfd = -indx - 1; /* XXX check for fdopen */
755 1.1.1.2 fvdl if (error = vn_open(&nd, flags, cmode)) {
756 1.1.1.2 fvdl ffree(fp);
757 1.1.1.2 fvdl if ((error == ENODEV || error == ENXIO) &&
758 1.1.1.3 fvdl p->p_dupfd >= 0 && /* XXX from fdopen */
759 1.1.1.2 fvdl (error =
760 1.1.1.3 fvdl dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
761 1.1.1.2 fvdl *retval = indx;
762 1.1.1.2 fvdl return (0);
763 1.1.1.2 fvdl }
764 1.1.1.2 fvdl if (error == ERESTART)
765 1.1.1.2 fvdl error = EINTR;
766 1.1.1.2 fvdl fdp->fd_ofiles[indx] = NULL;
767 1.1.1.2 fvdl return (error);
768 1.1.1.2 fvdl }
769 1.1.1.2 fvdl p->p_dupfd = 0;
770 1.1.1.2 fvdl vp = nd.ni_vp;
771 1.1.1.2 fvdl fp->f_flag = flags & FMASK;
772 1.1.1.2 fvdl fp->f_type = DTYPE_VNODE;
773 1.1.1.2 fvdl fp->f_ops = &vnops;
774 1.1.1.2 fvdl fp->f_data = (caddr_t)vp;
775 1.1.1.2 fvdl if (flags & (O_EXLOCK | O_SHLOCK)) {
776 1.1.1.2 fvdl lf.l_whence = SEEK_SET;
777 1.1.1.2 fvdl lf.l_start = 0;
778 1.1.1.2 fvdl lf.l_len = 0;
779 1.1.1.2 fvdl if (flags & O_EXLOCK)
780 1.1.1.2 fvdl lf.l_type = F_WRLCK;
781 1.1.1.2 fvdl else
782 1.1.1.2 fvdl lf.l_type = F_RDLCK;
783 1.1.1.2 fvdl type = F_FLOCK;
784 1.1.1.2 fvdl if ((flags & FNONBLOCK) == 0)
785 1.1.1.2 fvdl type |= F_WAIT;
786 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
787 1.1.1.2 fvdl if (error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) {
788 1.1.1.2 fvdl (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
789 1.1.1.2 fvdl ffree(fp);
790 1.1.1.2 fvdl fdp->fd_ofiles[indx] = NULL;
791 1.1.1.2 fvdl return (error);
792 1.1.1.2 fvdl }
793 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
794 1.1.1.2 fvdl fp->f_flag |= FHASLOCK;
795 1.1.1.2 fvdl }
796 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
797 1.1.1.2 fvdl *retval = indx;
798 1.1.1.2 fvdl return (0);
799 1.1.1.2 fvdl }
800 1.1.1.2 fvdl
801 1.1.1.2 fvdl #ifdef COMPAT_43
802 1.1.1.2 fvdl /*
803 1.1.1.2 fvdl * Create a file.
804 1.1.1.2 fvdl */
805 1.1.1.3 fvdl int
806 1.1.1.3 fvdl compat_43_creat(p, uap, retval)
807 1.1.1.3 fvdl struct proc *p;
808 1.1.1.3 fvdl register struct compat_43_creat_args /* {
809 1.1.1.3 fvdl syscallarg(char *) path;
810 1.1.1.3 fvdl syscallarg(int) mode;
811 1.1.1.3 fvdl } */ *uap;
812 1.1.1.3 fvdl register_t *retval;
813 1.1.1.3 fvdl {
814 1.1.1.3 fvdl struct open_args /* {
815 1.1.1.3 fvdl syscallarg(char *) path;
816 1.1.1.3 fvdl syscallarg(int) flags;
817 1.1.1.3 fvdl syscallarg(int) mode;
818 1.1.1.3 fvdl } */ nuap;
819 1.1.1.3 fvdl
820 1.1.1.3 fvdl SCARG(&nuap, path) = SCARG(uap, path);
821 1.1.1.3 fvdl SCARG(&nuap, mode) = SCARG(uap, mode);
822 1.1.1.3 fvdl SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
823 1.1.1.3 fvdl return (open(p, &nuap, retval));
824 1.1.1.2 fvdl }
825 1.1.1.2 fvdl #endif /* COMPAT_43 */
826 1.1.1.2 fvdl
827 1.1.1.2 fvdl /*
828 1.1.1.2 fvdl * Create a special file.
829 1.1.1.2 fvdl */
830 1.1.1.2 fvdl /* ARGSUSED */
831 1.1.1.3 fvdl int
832 1.1.1.2 fvdl mknod(p, uap, retval)
833 1.1.1.2 fvdl struct proc *p;
834 1.1.1.3 fvdl register struct mknod_args /* {
835 1.1.1.3 fvdl syscallarg(char *) path;
836 1.1.1.3 fvdl syscallarg(int) mode;
837 1.1.1.3 fvdl syscallarg(int) dev;
838 1.1.1.3 fvdl } */ *uap;
839 1.1.1.3 fvdl register_t *retval;
840 1.1.1.2 fvdl {
841 1.1.1.2 fvdl register struct vnode *vp;
842 1.1.1.2 fvdl struct vattr vattr;
843 1.1.1.2 fvdl int error;
844 1.1.1.3 fvdl int whiteout;
845 1.1.1.2 fvdl struct nameidata nd;
846 1.1.1.2 fvdl
847 1.1.1.2 fvdl if (error = suser(p->p_ucred, &p->p_acflag))
848 1.1.1.2 fvdl return (error);
849 1.1.1.3 fvdl NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
850 1.1.1.2 fvdl if (error = namei(&nd))
851 1.1.1.2 fvdl return (error);
852 1.1.1.2 fvdl vp = nd.ni_vp;
853 1.1.1.2 fvdl if (vp != NULL)
854 1.1.1.2 fvdl error = EEXIST;
855 1.1.1.2 fvdl else {
856 1.1.1.2 fvdl VATTR_NULL(&vattr);
857 1.1.1.3 fvdl vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
858 1.1.1.3 fvdl vattr.va_rdev = SCARG(uap, dev);
859 1.1.1.3 fvdl whiteout = 0;
860 1.1.1.2 fvdl
861 1.1.1.3 fvdl switch (SCARG(uap, mode) & S_IFMT) {
862 1.1.1.2 fvdl case S_IFMT: /* used by badsect to flag bad sectors */
863 1.1.1.2 fvdl vattr.va_type = VBAD;
864 1.1.1.2 fvdl break;
865 1.1.1.2 fvdl case S_IFCHR:
866 1.1.1.2 fvdl vattr.va_type = VCHR;
867 1.1.1.2 fvdl break;
868 1.1.1.2 fvdl case S_IFBLK:
869 1.1.1.2 fvdl vattr.va_type = VBLK;
870 1.1.1.2 fvdl break;
871 1.1.1.3 fvdl case S_IFWHT:
872 1.1.1.3 fvdl whiteout = 1;
873 1.1.1.3 fvdl break;
874 1.1.1.2 fvdl default:
875 1.1.1.2 fvdl error = EINVAL;
876 1.1.1.2 fvdl break;
877 1.1.1.2 fvdl }
878 1.1.1.2 fvdl }
879 1.1.1.2 fvdl if (!error) {
880 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
881 1.1.1.3 fvdl if (whiteout) {
882 1.1.1.3 fvdl error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
883 1.1.1.3 fvdl if (error)
884 1.1.1.3 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
885 1.1.1.3 fvdl vput(nd.ni_dvp);
886 1.1.1.3 fvdl } else {
887 1.1.1.3 fvdl error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
888 1.1.1.3 fvdl &nd.ni_cnd, &vattr);
889 1.1.1.3 fvdl }
890 1.1.1.2 fvdl } else {
891 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
892 1.1.1.2 fvdl if (nd.ni_dvp == vp)
893 1.1.1.2 fvdl vrele(nd.ni_dvp);
894 1.1.1.2 fvdl else
895 1.1.1.2 fvdl vput(nd.ni_dvp);
896 1.1.1.2 fvdl if (vp)
897 1.1.1.2 fvdl vrele(vp);
898 1.1.1.2 fvdl }
899 1.1.1.2 fvdl return (error);
900 1.1.1.2 fvdl }
901 1.1.1.2 fvdl
902 1.1.1.2 fvdl /*
903 1.1.1.3 fvdl * Create a named pipe.
904 1.1.1.2 fvdl */
905 1.1.1.2 fvdl /* ARGSUSED */
906 1.1.1.3 fvdl int
907 1.1.1.2 fvdl mkfifo(p, uap, retval)
908 1.1.1.2 fvdl struct proc *p;
909 1.1.1.3 fvdl register struct mkfifo_args /* {
910 1.1.1.3 fvdl syscallarg(char *) path;
911 1.1.1.3 fvdl syscallarg(int) mode;
912 1.1.1.3 fvdl } */ *uap;
913 1.1.1.3 fvdl register_t *retval;
914 1.1.1.2 fvdl {
915 1.1.1.2 fvdl struct vattr vattr;
916 1.1.1.2 fvdl int error;
917 1.1.1.2 fvdl struct nameidata nd;
918 1.1.1.2 fvdl
919 1.1.1.2 fvdl #ifndef FIFO
920 1.1.1.2 fvdl return (EOPNOTSUPP);
921 1.1.1.2 fvdl #else
922 1.1.1.3 fvdl NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
923 1.1.1.2 fvdl if (error = namei(&nd))
924 1.1.1.2 fvdl return (error);
925 1.1.1.2 fvdl if (nd.ni_vp != NULL) {
926 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
927 1.1.1.2 fvdl if (nd.ni_dvp == nd.ni_vp)
928 1.1.1.2 fvdl vrele(nd.ni_dvp);
929 1.1.1.2 fvdl else
930 1.1.1.2 fvdl vput(nd.ni_dvp);
931 1.1.1.2 fvdl vrele(nd.ni_vp);
932 1.1.1.2 fvdl return (EEXIST);
933 1.1.1.2 fvdl }
934 1.1.1.2 fvdl VATTR_NULL(&vattr);
935 1.1.1.2 fvdl vattr.va_type = VFIFO;
936 1.1.1.3 fvdl vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
937 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
938 1.1.1.2 fvdl return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
939 1.1.1.2 fvdl #endif /* FIFO */
940 1.1.1.2 fvdl }
941 1.1.1.2 fvdl
942 1.1.1.2 fvdl /*
943 1.1.1.2 fvdl * Make a hard file link.
944 1.1.1.2 fvdl */
945 1.1.1.2 fvdl /* ARGSUSED */
946 1.1.1.3 fvdl int
947 1.1.1.2 fvdl link(p, uap, retval)
948 1.1.1.2 fvdl struct proc *p;
949 1.1.1.3 fvdl register struct link_args /* {
950 1.1.1.3 fvdl syscallarg(char *) path;
951 1.1.1.3 fvdl syscallarg(char *) link;
952 1.1.1.3 fvdl } */ *uap;
953 1.1.1.3 fvdl register_t *retval;
954 1.1.1.2 fvdl {
955 1.1.1.2 fvdl register struct vnode *vp;
956 1.1.1.2 fvdl struct nameidata nd;
957 1.1.1.2 fvdl int error;
958 1.1.1.2 fvdl
959 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
960 1.1.1.2 fvdl if (error = namei(&nd))
961 1.1.1.2 fvdl return (error);
962 1.1.1.2 fvdl vp = nd.ni_vp;
963 1.1.1.2 fvdl if (vp->v_type != VDIR ||
964 1.1.1.2 fvdl (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
965 1.1.1.2 fvdl nd.ni_cnd.cn_nameiop = CREATE;
966 1.1.1.2 fvdl nd.ni_cnd.cn_flags = LOCKPARENT;
967 1.1.1.3 fvdl nd.ni_dirp = SCARG(uap, link);
968 1.1.1.2 fvdl if ((error = namei(&nd)) == 0) {
969 1.1.1.2 fvdl if (nd.ni_vp != NULL)
970 1.1.1.2 fvdl error = EEXIST;
971 1.1.1.2 fvdl if (!error) {
972 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred,
973 1.1.1.3 fvdl LEASE_WRITE);
974 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
975 1.1.1.3 fvdl error = VOP_LINK(vp, nd.ni_dvp, &nd.ni_cnd);
976 1.1.1.2 fvdl } else {
977 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
978 1.1.1.2 fvdl if (nd.ni_dvp == nd.ni_vp)
979 1.1.1.2 fvdl vrele(nd.ni_dvp);
980 1.1.1.2 fvdl else
981 1.1.1.2 fvdl vput(nd.ni_dvp);
982 1.1.1.2 fvdl if (nd.ni_vp)
983 1.1.1.2 fvdl vrele(nd.ni_vp);
984 1.1.1.2 fvdl }
985 1.1.1.2 fvdl }
986 1.1.1.2 fvdl }
987 1.1.1.2 fvdl vrele(vp);
988 1.1.1.2 fvdl return (error);
989 1.1.1.2 fvdl }
990 1.1.1.2 fvdl
991 1.1.1.2 fvdl /*
992 1.1.1.2 fvdl * Make a symbolic link.
993 1.1.1.2 fvdl */
994 1.1.1.2 fvdl /* ARGSUSED */
995 1.1.1.3 fvdl int
996 1.1.1.2 fvdl symlink(p, uap, retval)
997 1.1.1.2 fvdl struct proc *p;
998 1.1.1.3 fvdl register struct symlink_args /* {
999 1.1.1.3 fvdl syscallarg(char *) path;
1000 1.1.1.3 fvdl syscallarg(char *) link;
1001 1.1.1.3 fvdl } */ *uap;
1002 1.1.1.3 fvdl register_t *retval;
1003 1.1.1.2 fvdl {
1004 1.1.1.2 fvdl struct vattr vattr;
1005 1.1.1.2 fvdl char *path;
1006 1.1.1.2 fvdl int error;
1007 1.1.1.2 fvdl struct nameidata nd;
1008 1.1.1.2 fvdl
1009 1.1.1.2 fvdl MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1010 1.1.1.3 fvdl if (error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL))
1011 1.1.1.2 fvdl goto out;
1012 1.1.1.3 fvdl NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1013 1.1.1.2 fvdl if (error = namei(&nd))
1014 1.1.1.2 fvdl goto out;
1015 1.1.1.2 fvdl if (nd.ni_vp) {
1016 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1017 1.1.1.2 fvdl if (nd.ni_dvp == nd.ni_vp)
1018 1.1.1.2 fvdl vrele(nd.ni_dvp);
1019 1.1.1.2 fvdl else
1020 1.1.1.2 fvdl vput(nd.ni_dvp);
1021 1.1.1.2 fvdl vrele(nd.ni_vp);
1022 1.1.1.2 fvdl error = EEXIST;
1023 1.1.1.2 fvdl goto out;
1024 1.1.1.2 fvdl }
1025 1.1.1.2 fvdl VATTR_NULL(&vattr);
1026 1.1.1.2 fvdl vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1027 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1028 1.1.1.2 fvdl error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1029 1.1.1.2 fvdl out:
1030 1.1.1.2 fvdl FREE(path, M_NAMEI);
1031 1.1.1.2 fvdl return (error);
1032 1.1.1.2 fvdl }
1033 1.1.1.2 fvdl
1034 1.1.1.2 fvdl /*
1035 1.1.1.3 fvdl * Delete a whiteout from the filesystem.
1036 1.1.1.3 fvdl */
1037 1.1.1.3 fvdl /* ARGSUSED */
1038 1.1.1.3 fvdl int
1039 1.1.1.3 fvdl undelete(p, uap, retval)
1040 1.1.1.3 fvdl struct proc *p;
1041 1.1.1.3 fvdl register struct undelete_args /* {
1042 1.1.1.3 fvdl syscallarg(char *) path;
1043 1.1.1.3 fvdl } */ *uap;
1044 1.1.1.3 fvdl register_t *retval;
1045 1.1.1.3 fvdl {
1046 1.1.1.3 fvdl int error;
1047 1.1.1.3 fvdl struct nameidata nd;
1048 1.1.1.3 fvdl
1049 1.1.1.3 fvdl NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1050 1.1.1.3 fvdl SCARG(uap, path), p);
1051 1.1.1.3 fvdl error = namei(&nd);
1052 1.1.1.3 fvdl if (error)
1053 1.1.1.3 fvdl return (error);
1054 1.1.1.3 fvdl
1055 1.1.1.3 fvdl if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1056 1.1.1.3 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1057 1.1.1.3 fvdl if (nd.ni_dvp == nd.ni_vp)
1058 1.1.1.3 fvdl vrele(nd.ni_dvp);
1059 1.1.1.3 fvdl else
1060 1.1.1.3 fvdl vput(nd.ni_dvp);
1061 1.1.1.3 fvdl if (nd.ni_vp)
1062 1.1.1.3 fvdl vrele(nd.ni_vp);
1063 1.1.1.3 fvdl return (EEXIST);
1064 1.1.1.3 fvdl }
1065 1.1.1.3 fvdl
1066 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1067 1.1.1.3 fvdl if (error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE))
1068 1.1.1.3 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1069 1.1.1.3 fvdl vput(nd.ni_dvp);
1070 1.1.1.3 fvdl return (error);
1071 1.1.1.3 fvdl }
1072 1.1.1.3 fvdl
1073 1.1.1.3 fvdl /*
1074 1.1.1.2 fvdl * Delete a name from the filesystem.
1075 1.1.1.2 fvdl */
1076 1.1.1.2 fvdl /* ARGSUSED */
1077 1.1.1.3 fvdl int
1078 1.1.1.2 fvdl unlink(p, uap, retval)
1079 1.1.1.2 fvdl struct proc *p;
1080 1.1.1.3 fvdl struct unlink_args /* {
1081 1.1.1.3 fvdl syscallarg(char *) path;
1082 1.1.1.3 fvdl } */ *uap;
1083 1.1.1.3 fvdl register_t *retval;
1084 1.1.1.2 fvdl {
1085 1.1.1.2 fvdl register struct vnode *vp;
1086 1.1.1.2 fvdl int error;
1087 1.1.1.2 fvdl struct nameidata nd;
1088 1.1.1.2 fvdl
1089 1.1.1.3 fvdl NDINIT(&nd, DELETE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1090 1.1.1.2 fvdl if (error = namei(&nd))
1091 1.1.1.2 fvdl return (error);
1092 1.1.1.2 fvdl vp = nd.ni_vp;
1093 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1094 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1095 1.1.1.2 fvdl
1096 1.1.1.2 fvdl if (vp->v_type != VDIR ||
1097 1.1.1.2 fvdl (error = suser(p->p_ucred, &p->p_acflag)) == 0) {
1098 1.1.1.2 fvdl /*
1099 1.1.1.2 fvdl * The root of a mounted filesystem cannot be deleted.
1100 1.1.1.2 fvdl */
1101 1.1.1.2 fvdl if (vp->v_flag & VROOT)
1102 1.1.1.2 fvdl error = EBUSY;
1103 1.1.1.2 fvdl else
1104 1.1.1.2 fvdl (void)vnode_pager_uncache(vp);
1105 1.1.1.2 fvdl }
1106 1.1.1.2 fvdl
1107 1.1.1.2 fvdl if (!error) {
1108 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1109 1.1.1.2 fvdl error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1110 1.1.1.2 fvdl } else {
1111 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1112 1.1.1.2 fvdl if (nd.ni_dvp == vp)
1113 1.1.1.2 fvdl vrele(nd.ni_dvp);
1114 1.1.1.2 fvdl else
1115 1.1.1.2 fvdl vput(nd.ni_dvp);
1116 1.1.1.3 fvdl if (vp != NULLVP)
1117 1.1.1.3 fvdl vput(vp);
1118 1.1.1.2 fvdl }
1119 1.1.1.2 fvdl return (error);
1120 1.1.1.2 fvdl }
1121 1.1.1.2 fvdl
1122 1.1.1.2 fvdl /*
1123 1.1.1.2 fvdl * Reposition read/write file offset.
1124 1.1.1.2 fvdl */
1125 1.1.1.3 fvdl int
1126 1.1.1.2 fvdl lseek(p, uap, retval)
1127 1.1.1.2 fvdl struct proc *p;
1128 1.1.1.3 fvdl register struct lseek_args /* {
1129 1.1.1.3 fvdl syscallarg(int) fd;
1130 1.1.1.3 fvdl syscallarg(int) pad;
1131 1.1.1.3 fvdl syscallarg(off_t) offset;
1132 1.1.1.3 fvdl syscallarg(int) whence;
1133 1.1.1.3 fvdl } */ *uap;
1134 1.1.1.3 fvdl register_t *retval;
1135 1.1.1.2 fvdl {
1136 1.1.1.2 fvdl struct ucred *cred = p->p_ucred;
1137 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
1138 1.1.1.2 fvdl register struct file *fp;
1139 1.1.1.2 fvdl struct vattr vattr;
1140 1.1.1.2 fvdl int error;
1141 1.1.1.2 fvdl
1142 1.1.1.3 fvdl if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1143 1.1.1.3 fvdl (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1144 1.1.1.2 fvdl return (EBADF);
1145 1.1.1.2 fvdl if (fp->f_type != DTYPE_VNODE)
1146 1.1.1.2 fvdl return (ESPIPE);
1147 1.1.1.3 fvdl switch (SCARG(uap, whence)) {
1148 1.1.1.2 fvdl case L_INCR:
1149 1.1.1.3 fvdl fp->f_offset += SCARG(uap, offset);
1150 1.1.1.2 fvdl break;
1151 1.1.1.2 fvdl case L_XTND:
1152 1.1.1.2 fvdl if (error =
1153 1.1.1.2 fvdl VOP_GETATTR((struct vnode *)fp->f_data, &vattr, cred, p))
1154 1.1.1.2 fvdl return (error);
1155 1.1.1.3 fvdl fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1156 1.1.1.2 fvdl break;
1157 1.1.1.2 fvdl case L_SET:
1158 1.1.1.3 fvdl fp->f_offset = SCARG(uap, offset);
1159 1.1.1.2 fvdl break;
1160 1.1.1.2 fvdl default:
1161 1.1.1.2 fvdl return (EINVAL);
1162 1.1.1.2 fvdl }
1163 1.1.1.2 fvdl *(off_t *)retval = fp->f_offset;
1164 1.1.1.2 fvdl return (0);
1165 1.1.1.2 fvdl }
1166 1.1.1.2 fvdl
1167 1.1.1.2 fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1168 1.1.1.2 fvdl /*
1169 1.1.1.2 fvdl * Reposition read/write file offset.
1170 1.1.1.2 fvdl */
1171 1.1.1.3 fvdl int
1172 1.1.1.3 fvdl compat_43_lseek(p, uap, retval)
1173 1.1.1.2 fvdl struct proc *p;
1174 1.1.1.3 fvdl register struct compat_43_lseek_args /* {
1175 1.1.1.3 fvdl syscallarg(int) fd;
1176 1.1.1.3 fvdl syscallarg(long) offset;
1177 1.1.1.3 fvdl syscallarg(int) whence;
1178 1.1.1.3 fvdl } */ *uap;
1179 1.1.1.3 fvdl register_t *retval;
1180 1.1.1.3 fvdl {
1181 1.1.1.3 fvdl struct lseek_args /* {
1182 1.1.1.3 fvdl syscallarg(int) fd;
1183 1.1.1.3 fvdl syscallarg(int) pad;
1184 1.1.1.3 fvdl syscallarg(off_t) offset;
1185 1.1.1.3 fvdl syscallarg(int) whence;
1186 1.1.1.3 fvdl } */ nuap;
1187 1.1.1.2 fvdl off_t qret;
1188 1.1.1.2 fvdl int error;
1189 1.1.1.2 fvdl
1190 1.1.1.3 fvdl SCARG(&nuap, fd) = SCARG(uap, fd);
1191 1.1.1.3 fvdl SCARG(&nuap, offset) = SCARG(uap, offset);
1192 1.1.1.3 fvdl SCARG(&nuap, whence) = SCARG(uap, whence);
1193 1.1.1.2 fvdl error = lseek(p, &nuap, &qret);
1194 1.1.1.2 fvdl *(long *)retval = qret;
1195 1.1.1.2 fvdl return (error);
1196 1.1.1.2 fvdl }
1197 1.1.1.2 fvdl #endif /* COMPAT_43 */
1198 1.1.1.2 fvdl
1199 1.1.1.2 fvdl /*
1200 1.1.1.2 fvdl * Check access permissions.
1201 1.1.1.2 fvdl */
1202 1.1.1.3 fvdl int
1203 1.1.1.2 fvdl access(p, uap, retval)
1204 1.1.1.2 fvdl struct proc *p;
1205 1.1.1.3 fvdl register struct access_args /* {
1206 1.1.1.3 fvdl syscallarg(char *) path;
1207 1.1.1.3 fvdl syscallarg(int) flags;
1208 1.1.1.3 fvdl } */ *uap;
1209 1.1.1.3 fvdl register_t *retval;
1210 1.1.1.2 fvdl {
1211 1.1.1.2 fvdl register struct ucred *cred = p->p_ucred;
1212 1.1.1.2 fvdl register struct vnode *vp;
1213 1.1.1.2 fvdl int error, flags, t_gid, t_uid;
1214 1.1.1.2 fvdl struct nameidata nd;
1215 1.1.1.2 fvdl
1216 1.1.1.2 fvdl t_uid = cred->cr_uid;
1217 1.1.1.2 fvdl t_gid = cred->cr_groups[0];
1218 1.1.1.2 fvdl cred->cr_uid = p->p_cred->p_ruid;
1219 1.1.1.2 fvdl cred->cr_groups[0] = p->p_cred->p_rgid;
1220 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1221 1.1.1.3 fvdl SCARG(uap, path), p);
1222 1.1.1.2 fvdl if (error = namei(&nd))
1223 1.1.1.2 fvdl goto out1;
1224 1.1.1.2 fvdl vp = nd.ni_vp;
1225 1.1.1.2 fvdl
1226 1.1.1.2 fvdl /* Flags == 0 means only check for existence. */
1227 1.1.1.3 fvdl if (SCARG(uap, flags)) {
1228 1.1.1.2 fvdl flags = 0;
1229 1.1.1.3 fvdl if (SCARG(uap, flags) & R_OK)
1230 1.1.1.2 fvdl flags |= VREAD;
1231 1.1.1.3 fvdl if (SCARG(uap, flags) & W_OK)
1232 1.1.1.2 fvdl flags |= VWRITE;
1233 1.1.1.3 fvdl if (SCARG(uap, flags) & X_OK)
1234 1.1.1.2 fvdl flags |= VEXEC;
1235 1.1.1.2 fvdl if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1236 1.1.1.2 fvdl error = VOP_ACCESS(vp, flags, cred, p);
1237 1.1.1.2 fvdl }
1238 1.1.1.2 fvdl vput(vp);
1239 1.1.1.2 fvdl out1:
1240 1.1.1.2 fvdl cred->cr_uid = t_uid;
1241 1.1.1.2 fvdl cred->cr_groups[0] = t_gid;
1242 1.1.1.2 fvdl return (error);
1243 1.1.1.2 fvdl }
1244 1.1.1.2 fvdl
1245 1.1.1.2 fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1246 1.1.1.2 fvdl /*
1247 1.1.1.2 fvdl * Get file status; this version follows links.
1248 1.1.1.2 fvdl */
1249 1.1.1.2 fvdl /* ARGSUSED */
1250 1.1.1.3 fvdl int
1251 1.1.1.3 fvdl compat_43_stat(p, uap, retval)
1252 1.1.1.2 fvdl struct proc *p;
1253 1.1.1.3 fvdl register struct compat_43_stat_args /* {
1254 1.1.1.3 fvdl syscallarg(char *) path;
1255 1.1.1.3 fvdl syscallarg(struct ostat *) ub;
1256 1.1.1.3 fvdl } */ *uap;
1257 1.1.1.3 fvdl register_t *retval;
1258 1.1.1.2 fvdl {
1259 1.1.1.2 fvdl struct stat sb;
1260 1.1.1.2 fvdl struct ostat osb;
1261 1.1.1.2 fvdl int error;
1262 1.1.1.2 fvdl struct nameidata nd;
1263 1.1.1.2 fvdl
1264 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1265 1.1.1.3 fvdl SCARG(uap, path), p);
1266 1.1.1.2 fvdl if (error = namei(&nd))
1267 1.1.1.2 fvdl return (error);
1268 1.1.1.2 fvdl error = vn_stat(nd.ni_vp, &sb, p);
1269 1.1.1.2 fvdl vput(nd.ni_vp);
1270 1.1.1.2 fvdl if (error)
1271 1.1.1.2 fvdl return (error);
1272 1.1.1.2 fvdl cvtstat(&sb, &osb);
1273 1.1.1.3 fvdl error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1274 1.1.1.2 fvdl return (error);
1275 1.1.1.2 fvdl }
1276 1.1.1.2 fvdl
1277 1.1.1.2 fvdl /*
1278 1.1.1.2 fvdl * Get file status; this version does not follow links.
1279 1.1.1.2 fvdl */
1280 1.1.1.2 fvdl /* ARGSUSED */
1281 1.1.1.3 fvdl int
1282 1.1.1.3 fvdl compat_43_lstat(p, uap, retval)
1283 1.1.1.2 fvdl struct proc *p;
1284 1.1.1.3 fvdl register struct compat_43_lstat_args /* {
1285 1.1.1.3 fvdl syscallarg(char *) path;
1286 1.1.1.3 fvdl syscallarg(struct ostat *) ub;
1287 1.1.1.3 fvdl } */ *uap;
1288 1.1.1.3 fvdl register_t *retval;
1289 1.1.1.2 fvdl {
1290 1.1.1.3 fvdl struct vnode *vp, *dvp;
1291 1.1.1.3 fvdl struct stat sb, sb1;
1292 1.1.1.2 fvdl struct ostat osb;
1293 1.1.1.2 fvdl int error;
1294 1.1.1.2 fvdl struct nameidata nd;
1295 1.1.1.2 fvdl
1296 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1297 1.1.1.3 fvdl SCARG(uap, path), p);
1298 1.1.1.2 fvdl if (error = namei(&nd))
1299 1.1.1.2 fvdl return (error);
1300 1.1.1.3 fvdl /*
1301 1.1.1.3 fvdl * For symbolic links, always return the attributes of its
1302 1.1.1.3 fvdl * containing directory, except for mode, size, and links.
1303 1.1.1.3 fvdl */
1304 1.1.1.3 fvdl vp = nd.ni_vp;
1305 1.1.1.3 fvdl dvp = nd.ni_dvp;
1306 1.1.1.3 fvdl if (vp->v_type != VLNK) {
1307 1.1.1.3 fvdl if (dvp == vp)
1308 1.1.1.3 fvdl vrele(dvp);
1309 1.1.1.3 fvdl else
1310 1.1.1.3 fvdl vput(dvp);
1311 1.1.1.3 fvdl error = vn_stat(vp, &sb, p);
1312 1.1.1.3 fvdl vput(vp);
1313 1.1.1.3 fvdl if (error)
1314 1.1.1.3 fvdl return (error);
1315 1.1.1.3 fvdl } else {
1316 1.1.1.3 fvdl error = vn_stat(dvp, &sb, p);
1317 1.1.1.3 fvdl vput(dvp);
1318 1.1.1.3 fvdl if (error) {
1319 1.1.1.3 fvdl vput(vp);
1320 1.1.1.3 fvdl return (error);
1321 1.1.1.3 fvdl }
1322 1.1.1.3 fvdl error = vn_stat(vp, &sb1, p);
1323 1.1.1.3 fvdl vput(vp);
1324 1.1.1.3 fvdl if (error)
1325 1.1.1.3 fvdl return (error);
1326 1.1.1.3 fvdl sb.st_mode &= ~S_IFDIR;
1327 1.1.1.3 fvdl sb.st_mode |= S_IFLNK;
1328 1.1.1.3 fvdl sb.st_nlink = sb1.st_nlink;
1329 1.1.1.3 fvdl sb.st_size = sb1.st_size;
1330 1.1.1.3 fvdl sb.st_blocks = sb1.st_blocks;
1331 1.1.1.3 fvdl }
1332 1.1.1.2 fvdl cvtstat(&sb, &osb);
1333 1.1.1.3 fvdl error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
1334 1.1.1.2 fvdl return (error);
1335 1.1.1.2 fvdl }
1336 1.1.1.2 fvdl
1337 1.1.1.2 fvdl /*
1338 1.1.1.2 fvdl * Convert from an old to a new stat structure.
1339 1.1.1.2 fvdl */
1340 1.1.1.3 fvdl void
1341 1.1.1.2 fvdl cvtstat(st, ost)
1342 1.1.1.2 fvdl struct stat *st;
1343 1.1.1.2 fvdl struct ostat *ost;
1344 1.1.1.2 fvdl {
1345 1.1.1.2 fvdl
1346 1.1.1.2 fvdl ost->st_dev = st->st_dev;
1347 1.1.1.2 fvdl ost->st_ino = st->st_ino;
1348 1.1.1.2 fvdl ost->st_mode = st->st_mode;
1349 1.1.1.2 fvdl ost->st_nlink = st->st_nlink;
1350 1.1.1.2 fvdl ost->st_uid = st->st_uid;
1351 1.1.1.2 fvdl ost->st_gid = st->st_gid;
1352 1.1.1.2 fvdl ost->st_rdev = st->st_rdev;
1353 1.1.1.2 fvdl if (st->st_size < (quad_t)1 << 32)
1354 1.1.1.2 fvdl ost->st_size = st->st_size;
1355 1.1.1.2 fvdl else
1356 1.1.1.2 fvdl ost->st_size = -2;
1357 1.1.1.2 fvdl ost->st_atime = st->st_atime;
1358 1.1.1.2 fvdl ost->st_mtime = st->st_mtime;
1359 1.1.1.2 fvdl ost->st_ctime = st->st_ctime;
1360 1.1.1.2 fvdl ost->st_blksize = st->st_blksize;
1361 1.1.1.2 fvdl ost->st_blocks = st->st_blocks;
1362 1.1.1.2 fvdl ost->st_flags = st->st_flags;
1363 1.1.1.2 fvdl ost->st_gen = st->st_gen;
1364 1.1.1.2 fvdl }
1365 1.1.1.2 fvdl #endif /* COMPAT_43 || COMPAT_SUNOS */
1366 1.1.1.2 fvdl
1367 1.1.1.2 fvdl /*
1368 1.1.1.2 fvdl * Get file status; this version follows links.
1369 1.1.1.2 fvdl */
1370 1.1.1.2 fvdl /* ARGSUSED */
1371 1.1.1.3 fvdl int
1372 1.1.1.2 fvdl stat(p, uap, retval)
1373 1.1.1.2 fvdl struct proc *p;
1374 1.1.1.3 fvdl register struct stat_args /* {
1375 1.1.1.3 fvdl syscallarg(char *) path;
1376 1.1.1.3 fvdl syscallarg(struct stat *) ub;
1377 1.1.1.3 fvdl } */ *uap;
1378 1.1.1.3 fvdl register_t *retval;
1379 1.1.1.2 fvdl {
1380 1.1.1.2 fvdl struct stat sb;
1381 1.1.1.2 fvdl int error;
1382 1.1.1.2 fvdl struct nameidata nd;
1383 1.1.1.2 fvdl
1384 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1385 1.1.1.3 fvdl SCARG(uap, path), p);
1386 1.1.1.2 fvdl if (error = namei(&nd))
1387 1.1.1.2 fvdl return (error);
1388 1.1.1.2 fvdl error = vn_stat(nd.ni_vp, &sb, p);
1389 1.1.1.2 fvdl vput(nd.ni_vp);
1390 1.1.1.2 fvdl if (error)
1391 1.1.1.2 fvdl return (error);
1392 1.1.1.3 fvdl error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1393 1.1.1.2 fvdl return (error);
1394 1.1.1.2 fvdl }
1395 1.1.1.2 fvdl
1396 1.1.1.2 fvdl /*
1397 1.1.1.2 fvdl * Get file status; this version does not follow links.
1398 1.1.1.2 fvdl */
1399 1.1.1.2 fvdl /* ARGSUSED */
1400 1.1.1.3 fvdl int
1401 1.1.1.2 fvdl lstat(p, uap, retval)
1402 1.1.1.2 fvdl struct proc *p;
1403 1.1.1.3 fvdl register struct lstat_args /* {
1404 1.1.1.3 fvdl syscallarg(char *) path;
1405 1.1.1.3 fvdl syscallarg(struct stat *) ub;
1406 1.1.1.3 fvdl } */ *uap;
1407 1.1.1.3 fvdl register_t *retval;
1408 1.1.1.2 fvdl {
1409 1.1.1.2 fvdl int error;
1410 1.1.1.2 fvdl struct vnode *vp, *dvp;
1411 1.1.1.2 fvdl struct stat sb, sb1;
1412 1.1.1.2 fvdl struct nameidata nd;
1413 1.1.1.2 fvdl
1414 1.1.1.2 fvdl NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKPARENT, UIO_USERSPACE,
1415 1.1.1.3 fvdl SCARG(uap, path), p);
1416 1.1.1.2 fvdl if (error = namei(&nd))
1417 1.1.1.2 fvdl return (error);
1418 1.1.1.2 fvdl /*
1419 1.1.1.3 fvdl * For symbolic links, always return the attributes of its containing
1420 1.1.1.3 fvdl * directory, except for mode, size, inode number, and links.
1421 1.1.1.2 fvdl */
1422 1.1.1.2 fvdl vp = nd.ni_vp;
1423 1.1.1.2 fvdl dvp = nd.ni_dvp;
1424 1.1.1.2 fvdl if (vp->v_type != VLNK) {
1425 1.1.1.2 fvdl if (dvp == vp)
1426 1.1.1.2 fvdl vrele(dvp);
1427 1.1.1.2 fvdl else
1428 1.1.1.2 fvdl vput(dvp);
1429 1.1.1.2 fvdl error = vn_stat(vp, &sb, p);
1430 1.1.1.2 fvdl vput(vp);
1431 1.1.1.2 fvdl if (error)
1432 1.1.1.2 fvdl return (error);
1433 1.1.1.2 fvdl } else {
1434 1.1.1.2 fvdl error = vn_stat(dvp, &sb, p);
1435 1.1.1.2 fvdl vput(dvp);
1436 1.1.1.2 fvdl if (error) {
1437 1.1.1.2 fvdl vput(vp);
1438 1.1.1.2 fvdl return (error);
1439 1.1.1.2 fvdl }
1440 1.1.1.2 fvdl error = vn_stat(vp, &sb1, p);
1441 1.1.1.2 fvdl vput(vp);
1442 1.1.1.2 fvdl if (error)
1443 1.1.1.2 fvdl return (error);
1444 1.1.1.2 fvdl sb.st_mode &= ~S_IFDIR;
1445 1.1.1.2 fvdl sb.st_mode |= S_IFLNK;
1446 1.1.1.2 fvdl sb.st_nlink = sb1.st_nlink;
1447 1.1.1.2 fvdl sb.st_size = sb1.st_size;
1448 1.1.1.2 fvdl sb.st_blocks = sb1.st_blocks;
1449 1.1.1.3 fvdl sb.st_ino = sb1.st_ino;
1450 1.1.1.2 fvdl }
1451 1.1.1.3 fvdl error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1452 1.1.1.2 fvdl return (error);
1453 1.1.1.2 fvdl }
1454 1.1.1.2 fvdl
1455 1.1.1.2 fvdl /*
1456 1.1.1.2 fvdl * Get configurable pathname variables.
1457 1.1.1.2 fvdl */
1458 1.1.1.2 fvdl /* ARGSUSED */
1459 1.1.1.3 fvdl int
1460 1.1.1.2 fvdl pathconf(p, uap, retval)
1461 1.1.1.2 fvdl struct proc *p;
1462 1.1.1.3 fvdl register struct pathconf_args /* {
1463 1.1.1.3 fvdl syscallarg(char *) path;
1464 1.1.1.3 fvdl syscallarg(int) name;
1465 1.1.1.3 fvdl } */ *uap;
1466 1.1.1.3 fvdl register_t *retval;
1467 1.1.1.2 fvdl {
1468 1.1.1.2 fvdl int error;
1469 1.1.1.2 fvdl struct nameidata nd;
1470 1.1.1.2 fvdl
1471 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1472 1.1.1.3 fvdl SCARG(uap, path), p);
1473 1.1.1.2 fvdl if (error = namei(&nd))
1474 1.1.1.2 fvdl return (error);
1475 1.1.1.3 fvdl error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1476 1.1.1.2 fvdl vput(nd.ni_vp);
1477 1.1.1.2 fvdl return (error);
1478 1.1.1.2 fvdl }
1479 1.1.1.2 fvdl
1480 1.1.1.2 fvdl /*
1481 1.1.1.2 fvdl * Return target name of a symbolic link.
1482 1.1.1.2 fvdl */
1483 1.1.1.2 fvdl /* ARGSUSED */
1484 1.1.1.3 fvdl int
1485 1.1.1.2 fvdl readlink(p, uap, retval)
1486 1.1.1.2 fvdl struct proc *p;
1487 1.1.1.3 fvdl register struct readlink_args /* {
1488 1.1.1.3 fvdl syscallarg(char *) path;
1489 1.1.1.3 fvdl syscallarg(char *) buf;
1490 1.1.1.3 fvdl syscallarg(int) count;
1491 1.1.1.3 fvdl } */ *uap;
1492 1.1.1.3 fvdl register_t *retval;
1493 1.1.1.2 fvdl {
1494 1.1.1.2 fvdl register struct vnode *vp;
1495 1.1.1.2 fvdl struct iovec aiov;
1496 1.1.1.2 fvdl struct uio auio;
1497 1.1.1.2 fvdl int error;
1498 1.1.1.2 fvdl struct nameidata nd;
1499 1.1.1.2 fvdl
1500 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1501 1.1.1.3 fvdl SCARG(uap, path), p);
1502 1.1.1.2 fvdl if (error = namei(&nd))
1503 1.1.1.2 fvdl return (error);
1504 1.1.1.2 fvdl vp = nd.ni_vp;
1505 1.1.1.2 fvdl if (vp->v_type != VLNK)
1506 1.1.1.2 fvdl error = EINVAL;
1507 1.1.1.2 fvdl else {
1508 1.1.1.3 fvdl aiov.iov_base = SCARG(uap, buf);
1509 1.1.1.3 fvdl aiov.iov_len = SCARG(uap, count);
1510 1.1.1.2 fvdl auio.uio_iov = &aiov;
1511 1.1.1.2 fvdl auio.uio_iovcnt = 1;
1512 1.1.1.2 fvdl auio.uio_offset = 0;
1513 1.1.1.2 fvdl auio.uio_rw = UIO_READ;
1514 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
1515 1.1.1.2 fvdl auio.uio_procp = p;
1516 1.1.1.3 fvdl auio.uio_resid = SCARG(uap, count);
1517 1.1.1.2 fvdl error = VOP_READLINK(vp, &auio, p->p_ucred);
1518 1.1.1.2 fvdl }
1519 1.1.1.2 fvdl vput(vp);
1520 1.1.1.3 fvdl *retval = SCARG(uap, count) - auio.uio_resid;
1521 1.1.1.2 fvdl return (error);
1522 1.1.1.2 fvdl }
1523 1.1.1.2 fvdl
1524 1.1.1.2 fvdl /*
1525 1.1.1.2 fvdl * Change flags of a file given a path name.
1526 1.1.1.2 fvdl */
1527 1.1.1.2 fvdl /* ARGSUSED */
1528 1.1.1.3 fvdl int
1529 1.1.1.2 fvdl chflags(p, uap, retval)
1530 1.1.1.2 fvdl struct proc *p;
1531 1.1.1.3 fvdl register struct chflags_args /* {
1532 1.1.1.3 fvdl syscallarg(char *) path;
1533 1.1.1.3 fvdl syscallarg(int) flags;
1534 1.1.1.3 fvdl } */ *uap;
1535 1.1.1.3 fvdl register_t *retval;
1536 1.1.1.2 fvdl {
1537 1.1.1.2 fvdl register struct vnode *vp;
1538 1.1.1.2 fvdl struct vattr vattr;
1539 1.1.1.2 fvdl int error;
1540 1.1.1.2 fvdl struct nameidata nd;
1541 1.1.1.2 fvdl
1542 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1543 1.1.1.2 fvdl if (error = namei(&nd))
1544 1.1.1.2 fvdl return (error);
1545 1.1.1.2 fvdl vp = nd.ni_vp;
1546 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1547 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1548 1.1.1.3 fvdl VATTR_NULL(&vattr);
1549 1.1.1.3 fvdl vattr.va_flags = SCARG(uap, flags);
1550 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1551 1.1.1.2 fvdl vput(vp);
1552 1.1.1.2 fvdl return (error);
1553 1.1.1.2 fvdl }
1554 1.1.1.2 fvdl
1555 1.1.1.2 fvdl /*
1556 1.1.1.2 fvdl * Change flags of a file given a file descriptor.
1557 1.1.1.2 fvdl */
1558 1.1.1.2 fvdl /* ARGSUSED */
1559 1.1.1.3 fvdl int
1560 1.1.1.2 fvdl fchflags(p, uap, retval)
1561 1.1.1.2 fvdl struct proc *p;
1562 1.1.1.3 fvdl register struct fchflags_args /* {
1563 1.1.1.3 fvdl syscallarg(int) fd;
1564 1.1.1.3 fvdl syscallarg(int) flags;
1565 1.1.1.3 fvdl } */ *uap;
1566 1.1.1.3 fvdl register_t *retval;
1567 1.1.1.2 fvdl {
1568 1.1.1.2 fvdl struct vattr vattr;
1569 1.1.1.2 fvdl struct vnode *vp;
1570 1.1.1.2 fvdl struct file *fp;
1571 1.1.1.2 fvdl int error;
1572 1.1.1.2 fvdl
1573 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1574 1.1.1.2 fvdl return (error);
1575 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
1576 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1577 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1578 1.1.1.3 fvdl VATTR_NULL(&vattr);
1579 1.1.1.3 fvdl vattr.va_flags = SCARG(uap, flags);
1580 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1581 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
1582 1.1.1.2 fvdl return (error);
1583 1.1.1.2 fvdl }
1584 1.1.1.2 fvdl
1585 1.1.1.2 fvdl /*
1586 1.1.1.2 fvdl * Change mode of a file given path name.
1587 1.1.1.2 fvdl */
1588 1.1.1.2 fvdl /* ARGSUSED */
1589 1.1.1.3 fvdl int
1590 1.1.1.2 fvdl chmod(p, uap, retval)
1591 1.1.1.2 fvdl struct proc *p;
1592 1.1.1.3 fvdl register struct chmod_args /* {
1593 1.1.1.3 fvdl syscallarg(char *) path;
1594 1.1.1.3 fvdl syscallarg(int) mode;
1595 1.1.1.3 fvdl } */ *uap;
1596 1.1.1.3 fvdl register_t *retval;
1597 1.1.1.2 fvdl {
1598 1.1.1.2 fvdl register struct vnode *vp;
1599 1.1.1.2 fvdl struct vattr vattr;
1600 1.1.1.2 fvdl int error;
1601 1.1.1.2 fvdl struct nameidata nd;
1602 1.1.1.2 fvdl
1603 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1604 1.1.1.2 fvdl if (error = namei(&nd))
1605 1.1.1.2 fvdl return (error);
1606 1.1.1.2 fvdl vp = nd.ni_vp;
1607 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1608 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1609 1.1.1.3 fvdl VATTR_NULL(&vattr);
1610 1.1.1.3 fvdl vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1611 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1612 1.1.1.2 fvdl vput(vp);
1613 1.1.1.2 fvdl return (error);
1614 1.1.1.2 fvdl }
1615 1.1.1.2 fvdl
1616 1.1.1.2 fvdl /*
1617 1.1.1.2 fvdl * Change mode of a file given a file descriptor.
1618 1.1.1.2 fvdl */
1619 1.1.1.2 fvdl /* ARGSUSED */
1620 1.1.1.3 fvdl int
1621 1.1.1.2 fvdl fchmod(p, uap, retval)
1622 1.1.1.2 fvdl struct proc *p;
1623 1.1.1.3 fvdl register struct fchmod_args /* {
1624 1.1.1.3 fvdl syscallarg(int) fd;
1625 1.1.1.3 fvdl syscallarg(int) mode;
1626 1.1.1.3 fvdl } */ *uap;
1627 1.1.1.3 fvdl register_t *retval;
1628 1.1.1.2 fvdl {
1629 1.1.1.2 fvdl struct vattr vattr;
1630 1.1.1.2 fvdl struct vnode *vp;
1631 1.1.1.2 fvdl struct file *fp;
1632 1.1.1.2 fvdl int error;
1633 1.1.1.2 fvdl
1634 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1635 1.1.1.2 fvdl return (error);
1636 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
1637 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1638 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1639 1.1.1.3 fvdl VATTR_NULL(&vattr);
1640 1.1.1.3 fvdl vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1641 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1642 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
1643 1.1.1.2 fvdl return (error);
1644 1.1.1.2 fvdl }
1645 1.1.1.2 fvdl
1646 1.1.1.2 fvdl /*
1647 1.1.1.2 fvdl * Set ownership given a path name.
1648 1.1.1.2 fvdl */
1649 1.1.1.2 fvdl /* ARGSUSED */
1650 1.1.1.3 fvdl int
1651 1.1.1.2 fvdl chown(p, uap, retval)
1652 1.1.1.2 fvdl struct proc *p;
1653 1.1.1.3 fvdl register struct chown_args /* {
1654 1.1.1.3 fvdl syscallarg(char *) path;
1655 1.1.1.3 fvdl syscallarg(int) uid;
1656 1.1.1.3 fvdl syscallarg(int) gid;
1657 1.1.1.3 fvdl } */ *uap;
1658 1.1.1.3 fvdl register_t *retval;
1659 1.1.1.2 fvdl {
1660 1.1.1.2 fvdl register struct vnode *vp;
1661 1.1.1.2 fvdl struct vattr vattr;
1662 1.1.1.2 fvdl int error;
1663 1.1.1.2 fvdl struct nameidata nd;
1664 1.1.1.2 fvdl
1665 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1666 1.1.1.2 fvdl if (error = namei(&nd))
1667 1.1.1.2 fvdl return (error);
1668 1.1.1.2 fvdl vp = nd.ni_vp;
1669 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1670 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1671 1.1.1.3 fvdl VATTR_NULL(&vattr);
1672 1.1.1.3 fvdl vattr.va_uid = SCARG(uap, uid);
1673 1.1.1.3 fvdl vattr.va_gid = SCARG(uap, gid);
1674 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1675 1.1.1.2 fvdl vput(vp);
1676 1.1.1.2 fvdl return (error);
1677 1.1.1.2 fvdl }
1678 1.1.1.2 fvdl
1679 1.1.1.2 fvdl /*
1680 1.1.1.2 fvdl * Set ownership given a file descriptor.
1681 1.1.1.2 fvdl */
1682 1.1.1.2 fvdl /* ARGSUSED */
1683 1.1.1.3 fvdl int
1684 1.1.1.2 fvdl fchown(p, uap, retval)
1685 1.1.1.2 fvdl struct proc *p;
1686 1.1.1.3 fvdl register struct fchown_args /* {
1687 1.1.1.3 fvdl syscallarg(int) fd;
1688 1.1.1.3 fvdl syscallarg(int) uid;
1689 1.1.1.3 fvdl syscallarg(int) gid;
1690 1.1.1.3 fvdl } */ *uap;
1691 1.1.1.3 fvdl register_t *retval;
1692 1.1.1.2 fvdl {
1693 1.1.1.2 fvdl struct vattr vattr;
1694 1.1.1.2 fvdl struct vnode *vp;
1695 1.1.1.2 fvdl struct file *fp;
1696 1.1.1.2 fvdl int error;
1697 1.1.1.2 fvdl
1698 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1699 1.1.1.2 fvdl return (error);
1700 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
1701 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1702 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1703 1.1.1.3 fvdl VATTR_NULL(&vattr);
1704 1.1.1.3 fvdl vattr.va_uid = SCARG(uap, uid);
1705 1.1.1.3 fvdl vattr.va_gid = SCARG(uap, gid);
1706 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1707 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
1708 1.1.1.2 fvdl return (error);
1709 1.1.1.2 fvdl }
1710 1.1.1.2 fvdl
1711 1.1.1.2 fvdl /*
1712 1.1.1.2 fvdl * Set the access and modification times of a file.
1713 1.1.1.2 fvdl */
1714 1.1.1.2 fvdl /* ARGSUSED */
1715 1.1.1.3 fvdl int
1716 1.1.1.2 fvdl utimes(p, uap, retval)
1717 1.1.1.2 fvdl struct proc *p;
1718 1.1.1.3 fvdl register struct utimes_args /* {
1719 1.1.1.3 fvdl syscallarg(char *) path;
1720 1.1.1.3 fvdl syscallarg(struct timeval *) tptr;
1721 1.1.1.3 fvdl } */ *uap;
1722 1.1.1.3 fvdl register_t *retval;
1723 1.1.1.2 fvdl {
1724 1.1.1.2 fvdl register struct vnode *vp;
1725 1.1.1.2 fvdl struct timeval tv[2];
1726 1.1.1.2 fvdl struct vattr vattr;
1727 1.1.1.2 fvdl int error;
1728 1.1.1.2 fvdl struct nameidata nd;
1729 1.1.1.2 fvdl
1730 1.1.1.2 fvdl VATTR_NULL(&vattr);
1731 1.1.1.3 fvdl if (SCARG(uap, tptr) == NULL) {
1732 1.1.1.2 fvdl microtime(&tv[0]);
1733 1.1.1.2 fvdl tv[1] = tv[0];
1734 1.1.1.2 fvdl vattr.va_vaflags |= VA_UTIMES_NULL;
1735 1.1.1.3 fvdl } else if (error = copyin((caddr_t)SCARG(uap, tptr), (caddr_t)tv,
1736 1.1.1.3 fvdl sizeof (tv)))
1737 1.1.1.2 fvdl return (error);
1738 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1739 1.1.1.2 fvdl if (error = namei(&nd))
1740 1.1.1.2 fvdl return (error);
1741 1.1.1.2 fvdl vp = nd.ni_vp;
1742 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1743 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1744 1.1.1.3 fvdl vattr.va_atime.ts_sec = tv[0].tv_sec;
1745 1.1.1.3 fvdl vattr.va_atime.ts_nsec = tv[0].tv_usec * 1000;
1746 1.1.1.3 fvdl vattr.va_mtime.ts_sec = tv[1].tv_sec;
1747 1.1.1.3 fvdl vattr.va_mtime.ts_nsec = tv[1].tv_usec * 1000;
1748 1.1.1.3 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1749 1.1.1.2 fvdl vput(vp);
1750 1.1.1.2 fvdl return (error);
1751 1.1.1.2 fvdl }
1752 1.1.1.2 fvdl
1753 1.1.1.2 fvdl /*
1754 1.1.1.2 fvdl * Truncate a file given its path name.
1755 1.1.1.2 fvdl */
1756 1.1.1.2 fvdl /* ARGSUSED */
1757 1.1.1.3 fvdl int
1758 1.1.1.2 fvdl truncate(p, uap, retval)
1759 1.1.1.2 fvdl struct proc *p;
1760 1.1.1.3 fvdl register struct truncate_args /* {
1761 1.1.1.3 fvdl syscallarg(char *) path;
1762 1.1.1.3 fvdl syscallarg(int) pad;
1763 1.1.1.3 fvdl syscallarg(off_t) length;
1764 1.1.1.3 fvdl } */ *uap;
1765 1.1.1.3 fvdl register_t *retval;
1766 1.1.1.2 fvdl {
1767 1.1.1.2 fvdl register struct vnode *vp;
1768 1.1.1.2 fvdl struct vattr vattr;
1769 1.1.1.2 fvdl int error;
1770 1.1.1.2 fvdl struct nameidata nd;
1771 1.1.1.2 fvdl
1772 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1773 1.1.1.2 fvdl if (error = namei(&nd))
1774 1.1.1.2 fvdl return (error);
1775 1.1.1.2 fvdl vp = nd.ni_vp;
1776 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1777 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1778 1.1.1.2 fvdl if (vp->v_type == VDIR)
1779 1.1.1.2 fvdl error = EISDIR;
1780 1.1.1.2 fvdl else if ((error = vn_writechk(vp)) == 0 &&
1781 1.1.1.2 fvdl (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1782 1.1.1.2 fvdl VATTR_NULL(&vattr);
1783 1.1.1.3 fvdl vattr.va_size = SCARG(uap, length);
1784 1.1.1.2 fvdl error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1785 1.1.1.2 fvdl }
1786 1.1.1.2 fvdl vput(vp);
1787 1.1.1.2 fvdl return (error);
1788 1.1.1.2 fvdl }
1789 1.1.1.2 fvdl
1790 1.1.1.2 fvdl /*
1791 1.1.1.2 fvdl * Truncate a file given a file descriptor.
1792 1.1.1.2 fvdl */
1793 1.1.1.2 fvdl /* ARGSUSED */
1794 1.1.1.3 fvdl int
1795 1.1.1.2 fvdl ftruncate(p, uap, retval)
1796 1.1.1.2 fvdl struct proc *p;
1797 1.1.1.3 fvdl register struct ftruncate_args /* {
1798 1.1.1.3 fvdl syscallarg(int) fd;
1799 1.1.1.3 fvdl syscallarg(int) pad;
1800 1.1.1.3 fvdl syscallarg(off_t) length;
1801 1.1.1.3 fvdl } */ *uap;
1802 1.1.1.3 fvdl register_t *retval;
1803 1.1.1.2 fvdl {
1804 1.1.1.2 fvdl struct vattr vattr;
1805 1.1.1.2 fvdl struct vnode *vp;
1806 1.1.1.2 fvdl struct file *fp;
1807 1.1.1.2 fvdl int error;
1808 1.1.1.2 fvdl
1809 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1810 1.1.1.2 fvdl return (error);
1811 1.1.1.2 fvdl if ((fp->f_flag & FWRITE) == 0)
1812 1.1.1.2 fvdl return (EINVAL);
1813 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
1814 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1815 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1816 1.1.1.2 fvdl if (vp->v_type == VDIR)
1817 1.1.1.2 fvdl error = EISDIR;
1818 1.1.1.2 fvdl else if ((error = vn_writechk(vp)) == 0) {
1819 1.1.1.2 fvdl VATTR_NULL(&vattr);
1820 1.1.1.3 fvdl vattr.va_size = SCARG(uap, length);
1821 1.1.1.2 fvdl error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1822 1.1.1.2 fvdl }
1823 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
1824 1.1.1.2 fvdl return (error);
1825 1.1.1.2 fvdl }
1826 1.1.1.2 fvdl
1827 1.1.1.2 fvdl #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1828 1.1.1.2 fvdl /*
1829 1.1.1.2 fvdl * Truncate a file given its path name.
1830 1.1.1.2 fvdl */
1831 1.1.1.2 fvdl /* ARGSUSED */
1832 1.1.1.3 fvdl int
1833 1.1.1.3 fvdl compat_43_truncate(p, uap, retval)
1834 1.1.1.2 fvdl struct proc *p;
1835 1.1.1.3 fvdl register struct compat_43_truncate_args /* {
1836 1.1.1.3 fvdl syscallarg(char *) path;
1837 1.1.1.3 fvdl syscallarg(long) length;
1838 1.1.1.3 fvdl } */ *uap;
1839 1.1.1.3 fvdl register_t *retval;
1840 1.1.1.3 fvdl {
1841 1.1.1.3 fvdl struct truncate_args /* {
1842 1.1.1.3 fvdl syscallarg(char *) path;
1843 1.1.1.3 fvdl syscallarg(int) pad;
1844 1.1.1.3 fvdl syscallarg(off_t) length;
1845 1.1.1.3 fvdl } */ nuap;
1846 1.1.1.2 fvdl
1847 1.1.1.3 fvdl SCARG(&nuap, path) = SCARG(uap, path);
1848 1.1.1.3 fvdl SCARG(&nuap, length) = SCARG(uap, length);
1849 1.1.1.2 fvdl return (truncate(p, &nuap, retval));
1850 1.1.1.2 fvdl }
1851 1.1.1.2 fvdl
1852 1.1.1.2 fvdl /*
1853 1.1.1.2 fvdl * Truncate a file given a file descriptor.
1854 1.1.1.2 fvdl */
1855 1.1.1.2 fvdl /* ARGSUSED */
1856 1.1.1.3 fvdl int
1857 1.1.1.3 fvdl compat_43_ftruncate(p, uap, retval)
1858 1.1.1.2 fvdl struct proc *p;
1859 1.1.1.3 fvdl register struct compat_43_ftruncate_args /* {
1860 1.1.1.3 fvdl syscallarg(int) fd;
1861 1.1.1.3 fvdl syscallarg(long) length;
1862 1.1.1.3 fvdl } */ *uap;
1863 1.1.1.3 fvdl register_t *retval;
1864 1.1.1.3 fvdl {
1865 1.1.1.3 fvdl struct ftruncate_args /* {
1866 1.1.1.3 fvdl syscallarg(int) fd;
1867 1.1.1.3 fvdl syscallarg(int) pad;
1868 1.1.1.3 fvdl syscallarg(off_t) length;
1869 1.1.1.3 fvdl } */ nuap;
1870 1.1.1.2 fvdl
1871 1.1.1.3 fvdl SCARG(&nuap, fd) = SCARG(uap, fd);
1872 1.1.1.3 fvdl SCARG(&nuap, length) = SCARG(uap, length);
1873 1.1.1.2 fvdl return (ftruncate(p, &nuap, retval));
1874 1.1.1.2 fvdl }
1875 1.1.1.2 fvdl #endif /* COMPAT_43 || COMPAT_SUNOS */
1876 1.1.1.2 fvdl
1877 1.1.1.2 fvdl /*
1878 1.1.1.2 fvdl * Sync an open file.
1879 1.1.1.2 fvdl */
1880 1.1.1.2 fvdl /* ARGSUSED */
1881 1.1.1.3 fvdl int
1882 1.1.1.2 fvdl fsync(p, uap, retval)
1883 1.1.1.2 fvdl struct proc *p;
1884 1.1.1.3 fvdl struct fsync_args /* {
1885 1.1.1.3 fvdl syscallarg(int) fd;
1886 1.1.1.3 fvdl } */ *uap;
1887 1.1.1.3 fvdl register_t *retval;
1888 1.1.1.2 fvdl {
1889 1.1.1.2 fvdl register struct vnode *vp;
1890 1.1.1.2 fvdl struct file *fp;
1891 1.1.1.2 fvdl int error;
1892 1.1.1.2 fvdl
1893 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
1894 1.1.1.2 fvdl return (error);
1895 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
1896 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1897 1.1.1.2 fvdl error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1898 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
1899 1.1.1.2 fvdl return (error);
1900 1.1.1.2 fvdl }
1901 1.1.1.2 fvdl
1902 1.1.1.2 fvdl /*
1903 1.1.1.2 fvdl * Rename files. Source and destination must either both be directories,
1904 1.1.1.2 fvdl * or both not be directories. If target is a directory, it must be empty.
1905 1.1.1.2 fvdl */
1906 1.1.1.2 fvdl /* ARGSUSED */
1907 1.1.1.3 fvdl int
1908 1.1.1.2 fvdl rename(p, uap, retval)
1909 1.1.1.2 fvdl struct proc *p;
1910 1.1.1.3 fvdl register struct rename_args /* {
1911 1.1.1.3 fvdl syscallarg(char *) from;
1912 1.1.1.3 fvdl syscallarg(char *) to;
1913 1.1.1.3 fvdl } */ *uap;
1914 1.1.1.3 fvdl register_t *retval;
1915 1.1.1.2 fvdl {
1916 1.1.1.2 fvdl register struct vnode *tvp, *fvp, *tdvp;
1917 1.1.1.2 fvdl struct nameidata fromnd, tond;
1918 1.1.1.2 fvdl int error;
1919 1.1.1.2 fvdl
1920 1.1.1.2 fvdl NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1921 1.1.1.3 fvdl SCARG(uap, from), p);
1922 1.1.1.2 fvdl if (error = namei(&fromnd))
1923 1.1.1.2 fvdl return (error);
1924 1.1.1.2 fvdl fvp = fromnd.ni_vp;
1925 1.1.1.2 fvdl NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1926 1.1.1.3 fvdl UIO_USERSPACE, SCARG(uap, to), p);
1927 1.1.1.2 fvdl if (error = namei(&tond)) {
1928 1.1.1.2 fvdl VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1929 1.1.1.2 fvdl vrele(fromnd.ni_dvp);
1930 1.1.1.2 fvdl vrele(fvp);
1931 1.1.1.2 fvdl goto out1;
1932 1.1.1.2 fvdl }
1933 1.1.1.2 fvdl tdvp = tond.ni_dvp;
1934 1.1.1.2 fvdl tvp = tond.ni_vp;
1935 1.1.1.2 fvdl if (tvp != NULL) {
1936 1.1.1.2 fvdl if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1937 1.1.1.2 fvdl error = ENOTDIR;
1938 1.1.1.2 fvdl goto out;
1939 1.1.1.2 fvdl } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1940 1.1.1.2 fvdl error = EISDIR;
1941 1.1.1.2 fvdl goto out;
1942 1.1.1.2 fvdl }
1943 1.1.1.2 fvdl }
1944 1.1.1.2 fvdl if (fvp == tdvp)
1945 1.1.1.2 fvdl error = EINVAL;
1946 1.1.1.2 fvdl /*
1947 1.1.1.2 fvdl * If source is the same as the destination (that is the
1948 1.1.1.2 fvdl * same inode number with the same name in the same directory),
1949 1.1.1.2 fvdl * then there is nothing to do.
1950 1.1.1.2 fvdl */
1951 1.1.1.2 fvdl if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1952 1.1.1.2 fvdl fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1953 1.1.1.2 fvdl !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1954 1.1.1.2 fvdl fromnd.ni_cnd.cn_namelen))
1955 1.1.1.2 fvdl error = -1;
1956 1.1.1.2 fvdl out:
1957 1.1.1.2 fvdl if (!error) {
1958 1.1.1.3 fvdl VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1959 1.1.1.2 fvdl if (fromnd.ni_dvp != tdvp)
1960 1.1.1.3 fvdl VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1961 1.1.1.2 fvdl if (tvp)
1962 1.1.1.3 fvdl VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1963 1.1.1.2 fvdl error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1964 1.1.1.2 fvdl tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1965 1.1.1.2 fvdl } else {
1966 1.1.1.2 fvdl VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1967 1.1.1.2 fvdl if (tdvp == tvp)
1968 1.1.1.2 fvdl vrele(tdvp);
1969 1.1.1.2 fvdl else
1970 1.1.1.2 fvdl vput(tdvp);
1971 1.1.1.2 fvdl if (tvp)
1972 1.1.1.2 fvdl vput(tvp);
1973 1.1.1.2 fvdl VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1974 1.1.1.2 fvdl vrele(fromnd.ni_dvp);
1975 1.1.1.2 fvdl vrele(fvp);
1976 1.1.1.2 fvdl }
1977 1.1.1.2 fvdl vrele(tond.ni_startdir);
1978 1.1.1.2 fvdl FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1979 1.1.1.2 fvdl out1:
1980 1.1.1.2 fvdl if (fromnd.ni_startdir)
1981 1.1.1.2 fvdl vrele(fromnd.ni_startdir);
1982 1.1.1.2 fvdl FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1983 1.1.1.2 fvdl if (error == -1)
1984 1.1.1.2 fvdl return (0);
1985 1.1.1.2 fvdl return (error);
1986 1.1.1.2 fvdl }
1987 1.1.1.2 fvdl
1988 1.1.1.2 fvdl /*
1989 1.1.1.2 fvdl * Make a directory file.
1990 1.1.1.2 fvdl */
1991 1.1.1.2 fvdl /* ARGSUSED */
1992 1.1.1.3 fvdl int
1993 1.1.1.2 fvdl mkdir(p, uap, retval)
1994 1.1.1.2 fvdl struct proc *p;
1995 1.1.1.3 fvdl register struct mkdir_args /* {
1996 1.1.1.3 fvdl syscallarg(char *) path;
1997 1.1.1.3 fvdl syscallarg(int) mode;
1998 1.1.1.3 fvdl } */ *uap;
1999 1.1.1.3 fvdl register_t *retval;
2000 1.1.1.2 fvdl {
2001 1.1.1.2 fvdl register struct vnode *vp;
2002 1.1.1.2 fvdl struct vattr vattr;
2003 1.1.1.2 fvdl int error;
2004 1.1.1.2 fvdl struct nameidata nd;
2005 1.1.1.2 fvdl
2006 1.1.1.3 fvdl NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2007 1.1.1.2 fvdl if (error = namei(&nd))
2008 1.1.1.2 fvdl return (error);
2009 1.1.1.2 fvdl vp = nd.ni_vp;
2010 1.1.1.2 fvdl if (vp != NULL) {
2011 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2012 1.1.1.2 fvdl if (nd.ni_dvp == vp)
2013 1.1.1.2 fvdl vrele(nd.ni_dvp);
2014 1.1.1.2 fvdl else
2015 1.1.1.2 fvdl vput(nd.ni_dvp);
2016 1.1.1.2 fvdl vrele(vp);
2017 1.1.1.2 fvdl return (EEXIST);
2018 1.1.1.2 fvdl }
2019 1.1.1.2 fvdl VATTR_NULL(&vattr);
2020 1.1.1.2 fvdl vattr.va_type = VDIR;
2021 1.1.1.3 fvdl vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2022 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2023 1.1.1.2 fvdl error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2024 1.1.1.2 fvdl if (!error)
2025 1.1.1.2 fvdl vput(nd.ni_vp);
2026 1.1.1.2 fvdl return (error);
2027 1.1.1.2 fvdl }
2028 1.1.1.2 fvdl
2029 1.1.1.2 fvdl /*
2030 1.1.1.2 fvdl * Remove a directory file.
2031 1.1.1.2 fvdl */
2032 1.1.1.2 fvdl /* ARGSUSED */
2033 1.1.1.3 fvdl int
2034 1.1.1.2 fvdl rmdir(p, uap, retval)
2035 1.1.1.2 fvdl struct proc *p;
2036 1.1.1.3 fvdl struct rmdir_args /* {
2037 1.1.1.3 fvdl syscallarg(char *) path;
2038 1.1.1.3 fvdl } */ *uap;
2039 1.1.1.3 fvdl register_t *retval;
2040 1.1.1.2 fvdl {
2041 1.1.1.2 fvdl register struct vnode *vp;
2042 1.1.1.2 fvdl int error;
2043 1.1.1.2 fvdl struct nameidata nd;
2044 1.1.1.2 fvdl
2045 1.1.1.3 fvdl NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2046 1.1.1.3 fvdl SCARG(uap, path), p);
2047 1.1.1.2 fvdl if (error = namei(&nd))
2048 1.1.1.2 fvdl return (error);
2049 1.1.1.2 fvdl vp = nd.ni_vp;
2050 1.1.1.2 fvdl if (vp->v_type != VDIR) {
2051 1.1.1.2 fvdl error = ENOTDIR;
2052 1.1.1.2 fvdl goto out;
2053 1.1.1.2 fvdl }
2054 1.1.1.2 fvdl /*
2055 1.1.1.2 fvdl * No rmdir "." please.
2056 1.1.1.2 fvdl */
2057 1.1.1.2 fvdl if (nd.ni_dvp == vp) {
2058 1.1.1.2 fvdl error = EINVAL;
2059 1.1.1.2 fvdl goto out;
2060 1.1.1.2 fvdl }
2061 1.1.1.2 fvdl /*
2062 1.1.1.2 fvdl * The root of a mounted filesystem cannot be deleted.
2063 1.1.1.2 fvdl */
2064 1.1.1.2 fvdl if (vp->v_flag & VROOT)
2065 1.1.1.2 fvdl error = EBUSY;
2066 1.1.1.2 fvdl out:
2067 1.1.1.2 fvdl if (!error) {
2068 1.1.1.3 fvdl VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2069 1.1.1.3 fvdl VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2070 1.1.1.2 fvdl error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2071 1.1.1.2 fvdl } else {
2072 1.1.1.2 fvdl VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2073 1.1.1.2 fvdl if (nd.ni_dvp == vp)
2074 1.1.1.2 fvdl vrele(nd.ni_dvp);
2075 1.1.1.2 fvdl else
2076 1.1.1.2 fvdl vput(nd.ni_dvp);
2077 1.1.1.2 fvdl vput(vp);
2078 1.1.1.2 fvdl }
2079 1.1.1.2 fvdl return (error);
2080 1.1.1.2 fvdl }
2081 1.1.1.2 fvdl
2082 1.1.1.2 fvdl #ifdef COMPAT_43
2083 1.1.1.2 fvdl /*
2084 1.1.1.2 fvdl * Read a block of directory entries in a file system independent format.
2085 1.1.1.2 fvdl */
2086 1.1.1.3 fvdl int
2087 1.1.1.3 fvdl compat_43_getdirentries(p, uap, retval)
2088 1.1.1.2 fvdl struct proc *p;
2089 1.1.1.3 fvdl register struct compat_43_getdirentries_args /* {
2090 1.1.1.3 fvdl syscallarg(int) fd;
2091 1.1.1.3 fvdl syscallarg(char *) buf;
2092 1.1.1.3 fvdl syscallarg(u_int) count;
2093 1.1.1.3 fvdl syscallarg(long *) basep;
2094 1.1.1.3 fvdl } */ *uap;
2095 1.1.1.3 fvdl register_t *retval;
2096 1.1.1.2 fvdl {
2097 1.1.1.2 fvdl register struct vnode *vp;
2098 1.1.1.2 fvdl struct file *fp;
2099 1.1.1.2 fvdl struct uio auio, kuio;
2100 1.1.1.2 fvdl struct iovec aiov, kiov;
2101 1.1.1.2 fvdl struct dirent *dp, *edp;
2102 1.1.1.2 fvdl caddr_t dirbuf;
2103 1.1.1.3 fvdl int error, eofflag, readcnt;
2104 1.1.1.2 fvdl long loff;
2105 1.1.1.2 fvdl
2106 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2107 1.1.1.2 fvdl return (error);
2108 1.1.1.2 fvdl if ((fp->f_flag & FREAD) == 0)
2109 1.1.1.2 fvdl return (EBADF);
2110 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
2111 1.1.1.3 fvdl unionread:
2112 1.1.1.2 fvdl if (vp->v_type != VDIR)
2113 1.1.1.2 fvdl return (EINVAL);
2114 1.1.1.3 fvdl aiov.iov_base = SCARG(uap, buf);
2115 1.1.1.3 fvdl aiov.iov_len = SCARG(uap, count);
2116 1.1.1.2 fvdl auio.uio_iov = &aiov;
2117 1.1.1.2 fvdl auio.uio_iovcnt = 1;
2118 1.1.1.2 fvdl auio.uio_rw = UIO_READ;
2119 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
2120 1.1.1.2 fvdl auio.uio_procp = p;
2121 1.1.1.3 fvdl auio.uio_resid = SCARG(uap, count);
2122 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2123 1.1.1.2 fvdl loff = auio.uio_offset = fp->f_offset;
2124 1.1.1.2 fvdl # if (BYTE_ORDER != LITTLE_ENDIAN)
2125 1.1.1.2 fvdl if (vp->v_mount->mnt_maxsymlinklen <= 0) {
2126 1.1.1.3 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2127 1.1.1.3 fvdl (int *)0, (u_long *)0);
2128 1.1.1.2 fvdl fp->f_offset = auio.uio_offset;
2129 1.1.1.2 fvdl } else
2130 1.1.1.2 fvdl # endif
2131 1.1.1.2 fvdl {
2132 1.1.1.2 fvdl kuio = auio;
2133 1.1.1.2 fvdl kuio.uio_iov = &kiov;
2134 1.1.1.2 fvdl kuio.uio_segflg = UIO_SYSSPACE;
2135 1.1.1.3 fvdl kiov.iov_len = SCARG(uap, count);
2136 1.1.1.3 fvdl MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
2137 1.1.1.2 fvdl kiov.iov_base = dirbuf;
2138 1.1.1.3 fvdl error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
2139 1.1.1.3 fvdl (int *)0, (u_long *)0);
2140 1.1.1.2 fvdl fp->f_offset = kuio.uio_offset;
2141 1.1.1.2 fvdl if (error == 0) {
2142 1.1.1.3 fvdl readcnt = SCARG(uap, count) - kuio.uio_resid;
2143 1.1.1.2 fvdl edp = (struct dirent *)&dirbuf[readcnt];
2144 1.1.1.2 fvdl for (dp = (struct dirent *)dirbuf; dp < edp; ) {
2145 1.1.1.2 fvdl # if (BYTE_ORDER == LITTLE_ENDIAN)
2146 1.1.1.2 fvdl /*
2147 1.1.1.2 fvdl * The expected low byte of
2148 1.1.1.2 fvdl * dp->d_namlen is our dp->d_type.
2149 1.1.1.2 fvdl * The high MBZ byte of dp->d_namlen
2150 1.1.1.2 fvdl * is our dp->d_namlen.
2151 1.1.1.2 fvdl */
2152 1.1.1.2 fvdl dp->d_type = dp->d_namlen;
2153 1.1.1.2 fvdl dp->d_namlen = 0;
2154 1.1.1.2 fvdl # else
2155 1.1.1.2 fvdl /*
2156 1.1.1.2 fvdl * The dp->d_type is the high byte
2157 1.1.1.2 fvdl * of the expected dp->d_namlen,
2158 1.1.1.2 fvdl * so must be zero'ed.
2159 1.1.1.2 fvdl */
2160 1.1.1.2 fvdl dp->d_type = 0;
2161 1.1.1.2 fvdl # endif
2162 1.1.1.2 fvdl if (dp->d_reclen > 0) {
2163 1.1.1.2 fvdl dp = (struct dirent *)
2164 1.1.1.2 fvdl ((char *)dp + dp->d_reclen);
2165 1.1.1.2 fvdl } else {
2166 1.1.1.2 fvdl error = EIO;
2167 1.1.1.2 fvdl break;
2168 1.1.1.2 fvdl }
2169 1.1.1.2 fvdl }
2170 1.1.1.2 fvdl if (dp >= edp)
2171 1.1.1.2 fvdl error = uiomove(dirbuf, readcnt, &auio);
2172 1.1.1.2 fvdl }
2173 1.1.1.2 fvdl FREE(dirbuf, M_TEMP);
2174 1.1.1.2 fvdl }
2175 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
2176 1.1.1.2 fvdl if (error)
2177 1.1.1.2 fvdl return (error);
2178 1.1.1.3 fvdl
2179 1.1.1.3 fvdl #ifdef UNION
2180 1.1.1.3 fvdl {
2181 1.1.1.3 fvdl extern int (**union_vnodeop_p)();
2182 1.1.1.3 fvdl extern struct vnode *union_dircache __P((struct vnode*, struct proc*));
2183 1.1.1.3 fvdl
2184 1.1.1.3 fvdl if ((SCARG(uap, count) == auio.uio_resid) &&
2185 1.1.1.3 fvdl (vp->v_op == union_vnodeop_p)) {
2186 1.1.1.3 fvdl struct vnode *lvp;
2187 1.1.1.3 fvdl
2188 1.1.1.3 fvdl lvp = union_dircache(vp, p);
2189 1.1.1.3 fvdl if (lvp != NULLVP) {
2190 1.1.1.3 fvdl struct vattr va;
2191 1.1.1.3 fvdl
2192 1.1.1.3 fvdl /*
2193 1.1.1.3 fvdl * If the directory is opaque,
2194 1.1.1.3 fvdl * then don't show lower entries
2195 1.1.1.3 fvdl */
2196 1.1.1.3 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2197 1.1.1.3 fvdl if (va.va_flags & OPAQUE) {
2198 1.1.1.3 fvdl vput(lvp);
2199 1.1.1.3 fvdl lvp = NULL;
2200 1.1.1.3 fvdl }
2201 1.1.1.3 fvdl }
2202 1.1.1.3 fvdl
2203 1.1.1.3 fvdl if (lvp != NULLVP) {
2204 1.1.1.3 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2205 1.1.1.3 fvdl if (error) {
2206 1.1.1.3 fvdl vput(lvp);
2207 1.1.1.3 fvdl return (error);
2208 1.1.1.3 fvdl }
2209 1.1.1.3 fvdl VOP_UNLOCK(lvp, 0, p);
2210 1.1.1.3 fvdl fp->f_data = (caddr_t) lvp;
2211 1.1.1.3 fvdl fp->f_offset = 0;
2212 1.1.1.3 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
2213 1.1.1.3 fvdl if (error)
2214 1.1.1.3 fvdl return (error);
2215 1.1.1.3 fvdl vp = lvp;
2216 1.1.1.3 fvdl goto unionread;
2217 1.1.1.3 fvdl }
2218 1.1.1.3 fvdl }
2219 1.1.1.3 fvdl }
2220 1.1.1.3 fvdl #endif /* UNION */
2221 1.1.1.3 fvdl
2222 1.1.1.3 fvdl if ((SCARG(uap, count) == auio.uio_resid) &&
2223 1.1.1.3 fvdl (vp->v_flag & VROOT) &&
2224 1.1.1.3 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
2225 1.1.1.3 fvdl struct vnode *tvp = vp;
2226 1.1.1.3 fvdl vp = vp->v_mount->mnt_vnodecovered;
2227 1.1.1.3 fvdl VREF(vp);
2228 1.1.1.3 fvdl fp->f_data = (caddr_t) vp;
2229 1.1.1.3 fvdl fp->f_offset = 0;
2230 1.1.1.3 fvdl vrele(tvp);
2231 1.1.1.3 fvdl goto unionread;
2232 1.1.1.3 fvdl }
2233 1.1.1.3 fvdl error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2234 1.1.1.3 fvdl sizeof(long));
2235 1.1.1.3 fvdl *retval = SCARG(uap, count) - auio.uio_resid;
2236 1.1.1.2 fvdl return (error);
2237 1.1.1.2 fvdl }
2238 1.1.1.3 fvdl #endif /* COMPAT_43 */
2239 1.1.1.2 fvdl
2240 1.1.1.2 fvdl /*
2241 1.1.1.2 fvdl * Read a block of directory entries in a file system independent format.
2242 1.1.1.2 fvdl */
2243 1.1.1.3 fvdl int
2244 1.1.1.2 fvdl getdirentries(p, uap, retval)
2245 1.1.1.2 fvdl struct proc *p;
2246 1.1.1.3 fvdl register struct getdirentries_args /* {
2247 1.1.1.3 fvdl syscallarg(int) fd;
2248 1.1.1.3 fvdl syscallarg(char *) buf;
2249 1.1.1.3 fvdl syscallarg(u_int) count;
2250 1.1.1.3 fvdl syscallarg(long *) basep;
2251 1.1.1.3 fvdl } */ *uap;
2252 1.1.1.3 fvdl register_t *retval;
2253 1.1.1.2 fvdl {
2254 1.1.1.2 fvdl register struct vnode *vp;
2255 1.1.1.2 fvdl struct file *fp;
2256 1.1.1.2 fvdl struct uio auio;
2257 1.1.1.2 fvdl struct iovec aiov;
2258 1.1.1.2 fvdl long loff;
2259 1.1.1.3 fvdl int error, eofflag;
2260 1.1.1.2 fvdl
2261 1.1.1.3 fvdl if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
2262 1.1.1.2 fvdl return (error);
2263 1.1.1.2 fvdl if ((fp->f_flag & FREAD) == 0)
2264 1.1.1.2 fvdl return (EBADF);
2265 1.1.1.2 fvdl vp = (struct vnode *)fp->f_data;
2266 1.1.1.2 fvdl unionread:
2267 1.1.1.2 fvdl if (vp->v_type != VDIR)
2268 1.1.1.2 fvdl return (EINVAL);
2269 1.1.1.3 fvdl aiov.iov_base = SCARG(uap, buf);
2270 1.1.1.3 fvdl aiov.iov_len = SCARG(uap, count);
2271 1.1.1.2 fvdl auio.uio_iov = &aiov;
2272 1.1.1.2 fvdl auio.uio_iovcnt = 1;
2273 1.1.1.2 fvdl auio.uio_rw = UIO_READ;
2274 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
2275 1.1.1.2 fvdl auio.uio_procp = p;
2276 1.1.1.3 fvdl auio.uio_resid = SCARG(uap, count);
2277 1.1.1.3 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2278 1.1.1.2 fvdl loff = auio.uio_offset = fp->f_offset;
2279 1.1.1.3 fvdl error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
2280 1.1.1.3 fvdl (int *)0, (u_long *)0);
2281 1.1.1.2 fvdl fp->f_offset = auio.uio_offset;
2282 1.1.1.3 fvdl VOP_UNLOCK(vp, 0, p);
2283 1.1.1.2 fvdl if (error)
2284 1.1.1.2 fvdl return (error);
2285 1.1.1.2 fvdl
2286 1.1.1.2 fvdl #ifdef UNION
2287 1.1.1.2 fvdl {
2288 1.1.1.2 fvdl extern int (**union_vnodeop_p)();
2289 1.1.1.3 fvdl extern struct vnode *union_dircache __P((struct vnode*, struct proc*));
2290 1.1.1.2 fvdl
2291 1.1.1.3 fvdl if ((SCARG(uap, count) == auio.uio_resid) &&
2292 1.1.1.2 fvdl (vp->v_op == union_vnodeop_p)) {
2293 1.1.1.3 fvdl struct vnode *lvp;
2294 1.1.1.3 fvdl
2295 1.1.1.3 fvdl lvp = union_dircache(vp, p);
2296 1.1.1.3 fvdl if (lvp != NULLVP) {
2297 1.1.1.3 fvdl struct vattr va;
2298 1.1.1.2 fvdl
2299 1.1.1.3 fvdl /*
2300 1.1.1.3 fvdl * If the directory is opaque,
2301 1.1.1.3 fvdl * then don't show lower entries
2302 1.1.1.3 fvdl */
2303 1.1.1.3 fvdl error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2304 1.1.1.3 fvdl if (va.va_flags & OPAQUE) {
2305 1.1.1.3 fvdl vput(lvp);
2306 1.1.1.3 fvdl lvp = NULL;
2307 1.1.1.3 fvdl }
2308 1.1.1.3 fvdl }
2309 1.1.1.2 fvdl
2310 1.1.1.3 fvdl if (lvp != NULLVP) {
2311 1.1.1.3 fvdl error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2312 1.1.1.2 fvdl if (error) {
2313 1.1.1.3 fvdl vput(lvp);
2314 1.1.1.2 fvdl return (error);
2315 1.1.1.2 fvdl }
2316 1.1.1.3 fvdl VOP_UNLOCK(lvp, 0, p);
2317 1.1.1.3 fvdl fp->f_data = (caddr_t) lvp;
2318 1.1.1.2 fvdl fp->f_offset = 0;
2319 1.1.1.3 fvdl error = vn_close(vp, FREAD, fp->f_cred, p);
2320 1.1.1.2 fvdl if (error)
2321 1.1.1.2 fvdl return (error);
2322 1.1.1.3 fvdl vp = lvp;
2323 1.1.1.2 fvdl goto unionread;
2324 1.1.1.2 fvdl }
2325 1.1.1.2 fvdl }
2326 1.1.1.2 fvdl }
2327 1.1.1.3 fvdl #endif /* UNION */
2328 1.1.1.2 fvdl
2329 1.1.1.3 fvdl if ((SCARG(uap, count) == auio.uio_resid) &&
2330 1.1.1.2 fvdl (vp->v_flag & VROOT) &&
2331 1.1.1.2 fvdl (vp->v_mount->mnt_flag & MNT_UNION)) {
2332 1.1.1.2 fvdl struct vnode *tvp = vp;
2333 1.1.1.2 fvdl vp = vp->v_mount->mnt_vnodecovered;
2334 1.1.1.2 fvdl VREF(vp);
2335 1.1.1.2 fvdl fp->f_data = (caddr_t) vp;
2336 1.1.1.2 fvdl fp->f_offset = 0;
2337 1.1.1.2 fvdl vrele(tvp);
2338 1.1.1.2 fvdl goto unionread;
2339 1.1.1.2 fvdl }
2340 1.1.1.3 fvdl error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
2341 1.1.1.3 fvdl sizeof(long));
2342 1.1.1.3 fvdl *retval = SCARG(uap, count) - auio.uio_resid;
2343 1.1.1.2 fvdl return (error);
2344 1.1.1.2 fvdl }
2345 1.1.1.2 fvdl
2346 1.1.1.2 fvdl /*
2347 1.1.1.2 fvdl * Set the mode mask for creation of filesystem nodes.
2348 1.1.1.2 fvdl */
2349 1.1.1.3 fvdl int
2350 1.1.1.2 fvdl umask(p, uap, retval)
2351 1.1.1.2 fvdl struct proc *p;
2352 1.1.1.3 fvdl struct umask_args /* {
2353 1.1.1.3 fvdl syscallarg(int) newmask;
2354 1.1.1.3 fvdl } */ *uap;
2355 1.1.1.3 fvdl register_t *retval;
2356 1.1.1.2 fvdl {
2357 1.1.1.2 fvdl register struct filedesc *fdp;
2358 1.1.1.2 fvdl
2359 1.1.1.2 fvdl fdp = p->p_fd;
2360 1.1.1.2 fvdl *retval = fdp->fd_cmask;
2361 1.1.1.3 fvdl fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2362 1.1.1.2 fvdl return (0);
2363 1.1.1.2 fvdl }
2364 1.1.1.2 fvdl
2365 1.1.1.2 fvdl /*
2366 1.1.1.2 fvdl * Void all references to file by ripping underlying filesystem
2367 1.1.1.2 fvdl * away from vnode.
2368 1.1.1.2 fvdl */
2369 1.1.1.2 fvdl /* ARGSUSED */
2370 1.1.1.3 fvdl int
2371 1.1.1.2 fvdl revoke(p, uap, retval)
2372 1.1.1.2 fvdl struct proc *p;
2373 1.1.1.3 fvdl register struct revoke_args /* {
2374 1.1.1.3 fvdl syscallarg(char *) path;
2375 1.1.1.3 fvdl } */ *uap;
2376 1.1.1.3 fvdl register_t *retval;
2377 1.1.1.2 fvdl {
2378 1.1.1.2 fvdl register struct vnode *vp;
2379 1.1.1.2 fvdl struct vattr vattr;
2380 1.1.1.2 fvdl int error;
2381 1.1.1.2 fvdl struct nameidata nd;
2382 1.1.1.2 fvdl
2383 1.1.1.3 fvdl NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2384 1.1.1.2 fvdl if (error = namei(&nd))
2385 1.1.1.2 fvdl return (error);
2386 1.1.1.2 fvdl vp = nd.ni_vp;
2387 1.1.1.2 fvdl if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
2388 1.1.1.2 fvdl goto out;
2389 1.1.1.2 fvdl if (p->p_ucred->cr_uid != vattr.va_uid &&
2390 1.1.1.2 fvdl (error = suser(p->p_ucred, &p->p_acflag)))
2391 1.1.1.2 fvdl goto out;
2392 1.1.1.2 fvdl if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2393 1.1.1.3 fvdl VOP_REVOKE(vp, REVOKEALL);
2394 1.1.1.2 fvdl out:
2395 1.1.1.2 fvdl vrele(vp);
2396 1.1.1.2 fvdl return (error);
2397 1.1.1.2 fvdl }
2398 1.1.1.2 fvdl
2399 1.1.1.2 fvdl /*
2400 1.1.1.2 fvdl * Convert a user file descriptor to a kernel file entry.
2401 1.1.1.2 fvdl */
2402 1.1.1.3 fvdl int
2403 1.1.1.2 fvdl getvnode(fdp, fd, fpp)
2404 1.1.1.2 fvdl struct filedesc *fdp;
2405 1.1.1.2 fvdl struct file **fpp;
2406 1.1.1.2 fvdl int fd;
2407 1.1.1.2 fvdl {
2408 1.1.1.2 fvdl struct file *fp;
2409 1.1.1.2 fvdl
2410 1.1.1.2 fvdl if ((u_int)fd >= fdp->fd_nfiles ||
2411 1.1.1.2 fvdl (fp = fdp->fd_ofiles[fd]) == NULL)
2412 1.1.1.2 fvdl return (EBADF);
2413 1.1.1.2 fvdl if (fp->f_type != DTYPE_VNODE)
2414 1.1.1.2 fvdl return (EINVAL);
2415 1.1.1.2 fvdl *fpp = fp;
2416 1.1.1.2 fvdl return (0);
2417 1.1.1.2 fvdl }
2418