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