1 /* $NetBSD: ext2fs_vnops.c,v 1.117 2015/04/20 23:03:09 riastradh Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
37 * Modified for ext2fs by Manuel Bouyer.
41 * Copyright (c) 1997 Manuel Bouyer.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94
64 * Modified for ext2fs by Manuel Bouyer.
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.117 2015/04/20 23:03:09 riastradh Exp $");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/resourcevar.h>
73 #include <sys/kernel.h>
78 #include <sys/mount.h>
79 #include <sys/namei.h>
80 #include <sys/vnode.h>
81 #include <sys/lockf.h>
83 #include <sys/signalvar.h>
84 #include <sys/kauth.h>
86 #include <miscfs/fifofs/fifo.h>
87 #include <miscfs/genfs/genfs.h>
88 #include <miscfs/specfs/specdev.h>
90 #include <ufs/ufs/inode.h>
91 #include <ufs/ufs/ufs_extern.h>
92 #include <ufs/ufs/ufsmount.h>
94 #include <ufs/ext2fs/ext2fs.h>
95 #include <ufs/ext2fs/ext2fs_extern.h>
96 #include <ufs/ext2fs/ext2fs_dir.h>
100 static int ext2fs_chmod(struct vnode
*, int, kauth_cred_t
, struct lwp
*);
101 static int ext2fs_chown(struct vnode
*, uid_t
, gid_t
, kauth_cred_t
,
109 #define SETHIGH(q, h) { \
112 tmp.val[_QUAD_HIGHWORD] = (h); \
115 #define SETLOW(q, l) { \
118 tmp.val[_QUAD_LOWWORD] = (l); \
123 * Create a regular file
126 ext2fs_create(void *v
)
128 struct vop_create_v3_args
/* {
130 struct vnode **a_vpp;
131 struct componentname *a_cnp;
137 ext2fs_makeinode(MAKEIMODE(ap
->a_vap
->va_type
, ap
->a_vap
->va_mode
),
138 ap
->a_dvp
, ap
->a_vpp
, ap
->a_cnp
);
142 VN_KNOTE(ap
->a_dvp
, NOTE_WRITE
);
143 VOP_UNLOCK(*ap
->a_vpp
);
152 ext2fs_mknod(void *v
)
154 struct vop_mknod_v3_args
/* {
156 struct vnode **a_vpp;
157 struct componentname *a_cnp;
160 struct vattr
*vap
= ap
->a_vap
;
161 struct vnode
**vpp
= ap
->a_vpp
;
167 if ((error
= ext2fs_makeinode(MAKEIMODE(vap
->va_type
, vap
->va_mode
),
168 ap
->a_dvp
, vpp
, ap
->a_cnp
)) != 0)
170 VN_KNOTE(ap
->a_dvp
, NOTE_WRITE
);
172 mp
= (*vpp
)->v_mount
;
174 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
175 if (vap
->va_rdev
!= VNOVAL
) {
177 * Want to be able to use this to make badblock
178 * inodes, so don't truncate the dev number.
180 ip
->i_din
.e2fs_din
->e2di_rdev
= h2fs32(vap
->va_rdev
);
183 * Remove inode so that it will be reloaded by vcache_get and
184 * checked to see if it is an alias of an existing entry in
187 (*vpp
)->v_type
= VNON
;
190 error
= vcache_get(mp
, &ino
, sizeof(ino
), vpp
);
201 * Just check the APPEND flag.
207 struct vop_open_args
/* {
214 * Files marked append-only must be opened for appending.
216 if ((VTOI(ap
->a_vp
)->i_e2fs_flags
& EXT2_APPEND
) &&
217 (ap
->a_mode
& (FWRITE
| O_APPEND
)) == FWRITE
)
223 ext2fs_check_possible(struct vnode
*vp
, struct inode
*ip
, mode_t mode
)
227 * Disallow write attempts on read-only file systems;
228 * unless the file is a socket, fifo, or a block or
229 * character device resident on the file system.
232 switch (vp
->v_type
) {
236 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
244 /* If immutable bit set, nobody gets to write it. */
245 if ((mode
& VWRITE
) && (ip
->i_e2fs_flags
& EXT2_IMMUTABLE
))
252 ext2fs_check_permitted(struct vnode
*vp
, struct inode
*ip
, mode_t mode
,
256 return kauth_authorize_vnode(cred
, KAUTH_ACCESS_ACTION(mode
, vp
->v_type
,
257 ip
->i_e2fs_mode
& ALLPERMS
), vp
, NULL
, genfs_can_access(vp
->v_type
,
258 ip
->i_e2fs_mode
& ALLPERMS
, ip
->i_uid
, ip
->i_gid
, mode
, cred
));
262 ext2fs_access(void *v
)
264 struct vop_access_args
/* {
269 struct vnode
*vp
= ap
->a_vp
;
270 struct inode
*ip
= VTOI(vp
);
271 mode_t mode
= ap
->a_mode
;
274 error
= ext2fs_check_possible(vp
, ip
, mode
);
278 error
= ext2fs_check_permitted(vp
, ip
, mode
, ap
->a_cred
);
285 ext2fs_getattr(void *v
)
287 struct vop_getattr_args
/* {
292 struct vnode
*vp
= ap
->a_vp
;
293 struct inode
*ip
= VTOI(vp
);
294 struct vattr
*vap
= ap
->a_vap
;
296 EXT2FS_ITIMES(ip
, NULL
, NULL
, NULL
);
298 * Copy from inode table
300 vap
->va_fsid
= ip
->i_dev
;
301 vap
->va_fileid
= ip
->i_number
;
302 vap
->va_mode
= ip
->i_e2fs_mode
& ALLPERMS
;
303 vap
->va_nlink
= ip
->i_e2fs_nlink
;
304 vap
->va_uid
= ip
->i_uid
;
305 vap
->va_gid
= ip
->i_gid
;
306 vap
->va_rdev
= (dev_t
)fs2h32(ip
->i_din
.e2fs_din
->e2di_rdev
);
307 vap
->va_size
= vp
->v_size
;
308 vap
->va_atime
.tv_sec
= ip
->i_e2fs_atime
;
309 vap
->va_atime
.tv_nsec
= 0;
310 vap
->va_mtime
.tv_sec
= ip
->i_e2fs_mtime
;
311 vap
->va_mtime
.tv_nsec
= 0;
312 vap
->va_ctime
.tv_sec
= ip
->i_e2fs_ctime
;
313 vap
->va_ctime
.tv_nsec
= 0;
314 #ifdef EXT2FS_SYSTEM_FLAGS
315 vap
->va_flags
= (ip
->i_e2fs_flags
& EXT2_APPEND
) ? SF_APPEND
: 0;
316 vap
->va_flags
|= (ip
->i_e2fs_flags
& EXT2_IMMUTABLE
) ? SF_IMMUTABLE
: 0;
318 vap
->va_flags
= (ip
->i_e2fs_flags
& EXT2_APPEND
) ? UF_APPEND
: 0;
319 vap
->va_flags
|= (ip
->i_e2fs_flags
& EXT2_IMMUTABLE
) ? UF_IMMUTABLE
: 0;
321 vap
->va_gen
= ip
->i_e2fs_gen
;
322 /* this doesn't belong here */
323 if (vp
->v_type
== VBLK
)
324 vap
->va_blocksize
= BLKDEV_IOSIZE
;
325 else if (vp
->v_type
== VCHR
)
326 vap
->va_blocksize
= MAXBSIZE
;
328 vap
->va_blocksize
= vp
->v_mount
->mnt_stat
.f_iosize
;
329 vap
->va_bytes
= dbtob(ext2fs_nblock(ip
));
330 vap
->va_type
= vp
->v_type
;
331 vap
->va_filerev
= ip
->i_modrev
;
336 * Set attribute vnode op. called from several syscalls
339 ext2fs_setattr(void *v
)
341 struct vop_setattr_args
/* {
346 struct vattr
*vap
= ap
->a_vap
;
347 struct vnode
*vp
= ap
->a_vp
;
348 struct inode
*ip
= VTOI(vp
);
349 kauth_cred_t cred
= ap
->a_cred
;
350 struct lwp
*l
= curlwp
;
352 kauth_action_t action
= KAUTH_VNODE_WRITE_FLAGS
;
353 bool changing_sysflags
= false;
356 * Check for unsettable attributes.
358 if ((vap
->va_type
!= VNON
) || (vap
->va_nlink
!= (nlink_t
)VNOVAL
) ||
359 (vap
->va_fsid
!= VNOVAL
) || (vap
->va_fileid
!= VNOVAL
) ||
360 (vap
->va_blocksize
!= VNOVAL
) || (vap
->va_rdev
!= VNOVAL
) ||
361 ((int)vap
->va_bytes
!= VNOVAL
) || (vap
->va_gen
!= VNOVAL
)) {
364 if (vap
->va_flags
!= VNOVAL
) {
365 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
369 * Check if we're allowed to change the flags.
370 * If EXT2FS_SYSTEM_FLAGS is set, then the flags are treated
371 * as system flags, otherwise they're considered to be user
374 #ifdef EXT2FS_SYSTEM_FLAGS
375 /* Indicate we're changing system flags if we are. */
376 if ((vap
->va_flags
& SF_APPEND
) ||
377 (vap
->va_flags
& SF_IMMUTABLE
)) {
378 action
|= KAUTH_VNODE_WRITE_SYSFLAGS
;
379 changing_sysflags
= true;
382 /* Indicate the node has system flags if it does. */
383 if (ip
->i_e2fs_flags
& (EXT2_APPEND
| EXT2_IMMUTABLE
)) {
384 action
|= KAUTH_VNODE_HAS_SYSFLAGS
;
386 #endif /* EXT2FS_SYSTEM_FLAGS */
388 error
= kauth_authorize_vnode(cred
, action
, vp
, NULL
,
389 genfs_can_chflags(cred
, vp
->v_type
, ip
->i_uid
,
394 #ifdef EXT2FS_SYSTEM_FLAGS
395 ip
->i_e2fs_flags
&= ~(EXT2_APPEND
| EXT2_IMMUTABLE
);
397 (vap
->va_flags
& SF_APPEND
) ? EXT2_APPEND
: 0 |
398 (vap
->va_flags
& SF_IMMUTABLE
) ? EXT2_IMMUTABLE
: 0;
400 ip
->i_e2fs_flags
&= ~(EXT2_APPEND
| EXT2_IMMUTABLE
);
402 (vap
->va_flags
& UF_APPEND
) ? EXT2_APPEND
: 0 |
403 (vap
->va_flags
& UF_IMMUTABLE
) ? EXT2_IMMUTABLE
: 0;
405 ip
->i_flag
|= IN_CHANGE
;
406 if (vap
->va_flags
& (IMMUTABLE
| APPEND
))
409 if (ip
->i_e2fs_flags
& (EXT2_APPEND
| EXT2_IMMUTABLE
))
412 * Go through the fields and update iff not VNOVAL.
414 if (vap
->va_uid
!= (uid_t
)VNOVAL
|| vap
->va_gid
!= (gid_t
)VNOVAL
) {
415 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
417 error
= ext2fs_chown(vp
, vap
->va_uid
, vap
->va_gid
, cred
, l
);
421 if (vap
->va_size
!= VNOVAL
) {
423 * Disallow write attempts on read-only file systems;
424 * unless the file is a socket, fifo, or a block or
425 * character device resident on the file system.
427 switch (vp
->v_type
) {
432 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
437 error
= ext2fs_truncate(vp
, vap
->va_size
, 0, cred
);
442 if (vap
->va_atime
.tv_sec
!= VNOVAL
|| vap
->va_mtime
.tv_sec
!= VNOVAL
) {
443 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
445 error
= kauth_authorize_vnode(cred
, KAUTH_VNODE_WRITE_TIMES
, vp
,
446 NULL
, genfs_can_chtimes(vp
, vap
->va_vaflags
, ip
->i_uid
,
450 if (vap
->va_atime
.tv_sec
!= VNOVAL
)
451 if (!(vp
->v_mount
->mnt_flag
& MNT_NOATIME
))
452 ip
->i_flag
|= IN_ACCESS
;
453 if (vap
->va_mtime
.tv_sec
!= VNOVAL
) {
454 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
455 if (vp
->v_mount
->mnt_flag
& MNT_RELATIME
)
456 ip
->i_flag
|= IN_ACCESS
;
458 error
= ext2fs_update(vp
, &vap
->va_atime
, &vap
->va_mtime
,
464 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
465 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
467 error
= ext2fs_chmod(vp
, (int)vap
->va_mode
, cred
, l
);
469 VN_KNOTE(vp
, NOTE_ATTRIB
);
474 * Change the mode on a file.
475 * Inode must be locked before calling.
478 ext2fs_chmod(struct vnode
*vp
, int mode
, kauth_cred_t cred
, struct lwp
*l
)
480 struct inode
*ip
= VTOI(vp
);
483 error
= kauth_authorize_vnode(cred
, KAUTH_VNODE_WRITE_SECURITY
, vp
,
484 NULL
, genfs_can_chmod(vp
->v_type
, cred
, ip
->i_uid
, ip
->i_gid
,
489 ip
->i_e2fs_mode
&= ~ALLPERMS
;
490 ip
->i_e2fs_mode
|= (mode
& ALLPERMS
);
491 ip
->i_flag
|= IN_CHANGE
;
496 * Perform chown operation on inode ip;
497 * inode must be locked prior to call.
500 ext2fs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, kauth_cred_t cred
,
503 struct inode
*ip
= VTOI(vp
);
508 if (uid
== (uid_t
)VNOVAL
)
510 if (gid
== (gid_t
)VNOVAL
)
513 error
= kauth_authorize_vnode(cred
, KAUTH_VNODE_CHANGE_OWNERSHIP
, vp
,
514 NULL
, genfs_can_chown(cred
, ip
->i_uid
, ip
->i_gid
, uid
, gid
));
521 ip
->i_e2fs_gid
= gid
& 0xffff;
522 ip
->i_e2fs_uid
= uid
& 0xffff;
523 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
524 ip
->i_e2fs_gid_high
= (gid
>> 16) & 0xffff;
525 ip
->i_e2fs_uid_high
= (uid
>> 16) & 0xffff;
527 ip
->i_e2fs_gid_high
= 0;
528 ip
->i_e2fs_uid_high
= 0;
530 if (ouid
!= uid
|| ogid
!= gid
) {
531 ext2fs_set_inode_guid(ip
);
532 ip
->i_flag
|= IN_CHANGE
;
534 if (ouid
!= uid
&& (ip
->i_e2fs_mode
& ISUID
) &&
535 kauth_authorize_vnode(cred
, KAUTH_VNODE_RETAIN_SUID
,
536 vp
, NULL
, EPERM
) != 0)
537 ip
->i_e2fs_mode
&= ~ISUID
;
538 if (ogid
!= gid
&& (ip
->i_e2fs_mode
& ISGID
) &&
539 kauth_authorize_vnode(cred
, KAUTH_VNODE_RETAIN_SGID
,
540 vp
, NULL
, EPERM
) != 0)
541 ip
->i_e2fs_mode
&= ~ISGID
;
546 ext2fs_remove(void *v
)
548 struct vop_remove_args
/* {
551 struct componentname *a_cnp;
554 struct vnode
*vp
= ap
->a_vp
;
555 struct vnode
*dvp
= ap
->a_dvp
;
556 struct ufs_lookup_results
*ulr
;
559 /* XXX should handle this material another way */
560 ulr
= &VTOI(dvp
)->i_crap
;
561 UFS_CHECK_CRAPCOUNTER(VTOI(dvp
));
564 if (vp
->v_type
== VDIR
||
565 (ip
->i_e2fs_flags
& (EXT2_IMMUTABLE
| EXT2_APPEND
)) ||
566 (VTOI(dvp
)->i_e2fs_flags
& EXT2_APPEND
)) {
569 error
= ext2fs_dirremove(dvp
, ulr
, ap
->a_cnp
);
572 ip
->i_flag
|= IN_CHANGE
;
576 VN_KNOTE(vp
, NOTE_DELETE
);
577 VN_KNOTE(dvp
, NOTE_WRITE
);
587 * ext2fs_link: create hard link.
592 struct vop_link_v2_args
/* {
595 struct componentname *a_cnp;
597 struct vnode
*dvp
= ap
->a_dvp
;
598 struct vnode
*vp
= ap
->a_vp
;
599 struct componentname
*cnp
= ap
->a_cnp
;
602 struct ufs_lookup_results
*ulr
;
605 KASSERT(vp
->v_type
!= VDIR
);
606 KASSERT(dvp
->v_mount
== vp
->v_mount
);
608 /* XXX should handle this material another way */
609 ulr
= &VTOI(dvp
)->i_crap
;
610 UFS_CHECK_CRAPCOUNTER(VTOI(dvp
));
612 error
= vn_lock(vp
, LK_EXCLUSIVE
);
614 VOP_ABORTOP(dvp
, cnp
);
618 if ((nlink_t
)ip
->i_e2fs_nlink
>= LINK_MAX
) {
619 VOP_ABORTOP(dvp
, cnp
);
623 if (ip
->i_e2fs_flags
& (EXT2_IMMUTABLE
| EXT2_APPEND
)) {
624 VOP_ABORTOP(dvp
, cnp
);
629 ip
->i_flag
|= IN_CHANGE
;
630 error
= ext2fs_update(vp
, NULL
, NULL
, UPDATE_WAIT
);
632 error
= ext2fs_direnter(ip
, dvp
, ulr
, cnp
);
635 ip
->i_flag
|= IN_CHANGE
;
640 VN_KNOTE(vp
, NOTE_LINK
);
641 VN_KNOTE(dvp
, NOTE_WRITE
);
649 ext2fs_mkdir(void *v
)
651 struct vop_mkdir_v3_args
/* {
653 struct vnode **a_vpp;
654 struct componentname *a_cnp;
657 struct vnode
*dvp
= ap
->a_dvp
;
658 struct vattr
*vap
= ap
->a_vap
;
659 struct componentname
*cnp
= ap
->a_cnp
;
660 struct inode
*ip
, *dp
= VTOI(dvp
);
662 struct ext2fs_dirtemplate dirtemplate
;
664 struct ufs_lookup_results
*ulr
;
666 /* XXX should handle this material another way */
667 ulr
= &VTOI(dvp
)->i_crap
;
668 UFS_CHECK_CRAPCOUNTER(VTOI(dvp
));
670 if ((nlink_t
)dp
->i_e2fs_nlink
>= LINK_MAX
) {
674 dmode
= vap
->va_mode
& ACCESSPERMS
;
677 * Must simulate part of ext2fs_makeinode here to acquire the inode,
678 * but not have it entered in the parent directory. The entry is
679 * made later after writing "." and ".." entries.
681 if ((error
= ext2fs_valloc(dvp
, dmode
, cnp
->cn_cred
, &tvp
)) != 0)
684 ip
->i_uid
= kauth_cred_geteuid(cnp
->cn_cred
);
685 ip
->i_e2fs_uid
= ip
->i_uid
& 0xffff;
686 ip
->i_e2fs_gid
= dp
->i_e2fs_gid
;
687 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
688 ip
->i_e2fs_uid_high
= (ip
->i_uid
>> 16) & 0xffff;
689 ip
->i_e2fs_gid_high
= dp
->i_e2fs_gid_high
;
691 ip
->i_e2fs_uid_high
= 0;
692 ip
->i_e2fs_gid_high
= 0;
694 ip
->i_gid
= ip
->i_e2fs_gid
| (ip
->i_e2fs_gid_high
<< 16);
695 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
696 ip
->i_e2fs_mode
= dmode
;
697 tvp
->v_type
= VDIR
; /* Rest init'd in getnewvnode(). */
698 ip
->i_e2fs_nlink
= 2;
701 * Bump link count in parent directory
702 * to reflect work done below. Should
703 * be done before reference is created
704 * so reparation is possible if we crash.
707 dp
->i_flag
|= IN_CHANGE
;
708 if ((error
= ext2fs_update(dvp
, NULL
, NULL
, UPDATE_DIROP
)) != 0)
711 /* Initialize directory with "." and ".." from static template. */
712 memset(&dirtemplate
, 0, sizeof(dirtemplate
));
713 dirtemplate
.dot_ino
= h2fs32(ip
->i_number
);
714 dirtemplate
.dot_reclen
= h2fs16(12);
715 dirtemplate
.dot_namlen
= 1;
716 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
717 (ip
->i_e2fs
->e2fs
.e2fs_features_incompat
& EXT2F_INCOMPAT_FTYPE
)) {
718 dirtemplate
.dot_type
= EXT2_FT_DIR
;
720 dirtemplate
.dot_name
[0] = '.';
721 dirtemplate
.dotdot_ino
= h2fs32(dp
->i_number
);
722 dirtemplate
.dotdot_reclen
= h2fs16(VTOI(dvp
)->i_e2fs
->e2fs_bsize
- 12);
723 dirtemplate
.dotdot_namlen
= 2;
724 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
725 (ip
->i_e2fs
->e2fs
.e2fs_features_incompat
& EXT2F_INCOMPAT_FTYPE
)) {
726 dirtemplate
.dotdot_type
= EXT2_FT_DIR
;
728 dirtemplate
.dotdot_name
[0] = dirtemplate
.dotdot_name
[1] = '.';
729 error
= ufs_bufio(UIO_WRITE
, tvp
, (void *)&dirtemplate
,
730 sizeof (dirtemplate
), (off_t
)0, IO_NODELOCKED
|IO_SYNC
,
731 cnp
->cn_cred
, (size_t *)0, NULL
);
734 dp
->i_flag
|= IN_CHANGE
;
737 if (VTOI(dvp
)->i_e2fs
->e2fs_bsize
> dvp
->v_mount
->mnt_stat
.f_bsize
)
738 panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */
740 error
= ext2fs_setsize(ip
, VTOI(dvp
)->i_e2fs
->e2fs_bsize
);
743 dp
->i_flag
|= IN_CHANGE
;
746 ip
->i_flag
|= IN_CHANGE
;
747 uvm_vnp_setsize(tvp
, ext2fs_size(ip
));
750 /* Directory set up, now install its entry in the parent directory. */
751 error
= ext2fs_direnter(ip
, dvp
, ulr
, cnp
);
754 dp
->i_flag
|= IN_CHANGE
;
758 * No need to do an explicit ext2fs_truncate here, vrele will do this
759 * for us because we set the link count to 0.
762 ip
->i_e2fs_nlink
= 0;
763 ip
->i_flag
|= IN_CHANGE
;
766 VN_KNOTE(dvp
, NOTE_WRITE
| NOTE_LINK
);
778 ext2fs_rmdir(void *v
)
780 struct vop_rmdir_args
/* {
783 struct componentname *a_cnp;
785 struct vnode
*vp
= ap
->a_vp
;
786 struct vnode
*dvp
= ap
->a_dvp
;
787 struct componentname
*cnp
= ap
->a_cnp
;
788 struct inode
*ip
, *dp
;
790 struct ufs_lookup_results
*ulr
;
795 /* XXX should handle this material another way */
797 UFS_CHECK_CRAPCOUNTER(dp
);
800 * No rmdir "." please.
808 * Verify the directory is empty (and valid).
809 * (Rmdir ".." won't be valid since
810 * ".." will contain a reference to
811 * the current directory and thus be
815 if (ip
->i_e2fs_nlink
!= 2 ||
816 !ext2fs_dirempty(ip
, dp
->i_number
, cnp
->cn_cred
)) {
820 if ((dp
->i_e2fs_flags
& EXT2_APPEND
) ||
821 (ip
->i_e2fs_flags
& (EXT2_IMMUTABLE
| EXT2_APPEND
))) {
826 * Delete reference to directory before purging
827 * inode. If we crash in between, the directory
828 * will be reattached to lost+found,
830 error
= ext2fs_dirremove(dvp
, ulr
, cnp
);
834 dp
->i_flag
|= IN_CHANGE
;
835 VN_KNOTE(dvp
, NOTE_WRITE
| NOTE_LINK
);
840 * Truncate inode. The only stuff left
841 * in the directory is "." and "..". The
842 * "." reference is inconsequential since
843 * we're quashing it. The ".." reference
844 * has already been adjusted above. We've
845 * removed the "." reference and the reference
846 * in the parent directory, but there may be
847 * other hard links so decrement by 2 and
848 * worry about them later.
850 ip
->i_e2fs_nlink
-= 2;
851 error
= ext2fs_truncate(vp
, (off_t
)0, IO_SYNC
, cnp
->cn_cred
);
852 cache_purge(ITOV(ip
));
854 VN_KNOTE(vp
, NOTE_DELETE
);
862 * symlink -- make a symbolic link
865 ext2fs_symlink(void *v
)
867 struct vop_symlink_v3_args
/* {
869 struct vnode **a_vpp;
870 struct componentname *a_cnp;
874 struct vnode
*vp
, **vpp
;
879 error
= ext2fs_makeinode(IFLNK
| ap
->a_vap
->va_mode
, ap
->a_dvp
,
883 VN_KNOTE(ap
->a_dvp
, NOTE_WRITE
);
885 len
= strlen(ap
->a_target
);
887 if (len
< ip
->i_ump
->um_maxsymlinklen
) {
888 memcpy((char *)ip
->i_din
.e2fs_din
->e2di_shortlink
, ap
->a_target
, len
);
889 error
= ext2fs_setsize(ip
, len
);
892 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
893 if (vp
->v_mount
->mnt_flag
& MNT_RELATIME
)
894 ip
->i_flag
|= IN_ACCESS
;
895 uvm_vnp_setsize(vp
, len
);
897 error
= ufs_bufio(UIO_WRITE
, vp
, ap
->a_target
, len
, (off_t
)0,
898 IO_NODELOCKED
, ap
->a_cnp
->cn_cred
, (size_t *)0, NULL
);
907 * Return target name of a symbolic link
910 ext2fs_readlink(void *v
)
912 struct vop_readlink_args
/* {
917 struct vnode
*vp
= ap
->a_vp
;
918 struct inode
*ip
= VTOI(vp
);
919 struct ufsmount
*ump
= ip
->i_ump
;
922 isize
= ext2fs_size(ip
);
923 if (isize
< ump
->um_maxsymlinklen
||
924 (ump
->um_maxsymlinklen
== 0 && ext2fs_nblock(ip
) == 0)) {
925 uiomove((char *)ip
->i_din
.e2fs_din
->e2di_shortlink
, isize
, ap
->a_uio
);
928 return (UFS_BUFRD(vp
, ap
->a_uio
, 0, ap
->a_cred
));
932 * Advisory record locking support
935 ext2fs_advlock(void *v
)
937 struct vop_advlock_args
/* {
944 struct inode
*ip
= VTOI(ap
->a_vp
);
946 return lf_advlock(ap
, &ip
->i_lockf
, ext2fs_size(ip
));
950 ext2fs_fsync(void *v
)
952 struct vop_fsync_args
/* {
960 struct vnode
*vp
= ap
->a_vp
;
964 wait
= (ap
->a_flags
& FSYNC_WAIT
) != 0;
966 if (vp
->v_type
== VBLK
)
967 error
= spec_fsync(v
);
969 error
= vflushbuf(vp
, ap
->a_flags
);
970 if (error
== 0 && (ap
->a_flags
& FSYNC_DATAONLY
) == 0)
971 error
= ext2fs_update(vp
, NULL
, NULL
, wait
? UPDATE_WAIT
: 0);
973 if (error
== 0 && ap
->a_flags
& FSYNC_CACHE
) {
975 error
= VOP_IOCTL(VTOI(vp
)->i_devvp
, DIOCCACHESYNC
, &l
, FWRITE
,
983 * Initialize the vnode associated with a new inode, handle aliased
987 ext2fs_vinit(struct mount
*mntp
, int (**specops
)(void *),
988 int (**fifoops
)(void *), struct vnode
**vpp
)
996 switch(vp
->v_type
= IFTOVT(ip
->i_e2fs_mode
)) {
1000 spec_node_init(vp
, fs2h32(ip
->i_din
.e2fs_din
->e2di_rdev
));
1013 if (ip
->i_number
== UFS_ROOTINO
)
1014 vp
->v_vflag
|= VV_ROOT
;
1016 * Initialize modrev times
1018 getmicrouptime(&tv
);
1019 SETHIGH(ip
->i_modrev
, tv
.tv_sec
);
1020 SETLOW(ip
->i_modrev
, tv
.tv_usec
* 4294);
1026 * Allocate a new inode.
1029 ext2fs_makeinode(int mode
, struct vnode
*dvp
, struct vnode
**vpp
,
1030 struct componentname
*cnp
)
1032 struct inode
*ip
, *pdir
;
1035 struct ufs_lookup_results
*ulr
;
1039 /* XXX should handle this material another way */
1040 ulr
= &pdir
->i_crap
;
1041 UFS_CHECK_CRAPCOUNTER(pdir
);
1044 if ((mode
& IFMT
) == 0)
1047 if ((error
= ext2fs_valloc(dvp
, mode
, cnp
->cn_cred
, &tvp
)) != 0) {
1051 ip
->i_uid
= kauth_cred_geteuid(cnp
->cn_cred
);
1052 ip
->i_e2fs_uid
= ip
->i_uid
& 0xffff;
1053 ip
->i_e2fs_gid
= pdir
->i_e2fs_gid
;
1054 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
1055 ip
->i_e2fs_uid_high
= (ip
->i_uid
>> 16) & 0xffff;
1056 ip
->i_e2fs_gid_high
= pdir
->i_e2fs_gid_high
;
1058 ip
->i_e2fs_uid_high
= 0;
1059 ip
->i_e2fs_gid_high
= 0;
1061 ip
->i_gid
= ip
->i_e2fs_gid
| (ip
->i_e2fs_gid_high
<< 16);
1062 ip
->i_flag
|= IN_ACCESS
| IN_CHANGE
| IN_UPDATE
;
1063 ip
->i_e2fs_mode
= mode
;
1064 tvp
->v_type
= IFTOVT(mode
); /* Rest init'd in getnewvnode(). */
1065 ip
->i_e2fs_nlink
= 1;
1067 /* Authorize setting SGID if needed. */
1068 if (ip
->i_e2fs_mode
& ISGID
) {
1069 error
= kauth_authorize_vnode(cnp
->cn_cred
, KAUTH_VNODE_WRITE_SECURITY
,
1070 tvp
, NULL
, genfs_can_chmod(tvp
->v_type
, cnp
->cn_cred
, ip
->i_uid
,
1073 ip
->i_e2fs_mode
&= ~ISGID
;
1077 * Make sure inode goes to disk before directory entry.
1079 if ((error
= ext2fs_update(tvp
, NULL
, NULL
, UPDATE_WAIT
)) != 0)
1081 error
= ext2fs_direnter(ip
, dvp
, ulr
, cnp
);
1089 * Write error occurred trying to update the inode
1090 * or the directory so must deallocate the inode.
1092 tvp
->v_type
= VNON
; /* Stop explosion if VBLK */
1093 ip
->i_e2fs_nlink
= 0;
1094 ip
->i_flag
|= IN_CHANGE
;
1100 * Reclaim an inode so that it can be used for other purposes.
1103 ext2fs_reclaim(void *v
)
1105 struct vop_reclaim_args
/* {
1108 struct vnode
*vp
= ap
->a_vp
;
1109 struct inode
*ip
= VTOI(vp
);
1113 * The inode must be freed and updated before being removed
1114 * from its hash chain. Other threads trying to gain a hold
1115 * or lock on the inode will be stalled.
1117 if (ip
->i_omode
== 1 && (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1118 ext2fs_vfree(vp
, ip
->i_number
, ip
->i_e2fs_mode
);
1119 if ((error
= ufs_reclaim(vp
)) != 0)
1121 if (ip
->i_din
.e2fs_din
!= NULL
)
1122 pool_put(&ext2fs_dinode_pool
, ip
->i_din
.e2fs_din
);
1123 genfs_node_destroy(vp
);
1124 pool_put(&ext2fs_inode_pool
, vp
->v_data
);
1129 /* Global vfs data structures for ext2fs. */
1130 int (**ext2fs_vnodeop_p
)(void *);
1131 const struct vnodeopv_entry_desc ext2fs_vnodeop_entries
[] = {
1132 { &vop_default_desc
, vn_default_error
},
1133 { &vop_lookup_desc
, ext2fs_lookup
}, /* lookup */
1134 { &vop_create_desc
, ext2fs_create
}, /* create */
1135 { &vop_mknod_desc
, ext2fs_mknod
}, /* mknod */
1136 { &vop_open_desc
, ext2fs_open
}, /* open */
1137 { &vop_close_desc
, ufs_close
}, /* close */
1138 { &vop_access_desc
, ext2fs_access
}, /* access */
1139 { &vop_getattr_desc
, ext2fs_getattr
}, /* getattr */
1140 { &vop_setattr_desc
, ext2fs_setattr
}, /* setattr */
1141 { &vop_read_desc
, ext2fs_read
}, /* read */
1142 { &vop_write_desc
, ext2fs_write
}, /* write */
1143 { &vop_fallocate_desc
, genfs_eopnotsupp
}, /* fallocate */
1144 { &vop_fdiscard_desc
, genfs_eopnotsupp
}, /* fdiscard */
1145 { &vop_ioctl_desc
, ufs_ioctl
}, /* ioctl */
1146 { &vop_fcntl_desc
, ufs_fcntl
}, /* fcntl */
1147 { &vop_poll_desc
, ufs_poll
}, /* poll */
1148 { &vop_kqfilter_desc
, genfs_kqfilter
}, /* kqfilter */
1149 { &vop_revoke_desc
, ufs_revoke
}, /* revoke */
1150 { &vop_mmap_desc
, ufs_mmap
}, /* mmap */
1151 { &vop_fsync_desc
, ext2fs_fsync
}, /* fsync */
1152 { &vop_seek_desc
, ufs_seek
}, /* seek */
1153 { &vop_remove_desc
, ext2fs_remove
}, /* remove */
1154 { &vop_link_desc
, ext2fs_link
}, /* link */
1155 { &vop_rename_desc
, ext2fs_rename
}, /* rename */
1156 { &vop_mkdir_desc
, ext2fs_mkdir
}, /* mkdir */
1157 { &vop_rmdir_desc
, ext2fs_rmdir
}, /* rmdir */
1158 { &vop_symlink_desc
, ext2fs_symlink
}, /* symlink */
1159 { &vop_readdir_desc
, ext2fs_readdir
}, /* readdir */
1160 { &vop_readlink_desc
, ext2fs_readlink
}, /* readlink */
1161 { &vop_abortop_desc
, ufs_abortop
}, /* abortop */
1162 { &vop_inactive_desc
, ext2fs_inactive
}, /* inactive */
1163 { &vop_reclaim_desc
, ext2fs_reclaim
}, /* reclaim */
1164 { &vop_lock_desc
, ufs_lock
}, /* lock */
1165 { &vop_unlock_desc
, ufs_unlock
}, /* unlock */
1166 { &vop_bmap_desc
, ext2fs_bmap
}, /* bmap */
1167 { &vop_strategy_desc
, ufs_strategy
}, /* strategy */
1168 { &vop_print_desc
, ufs_print
}, /* print */
1169 { &vop_islocked_desc
, ufs_islocked
}, /* islocked */
1170 { &vop_pathconf_desc
, ufs_pathconf
}, /* pathconf */
1171 { &vop_advlock_desc
, ext2fs_advlock
}, /* advlock */
1172 { &vop_bwrite_desc
, vn_bwrite
}, /* bwrite */
1173 { &vop_getpages_desc
, genfs_getpages
}, /* getpages */
1174 { &vop_putpages_desc
, genfs_putpages
}, /* putpages */
1177 const struct vnodeopv_desc ext2fs_vnodeop_opv_desc
=
1178 { &ext2fs_vnodeop_p
, ext2fs_vnodeop_entries
};
1180 int (**ext2fs_specop_p
)(void *);
1181 const struct vnodeopv_entry_desc ext2fs_specop_entries
[] = {
1182 { &vop_default_desc
, vn_default_error
},
1183 { &vop_lookup_desc
, spec_lookup
}, /* lookup */
1184 { &vop_create_desc
, spec_create
}, /* create */
1185 { &vop_mknod_desc
, spec_mknod
}, /* mknod */
1186 { &vop_open_desc
, spec_open
}, /* open */
1187 { &vop_close_desc
, ufsspec_close
}, /* close */
1188 { &vop_access_desc
, ext2fs_access
}, /* access */
1189 { &vop_getattr_desc
, ext2fs_getattr
}, /* getattr */
1190 { &vop_setattr_desc
, ext2fs_setattr
}, /* setattr */
1191 { &vop_read_desc
, ufsspec_read
}, /* read */
1192 { &vop_write_desc
, ufsspec_write
}, /* write */
1193 { &vop_fallocate_desc
, spec_fallocate
}, /* fallocate */
1194 { &vop_fdiscard_desc
, spec_fdiscard
}, /* fdiscard */
1195 { &vop_ioctl_desc
, spec_ioctl
}, /* ioctl */
1196 { &vop_fcntl_desc
, ufs_fcntl
}, /* fcntl */
1197 { &vop_poll_desc
, spec_poll
}, /* poll */
1198 { &vop_kqfilter_desc
, spec_kqfilter
}, /* kqfilter */
1199 { &vop_revoke_desc
, spec_revoke
}, /* revoke */
1200 { &vop_mmap_desc
, spec_mmap
}, /* mmap */
1201 { &vop_fsync_desc
, ext2fs_fsync
}, /* fsync */
1202 { &vop_seek_desc
, spec_seek
}, /* seek */
1203 { &vop_remove_desc
, spec_remove
}, /* remove */
1204 { &vop_link_desc
, spec_link
}, /* link */
1205 { &vop_rename_desc
, spec_rename
}, /* rename */
1206 { &vop_mkdir_desc
, spec_mkdir
}, /* mkdir */
1207 { &vop_rmdir_desc
, spec_rmdir
}, /* rmdir */
1208 { &vop_symlink_desc
, spec_symlink
}, /* symlink */
1209 { &vop_readdir_desc
, spec_readdir
}, /* readdir */
1210 { &vop_readlink_desc
, spec_readlink
}, /* readlink */
1211 { &vop_abortop_desc
, spec_abortop
}, /* abortop */
1212 { &vop_inactive_desc
, ext2fs_inactive
}, /* inactive */
1213 { &vop_reclaim_desc
, ext2fs_reclaim
}, /* reclaim */
1214 { &vop_lock_desc
, ufs_lock
}, /* lock */
1215 { &vop_unlock_desc
, ufs_unlock
}, /* unlock */
1216 { &vop_bmap_desc
, spec_bmap
}, /* bmap */
1217 { &vop_strategy_desc
, spec_strategy
}, /* strategy */
1218 { &vop_print_desc
, ufs_print
}, /* print */
1219 { &vop_islocked_desc
, ufs_islocked
}, /* islocked */
1220 { &vop_pathconf_desc
, spec_pathconf
}, /* pathconf */
1221 { &vop_advlock_desc
, spec_advlock
}, /* advlock */
1222 { &vop_bwrite_desc
, vn_bwrite
}, /* bwrite */
1223 { &vop_getpages_desc
, spec_getpages
}, /* getpages */
1224 { &vop_putpages_desc
, spec_putpages
}, /* putpages */
1227 const struct vnodeopv_desc ext2fs_specop_opv_desc
=
1228 { &ext2fs_specop_p
, ext2fs_specop_entries
};
1230 int (**ext2fs_fifoop_p
)(void *);
1231 const struct vnodeopv_entry_desc ext2fs_fifoop_entries
[] = {
1232 { &vop_default_desc
, vn_default_error
},
1233 { &vop_lookup_desc
, vn_fifo_bypass
}, /* lookup */
1234 { &vop_create_desc
, vn_fifo_bypass
}, /* create */
1235 { &vop_mknod_desc
, vn_fifo_bypass
}, /* mknod */
1236 { &vop_open_desc
, vn_fifo_bypass
}, /* open */
1237 { &vop_close_desc
, ufsfifo_close
}, /* close */
1238 { &vop_access_desc
, ext2fs_access
}, /* access */
1239 { &vop_getattr_desc
, ext2fs_getattr
}, /* getattr */
1240 { &vop_setattr_desc
, ext2fs_setattr
}, /* setattr */
1241 { &vop_read_desc
, ufsfifo_read
}, /* read */
1242 { &vop_write_desc
, ufsfifo_write
}, /* write */
1243 { &vop_fallocate_desc
, vn_fifo_bypass
}, /* fallocate */
1244 { &vop_fdiscard_desc
, vn_fifo_bypass
}, /* fdiscard */
1245 { &vop_ioctl_desc
, vn_fifo_bypass
}, /* ioctl */
1246 { &vop_fcntl_desc
, ufs_fcntl
}, /* fcntl */
1247 { &vop_poll_desc
, vn_fifo_bypass
}, /* poll */
1248 { &vop_kqfilter_desc
, vn_fifo_bypass
}, /* kqfilter */
1249 { &vop_revoke_desc
, vn_fifo_bypass
}, /* revoke */
1250 { &vop_mmap_desc
, vn_fifo_bypass
}, /* mmap */
1251 { &vop_fsync_desc
, ext2fs_fsync
}, /* fsync */
1252 { &vop_seek_desc
, vn_fifo_bypass
}, /* seek */
1253 { &vop_remove_desc
, vn_fifo_bypass
}, /* remove */
1254 { &vop_link_desc
, vn_fifo_bypass
}, /* link */
1255 { &vop_rename_desc
, vn_fifo_bypass
}, /* rename */
1256 { &vop_mkdir_desc
, vn_fifo_bypass
}, /* mkdir */
1257 { &vop_rmdir_desc
, vn_fifo_bypass
}, /* rmdir */
1258 { &vop_symlink_desc
, vn_fifo_bypass
}, /* symlink */
1259 { &vop_readdir_desc
, vn_fifo_bypass
}, /* readdir */
1260 { &vop_readlink_desc
, vn_fifo_bypass
}, /* readlink */
1261 { &vop_abortop_desc
, vn_fifo_bypass
}, /* abortop */
1262 { &vop_inactive_desc
, ext2fs_inactive
}, /* inactive */
1263 { &vop_reclaim_desc
, ext2fs_reclaim
}, /* reclaim */
1264 { &vop_lock_desc
, ufs_lock
}, /* lock */
1265 { &vop_unlock_desc
, ufs_unlock
}, /* unlock */
1266 { &vop_bmap_desc
, vn_fifo_bypass
}, /* bmap */
1267 { &vop_strategy_desc
, vn_fifo_bypass
}, /* strategy */
1268 { &vop_print_desc
, ufs_print
}, /* print */
1269 { &vop_islocked_desc
, ufs_islocked
}, /* islocked */
1270 { &vop_pathconf_desc
, vn_fifo_bypass
}, /* pathconf */
1271 { &vop_advlock_desc
, vn_fifo_bypass
}, /* advlock */
1272 { &vop_bwrite_desc
, vn_bwrite
}, /* bwrite */
1273 { &vop_putpages_desc
, vn_fifo_bypass
}, /* putpages */
1276 const struct vnodeopv_desc ext2fs_fifoop_opv_desc
=
1277 { &ext2fs_fifoop_p
, ext2fs_fifoop_entries
};