ext2fs_vnops.c revision 1.136 1 1.136 thorpej /* $NetBSD: ext2fs_vnops.c,v 1.136 2021/10/20 03:08:19 thorpej Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer * (c) UNIX System Laboratories, Inc.
7 1.1 bouyer * All or some portions of this file are derived from material licensed
8 1.1 bouyer * to the University of California by American Telephone and Telegraph
9 1.1 bouyer * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1 bouyer * the permission of UNIX System Laboratories, Inc.
11 1.1 bouyer *
12 1.1 bouyer * Redistribution and use in source and binary forms, with or without
13 1.1 bouyer * modification, are permitted provided that the following conditions
14 1.1 bouyer * are met:
15 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
16 1.1 bouyer * notice, this list of conditions and the following disclaimer.
17 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
19 1.1 bouyer * documentation and/or other materials provided with the distribution.
20 1.49 agc * 3. Neither the name of the University nor the names of its contributors
21 1.49 agc * may be used to endorse or promote products derived from this software
22 1.49 agc * without specific prior written permission.
23 1.49 agc *
24 1.49 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.49 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.49 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.49 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.49 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.49 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.49 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.49 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.49 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.49 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.49 agc * SUCH DAMAGE.
35 1.49 agc *
36 1.49 agc * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
37 1.49 agc * Modified for ext2fs by Manuel Bouyer.
38 1.49 agc */
39 1.49 agc
40 1.49 agc /*
41 1.49 agc * Copyright (c) 1997 Manuel Bouyer.
42 1.49 agc *
43 1.49 agc * Redistribution and use in source and binary forms, with or without
44 1.49 agc * modification, are permitted provided that the following conditions
45 1.49 agc * are met:
46 1.49 agc * 1. Redistributions of source code must retain the above copyright
47 1.49 agc * notice, this list of conditions and the following disclaimer.
48 1.49 agc * 2. Redistributions in binary form must reproduce the above copyright
49 1.49 agc * notice, this list of conditions and the following disclaimer in the
50 1.49 agc * documentation and/or other materials provided with the distribution.
51 1.1 bouyer *
52 1.52 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 1.52 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 1.52 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 1.52 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 1.52 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 1.52 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 1.52 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 1.52 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 1.52 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 1.52 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 1.1 bouyer *
63 1.1 bouyer * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
64 1.1 bouyer * Modified for ext2fs by Manuel Bouyer.
65 1.1 bouyer */
66 1.40 lukem
67 1.40 lukem #include <sys/cdefs.h>
68 1.136 thorpej __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.136 2021/10/20 03:08:19 thorpej Exp $");
69 1.7 mrg
70 1.1 bouyer #include <sys/param.h>
71 1.1 bouyer #include <sys/systm.h>
72 1.1 bouyer #include <sys/resourcevar.h>
73 1.1 bouyer #include <sys/kernel.h>
74 1.1 bouyer #include <sys/file.h>
75 1.1 bouyer #include <sys/stat.h>
76 1.1 bouyer #include <sys/buf.h>
77 1.1 bouyer #include <sys/proc.h>
78 1.1 bouyer #include <sys/mount.h>
79 1.1 bouyer #include <sys/namei.h>
80 1.1 bouyer #include <sys/vnode.h>
81 1.1 bouyer #include <sys/lockf.h>
82 1.14 thorpej #include <sys/pool.h>
83 1.1 bouyer #include <sys/signalvar.h>
84 1.65 elad #include <sys/kauth.h>
85 1.1 bouyer
86 1.1 bouyer #include <miscfs/fifofs/fifo.h>
87 1.1 bouyer #include <miscfs/genfs/genfs.h>
88 1.1 bouyer #include <miscfs/specfs/specdev.h>
89 1.1 bouyer
90 1.1 bouyer #include <ufs/ufs/inode.h>
91 1.1 bouyer #include <ufs/ufs/ufs_extern.h>
92 1.1 bouyer #include <ufs/ufs/ufsmount.h>
93 1.1 bouyer
94 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
95 1.1 bouyer #include <ufs/ext2fs/ext2fs_extern.h>
96 1.1 bouyer #include <ufs/ext2fs/ext2fs_dir.h>
97 1.121 jdolecek #include <ufs/ext2fs/ext2fs_xattr.h>
98 1.1 bouyer
99 1.32 tsutsui extern int prtactive;
100 1.1 bouyer
101 1.67 ad static int ext2fs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *);
102 1.65 elad static int ext2fs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t,
103 1.67 ad struct lwp *);
104 1.128 hannken static int ext2fs_makeinode(struct vattr *, struct vnode *, struct vnode **,
105 1.128 hannken struct componentname *, int);
106 1.1 bouyer
107 1.1 bouyer union _qcvt {
108 1.1 bouyer int64_t qcvt;
109 1.1 bouyer int32_t val[2];
110 1.1 bouyer };
111 1.61 xtraeme
112 1.1 bouyer #define SETHIGH(q, h) { \
113 1.1 bouyer union _qcvt tmp; \
114 1.1 bouyer tmp.qcvt = (q); \
115 1.1 bouyer tmp.val[_QUAD_HIGHWORD] = (h); \
116 1.1 bouyer (q) = tmp.qcvt; \
117 1.1 bouyer }
118 1.1 bouyer #define SETLOW(q, l) { \
119 1.1 bouyer union _qcvt tmp; \
120 1.1 bouyer tmp.qcvt = (q); \
121 1.1 bouyer tmp.val[_QUAD_LOWWORD] = (l); \
122 1.1 bouyer (q) = tmp.qcvt; \
123 1.1 bouyer }
124 1.1 bouyer
125 1.1 bouyer /*
126 1.1 bouyer * Create a regular file
127 1.1 bouyer */
128 1.1 bouyer int
129 1.61 xtraeme ext2fs_create(void *v)
130 1.1 bouyer {
131 1.110 hannken struct vop_create_v3_args /* {
132 1.1 bouyer struct vnode *a_dvp;
133 1.1 bouyer struct vnode **a_vpp;
134 1.1 bouyer struct componentname *a_cnp;
135 1.1 bouyer struct vattr *a_vap;
136 1.1 bouyer } */ *ap = v;
137 1.42 jdolecek int error;
138 1.42 jdolecek
139 1.128 hannken error = ext2fs_makeinode(ap->a_vap, ap->a_dvp, ap->a_vpp, ap->a_cnp, 1);
140 1.42 jdolecek
141 1.42 jdolecek if (error)
142 1.122 christos return error;
143 1.110 hannken VOP_UNLOCK(*ap->a_vpp);
144 1.122 christos return 0;
145 1.1 bouyer }
146 1.1 bouyer
147 1.1 bouyer /*
148 1.1 bouyer * Mknod vnode call
149 1.1 bouyer */
150 1.1 bouyer /* ARGSUSED */
151 1.1 bouyer int
152 1.61 xtraeme ext2fs_mknod(void *v)
153 1.1 bouyer {
154 1.110 hannken struct vop_mknod_v3_args /* {
155 1.1 bouyer struct vnode *a_dvp;
156 1.1 bouyer struct vnode **a_vpp;
157 1.1 bouyer struct componentname *a_cnp;
158 1.1 bouyer struct vattr *a_vap;
159 1.1 bouyer } */ *ap = v;
160 1.25 augustss struct vattr *vap = ap->a_vap;
161 1.25 augustss struct vnode **vpp = ap->a_vpp;
162 1.25 augustss struct inode *ip;
163 1.1 bouyer int error;
164 1.1 bouyer
165 1.128 hannken if ((error = ext2fs_makeinode(vap, ap->a_dvp, vpp, ap->a_cnp, 1)) != 0)
166 1.122 christos return error;
167 1.1 bouyer ip = VTOI(*vpp);
168 1.1 bouyer ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
169 1.93 hannken VOP_UNLOCK(*vpp);
170 1.122 christos return 0;
171 1.1 bouyer }
172 1.1 bouyer
173 1.1 bouyer /*
174 1.1 bouyer * Open called.
175 1.1 bouyer *
176 1.1 bouyer * Just check the APPEND flag.
177 1.1 bouyer */
178 1.1 bouyer /* ARGSUSED */
179 1.1 bouyer int
180 1.61 xtraeme ext2fs_open(void *v)
181 1.1 bouyer {
182 1.1 bouyer struct vop_open_args /* {
183 1.1 bouyer struct vnode *a_vp;
184 1.1 bouyer int a_mode;
185 1.65 elad kauth_cred_t a_cred;
186 1.1 bouyer } */ *ap = v;
187 1.1 bouyer
188 1.1 bouyer /*
189 1.1 bouyer * Files marked append-only must be opened for appending.
190 1.1 bouyer */
191 1.1 bouyer if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) &&
192 1.1 bouyer (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
193 1.122 christos return EPERM;
194 1.122 christos return 0;
195 1.1 bouyer }
196 1.1 bouyer
197 1.88 elad static int
198 1.88 elad ext2fs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode)
199 1.1 bouyer {
200 1.1 bouyer
201 1.8 fvdl /*
202 1.8 fvdl * Disallow write attempts on read-only file systems;
203 1.8 fvdl * unless the file is a socket, fifo, or a block or
204 1.8 fvdl * character device resident on the file system.
205 1.8 fvdl */
206 1.8 fvdl if (mode & VWRITE) {
207 1.8 fvdl switch (vp->v_type) {
208 1.8 fvdl case VDIR:
209 1.8 fvdl case VLNK:
210 1.8 fvdl case VREG:
211 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
212 1.122 christos return EROFS;
213 1.8 fvdl break;
214 1.8 fvdl default:
215 1.8 fvdl break;
216 1.8 fvdl }
217 1.1 bouyer }
218 1.1 bouyer
219 1.1 bouyer /* If immutable bit set, nobody gets to write it. */
220 1.1 bouyer if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE))
221 1.122 christos return EPERM;
222 1.1 bouyer
223 1.88 elad return 0;
224 1.88 elad }
225 1.88 elad
226 1.88 elad static int
227 1.132 christos ext2fs_check_permitted(struct vnode *vp, struct inode *ip, accmode_t accmode,
228 1.88 elad kauth_cred_t cred)
229 1.88 elad {
230 1.88 elad
231 1.132 christos return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
232 1.132 christos vp->v_type, ip->i_e2fs_mode & ALLPERMS), vp, NULL,
233 1.132 christos genfs_can_access(vp, cred, ip->i_uid, ip->i_gid,
234 1.132 christos ip->i_e2fs_mode & ALLPERMS, NULL, accmode));
235 1.88 elad }
236 1.88 elad
237 1.88 elad int
238 1.88 elad ext2fs_access(void *v)
239 1.88 elad {
240 1.88 elad struct vop_access_args /* {
241 1.88 elad struct vnode *a_vp;
242 1.132 christos accmode_t a_accmode;
243 1.88 elad kauth_cred_t a_cred;
244 1.88 elad } */ *ap = v;
245 1.88 elad struct vnode *vp = ap->a_vp;
246 1.88 elad struct inode *ip = VTOI(vp);
247 1.132 christos accmode_t mode = ap->a_accmode;
248 1.88 elad int error;
249 1.88 elad
250 1.88 elad error = ext2fs_check_possible(vp, ip, mode);
251 1.88 elad if (error)
252 1.88 elad return error;
253 1.88 elad
254 1.88 elad error = ext2fs_check_permitted(vp, ip, mode, ap->a_cred);
255 1.88 elad
256 1.88 elad return error;
257 1.1 bouyer }
258 1.1 bouyer
259 1.1 bouyer /* ARGSUSED */
260 1.1 bouyer int
261 1.61 xtraeme ext2fs_getattr(void *v)
262 1.1 bouyer {
263 1.1 bouyer struct vop_getattr_args /* {
264 1.1 bouyer struct vnode *a_vp;
265 1.1 bouyer struct vattr *a_vap;
266 1.65 elad kauth_cred_t a_cred;
267 1.1 bouyer } */ *ap = v;
268 1.25 augustss struct vnode *vp = ap->a_vp;
269 1.25 augustss struct inode *ip = VTOI(vp);
270 1.25 augustss struct vattr *vap = ap->a_vap;
271 1.1 bouyer
272 1.62 christos EXT2FS_ITIMES(ip, NULL, NULL, NULL);
273 1.1 bouyer /*
274 1.1 bouyer * Copy from inode table
275 1.1 bouyer */
276 1.1 bouyer vap->va_fsid = ip->i_dev;
277 1.1 bouyer vap->va_fileid = ip->i_number;
278 1.1 bouyer vap->va_mode = ip->i_e2fs_mode & ALLPERMS;
279 1.1 bouyer vap->va_nlink = ip->i_e2fs_nlink;
280 1.83 mrg vap->va_uid = ip->i_uid;
281 1.83 mrg vap->va_gid = ip->i_gid;
282 1.44 fvdl vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din->e2di_rdev);
283 1.37 chs vap->va_size = vp->v_size;
284 1.119 jdolecek EXT2_DINODE_TIME_GET(&vap->va_atime, ip->i_din.e2fs_din, e2di_atime, EXT2_DINODE_SIZE(ip->i_e2fs));
285 1.119 jdolecek EXT2_DINODE_TIME_GET(&vap->va_mtime, ip->i_din.e2fs_din, e2di_mtime, EXT2_DINODE_SIZE(ip->i_e2fs));
286 1.119 jdolecek EXT2_DINODE_TIME_GET(&vap->va_ctime, ip->i_din.e2fs_din, e2di_ctime, EXT2_DINODE_SIZE(ip->i_e2fs));
287 1.119 jdolecek if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) {
288 1.119 jdolecek EXT2_DINODE_TIME_GET(&vap->va_birthtime, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs));
289 1.119 jdolecek }
290 1.120 jdolecek
291 1.120 jdolecek vap->va_flags = 0;
292 1.120 jdolecek vap->va_flags |= (ip->i_e2fs_flags & EXT2_NODUMP) ? UF_NODUMP : 0;
293 1.1 bouyer #ifdef EXT2FS_SYSTEM_FLAGS
294 1.1 bouyer vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0;
295 1.120 jdolecek vap->va_flags |= (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0;
296 1.1 bouyer #else
297 1.1 bouyer vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0;
298 1.120 jdolecek vap->va_flags |= (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0;
299 1.1 bouyer #endif
300 1.120 jdolecek
301 1.1 bouyer vap->va_gen = ip->i_e2fs_gen;
302 1.1 bouyer /* this doesn't belong here */
303 1.1 bouyer if (vp->v_type == VBLK)
304 1.1 bouyer vap->va_blocksize = BLKDEV_IOSIZE;
305 1.1 bouyer else if (vp->v_type == VCHR)
306 1.1 bouyer vap->va_blocksize = MAXBSIZE;
307 1.1 bouyer else
308 1.1 bouyer vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
309 1.105 jakllsch vap->va_bytes = dbtob(ext2fs_nblock(ip));
310 1.1 bouyer vap->va_type = vp->v_type;
311 1.1 bouyer vap->va_filerev = ip->i_modrev;
312 1.122 christos return 0;
313 1.1 bouyer }
314 1.1 bouyer
315 1.1 bouyer /*
316 1.1 bouyer * Set attribute vnode op. called from several syscalls
317 1.1 bouyer */
318 1.1 bouyer int
319 1.61 xtraeme ext2fs_setattr(void *v)
320 1.1 bouyer {
321 1.1 bouyer struct vop_setattr_args /* {
322 1.1 bouyer struct vnode *a_vp;
323 1.1 bouyer struct vattr *a_vap;
324 1.65 elad kauth_cred_t a_cred;
325 1.1 bouyer } */ *ap = v;
326 1.8 fvdl struct vattr *vap = ap->a_vap;
327 1.8 fvdl struct vnode *vp = ap->a_vp;
328 1.8 fvdl struct inode *ip = VTOI(vp);
329 1.65 elad kauth_cred_t cred = ap->a_cred;
330 1.76 pooka struct lwp *l = curlwp;
331 1.1 bouyer int error;
332 1.102 elad kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
333 1.102 elad bool changing_sysflags = false;
334 1.1 bouyer
335 1.1 bouyer /*
336 1.1 bouyer * Check for unsettable attributes.
337 1.1 bouyer */
338 1.5 christos if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
339 1.24 thorpej (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
340 1.24 thorpej (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
341 1.24 thorpej ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
342 1.122 christos return EINVAL;
343 1.1 bouyer }
344 1.1 bouyer if (vap->va_flags != VNOVAL) {
345 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
346 1.122 christos return EROFS;
347 1.102 elad
348 1.102 elad /*
349 1.102 elad * Check if we're allowed to change the flags.
350 1.102 elad * If EXT2FS_SYSTEM_FLAGS is set, then the flags are treated
351 1.102 elad * as system flags, otherwise they're considered to be user
352 1.102 elad * flags.
353 1.102 elad */
354 1.102 elad #ifdef EXT2FS_SYSTEM_FLAGS
355 1.102 elad /* Indicate we're changing system flags if we are. */
356 1.102 elad if ((vap->va_flags & SF_APPEND) ||
357 1.102 elad (vap->va_flags & SF_IMMUTABLE)) {
358 1.102 elad action |= KAUTH_VNODE_WRITE_SYSFLAGS;
359 1.102 elad changing_sysflags = true;
360 1.102 elad }
361 1.102 elad
362 1.102 elad /* Indicate the node has system flags if it does. */
363 1.102 elad if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) {
364 1.102 elad action |= KAUTH_VNODE_HAS_SYSFLAGS;
365 1.102 elad }
366 1.102 elad #endif /* EXT2FS_SYSTEM_FLAGS */
367 1.102 elad
368 1.102 elad error = kauth_authorize_vnode(cred, action, vp, NULL,
369 1.132 christos genfs_can_chflags(vp, cred, ip->i_uid, changing_sysflags));
370 1.102 elad if (error)
371 1.122 christos return error;
372 1.102 elad
373 1.120 jdolecek ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE | EXT2_NODUMP);
374 1.1 bouyer #ifdef EXT2FS_SYSTEM_FLAGS
375 1.102 elad ip->i_e2fs_flags |=
376 1.102 elad (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 |
377 1.102 elad (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
378 1.1 bouyer #else
379 1.24 thorpej ip->i_e2fs_flags |=
380 1.24 thorpej (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 |
381 1.24 thorpej (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE : 0;
382 1.1 bouyer #endif
383 1.120 jdolecek ip->i_e2fs_flags |=
384 1.120 jdolecek (vap->va_flags & UF_NODUMP) ? EXT2_NODUMP : 0;
385 1.1 bouyer ip->i_flag |= IN_CHANGE;
386 1.1 bouyer if (vap->va_flags & (IMMUTABLE | APPEND))
387 1.122 christos return 0;
388 1.1 bouyer }
389 1.1 bouyer if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE))
390 1.122 christos return EPERM;
391 1.1 bouyer /*
392 1.1 bouyer * Go through the fields and update iff not VNOVAL.
393 1.1 bouyer */
394 1.1 bouyer if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
395 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
396 1.122 christos return EROFS;
397 1.67 ad error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
398 1.1 bouyer if (error)
399 1.122 christos return error;
400 1.1 bouyer }
401 1.1 bouyer if (vap->va_size != VNOVAL) {
402 1.8 fvdl /*
403 1.8 fvdl * Disallow write attempts on read-only file systems;
404 1.8 fvdl * unless the file is a socket, fifo, or a block or
405 1.8 fvdl * character device resident on the file system.
406 1.8 fvdl */
407 1.8 fvdl switch (vp->v_type) {
408 1.8 fvdl case VDIR:
409 1.122 christos return EISDIR;
410 1.8 fvdl case VLNK:
411 1.8 fvdl case VREG:
412 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
413 1.122 christos return EROFS;
414 1.8 fvdl default:
415 1.8 fvdl break;
416 1.8 fvdl }
417 1.77 pooka error = ext2fs_truncate(vp, vap->va_size, 0, cred);
418 1.1 bouyer if (error)
419 1.122 christos return error;
420 1.1 bouyer }
421 1.1 bouyer ip = VTOI(vp);
422 1.119 jdolecek if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || vap->va_birthtime.tv_sec != VNOVAL) {
423 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
424 1.122 christos return EROFS;
425 1.102 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
426 1.132 christos NULL, genfs_can_chtimes(vp, cred, ip->i_uid,
427 1.132 christos vap->va_vaflags));
428 1.86 elad if (error)
429 1.122 christos return error;
430 1.1 bouyer if (vap->va_atime.tv_sec != VNOVAL)
431 1.1 bouyer if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
432 1.1 bouyer ip->i_flag |= IN_ACCESS;
433 1.101 christos if (vap->va_mtime.tv_sec != VNOVAL) {
434 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
435 1.101 christos if (vp->v_mount->mnt_flag & MNT_RELATIME)
436 1.101 christos ip->i_flag |= IN_ACCESS;
437 1.101 christos }
438 1.119 jdolecek if (vap->va_birthtime.tv_sec != VNOVAL &&
439 1.119 jdolecek EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) {
440 1.119 jdolecek
441 1.119 jdolecek EXT2_DINODE_TIME_SET(&vap->va_birthtime, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs));
442 1.119 jdolecek }
443 1.63 yamt error = ext2fs_update(vp, &vap->va_atime, &vap->va_mtime,
444 1.26 perseant UPDATE_WAIT);
445 1.1 bouyer if (error)
446 1.122 christos return error;
447 1.1 bouyer }
448 1.1 bouyer error = 0;
449 1.8 fvdl if (vap->va_mode != (mode_t)VNOVAL) {
450 1.8 fvdl if (vp->v_mount->mnt_flag & MNT_RDONLY)
451 1.122 christos return EROFS;
452 1.67 ad error = ext2fs_chmod(vp, (int)vap->va_mode, cred, l);
453 1.8 fvdl }
454 1.122 christos return error;
455 1.1 bouyer }
456 1.1 bouyer
457 1.1 bouyer /*
458 1.1 bouyer * Change the mode on a file.
459 1.1 bouyer * Inode must be locked before calling.
460 1.1 bouyer */
461 1.1 bouyer static int
462 1.67 ad ext2fs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l)
463 1.1 bouyer {
464 1.25 augustss struct inode *ip = VTOI(vp);
465 1.84 elad int error;
466 1.1 bouyer
467 1.102 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
468 1.132 christos NULL, genfs_can_chmod(vp, cred, ip->i_uid, ip->i_gid, mode));
469 1.84 elad if (error)
470 1.122 christos return error;
471 1.84 elad
472 1.1 bouyer ip->i_e2fs_mode &= ~ALLPERMS;
473 1.1 bouyer ip->i_e2fs_mode |= (mode & ALLPERMS);
474 1.1 bouyer ip->i_flag |= IN_CHANGE;
475 1.122 christos return 0;
476 1.1 bouyer }
477 1.1 bouyer
478 1.1 bouyer /*
479 1.1 bouyer * Perform chown operation on inode ip;
480 1.1 bouyer * inode must be locked prior to call.
481 1.1 bouyer */
482 1.1 bouyer static int
483 1.65 elad ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
484 1.67 ad struct lwp *l)
485 1.1 bouyer {
486 1.25 augustss struct inode *ip = VTOI(vp);
487 1.1 bouyer uid_t ouid;
488 1.1 bouyer gid_t ogid;
489 1.84 elad int error;
490 1.1 bouyer
491 1.1 bouyer if (uid == (uid_t)VNOVAL)
492 1.83 mrg uid = ip->i_uid;
493 1.1 bouyer if (gid == (gid_t)VNOVAL)
494 1.83 mrg gid = ip->i_gid;
495 1.84 elad
496 1.102 elad error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
497 1.132 christos NULL, genfs_can_chown(vp, cred, ip->i_uid, ip->i_gid, uid, gid));
498 1.84 elad if (error)
499 1.122 christos return error;
500 1.84 elad
501 1.83 mrg ogid = ip->i_gid;
502 1.83 mrg ouid = ip->i_uid;
503 1.1 bouyer
504 1.83 mrg ip->i_e2fs_gid = gid & 0xffff;
505 1.83 mrg ip->i_e2fs_uid = uid & 0xffff;
506 1.83 mrg if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
507 1.83 mrg ip->i_e2fs_gid_high = (gid >> 16) & 0xffff;
508 1.83 mrg ip->i_e2fs_uid_high = (uid >> 16) & 0xffff;
509 1.83 mrg } else {
510 1.83 mrg ip->i_e2fs_gid_high = 0;
511 1.83 mrg ip->i_e2fs_uid_high = 0;
512 1.83 mrg }
513 1.91 pooka if (ouid != uid || ogid != gid) {
514 1.91 pooka ext2fs_set_inode_guid(ip);
515 1.1 bouyer ip->i_flag |= IN_CHANGE;
516 1.91 pooka }
517 1.102 elad if (ouid != uid && (ip->i_e2fs_mode & ISUID) &&
518 1.102 elad kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SUID,
519 1.102 elad vp, NULL, EPERM) != 0)
520 1.1 bouyer ip->i_e2fs_mode &= ~ISUID;
521 1.102 elad if (ogid != gid && (ip->i_e2fs_mode & ISGID) &&
522 1.102 elad kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SGID,
523 1.102 elad vp, NULL, EPERM) != 0)
524 1.1 bouyer ip->i_e2fs_mode &= ~ISGID;
525 1.122 christos return 0;
526 1.1 bouyer }
527 1.1 bouyer
528 1.1 bouyer int
529 1.61 xtraeme ext2fs_remove(void *v)
530 1.1 bouyer {
531 1.136 thorpej struct vop_remove_v3_args /* {
532 1.1 bouyer struct vnode *a_dvp;
533 1.1 bouyer struct vnode *a_vp;
534 1.1 bouyer struct componentname *a_cnp;
535 1.136 thorpej nlink_t ctx_vp_new_nlink;
536 1.1 bouyer } */ *ap = v;
537 1.8 fvdl struct inode *ip;
538 1.8 fvdl struct vnode *vp = ap->a_vp;
539 1.8 fvdl struct vnode *dvp = ap->a_dvp;
540 1.100 dholland struct ufs_lookup_results *ulr;
541 1.1 bouyer int error;
542 1.1 bouyer
543 1.100 dholland /* XXX should handle this material another way */
544 1.100 dholland ulr = &VTOI(dvp)->i_crap;
545 1.100 dholland UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
546 1.100 dholland
547 1.1 bouyer ip = VTOI(vp);
548 1.2 fvdl if (vp->v_type == VDIR ||
549 1.2 fvdl (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) ||
550 1.1 bouyer (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) {
551 1.1 bouyer error = EPERM;
552 1.43 jdolecek } else {
553 1.100 dholland error = ext2fs_dirremove(dvp, ulr, ap->a_cnp);
554 1.43 jdolecek if (error == 0) {
555 1.43 jdolecek ip->i_e2fs_nlink--;
556 1.43 jdolecek ip->i_flag |= IN_CHANGE;
557 1.136 thorpej ap->ctx_vp_new_nlink = ip->i_e2fs_nlink;
558 1.43 jdolecek }
559 1.1 bouyer }
560 1.43 jdolecek
561 1.1 bouyer if (dvp == vp)
562 1.1 bouyer vrele(vp);
563 1.1 bouyer else
564 1.1 bouyer vput(vp);
565 1.122 christos return error;
566 1.1 bouyer }
567 1.1 bouyer
568 1.1 bouyer /*
569 1.98 rmind * ext2fs_link: create hard link.
570 1.1 bouyer */
571 1.1 bouyer int
572 1.61 xtraeme ext2fs_link(void *v)
573 1.1 bouyer {
574 1.117 riastrad struct vop_link_v2_args /* {
575 1.1 bouyer struct vnode *a_dvp;
576 1.1 bouyer struct vnode *a_vp;
577 1.1 bouyer struct componentname *a_cnp;
578 1.1 bouyer } */ *ap = v;
579 1.8 fvdl struct vnode *dvp = ap->a_dvp;
580 1.8 fvdl struct vnode *vp = ap->a_vp;
581 1.8 fvdl struct componentname *cnp = ap->a_cnp;
582 1.8 fvdl struct inode *ip;
583 1.1 bouyer int error;
584 1.100 dholland struct ufs_lookup_results *ulr;
585 1.1 bouyer
586 1.98 rmind KASSERT(dvp != vp);
587 1.98 rmind KASSERT(vp->v_type != VDIR);
588 1.98 rmind KASSERT(dvp->v_mount == vp->v_mount);
589 1.98 rmind
590 1.100 dholland /* XXX should handle this material another way */
591 1.100 dholland ulr = &VTOI(dvp)->i_crap;
592 1.100 dholland UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
593 1.100 dholland
594 1.98 rmind error = vn_lock(vp, LK_EXCLUSIVE);
595 1.98 rmind if (error) {
596 1.1 bouyer VOP_ABORTOP(dvp, cnp);
597 1.1 bouyer goto out2;
598 1.1 bouyer }
599 1.1 bouyer ip = VTOI(vp);
600 1.125 jdolecek if ((nlink_t)ip->i_e2fs_nlink >= EXT2FS_LINK_MAX) {
601 1.1 bouyer VOP_ABORTOP(dvp, cnp);
602 1.1 bouyer error = EMLINK;
603 1.1 bouyer goto out1;
604 1.1 bouyer }
605 1.1 bouyer if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) {
606 1.1 bouyer VOP_ABORTOP(dvp, cnp);
607 1.1 bouyer error = EPERM;
608 1.1 bouyer goto out1;
609 1.1 bouyer }
610 1.1 bouyer ip->i_e2fs_nlink++;
611 1.1 bouyer ip->i_flag |= IN_CHANGE;
612 1.63 yamt error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT);
613 1.1 bouyer if (!error)
614 1.100 dholland error = ext2fs_direnter(ip, dvp, ulr, cnp);
615 1.1 bouyer if (error) {
616 1.1 bouyer ip->i_e2fs_nlink--;
617 1.1 bouyer ip->i_flag |= IN_CHANGE;
618 1.1 bouyer }
619 1.1 bouyer out1:
620 1.98 rmind VOP_UNLOCK(vp);
621 1.1 bouyer out2:
622 1.122 christos return error;
623 1.1 bouyer }
624 1.1 bouyer
625 1.1 bouyer /*
626 1.1 bouyer * Mkdir system call
627 1.1 bouyer */
628 1.1 bouyer int
629 1.61 xtraeme ext2fs_mkdir(void *v)
630 1.1 bouyer {
631 1.110 hannken struct vop_mkdir_v3_args /* {
632 1.1 bouyer struct vnode *a_dvp;
633 1.1 bouyer struct vnode **a_vpp;
634 1.1 bouyer struct componentname *a_cnp;
635 1.1 bouyer struct vattr *a_vap;
636 1.1 bouyer } */ *ap = v;
637 1.55 mycroft struct vnode *dvp = ap->a_dvp;
638 1.55 mycroft struct componentname *cnp = ap->a_cnp;
639 1.55 mycroft struct inode *ip, *dp = VTOI(dvp);
640 1.55 mycroft struct vnode *tvp;
641 1.4 bouyer struct ext2fs_dirtemplate dirtemplate;
642 1.124 jdolecek int error;
643 1.100 dholland struct ufs_lookup_results *ulr;
644 1.100 dholland
645 1.100 dholland /* XXX should handle this material another way */
646 1.100 dholland ulr = &VTOI(dvp)->i_crap;
647 1.100 dholland UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
648 1.1 bouyer
649 1.128 hannken KASSERT(ap->a_vap->va_type == VDIR);
650 1.128 hannken
651 1.1 bouyer /*
652 1.124 jdolecek * Acquire the inode, but don't sync/direnter it just yet
653 1.1 bouyer */
654 1.128 hannken error = ext2fs_makeinode(ap->a_vap, ap->a_dvp, &tvp, ap->a_cnp, 0);
655 1.124 jdolecek if (error)
656 1.1 bouyer goto out;
657 1.124 jdolecek
658 1.124 jdolecek /* the link count is going to be 2 when all is done */
659 1.1 bouyer ip = VTOI(tvp);
660 1.1 bouyer ip->i_e2fs_nlink = 2;
661 1.1 bouyer
662 1.1 bouyer /*
663 1.1 bouyer * Bump link count in parent directory
664 1.1 bouyer * to reflect work done below. Should
665 1.1 bouyer * be done before reference is created
666 1.1 bouyer * so reparation is possible if we crash.
667 1.1 bouyer */
668 1.125 jdolecek if (dp->i_e2fs_nlink != EXT2FS_LINK_INF)
669 1.125 jdolecek dp->i_e2fs_nlink++;
670 1.125 jdolecek
671 1.125 jdolecek /*
672 1.125 jdolecek * If we hit the link limit, for directories just set the nlink
673 1.125 jdolecek * to special value 1, which means the link count is bigger
674 1.125 jdolecek * than EXT2FS_LINK_MAX.
675 1.125 jdolecek */
676 1.125 jdolecek if ((nlink_t)dp->i_e2fs_nlink >= EXT2FS_LINK_MAX) {
677 1.125 jdolecek dp->i_e2fs_nlink = EXT2FS_LINK_INF;
678 1.125 jdolecek
679 1.125 jdolecek /* set the feature flag DIR_NLINK if not set already */
680 1.125 jdolecek if (!EXT2F_HAS_ROCOMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) {
681 1.125 jdolecek dp->i_e2fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_DIR_NLINK;
682 1.125 jdolecek dp->i_e2fs->e2fs_fmod = 1;
683 1.125 jdolecek }
684 1.125 jdolecek }
685 1.125 jdolecek
686 1.1 bouyer dp->i_flag |= IN_CHANGE;
687 1.63 yamt if ((error = ext2fs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0)
688 1.1 bouyer goto bad;
689 1.1 bouyer
690 1.1 bouyer /* Initialize directory with "." and ".." from static template. */
691 1.13 perry memset(&dirtemplate, 0, sizeof(dirtemplate));
692 1.4 bouyer dirtemplate.dot_ino = h2fs32(ip->i_number);
693 1.4 bouyer dirtemplate.dot_reclen = h2fs16(12);
694 1.22 bouyer dirtemplate.dot_namlen = 1;
695 1.123 jdolecek if (EXT2F_HAS_INCOMPAT_FEATURE(dp->i_e2fs, EXT2F_INCOMPAT_FTYPE)) {
696 1.23 bouyer dirtemplate.dot_type = EXT2_FT_DIR;
697 1.22 bouyer }
698 1.4 bouyer dirtemplate.dot_name[0] = '.';
699 1.4 bouyer dirtemplate.dotdot_ino = h2fs32(dp->i_number);
700 1.123 jdolecek dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12);
701 1.22 bouyer dirtemplate.dotdot_namlen = 2;
702 1.123 jdolecek if (EXT2F_HAS_INCOMPAT_FEATURE(dp->i_e2fs, EXT2F_INCOMPAT_FTYPE)) {
703 1.23 bouyer dirtemplate.dotdot_type = EXT2_FT_DIR;
704 1.22 bouyer }
705 1.4 bouyer dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.';
706 1.116 riastrad error = ufs_bufio(UIO_WRITE, tvp, (void *)&dirtemplate,
707 1.116 riastrad sizeof (dirtemplate), (off_t)0, IO_NODELOCKED|IO_SYNC,
708 1.116 riastrad cnp->cn_cred, (size_t *)0, NULL);
709 1.1 bouyer if (error) {
710 1.125 jdolecek if (dp->i_e2fs_nlink != EXT2FS_LINK_INF)
711 1.125 jdolecek dp->i_e2fs_nlink--;
712 1.1 bouyer dp->i_flag |= IN_CHANGE;
713 1.1 bouyer goto bad;
714 1.1 bouyer }
715 1.55 mycroft if (VTOI(dvp)->i_e2fs->e2fs_bsize > dvp->v_mount->mnt_stat.f_bsize)
716 1.1 bouyer panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
717 1.1 bouyer else {
718 1.58 ws error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize);
719 1.58 ws if (error) {
720 1.125 jdolecek if (dp->i_e2fs_nlink != EXT2FS_LINK_INF)
721 1.125 jdolecek dp->i_e2fs_nlink--;
722 1.58 ws dp->i_flag |= IN_CHANGE;
723 1.58 ws goto bad;
724 1.58 ws }
725 1.1 bouyer ip->i_flag |= IN_CHANGE;
726 1.60 kml uvm_vnp_setsize(tvp, ext2fs_size(ip));
727 1.1 bouyer }
728 1.1 bouyer
729 1.114 snj /* Directory set up, now install its entry in the parent directory. */
730 1.100 dholland error = ext2fs_direnter(ip, dvp, ulr, cnp);
731 1.1 bouyer if (error != 0) {
732 1.125 jdolecek if (dp->i_e2fs_nlink != EXT2FS_LINK_INF)
733 1.125 jdolecek dp->i_e2fs_nlink--;
734 1.1 bouyer dp->i_flag |= IN_CHANGE;
735 1.1 bouyer }
736 1.1 bouyer bad:
737 1.1 bouyer /*
738 1.63 yamt * No need to do an explicit ext2fs_truncate here, vrele will do this
739 1.1 bouyer * for us because we set the link count to 0.
740 1.1 bouyer */
741 1.1 bouyer if (error) {
742 1.1 bouyer ip->i_e2fs_nlink = 0;
743 1.1 bouyer ip->i_flag |= IN_CHANGE;
744 1.1 bouyer vput(tvp);
745 1.42 jdolecek } else {
746 1.110 hannken VOP_UNLOCK(tvp);
747 1.1 bouyer *ap->a_vpp = tvp;
748 1.42 jdolecek }
749 1.1 bouyer out:
750 1.122 christos return error;
751 1.1 bouyer }
752 1.1 bouyer
753 1.1 bouyer /*
754 1.1 bouyer * Rmdir system call.
755 1.1 bouyer */
756 1.1 bouyer int
757 1.61 xtraeme ext2fs_rmdir(void *v)
758 1.1 bouyer {
759 1.126 riastrad struct vop_rmdir_v2_args /* {
760 1.1 bouyer struct vnode *a_dvp;
761 1.1 bouyer struct vnode *a_vp;
762 1.1 bouyer struct componentname *a_cnp;
763 1.1 bouyer } */ *ap = v;
764 1.25 augustss struct vnode *vp = ap->a_vp;
765 1.25 augustss struct vnode *dvp = ap->a_dvp;
766 1.25 augustss struct componentname *cnp = ap->a_cnp;
767 1.25 augustss struct inode *ip, *dp;
768 1.1 bouyer int error;
769 1.100 dholland struct ufs_lookup_results *ulr;
770 1.1 bouyer
771 1.1 bouyer ip = VTOI(vp);
772 1.1 bouyer dp = VTOI(dvp);
773 1.100 dholland
774 1.100 dholland /* XXX should handle this material another way */
775 1.100 dholland ulr = &dp->i_crap;
776 1.100 dholland UFS_CHECK_CRAPCOUNTER(dp);
777 1.100 dholland
778 1.1 bouyer /*
779 1.1 bouyer * No rmdir "." please.
780 1.1 bouyer */
781 1.1 bouyer if (dp == ip) {
782 1.126 riastrad vrele(vp);
783 1.122 christos return EINVAL;
784 1.1 bouyer }
785 1.1 bouyer /*
786 1.1 bouyer * Verify the directory is empty (and valid).
787 1.1 bouyer * (Rmdir ".." won't be valid since
788 1.1 bouyer * ".." will contain a reference to
789 1.1 bouyer * the current directory and thus be
790 1.1 bouyer * non-empty.)
791 1.1 bouyer */
792 1.1 bouyer error = 0;
793 1.125 jdolecek if ((ip->i_e2fs_nlink != 2 && ip->i_e2fs_nlink != EXT2FS_LINK_INF) ||
794 1.1 bouyer !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) {
795 1.1 bouyer error = ENOTEMPTY;
796 1.1 bouyer goto out;
797 1.1 bouyer }
798 1.1 bouyer if ((dp->i_e2fs_flags & EXT2_APPEND) ||
799 1.1 bouyer (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) {
800 1.1 bouyer error = EPERM;
801 1.1 bouyer goto out;
802 1.1 bouyer }
803 1.1 bouyer /*
804 1.1 bouyer * Delete reference to directory before purging
805 1.1 bouyer * inode. If we crash in between, the directory
806 1.1 bouyer * will be reattached to lost+found,
807 1.1 bouyer */
808 1.100 dholland error = ext2fs_dirremove(dvp, ulr, cnp);
809 1.1 bouyer if (error != 0)
810 1.1 bouyer goto out;
811 1.125 jdolecek if (dp->i_e2fs_nlink != EXT2FS_LINK_INF)
812 1.125 jdolecek dp->i_e2fs_nlink--;
813 1.1 bouyer dp->i_flag |= IN_CHANGE;
814 1.1 bouyer cache_purge(dvp);
815 1.1 bouyer /*
816 1.1 bouyer * Truncate inode. The only stuff left
817 1.1 bouyer * in the directory is "." and "..". The
818 1.1 bouyer * "." reference is inconsequential since
819 1.1 bouyer * we're quashing it. The ".." reference
820 1.1 bouyer * has already been adjusted above. We've
821 1.1 bouyer * removed the "." reference and the reference
822 1.1 bouyer * in the parent directory, but there may be
823 1.1 bouyer * other hard links so decrement by 2 and
824 1.1 bouyer * worry about them later.
825 1.1 bouyer */
826 1.1 bouyer ip->i_e2fs_nlink -= 2;
827 1.77 pooka error = ext2fs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
828 1.1 bouyer cache_purge(ITOV(ip));
829 1.1 bouyer out:
830 1.1 bouyer vput(vp);
831 1.122 christos return error;
832 1.1 bouyer }
833 1.1 bouyer
834 1.1 bouyer /*
835 1.1 bouyer * symlink -- make a symbolic link
836 1.1 bouyer */
837 1.1 bouyer int
838 1.61 xtraeme ext2fs_symlink(void *v)
839 1.1 bouyer {
840 1.110 hannken struct vop_symlink_v3_args /* {
841 1.1 bouyer struct vnode *a_dvp;
842 1.1 bouyer struct vnode **a_vpp;
843 1.1 bouyer struct componentname *a_cnp;
844 1.1 bouyer struct vattr *a_vap;
845 1.1 bouyer char *a_target;
846 1.1 bouyer } */ *ap = v;
847 1.55 mycroft struct vnode *vp, **vpp;
848 1.55 mycroft struct inode *ip;
849 1.55 mycroft int len, error;
850 1.1 bouyer
851 1.55 mycroft vpp = ap->a_vpp;
852 1.128 hannken KASSERT(ap->a_vap->va_type == VLNK);
853 1.128 hannken error = ext2fs_makeinode(ap->a_vap, ap->a_dvp, vpp, ap->a_cnp, 1);
854 1.1 bouyer if (error)
855 1.122 christos return error;
856 1.1 bouyer vp = *vpp;
857 1.1 bouyer len = strlen(ap->a_target);
858 1.55 mycroft ip = VTOI(vp);
859 1.55 mycroft if (len < ip->i_ump->um_maxsymlinklen) {
860 1.122 christos memcpy(ip->i_din.e2fs_din->e2di_shortlink, ap->a_target, len);
861 1.58 ws error = ext2fs_setsize(ip, len);
862 1.58 ws if (error)
863 1.58 ws goto bad;
864 1.1 bouyer ip->i_flag |= IN_CHANGE | IN_UPDATE;
865 1.101 christos if (vp->v_mount->mnt_flag & MNT_RELATIME)
866 1.101 christos ip->i_flag |= IN_ACCESS;
867 1.60 kml uvm_vnp_setsize(vp, len);
868 1.1 bouyer } else
869 1.116 riastrad error = ufs_bufio(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
870 1.116 riastrad IO_NODELOCKED, ap->a_cnp->cn_cred, (size_t *)0, NULL);
871 1.58 ws bad:
872 1.110 hannken VOP_UNLOCK(vp);
873 1.34 assar if (error)
874 1.110 hannken vrele(vp);
875 1.122 christos return error;
876 1.1 bouyer }
877 1.1 bouyer
878 1.1 bouyer /*
879 1.1 bouyer * Return target name of a symbolic link
880 1.1 bouyer */
881 1.1 bouyer int
882 1.61 xtraeme ext2fs_readlink(void *v)
883 1.1 bouyer {
884 1.1 bouyer struct vop_readlink_args /* {
885 1.1 bouyer struct vnode *a_vp;
886 1.1 bouyer struct uio *a_uio;
887 1.65 elad kauth_cred_t a_cred;
888 1.1 bouyer } */ *ap = v;
889 1.55 mycroft struct vnode *vp = ap->a_vp;
890 1.55 mycroft struct inode *ip = VTOI(vp);
891 1.55 mycroft struct ufsmount *ump = ip->i_ump;
892 1.55 mycroft int isize;
893 1.1 bouyer
894 1.58 ws isize = ext2fs_size(ip);
895 1.55 mycroft if (isize < ump->um_maxsymlinklen ||
896 1.105 jakllsch (ump->um_maxsymlinklen == 0 && ext2fs_nblock(ip) == 0)) {
897 1.122 christos uiomove(ip->i_din.e2fs_din->e2di_shortlink, isize, ap->a_uio);
898 1.122 christos return 0;
899 1.1 bouyer }
900 1.122 christos return UFS_BUFRD(vp, ap->a_uio, 0, ap->a_cred);
901 1.1 bouyer }
902 1.1 bouyer
903 1.1 bouyer /*
904 1.1 bouyer * Advisory record locking support
905 1.1 bouyer */
906 1.1 bouyer int
907 1.61 xtraeme ext2fs_advlock(void *v)
908 1.1 bouyer {
909 1.1 bouyer struct vop_advlock_args /* {
910 1.1 bouyer struct vnode *a_vp;
911 1.74 christos void * a_id;
912 1.1 bouyer int a_op;
913 1.1 bouyer struct flock *a_fl;
914 1.1 bouyer int a_flags;
915 1.1 bouyer } */ *ap = v;
916 1.25 augustss struct inode *ip = VTOI(ap->a_vp);
917 1.1 bouyer
918 1.58 ws return lf_advlock(ap, &ip->i_lockf, ext2fs_size(ip));
919 1.1 bouyer }
920 1.1 bouyer
921 1.63 yamt int
922 1.63 yamt ext2fs_fsync(void *v)
923 1.63 yamt {
924 1.63 yamt struct vop_fsync_args /* {
925 1.63 yamt struct vnode *a_vp;
926 1.65 elad kauth_cred_t a_cred;
927 1.63 yamt int a_flags;
928 1.63 yamt off_t offlo;
929 1.63 yamt off_t offhi;
930 1.63 yamt struct proc *a_p;
931 1.63 yamt } */ *ap = v;
932 1.63 yamt struct vnode *vp = ap->a_vp;
933 1.63 yamt int wait;
934 1.63 yamt int error;
935 1.63 yamt
936 1.63 yamt wait = (ap->a_flags & FSYNC_WAIT) != 0;
937 1.82 ad
938 1.82 ad if (vp->v_type == VBLK)
939 1.99 hannken error = spec_fsync(v);
940 1.63 yamt else
941 1.103 chs error = vflushbuf(vp, ap->a_flags);
942 1.99 hannken if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
943 1.63 yamt error = ext2fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
944 1.63 yamt
945 1.63 yamt if (error == 0 && ap->a_flags & FSYNC_CACHE) {
946 1.63 yamt int l = 0;
947 1.63 yamt error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE,
948 1.76 pooka curlwp->l_cred);
949 1.63 yamt }
950 1.63 yamt
951 1.63 yamt return error;
952 1.63 yamt }
953 1.63 yamt
954 1.1 bouyer /*
955 1.1 bouyer * Initialize the vnode associated with a new inode, handle aliased
956 1.1 bouyer * vnodes.
957 1.1 bouyer */
958 1.1 bouyer int
959 1.61 xtraeme ext2fs_vinit(struct mount *mntp, int (**specops)(void *),
960 1.61 xtraeme int (**fifoops)(void *), struct vnode **vpp)
961 1.1 bouyer {
962 1.66 kardel struct timeval tv;
963 1.1 bouyer struct inode *ip;
964 1.80 ad struct vnode *vp;
965 1.1 bouyer
966 1.1 bouyer vp = *vpp;
967 1.1 bouyer ip = VTOI(vp);
968 1.1 bouyer switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) {
969 1.1 bouyer case VCHR:
970 1.1 bouyer case VBLK:
971 1.1 bouyer vp->v_op = specops;
972 1.80 ad spec_node_init(vp, fs2h32(ip->i_din.e2fs_din->e2di_rdev));
973 1.1 bouyer break;
974 1.1 bouyer case VFIFO:
975 1.1 bouyer vp->v_op = fifoops;
976 1.1 bouyer break;
977 1.1 bouyer case VNON:
978 1.1 bouyer case VBAD:
979 1.1 bouyer case VSOCK:
980 1.1 bouyer case VLNK:
981 1.1 bouyer case VDIR:
982 1.1 bouyer case VREG:
983 1.1 bouyer break;
984 1.1 bouyer }
985 1.106 dholland if (ip->i_number == UFS_ROOTINO)
986 1.75 ad vp->v_vflag |= VV_ROOT;
987 1.1 bouyer /*
988 1.1 bouyer * Initialize modrev times
989 1.1 bouyer */
990 1.66 kardel getmicrouptime(&tv);
991 1.66 kardel SETHIGH(ip->i_modrev, tv.tv_sec);
992 1.131 kamil SETLOW(ip->i_modrev, tv.tv_usec * 4294U);
993 1.1 bouyer *vpp = vp;
994 1.122 christos return 0;
995 1.1 bouyer }
996 1.1 bouyer
997 1.1 bouyer /*
998 1.1 bouyer * Allocate a new inode.
999 1.1 bouyer */
1000 1.128 hannken static int
1001 1.128 hannken ext2fs_makeinode(struct vattr *vap, struct vnode *dvp, struct vnode **vpp,
1002 1.124 jdolecek struct componentname *cnp, int do_direnter)
1003 1.1 bouyer {
1004 1.25 augustss struct inode *ip, *pdir;
1005 1.1 bouyer struct vnode *tvp;
1006 1.102 elad int error;
1007 1.100 dholland struct ufs_lookup_results *ulr;
1008 1.1 bouyer
1009 1.1 bouyer pdir = VTOI(dvp);
1010 1.100 dholland
1011 1.100 dholland /* XXX should handle this material another way */
1012 1.100 dholland ulr = &pdir->i_crap;
1013 1.100 dholland UFS_CHECK_CRAPCOUNTER(pdir);
1014 1.100 dholland
1015 1.1 bouyer *vpp = NULL;
1016 1.1 bouyer
1017 1.129 hannken error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, NULL, &tvp);
1018 1.128 hannken if (error)
1019 1.128 hannken return error;
1020 1.128 hannken error = vn_lock(tvp, LK_EXCLUSIVE);
1021 1.128 hannken if (error) {
1022 1.128 hannken vrele(tvp);
1023 1.122 christos return error;
1024 1.1 bouyer }
1025 1.1 bouyer ip = VTOI(tvp);
1026 1.124 jdolecek if (do_direnter) {
1027 1.124 jdolecek /*
1028 1.124 jdolecek * Make sure inode goes to disk before directory entry.
1029 1.124 jdolecek */
1030 1.124 jdolecek if ((error = ext2fs_update(tvp, NULL, NULL, UPDATE_WAIT)) != 0)
1031 1.124 jdolecek goto bad;
1032 1.124 jdolecek error = ext2fs_direnter(ip, dvp, ulr, cnp);
1033 1.124 jdolecek if (error != 0)
1034 1.124 jdolecek goto bad;
1035 1.124 jdolecek }
1036 1.124 jdolecek
1037 1.1 bouyer *vpp = tvp;
1038 1.130 christos cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
1039 1.122 christos return 0;
1040 1.1 bouyer
1041 1.1 bouyer bad:
1042 1.1 bouyer /*
1043 1.1 bouyer * Write error occurred trying to update the inode
1044 1.1 bouyer * or the directory so must deallocate the inode.
1045 1.1 bouyer */
1046 1.1 bouyer ip->i_e2fs_nlink = 0;
1047 1.1 bouyer ip->i_flag |= IN_CHANGE;
1048 1.1 bouyer vput(tvp);
1049 1.122 christos return error;
1050 1.1 bouyer }
1051 1.1 bouyer
1052 1.1 bouyer /*
1053 1.1 bouyer * Reclaim an inode so that it can be used for other purposes.
1054 1.1 bouyer */
1055 1.1 bouyer int
1056 1.61 xtraeme ext2fs_reclaim(void *v)
1057 1.1 bouyer {
1058 1.127 riastrad struct vop_reclaim_v2_args /* {
1059 1.1 bouyer struct vnode *a_vp;
1060 1.1 bouyer } */ *ap = v;
1061 1.25 augustss struct vnode *vp = ap->a_vp;
1062 1.54 mycroft struct inode *ip = VTOI(vp);
1063 1.54 mycroft int error;
1064 1.45 fvdl
1065 1.127 riastrad VOP_UNLOCK(vp);
1066 1.127 riastrad
1067 1.94 hannken /*
1068 1.94 hannken * The inode must be freed and updated before being removed
1069 1.94 hannken * from its hash chain. Other threads trying to gain a hold
1070 1.111 hannken * or lock on the inode will be stalled.
1071 1.94 hannken */
1072 1.94 hannken if (ip->i_omode == 1 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1073 1.94 hannken ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode);
1074 1.76 pooka if ((error = ufs_reclaim(vp)) != 0)
1075 1.122 christos return error;
1076 1.45 fvdl if (ip->i_din.e2fs_din != NULL)
1077 1.118 jdolecek kmem_free(ip->i_din.e2fs_din, EXT2_DINODE_SIZE(ip->i_e2fs));
1078 1.73 ad genfs_node_destroy(vp);
1079 1.14 thorpej pool_put(&ext2fs_inode_pool, vp->v_data);
1080 1.1 bouyer vp->v_data = NULL;
1081 1.122 christos return 0;
1082 1.1 bouyer }
1083 1.1 bouyer
1084 1.1 bouyer /* Global vfs data structures for ext2fs. */
1085 1.61 xtraeme int (**ext2fs_vnodeop_p)(void *);
1086 1.31 jdolecek const struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = {
1087 1.1 bouyer { &vop_default_desc, vn_default_error },
1088 1.133 dholland { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
1089 1.8 fvdl { &vop_lookup_desc, ext2fs_lookup }, /* lookup */
1090 1.8 fvdl { &vop_create_desc, ext2fs_create }, /* create */
1091 1.1 bouyer { &vop_mknod_desc, ext2fs_mknod }, /* mknod */
1092 1.1 bouyer { &vop_open_desc, ext2fs_open }, /* open */
1093 1.1 bouyer { &vop_close_desc, ufs_close }, /* close */
1094 1.8 fvdl { &vop_access_desc, ext2fs_access }, /* access */
1095 1.132 christos { &vop_accessx_desc, genfs_accessx }, /* accessx */
1096 1.8 fvdl { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1097 1.8 fvdl { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1098 1.1 bouyer { &vop_read_desc, ext2fs_read }, /* read */
1099 1.1 bouyer { &vop_write_desc, ext2fs_write }, /* write */
1100 1.113 dholland { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
1101 1.113 dholland { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
1102 1.135 dholland { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */
1103 1.135 dholland { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1104 1.135 dholland { &vop_poll_desc, genfs_poll }, /* poll */
1105 1.42 jdolecek { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1106 1.135 dholland { &vop_revoke_desc, genfs_revoke }, /* revoke */
1107 1.135 dholland { &vop_mmap_desc, genfs_mmap }, /* mmap */
1108 1.1 bouyer { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1109 1.135 dholland { &vop_seek_desc, genfs_seek }, /* seek */
1110 1.8 fvdl { &vop_remove_desc, ext2fs_remove }, /* remove */
1111 1.1 bouyer { &vop_link_desc, ext2fs_link }, /* link */
1112 1.8 fvdl { &vop_rename_desc, ext2fs_rename }, /* rename */
1113 1.1 bouyer { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */
1114 1.1 bouyer { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */
1115 1.8 fvdl { &vop_symlink_desc, ext2fs_symlink }, /* symlink */
1116 1.8 fvdl { &vop_readdir_desc, ext2fs_readdir }, /* readdir */
1117 1.8 fvdl { &vop_readlink_desc, ext2fs_readlink }, /* readlink */
1118 1.135 dholland { &vop_abortop_desc, genfs_abortop }, /* abortop */
1119 1.8 fvdl { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1120 1.8 fvdl { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1121 1.135 dholland { &vop_lock_desc, genfs_lock }, /* lock */
1122 1.135 dholland { &vop_unlock_desc, genfs_unlock }, /* unlock */
1123 1.1 bouyer { &vop_bmap_desc, ext2fs_bmap }, /* bmap */
1124 1.8 fvdl { &vop_strategy_desc, ufs_strategy }, /* strategy */
1125 1.1 bouyer { &vop_print_desc, ufs_print }, /* print */
1126 1.135 dholland { &vop_islocked_desc, genfs_islocked }, /* islocked */
1127 1.8 fvdl { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
1128 1.8 fvdl { &vop_advlock_desc, ext2fs_advlock }, /* advlock */
1129 1.1 bouyer { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1130 1.30 chs { &vop_getpages_desc, genfs_getpages }, /* getpages */
1131 1.30 chs { &vop_putpages_desc, genfs_putpages }, /* putpages */
1132 1.121 jdolecek { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */
1133 1.121 jdolecek { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */
1134 1.121 jdolecek { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */
1135 1.121 jdolecek { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
1136 1.30 chs { NULL, NULL }
1137 1.1 bouyer };
1138 1.31 jdolecek const struct vnodeopv_desc ext2fs_vnodeop_opv_desc =
1139 1.1 bouyer { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries };
1140 1.1 bouyer
1141 1.61 xtraeme int (**ext2fs_specop_p)(void *);
1142 1.31 jdolecek const struct vnodeopv_entry_desc ext2fs_specop_entries[] = {
1143 1.1 bouyer { &vop_default_desc, vn_default_error },
1144 1.134 dholland GENFS_SPECOP_ENTRIES,
1145 1.1 bouyer { &vop_close_desc, ufsspec_close }, /* close */
1146 1.8 fvdl { &vop_access_desc, ext2fs_access }, /* access */
1147 1.132 christos { &vop_accessx_desc, genfs_accessx }, /* accessx */
1148 1.8 fvdl { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1149 1.8 fvdl { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1150 1.1 bouyer { &vop_read_desc, ufsspec_read }, /* read */
1151 1.1 bouyer { &vop_write_desc, ufsspec_write }, /* write */
1152 1.135 dholland { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1153 1.1 bouyer { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1154 1.8 fvdl { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1155 1.8 fvdl { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1156 1.135 dholland { &vop_lock_desc, genfs_lock }, /* lock */
1157 1.135 dholland { &vop_unlock_desc, genfs_unlock }, /* unlock */
1158 1.1 bouyer { &vop_print_desc, ufs_print }, /* print */
1159 1.135 dholland { &vop_islocked_desc, genfs_islocked }, /* islocked */
1160 1.1 bouyer { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1161 1.121 jdolecek { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */
1162 1.121 jdolecek { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */
1163 1.121 jdolecek { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */
1164 1.121 jdolecek { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
1165 1.30 chs { NULL, NULL }
1166 1.1 bouyer };
1167 1.31 jdolecek const struct vnodeopv_desc ext2fs_specop_opv_desc =
1168 1.1 bouyer { &ext2fs_specop_p, ext2fs_specop_entries };
1169 1.1 bouyer
1170 1.61 xtraeme int (**ext2fs_fifoop_p)(void *);
1171 1.31 jdolecek const struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = {
1172 1.1 bouyer { &vop_default_desc, vn_default_error },
1173 1.134 dholland GENFS_FIFOOP_ENTRIES,
1174 1.1 bouyer { &vop_close_desc, ufsfifo_close }, /* close */
1175 1.8 fvdl { &vop_access_desc, ext2fs_access }, /* access */
1176 1.132 christos { &vop_accessx_desc, genfs_accessx }, /* accessx */
1177 1.8 fvdl { &vop_getattr_desc, ext2fs_getattr }, /* getattr */
1178 1.8 fvdl { &vop_setattr_desc, ext2fs_setattr }, /* setattr */
1179 1.1 bouyer { &vop_read_desc, ufsfifo_read }, /* read */
1180 1.1 bouyer { &vop_write_desc, ufsfifo_write }, /* write */
1181 1.135 dholland { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1182 1.1 bouyer { &vop_fsync_desc, ext2fs_fsync }, /* fsync */
1183 1.8 fvdl { &vop_inactive_desc, ext2fs_inactive }, /* inactive */
1184 1.8 fvdl { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */
1185 1.135 dholland { &vop_lock_desc, genfs_lock }, /* lock */
1186 1.135 dholland { &vop_unlock_desc, genfs_unlock }, /* unlock */
1187 1.92 pooka { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
1188 1.1 bouyer { &vop_print_desc, ufs_print }, /* print */
1189 1.135 dholland { &vop_islocked_desc, genfs_islocked }, /* islocked */
1190 1.1 bouyer { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1191 1.121 jdolecek { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */
1192 1.121 jdolecek { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */
1193 1.121 jdolecek { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */
1194 1.121 jdolecek { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */
1195 1.30 chs { NULL, NULL }
1196 1.1 bouyer };
1197 1.31 jdolecek const struct vnodeopv_desc ext2fs_fifoop_opv_desc =
1198 1.1 bouyer { &ext2fs_fifoop_p, ext2fs_fifoop_entries };
1199