Kernel - fix access checks
[dragonfly.git] / sys / vfs / gnu / ext2fs / ext2_vnops.c
CommitLineData
984263bc
MD
1/*
2 * modified for EXT2FS support in Lites 1.1
3 *
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
6 */
7/*
8 * Copyright (c) 1982, 1986, 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95
45 * @(#)ext2_vnops.c 8.7 (Berkeley) 2/3/94
46 * $FreeBSD: src/sys/gnu/ext2fs/ext2_vnops.c,v 1.51.2.2 2003/01/02 17:26:18 bde Exp $
e92ca23a 47 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_vnops.c,v 1.43 2008/06/19 23:27:39 dillon Exp $
984263bc
MD
48 */
49
50#include "opt_quota.h"
51#include "opt_suiddir.h"
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/resourcevar.h>
56#include <sys/kernel.h>
57#include <sys/stat.h>
1f1db49f 58#include <sys/fcntl.h>
984263bc 59#include <sys/buf.h>
1f1db49f 60#include <sys/stat.h>
984263bc 61#include <sys/proc.h>
895c1f85 62#include <sys/priv.h>
984263bc
MD
63#include <sys/mount.h>
64#include <sys/time.h>
65#include <sys/vnode.h>
1f1db49f
MD
66#include <sys/dirent.h>
67#include <sys/lockf.h>
68#include <sys/event.h>
69#include <sys/conf.h>
70#include <sys/file.h>
71#include <sys/jail.h>
984263bc 72#include <sys/namei.h>
1f1db49f
MD
73#include <sys/signalvar.h>
74#include <sys/unistd.h>
984263bc
MD
75
76#include <vm/vm.h>
77#include <vm/vm_extern.h>
78#include <vm/vm_zone.h>
79#include <vm/vnode_pager.h>
1f1db49f 80
7b95be2a 81#include <sys/buf2.h>
61670a01 82#include <sys/thread2.h>
984263bc 83
1f1db49f 84#include <vfs/fifofs/fifo.h>
984263bc 85
1f1db49f
MD
86#include "dir.h"
87#include "quota.h"
88#include "inode.h"
89#include "ext2mount.h"
1f2de5d4
MD
90#include "ext2_fs_sb.h"
91#include "fs.h"
92#include "ext2_extern.h"
93#include "ext2_fs.h"
984263bc 94
1f1db49f
MD
95static int ext2_access (struct vop_access_args *);
96static int ext2_advlock (struct vop_advlock_args *);
87de5057
MD
97static int ext2_chmod (struct vnode *, int, struct ucred *);
98static int ext2_chown (struct vnode *, uid_t, gid_t, struct ucred *);
1f1db49f
MD
99static int ext2_close (struct vop_close_args *);
100static int ext2_getattr (struct vop_getattr_args *);
a6ee311a 101static int ext2_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *);
1f1db49f
MD
102static int ext2_mmap (struct vop_mmap_args *);
103static int ext2_open (struct vop_open_args *);
104static int ext2_pathconf (struct vop_pathconf_args *);
105static int ext2_print (struct vop_print_args *);
106static int ext2_readlink (struct vop_readlink_args *);
107static int ext2_setattr (struct vop_setattr_args *);
108static int ext2_strategy (struct vop_strategy_args *);
109static int ext2_whiteout (struct vop_old_whiteout_args *);
110static int filt_ext2read (struct knote *kn, long hint);
111static int filt_ext2write (struct knote *kn, long hint);
112static int filt_ext2vnode (struct knote *kn, long hint);
113static void filt_ext2detach (struct knote *kn);
114static int ext2_kqfilter (struct vop_kqfilter_args *ap);
1f1db49f
MD
115static int ext2fifo_close (struct vop_close_args *);
116static int ext2fifo_kqfilter (struct vop_kqfilter_args *);
117static int ext2fifo_read (struct vop_read_args *);
118static int ext2fifo_write (struct vop_write_args *);
984263bc 119
a6ee311a
RG
120static int ext2_fsync (struct vop_fsync_args *);
121static int ext2_read (struct vop_read_args *);
122static int ext2_write (struct vop_write_args *);
e62afb5f
MD
123static int ext2_remove (struct vop_old_remove_args *);
124static int ext2_link (struct vop_old_link_args *);
125static int ext2_rename (struct vop_old_rename_args *);
126static int ext2_mkdir (struct vop_old_mkdir_args *);
127static int ext2_rmdir (struct vop_old_rmdir_args *);
128static int ext2_create (struct vop_old_create_args *);
129static int ext2_mknod (struct vop_old_mknod_args *);
130static int ext2_symlink (struct vop_old_symlink_args *);
a6ee311a 131static int ext2_getpages (struct vop_getpages_args *);
984263bc 132
1f1db49f 133#include "ext2_readwrite.c"
984263bc 134
1f1db49f
MD
135union _qcvt {
136 int64_t qcvt;
137 int32_t val[2];
984263bc 138};
1f1db49f
MD
139#define SETHIGH(q, h) { \
140 union _qcvt tmp; \
141 tmp.qcvt = (q); \
142 tmp.val[_QUAD_HIGHWORD] = (h); \
143 (q) = tmp.qcvt; \
144}
145#define SETLOW(q, l) { \
146 union _qcvt tmp; \
147 tmp.qcvt = (q); \
148 tmp.val[_QUAD_LOWWORD] = (l); \
149 (q) = tmp.qcvt; \
150}
151#define VN_KNOTE(vp, b) \
152 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b))
984263bc 153
1f1db49f 154#define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
984263bc
MD
155
156/*
157 * A virgin directory (no blushing please).
158 * Note that the type and namlen fields are reversed relative to ufs.
159 * Also, we don't use `struct odirtemplate', since it would just cause
160 * endianness problems.
161 */
1f1db49f 162static struct dirtemplate ext2_mastertemplate = {
984263bc
MD
163 0, 12, 1, EXT2_FT_DIR, ".",
164 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".."
165};
1f1db49f 166static struct dirtemplate ext2_omastertemplate = {
984263bc
MD
167 0, 12, 1, EXT2_FT_UNKNOWN, ".",
168 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".."
169};
170
171/*
172 * Create a regular file
0f7f7a49
CP
173 *
174 * ext2_create(struct vnode *a_dvp, struct vnode **a_vpp,
175 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
176 */
177static int
e62afb5f 178ext2_create(struct vop_old_create_args *ap)
984263bc
MD
179{
180 int error;
181
182 error =
183 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode),
184 ap->a_dvp, ap->a_vpp, ap->a_cnp);
185 if (error)
186 return (error);
187 return (0);
188}
189
190/*
191 * Synch an open file.
0f7f7a49
CP
192 *
193 * ext2_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
194 * struct proc *a_p)
984263bc
MD
195 */
196/* ARGSUSED */
6bae6177
MD
197
198static int ext2_fsync_bp(struct buf *bp, void *data);
199
200struct ext2_fsync_bp_info {
201 struct vnode *vp;
202 int waitfor;
6bae6177
MD
203};
204
984263bc 205static int
0f7f7a49 206ext2_fsync(struct vop_fsync_args *ap)
984263bc 207{
6bae6177 208 struct ext2_fsync_bp_info info;
f7aae92f 209 struct vnode *vp = ap->a_vp;
0202303b 210 lwkt_tokref vlock;
6bae6177 211 int count;
984263bc
MD
212
213 /*
214 * XXX why is all this fs specific?
215 */
216
217 /*
218 * Flush all dirty buffers associated with a vnode.
219 */
220 ext2_discard_prealloc(VTOI(vp));
221
0202303b 222 lwkt_gettoken(&vlock, &vp->v_token);
6bae6177 223 info.vp = vp;
984263bc 224loop:
6bae6177
MD
225 info.waitfor = ap->a_waitfor;
226 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
227 ext2_fsync_bp, &info);
228 if (count)
984263bc 229 goto loop;
6bae6177 230
984263bc 231 if (ap->a_waitfor == MNT_WAIT) {
a9a20f98 232 bio_track_wait(&vp->v_track_write, 0, 0);
984263bc 233#if DIAGNOSTIC
6bae6177 234 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
984263bc
MD
235 vprint("ext2_fsync: dirty", vp);
236 goto loop;
237 }
238#endif
239 }
0202303b 240 lwkt_reltoken(&vlock);
1f1db49f 241 return (EXT2_UPDATE(ap->a_vp, ap->a_waitfor == MNT_WAIT));
984263bc
MD
242}
243
6bae6177
MD
244static int
245ext2_fsync_bp(struct buf *bp, void *data)
246{
247 struct ext2_fsync_bp_info *info = data;
248
249 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
250 return(0);
251 if ((bp->b_flags & B_DELWRI) == 0)
252 panic("ext2_fsync: not dirty");
253 bremfree(bp);
165dba55 254 crit_exit();
6bae6177
MD
255
256 /*
257 * Wait for I/O associated with indirect blocks to complete,
258 * since there is no way to quickly wait for them below.
259 */
260 if (bp->b_vp == info->vp || info->waitfor == MNT_NOWAIT)
b1ce5639 261 bawrite(bp);
6bae6177 262 else
b1ce5639 263 bwrite(bp);
165dba55 264 crit_enter();
6bae6177
MD
265 return(1);
266}
267
984263bc
MD
268/*
269 * Mknod vnode call
0f7f7a49
CP
270 *
271 * ext2_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
272 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
273 */
274/* ARGSUSED */
275static int
e62afb5f 276ext2_mknod(struct vop_old_mknod_args *ap)
984263bc
MD
277{
278 struct vattr *vap = ap->a_vap;
279 struct vnode **vpp = ap->a_vpp;
280 struct inode *ip;
281 ino_t ino;
282 int error;
283
0e9b9130
MD
284 if (vap->va_rmajor != VNOVAL &&
285 makeudev(vap->va_rmajor, vap->va_rminor) == NOUDEV) {
286 return (EINVAL);
287 }
288
984263bc
MD
289 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
290 ap->a_dvp, vpp, ap->a_cnp);
291 if (error)
292 return (error);
293 ip = VTOI(*vpp);
294 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
0e9b9130 295 if (vap->va_rmajor != VNOVAL) {
984263bc
MD
296 /*
297 * Want to be able to use this to make badblock
298 * inodes, so don't truncate the dev number.
299 */
0e9b9130 300 ip->i_rdev = makeudev(vap->va_rmajor, vap->va_rminor);
984263bc
MD
301 }
302 /*
303 * Remove inode, then reload it through VFS_VGET so it is
304 * checked to see if it is an alias of an existing entry in
305 * the inode cache.
306 */
984263bc
MD
307 (*vpp)->v_type = VNON;
308 ino = ip->i_number; /* Save this before vgone() invalidates ip. */
3c37c940 309 vgone_vxlocked(*vpp);
5fd012e0 310 vput(*vpp);
b9b0a6d0 311 error = VFS_VGET(ap->a_dvp->v_mount, NULL, ino, vpp);
984263bc
MD
312 if (error) {
313 *vpp = NULL;
314 return (error);
315 }
316 return (0);
317}
318
0f7f7a49
CP
319/*
320 * ext2_remove(struct vnode *a_dvp, struct vnode *a_vp,
321 * struct componentname *a_cnp)
322 */
984263bc 323static int
e62afb5f 324ext2_remove(struct vop_old_remove_args *ap)
984263bc
MD
325{
326 struct inode *ip;
327 struct vnode *vp = ap->a_vp;
328 struct vnode *dvp = ap->a_dvp;
329 int error;
330
331 ip = VTOI(vp);
332 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
333 (VTOI(dvp)->i_flags & APPEND)) {
334 error = EPERM;
335 goto out;
336 }
337 error = ext2_dirremove(dvp, ap->a_cnp);
338 if (error == 0) {
339 ip->i_nlink--;
340 ip->i_flag |= IN_CHANGE;
341 }
342out:
343 return (error);
344}
345
346/*
347 * link vnode call
0f7f7a49
CP
348 *
349 * ext2_link(struct vnode *a_tdvp, struct vnode *a_vp,
350 * struct componentname *a_cnp)
984263bc
MD
351 */
352static int
e62afb5f 353ext2_link(struct vop_old_link_args *ap)
984263bc
MD
354{
355 struct vnode *vp = ap->a_vp;
356 struct vnode *tdvp = ap->a_tdvp;
357 struct componentname *cnp = ap->a_cnp;
984263bc
MD
358 struct inode *ip;
359 int error;
360
984263bc
MD
361 if (tdvp->v_mount != vp->v_mount) {
362 error = EXDEV;
363 goto out2;
364 }
ca466bae 365 if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
984263bc
MD
366 goto out2;
367 }
368 ip = VTOI(vp);
369 if ((nlink_t)ip->i_nlink >= LINK_MAX) {
370 error = EMLINK;
371 goto out1;
372 }
373 if (ip->i_flags & (IMMUTABLE | APPEND)) {
374 error = EPERM;
375 goto out1;
376 }
377 ip->i_nlink++;
378 ip->i_flag |= IN_CHANGE;
1f1db49f 379 error = EXT2_UPDATE(vp, 1);
984263bc
MD
380 if (!error)
381 error = ext2_direnter(ip, tdvp, cnp);
382 if (error) {
383 ip->i_nlink--;
384 ip->i_flag |= IN_CHANGE;
385 }
386out1:
387 if (tdvp != vp)
a11aaa81 388 vn_unlock(vp);
984263bc
MD
389out2:
390 return (error);
391}
392
393/*
fad57d0e
MD
394 * Rename system call. fdvp, fvp are ref'd. tvp, tdvp are ref'd and locked.
395 * all vp's are released and must be in an unlocked state on return.
0f7f7a49
CP
396 *
397 * ext2_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
398 * struct componentname *a_fcnp, struct vnode *a_tdvp,
399 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
400 */
401static int
e62afb5f 402ext2_rename(struct vop_old_rename_args *ap)
984263bc
MD
403{
404 struct vnode *tvp = ap->a_tvp;
f7aae92f 405 struct vnode *tdvp = ap->a_tdvp;
984263bc
MD
406 struct vnode *fvp = ap->a_fvp;
407 struct vnode *fdvp = ap->a_fdvp;
408 struct componentname *tcnp = ap->a_tcnp;
409 struct componentname *fcnp = ap->a_fcnp;
984263bc
MD
410 struct inode *ip, *xp, *dp;
411 struct dirtemplate dirbuf;
412 int doingdirectory = 0, oldparent = 0, newparent = 0;
413 int error = 0;
414 u_char namlen;
415
984263bc
MD
416 /*
417 * Check for cross-device rename.
418 */
419 if ((fvp->v_mount != tdvp->v_mount) ||
fad57d0e
MD
420 (tvp && (fvp->v_mount != tvp->v_mount)) ||
421 tvp == tdvp) {
984263bc
MD
422 error = EXDEV;
423abortit:
424 if (tdvp == tvp)
425 vrele(tdvp);
426 else
427 vput(tdvp);
428 if (tvp)
429 vput(tvp);
430 vrele(fdvp);
431 vrele(fvp);
432 return (error);
433 }
434
435 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
436 (VTOI(tdvp)->i_flags & APPEND))) {
437 error = EPERM;
438 goto abortit;
439 }
440
441 /*
442 * Renaming a file to itself has no effect. The upper layers should
443 * not call us in that case. Temporarily just warn if they do.
444 */
445 if (fvp == tvp) {
984263bc
MD
446 error = 0;
447 goto abortit;
448 }
449
ca466bae 450 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
984263bc 451 goto abortit;
fad57d0e
MD
452
453 /*
454 * fvp, tvp, tdvp locked. fdvp not locked but note that fdvp may
455 * be equal to tdvp.
456 */
984263bc
MD
457 dp = VTOI(fdvp);
458 ip = VTOI(fvp);
459 if (ip->i_nlink >= LINK_MAX) {
a11aaa81 460 vn_unlock(fvp);
984263bc
MD
461 error = EMLINK;
462 goto abortit;
463 }
464 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))
465 || (dp->i_flags & APPEND)) {
a11aaa81 466 vn_unlock(fvp);
984263bc
MD
467 error = EPERM;
468 goto abortit;
469 }
470 if ((ip->i_mode & IFMT) == IFDIR) {
471 /*
472 * Avoid ".", "..", and aliases of "." for obvious reasons.
473 */
474 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
2b69e610 475 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT ||
984263bc 476 (ip->i_flag & IN_RENAME)) {
a11aaa81 477 vn_unlock(fvp);
984263bc
MD
478 error = EINVAL;
479 goto abortit;
480 }
481 ip->i_flag |= IN_RENAME;
482 oldparent = dp->i_number;
483 doingdirectory++;
484 }
984263bc
MD
485
486 /*
fad57d0e
MD
487 * tvp is non-NULL if the target exists. fvp is still locked but
488 * we will unlock it soon. The 'bad' goto target requires dp and
489 * xp to be correctly assigned.
984263bc
MD
490 */
491 dp = VTOI(tdvp);
984263bc
MD
492 if (tvp)
493 xp = VTOI(tvp);
fad57d0e
MD
494 else
495 xp = NULL;
984263bc
MD
496
497 /*
498 * 1) Bump link count while we're moving stuff
499 * around. If we crash somewhere before
500 * completing our work, the link count
501 * may be wrong, but correctable.
502 */
503 ip->i_nlink++;
504 ip->i_flag |= IN_CHANGE;
1f1db49f 505 if ((error = EXT2_UPDATE(fvp, 1)) != 0) {
a11aaa81 506 vn_unlock(fvp);
984263bc
MD
507 goto bad;
508 }
509
510 /*
511 * If ".." must be changed (ie the directory gets a new
512 * parent) then the source directory must not be in the
513 * directory heirarchy above the target, as this would
514 * orphan everything below the source directory. Also
515 * the user must have write permission in the source so
516 * as to be able to change "..". We must repeat the call
517 * to namei, as the parent directory is unlocked by the
518 * call to checkpath().
519 */
cb66845a 520 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
a11aaa81 521 vn_unlock(fvp);
fad57d0e
MD
522
523 /*
524 * tvp (if not NULL) and tdvp are locked. fvp and fdvp are not.
525 * dp and xp are set according to tdvp and tvp.
526 */
984263bc
MD
527 if (oldparent != dp->i_number)
528 newparent = dp->i_number;
529 if (doingdirectory && newparent) {
530 if (error) /* write access check above */
531 goto bad;
fad57d0e
MD
532
533 /*
534 * Prepare for relookup, get rid of xp
535 */
536 if (xp != NULL) {
984263bc 537 vput(tvp);
fad57d0e
MD
538 xp = NULL;
539 }
540
541 /*
542 * checkpath vput()'s tdvp (VTOI(dp)) on return no matter what,
543 * get an extra ref so we wind up with just an unlocked, ref'd
544 * tdvp. The 'out' target skips xp and tdvp cleanups. Our
545 * tdvp is now unlocked so we have to clean it up ourselves.
546 */
547 vref(tdvp);
984263bc 548 error = ext2_checkpath(ip, dp, tcnp->cn_cred);
6970b52a 549 tcnp->cn_flags |= CNP_PDIRUNLOCK;
fad57d0e
MD
550 if (error) {
551 vrele(tdvp);
984263bc 552 goto out;
fad57d0e
MD
553 }
554 /*
555 * relookup no longer messes with the ref count. An unlocked
556 * tdvp must be passed and if no error occurs a locked tdvp
557 * will be returned. We have to use the out target again.
558 */
984263bc 559 error = relookup(tdvp, &tvp, tcnp);
fad57d0e
MD
560 if (error) {
561 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
562 vrele(tdvp);
563 else
564 vput(tdvp);
984263bc 565 goto out;
fad57d0e
MD
566 }
567
568 /*
569 * tdvp is locked at this point. in the RENAME case tvp may
570 * be NULL without an error, assign xp accordingly. The
571 * 'bad' target can be used again after this.
572 */
984263bc 573 dp = VTOI(tdvp);
984263bc
MD
574 if (tvp)
575 xp = VTOI(tvp);
576 }
577 /*
578 * 2) If target doesn't exist, link the target
579 * to the source and unlink the source.
580 * Otherwise, rewrite the target directory
581 * entry to reference the source inode and
582 * expunge the original entry's existence.
fad57d0e
MD
583 *
584 * tdvp and tvp are cleaned up by this code. tvp is only good if
585 * xp is not NULL.
984263bc
MD
586 */
587 if (xp == NULL) {
588 if (dp->i_dev != ip->i_dev)
1f1db49f 589 panic("ext2_rename: EXDEV");
984263bc
MD
590 /*
591 * Account for ".." in new directory.
592 * When source and destination have the same
593 * parent we don't fool with the link count.
594 */
595 if (doingdirectory && newparent) {
596 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
597 error = EMLINK;
598 goto bad;
599 }
600 dp->i_nlink++;
601 dp->i_flag |= IN_CHANGE;
1f1db49f 602 error = EXT2_UPDATE(tdvp, 1);
984263bc
MD
603 if (error)
604 goto bad;
605 }
606 error = ext2_direnter(ip, tdvp, tcnp);
607 if (error) {
608 if (doingdirectory && newparent) {
609 dp->i_nlink--;
610 dp->i_flag |= IN_CHANGE;
1f1db49f 611 EXT2_UPDATE(tdvp, 1);
984263bc
MD
612 }
613 goto bad;
614 }
fad57d0e
MD
615
616 /*
617 * manual cleanup, we can't use the bad or out target after
618 * this.
619 */
984263bc
MD
620 vput(tdvp);
621 } else {
622 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
1f1db49f 623 panic("ext2_rename: EXDEV");
984263bc
MD
624 /*
625 * Short circuit rename(foo, foo).
626 */
627 if (xp->i_number == ip->i_number)
1f1db49f 628 panic("ext2_rename: same file");
984263bc
MD
629 /*
630 * If the parent directory is "sticky", then the user must
631 * own the parent directory, or the destination of the rename,
632 * otherwise the destination may not be changed (except by
633 * root). This implements append-only directories.
634 */
635 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 &&
636 tcnp->cn_cred->cr_uid != dp->i_uid &&
637 xp->i_uid != tcnp->cn_cred->cr_uid) {
638 error = EPERM;
639 goto bad;
640 }
641 /*
642 * Target must be empty if a directory and have no links
643 * to it. Also, ensure source and target are compatible
644 * (both directories, or both not directories).
645 */
646 if ((xp->i_mode&IFMT) == IFDIR) {
647 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) ||
648 xp->i_nlink > 2) {
649 error = ENOTEMPTY;
650 goto bad;
651 }
652 if (!doingdirectory) {
653 error = ENOTDIR;
654 goto bad;
655 }
984263bc
MD
656 } else if (doingdirectory) {
657 error = EISDIR;
658 goto bad;
659 }
660 error = ext2_dirrewrite(dp, ip, tcnp);
661 if (error)
662 goto bad;
663 /*
664 * If the target directory is in the same
665 * directory as the source directory,
666 * decrement the link count on the parent
667 * of the target directory.
668 */
669 if (doingdirectory && !newparent) {
670 dp->i_nlink--;
671 dp->i_flag |= IN_CHANGE;
672 }
fad57d0e
MD
673
674 /*
675 * manual cleanup, we can't use the bad or out target after
676 * this.
677 */
984263bc 678 vput(tdvp);
fad57d0e 679
984263bc
MD
680 /*
681 * Adjust the link count of the target to
682 * reflect the dirrewrite above. If this is
683 * a directory it is empty and there are
684 * no links to it, so we can squash the inode and
685 * any space associated with it. We disallowed
686 * renaming over top of a directory with links to
687 * it above, as the remaining link would point to
688 * a directory without "." or ".." entries.
689 */
690 xp->i_nlink--;
691 if (doingdirectory) {
692 if (--xp->i_nlink != 0)
1f1db49f
MD
693 panic("ext2_rename: linked directory");
694 error = EXT2_TRUNCATE(tvp, (off_t)0, IO_SYNC,
87de5057 695 tcnp->cn_cred);
984263bc
MD
696 }
697 xp->i_flag |= IN_CHANGE;
698 vput(tvp);
699 xp = NULL;
700 }
701
fad57d0e
MD
702 /*
703 * tvp and tdvp have been cleaned up. The bad and out targets may
704 * not be used. fvp and fdvp are ref'd but not locked. ip
705 * still represents the old fvp and ip->i_flag may still have IN_RENAME
706 * set (if doingdirectory).
707 */
708
984263bc
MD
709 /*
710 * 3) Unlink the source.
fad57d0e
MD
711 *
712 * fdvp is locked and ref'd. ap->a_fvp holds the old lookup unlocked
713 * and ref'd, fvp will hold the new lookup locked and ref'd.
714 *
715 * After the relookup ap->a_fvp must be released as part of our
716 * cleanup, not just fdvp and fvp. And, on success, fdvp and
717 * fvp will be locked so the bad and out targets cannot be used.
984263bc 718 */
2b69e610 719 fcnp->cn_flags &= ~CNP_MODMASK;
fad57d0e
MD
720 fcnp->cn_flags |= CNP_LOCKPARENT;
721 KKASSERT(fcnp->cn_flags & CNP_PDIRUNLOCK);
984263bc 722 error = relookup(fdvp, &fvp, fcnp);
fad57d0e 723 if (error) {
984263bc
MD
724 /*
725 * From name has disappeared.
726 */
727 if (doingdirectory)
1f1db49f 728 panic("ext2_rename: lost dir entry");
fad57d0e 729 /* ip->i_flag only sets IN_RENAME if doingdirectory */
984263bc 730 vrele(ap->a_fvp);
fad57d0e
MD
731 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
732 vrele(fdvp);
733 else
734 vput(fdvp);
984263bc
MD
735 return (0);
736 }
fad57d0e
MD
737 KKASSERT((fcnp->cn_flags & CNP_PDIRUNLOCK) == 0);
738
739 /*
740 * This case shouldn't occur
741 */
742 if (fvp == NULL) {
743 /*
744 * From name has disappeared.
745 */
746 if (doingdirectory)
1f1db49f 747 panic("ext2_rename: lost dir entry");
fad57d0e
MD
748 /* ip->i_flag only sets IN_RENAME if doingdirectory */
749 vrele(ap->a_fvp);
750 vput(fvp);
751 vput(fdvp);
752 return (0);
753 }
754
755 /*
756 * fvp and fdvp are both ref'd and locked.
757 */
758 xp = VTOI(fvp);
759 dp = VTOI(fdvp);
760
984263bc
MD
761 /*
762 * Ensure that the directory entry still exists and has not
763 * changed while the new name has been entered. If the source is
764 * a file then the entry may have been unlinked or renamed. In
765 * either case there is no further work to be done. If the source
766 * is a directory then it cannot have been rmdir'ed; its link
767 * count of three would cause a rmdir to fail with ENOTEMPTY.
768 * The IN_RENAME flag ensures that it cannot be moved by another
769 * rename.
770 */
771 if (xp != ip) {
772 if (doingdirectory)
1f1db49f 773 panic("ext2_rename: lost dir entry");
fad57d0e 774 /* ip->i_flag only sets IN_RENAME if doingdirectory */
984263bc
MD
775 } else {
776 /*
777 * If the source is a directory with a
778 * new parent, the link count of the old
779 * parent directory must be decremented
780 * and ".." set to point to the new parent.
781 */
782 if (doingdirectory && newparent) {
783 dp->i_nlink--;
784 dp->i_flag |= IN_CHANGE;
785 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf,
87de5057
MD
786 sizeof (struct dirtemplate), (off_t)0,
787 UIO_SYSSPACE, IO_NODELOCKED,
60233e58 788 tcnp->cn_cred, NULL);
984263bc 789 if (error == 0) {
1f1db49f 790 /* Like ext2 little-endian: */
984263bc
MD
791 namlen = dirbuf.dotdot_type;
792 if (namlen != 2 ||
793 dirbuf.dotdot_name[0] != '.' ||
794 dirbuf.dotdot_name[1] != '.') {
1f1db49f 795 ext2_dirbad(xp, (doff_t)12,
984263bc
MD
796 "rename: mangled dir");
797 } else {
798 dirbuf.dotdot_ino = newparent;
b1ce5639 799 vn_rdwr(UIO_WRITE, fvp,
87de5057
MD
800 (caddr_t)&dirbuf,
801 sizeof (struct dirtemplate),
802 (off_t)0, UIO_SYSSPACE,
803 IO_NODELOCKED|IO_SYNC,
60233e58 804 tcnp->cn_cred, NULL);
984263bc
MD
805 }
806 }
807 }
808 error = ext2_dirremove(fdvp, fcnp);
809 if (!error) {
810 xp->i_nlink--;
811 xp->i_flag |= IN_CHANGE;
812 }
813 xp->i_flag &= ~IN_RENAME;
814 }
fad57d0e
MD
815 vput(fdvp);
816 vput(fvp);
984263bc
MD
817 vrele(ap->a_fvp);
818 return (error);
819
820bad:
821 if (xp)
822 vput(ITOV(xp));
fad57d0e
MD
823 if (dp)
824 vput(ITOV(dp));
984263bc
MD
825out:
826 if (doingdirectory)
827 ip->i_flag &= ~IN_RENAME;
ca466bae 828 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
984263bc
MD
829 ip->i_nlink--;
830 ip->i_flag |= IN_CHANGE;
831 ip->i_flag &= ~IN_RENAME;
832 vput(fvp);
fad57d0e 833 } else {
984263bc 834 vrele(fvp);
fad57d0e 835 }
984263bc
MD
836 return (error);
837}
838
839/*
840 * Mkdir system call
0f7f7a49
CP
841 *
842 * ext2_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
843 * struct componentname *a_cnp, struct vattr *a_vap)
984263bc
MD
844 */
845static int
e62afb5f 846ext2_mkdir(struct vop_old_mkdir_args *ap)
984263bc 847{
f7aae92f
RG
848 struct vnode *dvp = ap->a_dvp;
849 struct vattr *vap = ap->a_vap;
850 struct componentname *cnp = ap->a_cnp;
851 struct inode *ip, *dp;
984263bc
MD
852 struct vnode *tvp;
853 struct dirtemplate dirtemplate, *dtp;
854 int error, dmode;
855
984263bc
MD
856 dp = VTOI(dvp);
857 if ((nlink_t)dp->i_nlink >= LINK_MAX) {
858 error = EMLINK;
859 goto out;
860 }
861 dmode = vap->va_mode & 0777;
862 dmode |= IFDIR;
863 /*
864 * Must simulate part of ext2_makeinode here to acquire the inode,
865 * but not have it entered in the parent directory. The entry is
866 * made later after writing "." and ".." entries.
867 */
1f1db49f 868 error = EXT2_VALLOC(dvp, dmode, cnp->cn_cred, &tvp);
984263bc
MD
869 if (error)
870 goto out;
871 ip = VTOI(tvp);
872 ip->i_gid = dp->i_gid;
873#ifdef SUIDDIR
874 {
875#ifdef QUOTA
876 struct ucred ucred, *ucp;
877 ucp = cnp->cn_cred;
1d94a9ab 878#endif
984263bc
MD
879 /*
880 * if we are hacking owners here, (only do this where told to)
881 * and we are not giving it TOO root, (would subvert quotas)
882 * then go ahead and give it to the other user.
883 * The new directory also inherits the SUID bit.
884 * If user's UID and dir UID are the same,
885 * 'give it away' so that the SUID is still forced on.
886 */
887 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
888 (dp->i_mode & ISUID) && dp->i_uid) {
889 dmode |= ISUID;
890 ip->i_uid = dp->i_uid;
891#ifdef QUOTA
892 if (dp->i_uid != cnp->cn_cred->cr_uid) {
893 /*
894 * make sure the correct user gets charged
895 * for the space.
896 * Make a dummy credential for the victim.
897 * XXX This seems to never be accessed out of
898 * our context so a stack variable is ok.
899 */
900 ucred.cr_ref = 1;
901 ucred.cr_uid = ip->i_uid;
902 ucred.cr_ngroups = 1;
903 ucred.cr_groups[0] = dp->i_gid;
904 ucp = &ucred;
905 }
1d94a9ab 906#endif
984263bc
MD
907 } else {
908 ip->i_uid = cnp->cn_cred->cr_uid;
909 }
910#ifdef QUOTA
1f1db49f
MD
911 if ((error = ext2_getinoquota(ip)) ||
912 (error = ext2_chkiq(ip, 1, ucp, 0))) {
913 EXT2_VFREE(tvp, ip->i_number, dmode);
984263bc
MD
914 vput(tvp);
915 return (error);
916 }
917#endif
918 }
919#else
920 ip->i_uid = cnp->cn_cred->cr_uid;
921#ifdef QUOTA
1f1db49f
MD
922 if ((error = ext2_getinoquota(ip)) ||
923 (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
924 EXT2_VFREE(tvp, ip->i_number, dmode);
984263bc
MD
925 vput(tvp);
926 return (error);
927 }
928#endif
929#endif
930 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
931 ip->i_mode = dmode;
932 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */
933 ip->i_nlink = 2;
2b69e610 934 if (cnp->cn_flags & CNP_ISWHITEOUT)
984263bc 935 ip->i_flags |= UF_OPAQUE;
1f1db49f 936 error = EXT2_UPDATE(tvp, 1);
984263bc 937
93f50d60
MD
938 /*
939 * The vnode must have a VM object in order to issue buffer cache
940 * ops on it.
941 */
1c843a13 942 vinitvmio(tvp, 0);
93f50d60 943
984263bc
MD
944 /*
945 * Bump link count in parent directory
946 * to reflect work done below. Should
947 * be done before reference is created
948 * so reparation is possible if we crash.
949 */
950 dp->i_nlink++;
951 dp->i_flag |= IN_CHANGE;
1f1db49f 952 error = EXT2_UPDATE(dvp, 1);
984263bc
MD
953 if (error)
954 goto bad;
955
956 /* Initialize directory with "." and ".." from static template. */
957 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
958 EXT2_FEATURE_INCOMPAT_FILETYPE))
1f1db49f 959 dtp = &ext2_mastertemplate;
984263bc 960 else
1f1db49f 961 dtp = &ext2_omastertemplate;
984263bc
MD
962 dirtemplate = *dtp;
963 dirtemplate.dot_ino = ip->i_number;
964 dirtemplate.dotdot_ino = dp->i_number;
965 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE
966 * so let's just redefine it - for this function only
967 */
968#undef DIRBLKSIZ
969#define DIRBLKSIZ VTOI(dvp)->i_e2fs->s_blocksize
970 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12;
971 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate,
87de5057 972 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
60233e58 973 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL);
984263bc
MD
974 if (error) {
975 dp->i_nlink--;
976 dp->i_flag |= IN_CHANGE;
977 goto bad;
978 }
1f1db49f
MD
979 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
980 panic("ext2_mkdir: blksize"); /* XXX should grow with balloc() */
984263bc
MD
981 else {
982 ip->i_size = DIRBLKSIZ;
983 ip->i_flag |= IN_CHANGE;
984 }
985
986 /* Directory set up, now install its entry in the parent directory. */
987 error = ext2_direnter(ip, dvp, cnp);
988 if (error) {
989 dp->i_nlink--;
990 dp->i_flag |= IN_CHANGE;
991 }
992bad:
993 /*
994 * No need to do an explicit VOP_TRUNCATE here, vrele will do this
995 * for us because we set the link count to 0.
996 */
997 if (error) {
998 ip->i_nlink = 0;
999 ip->i_flag |= IN_CHANGE;
1000 vput(tvp);
1001 } else
1002 *ap->a_vpp = tvp;
1003out:
1004 return (error);
1005#undef DIRBLKSIZ
1006#define DIRBLKSIZ DEV_BSIZE
1007}
1008
1009/*
1010 * Rmdir system call.
0f7f7a49
CP
1011 *
1012 * ext2_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1013 * struct componentname *a_cnp)
984263bc
MD
1014 */
1015static int
e62afb5f 1016ext2_rmdir(struct vop_old_rmdir_args *ap)
984263bc
MD
1017{
1018 struct vnode *vp = ap->a_vp;
1019 struct vnode *dvp = ap->a_dvp;
1020 struct componentname *cnp = ap->a_cnp;
984263bc
MD
1021 struct inode *ip, *dp;
1022 int error;
1023
1024 ip = VTOI(vp);
1025 dp = VTOI(dvp);
1026
1027 /*
1028 * Verify the directory is empty (and valid).
1029 * (Rmdir ".." won't be valid since
1030 * ".." will contain a reference to
1031 * the current directory and thus be
1032 * non-empty.)
1033 */
1034 error = 0;
1035 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) {
1036 error = ENOTEMPTY;
1037 goto out;
1038 }
1039 if ((dp->i_flags & APPEND)
1040 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1041 error = EPERM;
1042 goto out;
1043 }
1044 /*
1045 * Delete reference to directory before purging
1046 * inode. If we crash in between, the directory
1047 * will be reattached to lost+found,
1048 */
1049 error = ext2_dirremove(dvp, cnp);
1050 if (error)
1051 goto out;
1052 dp->i_nlink--;
1053 dp->i_flag |= IN_CHANGE;
a11aaa81 1054 vn_unlock(dvp);
984263bc
MD
1055 /*
1056 * Truncate inode. The only stuff left
1057 * in the directory is "." and "..". The
1058 * "." reference is inconsequential since
1059 * we're quashing it. The ".." reference
1060 * has already been adjusted above. We've
1061 * removed the "." reference and the reference
1062 * in the parent directory, but there may be
1063 * other hard links so decrement by 2 and
1064 * worry about them later.
1065 */
1066 ip->i_nlink -= 2;
87de5057 1067 error = EXT2_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred);
ca466bae 1068 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
984263bc
MD
1069out:
1070 return (error);
1071}
1072
1073/*
1074 * symlink -- make a symbolic link
0f7f7a49
CP
1075 *
1076 * ext2_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1077 * struct componentname *a_cnp, struct vattr *a_vap,
1078 * char *a_target)
984263bc
MD
1079 */
1080static int
e62afb5f 1081ext2_symlink(struct vop_old_symlink_args *ap)
984263bc 1082{
f7aae92f
RG
1083 struct vnode *vp, **vpp = ap->a_vpp;
1084 struct inode *ip;
984263bc
MD
1085 int len, error;
1086
1087 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp,
1088 vpp, ap->a_cnp);
1089 if (error)
1090 return (error);
1091 vp = *vpp;
1092 len = strlen(ap->a_target);
1093 if (len < vp->v_mount->mnt_maxsymlinklen) {
1094 ip = VTOI(vp);
1095 bcopy(ap->a_target, (char *)ip->i_shortlink, len);
1096 ip->i_size = len;
1097 ip->i_flag |= IN_CHANGE | IN_UPDATE;
6c23cda9 1098 } else {
93f50d60
MD
1099 /*
1100 * Make sure we have a VM object in order to use
1101 * the buffer cache.
1102 */
1103 if (vp->v_object == NULL)
1c843a13 1104 vinitvmio(vp, 0);
93f50d60 1105
984263bc 1106 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0,
87de5057 1107 UIO_SYSSPACE, IO_NODELOCKED,
60233e58 1108 ap->a_cnp->cn_cred, NULL);
dd41ecb0
SW
1109
1110 if (error)
1111 vput(vp);
6c23cda9
SW
1112 }
1113
984263bc
MD
1114 return (error);
1115}
1116
1117/*
1118 * Allocate a new inode.
1119 */
1120static int
0f7f7a49
CP
1121ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp,
1122 struct componentname *cnp)
984263bc 1123{
f7aae92f 1124 struct inode *ip, *pdir;
984263bc
MD
1125 struct vnode *tvp;
1126 int error;
1127
1128 pdir = VTOI(dvp);
984263bc
MD
1129 *vpp = NULL;
1130 if ((mode & IFMT) == 0)
1131 mode |= IFREG;
1132
1f1db49f 1133 error = EXT2_VALLOC(dvp, mode, cnp->cn_cred, &tvp);
984263bc
MD
1134 if (error) {
1135 return (error);
1136 }
1137 ip = VTOI(tvp);
1138 ip->i_gid = pdir->i_gid;
1139#ifdef SUIDDIR
1140 {
1141#ifdef QUOTA
1142 struct ucred ucred, *ucp;
1143 ucp = cnp->cn_cred;
1d94a9ab 1144#endif
984263bc
MD
1145 /*
1146 * if we are
1147 * not the owner of the directory,
1148 * and we are hacking owners here, (only do this where told to)
1149 * and we are not giving it TOO root, (would subvert quotas)
1150 * then go ahead and give it to the other user.
1151 * Note that this drops off the execute bits for security.
1152 */
1153 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) &&
1154 (pdir->i_mode & ISUID) &&
1155 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) {
1156 ip->i_uid = pdir->i_uid;
1157 mode &= ~07111;
1158#ifdef QUOTA
1159 /*
1160 * make sure the correct user gets charged
1161 * for the space.
1162 * Quickly knock up a dummy credential for the victim.
1163 * XXX This seems to never be accessed out of our
1164 * context so a stack variable is ok.
1165 */
1166 ucred.cr_ref = 1;
1167 ucred.cr_uid = ip->i_uid;
1168 ucred.cr_ngroups = 1;
1169 ucred.cr_groups[0] = pdir->i_gid;
1170 ucp = &ucred;
1d94a9ab 1171#endif
984263bc
MD
1172 } else {
1173 ip->i_uid = cnp->cn_cred->cr_uid;
1174 }
1175
1176#ifdef QUOTA
6f1a0d44 1177 if ((error = ext2_getinoquota(ip)) ||
1f1db49f
MD
1178 (error = ext2_chkiq(ip, 1, ucp, 0))) {
1179 EXT2_VFREE(tvp, ip->i_number, mode);
984263bc
MD
1180 vput(tvp);
1181 return (error);
1182 }
1183#endif
1184 }
1185#else
1186 ip->i_uid = cnp->cn_cred->cr_uid;
1187#ifdef QUOTA
1f1db49f
MD
1188 if ((error = ext2_getinoquota(ip)) ||
1189 (error = ext2_chkiq(ip, 1, cnp->cn_cred, 0))) {
1190 EXT2_VFREE(tvp, ip->i_number, mode);
984263bc
MD
1191 vput(tvp);
1192 return (error);
1193 }
1194#endif
1195#endif
1196 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1197 ip->i_mode = mode;
1198 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */
1199 ip->i_nlink = 1;
1200 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) &&
61b7a688 1201 priv_check_cred(cnp->cn_cred, PRIV_VFS_SETGID, 0))
984263bc
MD
1202 ip->i_mode &= ~ISGID;
1203
2b69e610 1204 if (cnp->cn_flags & CNP_ISWHITEOUT)
984263bc
MD
1205 ip->i_flags |= UF_OPAQUE;
1206
1c843a13
MD
1207 /*
1208 * Regular files and directories need VM objects. Softlinks do
1209 * not (not immediately anyway).
1210 */
1211 if (tvp->v_type == VREG || tvp->v_type == VDIR)
1212 vinitvmio(tvp, 0);
1213
984263bc
MD
1214 /*
1215 * Make sure inode goes to disk before directory entry.
1216 */
1f1db49f 1217 error = EXT2_UPDATE(tvp, 1);
984263bc
MD
1218 if (error)
1219 goto bad;
1220 error = ext2_direnter(ip, dvp, cnp);
1221 if (error)
1222 goto bad;
1223
1224 *vpp = tvp;
1225 return (0);
1226
1227bad:
1228 /*
1229 * Write error occurred trying to update the inode
1230 * or the directory so must deallocate the inode.
1231 */
1232 ip->i_nlink = 0;
1233 ip->i_flag |= IN_CHANGE;
1234 vput(tvp);
1235 return (error);
1236}
1237
1238/*
1239 * get page routine
1240 *
1241 * XXX By default, wimp out... note that a_offset is ignored (and always
1242 * XXX has been).
1243 */
1244static int
0f7f7a49 1245ext2_getpages(struct vop_getpages_args *ap)
984263bc
MD
1246{
1247 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1248 ap->a_reqpage));
1249}
1250
1f1db49f
MD
1251void
1252ext2_itimes(struct vnode *vp)
1253{
1254 struct inode *ip;
1255 struct timespec ts;
1256
1257 ip = VTOI(vp);
1258 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
1259 return;
1260 if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp))
1261 ip->i_flag |= IN_LAZYMOD;
1262 else
1263 ip->i_flag |= IN_MODIFIED;
1264 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
1265 vfs_timestamp(&ts);
1266 if (ip->i_flag & IN_ACCESS) {
1267 ip->i_atime = ts.tv_sec;
1268 ip->i_atimensec = ts.tv_nsec;
1269 }
1270 if (ip->i_flag & IN_UPDATE) {
1271 ip->i_mtime = ts.tv_sec;
1272 ip->i_mtimensec = ts.tv_nsec;
1273 ip->i_modrev++;
1274 }
1275 if (ip->i_flag & IN_CHANGE) {
1276 ip->i_ctime = ts.tv_sec;
1277 ip->i_ctimensec = ts.tv_nsec;
1278 }
1279 }
1280 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
1281}
1282
1283/*
1284 * Open called.
1285 *
1286 * Nothing to do.
1287 *
b478fdce
SW
1288 * ext2_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
1289 * struct file *a_fp)
1f1db49f
MD
1290 */
1291/* ARGSUSED */
1292static
1293int
1294ext2_open(struct vop_open_args *ap)
1295{
1296 struct vnode *vp = ap->a_vp;
1297
1298 /*
1299 * Files marked append-only must be opened for appending.
1300 */
1301 if ((VTOI(vp)->i_flags & APPEND) &&
1302 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) {
1303 return (EPERM);
1304 }
1f1db49f
MD
1305 return (vop_stdopen(ap));
1306}
1307
1308/*
1309 * Close called.
1310 *
1311 * Update the times on the inode.
1312 *
87de5057 1313 * ext2_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1f1db49f
MD
1314 */
1315/* ARGSUSED */
1316static
1317int
1318ext2_close(struct vop_close_args *ap)
1319{
1320 struct vnode *vp = ap->a_vp;
1321
3c37c940 1322 if (vp->v_sysref.refcnt > 1)
1f1db49f
MD
1323 ext2_itimes(vp);
1324 return (vop_stdclose(ap));
1325}
1326
1327/*
87de5057 1328 * ext2_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
1f1db49f
MD
1329 */
1330static
1331int
1332ext2_access(struct vop_access_args *ap)
1333{
1334 struct vnode *vp = ap->a_vp;
1335 struct inode *ip = VTOI(vp);
1f1db49f 1336 int error;
1f1db49f 1337
1dbf38bb
NT
1338#ifdef QUOTA
1339 if (ap->a_mode & VWRITE) {
1f1db49f
MD
1340 switch (vp->v_type) {
1341 case VDIR:
1342 case VLNK:
1343 case VREG:
1f1db49f
MD
1344 if ((error = ext2_getinoquota(ip)) != 0)
1345 return (error);
1f1db49f
MD
1346 break;
1347 default:
1348 break;
1349 }
1350 }
1dbf38bb 1351#endif
1f1db49f 1352
1dbf38bb
NT
1353 error = vop_helper_access(ap, ip->i_uid, ip->i_gid, ip->i_mode,
1354 ip->i_flags);
1355 return (error);
1f1db49f
MD
1356}
1357
1358/*
87de5057 1359 * ext2_getattr(struct vnode *a_vp, struct vattr *a_vap)
1f1db49f
MD
1360 */
1361/* ARGSUSED */
1362static
1363int
1364ext2_getattr(struct vop_getattr_args *ap)
1365{
1366 struct vnode *vp = ap->a_vp;
1367 struct inode *ip = VTOI(vp);
1368 struct vattr *vap = ap->a_vap;
1369
1f1db49f
MD
1370 ext2_itimes(vp);
1371 /*
1372 * Copy from inode table
1373 */
1374 vap->va_fsid = dev2udev(ip->i_dev);
1375 vap->va_fileid = ip->i_number;
1376 vap->va_mode = ip->i_mode & ~IFMT;
1377 vap->va_nlink = VFSTOEXT2(vp->v_mount)->um_i_effnlink_valid ?
1378 ip->i_effnlink : ip->i_nlink;
1379 vap->va_uid = ip->i_uid;
1380 vap->va_gid = ip->i_gid;
0e9b9130
MD
1381 vap->va_rmajor = umajor(ip->i_rdev);
1382 vap->va_rminor = uminor(ip->i_rdev);
1f1db49f
MD
1383 vap->va_size = ip->i_din.di_size;
1384 vap->va_atime.tv_sec = ip->i_atime;
1385 vap->va_atime.tv_nsec = ip->i_atimensec;
1386 vap->va_mtime.tv_sec = ip->i_mtime;
1387 vap->va_mtime.tv_nsec = ip->i_mtimensec;
1388 vap->va_ctime.tv_sec = ip->i_ctime;
1389 vap->va_ctime.tv_nsec = ip->i_ctimensec;
1390 vap->va_flags = ip->i_flags;
1391 vap->va_gen = ip->i_gen;
1392 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1393 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks);
1394 vap->va_type = IFTOVT(ip->i_mode);
1395 vap->va_filerev = ip->i_modrev;
1f1db49f
MD
1396 return (0);
1397}
1398
1399/*
1400 * Set attribute vnode op. called from several syscalls
1401 *
87de5057 1402 * ext2_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
1f1db49f
MD
1403 */
1404static
1405int
1406ext2_setattr(struct vop_setattr_args *ap)
1407{
1408 struct vattr *vap = ap->a_vap;
1409 struct vnode *vp = ap->a_vp;
1410 struct inode *ip = VTOI(vp);
1411 struct ucred *cred = ap->a_cred;
1412 int error;
1413
1414 /*
1415 * Check for unsettable attributes.
1416 */
1417 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
1418 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
0e9b9130 1419 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
1f1db49f
MD
1420 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
1421 return (EINVAL);
1422 }
1423 if (vap->va_flags != VNOVAL) {
1424 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1425 return (EROFS);
1426 if (cred->cr_uid != ip->i_uid &&
db813a0c 1427 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
1f1db49f
MD
1428 return (error);
1429 /*
1430 * Note that a root chflags becomes a user chflags when
1431 * we are jailed, unless the jail.chflags_allowed sysctl
1432 * is set.
1433 */
1434 if (cred->cr_uid == 0 &&
1435 (!jailed(cred) || jail_chflags_allowed)) {
1436 if ((ip->i_flags
1437 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) &&
1438 securelevel > 0)
1439 return (EPERM);
1440 ip->i_flags = vap->va_flags;
1441 } else {
1442 if (ip->i_flags
1443 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1444 (vap->va_flags & UF_SETTABLE) != vap->va_flags)
1445 return (EPERM);
1446 ip->i_flags &= SF_SETTABLE;
1447 ip->i_flags |= (vap->va_flags & UF_SETTABLE);
1448 }
1449 ip->i_flag |= IN_CHANGE;
1450 if (vap->va_flags & (IMMUTABLE | APPEND))
1451 return (0);
1452 }
1453 if (ip->i_flags & (IMMUTABLE | APPEND))
1454 return (EPERM);
1455 /*
1456 * Go through the fields and update iff not VNOVAL.
1457 */
1458 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1459 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1460 return (EROFS);
87de5057 1461 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred)) != 0)
1f1db49f
MD
1462 return (error);
1463 }
1464 if (vap->va_size != VNOVAL) {
1465 /*
1466 * Disallow write attempts on read-only filesystems;
1467 * unless the file is a socket, fifo, or a block or
1468 * character device resident on the filesystem.
1469 */
1470 switch (vp->v_type) {
1471 case VDIR:
1472 return (EISDIR);
1473 case VLNK:
1474 case VREG:
1475 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1476 return (EROFS);
1477 break;
1478 default:
1479 break;
1480 }
87de5057 1481 if ((error = EXT2_TRUNCATE(vp, vap->va_size, 0, cred)) != 0)
1f1db49f
MD
1482 return (error);
1483 }
1484 ip = VTOI(vp);
1485 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
1486 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1487 return (EROFS);
1488 if (cred->cr_uid != ip->i_uid &&
db813a0c 1489 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
1f1db49f 1490 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
cb66845a 1491 (error = VOP_EACCESS(vp, VWRITE, cred))))
1f1db49f
MD
1492 return (error);
1493 if (vap->va_atime.tv_sec != VNOVAL)
1494 ip->i_flag |= IN_ACCESS;
1495 if (vap->va_mtime.tv_sec != VNOVAL)
1496 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1497 ext2_itimes(vp);
1498 if (vap->va_atime.tv_sec != VNOVAL) {
1499 ip->i_atime = vap->va_atime.tv_sec;
1500 ip->i_atimensec = vap->va_atime.tv_nsec;
1501 }
1502 if (vap->va_mtime.tv_sec != VNOVAL) {
1503 ip->i_mtime = vap->va_mtime.tv_sec;
1504 ip->i_mtimensec = vap->va_mtime.tv_nsec;
1505 }
1506 error = EXT2_UPDATE(vp, 0);
1507 if (error)
1508 return (error);
1509 }
1510 error = 0;
1511 if (vap->va_mode != (mode_t)VNOVAL) {
1512 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1513 return (EROFS);
87de5057 1514 error = ext2_chmod(vp, (int)vap->va_mode, cred);
1f1db49f
MD
1515 }
1516 VN_KNOTE(vp, NOTE_ATTRIB);
1517 return (error);
1518}
1519
1520/*
1521 * Change the mode on a file.
1522 * Inode must be locked before calling.
1523 */
1524static int
87de5057 1525ext2_chmod(struct vnode *vp, int mode, struct ucred *cred)
1f1db49f
MD
1526{
1527 struct inode *ip = VTOI(vp);
1528 int error;
1529
1530 if (cred->cr_uid != ip->i_uid) {
a79e2ed8 1531 error = priv_check_cred(cred, PRIV_VFS_CHMOD, 0);
1f1db49f
MD
1532 if (error)
1533 return (error);
1534 }
1535 if (cred->cr_uid) {
1536 if (vp->v_type != VDIR && (mode & S_ISTXT))
1537 return (EFTYPE);
1538 if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
1539 return (EPERM);
1540 }
1541 ip->i_mode &= ~ALLPERMS;
1542 ip->i_mode |= (mode & ALLPERMS);
1543 ip->i_flag |= IN_CHANGE;
1544 return (0);
1545}
1546
1547/*
1548 * Perform chown operation on inode ip;
1549 * inode must be locked prior to call.
1550 */
1551static int
87de5057 1552ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1f1db49f
MD
1553{
1554 struct inode *ip = VTOI(vp);
1555 uid_t ouid;
1556 gid_t ogid;
1557 int error = 0;
1558#ifdef QUOTA
1559 int i;
1560 long change;
1561#endif
1562
1563 if (uid == (uid_t)VNOVAL)
1564 uid = ip->i_uid;
1565 if (gid == (gid_t)VNOVAL)
1566 gid = ip->i_gid;
1567 /*
1568 * If we don't own the file, are trying to change the owner
1569 * of the file, or are not a member of the target group,
1570 * the caller must be superuser or the call fails.
1571 */
1572 if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
1573 (gid != ip->i_gid && !(cred->cr_gid == gid ||
1574 groupmember((gid_t)gid, cred)))) &&
a79e2ed8 1575 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1f1db49f
MD
1576 return (error);
1577 ogid = ip->i_gid;
1578 ouid = ip->i_uid;
1579#ifdef QUOTA
1580 if ((error = ext2_getinoquota(ip)) != 0)
1581 return (error);
1582 if (ouid == uid) {
1583 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1584 ip->i_dquot[USRQUOTA] = NODQUOT;
1585 }
1586 if (ogid == gid) {
1587 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1588 ip->i_dquot[GRPQUOTA] = NODQUOT;
1589 }
1590 change = ip->i_blocks;
1591 (void) ext2_chkdq(ip, -change, cred, CHOWN);
1592 (void) ext2_chkiq(ip, -1, cred, CHOWN);
1593 for (i = 0; i < MAXQUOTAS; i++) {
1594 ext2_dqrele(vp, ip->i_dquot[i]);
1595 ip->i_dquot[i] = NODQUOT;
1596 }
1597#endif
1598 ip->i_gid = gid;
1599 ip->i_uid = uid;
1600#ifdef QUOTA
1601 if ((error = ext2_getinoquota(ip)) == 0) {
1602 if (ouid == uid) {
1603 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1604 ip->i_dquot[USRQUOTA] = NODQUOT;
1605 }
1606 if (ogid == gid) {
1607 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1608 ip->i_dquot[GRPQUOTA] = NODQUOT;
1609 }
1610 if ((error = ext2_chkdq(ip, change, cred, CHOWN)) == 0) {
1611 if ((error = ext2_chkiq(ip, 1, cred, CHOWN)) == 0)
1612 goto good;
1613 else
1614 (void)ext2_chkdq(ip, -change, cred, CHOWN|FORCE);
1615 }
1616 for (i = 0; i < MAXQUOTAS; i++) {
1617 ext2_dqrele(vp, ip->i_dquot[i]);
1618 ip->i_dquot[i] = NODQUOT;
1619 }
1620 }
1621 ip->i_gid = ogid;
1622 ip->i_uid = ouid;
1623 if (ext2_getinoquota(ip) == 0) {
1624 if (ouid == uid) {
1625 ext2_dqrele(vp, ip->i_dquot[USRQUOTA]);
1626 ip->i_dquot[USRQUOTA] = NODQUOT;
1627 }
1628 if (ogid == gid) {
1629 ext2_dqrele(vp, ip->i_dquot[GRPQUOTA]);
1630 ip->i_dquot[GRPQUOTA] = NODQUOT;
1631 }
1632 (void) ext2_chkdq(ip, change, cred, FORCE|CHOWN);
1633 (void) ext2_chkiq(ip, 1, cred, FORCE|CHOWN);
1634 (void) ext2_getinoquota(ip);
1635 }
1636 return (error);
1637good:
1638 if (ext2_getinoquota(ip))
1639 panic("ext2_chown: lost quota");
1640#endif /* QUOTA */
1641 ip->i_flag |= IN_CHANGE;
1642 if (cred->cr_uid != 0 && (ouid != uid || ogid != gid))
1643 ip->i_mode &= ~(ISUID | ISGID);
1644 return (0);
1645}
1646
1647/*
1648 * Mmap a file
1649 *
1650 * NB Currently unsupported.
1651 *
87de5057 1652 * ext2_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
1f1db49f
MD
1653 */
1654/* ARGSUSED */
1655static
1656int
1657ext2_mmap(struct vop_mmap_args *ap)
1658{
1659 return (EINVAL);
1660}
1661
1662/*
1663 * whiteout vnode call
1664 *
1665 * ext2_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags)
1666 */
1667static
1668int
1669ext2_whiteout(struct vop_old_whiteout_args *ap)
1670{
1671 return (EOPNOTSUPP);
1672}
1673
1674/*
1675 * Return target name of a symbolic link
1676 *
1677 * ext2_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1678 */
1679static
1680int
1681ext2_readlink(struct vop_readlink_args *ap)
1682{
1683 struct vnode *vp = ap->a_vp;
1684 struct inode *ip = VTOI(vp);
1685 int isize;
1686
1687 isize = ip->i_size;
1688 if ((isize < vp->v_mount->mnt_maxsymlinklen) ||
1689 (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */
1690 uiomove((char *)ip->i_shortlink, isize, ap->a_uio);
1691 return (0);
1692 }
1f1db49f
MD
1693 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1694}
1695
1696/*
1697 * Calculate the logical to physical mapping if not done already,
1698 * then call the device strategy routine.
1699 *
1700 * In order to be able to swap to a file, the VOP_BMAP operation may not
1701 * deadlock on memory. See ext2_bmap() for details.
1702 *
1703 * ext2_strategy(struct vnode *a_vp, struct bio *a_bio)
1704 */
1705static
1706int
1707ext2_strategy(struct vop_strategy_args *ap)
1708{
1709 struct bio *bio = ap->a_bio;
1710 struct bio *nbio;
1711 struct buf *bp = bio->bio_buf;
1712 struct vnode *vp = ap->a_vp;
1713 struct inode *ip;
1714 int error;
1715
1716 ip = VTOI(vp);
1717 if (vp->v_type == VBLK || vp->v_type == VCHR)
1718 panic("ext2_strategy: spec");
1719 nbio = push_bio(bio);
1720 if (nbio->bio_offset == NOOFFSET) {
08daea96 1721 error = VOP_BMAP(vp, bio->bio_offset, &nbio->bio_offset,
e92ca23a 1722 NULL, NULL, bp->b_cmd);
1f1db49f
MD
1723 if (error) {
1724 bp->b_error = error;
1725 bp->b_flags |= B_ERROR;
1726 /* I/O was never started on nbio, must biodone(bio) */
1727 biodone(bio);
1728 return (error);
1729 }
1730 if (nbio->bio_offset == NOOFFSET)
1731 vfs_bio_clrbuf(bp);
1732 }
1733 if (nbio->bio_offset == NOOFFSET) {
1734 /* I/O was never started on nbio, must biodone(bio) */
1735 biodone(bio);
1736 return (0);
1737 }
1738 vn_strategy(ip->i_devvp, nbio);
1739 return (0);
1740}
1741
1742/*
1743 * Print out the contents of an inode.
1744 *
1745 * ext2_print(struct vnode *a_vp)
1746 */
1747static
1748int
1749ext2_print(struct vop_print_args *ap)
1750{
1751 struct vnode *vp = ap->a_vp;
1752 struct inode *ip = VTOI(vp);
1753
086c1d7e 1754 kprintf("tag VT_EXT2FS, ino %lu, on dev %s (%d, %d)",
1f1db49f
MD
1755 (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev),
1756 minor(ip->i_dev));
1757 if (vp->v_type == VFIFO)
1758 fifo_printinfo(vp);
1759 lockmgr_printinfo(&vp->v_lock);
086c1d7e 1760 kprintf("\n");
1f1db49f
MD
1761 return (0);
1762}
1763
1f1db49f
MD
1764/*
1765 * Read wrapper for fifos.
1766 *
1767 * ext2fifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1768 * struct ucred *a_cred)
1769 */
1770static
1771int
1772ext2fifo_read(struct vop_read_args *ap)
1773{
1774 int error, resid;
1775 struct inode *ip;
1776 struct uio *uio;
1777
1778 uio = ap->a_uio;
1779 resid = uio->uio_resid;
66a1ddf5 1780 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1f1db49f
MD
1781 ip = VTOI(ap->a_vp);
1782 if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL &&
1783 (uio->uio_resid != resid || (error == 0 && resid != 0)))
1784 VTOI(ap->a_vp)->i_flag |= IN_ACCESS;
1785 return (error);
1786}
1787
1788/*
1789 * Write wrapper for fifos.
1790 *
1791 * ext2fifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1792 * struct ucred *a_cred)
1793 */
1794static
1795int
1796ext2fifo_write(struct vop_write_args *ap)
1797{
1798 int error, resid;
1799 struct inode *ip;
1800 struct uio *uio;
1801
1802 uio = ap->a_uio;
1803 resid = uio->uio_resid;
66a1ddf5 1804 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1f1db49f
MD
1805 ip = VTOI(ap->a_vp);
1806 if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0)))
1807 VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE;
1808 return (error);
1809}
1810
1811/*
1812 * Close wrapper for fifos.
1813 *
1814 * Update the times on the inode then do device close.
1815 *
87de5057 1816 * ext2fifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred)
1f1db49f
MD
1817 */
1818static
1819int
1820ext2fifo_close(struct vop_close_args *ap)
1821{
1822 struct vnode *vp = ap->a_vp;
1823
3c37c940 1824 if (vp->v_sysref.refcnt > 1)
1f1db49f 1825 ext2_itimes(vp);
66a1ddf5 1826 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1f1db49f
MD
1827}
1828
1829/*
1830 * Kqfilter wrapper for fifos.
1831 *
1832 * Fall through to ext2 kqfilter routines if needed
1833 */
1834static
1835int
1836ext2fifo_kqfilter(struct vop_kqfilter_args *ap)
1837{
1838 int error;
1839
66a1ddf5 1840 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1f1db49f
MD
1841 if (error)
1842 error = ext2_kqfilter(ap);
1843 return (error);
1844}
1845
1846/*
1847 * Return POSIX pathconf information applicable to ext2 filesystems.
1848 *
1849 * ext2_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1850 */
1851static
1852int
1853ext2_pathconf(struct vop_pathconf_args *ap)
1854{
1855 switch (ap->a_name) {
1856 case _PC_LINK_MAX:
1857 *ap->a_retval = LINK_MAX;
1858 return (0);
1859 case _PC_NAME_MAX:
1860 *ap->a_retval = NAME_MAX;
1861 return (0);
1862 case _PC_PATH_MAX:
1863 *ap->a_retval = PATH_MAX;
1864 return (0);
1865 case _PC_PIPE_BUF:
1866 *ap->a_retval = PIPE_BUF;
1867 return (0);
1868 case _PC_CHOWN_RESTRICTED:
1869 *ap->a_retval = 1;
1870 return (0);
1871 case _PC_NO_TRUNC:
1872 *ap->a_retval = 1;
1873 return (0);
1874 default:
1875 return (EINVAL);
1876 }
1877 /* NOTREACHED */
1878}
1879
1880/*
1881 * Advisory record locking support
1882 *
1883 * ext2_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
1884 * int a_flags)
1885 */
1886static
1887int
1888ext2_advlock(struct vop_advlock_args *ap)
1889{
1890 struct inode *ip = VTOI(ap->a_vp);
1891
1892 return (lf_advlock(ap, &(ip->i_lockf), ip->i_size));
1893}
1894
1895/*
1896 * Initialize the vnode associated with a new inode, handle aliased
1897 * vnodes.
1898 */
1899int
1900ext2_vinit(struct mount *mntp, struct vnode **vpp)
1901{
1902 struct inode *ip;
1903 struct vnode *vp;
1904 struct timeval tv;
1905
1906 vp = *vpp;
1907 ip = VTOI(vp);
1908
1909 switch(vp->v_type = IFTOVT(ip->i_mode)) {
1910 case VCHR:
1911 case VBLK:
1912 vp->v_ops = &mntp->mnt_vn_spec_ops;
0e9b9130 1913 addaliasu(vp, umajor(ip->i_rdev), uminor(ip->i_rdev));
1f1db49f
MD
1914 break;
1915 case VFIFO:
1916 vp->v_ops = &mntp->mnt_vn_fifo_ops;
1917 break;
93f50d60 1918 case VDIR:
1c843a13
MD
1919 case VREG:
1920 vinitvmio(vp, ip->i_size);
1921 break;
1922 case VLNK:
1923 if ((ip->i_size >= vp->v_mount->mnt_maxsymlinklen) &&
1924 ip->i_din.di_blocks != 0
1925 ) {
1926 vinitvmio(vp, ip->i_size);
1927 }
93f50d60 1928 break;
1f1db49f
MD
1929 default:
1930 break;
1931
1932 }
1933
1934 if (ip->i_number == ROOTINO)
1935 vp->v_flag |= VROOT;
1936 /*
1937 * Initialize modrev times
1938 */
1939 getmicrouptime(&tv);
1940 SETHIGH(ip->i_modrev, tv.tv_sec);
1941 SETLOW(ip->i_modrev, tv.tv_usec * 4294);
1942 *vpp = vp;
1943 return (0);
1944}
1945
1946static struct filterops ext2read_filtops =
1947 { 1, NULL, filt_ext2detach, filt_ext2read };
1948static struct filterops ext2write_filtops =
1949 { 1, NULL, filt_ext2detach, filt_ext2write };
1950static struct filterops ext2vnode_filtops =
1951 { 1, NULL, filt_ext2detach, filt_ext2vnode };
1952
1953/*
1954 * ext2_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1955 */
1956static int
1957ext2_kqfilter(struct vop_kqfilter_args *ap)
1958{
1959 struct vnode *vp = ap->a_vp;
1960 struct knote *kn = ap->a_kn;
0202303b 1961 lwkt_tokref vlock;
1f1db49f
MD
1962
1963 switch (kn->kn_filter) {
1964 case EVFILT_READ:
1965 kn->kn_fop = &ext2read_filtops;
1966 break;
1967 case EVFILT_WRITE:
1968 kn->kn_fop = &ext2write_filtops;
1969 break;
1970 case EVFILT_VNODE:
1971 kn->kn_fop = &ext2vnode_filtops;
1972 break;
1973 default:
1974 return (1);
1975 }
1976
1977 kn->kn_hook = (caddr_t)vp;
1978
0202303b 1979 lwkt_gettoken(&vlock, &vp->v_token);
1f1db49f 1980 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
0202303b 1981 lwkt_reltoken(&vlock);
1f1db49f
MD
1982
1983 return (0);
1984}
1985
1986static void
1987filt_ext2detach(struct knote *kn)
1988{
1989 struct vnode *vp = (struct vnode *)kn->kn_hook;
0202303b 1990 lwkt_tokref vlock;
1f1db49f 1991
0202303b 1992 lwkt_gettoken(&vlock, &vp->v_token);
1f1db49f
MD
1993 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
1994 kn, knote, kn_selnext);
0202303b 1995 lwkt_reltoken(&vlock);
1f1db49f
MD
1996}
1997
1998/*ARGSUSED*/
1999static int
2000filt_ext2read(struct knote *kn, long hint)
2001{
2002 struct vnode *vp = (struct vnode *)kn->kn_hook;
2003 struct inode *ip = VTOI(vp);
2004
2005 /*
2006 * filesystem is gone, so set the EOF flag and schedule
2007 * the knote for deletion.
2008 */
2009 if (hint == NOTE_REVOKE) {
2010 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2011 return (1);
2012 }
2013
2014 kn->kn_data = ip->i_size - kn->kn_fp->f_offset;
2015 return (kn->kn_data != 0);
2016}
2017
2018/*ARGSUSED*/
2019static int
2020filt_ext2write(struct knote *kn, long hint)
2021{
2022 /*
2023 * filesystem is gone, so set the EOF flag and schedule
2024 * the knote for deletion.
2025 */
2026 if (hint == NOTE_REVOKE)
2027 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2028
2029 kn->kn_data = 0;
2030 return (1);
2031}
2032
2033static int
2034filt_ext2vnode(struct knote *kn, long hint)
2035{
2036 if (kn->kn_sfflags & hint)
2037 kn->kn_fflags |= hint;
2038 if (hint == NOTE_REVOKE) {
2039 kn->kn_flags |= EV_EOF;
2040 return (1);
2041 }
2042 return (kn->kn_fflags != 0);
2043}
2044
66a1ddf5
MD
2045struct vop_ops ext2_vnode_vops = {
2046 .vop_default = vop_defaultop,
2047 .vop_fsync = ext2_fsync,
2048 .vop_read = ext2_read,
2049 .vop_reallocblks = ext2_reallocblks,
2050 .vop_write = ext2_write,
2051 .vop_access = ext2_access,
2052 .vop_advlock = ext2_advlock,
2053 .vop_bmap = ext2_bmap,
2054 .vop_old_lookup = ext2_lookup,
2055 .vop_close = ext2_close,
2056 .vop_old_create = ext2_create,
2057 .vop_getattr = ext2_getattr,
2058 .vop_inactive = ext2_inactive,
66a1ddf5 2059 .vop_old_link = ext2_link,
66a1ddf5
MD
2060 .vop_old_mkdir = ext2_mkdir,
2061 .vop_old_mknod = ext2_mknod,
2062 .vop_mmap = ext2_mmap,
2063 .vop_open = ext2_open,
2064 .vop_pathconf = ext2_pathconf,
2065 .vop_poll = vop_stdpoll,
2066 .vop_kqfilter = ext2_kqfilter,
2067 .vop_print = ext2_print,
2068 .vop_readdir = ext2_readdir,
2069 .vop_readlink = ext2_readlink,
2070 .vop_reclaim = ext2_reclaim,
2071 .vop_old_remove = ext2_remove,
2072 .vop_old_rename = ext2_rename,
2073 .vop_old_rmdir = ext2_rmdir,
2074 .vop_setattr = ext2_setattr,
2075 .vop_strategy = ext2_strategy,
2076 .vop_old_symlink = ext2_symlink,
66a1ddf5
MD
2077 .vop_old_whiteout = ext2_whiteout,
2078 .vop_getpages = ext2_getpages,
1787385d 2079 .vop_putpages = vop_stdputpages
1f1db49f 2080};
66a1ddf5
MD
2081
2082struct vop_ops ext2_spec_vops = {
8be7edad 2083 .vop_default = vop_defaultop,
66a1ddf5
MD
2084 .vop_fsync = ext2_fsync,
2085 .vop_access = ext2_access,
8be7edad 2086 .vop_close = ext2_close,
66a1ddf5
MD
2087 .vop_getattr = ext2_getattr,
2088 .vop_inactive = ext2_inactive,
66a1ddf5 2089 .vop_print = ext2_print,
8be7edad 2090 .vop_read = vop_stdnoread,
66a1ddf5
MD
2091 .vop_reclaim = ext2_reclaim,
2092 .vop_setattr = ext2_setattr,
8be7edad 2093 .vop_write = vop_stdnowrite
1f1db49f 2094};
66a1ddf5
MD
2095
2096struct vop_ops ext2_fifo_vops = {
2097 .vop_default = ext2_vnoperatefifo,
2098 .vop_fsync = ext2_fsync,
2099 .vop_access = ext2_access,
2100 .vop_close = ext2fifo_close,
2101 .vop_getattr = ext2_getattr,
2102 .vop_inactive = ext2_inactive,
66a1ddf5 2103 .vop_kqfilter = ext2fifo_kqfilter,
66a1ddf5
MD
2104 .vop_print = ext2_print,
2105 .vop_read = ext2fifo_read,
2106 .vop_reclaim = ext2_reclaim,
2107 .vop_setattr = ext2_setattr,
66a1ddf5 2108 .vop_write = ext2fifo_write
1f1db49f 2109};
1f1db49f 2110
66a1ddf5
MD
2111VNODEOP_SET(ext2_vnode_vops);
2112VNODEOP_SET(ext2_spec_vops);
2113VNODEOP_SET(ext2_fifo_vops);
1f1db49f
MD
2114
2115/*
31bd717a 2116 * ext2_vnoperate()
1f1db49f
MD
2117 */
2118int
2119ext2_vnoperate(struct vop_generic_args *ap)
2120{
66a1ddf5 2121 return (VOCALL(&ext2_vnode_vops, ap));
1f1db49f
MD
2122}
2123
2124/*
31bd717a 2125 * ext2_vnoperatefifo()
1f1db49f
MD
2126 */
2127int
2128ext2_vnoperatefifo(struct vop_generic_args *ap)
2129{
66a1ddf5 2130 return (VOCALL(&ext2_fifo_vops, ap));
1f1db49f
MD
2131}
2132