Disallow writes to filesystems mounted read-only via NULLFS. In this case
[dragonfly.git] / sys / kern / vfs_lookup.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
39 * $FreeBSD: src/sys/kern/vfs_lookup.c,v 1.38.2.3 2001/08/31 19:36:49 dillon Exp $
a11aaa81 40 * $DragonFly: src/sys/kern/vfs_lookup.c,v 1.23 2006/08/12 00:26:20 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ktrace.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
063147e5 47#include <sys/kernel.h>
984263bc
MD
48#include <sys/vnode.h>
49#include <sys/mount.h>
50#include <sys/filedesc.h>
51#include <sys/proc.h>
dadab5e9 52#include <sys/namei.h>
063147e5 53#include <sys/sysctl.h>
984263bc
MD
54
55#ifdef KTRACE
56#include <sys/ktrace.h>
57#endif
58
59#include <vm/vm_zone.h>
60
690a3127 61int varsym_enable = 0;
063147e5
MD
62SYSCTL_INT(_vfs, OID_AUTO, varsym_enable, CTLFLAG_RW, &varsym_enable, 0,
63 "Enable Variant Symlinks");
64
984263bc 65/*
fad57d0e 66 * OLD API FUNCTION
984263bc 67 *
fad57d0e
MD
68 * Relookup a path name component. This function is only used by the
69 * old API *_rename() code under very specific conditions. CNP_LOCKPARENT
70 * must be set in the cnp. dvp must be unlocked on entry and will be left
71 * unlocked if an error occurs.
984263bc 72 *
fad57d0e
MD
73 * The returned *vpp will be NULL if an error occurs. Note that no error
74 * will occur (error == 0) for CREATE requests on a non-existant target, but
75 * *vpp will be NULL in that case. Both dvp and *vpp (if not NULL) will be
76 * locked in the no-error case. No additional references are made to dvp,
77 * only the locking state changes.
984263bc
MD
78 */
79int
fad57d0e 80relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
984263bc 81{
984263bc 82 int rdonly; /* lookup read-only flag bit */
984263bc 83 int error = 0;
984263bc
MD
84
85 /*
86 * Setup: break out flag bits into variables.
87 */
fad57d0e
MD
88 KKASSERT(cnp->cn_flags & CNP_LOCKPARENT);
89 KKASSERT(cnp->cn_flags & CNP_PDIRUNLOCK);
ca466bae 90 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
2b69e610 91 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
984263bc 92
fad57d0e 93 *vpp = NULL;
2b69e610 94 rdonly = cnp->cn_flags & CNP_RDONLY;
984263bc 95
984263bc
MD
96 /*
97 * Search a new directory.
984263bc 98 */
984263bc
MD
99
100 /*
fad57d0e
MD
101 * Degenerate lookups (e.g. "/", "..", derived from "/.", etc)
102 * are not allowed.
984263bc
MD
103 */
104 if (cnp->cn_nameptr[0] == '\0') {
fad57d0e
MD
105 error = EISDIR;
106 goto bad;
984263bc 107 }
2b69e610 108 if (cnp->cn_flags & CNP_ISDOTDOT)
984263bc
MD
109 panic ("relookup: lookup on dot-dot");
110
111 /*
112 * We now have a segment name to search for, and a directory to search.
113 */
fad57d0e 114 if ((error = VOP_OLD_LOOKUP(dvp, vpp, cnp)) != 0) {
984263bc
MD
115 KASSERT(*vpp == NULL, ("leaf should be empty"));
116 if (error != EJUSTRETURN)
117 goto bad;
118 /*
119 * If creating and at end of pathname, then can consider
120 * allowing file to be created.
121 */
122 if (rdonly) {
123 error = EROFS;
124 goto bad;
125 }
984263bc
MD
126 /*
127 * We return with ni_vp NULL to indicate that the entry
128 * doesn't currently exist, leaving a pointer to the
129 * (possibly locked) directory inode in ndp->ni_dvp.
130 */
131 return (0);
132 }
984263bc
MD
133
134 /*
135 * Check for symbolic link
136 */
fad57d0e 137 KASSERT((*vpp)->v_type != VLNK || !(cnp->cn_flags & CNP_FOLLOW),
984263bc
MD
138 ("relookup: symlink found.\n"));
139
140 /*
141 * Disallow directory write attempts on read-only file systems.
142 */
fad57d0e
MD
143 if (rdonly && (cnp->cn_nameiop == NAMEI_DELETE ||
144 cnp->cn_nameiop == NAMEI_RENAME)) {
984263bc 145 error = EROFS;
fad57d0e 146 goto bad;
984263bc 147 }
fad57d0e 148 KKASSERT((cnp->cn_flags & CNP_PDIRUNLOCK) == 0);
984263bc
MD
149 return (0);
150
984263bc 151bad:
fad57d0e
MD
152 if ((cnp->cn_flags & CNP_PDIRUNLOCK) == 0) {
153 cnp->cn_flags |= CNP_PDIRUNLOCK;
a11aaa81 154 vn_unlock(dvp);
fad57d0e
MD
155 }
156 if (*vpp) {
157 vput(*vpp);
158 *vpp = NULL;
159 }
984263bc
MD
160 return (error);
161}
fad57d0e 162