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