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